Merge "Export memalign to linker"
diff --git a/libm/Android.bp b/libm/Android.bp
index 52229b6..3fe8620 100644
--- a/libm/Android.bp
+++ b/libm/Android.bp
@@ -489,6 +489,7 @@
         "-D_BSD_SOURCE",
         "-DFLT_EVAL_METHOD=0",
         "-include freebsd-compat.h",
+        "-include fenv-access.h",
         "-fno-math-errno",
         "-Wall",
         "-Werror",
diff --git a/libm/NOTICE b/libm/NOTICE
index 62d253b..0c37b2b 100644
--- a/libm/NOTICE
+++ b/libm/NOTICE
@@ -281,6 +281,34 @@
 
 -------------------------------------------------------------------
 
+Copyright (C) 2021 The Android Open Source Project
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+ * Redistributions of source code must retain the above copyright
+   notice, this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright
+   notice, this list of conditions and the following disclaimer in
+   the documentation and/or other materials provided with the
+   distribution.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+SUCH DAMAGE.
+
+-------------------------------------------------------------------
+
 Copyright (c) 1985, 1993
    The Regents of the University of California.  All rights reserved.
 
diff --git a/libm/fenv-access.h b/libm/fenv-access.h
new file mode 100644
index 0000000..7acb34d
--- /dev/null
+++ b/libm/fenv-access.h
@@ -0,0 +1,33 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *  * Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#pragma once
+
+#if defined(__x86_64__) || defined(__i386__)
+#pragma STDC FENV_ACCESS ON
+#endif
diff --git a/linker/linker_main.cpp b/linker/linker_main.cpp
index 2a690e9..513a903 100644
--- a/linker/linker_main.cpp
+++ b/linker/linker_main.cpp
@@ -39,6 +39,7 @@
 #include "linker_globals.h"
 #include "linker_phdr.h"
 #include "linker_relocate.h"
+#include "linker_relocs.h"
 #include "linker_tls.h"
 #include "linker_utils.h"
 
@@ -596,25 +597,66 @@
   }
 }
 
-// TODO: There is a similar ifunc resolver calling loop in libc_init_static.cpp, but that version
-// uses weak symbols, which don't work in the linker prior to its relocation. This version also
-// supports a load bias. When we stop supporting the gold linker in the NDK, then maybe we can use
-// non-weak definitions and merge the two loops.
 #if defined(USE_RELA)
-extern __LIBC_HIDDEN__ ElfW(Rela) __rela_iplt_start[], __rela_iplt_end[];
+using RelType = ElfW(Rela);
+#else
+using RelType = ElfW(Rel);
+#endif
 
+extern __LIBC_HIDDEN__ ElfW(Ehdr) __ehdr_start;
+
+static void find_irelative_relocs(RelType **begin, RelType **end) {
+  // Find the IRELATIVE relocations using the DT_JMPREL and DT_PLTRELSZ dynamic
+  // tags. In theory this could include more than just IRELATIVE relocations,
+  // but in practice the linker doesn't dynamically link against any other
+  // binaries, so we shouldn't expect to see anything else.
+  auto* ehdr = reinterpret_cast<char*>(&__ehdr_start);
+  auto* phdr = reinterpret_cast<ElfW(Phdr)*>(ehdr + __ehdr_start.e_phoff);
+  for (size_t i = 0; i != __ehdr_start.e_phnum; ++i) {
+    if (phdr[i].p_type != PT_DYNAMIC) {
+      continue;
+    }
+    auto *dyn = reinterpret_cast<ElfW(Dyn)*>(ehdr + phdr[i].p_vaddr);
+    ElfW(Addr) jmprel = 0, pltrelsz = 0;
+    for (size_t j = 0, size = phdr[i].p_filesz / sizeof(ElfW(Dyn)); j != size; ++j) {
+      if (dyn[j].d_tag == DT_JMPREL) {
+        jmprel = dyn[j].d_un.d_ptr;
+      } else if (dyn[j].d_tag == DT_PLTRELSZ) {
+        pltrelsz = dyn[j].d_un.d_ptr;
+      }
+    }
+    if (jmprel && pltrelsz) {
+      *begin = reinterpret_cast<RelType*>(ehdr + jmprel);
+      *end = reinterpret_cast<RelType*>(ehdr + jmprel + pltrelsz);
+    } else {
+      *begin = *end = nullptr;
+    }
+  }
+}
+
+#if defined(USE_RELA)
 static void call_ifunc_resolvers(ElfW(Addr) load_bias) {
-  for (ElfW(Rela) *r = __rela_iplt_start; r != __rela_iplt_end; ++r) {
+  ElfW(Rela)* begin;
+  ElfW(Rela)* end;
+  find_irelative_relocs(&begin, &end);
+  for (ElfW(Rela) *r = begin; r != end; ++r) {
+    if (ELFW(R_TYPE)(r->r_info) != R_GENERIC_IRELATIVE) {
+      continue;
+    }
     ElfW(Addr)* offset = reinterpret_cast<ElfW(Addr)*>(r->r_offset + load_bias);
     ElfW(Addr) resolver = r->r_addend + load_bias;
     *offset = __bionic_call_ifunc_resolver(resolver);
   }
 }
 #else
-extern __LIBC_HIDDEN__ ElfW(Rel) __rel_iplt_start[], __rel_iplt_end[];
-
 static void call_ifunc_resolvers(ElfW(Addr) load_bias) {
-  for (ElfW(Rel) *r = __rel_iplt_start; r != __rel_iplt_end; ++r) {
+  ElfW(Rel)* begin;
+  ElfW(Rel)* end;
+  find_irelative_relocs(&begin, &end);
+  for (ElfW(Rel) *r = begin; r != end; ++r) {
+    if (ELFW(R_TYPE)(r->r_info) != R_GENERIC_IRELATIVE) {
+      continue;
+    }
     ElfW(Addr)* offset = reinterpret_cast<ElfW(Addr)*>(r->r_offset + load_bias);
     ElfW(Addr) resolver = *offset + load_bias;
     *offset = __bionic_call_ifunc_resolver(resolver);