symbol.c 30 KB

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