LIEF: Library to Instrument Executable Formats Version 0.17.0
|
Loading...
Searching...
No Matches
Go to the documentation of this file.
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;
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),
86 #define ELF_RELOC(name, value) name = (value | R_X64),
90 #define ELF_RELOC(name, value) name = (value | R_AARCH64),
94 #define ELF_RELOC(name, value) name = (value | R_ARM),
98 #define ELF_RELOC(name, value) name = (value | R_HEXAGON),
102 #define ELF_RELOC(name, value) name = (value | R_X86),
106 #define ELF_RELOC(name, value) name = (value | R_LARCH),
110 #define ELF_RELOC(name, value) name = (value | R_MIPS),
114 #define ELF_RELOC(name, value) name = (value | R_PPC),
118 #define ELF_RELOC(name, value) name = (value | R_PPC64),
122 #define ELF_RELOC(name, value) name = (value | R_SPARC),
126 #define ELF_RELOC(name, value) name = (value | R_SYSZ),
130 #define ELF_RELOC(name, value) name = (value | R_RISCV),
134 #define ELF_RELOC(name, value) name = (value | R_BPF),
142 return static_cast<uint32_t
>(type) & R_MASK;
149 architecture_ = arch;
152 ~Relocation()
override =
default;
153 Relocation(
const Relocation& other) :
160 LIEF::Relocation{other},
162 addend_{other.addend_},
163 encoding_{other.encoding_},
164 architecture_{other.architecture_}
166 Relocation& operator=(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 {
198 bool is_rela()
const {
202 return encoding_ == ENCODING::RELA;
204 bool is_rel()
const {
208 return encoding_ == ENCODING::REL;
210 bool is_relatively_encoded()
const {
213 return encoding_ == ENCODING::RELR;
218 return encoding_ == ENCODING::ANDROID_SLEB;
220 uint32_t info()
const {
225 uint64_t r_info(Header::CLASS clazz)
const {
228 if (clazz == Header::CLASS::NONE) {
231 return clazz == Header::CLASS::ELF32 ?
232 uint32_t(info()) << 8 | to_value(type()) :
233 uint64_t(info()) << 32 | (to_value(type()) & 0xffffffffL);
238 return architecture_;
241 PURPOSE purpose()
const {
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;
257 size_t size()
const override;
264 return symbol_ !=
nullptr;
272 const Symbol* symbol()
const {
278 return section() !=
nullptr;
286 const Section* section()
const {
292 return symbol_table_;
296 return symbol_table_;
303 void type(TYPE type) {
307 void purpose(PURPOSE purpose) {
311 void info(uint32_t v) {
315 void symbol(Symbol* symbol) {
319 void section(Section* section) {
323 void symbol_table(Section* section) {
324 symbol_table_ = section;
326 result<uint64_t> resolve(uint64_t base_address = 0)
const;
331 void accept(Visitor& visitor)
const override;
333 LIEF_API friend std::ostream& operator<<(std::ostream& os,
const Relocation& entry);
337 LIEF_LOCAL Relocation(
const T& header, PURPOSE purpose, ENCODING enc, ARCH arch);
339 TYPE type_ = TYPE::UNKNOWN;
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;
349 Binary* binary_ =
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
TYPE
The different types of the relocation.
Definition ELF/Relocation.hpp:83
Relocation(ARCH arch)
Definition ELF/Relocation.hpp:148
static uint32_t to_value(TYPE type)
Definition ELF/Relocation.hpp:141
Section * symbol_table()
The associated symbol table (or a nullptr)
Definition ELF/Relocation.hpp:291
static TYPE type_from(uint32_t value, ARCH arch)
void accept(Visitor &visitor) const override
void swap(Relocation &other)
Definition ELF/Relocation.hpp:175
ENCODING encoding() const
The encoding of the relocation.
Definition ELF/Relocation.hpp:246
bool is_android_packed() const
True if the relocation is using the Android packed relocation format.
Definition ELF/Relocation.hpp:217
bool has_symbol() const
True if the current relocation is associated with a symbol.
Definition ELF/Relocation.hpp:263
bool has_section() const
True if the relocation has an associated section.
Definition ELF/Relocation.hpp:277
const Section * symbol_table() const
Definition ELF/Relocation.hpp:295
Relocation(uint64_t address, TYPE type, ENCODING enc)
void addend(int64_t addend)
Definition ELF/Relocation.hpp:299
Class wich represents an ELF Section.
Definition ELF/Section.hpp:48
Class which represents an ELF symbol.
Definition ELF/Symbol.hpp:35
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:36
#define LIEF_API
Definition visibility.h:41
#define LIEF_LOCAL
Definition visibility.h:42