symbol.c 32 KB

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