LIEF: Library to Instrument Executable Formats
Version 1.0.0
Toggle main menu visibility
Loading...
Searching...
No Matches
install
linux
x86_64
sdk
static
include
LIEF
PE
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
28
namespace
LIEF
{
29
namespace
PE
{
30
31
namespace
details
{
32
struct
pe_header;
33
}
34
36
class
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
;
83
case
MACHINE_TYPES::ARM
:
84
case
MACHINE_TYPES::THUMB
:
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
;
93
case
MACHINE_TYPES::RISCV32
:
94
case
MACHINE_TYPES::RISCV64
:
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
;
103
case
MACHINE_TYPES::LOONGARCH32
:
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
;
129
case
MACHINE_TYPES::MIPS16
:
130
case
MACHINE_TYPES::MIPSFPU
:
131
case
MACHINE_TYPES::MIPSFPU16
:
132
case
MACHINE_TYPES::R4000
:
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
;
141
case
MACHINE_TYPES::POWERPC
:
142
case
MACHINE_TYPES::POWERPCFP
:
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
212
MACHINE_TYPES
machine
()
const
{
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
263
bool
has_characteristic
(
CHARACTERISTICS
c)
const
{
264
return
(
characteristics
() &
static_cast<
uint32_t
>
(c)) > 0;
265
}
266
268
std::vector<CHARACTERISTICS>
characteristics_list
()
const
;
269
270
void
machine
(
MACHINE_TYPES
type) {
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
294
void
characteristics
(uint32_t
characteristics
) {
295
characteristics_ =
characteristics
;
296
}
297
298
void
signature
(
const
signature_t
& sig) {
299
signature_ = sig;
300
}
301
302
void
add_characteristic
(
CHARACTERISTICS
c) {
303
characteristics_ |=
static_cast<
uint32_t
>
(c);
304
}
305
306
void
remove_characteristic
(
CHARACTERISTICS
c) {
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_;
319
MACHINE_TYPES
machine_ =
MACHINE_TYPES::UNKNOWN
;
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
328
LIEF_API
const
char
*
to_string
(
Header::CHARACTERISTICS
c);
329
LIEF_API
const
char
*
to_string
(
Header::MACHINE_TYPES
c);
330
}
331
}
332
333
ENABLE_BITMASK_OPERATORS
(
LIEF::PE::Header::CHARACTERISTICS
);
334
#endif
Object.hpp
enums.hpp
LIEF::Object::Object
Object()
LIEF::PE::Header::CHARACTERISTICS
CHARACTERISTICS
Definition
PE/Header.hpp:148
LIEF::PE::Header::accept
void accept(Visitor &visitor) const override
LIEF::PE::Header::is_ppc
static bool is_ppc(MACHINE_TYPES ty)
Definition
PE/Header.hpp:138
LIEF::PE::Header::x86_64
static bool x86_64(MACHINE_TYPES ty)
Definition
PE/Header.hpp:122
LIEF::PE::Header::characteristics
uint32_t characteristics() const
Characteristics of the binary like whether it is a DLL or an executable.
Definition
PE/Header.hpp:258
LIEF::PE::Header::machine
MACHINE_TYPES machine() const
The targeted machine architecture like ARM, x86, AMD64, ...
Definition
PE/Header.hpp:212
LIEF::PE::Header::operator=
Header & operator=(const Header &)=default
LIEF::PE::Header::numberof_sections
uint16_t numberof_sections() const
The number of sections in the binary.
Definition
PE/Header.hpp:217
LIEF::PE::Header::remove_characteristic
void remove_characteristic(CHARACTERISTICS c)
Definition
PE/Header.hpp:306
LIEF::PE::Header::is_riscv
static bool is_riscv(MACHINE_TYPES ty)
Definition
PE/Header.hpp:90
LIEF::PE::Header::signature
const signature_t & signature() const
Signature (or magic byte) of the header. It must be: PE\0\0.
Definition
PE/Header.hpp:207
LIEF::PE::Header::signature
void signature(const signature_t &sig)
Definition
PE/Header.hpp:298
LIEF::PE::Header::sizeof_optional_header
uint16_t sizeof_optional_header() const
Size of the OptionalHeader AND the data directories which follows this header.
Definition
PE/Header.hpp:253
LIEF::PE::Header::has_characteristic
bool has_characteristic(CHARACTERISTICS c) const
Check if the given CHARACTERISTICS is present.
Definition
PE/Header.hpp:263
LIEF::PE::Header::~Header
~Header() override=default
LIEF::PE::Header::characteristics_list
std::vector< CHARACTERISTICS > characteristics_list() const
The list of the CHARACTERISTICS.
LIEF::PE::Header::numberof_symbols
void numberof_symbols(uint32_t nb)
Definition
PE/Header.hpp:286
LIEF::PE::Header::is_thumb
static bool is_thumb(MACHINE_TYPES ty)
Definition
PE/Header.hpp:114
LIEF::PE::Header::numberof_symbols
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
LIEF::PE::Header::pointerto_symbol_table
void pointerto_symbol_table(uint32_t ptr)
Definition
PE/Header.hpp:282
LIEF::PE::Header::is_arm
static bool is_arm(MACHINE_TYPES ty)
Definition
PE/Header.hpp:80
LIEF::PE::Header::machine
void machine(MACHINE_TYPES type)
Definition
PE/Header.hpp:270
LIEF::PE::Header::is_known_machine
static bool is_known_machine(uint16_t machine)
LIEF::PE::Header::add_characteristic
void add_characteristic(CHARACTERISTICS c)
Definition
PE/Header.hpp:302
LIEF::PE::Header::operator<<
friend std::ostream & operator<<(std::ostream &os, const Header &entry)
LIEF::PE::Header::signature_t
std::array< uint8_t, 4 > signature_t
Definition
PE/Header.hpp:38
LIEF::PE::Header::x86
static bool x86(MACHINE_TYPES ty)
Definition
PE/Header.hpp:118
LIEF::PE::Header::characteristics
void characteristics(uint32_t characteristics)
Definition
PE/Header.hpp:294
LIEF::PE::Header::is_arm64
static bool is_arm64(MACHINE_TYPES ty)
Definition
PE/Header.hpp:110
LIEF::PE::Header::pointerto_symbol_table
uint32_t pointerto_symbol_table() const
The offset of the COFF symbol table.
Definition
PE/Header.hpp:231
LIEF::PE::Header::numberof_sections
void numberof_sections(uint16_t nb)
Definition
PE/Header.hpp:274
LIEF::PE::Header::create
static Header create(PE_TYPE type)
LIEF::PE::Header::Header
Header(const details::pe_header &header)
LIEF::PE::Header::time_date_stamp
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
LIEF::PE::Header::is_loonarch
static bool is_loonarch(MACHINE_TYPES ty)
Definition
PE/Header.hpp:100
LIEF::PE::Header::is_mips
static bool is_mips(MACHINE_TYPES ty)
Definition
PE/Header.hpp:126
LIEF::PE::Header::sizeof_optional_header
void sizeof_optional_header(uint16_t size)
Definition
PE/Header.hpp:290
LIEF::PE::Header::Header
Header(const Header &)=default
LIEF::PE::Header::time_date_stamp
void time_date_stamp(uint32_t timestamp)
Definition
PE/Header.hpp:278
LIEF::PE::Header::MACHINE_TYPES
MACHINE_TYPES
Definition
PE/Header.hpp:41
LIEF::PE::Header::MACHINE_TYPES::RISCV128
@ RISCV128
Definition
PE/Header.hpp:65
LIEF::PE::Header::MACHINE_TYPES::ARMNT
@ ARMNT
Definition
PE/Header.hpp:48
LIEF::PE::Header::MACHINE_TYPES::LOONGARCH32
@ LOONGARCH32
Definition
PE/Header.hpp:53
LIEF::PE::Header::MACHINE_TYPES::POWERPCFP
@ POWERPCFP
Definition
PE/Header.hpp:60
LIEF::PE::Header::MACHINE_TYPES::LOONGARCH64
@ LOONGARCH64
Definition
PE/Header.hpp:54
LIEF::PE::Header::MACHINE_TYPES::ARM
@ ARM
Definition
PE/Header.hpp:47
LIEF::PE::Header::MACHINE_TYPES::MIPSFPU
@ MIPSFPU
Definition
PE/Header.hpp:57
LIEF::PE::Header::MACHINE_TYPES::R4000
@ R4000
Definition
PE/Header.hpp:62
LIEF::PE::Header::MACHINE_TYPES::UNKNOWN
@ UNKNOWN
Definition
PE/Header.hpp:42
LIEF::PE::Header::MACHINE_TYPES::WCEMIPSV2
@ WCEMIPSV2
Definition
PE/Header.hpp:71
LIEF::PE::Header::MACHINE_TYPES::AMD64
@ AMD64
Definition
PE/Header.hpp:46
LIEF::PE::Header::MACHINE_TYPES::MIPS16
@ MIPS16
Definition
PE/Header.hpp:56
LIEF::PE::Header::MACHINE_TYPES::MIPSFPU16
@ MIPSFPU16
Definition
PE/Header.hpp:58
LIEF::PE::Header::MACHINE_TYPES::RISCV32
@ RISCV32
Definition
PE/Header.hpp:63
LIEF::PE::Header::MACHINE_TYPES::RISCV64
@ RISCV64
Definition
PE/Header.hpp:64
LIEF::PE::Header::MACHINE_TYPES::POWERPCBE
@ POWERPCBE
Definition
PE/Header.hpp:61
LIEF::PE::Header::MACHINE_TYPES::ARM64
@ ARM64
Definition
PE/Header.hpp:49
LIEF::PE::Header::MACHINE_TYPES::I386
@ I386
Definition
PE/Header.hpp:51
LIEF::PE::Header::MACHINE_TYPES::POWERPC
@ POWERPC
Definition
PE/Header.hpp:59
LIEF::PE::Header::MACHINE_TYPES::THUMB
@ THUMB
Definition
PE/Header.hpp:70
LIEF::Visitor
Definition
Visitor.hpp:212
enums.hpp
ENABLE_BITMASK_OPERATORS
#define ENABLE_BITMASK_OPERATORS(X)
Definition
enums.hpp:24
LIEF::PE::details
Definition
DataDirectory.hpp:37
LIEF::PE
Namespace related to the LIEF's PE module.
Definition
Abstract/Header.hpp:32
LIEF::PE::to_string
const char * to_string(CODE_PAGES e)
LIEF::PE::PE_TYPE
PE_TYPE
Definition
PE/enums.hpp:22
LIEF
LIEF namespace.
Definition
Abstract/Binary.hpp:41
visibility.h
LIEF_API
#define LIEF_API
Definition
visibility.h:45
LIEF_LOCAL
#define LIEF_LOCAL
Definition
visibility.h:46
Generated by
1.18.0