symbol-elf.c 17 KB

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