LIEF: Library to Instrument Executable Formats Version 0.17.0
Loading...
Searching...
No Matches
PE/Header.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2025 R. Thomas
2 * Copyright 2017 - 2025 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:
81 return false;
85 return true;
86 }
87 return false;
88 }
89
90 static bool is_riscv(MACHINE_TYPES ty) {
91 switch (ty) {
92 default:
93 return false;
97 return true;
98 }
99 return false;
100 }
101
102 static bool is_loonarch(MACHINE_TYPES ty) {
103 switch (ty) {
104 default:
105 return false;
108 return true;
109 }
110 return false;
111 }
112
113
114 static bool is_arm64(MACHINE_TYPES ty) {
115 return ty == MACHINE_TYPES::ARM64;
116 }
117
118 static bool is_thumb(MACHINE_TYPES ty) {
119 return ty == MACHINE_TYPES::THUMB;
120 }
121
122 static bool x86(MACHINE_TYPES ty) {
123 return ty == MACHINE_TYPES::I386;
124 }
125
126 static bool x86_64(MACHINE_TYPES ty) {
127 return ty == MACHINE_TYPES::AMD64;
128 }
129
130 static bool is_mips(MACHINE_TYPES ty) {
131 switch (ty) {
132 default:
133 return false;
139 return true;
140 }
141 return false;
142 }
143
144 static bool is_ppc(MACHINE_TYPES ty) {
145 switch (ty) {
146 default:
147 return false;
151 return true;
152 }
153 return false;
154 }
155
174 static Header create(PE_TYPE type);
175
176 Header(const details::pe_header& header);
177 ~Header() override = default;
178
179 Header& operator=(const Header&) = default;
180 Header(const Header&) = default;
181
183 const signature_t& signature() const {
184 return signature_;
185 }
186
189 return machine_;
190 }
191
193 uint16_t numberof_sections() const {
194 return nb_sections_;
195 }
196
199 uint32_t time_date_stamp() const {
200 return timedatestamp_;
201 }
202
206 uint32_t pointerto_symbol_table() const {
207 return pointerto_symtab_;
208 }
209
214 uint32_t numberof_symbols() const {
215 return nb_symbols_;
216 }
217
225 uint16_t sizeof_optional_header() const {
226 return sizeof_opt_header_;
227 }
228
230 uint32_t characteristics() const {
231 return characteristics_;
232 }
233
236 return (characteristics() & static_cast<uint32_t>(c)) > 0;
237 }
238
240 std::vector<CHARACTERISTICS> characteristics_list() const;
241
243 machine_ = type;
244 }
245
246 void numberof_sections(uint16_t nb) {
247 nb_sections_ = nb;
248 }
249
250 void time_date_stamp(uint32_t timestamp) {
251 timedatestamp_ = timestamp;
252 }
253
254 void pointerto_symbol_table(uint32_t ptr) {
255 pointerto_symtab_ = ptr;
256 }
257
258 void numberof_symbols(uint32_t nb) {
259 nb_symbols_ = nb;
260 }
261
262 void sizeof_optional_header(uint16_t size) {
263 sizeof_opt_header_ = size;
264 }
265
267 characteristics_ = characteristics;
268 }
269
270 void signature(const signature_t& sig) {
271 signature_ = sig;
272 }
273
275 characteristics_ |= static_cast<uint32_t>(c);
276 }
277
279 characteristics_ &= ~static_cast<uint32_t>(c);
280 }
281
282 void accept(Visitor& visitor) const override;
283
284 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Header& entry);
285
287 LIEF_LOCAL Header() = default;
288
289 private:
290 signature_t signature_;
292 uint16_t nb_sections_ = 0;
293 uint32_t timedatestamp_ = 0;
294 uint32_t pointerto_symtab_ = 0;
295 uint32_t nb_symbols_ = 0;
296 uint16_t sizeof_opt_header_ = 0;
297 uint32_t characteristics_ = 0;
298};
299
302}
303}
304
306#endif
CHARACTERISTICS
Definition PE/Header.hpp:156
@ REMOVABLE_RUN_FROM_SWAP
Definition PE/Header.hpp:167
@ RELOCS_STRIPPED
Definition PE/Header.hpp:158
@ UP_SYSTEM_ONLY
Definition PE/Header.hpp:171
@ NEED_32BIT_MACHINE
Definition PE/Header.hpp:165
@ BYTES_REVERSED_HI
Definition PE/Header.hpp:172
@ NET_RUN_FROM_SWAP
Definition PE/Header.hpp:168
@ DLL
Definition PE/Header.hpp:170
@ LARGE_ADDRESS_AWARE
Definition PE/Header.hpp:163
@ BYTES_REVERSED_LO
Definition PE/Header.hpp:164
@ DEBUG_STRIPPED
Definition PE/Header.hpp:166
@ LOCAL_SYMS_STRIPPED
Definition PE/Header.hpp:161
@ NONE
Definition PE/Header.hpp:157
@ LINE_NUMS_STRIPPED
Definition PE/Header.hpp:160
@ AGGRESSIVE_WS_TRIM
Definition PE/Header.hpp:162
@ EXECUTABLE_IMAGE
Definition PE/Header.hpp:159
@ SYSTEM
Definition PE/Header.hpp:169
void accept(Visitor &visitor) const override
static bool is_ppc(MACHINE_TYPES ty)
Definition PE/Header.hpp:144
static bool x86_64(MACHINE_TYPES ty)
Definition PE/Header.hpp:126
uint32_t characteristics() const
Characteristics of the binary like whether it is a DLL or an executable.
Definition PE/Header.hpp:230
MACHINE_TYPES machine() const
The targeted machine architecture like ARM, x86, AMD64, ...
Definition PE/Header.hpp:188
Header & operator=(const Header &)=default
uint16_t numberof_sections() const
The number of sections in the binary.
Definition PE/Header.hpp:193
void remove_characteristic(CHARACTERISTICS c)
Definition PE/Header.hpp:278
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:183
void signature(const signature_t &sig)
Definition PE/Header.hpp:270
uint16_t sizeof_optional_header() const
Size of the OptionalHeader AND the data directories which follows this header.
Definition PE/Header.hpp:225
bool has_characteristic(CHARACTERISTICS c) const
Check if the given CHARACTERISTICS is present.
Definition PE/Header.hpp:235
~Header() override=default
std::vector< CHARACTERISTICS > characteristics_list() const
The list of the CHARACTERISTICS.
void numberof_symbols(uint32_t nb)
Definition PE/Header.hpp:258
static bool is_thumb(MACHINE_TYPES ty)
Definition PE/Header.hpp:118
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:214
void pointerto_symbol_table(uint32_t ptr)
Definition PE/Header.hpp:254
static bool is_arm(MACHINE_TYPES ty)
Definition PE/Header.hpp:78
void machine(MACHINE_TYPES type)
Definition PE/Header.hpp:242
static bool is_known_machine(uint16_t machine)
void add_characteristic(CHARACTERISTICS c)
Definition PE/Header.hpp:274
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:122
void characteristics(uint32_t characteristics)
Definition PE/Header.hpp:266
static bool is_arm64(MACHINE_TYPES ty)
Definition PE/Header.hpp:114
uint32_t pointerto_symbol_table() const
The offset of the COFF symbol table.
Definition PE/Header.hpp:206
void numberof_sections(uint16_t nb)
Definition PE/Header.hpp:246
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:199
static bool is_loonarch(MACHINE_TYPES ty)
Definition PE/Header.hpp:102
static bool is_mips(MACHINE_TYPES ty)
Definition PE/Header.hpp:130
void sizeof_optional_header(uint16_t size)
Definition PE/Header.hpp:262
Header(const Header &)=default
void time_date_stamp(uint32_t timestamp)
Definition PE/Header.hpp:250
MACHINE_TYPES
Definition PE/Header.hpp:40
@ ALPHA
Definition PE/Header.hpp:42
@ RISCV128
Definition PE/Header.hpp:64
@ M32R
Definition PE/Header.hpp:54
@ ARM64EC
Definition PE/Header.hpp:71
@ SH4
Definition PE/Header.hpp:67
@ ARMNT
Definition PE/Header.hpp:47
@ ALPHA64
Definition PE/Header.hpp:43
@ LOONGARCH32
Definition PE/Header.hpp:52
@ AM33
Definition PE/Header.hpp:44
@ POWERPCFP
Definition PE/Header.hpp:59
@ LOONGARCH64
Definition PE/Header.hpp:53
@ ARM
Definition PE/Header.hpp:46
@ IA64
Definition PE/Header.hpp:51
@ MIPSFPU
Definition PE/Header.hpp:56
@ EBC
Definition PE/Header.hpp:49
@ SH5
Definition PE/Header.hpp:68
@ 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
@ CHPE_X86
Definition PE/Header.hpp:73
@ RISCV64
Definition PE/Header.hpp:63
@ POWERPCBE
Definition PE/Header.hpp:60
@ ARM64
Definition PE/Header.hpp:48
@ SH3DSP
Definition PE/Header.hpp:66
@ SH3
Definition PE/Header.hpp:65
@ I386
Definition PE/Header.hpp:50
@ POWERPC
Definition PE/Header.hpp:58
@ THUMB
Definition PE/Header.hpp:69
@ ARM64X
Definition PE/Header.hpp:72
Definition Visitor.hpp:210
#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:41
#define LIEF_LOCAL
Definition visibility.h:42