This source file includes following definitions.
- elf_hash
- vdso_init_from_sysinfo_ehdr
- vdso_match_version
- vdso_sym
- vdso_init_from_auxv
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 #include <stdbool.h>
19 #include <stdint.h>
20 #include <string.h>
21 #include <limits.h>
22 #include <elf.h>
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 extern void vdso_init_from_auxv(void *auxv);
44 extern void vdso_init_from_sysinfo_ehdr(uintptr_t base);
45 extern void *vdso_sym(const char *version, const char *name);
46
47
48
49 #ifndef ELF_BITS
50 # if ULONG_MAX > 0xffffffffUL
51 # define ELF_BITS 64
52 # else
53 # define ELF_BITS 32
54 # endif
55 #endif
56
57 #define ELF_BITS_XFORM2(bits, x) Elf##bits##_##x
58 #define ELF_BITS_XFORM(bits, x) ELF_BITS_XFORM2(bits, x)
59 #define ELF(x) ELF_BITS_XFORM(ELF_BITS, x)
60
61 static struct vdso_info
62 {
63 bool valid;
64
65
66 uintptr_t load_addr;
67 uintptr_t load_offset;
68
69
70 ELF(Sym) *symtab;
71 const char *symstrings;
72 ELF(Word) *bucket, *chain;
73 ELF(Word) nbucket, nchain;
74
75
76 ELF(Versym) *versym;
77 ELF(Verdef) *verdef;
78 } vdso_info;
79
80
81 static unsigned long elf_hash(const unsigned char *name)
82 {
83 unsigned long h = 0, g;
84 while (*name)
85 {
86 h = (h << 4) + *name++;
87 if (g = h & 0xf0000000)
88 h ^= g >> 24;
89 h &= ~g;
90 }
91 return h;
92 }
93
94 void vdso_init_from_sysinfo_ehdr(uintptr_t base)
95 {
96 size_t i;
97 bool found_vaddr = false;
98
99 vdso_info.valid = false;
100
101 vdso_info.load_addr = base;
102
103 ELF(Ehdr) *hdr = (ELF(Ehdr)*)base;
104 if (hdr->e_ident[EI_CLASS] !=
105 (ELF_BITS == 32 ? ELFCLASS32 : ELFCLASS64)) {
106 return;
107 }
108
109 ELF(Phdr) *pt = (ELF(Phdr)*)(vdso_info.load_addr + hdr->e_phoff);
110 ELF(Dyn) *dyn = 0;
111
112
113
114
115
116 for (i = 0; i < hdr->e_phnum; i++)
117 {
118 if (pt[i].p_type == PT_LOAD && !found_vaddr) {
119 found_vaddr = true;
120 vdso_info.load_offset = base
121 + (uintptr_t)pt[i].p_offset
122 - (uintptr_t)pt[i].p_vaddr;
123 } else if (pt[i].p_type == PT_DYNAMIC) {
124 dyn = (ELF(Dyn)*)(base + pt[i].p_offset);
125 }
126 }
127
128 if (!found_vaddr || !dyn)
129 return;
130
131
132
133
134 ELF(Word) *hash = 0;
135 vdso_info.symstrings = 0;
136 vdso_info.symtab = 0;
137 vdso_info.versym = 0;
138 vdso_info.verdef = 0;
139 for (i = 0; dyn[i].d_tag != DT_NULL; i++) {
140 switch (dyn[i].d_tag) {
141 case DT_STRTAB:
142 vdso_info.symstrings = (const char *)
143 ((uintptr_t)dyn[i].d_un.d_ptr
144 + vdso_info.load_offset);
145 break;
146 case DT_SYMTAB:
147 vdso_info.symtab = (ELF(Sym) *)
148 ((uintptr_t)dyn[i].d_un.d_ptr
149 + vdso_info.load_offset);
150 break;
151 case DT_HASH:
152 hash = (ELF(Word) *)
153 ((uintptr_t)dyn[i].d_un.d_ptr
154 + vdso_info.load_offset);
155 break;
156 case DT_VERSYM:
157 vdso_info.versym = (ELF(Versym) *)
158 ((uintptr_t)dyn[i].d_un.d_ptr
159 + vdso_info.load_offset);
160 break;
161 case DT_VERDEF:
162 vdso_info.verdef = (ELF(Verdef) *)
163 ((uintptr_t)dyn[i].d_un.d_ptr
164 + vdso_info.load_offset);
165 break;
166 }
167 }
168 if (!vdso_info.symstrings || !vdso_info.symtab || !hash)
169 return;
170
171 if (!vdso_info.verdef)
172 vdso_info.versym = 0;
173
174
175 vdso_info.nbucket = hash[0];
176 vdso_info.nchain = hash[1];
177 vdso_info.bucket = &hash[2];
178 vdso_info.chain = &hash[vdso_info.nbucket + 2];
179
180
181 vdso_info.valid = true;
182 }
183
184 static bool vdso_match_version(ELF(Versym) ver,
185 const char *name, ELF(Word) hash)
186 {
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203 ver &= 0x7fff;
204 ELF(Verdef) *def = vdso_info.verdef;
205 while(true) {
206 if ((def->vd_flags & VER_FLG_BASE) == 0
207 && (def->vd_ndx & 0x7fff) == ver)
208 break;
209
210 if (def->vd_next == 0)
211 return false;
212
213 def = (ELF(Verdef) *)((char *)def + def->vd_next);
214 }
215
216
217 ELF(Verdaux) *aux = (ELF(Verdaux)*)((char *)def + def->vd_aux);
218 return def->vd_hash == hash
219 && !strcmp(name, vdso_info.symstrings + aux->vda_name);
220 }
221
222 void *vdso_sym(const char *version, const char *name)
223 {
224 unsigned long ver_hash;
225 if (!vdso_info.valid)
226 return 0;
227
228 ver_hash = elf_hash(version);
229 ELF(Word) chain = vdso_info.bucket[elf_hash(name) % vdso_info.nbucket];
230
231 for (; chain != STN_UNDEF; chain = vdso_info.chain[chain]) {
232 ELF(Sym) *sym = &vdso_info.symtab[chain];
233
234
235 if (ELF64_ST_TYPE(sym->st_info) != STT_FUNC)
236 continue;
237 if (ELF64_ST_BIND(sym->st_info) != STB_GLOBAL &&
238 ELF64_ST_BIND(sym->st_info) != STB_WEAK)
239 continue;
240 if (sym->st_shndx == SHN_UNDEF)
241 continue;
242 if (strcmp(name, vdso_info.symstrings + sym->st_name))
243 continue;
244
245
246 if (vdso_info.versym
247 && !vdso_match_version(vdso_info.versym[chain],
248 version, ver_hash))
249 continue;
250
251 return (void *)(vdso_info.load_offset + sym->st_value);
252 }
253
254 return 0;
255 }
256
257 void vdso_init_from_auxv(void *auxv)
258 {
259 ELF(auxv_t) *elf_auxv = auxv;
260 for (int i = 0; elf_auxv[i].a_type != AT_NULL; i++)
261 {
262 if (elf_auxv[i].a_type == AT_SYSINFO_EHDR) {
263 vdso_init_from_sysinfo_ehdr(elf_auxv[i].a_un.a_val);
264 return;
265 }
266 }
267
268 vdso_info.valid = false;
269 }