LIEF: Library to Instrument Executable Formats Version 0.15.0
Loading...
Searching...
No Matches
ELF/Relocation.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_ELF_RELOCATION_H
17#define LIEF_ELF_RELOCATION_H
18
19#include <string>
20#include <ostream>
21
22#include "LIEF/Object.hpp"
23#include "LIEF/visibility.h"
24
25#include "LIEF/Abstract/Relocation.hpp"
26
27#include "LIEF/ELF/enums.hpp"
28#include "LIEF/ELF/Header.hpp"
29
30namespace LIEF {
31namespace ELF {
32
33class Parser;
34class Binary;
35class Builder;
36class Symbol;
37class Section;
38
40class LIEF_API Relocation : public LIEF::Relocation {
41
42 friend class Parser;
43 friend class Binary;
44 friend class Builder;
45
46 public:
47
50 enum class PURPOSE {
51 NONE = 0,
52 PLTGOT = 1,
53 DYNAMIC = 2,
54 OBJECT = 3,
55 };
56
57 enum class ENCODING {
58 UNKNOWN = 0,
59 REL,
60 RELA,
61 RELR,
62 ANDROID_SLEB,
63 };
64
65 static constexpr uint64_t R_BIT = 27;
66 static constexpr uint64_t R_MASK = (uint64_t(1) << R_BIT) - 1;
67
68 static constexpr uint64_t R_X64 = uint64_t(1) << R_BIT;
69 static constexpr uint64_t R_AARCH64 = uint64_t(2) << R_BIT;
70 static constexpr uint64_t R_ARM = uint64_t(3) << R_BIT;
71 static constexpr uint64_t R_HEXAGON = uint64_t(4) << R_BIT;
72 static constexpr uint64_t R_X86 = uint64_t(5) << R_BIT;
73 static constexpr uint64_t R_LARCH = uint64_t(6) << R_BIT;
74 static constexpr uint64_t R_MIPS = uint64_t(7) << R_BIT;
75 static constexpr uint64_t R_PPC = uint64_t(8) << R_BIT;
76 static constexpr uint64_t R_PPC64 = uint64_t(9) << R_BIT;
77 static constexpr uint64_t R_SPARC = uint64_t(10) << R_BIT;
78 static constexpr uint64_t R_SYSZ = uint64_t(11) << R_BIT;
79
81 enum class TYPE : uint32_t {
82 UNKNOWN = uint32_t(-1),
83
84 #define ELF_RELOC(name, value) name = (value | R_X64),
85 #include "LIEF/ELF/Relocations/x86_64.def"
86 #undef ELF_RELOC
87
88 #define ELF_RELOC(name, value) name = (value | R_AARCH64),
89 #include "LIEF/ELF/Relocations/AArch64.def"
90 #undef ELF_RELOC
91
92 #define ELF_RELOC(name, value) name = (value | R_ARM),
93 #include "LIEF/ELF/Relocations/ARM.def"
94 #undef ELF_RELOC
95
96 #define ELF_RELOC(name, value) name = (value | R_HEXAGON),
97 #include "LIEF/ELF/Relocations/Hexagon.def"
98 #undef ELF_RELOC
99
100 #define ELF_RELOC(name, value) name = (value | R_X86),
101 #include "LIEF/ELF/Relocations/i386.def"
102 #undef ELF_RELOC
103
104 #define ELF_RELOC(name, value) name = (value | R_LARCH),
105 #include "LIEF/ELF/Relocations/LoongArch.def"
106 #undef ELF_RELOC
107
108 #define ELF_RELOC(name, value) name = (value | R_MIPS),
109 #include "LIEF/ELF/Relocations/Mips.def"
110 #undef ELF_RELOC
111
112 #define ELF_RELOC(name, value) name = (value | R_PPC),
113 #include "LIEF/ELF/Relocations/PowerPC.def"
114 #undef ELF_RELOC
115
116 #define ELF_RELOC(name, value) name = (value | R_PPC64),
117 #include "LIEF/ELF/Relocations/PowerPC64.def"
118 #undef ELF_RELOC
119
120 #define ELF_RELOC(name, value) name = (value | R_SPARC),
121 #include "LIEF/ELF/Relocations/Sparc.def"
122 #undef ELF_RELOC
123
124 #define ELF_RELOC(name, value) name = (value | R_SYSZ),
125 #include "LIEF/ELF/Relocations/SystemZ.def"
126 #undef ELF_RELOC
127 };
128
129 static TYPE type_from(uint32_t value, ARCH arch);
130
131 static uint32_t to_value(TYPE type) {
132 return static_cast<uint32_t>(type) & R_MASK;
133 }
134
135 Relocation(uint64_t address, TYPE type, ENCODING enc);
136
137 Relocation() = default;
138 Relocation(ARCH arch) {
139 architecture_ = arch;
140 }
141 ~Relocation() override = default;
142
143 Relocation& operator=(Relocation other);
144 Relocation(const Relocation& other);
145 void swap(Relocation& other);
146
148 int64_t addend() const {
149 return addend_;
150 }
151
153 TYPE type() const {
154 return type_;
155 }
156
159 bool is_rela() const {
160 return encoding_ == ENCODING::RELA;
161 }
162
165 bool is_rel() const {
166 return encoding_ == ENCODING::REL;
167 }
168
171 return encoding_ == ENCODING::RELR;
172 }
173
175 bool is_android_packed() const {
176 return encoding_ == ENCODING::ANDROID_SLEB;
177 }
178
180 uint32_t info() const {
181 return info_;
182 }
183
185 uint64_t r_info(Header::CLASS clazz) const {
186 if (clazz == Header::CLASS::NONE) {
187 return 0;
188 }
189 return clazz == Header::CLASS::ELF32 ?
190 uint32_t(info()) << 8 | to_value(type()) :
191 uint64_t(info()) << 32 | (to_value(type()) & 0xffffffffL);
192 }
193
196 return architecture_;
197 }
198
199 PURPOSE purpose() const {
200 return purpose_;
201 }
202
205 return encoding_;
206 }
207
209 bool is_relative() const {
210 return type_ == TYPE::AARCH64_RELATIVE || type_ == TYPE::X86_64_RELATIVE ||
211 type_ == TYPE::X86_RELATIVE || type_ == TYPE::ARM_RELATIVE ||
212 type_ == TYPE::HEX_RELATIVE || type_ == TYPE::PPC64_RELATIVE ||
213 type_ == TYPE::PPC_RELATIVE;
214 }
215
218 size_t size() const override;
219
221 bool has_symbol() const {
222 return symbol_ != nullptr;
223 }
224
227 return symbol_;
228 }
229
230 const Symbol* symbol() const {
231 return symbol_;
232 }
233
235 bool has_section() const {
236 return section() != nullptr;
237 }
238
241 return section_;
242 }
243
244 const Section* section() const {
245 return section_;
246 }
247
250 return symbol_table_;
251 }
252
253 const Section* symbol_table() const {
254 return symbol_table_;
255 }
256
257 void addend(int64_t addend) {
258 addend_ = addend;
259 }
260
261 void type(TYPE type) {
262 type_ = type;
263 }
264
265 void purpose(PURPOSE purpose) {
266 purpose_ = purpose;
267 }
268
269 void info(uint32_t v) {
270 info_ = v;
271 }
272
273 void symbol(Symbol* symbol) {
274 symbol_ = symbol;
275 }
276
277 void section(Section* section) {
278 section_ = section;
279 }
280
281 void symbol_table(Section* section) {
282 symbol_table_ = section;
283 }
284
285 void accept(Visitor& visitor) const override;
286
287 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Relocation& entry);
288
289 private:
290 template<class T>
291 LIEF_LOCAL Relocation(const T& header, PURPOSE purpose, ENCODING enc, ARCH arch);
292
293 TYPE type_ = TYPE::UNKNOWN;
294 int64_t addend_ = 0;
295 ENCODING encoding_ = ENCODING::UNKNOWN;
296 Symbol* symbol_ = nullptr;
297 ARCH architecture_ = ARCH::NONE;
298 PURPOSE purpose_ = PURPOSE::NONE;
299 Section* section_ = nullptr;
300 Section* symbol_table_ = nullptr;
301 uint32_t info_ = 0;
302};
303
304LIEF_API const char* to_string(Relocation::TYPE type);
305
306}
307}
308#endif
Class which represents an ELF binary.
Definition ELF/Binary.hpp:59
Class which takes an ELF::Binary object and reconstructs a valid binary.
Definition ELF/Builder.hpp:48
CLASS
Match the result of Elfxx_Ehdr.e_ident[EI_CLASS]
Definition ELF/Header.hpp:76
Class which parses and transforms an ELF file into a ELF::Binary object.
Definition ELF/Parser.hpp:45
Class that represents an ELF relocation.
Definition ELF/Relocation.hpp:40
uint32_t info() const
Relocation info which contains, for instance, the symbol index.
Definition ELF/Relocation.hpp:180
TYPE
The different types of the relocation.
Definition ELF/Relocation.hpp:81
TYPE type() const
Type of the relocation.
Definition ELF/Relocation.hpp:153
PURPOSE
The purpose of a relocation defines how this relocation is used by the loader.
Definition ELF/Relocation.hpp:50
Symbol * symbol()
Symbol associated with the relocation (or a nullptr)
Definition ELF/Relocation.hpp:226
Section * symbol_table()
The associated symbol table (or a nullptr)
Definition ELF/Relocation.hpp:249
ENCODING encoding() const
The encoding of the relocation.
Definition ELF/Relocation.hpp:204
int64_t addend() const
Additional value that can be involved in the relocation processing.
Definition ELF/Relocation.hpp:148
bool is_relative() const
True if the semantic of the relocation is <ARCH>_RELATIVE
Definition ELF/Relocation.hpp:209
size_t size() const override
Return the size (in bits) of the value associated with this relocation Return -1 if the size can't be...
bool is_android_packed() const
True if the relocation is using the Android packed relocation format.
Definition ELF/Relocation.hpp:175
bool has_symbol() const
True if the current relocation is associated with a symbol.
Definition ELF/Relocation.hpp:221
bool has_section() const
True if the relocation has an associated section.
Definition ELF/Relocation.hpp:235
bool is_rela() const
Check if the relocation uses the explicit addend() field (this is usually the case for 64 bits binari...
Definition ELF/Relocation.hpp:159
bool is_rel() const
Check if the relocation uses the implicit addend (i.e. not present in the ELF structure)
Definition ELF/Relocation.hpp:165
ARCH architecture() const
Target architecture for this relocation.
Definition ELF/Relocation.hpp:195
Section * section()
The section in which the relocation is applied (or a nullptr)
Definition ELF/Relocation.hpp:240
uint64_t r_info(Header::CLASS clazz) const
(re)Compute the raw r_info attribute based on the given ELF class
Definition ELF/Relocation.hpp:185
ENCODING
Definition ELF/Relocation.hpp:57
bool is_relatively_encoded() const
True if the relocation is using the relative encoding.
Definition ELF/Relocation.hpp:170
Class wich represents an ELF Section.
Definition ELF/Section.hpp:46
Class which represents an ELF symbol.
Definition ELF/Symbol.hpp:35
Class which represents an abstracted Relocation.
Definition Abstract/Relocation.hpp:27
ARCH
Machine architectures See current registered ELF machine architectures at: http://www....
Definition ELF/enums.hpp:30
LIEF namespace.
Definition Abstract/Binary.hpp:31