16using namespace object;
18#define STRINGIFY_ENUM_CASE(ns, name) \
22#define ELF_RELOC(name, value) STRINGIFY_ENUM_CASE(ELF, name)
29#include "llvm/BinaryFormat/ELFRelocs/M68k.def"
36#include "llvm/BinaryFormat/ELFRelocs/x86_64.def"
44#include "llvm/BinaryFormat/ELFRelocs/i386.def"
51#include "llvm/BinaryFormat/ELFRelocs/Mips.def"
58#include "llvm/BinaryFormat/ELFRelocs/AArch64.def"
65#include "llvm/BinaryFormat/ELFRelocs/ARM.def"
73#include "llvm/BinaryFormat/ELFRelocs/ARC.def"
80#include "llvm/BinaryFormat/ELFRelocs/AVR.def"
87#include "llvm/BinaryFormat/ELFRelocs/Hexagon.def"
94#include "llvm/BinaryFormat/ELFRelocs/Lanai.def"
101#include "llvm/BinaryFormat/ELFRelocs/PowerPC.def"
108#include "llvm/BinaryFormat/ELFRelocs/PowerPC64.def"
115#include "llvm/BinaryFormat/ELFRelocs/RISCV.def"
122#include "llvm/BinaryFormat/ELFRelocs/SystemZ.def"
131#include "llvm/BinaryFormat/ELFRelocs/Sparc.def"
138#include "llvm/BinaryFormat/ELFRelocs/AMDGPU.def"
145#include "llvm/BinaryFormat/ELFRelocs/BPF.def"
152#include "llvm/BinaryFormat/ELFRelocs/MSP430.def"
159#include "llvm/BinaryFormat/ELFRelocs/VE.def"
166#include "llvm/BinaryFormat/ELFRelocs/CSKY.def"
173#include "llvm/BinaryFormat/ELFRelocs/LoongArch.def"
180#include "llvm/BinaryFormat/ELFRelocs/Xtensa.def"
196 return ELF::R_X86_64_RELATIVE;
199 return ELF::R_386_RELATIVE;
203 return ELF::R_AARCH64_RELATIVE;
205 return ELF::R_ARM_RELATIVE;
208 return ELF::R_ARC_RELATIVE;
212 return ELF::R_HEX_RELATIVE;
218 return ELF::R_PPC64_RELATIVE;
220 return ELF::R_RISCV_RELATIVE;
222 return ELF::R_390_RELATIVE;
226 return ELF::R_SPARC_RELATIVE;
228 return ELF::R_CKCORE_RELATIVE;
230 return ELF::R_VE_RELATIVE;
236 return ELF::R_LARCH_RELATIVE;
335std::vector<typename ELFT::Rel>
369 Rel.setType(getRelativeRelocationType(),
false);
370 std::vector<Elf_Rel> Relocs;
373 using Addr =
typename ELFT::uint;
376 for (Elf_Relr R : relrs) {
377 typename ELFT::uint Entry = R;
378 if ((Entry & 1) == 0) {
380 Rel.r_offset = Entry;
381 Relocs.push_back(Rel);
387 if ((Entry & 1) != 0) {
389 Relocs.push_back(Rel);
391 Base += (CHAR_BIT *
sizeof(Entry) - 1) *
sizeof(
Addr);
404 Hdr = Data.getULEB128(&Hdr, &Err);
413 std::vector<Elf_Rel> Rels;
414 std::vector<Elf_Rela> Relas;
417 Error Err = object::decodeCrel<ELFT::Is64Bits>(
428 Relas[
I].r_offset = Crel.r_offset;
429 Relas[
I].setSymbolAndType(Crel.r_symidx, Crel.r_type,
false);
430 Relas[
I++].r_addend = Crel.r_addend;
432 Rels[
I].r_offset = Crel.r_offset;
433 Rels[
I++].setSymbolAndType(Crel.r_symidx, Crel.r_type,
false);
437 return std::move(Err);
438 return std::make_pair(std::move(Rels), std::move(Relas));
461 return createError(
"invalid packed relocation header");
465 uint64_t NumRelocs = Data.getSLEB128(Cur);
472 std::vector<Elf_Rela> Relocs;
473 Relocs.reserve(NumRelocs);
475 uint64_t NumRelocsInGroup = Data.getSLEB128(Cur);
478 if (NumRelocsInGroup > NumRelocs)
479 return createError(
"relocation group unexpectedly large");
480 NumRelocs -= NumRelocsInGroup;
482 uint64_t GroupFlags = Data.getSLEB128(Cur);
489 if (GroupedByOffsetDelta)
490 GroupOffsetDelta = Data.getSLEB128(Cur);
494 GroupRInfo = Data.getSLEB128(Cur);
496 if (GroupedByAddend && GroupHasAddend)
497 Addend += Data.getSLEB128(Cur);
502 for (
uint64_t I = 0; Cur &&
I != NumRelocsInGroup; ++
I) {
504 Offset += GroupedByOffsetDelta ? GroupOffsetDelta : Data.getSLEB128(Cur);
506 R.r_info = GroupedByInfo ? GroupRInfo : Data.getSLEB128(Cur);
507 if (GroupHasAddend && !GroupedByAddend)
508 Addend += Data.getSLEB128(Cur);
522#define DYNAMIC_STRINGIFY_ENUM(tag, value) \
526#define DYNAMIC_TAG(n, v)
530#define AARCH64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
531#include "llvm/BinaryFormat/DynamicTags.def"
532#undef AARCH64_DYNAMIC_TAG
538#define HEXAGON_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
539#include "llvm/BinaryFormat/DynamicTags.def"
540#undef HEXAGON_DYNAMIC_TAG
546#define MIPS_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
547#include "llvm/BinaryFormat/DynamicTags.def"
548#undef MIPS_DYNAMIC_TAG
554#define PPC_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
555#include "llvm/BinaryFormat/DynamicTags.def"
556#undef PPC_DYNAMIC_TAG
562#define PPC64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
563#include "llvm/BinaryFormat/DynamicTags.def"
564#undef PPC64_DYNAMIC_TAG
570#define RISCV_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
571#include "llvm/BinaryFormat/DynamicTags.def"
572#undef RISCV_DYNAMIC_TAG
579#define AARCH64_DYNAMIC_TAG(name, value)
580#define MIPS_DYNAMIC_TAG(name, value)
581#define HEXAGON_DYNAMIC_TAG(name, value)
582#define PPC_DYNAMIC_TAG(name, value)
583#define PPC64_DYNAMIC_TAG(name, value)
584#define RISCV_DYNAMIC_TAG(name, value)
586#define DYNAMIC_TAG_MARKER(name, value)
587#define DYNAMIC_TAG(name, value) case value: return #name;
588#include "llvm/BinaryFormat/DynamicTags.def"
590#undef AARCH64_DYNAMIC_TAG
591#undef MIPS_DYNAMIC_TAG
592#undef HEXAGON_DYNAMIC_TAG
593#undef PPC_DYNAMIC_TAG
594#undef PPC64_DYNAMIC_TAG
595#undef RISCV_DYNAMIC_TAG
596#undef DYNAMIC_TAG_MARKER
597#undef DYNAMIC_STRINGIFY_ENUM
599 return "<unknown:>0x" + utohexstr(
Type,
true);
605 return getDynamicTagAsString(getHeader().e_machine,
Type);
612 auto ProgramHeadersOrError = program_headers();
613 if (!ProgramHeadersOrError)
614 return ProgramHeadersOrError.takeError();
616 for (
const Elf_Phdr &Phdr : *ProgramHeadersOrError) {
618 const uint8_t *DynOffset = base() + Phdr.p_offset;
619 if (DynOffset > end())
621 "dynamic section offset past file size: corrupted ELF");
622 Dyn =
ArrayRef(
reinterpret_cast<const Elf_Dyn *
>(DynOffset),
623 Phdr.p_filesz /
sizeof(Elf_Dyn));
632 if (!SectionsOrError)
633 return SectionsOrError.takeError();
635 for (
const Elf_Shdr &Sec : *SectionsOrError) {
638 getSectionContentsAsArray<Elf_Dyn>(Sec);
651 return createError(
"invalid empty dynamic section");
653 if (Dyn.
back().d_tag != ELF::DT_NULL)
654 return createError(
"dynamic sections must be DT_NULL terminated");
662 auto ProgramHeadersOrError = program_headers();
663 if (!ProgramHeadersOrError)
664 return ProgramHeadersOrError.takeError();
668 for (
const Elf_Phdr &Phdr : *ProgramHeadersOrError)
670 LoadSegments.
push_back(
const_cast<Elf_Phdr *
>(&Phdr));
674 return A->p_vaddr <
B->p_vaddr;
678 WarnHandler(
"loadable segments are unsorted by virtual address"))
685 return VAddr < Phdr->p_vaddr;
688 if (
I == LoadSegments.
begin())
689 return createError(
"virtual address is not in any segment: 0x" +
692 const Elf_Phdr &Phdr = **
I;
693 uint64_t Delta = VAddr - Phdr.p_vaddr;
694 if (Delta >= Phdr.p_filesz)
695 return createError(
"virtual address is not in any segment: 0x" +
699 if (
Offset >= getBufSize())
700 return createError(
"can't map virtual address 0x" +
702 Twine(&Phdr - (*ProgramHeadersOrError).data() + 1) +
703 ": the segment ends at 0x" +
705 ", which is greater than the file size (0x" +
716template <
typename IntTy, std::enable_if_t<std::is_
unsigned_v<IntTy>,
int> = 0>
718 Error &ULEBSizeErr) {
724 if (
Value > std::numeric_limits<IntTy>::max()) {
725 ULEBSizeErr =
createError(
"ULEB128 value at offset 0x" +
727 Twine(std::numeric_limits<IntTy>::digits) +
731 return static_cast<IntTy
>(
Value);
734template <
typename ELFT>
739 std::vector<PGOAnalysisMap> *PGOAnalyses) {
746 if (IsRelocatable && RelaSec) {
748 "Can't read a SHT_LLVM_BB_ADDR_MAP section in a relocatable "
749 "object file without providing a relocation section.");
752 return createError(
"unable to read relocations for section " +
756 FunctionOffsetTranslations[Rela.r_offset] = Rela.r_addend;
758 auto GetAddressForRelocation =
761 FunctionOffsetTranslations.
find(RelocationOffsetInSection);
762 if (FOTIterator == FunctionOffsetTranslations.
end()) {
763 return createError(
"failed to get relocation data for offset: " +
765 " in section " +
describe(EF, Sec));
767 return FOTIterator->second;
774 std::vector<BBAddrMap> FunctionEntries;
791 GetAddressForRelocation(RelocationOffsetInSection);
794 return *AddressOrErr;
800 while (!ULEBSizeErr && !MetadataDecodeErr && Cur &&
803 Version = Data.getU8(Cur);
807 return createError(
"unsupported SHT_LLVM_BB_ADDR_MAP version: " +
808 Twine(
static_cast<int>(Version)));
809 Feature = Data.getU8(Cur);
813 if (!FeatEnableOrErr)
814 return FeatEnableOrErr.takeError();
815 FeatEnable = *FeatEnableOrErr;
816 if (Feature != 0 && Version < 2 && Cur)
818 "version should be >= 2 for SHT_LLVM_BB_ADDR_MAP when "
819 "PGO features are enabled: version = " +
820 Twine(
static_cast<int>(Version)) +
821 " feature = " +
Twine(
static_cast<int>(Feature)));
826 if (FeatEnable.MultiBBRange) {
827 NumBBRanges = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
828 if (!Cur || ULEBSizeErr)
831 return createError(
"invalid zero number of BB ranges at offset " +
835 auto AddressOrErr = ExtractAddress();
837 return AddressOrErr.takeError();
838 RangeBaseAddress = *AddressOrErr;
839 NumBlocksInBBRange = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
841 std::vector<BBAddrMap::BBRangeEntry> BBRangeEntries;
843 for (
uint32_t BBRangeIndex = 0; BBRangeIndex < NumBBRanges;
846 if (FeatEnable.MultiBBRange) {
847 auto AddressOrErr = ExtractAddress();
849 return AddressOrErr.takeError();
850 RangeBaseAddress = *AddressOrErr;
851 NumBlocksInBBRange = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
853 std::vector<BBAddrMap::BBEntry> BBEntries;
854 if (!FeatEnable.OmitBBEntries) {
855 for (
uint32_t BlockIndex = 0; !MetadataDecodeErr && !ULEBSizeErr &&
856 Cur && (BlockIndex < NumBlocksInBBRange);
859 ? readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr)
861 uint32_t Offset = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
862 uint32_t Size = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
863 uint32_t MD = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
866 Offset += PrevBBEndOffset;
871 if (!MetadataOrErr) {
872 MetadataDecodeErr = MetadataOrErr.
takeError();
875 BBEntries.push_back({
ID,
Offset,
Size, *MetadataOrErr});
877 TotalNumBlocks += BBEntries.size();
879 BBRangeEntries.push_back({RangeBaseAddress, std::move(BBEntries)});
881 FunctionEntries.push_back({std::move(BBRangeEntries)});
883 if (PGOAnalyses || FeatEnable.hasPGOAnalysis()) {
886 FeatEnable.FuncEntryCount
887 ? readULEB128As<uint64_t>(Data, Cur, ULEBSizeErr)
890 std::vector<PGOAnalysisMap::PGOBBEntry> PGOBBEntries;
892 FeatEnable.hasPGOAnalysisBBData() && !MetadataDecodeErr &&
893 !ULEBSizeErr && Cur && (BlockIndex < TotalNumBlocks);
897 ? readULEB128As<uint64_t>(Data, Cur, ULEBSizeErr)
903 if (FeatEnable.BrProb) {
904 auto SuccCount = readULEB128As<uint64_t>(Data, Cur, ULEBSizeErr);
906 uint32_t BBID = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
907 uint32_t BrProb = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
914 PGOBBEntries.push_back({
BlockFrequency(BBF), std::move(Successors)});
918 PGOAnalyses->push_back(
924 if (!Cur || ULEBSizeErr || MetadataDecodeErr)
926 std::move(MetadataDecodeErr));
927 return FunctionEntries;
933 std::vector<PGOAnalysisMap> *PGOAnalyses)
const {
934 size_t OriginalPGOSize = PGOAnalyses ? PGOAnalyses->size() : 0;
937 if (!AddrMapsOrErr && PGOAnalyses)
938 PGOAnalyses->resize(OriginalPGOSize);
939 return std::move(AddrMapsOrErr);
951 if (!DoesSectionMatch) {
955 if (*DoesSectionMatch) {
956 if (SecToRelocMap.
insert(std::make_pair(&Sec, (
const Elf_Shdr *)
nullptr))
968 ": failed to get a relocated section: " +
972 const Elf_Shdr *ContentsSec = *RelSecOrErr;
974 if (!DoesRelTargetMatch) {
978 if (*DoesRelTargetMatch)
979 SecToRelocMap[ContentsSec] = &Sec;
982 return std::move(Errors);
983 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
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.
const char * toString(DWARFSectionKind Kind)
static Expected< Metadata > decode(uint32_t V)
static Expected< Features > decode(uint8_t Val)