symbol-elf.c 40 KB

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