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 enum class MACHINE_TYPES {
41 UNKNOWN = 0x0,
42 ALPHA = 0x184,
43 ALPHA64 = 0x284,
44 AM33 = 0x1D3,
45 AMD64 = 0x8664,
46 ARM = 0x1C0,
47 ARMNT = 0x1C4,
48 ARM64 = 0xAA64,
49 EBC = 0xEBC,
50 I386 = 0x14C,
51 IA64 = 0x200,
52 LOONGARCH32 = 0x6232,
53 LOONGARCH64 = 0x6264,
54 M32R = 0x9041,
55 MIPS16 = 0x266,
56 MIPSFPU = 0x366,
57 MIPSFPU16 = 0x466,
58 POWERPC = 0x1F0,
59 POWERPCFP = 0x1F1,
60 POWERPCBE = 0x1F2,
61 R4000 = 0x166,
62 RISCV32 = 0x5032,
63 RISCV64 = 0x5064,
64 RISCV128 = 0x5128,
65 SH3 = 0x1A2,
66 SH3DSP = 0x1A3,
67 SH4 = 0x1A6,
68 SH5 = 0x1A8,
69 THUMB = 0x1C2,
70 WCEMIPSV2 = 0x169,
71 ARM64EC = 0xa641,
72 ARM64X = 0xa64e,
73 CHPE_X86 = 0x3a64,
74 };
75
76 static bool is_known_machine(uint16_t machine);
77
78 static bool is_arm(MACHINE_TYPES ty) {
79 switch (ty) {
80 default: return false;
83 case MACHINE_TYPES::ARMNT: return true;
84 }
85 return false;
86 }
87
88 static bool is_riscv(MACHINE_TYPES ty) {
89 switch (ty) {
90 default: return false;
93 case MACHINE_TYPES::RISCV128: return true;
94 }
95 return false;
96 }
97
98 static bool is_loonarch(MACHINE_TYPES ty) {
99 switch (ty) {
100 default: return false;
102 case MACHINE_TYPES::LOONGARCH64: return true;
103 }
104 return false;
105 }
106
107
108 static bool is_arm64(MACHINE_TYPES ty) {
109 return ty == MACHINE_TYPES::ARM64;
110 }
111
112 static bool is_thumb(MACHINE_TYPES ty) {
113 return ty == MACHINE_TYPES::THUMB;
114 }
115
116 static bool x86(MACHINE_TYPES ty) {
117 return ty == MACHINE_TYPES::I386;
118 }
119
120 static bool x86_64(MACHINE_TYPES ty) {
121 return ty == MACHINE_TYPES::AMD64;
122 }
123
124 static bool is_mips(MACHINE_TYPES ty) {
125 switch (ty) {
126 default: return false;
131 case MACHINE_TYPES::WCEMIPSV2: return true;
132 }
133 return false;
134 }
135
136 static bool is_ppc(MACHINE_TYPES ty) {
137 switch (ty) {
138 default: return false;
141 case MACHINE_TYPES::POWERPCBE: return true;
142 }
143 return false;
144 }
145
146 enum class CHARACTERISTICS {
147 NONE = 0x0000,
148 RELOCS_STRIPPED = 0x0001,
151 EXECUTABLE_IMAGE = 0x0002,
153 LINE_NUMS_STRIPPED = 0x0004,
155 LOCAL_SYMS_STRIPPED =
156 0x0008,
158 AGGRESSIVE_WS_TRIM = 0x0010,
160 LARGE_ADDRESS_AWARE = 0x0020,
161 BYTES_REVERSED_LO = 0x0080,
163 NEED_32BIT_MACHINE =
164 0x0100,
165 DEBUG_STRIPPED = 0x0200,
166 REMOVABLE_RUN_FROM_SWAP = 0x0400,
168 NET_RUN_FROM_SWAP = 0x0800,
170 SYSTEM = 0x1000,
171 DLL = 0x2000,
172 UP_SYSTEM_ONLY =
173 0x4000,
174 BYTES_REVERSED_HI = 0x8000,
176 };
177 static Header create(PE_TYPE type);
178
179 Header(const details::pe_header& header);
180 ~Header() override = default;
181
182 Header& operator=(const Header&) = default;
183 Header(const Header&) = default;
184
186 const signature_t& signature() const {
187 return signature_;
188 }
189
192 return machine_;
193 }
194
196 uint16_t numberof_sections() const {
197 return nb_sections_;
198 }
199
202 uint32_t time_date_stamp() const {
203 return timedatestamp_;
204 }
205
210 uint32_t pointerto_symbol_table() const {
211 return pointerto_symtab_;
212 }
213
219 uint32_t numberof_symbols() const {
220 return nb_symbols_;
221 }
222
232 uint16_t sizeof_optional_header() const {
233 return sizeof_opt_header_;
234 }
235
237 uint32_t characteristics() const {
238 return characteristics_;
239 }
240
243 return (characteristics() & static_cast<uint32_t>(c)) > 0;
244 }
245
247 std::vector<CHARACTERISTICS> characteristics_list() const;
248
250 machine_ = type;
251 }
252
253 void numberof_sections(uint16_t nb) {
254 nb_sections_ = nb;
255 }
256
257 void time_date_stamp(uint32_t timestamp) {
258 timedatestamp_ = timestamp;
259 }
260
261 void pointerto_symbol_table(uint32_t ptr) {
262 pointerto_symtab_ = ptr;
263 }
264
265 void numberof_symbols(uint32_t nb) {
266 nb_symbols_ = nb;
267 }
268
269 void sizeof_optional_header(uint16_t size) {
270 sizeof_opt_header_ = size;
271 }
272
274 characteristics_ = characteristics;
275 }
276
277 void signature(const signature_t& sig) {
278 signature_ = sig;
279 }
280
282 characteristics_ |= static_cast<uint32_t>(c);
283 }
284
286 characteristics_ &= ~static_cast<uint32_t>(c);
287 }
288
289 void accept(Visitor& visitor) const override;
290
291 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Header& entry);
292
294 LIEF_LOCAL Header() = default;
295
296 private:
297 signature_t signature_;
299 uint16_t nb_sections_ = 0;
300 uint32_t timedatestamp_ = 0;
301 uint32_t pointerto_symtab_ = 0;
302 uint32_t nb_symbols_ = 0;
303 uint16_t sizeof_opt_header_ = 0;
304 uint32_t characteristics_ = 0;
305};
306
309}
310}
311
313#endif
CHARACTERISTICS
Definition PE/Header.hpp:146
void accept(Visitor &visitor) const override
static bool is_ppc(MACHINE_TYPES ty)
Definition PE/Header.hpp:136
static bool x86_64(MACHINE_TYPES ty)
Definition PE/Header.hpp:120
uint32_t characteristics() const
Characteristics of the binary like whether it is a DLL or an executable.
Definition PE/Header.hpp:237
MACHINE_TYPES machine() const
The targeted machine architecture like ARM, x86, AMD64, ...
Definition PE/Header.hpp:191
Header & operator=(const Header &)=default
uint16_t numberof_sections() const
The number of sections in the binary.
Definition PE/Header.hpp:196
void remove_characteristic(CHARACTERISTICS c)
Definition PE/Header.hpp:285
static bool is_riscv(MACHINE_TYPES ty)
Definition PE/Header.hpp:88
const signature_t & signature() const
Signature (or magic byte) of the header. It must be: PE\0\0.
Definition PE/Header.hpp:186
void signature(const signature_t &sig)
Definition PE/Header.hpp:277
uint16_t sizeof_optional_header() const
Size of the OptionalHeader AND the data directories which follows this header.
Definition PE/Header.hpp:232
bool has_characteristic(CHARACTERISTICS c) const
Check if the given CHARACTERISTICS is present.
Definition PE/Header.hpp:242
~Header() override=default
std::vector< CHARACTERISTICS > characteristics_list() const
The list of the CHARACTERISTICS.
void numberof_symbols(uint32_t nb)
Definition PE/Header.hpp:265
static bool is_thumb(MACHINE_TYPES ty)
Definition PE/Header.hpp:112
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:219
void pointerto_symbol_table(uint32_t ptr)
Definition PE/Header.hpp:261
static bool is_arm(MACHINE_TYPES ty)
Definition PE/Header.hpp:78
void machine(MACHINE_TYPES type)
Definition PE/Header.hpp:249
static bool is_known_machine(uint16_t machine)
void add_characteristic(CHARACTERISTICS c)
Definition PE/Header.hpp:281
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:116
void characteristics(uint32_t characteristics)
Definition PE/Header.hpp:273
static bool is_arm64(MACHINE_TYPES ty)
Definition PE/Header.hpp:108
uint32_t pointerto_symbol_table() const
The offset of the COFF symbol table.
Definition PE/Header.hpp:210
void numberof_sections(uint16_t nb)
Definition PE/Header.hpp:253
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:202
static bool is_loonarch(MACHINE_TYPES ty)
Definition PE/Header.hpp:98
static bool is_mips(MACHINE_TYPES ty)
Definition PE/Header.hpp:124
void sizeof_optional_header(uint16_t size)
Definition PE/Header.hpp:269
Header(const Header &)=default
void time_date_stamp(uint32_t timestamp)
Definition PE/Header.hpp:257
MACHINE_TYPES
Definition PE/Header.hpp:40
@ RISCV128
Definition PE/Header.hpp:64
@ ARMNT
Definition PE/Header.hpp:47
@ LOONGARCH32
Definition PE/Header.hpp:52
@ POWERPCFP
Definition PE/Header.hpp:59
@ LOONGARCH64
Definition PE/Header.hpp:53
@ ARM
Definition PE/Header.hpp:46
@ MIPSFPU
Definition PE/Header.hpp:56
@ R4000
Definition PE/Header.hpp:61
@ UNKNOWN
Definition PE/Header.hpp:41
@ WCEMIPSV2
Definition PE/Header.hpp:70
@ AMD64
Definition PE/Header.hpp:45
@ MIPS16
Definition PE/Header.hpp:55
@ MIPSFPU16
Definition PE/Header.hpp:57
@ RISCV32
Definition PE/Header.hpp:62
@ RISCV64
Definition PE/Header.hpp:63
@ POWERPCBE
Definition PE/Header.hpp:60
@ ARM64
Definition PE/Header.hpp:48
@ I386
Definition PE/Header.hpp:50
@ POWERPC
Definition PE/Header.hpp:58
@ THUMB
Definition PE/Header.hpp:69
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:40
#define LIEF_API
Definition visibility.h:43
#define LIEF_LOCAL
Definition visibility.h:44