LIEF: Library to Instrument Executable Formats Version 0.16.0
Loading...
Searching...
No Matches
PE/Header.hpp
Go to the documentation of this file.
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}
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 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 POWERPCBE = 0x1F2,
57 R4000 = 0x166,
58 RISCV32 = 0x5032,
59 RISCV64 = 0x5064,
60 RISCV128 = 0x5128,
61 SH3 = 0x1A2,
62 SH3DSP = 0x1A3,
63 SH4 = 0x1A6,
64 SH5 = 0x1A8,
65 THUMB = 0x1C2,
66 WCEMIPSV2 = 0x169
67 };
68
69 enum class CHARACTERISTICS {
70 NONE = 0x0000,
71 RELOCS_STRIPPED = 0x0001,
72 EXECUTABLE_IMAGE = 0x0002,
73 LINE_NUMS_STRIPPED = 0x0004,
74 LOCAL_SYMS_STRIPPED = 0x0008,
75 AGGRESSIVE_WS_TRIM = 0x0010,
76 LARGE_ADDRESS_AWARE = 0x0020,
77 BYTES_REVERSED_LO = 0x0080,
78 NEED_32BIT_MACHINE = 0x0100,
79 DEBUG_STRIPPED = 0x0200,
80 REMOVABLE_RUN_FROM_SWAP = 0x0400,
81 NET_RUN_FROM_SWAP = 0x0800,
82 SYSTEM = 0x1000,
83 DLL = 0x2000,
84 UP_SYSTEM_ONLY = 0x4000,
85 BYTES_REVERSED_HI = 0x8000
86 };
87 static Header create(PE_TYPE type);
88
89 Header(const details::pe_header& header);
90 ~Header() override = default;
91
92 Header& operator=(const Header&) = default;
93 Header(const Header&) = default;
94 const signature_t& signature() const {
97 return signature_;
98 }
99 MACHINE_TYPES machine() const {
102 return machine_;
103 }
104 uint16_t numberof_sections() const {
107 return nb_sections_;
108 }
109 uint32_t time_date_stamp() const {
113 return timedatestamp_;
114 }
115 uint32_t pointerto_symbol_table() const {
120 return pointerto_symtab_;
121 }
122 uint32_t numberof_symbols() const {
128 return nb_symbols_;
129 }
130 uint16_t sizeof_optional_header() const {
139 return sizeof_opt_header_;
140 }
141 uint32_t characteristics() const {
144 return characteristics_;
145 }
146 bool has_characteristic(CHARACTERISTICS c) const {
149 return (characteristics() & static_cast<uint32_t>(c)) > 0;
150 }
151 std::vector<CHARACTERISTICS> characteristics_list() const;
154
155 void machine(MACHINE_TYPES type) {
156 machine_ = type;
157 }
158
159 void numberof_sections(uint16_t nb) {
160 nb_sections_ = nb;
161 }
162
163 void time_date_stamp(uint32_t timestamp) {
164 timedatestamp_ = timestamp;
165 }
166
167 void pointerto_symbol_table(uint32_t ptr) {
168 pointerto_symtab_ = ptr;
169 }
170
171 void numberof_symbols(uint32_t nb) {
172 nb_symbols_ = nb;
173 }
174
175 void sizeof_optional_header(uint16_t size) {
176 sizeof_opt_header_ = size;
177 }
178
179 void characteristics(uint32_t characteristics) {
180 characteristics_ = characteristics;
181 }
182
183 void signature(const signature_t& sig) {
184 signature_ = sig;
185 }
186
187 void add_characteristic(CHARACTERISTICS c) {
188 characteristics_ |= static_cast<uint32_t>(c);
189 }
190
191 void remove_characteristic(CHARACTERISTICS c) {
192 characteristics_ &= ~static_cast<uint32_t>(c);
193 }
194
195 void accept(Visitor& visitor) const override;
196
197 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Header& entry);
198
199 private:
200 Header() = default;
201 signature_t signature_;
202 MACHINE_TYPES machine_ = MACHINE_TYPES::UNKNOWN;
203 uint16_t nb_sections_ = 0;
204 uint32_t timedatestamp_ = 0;
205 uint32_t pointerto_symtab_;
206 uint32_t nb_symbols_ = 0;
207 uint16_t sizeof_opt_header_ = 0;
208 uint32_t characteristics_ = 0;
209};
210
211LIEF_API const char* to_string(Header::CHARACTERISTICS c);
212LIEF_API const char* to_string(Header::MACHINE_TYPES c);
213}
214}
215
216ENABLE_BITMASK_OPERATORS(LIEF::PE::Header::CHARACTERISTICS);
217#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:69
LIEF::PE::Header::accept
void accept(Visitor &visitor) const override
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:143
LIEF::PE::Header::machine
MACHINE_TYPES machine() const
The targeted machine architecture like ARM, x86, AMD64, ...
Definition PE/Header.hpp:101
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:106
LIEF::PE::Header::remove_characteristic
void remove_characteristic(CHARACTERISTICS c)
Definition PE/Header.hpp:191
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:96
LIEF::PE::Header::signature
void signature(const signature_t &sig)
Definition PE/Header.hpp:183
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:138
LIEF::PE::Header::has_characteristic
bool has_characteristic(CHARACTERISTICS c) const
Check if the given CHARACTERISTICS is present.
Definition PE/Header.hpp:148
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:171
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:127
LIEF::PE::Header::pointerto_symbol_table
void pointerto_symbol_table(uint32_t ptr)
Definition PE/Header.hpp:167
LIEF::PE::Header::machine
void machine(MACHINE_TYPES type)
Definition PE/Header.hpp:155
LIEF::PE::Header::add_characteristic
void add_characteristic(CHARACTERISTICS c)
Definition PE/Header.hpp:187
LIEF::PE::Header::operator<<
friend std::ostream & operator<<(std::ostream &os, const Header &entry)
LIEF::PE::Header::characteristics
void characteristics(uint32_t characteristics)
Definition PE/Header.hpp:179
LIEF::PE::Header::pointerto_symbol_table
uint32_t pointerto_symbol_table() const
The offset of the COFF symbol table.
Definition PE/Header.hpp:119
LIEF::PE::Header::numberof_sections
void numberof_sections(uint16_t nb)
Definition PE/Header.hpp:159
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:112
LIEF::PE::Header::sizeof_optional_header
void sizeof_optional_header(uint16_t size)
Definition PE/Header.hpp:175
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:163
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 CodeIntegrity.hpp:26
LIEF::PE
Namespace related to the LIEF's PE module.
Definition Abstract/Header.hpp:32
LIEF::PE::to_string
const char * to_string(DataDirectory::TYPES e)
LIEF
LIEF namespace.
Definition Abstract/Binary.hpp:36
visibility.h
LIEF_API
#define LIEF_API
Definition visibility.h:41