LIEF: Library to Instrument Executable Formats
Version 2.0.0
Toggle main menu visibility
Loading...
Searching...
No Matches
lief-install
x86_64
static
include
LIEF
PE
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
27
namespace
LIEF
{
28
class
vector_iostream
;
29
}
30
31
32
namespace
LIEF::PE
{
33
34
namespace
details
{
35
struct
pe_header;
36
}
37
39
class
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
;
86
case
MACHINE_TYPES::ARM
:
87
case
MACHINE_TYPES::THUMB
:
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
;
96
case
MACHINE_TYPES::RISCV32
:
97
case
MACHINE_TYPES::RISCV64
:
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
;
106
case
MACHINE_TYPES::LOONGARCH32
:
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
;
132
case
MACHINE_TYPES::MIPS16
:
133
case
MACHINE_TYPES::MIPSFPU
:
134
case
MACHINE_TYPES::MIPSFPU16
:
135
case
MACHINE_TYPES::R4000
:
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
;
144
case
MACHINE_TYPES::POWERPC
:
145
case
MACHINE_TYPES::POWERPCFP
:
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
215
MACHINE_TYPES
machine
()
const
{
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
266
bool
has_characteristic
(
CHARACTERISTICS
c)
const
{
267
return
(
characteristics
() &
static_cast<
uint32_t
>
(c)) > 0;
268
}
269
271
std::vector<CHARACTERISTICS>
characteristics_list
()
const
;
272
273
void
machine
(
MACHINE_TYPES
type) {
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
297
void
characteristics
(uint32_t
characteristics
) {
298
characteristics_ =
characteristics
;
299
}
300
301
void
signature
(
const
signature_t
& sig) {
302
signature_ = sig;
303
}
304
305
void
add_characteristic
(
CHARACTERISTICS
c) {
306
characteristics_ |=
static_cast<
uint32_t
>
(c);
307
}
308
309
void
remove_characteristic
(
CHARACTERISTICS
c) {
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
334
LIEF_API
const
char
*
to_string
(
Header::CHARACTERISTICS
c);
335
LIEF_API
const
char
*
to_string
(
Header::MACHINE_TYPES
c);
336
}
337
338
339
ENABLE_BITMASK_OPERATORS
(
LIEF::PE::Header::CHARACTERISTICS
);
340
#endif
Object.hpp
enums.hpp
LIEF::Object
Definition
Object.hpp:24
LIEF::PE::Header::CHARACTERISTICS
CHARACTERISTICS
Definition
PE/Header.hpp:151
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:141
LIEF::PE::Header::x86_64
static bool x86_64(MACHINE_TYPES ty)
Definition
PE/Header.hpp:125
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:261
LIEF::PE::Header::machine
MACHINE_TYPES machine() const
The targeted machine architecture like ARM, x86, AMD64, ...
Definition
PE/Header.hpp:215
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:220
LIEF::PE::Header::remove_characteristic
void remove_characteristic(CHARACTERISTICS c)
Definition
PE/Header.hpp:309
LIEF::PE::Header::is_riscv
static bool is_riscv(MACHINE_TYPES ty)
Definition
PE/Header.hpp:93
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:210
LIEF::PE::Header::signature
void signature(const signature_t &sig)
Definition
PE/Header.hpp:301
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:256
LIEF::PE::Header::has_characteristic
bool has_characteristic(CHARACTERISTICS c) const
Check if the given CHARACTERISTICS is present.
Definition
PE/Header.hpp:266
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:289
LIEF::PE::Header::is_thumb
static bool is_thumb(MACHINE_TYPES ty)
Definition
PE/Header.hpp:117
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:243
LIEF::PE::Header::pointerto_symbol_table
void pointerto_symbol_table(uint32_t ptr)
Definition
PE/Header.hpp:285
LIEF::PE::Header::is_arm
static bool is_arm(MACHINE_TYPES ty)
Definition
PE/Header.hpp:83
LIEF::PE::Header::machine
void machine(MACHINE_TYPES type)
Definition
PE/Header.hpp:273
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:305
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:41
LIEF::PE::Header::x86
static bool x86(MACHINE_TYPES ty)
Definition
PE/Header.hpp:121
LIEF::PE::Header::characteristics
void characteristics(uint32_t characteristics)
Definition
PE/Header.hpp:297
LIEF::PE::Header::is_arm64
static bool is_arm64(MACHINE_TYPES ty)
Definition
PE/Header.hpp:113
LIEF::PE::Header::pointerto_symbol_table
uint32_t pointerto_symbol_table() const
The offset of the COFF symbol table.
Definition
PE/Header.hpp:234
LIEF::PE::Header::numberof_sections
void numberof_sections(uint16_t nb)
Definition
PE/Header.hpp:277
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:226
LIEF::PE::Header::is_loonarch
static bool is_loonarch(MACHINE_TYPES ty)
Definition
PE/Header.hpp:103
LIEF::PE::Header::is_mips
static bool is_mips(MACHINE_TYPES ty)
Definition
PE/Header.hpp:129
LIEF::PE::Header::sizeof_optional_header
void sizeof_optional_header(uint16_t size)
Definition
PE/Header.hpp:293
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:281
LIEF::PE::Header::MACHINE_TYPES
MACHINE_TYPES
Definition
PE/Header.hpp:44
LIEF::PE::Header::MACHINE_TYPES::RISCV128
@ RISCV128
Definition
PE/Header.hpp:68
LIEF::PE::Header::MACHINE_TYPES::ARMNT
@ ARMNT
Definition
PE/Header.hpp:51
LIEF::PE::Header::MACHINE_TYPES::LOONGARCH32
@ LOONGARCH32
Definition
PE/Header.hpp:56
LIEF::PE::Header::MACHINE_TYPES::POWERPCFP
@ POWERPCFP
Definition
PE/Header.hpp:63
LIEF::PE::Header::MACHINE_TYPES::LOONGARCH64
@ LOONGARCH64
Definition
PE/Header.hpp:57
LIEF::PE::Header::MACHINE_TYPES::ARM
@ ARM
Definition
PE/Header.hpp:50
LIEF::PE::Header::MACHINE_TYPES::MIPSFPU
@ MIPSFPU
Definition
PE/Header.hpp:60
LIEF::PE::Header::MACHINE_TYPES::R4000
@ R4000
Definition
PE/Header.hpp:65
LIEF::PE::Header::MACHINE_TYPES::WCEMIPSV2
@ WCEMIPSV2
Definition
PE/Header.hpp:74
LIEF::PE::Header::MACHINE_TYPES::AMD64
@ AMD64
Definition
PE/Header.hpp:49
LIEF::PE::Header::MACHINE_TYPES::MIPS16
@ MIPS16
Definition
PE/Header.hpp:59
LIEF::PE::Header::MACHINE_TYPES::MIPSFPU16
@ MIPSFPU16
Definition
PE/Header.hpp:61
LIEF::PE::Header::MACHINE_TYPES::RISCV32
@ RISCV32
Definition
PE/Header.hpp:66
LIEF::PE::Header::MACHINE_TYPES::RISCV64
@ RISCV64
Definition
PE/Header.hpp:67
LIEF::PE::Header::MACHINE_TYPES::POWERPCBE
@ POWERPCBE
Definition
PE/Header.hpp:64
LIEF::PE::Header::MACHINE_TYPES::ARM64
@ ARM64
Definition
PE/Header.hpp:52
LIEF::PE::Header::MACHINE_TYPES::I386
@ I386
Definition
PE/Header.hpp:54
LIEF::PE::Header::MACHINE_TYPES::POWERPC
@ POWERPC
Definition
PE/Header.hpp:62
LIEF::PE::Header::MACHINE_TYPES::THUMB
@ THUMB
Definition
PE/Header.hpp:73
LIEF::Visitor
Definition
Visitor.hpp:211
LIEF::vector_iostream
Definition
iostream.hpp:33
enums.hpp
ENABLE_BITMASK_OPERATORS
#define ENABLE_BITMASK_OPERATORS(X)
Definition
enums.hpp:23
LIEF::PE::details
Definition
DataDirectory.hpp:37
LIEF::PE
Namespace related to the LIEF's PE module.
Definition
Abstract/Header.hpp:31
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:44
LIEF_LOCAL
#define LIEF_LOCAL
Definition
visibility.h:45
Generated by
1.19.0