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,
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 static constexpr uint64_t R_RISCV = uint64_t(12) << R_BIT;
79 static constexpr uint64_t R_BPF = uint64_t(13) << R_BIT;
80 static constexpr uint64_t R_SH4 = uint64_t(14) << R_BIT;
81
83 enum class TYPE : uint32_t {
84 UNKNOWN = uint32_t(-1),
85
86#define ELF_RELOC(name, value) name = (value | R_X64),
88#undef ELF_RELOC
89
90#define ELF_RELOC(name, value) name = (value | R_AARCH64),
92#undef ELF_RELOC
94#define ELF_RELOC(name, value) name = (value | R_ARM),
96#undef ELF_RELOC
98#define ELF_RELOC(name, value) name = (value | R_HEXAGON),
100#undef ELF_RELOC
102#define ELF_RELOC(name, value) name = (value | R_X86),
104#undef ELF_RELOC
106#define ELF_RELOC(name, value) name = (value | R_LARCH),
108#undef ELF_RELOC
110#define ELF_RELOC(name, value) name = (value | R_MIPS),
112#undef ELF_RELOC
114#define ELF_RELOC(name, value) name = (value | R_PPC),
116#undef ELF_RELOC
118#define ELF_RELOC(name, value) name = (value | R_PPC64),
120#undef ELF_RELOC
122#define ELF_RELOC(name, value) name = (value | R_SPARC),
124#undef ELF_RELOC
126#define ELF_RELOC(name, value) name = (value | R_SYSZ),
128#undef ELF_RELOC
130#define ELF_RELOC(name, value) name = (value | R_RISCV),
132#undef ELF_RELOC
134#define ELF_RELOC(name, value) name = (value | R_BPF),
136#undef ELF_RELOC
137
138#define ELF_RELOC(name, value) name = (value | R_SH4),
140#undef ELF_RELOC
141 };
142
143 static TYPE type_from(uint32_t value, ARCH arch);
144
145 static uint32_t to_value(TYPE type) {
146 return static_cast<uint32_t>(type) & R_MASK;
147 }
148
150
151 Relocation() = default;
153 architecture_ = arch;
156 ~Relocation() override = default;
157
163 Relocation(const Relocation& other) :
165 type_{other.type_},
166 addend_{other.addend_},
167 encoding_{other.encoding_},
168 architecture_{other.architecture_} {}
174 swap(other);
175 return *this;
178 void swap(Relocation& other) {
179 std::swap(address_, other.address_);
180 std::swap(type_, other.type_);
181 std::swap(addend_, other.addend_);
182 std::swap(encoding_, other.encoding_);
183 std::swap(symbol_, other.symbol_);
184 std::swap(architecture_, other.architecture_);
185 std::swap(purpose_, other.purpose_);
186 std::swap(section_, other.section_);
187 std::swap(symbol_table_, other.symbol_table_);
188 std::swap(info_, other.info_);
189 std::swap(binary_, other.binary_);
193 int64_t addend() const {
194 return addend_;
198 TYPE type() const {
199 return type_;
204 bool is_rela() const {
205 return encoding_ == ENCODING::RELA;
210 bool is_rel() const {
211 return encoding_ == ENCODING::REL;
216 return encoding_ == ENCODING::RELR;
217 }
220 bool is_android_packed() const {
221 return encoding_ == ENCODING::ANDROID_SLEB;
225 uint32_t info() const {
226 return info_;
230 uint64_t r_info(Header::CLASS clazz) const {
231 if (clazz == Header::CLASS::NONE) {
232 return 0;
234 return clazz == Header::CLASS::ELF32 ?
235 uint32_t(info()) << 8 | to_value(type()) :
236 uint64_t(info()) << 32 | (to_value(type()) & 0xffffffffL);
241 return architecture_;
244 PURPOSE purpose() const {
245 return purpose_;
246 }
250 return encoding_;
254 bool is_relative() const {
255 return type_ == TYPE::AARCH64_RELATIVE || type_ == TYPE::X86_64_RELATIVE ||
263 size_t size() const override;
266 bool has_symbol() const {
267 return symbol_ != nullptr;
272 return symbol_;
274
275 const Symbol* symbol() const {
276 return symbol_;
277 }
280 bool has_section() const {
281 return section() != nullptr;
286 return section_;
289 const Section* section() const {
290 return section_;
291 }
292
295 return symbol_table_;
296 }
297
298 const Section* symbol_table() const {
299 return symbol_table_;
300 }
302 void addend(int64_t addend) {
303 addend_ = addend;
306 void type(TYPE type) {
307 type_ = type;
311 purpose_ = purpose;
314 void info(uint32_t v) {
315 info_ = v;
319 symbol_ = symbol;
323 section_ = section;
327 symbol_table_ = section;
332 result<uint64_t> resolve(uint64_t base_address = 0) const;
334 void accept(Visitor& visitor) const override;
336 LIEF_API friend std::ostream& operator<<(std::ostream& os,
337 const Relocation& entry);
339 private:
340 template<class T>
341 LIEF_LOCAL Relocation(const T& header, PURPOSE purpose, ENCODING enc, ARCH arch);
344 int64_t addend_ = 0;
346 Symbol* symbol_ = nullptr;
347 ARCH architecture_ = ARCH::NONE;
349 Section* section_ = nullptr;
350 Section* symbol_table_ = nullptr;
351 uint32_t info_ = 0;
353 Binary* binary_ = nullptr;
360#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
Definition ELF/Header.hpp:75
@ ELF32
Invalid class.
Definition ELF/Header.hpp:76
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:225
TYPE
The different types of the relocation.
Definition ELF/Relocation.hpp:83
@ PPC64_RELATIVE
Definition ELF/Relocation.hpp:970
@ UNKNOWN
Definition ELF/Relocation.hpp:84
@ X86_64_RELATIVE
Definition ELF/Relocation.hpp:101
@ AARCH64_RELATIVE
Definition ELF/Relocation.hpp:284
@ HEX_RELATIVE
Definition ELF/Relocation.hpp:486
@ ARM_RELATIVE
Definition ELF/Relocation.hpp:324
@ PPC_RELATIVE
Definition ELF/Relocation.hpp:904
@ X86_RELATIVE
Definition ELF/Relocation.hpp:564
static constexpr uint64_t R_PPC64
Definition ELF/Relocation.hpp:75
friend std::ostream & operator<<(std::ostream &os, const Relocation &entry)
TYPE type() const
Type of the relocation.
Definition ELF/Relocation.hpp:198
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:71
static uint32_t to_value(TYPE type)
Definition ELF/Relocation.hpp:145
Symbol * symbol()
Symbol associated with the relocation (or a nullptr).
Definition ELF/Relocation.hpp:271
Section * symbol_table()
The associated symbol table (or a nullptr).
Definition ELF/Relocation.hpp:294
static TYPE type_from(uint32_t value, ARCH arch)
void accept(Visitor &visitor) const override
void swap(Relocation &other)
Definition ELF/Relocation.hpp:178
ENCODING encoding() const
The encoding of the relocation.
Definition ELF/Relocation.hpp:249
Relocation & operator=(Relocation other)
Copy assignment operator.
Definition ELF/Relocation.hpp:173
int64_t addend() const
Additional value that can be involved in the relocation processing.
Definition ELF/Relocation.hpp:193
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:244
bool is_relative() const
True if the semantic of the relocation is <ARCH>_RELATIVE.
Definition ELF/Relocation.hpp:254
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:220
static constexpr uint64_t R_MASK
Definition ELF/Relocation.hpp:65
static constexpr uint64_t R_BPF
Definition ELF/Relocation.hpp:79
bool has_symbol() const
True if the current relocation is associated with a symbol.
Definition ELF/Relocation.hpp:266
static constexpr uint64_t R_RISCV
Definition ELF/Relocation.hpp:78
bool has_section() const
True if the relocation has an associated section.
Definition ELF/Relocation.hpp:280
const Section * symbol_table() const
Definition ELF/Relocation.hpp:298
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:204
static constexpr uint64_t R_SPARC
Definition ELF/Relocation.hpp:76
friend class Binary
Definition ELF/Relocation.hpp:43
static constexpr uint64_t R_HEXAGON
Definition ELF/Relocation.hpp:70
static constexpr uint64_t R_SYSZ
Definition ELF/Relocation.hpp:77
bool is_rel() const
Check if the relocation uses the implicit addend (i.e. not present in the ELF structure).
Definition ELF/Relocation.hpp:210
ARCH architecture() const
Target architecture for this relocation.
Definition ELF/Relocation.hpp:240
Relocation(uint64_t address, TYPE type, ENCODING enc)
static constexpr uint64_t R_SH4
Definition ELF/Relocation.hpp:80
friend class Parser
Definition ELF/Relocation.hpp:42
static constexpr uint64_t R_BIT
Definition ELF/Relocation.hpp:64
Section * section()
The section in which the relocation is applied (or a nullptr).
Definition ELF/Relocation.hpp:285
static constexpr uint64_t R_ARM
Definition ELF/Relocation.hpp:69
static constexpr uint64_t R_MIPS
Definition ELF/Relocation.hpp:73
static constexpr uint64_t R_PPC
Definition ELF/Relocation.hpp:74
static constexpr uint64_t R_X64
Definition ELF/Relocation.hpp:67
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:230
static constexpr uint64_t R_LARCH
Definition ELF/Relocation.hpp:72
void addend(int64_t addend)
Definition ELF/Relocation.hpp:302
static constexpr uint64_t R_AARCH64
Definition ELF/Relocation.hpp:68
ENCODING
Definition ELF/Relocation.hpp:56
@ ANDROID_SLEB
The relocation is using the packed Android-SLEB128 format.
Definition ELF/Relocation.hpp:61
@ RELR
The relocation is using the relative relocation format.
Definition ELF/Relocation.hpp:60
@ UNKNOWN
Definition ELF/Relocation.hpp:57
@ RELA
The relocation is using the regular Elf_Rela structure.
Definition ELF/Relocation.hpp:59
@ REL
The relocation is using the regular Elf_Rel structure.
Definition ELF/Relocation.hpp:58
bool is_relatively_encoded() const
True if the relocation is using the relative encoding.
Definition ELF/Relocation.hpp:215
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
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:43
#define LIEF_LOCAL
Definition visibility.h:44