symbol-elf.c 46 KB

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