format.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. package pe
  2. // CONSTANTS / MAGIC NUMBERS
  3. // MZSignature is the signature of the MZ format. This is the value of the
  4. // Signature field in ImageDOSHeader.
  5. var MZSignature = [2]byte{'M', 'Z'}
  6. // PESignature is the signature of the PE format. This is the value of the
  7. // Signature field in ImageNTHeaders32 and ImageNTHeaders64.
  8. var PESignature = [4]byte{'P', 'E', 0, 0}
  9. // Enumeration of magic numbers
  10. const (
  11. // ImageNTOptionalHeader32Magic is the magic number for 32-bit optional
  12. // header (ImageOptionalHeader32)
  13. ImageNTOptionalHeader32Magic = 0x010b
  14. // ImageNTOptionalHeader64Magic is the magic number for 64-bit optional
  15. // header (ImageOptionalHeader64)
  16. ImageNTOptionalHeader64Magic = 0x020b
  17. )
  18. // Enumeration of structure lengths.
  19. const (
  20. // SizeOfImageDOSHeader is the on-disk size of the ImageDOSHeader
  21. // structure.
  22. SizeOfImageDOSHeader = 64
  23. // SizeOfImageFileHeader is the on-disk size of the ImageFileHeader
  24. // structure.
  25. SizeOfImageFileHeader = 20
  26. // SizeOfImageOptionalHeader32 is the on-disk size of the
  27. // ImageOptionalHeader32 structure.
  28. SizeOfImageOptionalHeader32 = 224
  29. // SizeOfImageOptionalHeader64 is the on-disk size of the
  30. // ImageOptionalHeader64 structure.
  31. SizeOfImageOptionalHeader64 = 240
  32. // SizeOfImageNTHeaders32 is the on-disk size of the ImageNTHeaders32
  33. // structure.
  34. SizeOfImageNTHeaders32 = 248
  35. // SizeOfImageNTHeaders64 is the on-disk size of the ImageNTHeaders64
  36. // structure.
  37. SizeOfImageNTHeaders64 = 264
  38. // SizeOfImageDataDirectory is the on-disk size of the ImageDataDirectory
  39. // structure.
  40. SizeOfImageDataDirectory = 8
  41. )
  42. // Enumeration of useful field offsets.
  43. const (
  44. // OffsetOfOptionalHeaderFromNTHeader is the offset from the start of
  45. // the NT header to the optional header magic value. This is helpful for
  46. // determining if the PE file is PE32 or PE64.
  47. OffsetOfOptionalHeaderFromNTHeader = 0x18
  48. )
  49. // Enumeration of fixed-size array lengths in PE
  50. const (
  51. // NumDirectoryEntries specifies the number of data directory entries.
  52. NumDirectoryEntries = 16
  53. // SectionNameLength is the size of a section short name.
  54. SectionNameLength = 8
  55. )
  56. // Enumeration of known Windows Loader limits. (Some of these may not be
  57. // imposed by the format itself and purely by Windows runtime.)
  58. const (
  59. // MaxNumSections specifies the maximum number of sections that are
  60. // allowed. This is imposed by Windows Loader.
  61. MaxNumSections = 96
  62. )
  63. // ENUMERATION VALUES
  64. // Enumeration of machine values for the file header.
  65. const (
  66. ImageFileMachineUnknown = 0x0000
  67. ImageFileMachineTargetHost = 0x0001
  68. ImageFileMachinei386 = 0x014c
  69. ImageFileMachineR3000BE = 0x0160
  70. ImageFileMachineR3000 = 0x0162
  71. ImageFileMachineR4000 = 0x0166
  72. ImageFileMachineR10000 = 0x0168
  73. ImageFileMachineWCEMIPSv2 = 0x0169
  74. ImageFileMachineAlpha = 0x0184
  75. ImageFileMachineSH3 = 0x01a2
  76. ImageFileMachineSH3DSP = 0x01a3
  77. ImageFileMachineSH3E = 0x01a4
  78. ImageFileMachineSH4 = 0x01a6
  79. ImageFileMachineSH5 = 0x01a8
  80. ImageFileMachineARM = 0x01c0
  81. ImageFileMachineTHUMB = 0x01c2
  82. ImageFileMachineARMNT = 0x01c4
  83. ImageFileMachineAM33 = 0x01d3
  84. ImageFileMachinePowerPC = 0x01F0
  85. ImageFileMachinePowerPCFP = 0x01f1
  86. ImageFileMachineIA64 = 0x0200
  87. ImageFileMachineMIPS16 = 0x0266
  88. ImageFileMachineAlpha64 = 0x0284
  89. ImageFileMachineMIPSFPU = 0x0366
  90. ImageFileMachineMIPSFPU16 = 0x0466
  91. ImageFileMachineAXP64 = ImageFileMachineAlpha64
  92. ImageFileMachineTricore = 0x0520
  93. ImageFileMachineCEF = 0x00CE
  94. ImageFileMachineEBC = 0x0EBC
  95. ImageFileMachineAMD64 = 0x8664
  96. ImageFileMachineM32R = 0x9041
  97. ImageFileMachineARM64 = 0xAA64
  98. ImageFileMachineCEE = 0x0C0E
  99. ImageFileMachineRISCV32 = 0x5032
  100. ImageFileMachineRISCV64 = 0x5064
  101. ImageFileMachineRISCV128 = 0x5128
  102. )
  103. // Enumeration of charateristics values for the file header.
  104. const (
  105. ImageFileRelocsStripped = 0x0001
  106. ImageFileExecutableImage = 0x0002
  107. ImageFileLineNumsStripped = 0x0004
  108. ImageFileLocalSymsStripped = 0x0008
  109. ImageFileAggressiveWSTrim = 0x0010
  110. ImageFileLargeAddressAware = 0x0020
  111. ImageFileBytesReversedLo = 0x0080
  112. ImageFile32BitMachine = 0x0100
  113. ImageFileDebugStripped = 0x0200
  114. ImageFileRemovableRunFromSwap = 0x0400
  115. ImageFileNetRunFromSwap = 0x0800
  116. ImageFileSystem = 0x1000
  117. ImageFileDLL = 0x2000
  118. ImageFileUPSystemOnly = 0x4000
  119. ImageFileBytesReversedHi = 0x8000
  120. )
  121. // Enumeration of image subsystem values.
  122. const (
  123. ImageSubsystemUnknown = 0
  124. ImageSubsystemNative = 1
  125. ImageSubsystemWindowsGUI = 2
  126. ImageSubsystemWindowsCUI = 3
  127. ImageSubsystemOS2CUI = 5
  128. ImageSubsystemPOSIXCUI = 7
  129. ImageSubsystemNativeWindows = 8
  130. ImageSubsystemWindowsCEGUI = 9
  131. ImageSubsystemEFIApplication = 10
  132. ImageSubsystemEFIBootServiceDriver = 11
  133. ImageSubsystemEFIRuntimeDriver = 12
  134. ImageSubsystemEFIROM = 13
  135. ImageSubsystemXBox = 14
  136. ImageSubsystemWindowsBootApplication = 16
  137. ImageSubsystemXBoxCodeCatalog = 17
  138. )
  139. // Enumeration of DLL characteristics values.
  140. const (
  141. ImageDLLCharacteristicsHighEntropyVA = 0x0020
  142. ImageDLLCharacteristicsDynamicBase = 0x0040
  143. ImageDLLCharacteristicsForceIntegrity = 0x0080
  144. ImageDLLCharacteristicsNXCompat = 0x0100
  145. ImageDLLCharacteristicsNoIsolation = 0x0200
  146. ImageDLLCharacteristicsNoSEH = 0x0400
  147. ImageDLLCharacteristicsNoBind = 0x0800
  148. ImageDLLCharacteristicsAppContainer = 0x1000
  149. ImageDLLCharacteristicsWDMDriver = 0x2000
  150. ImageDLLCharacteristicsGuardCF = 0x4000
  151. ImageDLLCharacteristicsTerminalServerAware = 0x8000
  152. )
  153. // Enumeration of image directory entry indexes. These represent indices into
  154. // the data directory array of the optional header.
  155. const (
  156. ImageDirectoryEntryExport = 0
  157. ImageDirectoryEntryImport = 1
  158. ImageDirectoryEntryResource = 2
  159. ImageDirectoryEntryException = 3
  160. ImageDirectoryEntrySecurity = 4
  161. ImageDirectoryEntryBaseReloc = 5
  162. ImageDirectoryEntryDebug = 6
  163. ImageDirectoryEntryCopyright = 7
  164. ImageDirectoryEntryArchitecture = 7
  165. ImageDirectoryEntryGlobalPtr = 8
  166. ImageDirectoryEntryTLS = 9
  167. ImageDirectoryEntryLoadConfig = 10
  168. ImageDirectoryEntryBoundImport = 11
  169. ImageDirectoryEntryIAT = 12
  170. ImageDirectoryEntryDelayImport = 13
  171. ImageDirectoryEntryCOMDescriptor = 14
  172. )
  173. // Enumeration of image section characteristics.
  174. const (
  175. ImageSectionCharacteristicsNoPad = 0x00000008
  176. ImageSectionCharacteristicsContainsCode = 0x00000020
  177. ImageSectionCharacteristicsContainsInitializedData = 0x00000040
  178. ImageSectionCharacteristicsContainsUninitailizedData = 0x00000080
  179. ImageSectionCharacteristicsLinkOther = 0x00000100
  180. ImageSectionCharacteristicsLinkInfo = 0x00000200
  181. ImageSectionCharacteristicsLinkRemove = 0x00000800
  182. ImageSectionCharacteristicsLinkCOMDAT = 0x00001000
  183. ImageSectionCharacteristicsNoDeferSpecExc = 0x00004000
  184. ImageSectionCharacteristicsGPRel = 0x00008000
  185. ImageSectionCharacteristicsMemoryFarData = 0x00008000
  186. ImageSectionCharacteristicsMemoryPurgeable = 0x00020000
  187. ImageSectionCharacteristicsMemory16Bit = 0x00020000
  188. ImageSectionCharacteristicsMemoryLocked = 0x00040000
  189. ImageSectionCharacteristicsMemoryPreload = 0x00080000
  190. ImageSectionCharacteristicsAlign1Bytes = 0x00100000
  191. ImageSectionCharacteristicsAlign2Bytes = 0x00200000
  192. ImageSectionCharacteristicsAlign4Bytes = 0x00300000
  193. ImageSectionCharacteristicsAlign8Bytes = 0x00400000
  194. ImageSectionCharacteristicsAlign16Bytes = 0x00500000
  195. ImageSectionCharacteristicsAlign32Bytes = 0x00600000
  196. ImageSectionCharacteristicsAlign64Bytes = 0x00700000
  197. ImageSectionCharacteristicsAlign128Bytes = 0x00800000
  198. ImageSectionCharacteristicsAlign256Bytes = 0x00900000
  199. ImageSectionCharacteristicsAlign512Bytes = 0x00A00000
  200. ImageSectionCharacteristicsAlign1024Bytes = 0x00B00000
  201. ImageSectionCharacteristicsAlign2048Bytes = 0x00C00000
  202. ImageSectionCharacteristicsAlign4096Bytes = 0x00D00000
  203. ImageSectionCharacteristicsAlign8192Bytes = 0x00E00000
  204. ImageSectionCharacteristicsAlignMask = 0x00F00000
  205. ImageSectionCharacteristicsLinkNumRelocOverflow = 0x01000000
  206. ImageSectionCharacteristicsMemoryDiscardable = 0x02000000
  207. ImageSectionCharacteristicsMemoryNotCached = 0x04000000
  208. ImageSectionCharacteristicsMemoryNotPaged = 0x08000000
  209. ImageSectionCharacteristicsMemoryShared = 0x10000000
  210. ImageSectionCharacteristicsMemoryExecute = 0x20000000
  211. ImageSectionCharacteristicsMemoryRead = 0x40000000
  212. ImageSectionCharacteristicsMemoryWrite = 0x80000000
  213. )
  214. // Enumeration of TLS characteristics.
  215. const (
  216. ImageSectionTLSCharacteristicsScaleIndex = 0x00000001
  217. )
  218. // Enumeration of relocation types.
  219. const (
  220. ImageRelBasedAbsolute = 0
  221. ImageRelBasedHigh = 1
  222. ImageRelBasedLow = 2
  223. ImageRelBasedHighLow = 3
  224. ImageRelBasedHighAdj = 4
  225. ImageRelBasedMachineSpecific5 = 5
  226. ImageRelBasedReserved = 6
  227. ImageRelBasedMachineSpecific7 = 7
  228. ImageRelBasedMachineSpecific8 = 8
  229. ImageRelBasedMachineSpecific9 = 9
  230. ImageRelBasedDir64 = 10
  231. )
  232. // ImageDOSHeader is the structure of the DOS MZ Executable format. All PE
  233. // files contain at least a valid stub DOS MZ executable at the top; the PE
  234. // format itself starts at the address specified by NewHeaderAddr.
  235. type ImageDOSHeader struct {
  236. Signature [2]byte
  237. LastPageBytes uint16
  238. CountPages uint16
  239. CountRelocs uint16
  240. HeaderLen uint16
  241. MinAlloc uint16
  242. MaxAlloc uint16
  243. InitialSS uint16
  244. InitialSP uint16
  245. Checksum uint16
  246. InitialIP uint16
  247. InitialCS uint16
  248. RelocAddr uint16
  249. OverlayNum uint16
  250. Reserved [4]uint16
  251. OEMID uint16
  252. OEMInfo uint16
  253. Reserved2 [10]uint16
  254. NewHeaderAddr uint32
  255. }
  256. // ImageFileHeader contains some of the basic attributes about the PE/COFF
  257. // file, including the number of sections and the machine type.
  258. type ImageFileHeader struct {
  259. Machine uint16
  260. NumberOfSections uint16
  261. TimeDateStamp uint32
  262. PointerToSymbolTable uint32
  263. NumberOfSymbols uint32
  264. SizeOfOptionalHeader uint16
  265. Characteristics uint16
  266. }
  267. // ImageOptionalHeader32 contains the optional header for 32-bit PE images. It
  268. // is only 'optional' in the sense that not all PE/COFF binaries have it,
  269. // however it is required for executables and DLLs.
  270. type ImageOptionalHeader32 struct {
  271. Magic uint16
  272. MajorLinkerVersion uint8
  273. MinorLinkerVersion uint8
  274. SizeOfCode uint32
  275. SizeOfInitializedData uint32
  276. SizeOfUninitializedData uint32
  277. AddressOfEntryPoint uint32
  278. BaseOfCode uint32
  279. BaseOfData uint32
  280. ImageBase uint32
  281. SectionAlignment uint32
  282. FileAlignment uint32
  283. MajorOperatingSystemVersion uint16
  284. MinorOperatingSystemVersion uint16
  285. MajorImageVersion uint16
  286. MinorImageVersion uint16
  287. MajorSubsystemVersion uint16
  288. MinorSubsystemVersion uint16
  289. Win32VersionValue uint32
  290. SizeOfImage uint32
  291. SizeOfHeaders uint32
  292. CheckSum uint32
  293. Subsystem uint16
  294. DllCharacteristics uint16
  295. SizeOfStackReserve uint32
  296. SizeOfStackCommit uint32
  297. SizeOfHeapReserve uint32
  298. SizeOfHeapCommit uint32
  299. LoaderFlags uint32
  300. NumberOfRvaAndSizes uint32
  301. DataDirectory [NumDirectoryEntries]ImageDataDirectory
  302. }
  303. // ImageOptionalHeader64 contains the optional header for 64-bit PE images.
  304. type ImageOptionalHeader64 struct {
  305. Magic uint16
  306. MajorLinkerVersion uint8
  307. MinorLinkerVersion uint8
  308. SizeOfCode uint32
  309. SizeOfInitializedData uint32
  310. SizeOfUninitializedData uint32
  311. AddressOfEntryPoint uint32
  312. BaseOfCode uint32
  313. ImageBase uint64
  314. SectionAlignment uint32
  315. FileAlignment uint32
  316. MajorOperatingSystemVersion uint16
  317. MinorOperatingSystemVersion uint16
  318. MajorImageVersion uint16
  319. MinorImageVersion uint16
  320. MajorSubsystemVersion uint16
  321. MinorSubsystemVersion uint16
  322. Win32VersionValue uint32
  323. SizeOfImage uint32
  324. SizeOfHeaders uint32
  325. CheckSum uint32
  326. Subsystem uint16
  327. DllCharacteristics uint16
  328. SizeOfStackReserve uint64
  329. SizeOfStackCommit uint64
  330. SizeOfHeapReserve uint64
  331. SizeOfHeapCommit uint64
  332. LoaderFlags uint32
  333. NumberOfRvaAndSizes uint32
  334. DataDirectory [NumDirectoryEntries]ImageDataDirectory
  335. }
  336. // To64 converts the ImageOptionalHeader32 to an ImageOptionalHeader64.
  337. func (i ImageOptionalHeader32) To64() ImageOptionalHeader64 {
  338. return ImageOptionalHeader64{
  339. Magic: i.Magic,
  340. MajorLinkerVersion: i.MajorLinkerVersion,
  341. MinorLinkerVersion: i.MinorLinkerVersion,
  342. SizeOfCode: i.SizeOfCode,
  343. SizeOfInitializedData: i.SizeOfInitializedData,
  344. SizeOfUninitializedData: i.SizeOfUninitializedData,
  345. AddressOfEntryPoint: i.AddressOfEntryPoint,
  346. BaseOfCode: i.BaseOfCode,
  347. ImageBase: uint64(i.ImageBase),
  348. SectionAlignment: i.SectionAlignment,
  349. FileAlignment: i.FileAlignment,
  350. MajorOperatingSystemVersion: i.MajorOperatingSystemVersion,
  351. MinorOperatingSystemVersion: i.MinorOperatingSystemVersion,
  352. MajorImageVersion: i.MajorImageVersion,
  353. MinorImageVersion: i.MinorImageVersion,
  354. MajorSubsystemVersion: i.MajorSubsystemVersion,
  355. MinorSubsystemVersion: i.MinorSubsystemVersion,
  356. Win32VersionValue: i.Win32VersionValue,
  357. SizeOfImage: i.SizeOfImage,
  358. SizeOfHeaders: i.SizeOfHeaders,
  359. CheckSum: i.CheckSum,
  360. Subsystem: i.Subsystem,
  361. DllCharacteristics: i.DllCharacteristics,
  362. SizeOfStackReserve: uint64(i.SizeOfStackReserve),
  363. SizeOfStackCommit: uint64(i.SizeOfStackCommit),
  364. SizeOfHeapReserve: uint64(i.SizeOfHeapReserve),
  365. SizeOfHeapCommit: uint64(i.SizeOfHeapCommit),
  366. LoaderFlags: i.LoaderFlags,
  367. NumberOfRvaAndSizes: i.NumberOfRvaAndSizes,
  368. DataDirectory: i.DataDirectory,
  369. }
  370. }
  371. // ImageNTHeaders32 contains the PE file headers for 32-bit PE images.
  372. type ImageNTHeaders32 struct {
  373. // Signature identifies the PE format; Always "PE\0\0".
  374. Signature [4]byte
  375. FileHeader ImageFileHeader
  376. OptionalHeader ImageOptionalHeader32
  377. }
  378. // ImageNTHeaders64 contains the PE file headers for 64-bit PE images.
  379. type ImageNTHeaders64 struct {
  380. // Signature identifies the PE format; Always "PE\0\0".
  381. Signature [4]byte
  382. FileHeader ImageFileHeader
  383. OptionalHeader ImageOptionalHeader64
  384. }
  385. // To64 converts the ImageNTHeaders32 to an ImageNTHeaders64.
  386. func (i ImageNTHeaders32) To64() ImageNTHeaders64 {
  387. return ImageNTHeaders64{
  388. Signature: i.Signature,
  389. FileHeader: i.FileHeader,
  390. OptionalHeader: i.OptionalHeader.To64(),
  391. }
  392. }
  393. // ImageDataDirectory holds a record for the given data directory. Each data
  394. // directory contains information about another section, such as the import
  395. // table. The index of the directory entry determines which section it
  396. // pertains to.
  397. type ImageDataDirectory struct {
  398. VirtualAddress uint32
  399. Size uint32
  400. }
  401. // ImageSectionHeader is the header for a section. Windows Loader uses these
  402. // entries to configure the memory mapping of the executable. A series of
  403. // these structures immediately follow the headers.
  404. type ImageSectionHeader struct {
  405. Name [SectionNameLength]byte
  406. PhysicalAddressOrVirtualSize uint32
  407. VirtualAddress uint32
  408. SizeOfRawData uint32
  409. PointerToRawData uint32
  410. PointerToRelocations uint32
  411. PointerToLinenumbers uint32
  412. NumberOfRelocations uint16
  413. NumberOfLinenumbers uint16
  414. Characteristics uint32
  415. }
  416. // ImageBaseRelocation holds the header for a single page of base relocation
  417. // data. The .reloc section of the binary contains a series of blocks of
  418. // base relocation data, each starting with this header and followed by n
  419. // 16-bit values that each represent a single relocation. The 4 most
  420. // significant bits specify the type of relocation, while the 12 least
  421. // significant bits contain the lower bits of the address (which is combined
  422. // with the virtual address of the page.) The SizeOfBlock value specifies the
  423. // size of an entire block, in bytes, including its header.
  424. type ImageBaseRelocation struct {
  425. VirtualAddress uint32
  426. SizeOfBlock uint32
  427. }
  428. // The ImageImportDescriptor contains information about an imported module.
  429. type ImageImportDescriptor struct {
  430. OriginalFirstThunk uint32
  431. TimeDateStamp uint32
  432. ForwarderChain uint32
  433. Name uint32
  434. FirstThunk uint32
  435. }
  436. // The ImageExportDirectory contains information about the module's exports.
  437. type ImageExportDirectory struct {
  438. Characteristics uint32
  439. TimeDateStamp uint32
  440. MajorVersion uint16
  441. MinorVersion uint16
  442. Name uint32
  443. Base uint32
  444. NumberOfFunctions uint32
  445. NumberOfNames uint32
  446. AddressOfFunctions uint32
  447. AddressOfNames uint32
  448. AddressOfNameOrdinals uint32
  449. }
  450. // ImageTLSDirectory32 contains information about the module's thread local
  451. // storage callbacks (in PE32)
  452. type ImageTLSDirectory32 struct {
  453. StartAddressOfRawData uint32
  454. EndAddressOfRawData uint32
  455. AddressOfIndex uint32
  456. AddressOfCallBacks uint32
  457. SizeOfZeroFill uint32
  458. Characteristics uint32
  459. }
  460. // ImageTLSDirectory64 contains information about the module's thread local
  461. // storage callbacks (in PE64)
  462. type ImageTLSDirectory64 struct {
  463. StartAddressOfRawData uint64
  464. EndAddressOfRawData uint64
  465. AddressOfIndex uint64
  466. AddressOfCallBacks uint64
  467. SizeOfZeroFill uint32
  468. Characteristics uint32
  469. }
  470. // To64 converts the ImageTLSDirectory32 to an ImageTLSDirectory64.
  471. func (i ImageTLSDirectory32) To64() ImageTLSDirectory64 {
  472. return ImageTLSDirectory64{
  473. StartAddressOfRawData: uint64(i.StartAddressOfRawData),
  474. EndAddressOfRawData: uint64(i.EndAddressOfRawData),
  475. AddressOfIndex: uint64(i.AddressOfIndex),
  476. AddressOfCallBacks: uint64(i.AddressOfCallBacks),
  477. SizeOfZeroFill: i.SizeOfZeroFill,
  478. Characteristics: i.Characteristics,
  479. }
  480. }