xref: /aosp_15_r20/external/linux-kselftest/tools/testing/selftests/vDSO/parse_vdso.c (revision 053f45be4e351dfd5e965df293cd45b779f579ee)
1 /*
2  * parse_vdso.c: Linux reference vDSO parser
3  * Written by Andrew Lutomirski, 2011-2014.
4  *
5  * This code is meant to be linked in to various programs that run on Linux.
6  * As such, it is available with as few restrictions as possible.  This file
7  * is licensed under the Creative Commons Zero License, version 1.0,
8  * available at http://creativecommons.org/publicdomain/zero/1.0/legalcode
9  *
10  * The vDSO is a regular ELF DSO that the kernel maps into user space when
11  * it starts a program.  It works equally well in statically and dynamically
12  * linked binaries.
13  *
14  * This code is tested on x86.  In principle it should work on any
15  * architecture that has a vDSO.
16  */
17 
18 #include <stdbool.h>
19 #include <stdint.h>
20 #include <string.h>
21 #include <limits.h>
22 #include <elf.h>
23 
24 #include "parse_vdso.h"
25 
26 /* And here's the code. */
27 #ifndef ELF_BITS
28 # if ULONG_MAX > 0xffffffffUL
29 #  define ELF_BITS 64
30 # else
31 #  define ELF_BITS 32
32 # endif
33 #endif
34 
35 #define ELF_BITS_XFORM2(bits, x) Elf##bits##_##x
36 #define ELF_BITS_XFORM(bits, x) ELF_BITS_XFORM2(bits, x)
37 #define ELF(x) ELF_BITS_XFORM(ELF_BITS, x)
38 
39 static struct vdso_info
40 {
41 	bool valid;
42 
43 	/* Load information */
44 	uintptr_t load_addr;
45 	uintptr_t load_offset;  /* load_addr - recorded vaddr */
46 
47 	/* Symbol table */
48 	ELF(Sym) *symtab;
49 	const char *symstrings;
50 	ELF(Word) *bucket, *chain;
51 	ELF(Word) nbucket, nchain;
52 
53 	/* Version table */
54 	ELF(Versym) *versym;
55 	ELF(Verdef) *verdef;
56 } vdso_info;
57 
58 /* Straight from the ELF specification. */
elf_hash(const char * s_name)59 static unsigned long elf_hash(const char *s_name)
60 {
61 	unsigned long h = 0, g;
62 	const unsigned char *name = (const unsigned char *)s_name;
63 
64 	while (*name)
65 	{
66 		h = (h << 4) + *name++;
67 		g = h & 0xf0000000;
68 		if (g)
69 			h ^= g >> 24;
70 		h &= ~g;
71 	}
72 	return h;
73 }
74 
vdso_init_from_sysinfo_ehdr(uintptr_t base)75 void vdso_init_from_sysinfo_ehdr(uintptr_t base)
76 {
77 	size_t i;
78 	bool found_vaddr = false;
79 
80 	vdso_info.valid = false;
81 
82 	vdso_info.load_addr = base;
83 
84 	ELF(Ehdr) *hdr = (ELF(Ehdr)*)base;
85 	if (hdr->e_ident[EI_CLASS] !=
86 	    (ELF_BITS == 32 ? ELFCLASS32 : ELFCLASS64)) {
87 		return;  /* Wrong ELF class -- check ELF_BITS */
88 	}
89 
90 	ELF(Phdr) *pt = (ELF(Phdr)*)(vdso_info.load_addr + hdr->e_phoff);
91 	ELF(Dyn) *dyn = 0;
92 
93 	/*
94 	 * We need two things from the segment table: the load offset
95 	 * and the dynamic table.
96 	 */
97 	for (i = 0; i < hdr->e_phnum; i++)
98 	{
99 		if (pt[i].p_type == PT_LOAD && !found_vaddr) {
100 			found_vaddr = true;
101 			vdso_info.load_offset =	base
102 				+ (uintptr_t)pt[i].p_offset
103 				- (uintptr_t)pt[i].p_vaddr;
104 		} else if (pt[i].p_type == PT_DYNAMIC) {
105 			dyn = (ELF(Dyn)*)(base + pt[i].p_offset);
106 		}
107 	}
108 
109 	if (!found_vaddr || !dyn)
110 		return;  /* Failed */
111 
112 	/*
113 	 * Fish out the useful bits of the dynamic table.
114 	 */
115 	ELF(Word) *hash = 0;
116 	vdso_info.symstrings = 0;
117 	vdso_info.symtab = 0;
118 	vdso_info.versym = 0;
119 	vdso_info.verdef = 0;
120 	for (i = 0; dyn[i].d_tag != DT_NULL; i++) {
121 		switch (dyn[i].d_tag) {
122 		case DT_STRTAB:
123 			vdso_info.symstrings = (const char *)
124 				((uintptr_t)dyn[i].d_un.d_ptr
125 				 + vdso_info.load_offset);
126 			break;
127 		case DT_SYMTAB:
128 			vdso_info.symtab = (ELF(Sym) *)
129 				((uintptr_t)dyn[i].d_un.d_ptr
130 				 + vdso_info.load_offset);
131 			break;
132 		case DT_HASH:
133 			hash = (ELF(Word) *)
134 				((uintptr_t)dyn[i].d_un.d_ptr
135 				 + vdso_info.load_offset);
136 			break;
137 		case DT_VERSYM:
138 			vdso_info.versym = (ELF(Versym) *)
139 				((uintptr_t)dyn[i].d_un.d_ptr
140 				 + vdso_info.load_offset);
141 			break;
142 		case DT_VERDEF:
143 			vdso_info.verdef = (ELF(Verdef) *)
144 				((uintptr_t)dyn[i].d_un.d_ptr
145 				 + vdso_info.load_offset);
146 			break;
147 		}
148 	}
149 	if (!vdso_info.symstrings || !vdso_info.symtab || !hash)
150 		return;  /* Failed */
151 
152 	if (!vdso_info.verdef)
153 		vdso_info.versym = 0;
154 
155 	/* Parse the hash table header. */
156 	vdso_info.nbucket = hash[0];
157 	vdso_info.nchain = hash[1];
158 	vdso_info.bucket = &hash[2];
159 	vdso_info.chain = &hash[vdso_info.nbucket + 2];
160 
161 	/* That's all we need. */
162 	vdso_info.valid = true;
163 }
164 
vdso_match_version(ELF (Versym)ver,const char * name,ELF (Word)hash)165 static bool vdso_match_version(ELF(Versym) ver,
166 			       const char *name, ELF(Word) hash)
167 {
168 	/*
169 	 * This is a helper function to check if the version indexed by
170 	 * ver matches name (which hashes to hash).
171 	 *
172 	 * The version definition table is a mess, and I don't know how
173 	 * to do this in better than linear time without allocating memory
174 	 * to build an index.  I also don't know why the table has
175 	 * variable size entries in the first place.
176 	 *
177 	 * For added fun, I can't find a comprehensible specification of how
178 	 * to parse all the weird flags in the table.
179 	 *
180 	 * So I just parse the whole table every time.
181 	 */
182 
183 	/* First step: find the version definition */
184 	ver &= 0x7fff;  /* Apparently bit 15 means "hidden" */
185 	ELF(Verdef) *def = vdso_info.verdef;
186 	while(true) {
187 		if ((def->vd_flags & VER_FLG_BASE) == 0
188 		    && (def->vd_ndx & 0x7fff) == ver)
189 			break;
190 
191 		if (def->vd_next == 0)
192 			return false;  /* No definition. */
193 
194 		def = (ELF(Verdef) *)((char *)def + def->vd_next);
195 	}
196 
197 	/* Now figure out whether it matches. */
198 	ELF(Verdaux) *aux = (ELF(Verdaux)*)((char *)def + def->vd_aux);
199 	return def->vd_hash == hash
200 		&& !strcmp(name, vdso_info.symstrings + aux->vda_name);
201 }
202 
vdso_sym(const char * version,const char * name)203 void *vdso_sym(const char *version, const char *name)
204 {
205 	unsigned long ver_hash;
206 	if (!vdso_info.valid)
207 		return 0;
208 
209 	ver_hash = elf_hash(version);
210 	ELF(Word) chain = vdso_info.bucket[elf_hash(name) % vdso_info.nbucket];
211 
212 	for (; chain != STN_UNDEF; chain = vdso_info.chain[chain]) {
213 		ELF(Sym) *sym = &vdso_info.symtab[chain];
214 
215 		/* Check for a defined global or weak function w/ right name. */
216 		if (ELF64_ST_TYPE(sym->st_info) != STT_FUNC)
217 			continue;
218 		if (ELF64_ST_BIND(sym->st_info) != STB_GLOBAL &&
219 		    ELF64_ST_BIND(sym->st_info) != STB_WEAK)
220 			continue;
221 		if (sym->st_shndx == SHN_UNDEF)
222 			continue;
223 		if (strcmp(name, vdso_info.symstrings + sym->st_name))
224 			continue;
225 
226 		/* Check symbol version. */
227 		if (vdso_info.versym
228 		    && !vdso_match_version(vdso_info.versym[chain],
229 					   version, ver_hash))
230 			continue;
231 
232 		return (void *)(vdso_info.load_offset + sym->st_value);
233 	}
234 
235 	return 0;
236 }
237 
vdso_init_from_auxv(void * auxv)238 void vdso_init_from_auxv(void *auxv)
239 {
240 	ELF(auxv_t) *elf_auxv = auxv;
241 	for (int i = 0; elf_auxv[i].a_type != AT_NULL; i++)
242 	{
243 		if (elf_auxv[i].a_type == AT_SYSINFO_EHDR) {
244 			vdso_init_from_sysinfo_ehdr(elf_auxv[i].a_un.a_val);
245 			return;
246 		}
247 	}
248 
249 	vdso_info.valid = false;
250 }
251