symbol.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  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. const char *sym_hist_filter;
  9. static struct symbol *symbol__new(u64 start, u64 len,
  10. const char *name, unsigned int priv_size,
  11. u64 obj_start, int verbose)
  12. {
  13. size_t namelen = strlen(name) + 1;
  14. struct symbol *self = calloc(1, priv_size + sizeof(*self) + namelen);
  15. if (!self)
  16. return NULL;
  17. if (verbose >= 2)
  18. printf("new symbol: %016Lx [%08lx]: %s, hist: %p, obj_start: %p\n",
  19. (u64)start, (unsigned long)len, name, self->hist, (void *)(unsigned long)obj_start);
  20. self->obj_start= obj_start;
  21. self->hist = NULL;
  22. self->hist_sum = 0;
  23. if (sym_hist_filter && !strcmp(name, sym_hist_filter))
  24. self->hist = calloc(sizeof(u64), len);
  25. if (priv_size) {
  26. memset(self, 0, priv_size);
  27. self = ((void *)self) + priv_size;
  28. }
  29. self->start = start;
  30. self->end = start + len - 1;
  31. memcpy(self->name, name, namelen);
  32. return self;
  33. }
  34. static void symbol__delete(struct symbol *self, unsigned int priv_size)
  35. {
  36. free(((void *)self) - priv_size);
  37. }
  38. static size_t symbol__fprintf(struct symbol *self, FILE *fp)
  39. {
  40. return fprintf(fp, " %llx-%llx %s\n",
  41. self->start, self->end, self->name);
  42. }
  43. struct dso *dso__new(const char *name, unsigned int sym_priv_size)
  44. {
  45. struct dso *self = malloc(sizeof(*self) + strlen(name) + 1);
  46. if (self != NULL) {
  47. strcpy(self->name, name);
  48. self->syms = RB_ROOT;
  49. self->sym_priv_size = sym_priv_size;
  50. self->find_symbol = dso__find_symbol;
  51. }
  52. return self;
  53. }
  54. static void dso__delete_symbols(struct dso *self)
  55. {
  56. struct symbol *pos;
  57. struct rb_node *next = rb_first(&self->syms);
  58. while (next) {
  59. pos = rb_entry(next, struct symbol, rb_node);
  60. next = rb_next(&pos->rb_node);
  61. rb_erase(&pos->rb_node, &self->syms);
  62. symbol__delete(pos, self->sym_priv_size);
  63. }
  64. }
  65. void dso__delete(struct dso *self)
  66. {
  67. dso__delete_symbols(self);
  68. free(self);
  69. }
  70. static void dso__insert_symbol(struct dso *self, struct symbol *sym)
  71. {
  72. struct rb_node **p = &self->syms.rb_node;
  73. struct rb_node *parent = NULL;
  74. const u64 ip = sym->start;
  75. struct symbol *s;
  76. while (*p != NULL) {
  77. parent = *p;
  78. s = rb_entry(parent, struct symbol, rb_node);
  79. if (ip < s->start)
  80. p = &(*p)->rb_left;
  81. else
  82. p = &(*p)->rb_right;
  83. }
  84. rb_link_node(&sym->rb_node, parent, p);
  85. rb_insert_color(&sym->rb_node, &self->syms);
  86. }
  87. struct symbol *dso__find_symbol(struct dso *self, u64 ip)
  88. {
  89. struct rb_node *n;
  90. if (self == NULL)
  91. return NULL;
  92. n = self->syms.rb_node;
  93. while (n) {
  94. struct symbol *s = rb_entry(n, struct symbol, rb_node);
  95. if (ip < s->start)
  96. n = n->rb_left;
  97. else if (ip > s->end)
  98. n = n->rb_right;
  99. else
  100. return s;
  101. }
  102. return NULL;
  103. }
  104. size_t dso__fprintf(struct dso *self, FILE *fp)
  105. {
  106. size_t ret = fprintf(fp, "dso: %s\n", self->name);
  107. struct rb_node *nd;
  108. for (nd = rb_first(&self->syms); nd; nd = rb_next(nd)) {
  109. struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
  110. ret += symbol__fprintf(pos, fp);
  111. }
  112. return ret;
  113. }
  114. static int dso__load_kallsyms(struct dso *self, symbol_filter_t filter, int verbose)
  115. {
  116. struct rb_node *nd, *prevnd;
  117. char *line = NULL;
  118. size_t n;
  119. FILE *file = fopen("/proc/kallsyms", "r");
  120. if (file == NULL)
  121. goto out_failure;
  122. while (!feof(file)) {
  123. u64 start;
  124. struct symbol *sym;
  125. int line_len, len;
  126. char symbol_type;
  127. line_len = getline(&line, &n, file);
  128. if (line_len < 0)
  129. break;
  130. if (!line)
  131. goto out_failure;
  132. line[--line_len] = '\0'; /* \n */
  133. len = hex2u64(line, &start);
  134. len++;
  135. if (len + 2 >= line_len)
  136. continue;
  137. symbol_type = toupper(line[len]);
  138. /*
  139. * We're interested only in code ('T'ext)
  140. */
  141. if (symbol_type != 'T' && symbol_type != 'W')
  142. continue;
  143. /*
  144. * Well fix up the end later, when we have all sorted.
  145. */
  146. sym = symbol__new(start, 0xdead, line + len + 2,
  147. self->sym_priv_size, 0, verbose);
  148. if (sym == NULL)
  149. goto out_delete_line;
  150. if (filter && filter(self, sym))
  151. symbol__delete(sym, self->sym_priv_size);
  152. else
  153. dso__insert_symbol(self, sym);
  154. }
  155. /*
  156. * Now that we have all sorted out, just set the ->end of all
  157. * symbols
  158. */
  159. prevnd = rb_first(&self->syms);
  160. if (prevnd == NULL)
  161. goto out_delete_line;
  162. for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
  163. struct symbol *prev = rb_entry(prevnd, struct symbol, rb_node),
  164. *curr = rb_entry(nd, struct symbol, rb_node);
  165. prev->end = curr->start - 1;
  166. prevnd = nd;
  167. }
  168. free(line);
  169. fclose(file);
  170. return 0;
  171. out_delete_line:
  172. free(line);
  173. out_failure:
  174. return -1;
  175. }
  176. static int dso__load_perf_map(struct dso *self, symbol_filter_t filter, int verbose)
  177. {
  178. char *line = NULL;
  179. size_t n;
  180. FILE *file;
  181. int nr_syms = 0;
  182. file = fopen(self->name, "r");
  183. if (file == NULL)
  184. goto out_failure;
  185. while (!feof(file)) {
  186. u64 start, size;
  187. struct symbol *sym;
  188. int line_len, len;
  189. line_len = getline(&line, &n, file);
  190. if (line_len < 0)
  191. break;
  192. if (!line)
  193. goto out_failure;
  194. line[--line_len] = '\0'; /* \n */
  195. len = hex2u64(line, &start);
  196. len++;
  197. if (len + 2 >= line_len)
  198. continue;
  199. len += hex2u64(line + len, &size);
  200. len++;
  201. if (len + 2 >= line_len)
  202. continue;
  203. sym = symbol__new(start, size, line + len,
  204. self->sym_priv_size, start, verbose);
  205. if (sym == NULL)
  206. goto out_delete_line;
  207. if (filter && filter(self, sym))
  208. symbol__delete(sym, self->sym_priv_size);
  209. else {
  210. dso__insert_symbol(self, sym);
  211. nr_syms++;
  212. }
  213. }
  214. free(line);
  215. fclose(file);
  216. return nr_syms;
  217. out_delete_line:
  218. free(line);
  219. out_failure:
  220. return -1;
  221. }
  222. /**
  223. * elf_symtab__for_each_symbol - iterate thru all the symbols
  224. *
  225. * @self: struct elf_symtab instance to iterate
  226. * @index: uint32_t index
  227. * @sym: GElf_Sym iterator
  228. */
  229. #define elf_symtab__for_each_symbol(syms, nr_syms, index, sym) \
  230. for (index = 0, gelf_getsym(syms, index, &sym);\
  231. index < nr_syms; \
  232. index++, gelf_getsym(syms, index, &sym))
  233. static inline uint8_t elf_sym__type(const GElf_Sym *sym)
  234. {
  235. return GELF_ST_TYPE(sym->st_info);
  236. }
  237. static inline int elf_sym__is_function(const GElf_Sym *sym)
  238. {
  239. return elf_sym__type(sym) == STT_FUNC &&
  240. sym->st_name != 0 &&
  241. sym->st_shndx != SHN_UNDEF &&
  242. sym->st_size != 0;
  243. }
  244. static inline const char *elf_sym__name(const GElf_Sym *sym,
  245. const Elf_Data *symstrs)
  246. {
  247. return symstrs->d_buf + sym->st_name;
  248. }
  249. static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
  250. GElf_Shdr *shp, const char *name,
  251. size_t *index)
  252. {
  253. Elf_Scn *sec = NULL;
  254. size_t cnt = 1;
  255. while ((sec = elf_nextscn(elf, sec)) != NULL) {
  256. char *str;
  257. gelf_getshdr(sec, shp);
  258. str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
  259. if (!strcmp(name, str)) {
  260. if (index)
  261. *index = cnt;
  262. break;
  263. }
  264. ++cnt;
  265. }
  266. return sec;
  267. }
  268. #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
  269. for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
  270. idx < nr_entries; \
  271. ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
  272. #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
  273. for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
  274. idx < nr_entries; \
  275. ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
  276. static int dso__synthesize_plt_symbols(struct dso *self, Elf *elf,
  277. GElf_Ehdr *ehdr, Elf_Scn *scn_dynsym,
  278. GElf_Shdr *shdr_dynsym,
  279. size_t dynsym_idx, int verbose)
  280. {
  281. uint32_t nr_rel_entries, idx;
  282. GElf_Sym sym;
  283. u64 plt_offset;
  284. GElf_Shdr shdr_plt;
  285. struct symbol *f;
  286. GElf_Shdr shdr_rel_plt;
  287. Elf_Data *reldata, *syms, *symstrs;
  288. Elf_Scn *scn_plt_rel, *scn_symstrs;
  289. char sympltname[1024];
  290. int nr = 0, symidx;
  291. scn_plt_rel = elf_section_by_name(elf, ehdr, &shdr_rel_plt,
  292. ".rela.plt", NULL);
  293. if (scn_plt_rel == NULL) {
  294. scn_plt_rel = elf_section_by_name(elf, ehdr, &shdr_rel_plt,
  295. ".rel.plt", NULL);
  296. if (scn_plt_rel == NULL)
  297. return 0;
  298. }
  299. if (shdr_rel_plt.sh_link != dynsym_idx)
  300. return 0;
  301. if (elf_section_by_name(elf, ehdr, &shdr_plt, ".plt", NULL) == NULL)
  302. return 0;
  303. /*
  304. * Fetch the relocation section to find the indexes to the GOT
  305. * and the symbols in the .dynsym they refer to.
  306. */
  307. reldata = elf_getdata(scn_plt_rel, NULL);
  308. if (reldata == NULL)
  309. return -1;
  310. syms = elf_getdata(scn_dynsym, NULL);
  311. if (syms == NULL)
  312. return -1;
  313. scn_symstrs = elf_getscn(elf, shdr_dynsym->sh_link);
  314. if (scn_symstrs == NULL)
  315. return -1;
  316. symstrs = elf_getdata(scn_symstrs, NULL);
  317. if (symstrs == NULL)
  318. return -1;
  319. nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
  320. plt_offset = shdr_plt.sh_offset;
  321. if (shdr_rel_plt.sh_type == SHT_RELA) {
  322. GElf_Rela pos_mem, *pos;
  323. elf_section__for_each_rela(reldata, pos, pos_mem, idx,
  324. nr_rel_entries) {
  325. symidx = GELF_R_SYM(pos->r_info);
  326. plt_offset += shdr_plt.sh_entsize;
  327. gelf_getsym(syms, symidx, &sym);
  328. snprintf(sympltname, sizeof(sympltname),
  329. "%s@plt", elf_sym__name(&sym, symstrs));
  330. f = symbol__new(plt_offset, shdr_plt.sh_entsize,
  331. sympltname, self->sym_priv_size, 0, verbose);
  332. if (!f)
  333. return -1;
  334. dso__insert_symbol(self, f);
  335. ++nr;
  336. }
  337. } else if (shdr_rel_plt.sh_type == SHT_REL) {
  338. GElf_Rel pos_mem, *pos;
  339. elf_section__for_each_rel(reldata, pos, pos_mem, idx,
  340. nr_rel_entries) {
  341. symidx = GELF_R_SYM(pos->r_info);
  342. plt_offset += shdr_plt.sh_entsize;
  343. gelf_getsym(syms, symidx, &sym);
  344. snprintf(sympltname, sizeof(sympltname),
  345. "%s@plt", elf_sym__name(&sym, symstrs));
  346. f = symbol__new(plt_offset, shdr_plt.sh_entsize,
  347. sympltname, self->sym_priv_size, 0, verbose);
  348. if (!f)
  349. return -1;
  350. dso__insert_symbol(self, f);
  351. ++nr;
  352. }
  353. } else {
  354. /*
  355. * TODO: There are still one more shdr_rel_plt.sh_type
  356. * I have to investigate, but probably should be ignored.
  357. */
  358. }
  359. return nr;
  360. }
  361. static int dso__load_sym(struct dso *self, int fd, const char *name,
  362. symbol_filter_t filter, int verbose)
  363. {
  364. Elf_Data *symstrs;
  365. uint32_t nr_syms;
  366. int err = -1;
  367. uint32_t index;
  368. GElf_Ehdr ehdr;
  369. GElf_Shdr shdr;
  370. Elf_Data *syms;
  371. GElf_Sym sym;
  372. Elf_Scn *sec, *sec_dynsym;
  373. Elf *elf;
  374. size_t dynsym_idx;
  375. int nr = 0;
  376. elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
  377. if (elf == NULL) {
  378. if (verbose)
  379. fprintf(stderr, "%s: cannot read %s ELF file.\n",
  380. __func__, name);
  381. goto out_close;
  382. }
  383. if (gelf_getehdr(elf, &ehdr) == NULL) {
  384. if (verbose)
  385. fprintf(stderr, "%s: cannot get elf header.\n", __func__);
  386. goto out_elf_end;
  387. }
  388. /*
  389. * We need to check if we have a .dynsym, so that we can handle the
  390. * .plt, synthesizing its symbols, that aren't on the symtabs (be it
  391. * .dynsym or .symtab)
  392. */
  393. sec_dynsym = elf_section_by_name(elf, &ehdr, &shdr,
  394. ".dynsym", &dynsym_idx);
  395. if (sec_dynsym != NULL) {
  396. nr = dso__synthesize_plt_symbols(self, elf, &ehdr,
  397. sec_dynsym, &shdr,
  398. dynsym_idx, verbose);
  399. if (nr < 0)
  400. goto out_elf_end;
  401. }
  402. /*
  403. * But if we have a full .symtab (that is a superset of .dynsym) we
  404. * should add the symbols not in the .dynsyn
  405. */
  406. sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
  407. if (sec == NULL) {
  408. if (sec_dynsym == NULL)
  409. goto out_elf_end;
  410. sec = sec_dynsym;
  411. gelf_getshdr(sec, &shdr);
  412. }
  413. syms = elf_getdata(sec, NULL);
  414. if (syms == NULL)
  415. goto out_elf_end;
  416. sec = elf_getscn(elf, shdr.sh_link);
  417. if (sec == NULL)
  418. goto out_elf_end;
  419. symstrs = elf_getdata(sec, NULL);
  420. if (symstrs == NULL)
  421. goto out_elf_end;
  422. nr_syms = shdr.sh_size / shdr.sh_entsize;
  423. memset(&sym, 0, sizeof(sym));
  424. self->prelinked = elf_section_by_name(elf, &ehdr, &shdr,
  425. ".gnu.prelink_undo",
  426. NULL) != NULL;
  427. elf_symtab__for_each_symbol(syms, nr_syms, index, sym) {
  428. struct symbol *f;
  429. u64 obj_start;
  430. if (!elf_sym__is_function(&sym))
  431. continue;
  432. sec = elf_getscn(elf, sym.st_shndx);
  433. if (!sec)
  434. goto out_elf_end;
  435. gelf_getshdr(sec, &shdr);
  436. obj_start = sym.st_value;
  437. if (self->prelinked) {
  438. if (verbose >= 2)
  439. printf("adjusting symbol: st_value: %Lx sh_addr: %Lx sh_offset: %Lx\n",
  440. (u64)sym.st_value, (u64)shdr.sh_addr, (u64)shdr.sh_offset);
  441. sym.st_value -= shdr.sh_addr - shdr.sh_offset;
  442. }
  443. f = symbol__new(sym.st_value, sym.st_size,
  444. elf_sym__name(&sym, symstrs),
  445. self->sym_priv_size, obj_start, verbose);
  446. if (!f)
  447. goto out_elf_end;
  448. if (filter && filter(self, f))
  449. symbol__delete(f, self->sym_priv_size);
  450. else {
  451. dso__insert_symbol(self, f);
  452. nr++;
  453. }
  454. }
  455. err = nr;
  456. out_elf_end:
  457. elf_end(elf);
  458. out_close:
  459. return err;
  460. }
  461. int dso__load(struct dso *self, symbol_filter_t filter, int verbose)
  462. {
  463. int size = strlen(self->name) + sizeof("/usr/lib/debug%s.debug");
  464. char *name = malloc(size);
  465. int variant = 0;
  466. int ret = -1;
  467. int fd;
  468. if (!name)
  469. return -1;
  470. self->prelinked = 0;
  471. if (strncmp(self->name, "/tmp/perf-", 10) == 0)
  472. return dso__load_perf_map(self, filter, verbose);
  473. more:
  474. do {
  475. switch (variant) {
  476. case 0: /* Fedora */
  477. snprintf(name, size, "/usr/lib/debug%s.debug", self->name);
  478. break;
  479. case 1: /* Ubuntu */
  480. snprintf(name, size, "/usr/lib/debug%s", self->name);
  481. break;
  482. case 2: /* Sane people */
  483. snprintf(name, size, "%s", self->name);
  484. break;
  485. default:
  486. goto out;
  487. }
  488. variant++;
  489. fd = open(name, O_RDONLY);
  490. } while (fd < 0);
  491. ret = dso__load_sym(self, fd, name, filter, verbose);
  492. close(fd);
  493. /*
  494. * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
  495. */
  496. if (!ret)
  497. goto more;
  498. out:
  499. free(name);
  500. return ret;
  501. }
  502. static int dso__load_vmlinux(struct dso *self, const char *vmlinux,
  503. symbol_filter_t filter, int verbose)
  504. {
  505. int err, fd = open(vmlinux, O_RDONLY);
  506. if (fd < 0)
  507. return -1;
  508. err = dso__load_sym(self, fd, vmlinux, filter, verbose);
  509. close(fd);
  510. return err;
  511. }
  512. int dso__load_kernel(struct dso *self, const char *vmlinux,
  513. symbol_filter_t filter, int verbose)
  514. {
  515. int err = -1;
  516. if (vmlinux)
  517. err = dso__load_vmlinux(self, vmlinux, filter, verbose);
  518. if (err < 0)
  519. err = dso__load_kallsyms(self, filter, verbose);
  520. return err;
  521. }
  522. void symbol__init(void)
  523. {
  524. elf_version(EV_CURRENT);
  525. }