pei-x86_64.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  1. /* BFD back-end for Intel 386 PE IMAGE COFF files.
  2. Copyright (C) 2006-2022 Free Software Foundation, Inc.
  3. This file is part of BFD, the Binary File Descriptor library.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  15. MA 02110-1301, USA.
  16. Written by Kai Tietz, OneVision Software GmbH&CoKg. */
  17. #include "sysdep.h"
  18. #include "bfd.h"
  19. #define TARGET_SYM x86_64_pei_vec
  20. #define TARGET_NAME "pei-x86-64"
  21. #define COFF_IMAGE_WITH_PE
  22. #define COFF_WITH_PE
  23. #define COFF_WITH_pex64
  24. #define PCRELOFFSET true
  25. #if defined (USE_MINGW64_LEADING_UNDERSCORES)
  26. #define TARGET_UNDERSCORE '_'
  27. #else
  28. #define TARGET_UNDERSCORE 0
  29. #endif
  30. /* Long section names not allowed in executable images, only object files. */
  31. #define COFF_LONG_SECTION_NAMES 0
  32. #define COFF_SUPPORT_GNU_LINKONCE
  33. #define COFF_LONG_FILENAMES
  34. #define PDATA_ROW_SIZE (3 * 4)
  35. #define COFF_SECTION_ALIGNMENT_ENTRIES \
  36. { COFF_SECTION_NAME_EXACT_MATCH (".bss"), \
  37. COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 4 }, \
  38. { COFF_SECTION_NAME_PARTIAL_MATCH (".data"), \
  39. COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 4 }, \
  40. { COFF_SECTION_NAME_PARTIAL_MATCH (".rdata"), \
  41. COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 4 }, \
  42. { COFF_SECTION_NAME_PARTIAL_MATCH (".text"), \
  43. COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 4 }, \
  44. { COFF_SECTION_NAME_PARTIAL_MATCH (".idata"), \
  45. COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 }, \
  46. { COFF_SECTION_NAME_EXACT_MATCH (".pdata"), \
  47. COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 }, \
  48. { COFF_SECTION_NAME_PARTIAL_MATCH (".debug"), \
  49. COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 0 }, \
  50. { COFF_SECTION_NAME_PARTIAL_MATCH (".gnu.linkonce.wi."), \
  51. COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 0 }
  52. /* Note we have to make sure not to include headers twice.
  53. Not all headers are wrapped in #ifdef guards, so we define
  54. PEI_HEADERS to prevent double including in coff-x86_64.c */
  55. #define PEI_HEADERS
  56. #include "sysdep.h"
  57. #include "bfd.h"
  58. #include "libbfd.h"
  59. #include "coff/x86_64.h"
  60. #include "coff/internal.h"
  61. #include "coff/pe.h"
  62. #include "libcoff.h"
  63. #include "libpei.h"
  64. #include "libiberty.h"
  65. #undef AOUTSZ
  66. #define AOUTSZ PEPAOUTSZ
  67. #define PEAOUTHDR PEPAOUTHDR
  68. /* Name of registers according to SEH conventions. */
  69. static const char * const pex_regs[16] = {
  70. "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
  71. "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
  72. };
  73. /* Swap in a runtime function. */
  74. static void
  75. pex64_get_runtime_function (bfd *abfd, struct pex64_runtime_function *rf,
  76. const void *data)
  77. {
  78. const struct external_pex64_runtime_function *ex_rf =
  79. (const struct external_pex64_runtime_function *) data;
  80. rf->rva_BeginAddress = bfd_get_32 (abfd, ex_rf->rva_BeginAddress);
  81. rf->rva_EndAddress = bfd_get_32 (abfd, ex_rf->rva_EndAddress);
  82. rf->rva_UnwindData = bfd_get_32 (abfd, ex_rf->rva_UnwindData);
  83. }
  84. /* Swap in unwind info header. */
  85. static bool
  86. pex64_get_unwind_info (bfd *abfd, struct pex64_unwind_info *ui,
  87. void *data, void *data_end)
  88. {
  89. struct external_pex64_unwind_info *ex_ui =
  90. (struct external_pex64_unwind_info *) data;
  91. bfd_byte *ex_dta = (bfd_byte *) data;
  92. bfd_byte *ex_dta_end = (bfd_byte *) data_end;
  93. memset (ui, 0, sizeof (struct pex64_unwind_info));
  94. if (ex_dta >= ex_dta_end || ex_dta + 4 >= ex_dta_end)
  95. return false;
  96. ui->Version = PEX64_UWI_VERSION (ex_ui->Version_Flags);
  97. ui->Flags = PEX64_UWI_FLAGS (ex_ui->Version_Flags);
  98. ui->SizeOfPrologue = (bfd_vma) ex_ui->SizeOfPrologue;
  99. ui->CountOfCodes = (bfd_vma) ex_ui->CountOfCodes;
  100. ui->FrameRegister = PEX64_UWI_FRAMEREG (ex_ui->FrameRegisterOffset);
  101. ui->FrameOffset = PEX64_UWI_FRAMEOFF (ex_ui->FrameRegisterOffset);
  102. ui->sizeofUnwindCodes = PEX64_UWI_SIZEOF_UWCODE_ARRAY (ui->CountOfCodes);
  103. ui->SizeOfBlock = ui->sizeofUnwindCodes + 4;
  104. ui->rawUnwindCodes = ex_dta + 4;
  105. ui->rawUnwindCodesEnd = ex_dta_end;
  106. ex_dta += ui->SizeOfBlock;
  107. if (ex_dta >= ex_dta_end)
  108. return false;
  109. switch (ui->Flags)
  110. {
  111. case UNW_FLAG_CHAININFO:
  112. if (ex_dta + 12 >= ex_dta_end)
  113. return false;
  114. ui->rva_BeginAddress = bfd_get_32 (abfd, ex_dta + 0);
  115. ui->rva_EndAddress = bfd_get_32 (abfd, ex_dta + 4);
  116. ui->rva_UnwindData = bfd_get_32 (abfd, ex_dta + 8);
  117. ui->SizeOfBlock += 12;
  118. return true;
  119. case UNW_FLAG_EHANDLER:
  120. case UNW_FLAG_UHANDLER:
  121. case UNW_FLAG_FHANDLER:
  122. if (ex_dta + 4 >= ex_dta_end)
  123. return false;
  124. ui->rva_ExceptionHandler = bfd_get_32 (abfd, ex_dta);
  125. ui->SizeOfBlock += 4;
  126. return true;
  127. default:
  128. return true;
  129. }
  130. }
  131. /* Display unwind codes. */
  132. static void
  133. pex64_xdata_print_uwd_codes (FILE *file, bfd *abfd,
  134. struct pex64_unwind_info *ui,
  135. struct pex64_runtime_function *rf)
  136. {
  137. unsigned int i;
  138. unsigned int tmp; /* At least 32 bits. */
  139. int save_allowed;
  140. if (ui->CountOfCodes == 0 || ui->rawUnwindCodes == NULL)
  141. return;
  142. /* According to UNWIND_CODE documentation:
  143. If an FP reg is used, the any unwind code taking an offset must only be
  144. used after the FP reg is established in the prolog.
  145. But there are counter examples of that in system dlls... */
  146. save_allowed = true;
  147. i = 0;
  148. if (ui->rawUnwindCodes + 1 >= ui->rawUnwindCodesEnd)
  149. {
  150. fprintf (file, _("warning: corrupt unwind data\n"));
  151. return;
  152. }
  153. if (ui->Version == 2
  154. && PEX64_UNWCODE_CODE (ui->rawUnwindCodes[1]) == UWOP_EPILOG)
  155. {
  156. /* Display epilog opcode (whose docoding is not fully documented).
  157. Looks to be designed to speed-up unwinding, as there is no need
  158. to decode instruction flow if outside an epilog. */
  159. unsigned int func_size = rf->rva_EndAddress - rf->rva_BeginAddress;
  160. if (ui->rawUnwindCodes + 1 + (ui->CountOfCodes * 2) >= ui->rawUnwindCodesEnd)
  161. {
  162. fprintf (file, _("warning: corrupt unwind data\n"));
  163. return;
  164. }
  165. fprintf (file, "\tv2 epilog (length: %02x) at pc+:",
  166. ui->rawUnwindCodes[0]);
  167. if (PEX64_UNWCODE_INFO (ui->rawUnwindCodes[1]))
  168. fprintf (file, " 0x%x", func_size - ui->rawUnwindCodes[0]);
  169. i++;
  170. for (; i < ui->CountOfCodes; i++)
  171. {
  172. const bfd_byte *dta = ui->rawUnwindCodes + 2 * i;
  173. unsigned int off;
  174. if (PEX64_UNWCODE_CODE (dta[1]) != UWOP_EPILOG)
  175. break;
  176. off = dta[0] | (PEX64_UNWCODE_INFO (dta[1]) << 8);
  177. if (off == 0)
  178. fprintf (file, " [pad]");
  179. else
  180. fprintf (file, " 0x%x", func_size - off);
  181. }
  182. fputc ('\n', file);
  183. }
  184. if (ui->rawUnwindCodes + 2 + (ui->CountOfCodes * 2) >= ui->rawUnwindCodesEnd)
  185. {
  186. fprintf (file, _("warning: corrupt unwind data\n"));
  187. return;
  188. }
  189. for (; i < ui->CountOfCodes; i++)
  190. {
  191. const bfd_byte *dta = ui->rawUnwindCodes + 2 * i;
  192. unsigned int info = PEX64_UNWCODE_INFO (dta[1]);
  193. int unexpected = false;
  194. fprintf (file, "\t pc+0x%02x: ", (unsigned int) dta[0]);
  195. switch (PEX64_UNWCODE_CODE (dta[1]))
  196. {
  197. case UWOP_PUSH_NONVOL:
  198. fprintf (file, "push %s", pex_regs[info]);
  199. break;
  200. case UWOP_ALLOC_LARGE:
  201. if (info == 0)
  202. {
  203. if (dta + 4 > ui->rawUnwindCodesEnd)
  204. {
  205. fprintf (file, _("warning: corrupt unwind data\n"));
  206. return;
  207. }
  208. tmp = bfd_get_16 (abfd, dta + 2) * 8;
  209. i++;
  210. }
  211. else
  212. {
  213. if (dta + 6 > ui->rawUnwindCodesEnd)
  214. {
  215. fprintf (file, _("warning: corrupt unwind data\n"));
  216. return;
  217. }
  218. tmp = bfd_get_32 (abfd, dta + 2);
  219. i += 2;
  220. }
  221. fprintf (file, "alloc large area: rsp = rsp - 0x%x", tmp);
  222. break;
  223. case UWOP_ALLOC_SMALL:
  224. fprintf (file, "alloc small area: rsp = rsp - 0x%x", (info + 1) * 8);
  225. break;
  226. case UWOP_SET_FPREG:
  227. /* According to the documentation, info field is unused. */
  228. fprintf (file, "FPReg: %s = rsp + 0x%x (info = 0x%x)",
  229. pex_regs[ui->FrameRegister],
  230. (unsigned int) ui->FrameOffset * 16, info);
  231. unexpected = ui->FrameRegister == 0;
  232. save_allowed = false;
  233. break;
  234. case UWOP_SAVE_NONVOL:
  235. if (dta + 4 > ui->rawUnwindCodesEnd)
  236. {
  237. fprintf (file, _("warning: corrupt unwind data\n"));
  238. return;
  239. }
  240. tmp = bfd_get_16 (abfd, dta + 2) * 8;
  241. i++;
  242. fprintf (file, "save %s at rsp + 0x%x", pex_regs[info], tmp);
  243. unexpected = !save_allowed;
  244. break;
  245. case UWOP_SAVE_NONVOL_FAR:
  246. if (dta + 6 > ui->rawUnwindCodesEnd)
  247. {
  248. fprintf (file, _("warning: corrupt unwind data\n"));
  249. return;
  250. }
  251. tmp = bfd_get_32 (abfd, dta + 2);
  252. i += 2;
  253. fprintf (file, "save %s at rsp + 0x%x", pex_regs[info], tmp);
  254. unexpected = !save_allowed;
  255. break;
  256. case UWOP_SAVE_XMM:
  257. if (ui->Version == 1)
  258. {
  259. if (dta + 4 > ui->rawUnwindCodesEnd)
  260. {
  261. fprintf (file, _("warning: corrupt unwind data\n"));
  262. return;
  263. }
  264. tmp = bfd_get_16 (abfd, dta + 2) * 8;
  265. i++;
  266. fprintf (file, "save mm%u at rsp + 0x%x", info, tmp);
  267. unexpected = !save_allowed;
  268. }
  269. else if (ui->Version == 2)
  270. {
  271. fprintf (file, "epilog %02x %01x", dta[0], info);
  272. unexpected = true;
  273. }
  274. break;
  275. case UWOP_SAVE_XMM_FAR:
  276. if (dta + 6 > ui->rawUnwindCodesEnd)
  277. {
  278. fprintf (file, _("warning: corrupt unwind data\n"));
  279. return;
  280. }
  281. tmp = bfd_get_32 (abfd, dta + 2) * 8;
  282. i += 2;
  283. fprintf (file, "save mm%u at rsp + 0x%x", info, tmp);
  284. unexpected = !save_allowed;
  285. break;
  286. case UWOP_SAVE_XMM128:
  287. if (dta + 4 > ui->rawUnwindCodesEnd)
  288. {
  289. fprintf (file, _("warning: corrupt unwind data\n"));
  290. return;
  291. }
  292. tmp = bfd_get_16 (abfd, dta + 2) * 16;
  293. i++;
  294. fprintf (file, "save xmm%u at rsp + 0x%x", info, tmp);
  295. unexpected = !save_allowed;
  296. break;
  297. case UWOP_SAVE_XMM128_FAR:
  298. if (dta + 6 > ui->rawUnwindCodesEnd)
  299. {
  300. fprintf (file, _("warning: corrupt unwind data\n"));
  301. return;
  302. }
  303. tmp = bfd_get_32 (abfd, dta + 2) * 16;
  304. i += 2;
  305. fprintf (file, "save xmm%u at rsp + 0x%x", info, tmp);
  306. unexpected = !save_allowed;
  307. break;
  308. case UWOP_PUSH_MACHFRAME:
  309. fprintf (file, "interrupt entry (SS, old RSP, EFLAGS, CS, RIP");
  310. if (info == 0)
  311. fprintf (file, ")");
  312. else if (info == 1)
  313. fprintf (file, ",ErrorCode)");
  314. else
  315. fprintf (file, ", unknown(%u))", info);
  316. break;
  317. default:
  318. /* PR 17512: file: 2245-7442-0.004. */
  319. fprintf (file, _("Unknown: %x"), PEX64_UNWCODE_CODE (dta[1]));
  320. break;
  321. }
  322. if (unexpected)
  323. fprintf (file, " [Unexpected!]");
  324. fputc ('\n', file);
  325. }
  326. }
  327. /* Check wether section SEC_NAME contains the xdata at address ADDR. */
  328. static asection *
  329. pex64_get_section_by_rva (bfd *abfd, bfd_vma addr, const char *sec_name)
  330. {
  331. asection *section = bfd_get_section_by_name (abfd, sec_name);
  332. bfd_vma vsize;
  333. bfd_size_type datasize = 0;
  334. if (section == NULL
  335. || coff_section_data (abfd, section) == NULL
  336. || pei_section_data (abfd, section) == NULL)
  337. return NULL;
  338. vsize = section->vma - pe_data (abfd)->pe_opthdr.ImageBase;
  339. datasize = section->size;
  340. if (!datasize || vsize > addr || (vsize + datasize) < addr)
  341. return NULL;
  342. return section;
  343. }
  344. /* Dump xdata at for function RF to FILE. The argument XDATA_SECTION
  345. designate the bfd section containing the xdata, XDATA is its content,
  346. and ENDX the size if known (or NULL). */
  347. static void
  348. pex64_dump_xdata (FILE *file, bfd *abfd,
  349. asection *xdata_section, bfd_byte *xdata, bfd_vma *endx,
  350. struct pex64_runtime_function *rf)
  351. {
  352. bfd_vma vaddr;
  353. bfd_vma end_addr;
  354. bfd_vma addr = rf->rva_UnwindData;
  355. bfd_size_type sec_size = xdata_section->rawsize > 0 ? xdata_section->rawsize : xdata_section->size;
  356. struct pex64_unwind_info ui;
  357. vaddr = xdata_section->vma - pe_data (abfd)->pe_opthdr.ImageBase;
  358. addr -= vaddr;
  359. /* PR 17512: file: 2245-7442-0.004. */
  360. if (addr >= sec_size)
  361. {
  362. fprintf (file, _("warning: xdata section corrupt\n"));
  363. return;
  364. }
  365. if (endx)
  366. {
  367. end_addr = endx[0] - vaddr;
  368. /* PR 17512: file: 2245-7442-0.004. */
  369. if (end_addr > sec_size)
  370. {
  371. fprintf (file, _("warning: xdata section corrupt\n"));
  372. end_addr = sec_size;
  373. }
  374. }
  375. else
  376. end_addr = sec_size;
  377. if (! pex64_get_unwind_info (abfd, &ui, xdata + addr, xdata + end_addr))
  378. {
  379. fprintf (file, _("warning: xdata section corrupt\n"));
  380. return;
  381. }
  382. if (ui.Version != 1 && ui.Version != 2)
  383. {
  384. unsigned int i;
  385. fprintf (file, "\tVersion %u (unknown).\n",
  386. (unsigned int) ui.Version);
  387. for (i = 0; addr < end_addr; addr += 1, i++)
  388. {
  389. if ((i & 15) == 0)
  390. fprintf (file, "\t %03x:", i);
  391. fprintf (file, " %02x", xdata[addr]);
  392. if ((i & 15) == 15)
  393. fprintf (file, "\n");
  394. }
  395. if ((i & 15) != 0)
  396. fprintf (file, "\n");
  397. return;
  398. }
  399. fprintf (file, "\tVersion: %d, Flags: ", ui.Version);
  400. switch (ui.Flags)
  401. {
  402. case UNW_FLAG_NHANDLER:
  403. fprintf (file, "none");
  404. break;
  405. case UNW_FLAG_EHANDLER:
  406. fprintf (file, "UNW_FLAG_EHANDLER");
  407. break;
  408. case UNW_FLAG_UHANDLER:
  409. fprintf (file, "UNW_FLAG_UHANDLER");
  410. break;
  411. case UNW_FLAG_FHANDLER:
  412. fprintf
  413. (file, "UNW_FLAG_EHANDLER | UNW_FLAG_UHANDLER");
  414. break;
  415. case UNW_FLAG_CHAININFO:
  416. fprintf (file, "UNW_FLAG_CHAININFO");
  417. break;
  418. default:
  419. fprintf (file, "unknown flags value 0x%x", (unsigned int) ui.Flags);
  420. break;
  421. }
  422. fputc ('\n', file);
  423. fprintf (file, "\tNbr codes: %u, ", (unsigned int) ui.CountOfCodes);
  424. fprintf (file, "Prologue size: 0x%02x, Frame offset: 0x%x, ",
  425. (unsigned int) ui.SizeOfPrologue, (unsigned int) ui.FrameOffset);
  426. fprintf (file, "Frame reg: %s\n",
  427. ui.FrameRegister == 0 ? "none"
  428. : pex_regs[(unsigned int) ui.FrameRegister]);
  429. /* PR 17512: file: 2245-7442-0.004. */
  430. if (ui.CountOfCodes * 2 + ui.rawUnwindCodes > xdata + xdata_section->size)
  431. fprintf (file, _("Too many unwind codes (%ld)\n"), (long) ui.CountOfCodes);
  432. else
  433. pex64_xdata_print_uwd_codes (file, abfd, &ui, rf);
  434. switch (ui.Flags)
  435. {
  436. case UNW_FLAG_EHANDLER:
  437. case UNW_FLAG_UHANDLER:
  438. case UNW_FLAG_FHANDLER:
  439. fprintf (file, "\tHandler: ");
  440. fprintf_vma (file, (ui.rva_ExceptionHandler
  441. + pe_data (abfd)->pe_opthdr.ImageBase));
  442. fprintf (file, ".\n");
  443. break;
  444. case UNW_FLAG_CHAININFO:
  445. fprintf (file, "\tChain: start: ");
  446. fprintf_vma (file, ui.rva_BeginAddress);
  447. fprintf (file, ", end: ");
  448. fprintf_vma (file, ui.rva_EndAddress);
  449. fprintf (file, "\n\t unwind data: ");
  450. fprintf_vma (file, ui.rva_UnwindData);
  451. fprintf (file, ".\n");
  452. break;
  453. }
  454. /* Now we need end of this xdata block. */
  455. addr += ui.SizeOfBlock;
  456. if (addr < end_addr)
  457. {
  458. unsigned int i;
  459. fprintf (file,"\tUser data:\n");
  460. for (i = 0; addr < end_addr; addr += 1, i++)
  461. {
  462. if ((i & 15) == 0)
  463. fprintf (file, "\t %03x:", i);
  464. fprintf (file, " %02x", xdata[addr]);
  465. if ((i & 15) == 15)
  466. fprintf (file, "\n");
  467. }
  468. if ((i & 15) != 0)
  469. fprintf (file, "\n");
  470. }
  471. }
  472. /* Helper function to sort xdata. The entries of xdata are sorted to know
  473. the size of each entry. */
  474. static int
  475. sort_xdata_arr (const void *l, const void *r)
  476. {
  477. const bfd_vma *lp = (const bfd_vma *) l;
  478. const bfd_vma *rp = (const bfd_vma *) r;
  479. if (*lp == *rp)
  480. return 0;
  481. return (*lp < *rp ? -1 : 1);
  482. }
  483. /* Display unwind tables for x86-64. */
  484. static bool
  485. pex64_bfd_print_pdata_section (bfd *abfd, void *vfile, asection *pdata_section)
  486. {
  487. FILE *file = (FILE *) vfile;
  488. bfd_byte *pdata = NULL;
  489. bfd_byte *xdata = NULL;
  490. asection *xdata_section = NULL;
  491. bfd_vma xdata_base;
  492. bfd_size_type i;
  493. bfd_size_type datasize;
  494. bfd_size_type stop;
  495. bfd_vma prev_beginaddress = (bfd_vma) -1;
  496. bfd_vma prev_unwinddata_rva = (bfd_vma) -1;
  497. bfd_vma imagebase;
  498. int onaline = PDATA_ROW_SIZE;
  499. int seen_error = 0;
  500. bfd_vma *xdata_arr = NULL;
  501. int xdata_arr_cnt;
  502. bool virt_size_is_zero = false;
  503. /* Sanity checks. */
  504. if (pdata_section == NULL
  505. || coff_section_data (abfd, pdata_section) == NULL
  506. || pei_section_data (abfd, pdata_section) == NULL)
  507. return true;
  508. stop = pei_section_data (abfd, pdata_section)->virt_size;
  509. if ((stop % onaline) != 0)
  510. fprintf (file,
  511. /* xgettext:c-format */
  512. _("Warning: %s section size (%ld) is not a multiple of %d\n"),
  513. pdata_section->name, (long) stop, onaline);
  514. datasize = pdata_section->size;
  515. if (datasize == 0)
  516. {
  517. if (stop)
  518. fprintf (file, _("Warning: %s section size is zero\n"),
  519. pdata_section->name);
  520. return true;
  521. }
  522. /* virt_size might be zero for objects. */
  523. if (stop == 0 && strcmp (abfd->xvec->name, "pe-x86-64") == 0)
  524. {
  525. stop = datasize;
  526. virt_size_is_zero = true;
  527. }
  528. else if (datasize < stop)
  529. {
  530. fprintf (file,
  531. /* xgettext:c-format */
  532. _("Warning: %s section size (%ld) is smaller than virtual size (%ld)\n"),
  533. pdata_section->name, (unsigned long) datasize,
  534. (unsigned long) stop);
  535. /* Be sure not to read past datasize. */
  536. stop = datasize;
  537. }
  538. /* Display functions table. */
  539. fprintf (file,
  540. _("\nThe Function Table (interpreted %s section contents)\n"),
  541. pdata_section->name);
  542. fprintf (file, _("vma:\t\t\tBeginAddress\t EndAddress\t UnwindData\n"));
  543. if (!bfd_malloc_and_get_section (abfd, pdata_section, &pdata))
  544. goto done;
  545. /* Table of xdata entries. */
  546. xdata_arr = (bfd_vma *) xmalloc (sizeof (bfd_vma) * ((stop / onaline) + 1));
  547. xdata_arr_cnt = 0;
  548. if (strcmp (abfd->xvec->name, "pei-x86-64") == 0)
  549. imagebase = pe_data (abfd)->pe_opthdr.ImageBase;
  550. else
  551. imagebase = 0;
  552. for (i = 0; i < stop; i += onaline)
  553. {
  554. struct pex64_runtime_function rf;
  555. if (i + PDATA_ROW_SIZE > stop)
  556. break;
  557. pex64_get_runtime_function (abfd, &rf, &pdata[i]);
  558. if (rf.rva_BeginAddress == 0 && rf.rva_EndAddress == 0
  559. && rf.rva_UnwindData == 0)
  560. /* We are probably into the padding of the section now. */
  561. break;
  562. fputc (' ', file);
  563. fprintf_vma (file, i + pdata_section->vma);
  564. fprintf (file, ":\t");
  565. fprintf_vma (file, imagebase + rf.rva_BeginAddress);
  566. fprintf (file, " ");
  567. fprintf_vma (file, imagebase + rf.rva_EndAddress);
  568. fprintf (file, " ");
  569. fprintf_vma (file, imagebase + rf.rva_UnwindData);
  570. fprintf (file, "\n");
  571. if (i != 0 && rf.rva_BeginAddress <= prev_beginaddress)
  572. {
  573. seen_error = 1;
  574. fprintf (file, " has %s begin address as predecessor\n",
  575. (rf.rva_BeginAddress < prev_beginaddress ? "smaller" : "same"));
  576. }
  577. prev_beginaddress = rf.rva_BeginAddress;
  578. /* Now we check for negative addresses. */
  579. if ((prev_beginaddress & 0x80000000) != 0)
  580. {
  581. seen_error = 1;
  582. fprintf (file, " has negative begin address\n");
  583. }
  584. if ((rf.rva_EndAddress & 0x80000000) != 0)
  585. {
  586. seen_error = 1;
  587. fprintf (file, " has negative end address\n");
  588. }
  589. if ((rf.rva_UnwindData & 0x80000000) != 0)
  590. {
  591. seen_error = 1;
  592. fprintf (file, " has negative unwind address\n");
  593. }
  594. else if ((rf.rva_UnwindData && !PEX64_IS_RUNTIME_FUNCTION_CHAINED (&rf))
  595. || virt_size_is_zero)
  596. xdata_arr[xdata_arr_cnt++] = rf.rva_UnwindData;
  597. }
  598. if (seen_error)
  599. goto done;
  600. /* Add end of list marker. */
  601. xdata_arr[xdata_arr_cnt++] = ~((bfd_vma) 0);
  602. /* Sort start RVAs of xdata. */
  603. if (xdata_arr_cnt > 1)
  604. qsort (xdata_arr, (size_t) xdata_arr_cnt, sizeof (bfd_vma),
  605. sort_xdata_arr);
  606. /* Find the section containing the unwind data (.xdata). */
  607. xdata_base = xdata_arr[0];
  608. /* For sections with long names, first look for the same
  609. section name, replacing .pdata by .xdata prefix. */
  610. if (strcmp (pdata_section->name, ".pdata") != 0)
  611. {
  612. size_t len = strlen (pdata_section->name);
  613. char *xdata_name = xmalloc (len + 1);
  614. xdata_name = memcpy (xdata_name, pdata_section->name, len + 1);
  615. /* Transform .pdata prefix into .xdata prefix. */
  616. if (len > 1)
  617. xdata_name [1] = 'x';
  618. xdata_section = pex64_get_section_by_rva (abfd, xdata_base,
  619. xdata_name);
  620. free (xdata_name);
  621. }
  622. /* Second, try the .xdata section itself. */
  623. if (!xdata_section)
  624. xdata_section = pex64_get_section_by_rva (abfd, xdata_base, ".xdata");
  625. /* Otherwise, if xdata_base is non zero, search also inside
  626. other standard sections. */
  627. if (!xdata_section && xdata_base)
  628. xdata_section = pex64_get_section_by_rva (abfd, xdata_base, ".rdata");
  629. if (!xdata_section && xdata_base)
  630. xdata_section = pex64_get_section_by_rva (abfd, xdata_base, ".data");
  631. if (!xdata_section && xdata_base)
  632. xdata_section = pex64_get_section_by_rva (abfd, xdata_base, ".pdata");
  633. if (!xdata_section && xdata_base)
  634. xdata_section = pex64_get_section_by_rva (abfd, xdata_base, ".text");
  635. /* Transfer xdata section into xdata array. */
  636. if (!xdata_section
  637. || !bfd_malloc_and_get_section (abfd, xdata_section, &xdata))
  638. goto done;
  639. /* Avoid "also used "... ouput for single unwind info
  640. in object file. */
  641. prev_unwinddata_rva = (bfd_vma) -1;
  642. /* Do dump of pdata related xdata. */
  643. for (i = 0; i < stop; i += onaline)
  644. {
  645. struct pex64_runtime_function rf;
  646. if (i + PDATA_ROW_SIZE > stop)
  647. break;
  648. pex64_get_runtime_function (abfd, &rf, &pdata[i]);
  649. if (rf.rva_BeginAddress == 0 && rf.rva_EndAddress == 0
  650. && rf.rva_UnwindData == 0)
  651. /* We are probably into the padding of the section now. */
  652. break;
  653. if (i == 0)
  654. fprintf (file, _("\nDump of %s\n"), xdata_section->name);
  655. fputc (' ', file);
  656. fprintf_vma (file, rf.rva_UnwindData + imagebase);
  657. if (prev_unwinddata_rva == rf.rva_UnwindData)
  658. {
  659. /* Do not dump again the xdata for the same entry. */
  660. fprintf (file, " also used for function at ");
  661. fprintf_vma (file, rf.rva_BeginAddress + imagebase);
  662. fputc ('\n', file);
  663. continue;
  664. }
  665. else
  666. prev_unwinddata_rva = rf.rva_UnwindData;
  667. fprintf (file, " (rva: %08x): ",
  668. (unsigned int) rf.rva_UnwindData);
  669. fprintf_vma (file, rf.rva_BeginAddress + imagebase);
  670. fprintf (file, " - ");
  671. fprintf_vma (file, rf.rva_EndAddress + imagebase);
  672. fputc ('\n', file);
  673. if (rf.rva_UnwindData != 0 || virt_size_is_zero)
  674. {
  675. if (PEX64_IS_RUNTIME_FUNCTION_CHAINED (&rf))
  676. {
  677. bfd_vma altent = PEX64_GET_UNWINDDATA_UNIFIED_RVA (&rf);
  678. bfd_vma pdata_vma = bfd_section_vma (pdata_section);
  679. struct pex64_runtime_function arf;
  680. fprintf (file, "\t shares information with ");
  681. altent += imagebase;
  682. if (altent >= pdata_vma
  683. && altent - pdata_vma + PDATA_ROW_SIZE <= stop)
  684. {
  685. pex64_get_runtime_function
  686. (abfd, &arf, &pdata[altent - pdata_vma]);
  687. fprintf (file, "pdata element at 0x");
  688. fprintf_vma (file, arf.rva_UnwindData);
  689. }
  690. else
  691. fprintf (file, "unknown pdata element");
  692. fprintf (file, ".\n");
  693. }
  694. else
  695. {
  696. bfd_vma *p;
  697. /* Search for the current entry in the sorted array. */
  698. p = (bfd_vma *)
  699. bsearch (&rf.rva_UnwindData, xdata_arr,
  700. (size_t) xdata_arr_cnt, sizeof (bfd_vma),
  701. sort_xdata_arr);
  702. /* Advance to the next pointer into the xdata section. We may
  703. have shared xdata entries, which will result in a string of
  704. identical pointers in the array; advance past all of them. */
  705. while (p[0] <= rf.rva_UnwindData)
  706. ++p;
  707. if (p[0] == ~((bfd_vma) 0))
  708. p = NULL;
  709. pex64_dump_xdata (file, abfd, xdata_section, xdata, p, &rf);
  710. }
  711. }
  712. }
  713. done:
  714. free (pdata);
  715. free (xdata_arr);
  716. free (xdata);
  717. return true;
  718. }
  719. struct pex64_paps
  720. {
  721. void *obj;
  722. /* Number of found pdata sections. */
  723. unsigned int pdata_count;
  724. };
  725. /* Functionn prototype. */
  726. bool pex64_bfd_print_pdata (bfd *, void *);
  727. /* Helper function for bfd_map_over_section. */
  728. static void
  729. pex64_print_all_pdata_sections (bfd *abfd, asection *pdata, void *arg)
  730. {
  731. struct pex64_paps *paps = arg;
  732. if (startswith (pdata->name, ".pdata"))
  733. {
  734. if (pex64_bfd_print_pdata_section (abfd, paps->obj, pdata))
  735. paps->pdata_count++;
  736. }
  737. }
  738. bool
  739. pex64_bfd_print_pdata (bfd *abfd, void *vfile)
  740. {
  741. asection *pdata_section = bfd_get_section_by_name (abfd, ".pdata");
  742. struct pex64_paps paps;
  743. if (pdata_section)
  744. return pex64_bfd_print_pdata_section (abfd, vfile, pdata_section);
  745. paps.obj = vfile;
  746. paps.pdata_count = 0;
  747. bfd_map_over_sections (abfd, pex64_print_all_pdata_sections, &paps);
  748. return paps.pdata_count != 0;
  749. }
  750. #define bfd_pe_print_pdata pex64_bfd_print_pdata
  751. #define bfd_coff_std_swap_table bfd_coff_pei_swap_table
  752. #include "coff-x86_64.c"