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
17 #include <link.h>
18 #include <sys/auxv.h>
19 #include <unistd.h>
20
21 // x86 has a vdso, but there's nothing useful to us in it.
22 #if defined(__aarch64__) || defined(__x86_64__)
23
24 #if defined(__aarch64__)
25 #define VDSO_CLOCK_GETTIME_SYMBOL "__kernel_clock_gettime"
26 #define VDSO_GETTIMEOFDAY_SYMBOL "__kernel_gettimeofday"
27 #elif defined(__x86_64__)
28 #define VDSO_CLOCK_GETTIME_SYMBOL "__vdso_clock_gettime"
29 #define VDSO_GETTIMEOFDAY_SYMBOL "__vdso_gettimeofday"
30 #endif
31
32 #include <time.h>
33
34 extern "C" int __clock_gettime(int, timespec*);
35 extern "C" int __gettimeofday(timeval*, struct timezone*);
36
37 struct vdso_entry {
38 const char* name;
39 void* fn;
40 };
41
42 enum {
43 VDSO_CLOCK_GETTIME = 0,
44 VDSO_GETTIMEOFDAY,
45 VDSO_END
46 };
47
48 static vdso_entry vdso_entries[] = {
49 [VDSO_CLOCK_GETTIME] = { VDSO_CLOCK_GETTIME_SYMBOL, reinterpret_cast<void*>(__clock_gettime) },
50 [VDSO_GETTIMEOFDAY] = { VDSO_GETTIMEOFDAY_SYMBOL, reinterpret_cast<void*>(__gettimeofday) },
51 };
52
clock_gettime(int clock_id,timespec * tp)53 int clock_gettime(int clock_id, timespec* tp) {
54 static int (*vdso_clock_gettime)(int, timespec*) =
55 (int (*)(int, timespec*)) vdso_entries[VDSO_CLOCK_GETTIME].fn;
56 return vdso_clock_gettime(clock_id, tp);
57 }
58
gettimeofday(timeval * tv,struct timezone * tz)59 int gettimeofday(timeval* tv, struct timezone* tz) {
60 static int (*vdso_gettimeofday)(timeval*, struct timezone*) =
61 (int (*)(timeval*, struct timezone*)) vdso_entries[VDSO_GETTIMEOFDAY].fn;
62 return vdso_gettimeofday(tv, tz);
63 }
64
__libc_init_vdso()65 void __libc_init_vdso() {
66 // Do we have a vdso?
67 uintptr_t vdso_ehdr_addr = getauxval(AT_SYSINFO_EHDR);
68 ElfW(Ehdr)* vdso_ehdr = reinterpret_cast<ElfW(Ehdr)*>(vdso_ehdr_addr);
69 if (vdso_ehdr == NULL) {
70 return;
71 }
72
73 // How many symbols does it have?
74 size_t symbol_count = 0;
75 ElfW(Shdr)* vdso_shdr = reinterpret_cast<ElfW(Shdr)*>(vdso_ehdr_addr + vdso_ehdr->e_shoff);
76 for (size_t i = 0; i < vdso_ehdr->e_shnum; ++i) {
77 if (vdso_shdr[i].sh_type == SHT_DYNSYM) {
78 symbol_count = vdso_shdr[i].sh_size / sizeof(ElfW(Sym));
79 }
80 }
81 if (symbol_count == 0) {
82 return;
83 }
84
85 // Where's the dynamic table?
86 ElfW(Addr) vdso_addr = 0;
87 ElfW(Dyn)* vdso_dyn = NULL;
88 ElfW(Phdr)* vdso_phdr = reinterpret_cast<ElfW(Phdr)*>(vdso_ehdr_addr + vdso_ehdr->e_phoff);
89 for (size_t i = 0; i < vdso_ehdr->e_phnum; ++i) {
90 if (vdso_phdr[i].p_type == PT_DYNAMIC) {
91 vdso_dyn = reinterpret_cast<ElfW(Dyn)*>(vdso_ehdr_addr + vdso_phdr[i].p_offset);
92 } else if (vdso_phdr[i].p_type == PT_LOAD) {
93 vdso_addr = vdso_ehdr_addr + vdso_phdr[i].p_offset - vdso_phdr[i].p_vaddr;
94 }
95 }
96 if (vdso_addr == 0 || vdso_dyn == NULL) {
97 return;
98 }
99
100 // Where are the string and symbol tables?
101 const char* strtab = NULL;
102 ElfW(Sym)* symtab = NULL;
103 for (ElfW(Dyn)* d = vdso_dyn; d->d_tag != DT_NULL; ++d) {
104 if (d->d_tag == DT_STRTAB) {
105 strtab = reinterpret_cast<const char*>(vdso_addr + d->d_un.d_ptr);
106 } else if (d->d_tag == DT_SYMTAB) {
107 symtab = reinterpret_cast<ElfW(Sym)*>(vdso_addr + d->d_un.d_ptr);
108 }
109 }
110 if (strtab == NULL || symtab == NULL) {
111 return;
112 }
113
114 // Are there any symbols we want?
115 for (size_t i = 0; i < symbol_count; ++i) {
116 for (size_t j = 0; j < VDSO_END; ++j) {
117 if (strcmp(vdso_entries[j].name, strtab + symtab[i].st_name) == 0) {
118 vdso_entries[j].fn = reinterpret_cast<void*>(vdso_addr + symtab[i].st_value);
119 }
120 }
121 }
122 }
123
124 #else
125
__libc_init_vdso()126 void __libc_init_vdso() {
127 }
128
129 #endif
130