LIEF: Library to Instrument Executable Formats Version 0.15.0
Loading...
Searching...
No Matches
OptionalHeader.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_OPTIONAL_HEADER_H
17#define LIEF_PE_OPTIONAL_HEADER_H
18#include <ostream>
19#include <vector>
20#include <cstdint>
21
22#include "LIEF/Object.hpp"
23#include "LIEF/visibility.h"
24
25#include "LIEF/enums.hpp"
26#include "LIEF/PE/enums.hpp"
27
28namespace LIEF {
29namespace PE {
30class Parser;
31class Binary;
32
33namespace details {
34struct pe32_optional_header;
35struct pe64_optional_header;
36}
37
39class LIEF_API OptionalHeader : public Object {
40 friend class Parser;
41 friend class Binary;
42 public:
43
44 enum class DLL_CHARACTERISTICS: size_t {
45 HIGH_ENTROPY_VA = 0x0020,
46 DYNAMIC_BASE = 0x0040,
47 FORCE_INTEGRITY = 0x0080,
48 NX_COMPAT = 0x0100,
49 NO_ISOLATION = 0x0200,
50 NO_SEH = 0x0400,
51 NO_BIND = 0x0800,
52 APPCONTAINER = 0x1000,
53 WDM_DRIVER = 0x2000,
54 GUARD_CF = 0x4000,
55 TERMINAL_SERVER_AWARE = 0x8000
56 };
57
58 enum class SUBSYSTEM: size_t {
59 UNKNOWN = 0,
60 NATIVE = 1,
61 WINDOWS_GUI = 2,
62 WINDOWS_CUI = 3,
63 OS2_CUI = 5,
64 POSIX_CUI = 7,
65 NATIVE_WINDOWS = 8,
66 WINDOWS_CE_GUI = 9,
67 EFI_APPLICATION = 10,
68 EFI_BOOT_SERVICE_DRIVER = 11,
69 EFI_RUNTIME_DRIVER = 12,
70 EFI_ROM = 13,
71 XBOX = 14,
72 WINDOWS_BOOT_APPLICATION = 16
73 };
74
75 OptionalHeader(const details::pe32_optional_header& header);
76 OptionalHeader(const details::pe64_optional_header& header);
77 ~OptionalHeader() override;
78
79 OptionalHeader& operator=(const OptionalHeader&);
81
82 static OptionalHeader create(PE_TYPE type);
83
85 PE_TYPE magic() const {
86 return magic_;
87 }
88
90 uint8_t major_linker_version() const {
91 return major_linker_version_;
92 }
93
95 uint8_t minor_linker_version() const {
96 return minor_linker_version_;
97 }
98
101 uint32_t sizeof_code() const {
102 return sizeof_code_;
103 }
104
110 uint32_t sizeof_initialized_data() const {
111 return sizeof_initialized_data_;
112 }
113
119 uint32_t sizeof_uninitialized_data() const {
120 return sizeof_uninitialized_data_;
121 }
122
128 uint32_t addressof_entrypoint() const {
129 return entrypoint_;
130 }
131
133 uint32_t baseof_code() const {
134 return baseof_code_;
135 }
136
140 uint32_t baseof_data() const {
141 return baseof_data_;
142 }
143
145 uint64_t imagebase() const {
146 return imagebase_;
147 }
148
153 uint32_t section_alignment() const {
154 return section_align_;
155 }
156
159 uint32_t file_alignment() const {
160 return file_align_;
161 }
162
165 return major_os_version_;
166 }
167
170 return minor_os_version_;
171 }
172
174 uint16_t major_image_version() const {
175 return major_image_version_;
176 }
177
179 uint16_t minor_image_version() const {
180 return minor_image_version_;
181 }
182
184 uint16_t major_subsystem_version() const {
185 return major_subsys_version_;
186 }
187
189 uint16_t minor_subsystem_version() const {
190 return minor_subsys_version_;
191 }
192
195 uint32_t win32_version_value() const {
196 return win32_version_value_;
197 }
198
202 uint32_t sizeof_image() const {
203 return sizeof_image_;
204 }
205
207 uint32_t sizeof_headers() const {
208 return sizeof_headers_;
209 }
210
215 uint32_t checksum() const {
216 return checksum_;
217 }
218
221 return subsystem_;
222 }
223
227 uint32_t dll_characteristics() const {
228 return dll_characteristics_;
229 }
230
235 uint64_t sizeof_stack_reserve() const {
236 return sizeof_stack_reserve_;
237 }
238
240 uint64_t sizeof_stack_commit() const {
241 return sizeof_stack_commit_;
242 }
243
245 uint64_t sizeof_heap_reserve() const {
246 return sizeof_heap_reserve_;
247 }
248
250 uint64_t sizeof_heap_commit() const {
251 return sizeof_heap_commit_;
252 }
253
255 uint32_t loader_flags() const {
256 return loader_flags_;
257 }
258
260 uint32_t numberof_rva_and_size() const {
261 return nb_rva_size_;
262 }
263
266
268 std::vector<DLL_CHARACTERISTICS> dll_characteristics_list() const;
269
272
275
276 void magic(PE_TYPE magic) {
277 magic_ = magic;
278 }
279
280 void major_linker_version(uint8_t value) {
281 major_linker_version_ = value;
282 }
283
284 void minor_linker_version(uint8_t value) {
285 minor_linker_version_ = value;
286 }
287
288 void sizeof_code(uint32_t value) {
289 sizeof_code_ = value;
290 }
291
292 void sizeof_initialized_data(uint32_t value) {
293 sizeof_initialized_data_ = value;
294 }
295
296 void sizeof_uninitialized_data(uint32_t value) {
297 sizeof_uninitialized_data_ = value;
298 }
299
300 void addressof_entrypoint(uint32_t value) {
301 entrypoint_ = value;
302 }
303
304 void baseof_code(uint32_t value) {
305 baseof_code_ = value;
306 }
307
308 void baseof_data(uint32_t value) {
309 baseof_data_ = value;
310 }
311
312 void imagebase(uint64_t value) {
313 imagebase_ = value;
314 }
315
316 void section_alignment(uint32_t value) {
317 section_align_ = value;
318 }
319
320 void file_alignment(uint32_t value) {
321 file_align_ = value;
322 }
323
324 void major_operating_system_version(uint16_t value) {
325 major_os_version_ = value;
326 }
327
328 void minor_operating_system_version(uint16_t value) {
329 minor_os_version_ = value;
330 }
331
332 void major_image_version(uint16_t value) {
333 major_image_version_ = value;
334 }
335
336 void minor_image_version(uint16_t value) {
337 minor_image_version_ = value;
338 }
339
340 void major_subsystem_version(uint16_t value) {
341 major_subsys_version_ = value;
342 }
343
344 void minor_subsystem_version(uint16_t value) {
345 minor_subsys_version_ = value;
346 }
347
348 void win32_version_value(uint32_t value) {
349 win32_version_value_ = value;
350 }
351
352 void sizeof_image(uint32_t value) {
353 sizeof_image_ = value;
354 }
355
356 void sizeof_headers(uint32_t value) {
357 sizeof_headers_ = value;
358 }
359
360 void checksum(uint32_t value) {
361 checksum_ = value;
362 }
363
364 void subsystem(SUBSYSTEM value) {
365 subsystem_ = value;
366 }
367
368 void dll_characteristics(uint32_t value) {
369 dll_characteristics_ = value;
370 }
371
372 void sizeof_stack_reserve(uint64_t value) {
373 sizeof_stack_reserve_ = value;
374 }
375
376 void sizeof_stack_commit(uint64_t value) {
377 sizeof_stack_commit_ = value;
378 }
379
380 void sizeof_heap_reserve(uint64_t value) {
381 sizeof_heap_reserve_ = value;
382 }
383
384 void sizeof_heap_commit(uint64_t value) {
385 sizeof_heap_commit_ = value;
386 }
387
388 void loader_flags(uint32_t value) {
389 loader_flags_ = value;
390 }
391
392 void numberof_rva_and_size(uint32_t value) {
393 nb_rva_size_ = value;
394 }
395
396 void accept(Visitor& visitor) const override;
397
398 OptionalHeader& operator+=(DLL_CHARACTERISTICS c);
399 OptionalHeader& operator-=(DLL_CHARACTERISTICS c);
400
401 LIEF_API friend std::ostream& operator<<(std::ostream& os, const OptionalHeader& entry);
402
403 private:
404 OptionalHeader();
405
406 PE_TYPE magic_ = PE_TYPE::PE32;
407 uint8_t major_linker_version_ = 0;
408 uint8_t minor_linker_version_ = 0;
409 uint32_t sizeof_code_ = 0;
410 uint32_t sizeof_initialized_data_ = 0;
411 uint32_t sizeof_uninitialized_data_ = 0;
412 uint32_t entrypoint_ = 0;
413 uint32_t baseof_code_ = 0;
414 uint32_t baseof_data_ = 0;
415 uint64_t imagebase_ = 0;
416 uint32_t section_align_ = 0;
417 uint32_t file_align_ = 0;
418 uint16_t major_os_version_ = 0;
419 uint16_t minor_os_version_ = 0;
420 uint16_t major_image_version_ = 0;
421 uint16_t minor_image_version_ = 0;
422 uint16_t major_subsys_version_ = 0;
423 uint16_t minor_subsys_version_ = 0;
424 uint32_t win32_version_value_ = 0;
425 uint32_t sizeof_image_ = 0;
426 uint32_t sizeof_headers_ = 0;
427 uint32_t checksum_ = 0;
428 SUBSYSTEM subsystem_ = SUBSYSTEM::UNKNOWN;
429 uint32_t dll_characteristics_ = 0;
430 uint64_t sizeof_stack_reserve_ = 0;
431 uint64_t sizeof_stack_commit_ = 0;
432 uint64_t sizeof_heap_reserve_ = 0;
433 uint64_t sizeof_heap_commit_ = 0;
434 uint32_t loader_flags_ = 0;
435 uint32_t nb_rva_size_ = 0;
436};
437
438LIEF_API const char* to_string(OptionalHeader::DLL_CHARACTERISTICS);
439LIEF_API const char* to_string(OptionalHeader::SUBSYSTEM);
440
441}
442}
443
445
446#endif
Definition Object.hpp:25
Class which represents a PE binary This is the main interface to manage and modify a PE executable.
Definition PE/Binary.hpp:54
Class which represents the PE OptionalHeader structure.
Definition OptionalHeader.hpp:39
DLL_CHARACTERISTICS
Definition OptionalHeader.hpp:44
uint16_t major_image_version() const
The major version number of the image.
Definition OptionalHeader.hpp:174
uint16_t minor_subsystem_version() const
The minor version number of the subsystem.
Definition OptionalHeader.hpp:189
SUBSYSTEM
Definition OptionalHeader.hpp:58
uint32_t numberof_rva_and_size() const
The number of DataDirectory that follow this header.
Definition OptionalHeader.hpp:260
uint32_t sizeof_uninitialized_data() const
The size of the uninitialized data which are usually located in the .bss section. If the uninitialize...
Definition OptionalHeader.hpp:119
std::vector< DLL_CHARACTERISTICS > dll_characteristics_list() const
Return the list of the dll_characteristics as an std::set of DLL_CHARACTERISTICS.
uint64_t sizeof_stack_commit() const
Size of the stack to commit.
Definition OptionalHeader.hpp:240
bool has(DLL_CHARACTERISTICS c) const
Check if the given DLL_CHARACTERISTICS is included in the dll_characteristics.
uint8_t minor_linker_version() const
The linker minor version.
Definition OptionalHeader.hpp:95
uint8_t major_linker_version() const
The linker major version.
Definition OptionalHeader.hpp:90
uint32_t sizeof_headers() const
Size of the DosHeader + PE Header + Section headers rounded up to a multiple of the file_alignment.
Definition OptionalHeader.hpp:207
void remove(DLL_CHARACTERISTICS c)
Remove a DLL_CHARACTERISTICS from the current characteristics.
uint16_t major_operating_system_version() const
The major version number of the required operating system.
Definition OptionalHeader.hpp:164
uint32_t win32_version_value() const
According to the official PE specifications, this value is reserved and should be 0.
Definition OptionalHeader.hpp:195
uint64_t sizeof_heap_commit() const
Size of the heap to commit.
Definition OptionalHeader.hpp:250
uint32_t sizeof_image() const
The size (in bytes) of the image, including all headers, as the image is loaded in memory.
Definition OptionalHeader.hpp:202
uint64_t imagebase() const
The preferred base address when mapping the binary in memory.
Definition OptionalHeader.hpp:145
uint32_t baseof_data() const
Address relative to the imagebase where the binary's data starts.
Definition OptionalHeader.hpp:140
uint32_t section_alignment() const
The alignment (in bytes) of sections when they are loaded into memory.
Definition OptionalHeader.hpp:153
uint32_t sizeof_code() const
The size of the code .text section or the sum of all the sections that contain code (ie....
Definition OptionalHeader.hpp:101
uint16_t minor_operating_system_version() const
The minor version number of the required operating system.
Definition OptionalHeader.hpp:169
uint16_t major_subsystem_version() const
The major version number of the subsystem.
Definition OptionalHeader.hpp:184
uint32_t loader_flags() const
According to the PE specifications, this value is reserved and should be 0.
Definition OptionalHeader.hpp:255
SUBSYSTEM subsystem() const
Target subsystem like Driver, XBox, Windows GUI, ...
Definition OptionalHeader.hpp:220
uint32_t file_alignment() const
The section's file alignment. This value must be a power of 2 between 512 and 64K....
Definition OptionalHeader.hpp:159
uint32_t baseof_code() const
Address relative to the imagebase where the binary's code starts.
Definition OptionalHeader.hpp:133
uint64_t sizeof_stack_reserve() const
Size of the stack to reserve when loading the PE binary.
Definition OptionalHeader.hpp:235
uint32_t sizeof_initialized_data() const
The size of the initialized data which are usually located in the .data section. If the initialized d...
Definition OptionalHeader.hpp:110
uint32_t dll_characteristics() const
Some characteristics of the underlying binary like the support of the PIE. The prefix dll comes from ...
Definition OptionalHeader.hpp:227
uint32_t checksum() const
The image file checksum. The algorithm for computing the checksum is incorporated into IMAGHELP....
Definition OptionalHeader.hpp:215
uint16_t minor_image_version() const
The minor version number of the image.
Definition OptionalHeader.hpp:179
uint32_t addressof_entrypoint() const
The address of the entry point relative to the image base when the executable file is loaded into mem...
Definition OptionalHeader.hpp:128
void add(DLL_CHARACTERISTICS c)
Add a DLL_CHARACTERISTICS to the current characteristics.
uint64_t sizeof_heap_reserve() const
Size of the heap to reserve when loading the PE binary.
Definition OptionalHeader.hpp:245
PE_TYPE magic() const
Magic bytes: either PE32 or PE32+ for 64-bits PE files.
Definition OptionalHeader.hpp:85
Main interface to parse PE binaries. In particular the static functions: Parser::parse should be used...
Definition PE/Parser.hpp:47
PE_TYPE
Definition PE/enums.hpp:680
LIEF namespace.
Definition Abstract/Binary.hpp:32