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;
333std::vector<typename ELFT::Rel>
367 Rel.setType(getRelativeRelocationType(),
false);
368 std::vector<Elf_Rel> Relocs;
371 using Addr =
typename ELFT::uint;
374 for (Elf_Relr R : relrs) {
375 typename ELFT::uint Entry = R;
376 if ((Entry & 1) == 0) {
378 Rel.r_offset = Entry;
379 Relocs.push_back(Rel);
385 if ((Entry & 1) != 0) {
387 Relocs.push_back(Rel);
389 Base += (CHAR_BIT *
sizeof(Entry) - 1) *
sizeof(
Addr);
402 Hdr = Data.getULEB128(&Hdr, &Err);
411 std::vector<Elf_Rel> Rels;
412 std::vector<Elf_Rela> Relas;
415 Error Err = object::decodeCrel<ELFT::Is64Bits>(
426 Relas[
I].r_offset = Crel.r_offset;
427 Relas[
I].setSymbolAndType(Crel.r_symidx, Crel.r_type,
false);
428 Relas[
I++].r_addend = Crel.r_addend;
430 Rels[
I].r_offset = Crel.r_offset;
431 Rels[
I++].setSymbolAndType(Crel.r_symidx, Crel.r_type,
false);
435 return std::move(Err);
436 return std::make_pair(std::move(Rels), std::move(Relas));
459 return createError(
"invalid packed relocation header");
463 uint64_t NumRelocs = Data.getSLEB128(Cur);
470 std::vector<Elf_Rela> Relocs;
471 Relocs.reserve(NumRelocs);
473 uint64_t NumRelocsInGroup = Data.getSLEB128(Cur);
476 if (NumRelocsInGroup > NumRelocs)
477 return createError(
"relocation group unexpectedly large");
478 NumRelocs -= NumRelocsInGroup;
480 uint64_t GroupFlags = Data.getSLEB128(Cur);
487 if (GroupedByOffsetDelta)
488 GroupOffsetDelta = Data.getSLEB128(Cur);
492 GroupRInfo = Data.getSLEB128(Cur);
494 if (GroupedByAddend && GroupHasAddend)
495 Addend += Data.getSLEB128(Cur);
500 for (
uint64_t I = 0; Cur &&
I != NumRelocsInGroup; ++
I) {
502 Offset += GroupedByOffsetDelta ? GroupOffsetDelta : Data.getSLEB128(Cur);
504 R.r_info = GroupedByInfo ? GroupRInfo : Data.getSLEB128(Cur);
505 if (GroupHasAddend && !GroupedByAddend)
506 Addend += Data.getSLEB128(Cur);
520#define DYNAMIC_STRINGIFY_ENUM(tag, value) \
524#define DYNAMIC_TAG(n, v)
528#define AARCH64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
529#include "llvm/BinaryFormat/DynamicTags.def"
530#undef AARCH64_DYNAMIC_TAG
536#define HEXAGON_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
537#include "llvm/BinaryFormat/DynamicTags.def"
538#undef HEXAGON_DYNAMIC_TAG
544#define MIPS_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
545#include "llvm/BinaryFormat/DynamicTags.def"
546#undef MIPS_DYNAMIC_TAG
552#define PPC_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
553#include "llvm/BinaryFormat/DynamicTags.def"
554#undef PPC_DYNAMIC_TAG
560#define PPC64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
561#include "llvm/BinaryFormat/DynamicTags.def"
562#undef PPC64_DYNAMIC_TAG
568#define RISCV_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
569#include "llvm/BinaryFormat/DynamicTags.def"
570#undef RISCV_DYNAMIC_TAG
577#define AARCH64_DYNAMIC_TAG(name, value)
578#define MIPS_DYNAMIC_TAG(name, value)
579#define HEXAGON_DYNAMIC_TAG(name, value)
580#define PPC_DYNAMIC_TAG(name, value)
581#define PPC64_DYNAMIC_TAG(name, value)
582#define RISCV_DYNAMIC_TAG(name, value)
584#define DYNAMIC_TAG_MARKER(name, value)
585#define DYNAMIC_TAG(name, value) case value: return #name;
586#include "llvm/BinaryFormat/DynamicTags.def"
588#undef AARCH64_DYNAMIC_TAG
589#undef MIPS_DYNAMIC_TAG
590#undef HEXAGON_DYNAMIC_TAG
591#undef PPC_DYNAMIC_TAG
592#undef PPC64_DYNAMIC_TAG
593#undef RISCV_DYNAMIC_TAG
594#undef DYNAMIC_TAG_MARKER
595#undef DYNAMIC_STRINGIFY_ENUM
597 return "<unknown:>0x" + utohexstr(
Type,
true);
603 return getDynamicTagAsString(getHeader().e_machine,
Type);
610 auto ProgramHeadersOrError = program_headers();
611 if (!ProgramHeadersOrError)
612 return ProgramHeadersOrError.takeError();
614 for (
const Elf_Phdr &Phdr : *ProgramHeadersOrError) {
616 const uint8_t *DynOffset = base() + Phdr.p_offset;
617 if (DynOffset > end())
619 "dynamic section offset past file size: corrupted ELF");
620 Dyn =
ArrayRef(
reinterpret_cast<const Elf_Dyn *
>(DynOffset),
621 Phdr.p_filesz /
sizeof(Elf_Dyn));
630 if (!SectionsOrError)
631 return SectionsOrError.takeError();
633 for (
const Elf_Shdr &Sec : *SectionsOrError) {
636 getSectionContentsAsArray<Elf_Dyn>(Sec);
649 return createError(
"invalid empty dynamic section");
651 if (Dyn.
back().d_tag != ELF::DT_NULL)
652 return createError(
"dynamic sections must be DT_NULL terminated");
660 auto ProgramHeadersOrError = program_headers();
661 if (!ProgramHeadersOrError)
662 return ProgramHeadersOrError.takeError();
666 for (
const Elf_Phdr &Phdr : *ProgramHeadersOrError)
668 LoadSegments.
push_back(
const_cast<Elf_Phdr *
>(&Phdr));
672 return A->p_vaddr <
B->p_vaddr;
676 WarnHandler(
"loadable segments are unsorted by virtual address"))
683 return VAddr < Phdr->p_vaddr;
686 if (
I == LoadSegments.
begin())
687 return createError(
"virtual address is not in any segment: 0x" +
690 const Elf_Phdr &Phdr = **
I;
691 uint64_t Delta = VAddr - Phdr.p_vaddr;
692 if (Delta >= Phdr.p_filesz)
693 return createError(
"virtual address is not in any segment: 0x" +
697 if (
Offset >= getBufSize())
698 return createError(
"can't map virtual address 0x" +
700 Twine(&Phdr - (*ProgramHeadersOrError).data() + 1) +
701 ": the segment ends at 0x" +
703 ", which is greater than the file size (0x" +
714template <
typename IntTy, std::enable_if_t<std::is_
unsigned_v<IntTy>,
int> = 0>
716 Error &ULEBSizeErr) {
722 if (
Value > std::numeric_limits<IntTy>::max()) {
723 ULEBSizeErr =
createError(
"ULEB128 value at offset 0x" +
725 Twine(std::numeric_limits<IntTy>::digits) +
729 return static_cast<IntTy
>(
Value);
732template <
typename ELFT>
737 std::vector<PGOAnalysisMap> *PGOAnalyses) {
744 if (IsRelocatable && RelaSec) {
746 "Can't read a SHT_LLVM_BB_ADDR_MAP section in a relocatable "
747 "object file without providing a relocation section.");
750 return createError(
"unable to read relocations for section " +
754 FunctionOffsetTranslations[Rela.r_offset] = Rela.r_addend;
756 auto GetAddressForRelocation =
759 FunctionOffsetTranslations.
find(RelocationOffsetInSection);
760 if (FOTIterator == FunctionOffsetTranslations.
end()) {
761 return createError(
"failed to get relocation data for offset: " +
763 " in section " +
describe(EF, Sec));
765 return FOTIterator->second;
772 std::vector<BBAddrMap> FunctionEntries;
789 GetAddressForRelocation(RelocationOffsetInSection);
792 return *AddressOrErr;
798 while (!ULEBSizeErr && !MetadataDecodeErr && Cur &&
801 Version = Data.getU8(Cur);
805 return createError(
"unsupported SHT_LLVM_BB_ADDR_MAP version: " +
806 Twine(
static_cast<int>(Version)));
807 Feature = Data.getU8(Cur);
811 if (!FeatEnableOrErr)
812 return FeatEnableOrErr.takeError();
813 FeatEnable = *FeatEnableOrErr;
814 if (Feature != 0 && Version < 2 && Cur)
816 "version should be >= 2 for SHT_LLVM_BB_ADDR_MAP when "
817 "PGO features are enabled: version = " +
818 Twine(
static_cast<int>(Version)) +
819 " feature = " +
Twine(
static_cast<int>(Feature)));
824 std::vector<BBAddrMap::BBEntry> BBEntries;
825 if (FeatEnable.MultiBBRange) {
826 NumBBRanges = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
827 if (!Cur || ULEBSizeErr)
830 return createError(
"invalid zero number of BB ranges at offset " +
834 auto AddressOrErr = ExtractAddress();
836 return AddressOrErr.takeError();
837 RangeBaseAddress = *AddressOrErr;
838 NumBlocksInBBRange = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
840 std::vector<BBAddrMap::BBRangeEntry> BBRangeEntries;
842 for (
uint32_t BBRangeIndex = 0; BBRangeIndex < NumBBRanges;
845 if (FeatEnable.MultiBBRange) {
846 auto AddressOrErr = ExtractAddress();
848 return AddressOrErr.takeError();
849 RangeBaseAddress = *AddressOrErr;
850 NumBlocksInBBRange = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
852 for (
uint32_t BlockIndex = 0; !MetadataDecodeErr && !ULEBSizeErr && Cur &&
853 (BlockIndex < NumBlocksInBBRange);
856 ? readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr)
858 uint32_t Offset = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
859 uint32_t Size = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
860 uint32_t MD = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
863 Offset += PrevBBEndOffset;
868 if (!MetadataOrErr) {
869 MetadataDecodeErr = MetadataOrErr.
takeError();
872 BBEntries.push_back({
ID,
Offset,
Size, *MetadataOrErr});
874 TotalNumBlocks += BBEntries.size();
875 BBRangeEntries.push_back({RangeBaseAddress, std::move(BBEntries)});
877 FunctionEntries.push_back({std::move(BBRangeEntries)});
879 if (PGOAnalyses || FeatEnable.hasPGOAnalysis()) {
882 FeatEnable.FuncEntryCount
883 ? readULEB128As<uint64_t>(Data, Cur, ULEBSizeErr)
886 std::vector<PGOAnalysisMap::PGOBBEntry> PGOBBEntries;
888 FeatEnable.hasPGOAnalysisBBData() && !MetadataDecodeErr &&
889 !ULEBSizeErr && Cur && (BlockIndex < TotalNumBlocks);
893 ? readULEB128As<uint64_t>(Data, Cur, ULEBSizeErr)
899 if (FeatEnable.BrProb) {
900 auto SuccCount = readULEB128As<uint64_t>(Data, Cur, ULEBSizeErr);
902 uint32_t BBID = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
903 uint32_t BrProb = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
910 PGOBBEntries.push_back({
BlockFrequency(BBF), std::move(Successors)});
914 PGOAnalyses->push_back(
920 if (!Cur || ULEBSizeErr || MetadataDecodeErr)
922 std::move(MetadataDecodeErr));
923 return FunctionEntries;
929 std::vector<PGOAnalysisMap> *PGOAnalyses)
const {
930 size_t OriginalPGOSize = PGOAnalyses ? PGOAnalyses->size() : 0;
933 if (!AddrMapsOrErr && PGOAnalyses)
934 PGOAnalyses->resize(OriginalPGOSize);
935 return std::move(AddrMapsOrErr);
947 if (!DoesSectionMatch) {
951 if (*DoesSectionMatch) {
952 if (SecToRelocMap.
insert(std::make_pair(&Sec, (
const Elf_Shdr *)
nullptr))
964 ": failed to get a relocated section: " +
968 const Elf_Shdr *ContentsSec = *RelSecOrErr;
970 if (!DoesRelTargetMatch) {
974 if (*DoesRelTargetMatch)
975 SecToRelocMap[ContentsSec] = &Sec;
978 return std::move(Errors);
979 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")
COFF::MachineTypes Machine
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)
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.
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 > 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.
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...
Expected< ArrayRef< uint8_t > > getSectionContents(const Elf_Shdr &Sec) const
Expected< Elf_Rela_Range > relas(const Elf_Shdr &Sec) const
Expected< RelsOrRelas > decodeCrel(ArrayRef< uint8_t > Content) const
Expected< uint64_t > getCrelHeader(ArrayRef< uint8_t > Content) const
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 ...
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
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)
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)
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)
static Expected< Features > decode(uint8_t Val)