symbol-elf.c 18 KB

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