symbol-elf.c 36 KB

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