16#ifndef LIEF_ELF_RELOCATION_H
17#define LIEF_ELF_RELOCATION_H
65 static constexpr uint64_t R_BIT = 27;
66 static constexpr uint64_t R_MASK = (uint64_t(1) << R_BIT) - 1;
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;
81 enum class TYPE : uint32_t {
82 UNKNOWN = uint32_t(-1),
84 #define ELF_RELOC(name, value) name = (value | R_X64),
88 #define ELF_RELOC(name, value) name = (value | R_AARCH64),
92 #define ELF_RELOC(name, value) name = (value | R_ARM),
96 #define ELF_RELOC(name, value) name = (value | R_HEXAGON),
100 #define ELF_RELOC(name, value) name = (value | R_X86),
104 #define ELF_RELOC(name, value) name = (value | R_LARCH),
108 #define ELF_RELOC(name, value) name = (value | R_MIPS),
112 #define ELF_RELOC(name, value) name = (value | R_PPC),
116 #define ELF_RELOC(name, value) name = (value | R_PPC64),
120 #define ELF_RELOC(name, value) name = (value | R_SPARC),
124 #define ELF_RELOC(name, value) name = (value | R_SYSZ),
132 return static_cast<uint32_t
>(type) & R_MASK;
139 architecture_ = arch;
160 return encoding_ == ENCODING::RELA;
166 return encoding_ == ENCODING::REL;
171 return encoding_ == ENCODING::RELR;
176 return encoding_ == ENCODING::ANDROID_SLEB;
186 if (clazz == Header::CLASS::NONE) {
189 return clazz == Header::CLASS::ELF32 ?
190 uint32_t(info()) << 8 | to_value(type()) :
191 uint64_t(info()) << 32 | (to_value(type()) & 0xffffffffL);
196 return architecture_;
210 return type_ == TYPE::AARCH64_RELATIVE || type_ == TYPE::X86_64_RELATIVE ||
211 type_ == TYPE::X86_RELATIVE || type_ == TYPE::ARM_RELATIVE ||
212 type_ == TYPE::HEX_RELATIVE || type_ == TYPE::PPC64_RELATIVE ||
213 type_ == TYPE::PPC_RELATIVE;
222 return symbol_ !=
nullptr;
236 return section() !=
nullptr;
250 return symbol_table_;
254 return symbol_table_;
282 symbol_table_ = section;
293 TYPE type_ = TYPE::UNKNOWN;
295 ENCODING encoding_ = ENCODING::UNKNOWN;
296 Symbol* symbol_ =
nullptr;
297 ARCH architecture_ = ARCH::NONE;
298 PURPOSE purpose_ = PURPOSE::NONE;
300 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:48
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:180
TYPE
The different types of the relocation.
Definition ELF/Relocation.hpp:81
friend std::ostream & operator<<(std::ostream &os, const Relocation &entry)
TYPE type() const
Type of the relocation.
Definition ELF/Relocation.hpp:153
PURPOSE
The purpose of a relocation defines how this relocation is used by the loader.
Definition ELF/Relocation.hpp:50
Relocation(ARCH arch)
Definition ELF/Relocation.hpp:138
static uint32_t to_value(TYPE type)
Definition ELF/Relocation.hpp:131
Symbol * symbol()
Symbol associated with the relocation (or a nullptr)
Definition ELF/Relocation.hpp:226
Section * symbol_table()
The associated symbol table (or a nullptr)
Definition ELF/Relocation.hpp:249
static TYPE type_from(uint32_t value, ARCH arch)
void accept(Visitor &visitor) const override
void swap(Relocation &other)
ENCODING encoding() const
The encoding of the relocation.
Definition ELF/Relocation.hpp:204
void symbol(Symbol *symbol)
Definition ELF/Relocation.hpp:273
Relocation & operator=(Relocation other)
int64_t addend() const
Additional value that can be involved in the relocation processing.
Definition ELF/Relocation.hpp:148
PURPOSE purpose() const
Definition ELF/Relocation.hpp:199
bool is_relative() const
True if the semantic of the relocation is <ARCH>_RELATIVE
Definition ELF/Relocation.hpp:209
~Relocation() override=default
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:175
Relocation(const Relocation &other)
bool has_symbol() const
True if the current relocation is associated with a symbol.
Definition ELF/Relocation.hpp:221
bool has_section() const
True if the relocation has an associated section.
Definition ELF/Relocation.hpp:235
const Section * symbol_table() const
Definition ELF/Relocation.hpp:253
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:159
const Section * section() const
Definition ELF/Relocation.hpp:244
void info(uint32_t v)
Definition ELF/Relocation.hpp:269
bool is_rel() const
Check if the relocation uses the implicit addend (i.e. not present in the ELF structure)
Definition ELF/Relocation.hpp:165
void symbol_table(Section *section)
Definition ELF/Relocation.hpp:281
ARCH architecture() const
Target architecture for this relocation.
Definition ELF/Relocation.hpp:195
Relocation(uint64_t address, TYPE type, ENCODING enc)
const Symbol * symbol() const
Definition ELF/Relocation.hpp:230
void section(Section *section)
Definition ELF/Relocation.hpp:277
Section * section()
The section in which the relocation is applied (or a nullptr)
Definition ELF/Relocation.hpp:240
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:185
void addend(int64_t addend)
Definition ELF/Relocation.hpp:257
void type(TYPE type)
Definition ELF/Relocation.hpp:261
void purpose(PURPOSE purpose)
Definition ELF/Relocation.hpp:265
ENCODING
Definition ELF/Relocation.hpp:57
bool is_relatively_encoded() const
True if the relocation is using the relative encoding.
Definition ELF/Relocation.hpp:170
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
Definition Visitor.hpp:224
const char * to_string(DynamicEntry::TAG e)
ARCH
Machine architectures See current registered ELF machine architectures at: http://www....
Definition ELF/enums.hpp:30
LIEF namespace.
Definition Abstract/Binary.hpp:32
#define LIEF_API
Definition visibility.h:41
#define LIEF_LOCAL
Definition visibility.h:42