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 - 2024 R. Thomas
2 * Copyright 2017 - 2024 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 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
139 static TYPE type_from(uint32_t value, ARCH arch);
140
141 static uint32_t to_value(TYPE type) {
142 return static_cast<uint32_t>(type) & R_MASK;
143 }
144
145 Relocation(uint64_t address, TYPE type, ENCODING enc);
146
147 Relocation() = default;
148 Relocation(ARCH arch) {
149 architecture_ = arch;
150 }
151
152 ~Relocation() override = default;
153 Relocation(const Relocation& other) :
160 LIEF::Relocation{other},
161 type_{other.type_},
162 addend_{other.addend_},
163 encoding_{other.encoding_},
164 architecture_{other.architecture_}
165 {}
166 Relocation& operator=(Relocation other) {
171 swap(other);
172 return *this;
173 }
174
175 void swap(Relocation& other) {
176 std::swap(address_, other.address_);
177 std::swap(type_, other.type_);
178 std::swap(addend_, other.addend_);
179 std::swap(encoding_, other.encoding_);
180 std::swap(symbol_, other.symbol_);
181 std::swap(architecture_, other.architecture_);
182 std::swap(purpose_, other.purpose_);
183 std::swap(section_, other.section_);
184 std::swap(symbol_table_, other.symbol_table_);
185 std::swap(info_, other.info_);
186 std::swap(binary_, other.binary_);
188 int64_t addend() const {
191 return addend_;
192 }
193 TYPE type() const {
196 return type_;
197 }
198 bool is_rela() const {
202 return encoding_ == ENCODING::RELA;
203 }
204 bool is_rel() const {
208 return encoding_ == ENCODING::REL;
209 }
210 bool is_relatively_encoded() const {
213 return encoding_ == ENCODING::RELR;
214 }
215 bool is_android_packed() const {
218 return encoding_ == ENCODING::ANDROID_SLEB;
219 }
220 uint32_t info() const {
223 return info_;
224 }
225 uint64_t r_info(Header::CLASS clazz) const {
228 if (clazz == Header::CLASS::NONE) {
229 return 0;
230 }
231 return clazz == Header::CLASS::ELF32 ?
232 uint32_t(info()) << 8 | to_value(type()) :
233 uint64_t(info()) << 32 | (to_value(type()) & 0xffffffffL);
234 }
235 ARCH architecture() const {
238 return architecture_;
239 }
240
241 PURPOSE purpose() const {
242 return purpose_;
243 }
244 ENCODING encoding() const {
247 return encoding_;
248 }
249 bool is_relative() const {
252 return type_ == TYPE::AARCH64_RELATIVE || type_ == TYPE::X86_64_RELATIVE ||
253 type_ == TYPE::X86_RELATIVE || type_ == TYPE::ARM_RELATIVE ||
254 type_ == TYPE::HEX_RELATIVE || type_ == TYPE::PPC64_RELATIVE ||
255 type_ == TYPE::PPC_RELATIVE;
256 }
257 size_t size() const override;
261 bool has_symbol() const {
264 return symbol_ != nullptr;
265 }
266 Symbol* symbol() {
269 return symbol_;
270 }
271
272 const Symbol* symbol() const {
273 return symbol_;
274 }
275 bool has_section() const {
278 return section() != nullptr;
279 }
280 Section* section() {
283 return section_;
284 }
285
286 const Section* section() const {
287 return section_;
288 }
289 Section* symbol_table() {
292 return symbol_table_;
293 }
294
295 const Section* symbol_table() const {
296 return symbol_table_;
297 }
298
299 void addend(int64_t addend) {
300 addend_ = addend;
301 }
302
303 void type(TYPE type) {
304 type_ = type;
305 }
306
307 void purpose(PURPOSE purpose) {
308 purpose_ = purpose;
309 }
310
311 void info(uint32_t v) {
312 info_ = v;
313 }
314
315 void symbol(Symbol* symbol) {
316 symbol_ = symbol;
317 }
318
319 void section(Section* section) {
320 section_ = section;
321 }
322
323 void symbol_table(Section* section) {
324 symbol_table_ = section;
325 }
326 result<uint64_t> resolve(uint64_t base_address = 0) const;
330
331 void accept(Visitor& visitor) const override;
332
333 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Relocation& entry);
334
335 private:
336 template<class T>
337 LIEF_LOCAL Relocation(const T& header, PURPOSE purpose, ENCODING enc, ARCH arch);
338
339 TYPE type_ = TYPE::UNKNOWN;
340 int64_t addend_ = 0;
341 ENCODING encoding_ = ENCODING::UNKNOWN;
342 Symbol* symbol_ = nullptr;
343 ARCH architecture_ = ARCH::NONE;
344 PURPOSE purpose_ = PURPOSE::NONE;
345 Section* section_ = nullptr;
346 Section* symbol_table_ = nullptr;
347 uint32_t info_ = 0;
348
349 Binary* binary_ = nullptr;
350};
351
352LIEF_API const char* to_string(Relocation::TYPE type);
353
354}
355}
356#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
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:83
LIEF::ELF::Relocation::Relocation
Relocation(ARCH arch)
Definition ELF/Relocation.hpp:148
LIEF::ELF::Relocation::to_value
static uint32_t to_value(TYPE type)
Definition ELF/Relocation.hpp:141
LIEF::ELF::Relocation::symbol_table
Section * symbol_table()
The associated symbol table (or a nullptr)
Definition ELF/Relocation.hpp:291
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::swap
void swap(Relocation &other)
Definition ELF/Relocation.hpp:175
LIEF::ELF::Relocation::encoding
ENCODING encoding() const
The encoding of the relocation.
Definition ELF/Relocation.hpp:246
LIEF::ELF::Relocation::is_android_packed
bool is_android_packed() const
True if the relocation is using the Android packed relocation format.
Definition ELF/Relocation.hpp:217
LIEF::ELF::Relocation::has_symbol
bool has_symbol() const
True if the current relocation is associated with a symbol.
Definition ELF/Relocation.hpp:263
LIEF::ELF::Relocation::has_section
bool has_section() const
True if the relocation has an associated section.
Definition ELF/Relocation.hpp:277
LIEF::ELF::Relocation::symbol_table
const Section * symbol_table() const
Definition ELF/Relocation.hpp:295
LIEF::ELF::Relocation::Relocation
Relocation(uint64_t address, TYPE type, ENCODING enc)
LIEF::ELF::Relocation::Relocation
Relocation()=default
LIEF::ELF::Relocation::addend
void addend(int64_t addend)
Definition ELF/Relocation.hpp:299
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:36
visibility.h
LIEF_API
#define LIEF_API
Definition visibility.h:41
LIEF_LOCAL
#define LIEF_LOCAL
Definition visibility.h:42
x86_64.def