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