symbol.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277
  1. #include "util.h"
  2. #include "../perf.h"
  3. #include "string.h"
  4. #include "symbol.h"
  5. #include "thread.h"
  6. #include "debug.h"
  7. #include <libelf.h>
  8. #include <gelf.h>
  9. #include <elf.h>
  10. #include <sys/utsname.h>
  11. const char *sym_hist_filter;
  12. enum dso_origin {
  13. DSO__ORIG_KERNEL = 0,
  14. DSO__ORIG_JAVA_JIT,
  15. DSO__ORIG_FEDORA,
  16. DSO__ORIG_UBUNTU,
  17. DSO__ORIG_BUILDID,
  18. DSO__ORIG_DSO,
  19. DSO__ORIG_KMODULE,
  20. DSO__ORIG_NOT_FOUND,
  21. };
  22. static void dsos__add(struct dso *dso);
  23. static struct dso *dsos__find(const char *name);
  24. static struct rb_root kernel_maps;
  25. static void dso__set_symbols_end(struct dso *self)
  26. {
  27. struct rb_node *nd, *prevnd = rb_first(&self->syms);
  28. if (prevnd == NULL)
  29. return;
  30. for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
  31. struct symbol *prev = rb_entry(prevnd, struct symbol, rb_node),
  32. *curr = rb_entry(nd, struct symbol, rb_node);
  33. if (prev->end == prev->start)
  34. prev->end = curr->start - 1;
  35. prevnd = nd;
  36. }
  37. }
  38. static void kernel_maps__fixup_sym_end(void)
  39. {
  40. struct map *prev, *curr;
  41. struct rb_node *nd, *prevnd = rb_first(&kernel_maps);
  42. if (prevnd == NULL)
  43. return;
  44. curr = rb_entry(prevnd, struct map, rb_node);
  45. dso__set_symbols_end(curr->dso);
  46. for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
  47. prev = curr;
  48. curr = rb_entry(nd, struct map, rb_node);
  49. prev->end = curr->start - 1;
  50. dso__set_symbols_end(curr->dso);
  51. }
  52. }
  53. static struct symbol *symbol__new(u64 start, u64 len, const char *name,
  54. unsigned int priv_size, int v)
  55. {
  56. size_t namelen = strlen(name) + 1;
  57. struct symbol *self = calloc(1, priv_size + sizeof(*self) + namelen);
  58. if (!self)
  59. return NULL;
  60. if (v >= 2)
  61. printf("new symbol: %016Lx [%08lx]: %s, hist: %p\n",
  62. start, (unsigned long)len, name, self->hist);
  63. self->hist = NULL;
  64. self->hist_sum = 0;
  65. if (sym_hist_filter && !strcmp(name, sym_hist_filter))
  66. self->hist = calloc(sizeof(u64), len);
  67. if (priv_size) {
  68. memset(self, 0, priv_size);
  69. self = ((void *)self) + priv_size;
  70. }
  71. self->start = start;
  72. self->end = len ? start + len - 1 : start;
  73. memcpy(self->name, name, namelen);
  74. return self;
  75. }
  76. static void symbol__delete(struct symbol *self, unsigned int priv_size)
  77. {
  78. free(((void *)self) - priv_size);
  79. }
  80. static size_t symbol__fprintf(struct symbol *self, FILE *fp)
  81. {
  82. return fprintf(fp, " %llx-%llx %s\n",
  83. self->start, self->end, self->name);
  84. }
  85. struct dso *dso__new(const char *name, unsigned int sym_priv_size)
  86. {
  87. struct dso *self = malloc(sizeof(*self) + strlen(name) + 1);
  88. if (self != NULL) {
  89. strcpy(self->name, name);
  90. self->long_name = self->name;
  91. self->short_name = self->name;
  92. self->syms = RB_ROOT;
  93. self->sym_priv_size = sym_priv_size;
  94. self->find_symbol = dso__find_symbol;
  95. self->slen_calculated = 0;
  96. self->origin = DSO__ORIG_NOT_FOUND;
  97. }
  98. return self;
  99. }
  100. static void dso__delete_symbols(struct dso *self)
  101. {
  102. struct symbol *pos;
  103. struct rb_node *next = rb_first(&self->syms);
  104. while (next) {
  105. pos = rb_entry(next, struct symbol, rb_node);
  106. next = rb_next(&pos->rb_node);
  107. rb_erase(&pos->rb_node, &self->syms);
  108. symbol__delete(pos, self->sym_priv_size);
  109. }
  110. }
  111. void dso__delete(struct dso *self)
  112. {
  113. dso__delete_symbols(self);
  114. if (self->long_name != self->name)
  115. free(self->long_name);
  116. free(self);
  117. }
  118. static void dso__insert_symbol(struct dso *self, struct symbol *sym)
  119. {
  120. struct rb_node **p = &self->syms.rb_node;
  121. struct rb_node *parent = NULL;
  122. const u64 ip = sym->start;
  123. struct symbol *s;
  124. while (*p != NULL) {
  125. parent = *p;
  126. s = rb_entry(parent, struct symbol, rb_node);
  127. if (ip < s->start)
  128. p = &(*p)->rb_left;
  129. else
  130. p = &(*p)->rb_right;
  131. }
  132. rb_link_node(&sym->rb_node, parent, p);
  133. rb_insert_color(&sym->rb_node, &self->syms);
  134. }
  135. struct symbol *dso__find_symbol(struct dso *self, u64 ip)
  136. {
  137. struct rb_node *n;
  138. if (self == NULL)
  139. return NULL;
  140. n = self->syms.rb_node;
  141. while (n) {
  142. struct symbol *s = rb_entry(n, struct symbol, rb_node);
  143. if (ip < s->start)
  144. n = n->rb_left;
  145. else if (ip > s->end)
  146. n = n->rb_right;
  147. else
  148. return s;
  149. }
  150. return NULL;
  151. }
  152. size_t dso__fprintf(struct dso *self, FILE *fp)
  153. {
  154. size_t ret = fprintf(fp, "dso: %s\n", self->short_name);
  155. struct rb_node *nd;
  156. for (nd = rb_first(&self->syms); nd; nd = rb_next(nd)) {
  157. struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
  158. ret += symbol__fprintf(pos, fp);
  159. }
  160. return ret;
  161. }
  162. static int maps__load_kallsyms(symbol_filter_t filter, int use_modules, int v)
  163. {
  164. struct map *map = kernel_map;
  165. char *line = NULL;
  166. size_t n;
  167. FILE *file = fopen("/proc/kallsyms", "r");
  168. int count = 0;
  169. if (file == NULL)
  170. goto out_failure;
  171. while (!feof(file)) {
  172. u64 start;
  173. struct symbol *sym;
  174. int line_len, len;
  175. char symbol_type;
  176. char *module, *symbol_name;
  177. line_len = getline(&line, &n, file);
  178. if (line_len < 0)
  179. break;
  180. if (!line)
  181. goto out_failure;
  182. line[--line_len] = '\0'; /* \n */
  183. len = hex2u64(line, &start);
  184. len++;
  185. if (len + 2 >= line_len)
  186. continue;
  187. symbol_type = toupper(line[len]);
  188. /*
  189. * We're interested only in code ('T'ext)
  190. */
  191. if (symbol_type != 'T' && symbol_type != 'W')
  192. continue;
  193. symbol_name = line + len + 2;
  194. module = strchr(symbol_name, '\t');
  195. if (module) {
  196. char *module_name_end;
  197. if (!use_modules)
  198. continue;
  199. *module = '\0';
  200. module = strchr(module + 1, '[');
  201. if (!module)
  202. continue;
  203. module_name_end = strchr(module + 1, ']');
  204. if (!module_name_end)
  205. continue;
  206. *(module_name_end + 1) = '\0';
  207. if (strcmp(map->dso->name, module)) {
  208. map = kernel_maps__find_by_dso_name(module);
  209. if (!map) {
  210. fputs("/proc/{kallsyms,modules} "
  211. "inconsistency!\n", stderr);
  212. return -1;
  213. }
  214. }
  215. start = map->map_ip(map, start);
  216. } else
  217. map = kernel_map;
  218. /*
  219. * Well fix up the end later, when we have all sorted.
  220. */
  221. sym = symbol__new(start, 0, symbol_name,
  222. map->dso->sym_priv_size, v);
  223. if (sym == NULL)
  224. goto out_delete_line;
  225. if (filter && filter(map, sym))
  226. symbol__delete(sym, map->dso->sym_priv_size);
  227. else {
  228. dso__insert_symbol(map->dso, sym);
  229. count++;
  230. }
  231. }
  232. free(line);
  233. fclose(file);
  234. return count;
  235. out_delete_line:
  236. free(line);
  237. out_failure:
  238. return -1;
  239. }
  240. static size_t kernel_maps__fprintf(FILE *fp)
  241. {
  242. size_t printed = fprintf(stderr, "Kernel maps:\n");
  243. struct rb_node *nd;
  244. printed += map__fprintf(kernel_map, fp);
  245. printed += dso__fprintf(kernel_map->dso, fp);
  246. for (nd = rb_first(&kernel_maps); nd; nd = rb_next(nd)) {
  247. struct map *pos = rb_entry(nd, struct map, rb_node);
  248. printed += map__fprintf(pos, fp);
  249. printed += dso__fprintf(pos->dso, fp);
  250. }
  251. return printed + fprintf(stderr, "END kernel maps\n");
  252. }
  253. static int dso__load_perf_map(struct dso *self, struct map *map,
  254. symbol_filter_t filter, int v)
  255. {
  256. char *line = NULL;
  257. size_t n;
  258. FILE *file;
  259. int nr_syms = 0;
  260. file = fopen(self->long_name, "r");
  261. if (file == NULL)
  262. goto out_failure;
  263. while (!feof(file)) {
  264. u64 start, size;
  265. struct symbol *sym;
  266. int line_len, len;
  267. line_len = getline(&line, &n, file);
  268. if (line_len < 0)
  269. break;
  270. if (!line)
  271. goto out_failure;
  272. line[--line_len] = '\0'; /* \n */
  273. len = hex2u64(line, &start);
  274. len++;
  275. if (len + 2 >= line_len)
  276. continue;
  277. len += hex2u64(line + len, &size);
  278. len++;
  279. if (len + 2 >= line_len)
  280. continue;
  281. sym = symbol__new(start, size, line + len,
  282. self->sym_priv_size, v);
  283. if (sym == NULL)
  284. goto out_delete_line;
  285. if (filter && filter(map, sym))
  286. symbol__delete(sym, self->sym_priv_size);
  287. else {
  288. dso__insert_symbol(self, sym);
  289. nr_syms++;
  290. }
  291. }
  292. free(line);
  293. fclose(file);
  294. return nr_syms;
  295. out_delete_line:
  296. free(line);
  297. out_failure:
  298. return -1;
  299. }
  300. /**
  301. * elf_symtab__for_each_symbol - iterate thru all the symbols
  302. *
  303. * @self: struct elf_symtab instance to iterate
  304. * @idx: uint32_t idx
  305. * @sym: GElf_Sym iterator
  306. */
  307. #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
  308. for (idx = 0, gelf_getsym(syms, idx, &sym);\
  309. idx < nr_syms; \
  310. idx++, gelf_getsym(syms, idx, &sym))
  311. static inline uint8_t elf_sym__type(const GElf_Sym *sym)
  312. {
  313. return GELF_ST_TYPE(sym->st_info);
  314. }
  315. static inline int elf_sym__is_function(const GElf_Sym *sym)
  316. {
  317. return elf_sym__type(sym) == STT_FUNC &&
  318. sym->st_name != 0 &&
  319. sym->st_shndx != SHN_UNDEF &&
  320. sym->st_size != 0;
  321. }
  322. static inline int elf_sym__is_label(const GElf_Sym *sym)
  323. {
  324. return elf_sym__type(sym) == STT_NOTYPE &&
  325. sym->st_name != 0 &&
  326. sym->st_shndx != SHN_UNDEF &&
  327. sym->st_shndx != SHN_ABS;
  328. }
  329. static inline const char *elf_sec__name(const GElf_Shdr *shdr,
  330. const Elf_Data *secstrs)
  331. {
  332. return secstrs->d_buf + shdr->sh_name;
  333. }
  334. static inline int elf_sec__is_text(const GElf_Shdr *shdr,
  335. const Elf_Data *secstrs)
  336. {
  337. return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
  338. }
  339. static inline const char *elf_sym__name(const GElf_Sym *sym,
  340. const Elf_Data *symstrs)
  341. {
  342. return symstrs->d_buf + sym->st_name;
  343. }
  344. static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
  345. GElf_Shdr *shp, const char *name,
  346. size_t *idx)
  347. {
  348. Elf_Scn *sec = NULL;
  349. size_t cnt = 1;
  350. while ((sec = elf_nextscn(elf, sec)) != NULL) {
  351. char *str;
  352. gelf_getshdr(sec, shp);
  353. str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
  354. if (!strcmp(name, str)) {
  355. if (idx)
  356. *idx = cnt;
  357. break;
  358. }
  359. ++cnt;
  360. }
  361. return sec;
  362. }
  363. #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
  364. for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
  365. idx < nr_entries; \
  366. ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
  367. #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
  368. for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
  369. idx < nr_entries; \
  370. ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
  371. /*
  372. * We need to check if we have a .dynsym, so that we can handle the
  373. * .plt, synthesizing its symbols, that aren't on the symtabs (be it
  374. * .dynsym or .symtab).
  375. * And always look at the original dso, not at debuginfo packages, that
  376. * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
  377. */
  378. static int dso__synthesize_plt_symbols(struct dso *self, int v)
  379. {
  380. uint32_t nr_rel_entries, idx;
  381. GElf_Sym sym;
  382. u64 plt_offset;
  383. GElf_Shdr shdr_plt;
  384. struct symbol *f;
  385. GElf_Shdr shdr_rel_plt, shdr_dynsym;
  386. Elf_Data *reldata, *syms, *symstrs;
  387. Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
  388. size_t dynsym_idx;
  389. GElf_Ehdr ehdr;
  390. char sympltname[1024];
  391. Elf *elf;
  392. int nr = 0, symidx, fd, err = 0;
  393. fd = open(self->long_name, O_RDONLY);
  394. if (fd < 0)
  395. goto out;
  396. elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
  397. if (elf == NULL)
  398. goto out_close;
  399. if (gelf_getehdr(elf, &ehdr) == NULL)
  400. goto out_elf_end;
  401. scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
  402. ".dynsym", &dynsym_idx);
  403. if (scn_dynsym == NULL)
  404. goto out_elf_end;
  405. scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
  406. ".rela.plt", NULL);
  407. if (scn_plt_rel == NULL) {
  408. scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
  409. ".rel.plt", NULL);
  410. if (scn_plt_rel == NULL)
  411. goto out_elf_end;
  412. }
  413. err = -1;
  414. if (shdr_rel_plt.sh_link != dynsym_idx)
  415. goto out_elf_end;
  416. if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
  417. goto out_elf_end;
  418. /*
  419. * Fetch the relocation section to find the idxes to the GOT
  420. * and the symbols in the .dynsym they refer to.
  421. */
  422. reldata = elf_getdata(scn_plt_rel, NULL);
  423. if (reldata == NULL)
  424. goto out_elf_end;
  425. syms = elf_getdata(scn_dynsym, NULL);
  426. if (syms == NULL)
  427. goto out_elf_end;
  428. scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
  429. if (scn_symstrs == NULL)
  430. goto out_elf_end;
  431. symstrs = elf_getdata(scn_symstrs, NULL);
  432. if (symstrs == NULL)
  433. goto out_elf_end;
  434. nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
  435. plt_offset = shdr_plt.sh_offset;
  436. if (shdr_rel_plt.sh_type == SHT_RELA) {
  437. GElf_Rela pos_mem, *pos;
  438. elf_section__for_each_rela(reldata, pos, pos_mem, idx,
  439. nr_rel_entries) {
  440. symidx = GELF_R_SYM(pos->r_info);
  441. plt_offset += shdr_plt.sh_entsize;
  442. gelf_getsym(syms, symidx, &sym);
  443. snprintf(sympltname, sizeof(sympltname),
  444. "%s@plt", elf_sym__name(&sym, symstrs));
  445. f = symbol__new(plt_offset, shdr_plt.sh_entsize,
  446. sympltname, self->sym_priv_size, v);
  447. if (!f)
  448. goto out_elf_end;
  449. dso__insert_symbol(self, f);
  450. ++nr;
  451. }
  452. } else if (shdr_rel_plt.sh_type == SHT_REL) {
  453. GElf_Rel pos_mem, *pos;
  454. elf_section__for_each_rel(reldata, pos, pos_mem, idx,
  455. nr_rel_entries) {
  456. symidx = GELF_R_SYM(pos->r_info);
  457. plt_offset += shdr_plt.sh_entsize;
  458. gelf_getsym(syms, symidx, &sym);
  459. snprintf(sympltname, sizeof(sympltname),
  460. "%s@plt", elf_sym__name(&sym, symstrs));
  461. f = symbol__new(plt_offset, shdr_plt.sh_entsize,
  462. sympltname, self->sym_priv_size, v);
  463. if (!f)
  464. goto out_elf_end;
  465. dso__insert_symbol(self, f);
  466. ++nr;
  467. }
  468. }
  469. err = 0;
  470. out_elf_end:
  471. elf_end(elf);
  472. out_close:
  473. close(fd);
  474. if (err == 0)
  475. return nr;
  476. out:
  477. fprintf(stderr, "%s: problems reading %s PLT info.\n",
  478. __func__, self->long_name);
  479. return 0;
  480. }
  481. static int dso__load_sym(struct dso *self, struct map *map, const char *name,
  482. int fd, symbol_filter_t filter, int kernel,
  483. int kmodule, int v)
  484. {
  485. Elf_Data *symstrs, *secstrs;
  486. uint32_t nr_syms;
  487. int err = -1;
  488. uint32_t idx;
  489. GElf_Ehdr ehdr;
  490. GElf_Shdr shdr;
  491. Elf_Data *syms;
  492. GElf_Sym sym;
  493. Elf_Scn *sec, *sec_strndx;
  494. Elf *elf;
  495. int nr = 0;
  496. elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
  497. if (elf == NULL) {
  498. if (v)
  499. fprintf(stderr, "%s: cannot read %s ELF file.\n",
  500. __func__, name);
  501. goto out_close;
  502. }
  503. if (gelf_getehdr(elf, &ehdr) == NULL) {
  504. if (v)
  505. fprintf(stderr, "%s: cannot get elf header.\n", __func__);
  506. goto out_elf_end;
  507. }
  508. sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
  509. if (sec == NULL) {
  510. sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
  511. if (sec == NULL)
  512. goto out_elf_end;
  513. }
  514. syms = elf_getdata(sec, NULL);
  515. if (syms == NULL)
  516. goto out_elf_end;
  517. sec = elf_getscn(elf, shdr.sh_link);
  518. if (sec == NULL)
  519. goto out_elf_end;
  520. symstrs = elf_getdata(sec, NULL);
  521. if (symstrs == NULL)
  522. goto out_elf_end;
  523. sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
  524. if (sec_strndx == NULL)
  525. goto out_elf_end;
  526. secstrs = elf_getdata(sec_strndx, NULL);
  527. if (secstrs == NULL)
  528. goto out_elf_end;
  529. nr_syms = shdr.sh_size / shdr.sh_entsize;
  530. memset(&sym, 0, sizeof(sym));
  531. if (!kernel) {
  532. self->adjust_symbols = (ehdr.e_type == ET_EXEC ||
  533. elf_section_by_name(elf, &ehdr, &shdr,
  534. ".gnu.prelink_undo",
  535. NULL) != NULL);
  536. } else self->adjust_symbols = 0;
  537. elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
  538. struct symbol *f;
  539. const char *elf_name;
  540. char *demangled;
  541. int is_label = elf_sym__is_label(&sym);
  542. const char *section_name;
  543. u64 sh_offset = 0;
  544. if (!is_label && !elf_sym__is_function(&sym))
  545. continue;
  546. sec = elf_getscn(elf, sym.st_shndx);
  547. if (!sec)
  548. goto out_elf_end;
  549. gelf_getshdr(sec, &shdr);
  550. if (is_label && !elf_sec__is_text(&shdr, secstrs))
  551. continue;
  552. section_name = elf_sec__name(&shdr, secstrs);
  553. if ((kernel || kmodule)) {
  554. if (strstr(section_name, ".init"))
  555. sh_offset = shdr.sh_offset;
  556. }
  557. if (self->adjust_symbols) {
  558. if (v >= 2)
  559. printf("adjusting symbol: st_value: %Lx sh_addr: %Lx sh_offset: %Lx\n",
  560. (u64)sym.st_value, (u64)shdr.sh_addr, (u64)shdr.sh_offset);
  561. sym.st_value -= shdr.sh_addr - shdr.sh_offset;
  562. }
  563. /*
  564. * We need to figure out if the object was created from C++ sources
  565. * DWARF DW_compile_unit has this, but we don't always have access
  566. * to it...
  567. */
  568. elf_name = elf_sym__name(&sym, symstrs);
  569. demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
  570. if (demangled != NULL)
  571. elf_name = demangled;
  572. f = symbol__new(sym.st_value + sh_offset, sym.st_size, elf_name,
  573. self->sym_priv_size, v);
  574. free(demangled);
  575. if (!f)
  576. goto out_elf_end;
  577. if (filter && filter(map, f))
  578. symbol__delete(f, self->sym_priv_size);
  579. else {
  580. dso__insert_symbol(self, f);
  581. nr++;
  582. }
  583. }
  584. err = nr;
  585. out_elf_end:
  586. elf_end(elf);
  587. out_close:
  588. return err;
  589. }
  590. #define BUILD_ID_SIZE 128
  591. static char *dso__read_build_id(struct dso *self, int v)
  592. {
  593. int i;
  594. GElf_Ehdr ehdr;
  595. GElf_Shdr shdr;
  596. Elf_Data *build_id_data;
  597. Elf_Scn *sec;
  598. char *build_id = NULL, *bid;
  599. unsigned char *raw;
  600. Elf *elf;
  601. int fd = open(self->long_name, O_RDONLY);
  602. if (fd < 0)
  603. goto out;
  604. elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
  605. if (elf == NULL) {
  606. if (v)
  607. fprintf(stderr, "%s: cannot read %s ELF file.\n",
  608. __func__, self->long_name);
  609. goto out_close;
  610. }
  611. if (gelf_getehdr(elf, &ehdr) == NULL) {
  612. if (v)
  613. fprintf(stderr, "%s: cannot get elf header.\n", __func__);
  614. goto out_elf_end;
  615. }
  616. sec = elf_section_by_name(elf, &ehdr, &shdr, ".note.gnu.build-id", NULL);
  617. if (sec == NULL)
  618. goto out_elf_end;
  619. build_id_data = elf_getdata(sec, NULL);
  620. if (build_id_data == NULL)
  621. goto out_elf_end;
  622. build_id = malloc(BUILD_ID_SIZE);
  623. if (build_id == NULL)
  624. goto out_elf_end;
  625. raw = build_id_data->d_buf + 16;
  626. bid = build_id;
  627. for (i = 0; i < 20; ++i) {
  628. sprintf(bid, "%02x", *raw);
  629. ++raw;
  630. bid += 2;
  631. }
  632. if (v >= 2)
  633. printf("%s(%s): %s\n", __func__, self->long_name, build_id);
  634. out_elf_end:
  635. elf_end(elf);
  636. out_close:
  637. close(fd);
  638. out:
  639. return build_id;
  640. }
  641. char dso__symtab_origin(const struct dso *self)
  642. {
  643. static const char origin[] = {
  644. [DSO__ORIG_KERNEL] = 'k',
  645. [DSO__ORIG_JAVA_JIT] = 'j',
  646. [DSO__ORIG_FEDORA] = 'f',
  647. [DSO__ORIG_UBUNTU] = 'u',
  648. [DSO__ORIG_BUILDID] = 'b',
  649. [DSO__ORIG_DSO] = 'd',
  650. [DSO__ORIG_KMODULE] = 'K',
  651. };
  652. if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
  653. return '!';
  654. return origin[self->origin];
  655. }
  656. int dso__load(struct dso *self, struct map *map, symbol_filter_t filter, int v)
  657. {
  658. int size = PATH_MAX;
  659. char *name = malloc(size), *build_id = NULL;
  660. int ret = -1;
  661. int fd;
  662. if (!name)
  663. return -1;
  664. self->adjust_symbols = 0;
  665. if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
  666. ret = dso__load_perf_map(self, map, filter, v);
  667. self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT :
  668. DSO__ORIG_NOT_FOUND;
  669. return ret;
  670. }
  671. self->origin = DSO__ORIG_FEDORA - 1;
  672. more:
  673. do {
  674. self->origin++;
  675. switch (self->origin) {
  676. case DSO__ORIG_FEDORA:
  677. snprintf(name, size, "/usr/lib/debug%s.debug",
  678. self->long_name);
  679. break;
  680. case DSO__ORIG_UBUNTU:
  681. snprintf(name, size, "/usr/lib/debug%s",
  682. self->long_name);
  683. break;
  684. case DSO__ORIG_BUILDID:
  685. build_id = dso__read_build_id(self, v);
  686. if (build_id != NULL) {
  687. snprintf(name, size,
  688. "/usr/lib/debug/.build-id/%.2s/%s.debug",
  689. build_id, build_id + 2);
  690. free(build_id);
  691. break;
  692. }
  693. self->origin++;
  694. /* Fall thru */
  695. case DSO__ORIG_DSO:
  696. snprintf(name, size, "%s", self->long_name);
  697. break;
  698. default:
  699. goto out;
  700. }
  701. fd = open(name, O_RDONLY);
  702. } while (fd < 0);
  703. ret = dso__load_sym(self, map, name, fd, filter, 0, 0, v);
  704. close(fd);
  705. /*
  706. * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
  707. */
  708. if (!ret)
  709. goto more;
  710. if (ret > 0) {
  711. int nr_plt = dso__synthesize_plt_symbols(self, v);
  712. if (nr_plt > 0)
  713. ret += nr_plt;
  714. }
  715. out:
  716. free(name);
  717. if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
  718. return 0;
  719. return ret;
  720. }
  721. struct map *kernel_map;
  722. static void kernel_maps__insert(struct map *map)
  723. {
  724. maps__insert(&kernel_maps, map);
  725. }
  726. struct symbol *kernel_maps__find_symbol(u64 ip, struct map **mapp)
  727. {
  728. /*
  729. * We can't have kernel_map in kernel_maps because it spans an address
  730. * space that includes the modules. The right way to fix this is to
  731. * create several maps, so that we don't have overlapping ranges with
  732. * modules. For now lets look first on the kernel dso.
  733. */
  734. struct map *map = maps__find(&kernel_maps, ip);
  735. struct symbol *sym;
  736. if (map) {
  737. ip = map->map_ip(map, ip);
  738. sym = map->dso->find_symbol(map->dso, ip);
  739. } else {
  740. map = kernel_map;
  741. sym = map->dso->find_symbol(map->dso, ip);
  742. }
  743. if (mapp)
  744. *mapp = map;
  745. return sym;
  746. }
  747. struct map *kernel_maps__find_by_dso_name(const char *name)
  748. {
  749. struct rb_node *nd;
  750. for (nd = rb_first(&kernel_maps); nd; nd = rb_next(nd)) {
  751. struct map *map = rb_entry(nd, struct map, rb_node);
  752. if (map->dso && strcmp(map->dso->name, name) == 0)
  753. return map;
  754. }
  755. return NULL;
  756. }
  757. static int dso__load_module_sym(struct dso *self, struct map *map,
  758. symbol_filter_t filter, int v)
  759. {
  760. int err = 0, fd = open(self->long_name, O_RDONLY);
  761. if (fd < 0) {
  762. if (v)
  763. fprintf(stderr, "%s: cannot open %s\n",
  764. __func__, self->long_name);
  765. return err;
  766. }
  767. err = dso__load_sym(self, map, self->long_name, fd, filter, 0, 1, v);
  768. close(fd);
  769. return err;
  770. }
  771. static int dsos__load_modules_sym_dir(char *dirname,
  772. symbol_filter_t filter, int v)
  773. {
  774. struct dirent *dent;
  775. int nr_symbols = 0, err;
  776. DIR *dir = opendir(dirname);
  777. if (!dir) {
  778. if (v)
  779. fprintf(stderr, "%s: cannot open %s dir\n", __func__,
  780. dirname);
  781. return -1;
  782. }
  783. while ((dent = readdir(dir)) != NULL) {
  784. char path[PATH_MAX];
  785. if (dent->d_type == DT_DIR) {
  786. if (!strcmp(dent->d_name, ".") ||
  787. !strcmp(dent->d_name, ".."))
  788. continue;
  789. snprintf(path, sizeof(path), "%s/%s",
  790. dirname, dent->d_name);
  791. err = dsos__load_modules_sym_dir(path, filter, v);
  792. if (err < 0)
  793. goto failure;
  794. } else {
  795. char *dot = strrchr(dent->d_name, '.'),
  796. dso_name[PATH_MAX];
  797. struct map *map;
  798. struct rb_node *last;
  799. if (dot == NULL || strcmp(dot, ".ko"))
  800. continue;
  801. snprintf(dso_name, sizeof(dso_name), "[%.*s]",
  802. (int)(dot - dent->d_name), dent->d_name);
  803. strxfrchar(dso_name, '-', '_');
  804. map = kernel_maps__find_by_dso_name(dso_name);
  805. if (map == NULL)
  806. continue;
  807. snprintf(path, sizeof(path), "%s/%s",
  808. dirname, dent->d_name);
  809. map->dso->long_name = strdup(path);
  810. if (map->dso->long_name == NULL)
  811. goto failure;
  812. err = dso__load_module_sym(map->dso, map, filter, v);
  813. if (err < 0)
  814. goto failure;
  815. last = rb_last(&map->dso->syms);
  816. if (last) {
  817. struct symbol *sym;
  818. sym = rb_entry(last, struct symbol, rb_node);
  819. map->end = map->start + sym->end;
  820. }
  821. }
  822. nr_symbols += err;
  823. }
  824. return nr_symbols;
  825. failure:
  826. closedir(dir);
  827. return -1;
  828. }
  829. static int dsos__load_modules_sym(symbol_filter_t filter, int v)
  830. {
  831. struct utsname uts;
  832. char modules_path[PATH_MAX];
  833. if (uname(&uts) < 0)
  834. return -1;
  835. snprintf(modules_path, sizeof(modules_path), "/lib/modules/%s/kernel",
  836. uts.release);
  837. return dsos__load_modules_sym_dir(modules_path, filter, v);
  838. }
  839. /*
  840. * Constructor variant for modules (where we know from /proc/modules where
  841. * they are loaded) and for vmlinux, where only after we load all the
  842. * symbols we'll know where it starts and ends.
  843. */
  844. static struct map *map__new2(u64 start, struct dso *dso)
  845. {
  846. struct map *self = malloc(sizeof(*self));
  847. if (self != NULL) {
  848. self->start = start;
  849. /*
  850. * Will be filled after we load all the symbols
  851. */
  852. self->end = 0;
  853. self->pgoff = 0;
  854. self->dso = dso;
  855. self->map_ip = map__map_ip;
  856. RB_CLEAR_NODE(&self->rb_node);
  857. }
  858. return self;
  859. }
  860. static int dsos__load_modules(unsigned int sym_priv_size)
  861. {
  862. char *line = NULL;
  863. size_t n;
  864. FILE *file = fopen("/proc/modules", "r");
  865. struct map *map;
  866. if (file == NULL)
  867. return -1;
  868. while (!feof(file)) {
  869. char name[PATH_MAX];
  870. u64 start;
  871. struct dso *dso;
  872. char *sep;
  873. int line_len;
  874. line_len = getline(&line, &n, file);
  875. if (line_len < 0)
  876. break;
  877. if (!line)
  878. goto out_failure;
  879. line[--line_len] = '\0'; /* \n */
  880. sep = strrchr(line, 'x');
  881. if (sep == NULL)
  882. continue;
  883. hex2u64(sep + 1, &start);
  884. sep = strchr(line, ' ');
  885. if (sep == NULL)
  886. continue;
  887. *sep = '\0';
  888. snprintf(name, sizeof(name), "[%s]", line);
  889. dso = dso__new(name, sym_priv_size);
  890. if (dso == NULL)
  891. goto out_delete_line;
  892. map = map__new2(start, dso);
  893. if (map == NULL) {
  894. dso__delete(dso);
  895. goto out_delete_line;
  896. }
  897. dso->origin = DSO__ORIG_KMODULE;
  898. kernel_maps__insert(map);
  899. dsos__add(dso);
  900. }
  901. free(line);
  902. fclose(file);
  903. return 0;
  904. out_delete_line:
  905. free(line);
  906. out_failure:
  907. return -1;
  908. }
  909. static int dso__load_vmlinux(struct dso *self, struct map *map,
  910. const char *vmlinux,
  911. symbol_filter_t filter, int v)
  912. {
  913. int err, fd = open(vmlinux, O_RDONLY);
  914. if (fd < 0)
  915. return -1;
  916. err = dso__load_sym(self, map, self->long_name, fd, filter, 1, 0, v);
  917. close(fd);
  918. return err;
  919. }
  920. int dsos__load_kernel(const char *vmlinux, unsigned int sym_priv_size,
  921. symbol_filter_t filter, int v, int use_modules)
  922. {
  923. int err = -1;
  924. struct dso *dso = dso__new(vmlinux, sym_priv_size);
  925. if (dso == NULL)
  926. return -1;
  927. dso->short_name = "[kernel]";
  928. kernel_map = map__new2(0, dso);
  929. if (kernel_map == NULL)
  930. goto out_delete_dso;
  931. kernel_map->map_ip = vdso__map_ip;
  932. if (use_modules && dsos__load_modules(sym_priv_size) < 0) {
  933. fprintf(stderr, "Failed to load list of modules in use! "
  934. "Continuing...\n");
  935. use_modules = 0;
  936. }
  937. if (vmlinux) {
  938. err = dso__load_vmlinux(dso, kernel_map, vmlinux, filter, v);
  939. if (err > 0 && use_modules) {
  940. int syms = dsos__load_modules_sym(filter, v);
  941. if (syms < 0)
  942. fprintf(stderr, "Failed to read module symbols!"
  943. " Continuing...\n");
  944. else
  945. err += syms;
  946. }
  947. }
  948. if (err <= 0)
  949. err = maps__load_kallsyms(filter, use_modules, v);
  950. if (err > 0) {
  951. struct rb_node *node = rb_first(&dso->syms);
  952. struct symbol *sym = rb_entry(node, struct symbol, rb_node);
  953. /*
  954. * Now that we have all sorted out, just set the ->end of all
  955. * symbols that still don't have it.
  956. */
  957. dso__set_symbols_end(dso);
  958. kernel_maps__fixup_sym_end();
  959. kernel_map->start = sym->start;
  960. node = rb_last(&dso->syms);
  961. sym = rb_entry(node, struct symbol, rb_node);
  962. kernel_map->end = sym->end;
  963. dso->origin = DSO__ORIG_KERNEL;
  964. /*
  965. * XXX See kernel_maps__find_symbol comment
  966. * kernel_maps__insert(kernel_map)
  967. */
  968. dsos__add(dso);
  969. if (v > 0)
  970. kernel_maps__fprintf(stderr);
  971. }
  972. return err;
  973. out_delete_dso:
  974. dso__delete(dso);
  975. return -1;
  976. }
  977. LIST_HEAD(dsos);
  978. struct dso *vdso;
  979. const char *vmlinux_name = "vmlinux";
  980. int modules;
  981. static void dsos__add(struct dso *dso)
  982. {
  983. list_add_tail(&dso->node, &dsos);
  984. }
  985. static struct dso *dsos__find(const char *name)
  986. {
  987. struct dso *pos;
  988. list_for_each_entry(pos, &dsos, node)
  989. if (strcmp(pos->name, name) == 0)
  990. return pos;
  991. return NULL;
  992. }
  993. struct dso *dsos__findnew(const char *name)
  994. {
  995. struct dso *dso = dsos__find(name);
  996. int nr;
  997. if (dso)
  998. return dso;
  999. dso = dso__new(name, 0);
  1000. if (!dso)
  1001. goto out_delete_dso;
  1002. nr = dso__load(dso, NULL, NULL, verbose);
  1003. if (nr < 0) {
  1004. eprintf("Failed to open: %s\n", name);
  1005. goto out_delete_dso;
  1006. }
  1007. if (!nr)
  1008. eprintf("No symbols found in: %s, maybe install a debug package?\n", name);
  1009. dsos__add(dso);
  1010. return dso;
  1011. out_delete_dso:
  1012. dso__delete(dso);
  1013. return NULL;
  1014. }
  1015. void dsos__fprintf(FILE *fp)
  1016. {
  1017. struct dso *pos;
  1018. list_for_each_entry(pos, &dsos, node)
  1019. dso__fprintf(pos, fp);
  1020. }
  1021. int load_kernel(void)
  1022. {
  1023. if (dsos__load_kernel(vmlinux_name, 0, NULL, verbose, modules) <= 0)
  1024. return -1;
  1025. vdso = dso__new("[vdso]", 0);
  1026. if (!vdso)
  1027. return -1;
  1028. dsos__add(vdso);
  1029. return 0;
  1030. }
  1031. void symbol__init(void)
  1032. {
  1033. elf_version(EV_CURRENT);
  1034. }