2014-07-16 01:53:13 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2015-10-06 20:08:13 +02:00
|
|
|
#include "private/bionic_globals.h"
|
|
|
|
#include "private/bionic_vdso.h"
|
2014-07-16 01:53:13 +02:00
|
|
|
|
2015-07-15 15:26:43 +02:00
|
|
|
#if defined(__aarch64__) || defined(__x86_64__) || defined (__i386__)
|
2014-07-16 01:53:13 +02:00
|
|
|
|
2015-07-21 00:34:27 +02:00
|
|
|
#include <limits.h>
|
2015-10-06 20:08:13 +02:00
|
|
|
#include <link.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
#include <sys/time.h>
|
2014-07-16 01:53:13 +02:00
|
|
|
#include <time.h>
|
2015-10-06 20:08:13 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
#include "private/KernelArgumentBlock.h"
|
2014-07-16 01:53:13 +02:00
|
|
|
|
|
|
|
int clock_gettime(int clock_id, timespec* tp) {
|
2015-10-06 20:08:13 +02:00
|
|
|
auto vdso_clock_gettime = reinterpret_cast<decltype(&clock_gettime)>(
|
|
|
|
__libc_globals->vdso[VDSO_CLOCK_GETTIME].fn);
|
|
|
|
if (__predict_true(vdso_clock_gettime)) {
|
|
|
|
return vdso_clock_gettime(clock_id, tp);
|
|
|
|
}
|
|
|
|
return __clock_gettime(clock_id, tp);
|
2014-07-16 01:53:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int gettimeofday(timeval* tv, struct timezone* tz) {
|
2015-10-06 20:08:13 +02:00
|
|
|
auto vdso_gettimeofday = reinterpret_cast<decltype(&gettimeofday)>(
|
|
|
|
__libc_globals->vdso[VDSO_GETTIMEOFDAY].fn);
|
|
|
|
if (__predict_true(vdso_gettimeofday)) {
|
|
|
|
return vdso_gettimeofday(tv, tz);
|
|
|
|
}
|
|
|
|
return __gettimeofday(tv, tz);
|
2014-07-16 01:53:13 +02:00
|
|
|
}
|
|
|
|
|
2015-10-06 20:08:13 +02:00
|
|
|
void __libc_init_vdso(libc_globals* globals, KernelArgumentBlock& args) {
|
|
|
|
auto&& vdso = globals->vdso;
|
|
|
|
vdso[VDSO_CLOCK_GETTIME] = { VDSO_CLOCK_GETTIME_SYMBOL,
|
|
|
|
reinterpret_cast<void*>(__clock_gettime) };
|
|
|
|
vdso[VDSO_GETTIMEOFDAY] = { VDSO_GETTIMEOFDAY_SYMBOL,
|
|
|
|
reinterpret_cast<void*>(__gettimeofday) };
|
|
|
|
|
2014-07-16 01:53:13 +02:00
|
|
|
// Do we have a vdso?
|
2015-10-06 20:08:13 +02:00
|
|
|
uintptr_t vdso_ehdr_addr = args.getauxval(AT_SYSINFO_EHDR);
|
2014-07-16 01:53:13 +02:00
|
|
|
ElfW(Ehdr)* vdso_ehdr = reinterpret_cast<ElfW(Ehdr)*>(vdso_ehdr_addr);
|
2015-07-21 00:34:27 +02:00
|
|
|
if (vdso_ehdr == nullptr) {
|
2014-07-16 01:53:13 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// How many symbols does it have?
|
|
|
|
size_t symbol_count = 0;
|
|
|
|
ElfW(Shdr)* vdso_shdr = reinterpret_cast<ElfW(Shdr)*>(vdso_ehdr_addr + vdso_ehdr->e_shoff);
|
|
|
|
for (size_t i = 0; i < vdso_ehdr->e_shnum; ++i) {
|
|
|
|
if (vdso_shdr[i].sh_type == SHT_DYNSYM) {
|
|
|
|
symbol_count = vdso_shdr[i].sh_size / sizeof(ElfW(Sym));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (symbol_count == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Where's the dynamic table?
|
|
|
|
ElfW(Addr) vdso_addr = 0;
|
2015-07-21 00:34:27 +02:00
|
|
|
ElfW(Dyn)* vdso_dyn = nullptr;
|
2014-07-16 01:53:13 +02:00
|
|
|
ElfW(Phdr)* vdso_phdr = reinterpret_cast<ElfW(Phdr)*>(vdso_ehdr_addr + vdso_ehdr->e_phoff);
|
|
|
|
for (size_t i = 0; i < vdso_ehdr->e_phnum; ++i) {
|
|
|
|
if (vdso_phdr[i].p_type == PT_DYNAMIC) {
|
|
|
|
vdso_dyn = reinterpret_cast<ElfW(Dyn)*>(vdso_ehdr_addr + vdso_phdr[i].p_offset);
|
|
|
|
} else if (vdso_phdr[i].p_type == PT_LOAD) {
|
|
|
|
vdso_addr = vdso_ehdr_addr + vdso_phdr[i].p_offset - vdso_phdr[i].p_vaddr;
|
|
|
|
}
|
|
|
|
}
|
2015-07-21 00:34:27 +02:00
|
|
|
if (vdso_addr == 0 || vdso_dyn == nullptr) {
|
2014-07-16 01:53:13 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Where are the string and symbol tables?
|
2015-07-21 00:34:27 +02:00
|
|
|
const char* strtab = nullptr;
|
|
|
|
ElfW(Sym)* symtab = nullptr;
|
2014-07-16 01:53:13 +02:00
|
|
|
for (ElfW(Dyn)* d = vdso_dyn; d->d_tag != DT_NULL; ++d) {
|
|
|
|
if (d->d_tag == DT_STRTAB) {
|
|
|
|
strtab = reinterpret_cast<const char*>(vdso_addr + d->d_un.d_ptr);
|
|
|
|
} else if (d->d_tag == DT_SYMTAB) {
|
|
|
|
symtab = reinterpret_cast<ElfW(Sym)*>(vdso_addr + d->d_un.d_ptr);
|
|
|
|
}
|
|
|
|
}
|
2015-07-21 00:34:27 +02:00
|
|
|
if (strtab == nullptr || symtab == nullptr) {
|
2014-07-16 01:53:13 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Are there any symbols we want?
|
|
|
|
for (size_t i = 0; i < symbol_count; ++i) {
|
|
|
|
for (size_t j = 0; j < VDSO_END; ++j) {
|
2015-10-06 20:08:13 +02:00
|
|
|
if (strcmp(vdso[j].name, strtab + symtab[i].st_name) == 0) {
|
|
|
|
vdso[j].fn = reinterpret_cast<void*>(vdso_addr + symtab[i].st_value);
|
2014-07-16 01:53:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2015-10-06 20:08:13 +02:00
|
|
|
void __libc_init_vdso(libc_globals*, KernelArgumentBlock&) {
|
2014-07-16 01:53:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|