xref: /linux/tools/perf/util/libbfd.c (revision 998ccc327b14c03861247540ff6f8135a5283621)
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,bool block)421 int libbfd__read_build_id(const char *filename, struct build_id *bid, bool block)
422 {
423 	size_t size = sizeof(bid->data);
424 	int err = -1, fd;
425 	bfd *abfd;
426 
427 	fd = open(filename, block ? O_RDONLY : (O_RDONLY | O_NONBLOCK));
428 	if (fd < 0)
429 		return -1;
430 
431 	ensure_bfd_init();
432 	abfd = bfd_fdopenr(filename, /*target=*/NULL, fd);
433 	if (!abfd)
434 		return -1;
435 
436 	if (!bfd_check_format(abfd, bfd_object)) {
437 		pr_debug2("%s: cannot read %s bfd file.\n", __func__, filename);
438 		goto out_close;
439 	}
440 
441 	if (!abfd->build_id || abfd->build_id->size > size)
442 		goto out_close;
443 
444 	memcpy(bid->data, abfd->build_id->data, abfd->build_id->size);
445 	memset(bid->data + abfd->build_id->size, 0, size - abfd->build_id->size);
446 	err = bid->size = abfd->build_id->size;
447 
448 out_close:
449 	bfd_close(abfd);
450 	return err;
451 }
452 
libbfd_filename__read_debuglink(const char * filename,char * debuglink,size_t size)453 int libbfd_filename__read_debuglink(const char *filename, char *debuglink,
454 				    size_t size)
455 {
456 	int err = -1;
457 	asection *section;
458 	bfd *abfd;
459 
460 	ensure_bfd_init();
461 	abfd = bfd_openr(filename, NULL);
462 	if (!abfd)
463 		return -1;
464 
465 	if (!bfd_check_format(abfd, bfd_object)) {
466 		pr_debug2("%s: cannot read %s bfd file.\n", __func__, filename);
467 		goto out_close;
468 	}
469 
470 	section = bfd_get_section_by_name(abfd, ".gnu_debuglink");
471 	if (!section)
472 		goto out_close;
473 
474 	if (section->size > size)
475 		goto out_close;
476 
477 	if (!bfd_get_section_contents(abfd, section, debuglink, 0,
478 				      section->size))
479 		goto out_close;
480 
481 	err = 0;
482 
483 out_close:
484 	bfd_close(abfd);
485 	return err;
486 }
487 
symbol__disassemble_bpf_libbfd(struct symbol * sym __maybe_unused,struct annotate_args * args __maybe_unused)488 int symbol__disassemble_bpf_libbfd(struct symbol *sym __maybe_unused,
489 				   struct annotate_args *args  __maybe_unused)
490 {
491 #ifdef HAVE_LIBBPF_SUPPORT
492 	struct annotation *notes = symbol__annotation(sym);
493 	struct bpf_prog_linfo *prog_linfo = NULL;
494 	struct bpf_prog_info_node *info_node;
495 	int len = sym->end - sym->start;
496 	disassembler_ftype disassemble;
497 	struct map *map = args->ms.map;
498 	struct perf_bpil *info_linear;
499 	struct disassemble_info info;
500 	struct dso *dso = map__dso(map);
501 	int pc = 0, count, sub_id;
502 	struct btf *btf = NULL;
503 	char tpath[PATH_MAX];
504 	size_t buf_size;
505 	int nr_skip = 0;
506 	char *buf;
507 	bfd *bfdf;
508 	int ret;
509 	FILE *s;
510 
511 	if (dso__binary_type(dso) != DSO_BINARY_TYPE__BPF_PROG_INFO)
512 		return SYMBOL_ANNOTATE_ERRNO__BPF_INVALID_FILE;
513 
514 	pr_debug("%s: handling sym %s addr %" PRIx64 " len %" PRIx64 "\n", __func__,
515 		  sym->name, sym->start, sym->end - sym->start);
516 
517 	memset(tpath, 0, sizeof(tpath));
518 	perf_exe(tpath, sizeof(tpath));
519 
520 	ensure_bfd_init();
521 	bfdf = bfd_openr(tpath, NULL);
522 	if (bfdf == NULL)
523 		abort();
524 
525 	if (!bfd_check_format(bfdf, bfd_object))
526 		abort();
527 
528 	s = open_memstream(&buf, &buf_size);
529 	if (!s) {
530 		ret = errno;
531 		goto out;
532 	}
533 	init_disassemble_info_compat(&info, s,
534 				     (fprintf_ftype) fprintf,
535 				     fprintf_styled);
536 	info.arch = bfd_get_arch(bfdf);
537 	info.mach = bfd_get_mach(bfdf);
538 
539 	info_node = perf_env__find_bpf_prog_info(dso__bpf_prog(dso)->env,
540 						 dso__bpf_prog(dso)->id);
541 	if (!info_node) {
542 		ret = SYMBOL_ANNOTATE_ERRNO__BPF_MISSING_BTF;
543 		goto out;
544 	}
545 	info_linear = info_node->info_linear;
546 	sub_id = dso__bpf_prog(dso)->sub_id;
547 
548 	info.buffer = (void *)(uintptr_t)(info_linear->info.jited_prog_insns);
549 	info.buffer_length = info_linear->info.jited_prog_len;
550 
551 	if (info_linear->info.nr_line_info)
552 		prog_linfo = bpf_prog_linfo__new(&info_linear->info);
553 
554 	if (info_linear->info.btf_id) {
555 		struct btf_node *node;
556 
557 		node = perf_env__find_btf(dso__bpf_prog(dso)->env,
558 					  info_linear->info.btf_id);
559 		if (node)
560 			btf = btf__new((__u8 *)(node->data),
561 				       node->data_size);
562 	}
563 
564 	disassemble_init_for_target(&info);
565 
566 #ifdef DISASM_FOUR_ARGS_SIGNATURE
567 	disassemble = disassembler(info.arch,
568 				   bfd_big_endian(bfdf),
569 				   info.mach,
570 				   bfdf);
571 #else
572 	disassemble = disassembler(bfdf);
573 #endif
574 	if (disassemble == NULL)
575 		abort();
576 
577 	fflush(s);
578 	do {
579 		const struct bpf_line_info *linfo = NULL;
580 		struct disasm_line *dl;
581 		size_t prev_buf_size;
582 		const char *srcline;
583 		u64 addr;
584 
585 		addr = pc + ((u64 *)(uintptr_t)(info_linear->info.jited_ksyms))[sub_id];
586 		count = disassemble(pc, &info);
587 
588 		if (prog_linfo)
589 			linfo = bpf_prog_linfo__lfind_addr_func(prog_linfo,
590 								addr, sub_id,
591 								nr_skip);
592 
593 		if (linfo && btf) {
594 			srcline = btf__name_by_offset(btf, linfo->line_off);
595 			nr_skip++;
596 		} else
597 			srcline = NULL;
598 
599 		fprintf(s, "\n");
600 		prev_buf_size = buf_size;
601 		fflush(s);
602 
603 		if (!annotate_opts.hide_src_code && srcline) {
604 			args->offset = -1;
605 			args->line = strdup(srcline);
606 			args->line_nr = 0;
607 			args->fileloc = NULL;
608 			args->ms.sym  = sym;
609 			dl = disasm_line__new(args);
610 			if (dl) {
611 				annotation_line__add(&dl->al,
612 						     &notes->src->source);
613 			}
614 		}
615 
616 		args->offset = pc;
617 		args->line = buf + prev_buf_size;
618 		args->line_nr = 0;
619 		args->fileloc = NULL;
620 		args->ms.sym  = sym;
621 		dl = disasm_line__new(args);
622 		if (dl)
623 			annotation_line__add(&dl->al, &notes->src->source);
624 
625 		pc += count;
626 	} while (count > 0 && pc < len);
627 
628 	ret = 0;
629 out:
630 	free(prog_linfo);
631 	btf__free(btf);
632 	fclose(s);
633 	bfd_close(bfdf);
634 	return ret;
635 #else
636 	return SYMBOL_ANNOTATE_ERRNO__NO_LIBOPCODES_FOR_BPF;
637 #endif
638 }
639