symbol-elf.c 38 KB

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