symbol-elf.c 50 KB

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