LIEF: Library to Instrument Executable Formats Version 1.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 <ostream>
20
21#include "LIEF/Object.hpp"
22#include "LIEF/visibility.h"
23#include "LIEF/errors.hpp"
24
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
41
42 friend class Parser;
43 friend class Binary;
44 friend class Builder;
45
46 public:
49 enum class PURPOSE {
50 NONE = 0,
52 PLTGOT,
53
55 DYNAMIC,
56
58 OBJECT,
59 };
60
61 enum class ENCODING {
62 UNKNOWN = 0,
63
65 REL,
66
68 RELA,
69
71 RELR,
72
74 ANDROID_SLEB,
75 };
76
77 static constexpr uint64_t R_BIT = 27;
78 static constexpr uint64_t R_MASK = (uint64_t(1) << R_BIT) - 1;
79
80 // clang-format off
81 static constexpr uint64_t R_X64 = uint64_t(1) << R_BIT;
82 static constexpr uint64_t R_AARCH64 = uint64_t(2) << R_BIT;
83 static constexpr uint64_t R_ARM = uint64_t(3) << R_BIT;
84 static constexpr uint64_t R_HEXAGON = uint64_t(4) << R_BIT;
85 static constexpr uint64_t R_X86 = uint64_t(5) << R_BIT;
86 static constexpr uint64_t R_LARCH = uint64_t(6) << R_BIT;
87 static constexpr uint64_t R_MIPS = uint64_t(7) << R_BIT;
88 static constexpr uint64_t R_PPC = uint64_t(8) << R_BIT;
89 static constexpr uint64_t R_PPC64 = uint64_t(9) << R_BIT;
90 static constexpr uint64_t R_SPARC = uint64_t(10) << R_BIT;
91 static constexpr uint64_t R_SYSZ = uint64_t(11) << R_BIT;
92 static constexpr uint64_t R_RISCV = uint64_t(12) << R_BIT;
93 static constexpr uint64_t R_BPF = uint64_t(13) << R_BIT;
94 static constexpr uint64_t R_SH4 = uint64_t(14) << R_BIT;
95 // clang-format on
96
98 enum class TYPE : uint32_t {
99 UNKNOWN = uint32_t(-1),
100
101#define ELF_RELOC(name, value) name = (value | R_X64),
103#undef ELF_RELOC
104
105#define ELF_RELOC(name, value) name = (value | R_AARCH64),
107#undef ELF_RELOC
109#define ELF_RELOC(name, value) name = (value | R_ARM),
111#undef ELF_RELOC
113#define ELF_RELOC(name, value) name = (value | R_HEXAGON),
115#undef ELF_RELOC
117#define ELF_RELOC(name, value) name = (value | R_X86),
119#undef ELF_RELOC
121#define ELF_RELOC(name, value) name = (value | R_LARCH),
123#undef ELF_RELOC
125#define ELF_RELOC(name, value) name = (value | R_MIPS),
127#undef ELF_RELOC
129#define ELF_RELOC(name, value) name = (value | R_PPC),
131#undef ELF_RELOC
133#define ELF_RELOC(name, value) name = (value | R_PPC64),
135#undef ELF_RELOC
137#define ELF_RELOC(name, value) name = (value | R_SPARC),
139#undef ELF_RELOC
141#define ELF_RELOC(name, value) name = (value | R_SYSZ),
143#undef ELF_RELOC
145#define ELF_RELOC(name, value) name = (value | R_RISCV),
147#undef ELF_RELOC
149#define ELF_RELOC(name, value) name = (value | R_BPF),
151#undef ELF_RELOC
152
153#define ELF_RELOC(name, value) name = (value | R_SH4),
155#undef ELF_RELOC
156 };
157
158 static TYPE type_from(uint32_t value, ARCH arch);
159
160 static uint32_t to_value(TYPE type) {
161 return static_cast<uint32_t>(type) & R_MASK;
162 }
163
165
166 Relocation() = default;
168 architecture_(arch) {}
170 ~Relocation() override = default;
177 Relocation(const Relocation& other) :
179 type_{other.type_},
180 addend_{other.addend_},
181 encoding_{other.encoding_},
182 architecture_{other.architecture_} {}
183
188 swap(other);
189 return *this;
190 }
192 void swap(Relocation& other) {
193 std::swap(address_, other.address_);
194 std::swap(type_, other.type_);
195 std::swap(addend_, other.addend_);
196 std::swap(encoding_, other.encoding_);
197 std::swap(symbol_, other.symbol_);
198 std::swap(architecture_, other.architecture_);
199 std::swap(purpose_, other.purpose_);
200 std::swap(section_, other.section_);
201 std::swap(symbol_table_, other.symbol_table_);
202 std::swap(info_, other.info_);
203 std::swap(binary_, other.binary_);
207 int64_t addend() const {
208 return addend_;
209 }
212 TYPE type() const {
213 return type_;
218 bool is_rela() const {
219 return encoding_ == ENCODING::RELA;
220 }
224 bool is_rel() const {
225 return encoding_ == ENCODING::REL;
226 }
230 return encoding_ == ENCODING::RELR;
232
234 bool is_android_packed() const {
235 return encoding_ == ENCODING::ANDROID_SLEB;
239 uint32_t info() const {
240 return info_;
244 uint64_t r_info(Header::CLASS clazz) const {
245 if (clazz == Header::CLASS::NONE) {
246 return 0;
248 return clazz == Header::CLASS::ELF32 ?
249 uint32_t(info()) << 8 | to_value(type()) :
250 uint64_t(info()) << 32 | (to_value(type()) & 0xffffffffL);
255 return architecture_;
258 PURPOSE purpose() const {
259 return purpose_;
261
264 return encoding_;
268 bool is_relative() const {
269 return type_ == TYPE::AARCH64_RELATIVE || type_ == TYPE::X86_64_RELATIVE ||
277 size_t size() const override;
278
280 bool has_symbol() const {
281 return symbol_ != nullptr;
286 return symbol_;
290 return symbol_;
292
294 bool has_section() const {
295 return section() != nullptr;
300 return section_;
304 return section_;
305 }
306
309 return symbol_table_;
310 }
311
313 return symbol_table_;
314 }
315
316 void addend(int64_t addend) {
317 addend_ = addend;
320 void type(TYPE type) {
321 type_ = type;
325 purpose_ = purpose;
328 void info(uint32_t v) {
329 info_ = v;
333 symbol_ = symbol;
337 section_ = section;
341 symbol_table_ = section;
346 result<uint64_t> resolve(uint64_t base_address = 0) const;
348 void accept(Visitor& visitor) const override;
350 LIEF_API friend std::ostream& operator<<(std::ostream& os,
351 const Relocation& entry);
353 private:
354 template<class T>
355 LIEF_LOCAL Relocation(const T& header, PURPOSE purpose, ENCODING enc, ARCH arch);
358 int64_t addend_ = 0;
360 Symbol* symbol_ = nullptr;
361 ARCH architecture_ = ARCH::NONE;
363 Section* section_ = nullptr;
364 Section* symbol_table_ = nullptr;
365 uint32_t info_ = 0;
367 Binary* binary_ = nullptr;
374#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:74
@ NONE
Invalid class.
Definition ELF/Header.hpp:75
@ ELF32
32-bit objects
Definition ELF/Header.hpp:76
Class which parses and transforms an ELF file into a ELF::Binary object.
Definition ELF/Parser.hpp:44
uint32_t info() const
Relocation info which contains, for instance, the symbol index.
Definition ELF/Relocation.hpp:239
TYPE
The different types of the relocation.
Definition ELF/Relocation.hpp:98
@ PPC64_RELATIVE
Definition ELF/Relocation.hpp:985
@ UNKNOWN
Definition ELF/Relocation.hpp:99
@ X86_64_RELATIVE
Definition ELF/Relocation.hpp:116
@ AARCH64_RELATIVE
Definition ELF/Relocation.hpp:299
@ HEX_RELATIVE
Definition ELF/Relocation.hpp:501
@ ARM_RELATIVE
Definition ELF/Relocation.hpp:339
@ PPC_RELATIVE
Definition ELF/Relocation.hpp:919
@ X86_RELATIVE
Definition ELF/Relocation.hpp:579
static constexpr uint64_t R_PPC64
Definition ELF/Relocation.hpp:89
friend std::ostream & operator<<(std::ostream &os, const Relocation &entry)
TYPE type() const
Type of the relocation.
Definition ELF/Relocation.hpp:212
PURPOSE
The purpose of a relocation defines how this relocation is used by the loader.
Definition ELF/Relocation.hpp:49
@ NONE
Definition ELF/Relocation.hpp:50
static constexpr uint64_t R_X86
Definition ELF/Relocation.hpp:85
static uint32_t to_value(TYPE type)
Definition ELF/Relocation.hpp:160
Symbol * symbol()
Symbol associated with the relocation (or a nullptr).
Definition ELF/Relocation.hpp:285
Section * symbol_table()
The associated symbol table (or a nullptr).
Definition ELF/Relocation.hpp:308
static TYPE type_from(uint32_t value, ARCH arch)
void accept(Visitor &visitor) const override
void swap(Relocation &other)
Definition ELF/Relocation.hpp:192
ENCODING encoding() const
The encoding of the relocation.
Definition ELF/Relocation.hpp:263
Relocation & operator=(Relocation other)
Copy assignment operator.
Definition ELF/Relocation.hpp:187
int64_t addend() const
Additional value that can be involved in the relocation processing.
Definition ELF/Relocation.hpp:207
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:258
bool is_relative() const
True if the semantic of the relocation is <ARCH>_RELATIVE.
Definition ELF/Relocation.hpp:268
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:234
static constexpr uint64_t R_MASK
Definition ELF/Relocation.hpp:78
static constexpr uint64_t R_BPF
Definition ELF/Relocation.hpp:93
bool has_symbol() const
True if the current relocation is associated with a symbol.
Definition ELF/Relocation.hpp:280
static constexpr uint64_t R_RISCV
Definition ELF/Relocation.hpp:92
bool has_section() const
True if the relocation has an associated section.
Definition ELF/Relocation.hpp:294
const Section * symbol_table() const
Definition ELF/Relocation.hpp:312
friend class Builder
Definition ELF/Relocation.hpp:44
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:218
static constexpr uint64_t R_SPARC
Definition ELF/Relocation.hpp:90
friend class Binary
Definition ELF/Relocation.hpp:43
static constexpr uint64_t R_HEXAGON
Definition ELF/Relocation.hpp:84
static constexpr uint64_t R_SYSZ
Definition ELF/Relocation.hpp:91
bool is_rel() const
Check if the relocation uses the implicit addend (i.e. not present in the ELF structure).
Definition ELF/Relocation.hpp:224
ARCH architecture() const
Target architecture for this relocation.
Definition ELF/Relocation.hpp:254
Relocation(uint64_t address, TYPE type, ENCODING enc)
static constexpr uint64_t R_SH4
Definition ELF/Relocation.hpp:94
friend class Parser
Definition ELF/Relocation.hpp:42
const Symbol * symbol() const
Definition ELF/Relocation.hpp:289
static constexpr uint64_t R_BIT
Definition ELF/Relocation.hpp:77
Section * section()
The section in which the relocation is applied (or a nullptr).
Definition ELF/Relocation.hpp:299
static constexpr uint64_t R_ARM
Definition ELF/Relocation.hpp:83
static constexpr uint64_t R_MIPS
Definition ELF/Relocation.hpp:87
static constexpr uint64_t R_PPC
Definition ELF/Relocation.hpp:88
static constexpr uint64_t R_X64
Definition ELF/Relocation.hpp:81
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:244
static constexpr uint64_t R_LARCH
Definition ELF/Relocation.hpp:86
void addend(int64_t addend)
Definition ELF/Relocation.hpp:316
static constexpr uint64_t R_AARCH64
Definition ELF/Relocation.hpp:82
ENCODING
Definition ELF/Relocation.hpp:61
@ ANDROID_SLEB
The relocation is using the packed Android-SLEB128 format.
Definition ELF/Relocation.hpp:74
@ RELR
The relocation is using the relative relocation format.
Definition ELF/Relocation.hpp:71
@ UNKNOWN
Definition ELF/Relocation.hpp:62
@ RELA
The relocation is using the regular Elf_Rela structure.
Definition ELF/Relocation.hpp:68
@ REL
The relocation is using the regular Elf_Rel structure.
Definition ELF/Relocation.hpp:65
bool is_relatively_encoded() const
True if the relocation is using the relative encoding.
Definition ELF/Relocation.hpp:229
Class which represents an ELF Section.
Definition ELF/Section.hpp:48
Class which represents an ELF symbol.
Definition ELF/Symbol.hpp:35
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:77
#define LIEF_LIFETIMEBOUND
Definition compiler_attributes.hpp:72
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:30
@ NONE
Definition ELF/enums.hpp:31
LIEF namespace.
Definition Abstract/Binary.hpp:40
#define LIEF_API
Definition visibility.h:45
#define LIEF_LOCAL
Definition visibility.h:46