xref: /linux/tools/lib/bpf/elf.c (revision 3ea5eb68b9d624935108b5e696859304edfac202)
1 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2 
3 #ifndef _GNU_SOURCE
4 #define _GNU_SOURCE
5 #endif
6 #include <libelf.h>
7 #include <gelf.h>
8 #include <fcntl.h>
9 #include <linux/kernel.h>
10 
11 #include "libbpf_internal.h"
12 #include "str_error.h"
13 
14 /* A SHT_GNU_versym section holds 16-bit words. This bit is set if
15  * the symbol is hidden and can only be seen when referenced using an
16  * explicit version number. This is a GNU extension.
17  */
18 #define VERSYM_HIDDEN	0x8000
19 
20 /* This is the mask for the rest of the data in a word read from a
21  * SHT_GNU_versym section.
22  */
23 #define VERSYM_VERSION	0x7fff
24 
25 int elf_open(const char *binary_path, struct elf_fd *elf_fd)
26 {
27 	char errmsg[STRERR_BUFSIZE];
28 	int fd, ret;
29 	Elf *elf;
30 
31 	elf_fd->elf = NULL;
32 	elf_fd->fd = -1;
33 
34 	if (elf_version(EV_CURRENT) == EV_NONE) {
35 		pr_warn("elf: failed to init libelf for %s\n", binary_path);
36 		return -LIBBPF_ERRNO__LIBELF;
37 	}
38 	fd = open(binary_path, O_RDONLY | O_CLOEXEC);
39 	if (fd < 0) {
40 		ret = -errno;
41 		pr_warn("elf: failed to open %s: %s\n", binary_path,
42 			libbpf_strerror_r(ret, errmsg, sizeof(errmsg)));
43 		return ret;
44 	}
45 	elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
46 	if (!elf) {
47 		pr_warn("elf: could not read elf from %s: %s\n", binary_path, elf_errmsg(-1));
48 		close(fd);
49 		return -LIBBPF_ERRNO__FORMAT;
50 	}
51 	elf_fd->fd = fd;
52 	elf_fd->elf = elf;
53 	return 0;
54 }
55 
56 void elf_close(struct elf_fd *elf_fd)
57 {
58 	if (!elf_fd)
59 		return;
60 	elf_end(elf_fd->elf);
61 	close(elf_fd->fd);
62 }
63 
64 /* Return next ELF section of sh_type after scn, or first of that type if scn is NULL. */
65 static Elf_Scn *elf_find_next_scn_by_type(Elf *elf, int sh_type, Elf_Scn *scn)
66 {
67 	while ((scn = elf_nextscn(elf, scn)) != NULL) {
68 		GElf_Shdr sh;
69 
70 		if (!gelf_getshdr(scn, &sh))
71 			continue;
72 		if (sh.sh_type == sh_type)
73 			return scn;
74 	}
75 	return NULL;
76 }
77 
78 struct elf_sym {
79 	const char *name;
80 	GElf_Sym sym;
81 	GElf_Shdr sh;
82 	int ver;
83 	bool hidden;
84 };
85 
86 struct elf_sym_iter {
87 	Elf *elf;
88 	Elf_Data *syms;
89 	Elf_Data *versyms;
90 	Elf_Data *verdefs;
91 	size_t nr_syms;
92 	size_t strtabidx;
93 	size_t verdef_strtabidx;
94 	size_t next_sym_idx;
95 	struct elf_sym sym;
96 	int st_type;
97 };
98 
99 static int elf_sym_iter_new(struct elf_sym_iter *iter,
100 			    Elf *elf, const char *binary_path,
101 			    int sh_type, int st_type)
102 {
103 	Elf_Scn *scn = NULL;
104 	GElf_Ehdr ehdr;
105 	GElf_Shdr sh;
106 
107 	memset(iter, 0, sizeof(*iter));
108 
109 	if (!gelf_getehdr(elf, &ehdr)) {
110 		pr_warn("elf: failed to get ehdr from %s: %s\n", binary_path, elf_errmsg(-1));
111 		return -EINVAL;
112 	}
113 
114 	scn = elf_find_next_scn_by_type(elf, sh_type, NULL);
115 	if (!scn) {
116 		pr_debug("elf: failed to find symbol table ELF sections in '%s'\n",
117 			 binary_path);
118 		return -ENOENT;
119 	}
120 
121 	if (!gelf_getshdr(scn, &sh))
122 		return -EINVAL;
123 
124 	iter->strtabidx = sh.sh_link;
125 	iter->syms = elf_getdata(scn, 0);
126 	if (!iter->syms) {
127 		pr_warn("elf: failed to get symbols for symtab section in '%s': %s\n",
128 			binary_path, elf_errmsg(-1));
129 		return -EINVAL;
130 	}
131 	iter->nr_syms = iter->syms->d_size / sh.sh_entsize;
132 	iter->elf = elf;
133 	iter->st_type = st_type;
134 
135 	/* Version symbol table is meaningful to dynsym only */
136 	if (sh_type != SHT_DYNSYM)
137 		return 0;
138 
139 	scn = elf_find_next_scn_by_type(elf, SHT_GNU_versym, NULL);
140 	if (!scn)
141 		return 0;
142 	iter->versyms = elf_getdata(scn, 0);
143 
144 	scn = elf_find_next_scn_by_type(elf, SHT_GNU_verdef, NULL);
145 	if (!scn)
146 		return 0;
147 
148 	iter->verdefs = elf_getdata(scn, 0);
149 	if (!iter->verdefs || !gelf_getshdr(scn, &sh)) {
150 		pr_warn("elf: failed to get verdef ELF section in '%s'\n", binary_path);
151 		return -EINVAL;
152 	}
153 	iter->verdef_strtabidx = sh.sh_link;
154 
155 	return 0;
156 }
157 
158 static struct elf_sym *elf_sym_iter_next(struct elf_sym_iter *iter)
159 {
160 	struct elf_sym *ret = &iter->sym;
161 	GElf_Sym *sym = &ret->sym;
162 	const char *name = NULL;
163 	GElf_Versym versym;
164 	Elf_Scn *sym_scn;
165 	size_t idx;
166 
167 	for (idx = iter->next_sym_idx; idx < iter->nr_syms; idx++) {
168 		if (!gelf_getsym(iter->syms, idx, sym))
169 			continue;
170 		if (GELF_ST_TYPE(sym->st_info) != iter->st_type)
171 			continue;
172 		name = elf_strptr(iter->elf, iter->strtabidx, sym->st_name);
173 		if (!name)
174 			continue;
175 		sym_scn = elf_getscn(iter->elf, sym->st_shndx);
176 		if (!sym_scn)
177 			continue;
178 		if (!gelf_getshdr(sym_scn, &ret->sh))
179 			continue;
180 
181 		iter->next_sym_idx = idx + 1;
182 		ret->name = name;
183 		ret->ver = 0;
184 		ret->hidden = false;
185 
186 		if (iter->versyms) {
187 			if (!gelf_getversym(iter->versyms, idx, &versym))
188 				continue;
189 			ret->ver = versym & VERSYM_VERSION;
190 			ret->hidden = versym & VERSYM_HIDDEN;
191 		}
192 		return ret;
193 	}
194 
195 	return NULL;
196 }
197 
198 static const char *elf_get_vername(struct elf_sym_iter *iter, int ver)
199 {
200 	GElf_Verdaux verdaux;
201 	GElf_Verdef verdef;
202 	int offset;
203 
204 	if (!iter->verdefs)
205 		return NULL;
206 
207 	offset = 0;
208 	while (gelf_getverdef(iter->verdefs, offset, &verdef)) {
209 		if (verdef.vd_ndx != ver) {
210 			if (!verdef.vd_next)
211 				break;
212 
213 			offset += verdef.vd_next;
214 			continue;
215 		}
216 
217 		if (!gelf_getverdaux(iter->verdefs, offset + verdef.vd_aux, &verdaux))
218 			break;
219 
220 		return elf_strptr(iter->elf, iter->verdef_strtabidx, verdaux.vda_name);
221 
222 	}
223 	return NULL;
224 }
225 
226 static bool symbol_match(struct elf_sym_iter *iter, int sh_type, struct elf_sym *sym,
227 			 const char *name, size_t name_len, const char *lib_ver)
228 {
229 	const char *ver_name;
230 
231 	/* Symbols are in forms of func, func@LIB_VER or func@@LIB_VER
232 	 * make sure the func part matches the user specified name
233 	 */
234 	if (strncmp(sym->name, name, name_len) != 0)
235 		return false;
236 
237 	/* ...but we don't want a search for "foo" to match 'foo2" also, so any
238 	 * additional characters in sname should be of the form "@@LIB".
239 	 */
240 	if (sym->name[name_len] != '\0' && sym->name[name_len] != '@')
241 		return false;
242 
243 	/* If user does not specify symbol version, then we got a match */
244 	if (!lib_ver)
245 		return true;
246 
247 	/* If user specifies symbol version, for dynamic symbols,
248 	 * get version name from ELF verdef section for comparison.
249 	 */
250 	if (sh_type == SHT_DYNSYM) {
251 		ver_name = elf_get_vername(iter, sym->ver);
252 		if (!ver_name)
253 			return false;
254 		return strcmp(ver_name, lib_ver) == 0;
255 	}
256 
257 	/* For normal symbols, it is already in form of func@LIB_VER */
258 	return strcmp(sym->name, name) == 0;
259 }
260 
261 /* Transform symbol's virtual address (absolute for binaries and relative
262  * for shared libs) into file offset, which is what kernel is expecting
263  * for uprobe/uretprobe attachment.
264  * See Documentation/trace/uprobetracer.rst for more details. This is done
265  * by looking up symbol's containing section's header and using iter's virtual
266  * address (sh_addr) and corresponding file offset (sh_offset) to transform
267  * sym.st_value (virtual address) into desired final file offset.
268  */
269 static unsigned long elf_sym_offset(struct elf_sym *sym)
270 {
271 	return sym->sym.st_value - sym->sh.sh_addr + sym->sh.sh_offset;
272 }
273 
274 /* Find offset of function name in the provided ELF object. "binary_path" is
275  * the path to the ELF binary represented by "elf", and only used for error
276  * reporting matters. "name" matches symbol name or name@@LIB for library
277  * functions.
278  */
279 long elf_find_func_offset(Elf *elf, const char *binary_path, const char *name)
280 {
281 	int i, sh_types[2] = { SHT_DYNSYM, SHT_SYMTAB };
282 	const char *at_symbol, *lib_ver;
283 	bool is_shared_lib;
284 	long ret = -ENOENT;
285 	size_t name_len;
286 	GElf_Ehdr ehdr;
287 
288 	if (!gelf_getehdr(elf, &ehdr)) {
289 		pr_warn("elf: failed to get ehdr from %s: %s\n", binary_path, elf_errmsg(-1));
290 		ret = -LIBBPF_ERRNO__FORMAT;
291 		goto out;
292 	}
293 	/* for shared lib case, we do not need to calculate relative offset */
294 	is_shared_lib = ehdr.e_type == ET_DYN;
295 
296 	/* Does name specify "@@LIB_VER" or "@LIB_VER" ? */
297 	at_symbol = strchr(name, '@');
298 	if (at_symbol) {
299 		name_len = at_symbol - name;
300 		/* skip second @ if it's @@LIB_VER case */
301 		if (at_symbol[1] == '@')
302 			at_symbol++;
303 		lib_ver = at_symbol + 1;
304 	} else {
305 		name_len = strlen(name);
306 		lib_ver = NULL;
307 	}
308 
309 	/* Search SHT_DYNSYM, SHT_SYMTAB for symbol. This search order is used because if
310 	 * a binary is stripped, it may only have SHT_DYNSYM, and a fully-statically
311 	 * linked binary may not have SHT_DYMSYM, so absence of a section should not be
312 	 * reported as a warning/error.
313 	 */
314 	for (i = 0; i < ARRAY_SIZE(sh_types); i++) {
315 		struct elf_sym_iter iter;
316 		struct elf_sym *sym;
317 		int last_bind = -1;
318 		int cur_bind;
319 
320 		ret = elf_sym_iter_new(&iter, elf, binary_path, sh_types[i], STT_FUNC);
321 		if (ret == -ENOENT)
322 			continue;
323 		if (ret)
324 			goto out;
325 
326 		while ((sym = elf_sym_iter_next(&iter))) {
327 			if (!symbol_match(&iter, sh_types[i], sym, name, name_len, lib_ver))
328 				continue;
329 
330 			cur_bind = GELF_ST_BIND(sym->sym.st_info);
331 
332 			if (ret > 0) {
333 				/* handle multiple matches */
334 				if (elf_sym_offset(sym) == ret) {
335 					/* same offset, no problem */
336 					continue;
337 				} else if (last_bind != STB_WEAK && cur_bind != STB_WEAK) {
338 					/* Only accept one non-weak bind. */
339 					pr_warn("elf: ambiguous match for '%s', '%s' in '%s'\n",
340 						sym->name, name, binary_path);
341 					ret = -LIBBPF_ERRNO__FORMAT;
342 					goto out;
343 				} else if (cur_bind == STB_WEAK) {
344 					/* already have a non-weak bind, and
345 					 * this is a weak bind, so ignore.
346 					 */
347 					continue;
348 				}
349 			}
350 
351 			ret = elf_sym_offset(sym);
352 			last_bind = cur_bind;
353 		}
354 		if (ret > 0)
355 			break;
356 	}
357 
358 	if (ret > 0) {
359 		pr_debug("elf: symbol address match for '%s' in '%s': 0x%lx\n", name, binary_path,
360 			 ret);
361 	} else {
362 		if (ret == 0) {
363 			pr_warn("elf: '%s' is 0 in symtab for '%s': %s\n", name, binary_path,
364 				is_shared_lib ? "should not be 0 in a shared library" :
365 						"try using shared library path instead");
366 			ret = -ENOENT;
367 		} else {
368 			pr_warn("elf: failed to find symbol '%s' in '%s'\n", name, binary_path);
369 		}
370 	}
371 out:
372 	return ret;
373 }
374 
375 /* Find offset of function name in ELF object specified by path. "name" matches
376  * symbol name or name@@LIB for library functions.
377  */
378 long elf_find_func_offset_from_file(const char *binary_path, const char *name)
379 {
380 	struct elf_fd elf_fd;
381 	long ret = -ENOENT;
382 
383 	ret = elf_open(binary_path, &elf_fd);
384 	if (ret)
385 		return ret;
386 	ret = elf_find_func_offset(elf_fd.elf, binary_path, name);
387 	elf_close(&elf_fd);
388 	return ret;
389 }
390 
391 struct symbol {
392 	const char *name;
393 	int bind;
394 	int idx;
395 };
396 
397 static int symbol_cmp(const void *a, const void *b)
398 {
399 	const struct symbol *sym_a = a;
400 	const struct symbol *sym_b = b;
401 
402 	return strcmp(sym_a->name, sym_b->name);
403 }
404 
405 /*
406  * Return offsets in @poffsets for symbols specified in @syms array argument.
407  * On success returns 0 and offsets are returned in allocated array with @cnt
408  * size, that needs to be released by the caller.
409  */
410 int elf_resolve_syms_offsets(const char *binary_path, int cnt,
411 			     const char **syms, unsigned long **poffsets,
412 			     int st_type)
413 {
414 	int sh_types[2] = { SHT_DYNSYM, SHT_SYMTAB };
415 	int err = 0, i, cnt_done = 0;
416 	unsigned long *offsets;
417 	struct symbol *symbols;
418 	struct elf_fd elf_fd;
419 
420 	err = elf_open(binary_path, &elf_fd);
421 	if (err)
422 		return err;
423 
424 	offsets = calloc(cnt, sizeof(*offsets));
425 	symbols = calloc(cnt, sizeof(*symbols));
426 
427 	if (!offsets || !symbols) {
428 		err = -ENOMEM;
429 		goto out;
430 	}
431 
432 	for (i = 0; i < cnt; i++) {
433 		symbols[i].name = syms[i];
434 		symbols[i].idx = i;
435 	}
436 
437 	qsort(symbols, cnt, sizeof(*symbols), symbol_cmp);
438 
439 	for (i = 0; i < ARRAY_SIZE(sh_types); i++) {
440 		struct elf_sym_iter iter;
441 		struct elf_sym *sym;
442 
443 		err = elf_sym_iter_new(&iter, elf_fd.elf, binary_path, sh_types[i], st_type);
444 		if (err == -ENOENT)
445 			continue;
446 		if (err)
447 			goto out;
448 
449 		while ((sym = elf_sym_iter_next(&iter))) {
450 			unsigned long sym_offset = elf_sym_offset(sym);
451 			int bind = GELF_ST_BIND(sym->sym.st_info);
452 			struct symbol *found, tmp = {
453 				.name = sym->name,
454 			};
455 			unsigned long *offset;
456 
457 			found = bsearch(&tmp, symbols, cnt, sizeof(*symbols), symbol_cmp);
458 			if (!found)
459 				continue;
460 
461 			offset = &offsets[found->idx];
462 			if (*offset > 0) {
463 				/* same offset, no problem */
464 				if (*offset == sym_offset)
465 					continue;
466 				/* handle multiple matches */
467 				if (found->bind != STB_WEAK && bind != STB_WEAK) {
468 					/* Only accept one non-weak bind. */
469 					pr_warn("elf: ambiguous match found '%s@%lu' in '%s' previous offset %lu\n",
470 						sym->name, sym_offset, binary_path, *offset);
471 					err = -ESRCH;
472 					goto out;
473 				} else if (bind == STB_WEAK) {
474 					/* already have a non-weak bind, and
475 					 * this is a weak bind, so ignore.
476 					 */
477 					continue;
478 				}
479 			} else {
480 				cnt_done++;
481 			}
482 			*offset = sym_offset;
483 			found->bind = bind;
484 		}
485 	}
486 
487 	if (cnt != cnt_done) {
488 		err = -ENOENT;
489 		goto out;
490 	}
491 
492 	*poffsets = offsets;
493 
494 out:
495 	free(symbols);
496 	if (err)
497 		free(offsets);
498 	elf_close(&elf_fd);
499 	return err;
500 }
501 
502 /*
503  * Return offsets in @poffsets for symbols specified by @pattern argument.
504  * On success returns 0 and offsets are returned in allocated @poffsets
505  * array with the @pctn size, that needs to be released by the caller.
506  */
507 int elf_resolve_pattern_offsets(const char *binary_path, const char *pattern,
508 				unsigned long **poffsets, size_t *pcnt)
509 {
510 	int sh_types[2] = { SHT_SYMTAB, SHT_DYNSYM };
511 	unsigned long *offsets = NULL;
512 	size_t cap = 0, cnt = 0;
513 	struct elf_fd elf_fd;
514 	int err = 0, i;
515 
516 	err = elf_open(binary_path, &elf_fd);
517 	if (err)
518 		return err;
519 
520 	for (i = 0; i < ARRAY_SIZE(sh_types); i++) {
521 		struct elf_sym_iter iter;
522 		struct elf_sym *sym;
523 
524 		err = elf_sym_iter_new(&iter, elf_fd.elf, binary_path, sh_types[i], STT_FUNC);
525 		if (err == -ENOENT)
526 			continue;
527 		if (err)
528 			goto out;
529 
530 		while ((sym = elf_sym_iter_next(&iter))) {
531 			if (!glob_match(sym->name, pattern))
532 				continue;
533 
534 			err = libbpf_ensure_mem((void **) &offsets, &cap, sizeof(*offsets),
535 						cnt + 1);
536 			if (err)
537 				goto out;
538 
539 			offsets[cnt++] = elf_sym_offset(sym);
540 		}
541 
542 		/* If we found anything in the first symbol section,
543 		 * do not search others to avoid duplicates.
544 		 */
545 		if (cnt)
546 			break;
547 	}
548 
549 	if (cnt) {
550 		*poffsets = offsets;
551 		*pcnt = cnt;
552 	} else {
553 		err = -ENOENT;
554 	}
555 
556 out:
557 	if (err)
558 		free(offsets);
559 	elf_close(&elf_fd);
560 	return err;
561 }
562