symbol.c 28 KB

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