symbol.c 31 KB

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