symbol-elf.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <fcntl.h>
  3. #include <stdio.h>
  4. #include <errno.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <unistd.h>
  8. #include <inttypes.h>
  9. #include "dso.h"
  10. #include "map.h"
  11. #include "maps.h"
  12. #include "symbol.h"
  13. #include "symsrc.h"
  14. #include "demangle-java.h"
  15. #include "demangle-rust.h"
  16. #include "machine.h"
  17. #include "vdso.h"
  18. #include "debug.h"
  19. #include "util/copyfile.h"
  20. #include <linux/ctype.h>
  21. #include <linux/kernel.h>
  22. #include <linux/zalloc.h>
  23. #include <symbol/kallsyms.h>
  24. #include <internal/lib.h>
  25. #ifndef EM_AARCH64
  26. #define EM_AARCH64 183 /* ARM 64 bit */
  27. #endif
  28. #ifndef ELF32_ST_VISIBILITY
  29. #define ELF32_ST_VISIBILITY(o) ((o) & 0x03)
  30. #endif
  31. /* For ELF64 the definitions are the same. */
  32. #ifndef ELF64_ST_VISIBILITY
  33. #define ELF64_ST_VISIBILITY(o) ELF32_ST_VISIBILITY (o)
  34. #endif
  35. /* How to extract information held in the st_other field. */
  36. #ifndef GELF_ST_VISIBILITY
  37. #define GELF_ST_VISIBILITY(val) ELF64_ST_VISIBILITY (val)
  38. #endif
  39. typedef Elf64_Nhdr GElf_Nhdr;
  40. #ifndef DMGL_PARAMS
  41. #define DMGL_NO_OPTS 0 /* For readability... */
  42. #define DMGL_PARAMS (1 << 0) /* Include function args */
  43. #define DMGL_ANSI (1 << 1) /* Include const, volatile, etc */
  44. #endif
  45. #ifdef HAVE_LIBBFD_SUPPORT
  46. #define PACKAGE 'perf'
  47. #include <bfd.h>
  48. #else
  49. #ifdef HAVE_CPLUS_DEMANGLE_SUPPORT
  50. extern char *cplus_demangle(const char *, int);
  51. static inline char *bfd_demangle(void __maybe_unused *v, const char *c, int i)
  52. {
  53. return cplus_demangle(c, i);
  54. }
  55. #else
  56. #ifdef NO_DEMANGLE
  57. static inline char *bfd_demangle(void __maybe_unused *v,
  58. const char __maybe_unused *c,
  59. int __maybe_unused i)
  60. {
  61. return NULL;
  62. }
  63. #endif
  64. #endif
  65. #endif
  66. #ifndef HAVE_ELF_GETPHDRNUM_SUPPORT
  67. static int elf_getphdrnum(Elf *elf, size_t *dst)
  68. {
  69. GElf_Ehdr gehdr;
  70. GElf_Ehdr *ehdr;
  71. ehdr = gelf_getehdr(elf, &gehdr);
  72. if (!ehdr)
  73. return -1;
  74. *dst = ehdr->e_phnum;
  75. return 0;
  76. }
  77. #endif
  78. #ifndef HAVE_ELF_GETSHDRSTRNDX_SUPPORT
  79. static int elf_getshdrstrndx(Elf *elf __maybe_unused, size_t *dst __maybe_unused)
  80. {
  81. pr_err("%s: update your libelf to > 0.140, this one lacks elf_getshdrstrndx().\n", __func__);
  82. return -1;
  83. }
  84. #endif
  85. #ifndef NT_GNU_BUILD_ID
  86. #define NT_GNU_BUILD_ID 3
  87. #endif
  88. /**
  89. * elf_symtab__for_each_symbol - iterate thru all the symbols
  90. *
  91. * @syms: struct elf_symtab instance to iterate
  92. * @idx: uint32_t idx
  93. * @sym: GElf_Sym iterator
  94. */
  95. #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
  96. for (idx = 0, gelf_getsym(syms, idx, &sym);\
  97. idx < nr_syms; \
  98. idx++, gelf_getsym(syms, idx, &sym))
  99. static inline uint8_t elf_sym__type(const GElf_Sym *sym)
  100. {
  101. return GELF_ST_TYPE(sym->st_info);
  102. }
  103. static inline uint8_t elf_sym__visibility(const GElf_Sym *sym)
  104. {
  105. return GELF_ST_VISIBILITY(sym->st_other);
  106. }
  107. #ifndef STT_GNU_IFUNC
  108. #define STT_GNU_IFUNC 10
  109. #endif
  110. static inline int elf_sym__is_function(const GElf_Sym *sym)
  111. {
  112. return (elf_sym__type(sym) == STT_FUNC ||
  113. elf_sym__type(sym) == STT_GNU_IFUNC) &&
  114. sym->st_name != 0 &&
  115. sym->st_shndx != SHN_UNDEF;
  116. }
  117. static inline bool elf_sym__is_object(const GElf_Sym *sym)
  118. {
  119. return elf_sym__type(sym) == STT_OBJECT &&
  120. sym->st_name != 0 &&
  121. sym->st_shndx != SHN_UNDEF;
  122. }
  123. static inline int elf_sym__is_label(const GElf_Sym *sym)
  124. {
  125. return elf_sym__type(sym) == STT_NOTYPE &&
  126. sym->st_name != 0 &&
  127. sym->st_shndx != SHN_UNDEF &&
  128. sym->st_shndx != SHN_ABS &&
  129. elf_sym__visibility(sym) != STV_HIDDEN &&
  130. elf_sym__visibility(sym) != STV_INTERNAL;
  131. }
  132. static bool elf_sym__filter(GElf_Sym *sym)
  133. {
  134. return elf_sym__is_function(sym) || elf_sym__is_object(sym);
  135. }
  136. static inline const char *elf_sym__name(const GElf_Sym *sym,
  137. const Elf_Data *symstrs)
  138. {
  139. return symstrs->d_buf + sym->st_name;
  140. }
  141. static inline const char *elf_sec__name(const GElf_Shdr *shdr,
  142. const Elf_Data *secstrs)
  143. {
  144. return secstrs->d_buf + shdr->sh_name;
  145. }
  146. static inline int elf_sec__is_text(const GElf_Shdr *shdr,
  147. const Elf_Data *secstrs)
  148. {
  149. return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
  150. }
  151. static inline bool elf_sec__is_data(const GElf_Shdr *shdr,
  152. const Elf_Data *secstrs)
  153. {
  154. return strstr(elf_sec__name(shdr, secstrs), "data") != NULL;
  155. }
  156. static bool elf_sec__filter(GElf_Shdr *shdr, Elf_Data *secstrs)
  157. {
  158. return elf_sec__is_text(shdr, secstrs) ||
  159. elf_sec__is_data(shdr, secstrs);
  160. }
  161. static size_t elf_addr_to_index(Elf *elf, GElf_Addr addr)
  162. {
  163. Elf_Scn *sec = NULL;
  164. GElf_Shdr shdr;
  165. size_t cnt = 1;
  166. while ((sec = elf_nextscn(elf, sec)) != NULL) {
  167. gelf_getshdr(sec, &shdr);
  168. if ((addr >= shdr.sh_addr) &&
  169. (addr < (shdr.sh_addr + shdr.sh_size)))
  170. return cnt;
  171. ++cnt;
  172. }
  173. return -1;
  174. }
  175. Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
  176. GElf_Shdr *shp, const char *name, size_t *idx)
  177. {
  178. Elf_Scn *sec = NULL;
  179. size_t cnt = 1;
  180. /* Elf is corrupted/truncated, avoid calling elf_strptr. */
  181. if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL))
  182. return NULL;
  183. while ((sec = elf_nextscn(elf, sec)) != NULL) {
  184. char *str;
  185. gelf_getshdr(sec, shp);
  186. str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
  187. if (str && !strcmp(name, str)) {
  188. if (idx)
  189. *idx = cnt;
  190. return sec;
  191. }
  192. ++cnt;
  193. }
  194. return NULL;
  195. }
  196. static bool want_demangle(bool is_kernel_sym)
  197. {
  198. return is_kernel_sym ? symbol_conf.demangle_kernel : symbol_conf.demangle;
  199. }
  200. static char *demangle_sym(struct dso *dso, int kmodule, const char *elf_name)
  201. {
  202. int demangle_flags = verbose > 0 ? (DMGL_PARAMS | DMGL_ANSI) : DMGL_NO_OPTS;
  203. char *demangled = NULL;
  204. /*
  205. * We need to figure out if the object was created from C++ sources
  206. * DWARF DW_compile_unit has this, but we don't always have access
  207. * to it...
  208. */
  209. if (!want_demangle(dso->kernel || kmodule))
  210. return demangled;
  211. demangled = bfd_demangle(NULL, elf_name, demangle_flags);
  212. if (demangled == NULL)
  213. demangled = java_demangle_sym(elf_name, JAVA_DEMANGLE_NORET);
  214. else if (rust_is_mangled(demangled))
  215. /*
  216. * Input to Rust demangling is the BFD-demangled
  217. * name which it Rust-demangles in place.
  218. */
  219. rust_demangle_sym(demangled);
  220. return demangled;
  221. }
  222. #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
  223. for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
  224. idx < nr_entries; \
  225. ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
  226. #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
  227. for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
  228. idx < nr_entries; \
  229. ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
  230. /*
  231. * We need to check if we have a .dynsym, so that we can handle the
  232. * .plt, synthesizing its symbols, that aren't on the symtabs (be it
  233. * .dynsym or .symtab).
  234. * And always look at the original dso, not at debuginfo packages, that
  235. * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
  236. */
  237. int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss)
  238. {
  239. uint32_t nr_rel_entries, idx;
  240. GElf_Sym sym;
  241. u64 plt_offset, plt_header_size, plt_entry_size;
  242. GElf_Shdr shdr_plt;
  243. struct symbol *f;
  244. GElf_Shdr shdr_rel_plt, shdr_dynsym;
  245. Elf_Data *reldata, *syms, *symstrs;
  246. Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
  247. size_t dynsym_idx;
  248. GElf_Ehdr ehdr;
  249. char sympltname[1024];
  250. Elf *elf;
  251. int nr = 0, symidx, err = 0;
  252. if (!ss->dynsym)
  253. return 0;
  254. elf = ss->elf;
  255. ehdr = ss->ehdr;
  256. scn_dynsym = ss->dynsym;
  257. shdr_dynsym = ss->dynshdr;
  258. dynsym_idx = ss->dynsym_idx;
  259. if (scn_dynsym == NULL)
  260. goto out_elf_end;
  261. scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
  262. ".rela.plt", NULL);
  263. if (scn_plt_rel == NULL) {
  264. scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
  265. ".rel.plt", NULL);
  266. if (scn_plt_rel == NULL)
  267. goto out_elf_end;
  268. }
  269. err = -1;
  270. if (shdr_rel_plt.sh_link != dynsym_idx)
  271. goto out_elf_end;
  272. if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
  273. goto out_elf_end;
  274. /*
  275. * Fetch the relocation section to find the idxes to the GOT
  276. * and the symbols in the .dynsym they refer to.
  277. */
  278. reldata = elf_getdata(scn_plt_rel, NULL);
  279. if (reldata == NULL)
  280. goto out_elf_end;
  281. syms = elf_getdata(scn_dynsym, NULL);
  282. if (syms == NULL)
  283. goto out_elf_end;
  284. scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
  285. if (scn_symstrs == NULL)
  286. goto out_elf_end;
  287. symstrs = elf_getdata(scn_symstrs, NULL);
  288. if (symstrs == NULL)
  289. goto out_elf_end;
  290. if (symstrs->d_size == 0)
  291. goto out_elf_end;
  292. nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
  293. plt_offset = shdr_plt.sh_offset;
  294. switch (ehdr.e_machine) {
  295. case EM_ARM:
  296. plt_header_size = 20;
  297. plt_entry_size = 12;
  298. break;
  299. case EM_AARCH64:
  300. plt_header_size = 32;
  301. plt_entry_size = 16;
  302. break;
  303. case EM_SPARC:
  304. plt_header_size = 48;
  305. plt_entry_size = 12;
  306. break;
  307. case EM_SPARCV9:
  308. plt_header_size = 128;
  309. plt_entry_size = 32;
  310. break;
  311. default: /* FIXME: s390/alpha/mips/parisc/poperpc/sh/xtensa need to be checked */
  312. plt_header_size = shdr_plt.sh_entsize;
  313. plt_entry_size = shdr_plt.sh_entsize;
  314. break;
  315. }
  316. plt_offset += plt_header_size;
  317. if (shdr_rel_plt.sh_type == SHT_RELA) {
  318. GElf_Rela pos_mem, *pos;
  319. elf_section__for_each_rela(reldata, pos, pos_mem, idx,
  320. nr_rel_entries) {
  321. const char *elf_name = NULL;
  322. char *demangled = NULL;
  323. symidx = GELF_R_SYM(pos->r_info);
  324. gelf_getsym(syms, symidx, &sym);
  325. elf_name = elf_sym__name(&sym, symstrs);
  326. demangled = demangle_sym(dso, 0, elf_name);
  327. if (demangled != NULL)
  328. elf_name = demangled;
  329. snprintf(sympltname, sizeof(sympltname),
  330. "%s@plt", elf_name);
  331. free(demangled);
  332. f = symbol__new(plt_offset, plt_entry_size,
  333. STB_GLOBAL, STT_FUNC, sympltname);
  334. if (!f)
  335. goto out_elf_end;
  336. plt_offset += plt_entry_size;
  337. symbols__insert(&dso->symbols, f);
  338. ++nr;
  339. }
  340. } else if (shdr_rel_plt.sh_type == SHT_REL) {
  341. GElf_Rel pos_mem, *pos;
  342. elf_section__for_each_rel(reldata, pos, pos_mem, idx,
  343. nr_rel_entries) {
  344. const char *elf_name = NULL;
  345. char *demangled = NULL;
  346. symidx = GELF_R_SYM(pos->r_info);
  347. gelf_getsym(syms, symidx, &sym);
  348. elf_name = elf_sym__name(&sym, symstrs);
  349. demangled = demangle_sym(dso, 0, elf_name);
  350. if (demangled != NULL)
  351. elf_name = demangled;
  352. snprintf(sympltname, sizeof(sympltname),
  353. "%s@plt", elf_name);
  354. free(demangled);
  355. f = symbol__new(plt_offset, plt_entry_size,
  356. STB_GLOBAL, STT_FUNC, sympltname);
  357. if (!f)
  358. goto out_elf_end;
  359. plt_offset += plt_entry_size;
  360. symbols__insert(&dso->symbols, f);
  361. ++nr;
  362. }
  363. }
  364. err = 0;
  365. out_elf_end:
  366. if (err == 0)
  367. return nr;
  368. pr_debug("%s: problems reading %s PLT info.\n",
  369. __func__, dso->long_name);
  370. return 0;
  371. }
  372. char *dso__demangle_sym(struct dso *dso, int kmodule, const char *elf_name)
  373. {
  374. return demangle_sym(dso, kmodule, elf_name);
  375. }
  376. /*
  377. * Align offset to 4 bytes as needed for note name and descriptor data.
  378. */
  379. #define NOTE_ALIGN(n) (((n) + 3) & -4U)
  380. static int elf_read_build_id(Elf *elf, void *bf, size_t size)
  381. {
  382. int err = -1;
  383. GElf_Ehdr ehdr;
  384. GElf_Shdr shdr;
  385. Elf_Data *data;
  386. Elf_Scn *sec;
  387. Elf_Kind ek;
  388. void *ptr;
  389. if (size < BUILD_ID_SIZE)
  390. goto out;
  391. ek = elf_kind(elf);
  392. if (ek != ELF_K_ELF)
  393. goto out;
  394. if (gelf_getehdr(elf, &ehdr) == NULL) {
  395. pr_err("%s: cannot get elf header.\n", __func__);
  396. goto out;
  397. }
  398. /*
  399. * Check following sections for notes:
  400. * '.note.gnu.build-id'
  401. * '.notes'
  402. * '.note' (VDSO specific)
  403. */
  404. do {
  405. sec = elf_section_by_name(elf, &ehdr, &shdr,
  406. ".note.gnu.build-id", NULL);
  407. if (sec)
  408. break;
  409. sec = elf_section_by_name(elf, &ehdr, &shdr,
  410. ".notes", NULL);
  411. if (sec)
  412. break;
  413. sec = elf_section_by_name(elf, &ehdr, &shdr,
  414. ".note", NULL);
  415. if (sec)
  416. break;
  417. return err;
  418. } while (0);
  419. data = elf_getdata(sec, NULL);
  420. if (data == NULL)
  421. goto out;
  422. ptr = data->d_buf;
  423. while (ptr < (data->d_buf + data->d_size)) {
  424. GElf_Nhdr *nhdr = ptr;
  425. size_t namesz = NOTE_ALIGN(nhdr->n_namesz),
  426. descsz = NOTE_ALIGN(nhdr->n_descsz);
  427. const char *name;
  428. ptr += sizeof(*nhdr);
  429. name = ptr;
  430. ptr += namesz;
  431. if (nhdr->n_type == NT_GNU_BUILD_ID &&
  432. nhdr->n_namesz == sizeof("GNU")) {
  433. if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
  434. size_t sz = min(size, descsz);
  435. memcpy(bf, ptr, sz);
  436. memset(bf + sz, 0, size - sz);
  437. err = descsz;
  438. break;
  439. }
  440. }
  441. ptr += descsz;
  442. }
  443. out:
  444. return err;
  445. }
  446. #ifdef HAVE_LIBBFD_BUILDID_SUPPORT
  447. int filename__read_build_id(const char *filename, struct build_id *bid)
  448. {
  449. size_t size = sizeof(bid->data);
  450. int err = -1;
  451. bfd *abfd;
  452. abfd = bfd_openr(filename, NULL);
  453. if (!abfd)
  454. return -1;
  455. if (!bfd_check_format(abfd, bfd_object)) {
  456. pr_debug2("%s: cannot read %s bfd file.\n", __func__, filename);
  457. goto out_close;
  458. }
  459. if (!abfd->build_id || abfd->build_id->size > size)
  460. goto out_close;
  461. memcpy(bid->data, abfd->build_id->data, abfd->build_id->size);
  462. memset(bid->data + abfd->build_id->size, 0, size - abfd->build_id->size);
  463. err = bid->size = abfd->build_id->size;
  464. out_close:
  465. bfd_close(abfd);
  466. return err;
  467. }
  468. #else // HAVE_LIBBFD_BUILDID_SUPPORT
  469. int filename__read_build_id(const char *filename, struct build_id *bid)
  470. {
  471. size_t size = sizeof(bid->data);
  472. int fd, err = -1;
  473. Elf *elf;
  474. if (size < BUILD_ID_SIZE)
  475. goto out;
  476. fd = open(filename, O_RDONLY);
  477. if (fd < 0)
  478. goto out;
  479. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  480. if (elf == NULL) {
  481. pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
  482. goto out_close;
  483. }
  484. err = elf_read_build_id(elf, bid->data, size);
  485. if (err > 0)
  486. bid->size = err;
  487. elf_end(elf);
  488. out_close:
  489. close(fd);
  490. out:
  491. return err;
  492. }
  493. #endif // HAVE_LIBBFD_BUILDID_SUPPORT
  494. int sysfs__read_build_id(const char *filename, struct build_id *bid)
  495. {
  496. size_t size = sizeof(bid->data);
  497. int fd, err = -1;
  498. fd = open(filename, O_RDONLY);
  499. if (fd < 0)
  500. goto out;
  501. while (1) {
  502. char bf[BUFSIZ];
  503. GElf_Nhdr nhdr;
  504. size_t namesz, descsz;
  505. if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
  506. break;
  507. namesz = NOTE_ALIGN(nhdr.n_namesz);
  508. descsz = NOTE_ALIGN(nhdr.n_descsz);
  509. if (nhdr.n_type == NT_GNU_BUILD_ID &&
  510. nhdr.n_namesz == sizeof("GNU")) {
  511. if (read(fd, bf, namesz) != (ssize_t)namesz)
  512. break;
  513. if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
  514. size_t sz = min(descsz, size);
  515. if (read(fd, bid->data, sz) == (ssize_t)sz) {
  516. memset(bid->data + sz, 0, size - sz);
  517. bid->size = sz;
  518. err = 0;
  519. break;
  520. }
  521. } else if (read(fd, bf, descsz) != (ssize_t)descsz)
  522. break;
  523. } else {
  524. int n = namesz + descsz;
  525. if (n > (int)sizeof(bf)) {
  526. n = sizeof(bf);
  527. pr_debug("%s: truncating reading of build id in sysfs file %s: n_namesz=%u, n_descsz=%u.\n",
  528. __func__, filename, nhdr.n_namesz, nhdr.n_descsz);
  529. }
  530. if (read(fd, bf, n) != n)
  531. break;
  532. }
  533. }
  534. close(fd);
  535. out:
  536. return err;
  537. }
  538. #ifdef HAVE_LIBBFD_SUPPORT
  539. int filename__read_debuglink(const char *filename, char *debuglink,
  540. size_t size)
  541. {
  542. int err = -1;
  543. asection *section;
  544. bfd *abfd;
  545. abfd = bfd_openr(filename, NULL);
  546. if (!abfd)
  547. return -1;
  548. if (!bfd_check_format(abfd, bfd_object)) {
  549. pr_debug2("%s: cannot read %s bfd file.\n", __func__, filename);
  550. goto out_close;
  551. }
  552. section = bfd_get_section_by_name(abfd, ".gnu_debuglink");
  553. if (!section)
  554. goto out_close;
  555. if (section->size > size)
  556. goto out_close;
  557. if (!bfd_get_section_contents(abfd, section, debuglink, 0,
  558. section->size))
  559. goto out_close;
  560. err = 0;
  561. out_close:
  562. bfd_close(abfd);
  563. return err;
  564. }
  565. #else
  566. int filename__read_debuglink(const char *filename, char *debuglink,
  567. size_t size)
  568. {
  569. int fd, err = -1;
  570. Elf *elf;
  571. GElf_Ehdr ehdr;
  572. GElf_Shdr shdr;
  573. Elf_Data *data;
  574. Elf_Scn *sec;
  575. Elf_Kind ek;
  576. fd = open(filename, O_RDONLY);
  577. if (fd < 0)
  578. goto out;
  579. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  580. if (elf == NULL) {
  581. pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
  582. goto out_close;
  583. }
  584. ek = elf_kind(elf);
  585. if (ek != ELF_K_ELF)
  586. goto out_elf_end;
  587. if (gelf_getehdr(elf, &ehdr) == NULL) {
  588. pr_err("%s: cannot get elf header.\n", __func__);
  589. goto out_elf_end;
  590. }
  591. sec = elf_section_by_name(elf, &ehdr, &shdr,
  592. ".gnu_debuglink", NULL);
  593. if (sec == NULL)
  594. goto out_elf_end;
  595. data = elf_getdata(sec, NULL);
  596. if (data == NULL)
  597. goto out_elf_end;
  598. /* the start of this section is a zero-terminated string */
  599. strncpy(debuglink, data->d_buf, size);
  600. err = 0;
  601. out_elf_end:
  602. elf_end(elf);
  603. out_close:
  604. close(fd);
  605. out:
  606. return err;
  607. }
  608. #endif
  609. static int dso__swap_init(struct dso *dso, unsigned char eidata)
  610. {
  611. static unsigned int const endian = 1;
  612. dso->needs_swap = DSO_SWAP__NO;
  613. switch (eidata) {
  614. case ELFDATA2LSB:
  615. /* We are big endian, DSO is little endian. */
  616. if (*(unsigned char const *)&endian != 1)
  617. dso->needs_swap = DSO_SWAP__YES;
  618. break;
  619. case ELFDATA2MSB:
  620. /* We are little endian, DSO is big endian. */
  621. if (*(unsigned char const *)&endian != 0)
  622. dso->needs_swap = DSO_SWAP__YES;
  623. break;
  624. default:
  625. pr_err("unrecognized DSO data encoding %d\n", eidata);
  626. return -EINVAL;
  627. }
  628. return 0;
  629. }
  630. bool symsrc__possibly_runtime(struct symsrc *ss)
  631. {
  632. return ss->dynsym || ss->opdsec;
  633. }
  634. bool symsrc__has_symtab(struct symsrc *ss)
  635. {
  636. return ss->symtab != NULL;
  637. }
  638. void symsrc__destroy(struct symsrc *ss)
  639. {
  640. zfree(&ss->name);
  641. elf_end(ss->elf);
  642. close(ss->fd);
  643. }
  644. bool elf__needs_adjust_symbols(GElf_Ehdr ehdr)
  645. {
  646. /*
  647. * Usually vmlinux is an ELF file with type ET_EXEC for most
  648. * architectures; except Arm64 kernel is linked with option
  649. * '-share', so need to check type ET_DYN.
  650. */
  651. return ehdr.e_type == ET_EXEC || ehdr.e_type == ET_REL ||
  652. ehdr.e_type == ET_DYN;
  653. }
  654. int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name,
  655. enum dso_binary_type type)
  656. {
  657. GElf_Ehdr ehdr;
  658. Elf *elf;
  659. int fd;
  660. if (dso__needs_decompress(dso)) {
  661. fd = dso__decompress_kmodule_fd(dso, name);
  662. if (fd < 0)
  663. return -1;
  664. type = dso->symtab_type;
  665. } else {
  666. fd = open(name, O_RDONLY);
  667. if (fd < 0) {
  668. dso->load_errno = errno;
  669. return -1;
  670. }
  671. }
  672. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  673. if (elf == NULL) {
  674. pr_debug("%s: cannot read %s ELF file.\n", __func__, name);
  675. dso->load_errno = DSO_LOAD_ERRNO__INVALID_ELF;
  676. goto out_close;
  677. }
  678. if (gelf_getehdr(elf, &ehdr) == NULL) {
  679. dso->load_errno = DSO_LOAD_ERRNO__INVALID_ELF;
  680. pr_debug("%s: cannot get elf header.\n", __func__);
  681. goto out_elf_end;
  682. }
  683. if (dso__swap_init(dso, ehdr.e_ident[EI_DATA])) {
  684. dso->load_errno = DSO_LOAD_ERRNO__INTERNAL_ERROR;
  685. goto out_elf_end;
  686. }
  687. /* Always reject images with a mismatched build-id: */
  688. if (dso->has_build_id && !symbol_conf.ignore_vmlinux_buildid) {
  689. u8 build_id[BUILD_ID_SIZE];
  690. struct build_id bid;
  691. int size;
  692. size = elf_read_build_id(elf, build_id, BUILD_ID_SIZE);
  693. if (size <= 0) {
  694. dso->load_errno = DSO_LOAD_ERRNO__CANNOT_READ_BUILDID;
  695. goto out_elf_end;
  696. }
  697. build_id__init(&bid, build_id, size);
  698. if (!dso__build_id_equal(dso, &bid)) {
  699. pr_debug("%s: build id mismatch for %s.\n", __func__, name);
  700. dso->load_errno = DSO_LOAD_ERRNO__MISMATCHING_BUILDID;
  701. goto out_elf_end;
  702. }
  703. }
  704. ss->is_64_bit = (gelf_getclass(elf) == ELFCLASS64);
  705. ss->symtab = elf_section_by_name(elf, &ehdr, &ss->symshdr, ".symtab",
  706. NULL);
  707. if (ss->symshdr.sh_type != SHT_SYMTAB)
  708. ss->symtab = NULL;
  709. ss->dynsym_idx = 0;
  710. ss->dynsym = elf_section_by_name(elf, &ehdr, &ss->dynshdr, ".dynsym",
  711. &ss->dynsym_idx);
  712. if (ss->dynshdr.sh_type != SHT_DYNSYM)
  713. ss->dynsym = NULL;
  714. ss->opdidx = 0;
  715. ss->opdsec = elf_section_by_name(elf, &ehdr, &ss->opdshdr, ".opd",
  716. &ss->opdidx);
  717. if (ss->opdshdr.sh_type != SHT_PROGBITS)
  718. ss->opdsec = NULL;
  719. if (dso->kernel == DSO_SPACE__USER)
  720. ss->adjust_symbols = true;
  721. else
  722. ss->adjust_symbols = elf__needs_adjust_symbols(ehdr);
  723. ss->name = strdup(name);
  724. if (!ss->name) {
  725. dso->load_errno = errno;
  726. goto out_elf_end;
  727. }
  728. ss->elf = elf;
  729. ss->fd = fd;
  730. ss->ehdr = ehdr;
  731. ss->type = type;
  732. return 0;
  733. out_elf_end:
  734. elf_end(elf);
  735. out_close:
  736. close(fd);
  737. return -1;
  738. }
  739. /**
  740. * ref_reloc_sym_not_found - has kernel relocation symbol been found.
  741. * @kmap: kernel maps and relocation reference symbol
  742. *
  743. * This function returns %true if we are dealing with the kernel maps and the
  744. * relocation reference symbol has not yet been found. Otherwise %false is
  745. * returned.
  746. */
  747. static bool ref_reloc_sym_not_found(struct kmap *kmap)
  748. {
  749. return kmap && kmap->ref_reloc_sym && kmap->ref_reloc_sym->name &&
  750. !kmap->ref_reloc_sym->unrelocated_addr;
  751. }
  752. /**
  753. * ref_reloc - kernel relocation offset.
  754. * @kmap: kernel maps and relocation reference symbol
  755. *
  756. * This function returns the offset of kernel addresses as determined by using
  757. * the relocation reference symbol i.e. if the kernel has not been relocated
  758. * then the return value is zero.
  759. */
  760. static u64 ref_reloc(struct kmap *kmap)
  761. {
  762. if (kmap && kmap->ref_reloc_sym &&
  763. kmap->ref_reloc_sym->unrelocated_addr)
  764. return kmap->ref_reloc_sym->addr -
  765. kmap->ref_reloc_sym->unrelocated_addr;
  766. return 0;
  767. }
  768. void __weak arch__sym_update(struct symbol *s __maybe_unused,
  769. GElf_Sym *sym __maybe_unused) { }
  770. static int dso__process_kernel_symbol(struct dso *dso, struct map *map,
  771. GElf_Sym *sym, GElf_Shdr *shdr,
  772. struct maps *kmaps, struct kmap *kmap,
  773. struct dso **curr_dsop, struct map **curr_mapp,
  774. const char *section_name,
  775. bool adjust_kernel_syms, bool kmodule, bool *remap_kernel)
  776. {
  777. struct dso *curr_dso = *curr_dsop;
  778. struct map *curr_map;
  779. char dso_name[PATH_MAX];
  780. /* Adjust symbol to map to file offset */
  781. if (adjust_kernel_syms)
  782. sym->st_value -= shdr->sh_addr - shdr->sh_offset;
  783. if (strcmp(section_name, (curr_dso->short_name + dso->short_name_len)) == 0)
  784. return 0;
  785. if (strcmp(section_name, ".text") == 0) {
  786. /*
  787. * The initial kernel mapping is based on
  788. * kallsyms and identity maps. Overwrite it to
  789. * map to the kernel dso.
  790. */
  791. if (*remap_kernel && dso->kernel && !kmodule) {
  792. *remap_kernel = false;
  793. map->start = shdr->sh_addr + ref_reloc(kmap);
  794. map->end = map->start + shdr->sh_size;
  795. map->pgoff = shdr->sh_offset;
  796. map->map_ip = map__map_ip;
  797. map->unmap_ip = map__unmap_ip;
  798. /* Ensure maps are correctly ordered */
  799. if (kmaps) {
  800. map__get(map);
  801. maps__remove(kmaps, map);
  802. maps__insert(kmaps, map);
  803. map__put(map);
  804. }
  805. }
  806. /*
  807. * The initial module mapping is based on
  808. * /proc/modules mapped to offset zero.
  809. * Overwrite it to map to the module dso.
  810. */
  811. if (*remap_kernel && kmodule) {
  812. *remap_kernel = false;
  813. map->pgoff = shdr->sh_offset;
  814. }
  815. *curr_mapp = map;
  816. *curr_dsop = dso;
  817. return 0;
  818. }
  819. if (!kmap)
  820. return 0;
  821. snprintf(dso_name, sizeof(dso_name), "%s%s", dso->short_name, section_name);
  822. curr_map = maps__find_by_name(kmaps, dso_name);
  823. if (curr_map == NULL) {
  824. u64 start = sym->st_value;
  825. if (kmodule)
  826. start += map->start + shdr->sh_offset;
  827. curr_dso = dso__new(dso_name);
  828. if (curr_dso == NULL)
  829. return -1;
  830. curr_dso->kernel = dso->kernel;
  831. curr_dso->long_name = dso->long_name;
  832. curr_dso->long_name_len = dso->long_name_len;
  833. curr_map = map__new2(start, curr_dso);
  834. dso__put(curr_dso);
  835. if (curr_map == NULL)
  836. return -1;
  837. if (curr_dso->kernel)
  838. map__kmap(curr_map)->kmaps = kmaps;
  839. if (adjust_kernel_syms) {
  840. curr_map->start = shdr->sh_addr + ref_reloc(kmap);
  841. curr_map->end = curr_map->start + shdr->sh_size;
  842. curr_map->pgoff = shdr->sh_offset;
  843. } else {
  844. curr_map->map_ip = curr_map->unmap_ip = identity__map_ip;
  845. }
  846. curr_dso->symtab_type = dso->symtab_type;
  847. maps__insert(kmaps, curr_map);
  848. /*
  849. * Add it before we drop the referece to curr_map, i.e. while
  850. * we still are sure to have a reference to this DSO via
  851. * *curr_map->dso.
  852. */
  853. dsos__add(&kmaps->machine->dsos, curr_dso);
  854. /* kmaps already got it */
  855. map__put(curr_map);
  856. dso__set_loaded(curr_dso);
  857. *curr_mapp = curr_map;
  858. *curr_dsop = curr_dso;
  859. } else
  860. *curr_dsop = curr_map->dso;
  861. return 0;
  862. }
  863. int dso__load_sym(struct dso *dso, struct map *map, struct symsrc *syms_ss,
  864. struct symsrc *runtime_ss, int kmodule)
  865. {
  866. struct kmap *kmap = dso->kernel ? map__kmap(map) : NULL;
  867. struct maps *kmaps = kmap ? map__kmaps(map) : NULL;
  868. struct map *curr_map = map;
  869. struct dso *curr_dso = dso;
  870. Elf_Data *symstrs, *secstrs;
  871. uint32_t nr_syms;
  872. int err = -1;
  873. uint32_t idx;
  874. GElf_Ehdr ehdr;
  875. GElf_Shdr shdr;
  876. GElf_Shdr tshdr;
  877. Elf_Data *syms, *opddata = NULL;
  878. GElf_Sym sym;
  879. Elf_Scn *sec, *sec_strndx;
  880. Elf *elf;
  881. int nr = 0;
  882. bool remap_kernel = false, adjust_kernel_syms = false;
  883. if (kmap && !kmaps)
  884. return -1;
  885. dso->symtab_type = syms_ss->type;
  886. dso->is_64_bit = syms_ss->is_64_bit;
  887. dso->rel = syms_ss->ehdr.e_type == ET_REL;
  888. /*
  889. * Modules may already have symbols from kallsyms, but those symbols
  890. * have the wrong values for the dso maps, so remove them.
  891. */
  892. if (kmodule && syms_ss->symtab)
  893. symbols__delete(&dso->symbols);
  894. if (!syms_ss->symtab) {
  895. /*
  896. * If the vmlinux is stripped, fail so we will fall back
  897. * to using kallsyms. The vmlinux runtime symbols aren't
  898. * of much use.
  899. */
  900. if (dso->kernel)
  901. goto out_elf_end;
  902. syms_ss->symtab = syms_ss->dynsym;
  903. syms_ss->symshdr = syms_ss->dynshdr;
  904. }
  905. elf = syms_ss->elf;
  906. ehdr = syms_ss->ehdr;
  907. sec = syms_ss->symtab;
  908. shdr = syms_ss->symshdr;
  909. if (elf_section_by_name(runtime_ss->elf, &runtime_ss->ehdr, &tshdr,
  910. ".text", NULL))
  911. dso->text_offset = tshdr.sh_addr - tshdr.sh_offset;
  912. if (runtime_ss->opdsec)
  913. opddata = elf_rawdata(runtime_ss->opdsec, NULL);
  914. syms = elf_getdata(sec, NULL);
  915. if (syms == NULL)
  916. goto out_elf_end;
  917. sec = elf_getscn(elf, shdr.sh_link);
  918. if (sec == NULL)
  919. goto out_elf_end;
  920. symstrs = elf_getdata(sec, NULL);
  921. if (symstrs == NULL)
  922. goto out_elf_end;
  923. sec_strndx = elf_getscn(runtime_ss->elf, runtime_ss->ehdr.e_shstrndx);
  924. if (sec_strndx == NULL)
  925. goto out_elf_end;
  926. secstrs = elf_getdata(sec_strndx, NULL);
  927. if (secstrs == NULL)
  928. goto out_elf_end;
  929. nr_syms = shdr.sh_size / shdr.sh_entsize;
  930. memset(&sym, 0, sizeof(sym));
  931. /*
  932. * The kernel relocation symbol is needed in advance in order to adjust
  933. * kernel maps correctly.
  934. */
  935. if (ref_reloc_sym_not_found(kmap)) {
  936. elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
  937. const char *elf_name = elf_sym__name(&sym, symstrs);
  938. if (strcmp(elf_name, kmap->ref_reloc_sym->name))
  939. continue;
  940. kmap->ref_reloc_sym->unrelocated_addr = sym.st_value;
  941. map->reloc = kmap->ref_reloc_sym->addr -
  942. kmap->ref_reloc_sym->unrelocated_addr;
  943. break;
  944. }
  945. }
  946. /*
  947. * Handle any relocation of vdso necessary because older kernels
  948. * attempted to prelink vdso to its virtual address.
  949. */
  950. if (dso__is_vdso(dso))
  951. map->reloc = map->start - dso->text_offset;
  952. dso->adjust_symbols = runtime_ss->adjust_symbols || ref_reloc(kmap);
  953. /*
  954. * Initial kernel and module mappings do not map to the dso.
  955. * Flag the fixups.
  956. */
  957. if (dso->kernel) {
  958. remap_kernel = true;
  959. adjust_kernel_syms = dso->adjust_symbols;
  960. }
  961. elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
  962. struct symbol *f;
  963. const char *elf_name = elf_sym__name(&sym, symstrs);
  964. char *demangled = NULL;
  965. int is_label = elf_sym__is_label(&sym);
  966. const char *section_name;
  967. bool used_opd = false;
  968. if (!is_label && !elf_sym__filter(&sym))
  969. continue;
  970. /* Reject ARM ELF "mapping symbols": these aren't unique and
  971. * don't identify functions, so will confuse the profile
  972. * output: */
  973. if (ehdr.e_machine == EM_ARM || ehdr.e_machine == EM_AARCH64) {
  974. if (elf_name[0] == '$' && strchr("adtx", elf_name[1])
  975. && (elf_name[2] == '\0' || elf_name[2] == '.'))
  976. continue;
  977. }
  978. if (runtime_ss->opdsec && sym.st_shndx == runtime_ss->opdidx) {
  979. u32 offset = sym.st_value - syms_ss->opdshdr.sh_addr;
  980. u64 *opd = opddata->d_buf + offset;
  981. sym.st_value = DSO__SWAP(dso, u64, *opd);
  982. sym.st_shndx = elf_addr_to_index(runtime_ss->elf,
  983. sym.st_value);
  984. used_opd = true;
  985. }
  986. /*
  987. * When loading symbols in a data mapping, ABS symbols (which
  988. * has a value of SHN_ABS in its st_shndx) failed at
  989. * elf_getscn(). And it marks the loading as a failure so
  990. * already loaded symbols cannot be fixed up.
  991. *
  992. * I'm not sure what should be done. Just ignore them for now.
  993. * - Namhyung Kim
  994. */
  995. if (sym.st_shndx == SHN_ABS)
  996. continue;
  997. sec = elf_getscn(runtime_ss->elf, sym.st_shndx);
  998. if (!sec)
  999. goto out_elf_end;
  1000. gelf_getshdr(sec, &shdr);
  1001. if (is_label && !elf_sec__filter(&shdr, secstrs))
  1002. continue;
  1003. section_name = elf_sec__name(&shdr, secstrs);
  1004. /* On ARM, symbols for thumb functions have 1 added to
  1005. * the symbol address as a flag - remove it */
  1006. if ((ehdr.e_machine == EM_ARM) &&
  1007. (GELF_ST_TYPE(sym.st_info) == STT_FUNC) &&
  1008. (sym.st_value & 1))
  1009. --sym.st_value;
  1010. if (dso->kernel) {
  1011. if (dso__process_kernel_symbol(dso, map, &sym, &shdr, kmaps, kmap, &curr_dso, &curr_map,
  1012. section_name, adjust_kernel_syms, kmodule, &remap_kernel))
  1013. goto out_elf_end;
  1014. } else if ((used_opd && runtime_ss->adjust_symbols) ||
  1015. (!used_opd && syms_ss->adjust_symbols)) {
  1016. pr_debug4("%s: adjusting symbol: st_value: %#" PRIx64 " "
  1017. "sh_addr: %#" PRIx64 " sh_offset: %#" PRIx64 "\n", __func__,
  1018. (u64)sym.st_value, (u64)shdr.sh_addr,
  1019. (u64)shdr.sh_offset);
  1020. sym.st_value -= shdr.sh_addr - shdr.sh_offset;
  1021. }
  1022. demangled = demangle_sym(dso, kmodule, elf_name);
  1023. if (demangled != NULL)
  1024. elf_name = demangled;
  1025. f = symbol__new(sym.st_value, sym.st_size,
  1026. GELF_ST_BIND(sym.st_info),
  1027. GELF_ST_TYPE(sym.st_info), elf_name);
  1028. free(demangled);
  1029. if (!f)
  1030. goto out_elf_end;
  1031. arch__sym_update(f, &sym);
  1032. __symbols__insert(&curr_dso->symbols, f, dso->kernel);
  1033. nr++;
  1034. }
  1035. /*
  1036. * For misannotated, zeroed, ASM function sizes.
  1037. */
  1038. if (nr > 0) {
  1039. symbols__fixup_end(&dso->symbols);
  1040. symbols__fixup_duplicate(&dso->symbols);
  1041. if (kmap) {
  1042. /*
  1043. * We need to fixup this here too because we create new
  1044. * maps here, for things like vsyscall sections.
  1045. */
  1046. maps__fixup_end(kmaps);
  1047. }
  1048. }
  1049. err = nr;
  1050. out_elf_end:
  1051. return err;
  1052. }
  1053. static int elf_read_maps(Elf *elf, bool exe, mapfn_t mapfn, void *data)
  1054. {
  1055. GElf_Phdr phdr;
  1056. size_t i, phdrnum;
  1057. int err;
  1058. u64 sz;
  1059. if (elf_getphdrnum(elf, &phdrnum))
  1060. return -1;
  1061. for (i = 0; i < phdrnum; i++) {
  1062. if (gelf_getphdr(elf, i, &phdr) == NULL)
  1063. return -1;
  1064. if (phdr.p_type != PT_LOAD)
  1065. continue;
  1066. if (exe) {
  1067. if (!(phdr.p_flags & PF_X))
  1068. continue;
  1069. } else {
  1070. if (!(phdr.p_flags & PF_R))
  1071. continue;
  1072. }
  1073. sz = min(phdr.p_memsz, phdr.p_filesz);
  1074. if (!sz)
  1075. continue;
  1076. err = mapfn(phdr.p_vaddr, sz, phdr.p_offset, data);
  1077. if (err)
  1078. return err;
  1079. }
  1080. return 0;
  1081. }
  1082. int file__read_maps(int fd, bool exe, mapfn_t mapfn, void *data,
  1083. bool *is_64_bit)
  1084. {
  1085. int err;
  1086. Elf *elf;
  1087. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  1088. if (elf == NULL)
  1089. return -1;
  1090. if (is_64_bit)
  1091. *is_64_bit = (gelf_getclass(elf) == ELFCLASS64);
  1092. err = elf_read_maps(elf, exe, mapfn, data);
  1093. elf_end(elf);
  1094. return err;
  1095. }
  1096. enum dso_type dso__type_fd(int fd)
  1097. {
  1098. enum dso_type dso_type = DSO__TYPE_UNKNOWN;
  1099. GElf_Ehdr ehdr;
  1100. Elf_Kind ek;
  1101. Elf *elf;
  1102. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  1103. if (elf == NULL)
  1104. goto out;
  1105. ek = elf_kind(elf);
  1106. if (ek != ELF_K_ELF)
  1107. goto out_end;
  1108. if (gelf_getclass(elf) == ELFCLASS64) {
  1109. dso_type = DSO__TYPE_64BIT;
  1110. goto out_end;
  1111. }
  1112. if (gelf_getehdr(elf, &ehdr) == NULL)
  1113. goto out_end;
  1114. if (ehdr.e_machine == EM_X86_64)
  1115. dso_type = DSO__TYPE_X32BIT;
  1116. else
  1117. dso_type = DSO__TYPE_32BIT;
  1118. out_end:
  1119. elf_end(elf);
  1120. out:
  1121. return dso_type;
  1122. }
  1123. static int copy_bytes(int from, off_t from_offs, int to, off_t to_offs, u64 len)
  1124. {
  1125. ssize_t r;
  1126. size_t n;
  1127. int err = -1;
  1128. char *buf = malloc(page_size);
  1129. if (buf == NULL)
  1130. return -1;
  1131. if (lseek(to, to_offs, SEEK_SET) != to_offs)
  1132. goto out;
  1133. if (lseek(from, from_offs, SEEK_SET) != from_offs)
  1134. goto out;
  1135. while (len) {
  1136. n = page_size;
  1137. if (len < n)
  1138. n = len;
  1139. /* Use read because mmap won't work on proc files */
  1140. r = read(from, buf, n);
  1141. if (r < 0)
  1142. goto out;
  1143. if (!r)
  1144. break;
  1145. n = r;
  1146. r = write(to, buf, n);
  1147. if (r < 0)
  1148. goto out;
  1149. if ((size_t)r != n)
  1150. goto out;
  1151. len -= n;
  1152. }
  1153. err = 0;
  1154. out:
  1155. free(buf);
  1156. return err;
  1157. }
  1158. struct kcore {
  1159. int fd;
  1160. int elfclass;
  1161. Elf *elf;
  1162. GElf_Ehdr ehdr;
  1163. };
  1164. static int kcore__open(struct kcore *kcore, const char *filename)
  1165. {
  1166. GElf_Ehdr *ehdr;
  1167. kcore->fd = open(filename, O_RDONLY);
  1168. if (kcore->fd == -1)
  1169. return -1;
  1170. kcore->elf = elf_begin(kcore->fd, ELF_C_READ, NULL);
  1171. if (!kcore->elf)
  1172. goto out_close;
  1173. kcore->elfclass = gelf_getclass(kcore->elf);
  1174. if (kcore->elfclass == ELFCLASSNONE)
  1175. goto out_end;
  1176. ehdr = gelf_getehdr(kcore->elf, &kcore->ehdr);
  1177. if (!ehdr)
  1178. goto out_end;
  1179. return 0;
  1180. out_end:
  1181. elf_end(kcore->elf);
  1182. out_close:
  1183. close(kcore->fd);
  1184. return -1;
  1185. }
  1186. static int kcore__init(struct kcore *kcore, char *filename, int elfclass,
  1187. bool temp)
  1188. {
  1189. kcore->elfclass = elfclass;
  1190. if (temp)
  1191. kcore->fd = mkstemp(filename);
  1192. else
  1193. kcore->fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0400);
  1194. if (kcore->fd == -1)
  1195. return -1;
  1196. kcore->elf = elf_begin(kcore->fd, ELF_C_WRITE, NULL);
  1197. if (!kcore->elf)
  1198. goto out_close;
  1199. if (!gelf_newehdr(kcore->elf, elfclass))
  1200. goto out_end;
  1201. memset(&kcore->ehdr, 0, sizeof(GElf_Ehdr));
  1202. return 0;
  1203. out_end:
  1204. elf_end(kcore->elf);
  1205. out_close:
  1206. close(kcore->fd);
  1207. unlink(filename);
  1208. return -1;
  1209. }
  1210. static void kcore__close(struct kcore *kcore)
  1211. {
  1212. elf_end(kcore->elf);
  1213. close(kcore->fd);
  1214. }
  1215. static int kcore__copy_hdr(struct kcore *from, struct kcore *to, size_t count)
  1216. {
  1217. GElf_Ehdr *ehdr = &to->ehdr;
  1218. GElf_Ehdr *kehdr = &from->ehdr;
  1219. memcpy(ehdr->e_ident, kehdr->e_ident, EI_NIDENT);
  1220. ehdr->e_type = kehdr->e_type;
  1221. ehdr->e_machine = kehdr->e_machine;
  1222. ehdr->e_version = kehdr->e_version;
  1223. ehdr->e_entry = 0;
  1224. ehdr->e_shoff = 0;
  1225. ehdr->e_flags = kehdr->e_flags;
  1226. ehdr->e_phnum = count;
  1227. ehdr->e_shentsize = 0;
  1228. ehdr->e_shnum = 0;
  1229. ehdr->e_shstrndx = 0;
  1230. if (from->elfclass == ELFCLASS32) {
  1231. ehdr->e_phoff = sizeof(Elf32_Ehdr);
  1232. ehdr->e_ehsize = sizeof(Elf32_Ehdr);
  1233. ehdr->e_phentsize = sizeof(Elf32_Phdr);
  1234. } else {
  1235. ehdr->e_phoff = sizeof(Elf64_Ehdr);
  1236. ehdr->e_ehsize = sizeof(Elf64_Ehdr);
  1237. ehdr->e_phentsize = sizeof(Elf64_Phdr);
  1238. }
  1239. if (!gelf_update_ehdr(to->elf, ehdr))
  1240. return -1;
  1241. if (!gelf_newphdr(to->elf, count))
  1242. return -1;
  1243. return 0;
  1244. }
  1245. static int kcore__add_phdr(struct kcore *kcore, int idx, off_t offset,
  1246. u64 addr, u64 len)
  1247. {
  1248. GElf_Phdr phdr = {
  1249. .p_type = PT_LOAD,
  1250. .p_flags = PF_R | PF_W | PF_X,
  1251. .p_offset = offset,
  1252. .p_vaddr = addr,
  1253. .p_paddr = 0,
  1254. .p_filesz = len,
  1255. .p_memsz = len,
  1256. .p_align = page_size,
  1257. };
  1258. if (!gelf_update_phdr(kcore->elf, idx, &phdr))
  1259. return -1;
  1260. return 0;
  1261. }
  1262. static off_t kcore__write(struct kcore *kcore)
  1263. {
  1264. return elf_update(kcore->elf, ELF_C_WRITE);
  1265. }
  1266. struct phdr_data {
  1267. off_t offset;
  1268. off_t rel;
  1269. u64 addr;
  1270. u64 len;
  1271. struct list_head node;
  1272. struct phdr_data *remaps;
  1273. };
  1274. struct sym_data {
  1275. u64 addr;
  1276. struct list_head node;
  1277. };
  1278. struct kcore_copy_info {
  1279. u64 stext;
  1280. u64 etext;
  1281. u64 first_symbol;
  1282. u64 last_symbol;
  1283. u64 first_module;
  1284. u64 first_module_symbol;
  1285. u64 last_module_symbol;
  1286. size_t phnum;
  1287. struct list_head phdrs;
  1288. struct list_head syms;
  1289. };
  1290. #define kcore_copy__for_each_phdr(k, p) \
  1291. list_for_each_entry((p), &(k)->phdrs, node)
  1292. static struct phdr_data *phdr_data__new(u64 addr, u64 len, off_t offset)
  1293. {
  1294. struct phdr_data *p = zalloc(sizeof(*p));
  1295. if (p) {
  1296. p->addr = addr;
  1297. p->len = len;
  1298. p->offset = offset;
  1299. }
  1300. return p;
  1301. }
  1302. static struct phdr_data *kcore_copy_info__addnew(struct kcore_copy_info *kci,
  1303. u64 addr, u64 len,
  1304. off_t offset)
  1305. {
  1306. struct phdr_data *p = phdr_data__new(addr, len, offset);
  1307. if (p)
  1308. list_add_tail(&p->node, &kci->phdrs);
  1309. return p;
  1310. }
  1311. static void kcore_copy__free_phdrs(struct kcore_copy_info *kci)
  1312. {
  1313. struct phdr_data *p, *tmp;
  1314. list_for_each_entry_safe(p, tmp, &kci->phdrs, node) {
  1315. list_del_init(&p->node);
  1316. free(p);
  1317. }
  1318. }
  1319. static struct sym_data *kcore_copy__new_sym(struct kcore_copy_info *kci,
  1320. u64 addr)
  1321. {
  1322. struct sym_data *s = zalloc(sizeof(*s));
  1323. if (s) {
  1324. s->addr = addr;
  1325. list_add_tail(&s->node, &kci->syms);
  1326. }
  1327. return s;
  1328. }
  1329. static void kcore_copy__free_syms(struct kcore_copy_info *kci)
  1330. {
  1331. struct sym_data *s, *tmp;
  1332. list_for_each_entry_safe(s, tmp, &kci->syms, node) {
  1333. list_del_init(&s->node);
  1334. free(s);
  1335. }
  1336. }
  1337. static int kcore_copy__process_kallsyms(void *arg, const char *name, char type,
  1338. u64 start)
  1339. {
  1340. struct kcore_copy_info *kci = arg;
  1341. if (!kallsyms__is_function(type))
  1342. return 0;
  1343. if (strchr(name, '[')) {
  1344. if (!kci->first_module_symbol || start < kci->first_module_symbol)
  1345. kci->first_module_symbol = start;
  1346. if (start > kci->last_module_symbol)
  1347. kci->last_module_symbol = start;
  1348. return 0;
  1349. }
  1350. if (!kci->first_symbol || start < kci->first_symbol)
  1351. kci->first_symbol = start;
  1352. if (!kci->last_symbol || start > kci->last_symbol)
  1353. kci->last_symbol = start;
  1354. if (!strcmp(name, "_stext")) {
  1355. kci->stext = start;
  1356. return 0;
  1357. }
  1358. if (!strcmp(name, "_etext")) {
  1359. kci->etext = start;
  1360. return 0;
  1361. }
  1362. if (is_entry_trampoline(name) && !kcore_copy__new_sym(kci, start))
  1363. return -1;
  1364. return 0;
  1365. }
  1366. static int kcore_copy__parse_kallsyms(struct kcore_copy_info *kci,
  1367. const char *dir)
  1368. {
  1369. char kallsyms_filename[PATH_MAX];
  1370. scnprintf(kallsyms_filename, PATH_MAX, "%s/kallsyms", dir);
  1371. if (symbol__restricted_filename(kallsyms_filename, "/proc/kallsyms"))
  1372. return -1;
  1373. if (kallsyms__parse(kallsyms_filename, kci,
  1374. kcore_copy__process_kallsyms) < 0)
  1375. return -1;
  1376. return 0;
  1377. }
  1378. static int kcore_copy__process_modules(void *arg,
  1379. const char *name __maybe_unused,
  1380. u64 start, u64 size __maybe_unused)
  1381. {
  1382. struct kcore_copy_info *kci = arg;
  1383. if (!kci->first_module || start < kci->first_module)
  1384. kci->first_module = start;
  1385. return 0;
  1386. }
  1387. static int kcore_copy__parse_modules(struct kcore_copy_info *kci,
  1388. const char *dir)
  1389. {
  1390. char modules_filename[PATH_MAX];
  1391. scnprintf(modules_filename, PATH_MAX, "%s/modules", dir);
  1392. if (symbol__restricted_filename(modules_filename, "/proc/modules"))
  1393. return -1;
  1394. if (modules__parse(modules_filename, kci,
  1395. kcore_copy__process_modules) < 0)
  1396. return -1;
  1397. return 0;
  1398. }
  1399. static int kcore_copy__map(struct kcore_copy_info *kci, u64 start, u64 end,
  1400. u64 pgoff, u64 s, u64 e)
  1401. {
  1402. u64 len, offset;
  1403. if (s < start || s >= end)
  1404. return 0;
  1405. offset = (s - start) + pgoff;
  1406. len = e < end ? e - s : end - s;
  1407. return kcore_copy_info__addnew(kci, s, len, offset) ? 0 : -1;
  1408. }
  1409. static int kcore_copy__read_map(u64 start, u64 len, u64 pgoff, void *data)
  1410. {
  1411. struct kcore_copy_info *kci = data;
  1412. u64 end = start + len;
  1413. struct sym_data *sdat;
  1414. if (kcore_copy__map(kci, start, end, pgoff, kci->stext, kci->etext))
  1415. return -1;
  1416. if (kcore_copy__map(kci, start, end, pgoff, kci->first_module,
  1417. kci->last_module_symbol))
  1418. return -1;
  1419. list_for_each_entry(sdat, &kci->syms, node) {
  1420. u64 s = round_down(sdat->addr, page_size);
  1421. if (kcore_copy__map(kci, start, end, pgoff, s, s + len))
  1422. return -1;
  1423. }
  1424. return 0;
  1425. }
  1426. static int kcore_copy__read_maps(struct kcore_copy_info *kci, Elf *elf)
  1427. {
  1428. if (elf_read_maps(elf, true, kcore_copy__read_map, kci) < 0)
  1429. return -1;
  1430. return 0;
  1431. }
  1432. static void kcore_copy__find_remaps(struct kcore_copy_info *kci)
  1433. {
  1434. struct phdr_data *p, *k = NULL;
  1435. u64 kend;
  1436. if (!kci->stext)
  1437. return;
  1438. /* Find phdr that corresponds to the kernel map (contains stext) */
  1439. kcore_copy__for_each_phdr(kci, p) {
  1440. u64 pend = p->addr + p->len - 1;
  1441. if (p->addr <= kci->stext && pend >= kci->stext) {
  1442. k = p;
  1443. break;
  1444. }
  1445. }
  1446. if (!k)
  1447. return;
  1448. kend = k->offset + k->len;
  1449. /* Find phdrs that remap the kernel */
  1450. kcore_copy__for_each_phdr(kci, p) {
  1451. u64 pend = p->offset + p->len;
  1452. if (p == k)
  1453. continue;
  1454. if (p->offset >= k->offset && pend <= kend)
  1455. p->remaps = k;
  1456. }
  1457. }
  1458. static void kcore_copy__layout(struct kcore_copy_info *kci)
  1459. {
  1460. struct phdr_data *p;
  1461. off_t rel = 0;
  1462. kcore_copy__find_remaps(kci);
  1463. kcore_copy__for_each_phdr(kci, p) {
  1464. if (!p->remaps) {
  1465. p->rel = rel;
  1466. rel += p->len;
  1467. }
  1468. kci->phnum += 1;
  1469. }
  1470. kcore_copy__for_each_phdr(kci, p) {
  1471. struct phdr_data *k = p->remaps;
  1472. if (k)
  1473. p->rel = p->offset - k->offset + k->rel;
  1474. }
  1475. }
  1476. static int kcore_copy__calc_maps(struct kcore_copy_info *kci, const char *dir,
  1477. Elf *elf)
  1478. {
  1479. if (kcore_copy__parse_kallsyms(kci, dir))
  1480. return -1;
  1481. if (kcore_copy__parse_modules(kci, dir))
  1482. return -1;
  1483. if (kci->stext)
  1484. kci->stext = round_down(kci->stext, page_size);
  1485. else
  1486. kci->stext = round_down(kci->first_symbol, page_size);
  1487. if (kci->etext) {
  1488. kci->etext = round_up(kci->etext, page_size);
  1489. } else if (kci->last_symbol) {
  1490. kci->etext = round_up(kci->last_symbol, page_size);
  1491. kci->etext += page_size;
  1492. }
  1493. if (kci->first_module_symbol &&
  1494. (!kci->first_module || kci->first_module_symbol < kci->first_module))
  1495. kci->first_module = kci->first_module_symbol;
  1496. kci->first_module = round_down(kci->first_module, page_size);
  1497. if (kci->last_module_symbol) {
  1498. kci->last_module_symbol = round_up(kci->last_module_symbol,
  1499. page_size);
  1500. kci->last_module_symbol += page_size;
  1501. }
  1502. if (!kci->stext || !kci->etext)
  1503. return -1;
  1504. if (kci->first_module && !kci->last_module_symbol)
  1505. return -1;
  1506. if (kcore_copy__read_maps(kci, elf))
  1507. return -1;
  1508. kcore_copy__layout(kci);
  1509. return 0;
  1510. }
  1511. static int kcore_copy__copy_file(const char *from_dir, const char *to_dir,
  1512. const char *name)
  1513. {
  1514. char from_filename[PATH_MAX];
  1515. char to_filename[PATH_MAX];
  1516. scnprintf(from_filename, PATH_MAX, "%s/%s", from_dir, name);
  1517. scnprintf(to_filename, PATH_MAX, "%s/%s", to_dir, name);
  1518. return copyfile_mode(from_filename, to_filename, 0400);
  1519. }
  1520. static int kcore_copy__unlink(const char *dir, const char *name)
  1521. {
  1522. char filename[PATH_MAX];
  1523. scnprintf(filename, PATH_MAX, "%s/%s", dir, name);
  1524. return unlink(filename);
  1525. }
  1526. static int kcore_copy__compare_fds(int from, int to)
  1527. {
  1528. char *buf_from;
  1529. char *buf_to;
  1530. ssize_t ret;
  1531. size_t len;
  1532. int err = -1;
  1533. buf_from = malloc(page_size);
  1534. buf_to = malloc(page_size);
  1535. if (!buf_from || !buf_to)
  1536. goto out;
  1537. while (1) {
  1538. /* Use read because mmap won't work on proc files */
  1539. ret = read(from, buf_from, page_size);
  1540. if (ret < 0)
  1541. goto out;
  1542. if (!ret)
  1543. break;
  1544. len = ret;
  1545. if (readn(to, buf_to, len) != (int)len)
  1546. goto out;
  1547. if (memcmp(buf_from, buf_to, len))
  1548. goto out;
  1549. }
  1550. err = 0;
  1551. out:
  1552. free(buf_to);
  1553. free(buf_from);
  1554. return err;
  1555. }
  1556. static int kcore_copy__compare_files(const char *from_filename,
  1557. const char *to_filename)
  1558. {
  1559. int from, to, err = -1;
  1560. from = open(from_filename, O_RDONLY);
  1561. if (from < 0)
  1562. return -1;
  1563. to = open(to_filename, O_RDONLY);
  1564. if (to < 0)
  1565. goto out_close_from;
  1566. err = kcore_copy__compare_fds(from, to);
  1567. close(to);
  1568. out_close_from:
  1569. close(from);
  1570. return err;
  1571. }
  1572. static int kcore_copy__compare_file(const char *from_dir, const char *to_dir,
  1573. const char *name)
  1574. {
  1575. char from_filename[PATH_MAX];
  1576. char to_filename[PATH_MAX];
  1577. scnprintf(from_filename, PATH_MAX, "%s/%s", from_dir, name);
  1578. scnprintf(to_filename, PATH_MAX, "%s/%s", to_dir, name);
  1579. return kcore_copy__compare_files(from_filename, to_filename);
  1580. }
  1581. /**
  1582. * kcore_copy - copy kallsyms, modules and kcore from one directory to another.
  1583. * @from_dir: from directory
  1584. * @to_dir: to directory
  1585. *
  1586. * This function copies kallsyms, modules and kcore files from one directory to
  1587. * another. kallsyms and modules are copied entirely. Only code segments are
  1588. * copied from kcore. It is assumed that two segments suffice: one for the
  1589. * kernel proper and one for all the modules. The code segments are determined
  1590. * from kallsyms and modules files. The kernel map starts at _stext or the
  1591. * lowest function symbol, and ends at _etext or the highest function symbol.
  1592. * The module map starts at the lowest module address and ends at the highest
  1593. * module symbol. Start addresses are rounded down to the nearest page. End
  1594. * addresses are rounded up to the nearest page. An extra page is added to the
  1595. * highest kernel symbol and highest module symbol to, hopefully, encompass that
  1596. * symbol too. Because it contains only code sections, the resulting kcore is
  1597. * unusual. One significant peculiarity is that the mapping (start -> pgoff)
  1598. * is not the same for the kernel map and the modules map. That happens because
  1599. * the data is copied adjacently whereas the original kcore has gaps. Finally,
  1600. * kallsyms and modules files are compared with their copies to check that
  1601. * modules have not been loaded or unloaded while the copies were taking place.
  1602. *
  1603. * Return: %0 on success, %-1 on failure.
  1604. */
  1605. int kcore_copy(const char *from_dir, const char *to_dir)
  1606. {
  1607. struct kcore kcore;
  1608. struct kcore extract;
  1609. int idx = 0, err = -1;
  1610. off_t offset, sz;
  1611. struct kcore_copy_info kci = { .stext = 0, };
  1612. char kcore_filename[PATH_MAX];
  1613. char extract_filename[PATH_MAX];
  1614. struct phdr_data *p;
  1615. INIT_LIST_HEAD(&kci.phdrs);
  1616. INIT_LIST_HEAD(&kci.syms);
  1617. if (kcore_copy__copy_file(from_dir, to_dir, "kallsyms"))
  1618. return -1;
  1619. if (kcore_copy__copy_file(from_dir, to_dir, "modules"))
  1620. goto out_unlink_kallsyms;
  1621. scnprintf(kcore_filename, PATH_MAX, "%s/kcore", from_dir);
  1622. scnprintf(extract_filename, PATH_MAX, "%s/kcore", to_dir);
  1623. if (kcore__open(&kcore, kcore_filename))
  1624. goto out_unlink_modules;
  1625. if (kcore_copy__calc_maps(&kci, from_dir, kcore.elf))
  1626. goto out_kcore_close;
  1627. if (kcore__init(&extract, extract_filename, kcore.elfclass, false))
  1628. goto out_kcore_close;
  1629. if (kcore__copy_hdr(&kcore, &extract, kci.phnum))
  1630. goto out_extract_close;
  1631. offset = gelf_fsize(extract.elf, ELF_T_EHDR, 1, EV_CURRENT) +
  1632. gelf_fsize(extract.elf, ELF_T_PHDR, kci.phnum, EV_CURRENT);
  1633. offset = round_up(offset, page_size);
  1634. kcore_copy__for_each_phdr(&kci, p) {
  1635. off_t offs = p->rel + offset;
  1636. if (kcore__add_phdr(&extract, idx++, offs, p->addr, p->len))
  1637. goto out_extract_close;
  1638. }
  1639. sz = kcore__write(&extract);
  1640. if (sz < 0 || sz > offset)
  1641. goto out_extract_close;
  1642. kcore_copy__for_each_phdr(&kci, p) {
  1643. off_t offs = p->rel + offset;
  1644. if (p->remaps)
  1645. continue;
  1646. if (copy_bytes(kcore.fd, p->offset, extract.fd, offs, p->len))
  1647. goto out_extract_close;
  1648. }
  1649. if (kcore_copy__compare_file(from_dir, to_dir, "modules"))
  1650. goto out_extract_close;
  1651. if (kcore_copy__compare_file(from_dir, to_dir, "kallsyms"))
  1652. goto out_extract_close;
  1653. err = 0;
  1654. out_extract_close:
  1655. kcore__close(&extract);
  1656. if (err)
  1657. unlink(extract_filename);
  1658. out_kcore_close:
  1659. kcore__close(&kcore);
  1660. out_unlink_modules:
  1661. if (err)
  1662. kcore_copy__unlink(to_dir, "modules");
  1663. out_unlink_kallsyms:
  1664. if (err)
  1665. kcore_copy__unlink(to_dir, "kallsyms");
  1666. kcore_copy__free_phdrs(&kci);
  1667. kcore_copy__free_syms(&kci);
  1668. return err;
  1669. }
  1670. int kcore_extract__create(struct kcore_extract *kce)
  1671. {
  1672. struct kcore kcore;
  1673. struct kcore extract;
  1674. size_t count = 1;
  1675. int idx = 0, err = -1;
  1676. off_t offset = page_size, sz;
  1677. if (kcore__open(&kcore, kce->kcore_filename))
  1678. return -1;
  1679. strcpy(kce->extract_filename, PERF_KCORE_EXTRACT);
  1680. if (kcore__init(&extract, kce->extract_filename, kcore.elfclass, true))
  1681. goto out_kcore_close;
  1682. if (kcore__copy_hdr(&kcore, &extract, count))
  1683. goto out_extract_close;
  1684. if (kcore__add_phdr(&extract, idx, offset, kce->addr, kce->len))
  1685. goto out_extract_close;
  1686. sz = kcore__write(&extract);
  1687. if (sz < 0 || sz > offset)
  1688. goto out_extract_close;
  1689. if (copy_bytes(kcore.fd, kce->offs, extract.fd, offset, kce->len))
  1690. goto out_extract_close;
  1691. err = 0;
  1692. out_extract_close:
  1693. kcore__close(&extract);
  1694. if (err)
  1695. unlink(kce->extract_filename);
  1696. out_kcore_close:
  1697. kcore__close(&kcore);
  1698. return err;
  1699. }
  1700. void kcore_extract__delete(struct kcore_extract *kce)
  1701. {
  1702. unlink(kce->extract_filename);
  1703. }
  1704. #ifdef HAVE_GELF_GETNOTE_SUPPORT
  1705. static void sdt_adjust_loc(struct sdt_note *tmp, GElf_Addr base_off)
  1706. {
  1707. if (!base_off)
  1708. return;
  1709. if (tmp->bit32)
  1710. tmp->addr.a32[SDT_NOTE_IDX_LOC] =
  1711. tmp->addr.a32[SDT_NOTE_IDX_LOC] + base_off -
  1712. tmp->addr.a32[SDT_NOTE_IDX_BASE];
  1713. else
  1714. tmp->addr.a64[SDT_NOTE_IDX_LOC] =
  1715. tmp->addr.a64[SDT_NOTE_IDX_LOC] + base_off -
  1716. tmp->addr.a64[SDT_NOTE_IDX_BASE];
  1717. }
  1718. static void sdt_adjust_refctr(struct sdt_note *tmp, GElf_Addr base_addr,
  1719. GElf_Addr base_off)
  1720. {
  1721. if (!base_off)
  1722. return;
  1723. if (tmp->bit32 && tmp->addr.a32[SDT_NOTE_IDX_REFCTR])
  1724. tmp->addr.a32[SDT_NOTE_IDX_REFCTR] -= (base_addr - base_off);
  1725. else if (tmp->addr.a64[SDT_NOTE_IDX_REFCTR])
  1726. tmp->addr.a64[SDT_NOTE_IDX_REFCTR] -= (base_addr - base_off);
  1727. }
  1728. /**
  1729. * populate_sdt_note : Parse raw data and identify SDT note
  1730. * @elf: elf of the opened file
  1731. * @data: raw data of a section with description offset applied
  1732. * @len: note description size
  1733. * @type: type of the note
  1734. * @sdt_notes: List to add the SDT note
  1735. *
  1736. * Responsible for parsing the @data in section .note.stapsdt in @elf and
  1737. * if its an SDT note, it appends to @sdt_notes list.
  1738. */
  1739. static int populate_sdt_note(Elf **elf, const char *data, size_t len,
  1740. struct list_head *sdt_notes)
  1741. {
  1742. const char *provider, *name, *args;
  1743. struct sdt_note *tmp = NULL;
  1744. GElf_Ehdr ehdr;
  1745. GElf_Shdr shdr;
  1746. int ret = -EINVAL;
  1747. union {
  1748. Elf64_Addr a64[NR_ADDR];
  1749. Elf32_Addr a32[NR_ADDR];
  1750. } buf;
  1751. Elf_Data dst = {
  1752. .d_buf = &buf, .d_type = ELF_T_ADDR, .d_version = EV_CURRENT,
  1753. .d_size = gelf_fsize((*elf), ELF_T_ADDR, NR_ADDR, EV_CURRENT),
  1754. .d_off = 0, .d_align = 0
  1755. };
  1756. Elf_Data src = {
  1757. .d_buf = (void *) data, .d_type = ELF_T_ADDR,
  1758. .d_version = EV_CURRENT, .d_size = dst.d_size, .d_off = 0,
  1759. .d_align = 0
  1760. };
  1761. tmp = (struct sdt_note *)calloc(1, sizeof(struct sdt_note));
  1762. if (!tmp) {
  1763. ret = -ENOMEM;
  1764. goto out_err;
  1765. }
  1766. INIT_LIST_HEAD(&tmp->note_list);
  1767. if (len < dst.d_size + 3)
  1768. goto out_free_note;
  1769. /* Translation from file representation to memory representation */
  1770. if (gelf_xlatetom(*elf, &dst, &src,
  1771. elf_getident(*elf, NULL)[EI_DATA]) == NULL) {
  1772. pr_err("gelf_xlatetom : %s\n", elf_errmsg(-1));
  1773. goto out_free_note;
  1774. }
  1775. /* Populate the fields of sdt_note */
  1776. provider = data + dst.d_size;
  1777. name = (const char *)memchr(provider, '\0', data + len - provider);
  1778. if (name++ == NULL)
  1779. goto out_free_note;
  1780. tmp->provider = strdup(provider);
  1781. if (!tmp->provider) {
  1782. ret = -ENOMEM;
  1783. goto out_free_note;
  1784. }
  1785. tmp->name = strdup(name);
  1786. if (!tmp->name) {
  1787. ret = -ENOMEM;
  1788. goto out_free_prov;
  1789. }
  1790. args = memchr(name, '\0', data + len - name);
  1791. /*
  1792. * There is no argument if:
  1793. * - We reached the end of the note;
  1794. * - There is not enough room to hold a potential string;
  1795. * - The argument string is empty or just contains ':'.
  1796. */
  1797. if (args == NULL || data + len - args < 2 ||
  1798. args[1] == ':' || args[1] == '\0')
  1799. tmp->args = NULL;
  1800. else {
  1801. tmp->args = strdup(++args);
  1802. if (!tmp->args) {
  1803. ret = -ENOMEM;
  1804. goto out_free_name;
  1805. }
  1806. }
  1807. if (gelf_getclass(*elf) == ELFCLASS32) {
  1808. memcpy(&tmp->addr, &buf, 3 * sizeof(Elf32_Addr));
  1809. tmp->bit32 = true;
  1810. } else {
  1811. memcpy(&tmp->addr, &buf, 3 * sizeof(Elf64_Addr));
  1812. tmp->bit32 = false;
  1813. }
  1814. if (!gelf_getehdr(*elf, &ehdr)) {
  1815. pr_debug("%s : cannot get elf header.\n", __func__);
  1816. ret = -EBADF;
  1817. goto out_free_args;
  1818. }
  1819. /* Adjust the prelink effect :
  1820. * Find out the .stapsdt.base section.
  1821. * This scn will help us to handle prelinking (if present).
  1822. * Compare the retrieved file offset of the base section with the
  1823. * base address in the description of the SDT note. If its different,
  1824. * then accordingly, adjust the note location.
  1825. */
  1826. if (elf_section_by_name(*elf, &ehdr, &shdr, SDT_BASE_SCN, NULL))
  1827. sdt_adjust_loc(tmp, shdr.sh_offset);
  1828. /* Adjust reference counter offset */
  1829. if (elf_section_by_name(*elf, &ehdr, &shdr, SDT_PROBES_SCN, NULL))
  1830. sdt_adjust_refctr(tmp, shdr.sh_addr, shdr.sh_offset);
  1831. list_add_tail(&tmp->note_list, sdt_notes);
  1832. return 0;
  1833. out_free_args:
  1834. zfree(&tmp->args);
  1835. out_free_name:
  1836. zfree(&tmp->name);
  1837. out_free_prov:
  1838. zfree(&tmp->provider);
  1839. out_free_note:
  1840. free(tmp);
  1841. out_err:
  1842. return ret;
  1843. }
  1844. /**
  1845. * construct_sdt_notes_list : constructs a list of SDT notes
  1846. * @elf : elf to look into
  1847. * @sdt_notes : empty list_head
  1848. *
  1849. * Scans the sections in 'elf' for the section
  1850. * .note.stapsdt. It, then calls populate_sdt_note to find
  1851. * out the SDT events and populates the 'sdt_notes'.
  1852. */
  1853. static int construct_sdt_notes_list(Elf *elf, struct list_head *sdt_notes)
  1854. {
  1855. GElf_Ehdr ehdr;
  1856. Elf_Scn *scn = NULL;
  1857. Elf_Data *data;
  1858. GElf_Shdr shdr;
  1859. size_t shstrndx, next;
  1860. GElf_Nhdr nhdr;
  1861. size_t name_off, desc_off, offset;
  1862. int ret = 0;
  1863. if (gelf_getehdr(elf, &ehdr) == NULL) {
  1864. ret = -EBADF;
  1865. goto out_ret;
  1866. }
  1867. if (elf_getshdrstrndx(elf, &shstrndx) != 0) {
  1868. ret = -EBADF;
  1869. goto out_ret;
  1870. }
  1871. /* Look for the required section */
  1872. scn = elf_section_by_name(elf, &ehdr, &shdr, SDT_NOTE_SCN, NULL);
  1873. if (!scn) {
  1874. ret = -ENOENT;
  1875. goto out_ret;
  1876. }
  1877. if ((shdr.sh_type != SHT_NOTE) || (shdr.sh_flags & SHF_ALLOC)) {
  1878. ret = -ENOENT;
  1879. goto out_ret;
  1880. }
  1881. data = elf_getdata(scn, NULL);
  1882. /* Get the SDT notes */
  1883. for (offset = 0; (next = gelf_getnote(data, offset, &nhdr, &name_off,
  1884. &desc_off)) > 0; offset = next) {
  1885. if (nhdr.n_namesz == sizeof(SDT_NOTE_NAME) &&
  1886. !memcmp(data->d_buf + name_off, SDT_NOTE_NAME,
  1887. sizeof(SDT_NOTE_NAME))) {
  1888. /* Check the type of the note */
  1889. if (nhdr.n_type != SDT_NOTE_TYPE)
  1890. goto out_ret;
  1891. ret = populate_sdt_note(&elf, ((data->d_buf) + desc_off),
  1892. nhdr.n_descsz, sdt_notes);
  1893. if (ret < 0)
  1894. goto out_ret;
  1895. }
  1896. }
  1897. if (list_empty(sdt_notes))
  1898. ret = -ENOENT;
  1899. out_ret:
  1900. return ret;
  1901. }
  1902. /**
  1903. * get_sdt_note_list : Wrapper to construct a list of sdt notes
  1904. * @head : empty list_head
  1905. * @target : file to find SDT notes from
  1906. *
  1907. * This opens the file, initializes
  1908. * the ELF and then calls construct_sdt_notes_list.
  1909. */
  1910. int get_sdt_note_list(struct list_head *head, const char *target)
  1911. {
  1912. Elf *elf;
  1913. int fd, ret;
  1914. fd = open(target, O_RDONLY);
  1915. if (fd < 0)
  1916. return -EBADF;
  1917. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  1918. if (!elf) {
  1919. ret = -EBADF;
  1920. goto out_close;
  1921. }
  1922. ret = construct_sdt_notes_list(elf, head);
  1923. elf_end(elf);
  1924. out_close:
  1925. close(fd);
  1926. return ret;
  1927. }
  1928. /**
  1929. * cleanup_sdt_note_list : free the sdt notes' list
  1930. * @sdt_notes: sdt notes' list
  1931. *
  1932. * Free up the SDT notes in @sdt_notes.
  1933. * Returns the number of SDT notes free'd.
  1934. */
  1935. int cleanup_sdt_note_list(struct list_head *sdt_notes)
  1936. {
  1937. struct sdt_note *tmp, *pos;
  1938. int nr_free = 0;
  1939. list_for_each_entry_safe(pos, tmp, sdt_notes, note_list) {
  1940. list_del_init(&pos->note_list);
  1941. zfree(&pos->args);
  1942. zfree(&pos->name);
  1943. zfree(&pos->provider);
  1944. free(pos);
  1945. nr_free++;
  1946. }
  1947. return nr_free;
  1948. }
  1949. /**
  1950. * sdt_notes__get_count: Counts the number of sdt events
  1951. * @start: list_head to sdt_notes list
  1952. *
  1953. * Returns the number of SDT notes in a list
  1954. */
  1955. int sdt_notes__get_count(struct list_head *start)
  1956. {
  1957. struct sdt_note *sdt_ptr;
  1958. int count = 0;
  1959. list_for_each_entry(sdt_ptr, start, note_list)
  1960. count++;
  1961. return count;
  1962. }
  1963. #endif
  1964. void symbol__elf_init(void)
  1965. {
  1966. elf_version(EV_CURRENT);
  1967. }