16 #define STRINGIFY_ENUM_CASE(ns, name) \ 20 #define ELF_RELOC(name, value) STRINGIFY_ENUM_CASE(ELF, name) 27 #include "llvm/BinaryFormat/ELFRelocs/x86_64.def" 35 #include "llvm/BinaryFormat/ELFRelocs/i386.def" 42 #include "llvm/BinaryFormat/ELFRelocs/Mips.def" 49 #include "llvm/BinaryFormat/ELFRelocs/AArch64.def" 56 #include "llvm/BinaryFormat/ELFRelocs/ARM.def" 64 #include "llvm/BinaryFormat/ELFRelocs/ARC.def" 71 #include "llvm/BinaryFormat/ELFRelocs/AVR.def" 78 #include "llvm/BinaryFormat/ELFRelocs/Hexagon.def" 85 #include "llvm/BinaryFormat/ELFRelocs/Lanai.def" 92 #include "llvm/BinaryFormat/ELFRelocs/PowerPC.def" 99 #include "llvm/BinaryFormat/ELFRelocs/PowerPC64.def" 106 #include "llvm/BinaryFormat/ELFRelocs/RISCV.def" 113 #include "llvm/BinaryFormat/ELFRelocs/SystemZ.def" 122 #include "llvm/BinaryFormat/ELFRelocs/Sparc.def" 129 #include "llvm/BinaryFormat/ELFRelocs/AMDGPU.def" 136 #include "llvm/BinaryFormat/ELFRelocs/BPF.def" 143 #include "llvm/BinaryFormat/ELFRelocs/MSP430.def" 159 return ELF::R_X86_64_RELATIVE;
162 return ELF::R_386_RELATIVE;
166 return ELF::R_AARCH64_RELATIVE;
168 return ELF::R_ARM_RELATIVE;
171 return ELF::R_ARC_RELATIVE;
175 return ELF::R_HEX_RELATIVE;
181 return ELF::R_PPC64_RELATIVE;
183 return ELF::R_RISCV_RELATIVE;
185 return ELF::R_390_RELATIVE;
189 return ELF::R_SPARC_RELATIVE;
270 template <
class ELFT>
306 Rela.setType(getRelativeRelocationType(),
false);
307 std::vector<Elf_Rela> Relocs;
310 typedef typename ELFT::uint
Word;
313 const size_t WordSize =
sizeof(
Word);
317 const size_t NBits = 8*WordSize - 1;
322 if ((Entry&1) == 0) {
324 Rela.r_offset = Entry;
325 Relocs.push_back(Rela);
327 Base = Entry + WordSize;
335 if ((Entry&1) != 0) {
337 Relocs.push_back(Rela);
343 Base += NBits * WordSize;
349 template <
class ELFT>
357 const uint8_t *Cur = ContentsOrErr->begin();
358 const uint8_t *End = ContentsOrErr->end();
359 if (ContentsOrErr->size() < 4 || Cur[0] !=
'A' || Cur[1] !=
'P' ||
360 Cur[2] !=
'S' || Cur[3] !=
'2')
361 return createError(
"invalid packed relocation header");
364 const char *ErrStr =
nullptr;
365 auto ReadSLEB = [&]() -> int64_t {
374 uint64_t NumRelocs = ReadSLEB();
375 uint64_t
Offset = ReadSLEB();
381 std::vector<Elf_Rela> Relocs;
382 Relocs.reserve(NumRelocs);
384 uint64_t NumRelocsInGroup = ReadSLEB();
385 if (NumRelocsInGroup > NumRelocs)
386 return createError(
"relocation group unexpectedly large");
387 NumRelocs -= NumRelocsInGroup;
389 uint64_t GroupFlags = ReadSLEB();
395 uint64_t GroupOffsetDelta;
396 if (GroupedByOffsetDelta)
397 GroupOffsetDelta = ReadSLEB();
401 GroupRInfo = ReadSLEB();
403 if (GroupedByAddend && GroupHasAddend)
404 Addend += ReadSLEB();
409 for (uint64_t
I = 0;
I != NumRelocsInGroup; ++
I) {
411 Offset += GroupedByOffsetDelta ? GroupOffsetDelta : ReadSLEB();
413 R.r_info = GroupedByInfo ? GroupRInfo : ReadSLEB();
414 if (GroupHasAddend && !GroupedByAddend)
415 Addend += ReadSLEB();
430 template <
class ELFT>
432 uint64_t Type)
const {
433 #define DYNAMIC_STRINGIFY_ENUM(tag, value) \ 437 #define DYNAMIC_TAG(n, v) 441 #define AARCH64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value) 442 #include "llvm/BinaryFormat/DynamicTags.def" 443 #undef AARCH64_DYNAMIC_TAG 449 #define HEXAGON_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value) 450 #include "llvm/BinaryFormat/DynamicTags.def" 451 #undef HEXAGON_DYNAMIC_TAG 457 #define MIPS_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value) 458 #include "llvm/BinaryFormat/DynamicTags.def" 459 #undef MIPS_DYNAMIC_TAG 465 #define PPC64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value) 466 #include "llvm/BinaryFormat/DynamicTags.def" 467 #undef PPC64_DYNAMIC_TAG 474 #define AARCH64_DYNAMIC_TAG(name, value) 475 #define MIPS_DYNAMIC_TAG(name, value) 476 #define HEXAGON_DYNAMIC_TAG(name, value) 477 #define PPC64_DYNAMIC_TAG(name, value) 479 #define DYNAMIC_TAG_MARKER(name, value) 480 #define DYNAMIC_TAG(name, value) case value: return #name; 481 #include "llvm/BinaryFormat/DynamicTags.def" 483 #undef AARCH64_DYNAMIC_TAG 484 #undef MIPS_DYNAMIC_TAG 485 #undef HEXAGON_DYNAMIC_TAG 486 #undef PPC64_DYNAMIC_TAG 487 #undef DYNAMIC_TAG_MARKER 488 #undef DYNAMIC_STRINGIFY_ENUM 490 return "<unknown:>0x" +
utohexstr(Type,
true);
494 template <
class ELFT>
496 return getDynamicTagAsString(getHeader()->e_machine, Type);
499 template <
class ELFT>
502 size_t DynSecSize = 0;
504 auto ProgramHeadersOrError = program_headers();
505 if (!ProgramHeadersOrError)
506 return ProgramHeadersOrError.takeError();
508 for (
const Elf_Phdr &Phdr : *ProgramHeadersOrError) {
511 reinterpret_cast<const Elf_Dyn *>(base() + Phdr.p_offset),
512 Phdr.p_filesz /
sizeof(
Elf_Dyn));
513 DynSecSize = Phdr.p_filesz;
521 auto SectionsOrError = sections();
522 if (!SectionsOrError)
523 return SectionsOrError.takeError();
525 for (
const Elf_Shdr &Sec : *SectionsOrError) {
528 getSectionContentsAsArray<Elf_Dyn>(&Sec);
532 DynSecSize = Sec.sh_size;
543 return createError(
"invalid empty dynamic section");
545 if (DynSecSize %
sizeof(
Elf_Dyn) != 0)
549 if (Dyn.
back().d_tag != ELF::DT_NULL)
551 return createError(
"dynamic sections must be DT_NULL terminated");
556 template <
class ELFT>
558 auto ProgramHeadersOrError = program_headers();
559 if (!ProgramHeadersOrError)
560 return ProgramHeadersOrError.takeError();
564 for (
const Elf_Phdr &Phdr : *ProgramHeadersOrError)
566 LoadSegments.
push_back(const_cast<Elf_Phdr *>(&Phdr));
571 return VAddr < Phdr->p_vaddr;
574 if (I == LoadSegments.
begin())
575 return createError(
"virtual address is not in any segment: 0x" +
579 uint64_t Delta = VAddr - Phdr.p_vaddr;
580 if (Delta >= Phdr.p_filesz)
581 return createError(
"virtual address is not in any segment: 0x" +
583 return base() + Phdr.p_offset + Delta;
typename ELFT::Dyn Elf_Dyn
const T & back() const
back - Get the last element.
This class represents lattice values for constants.
void push_back(const T &Elt)
StringRef getELFSectionTypeName(uint32_t Machine, uint32_t Type)
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.
uint32_t getELFRelativeRelocationType(uint32_t Machine)
static Error createError(const Twine &Err)
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...
std::string getDynamicTagAsString(unsigned Arch, uint64_t Type) const
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)
static Twine utohexstr(const uint64_t &Val)
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.
auto upper_bound(R &&Range, T &&Value) -> decltype(adl_begin(Range))
Provide wrappers to std::upper_bound which take ranges instead of having to pass begin/end explicitly...
std::string utohexstr(uint64_t X, bool LowerCase=false)