mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-10 09:32:06 +00:00
porting musl vdso implementation
This commit is contained in:
parent
113e1932aa
commit
44b66da298
3
libs/libglibc-compatibility/musl/aarch64/syscall_arch.h
Normal file
3
libs/libglibc-compatibility/musl/aarch64/syscall_arch.h
Normal file
@ -0,0 +1,3 @@
|
||||
#define VDSO_USEFUL
|
||||
#define VDSO_CGT_SYM "__kernel_clock_gettime"
|
||||
#define VDSO_CGT_VER "LINUX_2.6.39"
|
@ -5,21 +5,104 @@
|
||||
#include "musl_features.h"
|
||||
#include "syscall.h"
|
||||
|
||||
int __clock_gettime(clockid_t clk, struct timespec * ts)
|
||||
#ifdef VDSO_CGT_SYM
|
||||
|
||||
static void *volatile vdso_func;
|
||||
|
||||
#ifdef VDSO_CGT32_SYM
|
||||
static void *volatile vdso_func_32;
|
||||
static int cgt_time32_wrap(clockid_t clk, struct timespec *ts)
|
||||
{
|
||||
int r;
|
||||
r = __syscall(SYS_clock_gettime, clk, ts);
|
||||
if (r == -ENOSYS)
|
||||
{
|
||||
if (clk == CLOCK_REALTIME)
|
||||
{
|
||||
__syscall(SYS_gettimeofday, ts, 0);
|
||||
ts->tv_nsec = (int)ts->tv_nsec * 1000;
|
||||
return 0;
|
||||
}
|
||||
r = -EINVAL;
|
||||
}
|
||||
return __syscall_ret(r);
|
||||
long ts32[2];
|
||||
int (*f)(clockid_t, long[2]) =
|
||||
(int (*)(clockid_t, long[2]))vdso_func_32;
|
||||
int r = f(clk, ts32);
|
||||
if (!r) {
|
||||
/* Fallback to syscalls if time32 overflowed. Maybe
|
||||
* we lucked out and somehow migrated to a kernel with
|
||||
* time64 syscalls available. */
|
||||
if (ts32[0] < 0) {
|
||||
a_cas_p(&vdso_func, (void *)cgt_time32_wrap, 0);
|
||||
return -ENOSYS;
|
||||
}
|
||||
ts->tv_sec = ts32[0];
|
||||
ts->tv_nsec = ts32[1];
|
||||
}
|
||||
return r;
|
||||
}
|
||||
#endif
|
||||
|
||||
static int cgt_init(clockid_t clk, struct timespec *ts)
|
||||
{
|
||||
void *p = __vdsosym(VDSO_CGT_VER, VDSO_CGT_SYM);
|
||||
#ifdef VDSO_CGT32_SYM
|
||||
if (!p) {
|
||||
void *q = __vdsosym(VDSO_CGT32_VER, VDSO_CGT32_SYM);
|
||||
if (q) {
|
||||
a_cas_p(&vdso_func_32, 0, q);
|
||||
p = cgt_time32_wrap;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
int (*f)(clockid_t, struct timespec *) =
|
||||
(int (*)(clockid_t, struct timespec *))p;
|
||||
a_cas_p(&vdso_func, (void *)cgt_init, p);
|
||||
return f ? f(clk, ts) : -ENOSYS;
|
||||
}
|
||||
|
||||
static void *volatile vdso_func = (void *)cgt_init;
|
||||
|
||||
#endif
|
||||
|
||||
int __clock_gettime(clockid_t clk, struct timespec *ts)
|
||||
{
|
||||
int r;
|
||||
|
||||
#ifdef VDSO_CGT_SYM
|
||||
int (*f)(clockid_t, struct timespec *) =
|
||||
(int (*)(clockid_t, struct timespec *))vdso_func;
|
||||
if (f) {
|
||||
r = f(clk, ts);
|
||||
if (!r) return r;
|
||||
if (r == -EINVAL) return __syscall_ret(r);
|
||||
/* Fall through on errors other than EINVAL. Some buggy
|
||||
* vdso implementations return ENOSYS for clocks they
|
||||
* can't handle, rather than making the syscall. This
|
||||
* also handles the case where cgt_init fails to find
|
||||
* a vdso function to use. */
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef SYS_clock_gettime64
|
||||
r = -ENOSYS;
|
||||
if (sizeof(time_t) > 4)
|
||||
r = __syscall(SYS_clock_gettime64, clk, ts);
|
||||
if (SYS_clock_gettime == SYS_clock_gettime64 || r!=-ENOSYS)
|
||||
return __syscall_ret(r);
|
||||
long ts32[2];
|
||||
r = __syscall(SYS_clock_gettime, clk, ts32);
|
||||
if (r==-ENOSYS && clk==CLOCK_REALTIME) {
|
||||
r = __syscall(SYS_gettimeofday, ts32, 0);
|
||||
ts32[1] *= 1000;
|
||||
}
|
||||
if (!r) {
|
||||
ts->tv_sec = ts32[0];
|
||||
ts->tv_nsec = ts32[1];
|
||||
return r;
|
||||
}
|
||||
return __syscall_ret(r);
|
||||
#else
|
||||
r = __syscall(SYS_clock_gettime, clk, ts);
|
||||
if (r == -ENOSYS) {
|
||||
if (clk == CLOCK_REALTIME) {
|
||||
__syscall(SYS_gettimeofday, ts, 0);
|
||||
ts->tv_nsec = (int)ts->tv_nsec * 1000;
|
||||
return 0;
|
||||
}
|
||||
r = -EINVAL;
|
||||
}
|
||||
return __syscall_ret(r);
|
||||
#endif
|
||||
}
|
||||
|
||||
weak_alias(__clock_gettime, clock_gettime);
|
||||
|
@ -1,14 +1,11 @@
|
||||
#define _GNU_SOURCE
|
||||
#include <errno.h>
|
||||
#include <sched.h>
|
||||
#include <syscall.h>
|
||||
#include "syscall.h"
|
||||
#include "atomic.h"
|
||||
|
||||
#ifdef VDSO_GETCPU_SYM
|
||||
|
||||
void *__vdsosym(const char *, const char *);
|
||||
|
||||
static void *volatile vdso_func;
|
||||
|
||||
typedef long (*getcpu_f)(unsigned *, unsigned *, void *);
|
||||
|
@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <sys/syscall.h>
|
||||
#include <syscall_arch.h>
|
||||
|
||||
typedef long syscall_arg_t;
|
||||
|
||||
@ -9,3 +10,6 @@ long __syscall_ret(unsigned long);
|
||||
|
||||
__attribute__((visibility("hidden")))
|
||||
long __syscall(syscall_arg_t, ...);
|
||||
|
||||
__attribute__((visibility("hidden")))
|
||||
void *__vdsosym(const char *, const char *);
|
||||
|
92
libs/libglibc-compatibility/musl/vdso.c
Normal file
92
libs/libglibc-compatibility/musl/vdso.c
Normal file
@ -0,0 +1,92 @@
|
||||
#include <elf.h>
|
||||
#include <link.h>
|
||||
#include <limits.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <sys/auxv.h>
|
||||
#include "syscall.h"
|
||||
|
||||
#ifdef VDSO_USEFUL
|
||||
|
||||
#if ULONG_MAX == 0xffffffff
|
||||
typedef Elf32_Ehdr Ehdr;
|
||||
typedef Elf32_Phdr Phdr;
|
||||
typedef Elf32_Sym Sym;
|
||||
typedef Elf32_Verdef Verdef;
|
||||
typedef Elf32_Verdaux Verdaux;
|
||||
#else
|
||||
typedef Elf64_Ehdr Ehdr;
|
||||
typedef Elf64_Phdr Phdr;
|
||||
typedef Elf64_Sym Sym;
|
||||
typedef Elf64_Verdef Verdef;
|
||||
typedef Elf64_Verdaux Verdaux;
|
||||
#endif
|
||||
|
||||
static int checkver(Verdef *def, int vsym, const char *vername, char *strings)
|
||||
{
|
||||
vsym &= 0x7fff;
|
||||
for (;;) {
|
||||
if (!(def->vd_flags & VER_FLG_BASE)
|
||||
&& (def->vd_ndx & 0x7fff) == vsym)
|
||||
break;
|
||||
if (def->vd_next == 0)
|
||||
return 0;
|
||||
def = (Verdef *)((char *)def + def->vd_next);
|
||||
}
|
||||
Verdaux *aux = (Verdaux *)((char *)def + def->vd_aux);
|
||||
return !strcmp(vername, strings + aux->vda_name);
|
||||
}
|
||||
|
||||
#define OK_TYPES (1<<STT_NOTYPE | 1<<STT_OBJECT | 1<<STT_FUNC | 1<<STT_COMMON)
|
||||
#define OK_BINDS (1<<STB_GLOBAL | 1<<STB_WEAK | 1<<STB_GNU_UNIQUE)
|
||||
|
||||
void *__vdsosym(const char *vername, const char *name)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
// We don't have libc struct available here. Use getauxval instead.
|
||||
Ehdr *eh = (void *)getauxval(AT_SYSINFO_EHDR);
|
||||
Phdr *ph = (void *)((char *)eh + eh->e_phoff);
|
||||
size_t *dynv=0, base=-1;
|
||||
for (i=0; i<eh->e_phnum; i++, ph=(void *)((char *)ph+eh->e_phentsize)) {
|
||||
if (ph->p_type == PT_LOAD)
|
||||
base = (size_t)eh + ph->p_offset - ph->p_vaddr;
|
||||
else if (ph->p_type == PT_DYNAMIC)
|
||||
dynv = (void *)((char *)eh + ph->p_offset);
|
||||
}
|
||||
if (!dynv || base==(size_t)-1) return 0;
|
||||
|
||||
char *strings = 0;
|
||||
Sym *syms = 0;
|
||||
Elf_Symndx *hashtab = 0;
|
||||
uint16_t *versym = 0;
|
||||
Verdef *verdef = 0;
|
||||
|
||||
for (i=0; dynv[i]; i+=2) {
|
||||
void *p = (void *)(base + dynv[i+1]);
|
||||
switch(dynv[i]) {
|
||||
case DT_STRTAB: strings = p; break;
|
||||
case DT_SYMTAB: syms = p; break;
|
||||
case DT_HASH: hashtab = p; break;
|
||||
case DT_VERSYM: versym = p; break;
|
||||
case DT_VERDEF: verdef = p; break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!strings || !syms || !hashtab) return 0;
|
||||
if (!verdef) versym = 0;
|
||||
|
||||
for (i=0; i<hashtab[1]; i++) {
|
||||
if (!(1<<(syms[i].st_info&0xf) & OK_TYPES)) continue;
|
||||
if (!(1<<(syms[i].st_info>>4) & OK_BINDS)) continue;
|
||||
if (!syms[i].st_shndx) continue;
|
||||
if (strcmp(name, strings+syms[i].st_name)) continue;
|
||||
if (versym && !checkver(verdef, versym[i], vername, strings))
|
||||
continue;
|
||||
return (void *)(base + syms[i].st_value);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
5
libs/libglibc-compatibility/musl/x86_64/syscall_arch.h
Normal file
5
libs/libglibc-compatibility/musl/x86_64/syscall_arch.h
Normal file
@ -0,0 +1,5 @@
|
||||
#define VDSO_USEFUL
|
||||
#define VDSO_CGT_SYM "__vdso_clock_gettime"
|
||||
#define VDSO_CGT_VER "LINUX_2.6"
|
||||
#define VDSO_GETCPU_SYM "__vdso_getcpu"
|
||||
#define VDSO_GETCPU_VER "LINUX_2.6"
|
Loading…
Reference in New Issue
Block a user