• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 "private/bionic_globals.h"
18 #include "private/bionic_vdso.h"
19 
20 #include <limits.h>
21 #include <link.h>
22 #include <string.h>
23 #include <sys/auxv.h>
24 #include <sys/cdefs.h>
25 #include <sys/time.h>
26 #include <time.h>
27 #include <unistd.h>
28 
vdso_return(int result)29 static inline int vdso_return(int result) {
30   if (__predict_true(result == 0)) return 0;
31 
32   errno = -result;
33   return -1;
34 }
35 
clock_gettime(int clock_id,timespec * tp)36 int clock_gettime(int clock_id, timespec* tp) {
37   auto vdso_clock_gettime = reinterpret_cast<decltype(&clock_gettime)>(
38     __libc_globals->vdso[VDSO_CLOCK_GETTIME].fn);
39   if (__predict_true(vdso_clock_gettime)) {
40     return vdso_return(vdso_clock_gettime(clock_id, tp));
41   }
42   return __clock_gettime(clock_id, tp);
43 }
44 
clock_getres(int clock_id,timespec * tp)45 int 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 
gettimeofday(timeval * tv,struct timezone * tz)54 int gettimeofday(timeval* tv, struct timezone* tz) {
55   auto vdso_gettimeofday = reinterpret_cast<decltype(&gettimeofday)>(
56     __libc_globals->vdso[VDSO_GETTIMEOFDAY].fn);
57   if (__predict_true(vdso_gettimeofday)) {
58     return vdso_return(vdso_gettimeofday(tv, tz));
59   }
60   return __gettimeofday(tv, tz);
61 }
62 
time(time_t * t)63 time_t time(time_t* t) {
64   auto vdso_time = reinterpret_cast<decltype(&time)>(__libc_globals->vdso[VDSO_TIME].fn);
65   if (__predict_true(vdso_time)) {
66     return vdso_time(t);
67   }
68 
69   // We can't fallback to the time(2) system call because it doesn't exist for most architectures.
70   timeval tv;
71   if (gettimeofday(&tv, nullptr) == -1) return -1;
72   if (t) *t = tv.tv_sec;
73   return tv.tv_sec;
74 }
75 
__libc_init_vdso(libc_globals * globals)76 void __libc_init_vdso(libc_globals* globals) {
77   auto&& vdso = globals->vdso;
78   vdso[VDSO_CLOCK_GETTIME] = { VDSO_CLOCK_GETTIME_SYMBOL, nullptr };
79   vdso[VDSO_CLOCK_GETRES] = { VDSO_CLOCK_GETRES_SYMBOL, nullptr };
80   vdso[VDSO_GETTIMEOFDAY] = { VDSO_GETTIMEOFDAY_SYMBOL, nullptr };
81   vdso[VDSO_TIME] = { VDSO_TIME_SYMBOL, nullptr };
82 
83   // Do we have a vdso?
84   uintptr_t vdso_ehdr_addr = getauxval(AT_SYSINFO_EHDR);
85   ElfW(Ehdr)* vdso_ehdr = reinterpret_cast<ElfW(Ehdr)*>(vdso_ehdr_addr);
86   if (vdso_ehdr == nullptr) {
87     return;
88   }
89 
90   // How many symbols does it have?
91   size_t symbol_count = 0;
92   ElfW(Shdr)* vdso_shdr = reinterpret_cast<ElfW(Shdr)*>(vdso_ehdr_addr + vdso_ehdr->e_shoff);
93   for (size_t i = 0; i < vdso_ehdr->e_shnum; ++i) {
94     if (vdso_shdr[i].sh_type == SHT_DYNSYM) {
95       symbol_count = vdso_shdr[i].sh_size / sizeof(ElfW(Sym));
96     }
97   }
98   if (symbol_count == 0) {
99     return;
100   }
101 
102   // Where's the dynamic table?
103   ElfW(Addr) vdso_addr = 0;
104   ElfW(Dyn)* vdso_dyn = nullptr;
105   ElfW(Phdr)* vdso_phdr = reinterpret_cast<ElfW(Phdr)*>(vdso_ehdr_addr + vdso_ehdr->e_phoff);
106   for (size_t i = 0; i < vdso_ehdr->e_phnum; ++i) {
107     if (vdso_phdr[i].p_type == PT_DYNAMIC) {
108       vdso_dyn = reinterpret_cast<ElfW(Dyn)*>(vdso_ehdr_addr + vdso_phdr[i].p_offset);
109     } else if (vdso_phdr[i].p_type == PT_LOAD) {
110       vdso_addr = vdso_ehdr_addr + vdso_phdr[i].p_offset - vdso_phdr[i].p_vaddr;
111     }
112   }
113   if (vdso_addr == 0 || vdso_dyn == nullptr) {
114     return;
115   }
116 
117   // Where are the string and symbol tables?
118   const char* strtab = nullptr;
119   ElfW(Sym)* symtab = nullptr;
120   for (ElfW(Dyn)* d = vdso_dyn; d->d_tag != DT_NULL; ++d) {
121     if (d->d_tag == DT_STRTAB) {
122       strtab = reinterpret_cast<const char*>(vdso_addr + d->d_un.d_ptr);
123     } else if (d->d_tag == DT_SYMTAB) {
124       symtab = reinterpret_cast<ElfW(Sym)*>(vdso_addr + d->d_un.d_ptr);
125     }
126   }
127   if (strtab == nullptr || symtab == nullptr) {
128     return;
129   }
130 
131   // Are there any symbols we want?
132   for (size_t i = 0; i < symbol_count; ++i) {
133     for (size_t j = 0; j < VDSO_END; ++j) {
134       if (strcmp(vdso[j].name, strtab + symtab[i].st_name) == 0) {
135         vdso[j].fn = reinterpret_cast<void*>(vdso_addr + symtab[i].st_value);
136       }
137     }
138   }
139 }
140