//===- ELFTypes.h - Endian specific types for ELF ---------------*- C++ -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// #ifndef LLVM_OBJECT_ELFTYPES_H #define LLVM_OBJECT_ELFTYPES_H #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/StringRef.h" #include "llvm/BinaryFormat/ELF.h" #include "llvm/Object/Error.h" #include "llvm/Support/BlockFrequency.h" #include "llvm/Support/BranchProbability.h" #include "llvm/Support/Endian.h" #include "llvm/Support/Error.h" #include "llvm/Support/MathExtras.h" #include #include #include #include namespace llvm { namespace object { template struct Elf_Ehdr_Impl; template struct Elf_Shdr_Impl; template struct Elf_Sym_Impl; template struct Elf_Dyn_Impl; template struct Elf_Phdr_Impl; template struct Elf_Rel_Impl; template struct Elf_Verdef_Impl; template struct Elf_Verdaux_Impl; template struct Elf_Verneed_Impl; template struct Elf_Vernaux_Impl; template struct Elf_Versym_Impl; template struct Elf_Hash_Impl; template struct Elf_GnuHash_Impl; template struct Elf_Chdr_Impl; template struct Elf_Nhdr_Impl; template class Elf_Note_Impl; template class Elf_Note_Iterator_Impl; template struct Elf_CGProfile_Impl; template struct ELFType { private: template using packed = support::detail::packed_endian_specific_integral; public: static const endianness TargetEndianness = E; static const bool Is64Bits = Is64; using uint = std::conditional_t; using Ehdr = Elf_Ehdr_Impl>; using Shdr = Elf_Shdr_Impl>; using Sym = Elf_Sym_Impl>; using Dyn = Elf_Dyn_Impl>; using Phdr = Elf_Phdr_Impl>; using Rel = Elf_Rel_Impl, false>; using Rela = Elf_Rel_Impl, true>; using Relr = packed; using Verdef = Elf_Verdef_Impl>; using Verdaux = Elf_Verdaux_Impl>; using Verneed = Elf_Verneed_Impl>; using Vernaux = Elf_Vernaux_Impl>; using Versym = Elf_Versym_Impl>; using Hash = Elf_Hash_Impl>; using GnuHash = Elf_GnuHash_Impl>; using Chdr = Elf_Chdr_Impl>; using Nhdr = Elf_Nhdr_Impl>; using Note = Elf_Note_Impl>; using NoteIterator = Elf_Note_Iterator_Impl>; using CGProfile = Elf_CGProfile_Impl>; using DynRange = ArrayRef; using ShdrRange = ArrayRef; using SymRange = ArrayRef; using RelRange = ArrayRef; using RelaRange = ArrayRef; using RelrRange = ArrayRef; using PhdrRange = ArrayRef; using Half = packed; using Word = packed; using Sword = packed; using Xword = packed; using Sxword = packed; using Addr = packed; using Off = packed; }; using ELF32LE = ELFType; using ELF32BE = ELFType; using ELF64LE = ELFType; using ELF64BE = ELFType; // Use an alignment of 2 for the typedefs since that is the worst case for // ELF files in archives. // I really don't like doing this, but the alternative is copypasta. #define LLVM_ELF_IMPORT_TYPES_ELFT(ELFT) \ using Elf_Addr = typename ELFT::Addr; \ using Elf_Off = typename ELFT::Off; \ using Elf_Half = typename ELFT::Half; \ using Elf_Word = typename ELFT::Word; \ using Elf_Sword = typename ELFT::Sword; \ using Elf_Xword = typename ELFT::Xword; \ using Elf_Sxword = typename ELFT::Sxword; \ using uintX_t = typename ELFT::uint; \ using Elf_Ehdr = typename ELFT::Ehdr; \ using Elf_Shdr = typename ELFT::Shdr; \ using Elf_Sym = typename ELFT::Sym; \ using Elf_Dyn = typename ELFT::Dyn; \ using Elf_Phdr = typename ELFT::Phdr; \ using Elf_Rel = typename ELFT::Rel; \ using Elf_Rela = typename ELFT::Rela; \ using Elf_Relr = typename ELFT::Relr; \ using Elf_Verdef = typename ELFT::Verdef; \ using Elf_Verdaux = typename ELFT::Verdaux; \ using Elf_Verneed = typename ELFT::Verneed; \ using Elf_Vernaux = typename ELFT::Vernaux; \ using Elf_Versym = typename ELFT::Versym; \ using Elf_Hash = typename ELFT::Hash; \ using Elf_GnuHash = typename ELFT::GnuHash; \ using Elf_Chdr = typename ELFT::Chdr; \ using Elf_Nhdr = typename ELFT::Nhdr; \ using Elf_Note = typename ELFT::Note; \ using Elf_Note_Iterator = typename ELFT::NoteIterator; \ using Elf_CGProfile = typename ELFT::CGProfile; \ using Elf_Dyn_Range = typename ELFT::DynRange; \ using Elf_Shdr_Range = typename ELFT::ShdrRange; \ using Elf_Sym_Range = typename ELFT::SymRange; \ using Elf_Rel_Range = typename ELFT::RelRange; \ using Elf_Rela_Range = typename ELFT::RelaRange; \ using Elf_Relr_Range = typename ELFT::RelrRange; \ using Elf_Phdr_Range = typename ELFT::PhdrRange; #define LLVM_ELF_COMMA , #define LLVM_ELF_IMPORT_TYPES(E, W) \ LLVM_ELF_IMPORT_TYPES_ELFT(ELFType) // Section header. template struct Elf_Shdr_Base; template struct Elf_Shdr_Base> { LLVM_ELF_IMPORT_TYPES(TargetEndianness, false) Elf_Word sh_name; // Section name (index into string table) Elf_Word sh_type; // Section type (SHT_*) Elf_Word sh_flags; // Section flags (SHF_*) Elf_Addr sh_addr; // Address where section is to be loaded Elf_Off sh_offset; // File offset of section data, in bytes Elf_Word sh_size; // Size of section, in bytes Elf_Word sh_link; // Section type-specific header table index link Elf_Word sh_info; // Section type-specific extra information Elf_Word sh_addralign; // Section address alignment Elf_Word sh_entsize; // Size of records contained within the section }; template struct Elf_Shdr_Base> { LLVM_ELF_IMPORT_TYPES(TargetEndianness, true) Elf_Word sh_name; // Section name (index into string table) Elf_Word sh_type; // Section type (SHT_*) Elf_Xword sh_flags; // Section flags (SHF_*) Elf_Addr sh_addr; // Address where section is to be loaded Elf_Off sh_offset; // File offset of section data, in bytes Elf_Xword sh_size; // Size of section, in bytes Elf_Word sh_link; // Section type-specific header table index link Elf_Word sh_info; // Section type-specific extra information Elf_Xword sh_addralign; // Section address alignment Elf_Xword sh_entsize; // Size of records contained within the section }; template struct Elf_Shdr_Impl : Elf_Shdr_Base { using Elf_Shdr_Base::sh_entsize; using Elf_Shdr_Base::sh_size; /// Get the number of entities this section contains if it has any. unsigned getEntityCount() const { if (sh_entsize == 0) return 0; return sh_size / sh_entsize; } }; template struct Elf_Sym_Base; template struct Elf_Sym_Base> { LLVM_ELF_IMPORT_TYPES(TargetEndianness, false) Elf_Word st_name; // Symbol name (index into string table) Elf_Addr st_value; // Value or address associated with the symbol Elf_Word st_size; // Size of the symbol unsigned char st_info; // Symbol's type and binding attributes unsigned char st_other; // Must be zero; reserved Elf_Half st_shndx; // Which section (header table index) it's defined in }; template struct Elf_Sym_Base> { LLVM_ELF_IMPORT_TYPES(TargetEndianness, true) Elf_Word st_name; // Symbol name (index into string table) unsigned char st_info; // Symbol's type and binding attributes unsigned char st_other; // Must be zero; reserved Elf_Half st_shndx; // Which section (header table index) it's defined in Elf_Addr st_value; // Value or address associated with the symbol Elf_Xword st_size; // Size of the symbol }; template struct Elf_Sym_Impl : Elf_Sym_Base { using Elf_Sym_Base::st_info; using Elf_Sym_Base::st_shndx; using Elf_Sym_Base::st_other; using Elf_Sym_Base::st_value; // These accessors and mutators correspond to the ELF32_ST_BIND, // ELF32_ST_TYPE, and ELF32_ST_INFO macros defined in the ELF specification: unsigned char getBinding() const { return st_info >> 4; } unsigned char getType() const { return st_info & 0x0f; } uint64_t getValue() const { return st_value; } void setBinding(unsigned char b) { setBindingAndType(b, getType()); } void setType(unsigned char t) { setBindingAndType(getBinding(), t); } void setBindingAndType(unsigned char b, unsigned char t) { st_info = (b << 4) + (t & 0x0f); } /// Access to the STV_xxx flag stored in the first two bits of st_other. /// STV_DEFAULT: 0 /// STV_INTERNAL: 1 /// STV_HIDDEN: 2 /// STV_PROTECTED: 3 unsigned char getVisibility() const { return st_other & 0x3; } void setVisibility(unsigned char v) { assert(v < 4 && "Invalid value for visibility"); st_other = (st_other & ~0x3) | v; } bool isAbsolute() const { return st_shndx == ELF::SHN_ABS; } bool isCommon() const { return getType() == ELF::STT_COMMON || st_shndx == ELF::SHN_COMMON; } bool isDefined() const { return !isUndefined(); } bool isProcessorSpecific() const { return st_shndx >= ELF::SHN_LOPROC && st_shndx <= ELF::SHN_HIPROC; } bool isOSSpecific() const { return st_shndx >= ELF::SHN_LOOS && st_shndx <= ELF::SHN_HIOS; } bool isReserved() const { // ELF::SHN_HIRESERVE is 0xffff so st_shndx <= ELF::SHN_HIRESERVE is always // true and some compilers warn about it. return st_shndx >= ELF::SHN_LORESERVE; } bool isUndefined() const { return st_shndx == ELF::SHN_UNDEF; } bool isExternal() const { return getBinding() != ELF::STB_LOCAL; } Expected getName(StringRef StrTab) const; }; template Expected Elf_Sym_Impl::getName(StringRef StrTab) const { uint32_t Offset = this->st_name; if (Offset >= StrTab.size()) return createStringError(object_error::parse_failed, "st_name (0x%" PRIx32 ") is past the end of the string table" " of size 0x%zx", Offset, StrTab.size()); return StringRef(StrTab.data() + Offset); } /// Elf_Versym: This is the structure of entries in the SHT_GNU_versym section /// (.gnu.version). This structure is identical for ELF32 and ELF64. template struct Elf_Versym_Impl { LLVM_ELF_IMPORT_TYPES_ELFT(ELFT) Elf_Half vs_index; // Version index with flags (e.g. VERSYM_HIDDEN) }; /// Elf_Verdef: This is the structure of entries in the SHT_GNU_verdef section /// (.gnu.version_d). This structure is identical for ELF32 and ELF64. template struct Elf_Verdef_Impl { LLVM_ELF_IMPORT_TYPES_ELFT(ELFT) Elf_Half vd_version; // Version of this structure (e.g. VER_DEF_CURRENT) Elf_Half vd_flags; // Bitwise flags (VER_DEF_*) Elf_Half vd_ndx; // Version index, used in .gnu.version entries Elf_Half vd_cnt; // Number of Verdaux entries Elf_Word vd_hash; // Hash of name Elf_Word vd_aux; // Offset to the first Verdaux entry (in bytes) Elf_Word vd_next; // Offset to the next Verdef entry (in bytes) /// Get the first Verdaux entry for this Verdef. const Elf_Verdaux *getAux() const { return reinterpret_cast((const char *)this + vd_aux); } }; /// Elf_Verdaux: This is the structure of auxiliary data in the SHT_GNU_verdef /// section (.gnu.version_d). This structure is identical for ELF32 and ELF64. template struct Elf_Verdaux_Impl { LLVM_ELF_IMPORT_TYPES_ELFT(ELFT) Elf_Word vda_name; // Version name (offset in string table) Elf_Word vda_next; // Offset to next Verdaux entry (in bytes) }; /// Elf_Verneed: This is the structure of entries in the SHT_GNU_verneed /// section (.gnu.version_r). This structure is identical for ELF32 and ELF64. template struct Elf_Verneed_Impl { LLVM_ELF_IMPORT_TYPES_ELFT(ELFT) Elf_Half vn_version; // Version of this structure (e.g. VER_NEED_CURRENT) Elf_Half vn_cnt; // Number of associated Vernaux entries Elf_Word vn_file; // Library name (string table offset) Elf_Word vn_aux; // Offset to first Vernaux entry (in bytes) Elf_Word vn_next; // Offset to next Verneed entry (in bytes) }; /// Elf_Vernaux: This is the structure of auxiliary data in SHT_GNU_verneed /// section (.gnu.version_r). This structure is identical for ELF32 and ELF64. template struct Elf_Vernaux_Impl { LLVM_ELF_IMPORT_TYPES_ELFT(ELFT) Elf_Word vna_hash; // Hash of dependency name Elf_Half vna_flags; // Bitwise Flags (VER_FLAG_*) Elf_Half vna_other; // Version index, used in .gnu.version entries Elf_Word vna_name; // Dependency name Elf_Word vna_next; // Offset to next Vernaux entry (in bytes) }; /// Elf_Dyn_Base: This structure matches the form of entries in the dynamic /// table section (.dynamic) look like. template struct Elf_Dyn_Base; template struct Elf_Dyn_Base> { LLVM_ELF_IMPORT_TYPES(TargetEndianness, false) Elf_Sword d_tag; union { Elf_Word d_val; Elf_Addr d_ptr; } d_un; }; template struct Elf_Dyn_Base> { LLVM_ELF_IMPORT_TYPES(TargetEndianness, true) Elf_Sxword d_tag; union { Elf_Xword d_val; Elf_Addr d_ptr; } d_un; }; /// Elf_Dyn_Impl: This inherits from Elf_Dyn_Base, adding getters. template struct Elf_Dyn_Impl : Elf_Dyn_Base { using Elf_Dyn_Base::d_tag; using Elf_Dyn_Base::d_un; using intX_t = std::conditional_t; using uintX_t = std::conditional_t; intX_t getTag() const { return d_tag; } uintX_t getVal() const { return d_un.d_val; } uintX_t getPtr() const { return d_un.d_ptr; } }; template struct Elf_Rel_Impl, false> { LLVM_ELF_IMPORT_TYPES(TargetEndianness, false) static const bool IsRela = false; Elf_Addr r_offset; // Location (file byte offset, or program virtual addr) Elf_Word r_info; // Symbol table index and type of relocation to apply uint32_t getRInfo(bool isMips64EL) const { assert(!isMips64EL); return r_info; } void setRInfo(uint32_t R, bool IsMips64EL) { assert(!IsMips64EL); r_info = R; } // These accessors and mutators correspond to the ELF32_R_SYM, ELF32_R_TYPE, // and ELF32_R_INFO macros defined in the ELF specification: uint32_t getSymbol(bool isMips64EL) const { return this->getRInfo(isMips64EL) >> 8; } unsigned char getType(bool isMips64EL) const { return (unsigned char)(this->getRInfo(isMips64EL) & 0x0ff); } void setSymbol(uint32_t s, bool IsMips64EL) { setSymbolAndType(s, getType(IsMips64EL), IsMips64EL); } void setType(unsigned char t, bool IsMips64EL) { setSymbolAndType(getSymbol(IsMips64EL), t, IsMips64EL); } void setSymbolAndType(uint32_t s, unsigned char t, bool IsMips64EL) { this->setRInfo((s << 8) + t, IsMips64EL); } }; template struct Elf_Rel_Impl, true> : public Elf_Rel_Impl, false> { LLVM_ELF_IMPORT_TYPES(TargetEndianness, false) static const bool IsRela = true; Elf_Sword r_addend; // Compute value for relocatable field by adding this }; template struct Elf_Rel_Impl, false> { LLVM_ELF_IMPORT_TYPES(TargetEndianness, true) static const bool IsRela = false; Elf_Addr r_offset; // Location (file byte offset, or program virtual addr) Elf_Xword r_info; // Symbol table index and type of relocation to apply uint64_t getRInfo(bool isMips64EL) const { uint64_t t = r_info; if (!isMips64EL) return t; // Mips64 little endian has a "special" encoding of r_info. Instead of one // 64 bit little endian number, it is a little endian 32 bit number followed // by a 32 bit big endian number. return (t << 32) | ((t >> 8) & 0xff000000) | ((t >> 24) & 0x00ff0000) | ((t >> 40) & 0x0000ff00) | ((t >> 56) & 0x000000ff); } void setRInfo(uint64_t R, bool IsMips64EL) { if (IsMips64EL) r_info = (R >> 32) | ((R & 0xff000000) << 8) | ((R & 0x00ff0000) << 24) | ((R & 0x0000ff00) << 40) | ((R & 0x000000ff) << 56); else r_info = R; } // These accessors and mutators correspond to the ELF64_R_SYM, ELF64_R_TYPE, // and ELF64_R_INFO macros defined in the ELF specification: uint32_t getSymbol(bool isMips64EL) const { return (uint32_t)(this->getRInfo(isMips64EL) >> 32); } uint32_t getType(bool isMips64EL) const { return (uint32_t)(this->getRInfo(isMips64EL) & 0xffffffffL); } void setSymbol(uint32_t s, bool IsMips64EL) { setSymbolAndType(s, getType(IsMips64EL), IsMips64EL); } void setType(uint32_t t, bool IsMips64EL) { setSymbolAndType(getSymbol(IsMips64EL), t, IsMips64EL); } void setSymbolAndType(uint32_t s, uint32_t t, bool IsMips64EL) { this->setRInfo(((uint64_t)s << 32) + (t & 0xffffffffL), IsMips64EL); } }; template struct Elf_Rel_Impl, true> : public Elf_Rel_Impl, false> { LLVM_ELF_IMPORT_TYPES(TargetEndianness, true) static const bool IsRela = true; Elf_Sxword r_addend; // Compute value for relocatable field by adding this. }; template struct Elf_Ehdr_Impl { LLVM_ELF_IMPORT_TYPES_ELFT(ELFT) unsigned char e_ident[ELF::EI_NIDENT]; // ELF Identification bytes Elf_Half e_type; // Type of file (see ET_*) Elf_Half e_machine; // Required architecture for this file (see EM_*) Elf_Word e_version; // Must be equal to 1 Elf_Addr e_entry; // Address to jump to in order to start program Elf_Off e_phoff; // Program header table's file offset, in bytes Elf_Off e_shoff; // Section header table's file offset, in bytes Elf_Word e_flags; // Processor-specific flags Elf_Half e_ehsize; // Size of ELF header, in bytes Elf_Half e_phentsize; // Size of an entry in the program header table Elf_Half e_phnum; // Number of entries in the program header table Elf_Half e_shentsize; // Size of an entry in the section header table Elf_Half e_shnum; // Number of entries in the section header table Elf_Half e_shstrndx; // Section header table index of section name // string table bool checkMagic() const { return (memcmp(e_ident, ELF::ElfMagic, strlen(ELF::ElfMagic))) == 0; } unsigned char getFileClass() const { return e_ident[ELF::EI_CLASS]; } unsigned char getDataEncoding() const { return e_ident[ELF::EI_DATA]; } }; template struct Elf_Phdr_Impl> { LLVM_ELF_IMPORT_TYPES(TargetEndianness, false) Elf_Word p_type; // Type of segment Elf_Off p_offset; // FileOffset where segment is located, in bytes Elf_Addr p_vaddr; // Virtual Address of beginning of segment Elf_Addr p_paddr; // Physical address of beginning of segment (OS-specific) Elf_Word p_filesz; // Num. of bytes in file image of segment (may be zero) Elf_Word p_memsz; // Num. of bytes in mem image of segment (may be zero) Elf_Word p_flags; // Segment flags Elf_Word p_align; // Segment alignment constraint }; template struct Elf_Phdr_Impl> { LLVM_ELF_IMPORT_TYPES(TargetEndianness, true) Elf_Word p_type; // Type of segment Elf_Word p_flags; // Segment flags Elf_Off p_offset; // FileOffset where segment is located, in bytes Elf_Addr p_vaddr; // Virtual Address of beginning of segment Elf_Addr p_paddr; // Physical address of beginning of segment (OS-specific) Elf_Xword p_filesz; // Num. of bytes in file image of segment (may be zero) Elf_Xword p_memsz; // Num. of bytes in mem image of segment (may be zero) Elf_Xword p_align; // Segment alignment constraint }; // ELFT needed for endianness. template struct Elf_Hash_Impl { LLVM_ELF_IMPORT_TYPES_ELFT(ELFT) Elf_Word nbucket; Elf_Word nchain; ArrayRef buckets() const { return ArrayRef(&nbucket + 2, &nbucket + 2 + nbucket); } ArrayRef chains() const { return ArrayRef(&nbucket + 2 + nbucket, &nbucket + 2 + nbucket + nchain); } }; // .gnu.hash section template struct Elf_GnuHash_Impl { LLVM_ELF_IMPORT_TYPES_ELFT(ELFT) Elf_Word nbuckets; Elf_Word symndx; Elf_Word maskwords; Elf_Word shift2; ArrayRef filter() const { return ArrayRef(reinterpret_cast(&shift2 + 1), maskwords); } ArrayRef buckets() const { return ArrayRef( reinterpret_cast(filter().end()), nbuckets); } ArrayRef values(unsigned DynamicSymCount) const { assert(DynamicSymCount >= symndx); return ArrayRef(buckets().end(), DynamicSymCount - symndx); } }; // Compressed section headers. // http://www.sco.com/developers/gabi/latest/ch4.sheader.html#compression_header template struct Elf_Chdr_Impl> { LLVM_ELF_IMPORT_TYPES(TargetEndianness, false) Elf_Word ch_type; Elf_Word ch_size; Elf_Word ch_addralign; }; template struct Elf_Chdr_Impl> { LLVM_ELF_IMPORT_TYPES(TargetEndianness, true) Elf_Word ch_type; Elf_Word ch_reserved; Elf_Xword ch_size; Elf_Xword ch_addralign; }; /// Note header template struct Elf_Nhdr_Impl { LLVM_ELF_IMPORT_TYPES_ELFT(ELFT) Elf_Word n_namesz; Elf_Word n_descsz; Elf_Word n_type; /// Get the size of the note, including name, descriptor, and padding. Both /// the start and the end of the descriptor are aligned by the section /// alignment. In practice many 64-bit systems deviate from the generic ABI by /// using sh_addralign=4. size_t getSize(size_t Align) const { return alignToPowerOf2(sizeof(*this) + n_namesz, Align) + alignToPowerOf2(n_descsz, Align); } }; /// An ELF note. /// /// Wraps a note header, providing methods for accessing the name and /// descriptor safely. template class Elf_Note_Impl { LLVM_ELF_IMPORT_TYPES_ELFT(ELFT) const Elf_Nhdr_Impl &Nhdr; template friend class Elf_Note_Iterator_Impl; public: Elf_Note_Impl(const Elf_Nhdr_Impl &Nhdr) : Nhdr(Nhdr) {} /// Get the note's name, excluding the terminating null byte. StringRef getName() const { if (!Nhdr.n_namesz) return StringRef(); return StringRef(reinterpret_cast(&Nhdr) + sizeof(Nhdr), Nhdr.n_namesz - 1); } /// Get the note's descriptor. ArrayRef getDesc(size_t Align) const { if (!Nhdr.n_descsz) return ArrayRef(); return ArrayRef( reinterpret_cast(&Nhdr) + alignToPowerOf2(sizeof(Nhdr) + Nhdr.n_namesz, Align), Nhdr.n_descsz); } /// Get the note's descriptor as StringRef StringRef getDescAsStringRef(size_t Align) const { ArrayRef Desc = getDesc(Align); return StringRef(reinterpret_cast(Desc.data()), Desc.size()); } /// Get the note's type. Elf_Word getType() const { return Nhdr.n_type; } }; template class Elf_Note_Iterator_Impl { public: using iterator_category = std::forward_iterator_tag; using value_type = Elf_Note_Impl; using difference_type = std::ptrdiff_t; using pointer = value_type *; using reference = value_type &; private: // Nhdr being a nullptr marks the end of iteration. const Elf_Nhdr_Impl *Nhdr = nullptr; size_t RemainingSize = 0u; size_t Align = 0; Error *Err = nullptr; template friend class ELFFile; // Stop iteration and indicate an overflow. void stopWithOverflowError() { Nhdr = nullptr; *Err = make_error("ELF note overflows container", object_error::parse_failed); } // Advance Nhdr by NoteSize bytes, starting from NhdrPos. // // Assumes NoteSize <= RemainingSize. Ensures Nhdr->getSize() <= RemainingSize // upon returning. Handles stopping iteration when reaching the end of the // container, either cleanly or with an overflow error. void advanceNhdr(const uint8_t *NhdrPos, size_t NoteSize) { RemainingSize -= NoteSize; if (RemainingSize == 0u) { // Ensure that if the iterator walks to the end, the error is checked // afterwards. *Err = Error::success(); Nhdr = nullptr; } else if (sizeof(*Nhdr) > RemainingSize) stopWithOverflowError(); else { Nhdr = reinterpret_cast *>(NhdrPos + NoteSize); if (Nhdr->getSize(Align) > RemainingSize) stopWithOverflowError(); else *Err = Error::success(); } } Elf_Note_Iterator_Impl() = default; explicit Elf_Note_Iterator_Impl(Error &Err) : Err(&Err) {} Elf_Note_Iterator_Impl(const uint8_t *Start, size_t Size, size_t Align, Error &Err) : RemainingSize(Size), Align(Align), Err(&Err) { consumeError(std::move(Err)); assert(Start && "ELF note iterator starting at NULL"); advanceNhdr(Start, 0u); } public: Elf_Note_Iterator_Impl &operator++() { assert(Nhdr && "incremented ELF note end iterator"); const uint8_t *NhdrPos = reinterpret_cast(Nhdr); size_t NoteSize = Nhdr->getSize(Align); advanceNhdr(NhdrPos, NoteSize); return *this; } bool operator==(Elf_Note_Iterator_Impl Other) const { if (!Nhdr && Other.Err) (void)(bool)(*Other.Err); if (!Other.Nhdr && Err) (void)(bool)(*Err); return Nhdr == Other.Nhdr; } bool operator!=(Elf_Note_Iterator_Impl Other) const { return !(*this == Other); } Elf_Note_Impl operator*() const { assert(Nhdr && "dereferenced ELF note end iterator"); return Elf_Note_Impl(*Nhdr); } }; template struct Elf_CGProfile_Impl { LLVM_ELF_IMPORT_TYPES_ELFT(ELFT) Elf_Xword cgp_weight; }; // MIPS .reginfo section template struct Elf_Mips_RegInfo; template struct Elf_Mips_RegInfo> { LLVM_ELF_IMPORT_TYPES(TargetEndianness, false) Elf_Word ri_gprmask; // bit-mask of used general registers Elf_Word ri_cprmask[4]; // bit-mask of used co-processor registers Elf_Addr ri_gp_value; // gp register value }; template struct Elf_Mips_RegInfo> { LLVM_ELF_IMPORT_TYPES(TargetEndianness, true) Elf_Word ri_gprmask; // bit-mask of used general registers Elf_Word ri_pad; // unused padding field Elf_Word ri_cprmask[4]; // bit-mask of used co-processor registers Elf_Addr ri_gp_value; // gp register value }; // .MIPS.options section template struct Elf_Mips_Options { LLVM_ELF_IMPORT_TYPES_ELFT(ELFT) uint8_t kind; // Determines interpretation of variable part of descriptor uint8_t size; // Byte size of descriptor, including this header Elf_Half section; // Section header index of section affected, // or 0 for global options Elf_Word info; // Kind-specific information Elf_Mips_RegInfo &getRegInfo() { assert(kind == ELF::ODK_REGINFO); return *reinterpret_cast *>( (uint8_t *)this + sizeof(Elf_Mips_Options)); } const Elf_Mips_RegInfo &getRegInfo() const { return const_cast(this)->getRegInfo(); } }; // .MIPS.abiflags section content template struct Elf_Mips_ABIFlags { LLVM_ELF_IMPORT_TYPES_ELFT(ELFT) Elf_Half version; // Version of the structure uint8_t isa_level; // ISA level: 1-5, 32, and 64 uint8_t isa_rev; // ISA revision (0 for MIPS I - MIPS V) uint8_t gpr_size; // General purpose registers size uint8_t cpr1_size; // Co-processor 1 registers size uint8_t cpr2_size; // Co-processor 2 registers size uint8_t fp_abi; // Floating-point ABI flag Elf_Word isa_ext; // Processor-specific extension Elf_Word ases; // ASEs flags Elf_Word flags1; // General flags Elf_Word flags2; // General flags }; // Struct representing the BBAddrMap for one function. struct BBAddrMap { // Struct representing the BBAddrMap information for one basic block. struct BBEntry { struct Metadata { bool HasReturn : 1; // If this block ends with a return (or tail // call). bool HasTailCall : 1; // If this block ends with a tail call. bool IsEHPad : 1; // If this is an exception handling block. bool CanFallThrough : 1; // If this block can fall through to its next. bool HasIndirectBranch : 1; // If this block ends with an indirect branch // (branch via a register). bool operator==(const Metadata &Other) const { return HasReturn == Other.HasReturn && HasTailCall == Other.HasTailCall && IsEHPad == Other.IsEHPad && CanFallThrough == Other.CanFallThrough && HasIndirectBranch == Other.HasIndirectBranch; } // Encodes this struct as a uint32_t value. uint32_t encode() const { return static_cast(HasReturn) | (static_cast(HasTailCall) << 1) | (static_cast(IsEHPad) << 2) | (static_cast(CanFallThrough) << 3) | (static_cast(HasIndirectBranch) << 4); } // Decodes and returns a Metadata struct from a uint32_t value. static Expected decode(uint32_t V) { Metadata MD{/*HasReturn=*/static_cast(V & 1), /*HasTailCall=*/static_cast(V & (1 << 1)), /*IsEHPad=*/static_cast(V & (1 << 2)), /*CanFallThrough=*/static_cast(V & (1 << 3)), /*HasIndirectBranch=*/static_cast(V & (1 << 4))}; if (MD.encode() != V) return createStringError( std::error_code(), "invalid encoding for BBEntry::Metadata: 0x%x", V); return MD; } }; uint32_t ID; // Unique ID of this basic block. uint32_t Offset; // Offset of basic block relative to function start. uint32_t Size; // Size of the basic block. Metadata MD; // Metdata for this basic block. BBEntry(uint32_t ID, uint32_t Offset, uint32_t Size, Metadata MD) : ID(ID), Offset(Offset), Size(Size), MD(MD){}; bool operator==(const BBEntry &Other) const { return ID == Other.ID && Offset == Other.Offset && Size == Other.Size && MD == Other.MD; } bool hasReturn() const { return MD.HasReturn; } bool hasTailCall() const { return MD.HasTailCall; } bool isEHPad() const { return MD.IsEHPad; } bool canFallThrough() const { return MD.CanFallThrough; } bool hasIndirectBranch() const { return MD.HasIndirectBranch; } }; BBAddrMap(uint64_t Addr, std::vector BBEntries) : Addr(Addr), BBEntries(std::move(BBEntries)) {} // Returns the address of the corresponding function. uint64_t getFunctionAddress() const { return Addr; } // Returns the basic block entries for this function. const std::vector &getBBEntries() const { return BBEntries; } // Equality operator for unit testing. bool operator==(const BBAddrMap &Other) const { return Addr == Other.Addr && std::equal(BBEntries.begin(), BBEntries.end(), Other.BBEntries.begin()); } uint64_t Addr; // Function address std::vector BBEntries; // Basic block entries for this function. }; /// A feature extension of BBAddrMap that holds information relevant to PGO. struct PGOAnalysisMap { /// Bitfield of optional features to include in the PGO extended map. struct Features { bool FuncEntryCount : 1; bool BBFreq : 1; bool BrProb : 1; // True if at least one feature is enabled bool anyEnabled() const { return FuncEntryCount || BBFreq || BrProb; } // Encodes to minimum bit width representation. uint8_t encode() const { return (static_cast(FuncEntryCount) << 0) | (static_cast(BBFreq) << 1) | (static_cast(BrProb) << 2); } // Decodes from minimum bit width representation and validates no // unnecessary bits are used. static Expected decode(uint8_t Val) { Features Feat{static_cast(Val & (1 << 0)), static_cast(Val & (1 << 1)), static_cast(Val & (1 << 2))}; if (Feat.encode() != Val) return createStringError( std::error_code(), "invalid encoding for PGOAnalysisMap::Features: 0x%x", Val); return Feat; } bool operator==(const Features &Other) const { return std::tie(FuncEntryCount, BBFreq, BrProb) == std::tie(Other.FuncEntryCount, Other.BBFreq, Other.BrProb); } }; /// Extra basic block data with fields for block frequency and branch /// probability. struct PGOBBEntry { /// Single successor of a given basic block that contains the tag and branch /// probability associated with it. struct SuccessorEntry { /// Unique ID of this successor basic block. uint32_t ID; /// Branch Probability of the edge to this successor taken from MBPI. BranchProbability Prob; bool operator==(const SuccessorEntry &Other) const { return std::tie(ID, Prob) == std::tie(Other.ID, Other.Prob); } }; /// Block frequency taken from MBFI BlockFrequency BlockFreq; /// List of successors of the current block llvm::SmallVector Successors; bool operator==(const PGOBBEntry &Other) const { return std::tie(BlockFreq, Successors) == std::tie(Other.BlockFreq, Other.Successors); } }; uint64_t FuncEntryCount; // Prof count from IR function std::vector BBEntries; // Extended basic block entries // Flags to indicate if each PGO related info was enabled in this function Features FeatEnable; bool operator==(const PGOAnalysisMap &Other) const { return std::tie(FuncEntryCount, BBEntries, FeatEnable) == std::tie(Other.FuncEntryCount, Other.BBEntries, Other.FeatEnable); } }; } // end namespace object. } // end namespace llvm. #endif // LLVM_OBJECT_ELFTYPES_H