symbol.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350
  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, 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, 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 128
  674. static char *dso__read_build_id(struct dso *self)
  675. {
  676. int i;
  677. GElf_Ehdr ehdr;
  678. GElf_Shdr shdr;
  679. Elf_Data *build_id_data;
  680. Elf_Scn *sec;
  681. char *build_id = NULL, *bid;
  682. unsigned char *raw;
  683. Elf *elf;
  684. int fd = open(self->long_name, O_RDONLY);
  685. if (fd < 0)
  686. goto out;
  687. elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
  688. if (elf == NULL) {
  689. pr_err("%s: cannot read %s ELF file.\n", __func__,
  690. self->long_name);
  691. goto out_close;
  692. }
  693. if (gelf_getehdr(elf, &ehdr) == NULL) {
  694. pr_err("%s: cannot get elf header.\n", __func__);
  695. goto out_elf_end;
  696. }
  697. sec = elf_section_by_name(elf, &ehdr, &shdr, ".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. build_id = malloc(BUILD_ID_SIZE);
  704. if (build_id == NULL)
  705. goto out_elf_end;
  706. raw = build_id_data->d_buf + 16;
  707. bid = build_id;
  708. for (i = 0; i < 20; ++i) {
  709. sprintf(bid, "%02x", *raw);
  710. ++raw;
  711. bid += 2;
  712. }
  713. pr_debug2("%s(%s): %s\n", __func__, self->long_name, build_id);
  714. out_elf_end:
  715. elf_end(elf);
  716. out_close:
  717. close(fd);
  718. out:
  719. return build_id;
  720. }
  721. char dso__symtab_origin(const struct dso *self)
  722. {
  723. static const char origin[] = {
  724. [DSO__ORIG_KERNEL] = 'k',
  725. [DSO__ORIG_JAVA_JIT] = 'j',
  726. [DSO__ORIG_FEDORA] = 'f',
  727. [DSO__ORIG_UBUNTU] = 'u',
  728. [DSO__ORIG_BUILDID] = 'b',
  729. [DSO__ORIG_DSO] = 'd',
  730. [DSO__ORIG_KMODULE] = 'K',
  731. };
  732. if (self == NULL || self->origin == DSO__ORIG_NOT_FOUND)
  733. return '!';
  734. return origin[self->origin];
  735. }
  736. int dso__load(struct dso *self, struct map *map, symbol_filter_t filter)
  737. {
  738. int size = PATH_MAX;
  739. char *name = malloc(size), *build_id = NULL;
  740. int ret = -1;
  741. int fd;
  742. self->loaded = true;
  743. if (!name)
  744. return -1;
  745. self->adjust_symbols = 0;
  746. if (strncmp(self->name, "/tmp/perf-", 10) == 0) {
  747. ret = dso__load_perf_map(self, map, filter);
  748. self->origin = ret > 0 ? DSO__ORIG_JAVA_JIT :
  749. DSO__ORIG_NOT_FOUND;
  750. return ret;
  751. }
  752. self->origin = DSO__ORIG_FEDORA - 1;
  753. more:
  754. do {
  755. self->origin++;
  756. switch (self->origin) {
  757. case DSO__ORIG_FEDORA:
  758. snprintf(name, size, "/usr/lib/debug%s.debug",
  759. self->long_name);
  760. break;
  761. case DSO__ORIG_UBUNTU:
  762. snprintf(name, size, "/usr/lib/debug%s",
  763. self->long_name);
  764. break;
  765. case DSO__ORIG_BUILDID:
  766. build_id = dso__read_build_id(self);
  767. if (build_id != NULL) {
  768. snprintf(name, size,
  769. "/usr/lib/debug/.build-id/%.2s/%s.debug",
  770. build_id, build_id + 2);
  771. free(build_id);
  772. break;
  773. }
  774. self->origin++;
  775. /* Fall thru */
  776. case DSO__ORIG_DSO:
  777. snprintf(name, size, "%s", self->long_name);
  778. break;
  779. default:
  780. goto out;
  781. }
  782. fd = open(name, O_RDONLY);
  783. } while (fd < 0);
  784. ret = dso__load_sym(self, map, name, fd, filter, 0, 0);
  785. close(fd);
  786. /*
  787. * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
  788. */
  789. if (!ret)
  790. goto more;
  791. if (ret > 0) {
  792. int nr_plt = dso__synthesize_plt_symbols(self);
  793. if (nr_plt > 0)
  794. ret += nr_plt;
  795. }
  796. out:
  797. free(name);
  798. if (ret < 0 && strstr(self->name, " (deleted)") != NULL)
  799. return 0;
  800. return ret;
  801. }
  802. struct map *kernel_map;
  803. static void kernel_maps__insert(struct map *map)
  804. {
  805. maps__insert(&kernel_maps, map);
  806. }
  807. struct symbol *kernel_maps__find_symbol(u64 ip, struct map **mapp)
  808. {
  809. struct map *map = maps__find(&kernel_maps, ip);
  810. if (mapp)
  811. *mapp = map;
  812. if (map) {
  813. ip = map->map_ip(map, ip);
  814. return map->dso->find_symbol(map->dso, ip);
  815. }
  816. return NULL;
  817. }
  818. struct map *kernel_maps__find_by_dso_name(const char *name)
  819. {
  820. struct rb_node *nd;
  821. for (nd = rb_first(&kernel_maps); nd; nd = rb_next(nd)) {
  822. struct map *map = rb_entry(nd, struct map, rb_node);
  823. if (map->dso && strcmp(map->dso->name, name) == 0)
  824. return map;
  825. }
  826. return NULL;
  827. }
  828. static int dso__load_module_sym(struct dso *self, struct map *map,
  829. symbol_filter_t filter)
  830. {
  831. int err = 0, fd = open(self->long_name, O_RDONLY);
  832. self->loaded = true;
  833. if (fd < 0) {
  834. pr_err("%s: cannot open %s\n", __func__, self->long_name);
  835. return err;
  836. }
  837. err = dso__load_sym(self, map, self->long_name, fd, filter, 0, 1);
  838. close(fd);
  839. return err;
  840. }
  841. static int dsos__load_modules_sym_dir(char *dirname, symbol_filter_t filter)
  842. {
  843. struct dirent *dent;
  844. int nr_symbols = 0, err;
  845. DIR *dir = opendir(dirname);
  846. if (!dir) {
  847. pr_err("%s: cannot open %s dir\n", __func__, dirname);
  848. return -1;
  849. }
  850. while ((dent = readdir(dir)) != NULL) {
  851. char path[PATH_MAX];
  852. if (dent->d_type == DT_DIR) {
  853. if (!strcmp(dent->d_name, ".") ||
  854. !strcmp(dent->d_name, ".."))
  855. continue;
  856. snprintf(path, sizeof(path), "%s/%s",
  857. dirname, dent->d_name);
  858. err = dsos__load_modules_sym_dir(path, filter);
  859. if (err < 0)
  860. goto failure;
  861. } else {
  862. char *dot = strrchr(dent->d_name, '.'),
  863. dso_name[PATH_MAX];
  864. struct map *map;
  865. struct rb_node *last;
  866. if (dot == NULL || strcmp(dot, ".ko"))
  867. continue;
  868. snprintf(dso_name, sizeof(dso_name), "[%.*s]",
  869. (int)(dot - dent->d_name), dent->d_name);
  870. strxfrchar(dso_name, '-', '_');
  871. map = kernel_maps__find_by_dso_name(dso_name);
  872. if (map == NULL)
  873. continue;
  874. snprintf(path, sizeof(path), "%s/%s",
  875. dirname, dent->d_name);
  876. map->dso->long_name = strdup(path);
  877. if (map->dso->long_name == NULL)
  878. goto failure;
  879. err = dso__load_module_sym(map->dso, map, filter);
  880. if (err < 0)
  881. goto failure;
  882. last = rb_last(&map->dso->syms);
  883. if (last) {
  884. struct symbol *sym;
  885. /*
  886. * We do this here as well, even having the
  887. * symbol size found in the symtab because
  888. * misannotated ASM symbols may have the size
  889. * set to zero.
  890. */
  891. dso__fixup_sym_end(map->dso);
  892. sym = rb_entry(last, struct symbol, rb_node);
  893. map->end = map->start + sym->end;
  894. }
  895. }
  896. nr_symbols += err;
  897. }
  898. return nr_symbols;
  899. failure:
  900. closedir(dir);
  901. return -1;
  902. }
  903. static int dsos__load_modules_sym(symbol_filter_t filter)
  904. {
  905. struct utsname uts;
  906. char modules_path[PATH_MAX];
  907. if (uname(&uts) < 0)
  908. return -1;
  909. snprintf(modules_path, sizeof(modules_path), "/lib/modules/%s/kernel",
  910. uts.release);
  911. return dsos__load_modules_sym_dir(modules_path, filter);
  912. }
  913. /*
  914. * Constructor variant for modules (where we know from /proc/modules where
  915. * they are loaded) and for vmlinux, where only after we load all the
  916. * symbols we'll know where it starts and ends.
  917. */
  918. static struct map *map__new2(u64 start, struct dso *dso)
  919. {
  920. struct map *self = malloc(sizeof(*self));
  921. if (self != NULL) {
  922. /*
  923. * ->end will be filled after we load all the symbols
  924. */
  925. map__init(self, start, 0, 0, dso);
  926. }
  927. return self;
  928. }
  929. static int dsos__load_modules(void)
  930. {
  931. char *line = NULL;
  932. size_t n;
  933. FILE *file = fopen("/proc/modules", "r");
  934. struct map *map;
  935. if (file == NULL)
  936. return -1;
  937. while (!feof(file)) {
  938. char name[PATH_MAX];
  939. u64 start;
  940. struct dso *dso;
  941. char *sep;
  942. int line_len;
  943. line_len = getline(&line, &n, file);
  944. if (line_len < 0)
  945. break;
  946. if (!line)
  947. goto out_failure;
  948. line[--line_len] = '\0'; /* \n */
  949. sep = strrchr(line, 'x');
  950. if (sep == NULL)
  951. continue;
  952. hex2u64(sep + 1, &start);
  953. sep = strchr(line, ' ');
  954. if (sep == NULL)
  955. continue;
  956. *sep = '\0';
  957. snprintf(name, sizeof(name), "[%s]", line);
  958. dso = dso__new(name);
  959. if (dso == NULL)
  960. goto out_delete_line;
  961. map = map__new2(start, dso);
  962. if (map == NULL) {
  963. dso__delete(dso);
  964. goto out_delete_line;
  965. }
  966. dso->origin = DSO__ORIG_KMODULE;
  967. kernel_maps__insert(map);
  968. dsos__add(dso);
  969. }
  970. free(line);
  971. fclose(file);
  972. return 0;
  973. out_delete_line:
  974. free(line);
  975. out_failure:
  976. return -1;
  977. }
  978. static int dso__load_vmlinux(struct dso *self, struct map *map,
  979. const char *vmlinux, symbol_filter_t filter)
  980. {
  981. int err, fd = open(vmlinux, O_RDONLY);
  982. self->loaded = true;
  983. if (fd < 0)
  984. return -1;
  985. err = dso__load_sym(self, map, self->long_name, fd, filter, 1, 0);
  986. close(fd);
  987. return err;
  988. }
  989. int dsos__load_kernel(const char *vmlinux, symbol_filter_t filter,
  990. int use_modules)
  991. {
  992. int err = -1;
  993. struct dso *dso = dso__new(vmlinux);
  994. if (dso == NULL)
  995. return -1;
  996. dso->short_name = "[kernel]";
  997. kernel_map = map__new2(0, dso);
  998. if (kernel_map == NULL)
  999. goto out_delete_dso;
  1000. kernel_map->map_ip = kernel_map->unmap_ip = identity__map_ip;
  1001. if (use_modules && dsos__load_modules() < 0) {
  1002. pr_warning("Failed to load list of modules in use! "
  1003. "Continuing...\n");
  1004. use_modules = 0;
  1005. }
  1006. if (vmlinux) {
  1007. err = dso__load_vmlinux(dso, kernel_map, vmlinux, filter);
  1008. if (err > 0 && use_modules) {
  1009. int syms = dsos__load_modules_sym(filter);
  1010. if (syms < 0)
  1011. pr_warning("Failed to read module symbols!"
  1012. " Continuing...\n");
  1013. else
  1014. err += syms;
  1015. }
  1016. }
  1017. if (err <= 0)
  1018. err = kernel_maps__load_kallsyms(filter, use_modules);
  1019. if (err > 0) {
  1020. struct rb_node *node = rb_first(&dso->syms);
  1021. struct symbol *sym = rb_entry(node, struct symbol, rb_node);
  1022. kernel_map->start = sym->start;
  1023. node = rb_last(&dso->syms);
  1024. sym = rb_entry(node, struct symbol, rb_node);
  1025. kernel_map->end = sym->end;
  1026. dso->origin = DSO__ORIG_KERNEL;
  1027. kernel_maps__insert(kernel_map);
  1028. /*
  1029. * Now that we have all sorted out, just set the ->end of all
  1030. * maps:
  1031. */
  1032. kernel_maps__fixup_end();
  1033. dsos__add(dso);
  1034. if (verbose)
  1035. kernel_maps__fprintf(stderr);
  1036. }
  1037. return err;
  1038. out_delete_dso:
  1039. dso__delete(dso);
  1040. return -1;
  1041. }
  1042. LIST_HEAD(dsos);
  1043. struct dso *vdso;
  1044. const char *vmlinux_name = "vmlinux";
  1045. int modules;
  1046. static void dsos__add(struct dso *dso)
  1047. {
  1048. list_add_tail(&dso->node, &dsos);
  1049. }
  1050. static struct dso *dsos__find(const char *name)
  1051. {
  1052. struct dso *pos;
  1053. list_for_each_entry(pos, &dsos, node)
  1054. if (strcmp(pos->name, name) == 0)
  1055. return pos;
  1056. return NULL;
  1057. }
  1058. struct dso *dsos__findnew(const char *name)
  1059. {
  1060. struct dso *dso = dsos__find(name);
  1061. if (!dso) {
  1062. dso = dso__new(name);
  1063. if (dso != NULL)
  1064. dsos__add(dso);
  1065. }
  1066. return dso;
  1067. }
  1068. void dsos__fprintf(FILE *fp)
  1069. {
  1070. struct dso *pos;
  1071. list_for_each_entry(pos, &dsos, node)
  1072. dso__fprintf(pos, fp);
  1073. }
  1074. int load_kernel(symbol_filter_t filter)
  1075. {
  1076. if (dsos__load_kernel(vmlinux_name, filter, modules) <= 0)
  1077. return -1;
  1078. vdso = dso__new("[vdso]");
  1079. if (!vdso)
  1080. return -1;
  1081. dsos__add(vdso);
  1082. return 0;
  1083. }
  1084. void symbol__init(unsigned int priv_size)
  1085. {
  1086. elf_version(EV_CURRENT);
  1087. symbol__priv_size = priv_size;
  1088. }