symbol-elf.c 39 KB

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