symbol-elf.c 50 KB

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