LIEF: Library to Instrument Executable Formats Version 2.0.0
Loading...
Searching...
No Matches
PE/Binary.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2026 R. Thomas
2 * Copyright 2017 - 2026 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_BINARY_H
17#define LIEF_PE_BINARY_H
18
19#include <string_view>
20#include <memory>
21
22#include "LIEF/PE/Builder.hpp"
25#include "LIEF/PE/DosHeader.hpp"
26#include "LIEF/PE/Header.hpp"
27#include "LIEF/PE/Import.hpp"
31
32#include "LIEF/COFF/String.hpp"
33#include "LIEF/COFF/Symbol.hpp"
34
36
38#include "LIEF/visibility.h"
39
40
42namespace LIEF::PE {
43class ExceptionInfo;
44class CodeViewPDB;
45class Debug;
46class Export;
48class Parser;
49class Relocation;
50class ResourceData;
52class ResourceNode;
53class RichHeader;
54class TLS;
55class Factory;
56
60 friend class Parser;
61 friend class Builder;
62 friend class Factory;
63
64 public:
66 using sections_t = std::vector<std::unique_ptr<Section>>;
67
70
73
75 using data_directories_t = std::vector<std::unique_ptr<DataDirectory>>;
76
79
83
85 using relocations_t = std::vector<std::unique_ptr<Relocation>>;
86
89
93
95 using imports_t = std::vector<std::unique_ptr<Import>>;
96
99
102
104 using delay_imports_t = std::vector<std::unique_ptr<DelayImport>>;
105
108
112
114 using debug_entries_t = std::vector<std::unique_ptr<Debug>>;
115
118
122
124 using symbols_t = std::vector<std::unique_ptr<COFF::Symbol>>;
125
128
132
134 using strings_table_t = std::vector<COFF::String>;
135
138
141
143 using signatures_t = std::vector<Signature>;
144
147
150
152 using exceptions_t = std::vector<std::unique_ptr<ExceptionInfo>>;
153
156
160
162 ~Binary() override;
163
165 PE_TYPE type() const {
166 return type_;
167 }
168
173 uint64_t rva_to_offset(uint64_t RVA) const;
174
177 uint64_t va_to_offset(uint64_t VA) const {
178 uint64_t rva = VA - optional_header().imagebase();
179 return rva_to_offset(rva);
180 }
181
187 uint64_t slide = 0) const override;
188
190 uint64_t offset_to_rva(uint64_t offset) const;
191
195 uint64_t imagebase() const override {
196 return optional_header().imagebase();
197 }
198
203 return const_cast<Section*>(
204 static_cast<const Binary*>(this)->section_from_offset(offset)
205 );
206 }
207 const Section* section_from_offset(uint64_t offset) const LIEF_LIFETIMEBOUND;
208
212 Section* section_from_rva(uint64_t virtual_address) LIEF_LIFETIMEBOUND {
213 return const_cast<Section*>(
214 static_cast<const Binary*>(this)->section_from_rva(virtual_address)
215 );
216 }
217 const Section*
218 section_from_rva(uint64_t virtual_address) const LIEF_LIFETIMEBOUND;
219
222 return sections_;
223 }
224
226 return sections_;
227 }
228
231 return dos_header_;
232 }
233
235 return dos_header_;
236 }
237
240 return header_;
241 }
242
244 return header_;
245 }
246
250 return optional_header_;
251 }
252
254 return optional_header_;
255 }
256
262 uint32_t compute_checksum() const;
263
266 uint64_t virtual_size() const override;
267
269 uint32_t sizeof_headers() const;
270
273 return tls_.get();
274 }
275
276 const TLS* tls() const LIEF_LIFETIMEBOUND {
277 return tls_.get();
278 }
279
282
284 bool has_tls() const {
285 return tls_ != nullptr;
286 }
287
290
294 bool has_imports() const {
295 return !imports_.empty();
296 }
297
301 bool has_signatures() const {
302 return !signatures_.empty();
303 }
304
308 bool has_exports() const {
309 return export_ != nullptr;
310 }
311
313 bool has_resources() const {
314 return resources_ != nullptr;
315 }
316
318 bool has_exceptions() const {
319 if (const DataDirectory* dir = exceptions_dir()) {
320 return dir->size() > 0;
321 }
322 return false;
323 }
324
328 bool has_relocations() const {
329 return !relocations_.empty();
330 }
331
333 bool has_debug() const {
334 return !debug_.empty();
335 }
336
338 bool has_configuration() const {
339 return loadconfig_ != nullptr;
340 }
341
347
350 return signatures_;
351 }
352
354 return signatures_;
355 }
356
369
383
386 std::vector<uint8_t> authentihash(ALGORITHMS algo) const;
387
390 return export_.get();
391 }
392
394 return export_.get();
395 }
396
398
401 return symbols_;
402 }
403
405 return symbols_;
406 }
407
410 return strings_table_;
411 }
412
414 return strings_table_;
415 }
416
422 auto it = std::find_if(strings_table_.begin(), strings_table_.end(),
423 [offset](const COFF::String& item) {
424 return offset == item.offset();
425 });
426 return it == strings_table_.end() ? nullptr : &*it;
427 }
428
429 const COFF::String* find_coff_string(uint32_t offset) const LIEF_LIFETIMEBOUND {
430 return const_cast<Binary*>(this)->find_coff_string(offset);
431 }
432
435 return resources_.get();
436 }
437
439 return resources_.get();
440 }
441
445
447 set_resources(std::unique_ptr<ResourceNode> root) LIEF_LIFETIMEBOUND;
448
452
457 Section* get_section(std::string_view name) LIEF_LIFETIMEBOUND {
458 return const_cast<Section*>(
459 static_cast<const Binary*>(this)->get_section(name)
460 );
461 }
462 const Section* get_section(std::string_view name) const LIEF_LIFETIMEBOUND;
463
466 const Section* import_section() const;
468 return const_cast<Section*>(
469 static_cast<const Binary*>(this)->import_section()
470 );
471 }
472
478 void remove_section(std::string_view name, bool clear = false) override;
479
483 void remove(const Section& section, bool clear = false);
484
487
490 return relocations_;
491 }
492
494 return relocations_;
495 }
496
499
502
505 return data_directories_;
506 }
507
509 return data_directories_;
510 }
511
514 return const_cast<DataDirectory*>(
515 static_cast<const Binary*>(this)->data_directory(type)
516 );
517 }
518 const DataDirectory*
520
523 return data_directory(type) != nullptr;
524 }
525
528 return debug_;
529 }
530
532 return debug_;
533 }
534
537
539 bool remove_debug(const Debug& entry);
540
543
546
548 return const_cast<CodeViewPDB*>(
549 static_cast<const Binary*>(this)->codeview_pdb()
550 );
551 }
552
556 return loadconfig_.get();
557 }
558
560 return loadconfig_.get();
561 }
562
565 return overlay_;
566 }
567
569 return overlay_;
570 }
571
573 uint64_t overlay_offset() const {
574 return overlay_offset_;
575 }
576
579 return dos_stub_;
580 }
581
583 return dos_stub_;
584 }
585
587 void dos_stub(std::vector<uint8_t> content) {
588 dos_stub_ = std::move(content);
589 }
590
593 return rich_header_.get();
594 }
595
597 return rich_header_.get();
598 }
599
602
604 bool has_rich_header() const {
605 return rich_header_ != nullptr;
606 }
607
610 return imports_;
611 }
612
614 return imports_;
615 }
616
620 Import* get_import(std::string_view import_name) LIEF_LIFETIMEBOUND {
621 return const_cast<Import*>(
622 static_cast<const Binary*>(this)->get_import(import_name)
623 );
624 }
625
626 const Import* get_import(std::string_view import_name) const LIEF_LIFETIMEBOUND;
627
629 bool has_import(std::string_view import_name) const {
630 return get_import(import_name) != nullptr;
631 }
632
634 bool has_delay_imports() const {
635 return !delay_imports_.empty();
636 }
637
640 return delay_imports_;
641 }
642
644 return delay_imports_;
645 }
646
649 DelayImport* get_delay_import(std::string_view import_name) LIEF_LIFETIMEBOUND {
650 return const_cast<DelayImport*>(
651 static_cast<const Binary*>(this)->get_delay_import(import_name)
652 );
653 }
654 const DelayImport*
655 get_delay_import(std::string_view import_name) const LIEF_LIFETIMEBOUND;
656
657
659 bool has_delay_import(std::string_view import_name) const {
660 return get_delay_import(import_name) != nullptr;
661 }
662
667 Import& add_import(std::string_view name, int32_t pos = -1) LIEF_LIFETIMEBOUND {
668 if (pos < 0 || (size_t)pos >= imports_.size()) {
669 imports_.push_back(std::make_unique<Import>(std::string(name)));
670 return *imports_.back();
671 }
672 return **imports_.insert(imports_.begin() + pos,
673 std::make_unique<Import>(std::string(name)));
674 }
675
679 bool remove_import(std::string_view name);
680
683 imports_.clear();
684 }
685
687 std::unique_ptr<Builder> write(const std::string& filename) {
688 return write(filename, Builder::config_t());
689 }
690
693 std::unique_ptr<Builder> write(const std::string& filename,
694 const Builder::config_t& config);
695
700 std::unique_ptr<Builder> write(std::ostream& os) {
701 return write(os, Builder::config_t());
702 }
703
704 std::unique_ptr<Builder> write(std::ostream& os,
705 const Builder::config_t& config);
706
707 void accept(Visitor& visitor) const override;
708
714 void patch_address(uint64_t address, const std::vector<uint8_t>& patch_value,
715 VA_TYPES addr_type = VA_TYPES::AUTO) override;
716
717
725 void patch_address(uint64_t address, uint64_t patch_value,
726 size_t size = sizeof(uint64_t),
727 VA_TYPES addr_type = VA_TYPES::AUTO) override;
728
729
731 void fill_address(uint64_t address, size_t size, uint8_t value = 0,
732 VA_TYPES addr_type = VA_TYPES::AUTO);
733
741 uint64_t virtual_address, uint64_t size,
743 ) const LIEF_LIFETIMEBOUND override;
744
747 uint64_t entrypoint() const override {
748 return optional_header_.imagebase() + optional_header_.addressof_entrypoint();
749 }
750
752 bool is_pie() const override {
753 return optional_header_.has(OptionalHeader::DLL_CHARACTERISTICS::DYNAMIC_BASE);
754 }
755
757 bool has_nx() const override {
758 return optional_header_.has(OptionalHeader::DLL_CHARACTERISTICS::NX_COMPAT);
759 }
760
761 uint64_t last_section_offset() const;
762
767
771
776
780
785
789
794
798
804
808
813
817
822
826
831
835
840
844
849
853
858
862
867
870
873
880 return exceptions_;
881 }
882
884 return exceptions_;
885 }
886
893
895 return const_cast<Binary*>(this)->find_exception_at(rva);
896 }
897
899 bool is_arm64ec() const;
900
903 bool is_arm64x() const;
904
915 const Binary* nested_pe_binary() const {
916 return nested_.get();
917 }
918
920 return nested_.get();
921 }
922
924 result<uint64_t> get_function_address(std::string_view name) const override;
925
926 static bool classof(const LIEF::Binary* bin) {
927 return bin->format() == Binary::FORMATS::PE;
928 }
929
930 std::ostream& print(std::ostream& os) const override;
931
932
936 LIEF_LOCAL std::unique_ptr<Binary> move_nested_pe_binary() {
937 auto ret = std::move(nested_);
938 nested_ = nullptr;
939 return ret;
940 }
941
942 private:
943 struct sizing_info_t {
944 uint32_t nb_tls_callbacks = 0;
945 uint32_t load_config_size = 0;
946 };
947
950 result<uint64_t> make_space_for_new_section();
951
953 LIEF::Binary::symbols_t get_abstract_symbols() override;
954
955 LIEF::Header get_abstract_header() const override {
956 return LIEF::Header::from(*this);
957 }
958
960 LIEF::Binary::sections_t get_abstract_sections() override;
961
962 LIEF::Binary::relocations_t get_abstract_relocations() override;
963
964 LIEF::Binary::functions_t get_abstract_exported_functions() const override;
965 LIEF::Binary::functions_t get_abstract_imported_functions() const override;
966 std::vector<std::string> get_abstract_imported_libraries() const override;
967
968 void update_lookup_address_table_offset();
969 void update_iat();
970 void shift(uint64_t from, uint64_t by);
971
972 PE_TYPE type_ = PE_TYPE::PE32_PLUS;
973 DosHeader dos_header_;
974 Header header_;
975 OptionalHeader optional_header_;
976
977 int32_t available_sections_space_ = 0;
978
979 signatures_t signatures_;
980 sections_t sections_;
981 data_directories_t data_directories_;
982 symbols_t symbols_;
983 strings_table_t strings_table_;
984 relocations_t relocations_;
985 imports_t imports_;
986 delay_imports_t delay_imports_;
987 debug_entries_t debug_;
988 exceptions_t exceptions_;
989 uint64_t overlay_offset_ = 0;
990 std::vector<uint8_t> overlay_;
991 std::vector<uint8_t> dos_stub_;
992 std::vector<uint8_t> section_offset_padding_;
993
994 std::unique_ptr<RichHeader> rich_header_;
995 std::unique_ptr<Export> export_;
996 std::unique_ptr<ResourceNode> resources_;
997 std::unique_ptr<TLS> tls_;
998 std::unique_ptr<LoadConfiguration> loadconfig_;
999 std::unique_ptr<Binary> nested_;
1000
1001 sizing_info_t sizing_info_;
1002};
1003
1004}
1005
1006#endif
Generic interface representing a binary executable.
Definition Abstract/Binary.hpp:61
std::vector< Function > functions_t
Definition Abstract/Binary.hpp:84
@ PE
Definition Abstract/Binary.hpp:79
FORMATS format() const
Executable format (ELF, PE, Mach-O) of the underlying binary.
Definition Abstract/Binary.hpp:126
std::vector< Symbol * > symbols_t
Internal container.
Definition Abstract/Binary.hpp:96
VA_TYPES
Enumeration of virtual address types used for patching and memory access.
Definition Abstract/Binary.hpp:64
@ AUTO
Automatically determine if the address is absolute or relative (default behavior).
Definition Abstract/Binary.hpp:67
std::vector< Section * > sections_t
Internal container.
Definition Abstract/Binary.hpp:87
std::vector< Relocation * > relocations_t
Internal container.
Definition Abstract/Binary.hpp:105
This class represents a string located in the COFF string table.
Definition String.hpp:35
Definition Abstract/Header.hpp:40
static Header from(const LIEF::ELF::Binary &elf)
OptionalHeader & optional_header()
Header that follows the header(). It is named optional from the COFF specification but it is mandator...
Definition PE/Binary.hpp:249
ref_iterator< sections_t &, Section * > it_sections
Iterator that outputs Section& object.
Definition PE/Binary.hpp:69
const DataDirectory * export_dir() const
Definition PE/Binary.hpp:768
std::vector< std::unique_ptr< DelayImport > > delay_imports_t
Internal container for storing PE's DelayImport.
Definition PE/Binary.hpp:104
void remove_all_relocations()
Remove all the relocations.
DataDirectory * export_dir()
Return the data directory associated with the export table.
Definition PE/Binary.hpp:764
it_exceptions exceptions()
Iterator over the exception (_RUNTIME_FUNCTION) functions.
Definition PE/Binary.hpp:879
std::unique_ptr< Builder > write(std::ostream &os)
Reconstruct the binary object and write the raw PE in os stream.
Definition PE/Binary.hpp:700
std::vector< uint8_t > authentihash(ALGORITHMS algo) const
Compute the authentihash according to the algorithm provided in the first parameter.
ref_iterator< data_directories_t &, DataDirectory * > it_data_directories
Iterator that outputs DataDirectory&.
Definition PE/Binary.hpp:78
void remove_tls()
Remove the TLS from the binary.
const LoadConfiguration * load_configuration() const
Return the LoadConfiguration object or a nullptr if the binary does not use the LoadConfiguration.
Definition PE/Binary.hpp:555
std::ostream & print(std::ostream &os) const override
it_const_delay_imports delay_imports() const
Definition PE/Binary.hpp:643
it_const_debug_entries debug() const
Definition PE/Binary.hpp:531
void patch_address(uint64_t address, uint64_t patch_value, size_t size=sizeof(uint64_t), VA_TYPES addr_type=VA_TYPES::AUTO) override
Patch the address with the given value.
bool is_pie() const override
Check if the binary is position independent.
Definition PE/Binary.hpp:752
LIEF::Binary::functions_t ctor_functions() const override
Return the list of the binary constructors.
DataDirectory * exceptions_dir()
Return the data directory associated with the exceptions.
Definition PE/Binary.hpp:791
std::vector< std::unique_ptr< Section > > sections_t
Internal container for storing PE's Section.
Definition PE/Binary.hpp:66
const_ref_iterator< const imports_t &, const Import * > it_const_imports
Iterator that outputs const Import&.
Definition PE/Binary.hpp:101
const_ref_iterator< const delay_imports_t &, const DelayImport * > it_const_delay_imports
Iterator that outputs const DelayImport&.
Definition PE/Binary.hpp:110
void dos_stub(std::vector< uint8_t > content)
Update the DOS stub content.
Definition PE/Binary.hpp:587
DataDirectory * delay_dir()
Return the data directory associated with delayed imports.
Definition PE/Binary.hpp:855
it_imports imports()
Return an iterator over the binary imports.
Definition PE/Binary.hpp:609
bool has_resources() const
Check if the current binary has resources.
Definition PE/Binary.hpp:313
const DataDirectory * import_dir() const
Definition PE/Binary.hpp:777
TLS & tls(const TLS &tls)
Set a TLS object in the current Binary.
const ResourceNode * resources() const
Definition PE/Binary.hpp:438
bool has_debug() const
Check if the current binary contains debug information.
Definition PE/Binary.hpp:333
const DataDirectory * iat_dir() const
Definition PE/Binary.hpp:850
Import * get_import(std::string_view import_name)
Return the Import matching the provided name (case-sensitive).
Definition PE/Binary.hpp:620
it_const_exceptions exceptions() const
Definition PE/Binary.hpp:883
friend class Factory
Definition PE/Binary.hpp:62
const DataDirectory * tls_dir() const
Definition PE/Binary.hpp:832
const DataDirectory * debug_dir() const
Definition PE/Binary.hpp:823
bool has_relocations() const
Check if the current binary has relocations.
Definition PE/Binary.hpp:328
bool has_exports() const
Check if the current binary has exports.
Definition PE/Binary.hpp:308
ref_iterator< strings_table_t & > it_strings_table
Iterator that outputs COFF::String&.
Definition PE/Binary.hpp:137
std::vector< std::unique_ptr< ExceptionInfo > > exceptions_t
Internal container for storing runtime function associated with exceptions.
Definition PE/Binary.hpp:152
std::vector< std::unique_ptr< Relocation > > relocations_t
Internal container for storing PE's Relocation.
Definition PE/Binary.hpp:85
uint64_t rva_to_offset(uint64_t RVA) const
Convert a Relative Virtual Address into an offset.
result< ResourcesManager > resources_manager() const
Return the ResourcesManager (class to manage resources more easily than the tree one).
bool clear_debug()
Remove all debug info from the binary.
const Import * get_import(std::string_view import_name) const
result< uint64_t > offset_to_virtual_address(uint64_t offset, uint64_t slide=0) const override
Convert the given offset into an absolute virtual address.
it_const_imports imports() const
Definition PE/Binary.hpp:613
const OptionalHeader & optional_header() const
Definition PE/Binary.hpp:253
it_signatures signatures()
Definition PE/Binary.hpp:353
Signature::VERIFICATION_FLAGS verify_signature(const Signature &sig, Signature::VERIFICATION_CHECKS checks=Signature::VERIFICATION_CHECKS::DEFAULT) const
Verify the binary with the Signature object provided in the first parameter. It can be used to verify...
Debug * add_debug_info(const Debug &entry)
Add a new debug entry.
span< const uint8_t > overlay() const
Return the overlay content.
Definition PE/Binary.hpp:564
bool is_arm64x() const
True if this binary is compiled in ARM64X mode (contains both ARM64 and ARM64EC code).
bool has_import(std::string_view import_name) const
True if the binary imports the given library name
Definition PE/Binary.hpp:629
bool is_arm64ec() const
True if this binary is compiled in ARM64EC mode (emulation compatible).
DataDirectory * rsrc_dir()
Return the data directory associated with the resources tree.
Definition PE/Binary.hpp:782
void remove_section(std::string_view name, bool clear=false) override
Delete the section with the given name.
const_ref_iterator< const data_directories_t &, const DataDirectory * > it_const_data_directories
Iterator that outputs const DataDirectory&.
Definition PE/Binary.hpp:81
span< const uint8_t > get_content_from_virtual_address(uint64_t virtual_address, uint64_t size, Binary::VA_TYPES addr_type=Binary::VA_TYPES::AUTO) const override
Return the content located at the provided virtual address.
DelayImport * get_delay_import(std::string_view import_name)
Returns the DelayImport matching the given name. If it can't be found, it returns a nullptr.
Definition PE/Binary.hpp:649
const_ref_iterator< const signatures_t & > it_const_signatures
Iterator that outputs const Signature&.
Definition PE/Binary.hpp:149
const_ref_iterator< const symbols_t &, const COFF::Symbol * > it_const_symbols
Iterator that outputs const Symbol&.
Definition PE/Binary.hpp:130
std::vector< std::unique_ptr< Import > > imports_t
Internal container for storing PE's Import.
Definition PE/Binary.hpp:95
const DataDirectory * data_directory(DataDirectory::TYPES type) const
std::vector< std::unique_ptr< COFF::Symbol > > symbols_t
Internal container for storing COFF Symbols.
Definition PE/Binary.hpp:124
DataDirectory * tls_dir()
Return the data directory associated with TLS.
Definition PE/Binary.hpp:828
const DataDirectory * rsrc_dir() const
Definition PE/Binary.hpp:786
void fill_address(uint64_t address, size_t size, uint8_t value=0, VA_TYPES addr_type=VA_TYPES::AUTO)
Fill the content at the provided address with a fixed value.
ref_iterator< signatures_t & > it_signatures
Iterator that outputs Signature&.
Definition PE/Binary.hpp:146
bool has_rich_header() const
Check if the current binary has a RichHeader object.
Definition PE/Binary.hpp:604
ref_iterator< symbols_t &, COFF::Symbol * > it_symbols
Iterator that outputs Symbol&.
Definition PE/Binary.hpp:127
const Section * get_section(std::string_view name) const
bool has_signatures() const
Check if the current binary contains signatures.
Definition PE/Binary.hpp:301
bool has_delay_imports() const
Check if the current binary contains delay imports.
Definition PE/Binary.hpp:634
const DataDirectory * load_config_dir() const
Definition PE/Binary.hpp:841
void rich_header(const RichHeader &rich_header)
Set a RichHeader object in the current Binary.
DataDirectory * relocation_dir()
Return the data directory associated with the relocation table.
Definition PE/Binary.hpp:810
const DataDirectory * exceptions_dir() const
Definition PE/Binary.hpp:795
TLS * tls()
Return a reference to the TLS object.
Definition PE/Binary.hpp:272
it_const_data_directories data_directories() const
Definition PE/Binary.hpp:508
uint64_t entrypoint() const override
Return the binary's entrypoint (it is the same value as OptionalHeader::addressof_entrypoint).
Definition PE/Binary.hpp:747
std::vector< Signature > signatures_t
Internal container for storing PE's authenticode Signature.
Definition PE/Binary.hpp:143
Signature::VERIFICATION_FLAGS verify_signature(Signature::VERIFICATION_CHECKS checks=Signature::VERIFICATION_CHECKS::DEFAULT) const
Verify the binary against the embedded signature(s) (if any) First, it checks that the embedded signa...
const ExceptionInfo * find_exception_at(uint32_t rva) const
Definition PE/Binary.hpp:894
uint64_t last_section_offset() const
const_ref_iterator< const strings_table_t & > it_const_strings_table
Iterator that outputs const COFF::String&.
Definition PE/Binary.hpp:140
const DataDirectory * delay_dir() const
Definition PE/Binary.hpp:859
ref_iterator< relocations_t &, Relocation * > it_relocations
Iterator that outputs Relocation&.
Definition PE/Binary.hpp:88
static bool classof(const LIEF::Binary *bin)
Definition PE/Binary.hpp:926
it_symbols symbols()
Return binary Symbols.
Definition PE/Binary.hpp:400
Section * import_section()
Definition PE/Binary.hpp:467
void accept(Visitor &visitor) const override
Method so that a visitor can visit us.
LIEF::Binary::functions_t functions() const
All functions found in the binary
LIEF::Binary::functions_t exception_functions() const
Functions found in the Exception table directory.
void patch_address(uint64_t address, const std::vector< uint8_t > &patch_value, VA_TYPES addr_type=VA_TYPES::AUTO) override
Patch the content at virtual address address with patch_value.
const Section * section_from_offset(uint64_t offset) const
uint32_t compute_checksum() const
Re-compute the value of OptionalHeader::checksum. If both values do not match, it could mean that the...
std::unique_ptr< Builder > write(std::ostream &os, const Builder::config_t &config)
bool remove_import(std::string_view name)
Remove the imported library with the given name.
friend class Builder
Definition PE/Binary.hpp:61
COFF::String * find_coff_string(uint32_t offset)
Try to find the COFF string at the given offset in the COFF string table.
Definition PE/Binary.hpp:421
ResourceNode * set_resources(std::unique_ptr< ResourceNode > root)
std::vector< COFF::String > strings_table_t
Internal container for storing strings.
Definition PE/Binary.hpp:134
uint64_t offset_to_rva(uint64_t offset) const
Convert the given offset into a relative virtual address (RVA).
~Binary() override
const_ref_iterator< const relocations_t &, const Relocation * > it_const_relocations
Iterator that outputs const Relocation&.
Definition PE/Binary.hpp:91
span< uint8_t > overlay()
Definition PE/Binary.hpp:568
const_ref_iterator< const sections_t &, const Section * > it_const_sections
Iterator that outputs const Section& object.
Definition PE/Binary.hpp:72
uint64_t overlay_offset() const
Return the original overlay offset.
Definition PE/Binary.hpp:573
bool has_delay_import(std::string_view import_name) const
True if the binary delay-imports the given library name
Definition PE/Binary.hpp:659
Section * section_from_rva(uint64_t virtual_address)
Find the section that encompasses the given RVA.
Definition PE/Binary.hpp:212
Section * add_section(const Section &section)
Add a section to the binary and return the section added.
const Section * import_section() const
Return the section associated with import table or a nullptr if the binary does not have an import ta...
const Export * get_export() const
Definition PE/Binary.hpp:393
ref_iterator< debug_entries_t &, Debug * > it_debug_entries
Iterator that outputs Debug&.
Definition PE/Binary.hpp:117
PE_TYPE type() const
Return PE32 or PE32+.
Definition PE/Binary.hpp:165
bool has_tls() const
Check if the current binary has a TLS object.
Definition PE/Binary.hpp:284
Export & set_export(const Export &export_table)
DataDirectory * data_directory(DataDirectory::TYPES type)
Return the DataDirectory with the given type (or index).
Definition PE/Binary.hpp:513
const DelayImport * get_delay_import(std::string_view import_name) const
Section * section_from_offset(uint64_t offset)
Find the section that encompasses the given offset.
Definition PE/Binary.hpp:202
bool has_exceptions() const
Check if the current binary has exceptions.
Definition PE/Binary.hpp:318
it_delay_imports delay_imports()
Return an iterator over the binary's delay imports.
Definition PE/Binary.hpp:639
it_const_symbols symbols() const
Definition PE/Binary.hpp:404
const RichHeader * rich_header() const
Definition PE/Binary.hpp:596
ref_iterator< exceptions_t &, ExceptionInfo * > it_exceptions
Iterator that outputs ExceptionInfo&.
Definition PE/Binary.hpp:155
bool is_reproducible_build() const
Check if the current binary is reproducible build, replacing timestamps by a compile hash.
uint64_t virtual_size() const override
Compute the binary's virtual size. It should match OptionalHeader::sizeof_image.
std::unique_ptr< Builder > write(const std::string &filename, const Builder::config_t &config)
Reconstruct the binary object with the given configuration and write it in filename.
DataDirectory * debug_dir()
Return the data directory associated with the debug table.
Definition PE/Binary.hpp:819
uint64_t va_to_offset(uint64_t VA) const
Convert the absolute virtual address into an offset.
Definition PE/Binary.hpp:177
std::vector< std::unique_ptr< DataDirectory > > data_directories_t
Internal container for storing PE's DataDirectory.
Definition PE/Binary.hpp:75
const DataDirectory * relocation_dir() const
Definition PE/Binary.hpp:814
Binary * nested_pe_binary()
Definition PE/Binary.hpp:919
DosHeader & dos_header()
Return a reference to the PE::DosHeader object.
Definition PE/Binary.hpp:230
friend class Parser
Definition PE/Binary.hpp:60
it_const_relocations relocations() const
Definition PE/Binary.hpp:493
const Section * section_from_rva(uint64_t virtual_address) const
Import & add_import(std::string_view name, int32_t pos=-1)
Add an imported library (i.e. DLL) to the binary.
Definition PE/Binary.hpp:667
DataDirectory * iat_dir()
Return the data directory associated with the IAT.
Definition PE/Binary.hpp:846
DataDirectory * cert_dir()
Return the data directory associated with the certificate table (authenticode).
Definition PE/Binary.hpp:801
bool has_nx() const override
Check if the binary uses NX protection.
Definition PE/Binary.hpp:757
span< const uint8_t > dos_stub() const
Return the DOS stub content.
Definition PE/Binary.hpp:578
const COFF::String * find_coff_string(uint32_t offset) const
Definition PE/Binary.hpp:429
Header & header()
Return a reference to the PE::Header object.
Definition PE/Binary.hpp:239
it_relocations relocations()
Return an iterator over the PE's Relocation.
Definition PE/Binary.hpp:489
uint64_t imagebase() const override
Return binary's imagebase. 0 if not relevant.
Definition PE/Binary.hpp:195
bool has_configuration() const
Check if the current binary has a load configuration.
Definition PE/Binary.hpp:338
const CodeViewPDB * codeview_pdb() const
Return the CodeViewPDB object if present.
const TLS * tls() const
Definition PE/Binary.hpp:276
ResourceNode * resources()
Return resources as a tree or a nullptr if there is no resources.
Definition PE/Binary.hpp:434
ResourceNode * set_resources(const ResourceNode &root)
Change or set the current resource tree with the new one provided in parameter.
bool has(DataDirectory::TYPES type) const
Check if the current binary has the given DataDirectory::TYPES.
Definition PE/Binary.hpp:522
LoadConfiguration * load_configuration()
Definition PE/Binary.hpp:559
uint32_t sizeof_headers() const
Compute the size of all the headers.
const_ref_iterator< const debug_entries_t &, const Debug * > it_const_debug_entries
Iterator that outputs const Debug&.
Definition PE/Binary.hpp:120
bool remove_debug(const Debug &entry)
Remove a specific debug entry.
Section * get_section(std::string_view name)
Return binary's section from its name. If the section can't be found, return a nullptr.
Definition PE/Binary.hpp:457
DataDirectory * load_config_dir()
Return the data directory associated with the load config.
Definition PE/Binary.hpp:837
const Binary * nested_pe_binary() const
If the current binary contains dynamic relocations (e.g. LIEF::PE::DynamicFixupARM64X),...
Definition PE/Binary.hpp:915
std::vector< std::unique_ptr< Debug > > debug_entries_t
Internal container for storing Debug information.
Definition PE/Binary.hpp:114
std::unique_ptr< Builder > write(const std::string &filename)
Reconstruct the binary object and write the raw PE in filename.
Definition PE/Binary.hpp:687
it_strings_table coff_string_table()
Definition PE/Binary.hpp:413
it_const_signatures signatures() const
Return an iterator over the Signature object(s) if the binary is signed.
Definition PE/Binary.hpp:349
it_debug_entries debug()
Return an iterator over the Debug entries.
Definition PE/Binary.hpp:527
it_const_sections sections() const
Definition PE/Binary.hpp:225
it_const_strings_table coff_string_table() const
Iterator over the strings located in the COFF string table.
Definition PE/Binary.hpp:409
ref_iterator< imports_t &, Import * > it_imports
Iterator that outputs Import&.
Definition PE/Binary.hpp:98
bool has_imports() const
Check if the current binary contains imports.
Definition PE/Binary.hpp:294
ExceptionInfo * find_exception_at(uint32_t rva)
Try to find the exception info at the given RVA.
void remove(const Section &section, bool clear=false)
Remove the given section.
const Header & header() const
Definition PE/Binary.hpp:243
const DataDirectory * cert_dir() const
Definition PE/Binary.hpp:805
it_data_directories data_directories()
Return an iterator over the DataDirectory present in the Binary.
Definition PE/Binary.hpp:504
it_sections sections()
Return an iterator over the PE's Section.
Definition PE/Binary.hpp:221
void remove_all_imports()
Remove all libraries in the binary.
Definition PE/Binary.hpp:682
Export * get_export()
Return the Export object.
Definition PE/Binary.hpp:389
span< uint8_t > dos_stub()
Definition PE/Binary.hpp:582
DataDirectory * import_dir()
Return the data directory associated with the import table.
Definition PE/Binary.hpp:773
ref_iterator< delay_imports_t &, DelayImport * > it_delay_imports
Iterator that outputs DelayImport&.
Definition PE/Binary.hpp:107
Relocation & add_relocation(const Relocation &relocation)
Add a new PE Relocation.
const_ref_iterator< const exceptions_t &, const ExceptionInfo * > it_const_exceptions
Iterator that outputs const ExceptionInfo&.
Definition PE/Binary.hpp:158
result< uint64_t > get_function_address(std::string_view name) const override
Attempt to resolve the address of the function specified by name.
RichHeader * rich_header()
Return a reference to the RichHeader object.
Definition PE/Binary.hpp:592
const DosHeader & dos_header() const
Definition PE/Binary.hpp:234
CodeView PDB specialization.
Definition CodeViewPDB.hpp:37
Class that represents a PE data directory entry.
Definition DataDirectory.hpp:42
TYPES
Definition DataDirectory.hpp:51
@ EXPORT_TABLE
Definition DataDirectory.hpp:52
@ DELAY_IMPORT_DESCRIPTOR
Definition DataDirectory.hpp:65
@ RESOURCE_TABLE
Definition DataDirectory.hpp:54
@ BASE_RELOCATION_TABLE
Definition DataDirectory.hpp:57
@ EXCEPTION_TABLE
Definition DataDirectory.hpp:55
@ IAT
Definition DataDirectory.hpp:64
@ LOAD_CONFIG_TABLE
Definition DataDirectory.hpp:62
@ CERTIFICATE_TABLE
Definition DataDirectory.hpp:56
@ DEBUG_DIR
Definition DataDirectory.hpp:58
@ IMPORT_TABLE
Definition DataDirectory.hpp:53
@ TLS_TABLE
Definition DataDirectory.hpp:61
This class represents a generic entry in the debug data directory. For known types,...
Definition debug/Debug.hpp:40
Class that represents a PE delayed import.
Definition DelayImport.hpp:38
Class which represents the DosHeader, the first structure present at the beginning of a PE file.
Definition DosHeader.hpp:38
This class is the base class for any exception or runtime function entry.
Definition ExceptionInfo.hpp:33
Class which represents a PE Export.
Definition Export.hpp:41
This factory is used to create PE from scratch.
Definition Factory.hpp:29
Class that represents the PE header (which follows the DosHeader).
Definition PE/Header.hpp:36
Class that represents a PE import.
Definition Import.hpp:41
This class represents the load configuration data associated with the IMAGE_LOAD_CONFIG_DIRECTORY.
Definition LoadConfiguration.hpp:49
Class which represents the PE OptionalHeader structure.
Definition OptionalHeader.hpp:43
@ DYNAMIC_BASE
DLL can be relocated at load time.
Definition OptionalHeader.hpp:53
@ NX_COMPAT
Image is NX compatible.
Definition OptionalHeader.hpp:59
Main interface to parse PE binaries. In particular, the static Parser::parse functions should be used...
Definition PE/Parser.hpp:54
Class which represents the Base Relocation Block We usually find this structure in the ....
Definition PE/Relocation.hpp:43
Class which represents a Data Node in the PE resources tree.
Definition ResourceData.hpp:36
Definition ResourceDirectory.hpp:35
Class which represents a Node in the resource tree.
Definition ResourceNode.hpp:47
Class which represents the not-so-documented rich header.
Definition RichHeader.hpp:37
Class which represents a PE section.
Definition PE/Section.hpp:47
Main interface for the PKCS #7 signature scheme.
Definition Signature.hpp:42
VERIFICATION_CHECKS
Flags to tweak the verification process of the signature.
Definition Signature.hpp:98
@ DEFAULT
Default behavior that tries to follow the Microsoft verification process as close as possible.
Definition Signature.hpp:101
VERIFICATION_FLAGS
Flags returned by the verification functions.
Definition Signature.hpp:72
Class which represents the PE Thread Local Storage.
Definition TLS.hpp:44
Definition Visitor.hpp:212
Iterator which returns reference on container's values.
Definition iterators.hpp:121
Wrapper that contains an Object (T) or an error.
Definition errors.hpp:79
#define LIEF_LIFETIMEBOUND
Definition compiler_attributes.hpp:72
Namespace related to the LIEF's PE module.
Definition Abstract/Header.hpp:32
ALGORITHMS
Cryptography algorithms.
Definition PE/enums.hpp:28
PE_TYPE
Definition PE/enums.hpp:22
tcb::span< ElementType, Extent > span
Definition span.hpp:22
ref_iterator< CT, U, typename decay_t< CT >::const_iterator > const_ref_iterator
Iterator which returns a const ref on container's values.
Definition iterators.hpp:386
This structure is used to configure the build operation.
Definition PE/Builder.hpp:53
#define LIEF_API
Definition visibility.h:45
#define LIEF_LOCAL
Definition visibility.h:46