symbol.c 31 KB

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