symbol.c 36 KB

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