LIEF: Library to Instrument Executable Formats Version 2.0.0
Loading...
Searching...
No Matches
Abstract/Binary.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2026 R. Thomas
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15#ifndef LIEF_ABSTRACT_BINARY_H
16#define LIEF_ABSTRACT_BINARY_H
17
18#include <string_view>
19#include <unordered_map>
20#include <memory>
21#include <vector>
22
23#include "LIEF/Object.hpp"
25#include "LIEF/errors.hpp"
26#include "LIEF/iterators.hpp"
27#include "LIEF/span.hpp"
28#include "LIEF/visibility.h"
29
32
35
36namespace llvm {
37class MCInst;
38}
39
41namespace LIEF {
42class Section;
43class Relocation;
44class Symbol;
45
46class DebugInfo;
47
48namespace assembly {
49class Engine;
50}
51
60class LIEF_API Binary : public Object {
61 public:
63 enum class VA_TYPES {
66 AUTO = 0,
67
69 RVA = 1,
70
72 VA = 2,
73 };
74
82
83 using functions_t = std::vector<Function>;
84
86 using sections_t = std::vector<Section*>;
87
90
93
95 using symbols_t = std::vector<Symbol*>;
96
99
102
104 using relocations_t = std::vector<Relocation*>;
105
108
111
114
115 public:
118
119 ~Binary() override;
120
121 Binary& operator=(const Binary&) = delete;
122 Binary(const Binary&) = delete;
123
125 FORMATS format() const {
126 return format_;
127 }
128
130 Header header() const {
131 return get_abstract_header();
132 }
133
137 return get_abstract_symbols();
138 }
139
143 return const_cast<Binary*>(this)->get_abstract_symbols();
144 }
145
147 bool has_symbol(std::string_view name) const {
148 return get_symbol(name) != nullptr;
149 }
150
153 const Symbol* get_symbol(std::string_view name) const LIEF_LIFETIMEBOUND;
154
155 Symbol* get_symbol(std::string_view name) LIEF_LIFETIMEBOUND {
156 return const_cast<Symbol*>(static_cast<const Binary*>(this)->get_symbol(name));
157 }
158
161 return get_abstract_sections();
162 }
163
165 return const_cast<Binary*>(this)->get_abstract_sections();
166 }
167
169 virtual void remove_section(std::string_view name, bool clear = false) = 0;
170
173 return get_abstract_relocations();
174 }
175
177 return const_cast<Binary*>(this)->get_abstract_relocations();
178 }
179
181 virtual uint64_t entrypoint() const = 0;
182
184 uint64_t original_size() const {
185 return original_size_;
186 }
187
190 return get_abstract_exported_functions();
191 }
192
194 std::vector<std::string> imported_libraries() const {
195 return get_abstract_imported_libraries();
196 }
197
200 return get_abstract_imported_functions();
201 }
202
204 virtual result<uint64_t> get_function_address(std::string_view func_name) const;
205
207 void accept(Visitor& visitor) const override;
208
209 std::vector<uint64_t> xref(uint64_t address) const;
210
217 virtual void patch_address(uint64_t address,
218 const std::vector<uint8_t>& patch_value,
219 VA_TYPES addr_type = VA_TYPES::AUTO) = 0;
220
228 virtual void patch_address(uint64_t address, uint64_t patch_value,
229 size_t size = sizeof(uint64_t),
230 VA_TYPES addr_type = VA_TYPES::AUTO) = 0;
231
234 uint64_t virtual_address, uint64_t size, VA_TYPES addr_type = VA_TYPES::AUTO
235 ) const LIEF_LIFETIMEBOUND = 0;
236
238 template<class T>
241 VA_TYPES addr_type = VA_TYPES::AUTO) const {
242 T value;
243 static_assert(std::is_integral_v<T>, "Require an integral type");
245 get_content_from_virtual_address(va, sizeof(T), addr_type);
246 if (raw.empty() || raw.size() < sizeof(T)) {
248 }
249
250 std::copy(raw.data(), raw.data() + sizeof(T),
251 reinterpret_cast<uint8_t*>(&value));
252 return value;
253 }
254
260 void original_size(uint64_t size) {
261 original_size_ = size;
262 }
263
265 virtual bool is_pie() const = 0;
266
268 virtual bool has_nx() const = 0;
269
271 virtual uint64_t imagebase() const = 0;
272
274 virtual functions_t ctor_functions() const = 0;
275
282 uint64_t slide = 0) const = 0;
283
284 virtual std::ostream& print(std::ostream& os) const {
285 return os;
286 }
287
288 LIEF_API friend std::ostream& operator<<(std::ostream& os,
289 const Binary& binary) {
290 binary.print(os);
291 return os;
292 }
293
308
320 instructions_it disassemble(uint64_t address,
321 size_t size) const LIEF_LIFETIMEBOUND;
322
334
345 instructions_it disassemble(std::string_view function) const LIEF_LIFETIMEBOUND;
346
354 size_t size,
355 uint64_t address = 0) const LIEF_LIFETIMEBOUND;
356
357
363 disassemble(const std::vector<uint8_t>& buffer LIEF_LIFETIMEBOUND,
364 uint64_t address = 0) const LIEF_LIFETIMEBOUND {
365 return disassemble(buffer.data(), buffer.size(), address);
366 }
367
369 uint64_t address = 0) const LIEF_LIFETIMEBOUND {
370 return disassemble(buffer.data(), buffer.size(), address);
371 }
372
374 uint64_t address = 0) const LIEF_LIFETIMEBOUND {
375 return disassemble(buffer.data(), buffer.size(), address);
376 }
377
391 std::vector<uint8_t> assemble(uint64_t address, std::string_view Asm,
394
399 std::vector<uint8_t> assemble(uint64_t address, const llvm::MCInst& inst);
400
405 std::vector<uint8_t> assemble(uint64_t address,
406 const std::vector<llvm::MCInst>& insts);
407
410 virtual uint64_t page_size() const;
411
429 DebugInfo* load_debug_info(std::string_view path);
430
432 virtual uint64_t virtual_size() const {
433 return 0;
434 }
435
444 template<class T>
445 const T* cast() const LIEF_LIFETIMEBOUND {
446 static_assert(std::is_base_of_v<LIEF::Binary, T>,
447 "Require Abstract binary inheritance");
448 if (T::classof(this)) {
449 return static_cast<const T*>(this);
450 }
451 return nullptr;
452 }
453
454 template<class T>
456 return const_cast<T*>(static_cast<const Binary*>(this)->cast<T>());
457 }
458
459 protected:
460 FORMATS format_ = FORMATS::UNKNOWN;
461 mutable std::unique_ptr<DebugInfo> debug_info_;
462 mutable std::unordered_map<uint32_t, std::unique_ptr<assembly::Engine>> engines_;
463 uint64_t original_size_ = 0;
464
465 assembly::Engine* get_engine(uint64_t address) const;
466
467 template<uint32_t Key, class F>
468 LIEF_LOCAL assembly::Engine* get_cache_engine(uint64_t address, F&& f) const;
469
470 // These functions need to be overloaded by the object that claims to extend this
471 // Abstract Binary
472 virtual Header get_abstract_header() const = 0;
473 virtual symbols_t get_abstract_symbols() = 0;
474 virtual sections_t get_abstract_sections() = 0;
475 virtual relocations_t get_abstract_relocations() = 0;
476
477 virtual functions_t get_abstract_exported_functions() const = 0;
478 virtual functions_t get_abstract_imported_functions() const = 0;
479 virtual std::vector<std::string> get_abstract_imported_libraries() const = 0;
480};
481
484
485}
486
487
488#endif
Header header() const
Return the abstract header of the binary.
Definition Abstract/Binary.hpp:130
ref_iterator< symbols_t > it_symbols
Iterator that outputs LIEF::Symbol&.
Definition Abstract/Binary.hpp:98
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.
std::vector< Function > functions_t
Definition Abstract/Binary.hpp:83
functions_t imported_functions() const
Return functions imported by the binary.
Definition Abstract/Binary.hpp:199
virtual uint64_t entrypoint() const =0
Binary's entrypoint (if any).
const_ref_iterator< relocations_t > it_const_relocations
Iterator that outputs const LIEF::Relocation&.
Definition Abstract/Binary.hpp:110
const Symbol * get_symbol(std::string_view name) const
Return the Symbol with the given name If the symbol does not exist, return a nullptr.
FORMATS
Definition Abstract/Binary.hpp:75
@ UNKNOWN
Definition Abstract/Binary.hpp:76
@ ELF
Definition Abstract/Binary.hpp:77
@ OAT
Definition Abstract/Binary.hpp:80
@ PE
Definition Abstract/Binary.hpp:78
@ MACHO
Definition Abstract/Binary.hpp:79
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.
Symbol * get_symbol(std::string_view name)
Definition Abstract/Binary.hpp:155
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:240
FORMATS format() const
Executable format (ELF, PE, Mach-O) of the underlying binary.
Definition Abstract/Binary.hpp:125
friend std::ostream & operator<<(std::ostream &os, const Binary &binary)
Definition Abstract/Binary.hpp:288
ref_iterator< sections_t > it_sections
Iterator that outputs LIEF::Section&.
Definition Abstract/Binary.hpp:89
it_const_sections sections() const
Definition Abstract/Binary.hpp:164
const_ref_iterator< sections_t > it_const_sections
Iterator that outputs const LIEF::Section&.
Definition Abstract/Binary.hpp:92
it_relocations relocations()
Return an iterator over the binary relocations (LIEF::Relocation).
Definition Abstract/Binary.hpp:172
it_const_symbols symbols() const
Return an iterator over the abstracted symbols in which the elements can't be modified.
Definition Abstract/Binary.hpp:142
std::vector< Symbol * > symbols_t
Internal container.
Definition Abstract/Binary.hpp:95
~Binary() override
instructions_it disassemble(uint64_t address, size_t size) const
Disassemble code starting at the given virtual address and with the given size.
virtual uint64_t virtual_size() const
Size of the binary when mapped in memory.
Definition Abstract/Binary.hpp:432
bool has_symbol(std::string_view name) const
Check if a Symbol with the given name exists.
Definition Abstract/Binary.hpp:147
virtual void remove_section(std::string_view name, bool clear=false)=0
Remove all the sections in the underlying binary.
void original_size(uint64_t size)
Change binary's original size.
Definition Abstract/Binary.hpp:260
const T * cast() const
This function can be used to down cast a Binary:
Definition Abstract/Binary.hpp:445
VA_TYPES
Enumeration of virtual address types used for patching and memory access.
Definition Abstract/Binary.hpp:63
@ AUTO
Automatically determine if the address is absolute or relative (default behavior).
Definition Abstract/Binary.hpp:66
std::vector< uint64_t > xref(uint64_t address) const
std::vector< Section * > sections_t
Internal container.
Definition Abstract/Binary.hpp:86
virtual bool is_pie() const =0
Check if the binary is position independent.
T * cast()
Definition Abstract/Binary.hpp:455
functions_t exported_functions() const
Return the functions exported by the binary.
Definition Abstract/Binary.hpp:189
virtual uint64_t page_size() const
Get the default memory page size according to the architecture and the format of the current binary.
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:284
const_ref_iterator< symbols_t > it_const_symbols
Iterator that outputs const LIEF::Symbol&.
Definition Abstract/Binary.hpp:101
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:136
std::vector< uint8_t > assemble(uint64_t address, const std::vector< llvm::MCInst > &insts)
Assemble and patch the address with the given LLVM MCInst.
it_sections sections()
Return an iterator over the binary's sections (LIEF::Section).
Definition Abstract/Binary.hpp:160
virtual functions_t ctor_functions() const =0
Constructor functions that are called prior any other functions.
virtual result< uint64_t > get_function_address(std::string_view func_name) const
Return the address of the given function name.
std::vector< uint8_t > assemble(uint64_t address, std::string_view Asm, assembly::AssemblerConfig &config=assembly::AssemblerConfig::default_config())
Assemble and patch the provided assembly code at the specified address.
DebugInfo * load_debug_info(std::string_view path)
Load and associate an external debug file (e.g., DWARF or PDB) with this binary.
std::vector< uint8_t > assemble(uint64_t address, const llvm::MCInst &inst)
Assemble and patch the address with the given LLVM MCInst.
Binary & operator=(const Binary &)=delete
iterator_range< assembly::Instruction::Iterator > instructions_it
Instruction iterator.
Definition Abstract/Binary.hpp:113
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:194
Binary(FORMATS fmt)
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:184
void accept(Visitor &visitor) const override
Method so that a visitor can visit us.
it_const_relocations relocations() const
Definition Abstract/Binary.hpp:176
ref_iterator< relocations_t > it_relocations
Iterator that outputs LIEF::Relocation&.
Definition Abstract/Binary.hpp:107
std::vector< Relocation * > relocations_t
Internal container.
Definition Abstract/Binary.hpp:104
instructions_it disassemble(LIEF::span< uint8_t > buffer, uint64_t address=0) const
Definition Abstract/Binary.hpp:373
Binary(const Binary &)=delete
instructions_it disassemble(LIEF::span< const uint8_t > buffer, uint64_t address=0) const
Definition Abstract/Binary.hpp:368
virtual bool has_nx() const =0
Check if the binary uses NX protection.
This class provides a generic interface for accessing debug information from different formats such a...
Definition Abstract/DebugInfo.hpp:38
Definition Abstract/Header.hpp:39
Definition Object.hpp:24
Class which represents an abstracted Relocation.
Definition Abstract/Relocation.hpp:26
Class which represents an abstracted section.
Definition Abstract/Section.hpp:30
This class represents a symbol in an executable format.
Definition Abstract/Symbol.hpp:29
Definition Visitor.hpp:211
This class exposes the different elements that can be configured to assemble code.
Definition AssemblerConfig.hpp:29
static AssemblerConfig & default_config()
Default configuration.
Definition AssemblerConfig.hpp:50
This class interfaces the assembler/disassembler support.
Definition Engine.hpp:38
Definition iterators.hpp:680
Iterator which returns reference on container's values.
Definition iterators.hpp:120
Wrapper that contains an Object (T) or an error.
Definition errors.hpp:78
#define LIEF_LIFETIMEBOUND
Definition compiler_attributes.hpp:72
@ read_error
Definition errors.hpp:24
tl::unexpected< lief_errors > make_error_code(lief_errors e)
Create a standard error code from lief_errors.
Definition errors.hpp:54
Namespace related to assembly/disassembly support.
Definition Abstract/Binary.hpp:48
LIEF namespace.
Definition Abstract/Binary.hpp:41
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:385
const char * to_string(Binary::VA_TYPES e)
Definition Abstract/Binary.hpp:36
#define LIEF_API
Definition visibility.h:44
#define LIEF_LOCAL
Definition visibility.h:45