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