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