symbol-elf.c 38 KB

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