Use VDSO for clock_gettime(2) and gettimeofday(2).

Bug: 15387103
Change-Id: Ifc3608ea65060c1dc38120b10b6e79874f182a36
diff --git a/libc/Android.mk b/libc/Android.mk
index 51002e5..0485d72 100644
--- a/libc/Android.mk
+++ b/libc/Android.mk
@@ -229,6 +229,7 @@
     bionic/umount.cpp \
     bionic/unlink.cpp \
     bionic/utimes.cpp \
+    bionic/vdso.cpp \
     bionic/vfork.cpp \
     bionic/wait.cpp \
     bionic/wchar.cpp \
diff --git a/libc/SYSCALLS.TXT b/libc/SYSCALLS.TXT
index b4c8134..7ae054e 100644
--- a/libc/SYSCALLS.TXT
+++ b/libc/SYSCALLS.TXT
@@ -194,11 +194,9 @@
 int     swapoff(const char*) all
 
 # time
-int           gettimeofday(struct timeval*, struct timezone*)       all
 int           settimeofday(const struct timeval*, const struct timezone*)   all
 clock_t       times(struct tms*)       all
 int           nanosleep(const struct timespec*, struct timespec*)   all
-int           clock_gettime(clockid_t clk_id, struct timespec* tp)    all
 int           clock_settime(clockid_t clk_id, const struct timespec* tp)  all
 int           clock_getres(clockid_t clk_id, struct timespec* res)   all
 int           clock_nanosleep(clockid_t clock_id, int flags, const struct timespec* req, struct timespec* rem)  all
@@ -320,3 +318,9 @@
 
 # x86-specific
 int     __set_thread_area:set_thread_area(void*) x86
+
+# vdso stuff.
+int clock_gettime(clockid_t, timespec*)                 arm,mips,mips64,x86
+int __clock_gettime:clock_gettime(clockid_t, timespec*) arm64,x86_64
+int gettimeofday(timeval*, timezone*)                   arm,mips,mips64,x86
+int __gettimeofday:gettimeofday(timeval*, timezone*)    arm64,x86_64
diff --git a/libc/arch-arm64/syscalls/clock_gettime.S b/libc/arch-arm64/syscalls/__clock_gettime.S
similarity index 76%
rename from libc/arch-arm64/syscalls/clock_gettime.S
rename to libc/arch-arm64/syscalls/__clock_gettime.S
index ffdde57..f346648 100644
--- a/libc/arch-arm64/syscalls/clock_gettime.S
+++ b/libc/arch-arm64/syscalls/__clock_gettime.S
@@ -2,7 +2,7 @@
 
 #include <private/bionic_asm.h>
 
-ENTRY(clock_gettime)
+ENTRY(__clock_gettime)
     mov     x8, __NR_clock_gettime
     svc     #0
 
@@ -11,4 +11,5 @@
     b.hi    __set_errno
 
     ret
-END(clock_gettime)
+END(__clock_gettime)
+.hidden __clock_gettime
diff --git a/libc/arch-arm64/syscalls/gettimeofday.S b/libc/arch-arm64/syscalls/__gettimeofday.S
similarity index 77%
rename from libc/arch-arm64/syscalls/gettimeofday.S
rename to libc/arch-arm64/syscalls/__gettimeofday.S
index 3b6104b..6582c49 100644
--- a/libc/arch-arm64/syscalls/gettimeofday.S
+++ b/libc/arch-arm64/syscalls/__gettimeofday.S
@@ -2,7 +2,7 @@
 
 #include <private/bionic_asm.h>
 
-ENTRY(gettimeofday)
+ENTRY(__gettimeofday)
     mov     x8, __NR_gettimeofday
     svc     #0
 
@@ -11,4 +11,5 @@
     b.hi    __set_errno
 
     ret
-END(gettimeofday)
+END(__gettimeofday)
+.hidden __gettimeofday
diff --git a/libc/arch-x86_64/syscalls/clock_gettime.S b/libc/arch-x86_64/syscalls/__clock_gettime.S
similarity index 78%
rename from libc/arch-x86_64/syscalls/clock_gettime.S
rename to libc/arch-x86_64/syscalls/__clock_gettime.S
index 20850c8..7e553b8 100644
--- a/libc/arch-x86_64/syscalls/clock_gettime.S
+++ b/libc/arch-x86_64/syscalls/__clock_gettime.S
@@ -2,7 +2,7 @@
 
 #include <private/bionic_asm.h>
 
-ENTRY(clock_gettime)
+ENTRY(__clock_gettime)
     movl    $__NR_clock_gettime, %eax
     syscall
     cmpq    $-MAX_ERRNO, %rax
@@ -12,4 +12,5 @@
     call    __set_errno
 1:
     ret
-END(clock_gettime)
+END(__clock_gettime)
+.hidden __clock_gettime
diff --git a/libc/arch-x86_64/syscalls/gettimeofday.S b/libc/arch-x86_64/syscalls/__gettimeofday.S
similarity index 79%
rename from libc/arch-x86_64/syscalls/gettimeofday.S
rename to libc/arch-x86_64/syscalls/__gettimeofday.S
index 4867c30..a38eb64 100644
--- a/libc/arch-x86_64/syscalls/gettimeofday.S
+++ b/libc/arch-x86_64/syscalls/__gettimeofday.S
@@ -2,7 +2,7 @@
 
 #include <private/bionic_asm.h>
 
-ENTRY(gettimeofday)
+ENTRY(__gettimeofday)
     movl    $__NR_gettimeofday, %eax
     syscall
     cmpq    $-MAX_ERRNO, %rax
@@ -12,4 +12,5 @@
     call    __set_errno
 1:
     ret
-END(gettimeofday)
+END(__gettimeofday)
+.hidden __gettimeofday
diff --git a/libc/bionic/dl_iterate_phdr_static.cpp b/libc/bionic/dl_iterate_phdr_static.cpp
index 7e9eedd..155a7a0 100644
--- a/libc/bionic/dl_iterate_phdr_static.cpp
+++ b/libc/bionic/dl_iterate_phdr_static.cpp
@@ -27,6 +27,7 @@
  */
 
 #include <elf.h>
+#include <string.h>
 #include <sys/auxv.h>
 #include <sys/types.h>
 #include <link.h>
@@ -37,11 +38,9 @@
 int dl_iterate_phdr(int (*cb)(struct dl_phdr_info* info, size_t size, void* data), void* data) {
   ElfW(Ehdr)* ehdr = reinterpret_cast<ElfW(Ehdr)*>(&__executable_start);
 
-  // TODO: again, copied from linker.c. Find a better home for this later.
-  if (ehdr->e_ident[EI_MAG0] != ELFMAG0) return -1;
-  if (ehdr->e_ident[EI_MAG1] != ELFMAG1) return -1;
-  if (ehdr->e_ident[EI_MAG2] != ELFMAG2) return -1;
-  if (ehdr->e_ident[EI_MAG3] != ELFMAG3) return -1;
+  if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) != 0) {
+    return -1;
+  }
 
   // Dynamic binaries get their dl_iterate_phdr from the dynamic linker, but
   // static binaries get this. We don't have a list of shared objects to
@@ -54,7 +53,7 @@
   exe_info.dlpi_phdr = reinterpret_cast<ElfW(Phdr)*>(reinterpret_cast<uintptr_t>(ehdr) + ehdr->e_phoff);
   exe_info.dlpi_phnum = ehdr->e_phnum;
 
-#ifdef AT_SYSINFO_EHDR
+#if defined(AT_SYSINFO_EHDR)
   // Try the executable first.
   int rc = cb(&exe_info, sizeof(exe_info), data);
   if (rc != 0) {
diff --git a/libc/bionic/libc_init_common.cpp b/libc/bionic/libc_init_common.cpp
index aa76650..9e4eecd 100644
--- a/libc/bionic/libc_init_common.cpp
+++ b/libc/bionic/libc_init_common.cpp
@@ -51,6 +51,8 @@
 extern "C" int __set_tls(void* ptr);
 extern "C" int __set_tid_address(int* tid_address);
 
+void __libc_init_vdso();
+
 // Not public, but well-known in the BSDs.
 const char* __progname;
 
@@ -129,6 +131,8 @@
   _pthread_internal_add(main_thread);
 
   __system_properties_init(); // Requires 'environ'.
+
+  __libc_init_vdso();
 }
 
 /* This function will be called during normal program termination
diff --git a/libc/bionic/vdso.cpp b/libc/bionic/vdso.cpp
new file mode 100644
index 0000000..0875ee6
--- /dev/null
+++ b/libc/bionic/vdso.cpp
@@ -0,0 +1,129 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <link.h>
+#include <sys/auxv.h>
+#include <unistd.h>
+
+// x86 has a vdso, but there's nothing useful to us in it.
+#if defined(__aarch64__) || defined(__x86_64__)
+
+#if defined(__aarch64__)
+#define VDSO_CLOCK_GETTIME_SYMBOL "__kernel_clock_gettime"
+#define VDSO_GETTIMEOFDAY_SYMBOL  "__kernel_gettimeofday"
+#elif defined(__x86_64__)
+#define VDSO_CLOCK_GETTIME_SYMBOL "__vdso_clock_gettime"
+#define VDSO_GETTIMEOFDAY_SYMBOL  "__vdso_gettimeofday"
+#endif
+
+#include <time.h>
+
+extern "C" int __clock_gettime(int, timespec*);
+extern "C" int __gettimeofday(timeval*, struct timezone*);
+
+struct vdso_entry {
+  const char* name;
+  void* fn;
+};
+
+enum {
+  VDSO_CLOCK_GETTIME = 0,
+  VDSO_GETTIMEOFDAY,
+  VDSO_END
+};
+
+static vdso_entry vdso_entries[] = {
+  [VDSO_CLOCK_GETTIME] = { VDSO_CLOCK_GETTIME_SYMBOL, reinterpret_cast<void*>(__clock_gettime) },
+  [VDSO_GETTIMEOFDAY] = { VDSO_GETTIMEOFDAY_SYMBOL, reinterpret_cast<void*>(__gettimeofday) },
+};
+
+int clock_gettime(int clock_id, timespec* tp) {
+  static int (*vdso_clock_gettime)(int, timespec*) =
+      (int (*)(int, timespec*)) vdso_entries[VDSO_CLOCK_GETTIME].fn;
+  return vdso_clock_gettime(clock_id, tp);
+}
+
+int gettimeofday(timeval* tv, struct timezone* tz) {
+  static int (*vdso_gettimeofday)(timeval*, struct timezone*) =
+      (int (*)(timeval*, struct timezone*)) vdso_entries[VDSO_GETTIMEOFDAY].fn;
+  return vdso_gettimeofday(tv, tz);
+}
+
+void __libc_init_vdso() {
+  // Do we have a vdso?
+  uintptr_t vdso_ehdr_addr = getauxval(AT_SYSINFO_EHDR);
+  ElfW(Ehdr)* vdso_ehdr = reinterpret_cast<ElfW(Ehdr)*>(vdso_ehdr_addr);
+  if (vdso_ehdr == NULL) {
+    return;
+  }
+
+  // How many symbols does it have?
+  size_t symbol_count = 0;
+  ElfW(Shdr)* vdso_shdr = reinterpret_cast<ElfW(Shdr)*>(vdso_ehdr_addr + vdso_ehdr->e_shoff);
+  for (size_t i = 0; i < vdso_ehdr->e_shnum; ++i) {
+    if (vdso_shdr[i].sh_type == SHT_DYNSYM) {
+      symbol_count = vdso_shdr[i].sh_size / sizeof(ElfW(Sym));
+    }
+  }
+  if (symbol_count == 0) {
+    return;
+  }
+
+  // Where's the dynamic table?
+  ElfW(Addr) vdso_addr = 0;
+  ElfW(Dyn)* vdso_dyn = NULL;
+  ElfW(Phdr)* vdso_phdr = reinterpret_cast<ElfW(Phdr)*>(vdso_ehdr_addr + vdso_ehdr->e_phoff);
+  for (size_t i = 0; i < vdso_ehdr->e_phnum; ++i) {
+    if (vdso_phdr[i].p_type == PT_DYNAMIC) {
+      vdso_dyn = reinterpret_cast<ElfW(Dyn)*>(vdso_ehdr_addr + vdso_phdr[i].p_offset);
+    } else if (vdso_phdr[i].p_type == PT_LOAD) {
+      vdso_addr = vdso_ehdr_addr + vdso_phdr[i].p_offset - vdso_phdr[i].p_vaddr;
+    }
+  }
+  if (vdso_addr == 0 || vdso_dyn == NULL) {
+    return;
+  }
+
+  // Where are the string and symbol tables?
+  const char* strtab = NULL;
+  ElfW(Sym)* symtab = NULL;
+  for (ElfW(Dyn)* d = vdso_dyn; d->d_tag != DT_NULL; ++d) {
+    if (d->d_tag == DT_STRTAB) {
+      strtab = reinterpret_cast<const char*>(vdso_addr + d->d_un.d_ptr);
+    } else if (d->d_tag == DT_SYMTAB) {
+      symtab = reinterpret_cast<ElfW(Sym)*>(vdso_addr + d->d_un.d_ptr);
+    }
+  }
+  if (strtab == NULL || symtab == NULL) {
+    return;
+  }
+
+  // Are there any symbols we want?
+  for (size_t i = 0; i < symbol_count; ++i) {
+    for (size_t j = 0; j < VDSO_END; ++j) {
+      if (strcmp(vdso_entries[j].name, strtab + symtab[i].st_name) == 0) {
+        vdso_entries[j].fn = reinterpret_cast<void*>(vdso_addr + symtab[i].st_value);
+      }
+    }
+  }
+}
+
+#else
+
+void __libc_init_vdso() {
+}
+
+#endif