15using namespace object;
17#define STRINGIFY_ENUM_CASE(ns, name) \
21#define ELF_RELOC(name, value) STRINGIFY_ENUM_CASE(ELF, name)
28#include "llvm/BinaryFormat/ELFRelocs/M68k.def"
35#include "llvm/BinaryFormat/ELFRelocs/x86_64.def"
43#include "llvm/BinaryFormat/ELFRelocs/i386.def"
50#include "llvm/BinaryFormat/ELFRelocs/Mips.def"
57#include "llvm/BinaryFormat/ELFRelocs/AArch64.def"
64#include "llvm/BinaryFormat/ELFRelocs/ARM.def"
72#include "llvm/BinaryFormat/ELFRelocs/ARC.def"
79#include "llvm/BinaryFormat/ELFRelocs/AVR.def"
86#include "llvm/BinaryFormat/ELFRelocs/Hexagon.def"
93#include "llvm/BinaryFormat/ELFRelocs/Lanai.def"
100#include "llvm/BinaryFormat/ELFRelocs/PowerPC.def"
107#include "llvm/BinaryFormat/ELFRelocs/PowerPC64.def"
114#include "llvm/BinaryFormat/ELFRelocs/RISCV.def"
121#include "llvm/BinaryFormat/ELFRelocs/SystemZ.def"
130#include "llvm/BinaryFormat/ELFRelocs/Sparc.def"
137#include "llvm/BinaryFormat/ELFRelocs/AMDGPU.def"
144#include "llvm/BinaryFormat/ELFRelocs/BPF.def"
151#include "llvm/BinaryFormat/ELFRelocs/MSP430.def"
158#include "llvm/BinaryFormat/ELFRelocs/VE.def"
165#include "llvm/BinaryFormat/ELFRelocs/CSKY.def"
172#include "llvm/BinaryFormat/ELFRelocs/LoongArch.def"
179#include "llvm/BinaryFormat/ELFRelocs/Xtensa.def"
195 return ELF::R_X86_64_RELATIVE;
198 return ELF::R_386_RELATIVE;
202 return ELF::R_AARCH64_RELATIVE;
204 return ELF::R_ARM_RELATIVE;
207 return ELF::R_ARC_RELATIVE;
211 return ELF::R_HEX_RELATIVE;
217 return ELF::R_PPC64_RELATIVE;
219 return ELF::R_RISCV_RELATIVE;
221 return ELF::R_390_RELATIVE;
225 return ELF::R_SPARC_RELATIVE;
227 return ELF::R_CKCORE_RELATIVE;
229 return ELF::R_VE_RELATIVE;
235 return ELF::R_LARCH_RELATIVE;
328std::vector<typename ELFT::Rel>
362 Rel.setType(getRelativeRelocationType(),
false);
363 std::vector<Elf_Rel> Relocs;
366 using Addr =
typename ELFT::uint;
369 for (Elf_Relr R : relrs) {
370 typename ELFT::uint Entry = R;
371 if ((Entry & 1) == 0) {
373 Rel.r_offset = Entry;
374 Relocs.push_back(Rel);
380 if ((Entry & 1) != 0) {
382 Relocs.push_back(Rel);
384 Base += (CHAR_BIT *
sizeof(Entry) - 1) *
sizeof(
Addr);
402 return createError(
"invalid packed relocation header");
406 uint64_t NumRelocs = Data.getSLEB128(Cur);
413 std::vector<Elf_Rela> Relocs;
414 Relocs.reserve(NumRelocs);
416 uint64_t NumRelocsInGroup = Data.getSLEB128(Cur);
419 if (NumRelocsInGroup > NumRelocs)
420 return createError(
"relocation group unexpectedly large");
421 NumRelocs -= NumRelocsInGroup;
423 uint64_t GroupFlags = Data.getSLEB128(Cur);
430 if (GroupedByOffsetDelta)
431 GroupOffsetDelta = Data.getSLEB128(Cur);
435 GroupRInfo = Data.getSLEB128(Cur);
437 if (GroupedByAddend && GroupHasAddend)
438 Addend += Data.getSLEB128(Cur);
443 for (
uint64_t I = 0; Cur &&
I != NumRelocsInGroup; ++
I) {
445 Offset += GroupedByOffsetDelta ? GroupOffsetDelta : Data.getSLEB128(Cur);
447 R.r_info = GroupedByInfo ? GroupRInfo : Data.getSLEB128(Cur);
448 if (GroupHasAddend && !GroupedByAddend)
449 Addend += Data.getSLEB128(Cur);
463#define DYNAMIC_STRINGIFY_ENUM(tag, value) \
467#define DYNAMIC_TAG(n, v)
471#define AARCH64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
472#include "llvm/BinaryFormat/DynamicTags.def"
473#undef AARCH64_DYNAMIC_TAG
479#define HEXAGON_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
480#include "llvm/BinaryFormat/DynamicTags.def"
481#undef HEXAGON_DYNAMIC_TAG
487#define MIPS_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
488#include "llvm/BinaryFormat/DynamicTags.def"
489#undef MIPS_DYNAMIC_TAG
495#define PPC_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
496#include "llvm/BinaryFormat/DynamicTags.def"
497#undef PPC_DYNAMIC_TAG
503#define PPC64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
504#include "llvm/BinaryFormat/DynamicTags.def"
505#undef PPC64_DYNAMIC_TAG
511#define RISCV_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
512#include "llvm/BinaryFormat/DynamicTags.def"
513#undef RISCV_DYNAMIC_TAG
520#define AARCH64_DYNAMIC_TAG(name, value)
521#define MIPS_DYNAMIC_TAG(name, value)
522#define HEXAGON_DYNAMIC_TAG(name, value)
523#define PPC_DYNAMIC_TAG(name, value)
524#define PPC64_DYNAMIC_TAG(name, value)
525#define RISCV_DYNAMIC_TAG(name, value)
527#define DYNAMIC_TAG_MARKER(name, value)
528#define DYNAMIC_TAG(name, value) case value: return #name;
529#include "llvm/BinaryFormat/DynamicTags.def"
531#undef AARCH64_DYNAMIC_TAG
532#undef MIPS_DYNAMIC_TAG
533#undef HEXAGON_DYNAMIC_TAG
534#undef PPC_DYNAMIC_TAG
535#undef PPC64_DYNAMIC_TAG
536#undef RISCV_DYNAMIC_TAG
537#undef DYNAMIC_TAG_MARKER
538#undef DYNAMIC_STRINGIFY_ENUM
540 return "<unknown:>0x" + utohexstr(
Type,
true);
546 return getDynamicTagAsString(getHeader().e_machine,
Type);
553 auto ProgramHeadersOrError = program_headers();
554 if (!ProgramHeadersOrError)
555 return ProgramHeadersOrError.takeError();
557 for (
const Elf_Phdr &Phdr : *ProgramHeadersOrError) {
559 Dyn =
ArrayRef(
reinterpret_cast<const Elf_Dyn *
>(base() + Phdr.p_offset),
560 Phdr.p_filesz /
sizeof(Elf_Dyn));
569 if (!SectionsOrError)
570 return SectionsOrError.takeError();
572 for (
const Elf_Shdr &Sec : *SectionsOrError) {
575 getSectionContentsAsArray<Elf_Dyn>(Sec);
588 return createError(
"invalid empty dynamic section");
590 if (Dyn.
back().d_tag != ELF::DT_NULL)
591 return createError(
"dynamic sections must be DT_NULL terminated");
599 auto ProgramHeadersOrError = program_headers();
600 if (!ProgramHeadersOrError)
601 return ProgramHeadersOrError.takeError();
605 for (
const Elf_Phdr &Phdr : *ProgramHeadersOrError)
607 LoadSegments.
push_back(
const_cast<Elf_Phdr *
>(&Phdr));
611 return A->p_vaddr <
B->p_vaddr;
615 WarnHandler(
"loadable segments are unsorted by virtual address"))
622 return VAddr < Phdr->p_vaddr;
625 if (
I == LoadSegments.
begin())
626 return createError(
"virtual address is not in any segment: 0x" +
629 const Elf_Phdr &Phdr = **
I;
630 uint64_t Delta = VAddr - Phdr.p_vaddr;
631 if (Delta >= Phdr.p_filesz)
632 return createError(
"virtual address is not in any segment: 0x" +
636 if (
Offset >= getBufSize())
637 return createError(
"can't map virtual address 0x" +
639 Twine(&Phdr - (*ProgramHeadersOrError).data() + 1) +
640 ": the segment ends at 0x" +
642 ", which is greater than the file size (0x" +
651 const Elf_Shdr *RelaSec)
const {
652 bool IsRelocatable = getHeader().e_type ==
ELF::ET_REL;
658 if (IsRelocatable && RelaSec) {
660 "Can't read a SHT_LLVM_BB_ADDR_MAP section in a relocatable "
661 "object file without providing a relocation section.");
664 return createError(
"unable to read relocations for section " +
667 for (Elf_Rela Rela : *Relas)
668 FunctionOffsetTranslations[Rela.r_offset] = Rela.r_addend;
675 std::vector<BBAddrMap> FunctionEntries;
684 auto ReadULEB128AsUInt32 = [&Data, &Cur, &ULEBSizeErr]() ->
uint32_t {
690 if (
Value > UINT32_MAX) {
700 while (!ULEBSizeErr && !MetadataDecodeErr && Cur &&
703 Version = Data.getU8(Cur);
707 return createError(
"unsupported SHT_LLVM_BB_ADDR_MAP version: " +
708 Twine(
static_cast<int>(Version)));
712 uintX_t Address =
static_cast<uintX_t
>(Data.getAddress(Cur));
717 auto FOTIterator = FunctionOffsetTranslations.
find(SectionOffset);
718 if (FOTIterator == FunctionOffsetTranslations.
end()) {
719 return createError(
"failed to get relocation data for offset: " +
723 Address = FOTIterator->second;
725 uint32_t NumBlocks = ReadULEB128AsUInt32();
726 std::vector<BBAddrMap::BBEntry> BBEntries;
729 !MetadataDecodeErr && !ULEBSizeErr && Cur && (BlockIndex < NumBlocks);
731 uint32_t ID = Version >= 2 ? ReadULEB128AsUInt32() : BlockIndex;
734 uint32_t MD = ReadULEB128AsUInt32();
737 Offset += PrevBBEndOffset;
742 if (!MetadataOrErr) {
743 MetadataDecodeErr = MetadataOrErr.
takeError();
746 BBEntries.push_back({
ID,
Offset,
Size, *MetadataOrErr});
748 FunctionEntries.emplace_back(Address, std::move(BBEntries));
752 if (!Cur || ULEBSizeErr || MetadataDecodeErr)
754 std::move(MetadataDecodeErr));
755 return FunctionEntries;
767 if (!DoesSectionMatch) {
771 if (*DoesSectionMatch) {
772 if (SecToRelocMap.
insert(std::make_pair(&Sec, (
const Elf_Shdr *)
nullptr))
784 ": failed to get a relocated section: " +
788 const Elf_Shdr *ContentsSec = *RelSecOrErr;
790 if (!DoesRelTargetMatch) {
794 if (*DoesRelTargetMatch)
795 SecToRelocMap[ContentsSec] = &Sec;
798 return std::move(Errors);
799 return SecToRelocMap;
bbsections Prepares for basic block sections
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define STRINGIFY_ENUM_CASE(ns, name)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
const T & back() const
back - Get the last element.
bool empty() const
empty - Check if the array is empty.
iterator find(const_arg_type_t< KeyT > Val)
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
Tagged union holding either a T or a Error.
Error takeError()
Take ownership of the stored error.
This class implements a map that also provides access to all stored values in a deterministic order.
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
static Twine utohexstr(const uint64_t &Val)
The instances of the Type class are immutable: once they are created, they are never changed.
LLVM Value Representation.
An efficient, type-erasing, non-owning reference to a callable.
Expected< std::vector< Elf_Rela > > android_relas(const Elf_Shdr &Sec) const
std::string getDynamicTagAsString(unsigned Arch, uint64_t Type) const
Expected< std::vector< BBAddrMap > > decodeBBAddrMap(const Elf_Shdr &Sec, const Elf_Shdr *RelaSec=nullptr) const
Returns a vector of BBAddrMap structs corresponding to each function within the text section that the...
Expected< Elf_Dyn_Range > dynamicEntries() const
Expected< const uint8_t * > toMappedAddr(uint64_t VAddr, WarningHandler WarnHandler=&defaultWarningHandler) const
Expected< MapVector< const Elf_Shdr *, const Elf_Shdr * > > getSectionAndRelocations(std::function< Expected< bool >(const Elf_Shdr &)> IsMatch) const
Returns a map from every section matching IsMatch to its relocation section, or nullptr if it has no ...
std::vector< Elf_Rel > decode_relrs(Elf_Relr_Range relrs) const
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
@ RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG
@ RELOCATION_GROUPED_BY_INFO_FLAG
@ RELOCATION_GROUPED_BY_ADDEND_FLAG
@ RELOCATION_GROUP_HAS_ADDEND_FLAG
std::optional< const char * > toString(const std::optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract a string value from it.
Expected< const typename ELFT::Shdr * > getSection(typename ELFT::ShdrRange Sections, uint32_t Index)
Error createError(const Twine &Err)
StringRef getELFRelocationTypeName(uint32_t Machine, uint32_t Type)
uint32_t getELFRelativeRelocationType(uint32_t Machine)
static std::string describe(const ELFFile< ELFT > &Obj, const typename ELFT::Shdr &Sec)
StringRef getELFSectionTypeName(uint32_t Machine, uint32_t Type)
This is an optimization pass for GlobalISel generic memory operations.
void stable_sort(R &&Range)
auto upper_bound(R &&Range, T &&Value)
Provide wrappers to std::upper_bound which take ranges instead of having to pass begin/end explicitly...
Error joinErrors(Error E1, Error E2)
Concatenate errors.
bool is_sorted(R &&Range, Compare C)
Wrapper function around std::is_sorted to check if elements in a range R are sorted with respect to a...
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
static Expected< Metadata > decode(uint32_t V)