symbol-elf.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751
  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. goto out;
  497. if (!decompress_to_file(m.ext, name, fd)) {
  498. close(fd);
  499. fd = -1;
  500. }
  501. unlink(tmpbuf);
  502. out:
  503. free(m.ext);
  504. return fd;
  505. }
  506. bool symsrc__possibly_runtime(struct symsrc *ss)
  507. {
  508. return ss->dynsym || ss->opdsec;
  509. }
  510. bool symsrc__has_symtab(struct symsrc *ss)
  511. {
  512. return ss->symtab != NULL;
  513. }
  514. void symsrc__destroy(struct symsrc *ss)
  515. {
  516. zfree(&ss->name);
  517. elf_end(ss->elf);
  518. close(ss->fd);
  519. }
  520. int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name,
  521. enum dso_binary_type type)
  522. {
  523. int err = -1;
  524. GElf_Ehdr ehdr;
  525. Elf *elf;
  526. int fd;
  527. if (dso__needs_decompress(dso))
  528. fd = decompress_kmodule(dso, name, type);
  529. else
  530. fd = open(name, O_RDONLY);
  531. if (fd < 0)
  532. return -1;
  533. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  534. if (elf == NULL) {
  535. pr_debug("%s: cannot read %s ELF file.\n", __func__, name);
  536. goto out_close;
  537. }
  538. if (gelf_getehdr(elf, &ehdr) == NULL) {
  539. pr_debug("%s: cannot get elf header.\n", __func__);
  540. goto out_elf_end;
  541. }
  542. if (dso__swap_init(dso, ehdr.e_ident[EI_DATA]))
  543. goto out_elf_end;
  544. /* Always reject images with a mismatched build-id: */
  545. if (dso->has_build_id) {
  546. u8 build_id[BUILD_ID_SIZE];
  547. if (elf_read_build_id(elf, build_id, BUILD_ID_SIZE) < 0)
  548. goto out_elf_end;
  549. if (!dso__build_id_equal(dso, build_id))
  550. goto out_elf_end;
  551. }
  552. ss->is_64_bit = (gelf_getclass(elf) == ELFCLASS64);
  553. ss->symtab = elf_section_by_name(elf, &ehdr, &ss->symshdr, ".symtab",
  554. NULL);
  555. if (ss->symshdr.sh_type != SHT_SYMTAB)
  556. ss->symtab = NULL;
  557. ss->dynsym_idx = 0;
  558. ss->dynsym = elf_section_by_name(elf, &ehdr, &ss->dynshdr, ".dynsym",
  559. &ss->dynsym_idx);
  560. if (ss->dynshdr.sh_type != SHT_DYNSYM)
  561. ss->dynsym = NULL;
  562. ss->opdidx = 0;
  563. ss->opdsec = elf_section_by_name(elf, &ehdr, &ss->opdshdr, ".opd",
  564. &ss->opdidx);
  565. if (ss->opdshdr.sh_type != SHT_PROGBITS)
  566. ss->opdsec = NULL;
  567. if (dso->kernel == DSO_TYPE_USER) {
  568. GElf_Shdr shdr;
  569. ss->adjust_symbols = (ehdr.e_type == ET_EXEC ||
  570. ehdr.e_type == ET_REL ||
  571. dso__is_vdso(dso) ||
  572. elf_section_by_name(elf, &ehdr, &shdr,
  573. ".gnu.prelink_undo",
  574. NULL) != NULL);
  575. } else {
  576. ss->adjust_symbols = ehdr.e_type == ET_EXEC ||
  577. ehdr.e_type == ET_REL;
  578. }
  579. ss->name = strdup(name);
  580. if (!ss->name)
  581. goto out_elf_end;
  582. ss->elf = elf;
  583. ss->fd = fd;
  584. ss->ehdr = ehdr;
  585. ss->type = type;
  586. return 0;
  587. out_elf_end:
  588. elf_end(elf);
  589. out_close:
  590. close(fd);
  591. return err;
  592. }
  593. /**
  594. * ref_reloc_sym_not_found - has kernel relocation symbol been found.
  595. * @kmap: kernel maps and relocation reference symbol
  596. *
  597. * This function returns %true if we are dealing with the kernel maps and the
  598. * relocation reference symbol has not yet been found. Otherwise %false is
  599. * returned.
  600. */
  601. static bool ref_reloc_sym_not_found(struct kmap *kmap)
  602. {
  603. return kmap && kmap->ref_reloc_sym && kmap->ref_reloc_sym->name &&
  604. !kmap->ref_reloc_sym->unrelocated_addr;
  605. }
  606. /**
  607. * ref_reloc - kernel relocation offset.
  608. * @kmap: kernel maps and relocation reference symbol
  609. *
  610. * This function returns the offset of kernel addresses as determined by using
  611. * the relocation reference symbol i.e. if the kernel has not been relocated
  612. * then the return value is zero.
  613. */
  614. static u64 ref_reloc(struct kmap *kmap)
  615. {
  616. if (kmap && kmap->ref_reloc_sym &&
  617. kmap->ref_reloc_sym->unrelocated_addr)
  618. return kmap->ref_reloc_sym->addr -
  619. kmap->ref_reloc_sym->unrelocated_addr;
  620. return 0;
  621. }
  622. static bool want_demangle(bool is_kernel_sym)
  623. {
  624. return is_kernel_sym ? symbol_conf.demangle_kernel : symbol_conf.demangle;
  625. }
  626. int dso__load_sym(struct dso *dso, struct map *map,
  627. struct symsrc *syms_ss, struct symsrc *runtime_ss,
  628. symbol_filter_t filter, int kmodule)
  629. {
  630. struct kmap *kmap = dso->kernel ? map__kmap(map) : NULL;
  631. struct map *curr_map = map;
  632. struct dso *curr_dso = dso;
  633. Elf_Data *symstrs, *secstrs;
  634. uint32_t nr_syms;
  635. int err = -1;
  636. uint32_t idx;
  637. GElf_Ehdr ehdr;
  638. GElf_Shdr shdr;
  639. Elf_Data *syms, *opddata = NULL;
  640. GElf_Sym sym;
  641. Elf_Scn *sec, *sec_strndx;
  642. Elf *elf;
  643. int nr = 0;
  644. bool remap_kernel = false, adjust_kernel_syms = false;
  645. dso->symtab_type = syms_ss->type;
  646. dso->is_64_bit = syms_ss->is_64_bit;
  647. dso->rel = syms_ss->ehdr.e_type == ET_REL;
  648. /*
  649. * Modules may already have symbols from kallsyms, but those symbols
  650. * have the wrong values for the dso maps, so remove them.
  651. */
  652. if (kmodule && syms_ss->symtab)
  653. symbols__delete(&dso->symbols[map->type]);
  654. if (!syms_ss->symtab) {
  655. /*
  656. * If the vmlinux is stripped, fail so we will fall back
  657. * to using kallsyms. The vmlinux runtime symbols aren't
  658. * of much use.
  659. */
  660. if (dso->kernel)
  661. goto out_elf_end;
  662. syms_ss->symtab = syms_ss->dynsym;
  663. syms_ss->symshdr = syms_ss->dynshdr;
  664. }
  665. elf = syms_ss->elf;
  666. ehdr = syms_ss->ehdr;
  667. sec = syms_ss->symtab;
  668. shdr = syms_ss->symshdr;
  669. if (runtime_ss->opdsec)
  670. opddata = elf_rawdata(runtime_ss->opdsec, NULL);
  671. syms = elf_getdata(sec, NULL);
  672. if (syms == NULL)
  673. goto out_elf_end;
  674. sec = elf_getscn(elf, shdr.sh_link);
  675. if (sec == NULL)
  676. goto out_elf_end;
  677. symstrs = elf_getdata(sec, NULL);
  678. if (symstrs == NULL)
  679. goto out_elf_end;
  680. sec_strndx = elf_getscn(runtime_ss->elf, runtime_ss->ehdr.e_shstrndx);
  681. if (sec_strndx == NULL)
  682. goto out_elf_end;
  683. secstrs = elf_getdata(sec_strndx, NULL);
  684. if (secstrs == NULL)
  685. goto out_elf_end;
  686. nr_syms = shdr.sh_size / shdr.sh_entsize;
  687. memset(&sym, 0, sizeof(sym));
  688. /*
  689. * The kernel relocation symbol is needed in advance in order to adjust
  690. * kernel maps correctly.
  691. */
  692. if (ref_reloc_sym_not_found(kmap)) {
  693. elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
  694. const char *elf_name = elf_sym__name(&sym, symstrs);
  695. if (strcmp(elf_name, kmap->ref_reloc_sym->name))
  696. continue;
  697. kmap->ref_reloc_sym->unrelocated_addr = sym.st_value;
  698. map->reloc = kmap->ref_reloc_sym->addr -
  699. kmap->ref_reloc_sym->unrelocated_addr;
  700. break;
  701. }
  702. }
  703. dso->adjust_symbols = runtime_ss->adjust_symbols || ref_reloc(kmap);
  704. /*
  705. * Initial kernel and module mappings do not map to the dso. For
  706. * function mappings, flag the fixups.
  707. */
  708. if (map->type == MAP__FUNCTION && (dso->kernel || kmodule)) {
  709. remap_kernel = true;
  710. adjust_kernel_syms = dso->adjust_symbols;
  711. }
  712. elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
  713. struct symbol *f;
  714. const char *elf_name = elf_sym__name(&sym, symstrs);
  715. char *demangled = NULL;
  716. int is_label = elf_sym__is_label(&sym);
  717. const char *section_name;
  718. bool used_opd = false;
  719. if (!is_label && !elf_sym__is_a(&sym, map->type))
  720. continue;
  721. /* Reject ARM ELF "mapping symbols": these aren't unique and
  722. * don't identify functions, so will confuse the profile
  723. * output: */
  724. if (ehdr.e_machine == EM_ARM || ehdr.e_machine == EM_AARCH64) {
  725. if (elf_name[0] == '$' && strchr("adtx", elf_name[1])
  726. && (elf_name[2] == '\0' || elf_name[2] == '.'))
  727. continue;
  728. }
  729. if (runtime_ss->opdsec && sym.st_shndx == runtime_ss->opdidx) {
  730. u32 offset = sym.st_value - syms_ss->opdshdr.sh_addr;
  731. u64 *opd = opddata->d_buf + offset;
  732. sym.st_value = DSO__SWAP(dso, u64, *opd);
  733. sym.st_shndx = elf_addr_to_index(runtime_ss->elf,
  734. sym.st_value);
  735. used_opd = true;
  736. }
  737. /*
  738. * When loading symbols in a data mapping, ABS symbols (which
  739. * has a value of SHN_ABS in its st_shndx) failed at
  740. * elf_getscn(). And it marks the loading as a failure so
  741. * already loaded symbols cannot be fixed up.
  742. *
  743. * I'm not sure what should be done. Just ignore them for now.
  744. * - Namhyung Kim
  745. */
  746. if (sym.st_shndx == SHN_ABS)
  747. continue;
  748. sec = elf_getscn(runtime_ss->elf, sym.st_shndx);
  749. if (!sec)
  750. goto out_elf_end;
  751. gelf_getshdr(sec, &shdr);
  752. if (is_label && !elf_sec__is_a(&shdr, secstrs, map->type))
  753. continue;
  754. section_name = elf_sec__name(&shdr, secstrs);
  755. /* On ARM, symbols for thumb functions have 1 added to
  756. * the symbol address as a flag - remove it */
  757. if ((ehdr.e_machine == EM_ARM) &&
  758. (map->type == MAP__FUNCTION) &&
  759. (sym.st_value & 1))
  760. --sym.st_value;
  761. if (dso->kernel || kmodule) {
  762. char dso_name[PATH_MAX];
  763. /* Adjust symbol to map to file offset */
  764. if (adjust_kernel_syms)
  765. sym.st_value -= shdr.sh_addr - shdr.sh_offset;
  766. if (strcmp(section_name,
  767. (curr_dso->short_name +
  768. dso->short_name_len)) == 0)
  769. goto new_symbol;
  770. if (strcmp(section_name, ".text") == 0) {
  771. /*
  772. * The initial kernel mapping is based on
  773. * kallsyms and identity maps. Overwrite it to
  774. * map to the kernel dso.
  775. */
  776. if (remap_kernel && dso->kernel) {
  777. remap_kernel = false;
  778. map->start = shdr.sh_addr +
  779. ref_reloc(kmap);
  780. map->end = map->start + shdr.sh_size;
  781. map->pgoff = shdr.sh_offset;
  782. map->map_ip = map__map_ip;
  783. map->unmap_ip = map__unmap_ip;
  784. /* Ensure maps are correctly ordered */
  785. map_groups__remove(kmap->kmaps, map);
  786. map_groups__insert(kmap->kmaps, map);
  787. }
  788. /*
  789. * The initial module mapping is based on
  790. * /proc/modules mapped to offset zero.
  791. * Overwrite it to map to the module dso.
  792. */
  793. if (remap_kernel && kmodule) {
  794. remap_kernel = false;
  795. map->pgoff = shdr.sh_offset;
  796. }
  797. curr_map = map;
  798. curr_dso = dso;
  799. goto new_symbol;
  800. }
  801. if (!kmap)
  802. goto new_symbol;
  803. snprintf(dso_name, sizeof(dso_name),
  804. "%s%s", dso->short_name, section_name);
  805. curr_map = map_groups__find_by_name(kmap->kmaps, map->type, dso_name);
  806. if (curr_map == NULL) {
  807. u64 start = sym.st_value;
  808. if (kmodule)
  809. start += map->start + shdr.sh_offset;
  810. curr_dso = dso__new(dso_name);
  811. if (curr_dso == NULL)
  812. goto out_elf_end;
  813. curr_dso->kernel = dso->kernel;
  814. curr_dso->long_name = dso->long_name;
  815. curr_dso->long_name_len = dso->long_name_len;
  816. curr_map = map__new2(start, curr_dso,
  817. map->type);
  818. if (curr_map == NULL) {
  819. dso__delete(curr_dso);
  820. goto out_elf_end;
  821. }
  822. if (adjust_kernel_syms) {
  823. curr_map->start = shdr.sh_addr +
  824. ref_reloc(kmap);
  825. curr_map->end = curr_map->start +
  826. shdr.sh_size;
  827. curr_map->pgoff = shdr.sh_offset;
  828. } else {
  829. curr_map->map_ip = identity__map_ip;
  830. curr_map->unmap_ip = identity__map_ip;
  831. }
  832. curr_dso->symtab_type = dso->symtab_type;
  833. map_groups__insert(kmap->kmaps, curr_map);
  834. /*
  835. * The new DSO should go to the kernel DSOS
  836. */
  837. dsos__add(&map->groups->machine->kernel_dsos,
  838. curr_dso);
  839. dso__set_loaded(curr_dso, map->type);
  840. } else
  841. curr_dso = curr_map->dso;
  842. goto new_symbol;
  843. }
  844. if ((used_opd && runtime_ss->adjust_symbols)
  845. || (!used_opd && syms_ss->adjust_symbols)) {
  846. pr_debug4("%s: adjusting symbol: st_value: %#" PRIx64 " "
  847. "sh_addr: %#" PRIx64 " sh_offset: %#" PRIx64 "\n", __func__,
  848. (u64)sym.st_value, (u64)shdr.sh_addr,
  849. (u64)shdr.sh_offset);
  850. sym.st_value -= shdr.sh_addr - shdr.sh_offset;
  851. }
  852. new_symbol:
  853. /*
  854. * We need to figure out if the object was created from C++ sources
  855. * DWARF DW_compile_unit has this, but we don't always have access
  856. * to it...
  857. */
  858. if (want_demangle(dso->kernel || kmodule)) {
  859. int demangle_flags = DMGL_NO_OPTS;
  860. if (verbose)
  861. demangle_flags = DMGL_PARAMS | DMGL_ANSI;
  862. demangled = bfd_demangle(NULL, elf_name, demangle_flags);
  863. if (demangled != NULL)
  864. elf_name = demangled;
  865. }
  866. f = symbol__new(sym.st_value, sym.st_size,
  867. GELF_ST_BIND(sym.st_info), elf_name);
  868. free(demangled);
  869. if (!f)
  870. goto out_elf_end;
  871. if (filter && filter(curr_map, f))
  872. symbol__delete(f);
  873. else {
  874. symbols__insert(&curr_dso->symbols[curr_map->type], f);
  875. nr++;
  876. }
  877. }
  878. /*
  879. * For misannotated, zeroed, ASM function sizes.
  880. */
  881. if (nr > 0) {
  882. if (!symbol_conf.allow_aliases)
  883. symbols__fixup_duplicate(&dso->symbols[map->type]);
  884. symbols__fixup_end(&dso->symbols[map->type]);
  885. if (kmap) {
  886. /*
  887. * We need to fixup this here too because we create new
  888. * maps here, for things like vsyscall sections.
  889. */
  890. __map_groups__fixup_end(kmap->kmaps, map->type);
  891. }
  892. }
  893. err = nr;
  894. out_elf_end:
  895. return err;
  896. }
  897. static int elf_read_maps(Elf *elf, bool exe, mapfn_t mapfn, void *data)
  898. {
  899. GElf_Phdr phdr;
  900. size_t i, phdrnum;
  901. int err;
  902. u64 sz;
  903. if (elf_getphdrnum(elf, &phdrnum))
  904. return -1;
  905. for (i = 0; i < phdrnum; i++) {
  906. if (gelf_getphdr(elf, i, &phdr) == NULL)
  907. return -1;
  908. if (phdr.p_type != PT_LOAD)
  909. continue;
  910. if (exe) {
  911. if (!(phdr.p_flags & PF_X))
  912. continue;
  913. } else {
  914. if (!(phdr.p_flags & PF_R))
  915. continue;
  916. }
  917. sz = min(phdr.p_memsz, phdr.p_filesz);
  918. if (!sz)
  919. continue;
  920. err = mapfn(phdr.p_vaddr, sz, phdr.p_offset, data);
  921. if (err)
  922. return err;
  923. }
  924. return 0;
  925. }
  926. int file__read_maps(int fd, bool exe, mapfn_t mapfn, void *data,
  927. bool *is_64_bit)
  928. {
  929. int err;
  930. Elf *elf;
  931. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  932. if (elf == NULL)
  933. return -1;
  934. if (is_64_bit)
  935. *is_64_bit = (gelf_getclass(elf) == ELFCLASS64);
  936. err = elf_read_maps(elf, exe, mapfn, data);
  937. elf_end(elf);
  938. return err;
  939. }
  940. enum dso_type dso__type_fd(int fd)
  941. {
  942. enum dso_type dso_type = DSO__TYPE_UNKNOWN;
  943. GElf_Ehdr ehdr;
  944. Elf_Kind ek;
  945. Elf *elf;
  946. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  947. if (elf == NULL)
  948. goto out;
  949. ek = elf_kind(elf);
  950. if (ek != ELF_K_ELF)
  951. goto out_end;
  952. if (gelf_getclass(elf) == ELFCLASS64) {
  953. dso_type = DSO__TYPE_64BIT;
  954. goto out_end;
  955. }
  956. if (gelf_getehdr(elf, &ehdr) == NULL)
  957. goto out_end;
  958. if (ehdr.e_machine == EM_X86_64)
  959. dso_type = DSO__TYPE_X32BIT;
  960. else
  961. dso_type = DSO__TYPE_32BIT;
  962. out_end:
  963. elf_end(elf);
  964. out:
  965. return dso_type;
  966. }
  967. static int copy_bytes(int from, off_t from_offs, int to, off_t to_offs, u64 len)
  968. {
  969. ssize_t r;
  970. size_t n;
  971. int err = -1;
  972. char *buf = malloc(page_size);
  973. if (buf == NULL)
  974. return -1;
  975. if (lseek(to, to_offs, SEEK_SET) != to_offs)
  976. goto out;
  977. if (lseek(from, from_offs, SEEK_SET) != from_offs)
  978. goto out;
  979. while (len) {
  980. n = page_size;
  981. if (len < n)
  982. n = len;
  983. /* Use read because mmap won't work on proc files */
  984. r = read(from, buf, n);
  985. if (r < 0)
  986. goto out;
  987. if (!r)
  988. break;
  989. n = r;
  990. r = write(to, buf, n);
  991. if (r < 0)
  992. goto out;
  993. if ((size_t)r != n)
  994. goto out;
  995. len -= n;
  996. }
  997. err = 0;
  998. out:
  999. free(buf);
  1000. return err;
  1001. }
  1002. struct kcore {
  1003. int fd;
  1004. int elfclass;
  1005. Elf *elf;
  1006. GElf_Ehdr ehdr;
  1007. };
  1008. static int kcore__open(struct kcore *kcore, const char *filename)
  1009. {
  1010. GElf_Ehdr *ehdr;
  1011. kcore->fd = open(filename, O_RDONLY);
  1012. if (kcore->fd == -1)
  1013. return -1;
  1014. kcore->elf = elf_begin(kcore->fd, ELF_C_READ, NULL);
  1015. if (!kcore->elf)
  1016. goto out_close;
  1017. kcore->elfclass = gelf_getclass(kcore->elf);
  1018. if (kcore->elfclass == ELFCLASSNONE)
  1019. goto out_end;
  1020. ehdr = gelf_getehdr(kcore->elf, &kcore->ehdr);
  1021. if (!ehdr)
  1022. goto out_end;
  1023. return 0;
  1024. out_end:
  1025. elf_end(kcore->elf);
  1026. out_close:
  1027. close(kcore->fd);
  1028. return -1;
  1029. }
  1030. static int kcore__init(struct kcore *kcore, char *filename, int elfclass,
  1031. bool temp)
  1032. {
  1033. GElf_Ehdr *ehdr;
  1034. kcore->elfclass = elfclass;
  1035. if (temp)
  1036. kcore->fd = mkstemp(filename);
  1037. else
  1038. kcore->fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0400);
  1039. if (kcore->fd == -1)
  1040. return -1;
  1041. kcore->elf = elf_begin(kcore->fd, ELF_C_WRITE, NULL);
  1042. if (!kcore->elf)
  1043. goto out_close;
  1044. if (!gelf_newehdr(kcore->elf, elfclass))
  1045. goto out_end;
  1046. ehdr = gelf_getehdr(kcore->elf, &kcore->ehdr);
  1047. if (!ehdr)
  1048. goto out_end;
  1049. return 0;
  1050. out_end:
  1051. elf_end(kcore->elf);
  1052. out_close:
  1053. close(kcore->fd);
  1054. unlink(filename);
  1055. return -1;
  1056. }
  1057. static void kcore__close(struct kcore *kcore)
  1058. {
  1059. elf_end(kcore->elf);
  1060. close(kcore->fd);
  1061. }
  1062. static int kcore__copy_hdr(struct kcore *from, struct kcore *to, size_t count)
  1063. {
  1064. GElf_Ehdr *ehdr = &to->ehdr;
  1065. GElf_Ehdr *kehdr = &from->ehdr;
  1066. memcpy(ehdr->e_ident, kehdr->e_ident, EI_NIDENT);
  1067. ehdr->e_type = kehdr->e_type;
  1068. ehdr->e_machine = kehdr->e_machine;
  1069. ehdr->e_version = kehdr->e_version;
  1070. ehdr->e_entry = 0;
  1071. ehdr->e_shoff = 0;
  1072. ehdr->e_flags = kehdr->e_flags;
  1073. ehdr->e_phnum = count;
  1074. ehdr->e_shentsize = 0;
  1075. ehdr->e_shnum = 0;
  1076. ehdr->e_shstrndx = 0;
  1077. if (from->elfclass == ELFCLASS32) {
  1078. ehdr->e_phoff = sizeof(Elf32_Ehdr);
  1079. ehdr->e_ehsize = sizeof(Elf32_Ehdr);
  1080. ehdr->e_phentsize = sizeof(Elf32_Phdr);
  1081. } else {
  1082. ehdr->e_phoff = sizeof(Elf64_Ehdr);
  1083. ehdr->e_ehsize = sizeof(Elf64_Ehdr);
  1084. ehdr->e_phentsize = sizeof(Elf64_Phdr);
  1085. }
  1086. if (!gelf_update_ehdr(to->elf, ehdr))
  1087. return -1;
  1088. if (!gelf_newphdr(to->elf, count))
  1089. return -1;
  1090. return 0;
  1091. }
  1092. static int kcore__add_phdr(struct kcore *kcore, int idx, off_t offset,
  1093. u64 addr, u64 len)
  1094. {
  1095. GElf_Phdr gphdr;
  1096. GElf_Phdr *phdr;
  1097. phdr = gelf_getphdr(kcore->elf, idx, &gphdr);
  1098. if (!phdr)
  1099. return -1;
  1100. phdr->p_type = PT_LOAD;
  1101. phdr->p_flags = PF_R | PF_W | PF_X;
  1102. phdr->p_offset = offset;
  1103. phdr->p_vaddr = addr;
  1104. phdr->p_paddr = 0;
  1105. phdr->p_filesz = len;
  1106. phdr->p_memsz = len;
  1107. phdr->p_align = page_size;
  1108. if (!gelf_update_phdr(kcore->elf, idx, phdr))
  1109. return -1;
  1110. return 0;
  1111. }
  1112. static off_t kcore__write(struct kcore *kcore)
  1113. {
  1114. return elf_update(kcore->elf, ELF_C_WRITE);
  1115. }
  1116. struct phdr_data {
  1117. off_t offset;
  1118. u64 addr;
  1119. u64 len;
  1120. };
  1121. struct kcore_copy_info {
  1122. u64 stext;
  1123. u64 etext;
  1124. u64 first_symbol;
  1125. u64 last_symbol;
  1126. u64 first_module;
  1127. u64 last_module_symbol;
  1128. struct phdr_data kernel_map;
  1129. struct phdr_data modules_map;
  1130. };
  1131. static int kcore_copy__process_kallsyms(void *arg, const char *name, char type,
  1132. u64 start)
  1133. {
  1134. struct kcore_copy_info *kci = arg;
  1135. if (!symbol_type__is_a(type, MAP__FUNCTION))
  1136. return 0;
  1137. if (strchr(name, '[')) {
  1138. if (start > kci->last_module_symbol)
  1139. kci->last_module_symbol = start;
  1140. return 0;
  1141. }
  1142. if (!kci->first_symbol || start < kci->first_symbol)
  1143. kci->first_symbol = start;
  1144. if (!kci->last_symbol || start > kci->last_symbol)
  1145. kci->last_symbol = start;
  1146. if (!strcmp(name, "_stext")) {
  1147. kci->stext = start;
  1148. return 0;
  1149. }
  1150. if (!strcmp(name, "_etext")) {
  1151. kci->etext = start;
  1152. return 0;
  1153. }
  1154. return 0;
  1155. }
  1156. static int kcore_copy__parse_kallsyms(struct kcore_copy_info *kci,
  1157. const char *dir)
  1158. {
  1159. char kallsyms_filename[PATH_MAX];
  1160. scnprintf(kallsyms_filename, PATH_MAX, "%s/kallsyms", dir);
  1161. if (symbol__restricted_filename(kallsyms_filename, "/proc/kallsyms"))
  1162. return -1;
  1163. if (kallsyms__parse(kallsyms_filename, kci,
  1164. kcore_copy__process_kallsyms) < 0)
  1165. return -1;
  1166. return 0;
  1167. }
  1168. static int kcore_copy__process_modules(void *arg,
  1169. const char *name __maybe_unused,
  1170. u64 start)
  1171. {
  1172. struct kcore_copy_info *kci = arg;
  1173. if (!kci->first_module || start < kci->first_module)
  1174. kci->first_module = start;
  1175. return 0;
  1176. }
  1177. static int kcore_copy__parse_modules(struct kcore_copy_info *kci,
  1178. const char *dir)
  1179. {
  1180. char modules_filename[PATH_MAX];
  1181. scnprintf(modules_filename, PATH_MAX, "%s/modules", dir);
  1182. if (symbol__restricted_filename(modules_filename, "/proc/modules"))
  1183. return -1;
  1184. if (modules__parse(modules_filename, kci,
  1185. kcore_copy__process_modules) < 0)
  1186. return -1;
  1187. return 0;
  1188. }
  1189. static void kcore_copy__map(struct phdr_data *p, u64 start, u64 end, u64 pgoff,
  1190. u64 s, u64 e)
  1191. {
  1192. if (p->addr || s < start || s >= end)
  1193. return;
  1194. p->addr = s;
  1195. p->offset = (s - start) + pgoff;
  1196. p->len = e < end ? e - s : end - s;
  1197. }
  1198. static int kcore_copy__read_map(u64 start, u64 len, u64 pgoff, void *data)
  1199. {
  1200. struct kcore_copy_info *kci = data;
  1201. u64 end = start + len;
  1202. kcore_copy__map(&kci->kernel_map, start, end, pgoff, kci->stext,
  1203. kci->etext);
  1204. kcore_copy__map(&kci->modules_map, start, end, pgoff, kci->first_module,
  1205. kci->last_module_symbol);
  1206. return 0;
  1207. }
  1208. static int kcore_copy__read_maps(struct kcore_copy_info *kci, Elf *elf)
  1209. {
  1210. if (elf_read_maps(elf, true, kcore_copy__read_map, kci) < 0)
  1211. return -1;
  1212. return 0;
  1213. }
  1214. static int kcore_copy__calc_maps(struct kcore_copy_info *kci, const char *dir,
  1215. Elf *elf)
  1216. {
  1217. if (kcore_copy__parse_kallsyms(kci, dir))
  1218. return -1;
  1219. if (kcore_copy__parse_modules(kci, dir))
  1220. return -1;
  1221. if (kci->stext)
  1222. kci->stext = round_down(kci->stext, page_size);
  1223. else
  1224. kci->stext = round_down(kci->first_symbol, page_size);
  1225. if (kci->etext) {
  1226. kci->etext = round_up(kci->etext, page_size);
  1227. } else if (kci->last_symbol) {
  1228. kci->etext = round_up(kci->last_symbol, page_size);
  1229. kci->etext += page_size;
  1230. }
  1231. kci->first_module = round_down(kci->first_module, page_size);
  1232. if (kci->last_module_symbol) {
  1233. kci->last_module_symbol = round_up(kci->last_module_symbol,
  1234. page_size);
  1235. kci->last_module_symbol += page_size;
  1236. }
  1237. if (!kci->stext || !kci->etext)
  1238. return -1;
  1239. if (kci->first_module && !kci->last_module_symbol)
  1240. return -1;
  1241. return kcore_copy__read_maps(kci, elf);
  1242. }
  1243. static int kcore_copy__copy_file(const char *from_dir, const char *to_dir,
  1244. const char *name)
  1245. {
  1246. char from_filename[PATH_MAX];
  1247. char to_filename[PATH_MAX];
  1248. scnprintf(from_filename, PATH_MAX, "%s/%s", from_dir, name);
  1249. scnprintf(to_filename, PATH_MAX, "%s/%s", to_dir, name);
  1250. return copyfile_mode(from_filename, to_filename, 0400);
  1251. }
  1252. static int kcore_copy__unlink(const char *dir, const char *name)
  1253. {
  1254. char filename[PATH_MAX];
  1255. scnprintf(filename, PATH_MAX, "%s/%s", dir, name);
  1256. return unlink(filename);
  1257. }
  1258. static int kcore_copy__compare_fds(int from, int to)
  1259. {
  1260. char *buf_from;
  1261. char *buf_to;
  1262. ssize_t ret;
  1263. size_t len;
  1264. int err = -1;
  1265. buf_from = malloc(page_size);
  1266. buf_to = malloc(page_size);
  1267. if (!buf_from || !buf_to)
  1268. goto out;
  1269. while (1) {
  1270. /* Use read because mmap won't work on proc files */
  1271. ret = read(from, buf_from, page_size);
  1272. if (ret < 0)
  1273. goto out;
  1274. if (!ret)
  1275. break;
  1276. len = ret;
  1277. if (readn(to, buf_to, len) != (int)len)
  1278. goto out;
  1279. if (memcmp(buf_from, buf_to, len))
  1280. goto out;
  1281. }
  1282. err = 0;
  1283. out:
  1284. free(buf_to);
  1285. free(buf_from);
  1286. return err;
  1287. }
  1288. static int kcore_copy__compare_files(const char *from_filename,
  1289. const char *to_filename)
  1290. {
  1291. int from, to, err = -1;
  1292. from = open(from_filename, O_RDONLY);
  1293. if (from < 0)
  1294. return -1;
  1295. to = open(to_filename, O_RDONLY);
  1296. if (to < 0)
  1297. goto out_close_from;
  1298. err = kcore_copy__compare_fds(from, to);
  1299. close(to);
  1300. out_close_from:
  1301. close(from);
  1302. return err;
  1303. }
  1304. static int kcore_copy__compare_file(const char *from_dir, const char *to_dir,
  1305. const char *name)
  1306. {
  1307. char from_filename[PATH_MAX];
  1308. char to_filename[PATH_MAX];
  1309. scnprintf(from_filename, PATH_MAX, "%s/%s", from_dir, name);
  1310. scnprintf(to_filename, PATH_MAX, "%s/%s", to_dir, name);
  1311. return kcore_copy__compare_files(from_filename, to_filename);
  1312. }
  1313. /**
  1314. * kcore_copy - copy kallsyms, modules and kcore from one directory to another.
  1315. * @from_dir: from directory
  1316. * @to_dir: to directory
  1317. *
  1318. * This function copies kallsyms, modules and kcore files from one directory to
  1319. * another. kallsyms and modules are copied entirely. Only code segments are
  1320. * copied from kcore. It is assumed that two segments suffice: one for the
  1321. * kernel proper and one for all the modules. The code segments are determined
  1322. * from kallsyms and modules files. The kernel map starts at _stext or the
  1323. * lowest function symbol, and ends at _etext or the highest function symbol.
  1324. * The module map starts at the lowest module address and ends at the highest
  1325. * module symbol. Start addresses are rounded down to the nearest page. End
  1326. * addresses are rounded up to the nearest page. An extra page is added to the
  1327. * highest kernel symbol and highest module symbol to, hopefully, encompass that
  1328. * symbol too. Because it contains only code sections, the resulting kcore is
  1329. * unusual. One significant peculiarity is that the mapping (start -> pgoff)
  1330. * is not the same for the kernel map and the modules map. That happens because
  1331. * the data is copied adjacently whereas the original kcore has gaps. Finally,
  1332. * kallsyms and modules files are compared with their copies to check that
  1333. * modules have not been loaded or unloaded while the copies were taking place.
  1334. *
  1335. * Return: %0 on success, %-1 on failure.
  1336. */
  1337. int kcore_copy(const char *from_dir, const char *to_dir)
  1338. {
  1339. struct kcore kcore;
  1340. struct kcore extract;
  1341. size_t count = 2;
  1342. int idx = 0, err = -1;
  1343. off_t offset = page_size, sz, modules_offset = 0;
  1344. struct kcore_copy_info kci = { .stext = 0, };
  1345. char kcore_filename[PATH_MAX];
  1346. char extract_filename[PATH_MAX];
  1347. if (kcore_copy__copy_file(from_dir, to_dir, "kallsyms"))
  1348. return -1;
  1349. if (kcore_copy__copy_file(from_dir, to_dir, "modules"))
  1350. goto out_unlink_kallsyms;
  1351. scnprintf(kcore_filename, PATH_MAX, "%s/kcore", from_dir);
  1352. scnprintf(extract_filename, PATH_MAX, "%s/kcore", to_dir);
  1353. if (kcore__open(&kcore, kcore_filename))
  1354. goto out_unlink_modules;
  1355. if (kcore_copy__calc_maps(&kci, from_dir, kcore.elf))
  1356. goto out_kcore_close;
  1357. if (kcore__init(&extract, extract_filename, kcore.elfclass, false))
  1358. goto out_kcore_close;
  1359. if (!kci.modules_map.addr)
  1360. count -= 1;
  1361. if (kcore__copy_hdr(&kcore, &extract, count))
  1362. goto out_extract_close;
  1363. if (kcore__add_phdr(&extract, idx++, offset, kci.kernel_map.addr,
  1364. kci.kernel_map.len))
  1365. goto out_extract_close;
  1366. if (kci.modules_map.addr) {
  1367. modules_offset = offset + kci.kernel_map.len;
  1368. if (kcore__add_phdr(&extract, idx, modules_offset,
  1369. kci.modules_map.addr, kci.modules_map.len))
  1370. goto out_extract_close;
  1371. }
  1372. sz = kcore__write(&extract);
  1373. if (sz < 0 || sz > offset)
  1374. goto out_extract_close;
  1375. if (copy_bytes(kcore.fd, kci.kernel_map.offset, extract.fd, offset,
  1376. kci.kernel_map.len))
  1377. goto out_extract_close;
  1378. if (modules_offset && copy_bytes(kcore.fd, kci.modules_map.offset,
  1379. extract.fd, modules_offset,
  1380. kci.modules_map.len))
  1381. goto out_extract_close;
  1382. if (kcore_copy__compare_file(from_dir, to_dir, "modules"))
  1383. goto out_extract_close;
  1384. if (kcore_copy__compare_file(from_dir, to_dir, "kallsyms"))
  1385. goto out_extract_close;
  1386. err = 0;
  1387. out_extract_close:
  1388. kcore__close(&extract);
  1389. if (err)
  1390. unlink(extract_filename);
  1391. out_kcore_close:
  1392. kcore__close(&kcore);
  1393. out_unlink_modules:
  1394. if (err)
  1395. kcore_copy__unlink(to_dir, "modules");
  1396. out_unlink_kallsyms:
  1397. if (err)
  1398. kcore_copy__unlink(to_dir, "kallsyms");
  1399. return err;
  1400. }
  1401. int kcore_extract__create(struct kcore_extract *kce)
  1402. {
  1403. struct kcore kcore;
  1404. struct kcore extract;
  1405. size_t count = 1;
  1406. int idx = 0, err = -1;
  1407. off_t offset = page_size, sz;
  1408. if (kcore__open(&kcore, kce->kcore_filename))
  1409. return -1;
  1410. strcpy(kce->extract_filename, PERF_KCORE_EXTRACT);
  1411. if (kcore__init(&extract, kce->extract_filename, kcore.elfclass, true))
  1412. goto out_kcore_close;
  1413. if (kcore__copy_hdr(&kcore, &extract, count))
  1414. goto out_extract_close;
  1415. if (kcore__add_phdr(&extract, idx, offset, kce->addr, kce->len))
  1416. goto out_extract_close;
  1417. sz = kcore__write(&extract);
  1418. if (sz < 0 || sz > offset)
  1419. goto out_extract_close;
  1420. if (copy_bytes(kcore.fd, kce->offs, extract.fd, offset, kce->len))
  1421. goto out_extract_close;
  1422. err = 0;
  1423. out_extract_close:
  1424. kcore__close(&extract);
  1425. if (err)
  1426. unlink(kce->extract_filename);
  1427. out_kcore_close:
  1428. kcore__close(&kcore);
  1429. return err;
  1430. }
  1431. void kcore_extract__delete(struct kcore_extract *kce)
  1432. {
  1433. unlink(kce->extract_filename);
  1434. }
  1435. void symbol__elf_init(void)
  1436. {
  1437. elf_version(EV_CURRENT);
  1438. }