symbol-elf.c 50 KB

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