symbol.c 37 KB

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