symbol.c 18 KB

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