LIEF: Library to Instrument Executable Formats Version 1.0.0
Loading...
Searching...
No Matches
PE/Header.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2026 R. Thomas
2 * Copyright 2017 - 2026 Quarkslab
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#ifndef LIEF_PE_HEADER_H
17#define LIEF_PE_HEADER_H
18#include <array>
19#include <vector>
20#include <ostream>
21#include <cstdint>
22
23#include "LIEF/Object.hpp"
24#include "LIEF/visibility.h"
25#include "LIEF/enums.hpp"
26#include "LIEF/PE/enums.hpp"
27
28namespace LIEF {
29namespace PE {
30
31namespace details {
32struct pe_header;
33}
34
36class LIEF_API Header : public Object {
37 public:
38 using signature_t = std::array<uint8_t, /* PE Magic */ 4>;
39
40 // clang-format off
41 enum class MACHINE_TYPES {
42 UNKNOWN = 0x0,
43 ALPHA = 0x184 ,
44 ALPHA64 = 0x284 ,
45 AM33 = 0x1D3,
46 AMD64 = 0x8664,
47 ARM = 0x1C0,
48 ARMNT = 0x1C4,
49 ARM64 = 0xAA64,
50 EBC = 0xEBC,
51 I386 = 0x14C,
52 IA64 = 0x200,
53 LOONGARCH32 = 0x6232,
54 LOONGARCH64 = 0x6264,
55 M32R = 0x9041,
56 MIPS16 = 0x266,
57 MIPSFPU = 0x366,
58 MIPSFPU16 = 0x466,
59 POWERPC = 0x1F0,
60 POWERPCFP = 0x1F1,
61 POWERPCBE = 0x1F2,
62 R4000 = 0x166,
63 RISCV32 = 0x5032,
64 RISCV64 = 0x5064,
65 RISCV128 = 0x5128,
66 SH3 = 0x1A2,
67 SH3DSP = 0x1A3,
68 SH4 = 0x1A6,
69 SH5 = 0x1A8,
70 THUMB = 0x1C2,
71 WCEMIPSV2 = 0x169,
72 ARM64EC = 0xa641,
73 ARM64X = 0xa64e,
74 CHPE_X86 = 0x3a64,
75 };
76 // clang-format on
77
78 static bool is_known_machine(uint16_t machine);
79
80 static bool is_arm(MACHINE_TYPES ty) {
81 switch (ty) {
82 default: return false;
85 case MACHINE_TYPES::ARMNT: return true;
86 }
87 return false;
88 }
89
90 static bool is_riscv(MACHINE_TYPES ty) {
91 switch (ty) {
92 default: return false;
95 case MACHINE_TYPES::RISCV128: return true;
96 }
97 return false;
98 }
99
100 static bool is_loonarch(MACHINE_TYPES ty) {
101 switch (ty) {
102 default: return false;
104 case MACHINE_TYPES::LOONGARCH64: return true;
105 }
106 return false;
107 }
108
109
110 static bool is_arm64(MACHINE_TYPES ty) {
111 return ty == MACHINE_TYPES::ARM64;
112 }
113
114 static bool is_thumb(MACHINE_TYPES ty) {
115 return ty == MACHINE_TYPES::THUMB;
116 }
117
118 static bool x86(MACHINE_TYPES ty) {
119 return ty == MACHINE_TYPES::I386;
120 }
121
122 static bool x86_64(MACHINE_TYPES ty) {
123 return ty == MACHINE_TYPES::AMD64;
124 }
125
126 static bool is_mips(MACHINE_TYPES ty) {
127 switch (ty) {
128 default: return false;
133 case MACHINE_TYPES::WCEMIPSV2: return true;
134 }
135 return false;
136 }
137
138 static bool is_ppc(MACHINE_TYPES ty) {
139 switch (ty) {
140 default: return false;
143 case MACHINE_TYPES::POWERPCBE: return true;
144 }
145 return false;
146 }
147
148 enum class CHARACTERISTICS {
149 NONE = 0x0000,
152 RELOCS_STRIPPED = 0x0001,
153
155 EXECUTABLE_IMAGE = 0x0002,
156
158 LINE_NUMS_STRIPPED = 0x0004,
159
162 LOCAL_SYMS_STRIPPED = 0x0008,
163
165 AGGRESSIVE_WS_TRIM = 0x0010,
166
168 LARGE_ADDRESS_AWARE = 0x0020,
169
172 BYTES_REVERSED_LO = 0x0080,
173
175 NEED_32BIT_MACHINE = 0x0100,
176
178 DEBUG_STRIPPED = 0x0200,
179
181 REMOVABLE_RUN_FROM_SWAP = 0x0400,
182
184 NET_RUN_FROM_SWAP = 0x0800,
185
187 SYSTEM = 0x1000,
188
190 DLL = 0x2000,
191
193 UP_SYSTEM_ONLY = 0x4000,
194
196 BYTES_REVERSED_HI = 0x8000,
197 };
198 static Header create(PE_TYPE type);
199
200 Header(const details::pe_header& header);
201 ~Header() override = default;
202
203 Header& operator=(const Header&) = default;
204 Header(const Header&) = default;
205
207 const signature_t& signature() const {
208 return signature_;
209 }
210
213 return machine_;
214 }
215
217 uint16_t numberof_sections() const {
218 return nb_sections_;
219 }
220
223 uint32_t time_date_stamp() const {
224 return timedatestamp_;
225 }
226
231 uint32_t pointerto_symbol_table() const {
232 return pointerto_symtab_;
233 }
234
240 uint32_t numberof_symbols() const {
241 return nb_symbols_;
242 }
243
253 uint16_t sizeof_optional_header() const {
254 return sizeof_opt_header_;
255 }
256
258 uint32_t characteristics() const {
259 return characteristics_;
260 }
261
264 return (characteristics() & static_cast<uint32_t>(c)) > 0;
265 }
266
268 std::vector<CHARACTERISTICS> characteristics_list() const;
269
271 machine_ = type;
272 }
273
274 void numberof_sections(uint16_t nb) {
275 nb_sections_ = nb;
276 }
277
278 void time_date_stamp(uint32_t timestamp) {
279 timedatestamp_ = timestamp;
280 }
281
282 void pointerto_symbol_table(uint32_t ptr) {
283 pointerto_symtab_ = ptr;
284 }
285
286 void numberof_symbols(uint32_t nb) {
287 nb_symbols_ = nb;
288 }
289
290 void sizeof_optional_header(uint16_t size) {
291 sizeof_opt_header_ = size;
292 }
293
295 characteristics_ = characteristics;
296 }
297
298 void signature(const signature_t& sig) {
299 signature_ = sig;
300 }
301
303 characteristics_ |= static_cast<uint32_t>(c);
304 }
305
307 characteristics_ &= ~static_cast<uint32_t>(c);
308 }
309
310 void accept(Visitor& visitor) const override;
311
312 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Header& entry);
313
315 LIEF_LOCAL Header() = default;
316
317 private:
318 signature_t signature_;
320 uint16_t nb_sections_ = 0;
321 uint32_t timedatestamp_ = 0;
322 uint32_t pointerto_symtab_ = 0;
323 uint32_t nb_symbols_ = 0;
324 uint16_t sizeof_opt_header_ = 0;
325 uint32_t characteristics_ = 0;
326};
327
330}
331}
332
334#endif
CHARACTERISTICS
Definition PE/Header.hpp:148
void accept(Visitor &visitor) const override
static bool is_ppc(MACHINE_TYPES ty)
Definition PE/Header.hpp:138
static bool x86_64(MACHINE_TYPES ty)
Definition PE/Header.hpp:122
uint32_t characteristics() const
Characteristics of the binary like whether it is a DLL or an executable.
Definition PE/Header.hpp:258
MACHINE_TYPES machine() const
The targeted machine architecture like ARM, x86, AMD64, ...
Definition PE/Header.hpp:212
Header & operator=(const Header &)=default
uint16_t numberof_sections() const
The number of sections in the binary.
Definition PE/Header.hpp:217
void remove_characteristic(CHARACTERISTICS c)
Definition PE/Header.hpp:306
static bool is_riscv(MACHINE_TYPES ty)
Definition PE/Header.hpp:90
const signature_t & signature() const
Signature (or magic byte) of the header. It must be: PE\0\0.
Definition PE/Header.hpp:207
void signature(const signature_t &sig)
Definition PE/Header.hpp:298
uint16_t sizeof_optional_header() const
Size of the OptionalHeader AND the data directories which follows this header.
Definition PE/Header.hpp:253
bool has_characteristic(CHARACTERISTICS c) const
Check if the given CHARACTERISTICS is present.
Definition PE/Header.hpp:263
~Header() override=default
std::vector< CHARACTERISTICS > characteristics_list() const
The list of the CHARACTERISTICS.
void numberof_symbols(uint32_t nb)
Definition PE/Header.hpp:286
static bool is_thumb(MACHINE_TYPES ty)
Definition PE/Header.hpp:114
uint32_t numberof_symbols() const
The number of entries in the symbol table. This data can be used to locate the string table which imm...
Definition PE/Header.hpp:240
void pointerto_symbol_table(uint32_t ptr)
Definition PE/Header.hpp:282
static bool is_arm(MACHINE_TYPES ty)
Definition PE/Header.hpp:80
void machine(MACHINE_TYPES type)
Definition PE/Header.hpp:270
static bool is_known_machine(uint16_t machine)
void add_characteristic(CHARACTERISTICS c)
Definition PE/Header.hpp:302
friend std::ostream & operator<<(std::ostream &os, const Header &entry)
std::array< uint8_t, 4 > signature_t
Definition PE/Header.hpp:38
static bool x86(MACHINE_TYPES ty)
Definition PE/Header.hpp:118
void characteristics(uint32_t characteristics)
Definition PE/Header.hpp:294
static bool is_arm64(MACHINE_TYPES ty)
Definition PE/Header.hpp:110
uint32_t pointerto_symbol_table() const
The offset of the COFF symbol table.
Definition PE/Header.hpp:231
void numberof_sections(uint16_t nb)
Definition PE/Header.hpp:274
static Header create(PE_TYPE type)
Header(const details::pe_header &header)
uint32_t time_date_stamp() const
The low 32 bits of the number of seconds since January 1, 1970. It indicates when the file was create...
Definition PE/Header.hpp:223
static bool is_loonarch(MACHINE_TYPES ty)
Definition PE/Header.hpp:100
static bool is_mips(MACHINE_TYPES ty)
Definition PE/Header.hpp:126
void sizeof_optional_header(uint16_t size)
Definition PE/Header.hpp:290
Header(const Header &)=default
void time_date_stamp(uint32_t timestamp)
Definition PE/Header.hpp:278
MACHINE_TYPES
Definition PE/Header.hpp:41
@ RISCV128
Definition PE/Header.hpp:65
@ ARMNT
Definition PE/Header.hpp:48
@ LOONGARCH32
Definition PE/Header.hpp:53
@ POWERPCFP
Definition PE/Header.hpp:60
@ LOONGARCH64
Definition PE/Header.hpp:54
@ ARM
Definition PE/Header.hpp:47
@ MIPSFPU
Definition PE/Header.hpp:57
@ R4000
Definition PE/Header.hpp:62
@ UNKNOWN
Definition PE/Header.hpp:42
@ WCEMIPSV2
Definition PE/Header.hpp:71
@ AMD64
Definition PE/Header.hpp:46
@ MIPS16
Definition PE/Header.hpp:56
@ MIPSFPU16
Definition PE/Header.hpp:58
@ RISCV32
Definition PE/Header.hpp:63
@ RISCV64
Definition PE/Header.hpp:64
@ POWERPCBE
Definition PE/Header.hpp:61
@ ARM64
Definition PE/Header.hpp:49
@ I386
Definition PE/Header.hpp:51
@ POWERPC
Definition PE/Header.hpp:59
@ THUMB
Definition PE/Header.hpp:70
Definition Visitor.hpp:212
#define ENABLE_BITMASK_OPERATORS(X)
Definition enums.hpp:24
Definition DataDirectory.hpp:37
Namespace related to the LIEF's PE module.
Definition Abstract/Header.hpp:32
const char * to_string(CODE_PAGES e)
PE_TYPE
Definition PE/enums.hpp:22
LIEF namespace.
Definition Abstract/Binary.hpp:41
#define LIEF_API
Definition visibility.h:45
#define LIEF_LOCAL
Definition visibility.h:46