symbol.c 28 KB

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