LIEF: Library to Instrument Executable Formats Version 0.16.0
|
Loading...
Searching...
No Matches
Go to the documentation of this file.
16#ifndef LIEF_ABSTRACT_BINARY_H
17#define LIEF_ABSTRACT_BINARY_H
21#include <unordered_map>
67 using functions_t = std::vector<Function>;
68 using sections_t = std::vector<Section*>;
71 using it_sections = ref_iterator<sections_t>;
74 using it_const_sections = const_ref_iterator<sections_t>;
77 using symbols_t = std::vector<Symbol*>;
80 using it_symbols = ref_iterator<symbols_t>;
83 using it_const_symbols = const_ref_iterator<symbols_t>;
86 using relocations_t = std::vector<Relocation*>;
89 using it_relocations = ref_iterator<relocations_t>;
92 using it_const_relocations = const_ref_iterator<relocations_t>;
95 using instructions_it = iterator_range<assembly::Instruction::Iterator>;
115 return get_abstract_header();
120 return get_abstract_symbols();
122 it_const_symbols
symbols()
const {
125 return const_cast<Binary*
>(
this)->get_abstract_symbols();
127 bool has_symbol(
const std::string& name)
const {
130 return get_symbol(name) !=
nullptr;
132 const Symbol*
get_symbol(
const std::string& name)
const;
138 return const_cast<Symbol*
>(
static_cast<const Binary*
>(
this)->get_symbol(name));
143 return get_abstract_sections();
147 return const_cast<Binary*
>(
this)->get_abstract_sections();
149 virtual void remove_section(
const std::string& name,
bool clear =
false) = 0;
155 return get_abstract_relocations();
159 return const_cast<Binary*
>(
this)->get_abstract_relocations();
167 return original_size_;
172 return get_abstract_exported_functions();
177 return get_abstract_imported_libraries();
182 return get_abstract_imported_functions();
187 void accept(Visitor& visitor)
const override;
191 std::vector<uint64_t>
xref(uint64_t address)
const;
192 virtual void patch_address(uint64_t address,
const std::vector<uint8_t>& patch_value,
200 VA_TYPES addr_type = VA_TYPES::AUTO) = 0;
201 virtual void patch_address(uint64_t address, uint64_t patch_value,
size_t size =
sizeof(uint64_t),
209 VA_TYPES addr_type = VA_TYPES::AUTO) = 0;
210 virtual span<const uint8_t>
214 VA_TYPES addr_type = VA_TYPES::AUTO)
const = 0;
219 uint64_t va, VA_TYPES addr_type = VA_TYPES::AUTO)
const
222 static_assert(std::is_integral<T>::value,
"Require an integral type");
223 span<const uint8_t> raw = get_content_from_virtual_address(va,
sizeof(T), addr_type);
224 if (raw.empty() || raw.size() <
sizeof(T)) {
228 std::copy(raw.data(), raw.data() +
sizeof(T),
229 reinterpret_cast<uint8_t*
>(&value));
239 original_size_ = size;
241 virtual bool is_pie()
const = 0;
244 virtual bool has_nx()
const = 0;
260 virtual std::ostream&
print(std::ostream& os)
const {
263 virtual void write(
const std::string& name) = 0;
266 virtual void write(std::ostream& os) = 0;
287 instructions_it
disassemble(uint64_t address,
size_t size)
const;
300 instructions_it
disassemble(uint64_t address)
const;
312 instructions_it
disassemble(
const std::string& function)
const;
324 instructions_it
disassemble(
const uint8_t* buffer,
size_t size,
330 uint64_t address = 0)
const;
332 instructions_it
disassemble(
const std::vector<uint8_t>& buffer,
338 uint64_t address = 0)
const {
339 return disassemble(buffer.data(), buffer.size(), address);
343 uint64_t address = 0)
const {
344 return disassemble(buffer.data(), buffer.size(), address);
348 return disassemble(buffer.data(), buffer.size(), address);
352 FORMATS format_ = FORMATS::UNKNOWN;
353 mutable std::unique_ptr<DebugInfo> debug_info_;
354 mutable std::unordered_map<uint32_t, std::unique_ptr<assembly::Engine>> engines_;
355 uint64_t original_size_ = 0;
357 assembly::Engine* get_engine(uint64_t address)
const;
359 template<u
int32_t Key,
class F>
360 LIEF_LOCAL assembly::Engine* get_cache_engine(uint64_t address, F&& f)
const;
363 virtual Header get_abstract_header()
const = 0;
364 virtual symbols_t get_abstract_symbols() = 0;
365 virtual sections_t get_abstract_sections() = 0;
366 virtual relocations_t get_abstract_relocations() = 0;
368 virtual functions_t get_abstract_exported_functions()
const = 0;
369 virtual functions_t get_abstract_imported_functions()
const = 0;
370 virtual std::vector<std::string> get_abstract_imported_libraries()
const = 0;
Abstract binary that exposes an uniform API for the different executable file formats.
Definition Abstract/Binary.hpp:49
Header header() const
Return the abstract header of the binary.
Definition Abstract/Binary.hpp:114
virtual void patch_address(uint64_t address, const std::vector< uint8_t > &patch_value, VA_TYPES addr_type=VA_TYPES::AUTO)=0
Patch the content at virtual address address with patch_value.
functions_t imported_functions() const
Return functions imported by the binary.
Definition Abstract/Binary.hpp:181
virtual uint64_t entrypoint() const =0
Binary's entrypoint (if any)
FORMATS
Definition Abstract/Binary.hpp:59
virtual result< uint64_t > offset_to_virtual_address(uint64_t offset, uint64_t slide=0) const =0
Convert the given offset into a virtual address.
bool has_symbol(const std::string &name) const
Check if a Symbol with the given name exists.
Definition Abstract/Binary.hpp:129
const Symbol * get_symbol(const std::string &name) const
Return the Symbol with the given name If the symbol does not exist, return a nullptr.
Symbol * get_symbol(const std::string &name)
Definition Abstract/Binary.hpp:137
LIEF::result< T > get_int_from_virtual_address(uint64_t va, VA_TYPES addr_type=VA_TYPES::AUTO) const
Get the integer value at the given virtual address.
Definition Abstract/Binary.hpp:218
FORMATS format() const
Executable format (ELF, PE, Mach-O) of the underlying binary.
Definition Abstract/Binary.hpp:109
friend std::ostream & operator<<(std::ostream &os, const Binary &binary)
Definition Abstract/Binary.hpp:268
it_const_sections sections() const
Definition Abstract/Binary.hpp:146
virtual void remove_section(const std::string &name, bool clear=false)=0
Remove all the sections in the underlying binary.
it_relocations relocations()
Return an iterator over the binary relocation (LIEF::Relocation)
Definition Abstract/Binary.hpp:154
virtual void write(const std::string &name)=0
Build & transform the Binary object representation into a real executable.
it_const_symbols symbols() const
Return an iterator over the abstracted symbols in which the elements can't be modified.
Definition Abstract/Binary.hpp:124
instructions_it disassemble(uint64_t address, size_t size) const
Disassemble code starting a the given virtual address and with the given size.
void original_size(uint64_t size)
Change binary's original size.
Definition Abstract/Binary.hpp:238
VA_TYPES
Type of a virtual address.
Definition Abstract/Binary.hpp:53
instructions_it disassemble(const std::string &function) const
Disassemble code for the given symbol name.
std::vector< uint64_t > xref(uint64_t address) const
virtual bool is_pie() const =0
Check if the binary is position independent.
functions_t exported_functions() const
Return the functions exported by the binary.
Definition Abstract/Binary.hpp:171
virtual span< const uint8_t > get_content_from_virtual_address(uint64_t virtual_address, uint64_t size, VA_TYPES addr_type=VA_TYPES::AUTO) const =0
Return the content located at the given virtual address.
virtual std::ostream & print(std::ostream &os) const
Definition Abstract/Binary.hpp:260
virtual void patch_address(uint64_t address, uint64_t patch_value, size_t size=sizeof(uint64_t), VA_TYPES addr_type=VA_TYPES::AUTO)=0
Patch the address with the given value.
it_symbols symbols()
Return an iterator over the abstracted symbols in which the elements can be modified.
Definition Abstract/Binary.hpp:119
virtual result< uint64_t > get_function_address(const std::string &func_name) const
Return the address of the given function name.
it_sections sections()
Return an iterator over the binary's sections (LIEF::Section)
Definition Abstract/Binary.hpp:142
virtual functions_t ctor_functions() const =0
Constructor functions that are called prior any other functions.
instructions_it disassemble(uint64_t address) const
Disassemble code starting a the given virtual address.
Binary & operator=(const Binary &)=delete
virtual uint64_t imagebase() const =0
Default image base address if the ASLR is not enabled.
std::vector< std::string > imported_libraries() const
Return libraries which are imported by the binary.
Definition Abstract/Binary.hpp:176
instructions_it disassemble(const std::vector< uint8_t > &buffer, uint64_t address=0) const
Disassemble code provided by the given vector of bytes at the specified address parameter.
Definition Abstract/Binary.hpp:337
DebugInfo * debug_info() const
Return the debug info if present. It can be either a LIEF::dwarf::DebugInfo or a LIEF::pdb::DebugInfo...
uint64_t original_size() const
Binary's original size.
Definition Abstract/Binary.hpp:166
void accept(Visitor &visitor) const override
Method so that a visitor can visit us.
it_const_relocations relocations() const
Definition Abstract/Binary.hpp:158
instructions_it disassemble(LIEF::span< uint8_t > buffer, uint64_t address=0) const
Definition Abstract/Binary.hpp:347
Binary(const Binary &)=delete
instructions_it disassemble(LIEF::span< const uint8_t > buffer, uint64_t address=0) const
Definition Abstract/Binary.hpp:342
virtual void write(std::ostream &os)=0
instructions_it disassemble(const uint8_t *buffer, size_t size, uint64_t address=0) const
Disassemble code provided by the given buffer at the specified address parameter.
virtual bool has_nx() const =0
Check if the binary uses NX protection.
Definition Abstract/DebugInfo.hpp:25
Class which represents an abstracted Relocation.
Definition Abstract/Relocation.hpp:27
Class which represents an abstracted section.
Definition Abstract/Section.hpp:29
This class represents a symbol in an executable format.
Definition Abstract/Symbol.hpp:28
This class interfaces the assembler/disassembler support.
Definition Engine.hpp:33
@ read_error
Definition errors.hpp:24
tl::unexpected< lief_errors > make_error_code(lief_errors e)
Create an standard error code from lief_errors.
Definition errors.hpp:52
Namespace related to assembly/disassembly support.
Definition Abstract/Binary.hpp:43
LIEF namespace.
Definition Abstract/Binary.hpp:36
tcb::span< ElementType, Extent > span
Definition span.hpp:22
const char * to_string(Binary::VA_TYPES e)
tl::expected< T, lief_errors > result
Wrapper that contains an Object (T) or an error.
Definition errors.hpp:75
#define LIEF_API
Definition visibility.h:41
#define LIEF_LOCAL
Definition visibility.h:42