symbol-elf.c 40 KB

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