Merge "Refactoring: C++11 style DISABLE_ bionic marcos"
diff --git a/libc/Android.mk b/libc/Android.mk
index e87c07c..a81b6c2 100644
--- a/libc/Android.mk
+++ b/libc/Android.mk
@@ -146,6 +146,7 @@
     bionic/pause.cpp \
     bionic/pipe.cpp \
     bionic/poll.cpp \
+    bionic/posix_fadvise.cpp \
     bionic/posix_fallocate.cpp \
     bionic/posix_timers.cpp \
     bionic/pthread_atfork.cpp \
@@ -157,6 +158,7 @@
     bionic/pthread_exit.cpp \
     bionic/pthread_getcpuclockid.cpp \
     bionic/pthread_getschedparam.cpp \
+    bionic/pthread_gettid_np.cpp \
     bionic/pthread_internals.cpp \
     bionic/pthread_join.cpp \
     bionic/pthread_key.cpp \
diff --git a/libc/SYSCALLS.TXT b/libc/SYSCALLS.TXT
index bfa13b7..47b7ef8 100644
--- a/libc/SYSCALLS.TXT
+++ b/libc/SYSCALLS.TXT
@@ -167,6 +167,12 @@
 int fallocate64:fallocate(int, int, off64_t, off64_t) arm,mips,x86
 int fallocate|fallocate64(int, int, off_t, off_t) arm64,mips64,x86_64
 
+# posix_fadvise64 is awkward: arm has shuffled arguments,
+# the POSIX functions don't set errno, and no architecture has posix_fadvise.
+int __arm_fadvise64_64:arm_fadvise64_64(int, int, off64_t, off64_t) arm
+int __fadvise64:fadvise64_64(int, off64_t, off64_t, int) x86
+int __fadvise64:fadvise64(int, off64_t, off64_t, int) arm64,mips,mips64,x86_64
+
 int __fstatfs64:fstatfs64(int, size_t, struct statfs*)  arm,mips,x86
 int fstatfs64|fstatfs:fstatfs(int, struct statfs*)  arm64,mips64,x86_64
 int __statfs64:statfs64(const char*, size_t, struct statfs*)  arm,mips,x86
diff --git a/libc/arch-arm/syscalls/__arm_fadvise64_64.S b/libc/arch-arm/syscalls/__arm_fadvise64_64.S
new file mode 100644
index 0000000..761c4d6
--- /dev/null
+++ b/libc/arch-arm/syscalls/__arm_fadvise64_64.S
@@ -0,0 +1,22 @@
+/* Generated by gensyscalls.py. Do not edit. */
+
+#include <private/bionic_asm.h>
+
+ENTRY(__arm_fadvise64_64)
+    mov     ip, sp
+    stmfd   sp!, {r4, r5, r6, r7}
+    .cfi_def_cfa_offset 16
+    .cfi_rel_offset r4, 0
+    .cfi_rel_offset r5, 4
+    .cfi_rel_offset r6, 8
+    .cfi_rel_offset r7, 12
+    ldmfd   ip, {r4, r5, r6}
+    ldr     r7, =__NR_arm_fadvise64_64
+    swi     #0
+    ldmfd   sp!, {r4, r5, r6, r7}
+    .cfi_def_cfa_offset 0
+    cmn     r0, #(MAX_ERRNO + 1)
+    bxls    lr
+    neg     r0, r0
+    b       __set_errno_internal
+END(__arm_fadvise64_64)
diff --git a/libc/arch-arm64/include/machine/elf_machdep.h b/libc/arch-arm64/include/machine/elf_machdep.h
index 2bf8189..6eab313 100644
--- a/libc/arch-arm64/include/machine/elf_machdep.h
+++ b/libc/arch-arm64/include/machine/elf_machdep.h
@@ -99,6 +99,7 @@
 #define R_AARCH64_RELATIVE              1027    /* Adjust by program base.  */
 #define R_AARCH64_TLS_TPREL64           1030
 #define R_AARCH64_TLS_DTPREL32          1031
+#define R_AARCH64_IRELATIVE             1032
 
 #define R_TYPE(name)        __CONCAT(R_AARCH64_,name)
 
diff --git a/libc/arch-arm64/syscalls/__fadvise64.S b/libc/arch-arm64/syscalls/__fadvise64.S
new file mode 100644
index 0000000..695d094
--- /dev/null
+++ b/libc/arch-arm64/syscalls/__fadvise64.S
@@ -0,0 +1,15 @@
+/* Generated by gensyscalls.py. Do not edit. */
+
+#include <private/bionic_asm.h>
+
+ENTRY(__fadvise64)
+    mov     x8, __NR_fadvise64
+    svc     #0
+
+    cmn     x0, #(MAX_ERRNO + 1)
+    cneg    x0, x0, hi
+    b.hi    __set_errno_internal
+
+    ret
+END(__fadvise64)
+.hidden __fadvise64
diff --git a/libc/arch-mips/syscalls/__fadvise64.S b/libc/arch-mips/syscalls/__fadvise64.S
new file mode 100644
index 0000000..abacbb2
--- /dev/null
+++ b/libc/arch-mips/syscalls/__fadvise64.S
@@ -0,0 +1,19 @@
+/* Generated by gensyscalls.py. Do not edit. */
+
+#include <private/bionic_asm.h>
+
+ENTRY(__fadvise64)
+    .set noreorder
+    .cpload t9
+    li v0, __NR_fadvise64
+    syscall
+    bnez a3, 1f
+    move a0, v0
+    j ra
+    nop
+1:
+    la t9,__set_errno_internal
+    j t9
+    nop
+    .set reorder
+END(__fadvise64)
diff --git a/libc/arch-mips64/syscalls/__fadvise64.S b/libc/arch-mips64/syscalls/__fadvise64.S
new file mode 100644
index 0000000..90aceb0
--- /dev/null
+++ b/libc/arch-mips64/syscalls/__fadvise64.S
@@ -0,0 +1,26 @@
+/* Generated by gensyscalls.py. Do not edit. */
+
+#include <private/bionic_asm.h>
+
+ENTRY(__fadvise64)
+    .set push
+    .set noreorder
+    li v0, __NR_fadvise64
+    syscall
+    bnez a3, 1f
+    move a0, v0
+    j ra
+    nop
+1:
+    move t0, ra
+    bal     2f
+    nop
+2:
+    .cpsetup ra, t1, 2b
+    LA t9,__set_errno_internal
+    .cpreturn
+    j t9
+    move ra, t0
+    .set pop
+END(__fadvise64)
+.hidden __fadvise64
diff --git a/libc/arch-x86/include/machine/elf_machdep.h b/libc/arch-x86/include/machine/elf_machdep.h
index 442c561..4bce933 100644
--- a/libc/arch-x86/include/machine/elf_machdep.h
+++ b/libc/arch-x86/include/machine/elf_machdep.h
@@ -59,5 +59,6 @@
 #define	R_386_TLS_GOTDESC	39
 #define	R_386_TLS_DESC_CALL	40
 #define	R_386_TLS_DESC		41
+#define	R_386_IRELATIVE		42
 
 #define	R_TYPE(name)	__CONCAT(R_386_,name)
diff --git a/libc/arch-x86/syscalls/__fadvise64.S b/libc/arch-x86/syscalls/__fadvise64.S
new file mode 100644
index 0000000..6e4298a
--- /dev/null
+++ b/libc/arch-x86/syscalls/__fadvise64.S
@@ -0,0 +1,46 @@
+/* Generated by gensyscalls.py. Do not edit. */
+
+#include <private/bionic_asm.h>
+
+ENTRY(__fadvise64)
+    pushl   %ebx
+    .cfi_def_cfa_offset 8
+    .cfi_rel_offset ebx, 0
+    pushl   %ecx
+    .cfi_adjust_cfa_offset 4
+    .cfi_rel_offset ecx, 0
+    pushl   %edx
+    .cfi_adjust_cfa_offset 4
+    .cfi_rel_offset edx, 0
+    pushl   %esi
+    .cfi_adjust_cfa_offset 4
+    .cfi_rel_offset esi, 0
+    pushl   %edi
+    .cfi_adjust_cfa_offset 4
+    .cfi_rel_offset edi, 0
+    pushl   %ebp
+    .cfi_adjust_cfa_offset 4
+    .cfi_rel_offset ebp, 0
+    mov     28(%esp), %ebx
+    mov     32(%esp), %ecx
+    mov     36(%esp), %edx
+    mov     40(%esp), %esi
+    mov     44(%esp), %edi
+    mov     48(%esp), %ebp
+    movl    $__NR_fadvise64_64, %eax
+    int     $0x80
+    cmpl    $-MAX_ERRNO, %eax
+    jb      1f
+    negl    %eax
+    pushl   %eax
+    call    __set_errno_internal
+    addl    $4, %esp
+1:
+    popl    %ebp
+    popl    %edi
+    popl    %esi
+    popl    %edx
+    popl    %ecx
+    popl    %ebx
+    ret
+END(__fadvise64)
diff --git a/libc/arch-x86_64/include/machine/elf_machdep.h b/libc/arch-x86_64/include/machine/elf_machdep.h
index 20f8c6d..bf1f273 100644
--- a/libc/arch-x86_64/include/machine/elf_machdep.h
+++ b/libc/arch-x86_64/include/machine/elf_machdep.h
@@ -46,6 +46,8 @@
 #define R_X86_64_GOTTPOFF	22
 #define R_X86_64_TPOFF32	23
 
+#define R_X86_64_IRELATIVE	37
+
 #define	R_TYPE(name)	__CONCAT(R_X86_64_,name)
 
 #else	/*	!__i386__	*/
diff --git a/libc/arch-x86_64/syscalls/__fadvise64.S b/libc/arch-x86_64/syscalls/__fadvise64.S
new file mode 100644
index 0000000..8810d88
--- /dev/null
+++ b/libc/arch-x86_64/syscalls/__fadvise64.S
@@ -0,0 +1,17 @@
+/* Generated by gensyscalls.py. Do not edit. */
+
+#include <private/bionic_asm.h>
+
+ENTRY(__fadvise64)
+    movq    %rcx, %r10
+    movl    $__NR_fadvise64, %eax
+    syscall
+    cmpq    $-MAX_ERRNO, %rax
+    jb      1f
+    negl    %eax
+    movl    %eax, %edi
+    call    __set_errno_internal
+1:
+    ret
+END(__fadvise64)
+.hidden __fadvise64
diff --git a/libc/bionic/posix_fadvise.cpp b/libc/bionic/posix_fadvise.cpp
new file mode 100644
index 0000000..3a74af3
--- /dev/null
+++ b/libc/bionic/posix_fadvise.cpp
@@ -0,0 +1,51 @@
+/*
+ * Copyright (C) 2014 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.
+ */
+
+#include <fcntl.h>
+
+#include "private/ErrnoRestorer.h"
+
+extern "C" int __arm_fadvise64_64(int, int, off64_t, off64_t);
+extern "C" int __fadvise64(int, off64_t, off64_t, int);
+
+// No architecture actually has the 32-bit off_t system call.
+int posix_fadvise(int fd, off_t offset, off_t length, int advice) {
+  return posix_fadvise64(fd, offset, length, advice);
+}
+
+#if defined(__arm__)
+int posix_fadvise64(int fd, off64_t offset, off64_t length, int advice) {
+  ErrnoRestorer errno_restorer;
+  return (__arm_fadvise64_64(fd, advice, offset, length) == 0) ? 0 : errno;
+}
+#else
+int posix_fadvise64(int fd, off64_t offset, off64_t length, int advice) {
+  ErrnoRestorer errno_restorer;
+  return (__fadvise64(fd, offset, length, advice) == 0) ? 0 : errno;
+}
+#endif
diff --git a/libc/bionic/posix_timers.cpp b/libc/bionic/posix_timers.cpp
index 312bf9e..7ad0ef1 100644
--- a/libc/bionic/posix_timers.cpp
+++ b/libc/bionic/posix_timers.cpp
@@ -28,7 +28,6 @@
 
 #include "pthread_internal.h"
 #include "private/bionic_futex.h"
-#include "private/bionic_pthread.h"
 #include "private/kernel_sigset_t.h"
 
 #include <errno.h>
@@ -159,7 +158,7 @@
   sigevent se = *evp;
   se.sigev_signo = TIMER_SIGNAL;
   se.sigev_notify = SIGEV_THREAD_ID;
-  se.sigev_notify_thread_id = __pthread_gettid(timer->callback_thread);
+  se.sigev_notify_thread_id = pthread_gettid_np(timer->callback_thread);
   if (__timer_create(clock_id, &se, &timer->kernel_timer_id) == -1) {
     __timer_thread_stop(timer);
     return -1;
diff --git a/libc/bionic/pthread_gettid_np.cpp b/libc/bionic/pthread_gettid_np.cpp
new file mode 100644
index 0000000..f4663a7
--- /dev/null
+++ b/libc/bionic/pthread_gettid_np.cpp
@@ -0,0 +1,39 @@
+/*
+ * Copyright (C) 2014 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.
+ */
+
+#include "pthread_internal.h"
+#include "private/bionic_pthread.h"
+
+pid_t pthread_gettid_np(pthread_t t) {
+  return reinterpret_cast<pthread_internal_t*>(t)->tid;
+}
+
+// TODO: move callers over to pthread_gettid_np and remove this.
+pid_t __pthread_gettid(pthread_t t) {
+  return pthread_gettid_np(t);
+}
diff --git a/libc/bionic/pthread_internal.h b/libc/bionic/pthread_internal.h
index 7bcd758..a5b3002 100644
--- a/libc/bionic/pthread_internal.h
+++ b/libc/bionic/pthread_internal.h
@@ -98,7 +98,9 @@
 __LIBC_HIDDEN__ void __init_tls(pthread_internal_t* thread);
 __LIBC_HIDDEN__ void __init_alternate_signal_stack(pthread_internal_t*);
 __LIBC_HIDDEN__ void _pthread_internal_add(pthread_internal_t* thread);
-__LIBC_HIDDEN__ pthread_internal_t* __get_thread(void);
+
+/* Various third-party apps contain a backport of our pthread_rwlock implementation that uses this. */
+extern "C" __LIBC64_HIDDEN__ pthread_internal_t* __get_thread(void);
 
 __LIBC_HIDDEN__ void pthread_key_clean_all(void);
 __LIBC_HIDDEN__ void _pthread_internal_remove_locked(pthread_internal_t* thread);
diff --git a/libc/bionic/pthread_internals.cpp b/libc/bionic/pthread_internals.cpp
index 8c5d9c7..4c08ba8 100644
--- a/libc/bionic/pthread_internals.cpp
+++ b/libc/bionic/pthread_internals.cpp
@@ -29,7 +29,6 @@
 #include "pthread_internal.h"
 
 #include "private/bionic_futex.h"
-#include "private/bionic_pthread.h"
 #include "private/bionic_tls.h"
 #include "private/ScopedPthreadMutexLocker.h"
 
@@ -69,10 +68,6 @@
   return reinterpret_cast<pthread_internal_t*>(__get_tls()[TLS_SLOT_THREAD_ID]);
 }
 
-pid_t __pthread_gettid(pthread_t t) {
-  return reinterpret_cast<pthread_internal_t*>(t)->tid;
-}
-
 // Initialize 'ts' with the difference between 'abstime' and the current time
 // according to 'clock'. Returns -1 if abstime already expired, or 0 otherwise.
 int __timespec_from_absolute(timespec* ts, const timespec* abstime, clockid_t clock) {
diff --git a/libc/dns/resolv/res_cache.c b/libc/dns/resolv/res_cache.c
index 0201aa8..9ca5419 100644
--- a/libc/dns/resolv/res_cache.c
+++ b/libc/dns/resolv/res_cache.c
@@ -1965,10 +1965,25 @@
 {
     int i;
     char** ns;
+    int currentservers;
     int equal = 1;
 
-    // compare each name server against current name servers
     if (numservers > MAXNS) numservers = MAXNS;
+
+    // Find out how many nameservers we had before.
+    currentservers = 0;
+    for (ns = cache_info->nameservers; *ns; ns++)
+        currentservers++;
+
+    if (currentservers != numservers)
+        return 0;
+
+    // Compare each name server against current name servers.
+    // TODO: this is incorrect if the list of current or previous nameservers
+    // contains duplicates. This does not really matter because the framework
+    // filters out duplicates, but we should probably fix it. It's also
+    // insensitive to the order of the nameservers; we should probably fix that
+    // too.
     for (i = 0; i < numservers && equal; i++) {
         ns = cache_info->nameservers;
         equal = 0;
diff --git a/libc/include/fcntl.h b/libc/include/fcntl.h
index 32557d9..87c8481 100644
--- a/libc/include/fcntl.h
+++ b/libc/include/fcntl.h
@@ -72,6 +72,8 @@
 extern int openat64(int, const char*, int, ...);
 extern int open(const char*, int, ...);
 extern int open64(const char*, int, ...);
+extern int posix_fadvise64(int, off64_t, off64_t, int);
+extern int posix_fadvise(int, off_t, off_t, int);
 extern int posix_fallocate64(int, off64_t, off64_t);
 extern int posix_fallocate(int, off_t, off_t);
 extern ssize_t splice(int, off64_t*, int, off64_t*, size_t, unsigned int);
diff --git a/libc/include/pthread.h b/libc/include/pthread.h
index 29caafc..86a1005 100644
--- a/libc/include/pthread.h
+++ b/libc/include/pthread.h
@@ -188,6 +188,8 @@
 
 void* pthread_getspecific(pthread_key_t);
 
+pid_t pthread_gettid_np(pthread_t);
+
 int pthread_join(pthread_t, void**);
 
 int pthread_key_create(pthread_key_t*, void (*)(void*)) __nonnull((1));
diff --git a/libc/stdio/local.h b/libc/stdio/local.h
index 4511a71..d777dec 100644
--- a/libc/stdio/local.h
+++ b/libc/stdio/local.h
@@ -41,20 +41,17 @@
 #include "wcio.h"
 #include "fileext.h"
 
-#if defined(__LP64__)
 /*
  * Android <= KitKat had getc/putc macros in <stdio.h> that referred
  * to __srget/__swbuf, so those symbols need to be public for LP32
  * but can be hidden for LP64.
  */
-__LIBC_HIDDEN__ int __srget(FILE*);
-__LIBC_HIDDEN__ int __swbuf(int, FILE*);
-__LIBC_HIDDEN__ int __srefill(FILE*);
-#else
-__LIBC_ABI_PUBLIC__ int __srget(FILE*);
-__LIBC_ABI_PUBLIC__ int __swbuf(int, FILE*);
-__LIBC_ABI_PUBLIC__ int __srefill(FILE*);
-#endif
+__LIBC64_HIDDEN__ int __srget(FILE*);
+__LIBC64_HIDDEN__ int __swbuf(int, FILE*);
+__LIBC64_HIDDEN__ int __srefill(FILE*);
+
+/* This was referenced by the apportable middleware for LP32. */
+__LIBC64_HIDDEN__ int __swsetup(FILE*);
 
 #pragma GCC visibility push(hidden)
 
@@ -70,7 +67,6 @@
 void	__smakebuf(FILE *);
 int	__swhatbuf(FILE *, size_t *, int *);
 int	_fwalk(int (*)(FILE *));
-int	__swsetup(FILE *);
 int	__sflags(const char *, int *);
 wint_t __fgetwc_unlock(FILE *);
 wint_t	__ungetwc(wint_t, FILE *);
diff --git a/linker/dlfcn.cpp b/linker/dlfcn.cpp
index 3024b3c..9801fa1 100644
--- a/linker/dlfcn.cpp
+++ b/linker/dlfcn.cpp
@@ -118,7 +118,7 @@
     unsigned bind = ELF_ST_BIND(sym->st_info);
 
     if ((bind == STB_GLOBAL || bind == STB_WEAK) && sym->st_shndx != 0) {
-      return reinterpret_cast<void*>(sym->st_value + found->load_bias);
+      return reinterpret_cast<void*>(found->resolve_symbol_address(sym));
     }
 
     __bionic_format_dlerror("symbol found but not global", symbol);
@@ -148,7 +148,7 @@
   ElfW(Sym)* sym = dladdr_find_symbol(si, addr);
   if (sym != nullptr) {
     info->dli_sname = si->strtab + sym->st_name;
-    info->dli_saddr = reinterpret_cast<void*>(si->load_bias + sym->st_value);
+    info->dli_saddr = reinterpret_cast<void*>(si->resolve_symbol_address(sym));
   }
 
   return 1;
diff --git a/linker/linker.cpp b/linker/linker.cpp
index f436e85..1b4fb62 100644
--- a/linker/linker.cpp
+++ b/linker/linker.cpp
@@ -470,32 +470,6 @@
   }
 }
 
-void soinfo::resolve_ifunc_symbols() {
-  if (!get_has_ifuncs()) {
-    return;
-  }
-
-  phdr_table_unprotect_segments(phdr, phnum, load_bias);
-
-  TRACE_TYPE(IFUNC, "CHECKING FOR IFUNCS AND PERFORMING SYMBOL UPDATES");
-
-  for (size_t i = 0; i < nchain; ++i) {
-    ElfW(Sym)* s = &symtab[i];
-    if (ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC) {
-      // The address of the ifunc in the symbol table is the address of the
-      // function that chooses the function to which the ifunc will refer.
-      // In order to return the proper value, we run the choosing function
-      // in the linker and then return its result (minus the base offset).
-      TRACE_TYPE(IFUNC, "FOUND IFUNC");
-      ElfW(Addr) (*ifunc_ptr)();
-      ifunc_ptr = reinterpret_cast<ElfW(Addr)(*)()>(s->st_value + base);
-      s->st_value = (ifunc_ptr() - base);
-      TRACE_TYPE(IFUNC, "NEW VALUE IS %p", (void*)s->st_value);
-    }
-  }
-  phdr_table_protect_segments(phdr, phnum, load_bias);
-}
-
 static unsigned elfhash(const char* _name) {
     const unsigned char* name = reinterpret_cast<const unsigned char*>(_name);
     unsigned h = 0, g;
@@ -1111,52 +1085,14 @@
   protect_data(PROT_READ);
 }
 
-// ifuncs are only defined for x86
-#if defined(__i386__)
-static void soinfo_ifunc_relocate(soinfo* si, ElfW(Rel)* rel, unsigned count) {
-  for (size_t idx = 0; idx < count; ++idx, ++rel) {
-    ElfW(Sym)* s;
-    soinfo* lsi;
-    unsigned type = ELFW(R_TYPE)(rel->r_info);
-    unsigned sym = ELFW(R_SYM)(rel->r_info);
-    ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rel->r_offset + si->load_bias);
-    ElfW(Addr) sym_addr = 0;
-    const char* sym_name = nullptr;
-    sym_name = reinterpret_cast<const char*>(si->strtab + si->symtab[sym].st_name);
-    s = soinfo_do_lookup(si, sym_name, &lsi);
+static ElfW(Addr) call_ifunc_resolver(ElfW(Addr) resolver_addr) {
+  typedef ElfW(Addr) (*ifunc_resolver_t)(void);
+  ifunc_resolver_t ifunc_resolver = reinterpret_cast<ifunc_resolver_t>(resolver_addr);
+  ElfW(Addr) ifunc_addr = ifunc_resolver();
+  TRACE_TYPE(RELO, "Called ifunc_resolver@%p. The result is %p", ifunc_resolver, reinterpret_cast<void*>(ifunc_addr));
 
-    if (ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC && type == R_386_JMP_SLOT) {
-      TRACE("IFUNC RELOCATION, PASS 2: %p",  (void*)(sym_addr));
-      ElfW(Addr) (*ifunc_ptr)();
-      ifunc_ptr = reinterpret_cast<ElfW(Addr)(*)()>(s->st_value + si->base);
-      *reinterpret_cast<ElfW(Addr)*>(reloc) = ifunc_ptr();
-    }
-  }
+  return ifunc_addr;
 }
-#endif
-
-#if defined(__x86_64__)
-static void soinfo_ifunc_relocate(soinfo* si, ElfW(Rela)* rela, unsigned count) {
-  for (size_t idx = 0; idx < count; ++idx, ++rela) {
-    ElfW(Sym)* s;
-    soinfo* lsi;
-    unsigned type = ELFW(R_TYPE)(rela->r_info);
-    unsigned sym = ELFW(R_SYM)(rela->r_info);
-    ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rela->r_offset + si->load_bias);
-    ElfW(Addr) sym_addr = 0;
-    const char* sym_name = nullptr;
-    sym_name = reinterpret_cast<const char*>(si->strtab + si->symtab[sym].st_name);
-    s = soinfo_do_lookup(si, sym_name, &lsi);
-
-    if (ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC && type == R_X86_64_JUMP_SLOT) {
-      TRACE("IFUNC RELOCATION, PASS 2: %p",  (void*)(sym_addr + rela->r_addend));
-      ElfW(Addr) (*ifunc_ptr)();
-      ifunc_ptr = reinterpret_cast<ElfW(Addr)(*)()>(s->st_value + si->base);
-      *reinterpret_cast<ElfW(Addr)*>(reloc) = ifunc_ptr();
-    }
-  }
-}
-#endif
 
 #if defined(USE_RELA)
 int soinfo::Relocate(ElfW(Rela)* rela, unsigned count) {
@@ -1206,6 +1142,7 @@
         case R_AARCH64_ABS32:
         case R_AARCH64_ABS16:
         case R_AARCH64_RELATIVE:
+        case R_AARCH64_IRELATIVE:
           /*
            * The sym_addr was initialized to be zero above, or the relocation
            * code below does not care about value of sym_addr.
@@ -1218,6 +1155,7 @@
         case R_X86_64_32:
         case R_X86_64_64:
         case R_X86_64_RELATIVE:
+        case R_X86_64_IRELATIVE:
           // No need to do anything.
           break;
         case R_X86_64_PC32:
@@ -1230,7 +1168,7 @@
         }
       } else {
         // We got a definition.
-        sym_addr = static_cast<ElfW(Addr)>(s->st_value + lsi->load_bias);
+        sym_addr = lsi->resolve_symbol_address(s);
       }
       count_relocation(kRelocSymbol);
     }
@@ -1342,6 +1280,13 @@
         *reinterpret_cast<ElfW(Addr)*>(reloc) = (base + rela->r_addend);
         break;
 
+    case R_AARCH64_IRELATIVE:
+      count_relocation(kRelocRelative);
+      MARK(rela->r_offset);
+      TRACE_TYPE(RELO, "RELO IRELATIVE %16llx <- %16llx\n", reloc, (base + rela->r_addend));
+      *reinterpret_cast<ElfW(Addr)*>(reloc) = call_ifunc_resolver(base + rela->r_addend);
+      break;
+
     case R_AARCH64_COPY:
         /*
          * ET_EXEC is not supported so this should not happen.
@@ -1368,11 +1313,7 @@
       MARK(rela->r_offset);
       TRACE_TYPE(RELO, "RELO JMP_SLOT %08zx <- %08zx %s", static_cast<size_t>(reloc),
                  static_cast<size_t>(sym_addr + rela->r_addend), sym_name);
-      if (ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC) {
-        set_has_ifuncs(true);
-      } else {
-        *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend;
-      }
+      *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend;
       break;
     case R_X86_64_GLOB_DAT:
       count_relocation(kRelocAbsolute);
@@ -1392,6 +1333,12 @@
                  static_cast<size_t>(base));
       *reinterpret_cast<ElfW(Addr)*>(reloc) = base + rela->r_addend;
       break;
+    case R_X86_64_IRELATIVE:
+      count_relocation(kRelocRelative);
+      MARK(rela->r_offset);
+      TRACE_TYPE(RELO, "RELO IRELATIVE %16llx <- %16llx\n", reloc, (base + rela->r_addend));
+      *reinterpret_cast<ElfW(Addr)*>(reloc) = call_ifunc_resolver(base + rela->r_addend);
+      break;
     case R_X86_64_32:
       count_relocation(kRelocRelative);
       MARK(rela->r_offset);
@@ -1481,6 +1428,7 @@
                 case R_386_GLOB_DAT:
                 case R_386_32:
                 case R_386_RELATIVE:    /* Don't care. */
+                case R_386_IRELATIVE:
                     // sym_addr was initialized to be zero above or relocation
                     // code below does not care about value of sym_addr.
                     // No need to do anything.
@@ -1500,7 +1448,7 @@
                 }
             } else {
                 // We got a definition.
-                sym_addr = static_cast<ElfW(Addr)>(s->st_value + lsi->load_bias);
+                sym_addr = lsi->resolve_symbol_address(s);
             }
             count_relocation(kRelocSymbol);
         }
@@ -1549,11 +1497,7 @@
             count_relocation(kRelocAbsolute);
             MARK(rel->r_offset);
             TRACE_TYPE(RELO, "RELO JMP_SLOT %08x <- %08x %s", reloc, sym_addr, sym_name);
-            if (ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC) {
-              set_has_ifuncs(true);
-            } else {
-              *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr;
-            }
+            *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr;
             break;
         case R_386_GLOB_DAT:
             count_relocation(kRelocAbsolute);
@@ -1614,6 +1558,14 @@
                        reinterpret_cast<void*>(reloc), reinterpret_cast<void*>(base));
             *reinterpret_cast<ElfW(Addr)*>(reloc) += base;
             break;
+#if defined(__i386__)
+        case R_386_IRELATIVE:
+          count_relocation(kRelocRelative);
+          MARK(rel->r_offset);
+          TRACE_TYPE(RELO, "RELO IRELATIVE %p <- %p", reinterpret_cast<void*>(reloc), reinterpret_cast<void*>(base));
+          *reinterpret_cast<ElfW(Addr)*>(reloc) = call_ifunc_resolver(base + *reinterpret_cast<ElfW(Addr)*>(reloc));
+          break;
+#endif
 
         default:
             DL_ERR("unknown reloc type %d @ %p (%zu)", type, rel, idx);
@@ -1671,7 +1623,7 @@
             // FIXME: is this sufficient?
             // For reference see NetBSD link loader
             // http://cvsweb.netbsd.org/bsdweb.cgi/src/libexec/ld.elf_so/arch/mips/mips_reloc.c?rev=1.53&content-type=text/x-cvsweb-markup
-            *got = reinterpret_cast<ElfW(Addr)*>(lsi->load_bias + s->st_value);
+            *got = reinterpret_cast<ElfW(Addr)*>(lsi->resolve_symbol_address(s));
         }
     }
     return true;
@@ -1749,8 +1701,6 @@
   // DT_INIT should be called before DT_INIT_ARRAY if both are present.
   CallFunction("DT_INIT", init_func);
   CallArray("DT_INIT_ARRAY", init_array, init_array_count, false);
-
-  resolve_ifunc_symbols();
 }
 
 void soinfo::CallDestructors() {
@@ -1812,12 +1762,6 @@
   }
 }
 
-void soinfo::set_has_ifuncs(bool ifuncs) {
-  if (has_min_version(1)) {
-    has_ifuncs = ifuncs;
-  }
-}
-
 dev_t soinfo::get_st_dev() {
   if (has_min_version(0)) {
     return st_dev;
@@ -1834,14 +1778,6 @@
   return 0;
 }
 
-bool soinfo::get_has_ifuncs() {
-  if (has_min_version(1)) {
-    return has_ifuncs;
-  }
-
-  return false;
-}
-
 // This is a return on get_children()/get_parents() if
 // 'this->flags' does not have FLAG_NEW_SOINFO set.
 static soinfo::soinfo_list_t g_empty_list;
@@ -1862,6 +1798,14 @@
   return this->parents;
 }
 
+ElfW(Addr) soinfo::resolve_symbol_address(ElfW(Sym)* s) {
+  if (ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC) {
+    return call_ifunc_resolver(s->st_value + load_bias);
+  }
+
+  return static_cast<ElfW(Addr)>(s->st_value + load_bias);
+}
+
 /* Force any of the closed stdin, stdout and stderr to be associated with
    /dev/null. */
 static int nullify_closed_stdio() {
@@ -2168,44 +2112,32 @@
 #endif
 
 #if defined(USE_RELA)
-    if (plt_rela != nullptr) {
-        DEBUG("[ relocating %s plt ]\n", name);
-        if (Relocate(plt_rela, plt_rela_count)) {
-            return false;
-        }
-    }
     if (rela != nullptr) {
-        DEBUG("[ relocating %s ]\n", name);
+        DEBUG("[ relocating %s ]", name);
         if (Relocate(rela, rela_count)) {
             return false;
         }
     }
-#else
-    if (plt_rel != nullptr) {
+    if (plt_rela != nullptr) {
         DEBUG("[ relocating %s plt ]", name);
-        if (Relocate(plt_rel, plt_rel_count)) {
+        if (Relocate(plt_rela, plt_rela_count)) {
             return false;
         }
     }
+#else
     if (rel != nullptr) {
         DEBUG("[ relocating %s ]", name);
         if (Relocate(rel, rel_count)) {
             return false;
         }
     }
-#endif
-
-    // if there are ifuncs, we need to do an additional relocation pass.
-    // they cannot be resolved until the rest of the relocations are done
-    // because we need to call the resolution function which may be waiting
-    // on relocations.
-    if(get_has_ifuncs()) {
-#if defined(__i386__)
-      soinfo_ifunc_relocate(this, plt_rel, plt_rel_count);
-#elif defined(__x86_64__)
-      soinfo_ifunc_relocate(this, plt_rela, plt_rela_count);
-#endif
+    if (plt_rel != nullptr) {
+        DEBUG("[ relocating %s plt ]", name);
+        if (Relocate(plt_rel, plt_rel_count)) {
+            return false;
+        }
     }
+#endif
 
 #if defined(__mips__)
     if (!mips_relocate_got(this)) {
diff --git a/linker/linker.h b/linker/linker.h
index 3024d3a..37d513e 100644
--- a/linker/linker.h
+++ b/linker/linker.h
@@ -197,6 +197,8 @@
 #if !defined(__LP64__)
   bool has_text_relocations;
 #endif
+  // TODO: remove this flag, dynamic linker
+  // should not use it in any way.
   bool has_DT_SYMBOLIC;
 
   soinfo(const char* name, const struct stat* file_stat);
@@ -212,21 +214,20 @@
 
   void set_st_dev(dev_t st_dev);
   void set_st_ino(ino_t st_ino);
-  void set_has_ifuncs(bool ifunc);
   ino_t get_st_ino();
   dev_t get_st_dev();
-  bool get_has_ifuncs();
 
   soinfo_list_t& get_children();
   soinfo_list_t& get_parents();
 
+  ElfW(Addr) resolve_symbol_address(ElfW(Sym)* s);
+
   bool inline has_min_version(uint32_t min_version) {
     return (flags & FLAG_NEW_SOINFO) != 0 && version >= min_version;
   }
  private:
   void CallArray(const char* array_name, linker_function_t* functions, size_t count, bool reverse);
   void CallFunction(const char* function_name, linker_function_t function);
-  void resolve_ifunc_symbols();
 #if defined(USE_RELA)
   int Relocate(ElfW(Rela)* rela, unsigned count);
 #else
@@ -247,7 +248,6 @@
   soinfo_list_t parents;
 
   // version >= 1
-  bool has_ifuncs;
 };
 
 extern soinfo* get_libdl_info();
diff --git a/tests/Android.build.mk b/tests/Android.build.mk
index d4b0396..d54c851 100644
--- a/tests/Android.build.mk
+++ b/tests/Android.build.mk
@@ -37,6 +37,10 @@
 
 LOCAL_FORCE_STATIC_EXECUTABLE := $($(module)_force_static_executable)
 
+ifneq ($($(module)_multilib),)
+    LOCAL_MULTILIB := $($(module)_multilib)
+endif
+
 LOCAL_CFLAGS := \
     $(common_cflags) \
     $($(module)_cflags) \
diff --git a/tests/dlfcn_test.cpp b/tests/dlfcn_test.cpp
index aa0af44..5d6cc8b 100644
--- a/tests/dlfcn_test.cpp
+++ b/tests/dlfcn_test.cpp
@@ -89,8 +89,8 @@
   ASSERT_EQ(0, dlclose(handle2));
 }
 
-// ifuncs are only supported on intel for now
-#if defined(__i386__) || defined(__x86_64__)
+// ifuncs are only supported on intel and arm64 for now
+#if defined (__aarch64__) || defined(__i386__) || defined(__x86_64__)
 TEST(dlfcn, ifunc) {
   typedef const char* (*fn_ptr)();
 
@@ -124,9 +124,13 @@
   typedef const char* (*fn_ptr)();
 
   void* handle = dlopen("libtest_ifunc.so", RTLD_NOW);
-  ASSERT_TRUE(handle != NULL) << dlerror();
-  fn_ptr is_ctor_called =  reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called"));
-  ASSERT_TRUE(is_ctor_called != NULL) << dlerror();
+  ASSERT_TRUE(handle != nullptr) << dlerror();
+  fn_ptr is_ctor_called =  reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_irelative"));
+  ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
+  ASSERT_STREQ("false", is_ctor_called());
+
+  is_ctor_called =  reinterpret_cast<fn_ptr>(dlsym(handle, "is_ctor_called_jump_slot"));
+  ASSERT_TRUE(is_ctor_called != nullptr) << dlerror();
   ASSERT_STREQ("true", is_ctor_called());
   dlclose(handle);
 }
diff --git a/tests/fcntl_test.cpp b/tests/fcntl_test.cpp
index 5f20295..3fd0a8c 100644
--- a/tests/fcntl_test.cpp
+++ b/tests/fcntl_test.cpp
@@ -70,10 +70,32 @@
   ASSERT_EQ(ENOENT, errno);
 }
 
+TEST(fcntl, posix_fadvise) {
+  TemporaryFile tf;
+  errno = 0;
+
+  EXPECT_EQ(EBADF, posix_fadvise(-1, 0, 0, POSIX_FADV_NORMAL));
+  EXPECT_EQ(0, errno);
+
+  EXPECT_EQ(EBADF, posix_fadvise64(-1, 0, 0, POSIX_FADV_NORMAL));
+  EXPECT_EQ(0, errno);
+
+  EXPECT_EQ(EINVAL, posix_fadvise(tf.fd, 0, 0, -1));
+  EXPECT_EQ(0, errno);
+
+  EXPECT_EQ(EINVAL, posix_fadvise64(tf.fd, 0, 0, -1));
+  EXPECT_EQ(0, errno);
+
+  EXPECT_EQ(0, posix_fadvise(tf.fd, 0, 0, POSIX_FADV_NORMAL));
+  EXPECT_EQ(0, posix_fadvise64(tf.fd, 0, 0, POSIX_FADV_NORMAL));
+}
+
 TEST(fcntl, fallocate_EINVAL) {
   TemporaryFile tf;
 
-#if defined(__BIONIC__)
+  // fallocate/fallocate64 set errno.
+  // posix_fallocate/posix_fallocate64 return an errno value.
+
   errno = 0;
   ASSERT_EQ(-1, fallocate(tf.fd, 0, 0, -1));
   ASSERT_EQ(EINVAL, errno);
@@ -81,7 +103,6 @@
   errno = 0;
   ASSERT_EQ(-1, fallocate64(tf.fd, 0, 0, -1));
   ASSERT_EQ(EINVAL, errno);
-#endif
 
   errno = 0;
   ASSERT_EQ(EINVAL, posix_fallocate(tf.fd, 0, -1));
diff --git a/tests/libs/Android.mk b/tests/libs/Android.mk
index be6565b..a675a4f 100644
--- a/tests/libs/Android.mk
+++ b/tests/libs/Android.mk
@@ -295,7 +295,7 @@
 # -----------------------------------------------------------------------------
 # Library used by ifunc tests
 # -----------------------------------------------------------------------------
-ifeq ($(TARGET_ARCH),$(filter $(TARGET_ARCH),x86 x86_64))
+ifeq ($(TARGET_ARCH),$(filter $(TARGET_ARCH),arm64 x86 x86_64))
     libtest_ifunc_src_files := \
         dlopen_testlib_ifunc.c
 
@@ -303,6 +303,14 @@
     module := libtest_ifunc
     build_type := target
     build_target := SHARED_LIBRARY
+
+    ifeq ($(TARGET_ARCH),arm64)
+      libtest_ifunc_multilib := 64
+      # TODO: This is a workaround - remove it once gcc
+      # removes its Android ifunc checks
+      libtest_ifunc_cflags := -mglibc
+    endif
+
     include $(TEST_PATH)/Android.build.mk
 endif
 
diff --git a/tests/libs/dlopen_testlib_ifunc.c b/tests/libs/dlopen_testlib_ifunc.c
index 4874841..b68a3dd 100644
--- a/tests/libs/dlopen_testlib_ifunc.c
+++ b/tests/libs/dlopen_testlib_ifunc.c
@@ -23,8 +23,17 @@
   g_flag = 1;
 }
 
+static const char* is_ctor_called() __attribute__ ((ifunc("is_ctor_called_ifun")));
+
 const char* foo() __attribute__ ((ifunc ("foo_ifunc")));
-const char* is_ctor_called() __attribute__ ((ifunc("is_ctor_called_ifun")));
+
+// Static linker creates GLOBAL/IFUNC symbol and JUMP_SLOT relocation type for plt segment
+const char* is_ctor_called_jump_slot() __attribute__ ((ifunc("is_ctor_called_ifun")));
+
+const char* is_ctor_called_irelative() {
+  // Call internal ifunc-resolved function with IRELATIVE reloc
+  return is_ctor_called();
+}
 
 const char* return_true() {
   return "true";