16#ifndef LIEF_PE_BINARY_H
17#define LIEF_PE_BINARY_H
41class LoadConfiguration;
45class ResourceDirectory;
58 using sections_t = std::vector<std::unique_ptr<Section>>;
167 return optional_header().imagebase();
212 return optional_header_;
216 return optional_header_;
247 return tls_ !=
nullptr;
254 return !imports_.empty();
261 return !signatures_.empty();
268 return export_ !=
nullptr;
273 return resources_ !=
nullptr;
283 return !relocations_.empty();
288 return !debug_.empty();
293 return load_configuration_ !=
nullptr;
352 return export_.get();
356 return export_.get();
370 return resources_.get();
374 return resources_.get();
431 return data_directories_;
435 return data_directories_;
444 return data_directory(type) !=
nullptr;
462 return load_configuration_.get();
466 return load_configuration_.get();
480 return overlay_offset_;
494 dos_stub_ = std::move(content);
502 return rich_header_.get();
506 return rich_header_.get();
514 return rich_header_ !=
nullptr;
537 return get_import(import_name) !=
nullptr;
545 return !delay_imports_.empty();
550 return delay_imports_;
554 return delay_imports_;
569 return get_delay_import(import_name) !=
nullptr;
582 imports_.emplace_back(name);
583 return imports_.back();
598 void write(
const std::string& filename)
override;
604 void write(std::ostream& os)
override;
613 void patch_address(uint64_t address,
const std::vector<uint8_t>& patch_value,
623 void patch_address(uint64_t address, uint64_t patch_value,
size_t size =
sizeof(uint64_t),
632 uint64_t virtual_address, uint64_t size,
637 return optional_header_.imagebase() + optional_header_.addressof_entrypoint();
642 return optional_header_.has(OptionalHeader::DLL_CHARACTERISTICS::DYNAMIC_BASE);
647 return optional_header_.has(OptionalHeader::DLL_CHARACTERISTICS::NX_COMPAT);
662 return bin->
format() == Binary::FORMATS::PE;
665 std::ostream&
print(std::ostream& os)
const override;
672 void make_space_for_new_section();
686 std::vector<std::string> get_abstract_imported_libraries()
const override;
688 void update_lookup_address_table_offset();
691 PE_TYPE type_ = PE_TYPE::PE32_PLUS;
696 int32_t available_sections_space_ = 0;
707 uint64_t overlay_offset_ = 0;
708 std::vector<uint8_t> overlay_;
709 std::vector<uint8_t> dos_stub_;
710 std::vector<uint8_t> section_offset_padding_;
712 std::unique_ptr<RichHeader> rich_header_;
713 std::unique_ptr<Export> export_;
714 std::unique_ptr<ResourceNode> resources_;
715 std::unique_ptr<TLS> tls_;
716 std::unique_ptr<LoadConfiguration> load_configuration_;
Abstract binary that exposes an uniform API for the different executable file formats.
Definition Abstract/Binary.hpp:41
std::vector< Function > functions_t
Definition Abstract/Binary.hpp:59
FORMATS format() const
Executable format (ELF, PE, Mach-O) of the underlying binary.
Definition Abstract/Binary.hpp:98
std::vector< Symbol * > symbols_t
Internal container.
Definition Abstract/Binary.hpp:71
VA_TYPES
Type of a virtual address.
Definition Abstract/Binary.hpp:45
@ AUTO
Try to guess if it's relative or not.
std::vector< Section * > sections_t
Internal container.
Definition Abstract/Binary.hpp:62
std::vector< Relocation * > relocations_t
Internal container.
Definition Abstract/Binary.hpp:80
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:211
void remove_all_relocations()
Remove all the relocations.
std::vector< DelayImport > delay_imports_t
Internal container for storing PE's DelayImport.
Definition PE/Binary.hpp:94
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:461
std::ostream & print(std::ostream &os) const override
it_const_delay_imports delay_imports() const
Definition PE/Binary.hpp:553
it_const_debug_entries debug() const
Definition PE/Binary.hpp:452
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:641
LIEF::Binary::functions_t ctor_functions() const override
Return the list of the binary constructors.
std::vector< std::unique_ptr< Section > > sections_t
Internal container for storing PE's Section.
Definition PE/Binary.hpp:58
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:493
it_imports imports()
Return an iterator over the binary imports.
Definition PE/Binary.hpp:518
bool has_resources() const
Check if the current binary has resources.
Definition PE/Binary.hpp:272
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:360
const ResourceNode * resources() const
Definition PE/Binary.hpp:373
bool has_debug() const
Check if the current binary contains debug information.
Definition PE/Binary.hpp:287
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.
bool has_relocations() const
Check if the current binary has relocations.
Definition PE/Binary.hpp:282
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:267
uint64_t virtual_size() const
Compute the binary's virtual size. It should match OptionalHeader::sizeof_image.
std::vector< std::unique_ptr< Relocation > > relocations_t
Internal container for storing PE's Relocation.
Definition PE/Binary.hpp:76
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:522
const OptionalHeader & optional_header() const
Definition PE/Binary.hpp:215
it_signatures signatures()
Definition PE/Binary.hpp:306
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:470
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.
std::vector< Symbol > symbols_t
Internal container for storing COFF Symbols.
Definition PE/Binary.hpp:112
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:513
bool has_signatures() const
Check if the current binary contains signatures.
Definition PE/Binary.hpp:260
bool has_delay_imports() const
Check if the current binary contains delay imports.
Definition PE/Binary.hpp:544
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:234
it_const_data_directories data_directories() const
Definition PE/Binary.hpp:434
uint64_t entrypoint() const override
Return the binary's entrypoint (It is the same value as OptionalHeader::addressof_entrypoint.
Definition PE/Binary.hpp:636
std::vector< Signature > signatures_t
Internal container for storing PE's authenticode Signature.
Definition PE/Binary.hpp:130
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:590
static bool classof(const LIEF::Binary *bin)
Definition PE/Binary.hpp:661
const Section * get_section(const std::string &name) const
Section * import_section()
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:364
span< uint8_t > overlay()
Definition PE/Binary.hpp:474
Import & add_library(const std::string &name)
Add an imported library (i.e. DLL) to the binary.
Definition PE/Binary.hpp:581
uint64_t overlay_offset() const
Return the original overlay offset.
Definition PE/Binary.hpp:479
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.
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:355
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:246
DataDirectory * data_directory(DataDirectory::TYPES type)
Return the DataDirectory with the given type (or index)
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.
bool has_exceptions() const
Check if the current binary has exceptions.
it_delay_imports delay_imports()
Return an iterator over the binary's delay imports.
Definition PE/Binary.hpp:549
const RichHeader * rich_header() const
Definition PE/Binary.hpp:505
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.
std::vector< std::unique_ptr< DataDirectory > > data_directories_t
Internal container for storing PE's DataDirectory.
Definition PE/Binary.hpp:67
DosHeader & dos_header()
Return a reference to the PE::DosHeader object.
Definition PE/Binary.hpp:192
it_const_relocations relocations() const
Definition PE/Binary.hpp:419
const Section * section_from_rva(uint64_t virtual_address) const
std::vector< Import > imports_t
Internal container for storing PE's Import.
Definition PE/Binary.hpp:85
bool has_nx() const override
Check if the binary uses NX protection.
Definition PE/Binary.hpp:646
span< const uint8_t > dos_stub() const
Return the DOS stub content.
Definition PE/Binary.hpp:484
Header & header()
Return a reference to the PE::Header object.
Definition PE/Binary.hpp:201
it_relocations relocations()
Return an iterator over the PE's Relocation.
Definition PE/Binary.hpp:415
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:292
const CodeViewPDB * codeview_pdb() const
Return the CodeViewPDB object if present.
const TLS * tls() const
Definition PE/Binary.hpp:238
ResourceNode * resources()
Return resources as a tree or a nullptr if there is no resources.
Definition PE/Binary.hpp:369
bool has(DataDirectory::TYPES type) const
Check if the current binary has the given DataDirectory::TYPES.
Definition PE/Binary.hpp:443
LoadConfiguration * load_configuration()
Definition PE/Binary.hpp:465
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.
std::vector< std::string > strings_table_t
Internal container for storing strings.
Definition PE/Binary.hpp:121
std::vector< std::unique_ptr< Debug > > debug_entries_t
Internal container for storing Debug information.
Definition PE/Binary.hpp:103
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.
it_const_signatures signatures() const
Return an iterator over the Signature object(s) if the binary is signed.
Definition PE/Binary.hpp:302
it_debug_entries debug()
Return an iterator over the Debug entries.
Definition PE/Binary.hpp:448
it_const_sections sections() const
Definition PE/Binary.hpp:187
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:568
bool has_imports() const
Check if the current binary contains imports.
Definition PE/Binary.hpp:253
void remove(const Section §ion, bool clear=false)
Remove the given section.
const Header & header() const
Definition PE/Binary.hpp:205
it_data_directories data_directories()
Return an iterator over the DataDirectory present in the Binary.
Definition PE/Binary.hpp:430
it_sections sections()
Return an iterator over the PE's Section.
Definition PE/Binary.hpp:183
Export * get_export()
Return the Export object.
Definition PE/Binary.hpp:351
span< uint8_t > dos_stub()
Definition PE/Binary.hpp:488
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:536
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.
RichHeader * rich_header()
Return a reference to the RichHeader object.
Definition PE/Binary.hpp:501
const DosHeader & dos_header() const
Definition PE/Binary.hpp:196
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
Class that represents a PE data directory entry.
Definition DataDirectory.hpp:38
TYPES
Definition DataDirectory.hpp:47
Class that represents a PE delayed import.
Definition DelayImport.hpp:36
Class which represents a PE Export.
Definition Export.hpp:38
Class that represents an entry (i.e. an import) in the import table (Import).
Definition ImportEntry.hpp:36
Class that represents a PE import.
Definition Import.hpp:39
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 a PE section.
Definition PE/Section.hpp:40
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:91
VERIFICATION_FLAGS
Flags returned by the verification functions.
Definition Signature.hpp:68
Class which represents the PE Thread Local Storage.
Definition TLS.hpp:42
Definition Visitor.hpp:224
Iterator which returns reference on container's values.
Definition iterators.hpp:48
PE_SECTION_TYPES
Common section type.
Definition PE/enums.hpp:666
ALGORITHMS
Cryptography algorithms.
Definition PE/enums.hpp:686
PE_TYPE
Definition PE/enums.hpp:680
LIEF namespace.
Definition Abstract/Binary.hpp:32
tcb::span< ElementType, Extent > span
Definition span.hpp:22
tl::expected< T, lief_errors > result
Wrapper that contains an Object (T) or an error.
Definition errors.hpp:74
#define LIEF_API
Definition visibility.h:41