19#define STRINGIFY_ENUM_CASE(ns, name) \
23#define ELF_RELOC(name, value) STRINGIFY_ENUM_CASE(ELF, name)
30#include "llvm/BinaryFormat/ELFRelocs/M68k.def"
37#include "llvm/BinaryFormat/ELFRelocs/x86_64.def"
45#include "llvm/BinaryFormat/ELFRelocs/i386.def"
52#include "llvm/BinaryFormat/ELFRelocs/Mips.def"
59#include "llvm/BinaryFormat/ELFRelocs/AArch64.def"
66#include "llvm/BinaryFormat/ELFRelocs/ARM.def"
71 case ELF::EM_ARC_COMPACT:
72 case ELF::EM_ARC_COMPACT2:
74#include "llvm/BinaryFormat/ELFRelocs/ARC.def"
81#include "llvm/BinaryFormat/ELFRelocs/AVR.def"
88#include "llvm/BinaryFormat/ELFRelocs/Hexagon.def"
95#include "llvm/BinaryFormat/ELFRelocs/Lanai.def"
102#include "llvm/BinaryFormat/ELFRelocs/PowerPC.def"
109#include "llvm/BinaryFormat/ELFRelocs/PowerPC64.def"
116#include "llvm/BinaryFormat/ELFRelocs/RISCV.def"
123#include "llvm/BinaryFormat/ELFRelocs/SystemZ.def"
129 case ELF::EM_SPARC32PLUS:
130 case ELF::EM_SPARCV9:
132#include "llvm/BinaryFormat/ELFRelocs/Sparc.def"
139#include "llvm/BinaryFormat/ELFRelocs/AMDGPU.def"
146#include "llvm/BinaryFormat/ELFRelocs/BPF.def"
153#include "llvm/BinaryFormat/ELFRelocs/MSP430.def"
160#include "llvm/BinaryFormat/ELFRelocs/VE.def"
167#include "llvm/BinaryFormat/ELFRelocs/CSKY.def"
172 case ELF::EM_LOONGARCH:
174#include "llvm/BinaryFormat/ELFRelocs/LoongArch.def"
181#include "llvm/BinaryFormat/ELFRelocs/Xtensa.def"
186 case ELF::EM_NANOMIPS:
188#include "llvm/BinaryFormat/ELFRelocs/NanoMips.def"
203#define ELF_RISCV_NONSTANDARD_RELOC(vendor, name, number) \
204 if (Vendor == #vendor && Type == number) \
207#include "llvm/BinaryFormat/ELFRelocs/RISCV_nonstandard.def"
209#undef ELF_RISCV_NONSTANDARD_RELOC
217 return ELF::R_X86_64_RELATIVE;
220 return ELF::R_386_RELATIVE;
223 case ELF::EM_AARCH64:
224 return ELF::R_AARCH64_RELATIVE;
226 return ELF::R_ARM_RELATIVE;
227 case ELF::EM_ARC_COMPACT:
228 case ELF::EM_ARC_COMPACT2:
229 return ELF::R_ARC_RELATIVE;
232 case ELF::EM_HEXAGON:
233 return ELF::R_HEX_RELATIVE;
239 return ELF::R_PPC64_RELATIVE;
241 return ELF::R_RISCV_RELATIVE;
243 return ELF::R_390_RELATIVE;
245 case ELF::EM_SPARC32PLUS:
246 case ELF::EM_SPARCV9:
247 return ELF::R_SPARC_RELATIVE;
249 return ELF::R_CKCORE_RELATIVE;
251 return ELF::R_VE_RELATIVE;
256 case ELF::EM_LOONGARCH:
257 return ELF::R_LARCH_RELATIVE;
258 case ELF::EM_NANOMIPS:
259 return ELF::R_NANOMIPS_RELATIVE;
277 case ELF::EM_HEXAGON:
287 case ELF::EM_MIPS_RS3_LE:
301 case ELF::EM_AARCH64:
361std::vector<typename ELFT::Rel>
396 std::vector<Elf_Rel> Relocs;
399 using Addr =
typename ELFT::uint;
402 for (Elf_Relr R :
relrs) {
403 typename ELFT::uint Entry = R;
404 if ((Entry & 1) == 0) {
406 Rel.r_offset = Entry;
407 Relocs.push_back(Rel);
413 if ((Entry & 1) != 0) {
415 Relocs.push_back(Rel);
417 Base += (CHAR_BIT *
sizeof(Entry) - 1) *
sizeof(Addr);
430 Hdr =
Data.getULEB128(&Hdr, &Err);
439 std::vector<Elf_Rel> Rels;
440 std::vector<Elf_Rela> Relas;
445 [&](uint64_t
Count,
bool HasA) {
454 Relas[
I].r_offset = Crel.r_offset;
455 Relas[
I].setSymbolAndType(Crel.r_symidx, Crel.r_type,
false);
456 Relas[
I++].r_addend = Crel.r_addend;
458 Rels[
I].r_offset = Crel.r_offset;
459 Rels[
I++].setSymbolAndType(Crel.r_symidx, Crel.r_type,
false);
463 return std::move(Err);
464 return std::make_pair(std::move(Rels), std::move(Relas));
485 if (Content.
size() < 4 || Content[0] !=
'A' || Content[1] !=
'P' ||
486 Content[2] !=
'S' || Content[3] !=
'2')
487 return createError(
"invalid packed relocation header");
491 uint64_t NumRelocs =
Data.getSLEB128(Cur);
498 std::vector<Elf_Rela> Relocs;
499 Relocs.reserve(NumRelocs);
501 uint64_t NumRelocsInGroup =
Data.getSLEB128(Cur);
504 if (NumRelocsInGroup > NumRelocs)
505 return createError(
"relocation group unexpectedly large");
506 NumRelocs -= NumRelocsInGroup;
508 uint64_t GroupFlags =
Data.getSLEB128(Cur);
514 uint64_t GroupOffsetDelta;
515 if (GroupedByOffsetDelta)
516 GroupOffsetDelta =
Data.getSLEB128(Cur);
520 GroupRInfo =
Data.getSLEB128(Cur);
522 if (GroupedByAddend && GroupHasAddend)
523 Addend +=
Data.getSLEB128(Cur);
528 for (uint64_t
I = 0; Cur &&
I != NumRelocsInGroup; ++
I) {
530 Offset += GroupedByOffsetDelta ? GroupOffsetDelta :
Data.getSLEB128(Cur);
532 R.r_info = GroupedByInfo ? GroupRInfo :
Data.getSLEB128(Cur);
533 if (GroupHasAddend && !GroupedByAddend)
534 Addend +=
Data.getSLEB128(Cur);
547 uint64_t
Type)
const {
548#define DYNAMIC_STRINGIFY_ENUM(tag, value) \
552#define DYNAMIC_TAG(n, v)
554 case ELF::EM_AARCH64:
556#define AARCH64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
557#include "llvm/BinaryFormat/DynamicTags.def"
558#undef AARCH64_DYNAMIC_TAG
562 case ELF::EM_HEXAGON:
564#define HEXAGON_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
565#include "llvm/BinaryFormat/DynamicTags.def"
566#undef HEXAGON_DYNAMIC_TAG
572#define MIPS_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
573#include "llvm/BinaryFormat/DynamicTags.def"
574#undef MIPS_DYNAMIC_TAG
580#define PPC_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
581#include "llvm/BinaryFormat/DynamicTags.def"
582#undef PPC_DYNAMIC_TAG
588#define PPC64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
589#include "llvm/BinaryFormat/DynamicTags.def"
590#undef PPC64_DYNAMIC_TAG
596#define RISCV_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
597#include "llvm/BinaryFormat/DynamicTags.def"
598#undef RISCV_DYNAMIC_TAG
603 case ELF::EM_SPARC32PLUS:
604 case ELF::EM_SPARCV9:
606#define SPARC_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
607#include "llvm/BinaryFormat/DynamicTags.def"
608#undef SPARC_DYNAMIC_TAG
614#define X86_64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
615#include "llvm/BinaryFormat/DynamicTags.def"
616#undef X86_64_DYNAMIC_TAG
623#define AARCH64_DYNAMIC_TAG(name, value)
624#define MIPS_DYNAMIC_TAG(name, value)
625#define HEXAGON_DYNAMIC_TAG(name, value)
626#define PPC_DYNAMIC_TAG(name, value)
627#define PPC64_DYNAMIC_TAG(name, value)
628#define RISCV_DYNAMIC_TAG(name, value)
629#define SPARC_DYNAMIC_TAG(name, value)
630#define X86_64_DYNAMIC_TAG(name, value)
632#define DYNAMIC_TAG_MARKER(name, value)
633#define DYNAMIC_TAG(name, value) case value: return #name;
634#include "llvm/BinaryFormat/DynamicTags.def"
636#undef AARCH64_DYNAMIC_TAG
637#undef MIPS_DYNAMIC_TAG
638#undef HEXAGON_DYNAMIC_TAG
639#undef PPC_DYNAMIC_TAG
640#undef PPC64_DYNAMIC_TAG
641#undef RISCV_DYNAMIC_TAG
642#undef SPARC_DYNAMIC_TAG
643#undef X86_64_DYNAMIC_TAG
644#undef DYNAMIC_TAG_MARKER
645#undef DYNAMIC_STRINGIFY_ENUM
661 if (!ProgramHeadersOrError)
662 return ProgramHeadersOrError.takeError();
664 for (
const Elf_Phdr &Phdr : *ProgramHeadersOrError) {
667 if (DynOffset >
end())
669 "dynamic section offset past file size: corrupted ELF");
670 Dyn =
ArrayRef(
reinterpret_cast<const Elf_Dyn *
>(DynOffset),
671 Phdr.p_filesz /
sizeof(Elf_Dyn));
680 if (!SectionsOrError)
681 return SectionsOrError.takeError();
683 for (
const Elf_Shdr &Sec : *SectionsOrError) {
699 return createError(
"invalid empty dynamic section");
701 if (Dyn.
back().d_tag != ELF::DT_NULL)
702 return createError(
"dynamic sections must be DT_NULL terminated");
711 if (!ProgramHeadersOrError)
712 return ProgramHeadersOrError.takeError();
716 for (
const Elf_Phdr &Phdr : *ProgramHeadersOrError)
718 LoadSegments.
push_back(
const_cast<Elf_Phdr *
>(&Phdr));
722 return A->p_vaddr <
B->p_vaddr;
726 WarnHandler(
"loadable segments are unsorted by virtual address"))
733 return VAddr < Phdr->p_vaddr;
736 if (
I == LoadSegments.
begin())
737 return createError(
"virtual address is not in any segment: 0x" +
740 const Elf_Phdr &Phdr = **
I;
741 uint64_t Delta = VAddr - Phdr.p_vaddr;
742 if (Delta >= Phdr.p_filesz)
743 return createError(
"virtual address is not in any segment: 0x" +
746 uint64_t
Offset = Phdr.p_offset + Delta;
748 return createError(
"can't map virtual address 0x" +
750 Twine(&Phdr - (*ProgramHeadersOrError).data() + 1) +
751 ": the segment ends at 0x" +
753 ", which is greater than the file size (0x" +
766 ELFBBAddrMapAddressExtractor(
767 const DataExtractor &Data,
unsigned AddressSize,
bool IsRelocatable,
770 FunctionOffsetTranslations(std::move(FunctionOffsetTranslations)) {}
773 template <
typename ELFT>
782 if (IsRelocatable && RelaSec) {
784 "Can't read a SHT_LLVM_BB_ADDR_MAP section in a relocatable "
785 "object file without providing a relocation section.");
790 return createError(
"unable to read CREL relocations for section " +
794 FunctionOffsetTranslations[Rela.r_offset] = Rela.r_addend;
799 return createError(
"unable to read relocations for section " +
803 FunctionOffsetTranslations[Rela.r_offset] = Rela.r_addend;
808 return ELFBBAddrMapAddressExtractor(Data, AddressSize, IsRelocatable,
809 std::move(FunctionOffsetTranslations));
818 return *AddressOrErr;
819 auto FOTIterator = FunctionOffsetTranslations.find(
Offset);
820 if (FOTIterator == FunctionOffsetTranslations.end())
821 return createError(
"failed to get relocation data for offset: " +
823 return FOTIterator->second;
827template <
typename ELFT>
832 std::vector<PGOAnalysisMap> *PGOAnalyses) {
839 std::unique_ptr<uint8_t[]> DecompressedContent;
842 if (!SectionNameOrErr)
844 auto DecompressorOrErr =
846 EF.
isLE(), ELFT::Is64Bits);
847 if (!DecompressorOrErr)
848 return DecompressorOrErr.takeError();
849 size_t DecompressedSize = DecompressorOrErr->getDecompressedSize();
850 DecompressedContent = std::make_unique<uint8_t[]>(DecompressedSize);
853 if (
Error Err = DecompressorOrErr->decompress(DecompressedContentRef))
854 return std::move(Err);
855 Content = DecompressedContentRef;
859 auto ExtractorOrErr =
862 return ExtractorOrErr.takeError();
864 if (!BBAddrMapsOrErr)
867 return BBAddrMapsOrErr;
873 std::vector<PGOAnalysisMap> *PGOAnalyses)
const {
874 size_t OriginalPGOSize = PGOAnalyses ? PGOAnalyses->size() : 0;
877 if (!AddrMapsOrErr && PGOAnalyses)
878 PGOAnalyses->resize(OriginalPGOSize);
879 return std::move(AddrMapsOrErr);
891 if (!DoesSectionMatch) {
895 if (*DoesSectionMatch) {
908 ": failed to get a relocated section: " +
912 const Elf_Shdr *ContentsSec = *RelSecOrErr;
914 if (!DoesRelTargetMatch) {
918 if (*DoesRelTargetMatch)
919 SecToRelocMap[ContentsSec] = &Sec;
922 return std::move(Errors);
923 return SecToRelocMap;
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file declares common types and utilities for basic-block address maps.
bbsections Prepares for basic block sections
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define LLVM_EXPORT_TEMPLATE
#define STRINGIFY_ENUM_CASE(ns, name)
static Expected< std::vector< BBAddrMap > > decodeBBAddrMapImpl(const ELFFile< ELFT > &EF, const typename ELFFile< ELFT >::Elf_Shdr &Sec, const typename ELFFile< ELFT >::Elf_Shdr *RelaSec, std::vector< PGOAnalysisMap > *PGOAnalyses)
Function const char TargetMachine * Machine
Represent a constant reference to an array (0 or more elements consecutively in memory),...
const T & back() const
Get the last element.
size_t size() const
Get the array size.
bool empty() const
Check if the array is empty.
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 > try_emplace(const KeyT &Key, Ts &&...Args)
Represent a mutable reference to an array (0 or more elements consecutively in memory),...
void push_back(const T &Elt)
LLVM_NO_SANITIZE_CFI_UNRELATED_CAST iterator begin()
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
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(uint64_t Val)
The instances of the Type class are immutable: once they are created, they are never changed.
static LLVM_ABI Expected< Decompressor > create(StringRef Name, StringRef Data, bool IsLE, bool Is64Bit)
Create decompressor object.
const Elf_Ehdr & getHeader() const
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, std::vector< PGOAnalysisMap > *PGOAnalyses=nullptr) const
Returns a vector of BBAddrMap structs corresponding to each function within the text section that the...
uint32_t getRelativeRelocationType() const
Expected< ArrayRef< uint8_t > > getSectionContents(const Elf_Shdr &Sec) const
Expected< Elf_Rela_Range > relas(const Elf_Shdr &Sec) const
Expected< Elf_Phdr_Range > program_headers() const
Iterate over program header table.
size_t getBufSize() const
Expected< RelsOrRelas > decodeCrel(ArrayRef< uint8_t > Content) const
const uint8_t * end() const
Expected< uint64_t > getCrelHeader(ArrayRef< uint8_t > Content) const
Expected< Elf_Dyn_Range > dynamicEntries() const
const uint8_t * base() const
Expected< const uint8_t * > toMappedAddr(uint64_t VAddr, WarningHandler WarnHandler=&defaultWarningHandler) const
Expected< Elf_Relr_Range > relrs(const Elf_Shdr &Sec) 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 ...
Expected< StringRef > getSectionName(const Elf_Shdr &Section, WarningHandler WarnHandler=&defaultWarningHandler) const
llvm::function_ref< Error(const Twine &Msg)> WarningHandler
Expected< ArrayRef< T > > getSectionContentsAsArray(const Elf_Shdr &Sec) const
Expected< RelsOrRelas > crels(const Elf_Shdr &Sec) const
std::vector< Elf_Rel > decode_relrs(Elf_Relr_Range relrs) const
@ RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG
@ RELOCATION_GROUPED_BY_INFO_FLAG
@ RELOCATION_GROUPED_BY_ADDEND_FLAG
@ RELOCATION_GROUP_HAS_ADDEND_FLAG
Error decodeCrel(ArrayRef< uint8_t > Content, function_ref< void(uint64_t, bool)> HdrHandler, function_ref< void(Elf_Crel_Impl< Is64 >)> EntryHandler)
Expected< const typename ELFT::Shdr * > getSection(typename ELFT::ShdrRange Sections, uint32_t Index)
Error createError(const Twine &Err)
LLVM_ABI StringRef getELFRelocationTypeName(uint32_t Machine, uint32_t Type)
LLVM_ABI uint32_t getELFRelativeRelocationType(uint32_t Machine)
LLVM_ABI StringRef getELFSectionTypeName(uint32_t Machine, uint32_t Type)
LLVM_ABI StringRef getRISCVVendorRelocationTypeName(uint32_t Type, StringRef Vendor)
LLVM_ABI Expected< std::vector< BBAddrMap > > decodeBBAddrMapPayload(AddressExtractor &Extractor, std::vector< PGOAnalysisMap > *PGOAnalyses=nullptr)
Decodes one BB address map section payload.
std::string describe(const ELFFile< ELFT > &Obj, const typename ELFT::Shdr &Sec)
This is an optimization pass for GlobalISel generic memory operations.
void stable_sort(R &&Range)
std::string utohexstr(uint64_t X, bool LowerCase=false, unsigned Width=0)
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.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
ArrayRef(const T &OneElt) -> ArrayRef< T >
std::string toString(const APInt &I, unsigned Radix, bool Signed, bool formatAsCLiteral=false, bool UpperCase=true, bool InsertSeparators=false)
StringRef toStringRef(bool B)
Construct a string ref from a boolean.