LIEF: Library to Instrument Executable Formats Version 0.16.0
|
Loading...
Searching...
No Matches
Go to the documentation of this file.
16#ifndef LIEF_PE_BINARY_H
17#define LIEF_PE_BINARY_H
56 public:
using sections_t = std::vector<std::unique_ptr<Section>>;
59 using it_sections = ref_iterator<sections_t&, Section*>;
62 using it_const_sections = const_ref_iterator<const sections_t&, const Section*>;
65 using data_directories_t = std::vector<std::unique_ptr<DataDirectory>>;
68 using it_data_directories = ref_iterator<data_directories_t&, DataDirectory*>;
71 using it_const_data_directories = const_ref_iterator<const data_directories_t&, const DataDirectory*>;
74 using relocations_t = std::vector<std::unique_ptr<Relocation>>;
77 using it_relocations = ref_iterator<relocations_t&, Relocation*>;
80 using it_const_relocations = const_ref_iterator<const relocations_t&, const Relocation*>;
83 using imports_t = std::vector<Import>;
86 using it_imports = ref_iterator<imports_t&>;
89 using it_const_imports = const_ref_iterator<const imports_t&>;
92 using delay_imports_t = std::vector<DelayImport>;
95 using it_delay_imports = ref_iterator<delay_imports_t&>;
98 using it_const_delay_imports = const_ref_iterator<const delay_imports_t&>;
101 using debug_entries_t = std::vector<std::unique_ptr<Debug>>;
104 using it_debug_entries = ref_iterator<debug_entries_t&, Debug*>;
107 using it_const_debug_entries = const_ref_iterator<const debug_entries_t&, const Debug*>;
110 using symbols_t = std::vector<Symbol>;
113 using it_symbols = ref_iterator<symbols_t&>;
116 using it_const_symbols = const_ref_iterator<const symbols_t&>;
119 using strings_table_t = std::vector<std::string>;
122 using it_strings_table = ref_iterator<strings_table_t&>;
125 using it_const_strings_table = const_ref_iterator<const strings_table_t&>;
128 using signatures_t = std::vector<Signature>;
131 using it_signatures = ref_iterator<signatures_t&>;
134 using it_const_signatures = const_ref_iterator<const signatures_t&>;
167 return optional_header().imagebase();
174 return const_cast<Section*
>(
static_cast<const Binary*
>(
this)->section_from_offset(offset));
182 return const_cast<Section*
>(
static_cast<const Binary*
>(
this)->section_from_rva(virtual_address));
216 return optional_header_;
220 return optional_header_;
245 void tls(
const TLS& tls);
251 return tls_ !=
nullptr;
258 return !imports_.empty();
265 return !signatures_.empty();
272 return export_ !=
nullptr;
277 return resources_ !=
nullptr;
282 return has(DataDirectory::TYPES::EXCEPTION_TABLE);
289 return !relocations_.empty();
294 return !debug_.empty();
299 return load_configuration_ !=
nullptr;
324 Signature::VERIFICATION_CHECKS checks = Signature::VERIFICATION_CHECKS::DEFAULT)
const;
336 Signature::VERIFICATION_CHECKS checks = Signature::VERIFICATION_CHECKS::DEFAULT)
const;
337 std::vector<uint8_t>
authentihash(ALGORITHMS algo)
const;
358 return export_.get();
362 return export_.get();
364 std::vector<Symbol>&
symbols() {
376 return resources_.get();
380 return resources_.get();
397 return const_cast<Section*
>(
static_cast<const Binary*
>(
this)->get_section(name));
405 return const_cast<Section*
>(
static_cast<const Binary*
>(
this)->import_section());
407 void remove_section(
const std::string& name,
bool clear =
false)
override;
414 void remove(
const Section& section,
bool clear =
false);
422 PE_SECTION_TYPES type = PE_SECTION_TYPES::UNKNOWN);
441 return data_directories_;
445 return data_directories_;
450 return const_cast<DataDirectory*
>(
static_cast<const Binary*
>(
this)->data_directory(type));
453 bool has(DataDirectory::TYPES type)
const {
456 return data_directory(type) !=
nullptr;
458 it_debug_entries
debug() {
464 it_const_debug_entries
debug()
const {
474 return load_configuration_.get();
478 return load_configuration_.get();
480 span<const uint8_t>
overlay()
const {
492 return overlay_offset_;
494 span<const uint8_t>
dos_stub()
const {
503 void dos_stub(std::vector<uint8_t> content) {
506 dos_stub_ = std::move(content);
514 return rich_header_.get();
518 return rich_header_.get();
526 return rich_header_ !=
nullptr;
537 Import*
get_import(
const std::string& import_name) {
543 return const_cast<Import*
>(
static_cast<const Binary*
>(
this)->get_import(import_name));
545 const Import*
get_import(
const std::string& import_name)
const;
546 bool has_import(
const std::string& import_name)
const {
551 return get_import(import_name) !=
nullptr;
559 return !delay_imports_.empty();
564 return delay_imports_;
568 return delay_imports_;
576 return const_cast<DelayImport*
>(
static_cast<const Binary*
>(
this)->get_delay_import(import_name));
585 return get_delay_import(import_name) !=
nullptr;
598 imports_.emplace_back(name);
599 return imports_.back();
609 void write(
const std::string& filename)
override;
615 void write(std::ostream& os)
override;
622 void accept(Visitor& visitor)
const override;
623 void patch_address(uint64_t address,
const std::vector<uint8_t>& patch_value,
632 void patch_address(uint64_t address, uint64_t patch_value,
size_t size =
sizeof(uint64_t),
648 uint64_t virtual_address, uint64_t size,
649 Binary::VA_TYPES addr_type = Binary::VA_TYPES::AUTO)
const override;
653 return optional_header_.imagebase() + optional_header_.addressof_entrypoint();
655 bool is_pie()
const override {
658 return optional_header_.has(OptionalHeader::DLL_CHARACTERISTICS::DYNAMIC_BASE);
660 bool has_nx()
const override {
663 return optional_header_.has(OptionalHeader::DLL_CHARACTERISTICS::NX_COMPAT);
677 static bool classof(
const LIEF::Binary* bin) {
678 return bin->
format() == Binary::FORMATS::PE;
681 std::ostream&
print(std::ostream& os)
const override;
685 void make_space_for_new_section();
693 LIEF::Header get_abstract_header()
const override {
704 std::vector<std::string> get_abstract_imported_libraries()
const override;
706 void update_lookup_address_table_offset();
709 PE_TYPE type_ = PE_TYPE::PE32_PLUS;
710 DosHeader dos_header_;
712 OptionalHeader optional_header_;
714 int32_t available_sections_space_ = 0;
716 signatures_t signatures_;
717 sections_t sections_;
718 data_directories_t data_directories_;
720 strings_table_t strings_table_;
721 relocations_t relocations_;
723 delay_imports_t delay_imports_;
724 debug_entries_t debug_;
725 uint64_t overlay_offset_ = 0;
726 std::vector<uint8_t> overlay_;
727 std::vector<uint8_t> dos_stub_;
728 std::vector<uint8_t> section_offset_padding_;
730 std::unique_ptr<RichHeader> rich_header_;
731 std::unique_ptr<Export> export_;
732 std::unique_ptr<ResourceNode> resources_;
733 std::unique_ptr<TLS> tls_;
734 std::unique_ptr<LoadConfiguration> load_configuration_;
std::vector< Function > functions_t
Definition Abstract/Binary.hpp:67
FORMATS format() const
Executable format (ELF, PE, Mach-O) of the underlying binary.
Definition Abstract/Binary.hpp:109
std::vector< Symbol * > symbols_t
Internal container.
Definition Abstract/Binary.hpp:79
VA_TYPES
Type of a virtual address.
Definition Abstract/Binary.hpp:53
@ AUTO
Try to guess if it's relative or not.
Definition Abstract/Binary.hpp:54
std::vector< Section * > sections_t
Internal container.
Definition Abstract/Binary.hpp:70
std::vector< Relocation * > relocations_t
Internal container.
Definition Abstract/Binary.hpp:88
Class which represents a PE binary This is the main interface to manage and modify a PE executable.
Definition PE/Binary.hpp:52
OptionalHeader & optional_header()
Header that follows the header(). It is named optional from the COFF specfication but it is mandatory...
Definition PE/Binary.hpp:215
void remove_all_relocations()
Remove all the relocations.
std::vector< uint8_t > authentihash(ALGORITHMS algo) const
Compute the authentihash according to the algorithm provided in the first parameter.
const LoadConfiguration * load_configuration() const
Retrun the LoadConfiguration object or a nullptr if the binary does not use the LoadConfiguration.
Definition PE/Binary.hpp:473
std::ostream & print(std::ostream &os) const override
it_const_delay_imports delay_imports() const
Definition PE/Binary.hpp:567
it_const_debug_entries debug() const
Definition PE/Binary.hpp:464
void remove_section(const std::string &name, bool clear=false) override
Delete the section with the given name.
bool is_pie() const override
Check if the binary is position independent.
Definition PE/Binary.hpp:657
LIEF::Binary::functions_t ctor_functions() const override
Return the list of the binary constructors.
void set_resources(const ResourceDirectory &resource)
Set a new resource tree.
void dos_stub(std::vector< uint8_t > content)
Update the DOS stub content.
Definition PE/Binary.hpp:505
it_imports imports()
Return an iterator over the binary imports.
Definition PE/Binary.hpp:530
bool has_resources() const
Check if the current binary has resources.
Definition PE/Binary.hpp:276
uint32_t predict_function_rva(const std::string &library, const std::string &function)
Try to predict the RVA of the function function in the import library library
std::vector< Symbol > & symbols()
Return binary Symbols.
Definition PE/Binary.hpp:366
const ResourceNode * resources() const
Definition PE/Binary.hpp:379
bool has_debug() const
Check if the current binary contains debug information.
Definition PE/Binary.hpp:293
DelayImport * get_delay_import(const std::string &import_name)
Returns the PE::DelayImport from the given name. If it can't be found, return a nullptr.
Definition PE/Binary.hpp:575
bool has_relocations() const
Check if the current binary has relocations.
Definition PE/Binary.hpp:288
ImportEntry * add_import_function(const std::string &library, const std::string &function)
Add the function function of the library library. If the function fails, it returns a nullptr.
bool has_exports() const
Check if the current binary has exports.
Definition PE/Binary.hpp:271
uint64_t virtual_size() const
Compute the binary's virtual size. It should match OptionalHeader::sizeof_image.
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)
result< uint64_t > offset_to_virtual_address(uint64_t offset, uint64_t slide=0) const override
Convert the given offset into a virtual address.
it_const_imports imports() const
Definition PE/Binary.hpp:534
const OptionalHeader & optional_header() const
Definition PE/Binary.hpp:219
it_signatures signatures()
Definition PE/Binary.hpp:312
void patch_address(uint64_t address, uint64_t patch_value, size_t size=sizeof(uint64_t), LIEF::Binary::VA_TYPES addr_type=LIEF::Binary::VA_TYPES::AUTO) override
Patch the address with the given value.
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...
span< const uint8_t > overlay() const
Return the overlay content.
Definition PE/Binary.hpp:482
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 DataDirectory * data_directory(DataDirectory::TYPES type) const
Section * add_section(const Section §ion, PE_SECTION_TYPES type=PE_SECTION_TYPES::UNKNOWN)
Add a section to the binary and return the section added.
bool has_rich_header() const
Check if the current binary has a RichHeader object.
Definition PE/Binary.hpp:525
bool has_signatures() const
Check if the current binary contains signatures.
Definition PE/Binary.hpp:264
bool has_delay_imports() const
Check if the current binary contains delay imports.
Definition PE/Binary.hpp:558
void set_resources(const ResourceData &resource)
Set a new resource tree.
void write(std::ostream &os) override
Reconstruct the binary object and write the raw PE in os stream.
void rich_header(const RichHeader &rich_header)
Set a RichHeader object in the current Binary.
TLS * tls()
Return a reference to the TLS object.
Definition PE/Binary.hpp:238
it_const_data_directories data_directories() const
Definition PE/Binary.hpp:444
uint64_t entrypoint() const override
Return the binary's entrypoint (It is the same value as OptionalHeader::addressof_entrypoint.
Definition PE/Binary.hpp:652
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 Import * get_import(const std::string &import_name) const
void remove_all_libraries()
Remove all libraries in the binary.
Definition PE/Binary.hpp:606
static bool classof(const LIEF::Binary *bin)
Definition PE/Binary.hpp:677
const Section * get_section(const std::string &name) const
Section * import_section()
Definition PE/Binary.hpp:404
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.
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...
const std::vector< Symbol > & symbols() const
Definition PE/Binary.hpp:370
span< uint8_t > overlay()
Definition PE/Binary.hpp:486
Import & add_library(const std::string &name)
Add an imported library (i.e. DLL) to the binary.
Definition PE/Binary.hpp:597
uint64_t overlay_offset() const
Return the original overlay offset.
Definition PE/Binary.hpp:491
const DelayImport * get_delay_import(const std::string &import_name) const
Section * section_from_rva(uint64_t virtual_address)
Find the section associated that encompasses the given RVA.
Definition PE/Binary.hpp:181
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:361
PE_TYPE type() const
Return PE32 or PE32+
Definition PE/Binary.hpp:143
bool has_tls() const
Check if the current binary has a TLS object.
Definition PE/Binary.hpp:250
DataDirectory * data_directory(DataDirectory::TYPES type)
Return the DataDirectory with the given type (or index)
Definition PE/Binary.hpp:449
void remove_library(const std::string &name)
Remove the library with the given name
Section * section_from_offset(uint64_t offset)
Find the section associated that encompasses the given offset.
Definition PE/Binary.hpp:173
bool has_exceptions() const
Check if the current binary has exceptions.
Definition PE/Binary.hpp:281
it_delay_imports delay_imports()
Return an iterator over the binary's delay imports.
Definition PE/Binary.hpp:563
const RichHeader * rich_header() const
Definition PE/Binary.hpp:517
bool is_reproducible_build() const
Check if the current binary is reproducible build, replacing timestamps by a compile hash.
uint64_t va_to_offset(uint64_t VA) const
Convert the absolute virtual address into an offset.
DosHeader & dos_header()
Return a reference to the PE::DosHeader object.
Definition PE/Binary.hpp:196
it_const_relocations relocations() const
Definition PE/Binary.hpp:429
const Section * section_from_rva(uint64_t virtual_address) const
bool has_nx() const override
Check if the binary uses NX protection.
Definition PE/Binary.hpp:662
span< const uint8_t > dos_stub() const
Return the DOS stub content.
Definition PE/Binary.hpp:496
Header & header()
Return a reference to the PE::Header object.
Definition PE/Binary.hpp:205
it_relocations relocations()
Return an iterator over the PE's Relocation.
Definition PE/Binary.hpp:425
uint64_t imagebase() const override
Return binary's imagebase. 0 if not relevant.
Definition PE/Binary.hpp:166
bool has_configuration() const
Check if the current binary has a load configuration.
Definition PE/Binary.hpp:298
const CodeViewPDB * codeview_pdb() const
Return the CodeViewPDB object if present.
const TLS * tls() const
Definition PE/Binary.hpp:242
ResourceNode * resources()
Return resources as a tree or a nullptr if there is no resources.
Definition PE/Binary.hpp:375
bool has(DataDirectory::TYPES type) const
Check if the current binary has the given DataDirectory::TYPES.
Definition PE/Binary.hpp:455
LoadConfiguration * load_configuration()
Definition PE/Binary.hpp:477
uint32_t sizeof_headers() const
Compute the size of all the headers.
void tls(const TLS &tls)
Set a TLS object in the current Binary.
Import * get_import(const std::string &import_name)
Returns the PE::Import from the given name. If it can't be found, return a nullptr.
Definition PE/Binary.hpp:542
it_const_signatures signatures() const
Return an iterator over the Signature object(s) if the binary is signed.
Definition PE/Binary.hpp:308
it_debug_entries debug()
Return an iterator over the Debug entries.
Definition PE/Binary.hpp:460
it_const_sections sections() const
Definition PE/Binary.hpp:191
void patch_address(uint64_t address, const std::vector< uint8_t > &patch_value, LIEF::Binary::VA_TYPES addr_type=LIEF::Binary::VA_TYPES::AUTO) override
Patch the content at virtual address address with patch_value.
bool has_delay_import(const std::string &import_name) const
True if the binary delay-imports the given library name
Definition PE/Binary.hpp:584
bool has_imports() const
Check if the current binary contains imports.
Definition PE/Binary.hpp:257
void remove(const Section §ion, bool clear=false)
Remove the given section.
const Header & header() const
Definition PE/Binary.hpp:209
it_data_directories data_directories()
Return an iterator over the DataDirectory present in the Binary.
Definition PE/Binary.hpp:440
it_sections sections()
Return an iterator over the PE's Section.
Definition PE/Binary.hpp:187
Export * get_export()
Return the Export object.
Definition PE/Binary.hpp:357
span< uint8_t > dos_stub()
Definition PE/Binary.hpp:500
void write(const std::string &filename) override
Reconstruct the binary object and write the raw PE in filename
bool has_import(const std::string &import_name) const
True if the binary imports the given library name
Definition PE/Binary.hpp:550
Relocation & add_relocation(const Relocation &relocation)
Add a PE::Relocation.
Section * get_section(const std::string &name)
Return binary's section from its name. If the secion can't be found, return a nullptr.
Definition PE/Binary.hpp:396
RichHeader * rich_header()
Return a reference to the RichHeader object.
Definition PE/Binary.hpp:513
const DosHeader & dos_header() const
Definition PE/Binary.hpp:200
Class that is used to rebuild a raw PE binary from a PE::Binary object.
Definition PE/Builder.hpp:45
CodeView PDB specialization.
Definition CodeViewPDB.hpp:34
This class represents a generic entry in the debug data directory. For known types,...
Definition debug/Debug.hpp:38
Class which represents a PE Export.
Definition Export.hpp:38
Class that represents the default PE's LoadConfiguration
Definition LoadConfiguration.hpp:35
Main interface to parse PE binaries. In particular the static functions: Parser::parse should be used...
Definition PE/Parser.hpp:47
Class which represents the Base Relocation Block We usually find this structure in the ....
Definition PE/Relocation.hpp:37
Class which represents a Data Node in the PE resources tree.
Definition ResourceData.hpp:32
Definition ResourceDirectory.hpp:33
Class which represents a Node in the resource tree.
Definition ResourceNode.hpp:36
Class which represents the PE Thread Local Storage.
Definition TLS.hpp:42
Namespace related to the LIEF's PE module.
Definition Abstract/Header.hpp:32
@ TLS
Definition PE/enums.hpp:668
PE_TYPE
Definition PE/enums.hpp:680
LIEF namespace.
Definition Abstract/Binary.hpp:36
#define LIEF_API
Definition visibility.h:41