16#ifndef LIEF_ELF_RELOCATION_H
17#define LIEF_ELF_RELOCATION_H
80 static constexpr uint64_t
R_BIT = 27;
81 static constexpr uint64_t
R_MASK = (uint64_t(1) <<
R_BIT) - 1;
102 UNKNOWN = uint32_t(-1),
104#define ELF_RELOC(name, value) name = (value | R_X64),
108#define ELF_RELOC(name, value) name = (value | R_AARCH64),
112#define ELF_RELOC(name, value) name = (value | R_ARM),
116#define ELF_RELOC(name, value) name = (value | R_HEXAGON),
120#define ELF_RELOC(name, value) name = (value | R_X86),
124#define ELF_RELOC(name, value) name = (value | R_LARCH),
128#define ELF_RELOC(name, value) name = (value | R_MIPS),
132#define ELF_RELOC(name, value) name = (value | R_PPC),
136#define ELF_RELOC(name, value) name = (value | R_PPC64),
140#define ELF_RELOC(name, value) name = (value | R_SPARC),
144#define ELF_RELOC(name, value) name = (value | R_SYSZ),
148#define ELF_RELOC(name, value) name = (value | R_RISCV),
152#define ELF_RELOC(name, value) name = (value | R_BPF),
156#define ELF_RELOC(name, value) name = (value | R_SH4),
167 uint32_t sym_idx = 0;
170 uint8_t special_symbol = 0;
198 return (uint64_t(decoded.
sym_idx) << 0) |
227 architecture_(arch) {}
239 addend_{other.addend_},
240 encoding_{other.encoding_},
241 architecture_{other.architecture_},
242 metadata_{other.metadata_},
243 info_{other.info_} {}
254 std::swap(address_, other.address_);
255 std::swap(type_, other.type_);
256 std::swap(addend_, other.addend_);
257 std::swap(encoding_, other.encoding_);
258 std::swap(symbol_, other.symbol_);
259 std::swap(architecture_, other.architecture_);
260 std::swap(metadata_, other.metadata_);
261 std::swap(section_, other.section_);
262 std::swap(symbol_table_, other.symbol_table_);
263 std::swap(info_, other.info_);
264 std::swap(binary_, other.binary_);
317 return clazz == Header::CLASS::ELF32 ?
318 uint32_t(info()) << 8 | to_value(type()) :
325 logging::err(
"Failed to compute r_info: architectures mismatch: {} vs {}",
334 return architecture_;
360 return symbol_ !=
nullptr;
388 return symbol_table_;
392 return symbol_table_;
446 static_assert(
sizeof(Metadata) ==
sizeof(uint32_t));
452 uint8_t(metadata_.special_symbol),
453 uint8_t(metadata_.type2),
454 uint8_t(metadata_.type3),
458 uint8_t mips_n64_type2()
const {
459 return uint8_t(metadata_.type2);
463 PURPOSE purpose, ENCODING enc,
ARCH arch,
464 DecodedMipsN64 mips_n64) :
470 metadata_{uint32_t(
purpose), mips_n64.special_symbol, mips_n64.type2,
479 Metadata metadata_ = {};
Class which represents an ELF binary.
Definition ELF/Binary.hpp:61
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:47
Class that represents an ELF relocation.
Definition ELF/Relocation.hpp:43
uint32_t info() const
Relocation info which contains, for instance, the symbol index.
Definition ELF/Relocation.hpp:300
TYPE
The different types of the relocation.
Definition ELF/Relocation.hpp:101
@ PPC64_RELATIVE
Definition ELF/Relocation.hpp:988
@ UNKNOWN
Definition ELF/Relocation.hpp:102
@ X86_64_RELATIVE
Definition ELF/Relocation.hpp:119
@ AARCH64_RELATIVE
Definition ELF/Relocation.hpp:302
@ HEX_RELATIVE
Definition ELF/Relocation.hpp:504
@ ARM_RELATIVE
Definition ELF/Relocation.hpp:342
@ PPC_RELATIVE
Definition ELF/Relocation.hpp:922
@ X86_RELATIVE
Definition ELF/Relocation.hpp:582
static constexpr uint64_t R_PPC64
Definition ELF/Relocation.hpp:92
friend std::ostream & operator<<(std::ostream &os, const Relocation &entry)
TYPE type() const
Type of the relocation.
Definition ELF/Relocation.hpp:273
PURPOSE
The purpose of a relocation defines how this relocation is used by the loader.
Definition ELF/Relocation.hpp:52
static uint64_t encode_mips_n64(DecodedMipsN64 decoded, Header::ELF_DATA data)
Encode all the fields of a MIPS n64 relocation into a r_info value.
Definition ELF/Relocation.hpp:195
static constexpr uint64_t R_X86
Definition ELF/Relocation.hpp:88
static uint32_t to_value(TYPE type)
Definition ELF/Relocation.hpp:219
Symbol * symbol()
Symbol associated with the relocation (or a nullptr).
Definition ELF/Relocation.hpp:364
Section * symbol_table()
The associated symbol table (or a nullptr).
Definition ELF/Relocation.hpp:387
static TYPE type_from(uint32_t value, ARCH arch)
static uint64_t encode_mips_n64(uint32_t type_value, uint32_t sym_idx, Header::ELF_DATA data)
Definition ELF/Relocation.hpp:212
void accept(Visitor &visitor) const override
void swap(Relocation &other)
Definition ELF/Relocation.hpp:253
static DecodedMipsN64 decode_mips_n64(uint64_t r_info, Header::ELF_DATA data)
Decode r_info for MIPS n64.
Definition ELF/Relocation.hpp:180
ENCODING encoding() const
The encoding of the relocation.
Definition ELF/Relocation.hpp:342
Relocation & operator=(Relocation other)
Copy assignment operator.
Definition ELF/Relocation.hpp:248
uint64_t r_info(Header::CLASS clazz, Header::ELF_DATA data) const
(re)Compute the raw r_info attribute based on the given ELF class and endianness.
Definition ELF/Relocation.hpp:306
static std::unique_ptr< Relocation > create(const T &header, PURPOSE purpose, ENCODING enc, const Header &elf_hdr)
int64_t addend() const
Additional value that can be involved in the relocation processing.
Definition ELF/Relocation.hpp:268
result< uint64_t > resolve(uint64_t base_address=0) const
Try to resolve the value of the relocation such as *address() = resolve().
PURPOSE purpose() const
Definition ELF/Relocation.hpp:337
bool is_relative() const
True if the semantic of the relocation is <ARCH>_RELATIVE.
Definition ELF/Relocation.hpp:347
~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:295
static constexpr uint64_t R_MASK
Definition ELF/Relocation.hpp:81
static constexpr uint64_t R_BPF
Definition ELF/Relocation.hpp:96
bool has_symbol() const
True if the current relocation is associated with a symbol.
Definition ELF/Relocation.hpp:359
static constexpr uint64_t R_RISCV
Definition ELF/Relocation.hpp:95
bool has_section() const
True if the relocation has an associated section.
Definition ELF/Relocation.hpp:373
friend class Builder
Definition ELF/Relocation.hpp:47
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:279
static constexpr uint64_t R_SPARC
Definition ELF/Relocation.hpp:93
friend class Binary
Definition ELF/Relocation.hpp:46
static constexpr uint64_t R_HEXAGON
Definition ELF/Relocation.hpp:87
static constexpr uint64_t R_SYSZ
Definition ELF/Relocation.hpp:94
bool is_rel() const
Check if the relocation uses the implicit addend (i.e. not present in the ELF structure).
Definition ELF/Relocation.hpp:285
ARCH architecture() const
Target architecture for this relocation.
Definition ELF/Relocation.hpp:333
Relocation(uint64_t address, TYPE type, ENCODING enc)
static constexpr uint64_t R_SH4
Definition ELF/Relocation.hpp:97
friend class Parser
Definition ELF/Relocation.hpp:45
static constexpr uint64_t R_BIT
Definition ELF/Relocation.hpp:80
Section * section()
The section in which the relocation is applied (or a nullptr).
Definition ELF/Relocation.hpp:378
static constexpr uint64_t R_ARM
Definition ELF/Relocation.hpp:86
static constexpr uint64_t R_MIPS
Definition ELF/Relocation.hpp:90
static constexpr uint64_t R_PPC
Definition ELF/Relocation.hpp:91
static constexpr uint64_t R_X64
Definition ELF/Relocation.hpp:84
static constexpr uint64_t R_LARCH
Definition ELF/Relocation.hpp:89
void addend(int64_t addend)
Definition ELF/Relocation.hpp:395
static constexpr uint64_t R_AARCH64
Definition ELF/Relocation.hpp:85
ENCODING
Definition ELF/Relocation.hpp:64
@ ANDROID_SLEB
The relocation is using the packed Android-SLEB128 format.
Definition ELF/Relocation.hpp:77
@ RELR
The relocation is using the relative relocation format.
Definition ELF/Relocation.hpp:74
@ UNKNOWN
Definition ELF/Relocation.hpp:65
@ RELA
The relocation is using the regular Elf_Rela structure.
Definition ELF/Relocation.hpp:71
@ REL
The relocation is using the regular Elf_Rel structure.
Definition ELF/Relocation.hpp:68
bool is_relatively_encoded() const
True if the relocation is using the relative encoding.
Definition ELF/Relocation.hpp:290
Class which represents an ELF Section.
Definition ELF/Section.hpp:49
Class which represents an ELF symbol.
Definition ELF/Symbol.hpp:34
Class which represents an abstracted Relocation.
Definition Abstract/Relocation.hpp:27
virtual uint64_t address() const
Relocation's address.
Definition Abstract/Relocation.hpp:47
Definition Visitor.hpp:212
Wrapper that contains an Object (T) or an error.
Definition errors.hpp:79
#define LIEF_LIFETIMEBOUND
Definition compiler_attributes.hpp:72
const char * to_string(lief_errors err)
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:28
@ MIPS
Definition ELF/enums.hpp:37
@ NONE
Definition ELF/enums.hpp:29
void err(const std::string &msg)
Definition logging.hpp:143
LIEF namespace.
Definition Abstract/Binary.hpp:41
Fields decoded from the MIPS-specific n64 r_info layout.
Definition ELF/Relocation.hpp:162
uint8_t special_symbol
Special symbol used by the second relocation operation.
Definition ELF/Relocation.hpp:170
uint32_t sym_idx
Index of the symbol associated with the relocation.
Definition ELF/Relocation.hpp:167
uint8_t type3
Third relocation type.
Definition ELF/Relocation.hpp:176
uint32_t type_value
Primary relocation type.
Definition ELF/Relocation.hpp:164
uint8_t type2
Second relocation type.
Definition ELF/Relocation.hpp:173
#define LIEF_API
Definition visibility.h:45
#define LIEF_LOCAL
Definition visibility.h:46