peWriter.ml 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. (*
  2. * This file is part of ilLib
  3. * Copyright (c)2004-2013 Haxe Foundation
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  18. *)
  19. open PeData;;
  20. open IO;;
  21. open ExtString;;
  22. open ExtList;;
  23. exception Error_message of string
  24. let error msg = raise (Error_message msg)
  25. type 'a writer_ctx = {
  26. out : 'a IO.output;
  27. }
  28. let int_of_machine_type t = match t with
  29. | TUnknown -> 0x0 (* 0 - unmanaged PE files only *)
  30. | Ti386 -> 0x014c (* 0x014c - i386 *)
  31. | TR3000 -> 0x0162 (* 0x0162 - R3000 MIPS Little Endian *)
  32. | TR4000 -> 0x0166 (* 0x0166 - R4000 MIPS Little Endian *)
  33. | TR10000 -> 0x0168 (* 0x0168 - R10000 MIPS Little Endian *)
  34. | TWCeMipsV2 -> 0x0169 (* 0x0169 - MIPS Litlte Endian running MS Windows CE 2 *)
  35. | TAlpha -> 0x0184 (* 0x0184 - Alpha AXP *)
  36. | TSh3 -> 0x01a2 (* 0x01a2 - SH3 Little Endian *)
  37. | TSh3Dsp -> 0x01a3 (* 0x01a3 SH3DSP Little Endian *)
  38. | TSh3e -> 0x01a4 (* 0x01a4 SH3E Little Endian *)
  39. | TSh4 -> 0x01a6 (* 0x01a6 SH4 Little Endian *)
  40. | TSh5 -> 0x01a8
  41. | TArm -> 0x01c0 (* 0x1c0 ARM Little Endian *)
  42. | TArmN -> 0x01c4 (* 0x1c0 ARM Little Endian *)
  43. | TArm64 -> 0xaa64 (* 0x1c0 ARM Little Endian *)
  44. | TEbc -> 0xebc
  45. | TThumb -> 0x01c2 (* 0x1c2 ARM processor with Thumb decompressor *)
  46. | TAm33 -> 0x01d3 (* 0x1d3 AM33 processor *)
  47. | TPowerPC -> 0x01f0 (* 0x01f0 IBM PowerPC Little Endian *)
  48. | TPowerPCFP -> 0x01f1 (* 0x01f1 IBM PowerPC with FPU *)
  49. | TItanium64 -> 0x0200 (* 0x0200 Intel IA64 (Itanium( *)
  50. | TMips16 -> 0x0266 (* 0x0266 MIPS *)
  51. | TAlpha64 -> 0x0284 (* 0x0284 Alpha AXP64 *)
  52. | TMipsFpu -> 0x0366 (* 0x0366 MIPS with FPU *)
  53. | TMipsFpu16 -> 0x0466 (* 0x0466 MIPS16 with FPU *)
  54. | TTriCore -> 0x0520 (* 0x0520 Infineon *)
  55. | TAmd64 -> 0x8664 (* 0x8664 AMD x64 and Intel E64T *)
  56. | TM32R -> 0x9041 (* 0x9041 M32R *)
  57. let int_of_coff_props props = List.fold_left (fun acc prop ->
  58. (match prop with
  59. | RelocsStripped -> 0x1 (* 0x1 *)
  60. | ExecutableImage -> 0x2 (* 0x2 *)
  61. | LineNumsStripped -> 0x4 (* 0x4 *)
  62. | LocalSymsStripped -> 0x8 (* 0x8 *)
  63. | AgressiveWsTrim -> 0x10 (* 0x10 *)
  64. | LargeAddressAware -> 0x20 (* 0x20 *)
  65. | BytesReversedLO -> 0x80 (* 0x80 *)
  66. | Machine32Bit -> 0x100 (* 0x100 *)
  67. | DebugStripped -> 0x200 (* 0x200 *)
  68. | RemovableRunFromSwap -> 0x400 (* 0x400 *)
  69. | NetRunFromSwap -> 0x800 (* 0x800 *)
  70. | FileSystem -> 0x1000 (* 0x1000 *)
  71. | FileDll -> 0x2000 (* 0x2000 *)
  72. | UpSystemOnly -> 0x4000 (* 0x4000 *)
  73. | BytesReversedHI -> 0x8000 (* 0x8000 *)
  74. ) lor acc
  75. ) 0 props
  76. let int32_of_section_prop props = List.fold_left (fun acc prop ->
  77. Int32.logor (match prop with
  78. | SNoPad -> 0x8l (* 0x8 *)
  79. | SHasCode -> 0x20l (* 0x20 *)
  80. | SHasIData -> 0x40l (* 0x40 *)
  81. | SHasData -> 0x80l (* 0x80 *)
  82. | SHasLinkInfo -> 0x200l (* 0x200 *)
  83. | SLinkRemove -> 0x1000l (* 0x1000 *)
  84. | SGlobalRel -> 0x8000l (* 0x8000 *)
  85. | SHas16BitMem -> 0x20000l (* 0x20000 *)
  86. | SAlign1Bytes -> 0x100000l (* 0x100000 *)
  87. | SAlign2Bytes -> 0x200000l (* 0x200000 *)
  88. | SAlign4Bytes -> 0x300000l (* 0x300000 *)
  89. | SAlign8Bytes -> 0x400000l (* 0x400000 *)
  90. | SAlign16Bytes -> 0x500000l (* 0x500000 *)
  91. | SAlign32Bytes -> 0x600000l (* 0x600000 *)
  92. | SAlign64Bytes -> 0x700000l (* 0x700000 *)
  93. | SAlign128Bytes -> 0x800000l (* 0x800000 *)
  94. | SAlign256Bytes -> 0x900000l (* 0x900000 *)
  95. | SAlign512Bytes -> 0xA00000l (* 0xA00000 *)
  96. | SAlign1024Bytes -> 0xB00000l (* 0xB00000 *)
  97. | SAlign2048Bytes -> 0xC00000l (* 0xC00000 *)
  98. | SAlign4096Bytes -> 0xD00000l (* 0xD00000 *)
  99. | SAlign8192Bytes -> 0xE00000l (* 0xE00000 *)
  100. | SHasExtRelocs -> 0x1000000l (* 0x1000000 *)
  101. | SCanDiscard -> 0x02000000l (* 0x02000000 *)
  102. | SNotCached -> 0x04000000l (* 0x04000000 *)
  103. | SNotPaged -> 0x08000000l (* 0x08000000 *)
  104. | SShared -> 0x10000000l (* 0x10000000 *)
  105. | SExec -> 0x20000000l (* 0x20000000 *)
  106. | SRead -> 0x40000000l (* 0x40000000 *)
  107. | SWrite -> 0x80000000l (* 0x80000000 *)
  108. ) acc
  109. ) 0l props
  110. let int_of_pe_magic m = match m with
  111. | P32 -> 0x10b
  112. | PRom -> 0x107
  113. | P64 -> 0x20b
  114. let int_of_subsystem s = match s with
  115. | SUnknown -> 0 (* 0 *)
  116. | SNative -> 1 (* 1 *)
  117. | SWGui -> 2 (* 2 *)
  118. | SWCui -> 3 (* 3 *)
  119. | SPCui -> 7 (* 7 *)
  120. | SWCeGui -> 9 (* 9 *)
  121. | SEfi -> 10 (* 10 *)
  122. | SEfiBoot -> 11 (* 11 *)
  123. | SEfiRuntime -> 12 (* 12 *)
  124. | SEfiRom -> 13 (* 13 *)
  125. | SXbox -> 14 (* 14 *)
  126. let int_of_dll_props props = List.fold_left (fun acc prop ->
  127. (match prop with
  128. | DDynamicBase -> 0x0040 (* 0x0040 *)
  129. | DForceIntegrity -> 0x0080 (* 0x0080 *)
  130. | DNxCompat -> 0x0100 (* 0x0100 *)
  131. | DNoIsolation -> 0x0200 (* 0x0200 *)
  132. | DNoSeh -> 0x0400 (* 0x0400 *)
  133. | DNoBind -> 0x0800 (* 0x0800 *)
  134. | DWdmDriver -> 0x2000 (* 0x2000 *)
  135. | DTerminalServer -> 0x8000 (* 0x8000 *)
  136. ) lor acc
  137. ) 0 props
  138. let int_of_clr_flags props = List.fold_left (fun acc prop ->
  139. (match prop with
  140. | FIlOnly -> 0x1 (* 0x1 *)
  141. | F32BitRequired -> 0x2 (* 0x2 *)
  142. | FIlLibrary -> 0x4 (* 0x4 *)
  143. | FSigned -> 0x8 (* 0x8 *)
  144. | FNativeEntry -> 0x10 (* 0x10 *)
  145. | FTrackDebug -> 0x10000 (* 0x10000 *)
  146. ) lor acc
  147. ) 0 props