symbol.c 30 KB

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