symbol.c 14 KB

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