libbfd.c 14 KB

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