1#include <elf.h>
2#include <link.h>
3#include <limits.h>
4#include <stdint.h>
5#include <string.h>
6#include <sys/auxv.h>
7#include "syscall.h"
8
9#ifdef VDSO_USEFUL
10
11#if ULONG_MAX == 0xffffffff
12typedef Elf32_Ehdr Ehdr;
13typedef Elf32_Phdr Phdr;
14typedef Elf32_Sym Sym;
15typedef Elf32_Verdef Verdef;
16typedef Elf32_Verdaux Verdaux;
17#else
18typedef Elf64_Ehdr Ehdr;
19typedef Elf64_Phdr Phdr;
20typedef Elf64_Sym Sym;
21typedef Elf64_Verdef Verdef;
22typedef Elf64_Verdaux Verdaux;
23#endif
24
25static int checkver(Verdef *def, int vsym, const char *vername, char *strings)
26{
27 vsym &= 0x7fff;
28 for (;;) {
29 if (!(def->vd_flags & VER_FLG_BASE)
30 && (def->vd_ndx & 0x7fff) == vsym)
31 break;
32 if (def->vd_next == 0)
33 return 0;
34 def = (Verdef *)((char *)def + def->vd_next);
35 }
36 Verdaux *aux = (Verdaux *)((char *)def + def->vd_aux);
37 return !strcmp(vername, strings + aux->vda_name);
38}
39
40#define OK_TYPES (1<<STT_NOTYPE | 1<<STT_OBJECT | 1<<STT_FUNC | 1<<STT_COMMON)
41#define OK_BINDS (1<<STB_GLOBAL | 1<<STB_WEAK | 1<<STB_GNU_UNIQUE)
42
43extern char** environ;
44static Ehdr *eh = NULL;
45void *__vdsosym(const char *vername, const char *name);
46// We don't have libc struct available here. Compute aux vector manually.
47__attribute__((constructor)) static void auxv_init()
48{
49 size_t i, *auxv;
50 for (i=0; environ[i]; i++);
51 auxv = (void *)(environ+i+1);
52 for (i=0; auxv[i] != AT_SYSINFO_EHDR; i+=2)
53 if (!auxv[i]) return;
54 if (!auxv[i+1]) return;
55 eh = (void *)auxv[i+1];
56}
57
58void *__vdsosym(const char *vername, const char *name)
59{
60 size_t i;
61 if (!eh) return 0;
62 Phdr *ph = (void *)((char *)eh + eh->e_phoff);
63 size_t *dynv=0, base=-1;
64 for (i=0; i<eh->e_phnum; i++, ph=(void *)((char *)ph+eh->e_phentsize)) {
65 if (ph->p_type == PT_LOAD)
66 base = (size_t)eh + ph->p_offset - ph->p_vaddr;
67 else if (ph->p_type == PT_DYNAMIC)
68 dynv = (void *)((char *)eh + ph->p_offset);
69 }
70 if (!dynv || base==(size_t)-1) return 0;
71
72 char *strings = 0;
73 Sym *syms = 0;
74 Elf_Symndx *hashtab = 0;
75 uint16_t *versym = 0;
76 Verdef *verdef = 0;
77
78 for (i=0; dynv[i]; i+=2) {
79 void *p = (void *)(base + dynv[i+1]);
80 switch(dynv[i]) {
81 case DT_STRTAB: strings = p; break;
82 case DT_SYMTAB: syms = p; break;
83 case DT_HASH: hashtab = p; break;
84 case DT_VERSYM: versym = p; break;
85 case DT_VERDEF: verdef = p; break;
86 }
87 }
88
89 if (!strings || !syms || !hashtab) return 0;
90 if (!verdef) versym = 0;
91
92 for (i=0; i<hashtab[1]; i++) {
93 if (!(1<<(syms[i].st_info&0xf) & OK_TYPES)) continue;
94 if (!(1<<(syms[i].st_info>>4) & OK_BINDS)) continue;
95 if (!syms[i].st_shndx) continue;
96 if (strcmp(name, strings+syms[i].st_name)) continue;
97 if (versym && !checkver(verdef, versym[i], vername, strings))
98 continue;
99 return (void *)(base + syms[i].st_value);
100 }
101
102 return 0;
103}
104
105#endif
106