symbol-elf.c 40 KB

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