symbol-elf.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683
  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. bool symsrc__possibly_runtime(struct symsrc *ss)
  456. {
  457. return ss->dynsym || ss->opdsec;
  458. }
  459. bool symsrc__has_symtab(struct symsrc *ss)
  460. {
  461. return ss->symtab != NULL;
  462. }
  463. void symsrc__destroy(struct symsrc *ss)
  464. {
  465. zfree(&ss->name);
  466. elf_end(ss->elf);
  467. close(ss->fd);
  468. }
  469. int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name,
  470. enum dso_binary_type type)
  471. {
  472. int err = -1;
  473. GElf_Ehdr ehdr;
  474. Elf *elf;
  475. int fd;
  476. fd = open(name, O_RDONLY);
  477. if (fd < 0)
  478. return -1;
  479. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  480. if (elf == NULL) {
  481. pr_debug("%s: cannot read %s ELF file.\n", __func__, name);
  482. goto out_close;
  483. }
  484. if (gelf_getehdr(elf, &ehdr) == NULL) {
  485. pr_debug("%s: cannot get elf header.\n", __func__);
  486. goto out_elf_end;
  487. }
  488. if (dso__swap_init(dso, ehdr.e_ident[EI_DATA]))
  489. goto out_elf_end;
  490. /* Always reject images with a mismatched build-id: */
  491. if (dso->has_build_id) {
  492. u8 build_id[BUILD_ID_SIZE];
  493. if (elf_read_build_id(elf, build_id, BUILD_ID_SIZE) < 0)
  494. goto out_elf_end;
  495. if (!dso__build_id_equal(dso, build_id))
  496. goto out_elf_end;
  497. }
  498. ss->is_64_bit = (gelf_getclass(elf) == ELFCLASS64);
  499. ss->symtab = elf_section_by_name(elf, &ehdr, &ss->symshdr, ".symtab",
  500. NULL);
  501. if (ss->symshdr.sh_type != SHT_SYMTAB)
  502. ss->symtab = NULL;
  503. ss->dynsym_idx = 0;
  504. ss->dynsym = elf_section_by_name(elf, &ehdr, &ss->dynshdr, ".dynsym",
  505. &ss->dynsym_idx);
  506. if (ss->dynshdr.sh_type != SHT_DYNSYM)
  507. ss->dynsym = NULL;
  508. ss->opdidx = 0;
  509. ss->opdsec = elf_section_by_name(elf, &ehdr, &ss->opdshdr, ".opd",
  510. &ss->opdidx);
  511. if (ss->opdshdr.sh_type != SHT_PROGBITS)
  512. ss->opdsec = NULL;
  513. if (dso->kernel == DSO_TYPE_USER) {
  514. GElf_Shdr shdr;
  515. ss->adjust_symbols = (ehdr.e_type == ET_EXEC ||
  516. ehdr.e_type == ET_REL ||
  517. dso__is_vdso(dso) ||
  518. elf_section_by_name(elf, &ehdr, &shdr,
  519. ".gnu.prelink_undo",
  520. NULL) != NULL);
  521. } else {
  522. ss->adjust_symbols = ehdr.e_type == ET_EXEC ||
  523. ehdr.e_type == ET_REL;
  524. }
  525. ss->name = strdup(name);
  526. if (!ss->name)
  527. goto out_elf_end;
  528. ss->elf = elf;
  529. ss->fd = fd;
  530. ss->ehdr = ehdr;
  531. ss->type = type;
  532. return 0;
  533. out_elf_end:
  534. elf_end(elf);
  535. out_close:
  536. close(fd);
  537. return err;
  538. }
  539. /**
  540. * ref_reloc_sym_not_found - has kernel relocation symbol been found.
  541. * @kmap: kernel maps and relocation reference symbol
  542. *
  543. * This function returns %true if we are dealing with the kernel maps and the
  544. * relocation reference symbol has not yet been found. Otherwise %false is
  545. * returned.
  546. */
  547. static bool ref_reloc_sym_not_found(struct kmap *kmap)
  548. {
  549. return kmap && kmap->ref_reloc_sym && kmap->ref_reloc_sym->name &&
  550. !kmap->ref_reloc_sym->unrelocated_addr;
  551. }
  552. /**
  553. * ref_reloc - kernel relocation offset.
  554. * @kmap: kernel maps and relocation reference symbol
  555. *
  556. * This function returns the offset of kernel addresses as determined by using
  557. * the relocation reference symbol i.e. if the kernel has not been relocated
  558. * then the return value is zero.
  559. */
  560. static u64 ref_reloc(struct kmap *kmap)
  561. {
  562. if (kmap && kmap->ref_reloc_sym &&
  563. kmap->ref_reloc_sym->unrelocated_addr)
  564. return kmap->ref_reloc_sym->addr -
  565. kmap->ref_reloc_sym->unrelocated_addr;
  566. return 0;
  567. }
  568. static bool want_demangle(bool is_kernel_sym)
  569. {
  570. return is_kernel_sym ? symbol_conf.demangle_kernel : symbol_conf.demangle;
  571. }
  572. int dso__load_sym(struct dso *dso, struct map *map,
  573. struct symsrc *syms_ss, struct symsrc *runtime_ss,
  574. symbol_filter_t filter, int kmodule)
  575. {
  576. struct kmap *kmap = dso->kernel ? map__kmap(map) : NULL;
  577. struct map *curr_map = map;
  578. struct dso *curr_dso = dso;
  579. Elf_Data *symstrs, *secstrs;
  580. uint32_t nr_syms;
  581. int err = -1;
  582. uint32_t idx;
  583. GElf_Ehdr ehdr;
  584. GElf_Shdr shdr;
  585. Elf_Data *syms, *opddata = NULL;
  586. GElf_Sym sym;
  587. Elf_Scn *sec, *sec_strndx;
  588. Elf *elf;
  589. int nr = 0;
  590. bool remap_kernel = false, adjust_kernel_syms = false;
  591. dso->symtab_type = syms_ss->type;
  592. dso->is_64_bit = syms_ss->is_64_bit;
  593. dso->rel = syms_ss->ehdr.e_type == ET_REL;
  594. /*
  595. * Modules may already have symbols from kallsyms, but those symbols
  596. * have the wrong values for the dso maps, so remove them.
  597. */
  598. if (kmodule && syms_ss->symtab)
  599. symbols__delete(&dso->symbols[map->type]);
  600. if (!syms_ss->symtab) {
  601. /*
  602. * If the vmlinux is stripped, fail so we will fall back
  603. * to using kallsyms. The vmlinux runtime symbols aren't
  604. * of much use.
  605. */
  606. if (dso->kernel)
  607. goto out_elf_end;
  608. syms_ss->symtab = syms_ss->dynsym;
  609. syms_ss->symshdr = syms_ss->dynshdr;
  610. }
  611. elf = syms_ss->elf;
  612. ehdr = syms_ss->ehdr;
  613. sec = syms_ss->symtab;
  614. shdr = syms_ss->symshdr;
  615. if (runtime_ss->opdsec)
  616. opddata = elf_rawdata(runtime_ss->opdsec, NULL);
  617. syms = elf_getdata(sec, NULL);
  618. if (syms == NULL)
  619. goto out_elf_end;
  620. sec = elf_getscn(elf, shdr.sh_link);
  621. if (sec == NULL)
  622. goto out_elf_end;
  623. symstrs = elf_getdata(sec, NULL);
  624. if (symstrs == NULL)
  625. goto out_elf_end;
  626. sec_strndx = elf_getscn(runtime_ss->elf, runtime_ss->ehdr.e_shstrndx);
  627. if (sec_strndx == NULL)
  628. goto out_elf_end;
  629. secstrs = elf_getdata(sec_strndx, NULL);
  630. if (secstrs == NULL)
  631. goto out_elf_end;
  632. nr_syms = shdr.sh_size / shdr.sh_entsize;
  633. memset(&sym, 0, sizeof(sym));
  634. /*
  635. * The kernel relocation symbol is needed in advance in order to adjust
  636. * kernel maps correctly.
  637. */
  638. if (ref_reloc_sym_not_found(kmap)) {
  639. elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
  640. const char *elf_name = elf_sym__name(&sym, symstrs);
  641. if (strcmp(elf_name, kmap->ref_reloc_sym->name))
  642. continue;
  643. kmap->ref_reloc_sym->unrelocated_addr = sym.st_value;
  644. map->reloc = kmap->ref_reloc_sym->addr -
  645. kmap->ref_reloc_sym->unrelocated_addr;
  646. break;
  647. }
  648. }
  649. dso->adjust_symbols = runtime_ss->adjust_symbols || ref_reloc(kmap);
  650. /*
  651. * Initial kernel and module mappings do not map to the dso. For
  652. * function mappings, flag the fixups.
  653. */
  654. if (map->type == MAP__FUNCTION && (dso->kernel || kmodule)) {
  655. remap_kernel = true;
  656. adjust_kernel_syms = dso->adjust_symbols;
  657. }
  658. elf_symtab__for_each_symbol(syms, nr_syms, idx, sym) {
  659. struct symbol *f;
  660. const char *elf_name = elf_sym__name(&sym, symstrs);
  661. char *demangled = NULL;
  662. int is_label = elf_sym__is_label(&sym);
  663. const char *section_name;
  664. bool used_opd = false;
  665. if (!is_label && !elf_sym__is_a(&sym, map->type))
  666. continue;
  667. /* Reject ARM ELF "mapping symbols": these aren't unique and
  668. * don't identify functions, so will confuse the profile
  669. * output: */
  670. if (ehdr.e_machine == EM_ARM) {
  671. if (!strcmp(elf_name, "$a") ||
  672. !strcmp(elf_name, "$d") ||
  673. !strcmp(elf_name, "$t"))
  674. continue;
  675. }
  676. if (runtime_ss->opdsec && sym.st_shndx == runtime_ss->opdidx) {
  677. u32 offset = sym.st_value - syms_ss->opdshdr.sh_addr;
  678. u64 *opd = opddata->d_buf + offset;
  679. sym.st_value = DSO__SWAP(dso, u64, *opd);
  680. sym.st_shndx = elf_addr_to_index(runtime_ss->elf,
  681. sym.st_value);
  682. used_opd = true;
  683. }
  684. /*
  685. * When loading symbols in a data mapping, ABS symbols (which
  686. * has a value of SHN_ABS in its st_shndx) failed at
  687. * elf_getscn(). And it marks the loading as a failure so
  688. * already loaded symbols cannot be fixed up.
  689. *
  690. * I'm not sure what should be done. Just ignore them for now.
  691. * - Namhyung Kim
  692. */
  693. if (sym.st_shndx == SHN_ABS)
  694. continue;
  695. sec = elf_getscn(runtime_ss->elf, sym.st_shndx);
  696. if (!sec)
  697. goto out_elf_end;
  698. gelf_getshdr(sec, &shdr);
  699. if (is_label && !elf_sec__is_a(&shdr, secstrs, map->type))
  700. continue;
  701. section_name = elf_sec__name(&shdr, secstrs);
  702. /* On ARM, symbols for thumb functions have 1 added to
  703. * the symbol address as a flag - remove it */
  704. if ((ehdr.e_machine == EM_ARM) &&
  705. (map->type == MAP__FUNCTION) &&
  706. (sym.st_value & 1))
  707. --sym.st_value;
  708. if (dso->kernel || kmodule) {
  709. char dso_name[PATH_MAX];
  710. /* Adjust symbol to map to file offset */
  711. if (adjust_kernel_syms)
  712. sym.st_value -= shdr.sh_addr - shdr.sh_offset;
  713. if (strcmp(section_name,
  714. (curr_dso->short_name +
  715. dso->short_name_len)) == 0)
  716. goto new_symbol;
  717. if (strcmp(section_name, ".text") == 0) {
  718. /*
  719. * The initial kernel mapping is based on
  720. * kallsyms and identity maps. Overwrite it to
  721. * map to the kernel dso.
  722. */
  723. if (remap_kernel && dso->kernel) {
  724. remap_kernel = false;
  725. map->start = shdr.sh_addr +
  726. ref_reloc(kmap);
  727. map->end = map->start + shdr.sh_size;
  728. map->pgoff = shdr.sh_offset;
  729. map->map_ip = map__map_ip;
  730. map->unmap_ip = map__unmap_ip;
  731. /* Ensure maps are correctly ordered */
  732. map_groups__remove(kmap->kmaps, map);
  733. map_groups__insert(kmap->kmaps, map);
  734. }
  735. /*
  736. * The initial module mapping is based on
  737. * /proc/modules mapped to offset zero.
  738. * Overwrite it to map to the module dso.
  739. */
  740. if (remap_kernel && kmodule) {
  741. remap_kernel = false;
  742. map->pgoff = shdr.sh_offset;
  743. }
  744. curr_map = map;
  745. curr_dso = dso;
  746. goto new_symbol;
  747. }
  748. if (!kmap)
  749. goto new_symbol;
  750. snprintf(dso_name, sizeof(dso_name),
  751. "%s%s", dso->short_name, section_name);
  752. curr_map = map_groups__find_by_name(kmap->kmaps, map->type, dso_name);
  753. if (curr_map == NULL) {
  754. u64 start = sym.st_value;
  755. if (kmodule)
  756. start += map->start + shdr.sh_offset;
  757. curr_dso = dso__new(dso_name);
  758. if (curr_dso == NULL)
  759. goto out_elf_end;
  760. curr_dso->kernel = dso->kernel;
  761. curr_dso->long_name = dso->long_name;
  762. curr_dso->long_name_len = dso->long_name_len;
  763. curr_map = map__new2(start, curr_dso,
  764. map->type);
  765. if (curr_map == NULL) {
  766. dso__delete(curr_dso);
  767. goto out_elf_end;
  768. }
  769. if (adjust_kernel_syms) {
  770. curr_map->start = shdr.sh_addr +
  771. ref_reloc(kmap);
  772. curr_map->end = curr_map->start +
  773. shdr.sh_size;
  774. curr_map->pgoff = shdr.sh_offset;
  775. } else {
  776. curr_map->map_ip = identity__map_ip;
  777. curr_map->unmap_ip = identity__map_ip;
  778. }
  779. curr_dso->symtab_type = dso->symtab_type;
  780. map_groups__insert(kmap->kmaps, curr_map);
  781. /*
  782. * The new DSO should go to the kernel DSOS
  783. */
  784. dsos__add(&map->groups->machine->kernel_dsos,
  785. curr_dso);
  786. dso__set_loaded(curr_dso, map->type);
  787. } else
  788. curr_dso = curr_map->dso;
  789. goto new_symbol;
  790. }
  791. if ((used_opd && runtime_ss->adjust_symbols)
  792. || (!used_opd && syms_ss->adjust_symbols)) {
  793. pr_debug4("%s: adjusting symbol: st_value: %#" PRIx64 " "
  794. "sh_addr: %#" PRIx64 " sh_offset: %#" PRIx64 "\n", __func__,
  795. (u64)sym.st_value, (u64)shdr.sh_addr,
  796. (u64)shdr.sh_offset);
  797. sym.st_value -= shdr.sh_addr - shdr.sh_offset;
  798. }
  799. new_symbol:
  800. /*
  801. * We need to figure out if the object was created from C++ sources
  802. * DWARF DW_compile_unit has this, but we don't always have access
  803. * to it...
  804. */
  805. if (want_demangle(dso->kernel || kmodule)) {
  806. int demangle_flags = DMGL_NO_OPTS;
  807. if (verbose)
  808. demangle_flags = DMGL_PARAMS | DMGL_ANSI;
  809. demangled = bfd_demangle(NULL, elf_name, demangle_flags);
  810. if (demangled != NULL)
  811. elf_name = demangled;
  812. }
  813. f = symbol__new(sym.st_value, sym.st_size,
  814. GELF_ST_BIND(sym.st_info), elf_name);
  815. free(demangled);
  816. if (!f)
  817. goto out_elf_end;
  818. if (filter && filter(curr_map, f))
  819. symbol__delete(f);
  820. else {
  821. symbols__insert(&curr_dso->symbols[curr_map->type], f);
  822. nr++;
  823. }
  824. }
  825. /*
  826. * For misannotated, zeroed, ASM function sizes.
  827. */
  828. if (nr > 0) {
  829. symbols__fixup_duplicate(&dso->symbols[map->type]);
  830. symbols__fixup_end(&dso->symbols[map->type]);
  831. if (kmap) {
  832. /*
  833. * We need to fixup this here too because we create new
  834. * maps here, for things like vsyscall sections.
  835. */
  836. __map_groups__fixup_end(kmap->kmaps, map->type);
  837. }
  838. }
  839. err = nr;
  840. out_elf_end:
  841. return err;
  842. }
  843. static int elf_read_maps(Elf *elf, bool exe, mapfn_t mapfn, void *data)
  844. {
  845. GElf_Phdr phdr;
  846. size_t i, phdrnum;
  847. int err;
  848. u64 sz;
  849. if (elf_getphdrnum(elf, &phdrnum))
  850. return -1;
  851. for (i = 0; i < phdrnum; i++) {
  852. if (gelf_getphdr(elf, i, &phdr) == NULL)
  853. return -1;
  854. if (phdr.p_type != PT_LOAD)
  855. continue;
  856. if (exe) {
  857. if (!(phdr.p_flags & PF_X))
  858. continue;
  859. } else {
  860. if (!(phdr.p_flags & PF_R))
  861. continue;
  862. }
  863. sz = min(phdr.p_memsz, phdr.p_filesz);
  864. if (!sz)
  865. continue;
  866. err = mapfn(phdr.p_vaddr, sz, phdr.p_offset, data);
  867. if (err)
  868. return err;
  869. }
  870. return 0;
  871. }
  872. int file__read_maps(int fd, bool exe, mapfn_t mapfn, void *data,
  873. bool *is_64_bit)
  874. {
  875. int err;
  876. Elf *elf;
  877. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  878. if (elf == NULL)
  879. return -1;
  880. if (is_64_bit)
  881. *is_64_bit = (gelf_getclass(elf) == ELFCLASS64);
  882. err = elf_read_maps(elf, exe, mapfn, data);
  883. elf_end(elf);
  884. return err;
  885. }
  886. enum dso_type dso__type_fd(int fd)
  887. {
  888. enum dso_type dso_type = DSO__TYPE_UNKNOWN;
  889. GElf_Ehdr ehdr;
  890. Elf_Kind ek;
  891. Elf *elf;
  892. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  893. if (elf == NULL)
  894. goto out;
  895. ek = elf_kind(elf);
  896. if (ek != ELF_K_ELF)
  897. goto out_end;
  898. if (gelf_getclass(elf) == ELFCLASS64) {
  899. dso_type = DSO__TYPE_64BIT;
  900. goto out_end;
  901. }
  902. if (gelf_getehdr(elf, &ehdr) == NULL)
  903. goto out_end;
  904. if (ehdr.e_machine == EM_X86_64)
  905. dso_type = DSO__TYPE_X32BIT;
  906. else
  907. dso_type = DSO__TYPE_32BIT;
  908. out_end:
  909. elf_end(elf);
  910. out:
  911. return dso_type;
  912. }
  913. static int copy_bytes(int from, off_t from_offs, int to, off_t to_offs, u64 len)
  914. {
  915. ssize_t r;
  916. size_t n;
  917. int err = -1;
  918. char *buf = malloc(page_size);
  919. if (buf == NULL)
  920. return -1;
  921. if (lseek(to, to_offs, SEEK_SET) != to_offs)
  922. goto out;
  923. if (lseek(from, from_offs, SEEK_SET) != from_offs)
  924. goto out;
  925. while (len) {
  926. n = page_size;
  927. if (len < n)
  928. n = len;
  929. /* Use read because mmap won't work on proc files */
  930. r = read(from, buf, n);
  931. if (r < 0)
  932. goto out;
  933. if (!r)
  934. break;
  935. n = r;
  936. r = write(to, buf, n);
  937. if (r < 0)
  938. goto out;
  939. if ((size_t)r != n)
  940. goto out;
  941. len -= n;
  942. }
  943. err = 0;
  944. out:
  945. free(buf);
  946. return err;
  947. }
  948. struct kcore {
  949. int fd;
  950. int elfclass;
  951. Elf *elf;
  952. GElf_Ehdr ehdr;
  953. };
  954. static int kcore__open(struct kcore *kcore, const char *filename)
  955. {
  956. GElf_Ehdr *ehdr;
  957. kcore->fd = open(filename, O_RDONLY);
  958. if (kcore->fd == -1)
  959. return -1;
  960. kcore->elf = elf_begin(kcore->fd, ELF_C_READ, NULL);
  961. if (!kcore->elf)
  962. goto out_close;
  963. kcore->elfclass = gelf_getclass(kcore->elf);
  964. if (kcore->elfclass == ELFCLASSNONE)
  965. goto out_end;
  966. ehdr = gelf_getehdr(kcore->elf, &kcore->ehdr);
  967. if (!ehdr)
  968. goto out_end;
  969. return 0;
  970. out_end:
  971. elf_end(kcore->elf);
  972. out_close:
  973. close(kcore->fd);
  974. return -1;
  975. }
  976. static int kcore__init(struct kcore *kcore, char *filename, int elfclass,
  977. bool temp)
  978. {
  979. GElf_Ehdr *ehdr;
  980. kcore->elfclass = elfclass;
  981. if (temp)
  982. kcore->fd = mkstemp(filename);
  983. else
  984. kcore->fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0400);
  985. if (kcore->fd == -1)
  986. return -1;
  987. kcore->elf = elf_begin(kcore->fd, ELF_C_WRITE, NULL);
  988. if (!kcore->elf)
  989. goto out_close;
  990. if (!gelf_newehdr(kcore->elf, elfclass))
  991. goto out_end;
  992. ehdr = gelf_getehdr(kcore->elf, &kcore->ehdr);
  993. if (!ehdr)
  994. goto out_end;
  995. return 0;
  996. out_end:
  997. elf_end(kcore->elf);
  998. out_close:
  999. close(kcore->fd);
  1000. unlink(filename);
  1001. return -1;
  1002. }
  1003. static void kcore__close(struct kcore *kcore)
  1004. {
  1005. elf_end(kcore->elf);
  1006. close(kcore->fd);
  1007. }
  1008. static int kcore__copy_hdr(struct kcore *from, struct kcore *to, size_t count)
  1009. {
  1010. GElf_Ehdr *ehdr = &to->ehdr;
  1011. GElf_Ehdr *kehdr = &from->ehdr;
  1012. memcpy(ehdr->e_ident, kehdr->e_ident, EI_NIDENT);
  1013. ehdr->e_type = kehdr->e_type;
  1014. ehdr->e_machine = kehdr->e_machine;
  1015. ehdr->e_version = kehdr->e_version;
  1016. ehdr->e_entry = 0;
  1017. ehdr->e_shoff = 0;
  1018. ehdr->e_flags = kehdr->e_flags;
  1019. ehdr->e_phnum = count;
  1020. ehdr->e_shentsize = 0;
  1021. ehdr->e_shnum = 0;
  1022. ehdr->e_shstrndx = 0;
  1023. if (from->elfclass == ELFCLASS32) {
  1024. ehdr->e_phoff = sizeof(Elf32_Ehdr);
  1025. ehdr->e_ehsize = sizeof(Elf32_Ehdr);
  1026. ehdr->e_phentsize = sizeof(Elf32_Phdr);
  1027. } else {
  1028. ehdr->e_phoff = sizeof(Elf64_Ehdr);
  1029. ehdr->e_ehsize = sizeof(Elf64_Ehdr);
  1030. ehdr->e_phentsize = sizeof(Elf64_Phdr);
  1031. }
  1032. if (!gelf_update_ehdr(to->elf, ehdr))
  1033. return -1;
  1034. if (!gelf_newphdr(to->elf, count))
  1035. return -1;
  1036. return 0;
  1037. }
  1038. static int kcore__add_phdr(struct kcore *kcore, int idx, off_t offset,
  1039. u64 addr, u64 len)
  1040. {
  1041. GElf_Phdr gphdr;
  1042. GElf_Phdr *phdr;
  1043. phdr = gelf_getphdr(kcore->elf, idx, &gphdr);
  1044. if (!phdr)
  1045. return -1;
  1046. phdr->p_type = PT_LOAD;
  1047. phdr->p_flags = PF_R | PF_W | PF_X;
  1048. phdr->p_offset = offset;
  1049. phdr->p_vaddr = addr;
  1050. phdr->p_paddr = 0;
  1051. phdr->p_filesz = len;
  1052. phdr->p_memsz = len;
  1053. phdr->p_align = page_size;
  1054. if (!gelf_update_phdr(kcore->elf, idx, phdr))
  1055. return -1;
  1056. return 0;
  1057. }
  1058. static off_t kcore__write(struct kcore *kcore)
  1059. {
  1060. return elf_update(kcore->elf, ELF_C_WRITE);
  1061. }
  1062. struct phdr_data {
  1063. off_t offset;
  1064. u64 addr;
  1065. u64 len;
  1066. };
  1067. struct kcore_copy_info {
  1068. u64 stext;
  1069. u64 etext;
  1070. u64 first_symbol;
  1071. u64 last_symbol;
  1072. u64 first_module;
  1073. u64 last_module_symbol;
  1074. struct phdr_data kernel_map;
  1075. struct phdr_data modules_map;
  1076. };
  1077. static int kcore_copy__process_kallsyms(void *arg, const char *name, char type,
  1078. u64 start)
  1079. {
  1080. struct kcore_copy_info *kci = arg;
  1081. if (!symbol_type__is_a(type, MAP__FUNCTION))
  1082. return 0;
  1083. if (strchr(name, '[')) {
  1084. if (start > kci->last_module_symbol)
  1085. kci->last_module_symbol = start;
  1086. return 0;
  1087. }
  1088. if (!kci->first_symbol || start < kci->first_symbol)
  1089. kci->first_symbol = start;
  1090. if (!kci->last_symbol || start > kci->last_symbol)
  1091. kci->last_symbol = start;
  1092. if (!strcmp(name, "_stext")) {
  1093. kci->stext = start;
  1094. return 0;
  1095. }
  1096. if (!strcmp(name, "_etext")) {
  1097. kci->etext = start;
  1098. return 0;
  1099. }
  1100. return 0;
  1101. }
  1102. static int kcore_copy__parse_kallsyms(struct kcore_copy_info *kci,
  1103. const char *dir)
  1104. {
  1105. char kallsyms_filename[PATH_MAX];
  1106. scnprintf(kallsyms_filename, PATH_MAX, "%s/kallsyms", dir);
  1107. if (symbol__restricted_filename(kallsyms_filename, "/proc/kallsyms"))
  1108. return -1;
  1109. if (kallsyms__parse(kallsyms_filename, kci,
  1110. kcore_copy__process_kallsyms) < 0)
  1111. return -1;
  1112. return 0;
  1113. }
  1114. static int kcore_copy__process_modules(void *arg,
  1115. const char *name __maybe_unused,
  1116. u64 start)
  1117. {
  1118. struct kcore_copy_info *kci = arg;
  1119. if (!kci->first_module || start < kci->first_module)
  1120. kci->first_module = start;
  1121. return 0;
  1122. }
  1123. static int kcore_copy__parse_modules(struct kcore_copy_info *kci,
  1124. const char *dir)
  1125. {
  1126. char modules_filename[PATH_MAX];
  1127. scnprintf(modules_filename, PATH_MAX, "%s/modules", dir);
  1128. if (symbol__restricted_filename(modules_filename, "/proc/modules"))
  1129. return -1;
  1130. if (modules__parse(modules_filename, kci,
  1131. kcore_copy__process_modules) < 0)
  1132. return -1;
  1133. return 0;
  1134. }
  1135. static void kcore_copy__map(struct phdr_data *p, u64 start, u64 end, u64 pgoff,
  1136. u64 s, u64 e)
  1137. {
  1138. if (p->addr || s < start || s >= end)
  1139. return;
  1140. p->addr = s;
  1141. p->offset = (s - start) + pgoff;
  1142. p->len = e < end ? e - s : end - s;
  1143. }
  1144. static int kcore_copy__read_map(u64 start, u64 len, u64 pgoff, void *data)
  1145. {
  1146. struct kcore_copy_info *kci = data;
  1147. u64 end = start + len;
  1148. kcore_copy__map(&kci->kernel_map, start, end, pgoff, kci->stext,
  1149. kci->etext);
  1150. kcore_copy__map(&kci->modules_map, start, end, pgoff, kci->first_module,
  1151. kci->last_module_symbol);
  1152. return 0;
  1153. }
  1154. static int kcore_copy__read_maps(struct kcore_copy_info *kci, Elf *elf)
  1155. {
  1156. if (elf_read_maps(elf, true, kcore_copy__read_map, kci) < 0)
  1157. return -1;
  1158. return 0;
  1159. }
  1160. static int kcore_copy__calc_maps(struct kcore_copy_info *kci, const char *dir,
  1161. Elf *elf)
  1162. {
  1163. if (kcore_copy__parse_kallsyms(kci, dir))
  1164. return -1;
  1165. if (kcore_copy__parse_modules(kci, dir))
  1166. return -1;
  1167. if (kci->stext)
  1168. kci->stext = round_down(kci->stext, page_size);
  1169. else
  1170. kci->stext = round_down(kci->first_symbol, page_size);
  1171. if (kci->etext) {
  1172. kci->etext = round_up(kci->etext, page_size);
  1173. } else if (kci->last_symbol) {
  1174. kci->etext = round_up(kci->last_symbol, page_size);
  1175. kci->etext += page_size;
  1176. }
  1177. kci->first_module = round_down(kci->first_module, page_size);
  1178. if (kci->last_module_symbol) {
  1179. kci->last_module_symbol = round_up(kci->last_module_symbol,
  1180. page_size);
  1181. kci->last_module_symbol += page_size;
  1182. }
  1183. if (!kci->stext || !kci->etext)
  1184. return -1;
  1185. if (kci->first_module && !kci->last_module_symbol)
  1186. return -1;
  1187. return kcore_copy__read_maps(kci, elf);
  1188. }
  1189. static int kcore_copy__copy_file(const char *from_dir, const char *to_dir,
  1190. const char *name)
  1191. {
  1192. char from_filename[PATH_MAX];
  1193. char to_filename[PATH_MAX];
  1194. scnprintf(from_filename, PATH_MAX, "%s/%s", from_dir, name);
  1195. scnprintf(to_filename, PATH_MAX, "%s/%s", to_dir, name);
  1196. return copyfile_mode(from_filename, to_filename, 0400);
  1197. }
  1198. static int kcore_copy__unlink(const char *dir, const char *name)
  1199. {
  1200. char filename[PATH_MAX];
  1201. scnprintf(filename, PATH_MAX, "%s/%s", dir, name);
  1202. return unlink(filename);
  1203. }
  1204. static int kcore_copy__compare_fds(int from, int to)
  1205. {
  1206. char *buf_from;
  1207. char *buf_to;
  1208. ssize_t ret;
  1209. size_t len;
  1210. int err = -1;
  1211. buf_from = malloc(page_size);
  1212. buf_to = malloc(page_size);
  1213. if (!buf_from || !buf_to)
  1214. goto out;
  1215. while (1) {
  1216. /* Use read because mmap won't work on proc files */
  1217. ret = read(from, buf_from, page_size);
  1218. if (ret < 0)
  1219. goto out;
  1220. if (!ret)
  1221. break;
  1222. len = ret;
  1223. if (readn(to, buf_to, len) != (int)len)
  1224. goto out;
  1225. if (memcmp(buf_from, buf_to, len))
  1226. goto out;
  1227. }
  1228. err = 0;
  1229. out:
  1230. free(buf_to);
  1231. free(buf_from);
  1232. return err;
  1233. }
  1234. static int kcore_copy__compare_files(const char *from_filename,
  1235. const char *to_filename)
  1236. {
  1237. int from, to, err = -1;
  1238. from = open(from_filename, O_RDONLY);
  1239. if (from < 0)
  1240. return -1;
  1241. to = open(to_filename, O_RDONLY);
  1242. if (to < 0)
  1243. goto out_close_from;
  1244. err = kcore_copy__compare_fds(from, to);
  1245. close(to);
  1246. out_close_from:
  1247. close(from);
  1248. return err;
  1249. }
  1250. static int kcore_copy__compare_file(const char *from_dir, const char *to_dir,
  1251. const char *name)
  1252. {
  1253. char from_filename[PATH_MAX];
  1254. char to_filename[PATH_MAX];
  1255. scnprintf(from_filename, PATH_MAX, "%s/%s", from_dir, name);
  1256. scnprintf(to_filename, PATH_MAX, "%s/%s", to_dir, name);
  1257. return kcore_copy__compare_files(from_filename, to_filename);
  1258. }
  1259. /**
  1260. * kcore_copy - copy kallsyms, modules and kcore from one directory to another.
  1261. * @from_dir: from directory
  1262. * @to_dir: to directory
  1263. *
  1264. * This function copies kallsyms, modules and kcore files from one directory to
  1265. * another. kallsyms and modules are copied entirely. Only code segments are
  1266. * copied from kcore. It is assumed that two segments suffice: one for the
  1267. * kernel proper and one for all the modules. The code segments are determined
  1268. * from kallsyms and modules files. The kernel map starts at _stext or the
  1269. * lowest function symbol, and ends at _etext or the highest function symbol.
  1270. * The module map starts at the lowest module address and ends at the highest
  1271. * module symbol. Start addresses are rounded down to the nearest page. End
  1272. * addresses are rounded up to the nearest page. An extra page is added to the
  1273. * highest kernel symbol and highest module symbol to, hopefully, encompass that
  1274. * symbol too. Because it contains only code sections, the resulting kcore is
  1275. * unusual. One significant peculiarity is that the mapping (start -> pgoff)
  1276. * is not the same for the kernel map and the modules map. That happens because
  1277. * the data is copied adjacently whereas the original kcore has gaps. Finally,
  1278. * kallsyms and modules files are compared with their copies to check that
  1279. * modules have not been loaded or unloaded while the copies were taking place.
  1280. *
  1281. * Return: %0 on success, %-1 on failure.
  1282. */
  1283. int kcore_copy(const char *from_dir, const char *to_dir)
  1284. {
  1285. struct kcore kcore;
  1286. struct kcore extract;
  1287. size_t count = 2;
  1288. int idx = 0, err = -1;
  1289. off_t offset = page_size, sz, modules_offset = 0;
  1290. struct kcore_copy_info kci = { .stext = 0, };
  1291. char kcore_filename[PATH_MAX];
  1292. char extract_filename[PATH_MAX];
  1293. if (kcore_copy__copy_file(from_dir, to_dir, "kallsyms"))
  1294. return -1;
  1295. if (kcore_copy__copy_file(from_dir, to_dir, "modules"))
  1296. goto out_unlink_kallsyms;
  1297. scnprintf(kcore_filename, PATH_MAX, "%s/kcore", from_dir);
  1298. scnprintf(extract_filename, PATH_MAX, "%s/kcore", to_dir);
  1299. if (kcore__open(&kcore, kcore_filename))
  1300. goto out_unlink_modules;
  1301. if (kcore_copy__calc_maps(&kci, from_dir, kcore.elf))
  1302. goto out_kcore_close;
  1303. if (kcore__init(&extract, extract_filename, kcore.elfclass, false))
  1304. goto out_kcore_close;
  1305. if (!kci.modules_map.addr)
  1306. count -= 1;
  1307. if (kcore__copy_hdr(&kcore, &extract, count))
  1308. goto out_extract_close;
  1309. if (kcore__add_phdr(&extract, idx++, offset, kci.kernel_map.addr,
  1310. kci.kernel_map.len))
  1311. goto out_extract_close;
  1312. if (kci.modules_map.addr) {
  1313. modules_offset = offset + kci.kernel_map.len;
  1314. if (kcore__add_phdr(&extract, idx, modules_offset,
  1315. kci.modules_map.addr, kci.modules_map.len))
  1316. goto out_extract_close;
  1317. }
  1318. sz = kcore__write(&extract);
  1319. if (sz < 0 || sz > offset)
  1320. goto out_extract_close;
  1321. if (copy_bytes(kcore.fd, kci.kernel_map.offset, extract.fd, offset,
  1322. kci.kernel_map.len))
  1323. goto out_extract_close;
  1324. if (modules_offset && copy_bytes(kcore.fd, kci.modules_map.offset,
  1325. extract.fd, modules_offset,
  1326. kci.modules_map.len))
  1327. goto out_extract_close;
  1328. if (kcore_copy__compare_file(from_dir, to_dir, "modules"))
  1329. goto out_extract_close;
  1330. if (kcore_copy__compare_file(from_dir, to_dir, "kallsyms"))
  1331. goto out_extract_close;
  1332. err = 0;
  1333. out_extract_close:
  1334. kcore__close(&extract);
  1335. if (err)
  1336. unlink(extract_filename);
  1337. out_kcore_close:
  1338. kcore__close(&kcore);
  1339. out_unlink_modules:
  1340. if (err)
  1341. kcore_copy__unlink(to_dir, "modules");
  1342. out_unlink_kallsyms:
  1343. if (err)
  1344. kcore_copy__unlink(to_dir, "kallsyms");
  1345. return err;
  1346. }
  1347. int kcore_extract__create(struct kcore_extract *kce)
  1348. {
  1349. struct kcore kcore;
  1350. struct kcore extract;
  1351. size_t count = 1;
  1352. int idx = 0, err = -1;
  1353. off_t offset = page_size, sz;
  1354. if (kcore__open(&kcore, kce->kcore_filename))
  1355. return -1;
  1356. strcpy(kce->extract_filename, PERF_KCORE_EXTRACT);
  1357. if (kcore__init(&extract, kce->extract_filename, kcore.elfclass, true))
  1358. goto out_kcore_close;
  1359. if (kcore__copy_hdr(&kcore, &extract, count))
  1360. goto out_extract_close;
  1361. if (kcore__add_phdr(&extract, idx, offset, kce->addr, kce->len))
  1362. goto out_extract_close;
  1363. sz = kcore__write(&extract);
  1364. if (sz < 0 || sz > offset)
  1365. goto out_extract_close;
  1366. if (copy_bytes(kcore.fd, kce->offs, extract.fd, offset, kce->len))
  1367. goto out_extract_close;
  1368. err = 0;
  1369. out_extract_close:
  1370. kcore__close(&extract);
  1371. if (err)
  1372. unlink(kce->extract_filename);
  1373. out_kcore_close:
  1374. kcore__close(&kcore);
  1375. return err;
  1376. }
  1377. void kcore_extract__delete(struct kcore_extract *kce)
  1378. {
  1379. unlink(kce->extract_filename);
  1380. }
  1381. void symbol__elf_init(void)
  1382. {
  1383. elf_version(EV_CURRENT);
  1384. }