symbol.c 21 KB

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