github.com/primecitizens/pcz/std@v0.2.1/encoding/binfmt/pe/pe.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright 2023 The Prime Citizens 3 // 4 // Copyright 2009 The Go Authors. All rights reserved. 5 // Use of this source code is governed by a BSD-style 6 // license that can be found in the LICENSE file. 7 8 // PE (Portable Executable) file writing 9 // https://docs.microsoft.com/en-us/windows/win32/debug/pe-format 10 11 package pe 12 13 type FileHeader struct { 14 Machine uint16 15 NumberOfSections uint16 16 TimeDateStamp uint32 17 PointerToSymbolTable uint32 18 NumberOfSymbols uint32 19 SizeOfOptionalHeader uint16 20 Characteristics uint16 21 } 22 23 type DataDirectory struct { 24 VirtualAddress uint32 25 Size uint32 26 } 27 28 type OptionalHeader32 struct { 29 Magic uint16 30 MajorLinkerVersion uint8 31 MinorLinkerVersion uint8 32 SizeOfCode uint32 33 SizeOfInitializedData uint32 34 SizeOfUninitializedData uint32 35 AddressOfEntryPoint uint32 36 BaseOfCode uint32 37 BaseOfData uint32 38 ImageBase uint32 39 SectionAlignment uint32 40 FileAlignment uint32 41 MajorOperatingSystemVersion uint16 42 MinorOperatingSystemVersion uint16 43 MajorImageVersion uint16 44 MinorImageVersion uint16 45 MajorSubsystemVersion uint16 46 MinorSubsystemVersion uint16 47 Win32VersionValue uint32 48 SizeOfImage uint32 49 SizeOfHeaders uint32 50 CheckSum uint32 51 Subsystem uint16 52 DllCharacteristics uint16 53 SizeOfStackReserve uint32 54 SizeOfStackCommit uint32 55 SizeOfHeapReserve uint32 56 SizeOfHeapCommit uint32 57 LoaderFlags uint32 58 NumberOfRvaAndSizes uint32 59 DataDirectory [16]DataDirectory 60 } 61 62 type OptionalHeader64 struct { 63 Magic uint16 64 MajorLinkerVersion uint8 65 MinorLinkerVersion uint8 66 SizeOfCode uint32 67 SizeOfInitializedData uint32 68 SizeOfUninitializedData uint32 69 AddressOfEntryPoint uint32 70 BaseOfCode uint32 71 ImageBase uint64 72 SectionAlignment uint32 73 FileAlignment uint32 74 MajorOperatingSystemVersion uint16 75 MinorOperatingSystemVersion uint16 76 MajorImageVersion uint16 77 MinorImageVersion uint16 78 MajorSubsystemVersion uint16 79 MinorSubsystemVersion uint16 80 Win32VersionValue uint32 81 SizeOfImage uint32 82 SizeOfHeaders uint32 83 CheckSum uint32 84 Subsystem uint16 85 DllCharacteristics uint16 86 SizeOfStackReserve uint64 87 SizeOfStackCommit uint64 88 SizeOfHeapReserve uint64 89 SizeOfHeapCommit uint64 90 LoaderFlags uint32 91 NumberOfRvaAndSizes uint32 92 DataDirectory [16]DataDirectory 93 } 94 95 const ( 96 IMAGE_FILE_MACHINE_UNKNOWN = 0x0 97 IMAGE_FILE_MACHINE_AM33 = 0x1d3 98 IMAGE_FILE_MACHINE_AMD64 = 0x8664 99 IMAGE_FILE_MACHINE_ARM = 0x1c0 100 IMAGE_FILE_MACHINE_ARMNT = 0x1c4 101 IMAGE_FILE_MACHINE_ARM64 = 0xaa64 102 IMAGE_FILE_MACHINE_EBC = 0xebc 103 IMAGE_FILE_MACHINE_I386 = 0x14c 104 IMAGE_FILE_MACHINE_IA64 = 0x200 105 IMAGE_FILE_MACHINE_M32R = 0x9041 106 IMAGE_FILE_MACHINE_MIPS16 = 0x266 107 IMAGE_FILE_MACHINE_MIPSFPU = 0x366 108 IMAGE_FILE_MACHINE_MIPSFPU16 = 0x466 109 IMAGE_FILE_MACHINE_POWERPC = 0x1f0 110 IMAGE_FILE_MACHINE_POWERPCFP = 0x1f1 111 IMAGE_FILE_MACHINE_R4000 = 0x166 112 IMAGE_FILE_MACHINE_SH3 = 0x1a2 113 IMAGE_FILE_MACHINE_SH3DSP = 0x1a3 114 IMAGE_FILE_MACHINE_SH4 = 0x1a6 115 IMAGE_FILE_MACHINE_SH5 = 0x1a8 116 IMAGE_FILE_MACHINE_THUMB = 0x1c2 117 IMAGE_FILE_MACHINE_WCEMIPSV2 = 0x169 118 IMAGE_FILE_MACHINE_RISCV32 = 0x5032 119 IMAGE_FILE_MACHINE_RISCV64 = 0x5064 120 IMAGE_FILE_MACHINE_RISCV128 = 0x5128 121 ) 122 123 // IMAGE_DIRECTORY_ENTRY constants 124 const ( 125 IMAGE_DIRECTORY_ENTRY_EXPORT = 0 126 IMAGE_DIRECTORY_ENTRY_IMPORT = 1 127 IMAGE_DIRECTORY_ENTRY_RESOURCE = 2 128 IMAGE_DIRECTORY_ENTRY_EXCEPTION = 3 129 IMAGE_DIRECTORY_ENTRY_SECURITY = 4 130 IMAGE_DIRECTORY_ENTRY_BASERELOC = 5 131 IMAGE_DIRECTORY_ENTRY_DEBUG = 6 132 IMAGE_DIRECTORY_ENTRY_ARCHITECTURE = 7 133 IMAGE_DIRECTORY_ENTRY_GLOBALPTR = 8 134 IMAGE_DIRECTORY_ENTRY_TLS = 9 135 IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG = 10 136 IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT = 11 137 IMAGE_DIRECTORY_ENTRY_IAT = 12 138 IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT = 13 139 IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR = 14 140 ) 141 142 // Values of IMAGE_FILE_HEADER.Characteristics. These can be combined together. 143 const ( 144 IMAGE_FILE_RELOCS_STRIPPED = 0x0001 145 IMAGE_FILE_EXECUTABLE_IMAGE = 0x0002 146 IMAGE_FILE_LINE_NUMS_STRIPPED = 0x0004 147 IMAGE_FILE_LOCAL_SYMS_STRIPPED = 0x0008 148 IMAGE_FILE_AGGRESIVE_WS_TRIM = 0x0010 149 IMAGE_FILE_LARGE_ADDRESS_AWARE = 0x0020 150 IMAGE_FILE_BYTES_REVERSED_LO = 0x0080 151 IMAGE_FILE_32BIT_MACHINE = 0x0100 152 IMAGE_FILE_DEBUG_STRIPPED = 0x0200 153 IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP = 0x0400 154 IMAGE_FILE_NET_RUN_FROM_SWAP = 0x0800 155 IMAGE_FILE_SYSTEM = 0x1000 156 IMAGE_FILE_DLL = 0x2000 157 IMAGE_FILE_UP_SYSTEM_ONLY = 0x4000 158 IMAGE_FILE_BYTES_REVERSED_HI = 0x8000 159 ) 160 161 // OptionalHeader64.Subsystem and OptionalHeader32.Subsystem values. 162 const ( 163 IMAGE_SUBSYSTEM_UNKNOWN = 0 164 IMAGE_SUBSYSTEM_NATIVE = 1 165 IMAGE_SUBSYSTEM_WINDOWS_GUI = 2 166 IMAGE_SUBSYSTEM_WINDOWS_CUI = 3 167 IMAGE_SUBSYSTEM_OS2_CUI = 5 168 IMAGE_SUBSYSTEM_POSIX_CUI = 7 169 IMAGE_SUBSYSTEM_NATIVE_WINDOWS = 8 170 IMAGE_SUBSYSTEM_WINDOWS_CE_GUI = 9 171 IMAGE_SUBSYSTEM_EFI_APPLICATION = 10 172 IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER = 11 173 IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER = 12 174 IMAGE_SUBSYSTEM_EFI_ROM = 13 175 IMAGE_SUBSYSTEM_XBOX = 14 176 IMAGE_SUBSYSTEM_WINDOWS_BOOT_APPLICATION = 16 177 ) 178 179 // OptionalHeader64.DllCharacteristics and OptionalHeader32.DllCharacteristics 180 // values. These can be combined together. 181 const ( 182 IMAGE_DLLCHARACTERISTICS_HIGH_ENTROPY_VA = 0x0020 183 IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE = 0x0040 184 IMAGE_DLLCHARACTERISTICS_FORCE_INTEGRITY = 0x0080 185 IMAGE_DLLCHARACTERISTICS_NX_COMPAT = 0x0100 186 IMAGE_DLLCHARACTERISTICS_NO_ISOLATION = 0x0200 187 IMAGE_DLLCHARACTERISTICS_NO_SEH = 0x0400 188 IMAGE_DLLCHARACTERISTICS_NO_BIND = 0x0800 189 IMAGE_DLLCHARACTERISTICS_APPCONTAINER = 0x1000 190 IMAGE_DLLCHARACTERISTICS_WDM_DRIVER = 0x2000 191 IMAGE_DLLCHARACTERISTICS_GUARD_CF = 0x4000 192 IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE = 0x8000 193 ) 194 195 type IMAGE_IMPORT_DESCRIPTOR struct { 196 OriginalFirstThunk uint32 197 TimeDateStamp uint32 198 ForwarderChain uint32 199 Name uint32 200 FirstThunk uint32 201 } 202 203 type IMAGE_EXPORT_DIRECTORY struct { 204 Characteristics uint32 205 TimeDateStamp uint32 206 MajorVersion uint16 207 MinorVersion uint16 208 Name uint32 209 Base uint32 210 NumberOfFunctions uint32 211 NumberOfNames uint32 212 AddressOfFunctions uint32 213 AddressOfNames uint32 214 AddressOfNameOrdinals uint32 215 } 216 217 var ( 218 // PEBASE is the base address for the executable. 219 // It is small for 32-bit and large for 64-bit. 220 PEBASE int64 221 222 // SectionAlignment must be greater than or equal to FileAlignment. 223 // The default is the page size for the architecture. 224 PESECTALIGN int64 = 0x1000 225 226 // FileAlignment should be a power of 2 between 512 and 64 K, inclusive. 227 // The default is 512. If the SectionAlignment is less than 228 // the architecture's page size, then FileAlignment must match SectionAlignment. 229 PEFILEALIGN int64 = 2 << 8 230 ) 231 232 const ( 233 PeMinimumTargetMajorVersion = 6 234 PeMinimumTargetMinorVersion = 1 235 )