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