symbol.c 12 KB

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