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