HELLO·Android
系统源代码
IT资讯
技术文章
我的收藏
注册
登录
-
我收藏的文章
创建代码块
我的代码块
我的账号
Android 10
|
10.0.0_r6
下载
查看原文件
收藏
根目录
external
llvm
tools
llvm-readobj
ELFDumper.cpp
//===-- ELFDumper.cpp - ELF-specific dumper ---------------------*- C++ -*-===// // // The LLVM Compiler Infrastructure // // This file is distributed under the University of Illinois Open Source // License. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// /// /// \file /// \brief This file implements the ELF-specific dumper for llvm-readobj. /// //===----------------------------------------------------------------------===// #include "ARMAttributeParser.h" #include "ARMEHABIPrinter.h" #include "Error.h" #include "ObjDumper.h" #include "StackMapPrinter.h" #include "llvm-readobj.h" #include "llvm/ADT/Optional.h" #include "llvm/ADT/SmallString.h" #include "llvm/ADT/StringExtras.h" #include "llvm/Object/ELFObjectFile.h" #include "llvm/Support/ARMBuildAttributes.h" #include "llvm/Support/Compiler.h" #include "llvm/Support/Format.h" #include "llvm/Support/FormattedStream.h" #include "llvm/Support/MathExtras.h" #include "llvm/Support/MipsABIFlags.h" #include "llvm/Support/ScopedPrinter.h" #include "llvm/Support/raw_ostream.h" using namespace llvm; using namespace llvm::object; using namespace ELF; #define LLVM_READOBJ_ENUM_CASE(ns, enum) \ case ns::enum: return #enum; #define ENUM_ENT(enum, altName) \ { #enum, altName, ELF::enum } #define ENUM_ENT_1(enum) \ { #enum, #enum, ELF::enum } #define LLVM_READOBJ_PHDR_ENUM(ns, enum) \ case ns::enum: \ return std::string(#enum).substr(3); #define TYPEDEF_ELF_TYPES(ELFT) \ typedef ELFFile
ELFO; \ typedef typename ELFO::Elf_Shdr Elf_Shdr; \ typedef typename ELFO::Elf_Sym Elf_Sym; \ typedef typename ELFO::Elf_Dyn Elf_Dyn; \ typedef typename ELFO::Elf_Dyn_Range Elf_Dyn_Range; \ typedef typename ELFO::Elf_Rel Elf_Rel; \ typedef typename ELFO::Elf_Rela Elf_Rela; \ typedef typename ELFO::Elf_Rela_Range Elf_Rela_Range; \ typedef typename ELFO::Elf_Phdr Elf_Phdr; \ typedef typename ELFO::Elf_Half Elf_Half; \ typedef typename ELFO::Elf_Ehdr Elf_Ehdr; \ typedef typename ELFO::Elf_Word Elf_Word; \ typedef typename ELFO::Elf_Hash Elf_Hash; \ typedef typename ELFO::Elf_GnuHash Elf_GnuHash; \ typedef typename ELFO::uintX_t uintX_t; namespace { template
class DumpStyle; /// Represents a contiguous uniform range in the file. We cannot just create a /// range directly because when creating one of these from the .dynamic table /// the size, entity size and virtual address are different entries in arbitrary /// order (DT_REL, DT_RELSZ, DT_RELENT for example). struct DynRegionInfo { DynRegionInfo() : Addr(nullptr), Size(0), EntSize(0) {} DynRegionInfo(const void *A, uint64_t S, uint64_t ES) : Addr(A), Size(S), EntSize(ES) {} /// \brief Address in current address space. const void *Addr; /// \brief Size in bytes of the region. uint64_t Size; /// \brief Size of each entity in the region. uint64_t EntSize; template
ArrayRef
getAsArrayRef() const { const Type *Start = reinterpret_cast
(Addr); if (!Start) return {Start, Start}; if (EntSize != sizeof(Type) || Size % EntSize) reportError("Invalid entity size"); return {Start, Start + (Size / EntSize)}; } }; template
class ELFDumper : public ObjDumper { public: ELFDumper(const ELFFile
*Obj, ScopedPrinter &Writer); void printFileHeaders() override; void printSections() override; void printRelocations() override; void printDynamicRelocations() override; void printSymbols() override; void printDynamicSymbols() override; void printUnwindInfo() override; void printDynamicTable() override; void printNeededLibraries() override; void printProgramHeaders() override; void printHashTable() override; void printGnuHashTable() override; void printLoadName() override; void printVersionInfo() override; void printGroupSections() override; void printAttributes() override; void printMipsPLTGOT() override; void printMipsABIFlags() override; void printMipsReginfo() override; void printMipsOptions() override; void printStackMap() const override; void printHashHistogram() override; private: std::unique_ptr
> ELFDumperStyle; typedef ELFFile
ELFO; typedef typename ELFO::Elf_Shdr Elf_Shdr; typedef typename ELFO::Elf_Sym Elf_Sym; typedef typename ELFO::Elf_Sym_Range Elf_Sym_Range; typedef typename ELFO::Elf_Dyn Elf_Dyn; typedef typename ELFO::Elf_Dyn_Range Elf_Dyn_Range; typedef typename ELFO::Elf_Rel Elf_Rel; typedef typename ELFO::Elf_Rela Elf_Rela; typedef typename ELFO::Elf_Rel_Range Elf_Rel_Range; typedef typename ELFO::Elf_Rela_Range Elf_Rela_Range; typedef typename ELFO::Elf_Phdr Elf_Phdr; typedef typename ELFO::Elf_Half Elf_Half; typedef typename ELFO::Elf_Hash Elf_Hash; typedef typename ELFO::Elf_GnuHash Elf_GnuHash; typedef typename ELFO::Elf_Ehdr Elf_Ehdr; typedef typename ELFO::Elf_Word Elf_Word; typedef typename ELFO::uintX_t uintX_t; typedef typename ELFO::Elf_Versym Elf_Versym; typedef typename ELFO::Elf_Verneed Elf_Verneed; typedef typename ELFO::Elf_Vernaux Elf_Vernaux; typedef typename ELFO::Elf_Verdef Elf_Verdef; typedef typename ELFO::Elf_Verdaux Elf_Verdaux; DynRegionInfo checkDRI(DynRegionInfo DRI) { if (DRI.Addr < Obj->base() || (const uint8_t *)DRI.Addr + DRI.Size > Obj->base() + Obj->getBufSize()) error(llvm::object::object_error::parse_failed); return DRI; } DynRegionInfo createDRIFrom(const Elf_Phdr *P, uintX_t EntSize) { return checkDRI({Obj->base() + P->p_offset, P->p_filesz, EntSize}); } DynRegionInfo createDRIFrom(const Elf_Shdr *S) { return checkDRI({Obj->base() + S->sh_offset, S->sh_size, S->sh_entsize}); } void parseDynamicTable(ArrayRef
LoadSegments); void printValue(uint64_t Type, uint64_t Value); StringRef getDynamicString(uint64_t Offset) const; StringRef getSymbolVersion(StringRef StrTab, const Elf_Sym *symb, bool &IsDefault) const; void LoadVersionMap() const; void LoadVersionNeeds(const Elf_Shdr *ec) const; void LoadVersionDefs(const Elf_Shdr *sec) const; const ELFO *Obj; DynRegionInfo DynRelRegion; DynRegionInfo DynRelaRegion; DynRegionInfo DynPLTRelRegion; DynRegionInfo DynSymRegion; DynRegionInfo DynamicTable; StringRef DynamicStringTable; StringRef SOName; const Elf_Hash *HashTable = nullptr; const Elf_GnuHash *GnuHashTable = nullptr; const Elf_Shdr *DotSymtabSec = nullptr; StringRef DynSymtabName; ArrayRef
ShndxTable; const Elf_Shdr *dot_gnu_version_sec = nullptr; // .gnu.version const Elf_Shdr *dot_gnu_version_r_sec = nullptr; // .gnu.version_r const Elf_Shdr *dot_gnu_version_d_sec = nullptr; // .gnu.version_d // Records for each version index the corresponding Verdef or Vernaux entry. // This is filled the first time LoadVersionMap() is called. class VersionMapEntry : public PointerIntPair
{ public: // If the integer is 0, this is an Elf_Verdef*. // If the integer is 1, this is an Elf_Vernaux*. VersionMapEntry() : PointerIntPair
(nullptr, 0) {} VersionMapEntry(const Elf_Verdef *verdef) : PointerIntPair
(verdef, 0) {} VersionMapEntry(const Elf_Vernaux *vernaux) : PointerIntPair
(vernaux, 1) {} bool isNull() const { return getPointer() == nullptr; } bool isVerdef() const { return !isNull() && getInt() == 0; } bool isVernaux() const { return !isNull() && getInt() == 1; } const Elf_Verdef *getVerdef() const { return isVerdef() ? (const Elf_Verdef *)getPointer() : nullptr; } const Elf_Vernaux *getVernaux() const { return isVernaux() ? (const Elf_Vernaux *)getPointer() : nullptr; } }; mutable SmallVector
VersionMap; public: Elf_Dyn_Range dynamic_table() const { return DynamicTable.getAsArrayRef
(); } Elf_Sym_Range dynamic_symbols() const { return DynSymRegion.getAsArrayRef
(); } Elf_Rel_Range dyn_rels() const; Elf_Rela_Range dyn_relas() const; std::string getFullSymbolName(const Elf_Sym *Symbol, StringRef StrTable, bool IsDynamic) const; void printSymbolsHelper(bool IsDynamic) const; const Elf_Shdr *getDotSymtabSec() const { return DotSymtabSec; } ArrayRef
getShndxTable() const { return ShndxTable; } StringRef getDynamicStringTable() const { return DynamicStringTable; } const DynRegionInfo &getDynRelRegion() const { return DynRelRegion; } const DynRegionInfo &getDynRelaRegion() const { return DynRelaRegion; } const DynRegionInfo &getDynPLTRelRegion() const { return DynPLTRelRegion; } const Elf_Hash *getHashTable() const { return HashTable; } const Elf_GnuHash *getGnuHashTable() const { return GnuHashTable; } }; template
void ELFDumper
::printSymbolsHelper(bool IsDynamic) const { StringRef StrTable, SymtabName; size_t Entries = 0; Elf_Sym_Range Syms(nullptr, nullptr); if (IsDynamic) { StrTable = DynamicStringTable; Syms = dynamic_symbols(); SymtabName = DynSymtabName; if (DynSymRegion.Addr) Entries = DynSymRegion.Size / DynSymRegion.EntSize; } else { if (!DotSymtabSec) return; StrTable = unwrapOrError(Obj->getStringTableForSymtab(*DotSymtabSec)); Syms = Obj->symbols(DotSymtabSec); SymtabName = unwrapOrError(Obj->getSectionName(DotSymtabSec)); Entries = DotSymtabSec->getEntityCount(); } if (Syms.begin() == Syms.end()) return; ELFDumperStyle->printSymtabMessage(Obj, SymtabName, Entries); for (const auto &Sym : Syms) ELFDumperStyle->printSymbol(Obj, &Sym, Syms.begin(), StrTable, IsDynamic); } template
class DumpStyle { public: using Elf_Shdr = typename ELFFile
::Elf_Shdr; using Elf_Sym = typename ELFFile
::Elf_Sym; DumpStyle(ELFDumper
*Dumper) : Dumper(Dumper) {} virtual ~DumpStyle() {} virtual void printFileHeaders(const ELFFile
*Obj) = 0; virtual void printGroupSections(const ELFFile
*Obj) = 0; virtual void printRelocations(const ELFFile
*Obj) = 0; virtual void printSections(const ELFFile
*Obj) = 0; virtual void printSymbols(const ELFFile
*Obj) = 0; virtual void printDynamicSymbols(const ELFFile
*Obj) = 0; virtual void printDynamicRelocations(const ELFFile
*Obj) = 0; virtual void printSymtabMessage(const ELFFile
*obj, StringRef Name, size_t Offset) { return; } virtual void printSymbol(const ELFFile
*Obj, const Elf_Sym *Symbol, const Elf_Sym *FirstSym, StringRef StrTable, bool IsDynamic) = 0; virtual void printProgramHeaders(const ELFFile
*Obj) = 0; virtual void printHashHistogram(const ELFFile
*Obj) = 0; const ELFDumper
*dumper() const { return Dumper; } private: const ELFDumper
*Dumper; }; template
class GNUStyle : public DumpStyle
{ formatted_raw_ostream OS; public: TYPEDEF_ELF_TYPES(ELFT) GNUStyle(ScopedPrinter &W, ELFDumper
*Dumper) : DumpStyle
(Dumper), OS(W.getOStream()) {} void printFileHeaders(const ELFO *Obj) override; void printGroupSections(const ELFFile
*Obj) override; void printRelocations(const ELFO *Obj) override; void printSections(const ELFO *Obj) override; void printSymbols(const ELFO *Obj) override; void printDynamicSymbols(const ELFO *Obj) override; void printDynamicRelocations(const ELFO *Obj) override; virtual void printSymtabMessage(const ELFO *Obj, StringRef Name, size_t Offset) override; void printProgramHeaders(const ELFO *Obj) override; void printHashHistogram(const ELFFile
*Obj) override; private: struct Field { StringRef Str; unsigned Column; Field(StringRef S, unsigned Col) : Str(S), Column(Col) {} Field(unsigned Col) : Str(""), Column(Col) {} }; template
std::string printEnum(T Value, ArrayRef
> EnumValues) { for (const auto &EnumItem : EnumValues) if (EnumItem.Value == Value) return EnumItem.AltName; return to_hexString(Value, false); } formatted_raw_ostream &printField(struct Field F) { if (F.Column != 0) OS.PadToColumn(F.Column); OS << F.Str; OS.flush(); return OS; } void printRelocation(const ELFO *Obj, const Elf_Shdr *SymTab, const Elf_Rela &R, bool IsRela); void printSymbol(const ELFO *Obj, const Elf_Sym *Symbol, const Elf_Sym *First, StringRef StrTable, bool IsDynamic) override; std::string getSymbolSectionNdx(const ELFO *Obj, const Elf_Sym *Symbol, const Elf_Sym *FirstSym); void printDynamicRelocation(const ELFO *Obj, Elf_Rela R, bool IsRela); bool checkTLSSections(const Elf_Phdr &Phdr, const Elf_Shdr &Sec); bool checkoffsets(const Elf_Phdr &Phdr, const Elf_Shdr &Sec); bool checkVMA(const Elf_Phdr &Phdr, const Elf_Shdr &Sec); bool checkPTDynamic(const Elf_Phdr &Phdr, const Elf_Shdr &Sec); }; template
class LLVMStyle : public DumpStyle
{ public: TYPEDEF_ELF_TYPES(ELFT) LLVMStyle(ScopedPrinter &W, ELFDumper
*Dumper) : DumpStyle
(Dumper), W(W) {} void printFileHeaders(const ELFO *Obj) override; void printGroupSections(const ELFFile
*Obj) override; void printRelocations(const ELFO *Obj) override; void printRelocations(const Elf_Shdr *Sec, const ELFO *Obj); void printSections(const ELFO *Obj) override; void printSymbols(const ELFO *Obj) override; void printDynamicSymbols(const ELFO *Obj) override; void printDynamicRelocations(const ELFO *Obj) override; void printProgramHeaders(const ELFO *Obj) override; void printHashHistogram(const ELFFile
*Obj) override; private: void printRelocation(const ELFO *Obj, Elf_Rela Rel, const Elf_Shdr *SymTab); void printDynamicRelocation(const ELFO *Obj, Elf_Rela Rel); void printSymbol(const ELFO *Obj, const Elf_Sym *Symbol, const Elf_Sym *First, StringRef StrTable, bool IsDynamic) override; ScopedPrinter &W; }; } // namespace namespace llvm { template
static std::error_code createELFDumper(const ELFFile
*Obj, ScopedPrinter &Writer, std::unique_ptr
&Result) { Result.reset(new ELFDumper
(Obj, Writer)); return readobj_error::success; } std::error_code createELFDumper(const object::ObjectFile *Obj, ScopedPrinter &Writer, std::unique_ptr
&Result) { // Little-endian 32-bit if (const ELF32LEObjectFile *ELFObj = dyn_cast
(Obj)) return createELFDumper(ELFObj->getELFFile(), Writer, Result); // Big-endian 32-bit if (const ELF32BEObjectFile *ELFObj = dyn_cast
(Obj)) return createELFDumper(ELFObj->getELFFile(), Writer, Result); // Little-endian 64-bit if (const ELF64LEObjectFile *ELFObj = dyn_cast
(Obj)) return createELFDumper(ELFObj->getELFFile(), Writer, Result); // Big-endian 64-bit if (const ELF64BEObjectFile *ELFObj = dyn_cast
(Obj)) return createELFDumper(ELFObj->getELFFile(), Writer, Result); return readobj_error::unsupported_obj_file_format; } } // namespace llvm // Iterate through the versions needed section, and place each Elf_Vernaux // in the VersionMap according to its index. template
void ELFDumper
::LoadVersionNeeds(const Elf_Shdr *sec) const { unsigned vn_size = sec->sh_size; // Size of section in bytes unsigned vn_count = sec->sh_info; // Number of Verneed entries const char *sec_start = (const char *)Obj->base() + sec->sh_offset; const char *sec_end = sec_start + vn_size; // The first Verneed entry is at the start of the section. const char *p = sec_start; for (unsigned i = 0; i < vn_count; i++) { if (p + sizeof(Elf_Verneed) > sec_end) report_fatal_error("Section ended unexpectedly while scanning " "version needed records."); const Elf_Verneed *vn = reinterpret_cast
(p); if (vn->vn_version != ELF::VER_NEED_CURRENT) report_fatal_error("Unexpected verneed version"); // Iterate through the Vernaux entries const char *paux = p + vn->vn_aux; for (unsigned j = 0; j < vn->vn_cnt; j++) { if (paux + sizeof(Elf_Vernaux) > sec_end) report_fatal_error("Section ended unexpected while scanning auxiliary " "version needed records."); const Elf_Vernaux *vna = reinterpret_cast
(paux); size_t index = vna->vna_other & ELF::VERSYM_VERSION; if (index >= VersionMap.size()) VersionMap.resize(index + 1); VersionMap[index] = VersionMapEntry(vna); paux += vna->vna_next; } p += vn->vn_next; } } // Iterate through the version definitions, and place each Elf_Verdef // in the VersionMap according to its index. template
void ELFDumper
::LoadVersionDefs(const Elf_Shdr *sec) const { unsigned vd_size = sec->sh_size; // Size of section in bytes unsigned vd_count = sec->sh_info; // Number of Verdef entries const char *sec_start = (const char *)Obj->base() + sec->sh_offset; const char *sec_end = sec_start + vd_size; // The first Verdef entry is at the start of the section. const char *p = sec_start; for (unsigned i = 0; i < vd_count; i++) { if (p + sizeof(Elf_Verdef) > sec_end) report_fatal_error("Section ended unexpectedly while scanning " "version definitions."); const Elf_Verdef *vd = reinterpret_cast
(p); if (vd->vd_version != ELF::VER_DEF_CURRENT) report_fatal_error("Unexpected verdef version"); size_t index = vd->vd_ndx & ELF::VERSYM_VERSION; if (index >= VersionMap.size()) VersionMap.resize(index + 1); VersionMap[index] = VersionMapEntry(vd); p += vd->vd_next; } } template
void ELFDumper
::LoadVersionMap() const { // If there is no dynamic symtab or version table, there is nothing to do. if (!DynSymRegion.Addr || !dot_gnu_version_sec) return; // Has the VersionMap already been loaded? if (VersionMap.size() > 0) return; // The first two version indexes are reserved. // Index 0 is LOCAL, index 1 is GLOBAL. VersionMap.push_back(VersionMapEntry()); VersionMap.push_back(VersionMapEntry()); if (dot_gnu_version_d_sec) LoadVersionDefs(dot_gnu_version_d_sec); if (dot_gnu_version_r_sec) LoadVersionNeeds(dot_gnu_version_r_sec); } template
static void printVersionSymbolSection(ELFDumper
*Dumper, const ELFO *Obj, const typename ELFO::Elf_Shdr *Sec, ScopedPrinter &W) { DictScope SS(W, "Version symbols"); if (!Sec) return; StringRef Name = unwrapOrError(Obj->getSectionName(Sec)); W.printNumber("Section Name", Name, Sec->sh_name); W.printHex("Address", Sec->sh_addr); W.printHex("Offset", Sec->sh_offset); W.printNumber("Link", Sec->sh_link); const uint8_t *P = (const uint8_t *)Obj->base() + Sec->sh_offset; StringRef StrTable = Dumper->getDynamicStringTable(); // Same number of entries in the dynamic symbol table (DT_SYMTAB). ListScope Syms(W, "Symbols"); for (const typename ELFO::Elf_Sym &Sym : Dumper->dynamic_symbols()) { DictScope S(W, "Symbol"); std::string FullSymbolName = Dumper->getFullSymbolName(&Sym, StrTable, true /* IsDynamic */); W.printNumber("Version", *P); W.printString("Name", FullSymbolName); P += sizeof(typename ELFO::Elf_Half); } } static const EnumEntry
SymVersionFlags[] = { {"Base", "BASE", VER_FLG_BASE}, {"Weak", "WEAK", VER_FLG_WEAK}, {"Info", "INFO", VER_FLG_INFO}}; template
static void printVersionDefinitionSection(ELFDumper
*Dumper, const ELFO *Obj, const typename ELFO::Elf_Shdr *Sec, ScopedPrinter &W) { typedef typename ELFO::Elf_Verdef VerDef; typedef typename ELFO::Elf_Verdaux VerdAux; DictScope SD(W, "SHT_GNU_verdef"); if (!Sec) return; // The number of entries in the section SHT_GNU_verdef // is determined by DT_VERDEFNUM tag. unsigned VerDefsNum = 0; for (const typename ELFO::Elf_Dyn &Dyn : Dumper->dynamic_table()) { if (Dyn.d_tag == DT_VERDEFNUM) VerDefsNum = Dyn.d_un.d_val; } const uint8_t *SecStartAddress = (const uint8_t *)Obj->base() + Sec->sh_offset; const uint8_t *SecEndAddress = SecStartAddress + Sec->sh_size; const uint8_t *P = SecStartAddress; const typename ELFO::Elf_Shdr *StrTab = unwrapOrError(Obj->getSection(Sec->sh_link)); while (VerDefsNum--) { if (P + sizeof(VerDef) > SecEndAddress) report_fatal_error("invalid offset in the section"); auto *VD = reinterpret_cast
(P); DictScope Def(W, "Definition"); W.printNumber("Version", VD->vd_version); W.printEnum("Flags", VD->vd_flags, makeArrayRef(SymVersionFlags)); W.printNumber("Index", VD->vd_ndx); W.printNumber("Hash", VD->vd_hash); W.printString("Name", StringRef((const char *)(Obj->base() + StrTab->sh_offset + VD->getAux()->vda_name))); if (!VD->vd_cnt) report_fatal_error("at least one definition string must exist"); if (VD->vd_cnt > 2) report_fatal_error("more than one predecessor is not expected"); if (VD->vd_cnt == 2) { const uint8_t *PAux = P + VD->vd_aux + VD->getAux()->vda_next; const VerdAux *Aux = reinterpret_cast
(PAux); W.printString("Predecessor", StringRef((const char *)(Obj->base() + StrTab->sh_offset + Aux->vda_name))); } P += VD->vd_next; } } template
static void printVersionDependencySection(ELFDumper
*Dumper, const ELFO *Obj, const typename ELFO::Elf_Shdr *Sec, ScopedPrinter &W) { typedef typename ELFO::Elf_Verneed VerNeed; typedef typename ELFO::Elf_Vernaux VernAux; DictScope SD(W, "SHT_GNU_verneed"); if (!Sec) return; unsigned VerNeedNum = 0; for (const typename ELFO::Elf_Dyn &Dyn : Dumper->dynamic_table()) if (Dyn.d_tag == DT_VERNEEDNUM) VerNeedNum = Dyn.d_un.d_val; const uint8_t *SecData = (const uint8_t *)Obj->base() + Sec->sh_offset; const typename ELFO::Elf_Shdr *StrTab = unwrapOrError(Obj->getSection(Sec->sh_link)); const uint8_t *P = SecData; for (unsigned I = 0; I < VerNeedNum; ++I) { const VerNeed *Need = reinterpret_cast
(P); DictScope Entry(W, "Dependency"); W.printNumber("Version", Need->vn_version); W.printNumber("Count", Need->vn_cnt); W.printString("FileName", StringRef((const char *)(Obj->base() + StrTab->sh_offset + Need->vn_file))); const uint8_t *PAux = P + Need->vn_aux; for (unsigned J = 0; J < Need->vn_cnt; ++J) { const VernAux *Aux = reinterpret_cast
(PAux); DictScope Entry(W, "Entry"); W.printNumber("Hash", Aux->vna_hash); W.printEnum("Flags", Aux->vna_flags, makeArrayRef(SymVersionFlags)); W.printNumber("Index", Aux->vna_other); W.printString("Name", StringRef((const char *)(Obj->base() + StrTab->sh_offset + Aux->vna_name))); PAux += Aux->vna_next; } P += Need->vn_next; } } template
void ELFDumper
::printVersionInfo() { // Dump version symbol section. printVersionSymbolSection(this, Obj, dot_gnu_version_sec, W); // Dump version definition section. printVersionDefinitionSection(this, Obj, dot_gnu_version_d_sec, W); // Dump version dependency section. printVersionDependencySection(this, Obj, dot_gnu_version_r_sec, W); } template
StringRef ELFDumper
::getSymbolVersion(StringRef StrTab, const Elf_Sym *symb, bool &IsDefault) const { // This is a dynamic symbol. Look in the GNU symbol version table. if (!dot_gnu_version_sec) { // No version table. IsDefault = false; return StringRef(""); } // Determine the position in the symbol table of this entry. size_t entry_index = (reinterpret_cast
(symb) - reinterpret_cast
(DynSymRegion.Addr)) / sizeof(Elf_Sym); // Get the corresponding version index entry const Elf_Versym *vs = Obj->template getEntry
(dot_gnu_version_sec, entry_index); size_t version_index = vs->vs_index & ELF::VERSYM_VERSION; // Special markers for unversioned symbols. if (version_index == ELF::VER_NDX_LOCAL || version_index == ELF::VER_NDX_GLOBAL) { IsDefault = false; return StringRef(""); } // Lookup this symbol in the version table LoadVersionMap(); if (version_index >= VersionMap.size() || VersionMap[version_index].isNull()) reportError("Invalid version entry"); const VersionMapEntry &entry = VersionMap[version_index]; // Get the version name string size_t name_offset; if (entry.isVerdef()) { // The first Verdaux entry holds the name. name_offset = entry.getVerdef()->getAux()->vda_name; IsDefault = !(vs->vs_index & ELF::VERSYM_HIDDEN); } else { name_offset = entry.getVernaux()->vna_name; IsDefault = false; } if (name_offset >= StrTab.size()) reportError("Invalid string offset"); return StringRef(StrTab.data() + name_offset); } template
std::string ELFDumper
::getFullSymbolName(const Elf_Sym *Symbol, StringRef StrTable, bool IsDynamic) const { StringRef SymbolName = unwrapOrError(Symbol->getName(StrTable)); if (!IsDynamic) return SymbolName; std::string FullSymbolName(SymbolName); bool IsDefault; StringRef Version = getSymbolVersion(StrTable, &*Symbol, IsDefault); FullSymbolName += (IsDefault ? "@@" : "@"); FullSymbolName += Version; return FullSymbolName; } template
static void getSectionNameIndex(const ELFO &Obj, const typename ELFO::Elf_Sym *Symbol, const typename ELFO::Elf_Sym *FirstSym, ArrayRef
ShndxTable, StringRef &SectionName, unsigned &SectionIndex) { SectionIndex = Symbol->st_shndx; if (Symbol->isUndefined()) SectionName = "Undefined"; else if (Symbol->isProcessorSpecific()) SectionName = "Processor Specific"; else if (Symbol->isOSSpecific()) SectionName = "Operating System Specific"; else if (Symbol->isAbsolute()) SectionName = "Absolute"; else if (Symbol->isCommon()) SectionName = "Common"; else if (Symbol->isReserved() && SectionIndex != SHN_XINDEX) SectionName = "Reserved"; else { if (SectionIndex == SHN_XINDEX) SectionIndex = Obj.getExtendedSymbolTableIndex(Symbol, FirstSym, ShndxTable); const typename ELFO::Elf_Shdr *Sec = unwrapOrError(Obj.getSection(SectionIndex)); SectionName = unwrapOrError(Obj.getSectionName(Sec)); } } template
static const typename ELFO::Elf_Shdr * findNotEmptySectionByAddress(const ELFO *Obj, uint64_t Addr) { for (const auto &Shdr : Obj->sections()) if (Shdr.sh_addr == Addr && Shdr.sh_size > 0) return &Shdr; return nullptr; } template
static const typename ELFO::Elf_Shdr *findSectionByName(const ELFO &Obj, StringRef Name) { for (const auto &Shdr : Obj.sections()) { if (Name == unwrapOrError(Obj.getSectionName(&Shdr))) return &Shdr; } return nullptr; } static const EnumEntry
ElfClass[] = { {"None", "none", ELF::ELFCLASSNONE}, {"32-bit", "ELF32", ELF::ELFCLASS32}, {"64-bit", "ELF64", ELF::ELFCLASS64}, }; static const EnumEntry
ElfDataEncoding[] = { {"None", "none", ELF::ELFDATANONE}, {"LittleEndian", "2's complement, little endian", ELF::ELFDATA2LSB}, {"BigEndian", "2's complement, big endian", ELF::ELFDATA2MSB}, }; static const EnumEntry
ElfObjectFileType[] = { {"None", "NONE (none)", ELF::ET_NONE}, {"Relocatable", "REL (Relocatable file)", ELF::ET_REL}, {"Executable", "EXEC (Executable file)", ELF::ET_EXEC}, {"SharedObject", "DYN (Shared object file)", ELF::ET_DYN}, {"Core", "CORE (Core file)", ELF::ET_CORE}, }; static const EnumEntry
ElfOSABI[] = { {"SystemV", "UNIX - System V", ELF::ELFOSABI_NONE}, {"HPUX", "UNIX - HP-UX", ELF::ELFOSABI_HPUX}, {"NetBSD", "UNIX - NetBSD", ELF::ELFOSABI_NETBSD}, {"GNU/Linux", "UNIX - GNU", ELF::ELFOSABI_LINUX}, {"GNU/Hurd", "GNU/Hurd", ELF::ELFOSABI_HURD}, {"Solaris", "UNIX - Solaris", ELF::ELFOSABI_SOLARIS}, {"AIX", "UNIX - AIX", ELF::ELFOSABI_AIX}, {"IRIX", "UNIX - IRIX", ELF::ELFOSABI_IRIX}, {"FreeBSD", "UNIX - FreeBSD", ELF::ELFOSABI_FREEBSD}, {"TRU64", "UNIX - TRU64", ELF::ELFOSABI_TRU64}, {"Modesto", "Novell - Modesto", ELF::ELFOSABI_MODESTO}, {"OpenBSD", "UNIX - OpenBSD", ELF::ELFOSABI_OPENBSD}, {"OpenVMS", "VMS - OpenVMS", ELF::ELFOSABI_OPENVMS}, {"NSK", "HP - Non-Stop Kernel", ELF::ELFOSABI_NSK}, {"AROS", "AROS", ELF::ELFOSABI_AROS}, {"FenixOS", "FenixOS", ELF::ELFOSABI_FENIXOS}, {"CloudABI", "CloudABI", ELF::ELFOSABI_CLOUDABI}, {"C6000_ELFABI", "Bare-metal C6000", ELF::ELFOSABI_C6000_ELFABI}, {"C6000_LINUX", "Linux C6000", ELF::ELFOSABI_C6000_LINUX}, {"ARM", "ARM", ELF::ELFOSABI_ARM}, {"Standalone", "Standalone App", ELF::ELFOSABI_STANDALONE} }; static const EnumEntry
ElfMachineType[] = { ENUM_ENT(EM_NONE, "None"), ENUM_ENT(EM_M32, "WE32100"), ENUM_ENT(EM_SPARC, "Sparc"), ENUM_ENT(EM_386, "Intel 80386"), ENUM_ENT(EM_68K, "MC68000"), ENUM_ENT(EM_88K, "MC88000"), ENUM_ENT(EM_IAMCU, "EM_IAMCU"), ENUM_ENT(EM_860, "Intel 80860"), ENUM_ENT(EM_MIPS, "MIPS R3000"), ENUM_ENT(EM_S370, "IBM System/370"), ENUM_ENT(EM_MIPS_RS3_LE, "MIPS R3000 little-endian"), ENUM_ENT(EM_PARISC, "HPPA"), ENUM_ENT(EM_VPP500, "Fujitsu VPP500"), ENUM_ENT(EM_SPARC32PLUS, "Sparc v8+"), ENUM_ENT(EM_960, "Intel 80960"), ENUM_ENT(EM_PPC, "PowerPC"), ENUM_ENT(EM_PPC64, "PowerPC64"), ENUM_ENT(EM_S390, "IBM S/390"), ENUM_ENT(EM_SPU, "SPU"), ENUM_ENT(EM_V800, "NEC V800 series"), ENUM_ENT(EM_FR20, "Fujistsu FR20"), ENUM_ENT(EM_RH32, "TRW RH-32"), ENUM_ENT(EM_RCE, "Motorola RCE"), ENUM_ENT(EM_ARM, "ARM"), ENUM_ENT(EM_ALPHA, "EM_ALPHA"), ENUM_ENT(EM_SH, "Hitachi SH"), ENUM_ENT(EM_SPARCV9, "Sparc v9"), ENUM_ENT(EM_TRICORE, "Siemens Tricore"), ENUM_ENT(EM_ARC, "ARC"), ENUM_ENT(EM_H8_300, "Hitachi H8/300"), ENUM_ENT(EM_H8_300H, "Hitachi H8/300H"), ENUM_ENT(EM_H8S, "Hitachi H8S"), ENUM_ENT(EM_H8_500, "Hitachi H8/500"), ENUM_ENT(EM_IA_64, "Intel IA-64"), ENUM_ENT(EM_MIPS_X, "Stanford MIPS-X"), ENUM_ENT(EM_COLDFIRE, "Motorola Coldfire"), ENUM_ENT(EM_68HC12, "Motorola MC68HC12 Microcontroller"), ENUM_ENT(EM_MMA, "Fujitsu Multimedia Accelerator"), ENUM_ENT(EM_PCP, "Siemens PCP"), ENUM_ENT(EM_NCPU, "Sony nCPU embedded RISC processor"), ENUM_ENT(EM_NDR1, "Denso NDR1 microprocesspr"), ENUM_ENT(EM_STARCORE, "Motorola Star*Core processor"), ENUM_ENT(EM_ME16, "Toyota ME16 processor"), ENUM_ENT(EM_ST100, "STMicroelectronics ST100 processor"), ENUM_ENT(EM_TINYJ, "Advanced Logic Corp. TinyJ embedded processor"), ENUM_ENT(EM_X86_64, "Advanced Micro Devices X86-64"), ENUM_ENT(EM_PDSP, "Sony DSP processor"), ENUM_ENT(EM_PDP10, "Digital Equipment Corp. PDP-10"), ENUM_ENT(EM_PDP11, "Digital Equipment Corp. PDP-11"), ENUM_ENT(EM_FX66, "Siemens FX66 microcontroller"), ENUM_ENT(EM_ST9PLUS, "STMicroelectronics ST9+ 8/16 bit microcontroller"), ENUM_ENT(EM_ST7, "STMicroelectronics ST7 8-bit microcontroller"), ENUM_ENT(EM_68HC16, "Motorola MC68HC16 Microcontroller"), ENUM_ENT(EM_68HC11, "Motorola MC68HC11 Microcontroller"), ENUM_ENT(EM_68HC08, "Motorola MC68HC08 Microcontroller"), ENUM_ENT(EM_68HC05, "Motorola MC68HC05 Microcontroller"), ENUM_ENT(EM_SVX, "Silicon Graphics SVx"), ENUM_ENT(EM_ST19, "STMicroelectronics ST19 8-bit microcontroller"), ENUM_ENT(EM_VAX, "Digital VAX"), ENUM_ENT(EM_CRIS, "Axis Communications 32-bit embedded processor"), ENUM_ENT(EM_JAVELIN, "Infineon Technologies 32-bit embedded cpu"), ENUM_ENT(EM_FIREPATH, "Element 14 64-bit DSP processor"), ENUM_ENT(EM_ZSP, "LSI Logic's 16-bit DSP processor"), ENUM_ENT(EM_MMIX, "Donald Knuth's educational 64-bit processor"), ENUM_ENT(EM_HUANY, "Harvard Universitys's machine-independent object format"), ENUM_ENT(EM_PRISM, "Vitesse Prism"), ENUM_ENT(EM_AVR, "Atmel AVR 8-bit microcontroller"), ENUM_ENT(EM_FR30, "Fujitsu FR30"), ENUM_ENT(EM_D10V, "Mitsubishi D10V"), ENUM_ENT(EM_D30V, "Mitsubishi D30V"), ENUM_ENT(EM_V850, "NEC v850"), ENUM_ENT(EM_M32R, "Renesas M32R (formerly Mitsubishi M32r)"), ENUM_ENT(EM_MN10300, "Matsushita MN10300"), ENUM_ENT(EM_MN10200, "Matsushita MN10200"), ENUM_ENT(EM_PJ, "picoJava"), ENUM_ENT(EM_OPENRISC, "OpenRISC 32-bit embedded processor"), ENUM_ENT(EM_ARC_COMPACT, "EM_ARC_COMPACT"), ENUM_ENT(EM_XTENSA, "Tensilica Xtensa Processor"), ENUM_ENT(EM_VIDEOCORE, "Alphamosaic VideoCore processor"), ENUM_ENT(EM_TMM_GPP, "Thompson Multimedia General Purpose Processor"), ENUM_ENT(EM_NS32K, "National Semiconductor 32000 series"), ENUM_ENT(EM_TPC, "Tenor Network TPC processor"), ENUM_ENT(EM_SNP1K, "EM_SNP1K"), ENUM_ENT(EM_ST200, "STMicroelectronics ST200 microcontroller"), ENUM_ENT(EM_IP2K, "Ubicom IP2xxx 8-bit microcontrollers"), ENUM_ENT(EM_MAX, "MAX Processor"), ENUM_ENT(EM_CR, "National Semiconductor CompactRISC"), ENUM_ENT(EM_F2MC16, "Fujitsu F2MC16"), ENUM_ENT(EM_MSP430, "Texas Instruments msp430 microcontroller"), ENUM_ENT(EM_BLACKFIN, "Analog Devices Blackfin"), ENUM_ENT(EM_SE_C33, "S1C33 Family of Seiko Epson processors"), ENUM_ENT(EM_SEP, "Sharp embedded microprocessor"), ENUM_ENT(EM_ARCA, "Arca RISC microprocessor"), ENUM_ENT(EM_UNICORE, "Unicore"), ENUM_ENT(EM_EXCESS, "eXcess 16/32/64-bit configurable embedded CPU"), ENUM_ENT(EM_DXP, "Icera Semiconductor Inc. Deep Execution Processor"), ENUM_ENT(EM_ALTERA_NIOS2, "Altera Nios"), ENUM_ENT(EM_CRX, "National Semiconductor CRX microprocessor"), ENUM_ENT(EM_XGATE, "Motorola XGATE embedded processor"), ENUM_ENT(EM_C166, "Infineon Technologies xc16x"), ENUM_ENT(EM_M16C, "Renesas M16C"), ENUM_ENT(EM_DSPIC30F, "Microchip Technology dsPIC30F Digital Signal Controller"), ENUM_ENT(EM_CE, "Freescale Communication Engine RISC core"), ENUM_ENT(EM_M32C, "Renesas M32C"), ENUM_ENT(EM_TSK3000, "Altium TSK3000 core"), ENUM_ENT(EM_RS08, "Freescale RS08 embedded processor"), ENUM_ENT(EM_SHARC, "EM_SHARC"), ENUM_ENT(EM_ECOG2, "Cyan Technology eCOG2 microprocessor"), ENUM_ENT(EM_SCORE7, "SUNPLUS S+Core"), ENUM_ENT(EM_DSP24, "New Japan Radio (NJR) 24-bit DSP Processor"), ENUM_ENT(EM_VIDEOCORE3, "Broadcom VideoCore III processor"), ENUM_ENT(EM_LATTICEMICO32, "Lattice Mico32"), ENUM_ENT(EM_SE_C17, "Seiko Epson C17 family"), ENUM_ENT(EM_TI_C6000, "Texas Instruments TMS320C6000 DSP family"), ENUM_ENT(EM_TI_C2000, "Texas Instruments TMS320C2000 DSP family"), ENUM_ENT(EM_TI_C5500, "Texas Instruments TMS320C55x DSP family"), ENUM_ENT(EM_MMDSP_PLUS, "STMicroelectronics 64bit VLIW Data Signal Processor"), ENUM_ENT(EM_CYPRESS_M8C, "Cypress M8C microprocessor"), ENUM_ENT(EM_R32C, "Renesas R32C series microprocessors"), ENUM_ENT(EM_TRIMEDIA, "NXP Semiconductors TriMedia architecture family"), ENUM_ENT(EM_HEXAGON, "Qualcomm Hexagon"), ENUM_ENT(EM_8051, "Intel 8051 and variants"), ENUM_ENT(EM_STXP7X, "STMicroelectronics STxP7x family"), ENUM_ENT(EM_NDS32, "Andes Technology compact code size embedded RISC processor family"), ENUM_ENT(EM_ECOG1, "Cyan Technology eCOG1 microprocessor"), ENUM_ENT(EM_ECOG1X, "Cyan Technology eCOG1X family"), ENUM_ENT(EM_MAXQ30, "Dallas Semiconductor MAXQ30 Core microcontrollers"), ENUM_ENT(EM_XIMO16, "New Japan Radio (NJR) 16-bit DSP Processor"), ENUM_ENT(EM_MANIK, "M2000 Reconfigurable RISC Microprocessor"), ENUM_ENT(EM_CRAYNV2, "Cray Inc. NV2 vector architecture"), ENUM_ENT(EM_RX, "Renesas RX"), ENUM_ENT(EM_METAG, "Imagination Technologies Meta processor architecture"), ENUM_ENT(EM_MCST_ELBRUS, "MCST Elbrus general purpose hardware architecture"), ENUM_ENT(EM_ECOG16, "Cyan Technology eCOG16 family"), ENUM_ENT(EM_CR16, "Xilinx MicroBlaze"), ENUM_ENT(EM_ETPU, "Freescale Extended Time Processing Unit"), ENUM_ENT(EM_SLE9X, "Infineon Technologies SLE9X core"), ENUM_ENT(EM_L10M, "EM_L10M"), ENUM_ENT(EM_K10M, "EM_K10M"), ENUM_ENT(EM_AARCH64, "AArch64"), ENUM_ENT(EM_AVR32, "Atmel AVR 8-bit microcontroller"), ENUM_ENT(EM_STM8, "STMicroeletronics STM8 8-bit microcontroller"), ENUM_ENT(EM_TILE64, "Tilera TILE64 multicore architecture family"), ENUM_ENT(EM_TILEPRO, "Tilera TILEPro multicore architecture family"), ENUM_ENT(EM_CUDA, "NVIDIA CUDA architecture"), ENUM_ENT(EM_TILEGX, "Tilera TILE-Gx multicore architecture family"), ENUM_ENT(EM_CLOUDSHIELD, "EM_CLOUDSHIELD"), ENUM_ENT(EM_COREA_1ST, "EM_COREA_1ST"), ENUM_ENT(EM_COREA_2ND, "EM_COREA_2ND"), ENUM_ENT(EM_ARC_COMPACT2, "EM_ARC_COMPACT2"), ENUM_ENT(EM_OPEN8, "EM_OPEN8"), ENUM_ENT(EM_RL78, "Renesas RL78"), ENUM_ENT(EM_VIDEOCORE5, "Broadcom VideoCore V processor"), ENUM_ENT(EM_78KOR, "EM_78KOR"), ENUM_ENT(EM_56800EX, "EM_56800EX"), ENUM_ENT(EM_AMDGPU, "EM_AMDGPU"), ENUM_ENT(EM_WEBASSEMBLY, "EM_WEBASSEMBLY"), ENUM_ENT(EM_LANAI, "EM_LANAI"), }; static const EnumEntry
ElfSymbolBindings[] = { {"Local", "LOCAL", ELF::STB_LOCAL}, {"Global", "GLOBAL", ELF::STB_GLOBAL}, {"Weak", "WEAK", ELF::STB_WEAK}, {"Unique", "UNIQUE", ELF::STB_GNU_UNIQUE}}; static const EnumEntry
ElfSymbolVisibilities[] = { {"DEFAULT", "DEFAULT", ELF::STV_DEFAULT}, {"INTERNAL", "INTERNAL", ELF::STV_INTERNAL}, {"HIDDEN", "HIDDEN", ELF::STV_HIDDEN}, {"PROTECTED", "PROTECTED", ELF::STV_PROTECTED}}; static const EnumEntry
ElfSymbolTypes[] = { {"None", "NOTYPE", ELF::STT_NOTYPE}, {"Object", "OBJECT", ELF::STT_OBJECT}, {"Function", "FUNC", ELF::STT_FUNC}, {"Section", "SECTION", ELF::STT_SECTION}, {"File", "FILE", ELF::STT_FILE}, {"Common", "COMMON", ELF::STT_COMMON}, {"TLS", "TLS", ELF::STT_TLS}, {"GNU_IFunc", "IFUNC", ELF::STT_GNU_IFUNC}}; static const EnumEntry
AMDGPUSymbolTypes[] = { { "AMDGPU_HSA_KERNEL", ELF::STT_AMDGPU_HSA_KERNEL }, { "AMDGPU_HSA_INDIRECT_FUNCTION", ELF::STT_AMDGPU_HSA_INDIRECT_FUNCTION }, { "AMDGPU_HSA_METADATA", ELF::STT_AMDGPU_HSA_METADATA } }; static const char *getElfSectionType(unsigned Arch, unsigned Type) { switch (Arch) { case ELF::EM_ARM: switch (Type) { LLVM_READOBJ_ENUM_CASE(ELF, SHT_ARM_EXIDX); LLVM_READOBJ_ENUM_CASE(ELF, SHT_ARM_PREEMPTMAP); LLVM_READOBJ_ENUM_CASE(ELF, SHT_ARM_ATTRIBUTES); LLVM_READOBJ_ENUM_CASE(ELF, SHT_ARM_DEBUGOVERLAY); LLVM_READOBJ_ENUM_CASE(ELF, SHT_ARM_OVERLAYSECTION); } case ELF::EM_HEXAGON: switch (Type) { LLVM_READOBJ_ENUM_CASE(ELF, SHT_HEX_ORDERED); } case ELF::EM_X86_64: switch (Type) { LLVM_READOBJ_ENUM_CASE(ELF, SHT_X86_64_UNWIND); } case ELF::EM_MIPS: case ELF::EM_MIPS_RS3_LE: switch (Type) { LLVM_READOBJ_ENUM_CASE(ELF, SHT_MIPS_REGINFO); LLVM_READOBJ_ENUM_CASE(ELF, SHT_MIPS_OPTIONS); LLVM_READOBJ_ENUM_CASE(ELF, SHT_MIPS_ABIFLAGS); } } switch (Type) { LLVM_READOBJ_ENUM_CASE(ELF, SHT_NULL ); LLVM_READOBJ_ENUM_CASE(ELF, SHT_PROGBITS ); LLVM_READOBJ_ENUM_CASE(ELF, SHT_SYMTAB ); LLVM_READOBJ_ENUM_CASE(ELF, SHT_STRTAB ); LLVM_READOBJ_ENUM_CASE(ELF, SHT_RELA ); LLVM_READOBJ_ENUM_CASE(ELF, SHT_HASH ); LLVM_READOBJ_ENUM_CASE(ELF, SHT_DYNAMIC ); LLVM_READOBJ_ENUM_CASE(ELF, SHT_NOTE ); LLVM_READOBJ_ENUM_CASE(ELF, SHT_NOBITS ); LLVM_READOBJ_ENUM_CASE(ELF, SHT_REL ); LLVM_READOBJ_ENUM_CASE(ELF, SHT_SHLIB ); LLVM_READOBJ_ENUM_CASE(ELF, SHT_DYNSYM ); LLVM_READOBJ_ENUM_CASE(ELF, SHT_INIT_ARRAY ); LLVM_READOBJ_ENUM_CASE(ELF, SHT_FINI_ARRAY ); LLVM_READOBJ_ENUM_CASE(ELF, SHT_PREINIT_ARRAY ); LLVM_READOBJ_ENUM_CASE(ELF, SHT_GROUP ); LLVM_READOBJ_ENUM_CASE(ELF, SHT_SYMTAB_SHNDX ); LLVM_READOBJ_ENUM_CASE(ELF, SHT_GNU_ATTRIBUTES ); LLVM_READOBJ_ENUM_CASE(ELF, SHT_GNU_HASH ); LLVM_READOBJ_ENUM_CASE(ELF, SHT_GNU_verdef ); LLVM_READOBJ_ENUM_CASE(ELF, SHT_GNU_verneed ); LLVM_READOBJ_ENUM_CASE(ELF, SHT_GNU_versym ); default: return ""; } } static const char *getGroupType(uint32_t Flag) { if (Flag & ELF::GRP_COMDAT) return "COMDAT"; else return "(unknown)"; } static const EnumEntry
ElfSectionFlags[] = { ENUM_ENT(SHF_WRITE, "W"), ENUM_ENT(SHF_ALLOC, "A"), ENUM_ENT(SHF_EXCLUDE, "E"), ENUM_ENT(SHF_EXECINSTR, "X"), ENUM_ENT(SHF_MERGE, "M"), ENUM_ENT(SHF_STRINGS, "S"), ENUM_ENT(SHF_INFO_LINK, "I"), ENUM_ENT(SHF_LINK_ORDER, "L"), ENUM_ENT(SHF_OS_NONCONFORMING, "o"), ENUM_ENT(SHF_GROUP, "G"), ENUM_ENT(SHF_TLS, "T"), ENUM_ENT(SHF_MASKOS, "o"), ENUM_ENT(SHF_MASKPROC, "p"), ENUM_ENT_1(SHF_COMPRESSED), }; static const EnumEntry
ElfXCoreSectionFlags[] = { LLVM_READOBJ_ENUM_ENT(ELF, XCORE_SHF_CP_SECTION), LLVM_READOBJ_ENUM_ENT(ELF, XCORE_SHF_DP_SECTION) }; static const EnumEntry
ElfAMDGPUSectionFlags[] = { LLVM_READOBJ_ENUM_ENT(ELF, SHF_AMDGPU_HSA_GLOBAL), LLVM_READOBJ_ENUM_ENT(ELF, SHF_AMDGPU_HSA_READONLY), LLVM_READOBJ_ENUM_ENT(ELF, SHF_AMDGPU_HSA_CODE), LLVM_READOBJ_ENUM_ENT(ELF, SHF_AMDGPU_HSA_AGENT) }; static const EnumEntry
ElfHexagonSectionFlags[] = { LLVM_READOBJ_ENUM_ENT(ELF, SHF_HEX_GPREL) }; static const EnumEntry
ElfMipsSectionFlags[] = { LLVM_READOBJ_ENUM_ENT(ELF, SHF_MIPS_NODUPES), LLVM_READOBJ_ENUM_ENT(ELF, SHF_MIPS_NAMES ), LLVM_READOBJ_ENUM_ENT(ELF, SHF_MIPS_LOCAL ), LLVM_READOBJ_ENUM_ENT(ELF, SHF_MIPS_NOSTRIP), LLVM_READOBJ_ENUM_ENT(ELF, SHF_MIPS_GPREL ), LLVM_READOBJ_ENUM_ENT(ELF, SHF_MIPS_MERGE ), LLVM_READOBJ_ENUM_ENT(ELF, SHF_MIPS_ADDR ), LLVM_READOBJ_ENUM_ENT(ELF, SHF_MIPS_STRING ) }; static const EnumEntry
ElfX86_64SectionFlags[] = { LLVM_READOBJ_ENUM_ENT(ELF, SHF_X86_64_LARGE) }; static std::string getGNUFlags(uint64_t Flags) { std::string Str; for (auto Entry : ElfSectionFlags) { uint64_t Flag = Entry.Value & Flags; Flags &= ~Entry.Value; switch (Flag) { case ELF::SHF_WRITE: case ELF::SHF_ALLOC: case ELF::SHF_EXECINSTR: case ELF::SHF_MERGE: case ELF::SHF_STRINGS: case ELF::SHF_INFO_LINK: case ELF::SHF_LINK_ORDER: case ELF::SHF_OS_NONCONFORMING: case ELF::SHF_GROUP: case ELF::SHF_TLS: case ELF::SHF_EXCLUDE: Str += Entry.AltName; break; default: if (Flag & ELF::SHF_MASKOS) Str += "o"; else if (Flag & ELF::SHF_MASKPROC) Str += "p"; else if (Flag) Str += "x"; } } return Str; } static const char *getElfSegmentType(unsigned Arch, unsigned Type) { // Check potentially overlapped processor-specific // program header type. switch (Arch) { case ELF::EM_AMDGPU: switch (Type) { LLVM_READOBJ_ENUM_CASE(ELF, PT_AMDGPU_HSA_LOAD_GLOBAL_PROGRAM); LLVM_READOBJ_ENUM_CASE(ELF, PT_AMDGPU_HSA_LOAD_GLOBAL_AGENT); LLVM_READOBJ_ENUM_CASE(ELF, PT_AMDGPU_HSA_LOAD_READONLY_AGENT); LLVM_READOBJ_ENUM_CASE(ELF, PT_AMDGPU_HSA_LOAD_CODE_AGENT); } case ELF::EM_ARM: switch (Type) { LLVM_READOBJ_ENUM_CASE(ELF, PT_ARM_EXIDX); } case ELF::EM_MIPS: case ELF::EM_MIPS_RS3_LE: switch (Type) { LLVM_READOBJ_ENUM_CASE(ELF, PT_MIPS_REGINFO); LLVM_READOBJ_ENUM_CASE(ELF, PT_MIPS_RTPROC); LLVM_READOBJ_ENUM_CASE(ELF, PT_MIPS_OPTIONS); LLVM_READOBJ_ENUM_CASE(ELF, PT_MIPS_ABIFLAGS); } } switch (Type) { LLVM_READOBJ_ENUM_CASE(ELF, PT_NULL ); LLVM_READOBJ_ENUM_CASE(ELF, PT_LOAD ); LLVM_READOBJ_ENUM_CASE(ELF, PT_DYNAMIC); LLVM_READOBJ_ENUM_CASE(ELF, PT_INTERP ); LLVM_READOBJ_ENUM_CASE(ELF, PT_NOTE ); LLVM_READOBJ_ENUM_CASE(ELF, PT_SHLIB ); LLVM_READOBJ_ENUM_CASE(ELF, PT_PHDR ); LLVM_READOBJ_ENUM_CASE(ELF, PT_TLS ); LLVM_READOBJ_ENUM_CASE(ELF, PT_GNU_EH_FRAME); LLVM_READOBJ_ENUM_CASE(ELF, PT_SUNW_UNWIND); LLVM_READOBJ_ENUM_CASE(ELF, PT_GNU_STACK); LLVM_READOBJ_ENUM_CASE(ELF, PT_GNU_RELRO); default: return ""; } } static std::string getElfPtType(unsigned Arch, unsigned Type) { switch (Type) { LLVM_READOBJ_PHDR_ENUM(ELF, PT_NULL) LLVM_READOBJ_PHDR_ENUM(ELF, PT_LOAD) LLVM_READOBJ_PHDR_ENUM(ELF, PT_DYNAMIC) LLVM_READOBJ_PHDR_ENUM(ELF, PT_INTERP) LLVM_READOBJ_PHDR_ENUM(ELF, PT_NOTE) LLVM_READOBJ_PHDR_ENUM(ELF, PT_SHLIB) LLVM_READOBJ_PHDR_ENUM(ELF, PT_PHDR) LLVM_READOBJ_PHDR_ENUM(ELF, PT_TLS) LLVM_READOBJ_PHDR_ENUM(ELF, PT_GNU_EH_FRAME) LLVM_READOBJ_PHDR_ENUM(ELF, PT_SUNW_UNWIND) LLVM_READOBJ_PHDR_ENUM(ELF, PT_GNU_STACK) LLVM_READOBJ_PHDR_ENUM(ELF, PT_GNU_RELRO) default: // All machine specific PT_* types switch (Arch) { case ELF::EM_AMDGPU: switch (Type) { LLVM_READOBJ_ENUM_CASE(ELF, PT_AMDGPU_HSA_LOAD_GLOBAL_PROGRAM); LLVM_READOBJ_ENUM_CASE(ELF, PT_AMDGPU_HSA_LOAD_GLOBAL_AGENT); LLVM_READOBJ_ENUM_CASE(ELF, PT_AMDGPU_HSA_LOAD_READONLY_AGENT); LLVM_READOBJ_ENUM_CASE(ELF, PT_AMDGPU_HSA_LOAD_CODE_AGENT); } return ""; case ELF::EM_ARM: if (Type == ELF::PT_ARM_EXIDX) return "EXIDX"; return ""; case ELF::EM_MIPS: case ELF::EM_MIPS_RS3_LE: switch (Type) { case PT_MIPS_REGINFO: return "REGINFO"; case PT_MIPS_RTPROC: return "RTPROC"; case PT_MIPS_OPTIONS: return "OPTIONS"; case PT_MIPS_ABIFLAGS: return "ABIFLAGS"; } return ""; } } return std::string("
: ") + to_string(format_hex(Type, 1)); } static const EnumEntry
ElfSegmentFlags[] = { LLVM_READOBJ_ENUM_ENT(ELF, PF_X), LLVM_READOBJ_ENUM_ENT(ELF, PF_W), LLVM_READOBJ_ENUM_ENT(ELF, PF_R) }; static const EnumEntry
ElfHeaderMipsFlags[] = { LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_NOREORDER), LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_PIC), LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_CPIC), LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ABI2), LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_32BITMODE), LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_FP64), LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_NAN2008), LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ABI_O32), LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ABI_O64), LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ABI_EABI32), LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ABI_EABI64), LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_MACH_3900), LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_MACH_4010), LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_MACH_4100), LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_MACH_4650), LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_MACH_4120), LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_MACH_4111), LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_MACH_SB1), LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_MACH_OCTEON), LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_MACH_XLR), LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_MACH_OCTEON2), LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_MACH_OCTEON3), LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_MACH_5400), LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_MACH_5900), LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_MACH_5500), LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_MACH_9000), LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_MACH_LS2E), LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_MACH_LS2F), LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_MACH_LS3A), LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_MICROMIPS), LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ARCH_ASE_M16), LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ARCH_ASE_MDMX), LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ARCH_1), LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ARCH_2), LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ARCH_3), LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ARCH_4), LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ARCH_5), LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ARCH_32), LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ARCH_64), LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ARCH_32R2), LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ARCH_64R2), LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ARCH_32R6), LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ARCH_64R6) }; static const EnumEntry
ElfSymOtherFlags[] = { LLVM_READOBJ_ENUM_ENT(ELF, STV_INTERNAL), LLVM_READOBJ_ENUM_ENT(ELF, STV_HIDDEN), LLVM_READOBJ_ENUM_ENT(ELF, STV_PROTECTED) }; static const EnumEntry
ElfMipsSymOtherFlags[] = { LLVM_READOBJ_ENUM_ENT(ELF, STO_MIPS_OPTIONAL), LLVM_READOBJ_ENUM_ENT(ELF, STO_MIPS_PLT), LLVM_READOBJ_ENUM_ENT(ELF, STO_MIPS_PIC), LLVM_READOBJ_ENUM_ENT(ELF, STO_MIPS_MICROMIPS) }; static const EnumEntry
ElfMips16SymOtherFlags[] = { LLVM_READOBJ_ENUM_ENT(ELF, STO_MIPS_OPTIONAL), LLVM_READOBJ_ENUM_ENT(ELF, STO_MIPS_PLT), LLVM_READOBJ_ENUM_ENT(ELF, STO_MIPS_MIPS16) }; static const char *getElfMipsOptionsOdkType(unsigned Odk) { switch (Odk) { LLVM_READOBJ_ENUM_CASE(ELF, ODK_NULL); LLVM_READOBJ_ENUM_CASE(ELF, ODK_REGINFO); LLVM_READOBJ_ENUM_CASE(ELF, ODK_EXCEPTIONS); LLVM_READOBJ_ENUM_CASE(ELF, ODK_PAD); LLVM_READOBJ_ENUM_CASE(ELF, ODK_HWPATCH); LLVM_READOBJ_ENUM_CASE(ELF, ODK_FILL); LLVM_READOBJ_ENUM_CASE(ELF, ODK_TAGS); LLVM_READOBJ_ENUM_CASE(ELF, ODK_HWAND); LLVM_READOBJ_ENUM_CASE(ELF, ODK_HWOR); LLVM_READOBJ_ENUM_CASE(ELF, ODK_GP_GROUP); LLVM_READOBJ_ENUM_CASE(ELF, ODK_IDENT); LLVM_READOBJ_ENUM_CASE(ELF, ODK_PAGESIZE); default: return "Unknown"; } } template
ELFDumper
::ELFDumper(const ELFFile
*Obj, ScopedPrinter &Writer) : ObjDumper(Writer), Obj(Obj) { SmallVector
LoadSegments; for (const Elf_Phdr &Phdr : Obj->program_headers()) { if (Phdr.p_type == ELF::PT_DYNAMIC) { DynamicTable = createDRIFrom(&Phdr, sizeof(Elf_Dyn)); continue; } if (Phdr.p_type != ELF::PT_LOAD || Phdr.p_filesz == 0) continue; LoadSegments.push_back(&Phdr); } for (const Elf_Shdr &Sec : Obj->sections()) { switch (Sec.sh_type) { case ELF::SHT_SYMTAB: if (DotSymtabSec != nullptr) reportError("Multilpe SHT_SYMTAB"); DotSymtabSec = &Sec; break; case ELF::SHT_DYNSYM: if (DynSymRegion.Size) reportError("Multilpe SHT_DYNSYM"); DynSymRegion = createDRIFrom(&Sec); // This is only used (if Elf_Shdr present)for naming section in GNU style DynSymtabName = unwrapOrError(Obj->getSectionName(&Sec)); break; case ELF::SHT_SYMTAB_SHNDX: ShndxTable = unwrapOrError(Obj->getSHNDXTable(Sec)); break; case ELF::SHT_GNU_versym: if (dot_gnu_version_sec != nullptr) reportError("Multiple SHT_GNU_versym"); dot_gnu_version_sec = &Sec; break; case ELF::SHT_GNU_verdef: if (dot_gnu_version_d_sec != nullptr) reportError("Multiple SHT_GNU_verdef"); dot_gnu_version_d_sec = &Sec; break; case ELF::SHT_GNU_verneed: if (dot_gnu_version_r_sec != nullptr) reportError("Multilpe SHT_GNU_verneed"); dot_gnu_version_r_sec = &Sec; break; } } parseDynamicTable(LoadSegments); if (opts::Output == opts::GNU) ELFDumperStyle.reset(new GNUStyle
(Writer, this)); else ELFDumperStyle.reset(new LLVMStyle
(Writer, this)); } template
void ELFDumper
::parseDynamicTable( ArrayRef
LoadSegments) { auto toMappedAddr = [&](uint64_t VAddr) -> const uint8_t * { const Elf_Phdr *const *I = std::upper_bound( LoadSegments.begin(), LoadSegments.end(), VAddr, compareAddr
); if (I == LoadSegments.begin()) report_fatal_error("Virtual address is not in any segment"); --I; const Elf_Phdr &Phdr = **I; uint64_t Delta = VAddr - Phdr.p_vaddr; if (Delta >= Phdr.p_filesz) report_fatal_error("Virtual address is not in any segment"); return Obj->base() + Phdr.p_offset + Delta; }; uint64_t SONameOffset = 0; const char *StringTableBegin = nullptr; uint64_t StringTableSize = 0; for (const Elf_Dyn &Dyn : dynamic_table()) { switch (Dyn.d_tag) { case ELF::DT_HASH: HashTable = reinterpret_cast
(toMappedAddr(Dyn.getPtr())); break; case ELF::DT_GNU_HASH: GnuHashTable = reinterpret_cast
(toMappedAddr(Dyn.getPtr())); break; case ELF::DT_STRTAB: StringTableBegin = (const char *)toMappedAddr(Dyn.getPtr()); break; case ELF::DT_STRSZ: StringTableSize = Dyn.getVal(); break; case ELF::DT_SYMTAB: DynSymRegion.Addr = toMappedAddr(Dyn.getPtr()); DynSymRegion.EntSize = sizeof(Elf_Sym); break; case ELF::DT_RELA: DynRelaRegion.Addr = toMappedAddr(Dyn.getPtr()); break; case ELF::DT_RELASZ: DynRelaRegion.Size = Dyn.getVal(); break; case ELF::DT_RELAENT: DynRelaRegion.EntSize = Dyn.getVal(); break; case ELF::DT_SONAME: SONameOffset = Dyn.getVal(); break; case ELF::DT_REL: DynRelRegion.Addr = toMappedAddr(Dyn.getPtr()); break; case ELF::DT_RELSZ: DynRelRegion.Size = Dyn.getVal(); break; case ELF::DT_RELENT: DynRelRegion.EntSize = Dyn.getVal(); break; case ELF::DT_PLTREL: if (Dyn.getVal() == DT_REL) DynPLTRelRegion.EntSize = sizeof(Elf_Rel); else if (Dyn.getVal() == DT_RELA) DynPLTRelRegion.EntSize = sizeof(Elf_Rela); else reportError(Twine("unknown DT_PLTREL value of ") + Twine((uint64_t)Dyn.getVal())); break; case ELF::DT_JMPREL: DynPLTRelRegion.Addr = toMappedAddr(Dyn.getPtr()); break; case ELF::DT_PLTRELSZ: DynPLTRelRegion.Size = Dyn.getVal(); break; } } if (StringTableBegin) DynamicStringTable = StringRef(StringTableBegin, StringTableSize); if (SONameOffset) SOName = getDynamicString(SONameOffset); } template
typename ELFDumper
::Elf_Rel_Range ELFDumper
::dyn_rels() const { return DynRelRegion.getAsArrayRef
(); } template
typename ELFDumper
::Elf_Rela_Range ELFDumper
::dyn_relas() const { return DynRelaRegion.getAsArrayRef
(); } template
void ELFDumper
::printFileHeaders() { ELFDumperStyle->printFileHeaders(Obj); } template
void ELFDumper
::printSections() { ELFDumperStyle->printSections(Obj); } template
void ELFDumper
::printRelocations() { ELFDumperStyle->printRelocations(Obj); } template
void ELFDumper
::printProgramHeaders() { ELFDumperStyle->printProgramHeaders(Obj); } template
void ELFDumper
::printDynamicRelocations() { ELFDumperStyle->printDynamicRelocations(Obj); } template
void ELFDumper
::printSymbols() { ELFDumperStyle->printSymbols(Obj); } template
void ELFDumper
::printDynamicSymbols() { ELFDumperStyle->printDynamicSymbols(Obj); } template
void ELFDumper
::printHashHistogram() { ELFDumperStyle->printHashHistogram(Obj); } #define LLVM_READOBJ_TYPE_CASE(name) \ case DT_##name: return #name static const char *getTypeString(uint64_t Type) { switch (Type) { LLVM_READOBJ_TYPE_CASE(BIND_NOW); LLVM_READOBJ_TYPE_CASE(DEBUG); LLVM_READOBJ_TYPE_CASE(FINI); LLVM_READOBJ_TYPE_CASE(FINI_ARRAY); LLVM_READOBJ_TYPE_CASE(FINI_ARRAYSZ); LLVM_READOBJ_TYPE_CASE(FLAGS); LLVM_READOBJ_TYPE_CASE(FLAGS_1); LLVM_READOBJ_TYPE_CASE(HASH); LLVM_READOBJ_TYPE_CASE(INIT); LLVM_READOBJ_TYPE_CASE(INIT_ARRAY); LLVM_READOBJ_TYPE_CASE(INIT_ARRAYSZ); LLVM_READOBJ_TYPE_CASE(PREINIT_ARRAY); LLVM_READOBJ_TYPE_CASE(PREINIT_ARRAYSZ); LLVM_READOBJ_TYPE_CASE(JMPREL); LLVM_READOBJ_TYPE_CASE(NEEDED); LLVM_READOBJ_TYPE_CASE(NULL); LLVM_READOBJ_TYPE_CASE(PLTGOT); LLVM_READOBJ_TYPE_CASE(PLTREL); LLVM_READOBJ_TYPE_CASE(PLTRELSZ); LLVM_READOBJ_TYPE_CASE(REL); LLVM_READOBJ_TYPE_CASE(RELA); LLVM_READOBJ_TYPE_CASE(RELENT); LLVM_READOBJ_TYPE_CASE(RELSZ); LLVM_READOBJ_TYPE_CASE(RELAENT); LLVM_READOBJ_TYPE_CASE(RELASZ); LLVM_READOBJ_TYPE_CASE(RPATH); LLVM_READOBJ_TYPE_CASE(RUNPATH); LLVM_READOBJ_TYPE_CASE(SONAME); LLVM_READOBJ_TYPE_CASE(STRSZ); LLVM_READOBJ_TYPE_CASE(STRTAB); LLVM_READOBJ_TYPE_CASE(SYMBOLIC); LLVM_READOBJ_TYPE_CASE(SYMENT); LLVM_READOBJ_TYPE_CASE(SYMTAB); LLVM_READOBJ_TYPE_CASE(TEXTREL); LLVM_READOBJ_TYPE_CASE(VERDEF); LLVM_READOBJ_TYPE_CASE(VERDEFNUM); LLVM_READOBJ_TYPE_CASE(VERNEED); LLVM_READOBJ_TYPE_CASE(VERNEEDNUM); LLVM_READOBJ_TYPE_CASE(VERSYM); LLVM_READOBJ_TYPE_CASE(RELACOUNT); LLVM_READOBJ_TYPE_CASE(RELCOUNT); LLVM_READOBJ_TYPE_CASE(GNU_HASH); LLVM_READOBJ_TYPE_CASE(TLSDESC_PLT); LLVM_READOBJ_TYPE_CASE(TLSDESC_GOT); LLVM_READOBJ_TYPE_CASE(MIPS_RLD_VERSION); LLVM_READOBJ_TYPE_CASE(MIPS_RLD_MAP_REL); LLVM_READOBJ_TYPE_CASE(MIPS_FLAGS); LLVM_READOBJ_TYPE_CASE(MIPS_BASE_ADDRESS); LLVM_READOBJ_TYPE_CASE(MIPS_LOCAL_GOTNO); LLVM_READOBJ_TYPE_CASE(MIPS_SYMTABNO); LLVM_READOBJ_TYPE_CASE(MIPS_UNREFEXTNO); LLVM_READOBJ_TYPE_CASE(MIPS_GOTSYM); LLVM_READOBJ_TYPE_CASE(MIPS_RLD_MAP); LLVM_READOBJ_TYPE_CASE(MIPS_PLTGOT); LLVM_READOBJ_TYPE_CASE(MIPS_OPTIONS); default: return "unknown"; } } #undef LLVM_READOBJ_TYPE_CASE #define LLVM_READOBJ_DT_FLAG_ENT(prefix, enum) \ { #enum, prefix##_##enum } static const EnumEntry
ElfDynamicDTFlags[] = { LLVM_READOBJ_DT_FLAG_ENT(DF, ORIGIN), LLVM_READOBJ_DT_FLAG_ENT(DF, SYMBOLIC), LLVM_READOBJ_DT_FLAG_ENT(DF, TEXTREL), LLVM_READOBJ_DT_FLAG_ENT(DF, BIND_NOW), LLVM_READOBJ_DT_FLAG_ENT(DF, STATIC_TLS) }; static const EnumEntry
ElfDynamicDTFlags1[] = { LLVM_READOBJ_DT_FLAG_ENT(DF_1, NOW), LLVM_READOBJ_DT_FLAG_ENT(DF_1, GLOBAL), LLVM_READOBJ_DT_FLAG_ENT(DF_1, GROUP), LLVM_READOBJ_DT_FLAG_ENT(DF_1, NODELETE), LLVM_READOBJ_DT_FLAG_ENT(DF_1, LOADFLTR), LLVM_READOBJ_DT_FLAG_ENT(DF_1, INITFIRST), LLVM_READOBJ_DT_FLAG_ENT(DF_1, NOOPEN), LLVM_READOBJ_DT_FLAG_ENT(DF_1, ORIGIN), LLVM_READOBJ_DT_FLAG_ENT(DF_1, DIRECT), LLVM_READOBJ_DT_FLAG_ENT(DF_1, TRANS), LLVM_READOBJ_DT_FLAG_ENT(DF_1, INTERPOSE), LLVM_READOBJ_DT_FLAG_ENT(DF_1, NODEFLIB), LLVM_READOBJ_DT_FLAG_ENT(DF_1, NODUMP), LLVM_READOBJ_DT_FLAG_ENT(DF_1, CONFALT), LLVM_READOBJ_DT_FLAG_ENT(DF_1, ENDFILTEE), LLVM_READOBJ_DT_FLAG_ENT(DF_1, DISPRELDNE), LLVM_READOBJ_DT_FLAG_ENT(DF_1, NODIRECT), LLVM_READOBJ_DT_FLAG_ENT(DF_1, IGNMULDEF), LLVM_READOBJ_DT_FLAG_ENT(DF_1, NOKSYMS), LLVM_READOBJ_DT_FLAG_ENT(DF_1, NOHDR), LLVM_READOBJ_DT_FLAG_ENT(DF_1, EDITED), LLVM_READOBJ_DT_FLAG_ENT(DF_1, NORELOC), LLVM_READOBJ_DT_FLAG_ENT(DF_1, SYMINTPOSE), LLVM_READOBJ_DT_FLAG_ENT(DF_1, GLOBAUDIT), LLVM_READOBJ_DT_FLAG_ENT(DF_1, SINGLETON) }; static const EnumEntry
ElfDynamicDTMipsFlags[] = { LLVM_READOBJ_DT_FLAG_ENT(RHF, NONE), LLVM_READOBJ_DT_FLAG_ENT(RHF, QUICKSTART), LLVM_READOBJ_DT_FLAG_ENT(RHF, NOTPOT), LLVM_READOBJ_DT_FLAG_ENT(RHS, NO_LIBRARY_REPLACEMENT), LLVM_READOBJ_DT_FLAG_ENT(RHF, NO_MOVE), LLVM_READOBJ_DT_FLAG_ENT(RHF, SGI_ONLY), LLVM_READOBJ_DT_FLAG_ENT(RHF, GUARANTEE_INIT), LLVM_READOBJ_DT_FLAG_ENT(RHF, DELTA_C_PLUS_PLUS), LLVM_READOBJ_DT_FLAG_ENT(RHF, GUARANTEE_START_INIT), LLVM_READOBJ_DT_FLAG_ENT(RHF, PIXIE), LLVM_READOBJ_DT_FLAG_ENT(RHF, DEFAULT_DELAY_LOAD), LLVM_READOBJ_DT_FLAG_ENT(RHF, REQUICKSTART), LLVM_READOBJ_DT_FLAG_ENT(RHF, REQUICKSTARTED), LLVM_READOBJ_DT_FLAG_ENT(RHF, CORD), LLVM_READOBJ_DT_FLAG_ENT(RHF, NO_UNRES_UNDEF), LLVM_READOBJ_DT_FLAG_ENT(RHF, RLD_ORDER_SAFE) }; #undef LLVM_READOBJ_DT_FLAG_ENT template
void printFlags(T Value, ArrayRef
> Flags, raw_ostream &OS) { typedef EnumEntry
FlagEntry; typedef SmallVector
FlagVector; FlagVector SetFlags; for (const auto &Flag : Flags) { if (Flag.Value == 0) continue; if ((Value & Flag.Value) == Flag.Value) SetFlags.push_back(Flag); } for (const auto &Flag : SetFlags) { OS << Flag.Name << " "; } } template
StringRef ELFDumper
::getDynamicString(uint64_t Value) const { if (Value >= DynamicStringTable.size()) reportError("Invalid dynamic string table reference"); return StringRef(DynamicStringTable.data() + Value); } template
void ELFDumper
::printValue(uint64_t Type, uint64_t Value) { raw_ostream &OS = W.getOStream(); const char* ConvChar = (opts::Output == opts::GNU) ? "0x%" PRIx64 : "0x%" PRIX64; switch (Type) { case DT_PLTREL: if (Value == DT_REL) { OS << "REL"; break; } else if (Value == DT_RELA) { OS << "RELA"; break; } // Fallthrough. case DT_PLTGOT: case DT_HASH: case DT_STRTAB: case DT_SYMTAB: case DT_RELA: case DT_INIT: case DT_FINI: case DT_REL: case DT_JMPREL: case DT_INIT_ARRAY: case DT_FINI_ARRAY: case DT_PREINIT_ARRAY: case DT_DEBUG: case DT_VERDEF: case DT_VERNEED: case DT_VERSYM: case DT_GNU_HASH: case DT_NULL: case DT_MIPS_BASE_ADDRESS: case DT_MIPS_GOTSYM: case DT_MIPS_RLD_MAP: case DT_MIPS_RLD_MAP_REL: case DT_MIPS_PLTGOT: case DT_MIPS_OPTIONS: OS << format(ConvChar, Value); break; case DT_RELACOUNT: case DT_RELCOUNT: case DT_VERDEFNUM: case DT_VERNEEDNUM: case DT_MIPS_RLD_VERSION: case DT_MIPS_LOCAL_GOTNO: case DT_MIPS_SYMTABNO: case DT_MIPS_UNREFEXTNO: OS << Value; break; case DT_PLTRELSZ: case DT_RELASZ: case DT_RELAENT: case DT_STRSZ: case DT_SYMENT: case DT_RELSZ: case DT_RELENT: case DT_INIT_ARRAYSZ: case DT_FINI_ARRAYSZ: case DT_PREINIT_ARRAYSZ: OS << Value << " (bytes)"; break; case DT_NEEDED: OS << "SharedLibrary (" << getDynamicString(Value) << ")"; break; case DT_SONAME: OS << "LibrarySoname (" << getDynamicString(Value) << ")"; break; case DT_RPATH: case DT_RUNPATH: OS << getDynamicString(Value); break; case DT_MIPS_FLAGS: printFlags(Value, makeArrayRef(ElfDynamicDTMipsFlags), OS); break; case DT_FLAGS: printFlags(Value, makeArrayRef(ElfDynamicDTFlags), OS); break; case DT_FLAGS_1: printFlags(Value, makeArrayRef(ElfDynamicDTFlags1), OS); break; default: OS << format(ConvChar, Value); break; } } template
void ELFDumper
::printUnwindInfo() { W.startLine() << "UnwindInfo not implemented.\n"; } namespace { template <> void ELFDumper
>::printUnwindInfo() { const unsigned Machine = Obj->getHeader()->e_machine; if (Machine == EM_ARM) { ARM::EHABI::PrinterContext
> Ctx( W, Obj, DotSymtabSec); return Ctx.PrintUnwindInformation(); } W.startLine() << "UnwindInfo not implemented.\n"; } } template
void ELFDumper
::printDynamicTable() { auto I = dynamic_table().begin(); auto E = dynamic_table().end(); if (I == E) return; --E; while (I != E && E->getTag() == ELF::DT_NULL) --E; if (E->getTag() != ELF::DT_NULL) ++E; ++E; ptrdiff_t Total = std::distance(I, E); if (Total == 0) return; raw_ostream &OS = W.getOStream(); W.startLine() << "DynamicSection [ (" << Total << " entries)\n"; bool Is64 = ELFT::Is64Bits; W.startLine() << " Tag" << (Is64 ? " " : " ") << "Type" << " " << "Name/Value\n"; while (I != E) { const Elf_Dyn &Entry = *I; uintX_t Tag = Entry.getTag(); ++I; W.startLine() << " " << format_hex(Tag, Is64 ? 18 : 10, opts::Output != opts::GNU) << " " << format("%-21s", getTypeString(Tag)); printValue(Tag, Entry.getVal()); OS << "\n"; } W.startLine() << "]\n"; } template
void ELFDumper
::printNeededLibraries() { ListScope D(W, "NeededLibraries"); typedef std::vector
LibsTy; LibsTy Libs; for (const auto &Entry : dynamic_table()) if (Entry.d_tag == ELF::DT_NEEDED) Libs.push_back(getDynamicString(Entry.d_un.d_val)); std::stable_sort(Libs.begin(), Libs.end()); for (const auto &L : Libs) { outs() << " " << L << "\n"; } } template
void ELFDumper
::printHashTable() { DictScope D(W, "HashTable"); if (!HashTable) return; W.printNumber("Num Buckets", HashTable->nbucket); W.printNumber("Num Chains", HashTable->nchain); W.printList("Buckets", HashTable->buckets()); W.printList("Chains", HashTable->chains()); } template
void ELFDumper
::printGnuHashTable() { DictScope D(W, "GnuHashTable"); if (!GnuHashTable) return; W.printNumber("Num Buckets", GnuHashTable->nbuckets); W.printNumber("First Hashed Symbol Index", GnuHashTable->symndx); W.printNumber("Num Mask Words", GnuHashTable->maskwords); W.printNumber("Shift Count", GnuHashTable->shift2); W.printHexList("Bloom Filter", GnuHashTable->filter()); W.printList("Buckets", GnuHashTable->buckets()); Elf_Sym_Range Syms = dynamic_symbols(); unsigned NumSyms = std::distance(Syms.begin(), Syms.end()); if (!NumSyms) reportError("No dynamic symbol section"); W.printHexList("Values", GnuHashTable->values(NumSyms)); } template
void ELFDumper
::printLoadName() { outs() << "LoadName: " << SOName << '\n'; } template
void ELFDumper
::printAttributes() { W.startLine() << "Attributes not implemented.\n"; } namespace { template <> void ELFDumper
>::printAttributes() { if (Obj->getHeader()->e_machine != EM_ARM) { W.startLine() << "Attributes not implemented.\n"; return; } DictScope BA(W, "BuildAttributes"); for (const ELFO::Elf_Shdr &Sec : Obj->sections()) { if (Sec.sh_type != ELF::SHT_ARM_ATTRIBUTES) continue; ArrayRef
Contents = unwrapOrError(Obj->getSectionContents(&Sec)); if (Contents[0] != ARMBuildAttrs::Format_Version) { errs() << "unrecognised FormatVersion: 0x" << utohexstr(Contents[0]) << '\n'; continue; } W.printHex("FormatVersion", Contents[0]); if (Contents.size() == 1) continue; ARMAttributeParser(W).Parse(Contents); } } } namespace { template
class MipsGOTParser { public: typedef object::ELFFile
ELFO; typedef typename ELFO::Elf_Shdr Elf_Shdr; typedef typename ELFO::Elf_Sym Elf_Sym; typedef typename ELFO::Elf_Dyn_Range Elf_Dyn_Range; typedef typename ELFO::Elf_Addr GOTEntry; typedef typename ELFO::Elf_Rel Elf_Rel; typedef typename ELFO::Elf_Rela Elf_Rela; MipsGOTParser(ELFDumper
*Dumper, const ELFO *Obj, Elf_Dyn_Range DynTable, ScopedPrinter &W); void parseGOT(); void parsePLT(); private: ELFDumper
*Dumper; const ELFO *Obj; ScopedPrinter &W; llvm::Optional
DtPltGot; llvm::Optional
DtLocalGotNum; llvm::Optional
DtGotSym; llvm::Optional
DtMipsPltGot; llvm::Optional
DtJmpRel; std::size_t getGOTTotal(ArrayRef
GOT) const; const GOTEntry *makeGOTIter(ArrayRef
GOT, std::size_t EntryNum); void printGotEntry(uint64_t GotAddr, const GOTEntry *BeginIt, const GOTEntry *It); void printGlobalGotEntry(uint64_t GotAddr, const GOTEntry *BeginIt, const GOTEntry *It, const Elf_Sym *Sym, StringRef StrTable, bool IsDynamic); void printPLTEntry(uint64_t PLTAddr, const GOTEntry *BeginIt, const GOTEntry *It, StringRef Purpose); void printPLTEntry(uint64_t PLTAddr, const GOTEntry *BeginIt, const GOTEntry *It, StringRef StrTable, const Elf_Sym *Sym); }; } template
MipsGOTParser
::MipsGOTParser(ELFDumper
*Dumper, const ELFO *Obj, Elf_Dyn_Range DynTable, ScopedPrinter &W) : Dumper(Dumper), Obj(Obj), W(W) { for (const auto &Entry : DynTable) { switch (Entry.getTag()) { case ELF::DT_PLTGOT: DtPltGot = Entry.getVal(); break; case ELF::DT_MIPS_LOCAL_GOTNO: DtLocalGotNum = Entry.getVal(); break; case ELF::DT_MIPS_GOTSYM: DtGotSym = Entry.getVal(); break; case ELF::DT_MIPS_PLTGOT: DtMipsPltGot = Entry.getVal(); break; case ELF::DT_JMPREL: DtJmpRel = Entry.getVal(); break; } } } template
void MipsGOTParser
::parseGOT() { // See "Global Offset Table" in Chapter 5 in the following document // for detailed GOT description. // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf if (!DtPltGot) { W.startLine() << "Cannot find PLTGOT dynamic table tag.\n"; return; } if (!DtLocalGotNum) { W.startLine() << "Cannot find MIPS_LOCAL_GOTNO dynamic table tag.\n"; return; } if (!DtGotSym) { W.startLine() << "Cannot find MIPS_GOTSYM dynamic table tag.\n"; return; } StringRef StrTable = Dumper->getDynamicStringTable(); const Elf_Sym *DynSymBegin = Dumper->dynamic_symbols().begin(); const Elf_Sym *DynSymEnd = Dumper->dynamic_symbols().end(); std::size_t DynSymTotal = std::size_t(std::distance(DynSymBegin, DynSymEnd)); if (*DtGotSym > DynSymTotal) report_fatal_error("MIPS_GOTSYM exceeds a number of dynamic symbols"); std::size_t GlobalGotNum = DynSymTotal - *DtGotSym; if (*DtLocalGotNum + GlobalGotNum == 0) { W.startLine() << "GOT is empty.\n"; return; } const Elf_Shdr *GOTShdr = findNotEmptySectionByAddress(Obj, *DtPltGot); if (!GOTShdr) report_fatal_error("There is no not empty GOT section at 0x" + Twine::utohexstr(*DtPltGot)); ArrayRef
GOT = unwrapOrError(Obj->getSectionContents(GOTShdr)); if (*DtLocalGotNum + GlobalGotNum > getGOTTotal(GOT)) report_fatal_error("Number of GOT entries exceeds the size of GOT section"); const GOTEntry *GotBegin = makeGOTIter(GOT, 0); const GOTEntry *GotLocalEnd = makeGOTIter(GOT, *DtLocalGotNum); const GOTEntry *It = GotBegin; DictScope GS(W, "Primary GOT"); W.printHex("Canonical gp value", GOTShdr->sh_addr + 0x7ff0); { ListScope RS(W, "Reserved entries"); { DictScope D(W, "Entry"); printGotEntry(GOTShdr->sh_addr, GotBegin, It++); W.printString("Purpose", StringRef("Lazy resolver")); } if (It != GotLocalEnd && (*It >> (sizeof(GOTEntry) * 8 - 1)) != 0) { DictScope D(W, "Entry"); printGotEntry(GOTShdr->sh_addr, GotBegin, It++); W.printString("Purpose", StringRef("Module pointer (GNU extension)")); } } { ListScope LS(W, "Local entries"); for (; It != GotLocalEnd; ++It) { DictScope D(W, "Entry"); printGotEntry(GOTShdr->sh_addr, GotBegin, It); } } { ListScope GS(W, "Global entries"); const GOTEntry *GotGlobalEnd = makeGOTIter(GOT, *DtLocalGotNum + GlobalGotNum); const Elf_Sym *GotDynSym = DynSymBegin + *DtGotSym; for (; It != GotGlobalEnd; ++It) { DictScope D(W, "Entry"); printGlobalGotEntry(GOTShdr->sh_addr, GotBegin, It, GotDynSym++, StrTable, true); } } std::size_t SpecGotNum = getGOTTotal(GOT) - *DtLocalGotNum - GlobalGotNum; W.printNumber("Number of TLS and multi-GOT entries", uint64_t(SpecGotNum)); } template
void MipsGOTParser
::parsePLT() { if (!DtMipsPltGot) { W.startLine() << "Cannot find MIPS_PLTGOT dynamic table tag.\n"; return; } if (!DtJmpRel) { W.startLine() << "Cannot find JMPREL dynamic table tag.\n"; return; } const Elf_Shdr *PLTShdr = findNotEmptySectionByAddress(Obj, *DtMipsPltGot); if (!PLTShdr) report_fatal_error("There is no not empty PLTGOT section at 0x " + Twine::utohexstr(*DtMipsPltGot)); ArrayRef
PLT = unwrapOrError(Obj->getSectionContents(PLTShdr)); const Elf_Shdr *PLTRelShdr = findNotEmptySectionByAddress(Obj, *DtJmpRel); if (!PLTRelShdr) report_fatal_error("There is no not empty RELPLT section at 0x" + Twine::utohexstr(*DtJmpRel)); const Elf_Shdr *SymTable = unwrapOrError(Obj->getSection(PLTRelShdr->sh_link)); StringRef StrTable = unwrapOrError(Obj->getStringTableForSymtab(*SymTable)); const GOTEntry *PLTBegin = makeGOTIter(PLT, 0); const GOTEntry *PLTEnd = makeGOTIter(PLT, getGOTTotal(PLT)); const GOTEntry *It = PLTBegin; DictScope GS(W, "PLT GOT"); { ListScope RS(W, "Reserved entries"); printPLTEntry(PLTShdr->sh_addr, PLTBegin, It++, "PLT lazy resolver"); if (It != PLTEnd) printPLTEntry(PLTShdr->sh_addr, PLTBegin, It++, "Module pointer"); } { ListScope GS(W, "Entries"); switch (PLTRelShdr->sh_type) { case ELF::SHT_REL: for (const Elf_Rel *RI = Obj->rel_begin(PLTRelShdr), *RE = Obj->rel_end(PLTRelShdr); RI != RE && It != PLTEnd; ++RI, ++It) { const Elf_Sym *Sym = Obj->getRelocationSymbol(&*RI, SymTable); printPLTEntry(PLTShdr->sh_addr, PLTBegin, It, StrTable, Sym); } break; case ELF::SHT_RELA: for (const Elf_Rela *RI = Obj->rela_begin(PLTRelShdr), *RE = Obj->rela_end(PLTRelShdr); RI != RE && It != PLTEnd; ++RI, ++It) { const Elf_Sym *Sym = Obj->getRelocationSymbol(&*RI, SymTable); printPLTEntry(PLTShdr->sh_addr, PLTBegin, It, StrTable, Sym); } break; } } } template
std::size_t MipsGOTParser
::getGOTTotal(ArrayRef
GOT) const { return GOT.size() / sizeof(GOTEntry); } template
const typename MipsGOTParser
::GOTEntry * MipsGOTParser
::makeGOTIter(ArrayRef
GOT, std::size_t EntryNum) { const char *Data = reinterpret_cast
(GOT.data()); return reinterpret_cast
(Data + EntryNum * sizeof(GOTEntry)); } template
void MipsGOTParser
::printGotEntry(uint64_t GotAddr, const GOTEntry *BeginIt, const GOTEntry *It) { int64_t Offset = std::distance(BeginIt, It) * sizeof(GOTEntry); W.printHex("Address", GotAddr + Offset); W.printNumber("Access", Offset - 0x7ff0); W.printHex("Initial", *It); } template
void MipsGOTParser
::printGlobalGotEntry( uint64_t GotAddr, const GOTEntry *BeginIt, const GOTEntry *It, const Elf_Sym *Sym, StringRef StrTable, bool IsDynamic) { printGotEntry(GotAddr, BeginIt, It); W.printHex("Value", Sym->st_value); W.printEnum("Type", Sym->getType(), makeArrayRef(ElfSymbolTypes)); unsigned SectionIndex = 0; StringRef SectionName; getSectionNameIndex(*Obj, Sym, Dumper->dynamic_symbols().begin(), Dumper->getShndxTable(), SectionName, SectionIndex); W.printHex("Section", SectionName, SectionIndex); std::string FullSymbolName = Dumper->getFullSymbolName(Sym, StrTable, IsDynamic); W.printNumber("Name", FullSymbolName, Sym->st_name); } template
void MipsGOTParser
::printPLTEntry(uint64_t PLTAddr, const GOTEntry *BeginIt, const GOTEntry *It, StringRef Purpose) { DictScope D(W, "Entry"); int64_t Offset = std::distance(BeginIt, It) * sizeof(GOTEntry); W.printHex("Address", PLTAddr + Offset); W.printHex("Initial", *It); W.printString("Purpose", Purpose); } template
void MipsGOTParser
::printPLTEntry(uint64_t PLTAddr, const GOTEntry *BeginIt, const GOTEntry *It, StringRef StrTable, const Elf_Sym *Sym) { DictScope D(W, "Entry"); int64_t Offset = std::distance(BeginIt, It) * sizeof(GOTEntry); W.printHex("Address", PLTAddr + Offset); W.printHex("Initial", *It); W.printHex("Value", Sym->st_value); W.printEnum("Type", Sym->getType(), makeArrayRef(ElfSymbolTypes)); unsigned SectionIndex = 0; StringRef SectionName; getSectionNameIndex(*Obj, Sym, Dumper->dynamic_symbols().begin(), Dumper->getShndxTable(), SectionName, SectionIndex); W.printHex("Section", SectionName, SectionIndex); std::string FullSymbolName = Dumper->getFullSymbolName(Sym, StrTable, true); W.printNumber("Name", FullSymbolName, Sym->st_name); } template
void ELFDumper
::printMipsPLTGOT() { if (Obj->getHeader()->e_machine != EM_MIPS) { W.startLine() << "MIPS PLT GOT is available for MIPS targets only.\n"; return; } MipsGOTParser
GOTParser(this, Obj, dynamic_table(), W); GOTParser.parseGOT(); GOTParser.parsePLT(); } static const EnumEntry
ElfMipsISAExtType[] = { {"None", Mips::AFL_EXT_NONE}, {"Broadcom SB-1", Mips::AFL_EXT_SB1}, {"Cavium Networks Octeon", Mips::AFL_EXT_OCTEON}, {"Cavium Networks Octeon2", Mips::AFL_EXT_OCTEON2}, {"Cavium Networks OcteonP", Mips::AFL_EXT_OCTEONP}, {"Cavium Networks Octeon3", Mips::AFL_EXT_OCTEON3}, {"LSI R4010", Mips::AFL_EXT_4010}, {"Loongson 2E", Mips::AFL_EXT_LOONGSON_2E}, {"Loongson 2F", Mips::AFL_EXT_LOONGSON_2F}, {"Loongson 3A", Mips::AFL_EXT_LOONGSON_3A}, {"MIPS R4650", Mips::AFL_EXT_4650}, {"MIPS R5900", Mips::AFL_EXT_5900}, {"MIPS R10000", Mips::AFL_EXT_10000}, {"NEC VR4100", Mips::AFL_EXT_4100}, {"NEC VR4111/VR4181", Mips::AFL_EXT_4111}, {"NEC VR4120", Mips::AFL_EXT_4120}, {"NEC VR5400", Mips::AFL_EXT_5400}, {"NEC VR5500", Mips::AFL_EXT_5500}, {"RMI Xlr", Mips::AFL_EXT_XLR}, {"Toshiba R3900", Mips::AFL_EXT_3900} }; static const EnumEntry
ElfMipsASEFlags[] = { {"DSP", Mips::AFL_ASE_DSP}, {"DSPR2", Mips::AFL_ASE_DSPR2}, {"Enhanced VA Scheme", Mips::AFL_ASE_EVA}, {"MCU", Mips::AFL_ASE_MCU}, {"MDMX", Mips::AFL_ASE_MDMX}, {"MIPS-3D", Mips::AFL_ASE_MIPS3D}, {"MT", Mips::AFL_ASE_MT}, {"SmartMIPS", Mips::AFL_ASE_SMARTMIPS}, {"VZ", Mips::AFL_ASE_VIRT}, {"MSA", Mips::AFL_ASE_MSA}, {"MIPS16", Mips::AFL_ASE_MIPS16}, {"microMIPS", Mips::AFL_ASE_MICROMIPS}, {"XPA", Mips::AFL_ASE_XPA} }; static const EnumEntry
ElfMipsFpABIType[] = { {"Hard or soft float", Mips::Val_GNU_MIPS_ABI_FP_ANY}, {"Hard float (double precision)", Mips::Val_GNU_MIPS_ABI_FP_DOUBLE}, {"Hard float (single precision)", Mips::Val_GNU_MIPS_ABI_FP_SINGLE}, {"Soft float", Mips::Val_GNU_MIPS_ABI_FP_SOFT}, {"Hard float (MIPS32r2 64-bit FPU 12 callee-saved)", Mips::Val_GNU_MIPS_ABI_FP_OLD_64}, {"Hard float (32-bit CPU, Any FPU)", Mips::Val_GNU_MIPS_ABI_FP_XX}, {"Hard float (32-bit CPU, 64-bit FPU)", Mips::Val_GNU_MIPS_ABI_FP_64}, {"Hard float compat (32-bit CPU, 64-bit FPU)", Mips::Val_GNU_MIPS_ABI_FP_64A} }; static const EnumEntry
ElfMipsFlags1[] { {"ODDSPREG", Mips::AFL_FLAGS1_ODDSPREG}, }; static int getMipsRegisterSize(uint8_t Flag) { switch (Flag) { case Mips::AFL_REG_NONE: return 0; case Mips::AFL_REG_32: return 32; case Mips::AFL_REG_64: return 64; case Mips::AFL_REG_128: return 128; default: return -1; } } template
void ELFDumper
::printMipsABIFlags() { const Elf_Shdr *Shdr = findSectionByName(*Obj, ".MIPS.abiflags"); if (!Shdr) { W.startLine() << "There is no .MIPS.abiflags section in the file.\n"; return; } ArrayRef
Sec = unwrapOrError(Obj->getSectionContents(Shdr)); if (Sec.size() != sizeof(Elf_Mips_ABIFlags
)) { W.startLine() << "The .MIPS.abiflags section has a wrong size.\n"; return; } auto *Flags = reinterpret_cast
*>(Sec.data()); raw_ostream &OS = W.getOStream(); DictScope GS(W, "MIPS ABI Flags"); W.printNumber("Version", Flags->version); W.startLine() << "ISA: "; if (Flags->isa_rev <= 1) OS << format("MIPS%u", Flags->isa_level); else OS << format("MIPS%ur%u", Flags->isa_level, Flags->isa_rev); OS << "\n"; W.printEnum("ISA Extension", Flags->isa_ext, makeArrayRef(ElfMipsISAExtType)); W.printFlags("ASEs", Flags->ases, makeArrayRef(ElfMipsASEFlags)); W.printEnum("FP ABI", Flags->fp_abi, makeArrayRef(ElfMipsFpABIType)); W.printNumber("GPR size", getMipsRegisterSize(Flags->gpr_size)); W.printNumber("CPR1 size", getMipsRegisterSize(Flags->cpr1_size)); W.printNumber("CPR2 size", getMipsRegisterSize(Flags->cpr2_size)); W.printFlags("Flags 1", Flags->flags1, makeArrayRef(ElfMipsFlags1)); W.printHex("Flags 2", Flags->flags2); } template
static void printMipsReginfoData(ScopedPrinter &W, const Elf_Mips_RegInfo
&Reginfo) { W.printHex("GP", Reginfo.ri_gp_value); W.printHex("General Mask", Reginfo.ri_gprmask); W.printHex("Co-Proc Mask0", Reginfo.ri_cprmask[0]); W.printHex("Co-Proc Mask1", Reginfo.ri_cprmask[1]); W.printHex("Co-Proc Mask2", Reginfo.ri_cprmask[2]); W.printHex("Co-Proc Mask3", Reginfo.ri_cprmask[3]); } template
void ELFDumper
::printMipsReginfo() { const Elf_Shdr *Shdr = findSectionByName(*Obj, ".reginfo"); if (!Shdr) { W.startLine() << "There is no .reginfo section in the file.\n"; return; } ArrayRef
Sec = unwrapOrError(Obj->getSectionContents(Shdr)); if (Sec.size() != sizeof(Elf_Mips_RegInfo
)) { W.startLine() << "The .reginfo section has a wrong size.\n"; return; } DictScope GS(W, "MIPS RegInfo"); auto *Reginfo = reinterpret_cast
*>(Sec.data()); printMipsReginfoData(W, *Reginfo); } template
void ELFDumper
::printMipsOptions() { const Elf_Shdr *Shdr = findSectionByName(*Obj, ".MIPS.options"); if (!Shdr) { W.startLine() << "There is no .MIPS.options section in the file.\n"; return; } DictScope GS(W, "MIPS Options"); ArrayRef
Sec = unwrapOrError(Obj->getSectionContents(Shdr)); while (!Sec.empty()) { if (Sec.size() < sizeof(Elf_Mips_Options
)) { W.startLine() << "The .MIPS.options section has a wrong size.\n"; return; } auto *O = reinterpret_cast
*>(Sec.data()); DictScope GS(W, getElfMipsOptionsOdkType(O->kind)); switch (O->kind) { case ODK_REGINFO: printMipsReginfoData(W, O->getRegInfo()); break; default: W.startLine() << "Unsupported MIPS options tag.\n"; break; } Sec = Sec.slice(O->size); } } template
void ELFDumper
::printStackMap() const { const Elf_Shdr *StackMapSection = nullptr; for (const auto &Sec : Obj->sections()) { StringRef Name = unwrapOrError(Obj->getSectionName(&Sec)); if (Name == ".llvm_stackmaps") { StackMapSection = &Sec; break; } } if (!StackMapSection) return; StringRef StackMapContents; ArrayRef
StackMapContentsArray = unwrapOrError(Obj->getSectionContents(StackMapSection)); prettyPrintStackMap(llvm::outs(), StackMapV1Parser
( StackMapContentsArray)); } template
void ELFDumper
::printGroupSections() { ELFDumperStyle->printGroupSections(Obj); } static inline void printFields(formatted_raw_ostream &OS, StringRef Str1, StringRef Str2) { OS.PadToColumn(2u); OS << Str1; OS.PadToColumn(37u); OS << Str2 << "\n"; OS.flush(); } template
void GNUStyle
::printFileHeaders(const ELFO *Obj) { const Elf_Ehdr *e = Obj->getHeader(); OS << "ELF Header:\n"; OS << " Magic: "; std::string Str; for (int i = 0; i < ELF::EI_NIDENT; i++) OS << format(" %02x", static_cast
(e->e_ident[i])); OS << "\n"; Str = printEnum(e->e_ident[ELF::EI_CLASS], makeArrayRef(ElfClass)); printFields(OS, "Class:", Str); Str = printEnum(e->e_ident[ELF::EI_DATA], makeArrayRef(ElfDataEncoding)); printFields(OS, "Data:", Str); OS.PadToColumn(2u); OS << "Version:"; OS.PadToColumn(37u); OS << to_hexString(e->e_ident[ELF::EI_VERSION]); if (e->e_version == ELF::EV_CURRENT) OS << " (current)"; OS << "\n"; Str = printEnum(e->e_ident[ELF::EI_OSABI], makeArrayRef(ElfOSABI)); printFields(OS, "OS/ABI:", Str); Str = "0x" + to_hexString(e->e_version); Str = to_hexString(e->e_ident[ELF::EI_ABIVERSION]); printFields(OS, "ABI Version:", Str); Str = printEnum(e->e_type, makeArrayRef(ElfObjectFileType)); printFields(OS, "Type:", Str); Str = printEnum(e->e_machine, makeArrayRef(ElfMachineType)); printFields(OS, "Machine:", Str); Str = "0x" + to_hexString(e->e_version); printFields(OS, "Version:", Str); Str = "0x" + to_hexString(e->e_entry); printFields(OS, "Entry point address:", Str); Str = to_string(e->e_phoff) + " (bytes into file)"; printFields(OS, "Start of program headers:", Str); Str = to_string(e->e_shoff) + " (bytes into file)"; printFields(OS, "Start of section headers:", Str); Str = "0x" + to_hexString(e->e_flags); printFields(OS, "Flags:", Str); Str = to_string(e->e_ehsize) + " (bytes)"; printFields(OS, "Size of this header:", Str); Str = to_string(e->e_phentsize) + " (bytes)"; printFields(OS, "Size of program headers:", Str); Str = to_string(e->e_phnum); printFields(OS, "Number of program headers:", Str); Str = to_string(e->e_shentsize) + " (bytes)"; printFields(OS, "Size of section headers:", Str); Str = to_string(e->e_shnum); printFields(OS, "Number of section headers:", Str); Str = to_string(e->e_shstrndx); printFields(OS, "Section header string table index:", Str); } template
void GNUStyle
::printGroupSections(const ELFO *Obj) { uint32_t SectionIndex = 0; bool HasGroups = false; for (const Elf_Shdr &Sec : Obj->sections()) { if (Sec.sh_type == ELF::SHT_GROUP) { HasGroups = true; const Elf_Shdr *Symtab = unwrapOrError(Obj->getSection(Sec.sh_link)); StringRef StrTable = unwrapOrError(Obj->getStringTableForSymtab(*Symtab)); const Elf_Sym *Signature = Obj->template getEntry