LIEF: Library to Instrument Executable Formats Version 2.0.0
Loading...
Searching...
No Matches
ELF/Relocation.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2026 R. Thomas
2 * Copyright 2017 - 2026 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 <memory>
20#include <ostream>
21
22#include "LIEF/Object.hpp"
23#include "LIEF/errors.hpp"
24#include "LIEF/logging.hpp"
25#include "LIEF/visibility.h"
26
28
30#include "LIEF/ELF/Header.hpp"
31#include "LIEF/ELF/enums.hpp"
32
33
34namespace LIEF::ELF {
35
36class Parser;
37class Binary;
38class Builder;
39class Symbol;
40class Section;
41
44
45 friend class Parser;
46 friend class Binary;
47 friend class Builder;
48
49 public:
52 enum class PURPOSE {
53 NONE = 0,
55 PLTGOT,
56
58 DYNAMIC,
59
61 OBJECT,
62 };
63
64 enum class ENCODING {
65 UNKNOWN = 0,
66
68 REL,
69
71 RELA,
72
74 RELR,
75
77 ANDROID_SLEB,
78 };
79
80 static constexpr uint64_t R_BIT = 27;
81 static constexpr uint64_t R_MASK = (uint64_t(1) << R_BIT) - 1;
82
83 // clang-format off
84 static constexpr uint64_t R_X64 = uint64_t(1) << R_BIT;
85 static constexpr uint64_t R_AARCH64 = uint64_t(2) << R_BIT;
86 static constexpr uint64_t R_ARM = uint64_t(3) << R_BIT;
87 static constexpr uint64_t R_HEXAGON = uint64_t(4) << R_BIT;
88 static constexpr uint64_t R_X86 = uint64_t(5) << R_BIT;
89 static constexpr uint64_t R_LARCH = uint64_t(6) << R_BIT;
90 static constexpr uint64_t R_MIPS = uint64_t(7) << R_BIT;
91 static constexpr uint64_t R_PPC = uint64_t(8) << R_BIT;
92 static constexpr uint64_t R_PPC64 = uint64_t(9) << R_BIT;
93 static constexpr uint64_t R_SPARC = uint64_t(10) << R_BIT;
94 static constexpr uint64_t R_SYSZ = uint64_t(11) << R_BIT;
95 static constexpr uint64_t R_RISCV = uint64_t(12) << R_BIT;
96 static constexpr uint64_t R_BPF = uint64_t(13) << R_BIT;
97 static constexpr uint64_t R_SH4 = uint64_t(14) << R_BIT;
98 // clang-format on
99
101 enum class TYPE : uint32_t {
102 UNKNOWN = uint32_t(-1),
103
104#define ELF_RELOC(name, value) name = (value | R_X64),
106#undef ELF_RELOC
107
108#define ELF_RELOC(name, value) name = (value | R_AARCH64),
110#undef ELF_RELOC
112#define ELF_RELOC(name, value) name = (value | R_ARM),
114#undef ELF_RELOC
116#define ELF_RELOC(name, value) name = (value | R_HEXAGON),
118#undef ELF_RELOC
120#define ELF_RELOC(name, value) name = (value | R_X86),
122#undef ELF_RELOC
124#define ELF_RELOC(name, value) name = (value | R_LARCH),
126#undef ELF_RELOC
128#define ELF_RELOC(name, value) name = (value | R_MIPS),
130#undef ELF_RELOC
132#define ELF_RELOC(name, value) name = (value | R_PPC),
134#undef ELF_RELOC
136#define ELF_RELOC(name, value) name = (value | R_PPC64),
138#undef ELF_RELOC
140#define ELF_RELOC(name, value) name = (value | R_SPARC),
142#undef ELF_RELOC
144#define ELF_RELOC(name, value) name = (value | R_SYSZ),
146#undef ELF_RELOC
148#define ELF_RELOC(name, value) name = (value | R_RISCV),
150#undef ELF_RELOC
152#define ELF_RELOC(name, value) name = (value | R_BPF),
154#undef ELF_RELOC
155
156#define ELF_RELOC(name, value) name = (value | R_SH4),
158#undef ELF_RELOC
159 };
160
164 uint32_t type_value = 0;
165
167 uint32_t sym_idx = 0;
168
170 uint8_t special_symbol = 0;
173 uint8_t type2 = 0;
176 uint8_t type3 = 0;
177 };
182 return {
183 uint32_t(r_info >> 56), uint32_t(r_info & 0xffffffff),
184 uint8_t(r_info >> 32), uint8_t(r_info >> 48),
185 uint8_t(r_info >> 40),
186 };
188 return {
189 uint32_t(r_info & 0xff), uint32_t(r_info >> 32), uint8_t(r_info >> 24),
190 uint8_t(r_info >> 8), uint8_t(r_info >> 16),
191 };
193
195 static uint64_t encode_mips_n64(DecodedMipsN64 decoded, Header::ELF_DATA data) {
196 // clang-format off
198 return (uint64_t(decoded.sym_idx) << 0) |
199 (uint64_t(decoded.special_symbol) << 32) |
200 (uint64_t(decoded.type3) << 40) |
201 (uint64_t(decoded.type2) << 48) |
202 (uint64_t(decoded.type_value & 0xff) << 56);
204 return (uint64_t(decoded.sym_idx) << 32) |
205 (uint64_t(decoded.special_symbol) << 24) |
206 (uint64_t(decoded.type3) << 16) |
207 (uint64_t(decoded.type2) << 8) |
208 (uint64_t(decoded.type_value & 0xff) << 0);
209 // clang-format on
210 }
212 static uint64_t encode_mips_n64(uint32_t type_value, uint32_t sym_idx,
214 return encode_mips_n64({type_value, sym_idx}, data);
217 static TYPE type_from(uint32_t value, ARCH arch);
219 static uint32_t to_value(TYPE type) {
220 return static_cast<uint32_t>(type) & R_MASK;
225 Relocation() = default;
227 architecture_(arch) {}
229 ~Relocation() override = default;
236 Relocation(const Relocation& other) :
238 type_{other.type_},
239 addend_{other.addend_},
240 encoding_{other.encoding_},
241 architecture_{other.architecture_},
242 metadata_{other.metadata_},
243 info_{other.info_} {}
249 swap(other);
250 return *this;
253 void swap(Relocation& other) {
254 std::swap(address_, other.address_);
255 std::swap(type_, other.type_);
256 std::swap(addend_, other.addend_);
257 std::swap(encoding_, other.encoding_);
258 std::swap(symbol_, other.symbol_);
259 std::swap(architecture_, other.architecture_);
260 std::swap(metadata_, other.metadata_);
261 std::swap(section_, other.section_);
262 std::swap(symbol_table_, other.symbol_table_);
263 std::swap(info_, other.info_);
264 std::swap(binary_, other.binary_);
268 int64_t addend() const {
269 return addend_;
273 TYPE type() const {
274 return type_;
279 bool is_rela() const {
280 return encoding_ == ENCODING::RELA;
281 }
285 bool is_rel() const {
286 return encoding_ == ENCODING::REL;
291 return encoding_ == ENCODING::RELR;
292 }
295 bool is_android_packed() const {
296 return encoding_ == ENCODING::ANDROID_SLEB;
298
300 uint32_t info() const {
301 return info_;
306 uint64_t r_info(Header::CLASS clazz, Header::ELF_DATA data) const {
307 // Mips n64
308 if (architecture_ == ARCH::MIPS && clazz == Header::CLASS::ELF64 &&
310 {
311 return encode_mips_n64(mips_n64_info(), data);
312 }
313
314 if (clazz == Header::CLASS::NONE) {
315 return 0;
316 }
317 return clazz == Header::CLASS::ELF32 ?
318 uint32_t(info()) << 8 | to_value(type()) :
319 uint64_t(info()) << 32 | (to_value(type()) & 0xffffffffL);
323 uint64_t r_info(const Header& hdr) const {
324 if (hdr.machine_type() != architecture_) {
325 logging::err("Failed to compute r_info: architectures mismatch: {} vs {}",
326 to_string(hdr.machine_type()), to_string(architecture_));
327 return 0;
329 return r_info(hdr.identity_class(), hdr.identity_data());
334 return architecture_;
337 PURPOSE purpose() const {
338 return PURPOSE(metadata_.purpose);
343 return encoding_;
347 bool is_relative() const {
348 return type_ == TYPE::AARCH64_RELATIVE || type_ == TYPE::X86_64_RELATIVE ||
356 size_t size() const override;
359 bool has_symbol() const {
360 return symbol_ != nullptr;
365 return symbol_;
369 return symbol_;
373 bool has_section() const {
374 return section() != nullptr;
379 return section_;
383 return section_;
388 return symbol_table_;
392 return symbol_table_;
395 void addend(int64_t addend) {
396 addend_ = addend;
399 void type(TYPE type) {
400 type_ = type;
404 metadata_.purpose = uint32_t(purpose);
407 void info(uint32_t v) {
408 info_ = v;
412 symbol_ = symbol;
416 section_ = section;
420 symbol_table_ = section;
425 result<uint64_t> resolve(uint64_t base_address = 0) const;
427 void accept(Visitor& visitor) const override;
429 LIEF_API friend std::ostream& operator<<(std::ostream& os,
430 const Relocation& entry);
431 template<class T>
432 LIEF_LOCAL static std::unique_ptr<Relocation>
433 create(const T& header, PURPOSE purpose, ENCODING enc,
434 const Header& elf_hdr);
436 private:
437 // clang-format off
438 struct Metadata {
439 uint32_t purpose : 8,
440 special_symbol : 8,
441 type2 : 8,
442 type3 : 8;
443 };
444 // clang-format on
446 static_assert(sizeof(Metadata) == sizeof(uint32_t));
448 DecodedMipsN64 mips_n64_info() const {
449 return {
451 info(),
452 uint8_t(metadata_.special_symbol),
453 uint8_t(metadata_.type2),
454 uint8_t(metadata_.type3),
455 };
458 uint8_t mips_n64_type2() const {
459 return uint8_t(metadata_.type2);
460 }
461
462 LIEF_LOCAL Relocation(uint64_t addr, uint32_t info, TYPE type, int64_t addend,
463 PURPOSE purpose, ENCODING enc, ARCH arch,
464 DecodedMipsN64 mips_n64) :
465 LIEF::Relocation{addr, 0},
466 type_{type},
467 addend_{addend},
468 encoding_{enc},
469 architecture_{arch},
470 metadata_{uint32_t(purpose), mips_n64.special_symbol, mips_n64.type2,
471 mips_n64.type3},
472 info_{info} {}
475 int64_t addend_ = 0;
477 Symbol* symbol_ = nullptr;
478 ARCH architecture_ = ARCH::NONE;
479 Metadata metadata_ = {};
480 Section* section_ = nullptr;
481 Section* symbol_table_ = nullptr;
482 uint32_t info_ = 0;
484 Binary* binary_ = nullptr;
491#endif
Class which represents an ELF binary.
Definition ELF/Binary.hpp:61
Class which takes an ELF::Binary object and reconstructs a valid binary.
Definition ELF/Builder.hpp:48
Class which represents the ELF's header. This class mirrors the raw ELF Elfxx_Ehdr structure.
Definition ELF/Header.hpp:35
ELF_DATA
Match the result Elfxx_Ehdr.e_ident[EI_DATA].
Definition ELF/Header.hpp:108
@ LSB
2's complement, little endian
Definition ELF/Header.hpp:110
ELF_DATA identity_data() const
Specify the data encoding.
Definition ELF/Header.hpp:207
CLASS identity_class() const
Return the object's class. ELF64 or ELF32.
Definition ELF/Header.hpp:202
ARCH machine_type() const
Target architecture.
Definition ELF/Header.hpp:127
CLASS
Match the result of Elfxx_Ehdr.e_ident[EI_CLASS].
Definition ELF/Header.hpp:74
@ ELF64
64-bit objects
Definition ELF/Header.hpp:77
@ NONE
Invalid class.
Definition ELF/Header.hpp:75
Class which parses and transforms an ELF file into a ELF::Binary object.
Definition ELF/Parser.hpp:47
Class that represents an ELF relocation.
Definition ELF/Relocation.hpp:43
uint32_t info() const
Relocation info which contains, for instance, the symbol index.
Definition ELF/Relocation.hpp:300
TYPE
The different types of the relocation.
Definition ELF/Relocation.hpp:101
@ PPC64_RELATIVE
Definition ELF/Relocation.hpp:988
@ UNKNOWN
Definition ELF/Relocation.hpp:102
@ X86_64_RELATIVE
Definition ELF/Relocation.hpp:119
@ AARCH64_RELATIVE
Definition ELF/Relocation.hpp:302
@ HEX_RELATIVE
Definition ELF/Relocation.hpp:504
@ ARM_RELATIVE
Definition ELF/Relocation.hpp:342
@ PPC_RELATIVE
Definition ELF/Relocation.hpp:922
@ X86_RELATIVE
Definition ELF/Relocation.hpp:582
static constexpr uint64_t R_PPC64
Definition ELF/Relocation.hpp:92
friend std::ostream & operator<<(std::ostream &os, const Relocation &entry)
TYPE type() const
Type of the relocation.
Definition ELF/Relocation.hpp:273
PURPOSE
The purpose of a relocation defines how this relocation is used by the loader.
Definition ELF/Relocation.hpp:52
static uint64_t encode_mips_n64(DecodedMipsN64 decoded, Header::ELF_DATA data)
Encode all the fields of a MIPS n64 relocation into a r_info value.
Definition ELF/Relocation.hpp:195
static constexpr uint64_t R_X86
Definition ELF/Relocation.hpp:88
static uint32_t to_value(TYPE type)
Definition ELF/Relocation.hpp:219
Symbol * symbol()
Symbol associated with the relocation (or a nullptr).
Definition ELF/Relocation.hpp:364
Section * symbol_table()
The associated symbol table (or a nullptr).
Definition ELF/Relocation.hpp:387
static TYPE type_from(uint32_t value, ARCH arch)
static uint64_t encode_mips_n64(uint32_t type_value, uint32_t sym_idx, Header::ELF_DATA data)
Definition ELF/Relocation.hpp:212
void accept(Visitor &visitor) const override
void swap(Relocation &other)
Definition ELF/Relocation.hpp:253
static DecodedMipsN64 decode_mips_n64(uint64_t r_info, Header::ELF_DATA data)
Decode r_info for MIPS n64.
Definition ELF/Relocation.hpp:180
ENCODING encoding() const
The encoding of the relocation.
Definition ELF/Relocation.hpp:342
Relocation & operator=(Relocation other)
Copy assignment operator.
Definition ELF/Relocation.hpp:248
uint64_t r_info(Header::CLASS clazz, Header::ELF_DATA data) const
(re)Compute the raw r_info attribute based on the given ELF class and endianness.
Definition ELF/Relocation.hpp:306
static std::unique_ptr< Relocation > create(const T &header, PURPOSE purpose, ENCODING enc, const Header &elf_hdr)
int64_t addend() const
Additional value that can be involved in the relocation processing.
Definition ELF/Relocation.hpp:268
result< uint64_t > resolve(uint64_t base_address=0) const
Try to resolve the value of the relocation such as *address() = resolve().
PURPOSE purpose() const
Definition ELF/Relocation.hpp:337
bool is_relative() const
True if the semantic of the relocation is <ARCH>_RELATIVE.
Definition ELF/Relocation.hpp:347
~Relocation() override=default
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:295
static constexpr uint64_t R_MASK
Definition ELF/Relocation.hpp:81
static constexpr uint64_t R_BPF
Definition ELF/Relocation.hpp:96
bool has_symbol() const
True if the current relocation is associated with a symbol.
Definition ELF/Relocation.hpp:359
static constexpr uint64_t R_RISCV
Definition ELF/Relocation.hpp:95
bool has_section() const
True if the relocation has an associated section.
Definition ELF/Relocation.hpp:373
friend class Builder
Definition ELF/Relocation.hpp:47
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:279
static constexpr uint64_t R_SPARC
Definition ELF/Relocation.hpp:93
friend class Binary
Definition ELF/Relocation.hpp:46
static constexpr uint64_t R_HEXAGON
Definition ELF/Relocation.hpp:87
static constexpr uint64_t R_SYSZ
Definition ELF/Relocation.hpp:94
bool is_rel() const
Check if the relocation uses the implicit addend (i.e. not present in the ELF structure).
Definition ELF/Relocation.hpp:285
ARCH architecture() const
Target architecture for this relocation.
Definition ELF/Relocation.hpp:333
Relocation(uint64_t address, TYPE type, ENCODING enc)
static constexpr uint64_t R_SH4
Definition ELF/Relocation.hpp:97
friend class Parser
Definition ELF/Relocation.hpp:45
static constexpr uint64_t R_BIT
Definition ELF/Relocation.hpp:80
Section * section()
The section in which the relocation is applied (or a nullptr).
Definition ELF/Relocation.hpp:378
static constexpr uint64_t R_ARM
Definition ELF/Relocation.hpp:86
static constexpr uint64_t R_MIPS
Definition ELF/Relocation.hpp:90
static constexpr uint64_t R_PPC
Definition ELF/Relocation.hpp:91
static constexpr uint64_t R_X64
Definition ELF/Relocation.hpp:84
static constexpr uint64_t R_LARCH
Definition ELF/Relocation.hpp:89
void addend(int64_t addend)
Definition ELF/Relocation.hpp:395
static constexpr uint64_t R_AARCH64
Definition ELF/Relocation.hpp:85
ENCODING
Definition ELF/Relocation.hpp:64
@ ANDROID_SLEB
The relocation is using the packed Android-SLEB128 format.
Definition ELF/Relocation.hpp:77
@ RELR
The relocation is using the relative relocation format.
Definition ELF/Relocation.hpp:74
@ UNKNOWN
Definition ELF/Relocation.hpp:65
@ RELA
The relocation is using the regular Elf_Rela structure.
Definition ELF/Relocation.hpp:71
@ REL
The relocation is using the regular Elf_Rel structure.
Definition ELF/Relocation.hpp:68
bool is_relatively_encoded() const
True if the relocation is using the relative encoding.
Definition ELF/Relocation.hpp:290
Class which represents an ELF Section.
Definition ELF/Section.hpp:49
Class which represents an ELF symbol.
Definition ELF/Symbol.hpp:34
Class which represents an abstracted Relocation.
Definition Abstract/Relocation.hpp:27
virtual uint64_t address() const
Relocation's address.
Definition Abstract/Relocation.hpp:47
Definition Visitor.hpp:212
Wrapper that contains an Object (T) or an error.
Definition errors.hpp:79
#define LIEF_LIFETIMEBOUND
Definition compiler_attributes.hpp:72
const char * to_string(lief_errors err)
Namespace related to the LIEF's ELF module.
Definition Abstract/Header.hpp:28
const char * to_string(DynamicEntry::TAG e)
ARCH
Definition ELF/enums.hpp:28
@ MIPS
Definition ELF/enums.hpp:37
@ NONE
Definition ELF/enums.hpp:29
void err(const std::string &msg)
Definition logging.hpp:143
LIEF namespace.
Definition Abstract/Binary.hpp:41
Fields decoded from the MIPS-specific n64 r_info layout.
Definition ELF/Relocation.hpp:162
uint8_t special_symbol
Special symbol used by the second relocation operation.
Definition ELF/Relocation.hpp:170
uint32_t sym_idx
Index of the symbol associated with the relocation.
Definition ELF/Relocation.hpp:167
uint8_t type3
Third relocation type.
Definition ELF/Relocation.hpp:176
uint32_t type_value
Primary relocation type.
Definition ELF/Relocation.hpp:164
uint8_t type2
Second relocation type.
Definition ELF/Relocation.hpp:173
#define LIEF_API
Definition visibility.h:45
#define LIEF_LOCAL
Definition visibility.h:46