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/visibility.h"
22#include "LIEF/Object.hpp"
23#include "LIEF/iterators.hpp"
24#include "LIEF/errors.hpp"
25#include "LIEF/span.hpp"
26
27#include "LIEF/Abstract/Header.hpp"
28#include "LIEF/Abstract/Function.hpp"
29
31namespace LIEF {
32class Section;
33class Relocation;
34class Symbol;
35
38class LIEF_API Binary : public Object {
39 public:
40
42 enum class VA_TYPES {
43 AUTO = 0,
44 RVA = 1,
45 VA = 2,
46 };
47
48 enum FORMATS {
49 UNKNOWN = 0,
50 ELF,
51 PE,
52 MACHO,
53 OAT,
54 };
55
56 using functions_t = std::vector<Function>;
57
59 using sections_t = std::vector<Section*>;
60
63
66
68 using symbols_t = std::vector<Symbol*>;
69
72
75
77 using relocations_t = std::vector<Relocation*>;
78
81
84
85 public:
86 Binary();
87 Binary(FORMATS fmt) :
88 format_{fmt}
89 {}
90
91 ~Binary() override;
92
93 Binary& operator=(const Binary&);
94 Binary(const Binary&);
95
97 FORMATS format() const {
98 return format_;
99 }
100
102 Header header() const;
103
106
109
111 bool has_symbol(const std::string& name) const;
112
115 const Symbol* get_symbol(const std::string& name) const;
116
117 Symbol* get_symbol(const std::string& name);
118
121 it_const_sections sections() const;
122
124 virtual void remove_section(const std::string& name, bool clear = false) = 0;
125
128 it_const_relocations relocations() const;
129
131 virtual uint64_t entrypoint() const = 0;
132
134 uint64_t original_size() const {
135 return original_size_;
136 }
137
139 functions_t exported_functions() const;
140
142 std::vector<std::string> imported_libraries() const;
143
145 functions_t imported_functions() const;
146
148 virtual result<uint64_t> get_function_address(const std::string& func_name) const;
149
151 void accept(Visitor& visitor) const override;
152
153 std::vector<uint64_t> xref(uint64_t address) const;
154
161 virtual void patch_address(uint64_t address, const std::vector<uint8_t>& patch_value,
162 VA_TYPES addr_type = VA_TYPES::AUTO) = 0;
163
170 virtual void patch_address(uint64_t address, uint64_t patch_value, size_t size = sizeof(uint64_t),
171 VA_TYPES addr_type = VA_TYPES::AUTO) = 0;
172
174 virtual span<const uint8_t>
175 get_content_from_virtual_address(uint64_t virtual_address, uint64_t size,
176 VA_TYPES addr_type = VA_TYPES::AUTO) const = 0;
177
183 void original_size(uint64_t size) {
184 original_size_ = size;
185 }
186
188 virtual bool is_pie() const = 0;
189
191 virtual bool has_nx() const = 0;
192
194 virtual uint64_t imagebase() const = 0;
195
197 virtual functions_t ctor_functions() const = 0;
198
203 virtual result<uint64_t> offset_to_virtual_address(uint64_t offset, uint64_t slide = 0) const = 0;
204
205 virtual std::ostream& print(std::ostream& os) const;
206
208 virtual void write(const std::string& name) = 0;
209 virtual void write(std::ostream& os) = 0;
210
211 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Binary& binary);
212
213 protected:
214 FORMATS format_ = FORMATS::UNKNOWN;
215
216 uint64_t original_size_ = 0;
217
218 // These functions need to be overloaded by the object that claims to extend this Abstract Binary
219 virtual Header get_abstract_header() const = 0;
220 virtual symbols_t get_abstract_symbols() = 0;
221 virtual sections_t get_abstract_sections() = 0;
222 virtual relocations_t get_abstract_relocations() = 0;
223
224 virtual functions_t get_abstract_exported_functions() const = 0;
225 virtual functions_t get_abstract_imported_functions() const = 0;
226 virtual std::vector<std::string> get_abstract_imported_libraries() const = 0;
227};
228
229LIEF_API const char* to_string(Binary::VA_TYPES e);
230LIEF_API const char* to_string(Binary::FORMATS e);
231
232}
233
234
235#endif
Abstract binary that exposes an uniform API for the different executable file formats.
Definition Abstract/Binary.hpp:38
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:97
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:68
void original_size(uint64_t size)
Change binary's original size.
Definition Abstract/Binary.hpp:183
VA_TYPES
Type of a virtual address.
Definition Abstract/Binary.hpp:42
std::vector< Section * > sections_t
Internal container.
Definition Abstract/Binary.hpp:59
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:134
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:77
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:29
This class represents a symbol in an executable format.
Definition Abstract/Symbol.hpp:28
Definition Visitor.hpp:221
Iterator which returns reference on container's values.
Definition iterators.hpp:48
LIEF namespace.
Definition Abstract/Binary.hpp:31
tl::expected< T, lief_errors > result
Wrapper that contains an Object (T) or an error.
Definition errors.hpp:73