symbol-elf.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  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 HAVE_ELF_GETPHDRNUM
  10. static int elf_getphdrnum(Elf *elf, size_t *dst)
  11. {
  12. GElf_Ehdr gehdr;
  13. GElf_Ehdr *ehdr;
  14. ehdr = gelf_getehdr(elf, &gehdr);
  15. if (!ehdr)
  16. return -1;
  17. *dst = ehdr->e_phnum;
  18. return 0;
  19. }
  20. #endif
  21. #ifndef NT_GNU_BUILD_ID
  22. #define NT_GNU_BUILD_ID 3
  23. #endif
  24. /**
  25. * elf_symtab__for_each_symbol - iterate thru all the symbols
  26. *
  27. * @syms: struct elf_symtab instance to iterate
  28. * @idx: uint32_t idx
  29. * @sym: GElf_Sym iterator
  30. */
  31. #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
  32. for (idx = 0, gelf_getsym(syms, idx, &sym);\
  33. idx < nr_syms; \
  34. idx++, gelf_getsym(syms, idx, &sym))
  35. static inline uint8_t elf_sym__type(const GElf_Sym *sym)
  36. {
  37. return GELF_ST_TYPE(sym->st_info);
  38. }
  39. static inline int elf_sym__is_function(const GElf_Sym *sym)
  40. {
  41. return elf_sym__type(sym) == STT_FUNC &&
  42. sym->st_name != 0 &&
  43. sym->st_shndx != SHN_UNDEF;
  44. }
  45. static inline bool elf_sym__is_object(const GElf_Sym *sym)
  46. {
  47. return elf_sym__type(sym) == STT_OBJECT &&
  48. sym->st_name != 0 &&
  49. sym->st_shndx != SHN_UNDEF;
  50. }
  51. static inline int elf_sym__is_label(const GElf_Sym *sym)
  52. {
  53. return elf_sym__type(sym) == STT_NOTYPE &&
  54. sym->st_name != 0 &&
  55. sym->st_shndx != SHN_UNDEF &&
  56. sym->st_shndx != SHN_ABS;
  57. }
  58. static bool elf_sym__is_a(GElf_Sym *sym, enum map_type type)
  59. {
  60. switch (type) {
  61. case MAP__FUNCTION:
  62. return elf_sym__is_function(sym);
  63. case MAP__VARIABLE:
  64. return elf_sym__is_object(sym);
  65. default:
  66. return false;
  67. }
  68. }
  69. static inline const char *elf_sym__name(const GElf_Sym *sym,
  70. const Elf_Data *symstrs)
  71. {
  72. return symstrs->d_buf + sym->st_name;
  73. }
  74. static inline const char *elf_sec__name(const GElf_Shdr *shdr,
  75. const Elf_Data *secstrs)
  76. {
  77. return secstrs->d_buf + shdr->sh_name;
  78. }
  79. static inline int elf_sec__is_text(const GElf_Shdr *shdr,
  80. const Elf_Data *secstrs)
  81. {
  82. return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
  83. }
  84. static inline bool elf_sec__is_data(const GElf_Shdr *shdr,
  85. const Elf_Data *secstrs)
  86. {
  87. return strstr(elf_sec__name(shdr, secstrs), "data") != NULL;
  88. }
  89. static bool elf_sec__is_a(GElf_Shdr *shdr, Elf_Data *secstrs,
  90. enum map_type type)
  91. {
  92. switch (type) {
  93. case MAP__FUNCTION:
  94. return elf_sec__is_text(shdr, secstrs);
  95. case MAP__VARIABLE:
  96. return elf_sec__is_data(shdr, secstrs);
  97. default:
  98. return false;
  99. }
  100. }
  101. static size_t elf_addr_to_index(Elf *elf, GElf_Addr addr)
  102. {
  103. Elf_Scn *sec = NULL;
  104. GElf_Shdr shdr;
  105. size_t cnt = 1;
  106. while ((sec = elf_nextscn(elf, sec)) != NULL) {
  107. gelf_getshdr(sec, &shdr);
  108. if ((addr >= shdr.sh_addr) &&
  109. (addr < (shdr.sh_addr + shdr.sh_size)))
  110. return cnt;
  111. ++cnt;
  112. }
  113. return -1;
  114. }
  115. static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
  116. GElf_Shdr *shp, const char *name,
  117. size_t *idx)
  118. {
  119. Elf_Scn *sec = NULL;
  120. size_t cnt = 1;
  121. /* Elf is corrupted/truncated, avoid calling elf_strptr. */
  122. if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL))
  123. return NULL;
  124. while ((sec = elf_nextscn(elf, sec)) != NULL) {
  125. char *str;
  126. gelf_getshdr(sec, shp);
  127. str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
  128. if (!strcmp(name, str)) {
  129. if (idx)
  130. *idx = cnt;
  131. break;
  132. }
  133. ++cnt;
  134. }
  135. return sec;
  136. }
  137. #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
  138. for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
  139. idx < nr_entries; \
  140. ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
  141. #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
  142. for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
  143. idx < nr_entries; \
  144. ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
  145. /*
  146. * We need to check if we have a .dynsym, so that we can handle the
  147. * .plt, synthesizing its symbols, that aren't on the symtabs (be it
  148. * .dynsym or .symtab).
  149. * And always look at the original dso, not at debuginfo packages, that
  150. * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
  151. */
  152. int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss, struct map *map,
  153. symbol_filter_t filter)
  154. {
  155. uint32_t nr_rel_entries, idx;
  156. GElf_Sym sym;
  157. u64 plt_offset;
  158. GElf_Shdr shdr_plt;
  159. struct symbol *f;
  160. GElf_Shdr shdr_rel_plt, shdr_dynsym;
  161. Elf_Data *reldata, *syms, *symstrs;
  162. Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
  163. size_t dynsym_idx;
  164. GElf_Ehdr ehdr;
  165. char sympltname[1024];
  166. Elf *elf;
  167. int nr = 0, symidx, err = 0;
  168. if (!ss->dynsym)
  169. return 0;
  170. elf = ss->elf;
  171. ehdr = ss->ehdr;
  172. scn_dynsym = ss->dynsym;
  173. shdr_dynsym = ss->dynshdr;
  174. dynsym_idx = ss->dynsym_idx;
  175. if (scn_dynsym == NULL)
  176. goto out_elf_end;
  177. scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
  178. ".rela.plt", NULL);
  179. if (scn_plt_rel == NULL) {
  180. scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
  181. ".rel.plt", NULL);
  182. if (scn_plt_rel == NULL)
  183. goto out_elf_end;
  184. }
  185. err = -1;
  186. if (shdr_rel_plt.sh_link != dynsym_idx)
  187. goto out_elf_end;
  188. if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
  189. goto out_elf_end;
  190. /*
  191. * Fetch the relocation section to find the idxes to the GOT
  192. * and the symbols in the .dynsym they refer to.
  193. */
  194. reldata = elf_getdata(scn_plt_rel, NULL);
  195. if (reldata == NULL)
  196. goto out_elf_end;
  197. syms = elf_getdata(scn_dynsym, NULL);
  198. if (syms == NULL)
  199. goto out_elf_end;
  200. scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
  201. if (scn_symstrs == NULL)
  202. goto out_elf_end;
  203. symstrs = elf_getdata(scn_symstrs, NULL);
  204. if (symstrs == NULL)
  205. goto out_elf_end;
  206. if (symstrs->d_size == 0)
  207. goto out_elf_end;
  208. nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
  209. plt_offset = shdr_plt.sh_offset;
  210. if (shdr_rel_plt.sh_type == SHT_RELA) {
  211. GElf_Rela pos_mem, *pos;
  212. elf_section__for_each_rela(reldata, pos, pos_mem, idx,
  213. nr_rel_entries) {
  214. symidx = GELF_R_SYM(pos->r_info);
  215. plt_offset += shdr_plt.sh_entsize;
  216. gelf_getsym(syms, symidx, &sym);
  217. snprintf(sympltname, sizeof(sympltname),
  218. "%s@plt", elf_sym__name(&sym, symstrs));
  219. f = symbol__new(plt_offset, shdr_plt.sh_entsize,
  220. STB_GLOBAL, sympltname);
  221. if (!f)
  222. goto out_elf_end;
  223. if (filter && filter(map, f))
  224. symbol__delete(f);
  225. else {
  226. symbols__insert(&dso->symbols[map->type], f);
  227. ++nr;
  228. }
  229. }
  230. } else if (shdr_rel_plt.sh_type == SHT_REL) {
  231. GElf_Rel pos_mem, *pos;
  232. elf_section__for_each_rel(reldata, pos, pos_mem, idx,
  233. nr_rel_entries) {
  234. symidx = GELF_R_SYM(pos->r_info);
  235. plt_offset += shdr_plt.sh_entsize;
  236. gelf_getsym(syms, symidx, &sym);
  237. snprintf(sympltname, sizeof(sympltname),
  238. "%s@plt", elf_sym__name(&sym, symstrs));
  239. f = symbol__new(plt_offset, shdr_plt.sh_entsize,
  240. STB_GLOBAL, sympltname);
  241. if (!f)
  242. goto out_elf_end;
  243. if (filter && filter(map, f))
  244. symbol__delete(f);
  245. else {
  246. symbols__insert(&dso->symbols[map->type], f);
  247. ++nr;
  248. }
  249. }
  250. }
  251. err = 0;
  252. out_elf_end:
  253. if (err == 0)
  254. return nr;
  255. pr_debug("%s: problems reading %s PLT info.\n",
  256. __func__, dso->long_name);
  257. return 0;
  258. }
  259. /*
  260. * Align offset to 4 bytes as needed for note name and descriptor data.
  261. */
  262. #define NOTE_ALIGN(n) (((n) + 3) & -4U)
  263. static int elf_read_build_id(Elf *elf, void *bf, size_t size)
  264. {
  265. int err = -1;
  266. GElf_Ehdr ehdr;
  267. GElf_Shdr shdr;
  268. Elf_Data *data;
  269. Elf_Scn *sec;
  270. Elf_Kind ek;
  271. void *ptr;
  272. if (size < BUILD_ID_SIZE)
  273. goto out;
  274. ek = elf_kind(elf);
  275. if (ek != ELF_K_ELF)
  276. goto out;
  277. if (gelf_getehdr(elf, &ehdr) == NULL) {
  278. pr_err("%s: cannot get elf header.\n", __func__);
  279. goto out;
  280. }
  281. /*
  282. * Check following sections for notes:
  283. * '.note.gnu.build-id'
  284. * '.notes'
  285. * '.note' (VDSO specific)
  286. */
  287. do {
  288. sec = elf_section_by_name(elf, &ehdr, &shdr,
  289. ".note.gnu.build-id", NULL);
  290. if (sec)
  291. break;
  292. sec = elf_section_by_name(elf, &ehdr, &shdr,
  293. ".notes", NULL);
  294. if (sec)
  295. break;
  296. sec = elf_section_by_name(elf, &ehdr, &shdr,
  297. ".note", NULL);
  298. if (sec)
  299. break;
  300. return err;
  301. } while (0);
  302. data = elf_getdata(sec, NULL);
  303. if (data == NULL)
  304. goto out;
  305. ptr = data->d_buf;
  306. while (ptr < (data->d_buf + data->d_size)) {
  307. GElf_Nhdr *nhdr = ptr;
  308. size_t namesz = NOTE_ALIGN(nhdr->n_namesz),
  309. descsz = NOTE_ALIGN(nhdr->n_descsz);
  310. const char *name;
  311. ptr += sizeof(*nhdr);
  312. name = ptr;
  313. ptr += namesz;
  314. if (nhdr->n_type == NT_GNU_BUILD_ID &&
  315. nhdr->n_namesz == sizeof("GNU")) {
  316. if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
  317. size_t sz = min(size, descsz);
  318. memcpy(bf, ptr, sz);
  319. memset(bf + sz, 0, size - sz);
  320. err = descsz;
  321. break;
  322. }
  323. }
  324. ptr += descsz;
  325. }
  326. out:
  327. return err;
  328. }
  329. int filename__read_build_id(const char *filename, void *bf, size_t size)
  330. {
  331. int fd, err = -1;
  332. Elf *elf;
  333. if (size < BUILD_ID_SIZE)
  334. goto out;
  335. fd = open(filename, O_RDONLY);
  336. if (fd < 0)
  337. goto out;
  338. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  339. if (elf == NULL) {
  340. pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
  341. goto out_close;
  342. }
  343. err = elf_read_build_id(elf, bf, size);
  344. elf_end(elf);
  345. out_close:
  346. close(fd);
  347. out:
  348. return err;
  349. }
  350. int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
  351. {
  352. int fd, err = -1;
  353. if (size < BUILD_ID_SIZE)
  354. goto out;
  355. fd = open(filename, O_RDONLY);
  356. if (fd < 0)
  357. goto out;
  358. while (1) {
  359. char bf[BUFSIZ];
  360. GElf_Nhdr nhdr;
  361. size_t namesz, descsz;
  362. if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
  363. break;
  364. namesz = NOTE_ALIGN(nhdr.n_namesz);
  365. descsz = NOTE_ALIGN(nhdr.n_descsz);
  366. if (nhdr.n_type == NT_GNU_BUILD_ID &&
  367. nhdr.n_namesz == sizeof("GNU")) {
  368. if (read(fd, bf, namesz) != (ssize_t)namesz)
  369. break;
  370. if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
  371. size_t sz = min(descsz, size);
  372. if (read(fd, build_id, sz) == (ssize_t)sz) {
  373. memset(build_id + sz, 0, size - sz);
  374. err = 0;
  375. break;
  376. }
  377. } else if (read(fd, bf, descsz) != (ssize_t)descsz)
  378. break;
  379. } else {
  380. int n = namesz + descsz;
  381. if (read(fd, bf, n) != n)
  382. break;
  383. }
  384. }
  385. close(fd);
  386. out:
  387. return err;
  388. }
  389. int filename__read_debuglink(const char *filename, char *debuglink,
  390. size_t size)
  391. {
  392. int fd, err = -1;
  393. Elf *elf;
  394. GElf_Ehdr ehdr;
  395. GElf_Shdr shdr;
  396. Elf_Data *data;
  397. Elf_Scn *sec;
  398. Elf_Kind ek;
  399. fd = open(filename, O_RDONLY);
  400. if (fd < 0)
  401. goto out;
  402. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  403. if (elf == NULL) {
  404. pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
  405. goto out_close;
  406. }
  407. ek = elf_kind(elf);
  408. if (ek != ELF_K_ELF)
  409. goto out_close;
  410. if (gelf_getehdr(elf, &ehdr) == NULL) {
  411. pr_err("%s: cannot get elf header.\n", __func__);
  412. goto out_close;
  413. }
  414. sec = elf_section_by_name(elf, &ehdr, &shdr,
  415. ".gnu_debuglink", NULL);
  416. if (sec == NULL)
  417. goto out_close;
  418. data = elf_getdata(sec, NULL);
  419. if (data == NULL)
  420. goto out_close;
  421. /* the start of this section is a zero-terminated string */
  422. strncpy(debuglink, data->d_buf, size);
  423. elf_end(elf);
  424. out_close:
  425. close(fd);
  426. out:
  427. return err;
  428. }
  429. static int dso__swap_init(struct dso *dso, unsigned char eidata)
  430. {
  431. static unsigned int const endian = 1;
  432. dso->needs_swap = DSO_SWAP__NO;
  433. switch (eidata) {
  434. case ELFDATA2LSB:
  435. /* We are big endian, DSO is little endian. */
  436. if (*(unsigned char const *)&endian != 1)
  437. dso->needs_swap = DSO_SWAP__YES;
  438. break;
  439. case ELFDATA2MSB:
  440. /* We are little endian, DSO is big endian. */
  441. if (*(unsigned char const *)&endian != 0)
  442. dso->needs_swap = DSO_SWAP__YES;
  443. break;
  444. default:
  445. pr_err("unrecognized DSO data encoding %d\n", eidata);
  446. return -EINVAL;
  447. }
  448. return 0;
  449. }
  450. bool symsrc__possibly_runtime(struct symsrc *ss)
  451. {
  452. return ss->dynsym || ss->opdsec;
  453. }
  454. bool symsrc__has_symtab(struct symsrc *ss)
  455. {
  456. return ss->symtab != NULL;
  457. }
  458. void symsrc__destroy(struct symsrc *ss)
  459. {
  460. free(ss->name);
  461. elf_end(ss->elf);
  462. close(ss->fd);
  463. }
  464. int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name,
  465. enum dso_binary_type type)
  466. {
  467. int err = -1;
  468. GElf_Ehdr ehdr;
  469. Elf *elf;
  470. int fd;
  471. fd = open(name, O_RDONLY);
  472. if (fd < 0)
  473. return -1;
  474. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  475. if (elf == NULL) {
  476. pr_debug("%s: cannot read %s ELF file.\n", __func__, name);
  477. goto out_close;
  478. }
  479. if (gelf_getehdr(elf, &ehdr) == NULL) {
  480. pr_debug("%s: cannot get elf header.\n", __func__);
  481. goto out_elf_end;
  482. }
  483. if (dso__swap_init(dso, ehdr.e_ident[EI_DATA]))
  484. goto out_elf_end;
  485. /* Always reject images with a mismatched build-id: */
  486. if (dso->has_build_id) {
  487. u8 build_id[BUILD_ID_SIZE];
  488. if (elf_read_build_id(elf, build_id, BUILD_ID_SIZE) < 0)
  489. goto out_elf_end;
  490. if (!dso__build_id_equal(dso, build_id))
  491. goto out_elf_end;
  492. }
  493. ss->symtab = elf_section_by_name(elf, &ehdr, &ss->symshdr, ".symtab",
  494. NULL);
  495. if (ss->symshdr.sh_type != SHT_SYMTAB)
  496. ss->symtab = NULL;
  497. ss->dynsym_idx = 0;
  498. ss->dynsym = elf_section_by_name(elf, &ehdr, &ss->dynshdr, ".dynsym",
  499. &ss->dynsym_idx);
  500. if (ss->dynshdr.sh_type != SHT_DYNSYM)
  501. ss->dynsym = NULL;
  502. ss->opdidx = 0;
  503. ss->opdsec = elf_section_by_name(elf, &ehdr, &ss->opdshdr, ".opd",
  504. &ss->opdidx);
  505. if (ss->opdshdr.sh_type != SHT_PROGBITS)
  506. ss->opdsec = NULL;
  507. if (dso->kernel == DSO_TYPE_USER) {
  508. GElf_Shdr shdr;
  509. ss->adjust_symbols = (ehdr.e_type == ET_EXEC ||
  510. ehdr.e_type == ET_REL ||
  511. elf_section_by_name(elf, &ehdr, &shdr,
  512. ".gnu.prelink_undo",
  513. NULL) != NULL);
  514. } else {
  515. ss->adjust_symbols = ehdr.e_type == ET_EXEC ||
  516. ehdr.e_type == ET_REL;
  517. }
  518. ss->name = strdup(name);
  519. if (!ss->name)
  520. goto out_elf_end;
  521. ss->elf = elf;
  522. ss->fd = fd;
  523. ss->ehdr = ehdr;
  524. ss->type = type;
  525. return 0;
  526. out_elf_end:
  527. elf_end(elf);
  528. out_close:
  529. close(fd);
  530. return err;
  531. }
  532. /**
  533. * ref_reloc_sym_not_found - has kernel relocation symbol been found.
  534. * @kmap: kernel maps and relocation reference symbol
  535. *
  536. * This function returns %true if we are dealing with the kernel maps and the
  537. * relocation reference symbol has not yet been found. Otherwise %false is
  538. * returned.
  539. */
  540. static bool ref_reloc_sym_not_found(struct kmap *kmap)
  541. {
  542. return kmap && kmap->ref_reloc_sym && kmap->ref_reloc_sym->name &&
  543. !kmap->ref_reloc_sym->unrelocated_addr;
  544. }
  545. /**
  546. * ref_reloc - kernel relocation offset.
  547. * @kmap: kernel maps and relocation reference symbol
  548. *
  549. * This function returns the offset of kernel addresses as determined by using
  550. * the relocation reference symbol i.e. if the kernel has not been relocated
  551. * then the return value is zero.
  552. */
  553. static u64 ref_reloc(struct kmap *kmap)
  554. {
  555. if (kmap && kmap->ref_reloc_sym &&
  556. kmap->ref_reloc_sym->unrelocated_addr)
  557. return kmap->ref_reloc_sym->addr -
  558. kmap->ref_reloc_sym->unrelocated_addr;
  559. return 0;
  560. }
  561. int dso__load_sym(struct dso *dso, struct map *map,
  562. struct symsrc *syms_ss, struct symsrc *runtime_ss,
  563. symbol_filter_t filter, int kmodule)
  564. {
  565. struct kmap *kmap = dso->kernel ? map__kmap(map) : NULL;
  566. struct map *curr_map = map;
  567. struct dso *curr_dso = dso;
  568. Elf_Data *symstrs, *secstrs;
  569. uint32_t nr_syms;
  570. int err = -1;
  571. uint32_t idx;
  572. GElf_Ehdr ehdr;
  573. GElf_Shdr shdr;
  574. Elf_Data *syms, *opddata = NULL;
  575. GElf_Sym sym;
  576. Elf_Scn *sec, *sec_strndx;
  577. Elf *elf;
  578. int nr = 0;
  579. bool remap_kernel = false, adjust_kernel_syms = false;
  580. dso->symtab_type = syms_ss->type;
  581. dso->rel = syms_ss->ehdr.e_type == ET_REL;
  582. /*
  583. * Modules may already have symbols from kallsyms, but those symbols
  584. * have the wrong values for the dso maps, so remove them.
  585. */
  586. if (kmodule && syms_ss->symtab)
  587. symbols__delete(&dso->symbols[map->type]);
  588. if (!syms_ss->symtab) {
  589. syms_ss->symtab = syms_ss->dynsym;
  590. syms_ss->symshdr = syms_ss->dynshdr;
  591. }
  592. elf = syms_ss->elf;
  593. ehdr = syms_ss->ehdr;
  594. sec = syms_ss->symtab;
  595. shdr = syms_ss->symshdr;
  596. if (runtime_ss->opdsec)
  597. opddata = elf_rawdata(runtime_ss->opdsec, NULL);
  598. syms = elf_getdata(sec, NULL);
  599. if (syms == NULL)
  600. goto out_elf_end;
  601. sec = elf_getscn(elf, shdr.sh_link);
  602. if (sec == NULL)
  603. goto out_elf_end;
  604. symstrs = elf_getdata(sec, NULL);
  605. if (symstrs == NULL)
  606. goto out_elf_end;
  607. sec_strndx = elf_getscn(elf, ehdr.e_shstrndx);
  608. if (sec_strndx == NULL)
  609. goto out_elf_end;
  610. secstrs = elf_getdata(sec_strndx, NULL);
  611. if (secstrs == NULL)
  612. goto out_elf_end;
  613. nr_syms = shdr.sh_size / shdr.sh_entsize;
  614. memset(&sym, 0, sizeof(sym));
  615. /*
  616. * The kernel relocation symbol is needed in advance in order to adjust
  617. * kernel maps correctly.
  618. */
  619. if (ref_reloc_sym_not_found(kmap)) {
  620. elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
  621. const char *elf_name = elf_sym__name(&sym, symstrs);
  622. if (strcmp(elf_name, kmap->ref_reloc_sym->name))
  623. continue;
  624. kmap->ref_reloc_sym->unrelocated_addr = sym.st_value;
  625. break;
  626. }
  627. }
  628. dso->adjust_symbols = runtime_ss->adjust_symbols || ref_reloc(kmap);
  629. /*
  630. * Initial kernel and module mappings do not map to the dso. For
  631. * function mappings, flag the fixups.
  632. */
  633. if (map->type == MAP__FUNCTION && (dso->kernel || kmodule)) {
  634. remap_kernel = true;
  635. adjust_kernel_syms = dso->adjust_symbols;
  636. }
  637. elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
  638. struct symbol *f;
  639. const char *elf_name = elf_sym__name(&sym, symstrs);
  640. char *demangled = NULL;
  641. int is_label = elf_sym__is_label(&sym);
  642. const char *section_name;
  643. bool used_opd = false;
  644. if (!is_label && !elf_sym__is_a(&sym, map->type))
  645. continue;
  646. /* Reject ARM ELF "mapping symbols": these aren't unique and
  647. * don't identify functions, so will confuse the profile
  648. * output: */
  649. if (ehdr.e_machine == EM_ARM) {
  650. if (!strcmp(elf_name, "$a") ||
  651. !strcmp(elf_name, "$d") ||
  652. !strcmp(elf_name, "$t"))
  653. continue;
  654. }
  655. if (runtime_ss->opdsec && sym.st_shndx == runtime_ss->opdidx) {
  656. u32 offset = sym.st_value - syms_ss->opdshdr.sh_addr;
  657. u64 *opd = opddata->d_buf + offset;
  658. sym.st_value = DSO__SWAP(dso, u64, *opd);
  659. sym.st_shndx = elf_addr_to_index(runtime_ss->elf,
  660. sym.st_value);
  661. used_opd = true;
  662. }
  663. /*
  664. * When loading symbols in a data mapping, ABS symbols (which
  665. * has a value of SHN_ABS in its st_shndx) failed at
  666. * elf_getscn(). And it marks the loading as a failure so
  667. * already loaded symbols cannot be fixed up.
  668. *
  669. * I'm not sure what should be done. Just ignore them for now.
  670. * - Namhyung Kim
  671. */
  672. if (sym.st_shndx == SHN_ABS)
  673. continue;
  674. sec = elf_getscn(runtime_ss->elf, sym.st_shndx);
  675. if (!sec)
  676. goto out_elf_end;
  677. gelf_getshdr(sec, &shdr);
  678. if (is_label && !elf_sec__is_a(&shdr, secstrs, map->type))
  679. continue;
  680. section_name = elf_sec__name(&shdr, secstrs);
  681. /* On ARM, symbols for thumb functions have 1 added to
  682. * the symbol address as a flag - remove it */
  683. if ((ehdr.e_machine == EM_ARM) &&
  684. (map->type == MAP__FUNCTION) &&
  685. (sym.st_value & 1))
  686. --sym.st_value;
  687. if (dso->kernel || kmodule) {
  688. char dso_name[PATH_MAX];
  689. /* Adjust symbol to map to file offset */
  690. if (adjust_kernel_syms)
  691. sym.st_value -= shdr.sh_addr - shdr.sh_offset;
  692. if (strcmp(section_name,
  693. (curr_dso->short_name +
  694. dso->short_name_len)) == 0)
  695. goto new_symbol;
  696. if (strcmp(section_name, ".text") == 0) {
  697. /*
  698. * The initial kernel mapping is based on
  699. * kallsyms and identity maps. Overwrite it to
  700. * map to the kernel dso.
  701. */
  702. if (remap_kernel && dso->kernel) {
  703. remap_kernel = false;
  704. map->start = shdr.sh_addr +
  705. ref_reloc(kmap);
  706. map->end = map->start + shdr.sh_size;
  707. map->pgoff = shdr.sh_offset;
  708. map->map_ip = map__map_ip;
  709. map->unmap_ip = map__unmap_ip;
  710. /* Ensure maps are correctly ordered */
  711. map_groups__remove(kmap->kmaps, map);
  712. map_groups__insert(kmap->kmaps, map);
  713. }
  714. /*
  715. * The initial module mapping is based on
  716. * /proc/modules mapped to offset zero.
  717. * Overwrite it to map to the module dso.
  718. */
  719. if (remap_kernel && kmodule) {
  720. remap_kernel = false;
  721. map->pgoff = shdr.sh_offset;
  722. }
  723. curr_map = map;
  724. curr_dso = dso;
  725. goto new_symbol;
  726. }
  727. if (!kmap)
  728. goto new_symbol;
  729. snprintf(dso_name, sizeof(dso_name),
  730. "%s%s", dso->short_name, section_name);
  731. curr_map = map_groups__find_by_name(kmap->kmaps, map->type, dso_name);
  732. if (curr_map == NULL) {
  733. u64 start = sym.st_value;
  734. if (kmodule)
  735. start += map->start + shdr.sh_offset;
  736. curr_dso = dso__new(dso_name);
  737. if (curr_dso == NULL)
  738. goto out_elf_end;
  739. curr_dso->kernel = dso->kernel;
  740. curr_dso->long_name = dso->long_name;
  741. curr_dso->long_name_len = dso->long_name_len;
  742. curr_map = map__new2(start, curr_dso,
  743. map->type);
  744. if (curr_map == NULL) {
  745. dso__delete(curr_dso);
  746. goto out_elf_end;
  747. }
  748. if (adjust_kernel_syms) {
  749. curr_map->start = shdr.sh_addr +
  750. ref_reloc(kmap);
  751. curr_map->end = curr_map->start +
  752. shdr.sh_size;
  753. curr_map->pgoff = shdr.sh_offset;
  754. } else {
  755. curr_map->map_ip = identity__map_ip;
  756. curr_map->unmap_ip = identity__map_ip;
  757. }
  758. curr_dso->symtab_type = dso->symtab_type;
  759. map_groups__insert(kmap->kmaps, curr_map);
  760. dsos__add(&dso->node, curr_dso);
  761. dso__set_loaded(curr_dso, map->type);
  762. } else
  763. curr_dso = curr_map->dso;
  764. goto new_symbol;
  765. }
  766. if ((used_opd && runtime_ss->adjust_symbols)
  767. || (!used_opd && syms_ss->adjust_symbols)) {
  768. pr_debug4("%s: adjusting symbol: st_value: %#" PRIx64 " "
  769. "sh_addr: %#" PRIx64 " sh_offset: %#" PRIx64 "\n", __func__,
  770. (u64)sym.st_value, (u64)shdr.sh_addr,
  771. (u64)shdr.sh_offset);
  772. sym.st_value -= shdr.sh_addr - shdr.sh_offset;
  773. }
  774. /*
  775. * We need to figure out if the object was created from C++ sources
  776. * DWARF DW_compile_unit has this, but we don't always have access
  777. * to it...
  778. */
  779. if (symbol_conf.demangle) {
  780. /*
  781. * The demangler doesn't deal with cloned functions.
  782. * XXXX.clone.NUM or similar
  783. * Strip the dot part and readd it later.
  784. */
  785. char *p = (char *)elf_name, *dot;
  786. dot = strchr(elf_name, '.');
  787. if (dot) {
  788. p = strdup(elf_name);
  789. if (!p)
  790. goto new_symbol;
  791. dot = strchr(p, '.');
  792. *dot = 0;
  793. }
  794. demangled = bfd_demangle(NULL, p,
  795. DMGL_PARAMS | DMGL_ANSI);
  796. if (dot)
  797. *dot = '.';
  798. if (demangled && dot) {
  799. demangled = realloc(demangled, strlen(demangled) + strlen(dot) + 1);
  800. if (!demangled)
  801. goto new_symbol;
  802. strcpy(demangled + (dot - p), dot);
  803. }
  804. if (p != elf_name)
  805. free(p);
  806. if (demangled != NULL)
  807. elf_name = demangled;
  808. }
  809. new_symbol:
  810. f = symbol__new(sym.st_value, sym.st_size,
  811. GELF_ST_BIND(sym.st_info), elf_name);
  812. free(demangled);
  813. if (!f)
  814. goto out_elf_end;
  815. if (filter && filter(curr_map, f))
  816. symbol__delete(f);
  817. else {
  818. symbols__insert(&curr_dso->symbols[curr_map->type], f);
  819. nr++;
  820. }
  821. }
  822. /*
  823. * For misannotated, zeroed, ASM function sizes.
  824. */
  825. if (nr > 0) {
  826. symbols__fixup_duplicate(&dso->symbols[map->type]);
  827. symbols__fixup_end(&dso->symbols[map->type]);
  828. if (kmap) {
  829. /*
  830. * We need to fixup this here too because we create new
  831. * maps here, for things like vsyscall sections.
  832. */
  833. __map_groups__fixup_end(kmap->kmaps, map->type);
  834. }
  835. }
  836. err = nr;
  837. out_elf_end:
  838. return err;
  839. }
  840. static int elf_read_maps(Elf *elf, bool exe, mapfn_t mapfn, void *data)
  841. {
  842. GElf_Phdr phdr;
  843. size_t i, phdrnum;
  844. int err;
  845. u64 sz;
  846. if (elf_getphdrnum(elf, &phdrnum))
  847. return -1;
  848. for (i = 0; i < phdrnum; i++) {
  849. if (gelf_getphdr(elf, i, &phdr) == NULL)
  850. return -1;
  851. if (phdr.p_type != PT_LOAD)
  852. continue;
  853. if (exe) {
  854. if (!(phdr.p_flags & PF_X))
  855. continue;
  856. } else {
  857. if (!(phdr.p_flags & PF_R))
  858. continue;
  859. }
  860. sz = min(phdr.p_memsz, phdr.p_filesz);
  861. if (!sz)
  862. continue;
  863. err = mapfn(phdr.p_vaddr, sz, phdr.p_offset, data);
  864. if (err)
  865. return err;
  866. }
  867. return 0;
  868. }
  869. int file__read_maps(int fd, bool exe, mapfn_t mapfn, void *data,
  870. bool *is_64_bit)
  871. {
  872. int err;
  873. Elf *elf;
  874. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  875. if (elf == NULL)
  876. return -1;
  877. if (is_64_bit)
  878. *is_64_bit = (gelf_getclass(elf) == ELFCLASS64);
  879. err = elf_read_maps(elf, exe, mapfn, data);
  880. elf_end(elf);
  881. return err;
  882. }
  883. void symbol__elf_init(void)
  884. {
  885. elf_version(EV_CURRENT);
  886. }