Merge "Add IFUNC support for arm64 and IRELATIVE reloc"
diff --git a/libc/Android.mk b/libc/Android.mk
index d277551..ce75e0d 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/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/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/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/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/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));