symbol.c 36 KB

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