symbol-elf.c 47 KB

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