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_arm(MACHINE_TYPES ty) {
77 switch (ty) {
78 default:
79 return false;
80 case MACHINE_TYPES::ARM:
81 case MACHINE_TYPES::THUMB:
82 case MACHINE_TYPES::ARMNT:
83 return true;
84 }
85 return false;
86 }
87
88 static bool is_riscv(MACHINE_TYPES ty) {
89 switch (ty) {
90 default:
91 return false;
92 case MACHINE_TYPES::RISCV32:
93 case MACHINE_TYPES::RISCV64:
94 case MACHINE_TYPES::RISCV128:
95 return true;
96 }
97 return false;
98 }
99
100 static bool is_loonarch(MACHINE_TYPES ty) {
101 switch (ty) {
102 default:
103 return false;
104 case MACHINE_TYPES::LOONGARCH32:
105 case MACHINE_TYPES::LOONGARCH64:
106 return true;
107 }
108 return false;
109 }
110
111
112 static bool is_arm64(MACHINE_TYPES ty) {
113 return ty == MACHINE_TYPES::ARM64;
114 }
115
116 static bool is_thumb(MACHINE_TYPES ty) {
117 return ty == MACHINE_TYPES::THUMB;
118 }
119
120 static bool x86(MACHINE_TYPES ty) {
121 return ty == MACHINE_TYPES::I386;
122 }
123
124 static bool x86_64(MACHINE_TYPES ty) {
125 return ty == MACHINE_TYPES::AMD64;
126 }
127
128 static bool is_mips(MACHINE_TYPES ty) {
129 switch (ty) {
130 default:
131 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:
137 return true;
138 }
139 return false;
140 }
141
142 static bool is_ppc(MACHINE_TYPES ty) {
143 switch (ty) {
144 default:
145 return false;
146 case MACHINE_TYPES::POWERPC:
147 case MACHINE_TYPES::POWERPCFP:
148 case MACHINE_TYPES::POWERPCBE:
149 return true;
150 }
151 return false;
152 }
153
154 enum class CHARACTERISTICS {
155 NONE = 0x0000,
156 RELOCS_STRIPPED = 0x0001,
157 EXECUTABLE_IMAGE = 0x0002,
158 LINE_NUMS_STRIPPED = 0x0004,
159 LOCAL_SYMS_STRIPPED = 0x0008,
160 AGGRESSIVE_WS_TRIM = 0x0010,
161 LARGE_ADDRESS_AWARE = 0x0020,
162 BYTES_REVERSED_LO = 0x0080,
163 NEED_32BIT_MACHINE = 0x0100,
164 DEBUG_STRIPPED = 0x0200,
165 REMOVABLE_RUN_FROM_SWAP = 0x0400,
166 NET_RUN_FROM_SWAP = 0x0800,
167 SYSTEM = 0x1000,
168 DLL = 0x2000,
169 UP_SYSTEM_ONLY = 0x4000,
170 BYTES_REVERSED_HI = 0x8000
171 };
172 static Header create(PE_TYPE type);
173
174 Header(const details::pe_header& header);
175 ~Header() override = default;
176
177 Header& operator=(const Header&) = default;
178 Header(const Header&) = default;
179 const signature_t& signature() const {
182 return signature_;
183 }
184 MACHINE_TYPES machine() const {
187 return machine_;
188 }
189 uint16_t numberof_sections() const {
192 return nb_sections_;
193 }
194 uint32_t time_date_stamp() const {
198 return timedatestamp_;
199 }
200 uint32_t pointerto_symbol_table() const {
205 return pointerto_symtab_;
206 }
207 uint32_t numberof_symbols() const {
213 return nb_symbols_;
214 }
215 uint16_t sizeof_optional_header() const {
224 return sizeof_opt_header_;
225 }
226 uint32_t characteristics() const {
229 return characteristics_;
230 }
231 bool has_characteristic(CHARACTERISTICS c) const {
234 return (characteristics() & static_cast<uint32_t>(c)) > 0;
235 }
236 std::vector<CHARACTERISTICS> characteristics_list() const;
239
240 void machine(MACHINE_TYPES type) {
241 machine_ = type;
242 }
243
244 void numberof_sections(uint16_t nb) {
245 nb_sections_ = nb;
246 }
247
248 void time_date_stamp(uint32_t timestamp) {
249 timedatestamp_ = timestamp;
250 }
251
252 void pointerto_symbol_table(uint32_t ptr) {
253 pointerto_symtab_ = ptr;
254 }
255
256 void numberof_symbols(uint32_t nb) {
257 nb_symbols_ = nb;
258 }
259
260 void sizeof_optional_header(uint16_t size) {
261 sizeof_opt_header_ = size;
262 }
263
264 void characteristics(uint32_t characteristics) {
265 characteristics_ = characteristics;
266 }
267
268 void signature(const signature_t& sig) {
269 signature_ = sig;
270 }
271
272 void add_characteristic(CHARACTERISTICS c) {
273 characteristics_ |= static_cast<uint32_t>(c);
274 }
275
276 void remove_characteristic(CHARACTERISTICS c) {
277 characteristics_ &= ~static_cast<uint32_t>(c);
278 }
279
280 void accept(Visitor& visitor) const override;
281
282 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Header& entry);
283 LIEF_LOCAL Header() = default;
286
287 private:
288 signature_t signature_;
289 MACHINE_TYPES machine_ = MACHINE_TYPES::UNKNOWN;
290 uint16_t nb_sections_ = 0;
291 uint32_t timedatestamp_ = 0;
292 uint32_t pointerto_symtab_ = 0;
293 uint32_t nb_symbols_ = 0;
294 uint16_t sizeof_opt_header_ = 0;
295 uint32_t characteristics_ = 0;
296};
297
298LIEF_API const char* to_string(Header::CHARACTERISTICS c);
299LIEF_API const char* to_string(Header::MACHINE_TYPES c);
300}
301}
302
303ENABLE_BITMASK_OPERATORS(LIEF::PE::Header::CHARACTERISTICS);
304#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:154
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:124
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:228
LIEF::PE::Header::machine
MACHINE_TYPES machine() const
The targeted machine architecture like ARM, x86, AMD64, ...
Definition PE/Header.hpp:186
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:191
LIEF::PE::Header::remove_characteristic
void remove_characteristic(CHARACTERISTICS c)
Definition PE/Header.hpp:276
LIEF::PE::Header::is_riscv
static bool is_riscv(MACHINE_TYPES ty)
Definition PE/Header.hpp:88
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:181
LIEF::PE::Header::signature
void signature(const signature_t &sig)
Definition PE/Header.hpp:268
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:223
LIEF::PE::Header::has_characteristic
bool has_characteristic(CHARACTERISTICS c) const
Check if the given CHARACTERISTICS is present.
Definition PE/Header.hpp:233
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:256
LIEF::PE::Header::is_thumb
static bool is_thumb(MACHINE_TYPES ty)
Definition PE/Header.hpp:116
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:212
LIEF::PE::Header::pointerto_symbol_table
void pointerto_symbol_table(uint32_t ptr)
Definition PE/Header.hpp:252
LIEF::PE::Header::is_arm
static bool is_arm(MACHINE_TYPES ty)
Definition PE/Header.hpp:76
LIEF::PE::Header::machine
void machine(MACHINE_TYPES type)
Definition PE/Header.hpp:240
LIEF::PE::Header::add_characteristic
void add_characteristic(CHARACTERISTICS c)
Definition PE/Header.hpp:272
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:120
LIEF::PE::Header::characteristics
void characteristics(uint32_t characteristics)
Definition PE/Header.hpp:264
LIEF::PE::Header::is_arm64
static bool is_arm64(MACHINE_TYPES ty)
Definition PE/Header.hpp:112
LIEF::PE::Header::pointerto_symbol_table
uint32_t pointerto_symbol_table() const
The offset of the COFF symbol table.
Definition PE/Header.hpp:204
LIEF::PE::Header::numberof_sections
void numberof_sections(uint16_t nb)
Definition PE/Header.hpp:244
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:197
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:128
LIEF::PE::Header::sizeof_optional_header
void sizeof_optional_header(uint16_t size)
Definition PE/Header.hpp:260
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:248
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(AuxiliaryWeakExternal::CHARACTERISTICS e)
LIEF
LIEF namespace.
Definition Abstract/Binary.hpp:36
visibility.h
LIEF_API
#define LIEF_API
Definition visibility.h:41
LIEF_LOCAL
#define LIEF_LOCAL
Definition visibility.h:42