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;
38class LIEF_API Relocation : public LIEF::Relocation {
41
42 friend class Parser;
43 friend class Binary;
44 friend class Builder;
45
46 public:
47 enum class PURPOSE {
51 NONE = 0,
52 PLTGOT = 1,
53 DYNAMIC = 2,
54 OBJECT = 3,
55 };
56
57 enum class ENCODING {
58 UNKNOWN = 0,
59 REL,
60 RELA,
61 RELR,
62 ANDROID_SLEB,
63 };
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 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
150 Relocation(uint64_t address, TYPE type, ENCODING enc);
151
152 Relocation() = default;
153 Relocation(ARCH arch) {
154 architecture_ = arch;
155 }
156
157 ~Relocation() override = default;
158 Relocation(const Relocation& other) :
165 LIEF::Relocation{other},
166 type_{other.type_},
167 addend_{other.addend_},
168 encoding_{other.encoding_},
169 architecture_{other.architecture_}
170 {}
171 Relocation& operator=(Relocation other) {
176 swap(other);
177 return *this;
178 }
179
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_);
192 }
193 int64_t addend() const {
196 return addend_;
197 }
198 TYPE type() const {
201 return type_;
202 }
203 bool is_rela() const {
207 return encoding_ == ENCODING::RELA;
208 }
209 bool is_rel() const {
213 return encoding_ == ENCODING::REL;
214 }
215 bool is_relatively_encoded() const {
218 return encoding_ == ENCODING::RELR;
219 }
220 bool is_android_packed() const {
223 return encoding_ == ENCODING::ANDROID_SLEB;
224 }
225 uint32_t info() const {
228 return info_;
229 }
230 uint64_t r_info(Header::CLASS clazz) const {
233 if (clazz == Header::CLASS::NONE) {
234 return 0;
235 }
236 return clazz == Header::CLASS::ELF32 ?
237 uint32_t(info()) << 8 | to_value(type()) :
238 uint64_t(info()) << 32 | (to_value(type()) & 0xffffffffL);
239 }
240 ARCH architecture() const {
243 return architecture_;
244 }
245
246 PURPOSE purpose() const {
247 return purpose_;
248 }
249 ENCODING encoding() const {
252 return encoding_;
253 }
254 bool is_relative() const {
257 return type_ == TYPE::AARCH64_RELATIVE || type_ == TYPE::X86_64_RELATIVE ||
258 type_ == TYPE::X86_RELATIVE || type_ == TYPE::ARM_RELATIVE ||
259 type_ == TYPE::HEX_RELATIVE || type_ == TYPE::PPC64_RELATIVE ||
260 type_ == TYPE::PPC_RELATIVE;
261 }
262 size_t size() const override;
266 bool has_symbol() const {
269 return symbol_ != nullptr;
270 }
271 Symbol* symbol() {
274 return symbol_;
275 }
276
277 const Symbol* symbol() const {
278 return symbol_;
279 }
280 bool has_section() const {
283 return section() != nullptr;
284 }
285 Section* section() {
288 return section_;
289 }
290
291 const Section* section() const {
292 return section_;
293 }
294 Section* symbol_table() {
297 return symbol_table_;
298 }
299
300 const Section* symbol_table() const {
301 return symbol_table_;
302 }
303
304 void addend(int64_t addend) {
305 addend_ = addend;
306 }
307
308 void type(TYPE type) {
309 type_ = type;
310 }
311
312 void purpose(PURPOSE purpose) {
313 purpose_ = purpose;
314 }
315
316 void info(uint32_t v) {
317 info_ = v;
318 }
319
320 void symbol(Symbol* symbol) {
321 symbol_ = symbol;
322 }
323
324 void section(Section* section) {
325 section_ = section;
326 }
327
328 void symbol_table(Section* section) {
329 symbol_table_ = section;
330 }
331 result<uint64_t> resolve(uint64_t base_address = 0) const;
335
336 void accept(Visitor& visitor) const override;
337
338 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Relocation& entry);
339
340 private:
341 template<class T>
342 LIEF_LOCAL Relocation(const T& header, PURPOSE purpose, ENCODING enc, ARCH arch);
343
344 TYPE type_ = TYPE::UNKNOWN;
345 int64_t addend_ = 0;
346 ENCODING encoding_ = ENCODING::UNKNOWN;
347 Symbol* symbol_ = nullptr;
348 ARCH architecture_ = ARCH::NONE;
349 PURPOSE purpose_ = PURPOSE::NONE;
350 Section* section_ = nullptr;
351 Section* symbol_table_ = nullptr;
352 uint32_t info_ = 0;
353
354 Binary* binary_ = nullptr;
355};
356
357LIEF_API const char* to_string(Relocation::TYPE type);
358
359}
360}
361#endif
AArch64.def
ARM.def
Relocation.hpp
BPF.def
Header.hpp
enums.hpp
Hexagon.def
LoongArch.def
Mips.def
Object.hpp
PowerPC64.def
PowerPC.def
RISCV.def
SH4.def
Sparc.def
SystemZ.def
LIEF::ELF::Binary
Class which represents an ELF binary.
Definition ELF/Binary.hpp:59
LIEF::ELF::Builder
Class which takes an ELF::Binary object and reconstructs a valid binary.
Definition ELF/Builder.hpp:48
LIEF::ELF::Parser
Class which parses and transforms an ELF file into a ELF::Binary object.
Definition ELF/Parser.hpp:45
LIEF::ELF::Relocation
Class that represents an ELF relocation.
Definition ELF/Relocation.hpp:40
LIEF::ELF::Relocation::TYPE
TYPE
The different types of the relocation.
Definition ELF/Relocation.hpp:84
LIEF::ELF::Relocation::to_value
static uint32_t to_value(TYPE type)
Definition ELF/Relocation.hpp:146
LIEF::ELF::Relocation::symbol_table
Section * symbol_table()
The associated symbol table (or a nullptr)
Definition ELF/Relocation.hpp:296
LIEF::ELF::Relocation::type_from
static TYPE type_from(uint32_t value, ARCH arch)
LIEF::ELF::Relocation::accept
void accept(Visitor &visitor) const override
LIEF::ELF::Relocation::addend
int64_t addend() const
Additional value that can be involved in the relocation processing.
Definition ELF/Relocation.hpp:195
LIEF::ELF::Relocation::symbol_table
const Section * symbol_table() const
Definition ELF/Relocation.hpp:300
LIEF::ELF::Relocation::is_rela
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
LIEF::ELF::Relocation::section
const Section * section() const
Definition ELF/Relocation.hpp:291
LIEF::ELF::Relocation::is_rel
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
LIEF::ELF::Relocation::addend
void addend(int64_t addend)
Definition ELF/Relocation.hpp:304
LIEF::ELF::Section
Class wich represents an ELF Section.
Definition ELF/Section.hpp:48
LIEF::ELF::Symbol
Class which represents an ELF symbol.
Definition ELF/Symbol.hpp:35
errors.hpp
i386.def
LIEF::ELF
Namespace related to the LIEF's ELF module.
Definition Abstract/Header.hpp:28
LIEF::ELF::to_string
const char * to_string(DynamicEntry::TAG e)
LIEF::ELF::ARCH
ARCH
Definition ELF/enums.hpp:30
LIEF::ELF::ARCH::NONE
@ NONE
Definition ELF/enums.hpp:31
LIEF
LIEF namespace.
Definition Abstract/Binary.hpp:39
visibility.h
LIEF_API
#define LIEF_API
Definition visibility.h:41
LIEF_LOCAL
#define LIEF_LOCAL
Definition visibility.h:42
x86_64.def