16#ifndef LIEF_ELF_RELOCATION_H
17#define LIEF_ELF_RELOCATION_H
22#include "LIEF/Object.hpp"
23#include "LIEF/visibility.h"
25#include "LIEF/Abstract/Relocation.hpp"
27#include "LIEF/ELF/enums.hpp"
28#include "LIEF/ELF/Header.hpp"
64 static constexpr uint64_t R_BIT = 27;
65 static constexpr uint64_t R_MASK = (uint64_t(1) << R_BIT) - 1;
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;
80 enum class TYPE : uint32_t {
81 UNKNOWN = uint32_t(-1),
83 #define ELF_RELOC(name, value) name = (value | R_X64),
84 #include "LIEF/ELF/Relocations/x86_64.def"
87 #define ELF_RELOC(name, value) name = (value | R_AARCH64),
88 #include "LIEF/ELF/Relocations/AArch64.def"
91 #define ELF_RELOC(name, value) name = (value | R_ARM),
92 #include "LIEF/ELF/Relocations/ARM.def"
95 #define ELF_RELOC(name, value) name = (value | R_HEXAGON),
96 #include "LIEF/ELF/Relocations/Hexagon.def"
99 #define ELF_RELOC(name, value) name = (value | R_X86),
100 #include "LIEF/ELF/Relocations/i386.def"
103 #define ELF_RELOC(name, value) name = (value | R_LARCH),
104 #include "LIEF/ELF/Relocations/LoongArch.def"
107 #define ELF_RELOC(name, value) name = (value | R_MIPS),
108 #include "LIEF/ELF/Relocations/Mips.def"
111 #define ELF_RELOC(name, value) name = (value | R_PPC),
112 #include "LIEF/ELF/Relocations/PowerPC.def"
115 #define ELF_RELOC(name, value) name = (value | R_PPC64),
116 #include "LIEF/ELF/Relocations/PowerPC64.def"
119 #define ELF_RELOC(name, value) name = (value | R_SPARC),
120 #include "LIEF/ELF/Relocations/Sparc.def"
123 #define ELF_RELOC(name, value) name = (value | R_SYSZ),
124 #include "LIEF/ELF/Relocations/SystemZ.def"
128 static TYPE type_from(uint32_t value,
ARCH arch);
130 static uint32_t to_value(TYPE type) {
131 return static_cast<uint32_t
>(type) & R_MASK;
134 Relocation(uint64_t address, TYPE type, ENCODING enc);
138 architecture_ = arch;
140 ~Relocation()
override =
default;
142 Relocation& operator=(Relocation other);
143 Relocation(
const Relocation& other);
144 void swap(Relocation& other);
159 return encoding_ == ENCODING::RELA;
165 return encoding_ == ENCODING::REL;
170 return encoding_ == ENCODING::RELR;
175 return encoding_ == ENCODING::ANDROID_SLEB;
185 if (clazz == Header::CLASS::NONE) {
188 return clazz == Header::CLASS::ELF32 ?
189 uint32_t(info()) << 8 | to_value(type()) :
190 uint64_t(info()) << 32 | (to_value(type()) & 0xffffffffL);
195 return architecture_;
198 PURPOSE purpose()
const {
209 return type_ == TYPE::AARCH64_RELATIVE || type_ == TYPE::X86_64_RELATIVE ||
210 type_ == TYPE::X86_RELATIVE || type_ == TYPE::ARM_RELATIVE ||
211 type_ == TYPE::HEX_RELATIVE || type_ == TYPE::PPC64_RELATIVE ||
212 type_ == TYPE::PPC_RELATIVE;
221 return symbol_ !=
nullptr;
229 const Symbol* symbol()
const {
235 return section() !=
nullptr;
243 const Section* section()
const {
249 return symbol_table_;
252 const Section* symbol_table()
const {
253 return symbol_table_;
256 void addend(int64_t addend) {
260 void type(TYPE type) {
264 void purpose(PURPOSE purpose) {
268 void info(uint32_t v) {
272 void symbol(Symbol* symbol) {
276 void section(Section* section) {
280 void symbol_table(Section* section) {
281 symbol_table_ = section;
284 void accept(Visitor& visitor)
const override;
286 LIEF_API
friend std::ostream& operator<<(std::ostream& os,
const Relocation& entry);
290 LIEF_LOCAL Relocation(
const T& header, PURPOSE purpose, ENCODING enc, ARCH arch);
292 TYPE type_ = TYPE::UNKNOWN;
294 ENCODING encoding_ = ENCODING::UNKNOWN;
295 Symbol* symbol_ =
nullptr;
296 ARCH architecture_ = ARCH::NONE;
297 PURPOSE purpose_ = PURPOSE::NONE;
298 Section* section_ =
nullptr;
299 Section* symbol_table_ =
nullptr;
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:51
Class which parses and transforms an ELF file into a ELF::Binary object.
Definition ELF/Parser.hpp:45
Class that represents an ELF relocation.
Definition ELF/Relocation.hpp:40
uint32_t info() const
Relocation info which contains, for instance, the symbol index.
Definition ELF/Relocation.hpp:179
TYPE
The different types of the relocation.
Definition ELF/Relocation.hpp:80
TYPE type() const
Type of the relocation.
Definition ELF/Relocation.hpp:152
PURPOSE
The purpose of a relocation defines.
Definition ELF/Relocation.hpp:49
Symbol * symbol()
Symbol associated with the relocation (or a nullptr)
Definition ELF/Relocation.hpp:225
Section * symbol_table()
The associated symbol table (or a nullptr)
Definition ELF/Relocation.hpp:248
ENCODING encoding() const
The encoding of the relocation.
Definition ELF/Relocation.hpp:203
int64_t addend() const
Additional value that can be involved in the relocation processing.
Definition ELF/Relocation.hpp:147
bool is_relative() const
True if the semantic of the relocation is <ARCH>_RELATIVE
Definition ELF/Relocation.hpp:208
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:174
bool has_symbol() const
True if the current relocation is associated with a symbol.
Definition ELF/Relocation.hpp:220
bool has_section() const
True if the relocation has an associated section.
Definition ELF/Relocation.hpp:234
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:158
bool is_rel() const
Check if the relocation uses the implicit addend (i.e. not present in the ELF structure)
Definition ELF/Relocation.hpp:164
ARCH architecture() const
Target architecture for this relocation.
Definition ELF/Relocation.hpp:194
Section * section()
The section in which the relocation is applied (or a nullptr)
Definition ELF/Relocation.hpp:239
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:184
ENCODING
Definition ELF/Relocation.hpp:56
bool is_relatively_encoded() const
True if the relocation is using the relative encoding.
Definition ELF/Relocation.hpp:169
Class wich represents an ELF Section.
Definition ELF/Section.hpp:46
Class which represents an ELF symbol.
Definition ELF/Symbol.hpp:35
Class which represents an abstracted Relocation.
Definition Abstract/Relocation.hpp:27
ARCH
Machine architectures See current registered ELF machine architectures at: http://www....
Definition ELF/enums.hpp:30
LIEF namespace.
Definition Abstract/Binary.hpp:32