17 #define STRINGIFY_ENUM_CASE(ns, name) \ 21 #define ELF_RELOC(name, value) STRINGIFY_ENUM_CASE(ELF, name) 28 #include "llvm/BinaryFormat/ELFRelocs/x86_64.def" 36 #include "llvm/BinaryFormat/ELFRelocs/i386.def" 43 #include "llvm/BinaryFormat/ELFRelocs/Mips.def" 50 #include "llvm/BinaryFormat/ELFRelocs/AArch64.def" 57 #include "llvm/BinaryFormat/ELFRelocs/ARM.def" 65 #include "llvm/BinaryFormat/ELFRelocs/ARC.def" 72 #include "llvm/BinaryFormat/ELFRelocs/AVR.def" 79 #include "llvm/BinaryFormat/ELFRelocs/Hexagon.def" 86 #include "llvm/BinaryFormat/ELFRelocs/Lanai.def" 93 #include "llvm/BinaryFormat/ELFRelocs/PowerPC.def" 100 #include "llvm/BinaryFormat/ELFRelocs/PowerPC64.def" 107 #include "llvm/BinaryFormat/ELFRelocs/RISCV.def" 114 #include "llvm/BinaryFormat/ELFRelocs/SystemZ.def" 123 #include "llvm/BinaryFormat/ELFRelocs/Sparc.def" 130 #include "llvm/BinaryFormat/ELFRelocs/AMDGPU.def" 137 #include "llvm/BinaryFormat/ELFRelocs/BPF.def" 153 return ELF::R_X86_64_RELATIVE;
156 return ELF::R_386_RELATIVE;
160 return ELF::R_AARCH64_RELATIVE;
162 return ELF::R_ARM_RELATIVE;
165 return ELF::R_ARC_RELATIVE;
169 return ELF::R_HEX_RELATIVE;
175 return ELF::R_PPC64_RELATIVE;
177 return ELF::R_RISCV_RELATIVE;
179 return ELF::R_390_RELATIVE;
183 return ELF::R_SPARC_RELATIVE;
260 template <
class ELFT>
296 Rela.setType(getRelrRelocationType(),
false);
297 std::vector<Elf_Rela> Relocs;
300 typedef typename ELFT::uint
Word;
303 const size_t WordSize =
sizeof(
Word);
307 const size_t NBits = 8*WordSize - 1;
312 if ((Entry&1) == 0) {
314 Rela.r_offset = Entry;
315 Relocs.push_back(Rela);
317 Base = Entry + WordSize;
325 if ((Entry&1) != 0) {
327 Relocs.push_back(Rela);
333 Base += NBits * WordSize;
339 template <
class ELFT>
347 const uint8_t *Cur = ContentsOrErr->begin();
348 const uint8_t *End = ContentsOrErr->end();
349 if (ContentsOrErr->size() < 4 || Cur[0] !=
'A' || Cur[1] !=
'P' ||
350 Cur[2] !=
'S' || Cur[3] !=
'2')
351 return createError(
"invalid packed relocation header");
354 const char *ErrStr =
nullptr;
355 auto ReadSLEB = [&]() -> int64_t {
364 uint64_t NumRelocs = ReadSLEB();
365 uint64_t
Offset = ReadSLEB();
371 std::vector<Elf_Rela> Relocs;
372 Relocs.reserve(NumRelocs);
374 uint64_t NumRelocsInGroup = ReadSLEB();
375 if (NumRelocsInGroup > NumRelocs)
376 return createError(
"relocation group unexpectedly large");
377 NumRelocs -= NumRelocsInGroup;
379 uint64_t GroupFlags = ReadSLEB();
385 uint64_t GroupOffsetDelta;
386 if (GroupedByOffsetDelta)
387 GroupOffsetDelta = ReadSLEB();
391 GroupRInfo = ReadSLEB();
393 if (GroupedByAddend && GroupHasAddend)
394 Addend += ReadSLEB();
396 for (uint64_t
I = 0;
I != NumRelocsInGroup; ++
I) {
398 Offset += GroupedByOffsetDelta ? GroupOffsetDelta : ReadSLEB();
400 R.r_info = GroupedByInfo ? GroupRInfo : ReadSLEB();
402 if (GroupHasAddend) {
403 if (!GroupedByAddend)
404 Addend += ReadSLEB();
423 template <
class ELFT>
425 uint64_t Type)
const {
426 #define DYNAMIC_STRINGIFY_ENUM(tag, value) \ 430 #define DYNAMIC_TAG(n, v) 434 #define HEXAGON_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value) 435 #include "llvm/BinaryFormat/DynamicTags.def" 436 #undef HEXAGON_DYNAMIC_TAG 441 #define MIPS_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value) 442 #include "llvm/BinaryFormat/DynamicTags.def" 443 #undef MIPS_DYNAMIC_TAG 448 #define PPC64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value) 449 #include "llvm/BinaryFormat/DynamicTags.def" 450 #undef PPC64_DYNAMIC_TAG 456 #define MIPS_DYNAMIC_TAG(name, value) 457 #define HEXAGON_DYNAMIC_TAG(name, value) 458 #define PPC64_DYNAMIC_TAG(name, value) 460 #define DYNAMIC_TAG_MARKER(name, value) 461 #define DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value) 462 #include "llvm/BinaryFormat/DynamicTags.def" 464 #undef MIPS_DYNAMIC_TAG 465 #undef HEXAGON_DYNAMIC_TAG 466 #undef PPC64_DYNAMIC_TAG 467 #undef DYNAMIC_TAG_MARKER 468 #undef DYNAMIC_STRINGIFY_ENUM 474 template <
class ELFT>
476 return getDynamicTagAsString(getHeader()->e_machine, Type);
479 template <
class ELFT>
482 size_t DynSecSize = 0;
484 auto ProgramHeadersOrError = program_headers();
485 if (!ProgramHeadersOrError)
486 return ProgramHeadersOrError.takeError();
488 for (
const Elf_Phdr &Phdr : *ProgramHeadersOrError) {
491 reinterpret_cast<const Elf_Dyn *>(base() + Phdr.p_offset),
492 Phdr.p_filesz /
sizeof(
Elf_Dyn));
493 DynSecSize = Phdr.p_filesz;
501 auto SectionsOrError = sections();
502 if (!SectionsOrError)
503 return SectionsOrError.takeError();
505 for (
const Elf_Shdr &Sec : *SectionsOrError) {
508 getSectionContentsAsArray<Elf_Dyn>(&Sec);
512 DynSecSize = Sec.sh_size;
522 return createError(
"invalid empty dynamic section");
524 if (DynSecSize %
sizeof(
Elf_Dyn) != 0)
527 if (Dyn.
back().d_tag != ELF::DT_NULL)
528 return createError(
"dynamic sections must be DT_NULL terminated");
533 template <
class ELFT>
535 auto ProgramHeadersOrError = program_headers();
536 if (!ProgramHeadersOrError)
537 return ProgramHeadersOrError.takeError();
541 for (
const Elf_Phdr &Phdr : *ProgramHeadersOrError)
543 LoadSegments.
push_back(const_cast<Elf_Phdr *>(&Phdr));
546 std::upper_bound(LoadSegments.
begin(), LoadSegments.
end(), VAddr,
548 return VAddr < Phdr->p_vaddr;
551 if (I == LoadSegments.
begin())
552 return createError(
"Virtual address is not in any segment");
555 uint64_t Delta = VAddr - Phdr.p_vaddr;
556 if (Delta >= Phdr.p_filesz)
557 return createError(
"Virtual address is not in any segment");
558 return base() + Phdr.p_offset + Delta;
typename ELFT::Dyn Elf_Dyn
void push_back(const T &Elt)
const T & back() const
back - Get the last element.
Compute iterated dominance frontiers using a linear time algorithm.
StringRef getELFSectionTypeName(uint32_t Machine, uint32_t Type)
uint32_t getELFRelrRelocationType(uint32_t Machine)
const char * getDynamicTagAsString(unsigned Arch, uint64_t Type) const
static Error createError(StringRef Err)
Expected< std::vector< Elf_Rela > > decode_relrs(Elf_Relr_Range relrs) const
Error takeError()
Take ownership of the stored error.
Expected< const uint8_t * > toMappedAddr(uint64_t VAddr) const
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
int64_t decodeSLEB128(const uint8_t *p, unsigned *n=nullptr, const uint8_t *end=nullptr, const char **error=nullptr)
Utility function to decode a SLEB128 value.
support::ulittle32_t Word
typename ELFT::Phdr Elf_Phdr
Tagged union holding either a T or a Error.
typename ELFT::Relr Elf_Relr
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
COFF::MachineTypes Machine
The instances of the Type class are immutable: once they are created, they are never changed...
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
typename ELFT::RelrRange Elf_Relr_Range
typename ELFT::Rela Elf_Rela
StringRef getELFRelocationTypeName(uint32_t Machine, uint32_t Type)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
#define STRINGIFY_ENUM_CASE(ns, name)
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
Expected< std::vector< Elf_Rela > > android_relas(const Elf_Shdr *Sec) const
Expected< Elf_Dyn_Range > dynamicEntries() const
StringRef - Represent a constant reference to a string, i.e.
typename ELFT::Shdr Elf_Shdr
bool empty() const
empty - Check if the array is empty.