LIEF: Library to Instrument Executable Formats Version 0.15.0
Loading...
Searching...
No Matches
Abstract/Binary.hpp
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
21#include "LIEF/types.hpp"
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
28#include "LIEF/Abstract/Header.hpp"
29#include "LIEF/Abstract/Function.hpp"
30
32namespace LIEF {
33class Section;
34class Relocation;
35class Symbol;
36
39class LIEF_API Binary : public Object {
40 public:
41
43 enum class VA_TYPES {
44 AUTO = 0,
45 RVA = 1,
46 VA = 2,
47 };
48
49 enum FORMATS {
50 UNKNOWN = 0,
51 ELF,
52 PE,
53 MACHO,
54 OAT,
55 };
56
57 using functions_t = std::vector<Function>;
58
60 using sections_t = std::vector<Section*>;
61
64
67
69 using symbols_t = std::vector<Symbol*>;
70
73
76
78 using relocations_t = std::vector<Relocation*>;
79
82
85
86 public:
87 Binary();
88 Binary(FORMATS fmt) :
89 format_{fmt}
90 {}
91
92 ~Binary() override;
93
94 Binary& operator=(const Binary&);
95 Binary(const Binary&);
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
122 it_const_sections sections() const;
123
125 virtual void remove_section(const std::string& name, bool clear = false) = 0;
126
129 it_const_relocations relocations() const;
130
132 virtual uint64_t entrypoint() const = 0;
133
135 uint64_t original_size() const {
136 return original_size_;
137 }
138
140 functions_t exported_functions() const;
141
143 std::vector<std::string> imported_libraries() const;
144
146 functions_t imported_functions() const;
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
214 protected:
215 FORMATS format_ = FORMATS::UNKNOWN;
216
217 uint64_t original_size_ = 0;
218
219 // These functions need to be overloaded by the object that claims to extend this Abstract Binary
220 virtual Header get_abstract_header() const = 0;
221 virtual symbols_t get_abstract_symbols() = 0;
222 virtual sections_t get_abstract_sections() = 0;
223 virtual relocations_t get_abstract_relocations() = 0;
224
225 virtual functions_t get_abstract_exported_functions() const = 0;
226 virtual functions_t get_abstract_imported_functions() const = 0;
227 virtual std::vector<std::string> get_abstract_imported_libraries() const = 0;
228};
229
230LIEF_API const char* to_string(Binary::VA_TYPES e);
231LIEF_API const char* to_string(Binary::FORMATS e);
232
233}
234
235
236#endif
Abstract binary that exposes an uniform API for the different executable file formats.
Definition Abstract/Binary.hpp:39
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.
functions_t imported_functions() const
Return functions imported by the binary.
virtual uint64_t entrypoint() const =0
Binary's entrypoint (if any)
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.
FORMATS format() const
Executable format (ELF, PE, Mach-O) of the underlying binary.
Definition Abstract/Binary.hpp:98
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:69
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:43
std::vector< Section * > sections_t
Internal container.
Definition Abstract/Binary.hpp:60
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 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.
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.
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.
std::vector< Relocation * > relocations_t
Internal container.
Definition Abstract/Binary.hpp:78
virtual bool has_nx() const =0
Check if the binary uses NX protection.
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:30
This class represents a symbol in an executable format.
Definition Abstract/Symbol.hpp:28
Definition Visitor.hpp:219
Iterator which returns reference on container's values.
Definition iterators.hpp:48
LIEF namespace.
Definition Abstract/Binary.hpp:32
tl::expected< T, lief_errors > result
Wrapper that contains an Object (T) or an error.
Definition errors.hpp:72