symbol.c 30 KB

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