symbol-elf.c 36 KB

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