LIEF: Library to Instrument Executable Formats Version 1.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 "LIEF/PE/Header.hpp"
21#include "LIEF/PE/DosHeader.hpp"
22#include "LIEF/PE/Import.hpp"
25#include "LIEF/PE/Builder.hpp"
28
29#include "LIEF/COFF/Symbol.hpp"
30#include "LIEF/COFF/String.hpp"
31
33
34#include "LIEF/visibility.h"
35
36namespace LIEF {
37
39namespace PE {
40class ExceptionInfo;
41class CodeViewPDB;
42class Debug;
43class Export;
45class Parser;
46class Relocation;
47class ResourceData;
49class ResourceNode;
50class RichHeader;
51class TLS;
52class Factory;
53
57 friend class Parser;
58 friend class Builder;
59 friend class Factory;
60
61 public:
63 using sections_t = std::vector<std::unique_ptr<Section>>;
64
67
70
72 using data_directories_t = std::vector<std::unique_ptr<DataDirectory>>;
73
76
80
82 using relocations_t = std::vector<std::unique_ptr<Relocation>>;
83
86
90
92 using imports_t = std::vector<std::unique_ptr<Import>>;
93
96
99
101 using delay_imports_t = std::vector<std::unique_ptr<DelayImport>>;
102
105
109
111 using debug_entries_t = std::vector<std::unique_ptr<Debug>>;
112
115
119
121 using symbols_t = std::vector<std::unique_ptr<COFF::Symbol>>;
122
125
129
131 using strings_table_t = std::vector<COFF::String>;
132
135
138
140 using signatures_t = std::vector<Signature>;
141
144
147
149 using exceptions_t = std::vector<std::unique_ptr<ExceptionInfo>>;
150
153
157
159 ~Binary() override;
160
162 PE_TYPE type() const {
163 return type_;
164 }
165
170 uint64_t rva_to_offset(uint64_t RVA) const;
171
174 uint64_t va_to_offset(uint64_t VA) const {
175 uint64_t rva = VA - optional_header().imagebase();
176 return rva_to_offset(rva);
177 }
178
184 uint64_t slide = 0) const override;
185
187 uint64_t offset_to_rva(uint64_t offset) const;
188
192 uint64_t imagebase() const override {
193 return optional_header().imagebase();
194 }
195
200 return const_cast<Section*>(
201 static_cast<const Binary*>(this)->section_from_offset(offset)
202 );
203 }
204 const Section* section_from_offset(uint64_t offset) const LIEF_LIFETIMEBOUND;
205
209 Section* section_from_rva(uint64_t virtual_address) LIEF_LIFETIMEBOUND {
210 return const_cast<Section*>(
211 static_cast<const Binary*>(this)->section_from_rva(virtual_address)
212 );
213 }
214 const Section*
215 section_from_rva(uint64_t virtual_address) const LIEF_LIFETIMEBOUND;
216
219 return sections_;
220 }
221
223 return sections_;
224 }
225
228 return dos_header_;
229 }
230
232 return dos_header_;
233 }
234
237 return header_;
238 }
239
241 return header_;
242 }
243
247 return optional_header_;
248 }
249
251 return optional_header_;
252 }
253
259 uint32_t compute_checksum() const;
260
263 uint64_t virtual_size() const override;
264
266 uint32_t sizeof_headers() const;
267
270 return tls_.get();
271 }
272
273 const TLS* tls() const LIEF_LIFETIMEBOUND {
274 return tls_.get();
275 }
276
279
281 bool has_tls() const {
282 return tls_ != nullptr;
283 }
284
287
291 bool has_imports() const {
292 return !imports_.empty();
293 }
294
298 bool has_signatures() const {
299 return !signatures_.empty();
300 }
301
305 bool has_exports() const {
306 return export_ != nullptr;
307 }
308
310 bool has_resources() const {
311 return resources_ != nullptr;
312 }
313
315 bool has_exceptions() const {
316 if (const DataDirectory* dir = exceptions_dir()) {
317 return dir->size() > 0;
318 }
319 return false;
320 }
321
325 bool has_relocations() const {
326 return !relocations_.empty();
327 }
328
330 bool has_debug() const {
331 return !debug_.empty();
332 }
333
335 bool has_configuration() const {
336 return loadconfig_ != nullptr;
337 }
338
344
347 return signatures_;
348 }
349
351 return signatures_;
352 }
353
366
380
383 std::vector<uint8_t> authentihash(ALGORITHMS algo) const;
384
387 return export_.get();
388 }
389
391 return export_.get();
392 }
393
395
398 return symbols_;
399 }
400
402 return symbols_;
403 }
404
407 return strings_table_;
408 }
409
411 return strings_table_;
412 }
413
419 auto it = std::find_if(strings_table_.begin(), strings_table_.end(),
420 [offset](const COFF::String& item) {
421 return offset == item.offset();
422 });
423 return it == strings_table_.end() ? nullptr : &*it;
424 }
425
426 const COFF::String* find_coff_string(uint32_t offset) const LIEF_LIFETIMEBOUND {
427 return const_cast<Binary*>(this)->find_coff_string(offset);
428 }
429
432 return resources_.get();
433 }
434
436 return resources_.get();
437 }
438
442
444 set_resources(std::unique_ptr<ResourceNode> root) LIEF_LIFETIMEBOUND;
445
449
454 Section* get_section(const std::string& name) LIEF_LIFETIMEBOUND {
455 return const_cast<Section*>(
456 static_cast<const Binary*>(this)->get_section(name)
457 );
458 }
459 const Section* get_section(const std::string& name) const LIEF_LIFETIMEBOUND;
460
463 const Section* import_section() const;
465 return const_cast<Section*>(
466 static_cast<const Binary*>(this)->import_section()
467 );
468 }
469
475 void remove_section(const std::string& name, bool clear = false) override;
476
480 void remove(const Section& section, bool clear = false);
481
484
487 return relocations_;
488 }
489
491 return relocations_;
492 }
493
496
499
502 return data_directories_;
503 }
504
506 return data_directories_;
507 }
508
511 return const_cast<DataDirectory*>(
512 static_cast<const Binary*>(this)->data_directory(type)
513 );
514 }
515 const DataDirectory*
517
520 return data_directory(type) != nullptr;
521 }
522
525 return debug_;
526 }
527
529 return debug_;
530 }
531
534
536 bool remove_debug(const Debug& entry);
537
540
543
547 return loadconfig_.get();
548 }
549
551 return loadconfig_.get();
552 }
553
556 return overlay_;
557 }
558
560 return overlay_;
561 }
562
564 uint64_t overlay_offset() const {
565 return overlay_offset_;
566 }
567
570 return dos_stub_;
571 }
572
574 return dos_stub_;
575 }
576
578 void dos_stub(std::vector<uint8_t> content) {
579 dos_stub_ = std::move(content);
580 }
581
584 return rich_header_.get();
585 }
586
588 return rich_header_.get();
589 }
590
593
595 bool has_rich_header() const {
596 return rich_header_ != nullptr;
597 }
598
601 return imports_;
602 }
603
605 return imports_;
606 }
607
611 Import* get_import(const std::string& import_name) LIEF_LIFETIMEBOUND {
612 return const_cast<Import*>(
613 static_cast<const Binary*>(this)->get_import(import_name)
614 );
615 }
616
617 const Import*
618 get_import(const std::string& import_name) const LIEF_LIFETIMEBOUND;
619
621 bool has_import(const std::string& import_name) const {
622 return get_import(import_name) != nullptr;
623 }
624
626 bool has_delay_imports() const {
627 return !delay_imports_.empty();
628 }
629
632 return delay_imports_;
633 }
634
636 return delay_imports_;
637 }
638
642 get_delay_import(const std::string& import_name) LIEF_LIFETIMEBOUND {
643 return const_cast<DelayImport*>(
644 static_cast<const Binary*>(this)->get_delay_import(import_name)
645 );
646 }
647 const DelayImport*
648 get_delay_import(const std::string& import_name) const LIEF_LIFETIMEBOUND;
649
650
652 bool has_delay_import(const std::string& import_name) const {
653 return get_delay_import(import_name) != nullptr;
654 }
655
660 Import& add_import(const std::string& name,
661 int32_t pos = -1) LIEF_LIFETIMEBOUND {
662 if (pos < 0) {
663 imports_.push_back(std::unique_ptr<Import>(new Import(name)));
664 return *imports_.back();
665 }
666 assert(pos >= 0);
667 return **imports_.insert(imports_.begin() + pos,
668 std::unique_ptr<Import>(new Import(name)));
669 }
670
674 bool remove_import(const std::string& name);
675
678 imports_.clear();
679 }
680
682 std::unique_ptr<Builder> write(const std::string& filename) {
683 return write(filename, Builder::config_t());
684 }
685
688 std::unique_ptr<Builder> write(const std::string& filename,
689 const Builder::config_t& config);
690
695 std::unique_ptr<Builder> write(std::ostream& os) {
696 return write(os, Builder::config_t());
697 }
698
699 std::unique_ptr<Builder> write(std::ostream& os,
700 const Builder::config_t& config);
701
702 void accept(Visitor& visitor) const override;
703
709 void patch_address(uint64_t address, const std::vector<uint8_t>& patch_value,
710 VA_TYPES addr_type = VA_TYPES::AUTO) override;
711
712
720 void patch_address(uint64_t address, uint64_t patch_value,
721 size_t size = sizeof(uint64_t),
722 VA_TYPES addr_type = VA_TYPES::AUTO) override;
723
724
726 void fill_address(uint64_t address, size_t size, uint8_t value = 0,
727 VA_TYPES addr_type = VA_TYPES::AUTO);
728
736 uint64_t virtual_address, uint64_t size,
738 ) const override;
739
742 uint64_t entrypoint() const override {
743 return optional_header_.imagebase() + optional_header_.addressof_entrypoint();
744 }
745
747 bool is_pie() const override {
748 return optional_header_.has(OptionalHeader::DLL_CHARACTERISTICS::DYNAMIC_BASE);
749 }
750
752 bool has_nx() const override {
753 return optional_header_.has(OptionalHeader::DLL_CHARACTERISTICS::NX_COMPAT);
754 }
755
756 uint64_t last_section_offset() const;
757
762
766
771
775
780
784
789
793
799
803
808
812
817
821
826
830
835
839
844
848
853
857
862
865
868
875 return exceptions_;
876 }
877
879 return exceptions_;
880 }
881
888
890 return const_cast<Binary*>(this)->find_exception_at(rva);
891 }
892
894 bool is_arm64ec() const;
895
898 bool is_arm64x() const;
899
910 const Binary* nested_pe_binary() const {
911 return nested_.get();
912 }
913
915 return nested_.get();
916 }
917
919 result<uint64_t> get_function_address(const std::string& name) const override;
920
921 static bool classof(const LIEF::Binary* bin) {
922 return bin->format() == Binary::FORMATS::PE;
923 }
924
925 std::ostream& print(std::ostream& os) const override;
926
927
931 LIEF_LOCAL std::unique_ptr<Binary> move_nested_pe_binary() {
932 auto ret = std::move(nested_);
933 nested_ = nullptr;
934 return ret;
935 }
936
937 private:
938 struct sizing_info_t {
939 uint32_t nb_tls_callbacks = 0;
940 uint32_t load_config_size = 0;
941 };
942
945 result<uint64_t> make_space_for_new_section();
946
948 LIEF::Binary::symbols_t get_abstract_symbols() override;
949
950 LIEF::Header get_abstract_header() const override {
951 return LIEF::Header::from(*this);
952 }
953
955 LIEF::Binary::sections_t get_abstract_sections() override;
956
957 LIEF::Binary::relocations_t get_abstract_relocations() override;
958
959 LIEF::Binary::functions_t get_abstract_exported_functions() const override;
960 LIEF::Binary::functions_t get_abstract_imported_functions() const override;
961 std::vector<std::string> get_abstract_imported_libraries() const override;
962
963 void update_lookup_address_table_offset();
964 void update_iat();
965 void shift(uint64_t from, uint64_t by);
966
967 PE_TYPE type_ = PE_TYPE::PE32_PLUS;
968 DosHeader dos_header_;
969 Header header_;
970 OptionalHeader optional_header_;
971
972 int32_t available_sections_space_ = 0;
973
974 signatures_t signatures_;
975 sections_t sections_;
976 data_directories_t data_directories_;
977 symbols_t symbols_;
978 strings_table_t strings_table_;
979 relocations_t relocations_;
980 imports_t imports_;
981 delay_imports_t delay_imports_;
982 debug_entries_t debug_;
983 exceptions_t exceptions_;
984 uint64_t overlay_offset_ = 0;
985 std::vector<uint8_t> overlay_;
986 std::vector<uint8_t> dos_stub_;
987 std::vector<uint8_t> section_offset_padding_;
988
989 std::unique_ptr<RichHeader> rich_header_;
990 std::unique_ptr<Export> export_;
991 std::unique_ptr<ResourceNode> resources_;
992 std::unique_ptr<TLS> tls_;
993 std::unique_ptr<LoadConfiguration> loadconfig_;
994 std::unique_ptr<Binary> nested_;
995
996 sizing_info_t sizing_info_;
997};
998
999}
1000}
1001#endif
Generic interface representing a binary executable.
Definition Abstract/Binary.hpp:59
std::vector< Function > functions_t
Definition Abstract/Binary.hpp:82
@ PE
Definition Abstract/Binary.hpp:77
FORMATS format() const
Executable format (ELF, PE, Mach-O) of the underlying binary.
Definition Abstract/Binary.hpp:124
std::vector< Symbol * > symbols_t
Internal container.
Definition Abstract/Binary.hpp:94
VA_TYPES
Enumeration of virtual address types used for patching and memory access.
Definition Abstract/Binary.hpp:62
@ AUTO
Automatically determine if the address is absolute or relative (default behavior).
Definition Abstract/Binary.hpp:65
std::vector< Section * > sections_t
Internal container.
Definition Abstract/Binary.hpp:85
std::vector< Relocation * > relocations_t
Internal container.
Definition Abstract/Binary.hpp:103
This class represents a string located in the COFF string table.
Definition String.hpp:34
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:246
ref_iterator< sections_t &, Section * > it_sections
Iterator that outputs Section& object.
Definition PE/Binary.hpp:66
const DataDirectory * export_dir() const
Definition PE/Binary.hpp:763
std::vector< std::unique_ptr< DelayImport > > delay_imports_t
Internal container for storing PE's DelayImport.
Definition PE/Binary.hpp:101
void remove_all_relocations()
Remove all the relocations.
DataDirectory * export_dir()
Return the data directory associated with the export table.
Definition PE/Binary.hpp:759
it_exceptions exceptions()
Iterator over the exception (_RUNTIME_FUNCTION) functions.
Definition PE/Binary.hpp:874
std::unique_ptr< Builder > write(std::ostream &os)
Reconstruct the binary object and write the raw PE in os stream.
Definition PE/Binary.hpp:695
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:75
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:546
std::ostream & print(std::ostream &os) const override
it_const_delay_imports delay_imports() const
Definition PE/Binary.hpp:635
it_const_debug_entries debug() const
Definition PE/Binary.hpp:528
void remove_section(const std::string &name, bool clear=false) override
Delete the section with the given name.
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:747
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:786
std::vector< std::unique_ptr< Section > > sections_t
Internal container for storing PE's Section.
Definition PE/Binary.hpp:63
const_ref_iterator< const imports_t &, const Import * > it_const_imports
Iterator that outputs const Import&.
Definition PE/Binary.hpp:98
const_ref_iterator< const delay_imports_t &, const DelayImport * > it_const_delay_imports
Iterator that outputs const DelayImport&.
Definition PE/Binary.hpp:107
void dos_stub(std::vector< uint8_t > content)
Update the DOS stub content.
Definition PE/Binary.hpp:578
DataDirectory * delay_dir()
Return the data directory associated with delayed imports.
Definition PE/Binary.hpp:850
it_imports imports()
Return an iterator over the binary imports.
Definition PE/Binary.hpp:600
bool has_resources() const
Check if the current binary has resources.
Definition PE/Binary.hpp:310
const DataDirectory * import_dir() const
Definition PE/Binary.hpp:772
TLS & tls(const TLS &tls)
Set a TLS object in the current Binary.
const ResourceNode * resources() const
Definition PE/Binary.hpp:435
bool has_debug() const
Check if the current binary contains debug information.
Definition PE/Binary.hpp:330
const DataDirectory * iat_dir() const
Definition PE/Binary.hpp:845
DelayImport * get_delay_import(const std::string &import_name)
Returns the DelayImport matching the given name. If it can't be found, it returns a nullptr.
Definition PE/Binary.hpp:642
it_const_exceptions exceptions() const
Definition PE/Binary.hpp:878
friend class Factory
Definition PE/Binary.hpp:59
const DataDirectory * tls_dir() const
Definition PE/Binary.hpp:827
const DataDirectory * debug_dir() const
Definition PE/Binary.hpp:818
bool has_relocations() const
Check if the current binary has relocations.
Definition PE/Binary.hpp:325
bool has_exports() const
Check if the current binary has exports.
Definition PE/Binary.hpp:305
ref_iterator< strings_table_t & > it_strings_table
Iterator that outputs COFF::String&.
Definition PE/Binary.hpp:134
std::vector< std::unique_ptr< ExceptionInfo > > exceptions_t
Internal container for storing runtime function associated with exceptions.
Definition PE/Binary.hpp:149
std::vector< std::unique_ptr< Relocation > > relocations_t
Internal container for storing PE's Relocation.
Definition PE/Binary.hpp:82
uint64_t rva_to_offset(uint64_t RVA) const
Convert a Relative Virtual Address into an offset.
result< uint64_t > get_function_address(const std::string &name) const override
Attempt to resolve the address of the function specified by name.
result< ResourcesManager > resources_manager() const
Return the ResourcesManager (class to manage resources more easily than the tree one).
bool remove_import(const std::string &name)
Remove the imported library with the given name.
bool clear_debug()
Remove all debug info from the binary.
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:604
const OptionalHeader & optional_header() const
Definition PE/Binary.hpp:250
it_signatures signatures()
Definition PE/Binary.hpp:350
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:555
bool is_arm64x() const
True if this binary is compiled in ARM64X mode (contains both ARM64 and ARM64EC code).
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:777
const_ref_iterator< const data_directories_t &, const DataDirectory * > it_const_data_directories
Iterator that outputs const DataDirectory&.
Definition PE/Binary.hpp:78
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.
const_ref_iterator< const signatures_t & > it_const_signatures
Iterator that outputs const Signature&.
Definition PE/Binary.hpp:146
const_ref_iterator< const symbols_t &, const COFF::Symbol * > it_const_symbols
Iterator that outputs const Symbol&.
Definition PE/Binary.hpp:127
std::vector< std::unique_ptr< Import > > imports_t
Internal container for storing PE's Import.
Definition PE/Binary.hpp:92
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:121
DataDirectory * tls_dir()
Return the data directory associated with TLS.
Definition PE/Binary.hpp:823
const DataDirectory * rsrc_dir() const
Definition PE/Binary.hpp:781
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:143
bool has_rich_header() const
Check if the current binary has a RichHeader object.
Definition PE/Binary.hpp:595
ref_iterator< symbols_t &, COFF::Symbol * > it_symbols
Iterator that outputs Symbol&.
Definition PE/Binary.hpp:124
bool has_signatures() const
Check if the current binary contains signatures.
Definition PE/Binary.hpp:298
bool has_delay_imports() const
Check if the current binary contains delay imports.
Definition PE/Binary.hpp:626
const DataDirectory * load_config_dir() const
Definition PE/Binary.hpp:836
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:805
const DataDirectory * exceptions_dir() const
Definition PE/Binary.hpp:790
TLS * tls()
Return a reference to the TLS object.
Definition PE/Binary.hpp:269
it_const_data_directories data_directories() const
Definition PE/Binary.hpp:505
uint64_t entrypoint() const override
Return the binary's entrypoint (it is the same value as OptionalHeader::addressof_entrypoint).
Definition PE/Binary.hpp:742
std::vector< Signature > signatures_t
Internal container for storing PE's authenticode Signature.
Definition PE/Binary.hpp:140
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:889
const Import * get_import(const std::string &import_name) const
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:137
const DataDirectory * delay_dir() const
Definition PE/Binary.hpp:854
ref_iterator< relocations_t &, Relocation * > it_relocations
Iterator that outputs Relocation&.
Definition PE/Binary.hpp:85
static bool classof(const LIEF::Binary *bin)
Definition PE/Binary.hpp:921
const Section * get_section(const std::string &name) const
it_symbols symbols()
Return binary Symbols.
Definition PE/Binary.hpp:397
Section * import_section()
Definition PE/Binary.hpp:464
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)
friend class Builder
Definition PE/Binary.hpp:58
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:418
ResourceNode * set_resources(std::unique_ptr< ResourceNode > root)
std::vector< COFF::String > strings_table_t
Internal container for storing strings.
Definition PE/Binary.hpp:131
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:88
span< uint8_t > overlay()
Definition PE/Binary.hpp:559
const_ref_iterator< const sections_t &, const Section * > it_const_sections
Iterator that outputs const Section& object.
Definition PE/Binary.hpp:69
uint64_t overlay_offset() const
Return the original overlay offset.
Definition PE/Binary.hpp:564
const DelayImport * get_delay_import(const std::string &import_name) const
Section * section_from_rva(uint64_t virtual_address)
Find the section that encompasses the given RVA.
Definition PE/Binary.hpp:209
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:390
ref_iterator< debug_entries_t &, Debug * > it_debug_entries
Iterator that outputs Debug&.
Definition PE/Binary.hpp:114
PE_TYPE type() const
Return PE32 or PE32+.
Definition PE/Binary.hpp:162
bool has_tls() const
Check if the current binary has a TLS object.
Definition PE/Binary.hpp:281
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:510
Section * section_from_offset(uint64_t offset)
Find the section that encompasses the given offset.
Definition PE/Binary.hpp:199
bool has_exceptions() const
Check if the current binary has exceptions.
Definition PE/Binary.hpp:315
it_delay_imports delay_imports()
Return an iterator over the binary's delay imports.
Definition PE/Binary.hpp:631
it_const_symbols symbols() const
Definition PE/Binary.hpp:401
const RichHeader * rich_header() const
Definition PE/Binary.hpp:587
ref_iterator< exceptions_t &, ExceptionInfo * > it_exceptions
Iterator that outputs ExceptionInfo&.
Definition PE/Binary.hpp:152
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:814
uint64_t va_to_offset(uint64_t VA) const
Convert the absolute virtual address into an offset.
Definition PE/Binary.hpp:174
std::vector< std::unique_ptr< DataDirectory > > data_directories_t
Internal container for storing PE's DataDirectory.
Definition PE/Binary.hpp:72
const DataDirectory * relocation_dir() const
Definition PE/Binary.hpp:809
Binary * nested_pe_binary()
Definition PE/Binary.hpp:914
DosHeader & dos_header()
Return a reference to the PE::DosHeader object.
Definition PE/Binary.hpp:227
friend class Parser
Definition PE/Binary.hpp:57
Import & add_import(const std::string &name, int32_t pos=-1)
Add an imported library (i.e. DLL) to the binary.
Definition PE/Binary.hpp:660
it_const_relocations relocations() const
Definition PE/Binary.hpp:490
const Section * section_from_rva(uint64_t virtual_address) const
DataDirectory * iat_dir()
Return the data directory associated with the IAT.
Definition PE/Binary.hpp:841
DataDirectory * cert_dir()
Return the data directory associated with the certificate table (authenticode).
Definition PE/Binary.hpp:796
bool has_nx() const override
Check if the binary uses NX protection.
Definition PE/Binary.hpp:752
span< const uint8_t > dos_stub() const
Return the DOS stub content.
Definition PE/Binary.hpp:569
const COFF::String * find_coff_string(uint32_t offset) const
Definition PE/Binary.hpp:426
Header & header()
Return a reference to the PE::Header object.
Definition PE/Binary.hpp:236
it_relocations relocations()
Return an iterator over the PE's Relocation.
Definition PE/Binary.hpp:486
uint64_t imagebase() const override
Return binary's imagebase. 0 if not relevant.
Definition PE/Binary.hpp:192
bool has_configuration() const
Check if the current binary has a load configuration.
Definition PE/Binary.hpp:335
const CodeViewPDB * codeview_pdb() const
Return the CodeViewPDB object if present.
const TLS * tls() const
Definition PE/Binary.hpp:273
ResourceNode * resources()
Return resources as a tree or a nullptr if there is no resources.
Definition PE/Binary.hpp:431
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:519
LoadConfiguration * load_configuration()
Definition PE/Binary.hpp:550
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:117
bool remove_debug(const Debug &entry)
Remove a specific debug entry.
DataDirectory * load_config_dir()
Return the data directory associated with the load config.
Definition PE/Binary.hpp:832
const Binary * nested_pe_binary() const
If the current binary contains dynamic relocations (e.g. LIEF::PE::DynamicFixupARM64X),...
Definition PE/Binary.hpp:910
std::vector< std::unique_ptr< Debug > > debug_entries_t
Internal container for storing Debug information.
Definition PE/Binary.hpp:111
Import * get_import(const std::string &import_name)
Return the Import matching the provided name (case sensitive).
Definition PE/Binary.hpp:611
std::unique_ptr< Builder > write(const std::string &filename)
Reconstruct the binary object and write the raw PE in filename.
Definition PE/Binary.hpp:682
it_strings_table coff_string_table()
Definition PE/Binary.hpp:410
it_const_signatures signatures() const
Return an iterator over the Signature object(s) if the binary is signed.
Definition PE/Binary.hpp:346
it_debug_entries debug()
Return an iterator over the Debug entries.
Definition PE/Binary.hpp:524
it_const_sections sections() const
Definition PE/Binary.hpp:222
it_const_strings_table coff_string_table() const
Iterator over the strings located in the COFF string table.
Definition PE/Binary.hpp:406
ref_iterator< imports_t &, Import * > it_imports
Iterator that outputs Import&.
Definition PE/Binary.hpp:95
bool has_delay_import(const std::string &import_name) const
True if the binary delay-imports the given library name
Definition PE/Binary.hpp:652
bool has_imports() const
Check if the current binary contains imports.
Definition PE/Binary.hpp:291
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:240
const DataDirectory * cert_dir() const
Definition PE/Binary.hpp:800
it_data_directories data_directories()
Return an iterator over the DataDirectory present in the Binary.
Definition PE/Binary.hpp:501
it_sections sections()
Return an iterator over the PE's Section.
Definition PE/Binary.hpp:218
void remove_all_imports()
Remove all libraries in the binary.
Definition PE/Binary.hpp:677
Export * get_export()
Return the Export object.
Definition PE/Binary.hpp:386
span< uint8_t > dos_stub()
Definition PE/Binary.hpp:573
bool has_import(const std::string &import_name) const
True if the binary imports the given library name
Definition PE/Binary.hpp:621
DataDirectory * import_dir()
Return the data directory associated with the import table.
Definition PE/Binary.hpp:768
ref_iterator< delay_imports_t &, DelayImport * > it_delay_imports
Iterator that outputs DelayImport&.
Definition PE/Binary.hpp:104
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:155
Section * get_section(const std::string &name)
Return binary's section from its name. If the section can't be found, return a nullptr.
Definition PE/Binary.hpp:454
RichHeader * rich_header()
Return a reference to the RichHeader object.
Definition PE/Binary.hpp:583
const DosHeader & dos_header() const
Definition PE/Binary.hpp:231
CodeView PDB specialization.
Definition CodeViewPDB.hpp:35
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:37
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:39
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:40
This class represents the load configuration data associated with the IMAGE_LOAD_CONFIG_DIRECTORY.
Definition LoadConfiguration.hpp:47
Class which represents the PE OptionalHeader structure.
Definition OptionalHeader.hpp:42
@ DYNAMIC_BASE
DLL can be relocated at load time.
Definition OptionalHeader.hpp:49
@ NX_COMPAT
Image is NX compatible.
Definition OptionalHeader.hpp:51
Main interface to parse PE binaries. In particular, the static Parser::parse functions should be used...
Definition PE/Parser.hpp:52
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:33
Definition ResourceDirectory.hpp:33
Class which represents a Node in the resource tree.
Definition ResourceNode.hpp:46
Class which represents the not-so-documented rich header.
Definition RichHeader.hpp:37
Class which represents a PE section.
Definition PE/Section.hpp:46
Main interface for the PKCS #7 signature scheme.
Definition Signature.hpp:39
VERIFICATION_CHECKS
Flags to tweak the verification process of the signature.
Definition Signature.hpp:93
@ DEFAULT
Definition Signature.hpp:94
VERIFICATION_FLAGS
Flags returned by the verification functions.
Definition Signature.hpp:69
Class which represents the PE Thread Local Storage.
Definition TLS.hpp:43
Definition Visitor.hpp:212
Iterator which returns reference on container's values.
Definition iterators.hpp:45
Wrapper that contains an Object (T) or an error.
Definition errors.hpp:77
#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
LIEF namespace.
Definition Abstract/Binary.hpp:40
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:318
This structure is used to configure the build operation.
Definition PE/Builder.hpp:54
#define LIEF_API
Definition visibility.h:45
#define LIEF_LOCAL
Definition visibility.h:46