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 static constexpr uint64_t R_SH4 = uint64_t(14) << R_BIT;
82 enum class TYPE : uint32_t {
85 UNKNOWN = uint32_t(-1),
87 #define ELF_RELOC(name, value) name = (value | R_X64),
91 #define ELF_RELOC(name, value) name = (value | R_AARCH64),
95 #define ELF_RELOC(name, value) name = (value | R_ARM),
99 #define ELF_RELOC(name, value) name = (value | R_HEXAGON),
103 #define ELF_RELOC(name, value) name = (value | R_X86),
107 #define ELF_RELOC(name, value) name = (value | R_LARCH),
111 #define ELF_RELOC(name, value) name = (value | R_MIPS),
115 #define ELF_RELOC(name, value) name = (value | R_PPC),
119 #define ELF_RELOC(name, value) name = (value | R_PPC64),
123 #define ELF_RELOC(name, value) name = (value | R_SPARC),
127 #define ELF_RELOC(name, value) name = (value | R_SYSZ),
131 #define ELF_RELOC(name, value) name = (value | R_RISCV),
135 #define ELF_RELOC(name, value) name = (value | R_BPF),
139 #define ELF_RELOC(name, value) name = (value | R_SH4),
147 return static_cast<uint32_t
>(type) & R_MASK;
150 Relocation(uint64_t address, TYPE type, ENCODING enc);
152 Relocation() =
default;
153 Relocation(ARCH arch) {
154 architecture_ = arch;
157 ~Relocation()
override =
default;
158 Relocation(
const Relocation& other) :
165 LIEF::Relocation{other},
167 addend_{other.addend_},
168 encoding_{other.encoding_},
169 architecture_{other.architecture_}
171 Relocation& operator=(Relocation other) {
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_);
207 return encoding_ == ENCODING::RELA;
213 return encoding_ == ENCODING::REL;
215 bool is_relatively_encoded()
const {
218 return encoding_ == ENCODING::RELR;
220 bool is_android_packed()
const {
223 return encoding_ == ENCODING::ANDROID_SLEB;
225 uint32_t info()
const {
230 uint64_t r_info(Header::CLASS clazz)
const {
233 if (clazz == Header::CLASS::NONE) {
236 return clazz == Header::CLASS::ELF32 ?
237 uint32_t(info()) << 8 | to_value(type()) :
238 uint64_t(info()) << 32 | (to_value(type()) & 0xffffffffL);
243 return architecture_;
246 PURPOSE purpose()
const {
249 ENCODING encoding()
const {
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;
262 size_t size()
const override;
266 bool has_symbol()
const {
269 return symbol_ !=
nullptr;
277 const Symbol* symbol()
const {
280 bool has_section()
const {
283 return section() !=
nullptr;
297 return symbol_table_;
301 return symbol_table_;
308 void type(TYPE type) {
312 void purpose(PURPOSE purpose) {
316 void info(uint32_t v) {
320 void symbol(Symbol* symbol) {
324 void section(Section* section) {
328 void symbol_table(Section* section) {
329 symbol_table_ = section;
331 result<uint64_t> resolve(uint64_t base_address = 0)
const;
336 void accept(Visitor& visitor)
const override;
338 LIEF_API friend std::ostream& operator<<(std::ostream& os,
const Relocation& entry);
342 LIEF_LOCAL Relocation(
const T& header, PURPOSE purpose, ENCODING enc, ARCH arch);
344 TYPE type_ = TYPE::UNKNOWN;
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;
354 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:84
static uint32_t to_value(TYPE type)
Definition ELF/Relocation.hpp:146
Section * symbol_table()
The associated symbol table (or a nullptr)
Definition ELF/Relocation.hpp:296
static TYPE type_from(uint32_t value, ARCH arch)
void accept(Visitor &visitor) const override
int64_t addend() const
Additional value that can be involved in the relocation processing.
Definition ELF/Relocation.hpp:195
const Section * symbol_table() const
Definition ELF/Relocation.hpp:300
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
const Section * section() const
Definition ELF/Relocation.hpp:291
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
void addend(int64_t addend)
Definition ELF/Relocation.hpp:304
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:39
#define LIEF_API
Definition visibility.h:41
#define LIEF_LOCAL
Definition visibility.h:42