blob: e834ec77f76502fcb271cd51d2da6f04883d383a [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
Josh Gao93c0f5e2015-10-06 11:08:13 -070017#include "private/bionic_globals.h"
18#include "private/bionic_vdso.h"
Elliott Hughes625993d2014-07-15 16:53:13 -070019
Elliott Hughes613f8142015-07-20 22:34:27 +000020#include <limits.h>
Josh Gao93c0f5e2015-10-06 11:08:13 -070021#include <link.h>
22#include <string.h>
Ryan Prichard746ad152018-11-22 03:16:06 -080023#include <sys/auxv.h>
Josh Gao93c0f5e2015-10-06 11:08:13 -070024#include <sys/cdefs.h>
Elliott Hughes9a7d0482023-07-28 17:18:48 -070025#include <sys/hwprobe.h>
Josh Gao93c0f5e2015-10-06 11:08:13 -070026#include <sys/time.h>
Elliott Hughes625993d2014-07-15 16:53:13 -070027#include <time.h>
Josh Gao93c0f5e2015-10-06 11:08:13 -070028#include <unistd.h>
Elliott Hughes625993d2014-07-15 16:53:13 -070029
Elliott Hughes9a7d0482023-07-28 17:18:48 -070030extern "C" int __clock_gettime(int, struct timespec*);
31extern "C" int __clock_getres(int, struct timespec*);
32extern "C" int __gettimeofday(struct timeval*, struct timezone*);
33extern "C" int riscv_hwprobe(struct riscv_hwprobe*, size_t, size_t, unsigned long*, unsigned);
34
Elliott Hughes9591df52017-11-27 20:30:25 -080035static inline int vdso_return(int result) {
36 if (__predict_true(result == 0)) return 0;
37
38 errno = -result;
39 return -1;
40}
41
Elliott Hughes625993d2014-07-15 16:53:13 -070042int clock_gettime(int clock_id, timespec* tp) {
Josh Gao93c0f5e2015-10-06 11:08:13 -070043 auto vdso_clock_gettime = reinterpret_cast<decltype(&clock_gettime)>(
44 __libc_globals->vdso[VDSO_CLOCK_GETTIME].fn);
45 if (__predict_true(vdso_clock_gettime)) {
Elliott Hughes9591df52017-11-27 20:30:25 -080046 return vdso_return(vdso_clock_gettime(clock_id, tp));
Josh Gao93c0f5e2015-10-06 11:08:13 -070047 }
48 return __clock_gettime(clock_id, tp);
Elliott Hughes625993d2014-07-15 16:53:13 -070049}
50
Mark Salyzyn79249b02017-11-07 08:19:20 -080051int clock_getres(int clock_id, timespec* tp) {
52 auto vdso_clock_getres = reinterpret_cast<decltype(&clock_getres)>(
53 __libc_globals->vdso[VDSO_CLOCK_GETRES].fn);
54 if (__predict_true(vdso_clock_getres)) {
55 return vdso_return(vdso_clock_getres(clock_id, tp));
56 }
57 return __clock_getres(clock_id, tp);
58}
59
Elliott Hughes625993d2014-07-15 16:53:13 -070060int gettimeofday(timeval* tv, struct timezone* tz) {
Josh Gao93c0f5e2015-10-06 11:08:13 -070061 auto vdso_gettimeofday = reinterpret_cast<decltype(&gettimeofday)>(
62 __libc_globals->vdso[VDSO_GETTIMEOFDAY].fn);
63 if (__predict_true(vdso_gettimeofday)) {
Elliott Hughes9591df52017-11-27 20:30:25 -080064 return vdso_return(vdso_gettimeofday(tv, tz));
Josh Gao93c0f5e2015-10-06 11:08:13 -070065 }
66 return __gettimeofday(tv, tz);
Elliott Hughes625993d2014-07-15 16:53:13 -070067}
68
Mark Salyzyn4473ccd2017-12-04 13:51:29 -080069time_t time(time_t* t) {
Elliott Hughes9a7d0482023-07-28 17:18:48 -070070 // Only x86/x86-64 actually have time() in the vdso.
71#if defined(VDSO_TIME_SYMBOL)
Elliott Hughesf1515f62018-01-12 15:20:28 -080072 auto vdso_time = reinterpret_cast<decltype(&time)>(__libc_globals->vdso[VDSO_TIME].fn);
Mark Salyzyn4473ccd2017-12-04 13:51:29 -080073 if (__predict_true(vdso_time)) {
74 return vdso_time(t);
75 }
Elliott Hughes9a7d0482023-07-28 17:18:48 -070076#endif
Elliott Hughesf1515f62018-01-12 15:20:28 -080077
78 // We can't fallback to the time(2) system call because it doesn't exist for most architectures.
79 timeval tv;
80 if (gettimeofday(&tv, nullptr) == -1) return -1;
81 if (t) *t = tv.tv_sec;
82 return tv.tv_sec;
Mark Salyzyn4473ccd2017-12-04 13:51:29 -080083}
84
Elliott Hughes9a7d0482023-07-28 17:18:48 -070085#if defined(__riscv)
86int __riscv_hwprobe(struct riscv_hwprobe* _Nonnull pairs, size_t pair_count, size_t cpu_count,
87 unsigned long* _Nullable cpus, unsigned flags) {
88 auto vdso_riscv_hwprobe =
89 reinterpret_cast<decltype(&__riscv_hwprobe)>(__libc_globals->vdso[VDSO_RISCV_HWPROBE].fn);
90 if (__predict_true(vdso_riscv_hwprobe)) {
91 return vdso_return(vdso_riscv_hwprobe(pairs, pair_count, cpu_count, cpus, flags));
92 }
93 return riscv_hwprobe(pairs, pair_count, cpu_count, cpus, flags);
94}
95#endif
96
Ryan Prichard746ad152018-11-22 03:16:06 -080097void __libc_init_vdso(libc_globals* globals) {
Josh Gao93c0f5e2015-10-06 11:08:13 -070098 auto&& vdso = globals->vdso;
Elliott Hughes9a7d0482023-07-28 17:18:48 -070099 vdso[VDSO_CLOCK_GETTIME] = {VDSO_CLOCK_GETTIME_SYMBOL, nullptr};
100 vdso[VDSO_CLOCK_GETRES] = {VDSO_CLOCK_GETRES_SYMBOL, nullptr};
101 vdso[VDSO_GETTIMEOFDAY] = {VDSO_GETTIMEOFDAY_SYMBOL, nullptr};
102#if defined(VDSO_TIME_SYMBOL)
103 vdso[VDSO_TIME] = {VDSO_TIME_SYMBOL, nullptr};
104#endif
105#if defined(VDSO_RISCV_HWPROBE_SYMBOL)
106 vdso[VDSO_RISCV_HWPROBE] = {VDSO_RISCV_HWPROBE_SYMBOL, nullptr};
107#endif
Josh Gao93c0f5e2015-10-06 11:08:13 -0700108
Elliott Hughes625993d2014-07-15 16:53:13 -0700109 // Do we have a vdso?
Ryan Prichard746ad152018-11-22 03:16:06 -0800110 uintptr_t vdso_ehdr_addr = getauxval(AT_SYSINFO_EHDR);
Elliott Hughes625993d2014-07-15 16:53:13 -0700111 ElfW(Ehdr)* vdso_ehdr = reinterpret_cast<ElfW(Ehdr)*>(vdso_ehdr_addr);
Elliott Hughes613f8142015-07-20 22:34:27 +0000112 if (vdso_ehdr == nullptr) {
Elliott Hughes625993d2014-07-15 16:53:13 -0700113 return;
114 }
115
116 // How many symbols does it have?
117 size_t symbol_count = 0;
118 ElfW(Shdr)* vdso_shdr = reinterpret_cast<ElfW(Shdr)*>(vdso_ehdr_addr + vdso_ehdr->e_shoff);
119 for (size_t i = 0; i < vdso_ehdr->e_shnum; ++i) {
120 if (vdso_shdr[i].sh_type == SHT_DYNSYM) {
121 symbol_count = vdso_shdr[i].sh_size / sizeof(ElfW(Sym));
122 }
123 }
124 if (symbol_count == 0) {
125 return;
126 }
127
128 // Where's the dynamic table?
129 ElfW(Addr) vdso_addr = 0;
Elliott Hughes613f8142015-07-20 22:34:27 +0000130 ElfW(Dyn)* vdso_dyn = nullptr;
Elliott Hughes625993d2014-07-15 16:53:13 -0700131 ElfW(Phdr)* vdso_phdr = reinterpret_cast<ElfW(Phdr)*>(vdso_ehdr_addr + vdso_ehdr->e_phoff);
132 for (size_t i = 0; i < vdso_ehdr->e_phnum; ++i) {
133 if (vdso_phdr[i].p_type == PT_DYNAMIC) {
134 vdso_dyn = reinterpret_cast<ElfW(Dyn)*>(vdso_ehdr_addr + vdso_phdr[i].p_offset);
135 } else if (vdso_phdr[i].p_type == PT_LOAD) {
136 vdso_addr = vdso_ehdr_addr + vdso_phdr[i].p_offset - vdso_phdr[i].p_vaddr;
137 }
138 }
Elliott Hughes613f8142015-07-20 22:34:27 +0000139 if (vdso_addr == 0 || vdso_dyn == nullptr) {
Elliott Hughes625993d2014-07-15 16:53:13 -0700140 return;
141 }
142
143 // Where are the string and symbol tables?
Elliott Hughes613f8142015-07-20 22:34:27 +0000144 const char* strtab = nullptr;
145 ElfW(Sym)* symtab = nullptr;
Elliott Hughes625993d2014-07-15 16:53:13 -0700146 for (ElfW(Dyn)* d = vdso_dyn; d->d_tag != DT_NULL; ++d) {
147 if (d->d_tag == DT_STRTAB) {
148 strtab = reinterpret_cast<const char*>(vdso_addr + d->d_un.d_ptr);
149 } else if (d->d_tag == DT_SYMTAB) {
150 symtab = reinterpret_cast<ElfW(Sym)*>(vdso_addr + d->d_un.d_ptr);
151 }
152 }
Elliott Hughes613f8142015-07-20 22:34:27 +0000153 if (strtab == nullptr || symtab == nullptr) {
Elliott Hughes625993d2014-07-15 16:53:13 -0700154 return;
155 }
156
157 // Are there any symbols we want?
158 for (size_t i = 0; i < symbol_count; ++i) {
159 for (size_t j = 0; j < VDSO_END; ++j) {
Josh Gao93c0f5e2015-10-06 11:08:13 -0700160 if (strcmp(vdso[j].name, strtab + symtab[i].st_name) == 0) {
161 vdso[j].fn = reinterpret_cast<void*>(vdso_addr + symtab[i].st_value);
Elliott Hughes625993d2014-07-15 16:53:13 -0700162 }
163 }
164 }
165}