symbol.c 29 KB

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