15#ifndef LIEF_ELF_RELOCATION_H
16#define LIEF_ELF_RELOCATION_H
79 static constexpr uint64_t
R_BIT = 27;
80 static constexpr uint64_t
R_MASK = (uint64_t(1) <<
R_BIT) - 1;
101 UNKNOWN = uint32_t(-1),
103#define ELF_RELOC(name, value) name = (value | R_X64),
107#define ELF_RELOC(name, value) name = (value | R_AARCH64),
111#define ELF_RELOC(name, value) name = (value | R_ARM),
115#define ELF_RELOC(name, value) name = (value | R_HEXAGON),
119#define ELF_RELOC(name, value) name = (value | R_X86),
123#define ELF_RELOC(name, value) name = (value | R_LARCH),
127#define ELF_RELOC(name, value) name = (value | R_MIPS),
131#define ELF_RELOC(name, value) name = (value | R_PPC),
135#define ELF_RELOC(name, value) name = (value | R_PPC64),
139#define ELF_RELOC(name, value) name = (value | R_SPARC),
143#define ELF_RELOC(name, value) name = (value | R_SYSZ),
147#define ELF_RELOC(name, value) name = (value | R_RISCV),
151#define ELF_RELOC(name, value) name = (value | R_BPF),
155#define ELF_RELOC(name, value) name = (value | R_SH4),
166 uint32_t sym_idx = 0;
169 uint8_t special_symbol = 0;
197 return (uint64_t(decoded.
sym_idx) << 0) |
226 architecture_(arch) {}
238 addend_{other.addend_},
239 encoding_{other.encoding_},
240 architecture_{other.architecture_},
241 metadata_{other.metadata_},
242 info_{other.info_} {}
253 std::swap(address_, other.address_);
254 std::swap(type_, other.type_);
255 std::swap(addend_, other.addend_);
256 std::swap(encoding_, other.encoding_);
257 std::swap(symbol_, other.symbol_);
258 std::swap(architecture_, other.architecture_);
259 std::swap(metadata_, other.metadata_);
260 std::swap(section_, other.section_);
261 std::swap(symbol_table_, other.symbol_table_);
262 std::swap(info_, other.info_);
263 std::swap(binary_, other.binary_);
316 return clazz == Header::CLASS::ELF32 ?
317 uint32_t(info()) << 8 | to_value(type()) :
324 logging::err(
"Failed to compute r_info: architectures mismatch: {} vs {}",
333 return architecture_;
359 return symbol_ !=
nullptr;
387 return symbol_table_;
391 return symbol_table_;
445 static_assert(
sizeof(Metadata) ==
sizeof(uint32_t));
451 uint8_t(metadata_.special_symbol),
452 uint8_t(metadata_.type2),
453 uint8_t(metadata_.type3),
457 uint8_t mips_n64_type2()
const {
458 return uint8_t(metadata_.type2);
462 PURPOSE purpose, ENCODING enc,
ARCH arch,
463 DecodedMipsN64 mips_n64) :
469 metadata_{uint32_t(
purpose), mips_n64.special_symbol, mips_n64.type2,
478 Metadata metadata_ = {};
Class which represents an ELF binary.
Definition ELF/Binary.hpp:64
Class which takes an ELF::Binary object and reconstructs a valid binary.
Definition ELF/Builder.hpp:47
Class which parses and transforms an ELF file into a ELF::Binary object.
Definition ELF/Parser.hpp:46
Class that represents an ELF relocation.
Definition ELF/Relocation.hpp:42
uint32_t info() const
Relocation info which contains, for instance, the symbol index.
Definition ELF/Relocation.hpp:299
TYPE
The different types of the relocation.
Definition ELF/Relocation.hpp:100
@ PPC64_RELATIVE
Definition ELF/Relocation.hpp:987
@ UNKNOWN
Definition ELF/Relocation.hpp:101
@ X86_64_RELATIVE
Definition ELF/Relocation.hpp:118
@ AARCH64_RELATIVE
Definition ELF/Relocation.hpp:301
@ HEX_RELATIVE
Definition ELF/Relocation.hpp:503
@ ARM_RELATIVE
Definition ELF/Relocation.hpp:341
@ PPC_RELATIVE
Definition ELF/Relocation.hpp:921
@ X86_RELATIVE
Definition ELF/Relocation.hpp:581
static constexpr uint64_t R_PPC64
Definition ELF/Relocation.hpp:91
friend std::ostream & operator<<(std::ostream &os, const Relocation &entry)
TYPE type() const
Type of the relocation.
Definition ELF/Relocation.hpp:272
PURPOSE
The purpose of a relocation defines how this relocation is used by the loader.
Definition ELF/Relocation.hpp:51
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:194
static constexpr uint64_t R_X86
Definition ELF/Relocation.hpp:87
static uint32_t to_value(TYPE type)
Definition ELF/Relocation.hpp:218
Symbol * symbol()
Symbol associated with the relocation (or a nullptr).
Definition ELF/Relocation.hpp:363
Section * symbol_table()
The associated symbol table (or a nullptr).
Definition ELF/Relocation.hpp:386
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:211
void accept(Visitor &visitor) const override
void swap(Relocation &other)
Definition ELF/Relocation.hpp:252
static DecodedMipsN64 decode_mips_n64(uint64_t r_info, Header::ELF_DATA data)
Decode r_info for MIPS n64.
Definition ELF/Relocation.hpp:179
ENCODING encoding() const
The encoding of the relocation.
Definition ELF/Relocation.hpp:341
Relocation & operator=(Relocation other)
Copy assignment operator.
Definition ELF/Relocation.hpp:247
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:305
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:267
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:336
bool is_relative() const
True if the semantic of the relocation is <ARCH>_RELATIVE.
Definition ELF/Relocation.hpp:346
~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:294
static constexpr uint64_t R_MASK
Definition ELF/Relocation.hpp:80
static constexpr uint64_t R_BPF
Definition ELF/Relocation.hpp:95
bool has_symbol() const
True if the current relocation is associated with a symbol.
Definition ELF/Relocation.hpp:358
static constexpr uint64_t R_RISCV
Definition ELF/Relocation.hpp:94
bool has_section() const
True if the relocation has an associated section.
Definition ELF/Relocation.hpp:372
friend class Builder
Definition ELF/Relocation.hpp:46
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:278
static constexpr uint64_t R_SPARC
Definition ELF/Relocation.hpp:92
friend class Binary
Definition ELF/Relocation.hpp:45
static constexpr uint64_t R_HEXAGON
Definition ELF/Relocation.hpp:86
static constexpr uint64_t R_SYSZ
Definition ELF/Relocation.hpp:93
bool is_rel() const
Check if the relocation uses the implicit addend (i.e. not present in the ELF structure).
Definition ELF/Relocation.hpp:284
ARCH architecture() const
Target architecture for this relocation.
Definition ELF/Relocation.hpp:332
Relocation(uint64_t address, TYPE type, ENCODING enc)
static constexpr uint64_t R_SH4
Definition ELF/Relocation.hpp:96
friend class Parser
Definition ELF/Relocation.hpp:44
static constexpr uint64_t R_BIT
Definition ELF/Relocation.hpp:79
Section * section()
The section in which the relocation is applied (or a nullptr).
Definition ELF/Relocation.hpp:377
static constexpr uint64_t R_ARM
Definition ELF/Relocation.hpp:85
static constexpr uint64_t R_MIPS
Definition ELF/Relocation.hpp:89
static constexpr uint64_t R_PPC
Definition ELF/Relocation.hpp:90
static constexpr uint64_t R_X64
Definition ELF/Relocation.hpp:83
static constexpr uint64_t R_LARCH
Definition ELF/Relocation.hpp:88
void addend(int64_t addend)
Definition ELF/Relocation.hpp:394
static constexpr uint64_t R_AARCH64
Definition ELF/Relocation.hpp:84
ENCODING
Definition ELF/Relocation.hpp:63
@ ANDROID_SLEB
The relocation is using the packed Android-SLEB128 format.
Definition ELF/Relocation.hpp:76
@ RELR
The relocation is using the relative relocation format.
Definition ELF/Relocation.hpp:73
@ UNKNOWN
Definition ELF/Relocation.hpp:64
@ RELA
The relocation is using the regular Elf_Rela structure.
Definition ELF/Relocation.hpp:70
@ REL
The relocation is using the regular Elf_Rel structure.
Definition ELF/Relocation.hpp:67
bool is_relatively_encoded() const
True if the relocation is using the relative encoding.
Definition ELF/Relocation.hpp:289
Class which represents an ELF Section.
Definition ELF/Section.hpp:49
Class which represents an ELF symbol.
Definition ELF/Symbol.hpp:33
Class which represents an abstracted Relocation.
Definition Abstract/Relocation.hpp:26
virtual uint64_t address() const
Relocation's address.
Definition Abstract/Relocation.hpp:46
Definition Visitor.hpp:211
Wrapper that contains an Object (T) or an error.
Definition errors.hpp:78
#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:27
const char * to_string(DynamicEntry::TAG e)
ARCH
Definition ELF/enums.hpp:27
@ MIPS
Definition ELF/enums.hpp:36
@ NONE
Definition ELF/enums.hpp:28
void err(std::string_view 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:161
uint8_t special_symbol
Special symbol used by the second relocation operation.
Definition ELF/Relocation.hpp:169
uint32_t sym_idx
Index of the symbol associated with the relocation.
Definition ELF/Relocation.hpp:166
uint8_t type3
Third relocation type.
Definition ELF/Relocation.hpp:175
uint32_t type_value
Primary relocation type.
Definition ELF/Relocation.hpp:163
uint8_t type2
Second relocation type.
Definition ELF/Relocation.hpp:172
#define LIEF_API
Definition visibility.h:44
#define LIEF_LOCAL
Definition visibility.h:45