symbol.c 28 KB

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