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"
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"
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"
174#include "llvm/BinaryFormat/ELFRelocs/LoongArch.def"
181#include "llvm/BinaryFormat/ELFRelocs/Xtensa.def"
196#define ELF_RISCV_NONSTANDARD_RELOC(vendor, name, number) \
197 if (Vendor == #vendor && Type == number) \
200#include "llvm/BinaryFormat/ELFRelocs/RISCV_nonstandard.def"
202#undef ELF_RISCV_NONSTANDARD_RELOC
210 return ELF::R_X86_64_RELATIVE;
213 return ELF::R_386_RELATIVE;
217 return ELF::R_AARCH64_RELATIVE;
219 return ELF::R_ARM_RELATIVE;
222 return ELF::R_ARC_RELATIVE;
226 return ELF::R_HEX_RELATIVE;
232 return ELF::R_PPC64_RELATIVE;
234 return ELF::R_RISCV_RELATIVE;
236 return ELF::R_390_RELATIVE;
240 return ELF::R_SPARC_RELATIVE;
242 return ELF::R_CKCORE_RELATIVE;
244 return ELF::R_VE_RELATIVE;
250 return ELF::R_LARCH_RELATIVE;
351std::vector<typename ELFT::Rel>
386 std::vector<Elf_Rel> Relocs;
389 using Addr =
typename ELFT::uint;
392 for (Elf_Relr R :
relrs) {
393 typename ELFT::uint Entry = R;
394 if ((Entry & 1) == 0) {
396 Rel.r_offset = Entry;
397 Relocs.push_back(Rel);
399 Base = Entry +
sizeof(Addr);
403 if ((Entry & 1) != 0) {
405 Relocs.push_back(Rel);
407 Base += (CHAR_BIT *
sizeof(Entry) - 1) *
sizeof(Addr);
420 Hdr =
Data.getULEB128(&Hdr, &Err);
429 std::vector<Elf_Rel> Rels;
430 std::vector<Elf_Rela> Relas;
444 Relas[
I].r_offset = Crel.r_offset;
445 Relas[
I].setSymbolAndType(Crel.r_symidx, Crel.r_type,
false);
446 Relas[
I++].r_addend = Crel.r_addend;
448 Rels[
I].r_offset = Crel.r_offset;
449 Rels[
I++].setSymbolAndType(Crel.r_symidx, Crel.r_type,
false);
453 return std::move(Err);
454 return std::make_pair(std::move(Rels), std::move(Relas));
475 if (Content.
size() < 4 || Content[0] !=
'A' || Content[1] !=
'P' ||
476 Content[2] !=
'S' || Content[3] !=
'2')
477 return createError(
"invalid packed relocation header");
488 std::vector<Elf_Rela> Relocs;
489 Relocs.reserve(NumRelocs);
494 if (NumRelocsInGroup > NumRelocs)
495 return createError(
"relocation group unexpectedly large");
496 NumRelocs -= NumRelocsInGroup;
505 if (GroupedByOffsetDelta)
506 GroupOffsetDelta =
Data.getSLEB128(Cur);
510 GroupRInfo =
Data.getSLEB128(Cur);
512 if (GroupedByAddend && GroupHasAddend)
513 Addend +=
Data.getSLEB128(Cur);
518 for (
uint64_t I = 0; Cur &&
I != NumRelocsInGroup; ++
I) {
520 Offset += GroupedByOffsetDelta ? GroupOffsetDelta :
Data.getSLEB128(Cur);
522 R.r_info = GroupedByInfo ? GroupRInfo :
Data.getSLEB128(Cur);
523 if (GroupHasAddend && !GroupedByAddend)
524 Addend +=
Data.getSLEB128(Cur);
538#define DYNAMIC_STRINGIFY_ENUM(tag, value) \
542#define DYNAMIC_TAG(n, v)
546#define AARCH64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
547#include "llvm/BinaryFormat/DynamicTags.def"
548#undef AARCH64_DYNAMIC_TAG
554#define HEXAGON_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
555#include "llvm/BinaryFormat/DynamicTags.def"
556#undef HEXAGON_DYNAMIC_TAG
562#define MIPS_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
563#include "llvm/BinaryFormat/DynamicTags.def"
564#undef MIPS_DYNAMIC_TAG
570#define PPC_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
571#include "llvm/BinaryFormat/DynamicTags.def"
572#undef PPC_DYNAMIC_TAG
578#define PPC64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
579#include "llvm/BinaryFormat/DynamicTags.def"
580#undef PPC64_DYNAMIC_TAG
586#define RISCV_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
587#include "llvm/BinaryFormat/DynamicTags.def"
588#undef RISCV_DYNAMIC_TAG
595#define AARCH64_DYNAMIC_TAG(name, value)
596#define MIPS_DYNAMIC_TAG(name, value)
597#define HEXAGON_DYNAMIC_TAG(name, value)
598#define PPC_DYNAMIC_TAG(name, value)
599#define PPC64_DYNAMIC_TAG(name, value)
600#define RISCV_DYNAMIC_TAG(name, value)
602#define DYNAMIC_TAG_MARKER(name, value)
603#define DYNAMIC_TAG(name, value) case value: return #name;
604#include "llvm/BinaryFormat/DynamicTags.def"
606#undef AARCH64_DYNAMIC_TAG
607#undef MIPS_DYNAMIC_TAG
608#undef HEXAGON_DYNAMIC_TAG
609#undef PPC_DYNAMIC_TAG
610#undef PPC64_DYNAMIC_TAG
611#undef RISCV_DYNAMIC_TAG
612#undef DYNAMIC_TAG_MARKER
613#undef DYNAMIC_STRINGIFY_ENUM
629 if (!ProgramHeadersOrError)
630 return ProgramHeadersOrError.takeError();
632 for (
const Elf_Phdr &Phdr : *ProgramHeadersOrError) {
635 if (DynOffset >
end())
637 "dynamic section offset past file size: corrupted ELF");
638 Dyn =
ArrayRef(
reinterpret_cast<const Elf_Dyn *
>(DynOffset),
639 Phdr.p_filesz /
sizeof(Elf_Dyn));
648 if (!SectionsOrError)
649 return SectionsOrError.takeError();
651 for (
const Elf_Shdr &Sec : *SectionsOrError) {
667 return createError(
"invalid empty dynamic section");
669 if (Dyn.
back().d_tag != ELF::DT_NULL)
670 return createError(
"dynamic sections must be DT_NULL terminated");
679 if (!ProgramHeadersOrError)
680 return ProgramHeadersOrError.takeError();
684 for (
const Elf_Phdr &Phdr : *ProgramHeadersOrError)
686 LoadSegments.
push_back(
const_cast<Elf_Phdr *
>(&Phdr));
690 return A->p_vaddr <
B->p_vaddr;
694 WarnHandler(
"loadable segments are unsorted by virtual address"))
701 return VAddr < Phdr->p_vaddr;
704 if (
I == LoadSegments.
begin())
705 return createError(
"virtual address is not in any segment: 0x" +
708 const Elf_Phdr &Phdr = **
I;
709 uint64_t Delta = VAddr - Phdr.p_vaddr;
710 if (Delta >= Phdr.p_filesz)
711 return createError(
"virtual address is not in any segment: 0x" +
716 return createError(
"can't map virtual address 0x" +
718 Twine(&Phdr - (*ProgramHeadersOrError).data() + 1) +
719 ": the segment ends at 0x" +
721 ", which is greater than the file size (0x" +
732template <
typename IntTy, std::enable_if_t<std::is_
unsigned_v<IntTy>,
int> = 0>
734 Error &ULEBSizeErr) {
740 if (
Value > std::numeric_limits<IntTy>::max()) {
741 ULEBSizeErr =
createError(
"ULEB128 value at offset 0x" +
743 Twine(std::numeric_limits<IntTy>::digits) +
747 return static_cast<IntTy
>(
Value);
750template <
typename ELFT>
755 std::vector<PGOAnalysisMap> *PGOAnalyses) {
762 if (IsRelocatable && RelaSec) {
764 "Can't read a SHT_LLVM_BB_ADDR_MAP section in a relocatable "
765 "object file without providing a relocation section.");
769 return createError(
"unable to read CREL relocations for section " +
773 FunctionOffsetTranslations[Rela.r_offset] = Rela.r_addend;
779 return createError(
"unable to read relocations for section " +
783 FunctionOffsetTranslations[Rela.r_offset] = Rela.r_addend;
786 auto GetAddressForRelocation =
789 FunctionOffsetTranslations.
find(RelocationOffsetInSection);
790 if (FOTIterator == FunctionOffsetTranslations.
end()) {
791 return createError(
"failed to get relocation data for offset: " +
793 " in section " +
describe(EF, Sec));
795 return FOTIterator->second;
803 std::unique_ptr<uint8_t[]> DecompressedContent;
806 if (!SectionNameOrErr)
808 auto DecompressorOrErr =
810 EF.
isLE(), ELFT::Is64Bits);
811 if (!DecompressorOrErr)
812 return DecompressorOrErr.takeError();
813 size_t DecompressedSize = DecompressorOrErr->getDecompressedSize();
814 DecompressedContent = std::make_unique<uint8_t[]>(DecompressedSize);
817 if (
Error Err = DecompressorOrErr->decompress(DecompressedContentRef))
818 return std::move(Err);
819 Content = DecompressedContentRef;
823 std::vector<BBAddrMap> FunctionEntries;
840 GetAddressForRelocation(RelocationOffsetInSection);
843 return *AddressOrErr;
849 while (!ULEBSizeErr && !MetadataDecodeErr && Cur &&
851 Version =
Data.getU8(Cur);
855 return createError(
"unsupported SHT_LLVM_BB_ADDR_MAP version: " +
856 Twine(
static_cast<int>(Version)));
857 Feature = Version < 5 ?
Data.getU8(Cur) :
Data.getU16(Cur);
861 if (!FeatEnableOrErr)
862 return FeatEnableOrErr.takeError();
863 FeatEnable = *FeatEnableOrErr;
865 return createError(
"version should be >= 3 for SHT_LLVM_BB_ADDR_MAP when "
866 "callsite offsets feature is enabled: version = " +
867 Twine(
static_cast<int>(Version)) +
868 " feature = " +
Twine(
static_cast<int>(Feature)));
869 if (FeatEnable.
BBHash && Version < 4)
870 return createError(
"version should be >= 4 for SHT_LLVM_BB_ADDR_MAP when "
871 "basic block hash feature is enabled: version = " +
872 Twine(
static_cast<int>(Version)) +
873 " feature = " +
Twine(
static_cast<int>(Feature)));
875 return createError(
"version should be >= 5 for SHT_LLVM_BB_ADDR_MAP when "
876 "post link cfg feature is enabled: version = " +
877 Twine(
static_cast<int>(Version)) +
878 " feature = " +
Twine(
static_cast<int>(Feature)));
884 if (!Cur || ULEBSizeErr)
887 return createError(
"invalid zero number of BB ranges at offset " +
891 auto AddressOrErr = ExtractAddress();
893 return AddressOrErr.takeError();
894 RangeBaseAddress = *AddressOrErr;
897 std::vector<BBAddrMap::BBRangeEntry> BBRangeEntries;
899 for (
uint32_t BBRangeIndex = 0; BBRangeIndex < NumBBRanges;
903 auto AddressOrErr = ExtractAddress();
905 return AddressOrErr.takeError();
906 RangeBaseAddress = *AddressOrErr;
909 std::vector<BBAddrMap::BBEntry> BBEntries;
911 for (
uint32_t BlockIndex = 0; !MetadataDecodeErr && !ULEBSizeErr &&
912 Cur && (BlockIndex < NumBlocksInBBRange);
922 CallsiteEndOffsets.
reserve(NumCallsites);
926 LastCallsiteEndOffset +=
928 CallsiteEndOffsets.
push_back(LastCallsiteEndOffset);
932 LastCallsiteEndOffset;
937 if (!MetadataOrErr) {
938 MetadataDecodeErr = MetadataOrErr.
takeError();
941 BBEntries.push_back({
ID,
Offset + PrevBBEndOffset,
Size,
942 *MetadataOrErr, CallsiteEndOffsets, Hash});
945 TotalNumBlocks += BBEntries.size();
947 BBRangeEntries.push_back({RangeBaseAddress, std::move(BBEntries)});
949 FunctionEntries.push_back({std::move(BBRangeEntries)});
958 std::vector<PGOAnalysisMap::PGOBBEntry> PGOBBEntries;
961 !ULEBSizeErr && Cur && (BlockIndex < TotalNumBlocks);
992 PGOBBEntries.push_back(
997 PGOAnalyses->push_back(
1003 if (!Cur || ULEBSizeErr || MetadataDecodeErr)
1005 std::move(MetadataDecodeErr));
1006 return FunctionEntries;
1009template <
class ELFT>
1012 std::vector<PGOAnalysisMap> *PGOAnalyses)
const {
1013 size_t OriginalPGOSize = PGOAnalyses ? PGOAnalyses->size() : 0;
1016 if (!AddrMapsOrErr && PGOAnalyses)
1017 PGOAnalyses->resize(OriginalPGOSize);
1018 return std::move(AddrMapsOrErr);
1021template <
class ELFT>
1030 if (!DoesSectionMatch) {
1034 if (*DoesSectionMatch) {
1047 ": failed to get a relocated section: " +
1051 const Elf_Shdr *ContentsSec = *RelSecOrErr;
1053 if (!DoesRelTargetMatch) {
1057 if (*DoesRelTargetMatch)
1058 SecToRelocMap[ContentsSec] = &Sec;
1061 return std::move(Errors);
1062 return SecToRelocMap;
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
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
static IntTy readULEB128As(DataExtractor &Data, DataExtractor::Cursor &Cur, Error &ULEBSizeErr)
#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
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
const T & back() const
back - Get the last element.
size_t size() const
size - Get the array size.
bool empty() const
empty - Check if the array is empty.
static BranchProbability getRaw(uint32_t N)
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 > try_emplace(const KeyT &Key, Ts &&...Args)
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
void reserve(size_type N)
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(uint64_t Val)
The instances of the Type class are immutable: once they are created, they are never changed.
LLVM Value Representation.
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
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
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)
static std::string describe(const ELFFile< ELFT > &Obj, const typename ELFT::Shdr &Sec)
LLVM_ABI StringRef getRISCVVendorRelocationTypeName(uint32_t Type, StringRef Vendor)
static Error decodeCrel(ArrayRef< uint8_t > Content, function_ref< void(uint64_t, bool)> HdrHandler, function_ref< void(Elf_Crel_Impl< Is64 >)> EntryHandler)
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...
FunctionAddr VTableAddr uintptr_t uintptr_t Version
Error joinErrors(Error E1, Error E2)
Concatenate errors.
FunctionAddr VTableAddr Count
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.
FunctionAddr VTableAddr uintptr_t uintptr_t Data
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.
static Expected< Metadata > decode(uint32_t V)
bool hasPGOAnalysisBBData() const
static Expected< Features > decode(uint16_t Val)
bool hasPGOAnalysis() const