symbol.c 12 KB

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