| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * Copyright (C) 2014 The Android Open Source Project | 
 | 3 |  * | 
 | 4 |  * Licensed under the Apache License, Version 2.0 (the "License"); | 
 | 5 |  * you may not use this file except in compliance with the License. | 
 | 6 |  * You may obtain a copy of the License at | 
 | 7 |  * | 
 | 8 |  *      http://www.apache.org/licenses/LICENSE-2.0 | 
 | 9 |  * | 
 | 10 |  * Unless required by applicable law or agreed to in writing, software | 
 | 11 |  * distributed under the License is distributed on an "AS IS" BASIS, | 
 | 12 |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
 | 13 |  * See the License for the specific language governing permissions and | 
 | 14 |  * limitations under the License. | 
 | 15 |  */ | 
 | 16 |  | 
| Josh Gao | 93c0f5e | 2015-10-06 11:08:13 -0700 | [diff] [blame] | 17 | #include "private/bionic_globals.h" | 
 | 18 | #include "private/bionic_vdso.h" | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 19 |  | 
| Elliott Hughes | 4ce902c | 2016-07-12 13:55:51 -0700 | [diff] [blame] | 20 | #if defined(__aarch64__) || defined(__arm__) || defined(__i386__) || defined(__x86_64__) | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 21 |  | 
| Elliott Hughes | 613f814 | 2015-07-20 22:34:27 +0000 | [diff] [blame] | 22 | #include <limits.h> | 
| Josh Gao | 93c0f5e | 2015-10-06 11:08:13 -0700 | [diff] [blame] | 23 | #include <link.h> | 
 | 24 | #include <string.h> | 
 | 25 | #include <sys/cdefs.h> | 
 | 26 | #include <sys/time.h> | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 27 | #include <time.h> | 
| Josh Gao | 93c0f5e | 2015-10-06 11:08:13 -0700 | [diff] [blame] | 28 | #include <unistd.h> | 
 | 29 | #include "private/KernelArgumentBlock.h" | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 30 |  | 
| Elliott Hughes | 4ce902c | 2016-07-12 13:55:51 -0700 | [diff] [blame] | 31 | #define AT_SYSINFO_EHDR 33 /* until we have new enough uapi headers... */ | 
 | 32 |  | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 33 | int clock_gettime(int clock_id, timespec* tp) { | 
| Josh Gao | 93c0f5e | 2015-10-06 11:08:13 -0700 | [diff] [blame] | 34 |   auto vdso_clock_gettime = reinterpret_cast<decltype(&clock_gettime)>( | 
 | 35 |     __libc_globals->vdso[VDSO_CLOCK_GETTIME].fn); | 
 | 36 |   if (__predict_true(vdso_clock_gettime)) { | 
 | 37 |     return vdso_clock_gettime(clock_id, tp); | 
 | 38 |   } | 
 | 39 |   return __clock_gettime(clock_id, tp); | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 40 | } | 
 | 41 |  | 
 | 42 | int gettimeofday(timeval* tv, struct timezone* tz) { | 
| Josh Gao | 93c0f5e | 2015-10-06 11:08:13 -0700 | [diff] [blame] | 43 |   auto vdso_gettimeofday = reinterpret_cast<decltype(&gettimeofday)>( | 
 | 44 |     __libc_globals->vdso[VDSO_GETTIMEOFDAY].fn); | 
 | 45 |   if (__predict_true(vdso_gettimeofday)) { | 
 | 46 |     return vdso_gettimeofday(tv, tz); | 
 | 47 |   } | 
 | 48 |   return __gettimeofday(tv, tz); | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 49 | } | 
 | 50 |  | 
| Josh Gao | 93c0f5e | 2015-10-06 11:08:13 -0700 | [diff] [blame] | 51 | void __libc_init_vdso(libc_globals* globals, KernelArgumentBlock& args) { | 
 | 52 |   auto&& vdso = globals->vdso; | 
 | 53 |   vdso[VDSO_CLOCK_GETTIME] = { VDSO_CLOCK_GETTIME_SYMBOL, | 
 | 54 |                                reinterpret_cast<void*>(__clock_gettime) }; | 
 | 55 |   vdso[VDSO_GETTIMEOFDAY] = { VDSO_GETTIMEOFDAY_SYMBOL, | 
 | 56 |                               reinterpret_cast<void*>(__gettimeofday) }; | 
 | 57 |  | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 58 |   // Do we have a vdso? | 
| Josh Gao | 93c0f5e | 2015-10-06 11:08:13 -0700 | [diff] [blame] | 59 |   uintptr_t vdso_ehdr_addr = args.getauxval(AT_SYSINFO_EHDR); | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 60 |   ElfW(Ehdr)* vdso_ehdr = reinterpret_cast<ElfW(Ehdr)*>(vdso_ehdr_addr); | 
| Elliott Hughes | 613f814 | 2015-07-20 22:34:27 +0000 | [diff] [blame] | 61 |   if (vdso_ehdr == nullptr) { | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 62 |     return; | 
 | 63 |   } | 
 | 64 |  | 
 | 65 |   // How many symbols does it have? | 
 | 66 |   size_t symbol_count = 0; | 
 | 67 |   ElfW(Shdr)* vdso_shdr = reinterpret_cast<ElfW(Shdr)*>(vdso_ehdr_addr + vdso_ehdr->e_shoff); | 
 | 68 |   for (size_t i = 0; i < vdso_ehdr->e_shnum; ++i) { | 
 | 69 |     if (vdso_shdr[i].sh_type == SHT_DYNSYM) { | 
 | 70 |       symbol_count = vdso_shdr[i].sh_size / sizeof(ElfW(Sym)); | 
 | 71 |     } | 
 | 72 |   } | 
 | 73 |   if (symbol_count == 0) { | 
 | 74 |     return; | 
 | 75 |   } | 
 | 76 |  | 
 | 77 |   // Where's the dynamic table? | 
 | 78 |   ElfW(Addr) vdso_addr = 0; | 
| Elliott Hughes | 613f814 | 2015-07-20 22:34:27 +0000 | [diff] [blame] | 79 |   ElfW(Dyn)* vdso_dyn = nullptr; | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 80 |   ElfW(Phdr)* vdso_phdr = reinterpret_cast<ElfW(Phdr)*>(vdso_ehdr_addr + vdso_ehdr->e_phoff); | 
 | 81 |   for (size_t i = 0; i < vdso_ehdr->e_phnum; ++i) { | 
 | 82 |     if (vdso_phdr[i].p_type == PT_DYNAMIC) { | 
 | 83 |       vdso_dyn = reinterpret_cast<ElfW(Dyn)*>(vdso_ehdr_addr + vdso_phdr[i].p_offset); | 
 | 84 |     } else if (vdso_phdr[i].p_type == PT_LOAD) { | 
 | 85 |       vdso_addr = vdso_ehdr_addr + vdso_phdr[i].p_offset - vdso_phdr[i].p_vaddr; | 
 | 86 |     } | 
 | 87 |   } | 
| Elliott Hughes | 613f814 | 2015-07-20 22:34:27 +0000 | [diff] [blame] | 88 |   if (vdso_addr == 0 || vdso_dyn == nullptr) { | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 89 |     return; | 
 | 90 |   } | 
 | 91 |  | 
 | 92 |   // Where are the string and symbol tables? | 
| Elliott Hughes | 613f814 | 2015-07-20 22:34:27 +0000 | [diff] [blame] | 93 |   const char* strtab = nullptr; | 
 | 94 |   ElfW(Sym)* symtab = nullptr; | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 95 |   for (ElfW(Dyn)* d = vdso_dyn; d->d_tag != DT_NULL; ++d) { | 
 | 96 |     if (d->d_tag == DT_STRTAB) { | 
 | 97 |       strtab = reinterpret_cast<const char*>(vdso_addr + d->d_un.d_ptr); | 
 | 98 |     } else if (d->d_tag == DT_SYMTAB) { | 
 | 99 |       symtab = reinterpret_cast<ElfW(Sym)*>(vdso_addr + d->d_un.d_ptr); | 
 | 100 |     } | 
 | 101 |   } | 
| Elliott Hughes | 613f814 | 2015-07-20 22:34:27 +0000 | [diff] [blame] | 102 |   if (strtab == nullptr || symtab == nullptr) { | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 103 |     return; | 
 | 104 |   } | 
 | 105 |  | 
 | 106 |   // Are there any symbols we want? | 
 | 107 |   for (size_t i = 0; i < symbol_count; ++i) { | 
 | 108 |     for (size_t j = 0; j < VDSO_END; ++j) { | 
| Josh Gao | 93c0f5e | 2015-10-06 11:08:13 -0700 | [diff] [blame] | 109 |       if (strcmp(vdso[j].name, strtab + symtab[i].st_name) == 0) { | 
 | 110 |         vdso[j].fn = reinterpret_cast<void*>(vdso_addr + symtab[i].st_value); | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 111 |       } | 
 | 112 |     } | 
 | 113 |   } | 
 | 114 | } | 
 | 115 |  | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 116 | #else | 
 | 117 |  | 
| Josh Gao | 93c0f5e | 2015-10-06 11:08:13 -0700 | [diff] [blame] | 118 | void __libc_init_vdso(libc_globals*, KernelArgumentBlock&) { | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 119 | } | 
 | 120 |  | 
 | 121 | #endif |