| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
#pragma once |
| 3 |
|
| 4 |
#include "sparse-endian.h" |
| 5 |
#include "uki.h" |
| 6 |
|
| 7 |
#define IMAGE_DATA_DIRECTORY_INDEX_CERTIFICATION_TABLE 4U |
| 8 |
|
| 9 |
/* When naming things we try to stay close to the official Windows APIs as per: |
| 10 |
* → https://learn.microsoft.com/en-us/windows/win32/debug/pe-format */ |
| 11 |
|
| 12 |
typedef struct _packed_ _IMAGE_DOS_HEADER { |
| 13 |
le16_t e_magic; |
| 14 |
le16_t e_cblp; |
| 15 |
le16_t e_cp; |
| 16 |
le16_t e_crlc; |
| 17 |
le16_t e_cparhdr; |
| 18 |
le16_t e_minalloc; |
| 19 |
le16_t e_maxalloc; |
| 20 |
le16_t e_ss; |
| 21 |
le16_t e_sp; |
| 22 |
le16_t e_csum; |
| 23 |
le16_t e_ip; |
| 24 |
le16_t e_cs; |
| 25 |
le16_t e_lfarlc; |
| 26 |
le16_t e_ovno; |
| 27 |
le16_t e_res[4]; |
| 28 |
le16_t e_oemid; |
| 29 |
le16_t e_oeminfo; |
| 30 |
le16_t e_res2[10]; |
| 31 |
le32_t e_lfanew; |
| 32 |
} IMAGE_DOS_HEADER; |
| 33 |
|
| 34 |
typedef struct _packed_ _IMAGE_FILE_HEADER { |
| 35 |
le16_t Machine; |
| 36 |
le16_t NumberOfSections; |
| 37 |
le32_t TimeDateStamp; |
| 38 |
le32_t PointerToSymbolTable; |
| 39 |
le32_t NumberOfSymbols; |
| 40 |
le16_t SizeOfOptionalHeader; |
| 41 |
le16_t Characteristics; |
| 42 |
} IMAGE_FILE_HEADER; |
| 43 |
|
| 44 |
typedef struct _packed_ _IMAGE_DATA_DIRECTORY { |
| 45 |
le32_t VirtualAddress; |
| 46 |
le32_t Size; |
| 47 |
} IMAGE_DATA_DIRECTORY; |
| 48 |
|
| 49 |
typedef struct _packed_ _IMAGE_OPTIONAL_HEADER { |
| 50 |
/* Standard fields */ |
| 51 |
le16_t Magic; |
| 52 |
uint8_t MajorLinkerVersion; |
| 53 |
uint8_t MinorLinkerVersion; |
| 54 |
le32_t SizeOfCode; |
| 55 |
le32_t SizeOfInitializedData; |
| 56 |
le32_t SizeOfUninitializedData; |
| 57 |
le32_t AddressOfEntryPoint; |
| 58 |
le32_t BaseOfCode; |
| 59 |
|
| 60 |
/* Here the PE32 and PE32+ headers differ: PE32+ has one 64bit field, PE32+ has two 32bit fields */ |
| 61 |
union { |
| 62 |
struct { |
| 63 |
le32_t BaseOfData; |
| 64 |
le32_t pe32_ImageBase; |
| 65 |
}; |
| 66 |
le64_t pe32plus_ImageBase; |
| 67 |
}; |
| 68 |
|
| 69 |
/* Additional fields */ |
| 70 |
le32_t SectionAlignment; |
| 71 |
le32_t FileAlignment; |
| 72 |
le16_t MajorOperatingSystemVersion; |
| 73 |
le16_t MinorOperatingSystemVersion; |
| 74 |
le16_t MajorImageVersion; |
| 75 |
le16_t MinorImageVersion; |
| 76 |
le16_t MajorSubsystemVersion; |
| 77 |
le16_t MinorSubsystemVersion; |
| 78 |
le32_t Win32VersionValue; |
| 79 |
le32_t SizeOfImage; |
| 80 |
le32_t SizeOfHeaders; |
| 81 |
le32_t CheckSum; |
| 82 |
le16_t Subsystem; |
| 83 |
le16_t DllCharacteristics; |
| 84 |
|
| 85 |
/* Here similar: on PE32+ some fields are 64bit that are 32bit on PE32. */ |
| 86 |
union { |
| 87 |
struct { |
| 88 |
le32_t pe32_SizeOfStackReserve; |
| 89 |
le32_t pe32_SizeOfStackCommit; |
| 90 |
le32_t pe32_SizeOfHeapReserve; |
| 91 |
le32_t pe32_SizeOfHeapCommit; |
| 92 |
le32_t pe32_LoaderFlags; |
| 93 |
le32_t pe32_NumberOfRvaAndSizes; |
| 94 |
IMAGE_DATA_DIRECTORY pe32_DataDirectory[]; |
| 95 |
}; |
| 96 |
struct { |
| 97 |
le64_t pe32plus_SizeOfStackReserve; |
| 98 |
le64_t pe32plus_SizeOfStackCommit; |
| 99 |
le64_t pe32plus_SizeOfHeapReserve; |
| 100 |
le64_t pe32plus_SizeOfHeapCommit; |
| 101 |
le32_t pe32plus_LoaderFlags; |
| 102 |
le32_t pe32plus_NumberOfRvaAndSizes; |
| 103 |
IMAGE_DATA_DIRECTORY pe32plus_DataDirectory[]; |
| 104 |
}; |
| 105 |
}; |
| 106 |
} IMAGE_OPTIONAL_HEADER; |
| 107 |
|
| 108 |
typedef struct _packed_ PeHeader { |
| 109 |
le32_t signature; |
| 110 |
IMAGE_FILE_HEADER pe; |
| 111 |
IMAGE_OPTIONAL_HEADER optional; |
| 112 |
} PeHeader; |
| 113 |
|
| 114 |
typedef struct _packed_ _IMAGE_SECTION_HEADER { |
| 115 |
uint8_t Name[8]; |
| 116 |
le32_t VirtualSize; |
| 117 |
le32_t VirtualAddress; |
| 118 |
le32_t SizeOfRawData; |
| 119 |
le32_t PointerToRawData; |
| 120 |
le32_t PointerToRelocations; |
| 121 |
le32_t PointerToLinenumbers; |
| 122 |
le16_t NumberOfRelocations; |
| 123 |
le16_t NumberOfLinenumbers; |
| 124 |
le32_t Characteristics; |
| 125 |
} IMAGE_SECTION_HEADER; |
| 126 |
|
| 127 |
#define IMAGE_SUBSYSTEM_EFI_APPLICATION 10 |
| 128 |
|
| 129 |
bool pe_header_is_64bit(const PeHeader *h); |
| 130 |
|
| 131 |
#define PE_HEADER_OPTIONAL_FIELD(h, field) \ |
| 132 |
(pe_header_is_64bit(h) ? (h)->optional.pe32plus_##field : (h)->optional.pe32_##field) |
| 133 |
|
| 134 |
#define PE_HEADER_OPTIONAL_FIELD_OFFSET(h, field) \ |
| 135 |
(pe_header_is_64bit(h) ? offsetof(PeHeader, optional.pe32plus_##field) : offsetof(PeHeader, optional.pe32_##field)) |
| 136 |
|
| 137 |
const IMAGE_DATA_DIRECTORY* pe_header_get_data_directory(const PeHeader *h, size_t i); |
| 138 |
const IMAGE_SECTION_HEADER* pe_header_find_section(const PeHeader *pe_header, const IMAGE_SECTION_HEADER *sections, const char *name); |
| 139 |
const IMAGE_SECTION_HEADER* pe_section_table_find(const IMAGE_SECTION_HEADER *sections, size_t n_sections, const char *name); |
| 140 |
|
| 141 |
int pe_load_headers(int fd, IMAGE_DOS_HEADER **ret_dos_header, PeHeader **ret_pe_header); |
| 142 |
|
| 143 |
int pe_load_sections(int fd, const IMAGE_DOS_HEADER *dos_header, const PeHeader *pe_header, IMAGE_SECTION_HEADER **ret_sections); |
| 144 |
int pe_read_section_data(int fd, const IMAGE_SECTION_HEADER *section, size_t max_size, void **ret, size_t *ret_size); |
| 145 |
int pe_read_section_data_by_name(int fd, const PeHeader *pe_header, const IMAGE_SECTION_HEADER *sections, const char *name, size_t max_size, void **ret, size_t *ret_size); |
| 146 |
|
| 147 |
bool pe_is_uki(const PeHeader *pe_header, const IMAGE_SECTION_HEADER *sections); |
| 148 |
bool pe_is_addon(const PeHeader *pe_header, const IMAGE_SECTION_HEADER *sections); |
| 149 |
|
| 150 |
bool pe_is_native(const PeHeader *pe_header); |
| 151 |
int pe_is_native_fd(int fd); |
| 152 |
|
| 153 |
#if HAVE_OPENSSL |
| 154 |
int pe_hash(int fd, const EVP_MD *md, void **ret_hash, size_t *ret_hash_size); |
| 155 |
|
| 156 |
/* This does not depend on OpenSSL, but is currently only used by sbsign which requires OpenSSL. */ |
| 157 |
int pe_checksum(int fd, uint32_t *ret); |
| 158 |
|
| 159 |
int uki_hash(int fd, const EVP_MD *md, void *ret_hashes[static _UNIFIED_SECTION_MAX], size_t *ret_hash_size); |
| 160 |
#endif |