symbol-elf.c 50 KB

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