symbol.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  1. #include "util.h"
  2. #include "../perf.h"
  3. #include "string.h"
  4. #include "symbol.h"
  5. #include <libelf.h>
  6. #include <gelf.h>
  7. #include <elf.h>
  8. #ifndef NO_DEMANGLE
  9. #include <bfd.h>
  10. #else
  11. static inline
  12. char *bfd_demangle(void __used *v, const char __used *c, int __used i)
  13. {
  14. return NULL;
  15. }
  16. #endif
  17. const char *sym_hist_filter;
  18. #ifndef DMGL_PARAMS
  19. #define DMGL_PARAMS (1 << 0) /* Include function args */
  20. #define DMGL_ANSI (1 << 1) /* Include const, volatile, etc */
  21. #endif
  22. static struct symbol *symbol__new(u64 start, u64 len,
  23. const char *name, unsigned int priv_size,
  24. u64 obj_start, int verbose)
  25. {
  26. size_t namelen = strlen(name) + 1;
  27. struct symbol *self = calloc(1, priv_size + sizeof(*self) + namelen);
  28. if (!self)
  29. return NULL;
  30. if (verbose >= 2)
  31. printf("new symbol: %016Lx [%08lx]: %s, hist: %p, obj_start: %p\n",
  32. (u64)start, (unsigned long)len, name, self->hist, (void *)(unsigned long)obj_start);
  33. self->obj_start= obj_start;
  34. self->hist = NULL;
  35. self->hist_sum = 0;
  36. if (sym_hist_filter && !strcmp(name, sym_hist_filter))
  37. self->hist = calloc(sizeof(u64), len);
  38. if (priv_size) {
  39. memset(self, 0, priv_size);
  40. self = ((void *)self) + priv_size;
  41. }
  42. self->start = start;
  43. self->end = len ? start + len - 1 : start;
  44. memcpy(self->name, name, namelen);
  45. return self;
  46. }
  47. static void symbol__delete(struct symbol *self, unsigned int priv_size)
  48. {
  49. free(((void *)self) - priv_size);
  50. }
  51. static size_t symbol__fprintf(struct symbol *self, FILE *fp)
  52. {
  53. if (!self->module)
  54. return fprintf(fp, " %llx-%llx %s\n",
  55. self->start, self->end, self->name);
  56. else
  57. return fprintf(fp, " %llx-%llx %s \t[%s]\n",
  58. self->start, self->end, self->name, self->module->name);
  59. }
  60. struct dso *dso__new(const char *name, unsigned int sym_priv_size)
  61. {
  62. struct dso *self = malloc(sizeof(*self) + strlen(name) + 1);
  63. if (self != NULL) {
  64. strcpy(self->name, name);
  65. self->syms = RB_ROOT;
  66. self->sym_priv_size = sym_priv_size;
  67. self->find_symbol = dso__find_symbol;
  68. self->slen_calculated = 0;
  69. }
  70. return self;
  71. }
  72. static void dso__delete_symbols(struct dso *self)
  73. {
  74. struct symbol *pos;
  75. struct rb_node *next = rb_first(&self->syms);
  76. while (next) {
  77. pos = rb_entry(next, struct symbol, rb_node);
  78. next = rb_next(&pos->rb_node);
  79. rb_erase(&pos->rb_node, &self->syms);
  80. symbol__delete(pos, self->sym_priv_size);
  81. }
  82. }
  83. void dso__delete(struct dso *self)
  84. {
  85. dso__delete_symbols(self);
  86. free(self);
  87. }
  88. static void dso__insert_symbol(struct dso *self, struct symbol *sym)
  89. {
  90. struct rb_node **p = &self->syms.rb_node;
  91. struct rb_node *parent = NULL;
  92. const u64 ip = sym->start;
  93. struct symbol *s;
  94. while (*p != NULL) {
  95. parent = *p;
  96. s = rb_entry(parent, struct symbol, rb_node);
  97. if (ip < s->start)
  98. p = &(*p)->rb_left;
  99. else
  100. p = &(*p)->rb_right;
  101. }
  102. rb_link_node(&sym->rb_node, parent, p);
  103. rb_insert_color(&sym->rb_node, &self->syms);
  104. }
  105. struct symbol *dso__find_symbol(struct dso *self, u64 ip)
  106. {
  107. struct rb_node *n;
  108. if (self == NULL)
  109. return NULL;
  110. n = self->syms.rb_node;
  111. while (n) {
  112. struct symbol *s = rb_entry(n, struct symbol, rb_node);
  113. if (ip < s->start)
  114. n = n->rb_left;
  115. else if (ip > s->end)
  116. n = n->rb_right;
  117. else
  118. return s;
  119. }
  120. return NULL;
  121. }
  122. size_t dso__fprintf(struct dso *self, FILE *fp)
  123. {
  124. size_t ret = fprintf(fp, "dso: %s\n", self->name);
  125. struct rb_node *nd;
  126. for (nd = rb_first(&self->syms); nd; nd = rb_next(nd)) {
  127. struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
  128. ret += symbol__fprintf(pos, fp);
  129. }
  130. return ret;
  131. }
  132. static int dso__load_kallsyms(struct dso *self, symbol_filter_t filter, int verbose)
  133. {
  134. struct rb_node *nd, *prevnd;
  135. char *line = NULL;
  136. size_t n;
  137. FILE *file = fopen("/proc/kallsyms", "r");
  138. int count = 0;
  139. if (file == NULL)
  140. goto out_failure;
  141. while (!feof(file)) {
  142. u64 start;
  143. struct symbol *sym;
  144. int line_len, len;
  145. char symbol_type;
  146. line_len = getline(&line, &n, file);
  147. if (line_len < 0)
  148. break;
  149. if (!line)
  150. goto out_failure;
  151. line[--line_len] = '\0'; /* \n */
  152. len = hex2u64(line, &start);
  153. len++;
  154. if (len + 2 >= line_len)
  155. continue;
  156. symbol_type = toupper(line[len]);
  157. /*
  158. * We're interested only in code ('T'ext)
  159. */
  160. if (symbol_type != 'T' && symbol_type != 'W')
  161. continue;
  162. /*
  163. * Well fix up the end later, when we have all sorted.
  164. */
  165. sym = symbol__new(start, 0xdead, line + len + 2,
  166. self->sym_priv_size, 0, verbose);
  167. if (sym == NULL)
  168. goto out_delete_line;
  169. if (filter && filter(self, sym))
  170. symbol__delete(sym, self->sym_priv_size);
  171. else {
  172. dso__insert_symbol(self, sym);
  173. count++;
  174. }
  175. }
  176. /*
  177. * Now that we have all sorted out, just set the ->end of all
  178. * symbols
  179. */
  180. prevnd = rb_first(&self->syms);
  181. if (prevnd == NULL)
  182. goto out_delete_line;
  183. for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
  184. struct symbol *prev = rb_entry(prevnd, struct symbol, rb_node),
  185. *curr = rb_entry(nd, struct symbol, rb_node);
  186. prev->end = curr->start - 1;
  187. prevnd = nd;
  188. }
  189. free(line);
  190. fclose(file);
  191. return count;
  192. out_delete_line:
  193. free(line);
  194. out_failure:
  195. return -1;
  196. }
  197. static int dso__load_perf_map(struct dso *self, symbol_filter_t filter, int verbose)
  198. {
  199. char *line = NULL;
  200. size_t n;
  201. FILE *file;
  202. int nr_syms = 0;
  203. file = fopen(self->name, "r");
  204. if (file == NULL)
  205. goto out_failure;
  206. while (!feof(file)) {
  207. u64 start, size;
  208. struct symbol *sym;
  209. int line_len, len;
  210. line_len = getline(&line, &n, file);
  211. if (line_len < 0)
  212. break;
  213. if (!line)
  214. goto out_failure;
  215. line[--line_len] = '\0'; /* \n */
  216. len = hex2u64(line, &start);
  217. len++;
  218. if (len + 2 >= line_len)
  219. continue;
  220. len += hex2u64(line + len, &size);
  221. len++;
  222. if (len + 2 >= line_len)
  223. continue;
  224. sym = symbol__new(start, size, line + len,
  225. self->sym_priv_size, start, verbose);
  226. if (sym == NULL)
  227. goto out_delete_line;
  228. if (filter && filter(self, sym))
  229. symbol__delete(sym, self->sym_priv_size);
  230. else {
  231. dso__insert_symbol(self, sym);
  232. nr_syms++;
  233. }
  234. }
  235. free(line);
  236. fclose(file);
  237. return nr_syms;
  238. out_delete_line:
  239. free(line);
  240. out_failure:
  241. return -1;
  242. }
  243. /**
  244. * elf_symtab__for_each_symbol - iterate thru all the symbols
  245. *
  246. * @self: struct elf_symtab instance to iterate
  247. * @index: uint32_t index
  248. * @sym: GElf_Sym iterator
  249. */
  250. #define elf_symtab__for_each_symbol(syms, nr_syms, index, sym) \
  251. for (index = 0, gelf_getsym(syms, index, &sym);\
  252. index < nr_syms; \
  253. index++, gelf_getsym(syms, index, &sym))
  254. static inline uint8_t elf_sym__type(const GElf_Sym *sym)
  255. {
  256. return GELF_ST_TYPE(sym->st_info);
  257. }
  258. static inline int elf_sym__is_function(const GElf_Sym *sym)
  259. {
  260. return elf_sym__type(sym) == STT_FUNC &&
  261. sym->st_name != 0 &&
  262. sym->st_shndx != SHN_UNDEF &&
  263. sym->st_size != 0;
  264. }
  265. static inline int elf_sym__is_label(const GElf_Sym *sym)
  266. {
  267. return elf_sym__type(sym) == STT_NOTYPE &&
  268. sym->st_name != 0 &&
  269. sym->st_shndx != SHN_UNDEF &&
  270. sym->st_shndx != SHN_ABS;
  271. }
  272. static inline const char *elf_sec__name(const GElf_Shdr *shdr,
  273. const Elf_Data *secstrs)
  274. {
  275. return secstrs->d_buf + shdr->sh_name;
  276. }
  277. static inline int elf_sec__is_text(const GElf_Shdr *shdr,
  278. const Elf_Data *secstrs)
  279. {
  280. return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
  281. }
  282. static inline const char *elf_sym__name(const GElf_Sym *sym,
  283. const Elf_Data *symstrs)
  284. {
  285. return symstrs->d_buf + sym->st_name;
  286. }
  287. static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
  288. GElf_Shdr *shp, const char *name,
  289. size_t *index)
  290. {
  291. Elf_Scn *sec = NULL;
  292. size_t cnt = 1;
  293. while ((sec = elf_nextscn(elf, sec)) != NULL) {
  294. char *str;
  295. gelf_getshdr(sec, shp);
  296. str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
  297. if (!strcmp(name, str)) {
  298. if (index)
  299. *index = cnt;
  300. break;
  301. }
  302. ++cnt;
  303. }
  304. return sec;
  305. }
  306. #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
  307. for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
  308. idx < nr_entries; \
  309. ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
  310. #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
  311. for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
  312. idx < nr_entries; \
  313. ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
  314. /*
  315. * We need to check if we have a .dynsym, so that we can handle the
  316. * .plt, synthesizing its symbols, that aren't on the symtabs (be it
  317. * .dynsym or .symtab).
  318. * And always look at the original dso, not at debuginfo packages, that
  319. * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
  320. */
  321. static int dso__synthesize_plt_symbols(struct dso *self, int verbose)
  322. {
  323. uint32_t nr_rel_entries, idx;
  324. GElf_Sym sym;
  325. u64 plt_offset;
  326. GElf_Shdr shdr_plt;
  327. struct symbol *f;
  328. GElf_Shdr shdr_rel_plt, shdr_dynsym;
  329. Elf_Data *reldata, *syms, *symstrs;
  330. Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
  331. size_t dynsym_idx;
  332. GElf_Ehdr ehdr;
  333. char sympltname[1024];
  334. Elf *elf;
  335. int nr = 0, symidx, fd, err = 0;
  336. fd = open(self->name, O_RDONLY);
  337. if (fd < 0)
  338. goto out;
  339. elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
  340. if (elf == NULL)
  341. goto out_close;
  342. if (gelf_getehdr(elf, &ehdr) == NULL)
  343. goto out_elf_end;
  344. scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
  345. ".dynsym", &dynsym_idx);
  346. if (scn_dynsym == NULL)
  347. goto out_elf_end;
  348. scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
  349. ".rela.plt", NULL);
  350. if (scn_plt_rel == NULL) {
  351. scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
  352. ".rel.plt", NULL);
  353. if (scn_plt_rel == NULL)
  354. goto out_elf_end;
  355. }
  356. err = -1;
  357. if (shdr_rel_plt.sh_link != dynsym_idx)
  358. goto out_elf_end;
  359. if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
  360. goto out_elf_end;
  361. /*
  362. * Fetch the relocation section to find the indexes to the GOT
  363. * and the symbols in the .dynsym they refer to.
  364. */
  365. reldata = elf_getdata(scn_plt_rel, NULL);
  366. if (reldata == NULL)
  367. goto out_elf_end;
  368. syms = elf_getdata(scn_dynsym, NULL);
  369. if (syms == NULL)
  370. goto out_elf_end;
  371. scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
  372. if (scn_symstrs == NULL)
  373. goto out_elf_end;
  374. symstrs = elf_getdata(scn_symstrs, NULL);
  375. if (symstrs == NULL)
  376. goto out_elf_end;
  377. nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
  378. plt_offset = shdr_plt.sh_offset;
  379. if (shdr_rel_plt.sh_type == SHT_RELA) {
  380. GElf_Rela pos_mem, *pos;
  381. elf_section__for_each_rela(reldata, pos, pos_mem, idx,
  382. nr_rel_entries) {
  383. symidx = GELF_R_SYM(pos->r_info);
  384. plt_offset += shdr_plt.sh_entsize;
  385. gelf_getsym(syms, symidx, &sym);
  386. snprintf(sympltname, sizeof(sympltname),
  387. "%s@plt", elf_sym__name(&sym, symstrs));
  388. f = symbol__new(plt_offset, shdr_plt.sh_entsize,
  389. sympltname, self->sym_priv_size, 0, verbose);
  390. if (!f)
  391. goto out_elf_end;
  392. dso__insert_symbol(self, f);
  393. ++nr;
  394. }
  395. } else if (shdr_rel_plt.sh_type == SHT_REL) {
  396. GElf_Rel pos_mem, *pos;
  397. elf_section__for_each_rel(reldata, pos, pos_mem, idx,
  398. nr_rel_entries) {
  399. symidx = GELF_R_SYM(pos->r_info);
  400. plt_offset += shdr_plt.sh_entsize;
  401. gelf_getsym(syms, symidx, &sym);
  402. snprintf(sympltname, sizeof(sympltname),
  403. "%s@plt", elf_sym__name(&sym, symstrs));
  404. f = symbol__new(plt_offset, shdr_plt.sh_entsize,
  405. sympltname, self->sym_priv_size, 0, verbose);
  406. if (!f)
  407. goto out_elf_end;
  408. dso__insert_symbol(self, f);
  409. ++nr;
  410. }
  411. }
  412. err = 0;
  413. out_elf_end:
  414. elf_end(elf);
  415. out_close:
  416. close(fd);
  417. if (err == 0)
  418. return nr;
  419. out:
  420. fprintf(stderr, "%s: problems reading %s PLT info.\n",
  421. __func__, self->name);
  422. return 0;
  423. }
  424. static int dso__load_sym(struct dso *self, int fd, const char *name,
  425. symbol_filter_t filter, int verbose, struct module *mod)
  426. {
  427. Elf_Data *symstrs, *secstrs;
  428. uint32_t nr_syms;
  429. int err = -1;
  430. uint32_t index;
  431. GElf_Ehdr ehdr;
  432. GElf_Shdr shdr;
  433. Elf_Data *syms;
  434. GElf_Sym sym;
  435. Elf_Scn *sec, *sec_strndx;
  436. Elf *elf;
  437. int nr = 0, kernel = !strcmp("[kernel]", self->name);
  438. elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
  439. if (elf == NULL) {
  440. if (verbose)
  441. fprintf(stderr, "%s: cannot read %s ELF file.\n",
  442. __func__, name);
  443. goto out_close;
  444. }
  445. if (gelf_getehdr(elf, &ehdr) == NULL) {
  446. if (verbose)
  447. fprintf(stderr, "%s: cannot get elf header.\n", __func__);
  448. goto out_elf_end;
  449. }
  450. sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
  451. if (sec == NULL) {
  452. sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
  453. if (sec == NULL)
  454. goto out_elf_end;
  455. }
  456. syms = elf_getdata(sec, NULL);
  457. if (syms == NULL)
  458. goto out_elf_end;
  459. sec = elf_getscn(elf, shdr.sh_link);
  460. if (sec == NULL)
  461. goto out_elf_end;
  462. symstrs = elf_getdata(sec, NULL);
  463. if (symstrs == NULL)
  464. goto out_elf_end;
  465. sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
  466. if (sec_strndx == NULL)
  467. goto out_elf_end;
  468. secstrs = elf_getdata(sec_strndx, NULL);
  469. if (secstrs == NULL)
  470. goto out_elf_end;
  471. nr_syms = shdr.sh_size / shdr.sh_entsize;
  472. memset(&sym, 0, sizeof(sym));
  473. if (!kernel) {
  474. self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
  475. elf_section_by_name(elf, &ehdr, &shdr,
  476. ".gnu.prelink_undo",
  477. NULL) != NULL);
  478. } else self->adjust_symbols = 0;
  479. elf_symtab__for_each_symbol(syms, nr_syms, index, sym) {
  480. struct symbol *f;
  481. const char *name;
  482. char *demangled;
  483. u64 obj_start;
  484. struct section *section = NULL;
  485. int is_label = elf_sym__is_label(&sym);
  486. const char *section_name;
  487. if (!is_label && !elf_sym__is_function(&sym))
  488. continue;
  489. sec = elf_getscn(elf, sym.st_shndx);
  490. if (!sec)
  491. goto out_elf_end;
  492. gelf_getshdr(sec, &shdr);
  493. if (is_label && !elf_sec__is_text(&shdr, secstrs))
  494. continue;
  495. section_name = elf_sec__name(&shdr, secstrs);
  496. obj_start = sym.st_value;
  497. if (self->adjust_symbols) {
  498. if (verbose >= 2)
  499. printf("adjusting symbol: st_value: %Lx sh_addr: %Lx sh_offset: %Lx\n",
  500. (u64)sym.st_value, (u64)shdr.sh_addr, (u64)shdr.sh_offset);
  501. sym.st_value -= shdr.sh_addr - shdr.sh_offset;
  502. }
  503. if (mod) {
  504. section = mod->sections->find_section(mod->sections, section_name);
  505. if (section)
  506. sym.st_value += section->vma;
  507. else {
  508. fprintf(stderr, "dso__load_sym() module %s lookup of %s failed\n",
  509. mod->name, section_name);
  510. goto out_elf_end;
  511. }
  512. }
  513. /*
  514. * We need to figure out if the object was created from C++ sources
  515. * DWARF DW_compile_unit has this, but we don't always have access
  516. * to it...
  517. */
  518. name = elf_sym__name(&sym, symstrs);
  519. demangled = bfd_demangle(NULL, name, DMGL_PARAMS | DMGL_ANSI);
  520. if (demangled != NULL)
  521. name = demangled;
  522. f = symbol__new(sym.st_value, sym.st_size, name,
  523. self->sym_priv_size, obj_start, verbose);
  524. free(demangled);
  525. if (!f)
  526. goto out_elf_end;
  527. if (filter && filter(self, f))
  528. symbol__delete(f, self->sym_priv_size);
  529. else {
  530. f->module = mod;
  531. dso__insert_symbol(self, f);
  532. nr++;
  533. }
  534. }
  535. err = nr;
  536. out_elf_end:
  537. elf_end(elf);
  538. out_close:
  539. return err;
  540. }
  541. #define BUILD_ID_SIZE 128
  542. static char *dso__read_build_id(struct dso *self, int verbose)
  543. {
  544. int i;
  545. GElf_Ehdr ehdr;
  546. GElf_Shdr shdr;
  547. Elf_Data *build_id_data;
  548. Elf_Scn *sec;
  549. char *build_id = NULL, *bid;
  550. unsigned char *raw;
  551. Elf *elf;
  552. int fd = open(self->name, O_RDONLY);
  553. if (fd < 0)
  554. goto out;
  555. elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
  556. if (elf == NULL) {
  557. if (verbose)
  558. fprintf(stderr, "%s: cannot read %s ELF file.\n",
  559. __func__, self->name);
  560. goto out_close;
  561. }
  562. if (gelf_getehdr(elf, &ehdr) == NULL) {
  563. if (verbose)
  564. fprintf(stderr, "%s: cannot get elf header.\n", __func__);
  565. goto out_elf_end;
  566. }
  567. sec = elf_section_by_name(elf, &ehdr, &shdr, ".note.gnu.build-id", NULL);
  568. if (sec == NULL)
  569. goto out_elf_end;
  570. build_id_data = elf_getdata(sec, NULL);
  571. if (build_id_data == NULL)
  572. goto out_elf_end;
  573. build_id = malloc(BUILD_ID_SIZE);
  574. if (build_id == NULL)
  575. goto out_elf_end;
  576. raw = build_id_data->d_buf + 16;
  577. bid = build_id;
  578. for (i = 0; i < 20; ++i) {
  579. sprintf(bid, "%02x", *raw);
  580. ++raw;
  581. bid += 2;
  582. }
  583. if (verbose)
  584. printf("%s(%s): %s\n", __func__, self->name, build_id);
  585. out_elf_end:
  586. elf_end(elf);
  587. out_close:
  588. close(fd);
  589. out:
  590. return build_id;
  591. }
  592. int dso__load(struct dso *self, symbol_filter_t filter, int verbose)
  593. {
  594. int size = PATH_MAX;
  595. char *name = malloc(size), *build_id = NULL;
  596. int variant = 0;
  597. int ret = -1;
  598. int fd;
  599. if (!name)
  600. return -1;
  601. self->adjust_symbols = 0;
  602. if (strncmp(self->name, "/tmp/perf-", 10) == 0)
  603. return dso__load_perf_map(self, filter, verbose);
  604. more:
  605. do {
  606. switch (variant) {
  607. case 0: /* Fedora */
  608. snprintf(name, size, "/usr/lib/debug%s.debug", self->name);
  609. break;
  610. case 1: /* Ubuntu */
  611. snprintf(name, size, "/usr/lib/debug%s", self->name);
  612. break;
  613. case 2:
  614. build_id = dso__read_build_id(self, verbose);
  615. if (build_id != NULL) {
  616. snprintf(name, size,
  617. "/usr/lib/debug/.build-id/%.2s/%s.debug",
  618. build_id, build_id + 2);
  619. free(build_id);
  620. break;
  621. }
  622. variant++;
  623. /* Fall thru */
  624. case 3: /* Sane people */
  625. snprintf(name, size, "%s", self->name);
  626. break;
  627. default:
  628. goto out;
  629. }
  630. variant++;
  631. fd = open(name, O_RDONLY);
  632. } while (fd < 0);
  633. ret = dso__load_sym(self, fd, name, filter, verbose, NULL);
  634. close(fd);
  635. /*
  636. * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
  637. */
  638. if (!ret)
  639. goto more;
  640. if (ret > 0) {
  641. int nr_plt = dso__synthesize_plt_symbols(self, verbose);
  642. if (nr_plt > 0)
  643. ret += nr_plt;
  644. }
  645. out:
  646. free(name);
  647. return ret;
  648. }
  649. static int dso__load_module(struct dso *self, struct mod_dso *mods, const char *name,
  650. symbol_filter_t filter, int verbose)
  651. {
  652. struct module *mod = mod_dso__find_module(mods, name);
  653. int err = 0, fd;
  654. if (mod == NULL || !mod->active)
  655. return err;
  656. fd = open(mod->path, O_RDONLY);
  657. if (fd < 0)
  658. return err;
  659. err = dso__load_sym(self, fd, name, filter, verbose, mod);
  660. close(fd);
  661. return err;
  662. }
  663. int dso__load_modules(struct dso *self, symbol_filter_t filter, int verbose)
  664. {
  665. struct mod_dso *mods = mod_dso__new_dso("modules");
  666. struct module *pos;
  667. struct rb_node *next;
  668. int err;
  669. err = mod_dso__load_modules(mods);
  670. if (err <= 0)
  671. return err;
  672. /*
  673. * Iterate over modules, and load active symbols.
  674. */
  675. next = rb_first(&mods->mods);
  676. while (next) {
  677. pos = rb_entry(next, struct module, rb_node);
  678. err = dso__load_module(self, mods, pos->name, filter, verbose);
  679. if (err < 0)
  680. break;
  681. next = rb_next(&pos->rb_node);
  682. }
  683. if (err < 0) {
  684. mod_dso__delete_modules(mods);
  685. mod_dso__delete_self(mods);
  686. }
  687. return err;
  688. }
  689. static inline void dso__fill_symbol_holes(struct dso *self)
  690. {
  691. struct symbol *prev = NULL;
  692. struct rb_node *nd;
  693. for (nd = rb_last(&self->syms); nd; nd = rb_prev(nd)) {
  694. struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
  695. if (prev) {
  696. u64 hole = 0;
  697. int alias = pos->start == prev->start;
  698. if (!alias)
  699. hole = prev->start - pos->end - 1;
  700. if (hole || alias) {
  701. if (alias)
  702. pos->end = prev->end;
  703. else if (hole)
  704. pos->end = prev->start - 1;
  705. }
  706. }
  707. prev = pos;
  708. }
  709. }
  710. static int dso__load_vmlinux(struct dso *self, const char *vmlinux,
  711. symbol_filter_t filter, int verbose)
  712. {
  713. int err, fd = open(vmlinux, O_RDONLY);
  714. if (fd < 0)
  715. return -1;
  716. err = dso__load_sym(self, fd, vmlinux, filter, verbose, NULL);
  717. if (err > 0)
  718. dso__fill_symbol_holes(self);
  719. close(fd);
  720. return err;
  721. }
  722. int dso__load_kernel(struct dso *self, const char *vmlinux,
  723. symbol_filter_t filter, int verbose, int modules)
  724. {
  725. int err = -1;
  726. if (vmlinux) {
  727. err = dso__load_vmlinux(self, vmlinux, filter, verbose);
  728. if (err > 0 && modules)
  729. err = dso__load_modules(self, filter, verbose);
  730. }
  731. if (err <= 0)
  732. err = dso__load_kallsyms(self, filter, verbose);
  733. return err;
  734. }
  735. void symbol__init(void)
  736. {
  737. elf_version(EV_CURRENT);
  738. }