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 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15#ifndef LIEF_ELF_RELOCATION_H
16#define LIEF_ELF_RELOCATION_H
17
18#include <memory>
19#include <ostream>
20
21#include "LIEF/Object.hpp"
22#include "LIEF/errors.hpp"
23#include "LIEF/logging.hpp"
24#include "LIEF/visibility.h"
25
27
29#include "LIEF/ELF/Header.hpp"
30#include "LIEF/ELF/enums.hpp"
31
32
33namespace LIEF::ELF {
34
35class Parser;
36class Binary;
37class Builder;
38class Symbol;
39class Section;
40
43
44 friend class Parser;
45 friend class Binary;
46 friend class Builder;
47
48 public:
51 enum class PURPOSE {
52 NONE = 0,
54 PLTGOT,
55
57 DYNAMIC,
58
60 OBJECT,
61 };
62
63 enum class ENCODING {
64 UNKNOWN = 0,
65
67 REL,
68
70 RELA,
71
73 RELR,
74
76 ANDROID_SLEB,
77 };
78
79 static constexpr uint64_t R_BIT = 27;
80 static constexpr uint64_t R_MASK = (uint64_t(1) << R_BIT) - 1;
81
82 // clang-format off
83 static constexpr uint64_t R_X64 = uint64_t(1) << R_BIT;
84 static constexpr uint64_t R_AARCH64 = uint64_t(2) << R_BIT;
85 static constexpr uint64_t R_ARM = uint64_t(3) << R_BIT;
86 static constexpr uint64_t R_HEXAGON = uint64_t(4) << R_BIT;
87 static constexpr uint64_t R_X86 = uint64_t(5) << R_BIT;
88 static constexpr uint64_t R_LARCH = uint64_t(6) << R_BIT;
89 static constexpr uint64_t R_MIPS = uint64_t(7) << R_BIT;
90 static constexpr uint64_t R_PPC = uint64_t(8) << R_BIT;
91 static constexpr uint64_t R_PPC64 = uint64_t(9) << R_BIT;
92 static constexpr uint64_t R_SPARC = uint64_t(10) << R_BIT;
93 static constexpr uint64_t R_SYSZ = uint64_t(11) << R_BIT;
94 static constexpr uint64_t R_RISCV = uint64_t(12) << R_BIT;
95 static constexpr uint64_t R_BPF = uint64_t(13) << R_BIT;
96 static constexpr uint64_t R_SH4 = uint64_t(14) << R_BIT;
97 // clang-format on
98
100 enum class TYPE : uint32_t {
101 UNKNOWN = uint32_t(-1),
102
103#define ELF_RELOC(name, value) name = (value | R_X64),
105#undef ELF_RELOC
106
107#define ELF_RELOC(name, value) name = (value | R_AARCH64),
109#undef ELF_RELOC
111#define ELF_RELOC(name, value) name = (value | R_ARM),
113#undef ELF_RELOC
115#define ELF_RELOC(name, value) name = (value | R_HEXAGON),
117#undef ELF_RELOC
119#define ELF_RELOC(name, value) name = (value | R_X86),
121#undef ELF_RELOC
123#define ELF_RELOC(name, value) name = (value | R_LARCH),
125#undef ELF_RELOC
127#define ELF_RELOC(name, value) name = (value | R_MIPS),
129#undef ELF_RELOC
131#define ELF_RELOC(name, value) name = (value | R_PPC),
133#undef ELF_RELOC
135#define ELF_RELOC(name, value) name = (value | R_PPC64),
137#undef ELF_RELOC
139#define ELF_RELOC(name, value) name = (value | R_SPARC),
141#undef ELF_RELOC
143#define ELF_RELOC(name, value) name = (value | R_SYSZ),
145#undef ELF_RELOC
147#define ELF_RELOC(name, value) name = (value | R_RISCV),
149#undef ELF_RELOC
151#define ELF_RELOC(name, value) name = (value | R_BPF),
153#undef ELF_RELOC
154
155#define ELF_RELOC(name, value) name = (value | R_SH4),
157#undef ELF_RELOC
158 };
159
163 uint32_t type_value = 0;
164
166 uint32_t sym_idx = 0;
167
169 uint8_t special_symbol = 0;
172 uint8_t type2 = 0;
175 uint8_t type3 = 0;
176 };
181 return {
182 uint32_t(r_info >> 56), uint32_t(r_info & 0xffffffff),
183 uint8_t(r_info >> 32), uint8_t(r_info >> 48),
184 uint8_t(r_info >> 40),
185 };
187 return {
188 uint32_t(r_info & 0xff), uint32_t(r_info >> 32), uint8_t(r_info >> 24),
189 uint8_t(r_info >> 8), uint8_t(r_info >> 16),
190 };
192
194 static uint64_t encode_mips_n64(DecodedMipsN64 decoded, Header::ELF_DATA data) {
195 // clang-format off
197 return (uint64_t(decoded.sym_idx) << 0) |
198 (uint64_t(decoded.special_symbol) << 32) |
199 (uint64_t(decoded.type3) << 40) |
200 (uint64_t(decoded.type2) << 48) |
201 (uint64_t(decoded.type_value & 0xff) << 56);
203 return (uint64_t(decoded.sym_idx) << 32) |
204 (uint64_t(decoded.special_symbol) << 24) |
205 (uint64_t(decoded.type3) << 16) |
206 (uint64_t(decoded.type2) << 8) |
207 (uint64_t(decoded.type_value & 0xff) << 0);
208 // clang-format on
209 }
211 static uint64_t encode_mips_n64(uint32_t type_value, uint32_t sym_idx,
213 return encode_mips_n64({type_value, sym_idx}, data);
216 static TYPE type_from(uint32_t value, ARCH arch);
218 static uint32_t to_value(TYPE type) {
219 return static_cast<uint32_t>(type) & R_MASK;
224 Relocation() = default;
226 architecture_(arch) {}
228 ~Relocation() override = default;
235 Relocation(const Relocation& other) :
237 type_{other.type_},
238 addend_{other.addend_},
239 encoding_{other.encoding_},
240 architecture_{other.architecture_},
241 metadata_{other.metadata_},
242 info_{other.info_} {}
248 swap(other);
249 return *this;
252 void swap(Relocation& other) {
253 std::swap(address_, other.address_);
254 std::swap(type_, other.type_);
255 std::swap(addend_, other.addend_);
256 std::swap(encoding_, other.encoding_);
257 std::swap(symbol_, other.symbol_);
258 std::swap(architecture_, other.architecture_);
259 std::swap(metadata_, other.metadata_);
260 std::swap(section_, other.section_);
261 std::swap(symbol_table_, other.symbol_table_);
262 std::swap(info_, other.info_);
263 std::swap(binary_, other.binary_);
267 int64_t addend() const {
268 return addend_;
272 TYPE type() const {
273 return type_;
278 bool is_rela() const {
279 return encoding_ == ENCODING::RELA;
280 }
284 bool is_rel() const {
285 return encoding_ == ENCODING::REL;
290 return encoding_ == ENCODING::RELR;
291 }
294 bool is_android_packed() const {
295 return encoding_ == ENCODING::ANDROID_SLEB;
297
299 uint32_t info() const {
300 return info_;
305 uint64_t r_info(Header::CLASS clazz, Header::ELF_DATA data) const {
306 // Mips n64
307 if (architecture_ == ARCH::MIPS && clazz == Header::CLASS::ELF64 &&
309 {
310 return encode_mips_n64(mips_n64_info(), data);
311 }
312
313 if (clazz == Header::CLASS::NONE) {
314 return 0;
315 }
316 return clazz == Header::CLASS::ELF32 ?
317 uint32_t(info()) << 8 | to_value(type()) :
318 uint64_t(info()) << 32 | (to_value(type()) & 0xffffffffL);
322 uint64_t r_info(const Header& hdr) const {
323 if (hdr.machine_type() != architecture_) {
324 logging::err("Failed to compute r_info: architectures mismatch: {} vs {}",
325 to_string(hdr.machine_type()), to_string(architecture_));
326 return 0;
328 return r_info(hdr.identity_class(), hdr.identity_data());
333 return architecture_;
336 PURPOSE purpose() const {
337 return PURPOSE(metadata_.purpose);
342 return encoding_;
346 bool is_relative() const {
347 return type_ == TYPE::AARCH64_RELATIVE || type_ == TYPE::X86_64_RELATIVE ||
355 size_t size() const override;
358 bool has_symbol() const {
359 return symbol_ != nullptr;
364 return symbol_;
368 return symbol_;
372 bool has_section() const {
373 return section() != nullptr;
378 return section_;
382 return section_;
387 return symbol_table_;
391 return symbol_table_;
394 void addend(int64_t addend) {
395 addend_ = addend;
398 void type(TYPE type) {
399 type_ = type;
403 metadata_.purpose = uint32_t(purpose);
406 void info(uint32_t v) {
407 info_ = v;
411 symbol_ = symbol;
415 section_ = section;
419 symbol_table_ = section;
424 result<uint64_t> resolve(uint64_t base_address = 0) const;
426 void accept(Visitor& visitor) const override;
428 LIEF_API friend std::ostream& operator<<(std::ostream& os,
429 const Relocation& entry);
430 template<class T>
431 LIEF_LOCAL static std::unique_ptr<Relocation>
432 create(const T& header, PURPOSE purpose, ENCODING enc,
433 const Header& elf_hdr);
435 private:
436 // clang-format off
437 struct Metadata {
438 uint32_t purpose : 8,
439 special_symbol : 8,
440 type2 : 8,
441 type3 : 8;
442 };
443 // clang-format on
445 static_assert(sizeof(Metadata) == sizeof(uint32_t));
447 DecodedMipsN64 mips_n64_info() const {
448 return {
450 info(),
451 uint8_t(metadata_.special_symbol),
452 uint8_t(metadata_.type2),
453 uint8_t(metadata_.type3),
454 };
457 uint8_t mips_n64_type2() const {
458 return uint8_t(metadata_.type2);
459 }
460
461 LIEF_LOCAL Relocation(uint64_t addr, uint32_t info, TYPE type, int64_t addend,
462 PURPOSE purpose, ENCODING enc, ARCH arch,
463 DecodedMipsN64 mips_n64) :
464 LIEF::Relocation{addr, 0},
465 type_{type},
466 addend_{addend},
467 encoding_{enc},
468 architecture_{arch},
469 metadata_{uint32_t(purpose), mips_n64.special_symbol, mips_n64.type2,
470 mips_n64.type3},
471 info_{info} {}
474 int64_t addend_ = 0;
476 Symbol* symbol_ = nullptr;
477 ARCH architecture_ = ARCH::NONE;
478 Metadata metadata_ = {};
479 Section* section_ = nullptr;
480 Section* symbol_table_ = nullptr;
481 uint32_t info_ = 0;
483 Binary* binary_ = nullptr;
490#endif
Class which represents an ELF binary.
Definition ELF/Binary.hpp:64
Class which takes an ELF::Binary object and reconstructs a valid binary.
Definition ELF/Builder.hpp:47
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:46
Class that represents an ELF relocation.
Definition ELF/Relocation.hpp:42
uint32_t info() const
Relocation info which contains, for instance, the symbol index.
Definition ELF/Relocation.hpp:299
TYPE
The different types of the relocation.
Definition ELF/Relocation.hpp:100
@ PPC64_RELATIVE
Definition ELF/Relocation.hpp:987
@ UNKNOWN
Definition ELF/Relocation.hpp:101
@ X86_64_RELATIVE
Definition ELF/Relocation.hpp:118
@ AARCH64_RELATIVE
Definition ELF/Relocation.hpp:301
@ HEX_RELATIVE
Definition ELF/Relocation.hpp:503
@ ARM_RELATIVE
Definition ELF/Relocation.hpp:341
@ PPC_RELATIVE
Definition ELF/Relocation.hpp:921
@ X86_RELATIVE
Definition ELF/Relocation.hpp:581
static constexpr uint64_t R_PPC64
Definition ELF/Relocation.hpp:91
friend std::ostream & operator<<(std::ostream &os, const Relocation &entry)
TYPE type() const
Type of the relocation.
Definition ELF/Relocation.hpp:272
PURPOSE
The purpose of a relocation defines how this relocation is used by the loader.
Definition ELF/Relocation.hpp:51
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:194
static constexpr uint64_t R_X86
Definition ELF/Relocation.hpp:87
static uint32_t to_value(TYPE type)
Definition ELF/Relocation.hpp:218
Symbol * symbol()
Symbol associated with the relocation (or a nullptr).
Definition ELF/Relocation.hpp:363
Section * symbol_table()
The associated symbol table (or a nullptr).
Definition ELF/Relocation.hpp:386
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:211
void accept(Visitor &visitor) const override
void swap(Relocation &other)
Definition ELF/Relocation.hpp:252
static DecodedMipsN64 decode_mips_n64(uint64_t r_info, Header::ELF_DATA data)
Decode r_info for MIPS n64.
Definition ELF/Relocation.hpp:179
ENCODING encoding() const
The encoding of the relocation.
Definition ELF/Relocation.hpp:341
Relocation & operator=(Relocation other)
Copy assignment operator.
Definition ELF/Relocation.hpp:247
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:305
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:267
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:336
bool is_relative() const
True if the semantic of the relocation is <ARCH>_RELATIVE.
Definition ELF/Relocation.hpp:346
~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:294
static constexpr uint64_t R_MASK
Definition ELF/Relocation.hpp:80
static constexpr uint64_t R_BPF
Definition ELF/Relocation.hpp:95
bool has_symbol() const
True if the current relocation is associated with a symbol.
Definition ELF/Relocation.hpp:358
static constexpr uint64_t R_RISCV
Definition ELF/Relocation.hpp:94
bool has_section() const
True if the relocation has an associated section.
Definition ELF/Relocation.hpp:372
friend class Builder
Definition ELF/Relocation.hpp:46
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:278
static constexpr uint64_t R_SPARC
Definition ELF/Relocation.hpp:92
friend class Binary
Definition ELF/Relocation.hpp:45
static constexpr uint64_t R_HEXAGON
Definition ELF/Relocation.hpp:86
static constexpr uint64_t R_SYSZ
Definition ELF/Relocation.hpp:93
bool is_rel() const
Check if the relocation uses the implicit addend (i.e. not present in the ELF structure).
Definition ELF/Relocation.hpp:284
ARCH architecture() const
Target architecture for this relocation.
Definition ELF/Relocation.hpp:332
Relocation(uint64_t address, TYPE type, ENCODING enc)
static constexpr uint64_t R_SH4
Definition ELF/Relocation.hpp:96
friend class Parser
Definition ELF/Relocation.hpp:44
static constexpr uint64_t R_BIT
Definition ELF/Relocation.hpp:79
Section * section()
The section in which the relocation is applied (or a nullptr).
Definition ELF/Relocation.hpp:377
static constexpr uint64_t R_ARM
Definition ELF/Relocation.hpp:85
static constexpr uint64_t R_MIPS
Definition ELF/Relocation.hpp:89
static constexpr uint64_t R_PPC
Definition ELF/Relocation.hpp:90
static constexpr uint64_t R_X64
Definition ELF/Relocation.hpp:83
static constexpr uint64_t R_LARCH
Definition ELF/Relocation.hpp:88
void addend(int64_t addend)
Definition ELF/Relocation.hpp:394
static constexpr uint64_t R_AARCH64
Definition ELF/Relocation.hpp:84
ENCODING
Definition ELF/Relocation.hpp:63
@ ANDROID_SLEB
The relocation is using the packed Android-SLEB128 format.
Definition ELF/Relocation.hpp:76
@ RELR
The relocation is using the relative relocation format.
Definition ELF/Relocation.hpp:73
@ UNKNOWN
Definition ELF/Relocation.hpp:64
@ RELA
The relocation is using the regular Elf_Rela structure.
Definition ELF/Relocation.hpp:70
@ REL
The relocation is using the regular Elf_Rel structure.
Definition ELF/Relocation.hpp:67
bool is_relatively_encoded() const
True if the relocation is using the relative encoding.
Definition ELF/Relocation.hpp:289
Class which represents an ELF Section.
Definition ELF/Section.hpp:49
Class which represents an ELF symbol.
Definition ELF/Symbol.hpp:33
Class which represents an abstracted Relocation.
Definition Abstract/Relocation.hpp:26
virtual uint64_t address() const
Relocation's address.
Definition Abstract/Relocation.hpp:46
Definition Visitor.hpp:211
Wrapper that contains an Object (T) or an error.
Definition errors.hpp:78
#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:27
const char * to_string(DynamicEntry::TAG e)
ARCH
Definition ELF/enums.hpp:27
@ MIPS
Definition ELF/enums.hpp:36
@ NONE
Definition ELF/enums.hpp:28
void err(std::string_view 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:161
uint8_t special_symbol
Special symbol used by the second relocation operation.
Definition ELF/Relocation.hpp:169
uint32_t sym_idx
Index of the symbol associated with the relocation.
Definition ELF/Relocation.hpp:166
uint8_t type3
Third relocation type.
Definition ELF/Relocation.hpp:175
uint32_t type_value
Primary relocation type.
Definition ELF/Relocation.hpp:163
uint8_t type2
Second relocation type.
Definition ELF/Relocation.hpp:172
#define LIEF_API
Definition visibility.h:44
#define LIEF_LOCAL
Definition visibility.h:45