symbol.c 36 KB

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