symbol-elf.c 19 KB

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