| 1 |
/* |
| 2 |
genidl - Generate interface defintion language listing from a |
| 3 |
Portable Executable. |
| 4 |
Copyright (C) 2009-2016 mingw-w64 project |
| 5 |
|
| 6 |
This program is free software: you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation, either version 3 of the License, or |
| 9 |
(at your option) any later version. |
| 10 |
|
| 11 |
This program is distributed in the hope that it will be useful, |
| 12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
GNU General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
*/ |
| 19 |
|
| 20 |
#ifndef _GENIDL_READPE_H |
| 21 |
#define _GENIDL_READPE_H |
| 22 |
|
| 23 |
#pragma pack(push,1) |
| 24 |
|
| 25 |
/** |
| 26 |
* PE header layout structure |
| 27 |
* @see http://www.jbox.dk/sanos/source/include/os/pe.h.html |
| 28 |
*/ |
| 29 |
typedef struct sDosHeader { |
| 30 |
uint16_t magic; /**< PE signature, usually MZ or ZM */ |
| 31 |
uint16_t cblp; /**< (PE image size) % (512) */ |
| 32 |
uint16_t cp; /**< Size of PE image in 512 byte blocks */ |
| 33 |
uint16_t crlc; /**< Number of relocation table entries*/ |
| 34 |
uint16_t cparhdr; /**< Size of PE header */ |
| 35 |
uint16_t minalloc; /**< Minimum required memory */ |
| 36 |
uint16_t maxalloc; /**< Maximum required memory */ |
| 37 |
uint16_t ss; /**< Stack seg offset in load module */ |
| 38 |
uint16_t sp; /**< initial value of Stack Pointer */ |
| 39 |
uint16_t csum; /**< Image checksum */ |
| 40 |
uint16_t ip; /**< initial value of Instruction Pointer */ |
| 41 |
uint16_t cs; /**< Initial value of the CS register, relative to the segment the program was loaded at. */ |
| 42 |
uint16_t lfarlc; /**< Offset of the first relocation item in the file. */ |
| 43 |
uint16_t ovno; /**< Overlay number. */ |
| 44 |
uint16_t res[4]; /**< Reserved slack space */ |
| 45 |
uint16_t oemid; /**< OEM ID */ |
| 46 |
uint16_t oeminfo; /**< OEM info */ |
| 47 |
uint16_t res2[10]; /**< Reserved slack space */ |
| 48 |
int32_t lfanew; /**< Offset to new PE header */ |
| 49 |
} sDosHeader; |
| 50 |
|
| 51 |
/** |
| 52 |
* Data directory table structure layout |
| 53 |
*/ |
| 54 |
typedef struct sImgDataDir { |
| 55 |
uint32_t virtualAddress; /**< Relative Virtual Address */ |
| 56 |
uint32_t size; /**< Size of the table in bytes */ |
| 57 |
} sImgDataDir; |
| 58 |
|
| 59 |
#define IMG_NOOF_DIR_ENTRIES 16 |
| 60 |
|
| 61 |
/** |
| 62 |
* PE32 Optional header layout structure |
| 63 |
* @see http://www.microsoft.com/whdc/system/platform/firmware/PECOFF.mspx |
| 64 |
*/ |
| 65 |
typedef struct sImgOptionalHeaders32 { |
| 66 |
uint16_t Magic; /**< Identifies the type of image */ |
| 67 |
unsigned char MajorLinkerVersion; /**< Linker major version */ |
| 68 |
unsigned char MinorLinkerVersion; /**< Linker minor version */ |
| 69 |
uint32_t SizeOfCode; /**< Size of excutable .text and related sections */ |
| 70 |
uint32_t SizeOfInitializedData; /**< Size of initialized .data and related sections */ |
| 71 |
uint32_t SizeOfUninitializedData; /**< Size of unitialized .bss and related sections */ |
| 72 |
uint32_t AddressOfEntryPoint; /**< Address of image entry point relative to image base */ |
| 73 |
uint32_t BaseOfCode; /**< Address to start of .text section relative to image base */ |
| 74 |
uint32_t BaseOfData; /**< Address to start of .data section relative to image base */ |
| 75 |
uint32_t ImageBase; /**< Prefered load address of image */ |
| 76 |
uint32_t SectionAlignment; /**< Section allignment when loaded in memory */ |
| 77 |
uint32_t FileAlignment; /**< Section allignment of image on disk */ |
| 78 |
uint16_t MajorOperatingSystemVersion; /**< Minimum required OS version (Major version) */ |
| 79 |
uint16_t MinorOperatingSystemVersion; /**< Minimum required OS version (Minor version) */ |
| 80 |
uint16_t MajorImageVersion; /**< PE Image major version */ |
| 81 |
uint16_t MinorImageVersion; /**< PE Image minor version */ |
| 82 |
uint16_t MajorSubsystemVersion; /**< Subsystem major version */ |
| 83 |
uint16_t MinorSubsystemVersion; /**< Subsystem minor version */ |
| 84 |
uint32_t Win32VersionValue; /**< Reserved, set to 0 */ |
| 85 |
uint32_t SizeOfImage; /**< Total size of image in multiples of SectionAlignment */ |
| 86 |
uint32_t SizeOfHeaders; /**< Size of DOS stub header, PE header, and section headers in multiples of SectionAlignment */ |
| 87 |
uint32_t CheckSum; /**< Image checksum */ |
| 88 |
uint16_t Subsystem; /**< Image subsystem */ |
| 89 |
uint16_t DllCharacteristics; /**< DLL characteristics */ |
| 90 |
uint32_t SizeOfStackReserve; /**< Amount of stack to reserve */ |
| 91 |
uint32_t SizeOfStackCommit; /**< Amount of stack to commit on start */ |
| 92 |
uint32_t SizeOfHeapReserve; /**< Amount of heap to reserve */ |
| 93 |
uint32_t SizeOfHeapCommit; /**< Amount of heap to commit on start */ |
| 94 |
uint32_t LoaderFlags; /**< Reserved, set to 0 */ |
| 95 |
uint32_t NumberOfRvaAndSizes; /**< Number of DataDirectory entries at the end of the header */ |
| 96 |
sImgDataDir DataDirectory[IMG_NOOF_DIR_ENTRIES]; /**< Data directories */ |
| 97 |
} sImgOptionalHeaders32; |
| 98 |
|
| 99 |
/** |
| 100 |
* PE32+ Optional header layout structure |
| 101 |
* @see http://www.microsoft.com/whdc/system/platform/firmware/PECOFF.mspx |
| 102 |
*/ |
| 103 |
typedef struct sImgOptionalHeaders64 { |
| 104 |
uint16_t Magic; /**< Identifies the type of image */ |
| 105 |
unsigned char MajorLinkerVersion; /**< Linker major version */ |
| 106 |
unsigned char MinorLinkerVersion; /**< Linker minor version */ |
| 107 |
uint32_t SizeOfCode; /**< Size of excutable .text and related sections */ |
| 108 |
uint32_t SizeOfInitializedData; /**< Size of initialized .data and related sections */ |
| 109 |
uint32_t SizeOfUninitializedData; /**< Size of unitialized .bss and related sections */ |
| 110 |
uint32_t AddressOfEntryPoint; /**< Address of image entry point relative to image base */ |
| 111 |
uint32_t BaseOfCode; /**< Address to start of .text section relative to image base */ |
| 112 |
uint64_t ImageBase; /**< Prefered load address of image */ |
| 113 |
uint32_t SectionAlignment; /**< Section allignment when loaded in memory */ |
| 114 |
uint32_t FileAlignment; /**< Section allignment of image on disk */ |
| 115 |
uint16_t MajorOperatingSystemVersion; /**< Minimum required OS version (Major version) */ |
| 116 |
uint16_t MinorOperatingSystemVersion; /**< Minimum required OS version (Minor version) */ |
| 117 |
uint16_t MajorImageVersion; /**< PE Image major version */ |
| 118 |
uint16_t MinorImageVersion; /**< PE Image minor version */ |
| 119 |
uint16_t MajorSubsystemVersion; /**< Subsystem major version */ |
| 120 |
uint16_t MinorSubsystemVersion; /**< Subsystem minor version */ |
| 121 |
uint32_t Win32VersionValue; /**< Reserved, set to 0 */ |
| 122 |
uint32_t SizeOfImage; /**< Total size of image in multiples of SectionAlignment */ |
| 123 |
uint32_t SizeOfHeaders; /**< Size of DOS stub header, PE header, and section headers in multiples of SectionAlignment */ |
| 124 |
uint32_t CheckSum; /**< Image checksum */ |
| 125 |
uint16_t Subsystem; /**< Image subsystem */ |
| 126 |
uint16_t DllCharacteristics; /**< DLL characteristics */ |
| 127 |
uint64_t SizeOfStackReserve; /**< Amount of stack to reserve */ |
| 128 |
uint64_t SizeOfStackCommit; /**< Amount of stack to commit on start */ |
| 129 |
uint64_t SizeOfHeapReserve; /**< Amount of heap to reserve */ |
| 130 |
uint64_t SizeOfHeapCommit; /**< Amount of heap to commit on start */ |
| 131 |
uint32_t LoaderFlags; /**< Reserved, set to 0 */ |
| 132 |
uint32_t NumberOfRvaAndSizes; /**< Number of DataDirectory entries at the end of the header */ |
| 133 |
sImgDataDir DataDirectory[IMG_NOOF_DIR_ENTRIES]; /**< Data directories */ |
| 134 |
} sImgOptionalHeaders64; |
| 135 |
|
| 136 |
/** |
| 137 |
* COFF header structure layout |
| 138 |
*/ |
| 139 |
typedef struct sImgFileHeader { |
| 140 |
uint16_t Machine; /**< Machine ID to run image */ |
| 141 |
uint16_t NumberOfSections; /**< Number of sections, maximum of 96 */ |
| 142 |
uint32_t TimeDateStamp; /**< 32bit time_t seconds since epoch */ |
| 143 |
uint32_t PointerToSymbolTable; /**< File offset to COFF symbol table */ |
| 144 |
uint32_t NumberOfSymbols; /**< Number of symbol entries in COFF symbol table*/ |
| 145 |
uint16_t SizeOfOptionalHeader; /**< Size of optional header */ |
| 146 |
uint16_t Characteristics; /**< Object/Image attribute flags */ |
| 147 |
} sImgFileHeader; |
| 148 |
|
| 149 |
/** |
| 150 |
* PE32+ Headers structure layout on disk |
| 151 |
*/ |
| 152 |
typedef struct sImgNtHeaders64 { |
| 153 |
uint32_t Signature; /**< PE\0\0 signature */ |
| 154 |
sImgFileHeader FileHeader; /**< Common PE32/PE32+ header */ |
| 155 |
sImgOptionalHeaders64 OptionalHeader; /**< Optional fields for 64bit PE32+ executables */ |
| 156 |
} sImgNtHeaders64; |
| 157 |
|
| 158 |
/** |
| 159 |
* PE32 Headers structure layout on disk |
| 160 |
*/ |
| 161 |
typedef struct sImgNtHeaders32 { |
| 162 |
uint32_t Signature; /**< PE\0\0 signature */ |
| 163 |
sImgFileHeader FileHeader; /**< Common PE32/PE32+ header */ |
| 164 |
sImgOptionalHeaders32 OptionalHeader; /**< Optional fields for 32bit PE32 executables */ |
| 165 |
} sImgNtHeaders32; |
| 166 |
|
| 167 |
/** |
| 168 |
* Generic object type to hold both PE32 and PE32+ headers |
| 169 |
*/ |
| 170 |
typedef union uImgHeader { |
| 171 |
sImgNtHeaders32 hdr32; /**< PE32 header */ |
| 172 |
sImgNtHeaders64 hdr64; /**< PE32+ header */ |
| 173 |
} uImgHeader; |
| 174 |
|
| 175 |
#define IMG_FIRST_SECTION(ntheader) ((sImgSectionHdr *) ((ULONG_PTR)ntheader + FIELD_OFFSET(IMAGE_NT_HEADERS,OptionalHeader) + ((PIMAGE_NT_HEADERS)(ntheader))->FileHeader.SizeOfOptionalHeader)) |
| 176 |
|
| 177 |
/** |
| 178 |
* Section header structure layout |
| 179 |
*/ |
| 180 |
typedef struct sImgSectionHdr { |
| 181 |
unsigned char Name[8]; /**< Section Name */ |
| 182 |
union { |
| 183 |
uint32_t PhysicalAddress; /**< */ |
| 184 |
uint32_t VirtualSize; /**< Total section size when loaded in memory */ |
| 185 |
} Misc; |
| 186 |
uint32_t VirtualAddress; /**< Section offset from image base */ |
| 187 |
uint32_t SizeOfRawData; /**< Size of section on disk */ |
| 188 |
uint32_t PointerToRawData; /**< Pointer to first page of section */ |
| 189 |
uint32_t PointerToRelocations; /**< Pointer to section relocation entries */ |
| 190 |
uint32_t PointerToLinenumbers; /**< Pointer to COFF debug line-number entries */ |
| 191 |
uint16_t NumberOfRelocations; /**< Number of relocation entries in section */ |
| 192 |
uint16_t NumberOfLinenumbers; /**< Number of line-number entries in section */ |
| 193 |
uint32_t Characteristics; /**< Section characteristics flag */ |
| 194 |
} sImgSectionHdr; |
| 195 |
|
| 196 |
/** |
| 197 |
* Finds PE signature in file associated with fp |
| 198 |
* @param[in] fp Pointer to a file |
| 199 |
* @param[out] be64 indicates if PE is 64bit if 1, 32bit otherwise |
| 200 |
*@return Offset at which a PE signature is found |
| 201 |
*/ |
| 202 |
int32_t genidl_ispe (FILE *fp, int32_t *be64); |
| 203 |
|
| 204 |
/** |
| 205 |
* Reads the typelib from a file |
| 206 |
* @param[in] fp Pointer to file |
| 207 |
* @param[in] nores typelib to read in directory |
| 208 |
* @param[out] pDta raw typelib info |
| 209 |
* @param[out] szDta size of raw typelib information |
| 210 |
* @return 1 for success |
| 211 |
*/ |
| 212 |
int32_t genidl_pe_typelib_resource_read (FILE *fp, int32_t noRes, unsigned char **pDta, size_t *szDta); |
| 213 |
|
| 214 |
/** |
| 215 |
* Counts the number of typelib resources |
| 216 |
* @param[in] fp File to check |
| 217 |
* @return number of found typelib resource |
| 218 |
*/ |
| 219 |
int32_t genidl_pe_typelib_resource_count (FILE *fp); |
| 220 |
|
| 221 |
typedef struct sImgResourceDataEntry { |
| 222 |
uint32_t OffsetToData; |
| 223 |
uint32_t Size; |
| 224 |
uint32_t CodePage; |
| 225 |
uint32_t Reserved; |
| 226 |
} sImgResourceDataEntry; |
| 227 |
|
| 228 |
#ifdef __GNUC__ |
| 229 |
__extension__ |
| 230 |
#endif |
| 231 |
typedef struct sImgResourceDirectoryEntry { |
| 232 |
#ifdef __GNUC__ |
| 233 |
__extension__ |
| 234 |
#endif |
| 235 |
union { |
| 236 |
#ifdef __GNUC__ |
| 237 |
__extension__ |
| 238 |
#endif |
| 239 |
struct { |
| 240 |
uint32_t NameOffset:31; |
| 241 |
uint32_t NameIsString:1; |
| 242 |
}; |
| 243 |
uint32_t Name; |
| 244 |
uint16_t Id; |
| 245 |
}; |
| 246 |
#ifdef __GNUC__ |
| 247 |
__extension__ |
| 248 |
#endif |
| 249 |
union { |
| 250 |
uint32_t OffsetToData; |
| 251 |
#ifdef __GNUC__ |
| 252 |
__extension__ |
| 253 |
#endif |
| 254 |
struct { |
| 255 |
uint32_t OffsetToDirectory:31; |
| 256 |
uint32_t DataIsDirectory:1; |
| 257 |
}; |
| 258 |
}; |
| 259 |
} sImgResourceDirectoryEntry; |
| 260 |
|
| 261 |
typedef struct sImgResourceDirStringWC { |
| 262 |
uint16_t Length; |
| 263 |
uint16_t NameString[1]; |
| 264 |
} sImgResourceDirStringWC; |
| 265 |
|
| 266 |
typedef struct sImgResourceDirectory { |
| 267 |
uint32_t Characteristics; |
| 268 |
uint32_t TimeDateStamp; |
| 269 |
uint16_t MajorVersion; |
| 270 |
uint16_t MinorVersion; |
| 271 |
uint16_t NumberOfNamedEntries; |
| 272 |
uint16_t NumberOfIdEntries; |
| 273 |
} sImgResourceDirectory; |
| 274 |
|
| 275 |
#define GENIDL_IMG_RESDATA_IS_DIR 0x80000000 |
| 276 |
#define GENIDL_IMG_RESNAME_IS_STR 0x80000000 |
| 277 |
|
| 278 |
#pragma pack(pop) |
| 279 |
|
| 280 |
#endif |