symbol-elf.c 36 KB

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