blob: 029c1481cd256fd09b5c475a6eed6270c4ca8845 [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
Robert Jarzmik10726d52015-07-15 15:26:43 +020020#if defined(__aarch64__) || defined(__x86_64__) || defined (__i386__)
Elliott Hughes625993d2014-07-15 16:53:13 -070021
Elliott Hughes613f8142015-07-20 22:34:27 +000022#include <limits.h>
Josh Gao93c0f5e2015-10-06 11:08:13 -070023#include <link.h>
24#include <string.h>
25#include <sys/cdefs.h>
26#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>
29#include "private/KernelArgumentBlock.h"
Elliott Hughes625993d2014-07-15 16:53:13 -070030
31int clock_gettime(int clock_id, timespec* tp) {
Josh Gao93c0f5e2015-10-06 11:08:13 -070032 auto vdso_clock_gettime = reinterpret_cast<decltype(&clock_gettime)>(
33 __libc_globals->vdso[VDSO_CLOCK_GETTIME].fn);
34 if (__predict_true(vdso_clock_gettime)) {
35 return vdso_clock_gettime(clock_id, tp);
36 }
37 return __clock_gettime(clock_id, tp);
Elliott Hughes625993d2014-07-15 16:53:13 -070038}
39
40int gettimeofday(timeval* tv, struct timezone* tz) {
Josh Gao93c0f5e2015-10-06 11:08:13 -070041 auto vdso_gettimeofday = reinterpret_cast<decltype(&gettimeofday)>(
42 __libc_globals->vdso[VDSO_GETTIMEOFDAY].fn);
43 if (__predict_true(vdso_gettimeofday)) {
44 return vdso_gettimeofday(tv, tz);
45 }
46 return __gettimeofday(tv, tz);
Elliott Hughes625993d2014-07-15 16:53:13 -070047}
48
Josh Gao93c0f5e2015-10-06 11:08:13 -070049void __libc_init_vdso(libc_globals* globals, KernelArgumentBlock& args) {
50 auto&& vdso = globals->vdso;
51 vdso[VDSO_CLOCK_GETTIME] = { VDSO_CLOCK_GETTIME_SYMBOL,
52 reinterpret_cast<void*>(__clock_gettime) };
53 vdso[VDSO_GETTIMEOFDAY] = { VDSO_GETTIMEOFDAY_SYMBOL,
54 reinterpret_cast<void*>(__gettimeofday) };
55
Elliott Hughes625993d2014-07-15 16:53:13 -070056 // Do we have a vdso?
Josh Gao93c0f5e2015-10-06 11:08:13 -070057 uintptr_t vdso_ehdr_addr = args.getauxval(AT_SYSINFO_EHDR);
Elliott Hughes625993d2014-07-15 16:53:13 -070058 ElfW(Ehdr)* vdso_ehdr = reinterpret_cast<ElfW(Ehdr)*>(vdso_ehdr_addr);
Elliott Hughes613f8142015-07-20 22:34:27 +000059 if (vdso_ehdr == nullptr) {
Elliott Hughes625993d2014-07-15 16:53:13 -070060 return;
61 }
62
63 // How many symbols does it have?
64 size_t symbol_count = 0;
65 ElfW(Shdr)* vdso_shdr = reinterpret_cast<ElfW(Shdr)*>(vdso_ehdr_addr + vdso_ehdr->e_shoff);
66 for (size_t i = 0; i < vdso_ehdr->e_shnum; ++i) {
67 if (vdso_shdr[i].sh_type == SHT_DYNSYM) {
68 symbol_count = vdso_shdr[i].sh_size / sizeof(ElfW(Sym));
69 }
70 }
71 if (symbol_count == 0) {
72 return;
73 }
74
75 // Where's the dynamic table?
76 ElfW(Addr) vdso_addr = 0;
Elliott Hughes613f8142015-07-20 22:34:27 +000077 ElfW(Dyn)* vdso_dyn = nullptr;
Elliott Hughes625993d2014-07-15 16:53:13 -070078 ElfW(Phdr)* vdso_phdr = reinterpret_cast<ElfW(Phdr)*>(vdso_ehdr_addr + vdso_ehdr->e_phoff);
79 for (size_t i = 0; i < vdso_ehdr->e_phnum; ++i) {
80 if (vdso_phdr[i].p_type == PT_DYNAMIC) {
81 vdso_dyn = reinterpret_cast<ElfW(Dyn)*>(vdso_ehdr_addr + vdso_phdr[i].p_offset);
82 } else if (vdso_phdr[i].p_type == PT_LOAD) {
83 vdso_addr = vdso_ehdr_addr + vdso_phdr[i].p_offset - vdso_phdr[i].p_vaddr;
84 }
85 }
Elliott Hughes613f8142015-07-20 22:34:27 +000086 if (vdso_addr == 0 || vdso_dyn == nullptr) {
Elliott Hughes625993d2014-07-15 16:53:13 -070087 return;
88 }
89
90 // Where are the string and symbol tables?
Elliott Hughes613f8142015-07-20 22:34:27 +000091 const char* strtab = nullptr;
92 ElfW(Sym)* symtab = nullptr;
Elliott Hughes625993d2014-07-15 16:53:13 -070093 for (ElfW(Dyn)* d = vdso_dyn; d->d_tag != DT_NULL; ++d) {
94 if (d->d_tag == DT_STRTAB) {
95 strtab = reinterpret_cast<const char*>(vdso_addr + d->d_un.d_ptr);
96 } else if (d->d_tag == DT_SYMTAB) {
97 symtab = reinterpret_cast<ElfW(Sym)*>(vdso_addr + d->d_un.d_ptr);
98 }
99 }
Elliott Hughes613f8142015-07-20 22:34:27 +0000100 if (strtab == nullptr || symtab == nullptr) {
Elliott Hughes625993d2014-07-15 16:53:13 -0700101 return;
102 }
103
104 // Are there any symbols we want?
105 for (size_t i = 0; i < symbol_count; ++i) {
106 for (size_t j = 0; j < VDSO_END; ++j) {
Josh Gao93c0f5e2015-10-06 11:08:13 -0700107 if (strcmp(vdso[j].name, strtab + symtab[i].st_name) == 0) {
108 vdso[j].fn = reinterpret_cast<void*>(vdso_addr + symtab[i].st_value);
Elliott Hughes625993d2014-07-15 16:53:13 -0700109 }
110 }
111 }
112}
113
Elliott Hughes625993d2014-07-15 16:53:13 -0700114#else
115
Josh Gao93c0f5e2015-10-06 11:08:13 -0700116void __libc_init_vdso(libc_globals*, KernelArgumentBlock&) {
Elliott Hughes625993d2014-07-15 16:53:13 -0700117}
118
119#endif