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
49 enum class PURPOSE {
50 NONE = 0,
51 PLTGOT = 1,
52 DYNAMIC = 2,
53 OBJECT = 3,
54 };
55
56 enum class ENCODING {
57 UNKNOWN = 0,
58 REL,
59 RELA,
60 RELR,
61 ANDROID_SLEB,
62 };
63
64 static constexpr uint64_t R_BIT = 27;
65 static constexpr uint64_t R_MASK = (uint64_t(1) << R_BIT) - 1;
66
67 static constexpr uint64_t R_X64 = uint64_t(1) << R_BIT;
68 static constexpr uint64_t R_AARCH64 = uint64_t(2) << R_BIT;
69 static constexpr uint64_t R_ARM = uint64_t(3) << R_BIT;
70 static constexpr uint64_t R_HEXAGON = uint64_t(4) << R_BIT;
71 static constexpr uint64_t R_X86 = uint64_t(5) << R_BIT;
72 static constexpr uint64_t R_LARCH = uint64_t(6) << R_BIT;
73 static constexpr uint64_t R_MIPS = uint64_t(7) << R_BIT;
74 static constexpr uint64_t R_PPC = uint64_t(8) << R_BIT;
75 static constexpr uint64_t R_PPC64 = uint64_t(9) << R_BIT;
76 static constexpr uint64_t R_SPARC = uint64_t(10) << R_BIT;
77 static constexpr uint64_t R_SYSZ = uint64_t(11) << R_BIT;
78
80 enum class TYPE : uint32_t {
81 UNKNOWN = uint32_t(-1),
82
83 #define ELF_RELOC(name, value) name = (value | R_X64),
84 #include "LIEF/ELF/Relocations/x86_64.def"
85 #undef ELF_RELOC
86
87 #define ELF_RELOC(name, value) name = (value | R_AARCH64),
88 #include "LIEF/ELF/Relocations/AArch64.def"
89 #undef ELF_RELOC
90
91 #define ELF_RELOC(name, value) name = (value | R_ARM),
92 #include "LIEF/ELF/Relocations/ARM.def"
93 #undef ELF_RELOC
94
95 #define ELF_RELOC(name, value) name = (value | R_HEXAGON),
96 #include "LIEF/ELF/Relocations/Hexagon.def"
97 #undef ELF_RELOC
98
99 #define ELF_RELOC(name, value) name = (value | R_X86),
100 #include "LIEF/ELF/Relocations/i386.def"
101 #undef ELF_RELOC
102
103 #define ELF_RELOC(name, value) name = (value | R_LARCH),
104 #include "LIEF/ELF/Relocations/LoongArch.def"
105 #undef ELF_RELOC
106
107 #define ELF_RELOC(name, value) name = (value | R_MIPS),
108 #include "LIEF/ELF/Relocations/Mips.def"
109 #undef ELF_RELOC
110
111 #define ELF_RELOC(name, value) name = (value | R_PPC),
112 #include "LIEF/ELF/Relocations/PowerPC.def"
113 #undef ELF_RELOC
114
115 #define ELF_RELOC(name, value) name = (value | R_PPC64),
116 #include "LIEF/ELF/Relocations/PowerPC64.def"
117 #undef ELF_RELOC
118
119 #define ELF_RELOC(name, value) name = (value | R_SPARC),
120 #include "LIEF/ELF/Relocations/Sparc.def"
121 #undef ELF_RELOC
122
123 #define ELF_RELOC(name, value) name = (value | R_SYSZ),
124 #include "LIEF/ELF/Relocations/SystemZ.def"
125 #undef ELF_RELOC
126 };
127
128 static TYPE type_from(uint32_t value, ARCH arch);
129
130 static uint32_t to_value(TYPE type) {
131 return static_cast<uint32_t>(type) & R_MASK;
132 }
133
134 Relocation(uint64_t address, TYPE type, ENCODING enc);
135
136 Relocation() = default;
137 Relocation(ARCH arch) {
138 architecture_ = arch;
139 }
140 ~Relocation() override = default;
141
142 Relocation& operator=(Relocation other);
143 Relocation(const Relocation& other);
144 void swap(Relocation& other);
145
147 int64_t addend() const {
148 return addend_;
149 }
150
152 TYPE type() const {
153 return type_;
154 }
155
158 bool is_rela() const {
159 return encoding_ == ENCODING::RELA;
160 }
161
164 bool is_rel() const {
165 return encoding_ == ENCODING::REL;
166 }
167
170 return encoding_ == ENCODING::RELR;
171 }
172
174 bool is_android_packed() const {
175 return encoding_ == ENCODING::ANDROID_SLEB;
176 }
177
179 uint32_t info() const {
180 return info_;
181 }
182
184 uint64_t r_info(Header::CLASS clazz) const {
185 if (clazz == Header::CLASS::NONE) {
186 return 0;
187 }
188 return clazz == Header::CLASS::ELF32 ?
189 uint32_t(info()) << 8 | to_value(type()) :
190 uint64_t(info()) << 32 | (to_value(type()) & 0xffffffffL);
191 }
192
195 return architecture_;
196 }
197
198 PURPOSE purpose() const {
199 return purpose_;
200 }
201
204 return encoding_;
205 }
206
208 bool is_relative() const {
209 return type_ == TYPE::AARCH64_RELATIVE || type_ == TYPE::X86_64_RELATIVE ||
210 type_ == TYPE::X86_RELATIVE || type_ == TYPE::ARM_RELATIVE ||
211 type_ == TYPE::HEX_RELATIVE || type_ == TYPE::PPC64_RELATIVE ||
212 type_ == TYPE::PPC_RELATIVE;
213 }
214
217 size_t size() const override;
218
220 bool has_symbol() const {
221 return symbol_ != nullptr;
222 }
223
226 return symbol_;
227 }
228
229 const Symbol* symbol() const {
230 return symbol_;
231 }
232
234 bool has_section() const {
235 return section() != nullptr;
236 }
237
240 return section_;
241 }
242
243 const Section* section() const {
244 return section_;
245 }
246
249 return symbol_table_;
250 }
251
252 const Section* symbol_table() const {
253 return symbol_table_;
254 }
255
256 void addend(int64_t addend) {
257 addend_ = addend;
258 }
259
260 void type(TYPE type) {
261 type_ = type;
262 }
263
264 void purpose(PURPOSE purpose) {
265 purpose_ = purpose;
266 }
267
268 void info(uint32_t v) {
269 info_ = v;
270 }
271
272 void symbol(Symbol* symbol) {
273 symbol_ = symbol;
274 }
275
276 void section(Section* section) {
277 section_ = section;
278 }
279
280 void symbol_table(Section* section) {
281 symbol_table_ = section;
282 }
283
284 void accept(Visitor& visitor) const override;
285
286 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Relocation& entry);
287
288 private:
289 template<class T>
290 LIEF_LOCAL Relocation(const T& header, PURPOSE purpose, ENCODING enc, ARCH arch);
291
292 TYPE type_ = TYPE::UNKNOWN;
293 int64_t addend_ = 0;
294 ENCODING encoding_ = ENCODING::UNKNOWN;
295 Symbol* symbol_ = nullptr;
296 ARCH architecture_ = ARCH::NONE;
297 PURPOSE purpose_ = PURPOSE::NONE;
298 Section* section_ = nullptr;
299 Section* symbol_table_ = nullptr;
300 uint32_t info_ = 0;
301};
302
303LIEF_API const char* to_string(Relocation::TYPE type);
304
305}
306}
307#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:51
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:179
TYPE
The different types of the relocation.
Definition ELF/Relocation.hpp:80
TYPE type() const
Type of the relocation.
Definition ELF/Relocation.hpp:152
PURPOSE
The purpose of a relocation defines.
Definition ELF/Relocation.hpp:49
Symbol * symbol()
Symbol associated with the relocation (or a nullptr)
Definition ELF/Relocation.hpp:225
Section * symbol_table()
The associated symbol table (or a nullptr)
Definition ELF/Relocation.hpp:248
ENCODING encoding() const
The encoding of the relocation.
Definition ELF/Relocation.hpp:203
int64_t addend() const
Additional value that can be involved in the relocation processing.
Definition ELF/Relocation.hpp:147
bool is_relative() const
True if the semantic of the relocation is <ARCH>_RELATIVE
Definition ELF/Relocation.hpp:208
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:174
bool has_symbol() const
True if the current relocation is associated with a symbol.
Definition ELF/Relocation.hpp:220
bool has_section() const
True if the relocation has an associated section.
Definition ELF/Relocation.hpp:234
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:158
bool is_rel() const
Check if the relocation uses the implicit addend (i.e. not present in the ELF structure)
Definition ELF/Relocation.hpp:164
ARCH architecture() const
Target architecture for this relocation.
Definition ELF/Relocation.hpp:194
Section * section()
The section in which the relocation is applied (or a nullptr)
Definition ELF/Relocation.hpp:239
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:184
ENCODING
Definition ELF/Relocation.hpp:56
bool is_relatively_encoded() const
True if the relocation is using the relative encoding.
Definition ELF/Relocation.hpp:169
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:32