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
* 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 <cstdint>
20
#include <ostream>
21
#include <vector>
22
23
#include "
LIEF/Object.hpp
"
24
#include "
LIEF/PE/enums.hpp
"
25
#include "
LIEF/enums.hpp
"
26
#include "
LIEF/visibility.h
"
27
28
namespace
LIEF
{
29
class
vector_iostream
;
30
}
31
32
33
namespace
LIEF::PE
{
34
35
namespace
details
{
36
struct
pe_header;
37
}
38
40
class
LIEF_API
Header
:
public
Object
{
41
public
:
42
using
signature_t
= std::array<uint8_t,
/* PE Magic */
4>;
43
44
// clang-format off
45
enum class
MACHINE_TYPES
{
46
UNKNOWN = 0x0,
47
ALPHA = 0x184 ,
48
ALPHA64 = 0x284 ,
49
AM33 = 0x1D3,
50
AMD64 = 0x8664,
51
ARM = 0x1C0,
52
ARMNT = 0x1C4,
53
ARM64 = 0xAA64,
54
EBC = 0xEBC,
55
I386 = 0x14C,
56
IA64 = 0x200,
57
LOONGARCH32 = 0x6232,
58
LOONGARCH64 = 0x6264,
59
M32R = 0x9041,
60
MIPS16 = 0x266,
61
MIPSFPU = 0x366,
62
MIPSFPU16 = 0x466,
63
POWERPC = 0x1F0,
64
POWERPCFP = 0x1F1,
65
POWERPCBE = 0x1F2,
66
R4000 = 0x166,
67
RISCV32 = 0x5032,
68
RISCV64 = 0x5064,
69
RISCV128 = 0x5128,
70
SH3 = 0x1A2,
71
SH3DSP = 0x1A3,
72
SH4 = 0x1A6,
73
SH5 = 0x1A8,
74
THUMB = 0x1C2,
75
WCEMIPSV2 = 0x169,
76
ARM64EC = 0xa641,
77
ARM64X = 0xa64e,
78
CHPE_X86 = 0x3a64,
79
};
80
// clang-format on
81
82
static
bool
is_known_machine
(uint16_t
machine
);
83
84
static
bool
is_arm
(
MACHINE_TYPES
ty) {
85
switch
(ty) {
86
default
:
return
false
;
87
case
MACHINE_TYPES::ARM
:
88
case
MACHINE_TYPES::THUMB
:
89
case
MACHINE_TYPES::ARMNT
:
return
true
;
90
}
91
return
false
;
92
}
93
94
static
bool
is_riscv
(
MACHINE_TYPES
ty) {
95
switch
(ty) {
96
default
:
return
false
;
97
case
MACHINE_TYPES::RISCV32
:
98
case
MACHINE_TYPES::RISCV64
:
99
case
MACHINE_TYPES::RISCV128
:
return
true
;
100
}
101
return
false
;
102
}
103
104
static
bool
is_loonarch
(
MACHINE_TYPES
ty) {
105
switch
(ty) {
106
default
:
return
false
;
107
case
MACHINE_TYPES::LOONGARCH32
:
108
case
MACHINE_TYPES::LOONGARCH64
:
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
:
return
false
;
133
case
MACHINE_TYPES::MIPS16
:
134
case
MACHINE_TYPES::MIPSFPU
:
135
case
MACHINE_TYPES::MIPSFPU16
:
136
case
MACHINE_TYPES::R4000
:
137
case
MACHINE_TYPES::WCEMIPSV2
:
return
true
;
138
}
139
return
false
;
140
}
141
142
static
bool
is_ppc
(
MACHINE_TYPES
ty) {
143
switch
(ty) {
144
default
:
return
false
;
145
case
MACHINE_TYPES::POWERPC
:
146
case
MACHINE_TYPES::POWERPCFP
:
147
case
MACHINE_TYPES::POWERPCBE
:
return
true
;
148
}
149
return
false
;
150
}
151
152
enum class
CHARACTERISTICS
{
153
NONE = 0x0000,
156
RELOCS_STRIPPED = 0x0001,
157
159
EXECUTABLE_IMAGE = 0x0002,
160
162
LINE_NUMS_STRIPPED = 0x0004,
163
166
LOCAL_SYMS_STRIPPED = 0x0008,
167
169
AGGRESSIVE_WS_TRIM = 0x0010,
170
172
LARGE_ADDRESS_AWARE = 0x0020,
173
176
BYTES_REVERSED_LO = 0x0080,
177
179
NEED_32BIT_MACHINE = 0x0100,
180
182
DEBUG_STRIPPED = 0x0200,
183
185
REMOVABLE_RUN_FROM_SWAP = 0x0400,
186
188
NET_RUN_FROM_SWAP = 0x0800,
189
191
SYSTEM = 0x1000,
192
194
DLL = 0x2000,
195
197
UP_SYSTEM_ONLY = 0x4000,
198
200
BYTES_REVERSED_HI = 0x8000,
201
};
202
static
Header
create
(
PE_TYPE
type);
203
204
Header
(
const
details::pe_header& header);
205
~Header
()
override
=
default
;
206
207
Header
&
operator=
(
const
Header
&) =
default
;
208
Header
(
const
Header
&) =
default
;
209
211
const
signature_t
&
signature
()
const
{
212
return
signature_;
213
}
214
216
MACHINE_TYPES
machine
()
const
{
217
return
machine_;
218
}
219
221
uint16_t
numberof_sections
()
const
{
222
return
nb_sections_;
223
}
224
227
uint32_t
time_date_stamp
()
const
{
228
return
timedatestamp_;
229
}
230
235
uint32_t
pointerto_symbol_table
()
const
{
236
return
pointerto_symtab_;
237
}
238
244
uint32_t
numberof_symbols
()
const
{
245
return
nb_symbols_;
246
}
247
257
uint16_t
sizeof_optional_header
()
const
{
258
return
sizeof_opt_header_;
259
}
260
262
uint32_t
characteristics
()
const
{
263
return
characteristics_;
264
}
265
267
bool
has_characteristic
(
CHARACTERISTICS
c)
const
{
268
return
(
characteristics
() &
static_cast<
uint32_t
>
(c)) > 0;
269
}
270
272
std::vector<CHARACTERISTICS>
characteristics_list
()
const
;
273
274
void
machine
(
MACHINE_TYPES
type) {
275
machine_ = type;
276
}
277
278
void
numberof_sections
(uint16_t nb) {
279
nb_sections_ = nb;
280
}
281
282
void
time_date_stamp
(uint32_t timestamp) {
283
timedatestamp_ = timestamp;
284
}
285
286
void
pointerto_symbol_table
(uint32_t ptr) {
287
pointerto_symtab_ = ptr;
288
}
289
290
void
numberof_symbols
(uint32_t nb) {
291
nb_symbols_ = nb;
292
}
293
294
void
sizeof_optional_header
(uint16_t size) {
295
sizeof_opt_header_ = size;
296
}
297
298
void
characteristics
(uint32_t
characteristics
) {
299
characteristics_ =
characteristics
;
300
}
301
302
void
signature
(
const
signature_t
& sig) {
303
signature_ = sig;
304
}
305
306
void
add_characteristic
(
CHARACTERISTICS
c) {
307
characteristics_ |=
static_cast<
uint32_t
>
(c);
308
}
309
310
void
remove_characteristic
(
CHARACTERISTICS
c) {
311
characteristics_ &= ~static_cast<uint32_t>(c);
312
}
313
314
void
accept
(
Visitor
& visitor)
const override
;
315
316
LIEF_API
friend
std::ostream&
operator<<
(std::ostream& os,
const
Header
& entry);
317
319
LIEF_LOCAL
Header
() =
default
;
320
322
LIEF_LOCAL
size_t
serialize(
vector_iostream
& ios,
size_t
nb_sections)
const
;
323
324
private
:
325
signature_t
signature_{};
326
MACHINE_TYPES machine_ = MACHINE_TYPES::UNKNOWN;
327
uint16_t nb_sections_ = 0;
328
uint32_t timedatestamp_ = 0;
329
uint32_t pointerto_symtab_ = 0;
330
uint32_t nb_symbols_ = 0;
331
uint16_t sizeof_opt_header_ = 0;
332
uint32_t characteristics_ = 0;
333
};
334
335
LIEF_API
const
char
*
to_string
(
Header::CHARACTERISTICS
c);
336
LIEF_API
const
char
*
to_string
(
Header::MACHINE_TYPES
c);
337
}
338
339
340
ENABLE_BITMASK_OPERATORS
(
LIEF::PE::Header::CHARACTERISTICS
);
341
#endif
Object.hpp
enums.hpp
LIEF::Object::Object
Object()
LIEF::PE::Header::CHARACTERISTICS
CHARACTERISTICS
Definition
PE/Header.hpp:152
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:142
LIEF::PE::Header::x86_64
static bool x86_64(MACHINE_TYPES ty)
Definition
PE/Header.hpp:126
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:262
LIEF::PE::Header::machine
MACHINE_TYPES machine() const
The targeted machine architecture like ARM, x86, AMD64, ...
Definition
PE/Header.hpp:216
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:221
LIEF::PE::Header::remove_characteristic
void remove_characteristic(CHARACTERISTICS c)
Definition
PE/Header.hpp:310
LIEF::PE::Header::is_riscv
static bool is_riscv(MACHINE_TYPES ty)
Definition
PE/Header.hpp:94
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:211
LIEF::PE::Header::signature
void signature(const signature_t &sig)
Definition
PE/Header.hpp:302
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:257
LIEF::PE::Header::has_characteristic
bool has_characteristic(CHARACTERISTICS c) const
Check if the given CHARACTERISTICS is present.
Definition
PE/Header.hpp:267
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:290
LIEF::PE::Header::is_thumb
static bool is_thumb(MACHINE_TYPES ty)
Definition
PE/Header.hpp:118
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:244
LIEF::PE::Header::pointerto_symbol_table
void pointerto_symbol_table(uint32_t ptr)
Definition
PE/Header.hpp:286
LIEF::PE::Header::is_arm
static bool is_arm(MACHINE_TYPES ty)
Definition
PE/Header.hpp:84
LIEF::PE::Header::machine
void machine(MACHINE_TYPES type)
Definition
PE/Header.hpp:274
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:306
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:42
LIEF::PE::Header::x86
static bool x86(MACHINE_TYPES ty)
Definition
PE/Header.hpp:122
LIEF::PE::Header::characteristics
void characteristics(uint32_t characteristics)
Definition
PE/Header.hpp:298
LIEF::PE::Header::is_arm64
static bool is_arm64(MACHINE_TYPES ty)
Definition
PE/Header.hpp:114
LIEF::PE::Header::pointerto_symbol_table
uint32_t pointerto_symbol_table() const
The offset of the COFF symbol table.
Definition
PE/Header.hpp:235
LIEF::PE::Header::numberof_sections
void numberof_sections(uint16_t nb)
Definition
PE/Header.hpp:278
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:227
LIEF::PE::Header::is_loonarch
static bool is_loonarch(MACHINE_TYPES ty)
Definition
PE/Header.hpp:104
LIEF::PE::Header::is_mips
static bool is_mips(MACHINE_TYPES ty)
Definition
PE/Header.hpp:130
LIEF::PE::Header::sizeof_optional_header
void sizeof_optional_header(uint16_t size)
Definition
PE/Header.hpp:294
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:282
LIEF::PE::Header::MACHINE_TYPES
MACHINE_TYPES
Definition
PE/Header.hpp:45
LIEF::PE::Header::MACHINE_TYPES::RISCV128
@ RISCV128
Definition
PE/Header.hpp:69
LIEF::PE::Header::MACHINE_TYPES::ARMNT
@ ARMNT
Definition
PE/Header.hpp:52
LIEF::PE::Header::MACHINE_TYPES::LOONGARCH32
@ LOONGARCH32
Definition
PE/Header.hpp:57
LIEF::PE::Header::MACHINE_TYPES::POWERPCFP
@ POWERPCFP
Definition
PE/Header.hpp:64
LIEF::PE::Header::MACHINE_TYPES::LOONGARCH64
@ LOONGARCH64
Definition
PE/Header.hpp:58
LIEF::PE::Header::MACHINE_TYPES::ARM
@ ARM
Definition
PE/Header.hpp:51
LIEF::PE::Header::MACHINE_TYPES::MIPSFPU
@ MIPSFPU
Definition
PE/Header.hpp:61
LIEF::PE::Header::MACHINE_TYPES::R4000
@ R4000
Definition
PE/Header.hpp:66
LIEF::PE::Header::MACHINE_TYPES::WCEMIPSV2
@ WCEMIPSV2
Definition
PE/Header.hpp:75
LIEF::PE::Header::MACHINE_TYPES::AMD64
@ AMD64
Definition
PE/Header.hpp:50
LIEF::PE::Header::MACHINE_TYPES::MIPS16
@ MIPS16
Definition
PE/Header.hpp:60
LIEF::PE::Header::MACHINE_TYPES::MIPSFPU16
@ MIPSFPU16
Definition
PE/Header.hpp:62
LIEF::PE::Header::MACHINE_TYPES::RISCV32
@ RISCV32
Definition
PE/Header.hpp:67
LIEF::PE::Header::MACHINE_TYPES::RISCV64
@ RISCV64
Definition
PE/Header.hpp:68
LIEF::PE::Header::MACHINE_TYPES::POWERPCBE
@ POWERPCBE
Definition
PE/Header.hpp:65
LIEF::PE::Header::MACHINE_TYPES::ARM64
@ ARM64
Definition
PE/Header.hpp:53
LIEF::PE::Header::MACHINE_TYPES::I386
@ I386
Definition
PE/Header.hpp:55
LIEF::PE::Header::MACHINE_TYPES::POWERPC
@ POWERPC
Definition
PE/Header.hpp:63
LIEF::PE::Header::MACHINE_TYPES::THUMB
@ THUMB
Definition
PE/Header.hpp:74
LIEF::Visitor
Definition
Visitor.hpp:212
LIEF::vector_iostream
Definition
iostream.hpp:34
enums.hpp
ENABLE_BITMASK_OPERATORS
#define ENABLE_BITMASK_OPERATORS(X)
Definition
enums.hpp:24
LIEF::PE::details
Definition
DataDirectory.hpp:38
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:42
visibility.h
LIEF_API
#define LIEF_API
Definition
visibility.h:45
LIEF_LOCAL
#define LIEF_LOCAL
Definition
visibility.h:46
Generated by
1.19.0