LIEF: Library to Instrument Executable Formats Version 0.17.0
Loading...
Searching...
No Matches
Abstract/Binary.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2025 R. Thomas
2 * Copyright 2017 - 2025 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_ABSTRACT_BINARY_H
17#define LIEF_ABSTRACT_BINARY_H
18
19#include <vector>
20#include <memory>
21#include <unordered_map>
22
23#include "LIEF/visibility.h"
24#include "LIEF/Object.hpp"
25#include "LIEF/iterators.hpp"
26#include "LIEF/errors.hpp"
27#include "LIEF/span.hpp"
28
31
34
35namespace llvm {
36class MCInst;
37}
38
40namespace LIEF {
41class Section;
42class Relocation;
43class Symbol;
44
45class DebugInfo;
46
47namespace assembly {
48class Engine;
49}
50
53class LIEF_API Binary : public Object {
54 public:
55
57 enum class VA_TYPES {
58 AUTO = 0,
59 RVA = 1,
60 VA = 2,
61 };
62
70
71 using functions_t = std::vector<Function>;
72
74 using sections_t = std::vector<Section*>;
75
78
81
83 using symbols_t = std::vector<Symbol*>;
84
87
90
92 using relocations_t = std::vector<Relocation*>;
93
96
99
102
103 public:
106
107 ~Binary() override;
108
109 Binary& operator=(const Binary&) = delete;
110 Binary(const Binary&) = delete;
111
113 FORMATS format() const {
114 return format_;
115 }
116
118 Header header() const {
119 return get_abstract_header();
120 }
121
124 return get_abstract_symbols();
125 }
126
129 return const_cast<Binary*>(this)->get_abstract_symbols();
130 }
131
133 bool has_symbol(const std::string& name) const {
134 return get_symbol(name) != nullptr;
135 }
136
139 const Symbol* get_symbol(const std::string& name) const;
140
141 Symbol* get_symbol(const std::string& name) {
142 return const_cast<Symbol*>(static_cast<const Binary*>(this)->get_symbol(name));
143 }
144
147 return get_abstract_sections();
148 }
149
151 return const_cast<Binary*>(this)->get_abstract_sections();
152 }
153
155 virtual void remove_section(const std::string& name, bool clear = false) = 0;
156
159 return get_abstract_relocations();
160 }
161
163 return const_cast<Binary*>(this)->get_abstract_relocations();
164 }
165
167 virtual uint64_t entrypoint() const = 0;
168
170 uint64_t original_size() const {
171 return original_size_;
172 }
173
176 return get_abstract_exported_functions();
177 }
178
180 std::vector<std::string> imported_libraries() const {
181 return get_abstract_imported_libraries();
182 }
183
186 return get_abstract_imported_functions();
187 }
188
190 virtual result<uint64_t> get_function_address(const std::string& func_name) const;
191
193 void accept(Visitor& visitor) const override;
194
195 std::vector<uint64_t> xref(uint64_t address) const;
196
203 virtual void patch_address(uint64_t address, const std::vector<uint8_t>& patch_value,
204 VA_TYPES addr_type = VA_TYPES::AUTO) = 0;
205
212 virtual void patch_address(uint64_t address, uint64_t patch_value, size_t size = sizeof(uint64_t),
213 VA_TYPES addr_type = VA_TYPES::AUTO) = 0;
214
216 virtual span<const uint8_t>
217 get_content_from_virtual_address(uint64_t virtual_address, uint64_t size,
218 VA_TYPES addr_type = VA_TYPES::AUTO) const = 0;
219
221 template<class T>
223 uint64_t va, VA_TYPES addr_type = VA_TYPES::AUTO) const
224 {
225 T value;
226 static_assert(std::is_integral<T>::value, "Require an integral type");
227 span<const uint8_t> raw = get_content_from_virtual_address(va, sizeof(T), addr_type);
228 if (raw.empty() || raw.size() < sizeof(T)) {
230 }
231
232 std::copy(raw.data(), raw.data() + sizeof(T),
233 reinterpret_cast<uint8_t*>(&value));
234 return value;
235 }
236
242 void original_size(uint64_t size) {
243 original_size_ = size;
244 }
245
247 virtual bool is_pie() const = 0;
248
250 virtual bool has_nx() const = 0;
251
253 virtual uint64_t imagebase() const = 0;
254
256 virtual functions_t ctor_functions() const = 0;
257
262 virtual result<uint64_t> offset_to_virtual_address(uint64_t offset, uint64_t slide = 0) const = 0;
263
264 virtual std::ostream& print(std::ostream& os) const {
265 return os;
266 }
267
268 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Binary& binary) {
269 binary.print(os);
270 return os;
271 }
272
287
299 instructions_it disassemble(uint64_t address, size_t size) const;
300
311 instructions_it disassemble(uint64_t address) const;
312
323 instructions_it disassemble(const std::string& function) const;
324
329 instructions_it disassemble(const uint8_t* buffer, size_t size,
330 uint64_t address = 0) const;
331
332
337 instructions_it disassemble(const std::vector<uint8_t>& buffer,
338 uint64_t address = 0) const {
339 return disassemble(buffer.data(), buffer.size(), address);
340 }
341
343 uint64_t address = 0) const {
344 return disassemble(buffer.data(), buffer.size(), address);
345 }
346
347 instructions_it disassemble(LIEF::span<uint8_t> buffer, uint64_t address = 0) const {
348 return disassemble(buffer.data(), buffer.size(), address);
349 }
350
364 std::vector<uint8_t> assemble(uint64_t address, const std::string& Asm,
366
371 std::vector<uint8_t> assemble(uint64_t address, const llvm::MCInst& inst);
372
377 std::vector<uint8_t> assemble(uint64_t address,
378 const std::vector<llvm::MCInst>& insts);
379
382 virtual uint64_t page_size() const;
383
384 protected:
385 FORMATS format_ = FORMATS::UNKNOWN;
386 mutable std::unique_ptr<DebugInfo> debug_info_;
387 mutable std::unordered_map<uint32_t, std::unique_ptr<assembly::Engine>> engines_;
388 uint64_t original_size_ = 0;
389
390 assembly::Engine* get_engine(uint64_t address) const;
391
392 template<uint32_t Key, class F>
393 LIEF_LOCAL assembly::Engine* get_cache_engine(uint64_t address, F&& f) const;
394
395 // These functions need to be overloaded by the object that claims to extend this Abstract Binary
396 virtual Header get_abstract_header() const = 0;
397 virtual symbols_t get_abstract_symbols() = 0;
398 virtual sections_t get_abstract_sections() = 0;
399 virtual relocations_t get_abstract_relocations() = 0;
400
401 virtual functions_t get_abstract_exported_functions() const = 0;
402 virtual functions_t get_abstract_imported_functions() const = 0;
403 virtual std::vector<std::string> get_abstract_imported_libraries() const = 0;
404};
405
408
409}
410
411
412#endif
Header header() const
Return the abstract header of the binary.
Definition Abstract/Binary.hpp:118
ref_iterator< symbols_t > it_symbols
Iterator that outputs LIEF::Symbol&.
Definition Abstract/Binary.hpp:86
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:71
functions_t imported_functions() const
Return functions imported by the binary.
Definition Abstract/Binary.hpp:185
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:98
FORMATS
Definition Abstract/Binary.hpp:63
@ UNKNOWN
Definition Abstract/Binary.hpp:64
@ ELF
Definition Abstract/Binary.hpp:65
@ OAT
Definition Abstract/Binary.hpp:68
@ PE
Definition Abstract/Binary.hpp:66
@ MACHO
Definition Abstract/Binary.hpp:67
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:133
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:141
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:222
FORMATS format() const
Executable format (ELF, PE, Mach-O) of the underlying binary.
Definition Abstract/Binary.hpp:113
friend std::ostream & operator<<(std::ostream &os, const Binary &binary)
Definition Abstract/Binary.hpp:268
ref_iterator< sections_t > it_sections
Iterator that outputs LIEF::Section&.
Definition Abstract/Binary.hpp:77
it_const_sections sections() const
Definition Abstract/Binary.hpp:150
const_ref_iterator< sections_t > it_const_sections
Iterator that outputs const LIEF::Section&.
Definition Abstract/Binary.hpp:80
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:158
it_const_symbols symbols() const
Return an iterator over the abstracted symbols in which the elements can't be modified.
Definition Abstract/Binary.hpp:128
std::vector< Symbol * > symbols_t
Internal container.
Definition Abstract/Binary.hpp:83
~Binary() override
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:242
VA_TYPES
Type of a virtual address.
Definition Abstract/Binary.hpp:57
@ VA
Absolute.
Definition Abstract/Binary.hpp:60
@ RVA
Relative.
Definition Abstract/Binary.hpp:59
@ AUTO
Try to guess if it's relative or not.
Definition Abstract/Binary.hpp:58
instructions_it disassemble(const std::string &function) const
Disassemble code for the given symbol name.
std::vector< uint64_t > xref(uint64_t address) const
std::vector< Section * > sections_t
Internal container.
Definition Abstract/Binary.hpp:74
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:175
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:264
std::vector< uint8_t > assemble(uint64_t address, const std::string &Asm, assembly::AssemblerConfig &config=assembly::AssemblerConfig::default_config())
Assemble and patch the provided assembly code at the specified address.
const_ref_iterator< symbols_t > it_const_symbols
Iterator that outputs const LIEF::Symbol&.
Definition Abstract/Binary.hpp:89
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:123
virtual result< uint64_t > get_function_address(const std::string &func_name) const
Return the address of the given function name.
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:146
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.
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:101
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:180
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
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:170
void accept(Visitor &visitor) const override
Method so that a visitor can visit us.
it_const_relocations relocations() const
Definition Abstract/Binary.hpp:162
ref_iterator< relocations_t > it_relocations
Iterator that outputs LIEF::Relocation&.
Definition Abstract/Binary.hpp:95
std::vector< Relocation * > relocations_t
Internal container.
Definition Abstract/Binary.hpp:92
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
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
Definition Abstract/Header.hpp:40
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
Definition Visitor.hpp:210
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:36
Definition iterators.hpp:486
Iterator which returns reference on container's values.
Definition iterators.hpp:46
@ 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:47
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 return const ref on container's values.
Definition iterators.hpp:257
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
Definition Abstract/Binary.hpp:35
#define LIEF_API
Definition visibility.h:41
#define LIEF_LOCAL
Definition visibility.h:42