LIEF: Library to Instrument Executable Formats Version 0.15.0
Loading...
Searching...
No Matches
PE/Header.hpp
1/* Copyright 2017 - 2024 R. Thomas
2 * Copyright 2017 - 2024 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}
34
36class 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 AM33 = 0x1D3,
43 AMD64 = 0x8664,
44 ARM = 0x1C0,
45 ARMNT = 0x1C4,
46 ARM64 = 0xAA64,
47 EBC = 0xEBC,
48 I386 = 0x14C,
49 IA64 = 0x200,
50 M32R = 0x9041,
51 MIPS16 = 0x266,
52 MIPSFPU = 0x366,
53 MIPSFPU16 = 0x466,
54 POWERPC = 0x1F0,
55 POWERPCFP = 0x1F1,
56 R4000 = 0x166,
57 RISCV32 = 0x5032,
58 RISCV64 = 0x5064,
59 RISCV128 = 0x5128,
60 SH3 = 0x1A2,
61 SH3DSP = 0x1A3,
62 SH4 = 0x1A6,
63 SH5 = 0x1A8,
64 THUMB = 0x1C2,
65 WCEMIPSV2 = 0x169
66 };
67
68 enum class CHARACTERISTICS {
69 NONE = 0x0000,
70 RELOCS_STRIPPED = 0x0001,
71 EXECUTABLE_IMAGE = 0x0002,
72 LINE_NUMS_STRIPPED = 0x0004,
73 LOCAL_SYMS_STRIPPED = 0x0008,
74 AGGRESSIVE_WS_TRIM = 0x0010,
75 LARGE_ADDRESS_AWARE = 0x0020,
76 BYTES_REVERSED_LO = 0x0080,
77 NEED_32BIT_MACHINE = 0x0100,
78 DEBUG_STRIPPED = 0x0200,
79 REMOVABLE_RUN_FROM_SWAP = 0x0400,
80 NET_RUN_FROM_SWAP = 0x0800,
81 SYSTEM = 0x1000,
82 DLL = 0x2000,
83 UP_SYSTEM_ONLY = 0x4000,
84 BYTES_REVERSED_HI = 0x8000
85 };
86 static Header create(PE_TYPE type);
87
88 Header(const details::pe_header& header);
89 ~Header() override;
90
91 Header& operator=(const Header&);
92 Header(const Header&);
93
95 const signature_t& signature() const {
96 return signature_;
97 }
98
101 return machine_;
102 }
103
105 uint16_t numberof_sections() const {
106 return nb_sections_;
107 }
108
111 uint32_t time_date_stamp() const {
112 return timedatestamp_;
113 }
114
118 uint32_t pointerto_symbol_table() const {
119 return pointerto_symtab_;
120 }
121
126 uint32_t numberof_symbols() const {
127 return nb_symbols_;
128 }
129
137 uint16_t sizeof_optional_header() const {
138 return sizeof_opt_header_;
139 }
140
142 uint32_t characteristics() const {
143 return characteristics_;
144 }
145
148 return (characteristics() & static_cast<uint32_t>(c)) > 0;
149 }
150
152 std::vector<CHARACTERISTICS> characteristics_list() const;
153
154 void machine(MACHINE_TYPES type) {
155 machine_ = type;
156 }
157
158 void numberof_sections(uint16_t nb) {
159 nb_sections_ = nb;
160 }
161
162 void time_date_stamp(uint32_t timestamp) {
163 timedatestamp_ = timestamp;
164 }
165
166 void pointerto_symbol_table(uint32_t ptr) {
167 pointerto_symtab_ = ptr;
168 }
169
170 void numberof_symbols(uint32_t nb) {
171 nb_symbols_ = nb;
172 }
173
174 void sizeof_optional_header(uint16_t size) {
175 sizeof_opt_header_ = size;
176 }
177
178 void characteristics(uint32_t characteristics) {
179 characteristics_ = characteristics;
180 }
181
182 void signature(const signature_t& sig) {
183 signature_ = sig;
184 }
185
186 void add_characteristic(CHARACTERISTICS c) {
187 characteristics_ |= static_cast<uint32_t>(c);
188 }
189
190 void remove_characteristic(CHARACTERISTICS c) {
191 characteristics_ &= ~static_cast<uint32_t>(c);
192 }
193
194 void accept(Visitor& visitor) const override;
195
196 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Header& entry);
197
198 private:
199 Header();
200 signature_t signature_;
201 MACHINE_TYPES machine_ = MACHINE_TYPES::UNKNOWN;
202 uint16_t nb_sections_ = 0;
203 uint32_t timedatestamp_ = 0;
204 uint32_t pointerto_symtab_;
205 uint32_t nb_symbols_ = 0;
206 uint16_t sizeof_opt_header_ = 0;
207 uint32_t characteristics_ = 0;
208};
209
210LIEF_API const char* to_string(Header::CHARACTERISTICS c);
211LIEF_API const char* to_string(Header::MACHINE_TYPES c);
212}
213}
214
215ENABLE_BITMASK_OPERATORS(LIEF::PE::Header::CHARACTERISTICS);
216#endif
Definition Object.hpp:25
Class that represents the PE header (which follows the DosHeader)
Definition PE/Header.hpp:36
CHARACTERISTICS
Definition PE/Header.hpp:68
uint32_t characteristics() const
Characteristics of the binary like whether it is a DLL or an executable.
Definition PE/Header.hpp:142
MACHINE_TYPES machine() const
The targeted machine architecture like ARM, x86, AMD64, ...
Definition PE/Header.hpp:100
uint16_t numberof_sections() const
The number of sections in the binary.
Definition PE/Header.hpp:105
const signature_t & signature() const
Signature (or magic byte) of the header. It must be: PE\0\0
Definition PE/Header.hpp:95
uint16_t sizeof_optional_header() const
Size of the OptionalHeader AND the data directories which follows this header.
Definition PE/Header.hpp:137
bool has_characteristic(CHARACTERISTICS c) const
Check if the given CHARACTERISTICS is present.
Definition PE/Header.hpp:147
std::vector< CHARACTERISTICS > characteristics_list() const
The list of the CHARACTERISTICS.
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:126
uint32_t pointerto_symbol_table() const
The offset of the COFF symbol table.
Definition PE/Header.hpp:118
uint32_t time_date_stamp() const
The low 32 bits of the number of seconds since January 1, 1970. Basically, it indicates when the file...
Definition PE/Header.hpp:111
MACHINE_TYPES
Definition PE/Header.hpp:40
PE_TYPE
Definition PE/enums.hpp:680
LIEF namespace.
Definition Abstract/Binary.hpp:32