symbol-elf.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  1. #include <libelf.h>
  2. #include <gelf.h>
  3. #include <elf.h>
  4. #include <fcntl.h>
  5. #include <stdio.h>
  6. #include <errno.h>
  7. #include <string.h>
  8. #include <unistd.h>
  9. #include <inttypes.h>
  10. #include "symbol.h"
  11. #include "debug.h"
  12. #ifndef NT_GNU_BUILD_ID
  13. #define NT_GNU_BUILD_ID 3
  14. #endif
  15. /**
  16. * elf_symtab__for_each_symbol - iterate thru all the symbols
  17. *
  18. * @syms: struct elf_symtab instance to iterate
  19. * @idx: uint32_t idx
  20. * @sym: GElf_Sym iterator
  21. */
  22. #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
  23. for (idx = 0, gelf_getsym(syms, idx, &sym);\
  24. idx < nr_syms; \
  25. idx++, gelf_getsym(syms, idx, &sym))
  26. static inline uint8_t elf_sym__type(const GElf_Sym *sym)
  27. {
  28. return GELF_ST_TYPE(sym->st_info);
  29. }
  30. static inline int elf_sym__is_function(const GElf_Sym *sym)
  31. {
  32. return elf_sym__type(sym) == STT_FUNC &&
  33. sym->st_name != 0 &&
  34. sym->st_shndx != SHN_UNDEF;
  35. }
  36. static inline bool elf_sym__is_object(const GElf_Sym *sym)
  37. {
  38. return elf_sym__type(sym) == STT_OBJECT &&
  39. sym->st_name != 0 &&
  40. sym->st_shndx != SHN_UNDEF;
  41. }
  42. static inline int elf_sym__is_label(const GElf_Sym *sym)
  43. {
  44. return elf_sym__type(sym) == STT_NOTYPE &&
  45. sym->st_name != 0 &&
  46. sym->st_shndx != SHN_UNDEF &&
  47. sym->st_shndx != SHN_ABS;
  48. }
  49. static bool elf_sym__is_a(GElf_Sym *sym, enum map_type type)
  50. {
  51. switch (type) {
  52. case MAP__FUNCTION:
  53. return elf_sym__is_function(sym);
  54. case MAP__VARIABLE:
  55. return elf_sym__is_object(sym);
  56. default:
  57. return false;
  58. }
  59. }
  60. static inline const char *elf_sym__name(const GElf_Sym *sym,
  61. const Elf_Data *symstrs)
  62. {
  63. return symstrs->d_buf + sym->st_name;
  64. }
  65. static inline const char *elf_sec__name(const GElf_Shdr *shdr,
  66. const Elf_Data *secstrs)
  67. {
  68. return secstrs->d_buf + shdr->sh_name;
  69. }
  70. static inline int elf_sec__is_text(const GElf_Shdr *shdr,
  71. const Elf_Data *secstrs)
  72. {
  73. return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
  74. }
  75. static inline bool elf_sec__is_data(const GElf_Shdr *shdr,
  76. const Elf_Data *secstrs)
  77. {
  78. return strstr(elf_sec__name(shdr, secstrs), "data") != NULL;
  79. }
  80. static bool elf_sec__is_a(GElf_Shdr *shdr, Elf_Data *secstrs,
  81. enum map_type type)
  82. {
  83. switch (type) {
  84. case MAP__FUNCTION:
  85. return elf_sec__is_text(shdr, secstrs);
  86. case MAP__VARIABLE:
  87. return elf_sec__is_data(shdr, secstrs);
  88. default:
  89. return false;
  90. }
  91. }
  92. static size_t elf_addr_to_index(Elf *elf, GElf_Addr addr)
  93. {
  94. Elf_Scn *sec = NULL;
  95. GElf_Shdr shdr;
  96. size_t cnt = 1;
  97. while ((sec = elf_nextscn(elf, sec)) != NULL) {
  98. gelf_getshdr(sec, &shdr);
  99. if ((addr >= shdr.sh_addr) &&
  100. (addr < (shdr.sh_addr + shdr.sh_size)))
  101. return cnt;
  102. ++cnt;
  103. }
  104. return -1;
  105. }
  106. static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
  107. GElf_Shdr *shp, const char *name,
  108. size_t *idx)
  109. {
  110. Elf_Scn *sec = NULL;
  111. size_t cnt = 1;
  112. /* Elf is corrupted/truncated, avoid calling elf_strptr. */
  113. if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL))
  114. return NULL;
  115. while ((sec = elf_nextscn(elf, sec)) != NULL) {
  116. char *str;
  117. gelf_getshdr(sec, shp);
  118. str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
  119. if (!strcmp(name, str)) {
  120. if (idx)
  121. *idx = cnt;
  122. break;
  123. }
  124. ++cnt;
  125. }
  126. return sec;
  127. }
  128. #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
  129. for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
  130. idx < nr_entries; \
  131. ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
  132. #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
  133. for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
  134. idx < nr_entries; \
  135. ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
  136. /*
  137. * We need to check if we have a .dynsym, so that we can handle the
  138. * .plt, synthesizing its symbols, that aren't on the symtabs (be it
  139. * .dynsym or .symtab).
  140. * And always look at the original dso, not at debuginfo packages, that
  141. * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
  142. */
  143. int dso__synthesize_plt_symbols(struct dso *dso, char *name, struct map *map,
  144. symbol_filter_t filter)
  145. {
  146. uint32_t nr_rel_entries, idx;
  147. GElf_Sym sym;
  148. u64 plt_offset;
  149. GElf_Shdr shdr_plt;
  150. struct symbol *f;
  151. GElf_Shdr shdr_rel_plt, shdr_dynsym;
  152. Elf_Data *reldata, *syms, *symstrs;
  153. Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
  154. size_t dynsym_idx;
  155. GElf_Ehdr ehdr;
  156. char sympltname[1024];
  157. Elf *elf;
  158. int nr = 0, symidx, fd, err = 0;
  159. fd = open(name, O_RDONLY);
  160. if (fd < 0)
  161. goto out;
  162. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  163. if (elf == NULL)
  164. goto out_close;
  165. if (gelf_getehdr(elf, &ehdr) == NULL)
  166. goto out_elf_end;
  167. scn_dynsym = elf_section_by_name(elf, &ehdr, &shdr_dynsym,
  168. ".dynsym", &dynsym_idx);
  169. if (scn_dynsym == NULL)
  170. goto out_elf_end;
  171. scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
  172. ".rela.plt", NULL);
  173. if (scn_plt_rel == NULL) {
  174. scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
  175. ".rel.plt", NULL);
  176. if (scn_plt_rel == NULL)
  177. goto out_elf_end;
  178. }
  179. err = -1;
  180. if (shdr_rel_plt.sh_link != dynsym_idx)
  181. goto out_elf_end;
  182. if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
  183. goto out_elf_end;
  184. /*
  185. * Fetch the relocation section to find the idxes to the GOT
  186. * and the symbols in the .dynsym they refer to.
  187. */
  188. reldata = elf_getdata(scn_plt_rel, NULL);
  189. if (reldata == NULL)
  190. goto out_elf_end;
  191. syms = elf_getdata(scn_dynsym, NULL);
  192. if (syms == NULL)
  193. goto out_elf_end;
  194. scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
  195. if (scn_symstrs == NULL)
  196. goto out_elf_end;
  197. symstrs = elf_getdata(scn_symstrs, NULL);
  198. if (symstrs == NULL)
  199. goto out_elf_end;
  200. if (symstrs->d_size == 0)
  201. goto out_elf_end;
  202. nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
  203. plt_offset = shdr_plt.sh_offset;
  204. if (shdr_rel_plt.sh_type == SHT_RELA) {
  205. GElf_Rela pos_mem, *pos;
  206. elf_section__for_each_rela(reldata, pos, pos_mem, idx,
  207. nr_rel_entries) {
  208. symidx = GELF_R_SYM(pos->r_info);
  209. plt_offset += shdr_plt.sh_entsize;
  210. gelf_getsym(syms, symidx, &sym);
  211. snprintf(sympltname, sizeof(sympltname),
  212. "%s@plt", elf_sym__name(&sym, symstrs));
  213. f = symbol__new(plt_offset, shdr_plt.sh_entsize,
  214. STB_GLOBAL, sympltname);
  215. if (!f)
  216. goto out_elf_end;
  217. if (filter && filter(map, f))
  218. symbol__delete(f);
  219. else {
  220. symbols__insert(&dso->symbols[map->type], f);
  221. ++nr;
  222. }
  223. }
  224. } else if (shdr_rel_plt.sh_type == SHT_REL) {
  225. GElf_Rel pos_mem, *pos;
  226. elf_section__for_each_rel(reldata, pos, pos_mem, idx,
  227. nr_rel_entries) {
  228. symidx = GELF_R_SYM(pos->r_info);
  229. plt_offset += shdr_plt.sh_entsize;
  230. gelf_getsym(syms, symidx, &sym);
  231. snprintf(sympltname, sizeof(sympltname),
  232. "%s@plt", elf_sym__name(&sym, symstrs));
  233. f = symbol__new(plt_offset, shdr_plt.sh_entsize,
  234. STB_GLOBAL, sympltname);
  235. if (!f)
  236. goto out_elf_end;
  237. if (filter && filter(map, f))
  238. symbol__delete(f);
  239. else {
  240. symbols__insert(&dso->symbols[map->type], f);
  241. ++nr;
  242. }
  243. }
  244. }
  245. err = 0;
  246. out_elf_end:
  247. elf_end(elf);
  248. out_close:
  249. close(fd);
  250. if (err == 0)
  251. return nr;
  252. out:
  253. pr_debug("%s: problems reading %s PLT info.\n",
  254. __func__, dso->long_name);
  255. return 0;
  256. }
  257. /*
  258. * Align offset to 4 bytes as needed for note name and descriptor data.
  259. */
  260. #define NOTE_ALIGN(n) (((n) + 3) & -4U)
  261. static int elf_read_build_id(Elf *elf, void *bf, size_t size)
  262. {
  263. int err = -1;
  264. GElf_Ehdr ehdr;
  265. GElf_Shdr shdr;
  266. Elf_Data *data;
  267. Elf_Scn *sec;
  268. Elf_Kind ek;
  269. void *ptr;
  270. if (size < BUILD_ID_SIZE)
  271. goto out;
  272. ek = elf_kind(elf);
  273. if (ek != ELF_K_ELF)
  274. goto out;
  275. if (gelf_getehdr(elf, &ehdr) == NULL) {
  276. pr_err("%s: cannot get elf header.\n", __func__);
  277. goto out;
  278. }
  279. /*
  280. * Check following sections for notes:
  281. * '.note.gnu.build-id'
  282. * '.notes'
  283. * '.note' (VDSO specific)
  284. */
  285. do {
  286. sec = elf_section_by_name(elf, &ehdr, &shdr,
  287. ".note.gnu.build-id", NULL);
  288. if (sec)
  289. break;
  290. sec = elf_section_by_name(elf, &ehdr, &shdr,
  291. ".notes", NULL);
  292. if (sec)
  293. break;
  294. sec = elf_section_by_name(elf, &ehdr, &shdr,
  295. ".note", NULL);
  296. if (sec)
  297. break;
  298. return err;
  299. } while (0);
  300. data = elf_getdata(sec, NULL);
  301. if (data == NULL)
  302. goto out;
  303. ptr = data->d_buf;
  304. while (ptr < (data->d_buf + data->d_size)) {
  305. GElf_Nhdr *nhdr = ptr;
  306. size_t namesz = NOTE_ALIGN(nhdr->n_namesz),
  307. descsz = NOTE_ALIGN(nhdr->n_descsz);
  308. const char *name;
  309. ptr += sizeof(*nhdr);
  310. name = ptr;
  311. ptr += namesz;
  312. if (nhdr->n_type == NT_GNU_BUILD_ID &&
  313. nhdr->n_namesz == sizeof("GNU")) {
  314. if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
  315. size_t sz = min(size, descsz);
  316. memcpy(bf, ptr, sz);
  317. memset(bf + sz, 0, size - sz);
  318. err = descsz;
  319. break;
  320. }
  321. }
  322. ptr += descsz;
  323. }
  324. out:
  325. return err;
  326. }
  327. int filename__read_build_id(const char *filename, void *bf, size_t size)
  328. {
  329. int fd, err = -1;
  330. Elf *elf;
  331. if (size < BUILD_ID_SIZE)
  332. goto out;
  333. fd = open(filename, O_RDONLY);
  334. if (fd < 0)
  335. goto out;
  336. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  337. if (elf == NULL) {
  338. pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
  339. goto out_close;
  340. }
  341. err = elf_read_build_id(elf, bf, size);
  342. elf_end(elf);
  343. out_close:
  344. close(fd);
  345. out:
  346. return err;
  347. }
  348. int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
  349. {
  350. int fd, err = -1;
  351. if (size < BUILD_ID_SIZE)
  352. goto out;
  353. fd = open(filename, O_RDONLY);
  354. if (fd < 0)
  355. goto out;
  356. while (1) {
  357. char bf[BUFSIZ];
  358. GElf_Nhdr nhdr;
  359. size_t namesz, descsz;
  360. if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
  361. break;
  362. namesz = NOTE_ALIGN(nhdr.n_namesz);
  363. descsz = NOTE_ALIGN(nhdr.n_descsz);
  364. if (nhdr.n_type == NT_GNU_BUILD_ID &&
  365. nhdr.n_namesz == sizeof("GNU")) {
  366. if (read(fd, bf, namesz) != (ssize_t)namesz)
  367. break;
  368. if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
  369. size_t sz = min(descsz, size);
  370. if (read(fd, build_id, sz) == (ssize_t)sz) {
  371. memset(build_id + sz, 0, size - sz);
  372. err = 0;
  373. break;
  374. }
  375. } else if (read(fd, bf, descsz) != (ssize_t)descsz)
  376. break;
  377. } else {
  378. int n = namesz + descsz;
  379. if (read(fd, bf, n) != n)
  380. break;
  381. }
  382. }
  383. close(fd);
  384. out:
  385. return err;
  386. }
  387. int filename__read_debuglink(const char *filename, char *debuglink,
  388. size_t size)
  389. {
  390. int fd, err = -1;
  391. Elf *elf;
  392. GElf_Ehdr ehdr;
  393. GElf_Shdr shdr;
  394. Elf_Data *data;
  395. Elf_Scn *sec;
  396. Elf_Kind ek;
  397. fd = open(filename, O_RDONLY);
  398. if (fd < 0)
  399. goto out;
  400. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  401. if (elf == NULL) {
  402. pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
  403. goto out_close;
  404. }
  405. ek = elf_kind(elf);
  406. if (ek != ELF_K_ELF)
  407. goto out_close;
  408. if (gelf_getehdr(elf, &ehdr) == NULL) {
  409. pr_err("%s: cannot get elf header.\n", __func__);
  410. goto out_close;
  411. }
  412. sec = elf_section_by_name(elf, &ehdr, &shdr,
  413. ".gnu_debuglink", NULL);
  414. if (sec == NULL)
  415. goto out_close;
  416. data = elf_getdata(sec, NULL);
  417. if (data == NULL)
  418. goto out_close;
  419. /* the start of this section is a zero-terminated string */
  420. strncpy(debuglink, data->d_buf, size);
  421. elf_end(elf);
  422. out_close:
  423. close(fd);
  424. out:
  425. return err;
  426. }
  427. static int dso__swap_init(struct dso *dso, unsigned char eidata)
  428. {
  429. static unsigned int const endian = 1;
  430. dso->needs_swap = DSO_SWAP__NO;
  431. switch (eidata) {
  432. case ELFDATA2LSB:
  433. /* We are big endian, DSO is little endian. */
  434. if (*(unsigned char const *)&endian != 1)
  435. dso->needs_swap = DSO_SWAP__YES;
  436. break;
  437. case ELFDATA2MSB:
  438. /* We are little endian, DSO is big endian. */
  439. if (*(unsigned char const *)&endian != 0)
  440. dso->needs_swap = DSO_SWAP__YES;
  441. break;
  442. default:
  443. pr_err("unrecognized DSO data encoding %d\n", eidata);
  444. return -EINVAL;
  445. }
  446. return 0;
  447. }
  448. int dso__load_sym(struct dso *dso, struct map *map, const char *name, int fd,
  449. symbol_filter_t filter, int kmodule, int want_symtab)
  450. {
  451. struct kmap *kmap = dso->kernel ? map__kmap(map) : NULL;
  452. struct map *curr_map = map;
  453. struct dso *curr_dso = dso;
  454. Elf_Data *symstrs, *secstrs;
  455. uint32_t nr_syms;
  456. int err = -1;
  457. uint32_t idx;
  458. GElf_Ehdr ehdr;
  459. GElf_Shdr shdr, opdshdr;
  460. Elf_Data *syms, *opddata = NULL;
  461. GElf_Sym sym;
  462. Elf_Scn *sec, *sec_strndx, *opdsec;
  463. Elf *elf;
  464. int nr = 0;
  465. size_t opdidx = 0;
  466. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  467. if (elf == NULL) {
  468. pr_debug("%s: cannot read %s ELF file.\n", __func__, name);
  469. goto out_close;
  470. }
  471. if (gelf_getehdr(elf, &ehdr) == NULL) {
  472. pr_debug("%s: cannot get elf header.\n", __func__);
  473. goto out_elf_end;
  474. }
  475. if (dso__swap_init(dso, ehdr.e_ident[EI_DATA]))
  476. goto out_elf_end;
  477. /* Always reject images with a mismatched build-id: */
  478. if (dso->has_build_id) {
  479. u8 build_id[BUILD_ID_SIZE];
  480. if (elf_read_build_id(elf, build_id, BUILD_ID_SIZE) < 0)
  481. goto out_elf_end;
  482. if (!dso__build_id_equal(dso, build_id))
  483. goto out_elf_end;
  484. }
  485. sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
  486. if (sec == NULL) {
  487. if (want_symtab)
  488. goto out_elf_end;
  489. sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
  490. if (sec == NULL)
  491. goto out_elf_end;
  492. }
  493. opdsec = elf_section_by_name(elf, &ehdr, &opdshdr, ".opd", &opdidx);
  494. if (opdshdr.sh_type != SHT_PROGBITS)
  495. opdsec = NULL;
  496. if (opdsec)
  497. opddata = elf_rawdata(opdsec, NULL);
  498. syms = elf_getdata(sec, NULL);
  499. if (syms == NULL)
  500. goto out_elf_end;
  501. sec = elf_getscn(elf, shdr.sh_link);
  502. if (sec == NULL)
  503. goto out_elf_end;
  504. symstrs = elf_getdata(sec, NULL);
  505. if (symstrs == NULL)
  506. goto out_elf_end;
  507. sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
  508. if (sec_strndx == NULL)
  509. goto out_elf_end;
  510. secstrs = elf_getdata(sec_strndx, NULL);
  511. if (secstrs == NULL)
  512. goto out_elf_end;
  513. nr_syms = shdr.sh_size / shdr.sh_entsize;
  514. memset(&sym, 0, sizeof(sym));
  515. if (dso->kernel == DSO_TYPE_USER) {
  516. dso->adjust_symbols = (ehdr.e_type == ET_EXEC ||
  517. elf_section_by_name(elf, &ehdr, &shdr,
  518. ".gnu.prelink_undo",
  519. NULL) != NULL);
  520. } else {
  521. dso->adjust_symbols = 0;
  522. }
  523. elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
  524. struct symbol *f;
  525. const char *elf_name = elf_sym__name(&sym, symstrs);
  526. char *demangled = NULL;
  527. int is_label = elf_sym__is_label(&sym);
  528. const char *section_name;
  529. if (kmap && kmap->ref_reloc_sym && kmap->ref_reloc_sym->name &&
  530. strcmp(elf_name, kmap->ref_reloc_sym->name) == 0)
  531. kmap->ref_reloc_sym->unrelocated_addr = sym.st_value;
  532. if (!is_label && !elf_sym__is_a(&sym, map->type))
  533. continue;
  534. /* Reject ARM ELF "mapping symbols": these aren't unique and
  535. * don't identify functions, so will confuse the profile
  536. * output: */
  537. if (ehdr.e_machine == EM_ARM) {
  538. if (!strcmp(elf_name, "$a") ||
  539. !strcmp(elf_name, "$d") ||
  540. !strcmp(elf_name, "$t"))
  541. continue;
  542. }
  543. if (opdsec && sym.st_shndx == opdidx) {
  544. u32 offset = sym.st_value - opdshdr.sh_addr;
  545. u64 *opd = opddata->d_buf + offset;
  546. sym.st_value = DSO__SWAP(dso, u64, *opd);
  547. sym.st_shndx = elf_addr_to_index(elf, sym.st_value);
  548. }
  549. sec = elf_getscn(elf, sym.st_shndx);
  550. if (!sec)
  551. goto out_elf_end;
  552. gelf_getshdr(sec, &shdr);
  553. if (is_label && !elf_sec__is_a(&shdr, secstrs, map->type))
  554. continue;
  555. section_name = elf_sec__name(&shdr, secstrs);
  556. /* On ARM, symbols for thumb functions have 1 added to
  557. * the symbol address as a flag - remove it */
  558. if ((ehdr.e_machine == EM_ARM) &&
  559. (map->type == MAP__FUNCTION) &&
  560. (sym.st_value & 1))
  561. --sym.st_value;
  562. if (dso->kernel != DSO_TYPE_USER || kmodule) {
  563. char dso_name[PATH_MAX];
  564. if (strcmp(section_name,
  565. (curr_dso->short_name +
  566. dso->short_name_len)) == 0)
  567. goto new_symbol;
  568. if (strcmp(section_name, ".text") == 0) {
  569. curr_map = map;
  570. curr_dso = dso;
  571. goto new_symbol;
  572. }
  573. snprintf(dso_name, sizeof(dso_name),
  574. "%s%s", dso->short_name, section_name);
  575. curr_map = map_groups__find_by_name(kmap->kmaps, map->type, dso_name);
  576. if (curr_map == NULL) {
  577. u64 start = sym.st_value;
  578. if (kmodule)
  579. start += map->start + shdr.sh_offset;
  580. curr_dso = dso__new(dso_name);
  581. if (curr_dso == NULL)
  582. goto out_elf_end;
  583. curr_dso->kernel = dso->kernel;
  584. curr_dso->long_name = dso->long_name;
  585. curr_dso->long_name_len = dso->long_name_len;
  586. curr_map = map__new2(start, curr_dso,
  587. map->type);
  588. if (curr_map == NULL) {
  589. dso__delete(curr_dso);
  590. goto out_elf_end;
  591. }
  592. curr_map->map_ip = identity__map_ip;
  593. curr_map->unmap_ip = identity__map_ip;
  594. curr_dso->symtab_type = dso->symtab_type;
  595. map_groups__insert(kmap->kmaps, curr_map);
  596. dsos__add(&dso->node, curr_dso);
  597. dso__set_loaded(curr_dso, map->type);
  598. } else
  599. curr_dso = curr_map->dso;
  600. goto new_symbol;
  601. }
  602. if (curr_dso->adjust_symbols && sym.st_value) {
  603. pr_debug4("%s: adjusting symbol: st_value: %#" PRIx64 " "
  604. "sh_addr: %#" PRIx64 " sh_offset: %#" PRIx64 "\n", __func__,
  605. (u64)sym.st_value, (u64)shdr.sh_addr,
  606. (u64)shdr.sh_offset);
  607. sym.st_value -= shdr.sh_addr - shdr.sh_offset;
  608. }
  609. /*
  610. * We need to figure out if the object was created from C++ sources
  611. * DWARF DW_compile_unit has this, but we don't always have access
  612. * to it...
  613. */
  614. demangled = bfd_demangle(NULL, elf_name, DMGL_PARAMS | DMGL_ANSI);
  615. if (demangled != NULL)
  616. elf_name = demangled;
  617. new_symbol:
  618. f = symbol__new(sym.st_value, sym.st_size,
  619. GELF_ST_BIND(sym.st_info), elf_name);
  620. free(demangled);
  621. if (!f)
  622. goto out_elf_end;
  623. if (filter && filter(curr_map, f))
  624. symbol__delete(f);
  625. else {
  626. symbols__insert(&curr_dso->symbols[curr_map->type], f);
  627. nr++;
  628. }
  629. }
  630. /*
  631. * For misannotated, zeroed, ASM function sizes.
  632. */
  633. if (nr > 0) {
  634. symbols__fixup_duplicate(&dso->symbols[map->type]);
  635. symbols__fixup_end(&dso->symbols[map->type]);
  636. if (kmap) {
  637. /*
  638. * We need to fixup this here too because we create new
  639. * maps here, for things like vsyscall sections.
  640. */
  641. __map_groups__fixup_end(kmap->kmaps, map->type);
  642. }
  643. }
  644. err = nr;
  645. out_elf_end:
  646. elf_end(elf);
  647. out_close:
  648. return err;
  649. }
  650. void symbol__elf_init(void)
  651. {
  652. elf_version(EV_CURRENT);
  653. }