symbol-elf.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083
  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__is_a(GElf_Sym *sym, enum map_type type)
  100. {
  101. switch (type) {
  102. case MAP__FUNCTION:
  103. return elf_sym__is_function(sym);
  104. case MAP__VARIABLE:
  105. return elf_sym__is_object(sym);
  106. default:
  107. return false;
  108. }
  109. }
  110. static inline const char *elf_sym__name(const GElf_Sym *sym,
  111. const Elf_Data *symstrs)
  112. {
  113. return symstrs->d_buf + sym->st_name;
  114. }
  115. static inline const char *elf_sec__name(const GElf_Shdr *shdr,
  116. const Elf_Data *secstrs)
  117. {
  118. return secstrs->d_buf + shdr->sh_name;
  119. }
  120. static inline int elf_sec__is_text(const GElf_Shdr *shdr,
  121. const Elf_Data *secstrs)
  122. {
  123. return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
  124. }
  125. static inline bool elf_sec__is_data(const GElf_Shdr *shdr,
  126. const Elf_Data *secstrs)
  127. {
  128. return strstr(elf_sec__name(shdr, secstrs), "data") != NULL;
  129. }
  130. static bool elf_sec__is_a(GElf_Shdr *shdr, Elf_Data *secstrs,
  131. enum map_type type)
  132. {
  133. switch (type) {
  134. case MAP__FUNCTION:
  135. return elf_sec__is_text(shdr, secstrs);
  136. case MAP__VARIABLE:
  137. return elf_sec__is_data(shdr, secstrs);
  138. default:
  139. return false;
  140. }
  141. }
  142. static size_t elf_addr_to_index(Elf *elf, GElf_Addr addr)
  143. {
  144. Elf_Scn *sec = NULL;
  145. GElf_Shdr shdr;
  146. size_t cnt = 1;
  147. while ((sec = elf_nextscn(elf, sec)) != NULL) {
  148. gelf_getshdr(sec, &shdr);
  149. if ((addr >= shdr.sh_addr) &&
  150. (addr < (shdr.sh_addr + shdr.sh_size)))
  151. return cnt;
  152. ++cnt;
  153. }
  154. return -1;
  155. }
  156. Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
  157. GElf_Shdr *shp, const char *name, size_t *idx)
  158. {
  159. Elf_Scn *sec = NULL;
  160. size_t cnt = 1;
  161. /* Elf is corrupted/truncated, avoid calling elf_strptr. */
  162. if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL))
  163. return NULL;
  164. while ((sec = elf_nextscn(elf, sec)) != NULL) {
  165. char *str;
  166. gelf_getshdr(sec, shp);
  167. str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
  168. if (str && !strcmp(name, str)) {
  169. if (idx)
  170. *idx = cnt;
  171. return sec;
  172. }
  173. ++cnt;
  174. }
  175. return NULL;
  176. }
  177. static bool want_demangle(bool is_kernel_sym)
  178. {
  179. return is_kernel_sym ? symbol_conf.demangle_kernel : symbol_conf.demangle;
  180. }
  181. static char *demangle_sym(struct dso *dso, int kmodule, const char *elf_name)
  182. {
  183. int demangle_flags = verbose > 0 ? (DMGL_PARAMS | DMGL_ANSI) : DMGL_NO_OPTS;
  184. char *demangled = NULL;
  185. /*
  186. * We need to figure out if the object was created from C++ sources
  187. * DWARF DW_compile_unit has this, but we don't always have access
  188. * to it...
  189. */
  190. if (!want_demangle(dso->kernel || kmodule))
  191. return demangled;
  192. demangled = bfd_demangle(NULL, elf_name, demangle_flags);
  193. if (demangled == NULL)
  194. demangled = java_demangle_sym(elf_name, JAVA_DEMANGLE_NORET);
  195. else if (rust_is_mangled(demangled))
  196. /*
  197. * Input to Rust demangling is the BFD-demangled
  198. * name which it Rust-demangles in place.
  199. */
  200. rust_demangle_sym(demangled);
  201. return demangled;
  202. }
  203. #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
  204. for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
  205. idx < nr_entries; \
  206. ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
  207. #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
  208. for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
  209. idx < nr_entries; \
  210. ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
  211. /*
  212. * We need to check if we have a .dynsym, so that we can handle the
  213. * .plt, synthesizing its symbols, that aren't on the symtabs (be it
  214. * .dynsym or .symtab).
  215. * And always look at the original dso, not at debuginfo packages, that
  216. * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
  217. */
  218. int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss, struct map *map)
  219. {
  220. uint32_t nr_rel_entries, idx;
  221. GElf_Sym sym;
  222. u64 plt_offset, plt_header_size, plt_entry_size;
  223. GElf_Shdr shdr_plt;
  224. struct symbol *f;
  225. GElf_Shdr shdr_rel_plt, shdr_dynsym;
  226. Elf_Data *reldata, *syms, *symstrs;
  227. Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
  228. size_t dynsym_idx;
  229. GElf_Ehdr ehdr;
  230. char sympltname[1024];
  231. Elf *elf;
  232. int nr = 0, symidx, err = 0;
  233. if (!ss->dynsym)
  234. return 0;
  235. elf = ss->elf;
  236. ehdr = ss->ehdr;
  237. scn_dynsym = ss->dynsym;
  238. shdr_dynsym = ss->dynshdr;
  239. dynsym_idx = ss->dynsym_idx;
  240. if (scn_dynsym == NULL)
  241. goto out_elf_end;
  242. scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
  243. ".rela.plt", NULL);
  244. if (scn_plt_rel == NULL) {
  245. scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
  246. ".rel.plt", NULL);
  247. if (scn_plt_rel == NULL)
  248. goto out_elf_end;
  249. }
  250. err = -1;
  251. if (shdr_rel_plt.sh_link != dynsym_idx)
  252. goto out_elf_end;
  253. if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
  254. goto out_elf_end;
  255. /*
  256. * Fetch the relocation section to find the idxes to the GOT
  257. * and the symbols in the .dynsym they refer to.
  258. */
  259. reldata = elf_getdata(scn_plt_rel, NULL);
  260. if (reldata == NULL)
  261. goto out_elf_end;
  262. syms = elf_getdata(scn_dynsym, NULL);
  263. if (syms == NULL)
  264. goto out_elf_end;
  265. scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
  266. if (scn_symstrs == NULL)
  267. goto out_elf_end;
  268. symstrs = elf_getdata(scn_symstrs, NULL);
  269. if (symstrs == NULL)
  270. goto out_elf_end;
  271. if (symstrs->d_size == 0)
  272. goto out_elf_end;
  273. nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
  274. plt_offset = shdr_plt.sh_offset;
  275. switch (ehdr.e_machine) {
  276. case EM_ARM:
  277. plt_header_size = 20;
  278. plt_entry_size = 12;
  279. break;
  280. case EM_AARCH64:
  281. plt_header_size = 32;
  282. plt_entry_size = 16;
  283. break;
  284. default: /* FIXME: s390/alpha/mips/parisc/poperpc/sh/sparc/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, sympltname);
  307. if (!f)
  308. goto out_elf_end;
  309. plt_offset += plt_entry_size;
  310. symbols__insert(&dso->symbols[map->type], 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, sympltname);
  330. if (!f)
  331. goto out_elf_end;
  332. plt_offset += plt_entry_size;
  333. symbols__insert(&dso->symbols[map->type], 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. int dso__load_sym(struct dso *dso, struct map *map, struct symsrc *syms_ss,
  679. struct symsrc *runtime_ss, int kmodule)
  680. {
  681. struct kmap *kmap = dso->kernel ? map__kmap(map) : NULL;
  682. struct map_groups *kmaps = kmap ? map__kmaps(map) : NULL;
  683. struct map *curr_map = map;
  684. struct dso *curr_dso = dso;
  685. Elf_Data *symstrs, *secstrs;
  686. uint32_t nr_syms;
  687. int err = -1;
  688. uint32_t idx;
  689. GElf_Ehdr ehdr;
  690. GElf_Shdr shdr;
  691. GElf_Shdr tshdr;
  692. Elf_Data *syms, *opddata = NULL;
  693. GElf_Sym sym;
  694. Elf_Scn *sec, *sec_strndx;
  695. Elf *elf;
  696. int nr = 0;
  697. bool remap_kernel = false, adjust_kernel_syms = false;
  698. if (kmap && !kmaps)
  699. return -1;
  700. dso->symtab_type = syms_ss->type;
  701. dso->is_64_bit = syms_ss->is_64_bit;
  702. dso->rel = syms_ss->ehdr.e_type == ET_REL;
  703. /*
  704. * Modules may already have symbols from kallsyms, but those symbols
  705. * have the wrong values for the dso maps, so remove them.
  706. */
  707. if (kmodule && syms_ss->symtab)
  708. symbols__delete(&dso->symbols[map->type]);
  709. if (!syms_ss->symtab) {
  710. /*
  711. * If the vmlinux is stripped, fail so we will fall back
  712. * to using kallsyms. The vmlinux runtime symbols aren't
  713. * of much use.
  714. */
  715. if (dso->kernel)
  716. goto out_elf_end;
  717. syms_ss->symtab = syms_ss->dynsym;
  718. syms_ss->symshdr = syms_ss->dynshdr;
  719. }
  720. elf = syms_ss->elf;
  721. ehdr = syms_ss->ehdr;
  722. sec = syms_ss->symtab;
  723. shdr = syms_ss->symshdr;
  724. if (elf_section_by_name(runtime_ss->elf, &runtime_ss->ehdr, &tshdr,
  725. ".text", NULL))
  726. dso->text_offset = tshdr.sh_addr - tshdr.sh_offset;
  727. if (runtime_ss->opdsec)
  728. opddata = elf_rawdata(runtime_ss->opdsec, NULL);
  729. syms = elf_getdata(sec, NULL);
  730. if (syms == NULL)
  731. goto out_elf_end;
  732. sec = elf_getscn(elf, shdr.sh_link);
  733. if (sec == NULL)
  734. goto out_elf_end;
  735. symstrs = elf_getdata(sec, NULL);
  736. if (symstrs == NULL)
  737. goto out_elf_end;
  738. sec_strndx = elf_getscn(runtime_ss->elf, runtime_ss->ehdr.e_shstrndx);
  739. if (sec_strndx == NULL)
  740. goto out_elf_end;
  741. secstrs = elf_getdata(sec_strndx, NULL);
  742. if (secstrs == NULL)
  743. goto out_elf_end;
  744. nr_syms = shdr.sh_size / shdr.sh_entsize;
  745. memset(&sym, 0, sizeof(sym));
  746. /*
  747. * The kernel relocation symbol is needed in advance in order to adjust
  748. * kernel maps correctly.
  749. */
  750. if (ref_reloc_sym_not_found(kmap)) {
  751. elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
  752. const char *elf_name = elf_sym__name(&sym, symstrs);
  753. if (strcmp(elf_name, kmap->ref_reloc_sym->name))
  754. continue;
  755. kmap->ref_reloc_sym->unrelocated_addr = sym.st_value;
  756. map->reloc = kmap->ref_reloc_sym->addr -
  757. kmap->ref_reloc_sym->unrelocated_addr;
  758. break;
  759. }
  760. }
  761. /*
  762. * Handle any relocation of vdso necessary because older kernels
  763. * attempted to prelink vdso to its virtual address.
  764. */
  765. if (dso__is_vdso(dso))
  766. map->reloc = map->start - dso->text_offset;
  767. dso->adjust_symbols = runtime_ss->adjust_symbols || ref_reloc(kmap);
  768. /*
  769. * Initial kernel and module mappings do not map to the dso. For
  770. * function mappings, flag the fixups.
  771. */
  772. if (map->type == MAP__FUNCTION && (dso->kernel || kmodule)) {
  773. remap_kernel = true;
  774. adjust_kernel_syms = dso->adjust_symbols;
  775. }
  776. elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
  777. struct symbol *f;
  778. const char *elf_name = elf_sym__name(&sym, symstrs);
  779. char *demangled = NULL;
  780. int is_label = elf_sym__is_label(&sym);
  781. const char *section_name;
  782. bool used_opd = false;
  783. if (!is_label && !elf_sym__is_a(&sym, map->type))
  784. continue;
  785. /* Reject ARM ELF "mapping symbols": these aren't unique and
  786. * don't identify functions, so will confuse the profile
  787. * output: */
  788. if (ehdr.e_machine == EM_ARM || ehdr.e_machine == EM_AARCH64) {
  789. if (elf_name[0] == '$' && strchr("adtx", elf_name[1])
  790. && (elf_name[2] == '\0' || elf_name[2] == '.'))
  791. continue;
  792. }
  793. if (runtime_ss->opdsec && sym.st_shndx == runtime_ss->opdidx) {
  794. u32 offset = sym.st_value - syms_ss->opdshdr.sh_addr;
  795. u64 *opd = opddata->d_buf + offset;
  796. sym.st_value = DSO__SWAP(dso, u64, *opd);
  797. sym.st_shndx = elf_addr_to_index(runtime_ss->elf,
  798. sym.st_value);
  799. used_opd = true;
  800. }
  801. /*
  802. * When loading symbols in a data mapping, ABS symbols (which
  803. * has a value of SHN_ABS in its st_shndx) failed at
  804. * elf_getscn(). And it marks the loading as a failure so
  805. * already loaded symbols cannot be fixed up.
  806. *
  807. * I'm not sure what should be done. Just ignore them for now.
  808. * - Namhyung Kim
  809. */
  810. if (sym.st_shndx == SHN_ABS)
  811. continue;
  812. sec = elf_getscn(runtime_ss->elf, sym.st_shndx);
  813. if (!sec)
  814. goto out_elf_end;
  815. gelf_getshdr(sec, &shdr);
  816. if (is_label && !elf_sec__is_a(&shdr, secstrs, map->type))
  817. continue;
  818. section_name = elf_sec__name(&shdr, secstrs);
  819. /* On ARM, symbols for thumb functions have 1 added to
  820. * the symbol address as a flag - remove it */
  821. if ((ehdr.e_machine == EM_ARM) &&
  822. (map->type == MAP__FUNCTION) &&
  823. (sym.st_value & 1))
  824. --sym.st_value;
  825. if (dso->kernel || kmodule) {
  826. char dso_name[PATH_MAX];
  827. /* Adjust symbol to map to file offset */
  828. if (adjust_kernel_syms)
  829. sym.st_value -= shdr.sh_addr - shdr.sh_offset;
  830. if (strcmp(section_name,
  831. (curr_dso->short_name +
  832. dso->short_name_len)) == 0)
  833. goto new_symbol;
  834. if (strcmp(section_name, ".text") == 0) {
  835. /*
  836. * The initial kernel mapping is based on
  837. * kallsyms and identity maps. Overwrite it to
  838. * map to the kernel dso.
  839. */
  840. if (remap_kernel && dso->kernel) {
  841. remap_kernel = false;
  842. map->start = shdr.sh_addr +
  843. ref_reloc(kmap);
  844. map->end = map->start + shdr.sh_size;
  845. map->pgoff = shdr.sh_offset;
  846. map->map_ip = map__map_ip;
  847. map->unmap_ip = map__unmap_ip;
  848. /* Ensure maps are correctly ordered */
  849. if (kmaps) {
  850. map__get(map);
  851. map_groups__remove(kmaps, map);
  852. map_groups__insert(kmaps, map);
  853. map__put(map);
  854. }
  855. }
  856. /*
  857. * The initial module mapping is based on
  858. * /proc/modules mapped to offset zero.
  859. * Overwrite it to map to the module dso.
  860. */
  861. if (remap_kernel && kmodule) {
  862. remap_kernel = false;
  863. map->pgoff = shdr.sh_offset;
  864. }
  865. curr_map = map;
  866. curr_dso = dso;
  867. goto new_symbol;
  868. }
  869. if (!kmap)
  870. goto new_symbol;
  871. snprintf(dso_name, sizeof(dso_name),
  872. "%s%s", dso->short_name, section_name);
  873. curr_map = map_groups__find_by_name(kmaps, map->type, dso_name);
  874. if (curr_map == NULL) {
  875. u64 start = sym.st_value;
  876. if (kmodule)
  877. start += map->start + shdr.sh_offset;
  878. curr_dso = dso__new(dso_name);
  879. if (curr_dso == NULL)
  880. goto out_elf_end;
  881. curr_dso->kernel = dso->kernel;
  882. curr_dso->long_name = dso->long_name;
  883. curr_dso->long_name_len = dso->long_name_len;
  884. curr_map = map__new2(start, curr_dso,
  885. map->type);
  886. dso__put(curr_dso);
  887. if (curr_map == NULL) {
  888. goto out_elf_end;
  889. }
  890. if (adjust_kernel_syms) {
  891. curr_map->start = shdr.sh_addr +
  892. ref_reloc(kmap);
  893. curr_map->end = curr_map->start +
  894. shdr.sh_size;
  895. curr_map->pgoff = shdr.sh_offset;
  896. } else {
  897. curr_map->map_ip = identity__map_ip;
  898. curr_map->unmap_ip = identity__map_ip;
  899. }
  900. curr_dso->symtab_type = dso->symtab_type;
  901. map_groups__insert(kmaps, curr_map);
  902. /*
  903. * Add it before we drop the referece to curr_map,
  904. * i.e. while we still are sure to have a reference
  905. * to this DSO via curr_map->dso.
  906. */
  907. dsos__add(&map->groups->machine->dsos, curr_dso);
  908. /* kmaps already got it */
  909. map__put(curr_map);
  910. dso__set_loaded(curr_dso, map->type);
  911. } else
  912. curr_dso = curr_map->dso;
  913. goto new_symbol;
  914. }
  915. if ((used_opd && runtime_ss->adjust_symbols)
  916. || (!used_opd && syms_ss->adjust_symbols)) {
  917. pr_debug4("%s: adjusting symbol: st_value: %#" PRIx64 " "
  918. "sh_addr: %#" PRIx64 " sh_offset: %#" PRIx64 "\n", __func__,
  919. (u64)sym.st_value, (u64)shdr.sh_addr,
  920. (u64)shdr.sh_offset);
  921. sym.st_value -= shdr.sh_addr - shdr.sh_offset;
  922. }
  923. new_symbol:
  924. demangled = demangle_sym(dso, kmodule, elf_name);
  925. if (demangled != NULL)
  926. elf_name = demangled;
  927. f = symbol__new(sym.st_value, sym.st_size,
  928. GELF_ST_BIND(sym.st_info), elf_name);
  929. free(demangled);
  930. if (!f)
  931. goto out_elf_end;
  932. arch__sym_update(f, &sym);
  933. __symbols__insert(&curr_dso->symbols[curr_map->type], f, dso->kernel);
  934. nr++;
  935. }
  936. /*
  937. * For misannotated, zeroed, ASM function sizes.
  938. */
  939. if (nr > 0) {
  940. symbols__fixup_end(&dso->symbols[map->type]);
  941. symbols__fixup_duplicate(&dso->symbols[map->type]);
  942. if (kmap) {
  943. /*
  944. * We need to fixup this here too because we create new
  945. * maps here, for things like vsyscall sections.
  946. */
  947. __map_groups__fixup_end(kmaps, map->type);
  948. }
  949. }
  950. err = nr;
  951. out_elf_end:
  952. return err;
  953. }
  954. static int elf_read_maps(Elf *elf, bool exe, mapfn_t mapfn, void *data)
  955. {
  956. GElf_Phdr phdr;
  957. size_t i, phdrnum;
  958. int err;
  959. u64 sz;
  960. if (elf_getphdrnum(elf, &phdrnum))
  961. return -1;
  962. for (i = 0; i < phdrnum; i++) {
  963. if (gelf_getphdr(elf, i, &phdr) == NULL)
  964. return -1;
  965. if (phdr.p_type != PT_LOAD)
  966. continue;
  967. if (exe) {
  968. if (!(phdr.p_flags & PF_X))
  969. continue;
  970. } else {
  971. if (!(phdr.p_flags & PF_R))
  972. continue;
  973. }
  974. sz = min(phdr.p_memsz, phdr.p_filesz);
  975. if (!sz)
  976. continue;
  977. err = mapfn(phdr.p_vaddr, sz, phdr.p_offset, data);
  978. if (err)
  979. return err;
  980. }
  981. return 0;
  982. }
  983. int file__read_maps(int fd, bool exe, mapfn_t mapfn, void *data,
  984. bool *is_64_bit)
  985. {
  986. int err;
  987. Elf *elf;
  988. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  989. if (elf == NULL)
  990. return -1;
  991. if (is_64_bit)
  992. *is_64_bit = (gelf_getclass(elf) == ELFCLASS64);
  993. err = elf_read_maps(elf, exe, mapfn, data);
  994. elf_end(elf);
  995. return err;
  996. }
  997. enum dso_type dso__type_fd(int fd)
  998. {
  999. enum dso_type dso_type = DSO__TYPE_UNKNOWN;
  1000. GElf_Ehdr ehdr;
  1001. Elf_Kind ek;
  1002. Elf *elf;
  1003. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  1004. if (elf == NULL)
  1005. goto out;
  1006. ek = elf_kind(elf);
  1007. if (ek != ELF_K_ELF)
  1008. goto out_end;
  1009. if (gelf_getclass(elf) == ELFCLASS64) {
  1010. dso_type = DSO__TYPE_64BIT;
  1011. goto out_end;
  1012. }
  1013. if (gelf_getehdr(elf, &ehdr) == NULL)
  1014. goto out_end;
  1015. if (ehdr.e_machine == EM_X86_64)
  1016. dso_type = DSO__TYPE_X32BIT;
  1017. else
  1018. dso_type = DSO__TYPE_32BIT;
  1019. out_end:
  1020. elf_end(elf);
  1021. out:
  1022. return dso_type;
  1023. }
  1024. static int copy_bytes(int from, off_t from_offs, int to, off_t to_offs, u64 len)
  1025. {
  1026. ssize_t r;
  1027. size_t n;
  1028. int err = -1;
  1029. char *buf = malloc(page_size);
  1030. if (buf == NULL)
  1031. return -1;
  1032. if (lseek(to, to_offs, SEEK_SET) != to_offs)
  1033. goto out;
  1034. if (lseek(from, from_offs, SEEK_SET) != from_offs)
  1035. goto out;
  1036. while (len) {
  1037. n = page_size;
  1038. if (len < n)
  1039. n = len;
  1040. /* Use read because mmap won't work on proc files */
  1041. r = read(from, buf, n);
  1042. if (r < 0)
  1043. goto out;
  1044. if (!r)
  1045. break;
  1046. n = r;
  1047. r = write(to, buf, n);
  1048. if (r < 0)
  1049. goto out;
  1050. if ((size_t)r != n)
  1051. goto out;
  1052. len -= n;
  1053. }
  1054. err = 0;
  1055. out:
  1056. free(buf);
  1057. return err;
  1058. }
  1059. struct kcore {
  1060. int fd;
  1061. int elfclass;
  1062. Elf *elf;
  1063. GElf_Ehdr ehdr;
  1064. };
  1065. static int kcore__open(struct kcore *kcore, const char *filename)
  1066. {
  1067. GElf_Ehdr *ehdr;
  1068. kcore->fd = open(filename, O_RDONLY);
  1069. if (kcore->fd == -1)
  1070. return -1;
  1071. kcore->elf = elf_begin(kcore->fd, ELF_C_READ, NULL);
  1072. if (!kcore->elf)
  1073. goto out_close;
  1074. kcore->elfclass = gelf_getclass(kcore->elf);
  1075. if (kcore->elfclass == ELFCLASSNONE)
  1076. goto out_end;
  1077. ehdr = gelf_getehdr(kcore->elf, &kcore->ehdr);
  1078. if (!ehdr)
  1079. goto out_end;
  1080. return 0;
  1081. out_end:
  1082. elf_end(kcore->elf);
  1083. out_close:
  1084. close(kcore->fd);
  1085. return -1;
  1086. }
  1087. static int kcore__init(struct kcore *kcore, char *filename, int elfclass,
  1088. bool temp)
  1089. {
  1090. kcore->elfclass = elfclass;
  1091. if (temp)
  1092. kcore->fd = mkstemp(filename);
  1093. else
  1094. kcore->fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0400);
  1095. if (kcore->fd == -1)
  1096. return -1;
  1097. kcore->elf = elf_begin(kcore->fd, ELF_C_WRITE, NULL);
  1098. if (!kcore->elf)
  1099. goto out_close;
  1100. if (!gelf_newehdr(kcore->elf, elfclass))
  1101. goto out_end;
  1102. memset(&kcore->ehdr, 0, sizeof(GElf_Ehdr));
  1103. return 0;
  1104. out_end:
  1105. elf_end(kcore->elf);
  1106. out_close:
  1107. close(kcore->fd);
  1108. unlink(filename);
  1109. return -1;
  1110. }
  1111. static void kcore__close(struct kcore *kcore)
  1112. {
  1113. elf_end(kcore->elf);
  1114. close(kcore->fd);
  1115. }
  1116. static int kcore__copy_hdr(struct kcore *from, struct kcore *to, size_t count)
  1117. {
  1118. GElf_Ehdr *ehdr = &to->ehdr;
  1119. GElf_Ehdr *kehdr = &from->ehdr;
  1120. memcpy(ehdr->e_ident, kehdr->e_ident, EI_NIDENT);
  1121. ehdr->e_type = kehdr->e_type;
  1122. ehdr->e_machine = kehdr->e_machine;
  1123. ehdr->e_version = kehdr->e_version;
  1124. ehdr->e_entry = 0;
  1125. ehdr->e_shoff = 0;
  1126. ehdr->e_flags = kehdr->e_flags;
  1127. ehdr->e_phnum = count;
  1128. ehdr->e_shentsize = 0;
  1129. ehdr->e_shnum = 0;
  1130. ehdr->e_shstrndx = 0;
  1131. if (from->elfclass == ELFCLASS32) {
  1132. ehdr->e_phoff = sizeof(Elf32_Ehdr);
  1133. ehdr->e_ehsize = sizeof(Elf32_Ehdr);
  1134. ehdr->e_phentsize = sizeof(Elf32_Phdr);
  1135. } else {
  1136. ehdr->e_phoff = sizeof(Elf64_Ehdr);
  1137. ehdr->e_ehsize = sizeof(Elf64_Ehdr);
  1138. ehdr->e_phentsize = sizeof(Elf64_Phdr);
  1139. }
  1140. if (!gelf_update_ehdr(to->elf, ehdr))
  1141. return -1;
  1142. if (!gelf_newphdr(to->elf, count))
  1143. return -1;
  1144. return 0;
  1145. }
  1146. static int kcore__add_phdr(struct kcore *kcore, int idx, off_t offset,
  1147. u64 addr, u64 len)
  1148. {
  1149. GElf_Phdr phdr = {
  1150. .p_type = PT_LOAD,
  1151. .p_flags = PF_R | PF_W | PF_X,
  1152. .p_offset = offset,
  1153. .p_vaddr = addr,
  1154. .p_paddr = 0,
  1155. .p_filesz = len,
  1156. .p_memsz = len,
  1157. .p_align = page_size,
  1158. };
  1159. if (!gelf_update_phdr(kcore->elf, idx, &phdr))
  1160. return -1;
  1161. return 0;
  1162. }
  1163. static off_t kcore__write(struct kcore *kcore)
  1164. {
  1165. return elf_update(kcore->elf, ELF_C_WRITE);
  1166. }
  1167. struct phdr_data {
  1168. off_t offset;
  1169. u64 addr;
  1170. u64 len;
  1171. };
  1172. struct kcore_copy_info {
  1173. u64 stext;
  1174. u64 etext;
  1175. u64 first_symbol;
  1176. u64 last_symbol;
  1177. u64 first_module;
  1178. u64 last_module_symbol;
  1179. struct phdr_data kernel_map;
  1180. struct phdr_data modules_map;
  1181. };
  1182. static int kcore_copy__process_kallsyms(void *arg, const char *name, char type,
  1183. u64 start)
  1184. {
  1185. struct kcore_copy_info *kci = arg;
  1186. if (!symbol_type__is_a(type, MAP__FUNCTION))
  1187. return 0;
  1188. if (strchr(name, '[')) {
  1189. if (start > kci->last_module_symbol)
  1190. kci->last_module_symbol = start;
  1191. return 0;
  1192. }
  1193. if (!kci->first_symbol || start < kci->first_symbol)
  1194. kci->first_symbol = start;
  1195. if (!kci->last_symbol || start > kci->last_symbol)
  1196. kci->last_symbol = start;
  1197. if (!strcmp(name, "_stext")) {
  1198. kci->stext = start;
  1199. return 0;
  1200. }
  1201. if (!strcmp(name, "_etext")) {
  1202. kci->etext = start;
  1203. return 0;
  1204. }
  1205. return 0;
  1206. }
  1207. static int kcore_copy__parse_kallsyms(struct kcore_copy_info *kci,
  1208. const char *dir)
  1209. {
  1210. char kallsyms_filename[PATH_MAX];
  1211. scnprintf(kallsyms_filename, PATH_MAX, "%s/kallsyms", dir);
  1212. if (symbol__restricted_filename(kallsyms_filename, "/proc/kallsyms"))
  1213. return -1;
  1214. if (kallsyms__parse(kallsyms_filename, kci,
  1215. kcore_copy__process_kallsyms) < 0)
  1216. return -1;
  1217. return 0;
  1218. }
  1219. static int kcore_copy__process_modules(void *arg,
  1220. const char *name __maybe_unused,
  1221. u64 start, u64 size __maybe_unused)
  1222. {
  1223. struct kcore_copy_info *kci = arg;
  1224. if (!kci->first_module || start < kci->first_module)
  1225. kci->first_module = start;
  1226. return 0;
  1227. }
  1228. static int kcore_copy__parse_modules(struct kcore_copy_info *kci,
  1229. const char *dir)
  1230. {
  1231. char modules_filename[PATH_MAX];
  1232. scnprintf(modules_filename, PATH_MAX, "%s/modules", dir);
  1233. if (symbol__restricted_filename(modules_filename, "/proc/modules"))
  1234. return -1;
  1235. if (modules__parse(modules_filename, kci,
  1236. kcore_copy__process_modules) < 0)
  1237. return -1;
  1238. return 0;
  1239. }
  1240. static void kcore_copy__map(struct phdr_data *p, u64 start, u64 end, u64 pgoff,
  1241. u64 s, u64 e)
  1242. {
  1243. if (p->addr || s < start || s >= end)
  1244. return;
  1245. p->addr = s;
  1246. p->offset = (s - start) + pgoff;
  1247. p->len = e < end ? e - s : end - s;
  1248. }
  1249. static int kcore_copy__read_map(u64 start, u64 len, u64 pgoff, void *data)
  1250. {
  1251. struct kcore_copy_info *kci = data;
  1252. u64 end = start + len;
  1253. kcore_copy__map(&kci->kernel_map, start, end, pgoff, kci->stext,
  1254. kci->etext);
  1255. kcore_copy__map(&kci->modules_map, start, end, pgoff, kci->first_module,
  1256. kci->last_module_symbol);
  1257. return 0;
  1258. }
  1259. static int kcore_copy__read_maps(struct kcore_copy_info *kci, Elf *elf)
  1260. {
  1261. if (elf_read_maps(elf, true, kcore_copy__read_map, kci) < 0)
  1262. return -1;
  1263. return 0;
  1264. }
  1265. static int kcore_copy__calc_maps(struct kcore_copy_info *kci, const char *dir,
  1266. Elf *elf)
  1267. {
  1268. if (kcore_copy__parse_kallsyms(kci, dir))
  1269. return -1;
  1270. if (kcore_copy__parse_modules(kci, dir))
  1271. return -1;
  1272. if (kci->stext)
  1273. kci->stext = round_down(kci->stext, page_size);
  1274. else
  1275. kci->stext = round_down(kci->first_symbol, page_size);
  1276. if (kci->etext) {
  1277. kci->etext = round_up(kci->etext, page_size);
  1278. } else if (kci->last_symbol) {
  1279. kci->etext = round_up(kci->last_symbol, page_size);
  1280. kci->etext += page_size;
  1281. }
  1282. kci->first_module = round_down(kci->first_module, page_size);
  1283. if (kci->last_module_symbol) {
  1284. kci->last_module_symbol = round_up(kci->last_module_symbol,
  1285. page_size);
  1286. kci->last_module_symbol += page_size;
  1287. }
  1288. if (!kci->stext || !kci->etext)
  1289. return -1;
  1290. if (kci->first_module && !kci->last_module_symbol)
  1291. return -1;
  1292. return kcore_copy__read_maps(kci, elf);
  1293. }
  1294. static int kcore_copy__copy_file(const char *from_dir, const char *to_dir,
  1295. const char *name)
  1296. {
  1297. char from_filename[PATH_MAX];
  1298. char to_filename[PATH_MAX];
  1299. scnprintf(from_filename, PATH_MAX, "%s/%s", from_dir, name);
  1300. scnprintf(to_filename, PATH_MAX, "%s/%s", to_dir, name);
  1301. return copyfile_mode(from_filename, to_filename, 0400);
  1302. }
  1303. static int kcore_copy__unlink(const char *dir, const char *name)
  1304. {
  1305. char filename[PATH_MAX];
  1306. scnprintf(filename, PATH_MAX, "%s/%s", dir, name);
  1307. return unlink(filename);
  1308. }
  1309. static int kcore_copy__compare_fds(int from, int to)
  1310. {
  1311. char *buf_from;
  1312. char *buf_to;
  1313. ssize_t ret;
  1314. size_t len;
  1315. int err = -1;
  1316. buf_from = malloc(page_size);
  1317. buf_to = malloc(page_size);
  1318. if (!buf_from || !buf_to)
  1319. goto out;
  1320. while (1) {
  1321. /* Use read because mmap won't work on proc files */
  1322. ret = read(from, buf_from, page_size);
  1323. if (ret < 0)
  1324. goto out;
  1325. if (!ret)
  1326. break;
  1327. len = ret;
  1328. if (readn(to, buf_to, len) != (int)len)
  1329. goto out;
  1330. if (memcmp(buf_from, buf_to, len))
  1331. goto out;
  1332. }
  1333. err = 0;
  1334. out:
  1335. free(buf_to);
  1336. free(buf_from);
  1337. return err;
  1338. }
  1339. static int kcore_copy__compare_files(const char *from_filename,
  1340. const char *to_filename)
  1341. {
  1342. int from, to, err = -1;
  1343. from = open(from_filename, O_RDONLY);
  1344. if (from < 0)
  1345. return -1;
  1346. to = open(to_filename, O_RDONLY);
  1347. if (to < 0)
  1348. goto out_close_from;
  1349. err = kcore_copy__compare_fds(from, to);
  1350. close(to);
  1351. out_close_from:
  1352. close(from);
  1353. return err;
  1354. }
  1355. static int kcore_copy__compare_file(const char *from_dir, const char *to_dir,
  1356. const char *name)
  1357. {
  1358. char from_filename[PATH_MAX];
  1359. char to_filename[PATH_MAX];
  1360. scnprintf(from_filename, PATH_MAX, "%s/%s", from_dir, name);
  1361. scnprintf(to_filename, PATH_MAX, "%s/%s", to_dir, name);
  1362. return kcore_copy__compare_files(from_filename, to_filename);
  1363. }
  1364. /**
  1365. * kcore_copy - copy kallsyms, modules and kcore from one directory to another.
  1366. * @from_dir: from directory
  1367. * @to_dir: to directory
  1368. *
  1369. * This function copies kallsyms, modules and kcore files from one directory to
  1370. * another. kallsyms and modules are copied entirely. Only code segments are
  1371. * copied from kcore. It is assumed that two segments suffice: one for the
  1372. * kernel proper and one for all the modules. The code segments are determined
  1373. * from kallsyms and modules files. The kernel map starts at _stext or the
  1374. * lowest function symbol, and ends at _etext or the highest function symbol.
  1375. * The module map starts at the lowest module address and ends at the highest
  1376. * module symbol. Start addresses are rounded down to the nearest page. End
  1377. * addresses are rounded up to the nearest page. An extra page is added to the
  1378. * highest kernel symbol and highest module symbol to, hopefully, encompass that
  1379. * symbol too. Because it contains only code sections, the resulting kcore is
  1380. * unusual. One significant peculiarity is that the mapping (start -> pgoff)
  1381. * is not the same for the kernel map and the modules map. That happens because
  1382. * the data is copied adjacently whereas the original kcore has gaps. Finally,
  1383. * kallsyms and modules files are compared with their copies to check that
  1384. * modules have not been loaded or unloaded while the copies were taking place.
  1385. *
  1386. * Return: %0 on success, %-1 on failure.
  1387. */
  1388. int kcore_copy(const char *from_dir, const char *to_dir)
  1389. {
  1390. struct kcore kcore;
  1391. struct kcore extract;
  1392. size_t count = 2;
  1393. int idx = 0, err = -1;
  1394. off_t offset = page_size, sz, modules_offset = 0;
  1395. struct kcore_copy_info kci = { .stext = 0, };
  1396. char kcore_filename[PATH_MAX];
  1397. char extract_filename[PATH_MAX];
  1398. if (kcore_copy__copy_file(from_dir, to_dir, "kallsyms"))
  1399. return -1;
  1400. if (kcore_copy__copy_file(from_dir, to_dir, "modules"))
  1401. goto out_unlink_kallsyms;
  1402. scnprintf(kcore_filename, PATH_MAX, "%s/kcore", from_dir);
  1403. scnprintf(extract_filename, PATH_MAX, "%s/kcore", to_dir);
  1404. if (kcore__open(&kcore, kcore_filename))
  1405. goto out_unlink_modules;
  1406. if (kcore_copy__calc_maps(&kci, from_dir, kcore.elf))
  1407. goto out_kcore_close;
  1408. if (kcore__init(&extract, extract_filename, kcore.elfclass, false))
  1409. goto out_kcore_close;
  1410. if (!kci.modules_map.addr)
  1411. count -= 1;
  1412. if (kcore__copy_hdr(&kcore, &extract, count))
  1413. goto out_extract_close;
  1414. if (kcore__add_phdr(&extract, idx++, offset, kci.kernel_map.addr,
  1415. kci.kernel_map.len))
  1416. goto out_extract_close;
  1417. if (kci.modules_map.addr) {
  1418. modules_offset = offset + kci.kernel_map.len;
  1419. if (kcore__add_phdr(&extract, idx, modules_offset,
  1420. kci.modules_map.addr, kci.modules_map.len))
  1421. goto out_extract_close;
  1422. }
  1423. sz = kcore__write(&extract);
  1424. if (sz < 0 || sz > offset)
  1425. goto out_extract_close;
  1426. if (copy_bytes(kcore.fd, kci.kernel_map.offset, extract.fd, offset,
  1427. kci.kernel_map.len))
  1428. goto out_extract_close;
  1429. if (modules_offset && copy_bytes(kcore.fd, kci.modules_map.offset,
  1430. extract.fd, modules_offset,
  1431. kci.modules_map.len))
  1432. goto out_extract_close;
  1433. if (kcore_copy__compare_file(from_dir, to_dir, "modules"))
  1434. goto out_extract_close;
  1435. if (kcore_copy__compare_file(from_dir, to_dir, "kallsyms"))
  1436. goto out_extract_close;
  1437. err = 0;
  1438. out_extract_close:
  1439. kcore__close(&extract);
  1440. if (err)
  1441. unlink(extract_filename);
  1442. out_kcore_close:
  1443. kcore__close(&kcore);
  1444. out_unlink_modules:
  1445. if (err)
  1446. kcore_copy__unlink(to_dir, "modules");
  1447. out_unlink_kallsyms:
  1448. if (err)
  1449. kcore_copy__unlink(to_dir, "kallsyms");
  1450. return err;
  1451. }
  1452. int kcore_extract__create(struct kcore_extract *kce)
  1453. {
  1454. struct kcore kcore;
  1455. struct kcore extract;
  1456. size_t count = 1;
  1457. int idx = 0, err = -1;
  1458. off_t offset = page_size, sz;
  1459. if (kcore__open(&kcore, kce->kcore_filename))
  1460. return -1;
  1461. strcpy(kce->extract_filename, PERF_KCORE_EXTRACT);
  1462. if (kcore__init(&extract, kce->extract_filename, kcore.elfclass, true))
  1463. goto out_kcore_close;
  1464. if (kcore__copy_hdr(&kcore, &extract, count))
  1465. goto out_extract_close;
  1466. if (kcore__add_phdr(&extract, idx, offset, kce->addr, kce->len))
  1467. goto out_extract_close;
  1468. sz = kcore__write(&extract);
  1469. if (sz < 0 || sz > offset)
  1470. goto out_extract_close;
  1471. if (copy_bytes(kcore.fd, kce->offs, extract.fd, offset, kce->len))
  1472. goto out_extract_close;
  1473. err = 0;
  1474. out_extract_close:
  1475. kcore__close(&extract);
  1476. if (err)
  1477. unlink(kce->extract_filename);
  1478. out_kcore_close:
  1479. kcore__close(&kcore);
  1480. return err;
  1481. }
  1482. void kcore_extract__delete(struct kcore_extract *kce)
  1483. {
  1484. unlink(kce->extract_filename);
  1485. }
  1486. #ifdef HAVE_GELF_GETNOTE_SUPPORT
  1487. /**
  1488. * populate_sdt_note : Parse raw data and identify SDT note
  1489. * @elf: elf of the opened file
  1490. * @data: raw data of a section with description offset applied
  1491. * @len: note description size
  1492. * @type: type of the note
  1493. * @sdt_notes: List to add the SDT note
  1494. *
  1495. * Responsible for parsing the @data in section .note.stapsdt in @elf and
  1496. * if its an SDT note, it appends to @sdt_notes list.
  1497. */
  1498. static int populate_sdt_note(Elf **elf, const char *data, size_t len,
  1499. struct list_head *sdt_notes)
  1500. {
  1501. const char *provider, *name, *args;
  1502. struct sdt_note *tmp = NULL;
  1503. GElf_Ehdr ehdr;
  1504. GElf_Addr base_off = 0;
  1505. GElf_Shdr shdr;
  1506. int ret = -EINVAL;
  1507. union {
  1508. Elf64_Addr a64[NR_ADDR];
  1509. Elf32_Addr a32[NR_ADDR];
  1510. } buf;
  1511. Elf_Data dst = {
  1512. .d_buf = &buf, .d_type = ELF_T_ADDR, .d_version = EV_CURRENT,
  1513. .d_size = gelf_fsize((*elf), ELF_T_ADDR, NR_ADDR, EV_CURRENT),
  1514. .d_off = 0, .d_align = 0
  1515. };
  1516. Elf_Data src = {
  1517. .d_buf = (void *) data, .d_type = ELF_T_ADDR,
  1518. .d_version = EV_CURRENT, .d_size = dst.d_size, .d_off = 0,
  1519. .d_align = 0
  1520. };
  1521. tmp = (struct sdt_note *)calloc(1, sizeof(struct sdt_note));
  1522. if (!tmp) {
  1523. ret = -ENOMEM;
  1524. goto out_err;
  1525. }
  1526. INIT_LIST_HEAD(&tmp->note_list);
  1527. if (len < dst.d_size + 3)
  1528. goto out_free_note;
  1529. /* Translation from file representation to memory representation */
  1530. if (gelf_xlatetom(*elf, &dst, &src,
  1531. elf_getident(*elf, NULL)[EI_DATA]) == NULL) {
  1532. pr_err("gelf_xlatetom : %s\n", elf_errmsg(-1));
  1533. goto out_free_note;
  1534. }
  1535. /* Populate the fields of sdt_note */
  1536. provider = data + dst.d_size;
  1537. name = (const char *)memchr(provider, '\0', data + len - provider);
  1538. if (name++ == NULL)
  1539. goto out_free_note;
  1540. tmp->provider = strdup(provider);
  1541. if (!tmp->provider) {
  1542. ret = -ENOMEM;
  1543. goto out_free_note;
  1544. }
  1545. tmp->name = strdup(name);
  1546. if (!tmp->name) {
  1547. ret = -ENOMEM;
  1548. goto out_free_prov;
  1549. }
  1550. args = memchr(name, '\0', data + len - name);
  1551. /*
  1552. * There is no argument if:
  1553. * - We reached the end of the note;
  1554. * - There is not enough room to hold a potential string;
  1555. * - The argument string is empty or just contains ':'.
  1556. */
  1557. if (args == NULL || data + len - args < 2 ||
  1558. args[1] == ':' || args[1] == '\0')
  1559. tmp->args = NULL;
  1560. else {
  1561. tmp->args = strdup(++args);
  1562. if (!tmp->args) {
  1563. ret = -ENOMEM;
  1564. goto out_free_name;
  1565. }
  1566. }
  1567. if (gelf_getclass(*elf) == ELFCLASS32) {
  1568. memcpy(&tmp->addr, &buf, 3 * sizeof(Elf32_Addr));
  1569. tmp->bit32 = true;
  1570. } else {
  1571. memcpy(&tmp->addr, &buf, 3 * sizeof(Elf64_Addr));
  1572. tmp->bit32 = false;
  1573. }
  1574. if (!gelf_getehdr(*elf, &ehdr)) {
  1575. pr_debug("%s : cannot get elf header.\n", __func__);
  1576. ret = -EBADF;
  1577. goto out_free_args;
  1578. }
  1579. /* Adjust the prelink effect :
  1580. * Find out the .stapsdt.base section.
  1581. * This scn will help us to handle prelinking (if present).
  1582. * Compare the retrieved file offset of the base section with the
  1583. * base address in the description of the SDT note. If its different,
  1584. * then accordingly, adjust the note location.
  1585. */
  1586. if (elf_section_by_name(*elf, &ehdr, &shdr, SDT_BASE_SCN, NULL)) {
  1587. base_off = shdr.sh_offset;
  1588. if (base_off) {
  1589. if (tmp->bit32)
  1590. tmp->addr.a32[0] = tmp->addr.a32[0] + base_off -
  1591. tmp->addr.a32[1];
  1592. else
  1593. tmp->addr.a64[0] = tmp->addr.a64[0] + base_off -
  1594. tmp->addr.a64[1];
  1595. }
  1596. }
  1597. list_add_tail(&tmp->note_list, sdt_notes);
  1598. return 0;
  1599. out_free_args:
  1600. free(tmp->args);
  1601. out_free_name:
  1602. free(tmp->name);
  1603. out_free_prov:
  1604. free(tmp->provider);
  1605. out_free_note:
  1606. free(tmp);
  1607. out_err:
  1608. return ret;
  1609. }
  1610. /**
  1611. * construct_sdt_notes_list : constructs a list of SDT notes
  1612. * @elf : elf to look into
  1613. * @sdt_notes : empty list_head
  1614. *
  1615. * Scans the sections in 'elf' for the section
  1616. * .note.stapsdt. It, then calls populate_sdt_note to find
  1617. * out the SDT events and populates the 'sdt_notes'.
  1618. */
  1619. static int construct_sdt_notes_list(Elf *elf, struct list_head *sdt_notes)
  1620. {
  1621. GElf_Ehdr ehdr;
  1622. Elf_Scn *scn = NULL;
  1623. Elf_Data *data;
  1624. GElf_Shdr shdr;
  1625. size_t shstrndx, next;
  1626. GElf_Nhdr nhdr;
  1627. size_t name_off, desc_off, offset;
  1628. int ret = 0;
  1629. if (gelf_getehdr(elf, &ehdr) == NULL) {
  1630. ret = -EBADF;
  1631. goto out_ret;
  1632. }
  1633. if (elf_getshdrstrndx(elf, &shstrndx) != 0) {
  1634. ret = -EBADF;
  1635. goto out_ret;
  1636. }
  1637. /* Look for the required section */
  1638. scn = elf_section_by_name(elf, &ehdr, &shdr, SDT_NOTE_SCN, NULL);
  1639. if (!scn) {
  1640. ret = -ENOENT;
  1641. goto out_ret;
  1642. }
  1643. if ((shdr.sh_type != SHT_NOTE) || (shdr.sh_flags & SHF_ALLOC)) {
  1644. ret = -ENOENT;
  1645. goto out_ret;
  1646. }
  1647. data = elf_getdata(scn, NULL);
  1648. /* Get the SDT notes */
  1649. for (offset = 0; (next = gelf_getnote(data, offset, &nhdr, &name_off,
  1650. &desc_off)) > 0; offset = next) {
  1651. if (nhdr.n_namesz == sizeof(SDT_NOTE_NAME) &&
  1652. !memcmp(data->d_buf + name_off, SDT_NOTE_NAME,
  1653. sizeof(SDT_NOTE_NAME))) {
  1654. /* Check the type of the note */
  1655. if (nhdr.n_type != SDT_NOTE_TYPE)
  1656. goto out_ret;
  1657. ret = populate_sdt_note(&elf, ((data->d_buf) + desc_off),
  1658. nhdr.n_descsz, sdt_notes);
  1659. if (ret < 0)
  1660. goto out_ret;
  1661. }
  1662. }
  1663. if (list_empty(sdt_notes))
  1664. ret = -ENOENT;
  1665. out_ret:
  1666. return ret;
  1667. }
  1668. /**
  1669. * get_sdt_note_list : Wrapper to construct a list of sdt notes
  1670. * @head : empty list_head
  1671. * @target : file to find SDT notes from
  1672. *
  1673. * This opens the file, initializes
  1674. * the ELF and then calls construct_sdt_notes_list.
  1675. */
  1676. int get_sdt_note_list(struct list_head *head, const char *target)
  1677. {
  1678. Elf *elf;
  1679. int fd, ret;
  1680. fd = open(target, O_RDONLY);
  1681. if (fd < 0)
  1682. return -EBADF;
  1683. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  1684. if (!elf) {
  1685. ret = -EBADF;
  1686. goto out_close;
  1687. }
  1688. ret = construct_sdt_notes_list(elf, head);
  1689. elf_end(elf);
  1690. out_close:
  1691. close(fd);
  1692. return ret;
  1693. }
  1694. /**
  1695. * cleanup_sdt_note_list : free the sdt notes' list
  1696. * @sdt_notes: sdt notes' list
  1697. *
  1698. * Free up the SDT notes in @sdt_notes.
  1699. * Returns the number of SDT notes free'd.
  1700. */
  1701. int cleanup_sdt_note_list(struct list_head *sdt_notes)
  1702. {
  1703. struct sdt_note *tmp, *pos;
  1704. int nr_free = 0;
  1705. list_for_each_entry_safe(pos, tmp, sdt_notes, note_list) {
  1706. list_del(&pos->note_list);
  1707. free(pos->name);
  1708. free(pos->provider);
  1709. free(pos);
  1710. nr_free++;
  1711. }
  1712. return nr_free;
  1713. }
  1714. /**
  1715. * sdt_notes__get_count: Counts the number of sdt events
  1716. * @start: list_head to sdt_notes list
  1717. *
  1718. * Returns the number of SDT notes in a list
  1719. */
  1720. int sdt_notes__get_count(struct list_head *start)
  1721. {
  1722. struct sdt_note *sdt_ptr;
  1723. int count = 0;
  1724. list_for_each_entry(sdt_ptr, start, note_list)
  1725. count++;
  1726. return count;
  1727. }
  1728. #endif
  1729. void symbol__elf_init(void)
  1730. {
  1731. elf_version(EV_CURRENT);
  1732. }