symbol-elf.c 19 KB

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