symbol-elf.c 51 KB

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