LIEF: Library to Instrument Executable Formats Version 0.15.1
Loading...
Searching...
No Matches
Abstract/Binary.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2024 R. Thomas
2 * Copyright 2017 - 2024 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
22#include "LIEF/visibility.h"
23#include "LIEF/Object.hpp"
24#include "LIEF/iterators.hpp"
25#include "LIEF/errors.hpp"
26#include "LIEF/span.hpp"
27
30
32namespace LIEF {
33class Section;
34class Relocation;
35class Symbol;
36
37class DebugInfo;
38
41class LIEF_API Binary : public Object {
42 public:
43
45 enum class VA_TYPES {
46 AUTO = 0,
47 RVA = 1,
48 VA = 2,
49 };
50
51 enum FORMATS {
52 UNKNOWN = 0,
57 };
58
59 using functions_t = std::vector<Function>;
60
62 using sections_t = std::vector<Section*>;
63
66
69
71 using symbols_t = std::vector<Symbol*>;
72
75
78
80 using relocations_t = std::vector<Relocation*>;
81
84
87
88 public:
91
92 ~Binary() override;
93
94 Binary& operator=(const Binary&) = delete;
95 Binary(const Binary&) = delete;
96
98 FORMATS format() const {
99 return format_;
100 }
101
103 Header header() const;
104
107
110
112 bool has_symbol(const std::string& name) const;
113
116 const Symbol* get_symbol(const std::string& name) const;
117
118 Symbol* get_symbol(const std::string& name);
119
123
125 virtual void remove_section(const std::string& name, bool clear = false) = 0;
126
130
132 virtual uint64_t entrypoint() const = 0;
133
135 uint64_t original_size() const {
136 return original_size_;
137 }
138
141
143 std::vector<std::string> imported_libraries() const;
144
147
149 virtual result<uint64_t> get_function_address(const std::string& func_name) const;
150
152 void accept(Visitor& visitor) const override;
153
154 std::vector<uint64_t> xref(uint64_t address) const;
155
162 virtual void patch_address(uint64_t address, const std::vector<uint8_t>& patch_value,
163 VA_TYPES addr_type = VA_TYPES::AUTO) = 0;
164
171 virtual void patch_address(uint64_t address, uint64_t patch_value, size_t size = sizeof(uint64_t),
172 VA_TYPES addr_type = VA_TYPES::AUTO) = 0;
173
175 virtual span<const uint8_t>
176 get_content_from_virtual_address(uint64_t virtual_address, uint64_t size,
177 VA_TYPES addr_type = VA_TYPES::AUTO) const = 0;
178
184 void original_size(uint64_t size) {
185 original_size_ = size;
186 }
187
189 virtual bool is_pie() const = 0;
190
192 virtual bool has_nx() const = 0;
193
195 virtual uint64_t imagebase() const = 0;
196
198 virtual functions_t ctor_functions() const = 0;
199
204 virtual result<uint64_t> offset_to_virtual_address(uint64_t offset, uint64_t slide = 0) const = 0;
205
206 virtual std::ostream& print(std::ostream& os) const;
207
209 virtual void write(const std::string& name) = 0;
210 virtual void write(std::ostream& os) = 0;
211
212 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Binary& binary);
213
228
229 protected:
230 FORMATS format_ = FORMATS::UNKNOWN;
231 mutable std::unique_ptr<DebugInfo> debug_info_;
232 uint64_t original_size_ = 0;
233
234 // These functions need to be overloaded by the object that claims to extend this Abstract Binary
235 virtual Header get_abstract_header() const = 0;
236 virtual symbols_t get_abstract_symbols() = 0;
237 virtual sections_t get_abstract_sections() = 0;
238 virtual relocations_t get_abstract_relocations() = 0;
239
240 virtual functions_t get_abstract_exported_functions() const = 0;
241 virtual functions_t get_abstract_imported_functions() const = 0;
242 virtual std::vector<std::string> get_abstract_imported_libraries() const = 0;
243};
244
247
248}
249
250
251#endif
Abstract binary that exposes an uniform API for the different executable file formats.
Definition Abstract/Binary.hpp:41
Header header() const
Return the abstract header of the binary.
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:59
functions_t imported_functions() const
Return functions imported by the binary.
virtual uint64_t entrypoint() const =0
Binary's entrypoint (if any)
FORMATS
Definition Abstract/Binary.hpp:51
@ ELF
Definition Abstract/Binary.hpp:53
@ OAT
Definition Abstract/Binary.hpp:56
@ PE
Definition Abstract/Binary.hpp:54
@ MACHO
Definition Abstract/Binary.hpp:55
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.
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)
FORMATS format() const
Executable format (ELF, PE, Mach-O) of the underlying binary.
Definition Abstract/Binary.hpp:98
friend std::ostream & operator<<(std::ostream &os, const Binary &binary)
it_const_sections sections() const
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)
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.
std::vector< Symbol * > symbols_t
Internal container.
Definition Abstract/Binary.hpp:71
~Binary() override
void original_size(uint64_t size)
Change binary's original size.
Definition Abstract/Binary.hpp:184
VA_TYPES
Type of a virtual address.
Definition Abstract/Binary.hpp:45
std::vector< uint64_t > xref(uint64_t address) const
std::vector< Section * > sections_t
Internal container.
Definition Abstract/Binary.hpp:62
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.
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
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.
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)
virtual functions_t ctor_functions() const =0
Constructor functions that are called prior any other functions.
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.
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:135
void accept(Visitor &visitor) const override
Method so that a visitor can visit us.
it_const_relocations relocations() const
std::vector< Relocation * > relocations_t
Internal container.
Definition Abstract/Binary.hpp:80
Binary(const Binary &)=delete
virtual void write(std::ostream &os)=0
virtual bool has_nx() const =0
Check if the binary uses NX protection.
Definition Abstract/DebugInfo.hpp:24
Definition Abstract/Header.hpp:29
Definition Object.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
Definition Visitor.hpp:224
Iterator which returns reference on container's values.
Definition iterators.hpp:48
LIEF namespace.
Definition Abstract/Binary.hpp:32
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:74
#define LIEF_API
Definition visibility.h:41