symbol-elf.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787
  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 "demangle-java.h"
  9. #include "machine.h"
  10. #include "vdso.h"
  11. #include <symbol/kallsyms.h>
  12. #include "debug.h"
  13. #ifndef EM_AARCH64
  14. #define EM_AARCH64 183 /* ARM 64 bit */
  15. #endif
  16. #ifdef HAVE_CPLUS_DEMANGLE_SUPPORT
  17. extern char *cplus_demangle(const char *, int);
  18. static inline char *bfd_demangle(void __maybe_unused *v, const char *c, int i)
  19. {
  20. return cplus_demangle(c, i);
  21. }
  22. #else
  23. #ifdef NO_DEMANGLE
  24. static inline char *bfd_demangle(void __maybe_unused *v,
  25. const char __maybe_unused *c,
  26. int __maybe_unused i)
  27. {
  28. return NULL;
  29. }
  30. #else
  31. #define PACKAGE 'perf'
  32. #include <bfd.h>
  33. #endif
  34. #endif
  35. #ifndef HAVE_ELF_GETPHDRNUM_SUPPORT
  36. static int elf_getphdrnum(Elf *elf, size_t *dst)
  37. {
  38. GElf_Ehdr gehdr;
  39. GElf_Ehdr *ehdr;
  40. ehdr = gelf_getehdr(elf, &gehdr);
  41. if (!ehdr)
  42. return -1;
  43. *dst = ehdr->e_phnum;
  44. return 0;
  45. }
  46. #endif
  47. #ifndef NT_GNU_BUILD_ID
  48. #define NT_GNU_BUILD_ID 3
  49. #endif
  50. /**
  51. * elf_symtab__for_each_symbol - iterate thru all the symbols
  52. *
  53. * @syms: struct elf_symtab instance to iterate
  54. * @idx: uint32_t idx
  55. * @sym: GElf_Sym iterator
  56. */
  57. #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
  58. for (idx = 0, gelf_getsym(syms, idx, &sym);\
  59. idx < nr_syms; \
  60. idx++, gelf_getsym(syms, idx, &sym))
  61. static inline uint8_t elf_sym__type(const GElf_Sym *sym)
  62. {
  63. return GELF_ST_TYPE(sym->st_info);
  64. }
  65. #ifndef STT_GNU_IFUNC
  66. #define STT_GNU_IFUNC 10
  67. #endif
  68. static inline int elf_sym__is_function(const GElf_Sym *sym)
  69. {
  70. return (elf_sym__type(sym) == STT_FUNC ||
  71. elf_sym__type(sym) == STT_GNU_IFUNC) &&
  72. sym->st_name != 0 &&
  73. sym->st_shndx != SHN_UNDEF;
  74. }
  75. static inline bool elf_sym__is_object(const GElf_Sym *sym)
  76. {
  77. return elf_sym__type(sym) == STT_OBJECT &&
  78. sym->st_name != 0 &&
  79. sym->st_shndx != SHN_UNDEF;
  80. }
  81. static inline int elf_sym__is_label(const GElf_Sym *sym)
  82. {
  83. return elf_sym__type(sym) == STT_NOTYPE &&
  84. sym->st_name != 0 &&
  85. sym->st_shndx != SHN_UNDEF &&
  86. sym->st_shndx != SHN_ABS;
  87. }
  88. static bool elf_sym__is_a(GElf_Sym *sym, enum map_type type)
  89. {
  90. switch (type) {
  91. case MAP__FUNCTION:
  92. return elf_sym__is_function(sym);
  93. case MAP__VARIABLE:
  94. return elf_sym__is_object(sym);
  95. default:
  96. return false;
  97. }
  98. }
  99. static inline const char *elf_sym__name(const GElf_Sym *sym,
  100. const Elf_Data *symstrs)
  101. {
  102. return symstrs->d_buf + sym->st_name;
  103. }
  104. static inline const char *elf_sec__name(const GElf_Shdr *shdr,
  105. const Elf_Data *secstrs)
  106. {
  107. return secstrs->d_buf + shdr->sh_name;
  108. }
  109. static inline int elf_sec__is_text(const GElf_Shdr *shdr,
  110. const Elf_Data *secstrs)
  111. {
  112. return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
  113. }
  114. static inline bool elf_sec__is_data(const GElf_Shdr *shdr,
  115. const Elf_Data *secstrs)
  116. {
  117. return strstr(elf_sec__name(shdr, secstrs), "data") != NULL;
  118. }
  119. static bool elf_sec__is_a(GElf_Shdr *shdr, Elf_Data *secstrs,
  120. enum map_type type)
  121. {
  122. switch (type) {
  123. case MAP__FUNCTION:
  124. return elf_sec__is_text(shdr, secstrs);
  125. case MAP__VARIABLE:
  126. return elf_sec__is_data(shdr, secstrs);
  127. default:
  128. return false;
  129. }
  130. }
  131. static size_t elf_addr_to_index(Elf *elf, GElf_Addr addr)
  132. {
  133. Elf_Scn *sec = NULL;
  134. GElf_Shdr shdr;
  135. size_t cnt = 1;
  136. while ((sec = elf_nextscn(elf, sec)) != NULL) {
  137. gelf_getshdr(sec, &shdr);
  138. if ((addr >= shdr.sh_addr) &&
  139. (addr < (shdr.sh_addr + shdr.sh_size)))
  140. return cnt;
  141. ++cnt;
  142. }
  143. return -1;
  144. }
  145. Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
  146. GElf_Shdr *shp, const char *name, size_t *idx)
  147. {
  148. Elf_Scn *sec = NULL;
  149. size_t cnt = 1;
  150. /* Elf is corrupted/truncated, avoid calling elf_strptr. */
  151. if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL))
  152. return NULL;
  153. while ((sec = elf_nextscn(elf, sec)) != NULL) {
  154. char *str;
  155. gelf_getshdr(sec, shp);
  156. str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
  157. if (str && !strcmp(name, str)) {
  158. if (idx)
  159. *idx = cnt;
  160. return sec;
  161. }
  162. ++cnt;
  163. }
  164. return NULL;
  165. }
  166. #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
  167. for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
  168. idx < nr_entries; \
  169. ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
  170. #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
  171. for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
  172. idx < nr_entries; \
  173. ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
  174. /*
  175. * We need to check if we have a .dynsym, so that we can handle the
  176. * .plt, synthesizing its symbols, that aren't on the symtabs (be it
  177. * .dynsym or .symtab).
  178. * And always look at the original dso, not at debuginfo packages, that
  179. * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
  180. */
  181. int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss, struct map *map,
  182. symbol_filter_t filter)
  183. {
  184. uint32_t nr_rel_entries, idx;
  185. GElf_Sym sym;
  186. u64 plt_offset;
  187. GElf_Shdr shdr_plt;
  188. struct symbol *f;
  189. GElf_Shdr shdr_rel_plt, shdr_dynsym;
  190. Elf_Data *reldata, *syms, *symstrs;
  191. Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
  192. size_t dynsym_idx;
  193. GElf_Ehdr ehdr;
  194. char sympltname[1024];
  195. Elf *elf;
  196. int nr = 0, symidx, err = 0;
  197. if (!ss->dynsym)
  198. return 0;
  199. elf = ss->elf;
  200. ehdr = ss->ehdr;
  201. scn_dynsym = ss->dynsym;
  202. shdr_dynsym = ss->dynshdr;
  203. dynsym_idx = ss->dynsym_idx;
  204. if (scn_dynsym == NULL)
  205. goto out_elf_end;
  206. scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
  207. ".rela.plt", NULL);
  208. if (scn_plt_rel == NULL) {
  209. scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
  210. ".rel.plt", NULL);
  211. if (scn_plt_rel == NULL)
  212. goto out_elf_end;
  213. }
  214. err = -1;
  215. if (shdr_rel_plt.sh_link != dynsym_idx)
  216. goto out_elf_end;
  217. if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
  218. goto out_elf_end;
  219. /*
  220. * Fetch the relocation section to find the idxes to the GOT
  221. * and the symbols in the .dynsym they refer to.
  222. */
  223. reldata = elf_getdata(scn_plt_rel, NULL);
  224. if (reldata == NULL)
  225. goto out_elf_end;
  226. syms = elf_getdata(scn_dynsym, NULL);
  227. if (syms == NULL)
  228. goto out_elf_end;
  229. scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
  230. if (scn_symstrs == NULL)
  231. goto out_elf_end;
  232. symstrs = elf_getdata(scn_symstrs, NULL);
  233. if (symstrs == NULL)
  234. goto out_elf_end;
  235. if (symstrs->d_size == 0)
  236. goto out_elf_end;
  237. nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
  238. plt_offset = shdr_plt.sh_offset;
  239. if (shdr_rel_plt.sh_type == SHT_RELA) {
  240. GElf_Rela pos_mem, *pos;
  241. elf_section__for_each_rela(reldata, pos, pos_mem, idx,
  242. nr_rel_entries) {
  243. symidx = GELF_R_SYM(pos->r_info);
  244. plt_offset += shdr_plt.sh_entsize;
  245. gelf_getsym(syms, symidx, &sym);
  246. snprintf(sympltname, sizeof(sympltname),
  247. "%s@plt", elf_sym__name(&sym, symstrs));
  248. f = symbol__new(plt_offset, shdr_plt.sh_entsize,
  249. STB_GLOBAL, sympltname);
  250. if (!f)
  251. goto out_elf_end;
  252. if (filter && filter(map, f))
  253. symbol__delete(f);
  254. else {
  255. symbols__insert(&dso->symbols[map->type], f);
  256. ++nr;
  257. }
  258. }
  259. } else if (shdr_rel_plt.sh_type == SHT_REL) {
  260. GElf_Rel pos_mem, *pos;
  261. elf_section__for_each_rel(reldata, pos, pos_mem, idx,
  262. nr_rel_entries) {
  263. symidx = GELF_R_SYM(pos->r_info);
  264. plt_offset += shdr_plt.sh_entsize;
  265. gelf_getsym(syms, symidx, &sym);
  266. snprintf(sympltname, sizeof(sympltname),
  267. "%s@plt", elf_sym__name(&sym, symstrs));
  268. f = symbol__new(plt_offset, shdr_plt.sh_entsize,
  269. STB_GLOBAL, sympltname);
  270. if (!f)
  271. goto out_elf_end;
  272. if (filter && filter(map, f))
  273. symbol__delete(f);
  274. else {
  275. symbols__insert(&dso->symbols[map->type], f);
  276. ++nr;
  277. }
  278. }
  279. }
  280. err = 0;
  281. out_elf_end:
  282. if (err == 0)
  283. return nr;
  284. pr_debug("%s: problems reading %s PLT info.\n",
  285. __func__, dso->long_name);
  286. return 0;
  287. }
  288. /*
  289. * Align offset to 4 bytes as needed for note name and descriptor data.
  290. */
  291. #define NOTE_ALIGN(n) (((n) + 3) & -4U)
  292. static int elf_read_build_id(Elf *elf, void *bf, size_t size)
  293. {
  294. int err = -1;
  295. GElf_Ehdr ehdr;
  296. GElf_Shdr shdr;
  297. Elf_Data *data;
  298. Elf_Scn *sec;
  299. Elf_Kind ek;
  300. void *ptr;
  301. if (size < BUILD_ID_SIZE)
  302. goto out;
  303. ek = elf_kind(elf);
  304. if (ek != ELF_K_ELF)
  305. goto out;
  306. if (gelf_getehdr(elf, &ehdr) == NULL) {
  307. pr_err("%s: cannot get elf header.\n", __func__);
  308. goto out;
  309. }
  310. /*
  311. * Check following sections for notes:
  312. * '.note.gnu.build-id'
  313. * '.notes'
  314. * '.note' (VDSO specific)
  315. */
  316. do {
  317. sec = elf_section_by_name(elf, &ehdr, &shdr,
  318. ".note.gnu.build-id", NULL);
  319. if (sec)
  320. break;
  321. sec = elf_section_by_name(elf, &ehdr, &shdr,
  322. ".notes", NULL);
  323. if (sec)
  324. break;
  325. sec = elf_section_by_name(elf, &ehdr, &shdr,
  326. ".note", NULL);
  327. if (sec)
  328. break;
  329. return err;
  330. } while (0);
  331. data = elf_getdata(sec, NULL);
  332. if (data == NULL)
  333. goto out;
  334. ptr = data->d_buf;
  335. while (ptr < (data->d_buf + data->d_size)) {
  336. GElf_Nhdr *nhdr = ptr;
  337. size_t namesz = NOTE_ALIGN(nhdr->n_namesz),
  338. descsz = NOTE_ALIGN(nhdr->n_descsz);
  339. const char *name;
  340. ptr += sizeof(*nhdr);
  341. name = ptr;
  342. ptr += namesz;
  343. if (nhdr->n_type == NT_GNU_BUILD_ID &&
  344. nhdr->n_namesz == sizeof("GNU")) {
  345. if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
  346. size_t sz = min(size, descsz);
  347. memcpy(bf, ptr, sz);
  348. memset(bf + sz, 0, size - sz);
  349. err = descsz;
  350. break;
  351. }
  352. }
  353. ptr += descsz;
  354. }
  355. out:
  356. return err;
  357. }
  358. int filename__read_build_id(const char *filename, void *bf, size_t size)
  359. {
  360. int fd, err = -1;
  361. Elf *elf;
  362. if (size < BUILD_ID_SIZE)
  363. goto out;
  364. fd = open(filename, O_RDONLY);
  365. if (fd < 0)
  366. goto out;
  367. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  368. if (elf == NULL) {
  369. pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
  370. goto out_close;
  371. }
  372. err = elf_read_build_id(elf, bf, size);
  373. elf_end(elf);
  374. out_close:
  375. close(fd);
  376. out:
  377. return err;
  378. }
  379. int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
  380. {
  381. int fd, err = -1;
  382. if (size < BUILD_ID_SIZE)
  383. goto out;
  384. fd = open(filename, O_RDONLY);
  385. if (fd < 0)
  386. goto out;
  387. while (1) {
  388. char bf[BUFSIZ];
  389. GElf_Nhdr nhdr;
  390. size_t namesz, descsz;
  391. if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
  392. break;
  393. namesz = NOTE_ALIGN(nhdr.n_namesz);
  394. descsz = NOTE_ALIGN(nhdr.n_descsz);
  395. if (nhdr.n_type == NT_GNU_BUILD_ID &&
  396. nhdr.n_namesz == sizeof("GNU")) {
  397. if (read(fd, bf, namesz) != (ssize_t)namesz)
  398. break;
  399. if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
  400. size_t sz = min(descsz, size);
  401. if (read(fd, build_id, sz) == (ssize_t)sz) {
  402. memset(build_id + sz, 0, size - sz);
  403. err = 0;
  404. break;
  405. }
  406. } else if (read(fd, bf, descsz) != (ssize_t)descsz)
  407. break;
  408. } else {
  409. int n = namesz + descsz;
  410. if (read(fd, bf, n) != n)
  411. break;
  412. }
  413. }
  414. close(fd);
  415. out:
  416. return err;
  417. }
  418. int filename__read_debuglink(const char *filename, char *debuglink,
  419. size_t size)
  420. {
  421. int fd, err = -1;
  422. Elf *elf;
  423. GElf_Ehdr ehdr;
  424. GElf_Shdr shdr;
  425. Elf_Data *data;
  426. Elf_Scn *sec;
  427. Elf_Kind ek;
  428. fd = open(filename, O_RDONLY);
  429. if (fd < 0)
  430. goto out;
  431. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  432. if (elf == NULL) {
  433. pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
  434. goto out_close;
  435. }
  436. ek = elf_kind(elf);
  437. if (ek != ELF_K_ELF)
  438. goto out_elf_end;
  439. if (gelf_getehdr(elf, &ehdr) == NULL) {
  440. pr_err("%s: cannot get elf header.\n", __func__);
  441. goto out_elf_end;
  442. }
  443. sec = elf_section_by_name(elf, &ehdr, &shdr,
  444. ".gnu_debuglink", NULL);
  445. if (sec == NULL)
  446. goto out_elf_end;
  447. data = elf_getdata(sec, NULL);
  448. if (data == NULL)
  449. goto out_elf_end;
  450. /* the start of this section is a zero-terminated string */
  451. strncpy(debuglink, data->d_buf, size);
  452. err = 0;
  453. out_elf_end:
  454. elf_end(elf);
  455. out_close:
  456. close(fd);
  457. out:
  458. return err;
  459. }
  460. static int dso__swap_init(struct dso *dso, unsigned char eidata)
  461. {
  462. static unsigned int const endian = 1;
  463. dso->needs_swap = DSO_SWAP__NO;
  464. switch (eidata) {
  465. case ELFDATA2LSB:
  466. /* We are big endian, DSO is little endian. */
  467. if (*(unsigned char const *)&endian != 1)
  468. dso->needs_swap = DSO_SWAP__YES;
  469. break;
  470. case ELFDATA2MSB:
  471. /* We are little endian, DSO is big endian. */
  472. if (*(unsigned char const *)&endian != 0)
  473. dso->needs_swap = DSO_SWAP__YES;
  474. break;
  475. default:
  476. pr_err("unrecognized DSO data encoding %d\n", eidata);
  477. return -EINVAL;
  478. }
  479. return 0;
  480. }
  481. static int decompress_kmodule(struct dso *dso, const char *name,
  482. enum dso_binary_type type)
  483. {
  484. int fd = -1;
  485. char tmpbuf[] = "/tmp/perf-kmod-XXXXXX";
  486. struct kmod_path m;
  487. if (type != DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP &&
  488. type != DSO_BINARY_TYPE__GUEST_KMODULE_COMP &&
  489. type != DSO_BINARY_TYPE__BUILD_ID_CACHE)
  490. return -1;
  491. if (type == DSO_BINARY_TYPE__BUILD_ID_CACHE)
  492. name = dso->long_name;
  493. if (kmod_path__parse_ext(&m, name) || !m.comp)
  494. return -1;
  495. fd = mkstemp(tmpbuf);
  496. if (fd < 0) {
  497. dso->load_errno = errno;
  498. goto out;
  499. }
  500. if (!decompress_to_file(m.ext, name, fd)) {
  501. dso->load_errno = DSO_LOAD_ERRNO__DECOMPRESSION_FAILURE;
  502. close(fd);
  503. fd = -1;
  504. }
  505. unlink(tmpbuf);
  506. out:
  507. free(m.ext);
  508. return fd;
  509. }
  510. bool symsrc__possibly_runtime(struct symsrc *ss)
  511. {
  512. return ss->dynsym || ss->opdsec;
  513. }
  514. bool symsrc__has_symtab(struct symsrc *ss)
  515. {
  516. return ss->symtab != NULL;
  517. }
  518. void symsrc__destroy(struct symsrc *ss)
  519. {
  520. zfree(&ss->name);
  521. elf_end(ss->elf);
  522. close(ss->fd);
  523. }
  524. bool __weak elf__needs_adjust_symbols(GElf_Ehdr ehdr)
  525. {
  526. return ehdr.e_type == ET_EXEC || ehdr.e_type == ET_REL;
  527. }
  528. int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name,
  529. enum dso_binary_type type)
  530. {
  531. int err = -1;
  532. GElf_Ehdr ehdr;
  533. Elf *elf;
  534. int fd;
  535. if (dso__needs_decompress(dso)) {
  536. fd = decompress_kmodule(dso, name, type);
  537. if (fd < 0)
  538. return -1;
  539. } else {
  540. fd = open(name, O_RDONLY);
  541. if (fd < 0) {
  542. dso->load_errno = errno;
  543. return -1;
  544. }
  545. }
  546. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  547. if (elf == NULL) {
  548. pr_debug("%s: cannot read %s ELF file.\n", __func__, name);
  549. dso->load_errno = DSO_LOAD_ERRNO__INVALID_ELF;
  550. goto out_close;
  551. }
  552. if (gelf_getehdr(elf, &ehdr) == NULL) {
  553. dso->load_errno = DSO_LOAD_ERRNO__INVALID_ELF;
  554. pr_debug("%s: cannot get elf header.\n", __func__);
  555. goto out_elf_end;
  556. }
  557. if (dso__swap_init(dso, ehdr.e_ident[EI_DATA])) {
  558. dso->load_errno = DSO_LOAD_ERRNO__INTERNAL_ERROR;
  559. goto out_elf_end;
  560. }
  561. /* Always reject images with a mismatched build-id: */
  562. if (dso->has_build_id) {
  563. u8 build_id[BUILD_ID_SIZE];
  564. if (elf_read_build_id(elf, build_id, BUILD_ID_SIZE) < 0) {
  565. dso->load_errno = DSO_LOAD_ERRNO__CANNOT_READ_BUILDID;
  566. goto out_elf_end;
  567. }
  568. if (!dso__build_id_equal(dso, build_id)) {
  569. pr_debug("%s: build id mismatch for %s.\n", __func__, name);
  570. dso->load_errno = DSO_LOAD_ERRNO__MISMATCHING_BUILDID;
  571. goto out_elf_end;
  572. }
  573. }
  574. ss->is_64_bit = (gelf_getclass(elf) == ELFCLASS64);
  575. ss->symtab = elf_section_by_name(elf, &ehdr, &ss->symshdr, ".symtab",
  576. NULL);
  577. if (ss->symshdr.sh_type != SHT_SYMTAB)
  578. ss->symtab = NULL;
  579. ss->dynsym_idx = 0;
  580. ss->dynsym = elf_section_by_name(elf, &ehdr, &ss->dynshdr, ".dynsym",
  581. &ss->dynsym_idx);
  582. if (ss->dynshdr.sh_type != SHT_DYNSYM)
  583. ss->dynsym = NULL;
  584. ss->opdidx = 0;
  585. ss->opdsec = elf_section_by_name(elf, &ehdr, &ss->opdshdr, ".opd",
  586. &ss->opdidx);
  587. if (ss->opdshdr.sh_type != SHT_PROGBITS)
  588. ss->opdsec = NULL;
  589. if (dso->kernel == DSO_TYPE_USER)
  590. ss->adjust_symbols = true;
  591. else
  592. ss->adjust_symbols = elf__needs_adjust_symbols(ehdr);
  593. ss->name = strdup(name);
  594. if (!ss->name) {
  595. dso->load_errno = errno;
  596. goto out_elf_end;
  597. }
  598. ss->elf = elf;
  599. ss->fd = fd;
  600. ss->ehdr = ehdr;
  601. ss->type = type;
  602. return 0;
  603. out_elf_end:
  604. elf_end(elf);
  605. out_close:
  606. close(fd);
  607. return err;
  608. }
  609. /**
  610. * ref_reloc_sym_not_found - has kernel relocation symbol been found.
  611. * @kmap: kernel maps and relocation reference symbol
  612. *
  613. * This function returns %true if we are dealing with the kernel maps and the
  614. * relocation reference symbol has not yet been found. Otherwise %false is
  615. * returned.
  616. */
  617. static bool ref_reloc_sym_not_found(struct kmap *kmap)
  618. {
  619. return kmap && kmap->ref_reloc_sym && kmap->ref_reloc_sym->name &&
  620. !kmap->ref_reloc_sym->unrelocated_addr;
  621. }
  622. /**
  623. * ref_reloc - kernel relocation offset.
  624. * @kmap: kernel maps and relocation reference symbol
  625. *
  626. * This function returns the offset of kernel addresses as determined by using
  627. * the relocation reference symbol i.e. if the kernel has not been relocated
  628. * then the return value is zero.
  629. */
  630. static u64 ref_reloc(struct kmap *kmap)
  631. {
  632. if (kmap && kmap->ref_reloc_sym &&
  633. kmap->ref_reloc_sym->unrelocated_addr)
  634. return kmap->ref_reloc_sym->addr -
  635. kmap->ref_reloc_sym->unrelocated_addr;
  636. return 0;
  637. }
  638. static bool want_demangle(bool is_kernel_sym)
  639. {
  640. return is_kernel_sym ? symbol_conf.demangle_kernel : symbol_conf.demangle;
  641. }
  642. void __weak arch__sym_update(struct symbol *s __maybe_unused,
  643. GElf_Sym *sym __maybe_unused) { }
  644. int dso__load_sym(struct dso *dso, struct map *map,
  645. struct symsrc *syms_ss, struct symsrc *runtime_ss,
  646. symbol_filter_t filter, int kmodule)
  647. {
  648. struct kmap *kmap = dso->kernel ? map__kmap(map) : NULL;
  649. struct map_groups *kmaps = kmap ? map__kmaps(map) : NULL;
  650. struct map *curr_map = map;
  651. struct dso *curr_dso = dso;
  652. Elf_Data *symstrs, *secstrs;
  653. uint32_t nr_syms;
  654. int err = -1;
  655. uint32_t idx;
  656. GElf_Ehdr ehdr;
  657. GElf_Shdr shdr;
  658. GElf_Shdr tshdr;
  659. Elf_Data *syms, *opddata = NULL;
  660. GElf_Sym sym;
  661. Elf_Scn *sec, *sec_strndx;
  662. Elf *elf;
  663. int nr = 0;
  664. bool remap_kernel = false, adjust_kernel_syms = false;
  665. if (kmap && !kmaps)
  666. return -1;
  667. dso->symtab_type = syms_ss->type;
  668. dso->is_64_bit = syms_ss->is_64_bit;
  669. dso->rel = syms_ss->ehdr.e_type == ET_REL;
  670. /*
  671. * Modules may already have symbols from kallsyms, but those symbols
  672. * have the wrong values for the dso maps, so remove them.
  673. */
  674. if (kmodule && syms_ss->symtab)
  675. symbols__delete(&dso->symbols[map->type]);
  676. if (!syms_ss->symtab) {
  677. /*
  678. * If the vmlinux is stripped, fail so we will fall back
  679. * to using kallsyms. The vmlinux runtime symbols aren't
  680. * of much use.
  681. */
  682. if (dso->kernel)
  683. goto out_elf_end;
  684. syms_ss->symtab = syms_ss->dynsym;
  685. syms_ss->symshdr = syms_ss->dynshdr;
  686. }
  687. elf = syms_ss->elf;
  688. ehdr = syms_ss->ehdr;
  689. sec = syms_ss->symtab;
  690. shdr = syms_ss->symshdr;
  691. if (elf_section_by_name(elf, &ehdr, &tshdr, ".text", NULL))
  692. dso->text_offset = tshdr.sh_addr - tshdr.sh_offset;
  693. if (runtime_ss->opdsec)
  694. opddata = elf_rawdata(runtime_ss->opdsec, NULL);
  695. syms = elf_getdata(sec, NULL);
  696. if (syms == NULL)
  697. goto out_elf_end;
  698. sec = elf_getscn(elf, shdr.sh_link);
  699. if (sec == NULL)
  700. goto out_elf_end;
  701. symstrs = elf_getdata(sec, NULL);
  702. if (symstrs == NULL)
  703. goto out_elf_end;
  704. sec_strndx = elf_getscn(runtime_ss->elf, runtime_ss->ehdr.e_shstrndx);
  705. if (sec_strndx == NULL)
  706. goto out_elf_end;
  707. secstrs = elf_getdata(sec_strndx, NULL);
  708. if (secstrs == NULL)
  709. goto out_elf_end;
  710. nr_syms = shdr.sh_size / shdr.sh_entsize;
  711. memset(&sym, 0, sizeof(sym));
  712. /*
  713. * The kernel relocation symbol is needed in advance in order to adjust
  714. * kernel maps correctly.
  715. */
  716. if (ref_reloc_sym_not_found(kmap)) {
  717. elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
  718. const char *elf_name = elf_sym__name(&sym, symstrs);
  719. if (strcmp(elf_name, kmap->ref_reloc_sym->name))
  720. continue;
  721. kmap->ref_reloc_sym->unrelocated_addr = sym.st_value;
  722. map->reloc = kmap->ref_reloc_sym->addr -
  723. kmap->ref_reloc_sym->unrelocated_addr;
  724. break;
  725. }
  726. }
  727. /*
  728. * Handle any relocation of vdso necessary because older kernels
  729. * attempted to prelink vdso to its virtual address.
  730. */
  731. if (dso__is_vdso(dso))
  732. map->reloc = map->start - dso->text_offset;
  733. dso->adjust_symbols = runtime_ss->adjust_symbols || ref_reloc(kmap);
  734. /*
  735. * Initial kernel and module mappings do not map to the dso. For
  736. * function mappings, flag the fixups.
  737. */
  738. if (map->type == MAP__FUNCTION && (dso->kernel || kmodule)) {
  739. remap_kernel = true;
  740. adjust_kernel_syms = dso->adjust_symbols;
  741. }
  742. elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
  743. struct symbol *f;
  744. const char *elf_name = elf_sym__name(&sym, symstrs);
  745. char *demangled = NULL;
  746. int is_label = elf_sym__is_label(&sym);
  747. const char *section_name;
  748. bool used_opd = false;
  749. if (!is_label && !elf_sym__is_a(&sym, map->type))
  750. continue;
  751. /* Reject ARM ELF "mapping symbols": these aren't unique and
  752. * don't identify functions, so will confuse the profile
  753. * output: */
  754. if (ehdr.e_machine == EM_ARM || ehdr.e_machine == EM_AARCH64) {
  755. if (elf_name[0] == '$' && strchr("adtx", elf_name[1])
  756. && (elf_name[2] == '\0' || elf_name[2] == '.'))
  757. continue;
  758. }
  759. if (runtime_ss->opdsec && sym.st_shndx == runtime_ss->opdidx) {
  760. u32 offset = sym.st_value - syms_ss->opdshdr.sh_addr;
  761. u64 *opd = opddata->d_buf + offset;
  762. sym.st_value = DSO__SWAP(dso, u64, *opd);
  763. sym.st_shndx = elf_addr_to_index(runtime_ss->elf,
  764. sym.st_value);
  765. used_opd = true;
  766. }
  767. /*
  768. * When loading symbols in a data mapping, ABS symbols (which
  769. * has a value of SHN_ABS in its st_shndx) failed at
  770. * elf_getscn(). And it marks the loading as a failure so
  771. * already loaded symbols cannot be fixed up.
  772. *
  773. * I'm not sure what should be done. Just ignore them for now.
  774. * - Namhyung Kim
  775. */
  776. if (sym.st_shndx == SHN_ABS)
  777. continue;
  778. sec = elf_getscn(runtime_ss->elf, sym.st_shndx);
  779. if (!sec)
  780. goto out_elf_end;
  781. gelf_getshdr(sec, &shdr);
  782. if (is_label && !elf_sec__is_a(&shdr, secstrs, map->type))
  783. continue;
  784. section_name = elf_sec__name(&shdr, secstrs);
  785. /* On ARM, symbols for thumb functions have 1 added to
  786. * the symbol address as a flag - remove it */
  787. if ((ehdr.e_machine == EM_ARM) &&
  788. (map->type == MAP__FUNCTION) &&
  789. (sym.st_value & 1))
  790. --sym.st_value;
  791. if (dso->kernel || kmodule) {
  792. char dso_name[PATH_MAX];
  793. /* Adjust symbol to map to file offset */
  794. if (adjust_kernel_syms)
  795. sym.st_value -= shdr.sh_addr - shdr.sh_offset;
  796. if (strcmp(section_name,
  797. (curr_dso->short_name +
  798. dso->short_name_len)) == 0)
  799. goto new_symbol;
  800. if (strcmp(section_name, ".text") == 0) {
  801. /*
  802. * The initial kernel mapping is based on
  803. * kallsyms and identity maps. Overwrite it to
  804. * map to the kernel dso.
  805. */
  806. if (remap_kernel && dso->kernel) {
  807. remap_kernel = false;
  808. map->start = shdr.sh_addr +
  809. ref_reloc(kmap);
  810. map->end = map->start + shdr.sh_size;
  811. map->pgoff = shdr.sh_offset;
  812. map->map_ip = map__map_ip;
  813. map->unmap_ip = map__unmap_ip;
  814. /* Ensure maps are correctly ordered */
  815. if (kmaps) {
  816. map__get(map);
  817. map_groups__remove(kmaps, map);
  818. map_groups__insert(kmaps, map);
  819. map__put(map);
  820. }
  821. }
  822. /*
  823. * The initial module mapping is based on
  824. * /proc/modules mapped to offset zero.
  825. * Overwrite it to map to the module dso.
  826. */
  827. if (remap_kernel && kmodule) {
  828. remap_kernel = false;
  829. map->pgoff = shdr.sh_offset;
  830. }
  831. curr_map = map;
  832. curr_dso = dso;
  833. goto new_symbol;
  834. }
  835. if (!kmap)
  836. goto new_symbol;
  837. snprintf(dso_name, sizeof(dso_name),
  838. "%s%s", dso->short_name, section_name);
  839. curr_map = map_groups__find_by_name(kmaps, map->type, dso_name);
  840. if (curr_map == NULL) {
  841. u64 start = sym.st_value;
  842. if (kmodule)
  843. start += map->start + shdr.sh_offset;
  844. curr_dso = dso__new(dso_name);
  845. if (curr_dso == NULL)
  846. goto out_elf_end;
  847. curr_dso->kernel = dso->kernel;
  848. curr_dso->long_name = dso->long_name;
  849. curr_dso->long_name_len = dso->long_name_len;
  850. curr_map = map__new2(start, curr_dso,
  851. map->type);
  852. dso__put(curr_dso);
  853. if (curr_map == NULL) {
  854. goto out_elf_end;
  855. }
  856. if (adjust_kernel_syms) {
  857. curr_map->start = shdr.sh_addr +
  858. ref_reloc(kmap);
  859. curr_map->end = curr_map->start +
  860. shdr.sh_size;
  861. curr_map->pgoff = shdr.sh_offset;
  862. } else {
  863. curr_map->map_ip = identity__map_ip;
  864. curr_map->unmap_ip = identity__map_ip;
  865. }
  866. curr_dso->symtab_type = dso->symtab_type;
  867. map_groups__insert(kmaps, curr_map);
  868. /*
  869. * Add it before we drop the referece to curr_map,
  870. * i.e. while we still are sure to have a reference
  871. * to this DSO via curr_map->dso.
  872. */
  873. dsos__add(&map->groups->machine->dsos, curr_dso);
  874. /* kmaps already got it */
  875. map__put(curr_map);
  876. dso__set_loaded(curr_dso, map->type);
  877. } else
  878. curr_dso = curr_map->dso;
  879. goto new_symbol;
  880. }
  881. if ((used_opd && runtime_ss->adjust_symbols)
  882. || (!used_opd && syms_ss->adjust_symbols)) {
  883. pr_debug4("%s: adjusting symbol: st_value: %#" PRIx64 " "
  884. "sh_addr: %#" PRIx64 " sh_offset: %#" PRIx64 "\n", __func__,
  885. (u64)sym.st_value, (u64)shdr.sh_addr,
  886. (u64)shdr.sh_offset);
  887. sym.st_value -= shdr.sh_addr - shdr.sh_offset;
  888. }
  889. new_symbol:
  890. /*
  891. * We need to figure out if the object was created from C++ sources
  892. * DWARF DW_compile_unit has this, but we don't always have access
  893. * to it...
  894. */
  895. if (want_demangle(dso->kernel || kmodule)) {
  896. int demangle_flags = DMGL_NO_OPTS;
  897. if (verbose)
  898. demangle_flags = DMGL_PARAMS | DMGL_ANSI;
  899. demangled = bfd_demangle(NULL, elf_name, demangle_flags);
  900. if (demangled == NULL)
  901. demangled = java_demangle_sym(elf_name, JAVA_DEMANGLE_NORET);
  902. if (demangled != NULL)
  903. elf_name = demangled;
  904. }
  905. f = symbol__new(sym.st_value, sym.st_size,
  906. GELF_ST_BIND(sym.st_info), elf_name);
  907. free(demangled);
  908. if (!f)
  909. goto out_elf_end;
  910. arch__sym_update(f, &sym);
  911. if (filter && filter(curr_map, f))
  912. symbol__delete(f);
  913. else {
  914. symbols__insert(&curr_dso->symbols[curr_map->type], f);
  915. nr++;
  916. }
  917. }
  918. /*
  919. * For misannotated, zeroed, ASM function sizes.
  920. */
  921. if (nr > 0) {
  922. if (!symbol_conf.allow_aliases)
  923. symbols__fixup_duplicate(&dso->symbols[map->type]);
  924. symbols__fixup_end(&dso->symbols[map->type]);
  925. if (kmap) {
  926. /*
  927. * We need to fixup this here too because we create new
  928. * maps here, for things like vsyscall sections.
  929. */
  930. __map_groups__fixup_end(kmaps, map->type);
  931. }
  932. }
  933. err = nr;
  934. out_elf_end:
  935. return err;
  936. }
  937. static int elf_read_maps(Elf *elf, bool exe, mapfn_t mapfn, void *data)
  938. {
  939. GElf_Phdr phdr;
  940. size_t i, phdrnum;
  941. int err;
  942. u64 sz;
  943. if (elf_getphdrnum(elf, &phdrnum))
  944. return -1;
  945. for (i = 0; i < phdrnum; i++) {
  946. if (gelf_getphdr(elf, i, &phdr) == NULL)
  947. return -1;
  948. if (phdr.p_type != PT_LOAD)
  949. continue;
  950. if (exe) {
  951. if (!(phdr.p_flags & PF_X))
  952. continue;
  953. } else {
  954. if (!(phdr.p_flags & PF_R))
  955. continue;
  956. }
  957. sz = min(phdr.p_memsz, phdr.p_filesz);
  958. if (!sz)
  959. continue;
  960. err = mapfn(phdr.p_vaddr, sz, phdr.p_offset, data);
  961. if (err)
  962. return err;
  963. }
  964. return 0;
  965. }
  966. int file__read_maps(int fd, bool exe, mapfn_t mapfn, void *data,
  967. bool *is_64_bit)
  968. {
  969. int err;
  970. Elf *elf;
  971. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  972. if (elf == NULL)
  973. return -1;
  974. if (is_64_bit)
  975. *is_64_bit = (gelf_getclass(elf) == ELFCLASS64);
  976. err = elf_read_maps(elf, exe, mapfn, data);
  977. elf_end(elf);
  978. return err;
  979. }
  980. enum dso_type dso__type_fd(int fd)
  981. {
  982. enum dso_type dso_type = DSO__TYPE_UNKNOWN;
  983. GElf_Ehdr ehdr;
  984. Elf_Kind ek;
  985. Elf *elf;
  986. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  987. if (elf == NULL)
  988. goto out;
  989. ek = elf_kind(elf);
  990. if (ek != ELF_K_ELF)
  991. goto out_end;
  992. if (gelf_getclass(elf) == ELFCLASS64) {
  993. dso_type = DSO__TYPE_64BIT;
  994. goto out_end;
  995. }
  996. if (gelf_getehdr(elf, &ehdr) == NULL)
  997. goto out_end;
  998. if (ehdr.e_machine == EM_X86_64)
  999. dso_type = DSO__TYPE_X32BIT;
  1000. else
  1001. dso_type = DSO__TYPE_32BIT;
  1002. out_end:
  1003. elf_end(elf);
  1004. out:
  1005. return dso_type;
  1006. }
  1007. static int copy_bytes(int from, off_t from_offs, int to, off_t to_offs, u64 len)
  1008. {
  1009. ssize_t r;
  1010. size_t n;
  1011. int err = -1;
  1012. char *buf = malloc(page_size);
  1013. if (buf == NULL)
  1014. return -1;
  1015. if (lseek(to, to_offs, SEEK_SET) != to_offs)
  1016. goto out;
  1017. if (lseek(from, from_offs, SEEK_SET) != from_offs)
  1018. goto out;
  1019. while (len) {
  1020. n = page_size;
  1021. if (len < n)
  1022. n = len;
  1023. /* Use read because mmap won't work on proc files */
  1024. r = read(from, buf, n);
  1025. if (r < 0)
  1026. goto out;
  1027. if (!r)
  1028. break;
  1029. n = r;
  1030. r = write(to, buf, n);
  1031. if (r < 0)
  1032. goto out;
  1033. if ((size_t)r != n)
  1034. goto out;
  1035. len -= n;
  1036. }
  1037. err = 0;
  1038. out:
  1039. free(buf);
  1040. return err;
  1041. }
  1042. struct kcore {
  1043. int fd;
  1044. int elfclass;
  1045. Elf *elf;
  1046. GElf_Ehdr ehdr;
  1047. };
  1048. static int kcore__open(struct kcore *kcore, const char *filename)
  1049. {
  1050. GElf_Ehdr *ehdr;
  1051. kcore->fd = open(filename, O_RDONLY);
  1052. if (kcore->fd == -1)
  1053. return -1;
  1054. kcore->elf = elf_begin(kcore->fd, ELF_C_READ, NULL);
  1055. if (!kcore->elf)
  1056. goto out_close;
  1057. kcore->elfclass = gelf_getclass(kcore->elf);
  1058. if (kcore->elfclass == ELFCLASSNONE)
  1059. goto out_end;
  1060. ehdr = gelf_getehdr(kcore->elf, &kcore->ehdr);
  1061. if (!ehdr)
  1062. goto out_end;
  1063. return 0;
  1064. out_end:
  1065. elf_end(kcore->elf);
  1066. out_close:
  1067. close(kcore->fd);
  1068. return -1;
  1069. }
  1070. static int kcore__init(struct kcore *kcore, char *filename, int elfclass,
  1071. bool temp)
  1072. {
  1073. kcore->elfclass = elfclass;
  1074. if (temp)
  1075. kcore->fd = mkstemp(filename);
  1076. else
  1077. kcore->fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0400);
  1078. if (kcore->fd == -1)
  1079. return -1;
  1080. kcore->elf = elf_begin(kcore->fd, ELF_C_WRITE, NULL);
  1081. if (!kcore->elf)
  1082. goto out_close;
  1083. if (!gelf_newehdr(kcore->elf, elfclass))
  1084. goto out_end;
  1085. memset(&kcore->ehdr, 0, sizeof(GElf_Ehdr));
  1086. return 0;
  1087. out_end:
  1088. elf_end(kcore->elf);
  1089. out_close:
  1090. close(kcore->fd);
  1091. unlink(filename);
  1092. return -1;
  1093. }
  1094. static void kcore__close(struct kcore *kcore)
  1095. {
  1096. elf_end(kcore->elf);
  1097. close(kcore->fd);
  1098. }
  1099. static int kcore__copy_hdr(struct kcore *from, struct kcore *to, size_t count)
  1100. {
  1101. GElf_Ehdr *ehdr = &to->ehdr;
  1102. GElf_Ehdr *kehdr = &from->ehdr;
  1103. memcpy(ehdr->e_ident, kehdr->e_ident, EI_NIDENT);
  1104. ehdr->e_type = kehdr->e_type;
  1105. ehdr->e_machine = kehdr->e_machine;
  1106. ehdr->e_version = kehdr->e_version;
  1107. ehdr->e_entry = 0;
  1108. ehdr->e_shoff = 0;
  1109. ehdr->e_flags = kehdr->e_flags;
  1110. ehdr->e_phnum = count;
  1111. ehdr->e_shentsize = 0;
  1112. ehdr->e_shnum = 0;
  1113. ehdr->e_shstrndx = 0;
  1114. if (from->elfclass == ELFCLASS32) {
  1115. ehdr->e_phoff = sizeof(Elf32_Ehdr);
  1116. ehdr->e_ehsize = sizeof(Elf32_Ehdr);
  1117. ehdr->e_phentsize = sizeof(Elf32_Phdr);
  1118. } else {
  1119. ehdr->e_phoff = sizeof(Elf64_Ehdr);
  1120. ehdr->e_ehsize = sizeof(Elf64_Ehdr);
  1121. ehdr->e_phentsize = sizeof(Elf64_Phdr);
  1122. }
  1123. if (!gelf_update_ehdr(to->elf, ehdr))
  1124. return -1;
  1125. if (!gelf_newphdr(to->elf, count))
  1126. return -1;
  1127. return 0;
  1128. }
  1129. static int kcore__add_phdr(struct kcore *kcore, int idx, off_t offset,
  1130. u64 addr, u64 len)
  1131. {
  1132. GElf_Phdr phdr = {
  1133. .p_type = PT_LOAD,
  1134. .p_flags = PF_R | PF_W | PF_X,
  1135. .p_offset = offset,
  1136. .p_vaddr = addr,
  1137. .p_paddr = 0,
  1138. .p_filesz = len,
  1139. .p_memsz = len,
  1140. .p_align = page_size,
  1141. };
  1142. if (!gelf_update_phdr(kcore->elf, idx, &phdr))
  1143. return -1;
  1144. return 0;
  1145. }
  1146. static off_t kcore__write(struct kcore *kcore)
  1147. {
  1148. return elf_update(kcore->elf, ELF_C_WRITE);
  1149. }
  1150. struct phdr_data {
  1151. off_t offset;
  1152. u64 addr;
  1153. u64 len;
  1154. };
  1155. struct kcore_copy_info {
  1156. u64 stext;
  1157. u64 etext;
  1158. u64 first_symbol;
  1159. u64 last_symbol;
  1160. u64 first_module;
  1161. u64 last_module_symbol;
  1162. struct phdr_data kernel_map;
  1163. struct phdr_data modules_map;
  1164. };
  1165. static int kcore_copy__process_kallsyms(void *arg, const char *name, char type,
  1166. u64 start)
  1167. {
  1168. struct kcore_copy_info *kci = arg;
  1169. if (!symbol_type__is_a(type, MAP__FUNCTION))
  1170. return 0;
  1171. if (strchr(name, '[')) {
  1172. if (start > kci->last_module_symbol)
  1173. kci->last_module_symbol = start;
  1174. return 0;
  1175. }
  1176. if (!kci->first_symbol || start < kci->first_symbol)
  1177. kci->first_symbol = start;
  1178. if (!kci->last_symbol || start > kci->last_symbol)
  1179. kci->last_symbol = start;
  1180. if (!strcmp(name, "_stext")) {
  1181. kci->stext = start;
  1182. return 0;
  1183. }
  1184. if (!strcmp(name, "_etext")) {
  1185. kci->etext = start;
  1186. return 0;
  1187. }
  1188. return 0;
  1189. }
  1190. static int kcore_copy__parse_kallsyms(struct kcore_copy_info *kci,
  1191. const char *dir)
  1192. {
  1193. char kallsyms_filename[PATH_MAX];
  1194. scnprintf(kallsyms_filename, PATH_MAX, "%s/kallsyms", dir);
  1195. if (symbol__restricted_filename(kallsyms_filename, "/proc/kallsyms"))
  1196. return -1;
  1197. if (kallsyms__parse(kallsyms_filename, kci,
  1198. kcore_copy__process_kallsyms) < 0)
  1199. return -1;
  1200. return 0;
  1201. }
  1202. static int kcore_copy__process_modules(void *arg,
  1203. const char *name __maybe_unused,
  1204. u64 start)
  1205. {
  1206. struct kcore_copy_info *kci = arg;
  1207. if (!kci->first_module || start < kci->first_module)
  1208. kci->first_module = start;
  1209. return 0;
  1210. }
  1211. static int kcore_copy__parse_modules(struct kcore_copy_info *kci,
  1212. const char *dir)
  1213. {
  1214. char modules_filename[PATH_MAX];
  1215. scnprintf(modules_filename, PATH_MAX, "%s/modules", dir);
  1216. if (symbol__restricted_filename(modules_filename, "/proc/modules"))
  1217. return -1;
  1218. if (modules__parse(modules_filename, kci,
  1219. kcore_copy__process_modules) < 0)
  1220. return -1;
  1221. return 0;
  1222. }
  1223. static void kcore_copy__map(struct phdr_data *p, u64 start, u64 end, u64 pgoff,
  1224. u64 s, u64 e)
  1225. {
  1226. if (p->addr || s < start || s >= end)
  1227. return;
  1228. p->addr = s;
  1229. p->offset = (s - start) + pgoff;
  1230. p->len = e < end ? e - s : end - s;
  1231. }
  1232. static int kcore_copy__read_map(u64 start, u64 len, u64 pgoff, void *data)
  1233. {
  1234. struct kcore_copy_info *kci = data;
  1235. u64 end = start + len;
  1236. kcore_copy__map(&kci->kernel_map, start, end, pgoff, kci->stext,
  1237. kci->etext);
  1238. kcore_copy__map(&kci->modules_map, start, end, pgoff, kci->first_module,
  1239. kci->last_module_symbol);
  1240. return 0;
  1241. }
  1242. static int kcore_copy__read_maps(struct kcore_copy_info *kci, Elf *elf)
  1243. {
  1244. if (elf_read_maps(elf, true, kcore_copy__read_map, kci) < 0)
  1245. return -1;
  1246. return 0;
  1247. }
  1248. static int kcore_copy__calc_maps(struct kcore_copy_info *kci, const char *dir,
  1249. Elf *elf)
  1250. {
  1251. if (kcore_copy__parse_kallsyms(kci, dir))
  1252. return -1;
  1253. if (kcore_copy__parse_modules(kci, dir))
  1254. return -1;
  1255. if (kci->stext)
  1256. kci->stext = round_down(kci->stext, page_size);
  1257. else
  1258. kci->stext = round_down(kci->first_symbol, page_size);
  1259. if (kci->etext) {
  1260. kci->etext = round_up(kci->etext, page_size);
  1261. } else if (kci->last_symbol) {
  1262. kci->etext = round_up(kci->last_symbol, page_size);
  1263. kci->etext += page_size;
  1264. }
  1265. kci->first_module = round_down(kci->first_module, page_size);
  1266. if (kci->last_module_symbol) {
  1267. kci->last_module_symbol = round_up(kci->last_module_symbol,
  1268. page_size);
  1269. kci->last_module_symbol += page_size;
  1270. }
  1271. if (!kci->stext || !kci->etext)
  1272. return -1;
  1273. if (kci->first_module && !kci->last_module_symbol)
  1274. return -1;
  1275. return kcore_copy__read_maps(kci, elf);
  1276. }
  1277. static int kcore_copy__copy_file(const char *from_dir, const char *to_dir,
  1278. const char *name)
  1279. {
  1280. char from_filename[PATH_MAX];
  1281. char to_filename[PATH_MAX];
  1282. scnprintf(from_filename, PATH_MAX, "%s/%s", from_dir, name);
  1283. scnprintf(to_filename, PATH_MAX, "%s/%s", to_dir, name);
  1284. return copyfile_mode(from_filename, to_filename, 0400);
  1285. }
  1286. static int kcore_copy__unlink(const char *dir, const char *name)
  1287. {
  1288. char filename[PATH_MAX];
  1289. scnprintf(filename, PATH_MAX, "%s/%s", dir, name);
  1290. return unlink(filename);
  1291. }
  1292. static int kcore_copy__compare_fds(int from, int to)
  1293. {
  1294. char *buf_from;
  1295. char *buf_to;
  1296. ssize_t ret;
  1297. size_t len;
  1298. int err = -1;
  1299. buf_from = malloc(page_size);
  1300. buf_to = malloc(page_size);
  1301. if (!buf_from || !buf_to)
  1302. goto out;
  1303. while (1) {
  1304. /* Use read because mmap won't work on proc files */
  1305. ret = read(from, buf_from, page_size);
  1306. if (ret < 0)
  1307. goto out;
  1308. if (!ret)
  1309. break;
  1310. len = ret;
  1311. if (readn(to, buf_to, len) != (int)len)
  1312. goto out;
  1313. if (memcmp(buf_from, buf_to, len))
  1314. goto out;
  1315. }
  1316. err = 0;
  1317. out:
  1318. free(buf_to);
  1319. free(buf_from);
  1320. return err;
  1321. }
  1322. static int kcore_copy__compare_files(const char *from_filename,
  1323. const char *to_filename)
  1324. {
  1325. int from, to, err = -1;
  1326. from = open(from_filename, O_RDONLY);
  1327. if (from < 0)
  1328. return -1;
  1329. to = open(to_filename, O_RDONLY);
  1330. if (to < 0)
  1331. goto out_close_from;
  1332. err = kcore_copy__compare_fds(from, to);
  1333. close(to);
  1334. out_close_from:
  1335. close(from);
  1336. return err;
  1337. }
  1338. static int kcore_copy__compare_file(const char *from_dir, const char *to_dir,
  1339. const char *name)
  1340. {
  1341. char from_filename[PATH_MAX];
  1342. char to_filename[PATH_MAX];
  1343. scnprintf(from_filename, PATH_MAX, "%s/%s", from_dir, name);
  1344. scnprintf(to_filename, PATH_MAX, "%s/%s", to_dir, name);
  1345. return kcore_copy__compare_files(from_filename, to_filename);
  1346. }
  1347. /**
  1348. * kcore_copy - copy kallsyms, modules and kcore from one directory to another.
  1349. * @from_dir: from directory
  1350. * @to_dir: to directory
  1351. *
  1352. * This function copies kallsyms, modules and kcore files from one directory to
  1353. * another. kallsyms and modules are copied entirely. Only code segments are
  1354. * copied from kcore. It is assumed that two segments suffice: one for the
  1355. * kernel proper and one for all the modules. The code segments are determined
  1356. * from kallsyms and modules files. The kernel map starts at _stext or the
  1357. * lowest function symbol, and ends at _etext or the highest function symbol.
  1358. * The module map starts at the lowest module address and ends at the highest
  1359. * module symbol. Start addresses are rounded down to the nearest page. End
  1360. * addresses are rounded up to the nearest page. An extra page is added to the
  1361. * highest kernel symbol and highest module symbol to, hopefully, encompass that
  1362. * symbol too. Because it contains only code sections, the resulting kcore is
  1363. * unusual. One significant peculiarity is that the mapping (start -> pgoff)
  1364. * is not the same for the kernel map and the modules map. That happens because
  1365. * the data is copied adjacently whereas the original kcore has gaps. Finally,
  1366. * kallsyms and modules files are compared with their copies to check that
  1367. * modules have not been loaded or unloaded while the copies were taking place.
  1368. *
  1369. * Return: %0 on success, %-1 on failure.
  1370. */
  1371. int kcore_copy(const char *from_dir, const char *to_dir)
  1372. {
  1373. struct kcore kcore;
  1374. struct kcore extract;
  1375. size_t count = 2;
  1376. int idx = 0, err = -1;
  1377. off_t offset = page_size, sz, modules_offset = 0;
  1378. struct kcore_copy_info kci = { .stext = 0, };
  1379. char kcore_filename[PATH_MAX];
  1380. char extract_filename[PATH_MAX];
  1381. if (kcore_copy__copy_file(from_dir, to_dir, "kallsyms"))
  1382. return -1;
  1383. if (kcore_copy__copy_file(from_dir, to_dir, "modules"))
  1384. goto out_unlink_kallsyms;
  1385. scnprintf(kcore_filename, PATH_MAX, "%s/kcore", from_dir);
  1386. scnprintf(extract_filename, PATH_MAX, "%s/kcore", to_dir);
  1387. if (kcore__open(&kcore, kcore_filename))
  1388. goto out_unlink_modules;
  1389. if (kcore_copy__calc_maps(&kci, from_dir, kcore.elf))
  1390. goto out_kcore_close;
  1391. if (kcore__init(&extract, extract_filename, kcore.elfclass, false))
  1392. goto out_kcore_close;
  1393. if (!kci.modules_map.addr)
  1394. count -= 1;
  1395. if (kcore__copy_hdr(&kcore, &extract, count))
  1396. goto out_extract_close;
  1397. if (kcore__add_phdr(&extract, idx++, offset, kci.kernel_map.addr,
  1398. kci.kernel_map.len))
  1399. goto out_extract_close;
  1400. if (kci.modules_map.addr) {
  1401. modules_offset = offset + kci.kernel_map.len;
  1402. if (kcore__add_phdr(&extract, idx, modules_offset,
  1403. kci.modules_map.addr, kci.modules_map.len))
  1404. goto out_extract_close;
  1405. }
  1406. sz = kcore__write(&extract);
  1407. if (sz < 0 || sz > offset)
  1408. goto out_extract_close;
  1409. if (copy_bytes(kcore.fd, kci.kernel_map.offset, extract.fd, offset,
  1410. kci.kernel_map.len))
  1411. goto out_extract_close;
  1412. if (modules_offset && copy_bytes(kcore.fd, kci.modules_map.offset,
  1413. extract.fd, modules_offset,
  1414. kci.modules_map.len))
  1415. goto out_extract_close;
  1416. if (kcore_copy__compare_file(from_dir, to_dir, "modules"))
  1417. goto out_extract_close;
  1418. if (kcore_copy__compare_file(from_dir, to_dir, "kallsyms"))
  1419. goto out_extract_close;
  1420. err = 0;
  1421. out_extract_close:
  1422. kcore__close(&extract);
  1423. if (err)
  1424. unlink(extract_filename);
  1425. out_kcore_close:
  1426. kcore__close(&kcore);
  1427. out_unlink_modules:
  1428. if (err)
  1429. kcore_copy__unlink(to_dir, "modules");
  1430. out_unlink_kallsyms:
  1431. if (err)
  1432. kcore_copy__unlink(to_dir, "kallsyms");
  1433. return err;
  1434. }
  1435. int kcore_extract__create(struct kcore_extract *kce)
  1436. {
  1437. struct kcore kcore;
  1438. struct kcore extract;
  1439. size_t count = 1;
  1440. int idx = 0, err = -1;
  1441. off_t offset = page_size, sz;
  1442. if (kcore__open(&kcore, kce->kcore_filename))
  1443. return -1;
  1444. strcpy(kce->extract_filename, PERF_KCORE_EXTRACT);
  1445. if (kcore__init(&extract, kce->extract_filename, kcore.elfclass, true))
  1446. goto out_kcore_close;
  1447. if (kcore__copy_hdr(&kcore, &extract, count))
  1448. goto out_extract_close;
  1449. if (kcore__add_phdr(&extract, idx, offset, kce->addr, kce->len))
  1450. goto out_extract_close;
  1451. sz = kcore__write(&extract);
  1452. if (sz < 0 || sz > offset)
  1453. goto out_extract_close;
  1454. if (copy_bytes(kcore.fd, kce->offs, extract.fd, offset, kce->len))
  1455. goto out_extract_close;
  1456. err = 0;
  1457. out_extract_close:
  1458. kcore__close(&extract);
  1459. if (err)
  1460. unlink(kce->extract_filename);
  1461. out_kcore_close:
  1462. kcore__close(&kcore);
  1463. return err;
  1464. }
  1465. void kcore_extract__delete(struct kcore_extract *kce)
  1466. {
  1467. unlink(kce->extract_filename);
  1468. }
  1469. void symbol__elf_init(void)
  1470. {
  1471. elf_version(EV_CURRENT);
  1472. }