blob: 44899e7cf12c928d8d73393968e329485dc65761 [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>
23#include <sys/cdefs.h>
24#include <sys/time.h>
Elliott Hughes625993d2014-07-15 16:53:13 -070025#include <time.h>
Josh Gao93c0f5e2015-10-06 11:08:13 -070026#include <unistd.h>
27#include "private/KernelArgumentBlock.h"
Elliott Hughes625993d2014-07-15 16:53:13 -070028
Elliott Hughes9591df52017-11-27 20:30:25 -080029static inline int vdso_return(int result) {
30 if (__predict_true(result == 0)) return 0;
31
32 errno = -result;
33 return -1;
34}
35
Elliott Hughes625993d2014-07-15 16:53:13 -070036int clock_gettime(int clock_id, timespec* tp) {
Josh Gao93c0f5e2015-10-06 11:08:13 -070037 auto vdso_clock_gettime = reinterpret_cast<decltype(&clock_gettime)>(
38 __libc_globals->vdso[VDSO_CLOCK_GETTIME].fn);
39 if (__predict_true(vdso_clock_gettime)) {
Elliott Hughes9591df52017-11-27 20:30:25 -080040 return vdso_return(vdso_clock_gettime(clock_id, tp));
Josh Gao93c0f5e2015-10-06 11:08:13 -070041 }
42 return __clock_gettime(clock_id, tp);
Elliott Hughes625993d2014-07-15 16:53:13 -070043}
44
Mark Salyzyn79249b02017-11-07 08:19:20 -080045int clock_getres(int clock_id, timespec* tp) {
46 auto vdso_clock_getres = reinterpret_cast<decltype(&clock_getres)>(
47 __libc_globals->vdso[VDSO_CLOCK_GETRES].fn);
48 if (__predict_true(vdso_clock_getres)) {
49 return vdso_return(vdso_clock_getres(clock_id, tp));
50 }
51 return __clock_getres(clock_id, tp);
52}
53
Elliott Hughes625993d2014-07-15 16:53:13 -070054int gettimeofday(timeval* tv, struct timezone* tz) {
Josh Gao93c0f5e2015-10-06 11:08:13 -070055 auto vdso_gettimeofday = reinterpret_cast<decltype(&gettimeofday)>(
56 __libc_globals->vdso[VDSO_GETTIMEOFDAY].fn);
57 if (__predict_true(vdso_gettimeofday)) {
Elliott Hughes9591df52017-11-27 20:30:25 -080058 return vdso_return(vdso_gettimeofday(tv, tz));
Josh Gao93c0f5e2015-10-06 11:08:13 -070059 }
60 return __gettimeofday(tv, tz);
Elliott Hughes625993d2014-07-15 16:53:13 -070061}
62
Mark Salyzyn4473ccd2017-12-04 13:51:29 -080063time_t time(time_t* t) {
64 auto vdso_time = reinterpret_cast<decltype(&time)>(
65 __libc_globals->vdso[VDSO_TIME].fn);
66 if (__predict_true(vdso_time)) {
67 return vdso_time(t);
68 }
69 return __time(t);
70}
71
Josh Gao93c0f5e2015-10-06 11:08:13 -070072void __libc_init_vdso(libc_globals* globals, KernelArgumentBlock& args) {
73 auto&& vdso = globals->vdso;
Elliott Hughes9591df52017-11-27 20:30:25 -080074 vdso[VDSO_CLOCK_GETTIME] = { VDSO_CLOCK_GETTIME_SYMBOL, nullptr };
Mark Salyzyn79249b02017-11-07 08:19:20 -080075 vdso[VDSO_CLOCK_GETRES] = { VDSO_CLOCK_GETRES_SYMBOL, nullptr };
Elliott Hughes9591df52017-11-27 20:30:25 -080076 vdso[VDSO_GETTIMEOFDAY] = { VDSO_GETTIMEOFDAY_SYMBOL, nullptr };
Mark Salyzyn4473ccd2017-12-04 13:51:29 -080077 vdso[VDSO_TIME] = { VDSO_TIME_SYMBOL, nullptr };
Josh Gao93c0f5e2015-10-06 11:08:13 -070078
Elliott Hughes625993d2014-07-15 16:53:13 -070079 // Do we have a vdso?
Josh Gao93c0f5e2015-10-06 11:08:13 -070080 uintptr_t vdso_ehdr_addr = args.getauxval(AT_SYSINFO_EHDR);
Elliott Hughes625993d2014-07-15 16:53:13 -070081 ElfW(Ehdr)* vdso_ehdr = reinterpret_cast<ElfW(Ehdr)*>(vdso_ehdr_addr);
Elliott Hughes613f8142015-07-20 22:34:27 +000082 if (vdso_ehdr == nullptr) {
Elliott Hughes625993d2014-07-15 16:53:13 -070083 return;
84 }
85
86 // How many symbols does it have?
87 size_t symbol_count = 0;
88 ElfW(Shdr)* vdso_shdr = reinterpret_cast<ElfW(Shdr)*>(vdso_ehdr_addr + vdso_ehdr->e_shoff);
89 for (size_t i = 0; i < vdso_ehdr->e_shnum; ++i) {
90 if (vdso_shdr[i].sh_type == SHT_DYNSYM) {
91 symbol_count = vdso_shdr[i].sh_size / sizeof(ElfW(Sym));
92 }
93 }
94 if (symbol_count == 0) {
95 return;
96 }
97
98 // Where's the dynamic table?
99 ElfW(Addr) vdso_addr = 0;
Elliott Hughes613f8142015-07-20 22:34:27 +0000100 ElfW(Dyn)* vdso_dyn = nullptr;
Elliott Hughes625993d2014-07-15 16:53:13 -0700101 ElfW(Phdr)* vdso_phdr = reinterpret_cast<ElfW(Phdr)*>(vdso_ehdr_addr + vdso_ehdr->e_phoff);
102 for (size_t i = 0; i < vdso_ehdr->e_phnum; ++i) {
103 if (vdso_phdr[i].p_type == PT_DYNAMIC) {
104 vdso_dyn = reinterpret_cast<ElfW(Dyn)*>(vdso_ehdr_addr + vdso_phdr[i].p_offset);
105 } else if (vdso_phdr[i].p_type == PT_LOAD) {
106 vdso_addr = vdso_ehdr_addr + vdso_phdr[i].p_offset - vdso_phdr[i].p_vaddr;
107 }
108 }
Elliott Hughes613f8142015-07-20 22:34:27 +0000109 if (vdso_addr == 0 || vdso_dyn == nullptr) {
Elliott Hughes625993d2014-07-15 16:53:13 -0700110 return;
111 }
112
113 // Where are the string and symbol tables?
Elliott Hughes613f8142015-07-20 22:34:27 +0000114 const char* strtab = nullptr;
115 ElfW(Sym)* symtab = nullptr;
Elliott Hughes625993d2014-07-15 16:53:13 -0700116 for (ElfW(Dyn)* d = vdso_dyn; d->d_tag != DT_NULL; ++d) {
117 if (d->d_tag == DT_STRTAB) {
118 strtab = reinterpret_cast<const char*>(vdso_addr + d->d_un.d_ptr);
119 } else if (d->d_tag == DT_SYMTAB) {
120 symtab = reinterpret_cast<ElfW(Sym)*>(vdso_addr + d->d_un.d_ptr);
121 }
122 }
Elliott Hughes613f8142015-07-20 22:34:27 +0000123 if (strtab == nullptr || symtab == nullptr) {
Elliott Hughes625993d2014-07-15 16:53:13 -0700124 return;
125 }
126
127 // Are there any symbols we want?
128 for (size_t i = 0; i < symbol_count; ++i) {
129 for (size_t j = 0; j < VDSO_END; ++j) {
Josh Gao93c0f5e2015-10-06 11:08:13 -0700130 if (strcmp(vdso[j].name, strtab + symtab[i].st_name) == 0) {
131 vdso[j].fn = reinterpret_cast<void*>(vdso_addr + symtab[i].st_value);
Elliott Hughes625993d2014-07-15 16:53:13 -0700132 }
133 }
134 }
135}