xref: /linux/tools/perf/util/libbfd.c (revision 473f6c8f437b049f8ec015d57cd59bb983b1d85c)
1 // SPDX-License-Identifier: GPL-2.0
2 #include "libbfd.h"
3 #include "annotate.h"
4 #include "bpf-event.h"
5 #include "bpf-utils.h"
6 #include "debug.h"
7 #include "dso.h"
8 #include "env.h"
9 #include "map.h"
10 #include "srcline.h"
11 #include "symbol.h"
12 #include "symbol_conf.h"
13 #include "util.h"
14 #include <tools/dis-asm-compat.h>
15 #ifdef HAVE_LIBBPF_SUPPORT
16 #include <bpf/bpf.h>
17 #include <bpf/btf.h>
18 #include <bpf/libbpf.h>
19 #endif
20 #include <fcntl.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #define PACKAGE "perf"
24 #include <bfd.h>
25 
26 /*
27  * Implement addr2line using libbfd.
28  */
29 struct a2l_data {
30 	const char *input;
31 	u64 addr;
32 
33 	bool found;
34 	const char *filename;
35 	const char *funcname;
36 	unsigned int line;
37 
38 	bfd *abfd;
39 	asymbol **syms;
40 };
41 
42 static bool perf_bfd_lock(void *bfd_mutex)
43 {
44 	mutex_lock(bfd_mutex);
45 	return true;
46 }
47 
48 static bool perf_bfd_unlock(void *bfd_mutex)
49 {
50 	mutex_unlock(bfd_mutex);
51 	return true;
52 }
53 
54 static void perf_bfd_init(void)
55 {
56 	static struct mutex bfd_mutex;
57 
58 	mutex_init_recursive(&bfd_mutex);
59 
60 	if (bfd_init() != BFD_INIT_MAGIC) {
61 		pr_err("Error initializing libbfd\n");
62 		return;
63 	}
64 	if (!bfd_thread_init(perf_bfd_lock, perf_bfd_unlock, &bfd_mutex))
65 		pr_err("Error initializing libbfd threading\n");
66 }
67 
68 static void ensure_bfd_init(void)
69 {
70 	static pthread_once_t bfd_init_once = PTHREAD_ONCE_INIT;
71 
72 	pthread_once(&bfd_init_once, perf_bfd_init);
73 }
74 
75 static int bfd_error(const char *string)
76 {
77 	const char *errmsg;
78 
79 	errmsg = bfd_errmsg(bfd_get_error());
80 	fflush(stdout);
81 
82 	if (string)
83 		pr_debug("%s: %s\n", string, errmsg);
84 	else
85 		pr_debug("%s\n", errmsg);
86 
87 	return -1;
88 }
89 
90 static int slurp_symtab(bfd *abfd, struct a2l_data *a2l)
91 {
92 	long storage;
93 	long symcount;
94 	asymbol **syms;
95 	bool dynamic = false;
96 
97 	if ((bfd_get_file_flags(abfd) & HAS_SYMS) == 0)
98 		return bfd_error(bfd_get_filename(abfd));
99 
100 	storage = bfd_get_symtab_upper_bound(abfd);
101 	if (storage == 0L) {
102 		storage = bfd_get_dynamic_symtab_upper_bound(abfd);
103 		dynamic = true;
104 	}
105 	if (storage < 0L)
106 		return bfd_error(bfd_get_filename(abfd));
107 
108 	syms = malloc(storage);
109 	if (dynamic)
110 		symcount = bfd_canonicalize_dynamic_symtab(abfd, syms);
111 	else
112 		symcount = bfd_canonicalize_symtab(abfd, syms);
113 
114 	if (symcount < 0) {
115 		free(syms);
116 		return bfd_error(bfd_get_filename(abfd));
117 	}
118 
119 	a2l->syms = syms;
120 	return 0;
121 }
122 
123 static void find_address_in_section(bfd *abfd, asection *section, void *data)
124 {
125 	bfd_vma pc, vma;
126 	bfd_size_type size;
127 	struct a2l_data *a2l = data;
128 	flagword flags;
129 
130 	if (a2l->found)
131 		return;
132 
133 #ifdef bfd_get_section_flags
134 	flags = bfd_get_section_flags(abfd, section);
135 #else
136 	flags = bfd_section_flags(section);
137 #endif
138 	if ((flags & SEC_ALLOC) == 0)
139 		return;
140 
141 	pc = a2l->addr;
142 #ifdef bfd_get_section_vma
143 	vma = bfd_get_section_vma(abfd, section);
144 #else
145 	vma = bfd_section_vma(section);
146 #endif
147 #ifdef bfd_get_section_size
148 	size = bfd_get_section_size(section);
149 #else
150 	size = bfd_section_size(section);
151 #endif
152 
153 	if (pc < vma || pc >= vma + size)
154 		return;
155 
156 	a2l->found = bfd_find_nearest_line(abfd, section, a2l->syms, pc - vma,
157 					   &a2l->filename, &a2l->funcname,
158 					   &a2l->line);
159 
160 	if (a2l->filename && !strlen(a2l->filename))
161 		a2l->filename = NULL;
162 }
163 
164 static struct a2l_data *addr2line_init(const char *path)
165 {
166 	bfd *abfd;
167 	struct a2l_data *a2l = NULL;
168 
169 	ensure_bfd_init();
170 	abfd = bfd_openr(path, NULL);
171 	if (abfd == NULL)
172 		return NULL;
173 
174 	if (!bfd_check_format(abfd, bfd_object))
175 		goto out;
176 
177 	a2l = zalloc(sizeof(*a2l));
178 	if (a2l == NULL)
179 		goto out;
180 
181 	a2l->abfd = abfd;
182 	a2l->input = strdup(path);
183 	if (a2l->input == NULL)
184 		goto out;
185 
186 	if (slurp_symtab(abfd, a2l))
187 		goto out;
188 
189 	return a2l;
190 
191 out:
192 	if (a2l) {
193 		zfree((char **)&a2l->input);
194 		free(a2l);
195 	}
196 	bfd_close(abfd);
197 	return NULL;
198 }
199 
200 static void addr2line_cleanup(struct a2l_data *a2l)
201 {
202 	if (a2l->abfd)
203 		bfd_close(a2l->abfd);
204 	zfree((char **)&a2l->input);
205 	zfree(&a2l->syms);
206 	free(a2l);
207 }
208 
209 static int inline_list__append_dso_a2l(struct dso *dso,
210 				       struct inline_node *node,
211 				       struct symbol *sym)
212 {
213 	struct a2l_data *a2l = dso__a2l(dso);
214 	struct symbol *inline_sym = new_inline_sym(dso, sym, a2l->funcname);
215 	char *srcline = NULL;
216 
217 	if (a2l->filename)
218 		srcline = srcline_from_fileline(a2l->filename, a2l->line);
219 
220 	return inline_list__append(inline_sym, srcline, node);
221 }
222 
223 int libbfd__addr2line(const char *dso_name, u64 addr,
224 		      char **file, unsigned int *line, struct dso *dso,
225 		      bool unwind_inlines, struct inline_node *node,
226 		      struct symbol *sym)
227 {
228 	int ret = 0;
229 	struct a2l_data *a2l = dso__a2l(dso);
230 
231 	if (!a2l) {
232 		a2l = addr2line_init(dso_name);
233 		dso__set_a2l(dso, a2l);
234 	}
235 
236 	if (a2l == NULL) {
237 		if (!symbol_conf.addr2line_disable_warn)
238 			pr_warning("addr2line_init failed for %s\n", dso_name);
239 		return 0;
240 	}
241 
242 	a2l->addr = addr;
243 	a2l->found = false;
244 
245 	bfd_map_over_sections(a2l->abfd, find_address_in_section, a2l);
246 
247 	if (!a2l->found)
248 		return 0;
249 
250 	if (unwind_inlines) {
251 		int cnt = 0;
252 
253 		if (node && inline_list__append_dso_a2l(dso, node, sym))
254 			return 0;
255 
256 		while (bfd_find_inliner_info(a2l->abfd, &a2l->filename,
257 					     &a2l->funcname, &a2l->line) &&
258 		       cnt++ < MAX_INLINE_NEST) {
259 
260 			if (a2l->filename && !strlen(a2l->filename))
261 				a2l->filename = NULL;
262 
263 			if (node != NULL) {
264 				if (inline_list__append_dso_a2l(dso, node, sym))
265 					return 0;
266 				// found at least one inline frame
267 				ret = 1;
268 			}
269 		}
270 	}
271 
272 	if (file) {
273 		*file = a2l->filename ? strdup(a2l->filename) : NULL;
274 		ret = *file ? 1 : 0;
275 	}
276 
277 	if (line)
278 		*line = a2l->line;
279 
280 	return ret;
281 }
282 
283 void dso__free_a2l_libbfd(struct dso *dso)
284 {
285 	struct a2l_data *a2l = dso__a2l(dso);
286 
287 	if (!a2l)
288 		return;
289 
290 	addr2line_cleanup(a2l);
291 
292 	dso__set_a2l(dso, NULL);
293 }
294 
295 static int bfd_symbols__cmpvalue(const void *a, const void *b)
296 {
297 	const asymbol *as = *(const asymbol **)a, *bs = *(const asymbol **)b;
298 
299 	if (bfd_asymbol_value(as) != bfd_asymbol_value(bs))
300 		return bfd_asymbol_value(as) - bfd_asymbol_value(bs);
301 
302 	return bfd_asymbol_name(as)[0] - bfd_asymbol_name(bs)[0];
303 }
304 
305 static int bfd2elf_binding(asymbol *symbol)
306 {
307 	if (symbol->flags & BSF_WEAK)
308 		return STB_WEAK;
309 	if (symbol->flags & BSF_GLOBAL)
310 		return STB_GLOBAL;
311 	if (symbol->flags & BSF_LOCAL)
312 		return STB_LOCAL;
313 	return -1;
314 }
315 
316 int dso__load_bfd_symbols(struct dso *dso, const char *debugfile)
317 {
318 	int err = -1;
319 	long symbols_size, symbols_count, i;
320 	asection *section;
321 	asymbol **symbols, *sym;
322 	struct symbol *symbol;
323 	bfd *abfd;
324 	u64 start, len;
325 
326 	ensure_bfd_init();
327 	abfd = bfd_openr(debugfile, NULL);
328 	if (!abfd)
329 		return -1;
330 
331 	if (!bfd_check_format(abfd, bfd_object)) {
332 		pr_debug2("%s: cannot read %s bfd file.\n", __func__,
333 			  dso__long_name(dso));
334 		goto out_close;
335 	}
336 
337 	if (bfd_get_flavour(abfd) == bfd_target_elf_flavour)
338 		goto out_close;
339 
340 	symbols_size = bfd_get_symtab_upper_bound(abfd);
341 	if (symbols_size == 0) {
342 		bfd_close(abfd);
343 		return 0;
344 	}
345 
346 	if (symbols_size < 0)
347 		goto out_close;
348 
349 	symbols = malloc(symbols_size);
350 	if (!symbols)
351 		goto out_close;
352 
353 	symbols_count = bfd_canonicalize_symtab(abfd, symbols);
354 	if (symbols_count < 0)
355 		goto out_free;
356 
357 	section = bfd_get_section_by_name(abfd, ".text");
358 	if (section) {
359 		for (i = 0; i < symbols_count; ++i) {
360 			if (!strcmp(bfd_asymbol_name(symbols[i]), "__ImageBase") ||
361 			    !strcmp(bfd_asymbol_name(symbols[i]), "__image_base__"))
362 				break;
363 		}
364 		if (i < symbols_count) {
365 			/* PE symbols can only have 4 bytes, so use .text high bits */
366 			u64 text_offset = (section->vma - (u32)section->vma)
367 				+ (u32)bfd_asymbol_value(symbols[i]);
368 			dso__set_text_offset(dso, text_offset);
369 			dso__set_text_end(dso, (section->vma - text_offset) + section->size);
370 		} else {
371 			dso__set_text_offset(dso, section->vma - section->filepos);
372 			dso__set_text_end(dso, section->filepos + section->size);
373 		}
374 	}
375 
376 	qsort(symbols, symbols_count, sizeof(asymbol *), bfd_symbols__cmpvalue);
377 
378 #ifdef bfd_get_section
379 #define bfd_asymbol_section bfd_get_section
380 #endif
381 	for (i = 0; i < symbols_count; ++i) {
382 		sym = symbols[i];
383 		section = bfd_asymbol_section(sym);
384 		if (bfd2elf_binding(sym) < 0)
385 			continue;
386 
387 		while (i + 1 < symbols_count &&
388 		       bfd_asymbol_section(symbols[i + 1]) == section &&
389 		       bfd2elf_binding(symbols[i + 1]) < 0)
390 			i++;
391 
392 		if (i + 1 < symbols_count &&
393 		    bfd_asymbol_section(symbols[i + 1]) == section)
394 			len = symbols[i + 1]->value - sym->value;
395 		else
396 			len = section->size - sym->value;
397 
398 		start = bfd_asymbol_value(sym) - dso__text_offset(dso);
399 		symbol = symbol__new(start, len, bfd2elf_binding(sym), STT_FUNC,
400 				     bfd_asymbol_name(sym));
401 		if (!symbol)
402 			goto out_free;
403 
404 		symbols__insert(dso__symbols(dso), symbol);
405 	}
406 #ifdef bfd_get_section
407 #undef bfd_asymbol_section
408 #endif
409 
410 	symbols__fixup_end(dso__symbols(dso), false);
411 	symbols__fixup_duplicate(dso__symbols(dso));
412 	dso__set_adjust_symbols(dso, true);
413 
414 	err = 0;
415 out_free:
416 	free(symbols);
417 out_close:
418 	bfd_close(abfd);
419 	return err;
420 }
421 
422 int libbfd__read_build_id(const char *filename, struct build_id *bid)
423 {
424 	size_t size = sizeof(bid->data);
425 	int err = -1, fd;
426 	bfd *abfd;
427 
428 	if (!filename)
429 		return -EFAULT;
430 
431 	errno = 0;
432 	if (!is_regular_file(filename))
433 		return errno == 0 ? -EWOULDBLOCK : -errno;
434 
435 	fd = open(filename, O_RDONLY);
436 	if (fd < 0)
437 		return -1;
438 
439 	ensure_bfd_init();
440 	abfd = bfd_fdopenr(filename, /*target=*/NULL, fd);
441 	if (!abfd)
442 		return -1;
443 
444 	if (!bfd_check_format(abfd, bfd_object)) {
445 		pr_debug2("%s: cannot read %s bfd file.\n", __func__, filename);
446 		goto out_close;
447 	}
448 
449 	if (!abfd->build_id || abfd->build_id->size > size)
450 		goto out_close;
451 
452 	memcpy(bid->data, abfd->build_id->data, abfd->build_id->size);
453 	memset(bid->data + abfd->build_id->size, 0, size - abfd->build_id->size);
454 	err = bid->size = abfd->build_id->size;
455 
456 out_close:
457 	bfd_close(abfd);
458 	return err;
459 }
460 
461 int libbfd_filename__read_debuglink(const char *filename, char *debuglink,
462 				    size_t size)
463 {
464 	int err = -1;
465 	asection *section;
466 	bfd *abfd;
467 
468 	ensure_bfd_init();
469 	abfd = bfd_openr(filename, NULL);
470 	if (!abfd)
471 		return -1;
472 
473 	if (!bfd_check_format(abfd, bfd_object)) {
474 		pr_debug2("%s: cannot read %s bfd file.\n", __func__, filename);
475 		goto out_close;
476 	}
477 
478 	section = bfd_get_section_by_name(abfd, ".gnu_debuglink");
479 	if (!section)
480 		goto out_close;
481 
482 	if (section->size > size)
483 		goto out_close;
484 
485 	if (!bfd_get_section_contents(abfd, section, debuglink, 0,
486 				      section->size))
487 		goto out_close;
488 
489 	err = 0;
490 
491 out_close:
492 	bfd_close(abfd);
493 	return err;
494 }
495 
496 int symbol__disassemble_bpf_libbfd(struct symbol *sym __maybe_unused,
497 				   struct annotate_args *args  __maybe_unused)
498 {
499 #ifdef HAVE_LIBBPF_SUPPORT
500 	struct annotation *notes = symbol__annotation(sym);
501 	struct bpf_prog_linfo *prog_linfo = NULL;
502 	struct bpf_prog_info_node *info_node;
503 	int len = sym->end - sym->start;
504 	disassembler_ftype disassemble;
505 	struct map *map = args->ms->map;
506 	struct perf_bpil *info_linear;
507 	struct disassemble_info info;
508 	struct dso *dso = map__dso(map);
509 	int pc = 0, count, sub_id;
510 	struct btf *btf = NULL;
511 	char tpath[PATH_MAX];
512 	size_t buf_size;
513 	int nr_skip = 0;
514 	char *buf = NULL;
515 	bfd *bfdf;
516 	int ret;
517 	FILE *s;
518 
519 	if (dso__binary_type(dso) != DSO_BINARY_TYPE__BPF_PROG_INFO)
520 		return SYMBOL_ANNOTATE_ERRNO__BPF_INVALID_FILE;
521 
522 	pr_debug("%s: handling sym %s addr %" PRIx64 " len %" PRIx64 "\n", __func__,
523 		  sym->name, sym->start, sym->end - sym->start);
524 
525 	memset(tpath, 0, sizeof(tpath));
526 	perf_exe(tpath, sizeof(tpath));
527 
528 	ensure_bfd_init();
529 	bfdf = bfd_openr(tpath, NULL);
530 	if (bfdf == NULL)
531 		abort();
532 
533 	if (!bfd_check_format(bfdf, bfd_object))
534 		abort();
535 
536 	s = open_memstream(&buf, &buf_size);
537 	if (!s) {
538 		ret = errno;
539 		goto out;
540 	}
541 	init_disassemble_info_compat(&info, s,
542 				     (fprintf_ftype) fprintf,
543 				     fprintf_styled);
544 	info.arch = bfd_get_arch(bfdf);
545 	info.mach = bfd_get_mach(bfdf);
546 
547 	info_node = perf_env__find_bpf_prog_info(dso__bpf_prog(dso)->env,
548 						 dso__bpf_prog(dso)->id);
549 	if (!info_node) {
550 		ret = SYMBOL_ANNOTATE_ERRNO__BPF_MISSING_BTF;
551 		goto out;
552 	}
553 	info_linear = info_node->info_linear;
554 	sub_id = dso__bpf_prog(dso)->sub_id;
555 
556 	/* jited_prog_insns is only valid if bpil_offs_to_addr() converted it */
557 	if (!(info_linear->arrays & (1UL << PERF_BPIL_JITED_INSNS))) {
558 		ret = SYMBOL_ANNOTATE_ERRNO__BPF_MISSING_BTF;
559 		goto out;
560 	}
561 	info.buffer = (void *)(uintptr_t)(info_linear->info.jited_prog_insns);
562 	info.buffer_length = info_linear->info.jited_prog_len;
563 
564 	if (info_linear->info.nr_line_info)
565 		prog_linfo = bpf_prog_linfo__new(&info_linear->info);
566 
567 	if (info_linear->info.btf_id) {
568 		struct btf_node *node;
569 
570 		node = perf_env__find_btf(dso__bpf_prog(dso)->env,
571 					  info_linear->info.btf_id);
572 		if (node)
573 			btf = btf__new((__u8 *)(node->data),
574 				       node->data_size);
575 	}
576 
577 	disassemble_init_for_target(&info);
578 
579 #ifdef DISASM_FOUR_ARGS_SIGNATURE
580 	disassemble = disassembler(info.arch,
581 				   bfd_big_endian(bfdf),
582 				   info.mach,
583 				   bfdf);
584 #else
585 	disassemble = disassembler(bfdf);
586 #endif
587 	if (disassemble == NULL)
588 		abort();
589 
590 	/* jited_ksyms is only valid if bpil_offs_to_addr() converted it */
591 	if (!(info_linear->arrays & (1UL << PERF_BPIL_JITED_KSYMS))) {
592 		ret = SYMBOL_ANNOTATE_ERRNO__BPF_MISSING_BTF;
593 		goto out;
594 	}
595 
596 	fflush(s);
597 	do {
598 		const struct bpf_line_info *linfo = NULL;
599 		struct disasm_line *dl;
600 		size_t prev_buf_size;
601 		const char *srcline;
602 		u64 addr;
603 
604 		addr = pc + ((u64 *)(uintptr_t)(info_linear->info.jited_ksyms))[sub_id];
605 		count = disassemble(pc, &info);
606 
607 		if (prog_linfo)
608 			linfo = bpf_prog_linfo__lfind_addr_func(prog_linfo,
609 								addr, sub_id,
610 								nr_skip);
611 
612 		if (linfo && btf) {
613 			srcline = btf__name_by_offset(btf, linfo->line_off);
614 			nr_skip++;
615 		} else
616 			srcline = NULL;
617 
618 		fprintf(s, "\n");
619 		prev_buf_size = buf_size;
620 		fflush(s);
621 
622 		if (!annotate_opts.hide_src_code && srcline) {
623 			args->offset = -1;
624 			args->line = (char *)srcline;
625 			args->line_nr = 0;
626 			args->fileloc = NULL;
627 			args->ms->sym = sym;
628 			dl = disasm_line__new(args);
629 			if (dl) {
630 				annotation_line__add(&dl->al,
631 						     &notes->src->source);
632 			}
633 		}
634 
635 		args->offset = pc;
636 		args->line = buf + prev_buf_size;
637 		args->line_nr = 0;
638 		args->fileloc = NULL;
639 		args->ms->sym = sym;
640 		dl = disasm_line__new(args);
641 		if (dl)
642 			annotation_line__add(&dl->al, &notes->src->source);
643 
644 		pc += count;
645 	} while (count > 0 && pc < len);
646 
647 	ret = 0;
648 out:
649 	bpf_prog_linfo__free(prog_linfo);
650 	btf__free(btf);
651 	if (s) {
652 		fclose(s);
653 		free(buf);
654 	}
655 	bfd_close(bfdf);
656 	return ret;
657 #else
658 	return SYMBOL_ANNOTATE_ERRNO__NO_LIBOPCODES_FOR_BPF;
659 #endif
660 }
661