blob: d4aea986b123913bb6dc58990c270e45d69deacd [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
Daniel Micaydf1a3c62015-07-17 12:13:27 -040033#include <errno.h>
34#include <limits.h>
35#include <sys/mman.h>
Elliott Hughes625993d2014-07-15 16:53:13 -070036#include <time.h>
37
Daniel Micaydf1a3c62015-07-17 12:13:27 -040038#include "private/bionic_prctl.h"
39#include "private/libc_logging.h"
40
Elliott Hughes625993d2014-07-15 16:53:13 -070041extern "C" int __clock_gettime(int, timespec*);
42extern "C" int __gettimeofday(timeval*, struct timezone*);
43
44struct vdso_entry {
45 const char* name;
46 void* fn;
47};
48
49enum {
50 VDSO_CLOCK_GETTIME = 0,
51 VDSO_GETTIMEOFDAY,
52 VDSO_END
53};
54
Daniel Micaydf1a3c62015-07-17 12:13:27 -040055static const vdso_entry vdso_entries_template[] = {
Elliott Hughes625993d2014-07-15 16:53:13 -070056 [VDSO_CLOCK_GETTIME] = { VDSO_CLOCK_GETTIME_SYMBOL, reinterpret_cast<void*>(__clock_gettime) },
57 [VDSO_GETTIMEOFDAY] = { VDSO_GETTIMEOFDAY_SYMBOL, reinterpret_cast<void*>(__gettimeofday) },
58};
59
Daniel Micaydf1a3c62015-07-17 12:13:27 -040060static vdso_entry* vdso_entries;
61
Elliott Hughes625993d2014-07-15 16:53:13 -070062int clock_gettime(int clock_id, timespec* tp) {
Daniel Micaydf1a3c62015-07-17 12:13:27 -040063 int (*vdso_clock_gettime)(int, timespec*) =
Elliott Hughes8b5df392015-01-21 16:19:07 -080064 reinterpret_cast<int (*)(int, timespec*)>(vdso_entries[VDSO_CLOCK_GETTIME].fn);
Elliott Hughes625993d2014-07-15 16:53:13 -070065 return vdso_clock_gettime(clock_id, tp);
66}
67
68int gettimeofday(timeval* tv, struct timezone* tz) {
Daniel Micaydf1a3c62015-07-17 12:13:27 -040069 int (*vdso_gettimeofday)(timeval*, struct timezone*) =
Elliott Hughes8b5df392015-01-21 16:19:07 -080070 reinterpret_cast<int (*)(timeval*, struct timezone*)>(vdso_entries[VDSO_GETTIMEOFDAY].fn);
Elliott Hughes625993d2014-07-15 16:53:13 -070071 return vdso_gettimeofday(tv, tz);
72}
73
Daniel Micaydf1a3c62015-07-17 12:13:27 -040074static void __libc_init_vdso_entries() {
75 // Set up the defaults in case we don't have a vdso or can't find everything we're looking for.
76 memcpy(vdso_entries, vdso_entries_template, sizeof(vdso_entries_template));
77
Elliott Hughes625993d2014-07-15 16:53:13 -070078 // Do we have a vdso?
79 uintptr_t vdso_ehdr_addr = getauxval(AT_SYSINFO_EHDR);
80 ElfW(Ehdr)* vdso_ehdr = reinterpret_cast<ElfW(Ehdr)*>(vdso_ehdr_addr);
Daniel Micaydf1a3c62015-07-17 12:13:27 -040081 if (vdso_ehdr == nullptr) {
Elliott Hughes625993d2014-07-15 16:53:13 -070082 return;
83 }
84
85 // How many symbols does it have?
86 size_t symbol_count = 0;
87 ElfW(Shdr)* vdso_shdr = reinterpret_cast<ElfW(Shdr)*>(vdso_ehdr_addr + vdso_ehdr->e_shoff);
88 for (size_t i = 0; i < vdso_ehdr->e_shnum; ++i) {
89 if (vdso_shdr[i].sh_type == SHT_DYNSYM) {
90 symbol_count = vdso_shdr[i].sh_size / sizeof(ElfW(Sym));
91 }
92 }
93 if (symbol_count == 0) {
94 return;
95 }
96
97 // Where's the dynamic table?
98 ElfW(Addr) vdso_addr = 0;
Daniel Micaydf1a3c62015-07-17 12:13:27 -040099 ElfW(Dyn)* vdso_dyn = nullptr;
Elliott Hughes625993d2014-07-15 16:53:13 -0700100 ElfW(Phdr)* vdso_phdr = reinterpret_cast<ElfW(Phdr)*>(vdso_ehdr_addr + vdso_ehdr->e_phoff);
101 for (size_t i = 0; i < vdso_ehdr->e_phnum; ++i) {
102 if (vdso_phdr[i].p_type == PT_DYNAMIC) {
103 vdso_dyn = reinterpret_cast<ElfW(Dyn)*>(vdso_ehdr_addr + vdso_phdr[i].p_offset);
104 } else if (vdso_phdr[i].p_type == PT_LOAD) {
105 vdso_addr = vdso_ehdr_addr + vdso_phdr[i].p_offset - vdso_phdr[i].p_vaddr;
106 }
107 }
Daniel Micaydf1a3c62015-07-17 12:13:27 -0400108 if (vdso_addr == 0 || vdso_dyn == nullptr) {
Elliott Hughes625993d2014-07-15 16:53:13 -0700109 return;
110 }
111
112 // Where are the string and symbol tables?
Daniel Micaydf1a3c62015-07-17 12:13:27 -0400113 const char* strtab = nullptr;
114 ElfW(Sym)* symtab = nullptr;
Elliott Hughes625993d2014-07-15 16:53:13 -0700115 for (ElfW(Dyn)* d = vdso_dyn; d->d_tag != DT_NULL; ++d) {
116 if (d->d_tag == DT_STRTAB) {
117 strtab = reinterpret_cast<const char*>(vdso_addr + d->d_un.d_ptr);
118 } else if (d->d_tag == DT_SYMTAB) {
119 symtab = reinterpret_cast<ElfW(Sym)*>(vdso_addr + d->d_un.d_ptr);
120 }
121 }
Daniel Micaydf1a3c62015-07-17 12:13:27 -0400122 if (strtab == nullptr || symtab == nullptr) {
Elliott Hughes625993d2014-07-15 16:53:13 -0700123 return;
124 }
125
126 // Are there any symbols we want?
127 for (size_t i = 0; i < symbol_count; ++i) {
128 for (size_t j = 0; j < VDSO_END; ++j) {
129 if (strcmp(vdso_entries[j].name, strtab + symtab[i].st_name) == 0) {
130 vdso_entries[j].fn = reinterpret_cast<void*>(vdso_addr + symtab[i].st_value);
131 }
132 }
133 }
134}
135
Daniel Micaydf1a3c62015-07-17 12:13:27 -0400136void __libc_init_vdso() {
137 static_assert(PAGE_SIZE >= sizeof(vdso_entries_template), "vdso_entries_template too large");
138 vdso_entries = reinterpret_cast<vdso_entry*>(mmap(nullptr, sizeof(vdso_entries_template), PROT_READ|PROT_WRITE,
139 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0));
140 if (vdso_entries == MAP_FAILED) {
141 __libc_fatal("failed to allocate vdso function pointer table: %s", strerror(errno));
142 }
143 __libc_init_vdso_entries();
144 if (mprotect(vdso_entries, sizeof(vdso_entries_template), PROT_READ) == -1) {
145 __libc_fatal("failed to mprotect PROT_READ vdso function pointer table: %s", strerror(errno));
146 }
147 prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, vdso_entries, sizeof(vdso_entries_template), "vdso function pointer table");
148}
149
Elliott Hughes625993d2014-07-15 16:53:13 -0700150#else
151
152void __libc_init_vdso() {
153}
154
155#endif