symbol-elf.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740
  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 "machine.h"
  9. #include "vdso.h"
  10. #include <symbol/kallsyms.h>
  11. #include "debug.h"
  12. #ifdef HAVE_CPLUS_DEMANGLE_SUPPORT
  13. extern char *cplus_demangle(const char *, int);
  14. static inline char *bfd_demangle(void __maybe_unused *v, const char *c, int i)
  15. {
  16. return cplus_demangle(c, i);
  17. }
  18. #else
  19. #ifdef NO_DEMANGLE
  20. static inline char *bfd_demangle(void __maybe_unused *v,
  21. const char __maybe_unused *c,
  22. int __maybe_unused i)
  23. {
  24. return NULL;
  25. }
  26. #else
  27. #define PACKAGE 'perf'
  28. #include <bfd.h>
  29. #endif
  30. #endif
  31. #ifndef HAVE_ELF_GETPHDRNUM_SUPPORT
  32. static int elf_getphdrnum(Elf *elf, size_t *dst)
  33. {
  34. GElf_Ehdr gehdr;
  35. GElf_Ehdr *ehdr;
  36. ehdr = gelf_getehdr(elf, &gehdr);
  37. if (!ehdr)
  38. return -1;
  39. *dst = ehdr->e_phnum;
  40. return 0;
  41. }
  42. #endif
  43. #ifndef NT_GNU_BUILD_ID
  44. #define NT_GNU_BUILD_ID 3
  45. #endif
  46. /**
  47. * elf_symtab__for_each_symbol - iterate thru all the symbols
  48. *
  49. * @syms: struct elf_symtab instance to iterate
  50. * @idx: uint32_t idx
  51. * @sym: GElf_Sym iterator
  52. */
  53. #define elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) \
  54. for (idx = 0, gelf_getsym(syms, idx, &sym);\
  55. idx < nr_syms; \
  56. idx++, gelf_getsym(syms, idx, &sym))
  57. static inline uint8_t elf_sym__type(const GElf_Sym *sym)
  58. {
  59. return GELF_ST_TYPE(sym->st_info);
  60. }
  61. static inline int elf_sym__is_function(const GElf_Sym *sym)
  62. {
  63. return (elf_sym__type(sym) == STT_FUNC ||
  64. elf_sym__type(sym) == STT_GNU_IFUNC) &&
  65. sym->st_name != 0 &&
  66. sym->st_shndx != SHN_UNDEF;
  67. }
  68. static inline bool elf_sym__is_object(const GElf_Sym *sym)
  69. {
  70. return elf_sym__type(sym) == STT_OBJECT &&
  71. sym->st_name != 0 &&
  72. sym->st_shndx != SHN_UNDEF;
  73. }
  74. static inline int elf_sym__is_label(const GElf_Sym *sym)
  75. {
  76. return elf_sym__type(sym) == STT_NOTYPE &&
  77. sym->st_name != 0 &&
  78. sym->st_shndx != SHN_UNDEF &&
  79. sym->st_shndx != SHN_ABS;
  80. }
  81. static bool elf_sym__is_a(GElf_Sym *sym, enum map_type type)
  82. {
  83. switch (type) {
  84. case MAP__FUNCTION:
  85. return elf_sym__is_function(sym);
  86. case MAP__VARIABLE:
  87. return elf_sym__is_object(sym);
  88. default:
  89. return false;
  90. }
  91. }
  92. static inline const char *elf_sym__name(const GElf_Sym *sym,
  93. const Elf_Data *symstrs)
  94. {
  95. return symstrs->d_buf + sym->st_name;
  96. }
  97. static inline const char *elf_sec__name(const GElf_Shdr *shdr,
  98. const Elf_Data *secstrs)
  99. {
  100. return secstrs->d_buf + shdr->sh_name;
  101. }
  102. static inline int elf_sec__is_text(const GElf_Shdr *shdr,
  103. const Elf_Data *secstrs)
  104. {
  105. return strstr(elf_sec__name(shdr, secstrs), "text") != NULL;
  106. }
  107. static inline bool elf_sec__is_data(const GElf_Shdr *shdr,
  108. const Elf_Data *secstrs)
  109. {
  110. return strstr(elf_sec__name(shdr, secstrs), "data") != NULL;
  111. }
  112. static bool elf_sec__is_a(GElf_Shdr *shdr, Elf_Data *secstrs,
  113. enum map_type type)
  114. {
  115. switch (type) {
  116. case MAP__FUNCTION:
  117. return elf_sec__is_text(shdr, secstrs);
  118. case MAP__VARIABLE:
  119. return elf_sec__is_data(shdr, secstrs);
  120. default:
  121. return false;
  122. }
  123. }
  124. static size_t elf_addr_to_index(Elf *elf, GElf_Addr addr)
  125. {
  126. Elf_Scn *sec = NULL;
  127. GElf_Shdr shdr;
  128. size_t cnt = 1;
  129. while ((sec = elf_nextscn(elf, sec)) != NULL) {
  130. gelf_getshdr(sec, &shdr);
  131. if ((addr >= shdr.sh_addr) &&
  132. (addr < (shdr.sh_addr + shdr.sh_size)))
  133. return cnt;
  134. ++cnt;
  135. }
  136. return -1;
  137. }
  138. Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
  139. GElf_Shdr *shp, const char *name, size_t *idx)
  140. {
  141. Elf_Scn *sec = NULL;
  142. size_t cnt = 1;
  143. /* Elf is corrupted/truncated, avoid calling elf_strptr. */
  144. if (!elf_rawdata(elf_getscn(elf, ep->e_shstrndx), NULL))
  145. return NULL;
  146. while ((sec = elf_nextscn(elf, sec)) != NULL) {
  147. char *str;
  148. gelf_getshdr(sec, shp);
  149. str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
  150. if (str && !strcmp(name, str)) {
  151. if (idx)
  152. *idx = cnt;
  153. return sec;
  154. }
  155. ++cnt;
  156. }
  157. return NULL;
  158. }
  159. #define elf_section__for_each_rel(reldata, pos, pos_mem, idx, nr_entries) \
  160. for (idx = 0, pos = gelf_getrel(reldata, 0, &pos_mem); \
  161. idx < nr_entries; \
  162. ++idx, pos = gelf_getrel(reldata, idx, &pos_mem))
  163. #define elf_section__for_each_rela(reldata, pos, pos_mem, idx, nr_entries) \
  164. for (idx = 0, pos = gelf_getrela(reldata, 0, &pos_mem); \
  165. idx < nr_entries; \
  166. ++idx, pos = gelf_getrela(reldata, idx, &pos_mem))
  167. /*
  168. * We need to check if we have a .dynsym, so that we can handle the
  169. * .plt, synthesizing its symbols, that aren't on the symtabs (be it
  170. * .dynsym or .symtab).
  171. * And always look at the original dso, not at debuginfo packages, that
  172. * have the PLT data stripped out (shdr_rel_plt.sh_type == SHT_NOBITS).
  173. */
  174. int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss, struct map *map,
  175. symbol_filter_t filter)
  176. {
  177. uint32_t nr_rel_entries, idx;
  178. GElf_Sym sym;
  179. u64 plt_offset;
  180. GElf_Shdr shdr_plt;
  181. struct symbol *f;
  182. GElf_Shdr shdr_rel_plt, shdr_dynsym;
  183. Elf_Data *reldata, *syms, *symstrs;
  184. Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym;
  185. size_t dynsym_idx;
  186. GElf_Ehdr ehdr;
  187. char sympltname[1024];
  188. Elf *elf;
  189. int nr = 0, symidx, err = 0;
  190. if (!ss->dynsym)
  191. return 0;
  192. elf = ss->elf;
  193. ehdr = ss->ehdr;
  194. scn_dynsym = ss->dynsym;
  195. shdr_dynsym = ss->dynshdr;
  196. dynsym_idx = ss->dynsym_idx;
  197. if (scn_dynsym == NULL)
  198. goto out_elf_end;
  199. scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
  200. ".rela.plt", NULL);
  201. if (scn_plt_rel == NULL) {
  202. scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt,
  203. ".rel.plt", NULL);
  204. if (scn_plt_rel == NULL)
  205. goto out_elf_end;
  206. }
  207. err = -1;
  208. if (shdr_rel_plt.sh_link != dynsym_idx)
  209. goto out_elf_end;
  210. if (elf_section_by_name(elf, &ehdr, &shdr_plt, ".plt", NULL) == NULL)
  211. goto out_elf_end;
  212. /*
  213. * Fetch the relocation section to find the idxes to the GOT
  214. * and the symbols in the .dynsym they refer to.
  215. */
  216. reldata = elf_getdata(scn_plt_rel, NULL);
  217. if (reldata == NULL)
  218. goto out_elf_end;
  219. syms = elf_getdata(scn_dynsym, NULL);
  220. if (syms == NULL)
  221. goto out_elf_end;
  222. scn_symstrs = elf_getscn(elf, shdr_dynsym.sh_link);
  223. if (scn_symstrs == NULL)
  224. goto out_elf_end;
  225. symstrs = elf_getdata(scn_symstrs, NULL);
  226. if (symstrs == NULL)
  227. goto out_elf_end;
  228. if (symstrs->d_size == 0)
  229. goto out_elf_end;
  230. nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize;
  231. plt_offset = shdr_plt.sh_offset;
  232. if (shdr_rel_plt.sh_type == SHT_RELA) {
  233. GElf_Rela pos_mem, *pos;
  234. elf_section__for_each_rela(reldata, pos, pos_mem, idx,
  235. nr_rel_entries) {
  236. symidx = GELF_R_SYM(pos->r_info);
  237. plt_offset += shdr_plt.sh_entsize;
  238. gelf_getsym(syms, symidx, &sym);
  239. snprintf(sympltname, sizeof(sympltname),
  240. "%s@plt", elf_sym__name(&sym, symstrs));
  241. f = symbol__new(plt_offset, shdr_plt.sh_entsize,
  242. STB_GLOBAL, sympltname);
  243. if (!f)
  244. goto out_elf_end;
  245. if (filter && filter(map, f))
  246. symbol__delete(f);
  247. else {
  248. symbols__insert(&dso->symbols[map->type], f);
  249. ++nr;
  250. }
  251. }
  252. } else if (shdr_rel_plt.sh_type == SHT_REL) {
  253. GElf_Rel pos_mem, *pos;
  254. elf_section__for_each_rel(reldata, pos, pos_mem, idx,
  255. nr_rel_entries) {
  256. symidx = GELF_R_SYM(pos->r_info);
  257. plt_offset += shdr_plt.sh_entsize;
  258. gelf_getsym(syms, symidx, &sym);
  259. snprintf(sympltname, sizeof(sympltname),
  260. "%s@plt", elf_sym__name(&sym, symstrs));
  261. f = symbol__new(plt_offset, shdr_plt.sh_entsize,
  262. STB_GLOBAL, sympltname);
  263. if (!f)
  264. goto out_elf_end;
  265. if (filter && filter(map, f))
  266. symbol__delete(f);
  267. else {
  268. symbols__insert(&dso->symbols[map->type], f);
  269. ++nr;
  270. }
  271. }
  272. }
  273. err = 0;
  274. out_elf_end:
  275. if (err == 0)
  276. return nr;
  277. pr_debug("%s: problems reading %s PLT info.\n",
  278. __func__, dso->long_name);
  279. return 0;
  280. }
  281. /*
  282. * Align offset to 4 bytes as needed for note name and descriptor data.
  283. */
  284. #define NOTE_ALIGN(n) (((n) + 3) & -4U)
  285. static int elf_read_build_id(Elf *elf, void *bf, size_t size)
  286. {
  287. int err = -1;
  288. GElf_Ehdr ehdr;
  289. GElf_Shdr shdr;
  290. Elf_Data *data;
  291. Elf_Scn *sec;
  292. Elf_Kind ek;
  293. void *ptr;
  294. if (size < BUILD_ID_SIZE)
  295. goto out;
  296. ek = elf_kind(elf);
  297. if (ek != ELF_K_ELF)
  298. goto out;
  299. if (gelf_getehdr(elf, &ehdr) == NULL) {
  300. pr_err("%s: cannot get elf header.\n", __func__);
  301. goto out;
  302. }
  303. /*
  304. * Check following sections for notes:
  305. * '.note.gnu.build-id'
  306. * '.notes'
  307. * '.note' (VDSO specific)
  308. */
  309. do {
  310. sec = elf_section_by_name(elf, &ehdr, &shdr,
  311. ".note.gnu.build-id", NULL);
  312. if (sec)
  313. break;
  314. sec = elf_section_by_name(elf, &ehdr, &shdr,
  315. ".notes", NULL);
  316. if (sec)
  317. break;
  318. sec = elf_section_by_name(elf, &ehdr, &shdr,
  319. ".note", NULL);
  320. if (sec)
  321. break;
  322. return err;
  323. } while (0);
  324. data = elf_getdata(sec, NULL);
  325. if (data == NULL)
  326. goto out;
  327. ptr = data->d_buf;
  328. while (ptr < (data->d_buf + data->d_size)) {
  329. GElf_Nhdr *nhdr = ptr;
  330. size_t namesz = NOTE_ALIGN(nhdr->n_namesz),
  331. descsz = NOTE_ALIGN(nhdr->n_descsz);
  332. const char *name;
  333. ptr += sizeof(*nhdr);
  334. name = ptr;
  335. ptr += namesz;
  336. if (nhdr->n_type == NT_GNU_BUILD_ID &&
  337. nhdr->n_namesz == sizeof("GNU")) {
  338. if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
  339. size_t sz = min(size, descsz);
  340. memcpy(bf, ptr, sz);
  341. memset(bf + sz, 0, size - sz);
  342. err = descsz;
  343. break;
  344. }
  345. }
  346. ptr += descsz;
  347. }
  348. out:
  349. return err;
  350. }
  351. int filename__read_build_id(const char *filename, void *bf, size_t size)
  352. {
  353. int fd, err = -1;
  354. Elf *elf;
  355. if (size < BUILD_ID_SIZE)
  356. goto out;
  357. fd = open(filename, O_RDONLY);
  358. if (fd < 0)
  359. goto out;
  360. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  361. if (elf == NULL) {
  362. pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
  363. goto out_close;
  364. }
  365. err = elf_read_build_id(elf, bf, size);
  366. elf_end(elf);
  367. out_close:
  368. close(fd);
  369. out:
  370. return err;
  371. }
  372. int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
  373. {
  374. int fd, err = -1;
  375. if (size < BUILD_ID_SIZE)
  376. goto out;
  377. fd = open(filename, O_RDONLY);
  378. if (fd < 0)
  379. goto out;
  380. while (1) {
  381. char bf[BUFSIZ];
  382. GElf_Nhdr nhdr;
  383. size_t namesz, descsz;
  384. if (read(fd, &nhdr, sizeof(nhdr)) != sizeof(nhdr))
  385. break;
  386. namesz = NOTE_ALIGN(nhdr.n_namesz);
  387. descsz = NOTE_ALIGN(nhdr.n_descsz);
  388. if (nhdr.n_type == NT_GNU_BUILD_ID &&
  389. nhdr.n_namesz == sizeof("GNU")) {
  390. if (read(fd, bf, namesz) != (ssize_t)namesz)
  391. break;
  392. if (memcmp(bf, "GNU", sizeof("GNU")) == 0) {
  393. size_t sz = min(descsz, size);
  394. if (read(fd, build_id, sz) == (ssize_t)sz) {
  395. memset(build_id + sz, 0, size - sz);
  396. err = 0;
  397. break;
  398. }
  399. } else if (read(fd, bf, descsz) != (ssize_t)descsz)
  400. break;
  401. } else {
  402. int n = namesz + descsz;
  403. if (read(fd, bf, n) != n)
  404. break;
  405. }
  406. }
  407. close(fd);
  408. out:
  409. return err;
  410. }
  411. int filename__read_debuglink(const char *filename, char *debuglink,
  412. size_t size)
  413. {
  414. int fd, err = -1;
  415. Elf *elf;
  416. GElf_Ehdr ehdr;
  417. GElf_Shdr shdr;
  418. Elf_Data *data;
  419. Elf_Scn *sec;
  420. Elf_Kind ek;
  421. fd = open(filename, O_RDONLY);
  422. if (fd < 0)
  423. goto out;
  424. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  425. if (elf == NULL) {
  426. pr_debug2("%s: cannot read %s ELF file.\n", __func__, filename);
  427. goto out_close;
  428. }
  429. ek = elf_kind(elf);
  430. if (ek != ELF_K_ELF)
  431. goto out_elf_end;
  432. if (gelf_getehdr(elf, &ehdr) == NULL) {
  433. pr_err("%s: cannot get elf header.\n", __func__);
  434. goto out_elf_end;
  435. }
  436. sec = elf_section_by_name(elf, &ehdr, &shdr,
  437. ".gnu_debuglink", NULL);
  438. if (sec == NULL)
  439. goto out_elf_end;
  440. data = elf_getdata(sec, NULL);
  441. if (data == NULL)
  442. goto out_elf_end;
  443. /* the start of this section is a zero-terminated string */
  444. strncpy(debuglink, data->d_buf, size);
  445. err = 0;
  446. out_elf_end:
  447. elf_end(elf);
  448. out_close:
  449. close(fd);
  450. out:
  451. return err;
  452. }
  453. static int dso__swap_init(struct dso *dso, unsigned char eidata)
  454. {
  455. static unsigned int const endian = 1;
  456. dso->needs_swap = DSO_SWAP__NO;
  457. switch (eidata) {
  458. case ELFDATA2LSB:
  459. /* We are big endian, DSO is little endian. */
  460. if (*(unsigned char const *)&endian != 1)
  461. dso->needs_swap = DSO_SWAP__YES;
  462. break;
  463. case ELFDATA2MSB:
  464. /* We are little endian, DSO is big endian. */
  465. if (*(unsigned char const *)&endian != 0)
  466. dso->needs_swap = DSO_SWAP__YES;
  467. break;
  468. default:
  469. pr_err("unrecognized DSO data encoding %d\n", eidata);
  470. return -EINVAL;
  471. }
  472. return 0;
  473. }
  474. static int decompress_kmodule(struct dso *dso, const char *name,
  475. enum dso_binary_type type)
  476. {
  477. int fd;
  478. const char *ext = strrchr(name, '.');
  479. char tmpbuf[] = "/tmp/perf-kmod-XXXXXX";
  480. if (type != DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP &&
  481. type != DSO_BINARY_TYPE__GUEST_KMODULE_COMP &&
  482. type != DSO_BINARY_TYPE__BUILD_ID_CACHE)
  483. return -1;
  484. if (!ext || !is_supported_compression(ext + 1)) {
  485. ext = strrchr(dso->name, '.');
  486. if (!ext || !is_supported_compression(ext + 1))
  487. return -1;
  488. }
  489. fd = mkstemp(tmpbuf);
  490. if (fd < 0)
  491. return -1;
  492. if (!decompress_to_file(ext + 1, name, fd)) {
  493. close(fd);
  494. fd = -1;
  495. }
  496. unlink(tmpbuf);
  497. return fd;
  498. }
  499. bool symsrc__possibly_runtime(struct symsrc *ss)
  500. {
  501. return ss->dynsym || ss->opdsec;
  502. }
  503. bool symsrc__has_symtab(struct symsrc *ss)
  504. {
  505. return ss->symtab != NULL;
  506. }
  507. void symsrc__destroy(struct symsrc *ss)
  508. {
  509. zfree(&ss->name);
  510. elf_end(ss->elf);
  511. close(ss->fd);
  512. }
  513. int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name,
  514. enum dso_binary_type type)
  515. {
  516. int err = -1;
  517. GElf_Ehdr ehdr;
  518. Elf *elf;
  519. int fd;
  520. if (dso__needs_decompress(dso))
  521. fd = decompress_kmodule(dso, name, type);
  522. else
  523. fd = open(name, O_RDONLY);
  524. if (fd < 0)
  525. return -1;
  526. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  527. if (elf == NULL) {
  528. pr_debug("%s: cannot read %s ELF file.\n", __func__, name);
  529. goto out_close;
  530. }
  531. if (gelf_getehdr(elf, &ehdr) == NULL) {
  532. pr_debug("%s: cannot get elf header.\n", __func__);
  533. goto out_elf_end;
  534. }
  535. if (dso__swap_init(dso, ehdr.e_ident[EI_DATA]))
  536. goto out_elf_end;
  537. /* Always reject images with a mismatched build-id: */
  538. if (dso->has_build_id) {
  539. u8 build_id[BUILD_ID_SIZE];
  540. if (elf_read_build_id(elf, build_id, BUILD_ID_SIZE) < 0)
  541. goto out_elf_end;
  542. if (!dso__build_id_equal(dso, build_id))
  543. goto out_elf_end;
  544. }
  545. ss->is_64_bit = (gelf_getclass(elf) == ELFCLASS64);
  546. ss->symtab = elf_section_by_name(elf, &ehdr, &ss->symshdr, ".symtab",
  547. NULL);
  548. if (ss->symshdr.sh_type != SHT_SYMTAB)
  549. ss->symtab = NULL;
  550. ss->dynsym_idx = 0;
  551. ss->dynsym = elf_section_by_name(elf, &ehdr, &ss->dynshdr, ".dynsym",
  552. &ss->dynsym_idx);
  553. if (ss->dynshdr.sh_type != SHT_DYNSYM)
  554. ss->dynsym = NULL;
  555. ss->opdidx = 0;
  556. ss->opdsec = elf_section_by_name(elf, &ehdr, &ss->opdshdr, ".opd",
  557. &ss->opdidx);
  558. if (ss->opdshdr.sh_type != SHT_PROGBITS)
  559. ss->opdsec = NULL;
  560. if (dso->kernel == DSO_TYPE_USER) {
  561. GElf_Shdr shdr;
  562. ss->adjust_symbols = (ehdr.e_type == ET_EXEC ||
  563. ehdr.e_type == ET_REL ||
  564. dso__is_vdso(dso) ||
  565. elf_section_by_name(elf, &ehdr, &shdr,
  566. ".gnu.prelink_undo",
  567. NULL) != NULL);
  568. } else {
  569. ss->adjust_symbols = ehdr.e_type == ET_EXEC ||
  570. ehdr.e_type == ET_REL;
  571. }
  572. ss->name = strdup(name);
  573. if (!ss->name)
  574. goto out_elf_end;
  575. ss->elf = elf;
  576. ss->fd = fd;
  577. ss->ehdr = ehdr;
  578. ss->type = type;
  579. return 0;
  580. out_elf_end:
  581. elf_end(elf);
  582. out_close:
  583. close(fd);
  584. return err;
  585. }
  586. /**
  587. * ref_reloc_sym_not_found - has kernel relocation symbol been found.
  588. * @kmap: kernel maps and relocation reference symbol
  589. *
  590. * This function returns %true if we are dealing with the kernel maps and the
  591. * relocation reference symbol has not yet been found. Otherwise %false is
  592. * returned.
  593. */
  594. static bool ref_reloc_sym_not_found(struct kmap *kmap)
  595. {
  596. return kmap && kmap->ref_reloc_sym && kmap->ref_reloc_sym->name &&
  597. !kmap->ref_reloc_sym->unrelocated_addr;
  598. }
  599. /**
  600. * ref_reloc - kernel relocation offset.
  601. * @kmap: kernel maps and relocation reference symbol
  602. *
  603. * This function returns the offset of kernel addresses as determined by using
  604. * the relocation reference symbol i.e. if the kernel has not been relocated
  605. * then the return value is zero.
  606. */
  607. static u64 ref_reloc(struct kmap *kmap)
  608. {
  609. if (kmap && kmap->ref_reloc_sym &&
  610. kmap->ref_reloc_sym->unrelocated_addr)
  611. return kmap->ref_reloc_sym->addr -
  612. kmap->ref_reloc_sym->unrelocated_addr;
  613. return 0;
  614. }
  615. static bool want_demangle(bool is_kernel_sym)
  616. {
  617. return is_kernel_sym ? symbol_conf.demangle_kernel : symbol_conf.demangle;
  618. }
  619. int dso__load_sym(struct dso *dso, struct map *map,
  620. struct symsrc *syms_ss, struct symsrc *runtime_ss,
  621. symbol_filter_t filter, int kmodule)
  622. {
  623. struct kmap *kmap = dso->kernel ? map__kmap(map) : NULL;
  624. struct map *curr_map = map;
  625. struct dso *curr_dso = dso;
  626. Elf_Data *symstrs, *secstrs;
  627. uint32_t nr_syms;
  628. int err = -1;
  629. uint32_t idx;
  630. GElf_Ehdr ehdr;
  631. GElf_Shdr shdr;
  632. Elf_Data *syms, *opddata = NULL;
  633. GElf_Sym sym;
  634. Elf_Scn *sec, *sec_strndx;
  635. Elf *elf;
  636. int nr = 0;
  637. bool remap_kernel = false, adjust_kernel_syms = false;
  638. dso->symtab_type = syms_ss->type;
  639. dso->is_64_bit = syms_ss->is_64_bit;
  640. dso->rel = syms_ss->ehdr.e_type == ET_REL;
  641. /*
  642. * Modules may already have symbols from kallsyms, but those symbols
  643. * have the wrong values for the dso maps, so remove them.
  644. */
  645. if (kmodule && syms_ss->symtab)
  646. symbols__delete(&dso->symbols[map->type]);
  647. if (!syms_ss->symtab) {
  648. /*
  649. * If the vmlinux is stripped, fail so we will fall back
  650. * to using kallsyms. The vmlinux runtime symbols aren't
  651. * of much use.
  652. */
  653. if (dso->kernel)
  654. goto out_elf_end;
  655. syms_ss->symtab = syms_ss->dynsym;
  656. syms_ss->symshdr = syms_ss->dynshdr;
  657. }
  658. elf = syms_ss->elf;
  659. ehdr = syms_ss->ehdr;
  660. sec = syms_ss->symtab;
  661. shdr = syms_ss->symshdr;
  662. if (runtime_ss->opdsec)
  663. opddata = elf_rawdata(runtime_ss->opdsec, NULL);
  664. syms = elf_getdata(sec, NULL);
  665. if (syms == NULL)
  666. goto out_elf_end;
  667. sec = elf_getscn(elf, shdr.sh_link);
  668. if (sec == NULL)
  669. goto out_elf_end;
  670. symstrs = elf_getdata(sec, NULL);
  671. if (symstrs == NULL)
  672. goto out_elf_end;
  673. sec_strndx = elf_getscn(runtime_ss->elf, runtime_ss->ehdr.e_shstrndx);
  674. if (sec_strndx == NULL)
  675. goto out_elf_end;
  676. secstrs = elf_getdata(sec_strndx, NULL);
  677. if (secstrs == NULL)
  678. goto out_elf_end;
  679. nr_syms = shdr.sh_size / shdr.sh_entsize;
  680. memset(&sym, 0, sizeof(sym));
  681. /*
  682. * The kernel relocation symbol is needed in advance in order to adjust
  683. * kernel maps correctly.
  684. */
  685. if (ref_reloc_sym_not_found(kmap)) {
  686. elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
  687. const char *elf_name = elf_sym__name(&sym, symstrs);
  688. if (strcmp(elf_name, kmap->ref_reloc_sym->name))
  689. continue;
  690. kmap->ref_reloc_sym->unrelocated_addr = sym.st_value;
  691. map->reloc = kmap->ref_reloc_sym->addr -
  692. kmap->ref_reloc_sym->unrelocated_addr;
  693. break;
  694. }
  695. }
  696. dso->adjust_symbols = runtime_ss->adjust_symbols || ref_reloc(kmap);
  697. /*
  698. * Initial kernel and module mappings do not map to the dso. For
  699. * function mappings, flag the fixups.
  700. */
  701. if (map->type == MAP__FUNCTION && (dso->kernel || kmodule)) {
  702. remap_kernel = true;
  703. adjust_kernel_syms = dso->adjust_symbols;
  704. }
  705. elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
  706. struct symbol *f;
  707. const char *elf_name = elf_sym__name(&sym, symstrs);
  708. char *demangled = NULL;
  709. int is_label = elf_sym__is_label(&sym);
  710. const char *section_name;
  711. bool used_opd = false;
  712. if (!is_label && !elf_sym__is_a(&sym, map->type))
  713. continue;
  714. /* Reject ARM ELF "mapping symbols": these aren't unique and
  715. * don't identify functions, so will confuse the profile
  716. * output: */
  717. if (ehdr.e_machine == EM_ARM) {
  718. if (!strcmp(elf_name, "$a") ||
  719. !strcmp(elf_name, "$d") ||
  720. !strcmp(elf_name, "$t"))
  721. continue;
  722. }
  723. if (runtime_ss->opdsec && sym.st_shndx == runtime_ss->opdidx) {
  724. u32 offset = sym.st_value - syms_ss->opdshdr.sh_addr;
  725. u64 *opd = opddata->d_buf + offset;
  726. sym.st_value = DSO__SWAP(dso, u64, *opd);
  727. sym.st_shndx = elf_addr_to_index(runtime_ss->elf,
  728. sym.st_value);
  729. used_opd = true;
  730. }
  731. /*
  732. * When loading symbols in a data mapping, ABS symbols (which
  733. * has a value of SHN_ABS in its st_shndx) failed at
  734. * elf_getscn(). And it marks the loading as a failure so
  735. * already loaded symbols cannot be fixed up.
  736. *
  737. * I'm not sure what should be done. Just ignore them for now.
  738. * - Namhyung Kim
  739. */
  740. if (sym.st_shndx == SHN_ABS)
  741. continue;
  742. sec = elf_getscn(runtime_ss->elf, sym.st_shndx);
  743. if (!sec)
  744. goto out_elf_end;
  745. gelf_getshdr(sec, &shdr);
  746. if (is_label && !elf_sec__is_a(&shdr, secstrs, map->type))
  747. continue;
  748. section_name = elf_sec__name(&shdr, secstrs);
  749. /* On ARM, symbols for thumb functions have 1 added to
  750. * the symbol address as a flag - remove it */
  751. if ((ehdr.e_machine == EM_ARM) &&
  752. (map->type == MAP__FUNCTION) &&
  753. (sym.st_value & 1))
  754. --sym.st_value;
  755. if (dso->kernel || kmodule) {
  756. char dso_name[PATH_MAX];
  757. /* Adjust symbol to map to file offset */
  758. if (adjust_kernel_syms)
  759. sym.st_value -= shdr.sh_addr - shdr.sh_offset;
  760. if (strcmp(section_name,
  761. (curr_dso->short_name +
  762. dso->short_name_len)) == 0)
  763. goto new_symbol;
  764. if (strcmp(section_name, ".text") == 0) {
  765. /*
  766. * The initial kernel mapping is based on
  767. * kallsyms and identity maps. Overwrite it to
  768. * map to the kernel dso.
  769. */
  770. if (remap_kernel && dso->kernel) {
  771. remap_kernel = false;
  772. map->start = shdr.sh_addr +
  773. ref_reloc(kmap);
  774. map->end = map->start + shdr.sh_size;
  775. map->pgoff = shdr.sh_offset;
  776. map->map_ip = map__map_ip;
  777. map->unmap_ip = map__unmap_ip;
  778. /* Ensure maps are correctly ordered */
  779. map_groups__remove(kmap->kmaps, map);
  780. map_groups__insert(kmap->kmaps, map);
  781. }
  782. /*
  783. * The initial module mapping is based on
  784. * /proc/modules mapped to offset zero.
  785. * Overwrite it to map to the module dso.
  786. */
  787. if (remap_kernel && kmodule) {
  788. remap_kernel = false;
  789. map->pgoff = shdr.sh_offset;
  790. }
  791. curr_map = map;
  792. curr_dso = dso;
  793. goto new_symbol;
  794. }
  795. if (!kmap)
  796. goto new_symbol;
  797. snprintf(dso_name, sizeof(dso_name),
  798. "%s%s", dso->short_name, section_name);
  799. curr_map = map_groups__find_by_name(kmap->kmaps, map->type, dso_name);
  800. if (curr_map == NULL) {
  801. u64 start = sym.st_value;
  802. if (kmodule)
  803. start += map->start + shdr.sh_offset;
  804. curr_dso = dso__new(dso_name);
  805. if (curr_dso == NULL)
  806. goto out_elf_end;
  807. curr_dso->kernel = dso->kernel;
  808. curr_dso->long_name = dso->long_name;
  809. curr_dso->long_name_len = dso->long_name_len;
  810. curr_map = map__new2(start, curr_dso,
  811. map->type);
  812. if (curr_map == NULL) {
  813. dso__delete(curr_dso);
  814. goto out_elf_end;
  815. }
  816. if (adjust_kernel_syms) {
  817. curr_map->start = shdr.sh_addr +
  818. ref_reloc(kmap);
  819. curr_map->end = curr_map->start +
  820. shdr.sh_size;
  821. curr_map->pgoff = shdr.sh_offset;
  822. } else {
  823. curr_map->map_ip = identity__map_ip;
  824. curr_map->unmap_ip = identity__map_ip;
  825. }
  826. curr_dso->symtab_type = dso->symtab_type;
  827. map_groups__insert(kmap->kmaps, curr_map);
  828. /*
  829. * The new DSO should go to the kernel DSOS
  830. */
  831. dsos__add(&map->groups->machine->kernel_dsos,
  832. curr_dso);
  833. dso__set_loaded(curr_dso, map->type);
  834. } else
  835. curr_dso = curr_map->dso;
  836. goto new_symbol;
  837. }
  838. if ((used_opd && runtime_ss->adjust_symbols)
  839. || (!used_opd && syms_ss->adjust_symbols)) {
  840. pr_debug4("%s: adjusting symbol: st_value: %#" PRIx64 " "
  841. "sh_addr: %#" PRIx64 " sh_offset: %#" PRIx64 "\n", __func__,
  842. (u64)sym.st_value, (u64)shdr.sh_addr,
  843. (u64)shdr.sh_offset);
  844. sym.st_value -= shdr.sh_addr - shdr.sh_offset;
  845. }
  846. new_symbol:
  847. /*
  848. * We need to figure out if the object was created from C++ sources
  849. * DWARF DW_compile_unit has this, but we don't always have access
  850. * to it...
  851. */
  852. if (want_demangle(dso->kernel || kmodule)) {
  853. int demangle_flags = DMGL_NO_OPTS;
  854. if (verbose)
  855. demangle_flags = DMGL_PARAMS | DMGL_ANSI;
  856. demangled = bfd_demangle(NULL, elf_name, demangle_flags);
  857. if (demangled != NULL)
  858. elf_name = demangled;
  859. }
  860. f = symbol__new(sym.st_value, sym.st_size,
  861. GELF_ST_BIND(sym.st_info), elf_name);
  862. free(demangled);
  863. if (!f)
  864. goto out_elf_end;
  865. if (filter && filter(curr_map, f))
  866. symbol__delete(f);
  867. else {
  868. symbols__insert(&curr_dso->symbols[curr_map->type], f);
  869. nr++;
  870. }
  871. }
  872. /*
  873. * For misannotated, zeroed, ASM function sizes.
  874. */
  875. if (nr > 0) {
  876. symbols__fixup_duplicate(&dso->symbols[map->type]);
  877. symbols__fixup_end(&dso->symbols[map->type]);
  878. if (kmap) {
  879. /*
  880. * We need to fixup this here too because we create new
  881. * maps here, for things like vsyscall sections.
  882. */
  883. __map_groups__fixup_end(kmap->kmaps, map->type);
  884. }
  885. }
  886. err = nr;
  887. out_elf_end:
  888. return err;
  889. }
  890. static int elf_read_maps(Elf *elf, bool exe, mapfn_t mapfn, void *data)
  891. {
  892. GElf_Phdr phdr;
  893. size_t i, phdrnum;
  894. int err;
  895. u64 sz;
  896. if (elf_getphdrnum(elf, &phdrnum))
  897. return -1;
  898. for (i = 0; i < phdrnum; i++) {
  899. if (gelf_getphdr(elf, i, &phdr) == NULL)
  900. return -1;
  901. if (phdr.p_type != PT_LOAD)
  902. continue;
  903. if (exe) {
  904. if (!(phdr.p_flags & PF_X))
  905. continue;
  906. } else {
  907. if (!(phdr.p_flags & PF_R))
  908. continue;
  909. }
  910. sz = min(phdr.p_memsz, phdr.p_filesz);
  911. if (!sz)
  912. continue;
  913. err = mapfn(phdr.p_vaddr, sz, phdr.p_offset, data);
  914. if (err)
  915. return err;
  916. }
  917. return 0;
  918. }
  919. int file__read_maps(int fd, bool exe, mapfn_t mapfn, void *data,
  920. bool *is_64_bit)
  921. {
  922. int err;
  923. Elf *elf;
  924. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  925. if (elf == NULL)
  926. return -1;
  927. if (is_64_bit)
  928. *is_64_bit = (gelf_getclass(elf) == ELFCLASS64);
  929. err = elf_read_maps(elf, exe, mapfn, data);
  930. elf_end(elf);
  931. return err;
  932. }
  933. enum dso_type dso__type_fd(int fd)
  934. {
  935. enum dso_type dso_type = DSO__TYPE_UNKNOWN;
  936. GElf_Ehdr ehdr;
  937. Elf_Kind ek;
  938. Elf *elf;
  939. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  940. if (elf == NULL)
  941. goto out;
  942. ek = elf_kind(elf);
  943. if (ek != ELF_K_ELF)
  944. goto out_end;
  945. if (gelf_getclass(elf) == ELFCLASS64) {
  946. dso_type = DSO__TYPE_64BIT;
  947. goto out_end;
  948. }
  949. if (gelf_getehdr(elf, &ehdr) == NULL)
  950. goto out_end;
  951. if (ehdr.e_machine == EM_X86_64)
  952. dso_type = DSO__TYPE_X32BIT;
  953. else
  954. dso_type = DSO__TYPE_32BIT;
  955. out_end:
  956. elf_end(elf);
  957. out:
  958. return dso_type;
  959. }
  960. static int copy_bytes(int from, off_t from_offs, int to, off_t to_offs, u64 len)
  961. {
  962. ssize_t r;
  963. size_t n;
  964. int err = -1;
  965. char *buf = malloc(page_size);
  966. if (buf == NULL)
  967. return -1;
  968. if (lseek(to, to_offs, SEEK_SET) != to_offs)
  969. goto out;
  970. if (lseek(from, from_offs, SEEK_SET) != from_offs)
  971. goto out;
  972. while (len) {
  973. n = page_size;
  974. if (len < n)
  975. n = len;
  976. /* Use read because mmap won't work on proc files */
  977. r = read(from, buf, n);
  978. if (r < 0)
  979. goto out;
  980. if (!r)
  981. break;
  982. n = r;
  983. r = write(to, buf, n);
  984. if (r < 0)
  985. goto out;
  986. if ((size_t)r != n)
  987. goto out;
  988. len -= n;
  989. }
  990. err = 0;
  991. out:
  992. free(buf);
  993. return err;
  994. }
  995. struct kcore {
  996. int fd;
  997. int elfclass;
  998. Elf *elf;
  999. GElf_Ehdr ehdr;
  1000. };
  1001. static int kcore__open(struct kcore *kcore, const char *filename)
  1002. {
  1003. GElf_Ehdr *ehdr;
  1004. kcore->fd = open(filename, O_RDONLY);
  1005. if (kcore->fd == -1)
  1006. return -1;
  1007. kcore->elf = elf_begin(kcore->fd, ELF_C_READ, NULL);
  1008. if (!kcore->elf)
  1009. goto out_close;
  1010. kcore->elfclass = gelf_getclass(kcore->elf);
  1011. if (kcore->elfclass == ELFCLASSNONE)
  1012. goto out_end;
  1013. ehdr = gelf_getehdr(kcore->elf, &kcore->ehdr);
  1014. if (!ehdr)
  1015. goto out_end;
  1016. return 0;
  1017. out_end:
  1018. elf_end(kcore->elf);
  1019. out_close:
  1020. close(kcore->fd);
  1021. return -1;
  1022. }
  1023. static int kcore__init(struct kcore *kcore, char *filename, int elfclass,
  1024. bool temp)
  1025. {
  1026. GElf_Ehdr *ehdr;
  1027. kcore->elfclass = elfclass;
  1028. if (temp)
  1029. kcore->fd = mkstemp(filename);
  1030. else
  1031. kcore->fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0400);
  1032. if (kcore->fd == -1)
  1033. return -1;
  1034. kcore->elf = elf_begin(kcore->fd, ELF_C_WRITE, NULL);
  1035. if (!kcore->elf)
  1036. goto out_close;
  1037. if (!gelf_newehdr(kcore->elf, elfclass))
  1038. goto out_end;
  1039. ehdr = gelf_getehdr(kcore->elf, &kcore->ehdr);
  1040. if (!ehdr)
  1041. goto out_end;
  1042. return 0;
  1043. out_end:
  1044. elf_end(kcore->elf);
  1045. out_close:
  1046. close(kcore->fd);
  1047. unlink(filename);
  1048. return -1;
  1049. }
  1050. static void kcore__close(struct kcore *kcore)
  1051. {
  1052. elf_end(kcore->elf);
  1053. close(kcore->fd);
  1054. }
  1055. static int kcore__copy_hdr(struct kcore *from, struct kcore *to, size_t count)
  1056. {
  1057. GElf_Ehdr *ehdr = &to->ehdr;
  1058. GElf_Ehdr *kehdr = &from->ehdr;
  1059. memcpy(ehdr->e_ident, kehdr->e_ident, EI_NIDENT);
  1060. ehdr->e_type = kehdr->e_type;
  1061. ehdr->e_machine = kehdr->e_machine;
  1062. ehdr->e_version = kehdr->e_version;
  1063. ehdr->e_entry = 0;
  1064. ehdr->e_shoff = 0;
  1065. ehdr->e_flags = kehdr->e_flags;
  1066. ehdr->e_phnum = count;
  1067. ehdr->e_shentsize = 0;
  1068. ehdr->e_shnum = 0;
  1069. ehdr->e_shstrndx = 0;
  1070. if (from->elfclass == ELFCLASS32) {
  1071. ehdr->e_phoff = sizeof(Elf32_Ehdr);
  1072. ehdr->e_ehsize = sizeof(Elf32_Ehdr);
  1073. ehdr->e_phentsize = sizeof(Elf32_Phdr);
  1074. } else {
  1075. ehdr->e_phoff = sizeof(Elf64_Ehdr);
  1076. ehdr->e_ehsize = sizeof(Elf64_Ehdr);
  1077. ehdr->e_phentsize = sizeof(Elf64_Phdr);
  1078. }
  1079. if (!gelf_update_ehdr(to->elf, ehdr))
  1080. return -1;
  1081. if (!gelf_newphdr(to->elf, count))
  1082. return -1;
  1083. return 0;
  1084. }
  1085. static int kcore__add_phdr(struct kcore *kcore, int idx, off_t offset,
  1086. u64 addr, u64 len)
  1087. {
  1088. GElf_Phdr gphdr;
  1089. GElf_Phdr *phdr;
  1090. phdr = gelf_getphdr(kcore->elf, idx, &gphdr);
  1091. if (!phdr)
  1092. return -1;
  1093. phdr->p_type = PT_LOAD;
  1094. phdr->p_flags = PF_R | PF_W | PF_X;
  1095. phdr->p_offset = offset;
  1096. phdr->p_vaddr = addr;
  1097. phdr->p_paddr = 0;
  1098. phdr->p_filesz = len;
  1099. phdr->p_memsz = len;
  1100. phdr->p_align = page_size;
  1101. if (!gelf_update_phdr(kcore->elf, idx, phdr))
  1102. return -1;
  1103. return 0;
  1104. }
  1105. static off_t kcore__write(struct kcore *kcore)
  1106. {
  1107. return elf_update(kcore->elf, ELF_C_WRITE);
  1108. }
  1109. struct phdr_data {
  1110. off_t offset;
  1111. u64 addr;
  1112. u64 len;
  1113. };
  1114. struct kcore_copy_info {
  1115. u64 stext;
  1116. u64 etext;
  1117. u64 first_symbol;
  1118. u64 last_symbol;
  1119. u64 first_module;
  1120. u64 last_module_symbol;
  1121. struct phdr_data kernel_map;
  1122. struct phdr_data modules_map;
  1123. };
  1124. static int kcore_copy__process_kallsyms(void *arg, const char *name, char type,
  1125. u64 start)
  1126. {
  1127. struct kcore_copy_info *kci = arg;
  1128. if (!symbol_type__is_a(type, MAP__FUNCTION))
  1129. return 0;
  1130. if (strchr(name, '[')) {
  1131. if (start > kci->last_module_symbol)
  1132. kci->last_module_symbol = start;
  1133. return 0;
  1134. }
  1135. if (!kci->first_symbol || start < kci->first_symbol)
  1136. kci->first_symbol = start;
  1137. if (!kci->last_symbol || start > kci->last_symbol)
  1138. kci->last_symbol = start;
  1139. if (!strcmp(name, "_stext")) {
  1140. kci->stext = start;
  1141. return 0;
  1142. }
  1143. if (!strcmp(name, "_etext")) {
  1144. kci->etext = start;
  1145. return 0;
  1146. }
  1147. return 0;
  1148. }
  1149. static int kcore_copy__parse_kallsyms(struct kcore_copy_info *kci,
  1150. const char *dir)
  1151. {
  1152. char kallsyms_filename[PATH_MAX];
  1153. scnprintf(kallsyms_filename, PATH_MAX, "%s/kallsyms", dir);
  1154. if (symbol__restricted_filename(kallsyms_filename, "/proc/kallsyms"))
  1155. return -1;
  1156. if (kallsyms__parse(kallsyms_filename, kci,
  1157. kcore_copy__process_kallsyms) < 0)
  1158. return -1;
  1159. return 0;
  1160. }
  1161. static int kcore_copy__process_modules(void *arg,
  1162. const char *name __maybe_unused,
  1163. u64 start)
  1164. {
  1165. struct kcore_copy_info *kci = arg;
  1166. if (!kci->first_module || start < kci->first_module)
  1167. kci->first_module = start;
  1168. return 0;
  1169. }
  1170. static int kcore_copy__parse_modules(struct kcore_copy_info *kci,
  1171. const char *dir)
  1172. {
  1173. char modules_filename[PATH_MAX];
  1174. scnprintf(modules_filename, PATH_MAX, "%s/modules", dir);
  1175. if (symbol__restricted_filename(modules_filename, "/proc/modules"))
  1176. return -1;
  1177. if (modules__parse(modules_filename, kci,
  1178. kcore_copy__process_modules) < 0)
  1179. return -1;
  1180. return 0;
  1181. }
  1182. static void kcore_copy__map(struct phdr_data *p, u64 start, u64 end, u64 pgoff,
  1183. u64 s, u64 e)
  1184. {
  1185. if (p->addr || s < start || s >= end)
  1186. return;
  1187. p->addr = s;
  1188. p->offset = (s - start) + pgoff;
  1189. p->len = e < end ? e - s : end - s;
  1190. }
  1191. static int kcore_copy__read_map(u64 start, u64 len, u64 pgoff, void *data)
  1192. {
  1193. struct kcore_copy_info *kci = data;
  1194. u64 end = start + len;
  1195. kcore_copy__map(&kci->kernel_map, start, end, pgoff, kci->stext,
  1196. kci->etext);
  1197. kcore_copy__map(&kci->modules_map, start, end, pgoff, kci->first_module,
  1198. kci->last_module_symbol);
  1199. return 0;
  1200. }
  1201. static int kcore_copy__read_maps(struct kcore_copy_info *kci, Elf *elf)
  1202. {
  1203. if (elf_read_maps(elf, true, kcore_copy__read_map, kci) < 0)
  1204. return -1;
  1205. return 0;
  1206. }
  1207. static int kcore_copy__calc_maps(struct kcore_copy_info *kci, const char *dir,
  1208. Elf *elf)
  1209. {
  1210. if (kcore_copy__parse_kallsyms(kci, dir))
  1211. return -1;
  1212. if (kcore_copy__parse_modules(kci, dir))
  1213. return -1;
  1214. if (kci->stext)
  1215. kci->stext = round_down(kci->stext, page_size);
  1216. else
  1217. kci->stext = round_down(kci->first_symbol, page_size);
  1218. if (kci->etext) {
  1219. kci->etext = round_up(kci->etext, page_size);
  1220. } else if (kci->last_symbol) {
  1221. kci->etext = round_up(kci->last_symbol, page_size);
  1222. kci->etext += page_size;
  1223. }
  1224. kci->first_module = round_down(kci->first_module, page_size);
  1225. if (kci->last_module_symbol) {
  1226. kci->last_module_symbol = round_up(kci->last_module_symbol,
  1227. page_size);
  1228. kci->last_module_symbol += page_size;
  1229. }
  1230. if (!kci->stext || !kci->etext)
  1231. return -1;
  1232. if (kci->first_module && !kci->last_module_symbol)
  1233. return -1;
  1234. return kcore_copy__read_maps(kci, elf);
  1235. }
  1236. static int kcore_copy__copy_file(const char *from_dir, const char *to_dir,
  1237. const char *name)
  1238. {
  1239. char from_filename[PATH_MAX];
  1240. char to_filename[PATH_MAX];
  1241. scnprintf(from_filename, PATH_MAX, "%s/%s", from_dir, name);
  1242. scnprintf(to_filename, PATH_MAX, "%s/%s", to_dir, name);
  1243. return copyfile_mode(from_filename, to_filename, 0400);
  1244. }
  1245. static int kcore_copy__unlink(const char *dir, const char *name)
  1246. {
  1247. char filename[PATH_MAX];
  1248. scnprintf(filename, PATH_MAX, "%s/%s", dir, name);
  1249. return unlink(filename);
  1250. }
  1251. static int kcore_copy__compare_fds(int from, int to)
  1252. {
  1253. char *buf_from;
  1254. char *buf_to;
  1255. ssize_t ret;
  1256. size_t len;
  1257. int err = -1;
  1258. buf_from = malloc(page_size);
  1259. buf_to = malloc(page_size);
  1260. if (!buf_from || !buf_to)
  1261. goto out;
  1262. while (1) {
  1263. /* Use read because mmap won't work on proc files */
  1264. ret = read(from, buf_from, page_size);
  1265. if (ret < 0)
  1266. goto out;
  1267. if (!ret)
  1268. break;
  1269. len = ret;
  1270. if (readn(to, buf_to, len) != (int)len)
  1271. goto out;
  1272. if (memcmp(buf_from, buf_to, len))
  1273. goto out;
  1274. }
  1275. err = 0;
  1276. out:
  1277. free(buf_to);
  1278. free(buf_from);
  1279. return err;
  1280. }
  1281. static int kcore_copy__compare_files(const char *from_filename,
  1282. const char *to_filename)
  1283. {
  1284. int from, to, err = -1;
  1285. from = open(from_filename, O_RDONLY);
  1286. if (from < 0)
  1287. return -1;
  1288. to = open(to_filename, O_RDONLY);
  1289. if (to < 0)
  1290. goto out_close_from;
  1291. err = kcore_copy__compare_fds(from, to);
  1292. close(to);
  1293. out_close_from:
  1294. close(from);
  1295. return err;
  1296. }
  1297. static int kcore_copy__compare_file(const char *from_dir, const char *to_dir,
  1298. const char *name)
  1299. {
  1300. char from_filename[PATH_MAX];
  1301. char to_filename[PATH_MAX];
  1302. scnprintf(from_filename, PATH_MAX, "%s/%s", from_dir, name);
  1303. scnprintf(to_filename, PATH_MAX, "%s/%s", to_dir, name);
  1304. return kcore_copy__compare_files(from_filename, to_filename);
  1305. }
  1306. /**
  1307. * kcore_copy - copy kallsyms, modules and kcore from one directory to another.
  1308. * @from_dir: from directory
  1309. * @to_dir: to directory
  1310. *
  1311. * This function copies kallsyms, modules and kcore files from one directory to
  1312. * another. kallsyms and modules are copied entirely. Only code segments are
  1313. * copied from kcore. It is assumed that two segments suffice: one for the
  1314. * kernel proper and one for all the modules. The code segments are determined
  1315. * from kallsyms and modules files. The kernel map starts at _stext or the
  1316. * lowest function symbol, and ends at _etext or the highest function symbol.
  1317. * The module map starts at the lowest module address and ends at the highest
  1318. * module symbol. Start addresses are rounded down to the nearest page. End
  1319. * addresses are rounded up to the nearest page. An extra page is added to the
  1320. * highest kernel symbol and highest module symbol to, hopefully, encompass that
  1321. * symbol too. Because it contains only code sections, the resulting kcore is
  1322. * unusual. One significant peculiarity is that the mapping (start -> pgoff)
  1323. * is not the same for the kernel map and the modules map. That happens because
  1324. * the data is copied adjacently whereas the original kcore has gaps. Finally,
  1325. * kallsyms and modules files are compared with their copies to check that
  1326. * modules have not been loaded or unloaded while the copies were taking place.
  1327. *
  1328. * Return: %0 on success, %-1 on failure.
  1329. */
  1330. int kcore_copy(const char *from_dir, const char *to_dir)
  1331. {
  1332. struct kcore kcore;
  1333. struct kcore extract;
  1334. size_t count = 2;
  1335. int idx = 0, err = -1;
  1336. off_t offset = page_size, sz, modules_offset = 0;
  1337. struct kcore_copy_info kci = { .stext = 0, };
  1338. char kcore_filename[PATH_MAX];
  1339. char extract_filename[PATH_MAX];
  1340. if (kcore_copy__copy_file(from_dir, to_dir, "kallsyms"))
  1341. return -1;
  1342. if (kcore_copy__copy_file(from_dir, to_dir, "modules"))
  1343. goto out_unlink_kallsyms;
  1344. scnprintf(kcore_filename, PATH_MAX, "%s/kcore", from_dir);
  1345. scnprintf(extract_filename, PATH_MAX, "%s/kcore", to_dir);
  1346. if (kcore__open(&kcore, kcore_filename))
  1347. goto out_unlink_modules;
  1348. if (kcore_copy__calc_maps(&kci, from_dir, kcore.elf))
  1349. goto out_kcore_close;
  1350. if (kcore__init(&extract, extract_filename, kcore.elfclass, false))
  1351. goto out_kcore_close;
  1352. if (!kci.modules_map.addr)
  1353. count -= 1;
  1354. if (kcore__copy_hdr(&kcore, &extract, count))
  1355. goto out_extract_close;
  1356. if (kcore__add_phdr(&extract, idx++, offset, kci.kernel_map.addr,
  1357. kci.kernel_map.len))
  1358. goto out_extract_close;
  1359. if (kci.modules_map.addr) {
  1360. modules_offset = offset + kci.kernel_map.len;
  1361. if (kcore__add_phdr(&extract, idx, modules_offset,
  1362. kci.modules_map.addr, kci.modules_map.len))
  1363. goto out_extract_close;
  1364. }
  1365. sz = kcore__write(&extract);
  1366. if (sz < 0 || sz > offset)
  1367. goto out_extract_close;
  1368. if (copy_bytes(kcore.fd, kci.kernel_map.offset, extract.fd, offset,
  1369. kci.kernel_map.len))
  1370. goto out_extract_close;
  1371. if (modules_offset && copy_bytes(kcore.fd, kci.modules_map.offset,
  1372. extract.fd, modules_offset,
  1373. kci.modules_map.len))
  1374. goto out_extract_close;
  1375. if (kcore_copy__compare_file(from_dir, to_dir, "modules"))
  1376. goto out_extract_close;
  1377. if (kcore_copy__compare_file(from_dir, to_dir, "kallsyms"))
  1378. goto out_extract_close;
  1379. err = 0;
  1380. out_extract_close:
  1381. kcore__close(&extract);
  1382. if (err)
  1383. unlink(extract_filename);
  1384. out_kcore_close:
  1385. kcore__close(&kcore);
  1386. out_unlink_modules:
  1387. if (err)
  1388. kcore_copy__unlink(to_dir, "modules");
  1389. out_unlink_kallsyms:
  1390. if (err)
  1391. kcore_copy__unlink(to_dir, "kallsyms");
  1392. return err;
  1393. }
  1394. int kcore_extract__create(struct kcore_extract *kce)
  1395. {
  1396. struct kcore kcore;
  1397. struct kcore extract;
  1398. size_t count = 1;
  1399. int idx = 0, err = -1;
  1400. off_t offset = page_size, sz;
  1401. if (kcore__open(&kcore, kce->kcore_filename))
  1402. return -1;
  1403. strcpy(kce->extract_filename, PERF_KCORE_EXTRACT);
  1404. if (kcore__init(&extract, kce->extract_filename, kcore.elfclass, true))
  1405. goto out_kcore_close;
  1406. if (kcore__copy_hdr(&kcore, &extract, count))
  1407. goto out_extract_close;
  1408. if (kcore__add_phdr(&extract, idx, offset, kce->addr, kce->len))
  1409. goto out_extract_close;
  1410. sz = kcore__write(&extract);
  1411. if (sz < 0 || sz > offset)
  1412. goto out_extract_close;
  1413. if (copy_bytes(kcore.fd, kce->offs, extract.fd, offset, kce->len))
  1414. goto out_extract_close;
  1415. err = 0;
  1416. out_extract_close:
  1417. kcore__close(&extract);
  1418. if (err)
  1419. unlink(kce->extract_filename);
  1420. out_kcore_close:
  1421. kcore__close(&kcore);
  1422. return err;
  1423. }
  1424. void kcore_extract__delete(struct kcore_extract *kce)
  1425. {
  1426. unlink(kce->extract_filename);
  1427. }
  1428. void symbol__elf_init(void)
  1429. {
  1430. elf_version(EV_CURRENT);
  1431. }