symbol-elf.c 37 KB

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