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}
34class 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;
82 case MACHINE_TYPES::ARM:
83 case MACHINE_TYPES::THUMB:
84 case MACHINE_TYPES::ARMNT:
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;
94 case MACHINE_TYPES::RISCV32:
95 case MACHINE_TYPES::RISCV64:
96 case MACHINE_TYPES::RISCV128:
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;
106 case MACHINE_TYPES::LOONGARCH32:
107 case MACHINE_TYPES::LOONGARCH64:
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;
134 case MACHINE_TYPES::MIPS16:
135 case MACHINE_TYPES::MIPSFPU:
136 case MACHINE_TYPES::MIPSFPU16:
137 case MACHINE_TYPES::R4000:
138 case MACHINE_TYPES::WCEMIPSV2:
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;
148 case MACHINE_TYPES::POWERPC:
149 case MACHINE_TYPES::POWERPCFP:
150 case MACHINE_TYPES::POWERPCBE:
151 return true;
152 }
153 return false;
154 }
155
156 enum class CHARACTERISTICS {
157 NONE = 0x0000,
158 RELOCS_STRIPPED = 0x0001,
159 EXECUTABLE_IMAGE = 0x0002,
160 LINE_NUMS_STRIPPED = 0x0004,
161 LOCAL_SYMS_STRIPPED = 0x0008,
162 AGGRESSIVE_WS_TRIM = 0x0010,
163 LARGE_ADDRESS_AWARE = 0x0020,
164 BYTES_REVERSED_LO = 0x0080,
165 NEED_32BIT_MACHINE = 0x0100,
166 DEBUG_STRIPPED = 0x0200,
167 REMOVABLE_RUN_FROM_SWAP = 0x0400,
168 NET_RUN_FROM_SWAP = 0x0800,
169 SYSTEM = 0x1000,
170 DLL = 0x2000,
171 UP_SYSTEM_ONLY = 0x4000,
172 BYTES_REVERSED_HI = 0x8000
173 };
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 const signature_t& signature() const {
184 return signature_;
185 }
186 MACHINE_TYPES machine() const {
189 return machine_;
190 }
191 uint16_t numberof_sections() const {
194 return nb_sections_;
195 }
196 uint32_t time_date_stamp() const {
200 return timedatestamp_;
201 }
202 uint32_t pointerto_symbol_table() const {
207 return pointerto_symtab_;
208 }
209 uint32_t numberof_symbols() const {
215 return nb_symbols_;
216 }
217 uint16_t sizeof_optional_header() const {
226 return sizeof_opt_header_;
227 }
228 uint32_t characteristics() const {
231 return characteristics_;
232 }
233 bool has_characteristic(CHARACTERISTICS c) const {
236 return (characteristics() & static_cast<uint32_t>(c)) > 0;
237 }
238 std::vector<CHARACTERISTICS> characteristics_list() const;
241
242 void machine(MACHINE_TYPES type) {
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
266 void characteristics(uint32_t characteristics) {
267 characteristics_ = characteristics;
268 }
269
270 void signature(const signature_t& sig) {
271 signature_ = sig;
272 }
273
274 void add_characteristic(CHARACTERISTICS c) {
275 characteristics_ |= static_cast<uint32_t>(c);
276 }
277
278 void remove_characteristic(CHARACTERISTICS c) {
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 LIEF_LOCAL Header() = default;
288
289 private:
290 signature_t signature_;
291 MACHINE_TYPES machine_ = MACHINE_TYPES::UNKNOWN;
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
300LIEF_API const char* to_string(Header::CHARACTERISTICS c);
301LIEF_API const char* to_string(Header::MACHINE_TYPES c);
302}
303}
304
305ENABLE_BITMASK_OPERATORS(LIEF::PE::Header::CHARACTERISTICS);
306#endif
Object.hpp
enums.hpp
LIEF::PE::Header
Class that represents the PE header (which follows the DosHeader)
Definition PE/Header.hpp:36
LIEF::PE::Header::CHARACTERISTICS
CHARACTERISTICS
Definition PE/Header.hpp:156
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:144
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:230
LIEF::PE::Header::machine
MACHINE_TYPES machine() const
The targeted machine architecture like ARM, x86, AMD64, ...
Definition PE/Header.hpp:188
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:193
LIEF::PE::Header::remove_characteristic
void remove_characteristic(CHARACTERISTICS c)
Definition PE/Header.hpp:278
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:183
LIEF::PE::Header::signature
void signature(const signature_t &sig)
Definition PE/Header.hpp:270
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:225
LIEF::PE::Header::has_characteristic
bool has_characteristic(CHARACTERISTICS c) const
Check if the given CHARACTERISTICS is present.
Definition PE/Header.hpp:235
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:258
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:214
LIEF::PE::Header::pointerto_symbol_table
void pointerto_symbol_table(uint32_t ptr)
Definition PE/Header.hpp:254
LIEF::PE::Header::is_arm
static bool is_arm(MACHINE_TYPES ty)
Definition PE/Header.hpp:78
LIEF::PE::Header::machine
void machine(MACHINE_TYPES type)
Definition PE/Header.hpp:242
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:274
LIEF::PE::Header::operator<<
friend std::ostream & operator<<(std::ostream &os, const Header &entry)
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:266
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:206
LIEF::PE::Header::numberof_sections
void numberof_sections(uint16_t nb)
Definition PE/Header.hpp:246
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:199
LIEF::PE::Header::is_loonarch
static bool is_loonarch(MACHINE_TYPES ty)
Definition PE/Header.hpp:102
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:262
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:250
LIEF::PE::Header::MACHINE_TYPES
MACHINE_TYPES
Definition PE/Header.hpp:40
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
LIEF namespace.
Definition Abstract/Binary.hpp:39
visibility.h
LIEF_API
#define LIEF_API
Definition visibility.h:41
LIEF_LOCAL
#define LIEF_LOCAL
Definition visibility.h:42