LIEF: Library to Instrument Executable Formats Version 0.17.0
Loading...
Searching...
No Matches
ELF/Relocation.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2025 R. Thomas
2 * Copyright 2017 - 2025 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:
47
50 enum class PURPOSE {
51 NONE = 0,
52 PLTGOT = 1,
53 DYNAMIC = 2,
54 OBJECT = 3,
55 };
56
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 static constexpr uint64_t R_RISCV = uint64_t(12) << R_BIT;
80 static constexpr uint64_t R_BPF = uint64_t(13) << R_BIT;
81 static constexpr uint64_t R_SH4 = uint64_t(14) << R_BIT;
82
84 enum class TYPE : uint32_t {
85 UNKNOWN = uint32_t(-1),
86
87 #define ELF_RELOC(name, value) name = (value | R_X64),
89 #undef ELF_RELOC
90
91 #define ELF_RELOC(name, value) name = (value | R_AARCH64),
93 #undef ELF_RELOC
95 #define ELF_RELOC(name, value) name = (value | R_ARM),
97 #undef ELF_RELOC
99 #define ELF_RELOC(name, value) name = (value | R_HEXAGON),
101 #undef ELF_RELOC
103 #define ELF_RELOC(name, value) name = (value | R_X86),
105 #undef ELF_RELOC
107 #define ELF_RELOC(name, value) name = (value | R_LARCH),
109 #undef ELF_RELOC
111 #define ELF_RELOC(name, value) name = (value | R_MIPS),
113 #undef ELF_RELOC
115 #define ELF_RELOC(name, value) name = (value | R_PPC),
117 #undef ELF_RELOC
119 #define ELF_RELOC(name, value) name = (value | R_PPC64),
121 #undef ELF_RELOC
123 #define ELF_RELOC(name, value) name = (value | R_SPARC),
125 #undef ELF_RELOC
127 #define ELF_RELOC(name, value) name = (value | R_SYSZ),
129 #undef ELF_RELOC
131 #define ELF_RELOC(name, value) name = (value | R_RISCV),
133 #undef ELF_RELOC
135 #define ELF_RELOC(name, value) name = (value | R_BPF),
137 #undef ELF_RELOC
138
139 #define ELF_RELOC(name, value) name = (value | R_SH4),
141 #undef ELF_RELOC
142 };
143
144 static TYPE type_from(uint32_t value, ARCH arch);
145
146 static uint32_t to_value(TYPE type) {
147 return static_cast<uint32_t>(type) & R_MASK;
148 }
149
151
152 Relocation() = default;
154 architecture_ = arch;
157 ~Relocation() override = default;
158
164 Relocation(const Relocation& other) :
166 type_{other.type_},
167 addend_{other.addend_},
168 encoding_{other.encoding_},
169 architecture_{other.architecture_}
170 {}
176 swap(other);
177 return *this;
180 void swap(Relocation& other) {
181 std::swap(address_, other.address_);
182 std::swap(type_, other.type_);
183 std::swap(addend_, other.addend_);
184 std::swap(encoding_, other.encoding_);
185 std::swap(symbol_, other.symbol_);
186 std::swap(architecture_, other.architecture_);
187 std::swap(purpose_, other.purpose_);
188 std::swap(section_, other.section_);
189 std::swap(symbol_table_, other.symbol_table_);
190 std::swap(info_, other.info_);
191 std::swap(binary_, other.binary_);
193
195 int64_t addend() const {
196 return addend_;
200 TYPE type() const {
201 return type_;
203
206 bool is_rela() const {
207 return encoding_ == ENCODING::RELA;
212 bool is_rel() const {
213 return encoding_ == ENCODING::REL;
218 return encoding_ == ENCODING::RELR;
222 bool is_android_packed() const {
223 return encoding_ == ENCODING::ANDROID_SLEB;
227 uint32_t info() const {
228 return info_;
232 uint64_t r_info(Header::CLASS clazz) const {
233 if (clazz == Header::CLASS::NONE) {
234 return 0;
236 return clazz == Header::CLASS::ELF32 ?
237 uint32_t(info()) << 8 | to_value(type()) :
238 uint64_t(info()) << 32 | (to_value(type()) & 0xffffffffL);
243 return architecture_;
246 PURPOSE purpose() const {
247 return purpose_;
252 return encoding_;
256 bool is_relative() const {
257 return type_ == TYPE::AARCH64_RELATIVE || type_ == TYPE::X86_64_RELATIVE ||
265 size_t size() const override;
268 bool has_symbol() const {
269 return symbol_ != nullptr;
274 return symbol_;
275 }
277 const Symbol* symbol() const {
278 return symbol_;
282 bool has_section() const {
283 return section() != nullptr;
288 return section_;
291 const Section* section() const {
292 return section_;
293 }
294
297 return symbol_table_;
298 }
299
300 const Section* symbol_table() const {
301 return symbol_table_;
304 void addend(int64_t addend) {
305 addend_ = addend;
308 void type(TYPE type) {
309 type_ = type;
313 purpose_ = purpose;
316 void info(uint32_t v) {
317 info_ = v;
321 symbol_ = symbol;
325 section_ = section;
329 symbol_table_ = section;
334 result<uint64_t> resolve(uint64_t base_address = 0) const;
336 void accept(Visitor& visitor) const override;
338 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Relocation& entry);
340 private:
341 template<class T>
342 LIEF_LOCAL Relocation(const T& header, PURPOSE purpose, ENCODING enc, ARCH arch);
345 int64_t addend_ = 0;
347 Symbol* symbol_ = nullptr;
348 ARCH architecture_ = ARCH::NONE;
350 Section* section_ = nullptr;
351 Section* symbol_table_ = nullptr;
352 uint32_t info_ = 0;
354 Binary* binary_ = nullptr;
361#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:73
@ NONE
Definition ELF/Header.hpp:74
@ ELF32
Invalid class.
Definition ELF/Header.hpp:75
Class which parses and transforms an ELF file into a ELF::Binary object.
Definition ELF/Parser.hpp:45
uint32_t info() const
Relocation info which contains, for instance, the symbol index.
Definition ELF/Relocation.hpp:227
TYPE
The different types of the relocation.
Definition ELF/Relocation.hpp:84
@ PPC64_RELATIVE
Definition ELF/Relocation.hpp:971
@ UNKNOWN
Definition ELF/Relocation.hpp:85
@ X86_64_RELATIVE
Definition ELF/Relocation.hpp:102
@ AARCH64_RELATIVE
Definition ELF/Relocation.hpp:285
@ HEX_RELATIVE
Definition ELF/Relocation.hpp:487
@ ARM_RELATIVE
Definition ELF/Relocation.hpp:325
@ PPC_RELATIVE
Definition ELF/Relocation.hpp:905
@ X86_RELATIVE
Definition ELF/Relocation.hpp:565
static constexpr uint64_t R_PPC64
Definition ELF/Relocation.hpp:76
friend std::ostream & operator<<(std::ostream &os, const Relocation &entry)
TYPE type() const
Type of the relocation.
Definition ELF/Relocation.hpp:200
PURPOSE
The purpose of a relocation defines how this relocation is used by the loader.
Definition ELF/Relocation.hpp:50
@ DYNAMIC
The relocation is used for regulard data/code relocation.
Definition ELF/Relocation.hpp:53
@ OBJECT
The relocation is used in an object file.
Definition ELF/Relocation.hpp:54
@ PLTGOT
The relocation is associated with the PLT/GOT resolution.
Definition ELF/Relocation.hpp:52
@ NONE
Definition ELF/Relocation.hpp:51
static constexpr uint64_t R_X86
Definition ELF/Relocation.hpp:72
static uint32_t to_value(TYPE type)
Definition ELF/Relocation.hpp:146
Symbol * symbol()
Symbol associated with the relocation (or a nullptr).
Definition ELF/Relocation.hpp:273
Section * symbol_table()
The associated symbol table (or a nullptr).
Definition ELF/Relocation.hpp:296
static TYPE type_from(uint32_t value, ARCH arch)
void accept(Visitor &visitor) const override
void swap(Relocation &other)
Definition ELF/Relocation.hpp:180
ENCODING encoding() const
The encoding of the relocation.
Definition ELF/Relocation.hpp:251
Relocation & operator=(Relocation other)
Copy assignment operator.
Definition ELF/Relocation.hpp:175
int64_t addend() const
Additional value that can be involved in the relocation processing.
Definition ELF/Relocation.hpp:195
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:246
bool is_relative() const
True if the semantic of the relocation is <ARCH>_RELATIVE.
Definition ELF/Relocation.hpp:256
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:222
static constexpr uint64_t R_MASK
Definition ELF/Relocation.hpp:66
static constexpr uint64_t R_BPF
Definition ELF/Relocation.hpp:80
bool has_symbol() const
True if the current relocation is associated with a symbol.
Definition ELF/Relocation.hpp:268
static constexpr uint64_t R_RISCV
Definition ELF/Relocation.hpp:79
bool has_section() const
True if the relocation has an associated section.
Definition ELF/Relocation.hpp:282
const Section * symbol_table() const
Definition ELF/Relocation.hpp:300
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:206
const Section * section() const
Definition ELF/Relocation.hpp:291
static constexpr uint64_t R_SPARC
Definition ELF/Relocation.hpp:77
friend class Binary
Definition ELF/Relocation.hpp:43
static constexpr uint64_t R_HEXAGON
Definition ELF/Relocation.hpp:71
static constexpr uint64_t R_SYSZ
Definition ELF/Relocation.hpp:78
bool is_rel() const
Check if the relocation uses the implicit addend (i.e. not present in the ELF structure).
Definition ELF/Relocation.hpp:212
ARCH architecture() const
Target architecture for this relocation.
Definition ELF/Relocation.hpp:242
Relocation(uint64_t address, TYPE type, ENCODING enc)
static constexpr uint64_t R_SH4
Definition ELF/Relocation.hpp:81
friend class Parser
Definition ELF/Relocation.hpp:42
static constexpr uint64_t R_BIT
Definition ELF/Relocation.hpp:65
Section * section()
The section in which the relocation is applied (or a nullptr).
Definition ELF/Relocation.hpp:287
static constexpr uint64_t R_ARM
Definition ELF/Relocation.hpp:70
static constexpr uint64_t R_MIPS
Definition ELF/Relocation.hpp:74
static constexpr uint64_t R_PPC
Definition ELF/Relocation.hpp:75
static constexpr uint64_t R_X64
Definition ELF/Relocation.hpp:68
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:232
static constexpr uint64_t R_LARCH
Definition ELF/Relocation.hpp:73
void addend(int64_t addend)
Definition ELF/Relocation.hpp:304
static constexpr uint64_t R_AARCH64
Definition ELF/Relocation.hpp:69
ENCODING
Definition ELF/Relocation.hpp:57
@ ANDROID_SLEB
The relocation is using the packed Android-SLEB128 format.
Definition ELF/Relocation.hpp:62
@ RELR
The relocation is using the relative relocation format.
Definition ELF/Relocation.hpp:61
@ UNKNOWN
Definition ELF/Relocation.hpp:58
@ RELA
The relocation is using the regular Elf_Rela structure.
Definition ELF/Relocation.hpp:60
@ REL
The relocation is using the regular Elf_Rel structure.
Definition ELF/Relocation.hpp:59
bool is_relatively_encoded() const
True if the relocation is using the relative encoding.
Definition ELF/Relocation.hpp:217
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:48
Definition Visitor.hpp:210
Wrapper that contains an Object (T) or an error.
Definition errors.hpp:75
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:41
#define LIEF_LOCAL
Definition visibility.h:42