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