symbol-elf.c 46 KB

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