blob: 7e140933393c7956bb1497d066b8b9aef97c2e4c [file] [log] [blame]
//===- ELFDumper.cpp - ELF-specific dumper --------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
///
/// \file
/// This file implements the ELF-specific dumper for llvm-readobj.
///
//===----------------------------------------------------------------------===//
#include "ARMEHABIPrinter.h"
#include "DwarfCFIEHPrinter.h"
#include "Error.h"
#include "ObjDumper.h"
#include "StackMapPrinter.h"
#include "llvm-readobj.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/MapVector.h"
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/PointerIntPair.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/Twine.h"
#include "llvm/BinaryFormat/AMDGPUMetadataVerifier.h"
#include "llvm/BinaryFormat/ELF.h"
#include "llvm/Demangle/Demangle.h"
#include "llvm/Object/ELF.h"
#include "llvm/Object/ELFObjectFile.h"
#include "llvm/Object/ELFTypes.h"
#include "llvm/Object/Error.h"
#include "llvm/Object/ObjectFile.h"
#include "llvm/Object/RelocationResolver.h"
#include "llvm/Object/StackMapParser.h"
#include "llvm/Support/AMDGPUMetadata.h"
#include "llvm/Support/ARMAttributeParser.h"
#include "llvm/Support/ARMBuildAttributes.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/Endian.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/FormattedStream.h"
#include "llvm/Support/LEB128.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/MipsABIFlags.h"
#include "llvm/Support/ScopedPrinter.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
#include <cinttypes>
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <iterator>
#include <memory>
#include <string>
#include <system_error>
#include <unordered_set>
#include <vector>
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) \
using ELFO = ELFFile<ELFT>; \
using Elf_Addr = typename ELFT::Addr; \
using Elf_Shdr = typename ELFT::Shdr; \
using Elf_Sym = typename ELFT::Sym; \
using Elf_Dyn = typename ELFT::Dyn; \
using Elf_Dyn_Range = typename ELFT::DynRange; \
using Elf_Rel = typename ELFT::Rel; \
using Elf_Rela = typename ELFT::Rela; \
using Elf_Relr = typename ELFT::Relr; \
using Elf_Rel_Range = typename ELFT::RelRange; \
using Elf_Rela_Range = typename ELFT::RelaRange; \
using Elf_Relr_Range = typename ELFT::RelrRange; \
using Elf_Phdr = typename ELFT::Phdr; \
using Elf_Half = typename ELFT::Half; \
using Elf_Ehdr = typename ELFT::Ehdr; \
using Elf_Word = typename ELFT::Word; \
using Elf_Hash = typename ELFT::Hash; \
using Elf_GnuHash = typename ELFT::GnuHash; \
using Elf_Note = typename ELFT::Note; \
using Elf_Sym_Range = typename ELFT::SymRange; \
using Elf_Versym = typename ELFT::Versym; \
using Elf_Verneed = typename ELFT::Verneed; \
using Elf_Vernaux = typename ELFT::Vernaux; \
using Elf_Verdef = typename ELFT::Verdef; \
using Elf_Verdaux = typename ELFT::Verdaux; \
using Elf_CGProfile = typename ELFT::CGProfile; \
using uintX_t = typename ELFT::uint;
namespace {
template <class ELFT> 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(StringRef ObjName) : FileName(ObjName) {}
DynRegionInfo(const void *A, uint64_t S, uint64_t ES, StringRef ObjName)
: Addr(A), Size(S), EntSize(ES), FileName(ObjName) {}
/// Address in current address space.
const void *Addr = nullptr;
/// Size in bytes of the region.
uint64_t Size = 0;
/// Size of each entity in the region.
uint64_t EntSize = 0;
/// Name of the file. Used for error reporting.
StringRef FileName;
template <typename Type> ArrayRef<Type> getAsArrayRef() const {
const Type *Start = reinterpret_cast<const Type *>(Addr);
if (!Start)
return {Start, Start};
if (EntSize != sizeof(Type) || Size % EntSize) {
// TODO: Add a section index to this warning.
reportWarning(createError("invalid section size (" + Twine(Size) +
") or entity size (" + Twine(EntSize) + ")"),
FileName);
return {Start, Start};
}
return {Start, Start + (Size / EntSize)};
}
};
template <typename ELFT> class ELFDumper : public ObjDumper {
public:
ELFDumper(const object::ELFObjectFile<ELFT> *ObjF, ScopedPrinter &Writer);
void printFileHeaders() override;
void printSectionHeaders() override;
void printRelocations() override;
void printDynamicRelocations() override;
void printSymbols(bool PrintSymbols, bool PrintDynamicSymbols) override;
void printHashSymbols() override;
void printUnwindInfo() override;
void printDynamicTable() override;
void printNeededLibraries() override;
void printProgramHeaders(bool PrintProgramHeaders,
cl::boolOrDefault PrintSectionMapping) override;
void printHashTable() override;
void printGnuHashTable() override;
void printLoadName() override;
void printVersionInfo() override;
void printGroupSections() override;
void printArchSpecificInfo() override;
void printStackMap() const override;
void printHashHistogram() override;
void printCGProfile() override;
void printAddrsig() override;
void printNotes() override;
void printELFLinkerOptions() override;
void printStackSizes() override;
const object::ELFObjectFile<ELFT> *getElfObject() const { return ObjF; };
private:
std::unique_ptr<DumpStyle<ELFT>> ELFDumperStyle;
TYPEDEF_ELF_TYPES(ELFT)
DynRegionInfo checkDRI(DynRegionInfo DRI) {
const ELFFile<ELFT> *Obj = ObjF->getELFFile();
if (DRI.Addr < Obj->base() ||
reinterpret_cast<const uint8_t *>(DRI.Addr) + DRI.Size >
Obj->base() + Obj->getBufSize())
reportError(errorCodeToError(llvm::object::object_error::parse_failed),
ObjF->getFileName());
return DRI;
}
DynRegionInfo createDRIFrom(const Elf_Phdr *P, uintX_t EntSize) {
return checkDRI({ObjF->getELFFile()->base() + P->p_offset, P->p_filesz,
EntSize, ObjF->getFileName()});
}
DynRegionInfo createDRIFrom(const Elf_Shdr *S) {
return checkDRI({ObjF->getELFFile()->base() + S->sh_offset, S->sh_size,
S->sh_entsize, ObjF->getFileName()});
}
void printAttributes();
void printMipsReginfo();
void printMipsOptions();
std::pair<const Elf_Phdr *, const Elf_Shdr *>
findDynamic(const ELFFile<ELFT> *Obj);
void loadDynamicTable(const ELFFile<ELFT> *Obj);
void parseDynamicTable();
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 object::ELFObjectFile<ELFT> *ObjF;
DynRegionInfo DynRelRegion;
DynRegionInfo DynRelaRegion;
DynRegionInfo DynRelrRegion;
DynRegionInfo DynPLTRelRegion;
DynRegionInfo DynSymRegion;
DynRegionInfo DynamicTable;
StringRef DynamicStringTable;
std::string SOName = "<Not found>";
const Elf_Hash *HashTable = nullptr;
const Elf_GnuHash *GnuHashTable = nullptr;
const Elf_Shdr *DotSymtabSec = nullptr;
const Elf_Shdr *DotCGProfileSec = nullptr;
const Elf_Shdr *DotAddrsigSec = nullptr;
StringRef DynSymtabName;
ArrayRef<Elf_Word> ShndxTable;
const Elf_Shdr *SymbolVersionSection = nullptr; // .gnu.version
const Elf_Shdr *SymbolVersionNeedSection = nullptr; // .gnu.version_r
const Elf_Shdr *SymbolVersionDefSection = 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<const void *, 1> {
public:
// If the integer is 0, this is an Elf_Verdef*.
// If the integer is 1, this is an Elf_Vernaux*.
VersionMapEntry() : PointerIntPair<const void *, 1>(nullptr, 0) {}
VersionMapEntry(const Elf_Verdef *verdef)
: PointerIntPair<const void *, 1>(verdef, 0) {}
VersionMapEntry(const Elf_Vernaux *vernaux)
: PointerIntPair<const void *, 1>(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<VersionMapEntry, 16> VersionMap;
public:
Elf_Dyn_Range dynamic_table() const {
// A valid .dynamic section contains an array of entries terminated
// with a DT_NULL entry. However, sometimes the section content may
// continue past the DT_NULL entry, so to dump the section correctly,
// we first find the end of the entries by iterating over them.
Elf_Dyn_Range Table = DynamicTable.getAsArrayRef<Elf_Dyn>();
size_t Size = 0;
while (Size < Table.size())
if (Table[Size++].getTag() == DT_NULL)
break;
return Table.slice(0, Size);
}
Elf_Sym_Range dynamic_symbols() const {
return DynSymRegion.getAsArrayRef<Elf_Sym>();
}
Elf_Rel_Range dyn_rels() const;
Elf_Rela_Range dyn_relas() const;
Elf_Relr_Range dyn_relrs() const;
std::string getFullSymbolName(const Elf_Sym *Symbol, StringRef StrTable,
bool IsDynamic) const;
void getSectionNameIndex(const Elf_Sym *Symbol, const Elf_Sym *FirstSym,
StringRef &SectionName,
unsigned &SectionIndex) const;
Expected<std::string> getStaticSymbolName(uint32_t Index) const;
std::string getDynamicString(uint64_t Value) const;
StringRef getSymbolVersionByIndex(StringRef StrTab,
uint32_t VersionSymbolIndex,
bool &IsDefault) const;
void printSymbolsHelper(bool IsDynamic) const;
void printDynamicEntry(raw_ostream &OS, uint64_t Type, uint64_t Value) const;
const Elf_Shdr *getDotSymtabSec() const { return DotSymtabSec; }
const Elf_Shdr *getDotCGProfileSec() const { return DotCGProfileSec; }
const Elf_Shdr *getDotAddrsigSec() const { return DotAddrsigSec; }
ArrayRef<Elf_Word> getShndxTable() const { return ShndxTable; }
StringRef getDynamicStringTable() const { return DynamicStringTable; }
const DynRegionInfo &getDynRelRegion() const { return DynRelRegion; }
const DynRegionInfo &getDynRelaRegion() const { return DynRelaRegion; }
const DynRegionInfo &getDynRelrRegion() const { return DynRelrRegion; }
const DynRegionInfo &getDynPLTRelRegion() const { return DynPLTRelRegion; }
const DynRegionInfo &getDynamicTableRegion() const { return DynamicTable; }
const Elf_Hash *getHashTable() const { return HashTable; }
const Elf_GnuHash *getGnuHashTable() const { return GnuHashTable; }
};
template <class ELFT>
void ELFDumper<ELFT>::printSymbolsHelper(bool IsDynamic) const {
StringRef StrTable, SymtabName;
size_t Entries = 0;
Elf_Sym_Range Syms(nullptr, nullptr);
const ELFFile<ELFT> *Obj = ObjF->getELFFile();
if (IsDynamic) {
StrTable = DynamicStringTable;
Syms = dynamic_symbols();
SymtabName = DynSymtabName;
if (DynSymRegion.Addr)
Entries = DynSymRegion.Size / DynSymRegion.EntSize;
} else {
if (!DotSymtabSec)
return;
StrTable = unwrapOrError(ObjF->getFileName(),
Obj->getStringTableForSymtab(*DotSymtabSec));
Syms = unwrapOrError(ObjF->getFileName(), Obj->symbols(DotSymtabSec));
SymtabName =
unwrapOrError(ObjF->getFileName(), Obj->getSectionName(DotSymtabSec));
Entries = DotSymtabSec->getEntityCount();
}
if (Syms.begin() == Syms.end())
return;
// The st_other field has 2 logical parts. The first two bits hold the symbol
// visibility (STV_*) and the remainder hold other platform-specific values.
bool NonVisibilityBitsUsed = llvm::find_if(Syms, [](const Elf_Sym &S) {
return S.st_other & ~0x3;
}) != Syms.end();
ELFDumperStyle->printSymtabMessage(Obj, SymtabName, Entries,
NonVisibilityBitsUsed);
for (const auto &Sym : Syms)
ELFDumperStyle->printSymbol(Obj, &Sym, Syms.begin(), StrTable, IsDynamic,
NonVisibilityBitsUsed);
}
template <class ELFT> class MipsGOTParser;
template <typename ELFT> class DumpStyle {
public:
using Elf_Shdr = typename ELFT::Shdr;
using Elf_Sym = typename ELFT::Sym;
using Elf_Addr = typename ELFT::Addr;
DumpStyle(ELFDumper<ELFT> *Dumper) : Dumper(Dumper) {
FileName = this->Dumper->getElfObject()->getFileName();
// Dumper reports all non-critical errors as warnings.
// It does not print the same warning more than once.
WarningHandler = [this](const Twine &Msg) {
if (Warnings.insert(Msg.str()).second)
reportWarning(createError(Msg), FileName);
return Error::success();
};
}
virtual ~DumpStyle() = default;
virtual void printFileHeaders(const ELFFile<ELFT> *Obj) = 0;
virtual void printGroupSections(const ELFFile<ELFT> *Obj) = 0;
virtual void printRelocations(const ELFFile<ELFT> *Obj) = 0;
virtual void printSectionHeaders(const ELFFile<ELFT> *Obj) = 0;
virtual void printSymbols(const ELFFile<ELFT> *Obj, bool PrintSymbols,
bool PrintDynamicSymbols) = 0;
virtual void printHashSymbols(const ELFFile<ELFT> *Obj) {}
virtual void printDynamic(const ELFFile<ELFT> *Obj) {}
virtual void printDynamicRelocations(const ELFFile<ELFT> *Obj) = 0;
virtual void printSymtabMessage(const ELFFile<ELFT> *Obj, StringRef Name,
size_t Offset, bool NonVisibilityBitsUsed) {}
virtual void printSymbol(const ELFFile<ELFT> *Obj, const Elf_Sym *Symbol,
const Elf_Sym *FirstSym, StringRef StrTable,
bool IsDynamic, bool NonVisibilityBitsUsed) = 0;
virtual void printProgramHeaders(const ELFFile<ELFT> *Obj,
bool PrintProgramHeaders,
cl::boolOrDefault PrintSectionMapping) = 0;
virtual void printVersionSymbolSection(const ELFFile<ELFT> *Obj,
const Elf_Shdr *Sec) = 0;
virtual void printVersionDefinitionSection(const ELFFile<ELFT> *Obj,
const Elf_Shdr *Sec) = 0;
virtual void printVersionDependencySection(const ELFFile<ELFT> *Obj,
const Elf_Shdr *Sec) = 0;
virtual void printHashHistogram(const ELFFile<ELFT> *Obj) = 0;
virtual void printCGProfile(const ELFFile<ELFT> *Obj) = 0;
virtual void printAddrsig(const ELFFile<ELFT> *Obj) = 0;
virtual void printNotes(const ELFFile<ELFT> *Obj) = 0;
virtual void printELFLinkerOptions(const ELFFile<ELFT> *Obj) = 0;
virtual void printStackSizes(const ELFObjectFile<ELFT> *Obj) = 0;
void printNonRelocatableStackSizes(const ELFObjectFile<ELFT> *Obj,
std::function<void()> PrintHeader);
void printRelocatableStackSizes(const ELFObjectFile<ELFT> *Obj,
std::function<void()> PrintHeader);
void printFunctionStackSize(const ELFObjectFile<ELFT> *Obj, uint64_t SymValue,
SectionRef FunctionSec,
const StringRef SectionName, DataExtractor Data,
uint64_t *Offset);
void printStackSize(const ELFObjectFile<ELFT> *Obj, RelocationRef Rel,
SectionRef FunctionSec,
const StringRef &StackSizeSectionName,
const RelocationResolver &Resolver, DataExtractor Data);
virtual void printStackSizeEntry(uint64_t Size, StringRef FuncName) = 0;
virtual void printMipsGOT(const MipsGOTParser<ELFT> &Parser) = 0;
virtual void printMipsPLT(const MipsGOTParser<ELFT> &Parser) = 0;
virtual void printMipsABIFlags(const ELFObjectFile<ELFT> *Obj) = 0;
const ELFDumper<ELFT> *dumper() const { return Dumper; }
protected:
std::function<Error(const Twine &Msg)> WarningHandler;
StringRef FileName;
private:
std::unordered_set<std::string> Warnings;
const ELFDumper<ELFT> *Dumper;
};
template <typename ELFT> class GNUStyle : public DumpStyle<ELFT> {
formatted_raw_ostream &OS;
public:
TYPEDEF_ELF_TYPES(ELFT)
GNUStyle(ScopedPrinter &W, ELFDumper<ELFT> *Dumper)
: DumpStyle<ELFT>(Dumper),
OS(static_cast<formatted_raw_ostream&>(W.getOStream())) {
assert (&W.getOStream() == &llvm::fouts());
}
void printFileHeaders(const ELFO *Obj) override;
void printGroupSections(const ELFFile<ELFT> *Obj) override;
void printRelocations(const ELFO *Obj) override;
void printSectionHeaders(const ELFO *Obj) override;
void printSymbols(const ELFO *Obj, bool PrintSymbols,
bool PrintDynamicSymbols) override;
void printHashSymbols(const ELFO *Obj) override;
void printDynamic(const ELFFile<ELFT> *Obj) override;
void printDynamicRelocations(const ELFO *Obj) override;
void printSymtabMessage(const ELFO *Obj, StringRef Name, size_t Offset,
bool NonVisibilityBitsUsed) override;
void printProgramHeaders(const ELFO *Obj, bool PrintProgramHeaders,
cl::boolOrDefault PrintSectionMapping) override;
void printVersionSymbolSection(const ELFFile<ELFT> *Obj,
const Elf_Shdr *Sec) override;
void printVersionDefinitionSection(const ELFFile<ELFT> *Obj,
const Elf_Shdr *Sec) override;
void printVersionDependencySection(const ELFFile<ELFT> *Obj,
const Elf_Shdr *Sec) override;
void printHashHistogram(const ELFFile<ELFT> *Obj) override;
void printCGProfile(const ELFFile<ELFT> *Obj) override;
void printAddrsig(const ELFFile<ELFT> *Obj) override;
void printNotes(const ELFFile<ELFT> *Obj) override;
void printELFLinkerOptions(const ELFFile<ELFT> *Obj) override;
void printStackSizes(const ELFObjectFile<ELFT> *Obj) override;
void printStackSizeEntry(uint64_t Size, StringRef FuncName) override;
void printMipsGOT(const MipsGOTParser<ELFT> &Parser) override;
void printMipsPLT(const MipsGOTParser<ELFT> &Parser) override;
void printMipsABIFlags(const ELFObjectFile<ELFT> *Obj) override;
private:
struct Field {
std::string Str;
unsigned Column;
Field(StringRef S, unsigned Col) : Str(S), Column(Col) {}
Field(unsigned Col) : Column(Col) {}
};
template <typename T, typename TEnum>
std::string printEnum(T Value, ArrayRef<EnumEntry<TEnum>> EnumValues) {
for (const auto &EnumItem : EnumValues)
if (EnumItem.Value == Value)
return EnumItem.AltName;
return to_hexString(Value, false);
}
template <typename T, typename TEnum>
std::string printFlags(T Value, ArrayRef<EnumEntry<TEnum>> EnumValues,
TEnum EnumMask1 = {}, TEnum EnumMask2 = {},
TEnum EnumMask3 = {}) {
std::string Str;
for (const auto &Flag : EnumValues) {
if (Flag.Value == 0)
continue;
TEnum EnumMask{};
if (Flag.Value & EnumMask1)
EnumMask = EnumMask1;
else if (Flag.Value & EnumMask2)
EnumMask = EnumMask2;
else if (Flag.Value & EnumMask3)
EnumMask = EnumMask3;
bool IsEnum = (Flag.Value & EnumMask) != 0;
if ((!IsEnum && (Value & Flag.Value) == Flag.Value) ||
(IsEnum && (Value & EnumMask) == Flag.Value)) {
if (!Str.empty())
Str += ", ";
Str += Flag.AltName;
}
}
return Str;
}
formatted_raw_ostream &printField(struct Field F) {
if (F.Column != 0)
OS.PadToColumn(F.Column);
OS << F.Str;
OS.flush();
return OS;
}
void printHashedSymbol(const ELFO *Obj, const Elf_Sym *FirstSym, uint32_t Sym,
StringRef StrTable, uint32_t Bucket);
void printRelocHeader(unsigned SType);
void printRelocation(const ELFO *Obj, const Elf_Shdr *SymTab,
const Elf_Rela &R, bool IsRela);
void printRelocation(const ELFO *Obj, const Elf_Sym *Sym,
StringRef SymbolName, const Elf_Rela &R, bool IsRela);
void printSymbol(const ELFO *Obj, const Elf_Sym *Symbol, const Elf_Sym *First,
StringRef StrTable, bool IsDynamic,
bool NonVisibilityBitsUsed) 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);
void printProgramHeaders(const ELFO *Obj);
void printSectionMapping(const ELFO *Obj);
};
template <typename ELFT> class LLVMStyle : public DumpStyle<ELFT> {
public:
TYPEDEF_ELF_TYPES(ELFT)
LLVMStyle(ScopedPrinter &W, ELFDumper<ELFT> *Dumper)
: DumpStyle<ELFT>(Dumper), W(W) {}
void printFileHeaders(const ELFO *Obj) override;
void printGroupSections(const ELFFile<ELFT> *Obj) override;
void printRelocations(const ELFO *Obj) override;
void printRelocations(const Elf_Shdr *Sec, const ELFO *Obj);
void printSectionHeaders(const ELFO *Obj) override;
void printSymbols(const ELFO *Obj, bool PrintSymbols,
bool PrintDynamicSymbols) override;
void printDynamic(const ELFFile<ELFT> *Obj) override;
void printDynamicRelocations(const ELFO *Obj) override;
void printProgramHeaders(const ELFO *Obj, bool PrintProgramHeaders,
cl::boolOrDefault PrintSectionMapping) override;
void printVersionSymbolSection(const ELFFile<ELFT> *Obj,
const Elf_Shdr *Sec) override;
void printVersionDefinitionSection(const ELFFile<ELFT> *Obj,
const Elf_Shdr *Sec) override;
void printVersionDependencySection(const ELFFile<ELFT> *Obj,
const Elf_Shdr *Sec) override;
void printHashHistogram(const ELFFile<ELFT> *Obj) override;
void printCGProfile(const ELFFile<ELFT> *Obj) override;
void printAddrsig(const ELFFile<ELFT> *Obj) override;
void printNotes(const ELFFile<ELFT> *Obj) override;
void printELFLinkerOptions(const ELFFile<ELFT> *Obj) override;
void printStackSizes(const ELFObjectFile<ELFT> *Obj) override;
void printStackSizeEntry(uint64_t Size, StringRef FuncName) override;
void printMipsGOT(const MipsGOTParser<ELFT> &Parser) override;
void printMipsPLT(const MipsGOTParser<ELFT> &Parser) override;
void printMipsABIFlags(const ELFObjectFile<ELFT> *Obj) override;
private:
void printRelocation(const ELFO *Obj, Elf_Rela Rel, const Elf_Shdr *SymTab);
void printDynamicRelocation(const ELFO *Obj, Elf_Rela Rel);
void printSymbols(const ELFO *Obj);
void printDynamicSymbols(const ELFO *Obj);
void printSymbol(const ELFO *Obj, const Elf_Sym *Symbol, const Elf_Sym *First,
StringRef StrTable, bool IsDynamic,
bool /*NonVisibilityBitsUsed*/) override;
void printProgramHeaders(const ELFO *Obj);
void printSectionMapping(const ELFO *Obj) {}
ScopedPrinter &W;
};
} // end anonymous namespace
namespace llvm {
template <class ELFT>
static std::error_code createELFDumper(const ELFObjectFile<ELFT> *Obj,
ScopedPrinter &Writer,
std::unique_ptr<ObjDumper> &Result) {
Result.reset(new ELFDumper<ELFT>(Obj, Writer));
return readobj_error::success;
}
std::error_code createELFDumper(const object::ObjectFile *Obj,
ScopedPrinter &Writer,
std::unique_ptr<ObjDumper> &Result) {
// Little-endian 32-bit
if (const ELF32LEObjectFile *ELFObj = dyn_cast<ELF32LEObjectFile>(Obj))
return createELFDumper(ELFObj, Writer, Result);
// Big-endian 32-bit
if (const ELF32BEObjectFile *ELFObj = dyn_cast<ELF32BEObjectFile>(Obj))
return createELFDumper(ELFObj, Writer, Result);
// Little-endian 64-bit
if (const ELF64LEObjectFile *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj))
return createELFDumper(ELFObj, Writer, Result);
// Big-endian 64-bit
if (const ELF64BEObjectFile *ELFObj = dyn_cast<ELF64BEObjectFile>(Obj))
return createELFDumper(ELFObj, Writer, Result);
return readobj_error::unsupported_obj_file_format;
}
} // end namespace llvm
// Iterate through the versions needed section, and place each Elf_Vernaux
// in the VersionMap according to its index.
template <class ELFT>
void ELFDumper<ELFT>::LoadVersionNeeds(const Elf_Shdr *Sec) const {
unsigned VerneedSize = Sec->sh_size; // Size of section in bytes
unsigned VerneedEntries = Sec->sh_info; // Number of Verneed entries
const uint8_t *VerneedStart = reinterpret_cast<const uint8_t *>(
ObjF->getELFFile()->base() + Sec->sh_offset);
const uint8_t *VerneedEnd = VerneedStart + VerneedSize;
// The first Verneed entry is at the start of the section.
const uint8_t *VerneedBuf = VerneedStart;
for (unsigned VerneedIndex = 0; VerneedIndex < VerneedEntries;
++VerneedIndex) {
if (VerneedBuf + sizeof(Elf_Verneed) > VerneedEnd)
report_fatal_error("Section ended unexpectedly while scanning "
"version needed records.");
const Elf_Verneed *Verneed =
reinterpret_cast<const Elf_Verneed *>(VerneedBuf);
if (Verneed->vn_version != ELF::VER_NEED_CURRENT)
report_fatal_error("Unexpected verneed version");
// Iterate through the Vernaux entries
const uint8_t *VernauxBuf = VerneedBuf + Verneed->vn_aux;
for (unsigned VernauxIndex = 0; VernauxIndex < Verneed->vn_cnt;
++VernauxIndex) {
if (VernauxBuf + sizeof(Elf_Vernaux) > VerneedEnd)
report_fatal_error("Section ended unexpected while scanning auxiliary "
"version needed records.");
const Elf_Vernaux *Vernaux =
reinterpret_cast<const Elf_Vernaux *>(VernauxBuf);
size_t Index = Vernaux->vna_other & ELF::VERSYM_VERSION;
if (Index >= VersionMap.size())
VersionMap.resize(Index + 1);
VersionMap[Index] = VersionMapEntry(Vernaux);
VernauxBuf += Vernaux->vna_next;
}
VerneedBuf += Verneed->vn_next;
}
}
// Iterate through the version definitions, and place each Elf_Verdef
// in the VersionMap according to its index.
template <class ELFT>
void ELFDumper<ELFT>::LoadVersionDefs(const Elf_Shdr *Sec) const {
unsigned VerdefSize = Sec->sh_size; // Size of section in bytes
unsigned VerdefEntries = Sec->sh_info; // Number of Verdef entries
const uint8_t *VerdefStart = reinterpret_cast<const uint8_t *>(
ObjF->getELFFile()->base() + Sec->sh_offset);
const uint8_t *VerdefEnd = VerdefStart + VerdefSize;
// The first Verdef entry is at the start of the section.
const uint8_t *VerdefBuf = VerdefStart;
for (unsigned VerdefIndex = 0; VerdefIndex < VerdefEntries; ++VerdefIndex) {
if (VerdefBuf + sizeof(Elf_Verdef) > VerdefEnd)
report_fatal_error("Section ended unexpectedly while scanning "
"version definitions.");
const Elf_Verdef *Verdef = reinterpret_cast<const Elf_Verdef *>(VerdefBuf);
if (Verdef->vd_version != ELF::VER_DEF_CURRENT)
report_fatal_error("Unexpected verdef version");
size_t Index = Verdef->vd_ndx & ELF::VERSYM_VERSION;
if (Index >= VersionMap.size())
VersionMap.resize(Index + 1);
VersionMap[Index] = VersionMapEntry(Verdef);
VerdefBuf += Verdef->vd_next;
}
}
template <class ELFT> void ELFDumper<ELFT>::LoadVersionMap() const {
// If there is no dynamic symtab or version table, there is nothing to do.
if (!DynSymRegion.Addr || !SymbolVersionSection)
return;
// Has the VersionMap already been loaded?
if (!VersionMap.empty())
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 (SymbolVersionDefSection)
LoadVersionDefs(SymbolVersionDefSection);
if (SymbolVersionNeedSection)
LoadVersionNeeds(SymbolVersionNeedSection);
}
template <typename ELFT>
StringRef ELFDumper<ELFT>::getSymbolVersion(StringRef StrTab,
const Elf_Sym *Sym,
bool &IsDefault) const {
// This is a dynamic symbol. Look in the GNU symbol version table.
if (!SymbolVersionSection) {
// No version table.
IsDefault = false;
return "";
}
// Determine the position in the symbol table of this entry.
size_t EntryIndex = (reinterpret_cast<uintptr_t>(Sym) -
reinterpret_cast<uintptr_t>(DynSymRegion.Addr)) /
sizeof(Elf_Sym);
// Get the corresponding version index entry.
const Elf_Versym *Versym = unwrapOrError(
ObjF->getFileName(), ObjF->getELFFile()->template getEntry<Elf_Versym>(
SymbolVersionSection, EntryIndex));
return this->getSymbolVersionByIndex(StrTab, Versym->vs_index, IsDefault);
}
static std::string maybeDemangle(StringRef Name) {
return opts::Demangle ? demangle(Name) : Name.str();
}
template <typename ELFT>
Expected<std::string>
ELFDumper<ELFT>::getStaticSymbolName(uint32_t Index) const {
const ELFFile<ELFT> *Obj = ObjF->getELFFile();
Expected<const typename ELFT::Sym *> SymOrErr =
Obj->getSymbol(DotSymtabSec, Index);
if (!SymOrErr)
return SymOrErr.takeError();
Expected<StringRef> StrTabOrErr = Obj->getStringTableForSymtab(*DotSymtabSec);
if (!StrTabOrErr)
return StrTabOrErr.takeError();
Expected<StringRef> NameOrErr = (*SymOrErr)->getName(*StrTabOrErr);
if (!NameOrErr)
return NameOrErr.takeError();
return maybeDemangle(*NameOrErr);
}
template <typename ELFT>
StringRef ELFDumper<ELFT>::getSymbolVersionByIndex(StringRef StrTab,
uint32_t SymbolVersionIndex,
bool &IsDefault) const {
size_t VersionIndex = SymbolVersionIndex & VERSYM_VERSION;
// Special markers for unversioned symbols.
if (VersionIndex == VER_NDX_LOCAL || VersionIndex == VER_NDX_GLOBAL) {
IsDefault = false;
return "";
}
// Lookup this symbol in the version table.
LoadVersionMap();
if (VersionIndex >= VersionMap.size() || VersionMap[VersionIndex].isNull())
reportError(createError("Invalid version entry"), ObjF->getFileName());
const VersionMapEntry &Entry = VersionMap[VersionIndex];
// Get the version name string.
size_t NameOffset;
if (Entry.isVerdef()) {
// The first Verdaux entry holds the name.
NameOffset = Entry.getVerdef()->getAux()->vda_name;
IsDefault = !(SymbolVersionIndex & VERSYM_HIDDEN);
} else {
NameOffset = Entry.getVernaux()->vna_name;
IsDefault = false;
}
if (NameOffset >= StrTab.size())
reportError(createError("Invalid string offset"), ObjF->getFileName());
return StrTab.data() + NameOffset;
}
template <typename ELFT>
std::string ELFDumper<ELFT>::getFullSymbolName(const Elf_Sym *Symbol,
StringRef StrTable,
bool IsDynamic) const {
std::string SymbolName = maybeDemangle(
unwrapOrError(ObjF->getFileName(), Symbol->getName(StrTable)));
if (SymbolName.empty() && Symbol->getType() == ELF::STT_SECTION) {
unsigned SectionIndex;
StringRef SectionName;
Elf_Sym_Range Syms = unwrapOrError(
ObjF->getFileName(), ObjF->getELFFile()->symbols(DotSymtabSec));
getSectionNameIndex(Symbol, Syms.begin(), SectionName, SectionIndex);
return SectionName;
}
if (!IsDynamic)
return SymbolName;
bool IsDefault;
StringRef Version = getSymbolVersion(StrTable, &*Symbol, IsDefault);
if (!Version.empty()) {
SymbolName += (IsDefault ? "@@" : "@");
SymbolName += Version;
}
return SymbolName;
}
template <typename ELFT>
void ELFDumper<ELFT>::getSectionNameIndex(const Elf_Sym *Symbol,
const Elf_Sym *FirstSym,
StringRef &SectionName,
unsigned &SectionIndex) const {
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 = unwrapOrError(ObjF->getFileName(),
object::getExtendedSymbolTableIndex<ELFT>(
Symbol, FirstSym, ShndxTable));
const ELFFile<ELFT> *Obj = ObjF->getELFFile();
const typename ELFT::Shdr *Sec =
unwrapOrError(ObjF->getFileName(), Obj->getSection(SectionIndex));
SectionName = unwrapOrError(ObjF->getFileName(), Obj->getSectionName(Sec));
}
}
template <class ELFO>
static const typename ELFO::Elf_Shdr *
findNotEmptySectionByAddress(const ELFO *Obj, StringRef FileName,
uint64_t Addr) {
for (const auto &Shdr : unwrapOrError(FileName, Obj->sections()))
if (Shdr.sh_addr == Addr && Shdr.sh_size > 0)
return &Shdr;
return nullptr;
}
template <class ELFO>
static const typename ELFO::Elf_Shdr *
findSectionByName(const ELFO &Obj, StringRef FileName, StringRef Name) {
for (const auto &Shdr : unwrapOrError(FileName, Obj.sections()))
if (Name == unwrapOrError(FileName, Obj.getSectionName(&Shdr)))
return &Shdr;
return nullptr;
}
static const EnumEntry<unsigned> ElfClass[] = {
{"None", "none", ELF::ELFCLASSNONE},
{"32-bit", "ELF32", ELF::ELFCLASS32},
{"64-bit", "ELF64", ELF::ELFCLASS64},
};
static const EnumEntry<unsigned> ElfDataEncoding[] = {
{"None", "none", ELF::ELFDATANONE},
{"LittleEndian", "2's complement, little endian", ELF::ELFDATA2LSB},
{"BigEndian", "2's complement, big endian", ELF::ELFDATA2MSB},
};
static const EnumEntry<unsigned> 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<unsigned> 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},
{"Standalone", "Standalone App", ELF::ELFOSABI_STANDALONE}
};
static const EnumEntry<unsigned> SymVersionFlags[] = {
{"Base", "BASE", VER_FLG_BASE},
{"Weak", "WEAK", VER_FLG_WEAK},
{"Info", "INFO", VER_FLG_INFO}};
static const EnumEntry<unsigned> AMDGPUElfOSABI[] = {
{"AMDGPU_HSA", "AMDGPU - HSA", ELF::ELFOSABI_AMDGPU_HSA},
{"AMDGPU_PAL", "AMDGPU - PAL", ELF::ELFOSABI_AMDGPU_PAL},
{"AMDGPU_MESA3D", "AMDGPU - MESA3D", ELF::ELFOSABI_AMDGPU_MESA3D}
};
static const EnumEntry<unsigned> ARMElfOSABI[] = {
{"ARM", "ARM", ELF::ELFOSABI_ARM}
};
static const EnumEntry<unsigned> C6000ElfOSABI[] = {
{"C6000_ELFABI", "Bare-metal C6000", ELF::ELFOSABI_C6000_ELFABI},
{"C6000_LINUX", "Linux C6000", ELF::ELFOSABI_C6000_LINUX}
};
static const EnumEntry<unsigned> 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 Corporation 32-bit microprocessor family"),
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_RISCV, "RISC-V"),
ENUM_ENT(EM_LANAI, "EM_LANAI"),
ENUM_ENT(EM_BPF, "EM_BPF"),
};
static const EnumEntry<unsigned> 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<unsigned> ElfSymbolVisibilities[] = {
{"DEFAULT", "DEFAULT", ELF::STV_DEFAULT},
{"INTERNAL", "INTERNAL", ELF::STV_INTERNAL},
{"HIDDEN", "HIDDEN", ELF::STV_HIDDEN},
{"PROTECTED", "PROTECTED", ELF::STV_PROTECTED}};
static const EnumEntry<unsigned> AMDGPUSymbolTypes[] = {
{ "AMDGPU_HSA_KERNEL", ELF::STT_AMDGPU_HSA_KERNEL }
};
static const char *getGroupType(uint32_t Flag) {
if (Flag & ELF::GRP_COMDAT)
return "COMDAT";
else
return "(unknown)";
}
static const EnumEntry<unsigned> 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<unsigned> ElfXCoreSectionFlags[] = {
LLVM_READOBJ_ENUM_ENT(ELF, XCORE_SHF_CP_SECTION),
LLVM_READOBJ_ENUM_ENT(ELF, XCORE_SHF_DP_SECTION)
};
static const EnumEntry<unsigned> ElfARMSectionFlags[] = {
LLVM_READOBJ_ENUM_ENT(ELF, SHF_ARM_PURECODE)
};
static const EnumEntry<unsigned> ElfHexagonSectionFlags[] = {
LLVM_READOBJ_ENUM_ENT(ELF, SHF_HEX_GPREL)
};
static const EnumEntry<unsigned> 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<unsigned> 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_ARM:
switch (Type) { LLVM_READOBJ_ENUM_CASE(ELF, PT_ARM_EXIDX); }
break;
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);
}
break;
}
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);
LLVM_READOBJ_ENUM_CASE(ELF, PT_OPENBSD_RANDOMIZE);
LLVM_READOBJ_ENUM_CASE(ELF, PT_OPENBSD_WXNEEDED);
LLVM_READOBJ_ENUM_CASE(ELF, PT_OPENBSD_BOOTDATA);
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_ARM:
if (Type == ELF::PT_ARM_EXIDX)
return "EXIDX";
break;
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";
}
break;
}
}
return std::string("<unknown>: ") + to_string(format_hex(Type, 1));
}
static const EnumEntry<unsigned> 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<unsigned> ElfHeaderMipsFlags[] = {
ENUM_ENT(EF_MIPS_NOREORDER, "noreorder"),
ENUM_ENT(EF_MIPS_PIC, "pic"),
ENUM_ENT(EF_MIPS_CPIC, "cpic"),
ENUM_ENT(EF_MIPS_ABI2, "abi2"),
ENUM_ENT(EF_MIPS_32BITMODE, "32bitmode"),
ENUM_ENT(EF_MIPS_FP64, "fp64"),
ENUM_ENT(EF_MIPS_NAN2008, "nan2008"),
ENUM_ENT(EF_MIPS_ABI_O32, "o32"),
ENUM_ENT(EF_MIPS_ABI_O64, "o64"),
ENUM_ENT(EF_MIPS_ABI_EABI32, "eabi32"),
ENUM_ENT(EF_MIPS_ABI_EABI64, "eabi64"),
ENUM_ENT(EF_MIPS_MACH_3900, "3900"),
ENUM_ENT(EF_MIPS_MACH_4010, "4010"),
ENUM_ENT(EF_MIPS_MACH_4100, "4100"),
ENUM_ENT(EF_MIPS_MACH_4650, "4650"),
ENUM_ENT(EF_MIPS_MACH_4120, "4120"),
ENUM_ENT(EF_MIPS_MACH_4111, "4111"),
ENUM_ENT(EF_MIPS_MACH_SB1, "sb1"),
ENUM_ENT(EF_MIPS_MACH_OCTEON, "octeon"),
ENUM_ENT(EF_MIPS_MACH_XLR, "xlr"),
ENUM_ENT(EF_MIPS_MACH_OCTEON2, "octeon2"),
ENUM_ENT(EF_MIPS_MACH_OCTEON3, "octeon3"),
ENUM_ENT(EF_MIPS_MACH_5400, "5400"),
ENUM_ENT(EF_MIPS_MACH_5900, "5900"),
ENUM_ENT(EF_MIPS_MACH_5500, "5500"),
ENUM_ENT(EF_MIPS_MACH_9000, "9000"),
ENUM_ENT(EF_MIPS_MACH_LS2E, "loongson-2e"),
ENUM_ENT(EF_MIPS_MACH_LS2F, "loongson-2f"),
ENUM_ENT(EF_MIPS_MACH_LS3A, "loongson-3a"),
ENUM_ENT(EF_MIPS_MICROMIPS, "micromips"),
ENUM_ENT(EF_MIPS_ARCH_ASE_M16, "mips16"),
ENUM_ENT(EF_MIPS_ARCH_ASE_MDMX, "mdmx"),
ENUM_ENT(EF_MIPS_ARCH_1, "mips1"),
ENUM_ENT(EF_MIPS_ARCH_2, "mips2"),
ENUM_ENT(EF_MIPS_ARCH_3, "mips3"),
ENUM_ENT(EF_MIPS_ARCH_4, "mips4"),
ENUM_ENT(EF_MIPS_ARCH_5, "mips5"),
ENUM_ENT(EF_MIPS_ARCH_32, "mips32"),
ENUM_ENT(EF_MIPS_ARCH_64, "mips64"),
ENUM_ENT(EF_MIPS_ARCH_32R2, "mips32r2"),
ENUM_ENT(EF_MIPS_ARCH_64R2, "mips64r2"),
ENUM_ENT(EF_MIPS_ARCH_32R6, "mips32r6"),
ENUM_ENT(EF_MIPS_ARCH_64R6, "mips64r6")
};
static const EnumEntry<unsigned> ElfHeaderAMDGPUFlags[] = {
LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_NONE),
LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_R600),
LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_R630),
LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_RS880),
LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_RV670),
LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_RV710),
LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_RV730),
LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_RV770),
LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_CEDAR),
LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_CYPRESS),
LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_JUNIPER),
LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_REDWOOD),
LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_SUMO),
LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_BARTS),
LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_CAICOS),
LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_CAYMAN),
LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_R600_TURKS),
LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX600),
LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX601),
LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX700),
LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX701),
LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX702),
LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX703),
LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX704),
LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX801),
LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX802),
LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX803),
LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX810),
LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX900),
LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX902),
LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX904),
LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX906),
LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX908),
LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX909),
LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX1010),
LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX1011),
LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_MACH_AMDGCN_GFX1012),
LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_XNACK),
LLVM_READOBJ_ENUM_ENT(ELF, EF_AMDGPU_SRAM_ECC)
};
static const EnumEntry<unsigned> ElfHeaderRISCVFlags[] = {
ENUM_ENT(EF_RISCV_RVC, "RVC"),
ENUM_ENT(EF_RISCV_FLOAT_ABI_SINGLE, "single-float ABI"),
ENUM_ENT(EF_RISCV_FLOAT_ABI_DOUBLE, "double-float ABI"),
ENUM_ENT(EF_RISCV_FLOAT_ABI_QUAD, "quad-float ABI"),
ENUM_ENT(EF_RISCV_RVE, "RVE")
};
static const EnumEntry<unsigned> 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<unsigned> 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<unsigned> 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 <typename ELFT>
std::pair<const typename ELFT::Phdr *, const typename ELFT::Shdr *>
ELFDumper<ELFT>::findDynamic(const ELFFile<ELFT> *Obj) {
// Try to locate the PT_DYNAMIC header.
const Elf_Phdr *DynamicPhdr = nullptr;
for (const Elf_Phdr &Phdr :
unwrapOrError(ObjF->getFileName(), Obj->program_headers())) {
if (Phdr.p_type != ELF::PT_DYNAMIC)
continue;
DynamicPhdr = &Phdr;
break;
}
// Try to locate the .dynamic section in the sections header table.
const Elf_Shdr *DynamicSec = nullptr;
for (const Elf_Shdr &Sec :
unwrapOrError(ObjF->getFileName(), Obj->sections())) {
if (Sec.sh_type != ELF::SHT_DYNAMIC)
continue;
DynamicSec = &Sec;
break;
}
if (DynamicPhdr && DynamicPhdr->p_offset + DynamicPhdr->p_filesz >
ObjF->getMemoryBufferRef().getBufferSize()) {
reportWarning(
createError(
"PT_DYNAMIC segment offset + size exceeds the size of the file"),
ObjF->getFileName());
// Don't use the broken dynamic header.
DynamicPhdr = nullptr;
}
if (DynamicPhdr && DynamicSec) {
StringRef Name =
unwrapOrError(ObjF->getFileName(), Obj->getSectionName(DynamicSec));
if (DynamicSec->sh_addr + DynamicSec->sh_size >
DynamicPhdr->p_vaddr + DynamicPhdr->p_memsz ||
DynamicSec->sh_addr < DynamicPhdr->p_vaddr)
reportWarning(createError("The SHT_DYNAMIC section '" + Name +
"' is not contained within the "
"PT_DYNAMIC segment"),
ObjF->getFileName());
if (DynamicSec->sh_addr != DynamicPhdr->p_vaddr)
reportWarning(createError("The SHT_DYNAMIC section '" + Name +
"' is not at the start of "
"PT_DYNAMIC segment"),
ObjF->getFileName());
}
return std::make_pair(DynamicPhdr, DynamicSec);
}
template <typename ELFT>
void ELFDumper<ELFT>::loadDynamicTable(const ELFFile<ELFT> *Obj) {
const Elf_Phdr *DynamicPhdr;
const Elf_Shdr *DynamicSec;
std::tie(DynamicPhdr, DynamicSec) = findDynamic(Obj);
if (!DynamicPhdr && !DynamicSec)
return;
DynRegionInfo FromPhdr(ObjF->getFileName());
bool IsPhdrTableValid = false;
if (DynamicPhdr) {
FromPhdr = createDRIFrom(DynamicPhdr, sizeof(Elf_Dyn));
IsPhdrTableValid = !FromPhdr.getAsArrayRef<Elf_Dyn>().empty();
}
// Locate the dynamic table described in a section header.
// Ignore sh_entsize and use the expected value for entry size explicitly.
// This allows us to dump dynamic sections with a broken sh_entsize
// field.
DynRegionInfo FromSec(ObjF->getFileName());
bool IsSecTableValid = false;
if (DynamicSec) {
FromSec =
checkDRI({ObjF->getELFFile()->base() + DynamicSec->sh_offset,
DynamicSec->sh_size, sizeof(Elf_Dyn), ObjF->getFileName()});
IsSecTableValid = !FromSec.getAsArrayRef<Elf_Dyn>().empty();
}
// When we only have information from one of the SHT_DYNAMIC section header or
// PT_DYNAMIC program header, just use that.
if (!DynamicPhdr || !DynamicSec) {
if ((DynamicPhdr && IsPhdrTableValid) || (DynamicSec && IsSecTableValid)) {
DynamicTable = DynamicPhdr ? FromPhdr : FromSec;
parseDynamicTable();
} else {
reportWarning(createError("no valid dynamic table was found"),
ObjF->getFileName());
}
return;
}
// At this point we have tables found from the section header and from the
// dynamic segment. Usually they match, but we have to do sanity checks to
// verify that.
if (FromPhdr.Addr != FromSec.Addr)
reportWarning(createError("SHT_DYNAMIC section header and PT_DYNAMIC "
"program header disagree about "
"the location of the dynamic table"),
ObjF->getFileName());
if (!IsPhdrTableValid && !IsSecTableValid) {
reportWarning(createError("no valid dynamic table was found"),
ObjF->getFileName());
return;
}
// Information in the PT_DYNAMIC program header has priority over the information
// in a section header.
if (IsPhdrTableValid) {
if (!IsSecTableValid)
reportWarning(
createError(
"SHT_DYNAMIC dynamic table is invalid: PT_DYNAMIC will be used"),
ObjF->getFileName());
DynamicTable = FromPhdr;
} else {
reportWarning(
createError(
"PT_DYNAMIC dynamic table is invalid: SHT_DYNAMIC will be used"),
ObjF->getFileName());
DynamicTable = FromSec;
}
parseDynamicTable();
}
template <typename ELFT>
ELFDumper<ELFT>::ELFDumper(const object::ELFObjectFile<ELFT> *ObjF,
ScopedPrinter &Writer)
: ObjDumper(Writer), ObjF(ObjF), DynRelRegion(ObjF->getFileName()),
DynRelaRegion(ObjF->getFileName()), DynRelrRegion(ObjF->getFileName()),
DynPLTRelRegion(ObjF->getFileName()), DynSymRegion(ObjF->getFileName()),
DynamicTable(ObjF->getFileName()) {
const ELFFile<ELFT> *Obj = ObjF->getELFFile();
for (const Elf_Shdr &Sec :
unwrapOrError(ObjF->getFileName(), Obj->sections())) {
switch (Sec.sh_type) {
case ELF::SHT_SYMTAB:
if (!DotSymtabSec)
DotSymtabSec = &Sec;
break;
case ELF::SHT_DYNSYM:
if (!DynSymRegion.Size) {
DynSymRegion = createDRIFrom(&Sec);
// This is only used (if Elf_Shdr present)for naming section in GNU
// style
DynSymtabName =
unwrapOrError(ObjF->getFileName(), Obj->getSectionName(&Sec));
if (Expected<StringRef> E = Obj->getStringTableForSymtab(Sec))
DynamicStringTable = *E;
else
reportWarning(E.takeError(), ObjF->getFileName());
}
break;
case ELF::SHT_SYMTAB_SHNDX:
ShndxTable = unwrapOrError(ObjF->getFileName(), Obj->getSHNDXTable(Sec));
break;
case ELF::SHT_GNU_versym:
if (!SymbolVersionSection)
SymbolVersionSection = &Sec;
break;
case ELF::SHT_GNU_verdef:
if (!SymbolVersionDefSection)
SymbolVersionDefSection = &Sec;
break;
case ELF::SHT_GNU_verneed:
if (!SymbolVersionNeedSection)
SymbolVersionNeedSection = &Sec;
break;
case ELF::SHT_LLVM_CALL_GRAPH_PROFILE:
if (!DotCGProfileSec)
DotCGProfileSec = &Sec;
break;
case ELF::SHT_LLVM_ADDRSIG:
if (!DotAddrsigSec)
DotAddrsigSec = &Sec;
break;
}
}
loadDynamicTable(Obj);
if (opts::Output == opts::GNU)
ELFDumperStyle.reset(new GNUStyle<ELFT>(Writer, this));
else
ELFDumperStyle.reset(new LLVMStyle<ELFT>(Writer, this));
}
static const char *getTypeString(unsigned Arch, uint64_t Type) {
#define DYNAMIC_TAG(n, v)
switch (Arch) {
case EM_AARCH64:
switch (Type) {
#define AARCH64_DYNAMIC_TAG(name, value) \
case DT_##name: \
return #name;
#include "llvm/BinaryFormat/DynamicTags.def"
#undef AARCH64_DYNAMIC_TAG
}
break;
case EM_HEXAGON:
switch (Type) {
#define HEXAGON_DYNAMIC_TAG(name, value) \
case DT_##name: \
return #name;
#include "llvm/BinaryFormat/DynamicTags.def"
#undef HEXAGON_DYNAMIC_TAG
}
break;
case EM_MIPS:
switch (Type) {
#define MIPS_DYNAMIC_TAG(name, value) \
case DT_##name: \
return #name;
#include "llvm/BinaryFormat/DynamicTags.def"
#undef MIPS_DYNAMIC_TAG
}
break;
case EM_PPC64:
switch (Type) {
#define PPC64_DYNAMIC_TAG(name, value) \
case DT_##name: \
return #name;
#include "llvm/BinaryFormat/DynamicTags.def"
#undef PPC64_DYNAMIC_TAG
}
break;
}
#undef DYNAMIC_TAG
switch (Type) {
// Now handle all dynamic tags except the architecture specific ones
#define AARCH64_DYNAMIC_TAG(name, value)
#define MIPS_DYNAMIC_TAG(name, value)
#define HEXAGON_DYNAMIC_TAG(name, value)
#define PPC64_DYNAMIC_TAG(name, value)
// Also ignore marker tags such as DT_HIOS (maps to DT_VERNEEDNUM), etc.
#define DYNAMIC_TAG_MARKER(name, value)
#define DYNAMIC_TAG(name, value) \
case DT_##name: \
return #name;
#include "llvm/BinaryFormat/DynamicTags.def"
#undef DYNAMIC_TAG
#undef AARCH64_DYNAMIC_TAG
#undef MIPS_DYNAMIC_TAG
#undef HEXAGON_DYNAMIC_TAG
#undef PPC64_DYNAMIC_TAG
#undef DYNAMIC_TAG_MARKER
default:
return "unknown";
}
}
template <typename ELFT> void ELFDumper<ELFT>::parseDynamicTable() {
auto toMappedAddr = [&](uint64_t Tag, uint64_t VAddr) -> const uint8_t * {
auto MappedAddrOrError = ObjF->getELFFile()->toMappedAddr(VAddr);
if (!MappedAddrOrError) {
Error Err =
createError("Unable to parse DT_" +
Twine(getTypeString(
ObjF->getELFFile()->getHeader()->e_machine, Tag)) +
": " + llvm::toString(MappedAddrOrError.takeError()));
reportWarning(std::move(Err), ObjF->getFileName());
return nullptr;
}
return MappedAddrOrError.get();
};
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<const Elf_Hash *>(
toMappedAddr(Dyn.getTag(), Dyn.getPtr()));
break;
case ELF::DT_GNU_HASH:
GnuHashTable = reinterpret_cast<const Elf_GnuHash *>(
toMappedAddr(Dyn.getTag(), Dyn.getPtr()));
break;
case ELF::DT_STRTAB:
StringTableBegin = reinterpret_cast<const char *>(
toMappedAddr(Dyn.getTag(), Dyn.getPtr()));
break;
case ELF::DT_STRSZ:
StringTableSize = Dyn.getVal();
break;
case ELF::DT_SYMTAB: {
// Often we find the information about the dynamic symbol table
// location in the SHT_DYNSYM section header. However, the value in
// DT_SYMTAB has priority, because it is used by dynamic loaders to
// locate .dynsym at runtime. The location we find in the section header
// and the location we find here should match. If we can't map the
// DT_SYMTAB value to an address (e.g. when there are no program headers), we
// ignore its value.
if (const uint8_t *VA = toMappedAddr(Dyn.getTag(), Dyn.getPtr())) {
// EntSize is non-zero if the dynamic symbol table has been found via a
// section header.
if (DynSymRegion.EntSize && VA != DynSymRegion.Addr)
reportWarning(
createError(
"SHT_DYNSYM section header and DT_SYMTAB disagree about "
"the location of the dynamic symbol table"),
ObjF->getFileName());
DynSymRegion.Addr = VA;
DynSymRegion.EntSize = sizeof(Elf_Sym);
}
break;
}
case ELF::DT_RELA:
DynRelaRegion.Addr = toMappedAddr(Dyn.getTag(), 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.getTag(), Dyn.getPtr());
break;
case ELF::DT_RELSZ:
DynRelRegion.Size = Dyn.getVal();
break;
case ELF::DT_RELENT:
DynRelRegion.EntSize = Dyn.getVal();
break;
case ELF::DT_RELR:
case ELF::DT_ANDROID_RELR:
DynRelrRegion.Addr = toMappedAddr(Dyn.getTag(), Dyn.getPtr());
break;
case ELF::DT_RELRSZ:
case ELF::DT_ANDROID_RELRSZ:
DynRelrRegion.Size = Dyn.getVal();
break;
case ELF::DT_RELRENT:
case ELF::DT_ANDROID_RELRENT:
DynRelrRegion.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(createError(Twine("unknown DT_PLTREL value of ") +
Twine((uint64_t)Dyn.getVal())),
ObjF->getFileName());
break;
case ELF::DT_JMPREL:
DynPLTRelRegion.Addr = toMappedAddr(Dyn.getTag(), Dyn.getPtr());
break;
case ELF::DT_PLTRELSZ:
DynPLTRelRegion.Size = Dyn.getVal();
break;
}
}
if (StringTableBegin)
DynamicStringTable = StringRef(StringTableBegin, StringTableSize);
SOName = getDynamicString(SONameOffset);
}
template <typename ELFT>
typename ELFDumper<ELFT>::Elf_Rel_Range ELFDumper<ELFT>::dyn_rels() const {
return DynRelRegion.getAsArrayRef<Elf_Rel>();
}
template <typename ELFT>
typename ELFDumper<ELFT>::Elf_Rela_Range ELFDumper<ELFT>::dyn_relas() const {
return DynRelaRegion.getAsArrayRef<Elf_Rela>();
}
template <typename ELFT>
typename ELFDumper<ELFT>::Elf_Relr_Range ELFDumper<ELFT>::dyn_relrs() const {
return DynRelrRegion.getAsArrayRef<Elf_Relr>();
}
template <class ELFT> void ELFDumper<ELFT>::printFileHeaders() {
ELFDumperStyle->printFileHeaders(ObjF->getELFFile());
}
template <class ELFT> void ELFDumper<ELFT>::printSectionHeaders() {
ELFDumperStyle->printSectionHeaders(ObjF->getELFFile());
}
template <class ELFT> void ELFDumper<ELFT>::printRelocations() {
ELFDumperStyle->printRelocations(ObjF->getELFFile());
}
template <class ELFT>
void ELFDumper<ELFT>::printProgramHeaders(
bool PrintProgramHeaders, cl::boolOrDefault PrintSectionMapping) {
ELFDumperStyle->printProgramHeaders(ObjF->getELFFile(), PrintProgramHeaders,
PrintSectionMapping);
}
template <typename ELFT> void ELFDumper<ELFT>::printVersionInfo() {
// Dump version symbol section.
ELFDumperStyle->printVersionSymbolSection(ObjF->getELFFile(),
SymbolVersionSection);
// Dump version definition section.
ELFDumperStyle->printVersionDefinitionSection(ObjF->getELFFile(),
SymbolVersionDefSection);
// Dump version dependency section.
ELFDumperStyle->printVersionDependencySection(ObjF->getELFFile(),
SymbolVersionNeedSection);
}
template <class ELFT> void ELFDumper<ELFT>::printDynamicRelocations() {
ELFDumperStyle->printDynamicRelocations(ObjF->getELFFile());
}
template <class ELFT>
void ELFDumper<ELFT>::printSymbols(bool PrintSymbols,
bool PrintDynamicSymbols) {
ELFDumperStyle->printSymbols(ObjF->getELFFile(), PrintSymbols,
PrintDynamicSymbols);
}
template <class ELFT> void ELFDumper<ELFT>::printHashSymbols() {
ELFDumperStyle->printHashSymbols(ObjF->getELFFile());
}
template <class ELFT> void ELFDumper<ELFT>::printHashHistogram() {
ELFDumperStyle->printHashHistogram(ObjF->getELFFile());
}
template <class ELFT> void ELFDumper<ELFT>::printCGProfile() {
ELFDumperStyle->printCGProfile(ObjF->getELFFile());
}
template <class ELFT> void ELFDumper<ELFT>::printNotes() {
ELFDumperStyle->printNotes(ObjF->getELFFile());
}
template <class ELFT> void ELFDumper<ELFT>::printELFLinkerOptions() {
ELFDumperStyle->printELFLinkerOptions(ObjF->getELFFile());
}
template <class ELFT> void ELFDumper<ELFT>::printStackSizes() {
ELFDumperStyle->printStackSizes(ObjF);
}
#define LLVM_READOBJ_DT_FLAG_ENT(prefix, enum) \
{ #enum, prefix##_##enum }
static const EnumEntry<unsigned> 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<unsigned> 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, DISPRELPND),
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<unsigned> 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 <typename T, typename TFlag>
void printFlags(T Value, ArrayRef<EnumEntry<TFlag>> Flags, raw_ostream &OS) {
using FlagEntry = EnumEntry<TFlag>;
using FlagVector = SmallVector<FlagEntry, 10>;
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 <class ELFT>
void ELFDumper<ELFT>::printDynamicEntry(raw_ostream &OS, uint64_t Type,
uint64_t Value) const {
const char *ConvChar =
(opts::Output == opts::GNU) ? "0x%" PRIx64 : "0x%" PRIX64;
// Handle custom printing of architecture specific tags
switch (ObjF->getELFFile()->getHeader()->e_machine) {
case EM_AARCH64:
switch (Type) {
case DT_AARCH64_BTI_PLT:
case DT_AARCH64_PAC_PLT:
OS << Value;
return;
default:
break;
}
break;
case EM_HEXAGON:
switch (Type) {
case DT_HEXAGON_VER:
OS << Value;
return;
case DT_HEXAGON_SYMSZ:
case DT_HEXAGON_PLT:
OS << format(ConvChar, Value);
return;
default:
break;
}
break;
case EM_MIPS:
switch (Type) {
case DT_MIPS_RLD_VERSION:
case DT_MIPS_LOCAL_GOTNO:
case DT_MIPS_SYMTABNO:
case DT_MIPS_UNREFEXTNO:
OS << Value;
return;
case DT_MIPS_TIME_STAMP:
case DT_MIPS_ICHECKSUM:
case DT_MIPS_IVERSION:
case DT_MIPS_BASE_ADDRESS:
case DT_MIPS_MSYM:
case DT_MIPS_CONFLICT:
case DT_MIPS_LIBLIST:
case DT_MIPS_CONFLICTNO:
case DT_MIPS_LIBLISTNO:
case DT_MIPS_GOTSYM:
case DT_MIPS_HIPAGENO:
case DT_MIPS_RLD_MAP:
case DT_MIPS_DELTA_CLASS:
case DT_MIPS_DELTA_CLASS_NO:
case DT_MIPS_DELTA_INSTANCE:
case DT_MIPS_DELTA_RELOC:
case DT_MIPS_DELTA_RELOC_NO:
case DT_MIPS_DELTA_SYM:
case DT_MIPS_DELTA_SYM_NO:
case DT_MIPS_DELTA_CLASSSYM:
case DT_MIPS_DELTA_CLASSSYM_NO:
case DT_MIPS_CXX_FLAGS:
case DT_MIPS_PIXIE_INIT:
case DT_MIPS_SYMBOL_LIB:
case DT_MIPS_LOCALPAGE_GOTIDX:
case DT_MIPS_LOCAL_GOTIDX:
case DT_MIPS_HIDDEN_GOTIDX:
case DT_MIPS_PROTECTED_GOTIDX:
case DT_MIPS_OPTIONS:
case DT_MIPS_INTERFACE:
case DT_MIPS_DYNSTR_ALIGN:
case DT_MIPS_INTERFACE_SIZE:
case DT_MIPS_RLD_TEXT_RESOLVE_ADDR:
case DT_MIPS_PERF_SUFFIX:
case DT_MIPS_COMPACT_SIZE:
case DT_MIPS_GP_VALUE:
case DT_MIPS_AUX_DYNAMIC:
case DT_MIPS_PLTGOT:
case DT_MIPS_RWPLT:
case DT_MIPS_RLD_MAP_REL:
OS << format(ConvChar, Value);
return;
case DT_MIPS_FLAGS:
printFlags(Value, makeArrayRef(ElfDynamicDTMipsFlags), OS);
return;
default:
break;
}
break;
default:
break;
}
switch (Type) {
case DT_PLTREL:
if (Value == DT_REL) {
OS << "REL";
break;
} else if (Value == DT_RELA) {
OS << "RELA";
break;
}
LLVM_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:
OS << format(ConvChar, Value);
break;
case DT_RELACOUNT:
case DT_RELCOUNT:
case DT_VERDEFNUM:
case DT_VERNEEDNUM:
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:
case DT_ANDROID_RELSZ:
case DT_ANDROID_RELASZ:
OS << Value << " (bytes)";
break;
case DT_NEEDED:
case DT_SONAME:
case DT_AUXILIARY:
case DT_USED:
case DT_FILTER:
case DT_RPATH:
case DT_RUNPATH: {
const std::map<uint64_t, const char*> TagNames = {
{DT_NEEDED, "Shared library"},
{DT_SONAME, "Library soname"},
{DT_AUXILIARY, "Auxiliary library"},
{DT_USED, "Not needed object"},
{DT_FILTER, "Filter library"},
{DT_RPATH, "Library rpath"},
{DT_RUNPATH, "Library runpath"},
};
OS << TagNames.at(Type) << ": [" << getDynamicString(Value) << "]";
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 <class ELFT>
std::string ELFDumper<ELFT>::getDynamicString(uint64_t Value) const {
if (DynamicStringTable.empty())
return "<String table is empty or was not found>";
if (Value < DynamicStringTable.size())
return DynamicStringTable.data() + Value;
return Twine("<Invalid offset 0x" + utohexstr(Value) + ">").str();
}
template <class ELFT> void ELFDumper<ELFT>::printUnwindInfo() {
DwarfCFIEH::PrinterContext<ELFT> Ctx(W, ObjF);
Ctx.printUnwindInformation();
}
namespace {
template <> void ELFDumper<ELF32LE>::printUnwindInfo() {
const ELFFile<ELF32LE> *Obj = ObjF->getELFFile();
const unsigned Machine = Obj->getHeader()->e_machine;
if (Machine == EM_ARM) {
ARM::EHABI::PrinterContext<ELF32LE> Ctx(W, Obj, ObjF->getFileName(),
DotSymtabSec);
Ctx.PrintUnwindInformation();
}
DwarfCFIEH::PrinterContext<ELF32LE> Ctx(W, ObjF);
Ctx.printUnwindInformation();
}
} // end anonymous namespace
template <class ELFT> void ELFDumper<ELFT>::printDynamicTable() {
ELFDumperStyle->printDynamic(ObjF->getELFFile());
}
template <class ELFT> void ELFDumper<ELFT>::printNeededLibraries() {
ListScope D(W, "NeededLibraries");
std::vector<std::string> Libs;
for (const auto &Entry : dynamic_table())
if (Entry.d_tag == ELF::DT_NEEDED)
Libs.push_back(getDynamicString(Entry.d_un.d_val));
llvm::stable_sort(Libs);
for (const auto &L : Libs)
W.startLine() << L << "\n";
}
template <typename ELFT> void ELFDumper<ELFT>::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 <typename ELFT> void ELFDumper<ELFT>::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(createError("No dynamic symbol section"), ObjF->getFileName());
W.printHexList("Values", GnuHashTable->values(NumSyms));
}
template <typename ELFT> void ELFDumper<ELFT>::printLoadName() {
W.printString("LoadName", SOName);
}
template <class ELFT> void ELFDumper<ELFT>::printArchSpecificInfo() {
const ELFFile<ELFT> *Obj = ObjF->getELFFile();
switch (Obj->getHeader()->e_machine) {
case EM_ARM:
printAttributes();
break;
case EM_MIPS: {
ELFDumperStyle->printMipsABIFlags(ObjF);
printMipsOptions();
printMipsReginfo();
MipsGOTParser<ELFT> Parser(Obj, ObjF->getFileName(), dynamic_table(),
dynamic_symbols());
if (Parser.hasGot())
ELFDumperStyle->printMipsGOT(Parser);
if (Parser.hasPlt())
ELFDumperStyle->printMipsPLT(Parser);
break;
}
default:
break;
}
}
template <class ELFT> void ELFDumper<ELFT>::printAttributes() {
W.startLine() << "Attributes not implemented.\n";
}
namespace {
template <> void ELFDumper<ELF32LE>::printAttributes() {
const ELFFile<ELF32LE> *Obj = ObjF->getELFFile();
if (Obj->getHeader()->e_machine != EM_ARM) {
W.startLine() << "Attributes not implemented.\n";
return;
}
DictScope BA(W, "BuildAttributes");
for (const ELFO::Elf_Shdr &Sec :
unwrapOrError(ObjF->getFileName(), Obj->sections())) {
if (Sec.sh_type != ELF::SHT_ARM_ATTRIBUTES)
continue;
ArrayRef<uint8_t> Contents =
unwrapOrError(ObjF->getFileName(), Obj->getSectionContents(&Sec));
if (Contents[0] != ARMBuildAttrs::Format_Version) {
errs() << "unrecognised FormatVersion: 0x"
<< Twine::utohexstr(Contents[0]) << '\n';
continue;
}
W.printHex("FormatVersion", Contents[0]);
if (Contents.size() == 1)
continue;
ARMAttributeParser(&W).Parse(Contents, true);
}
}
template <class ELFT> class MipsGOTParser {
public:
TYPEDEF_ELF_TYPES(ELFT)
using Entry = typename ELFO::Elf_Addr;
using Entries = ArrayRef<Entry>;
const bool IsStatic;
const ELFO * const Obj;
MipsGOTParser(const ELFO *Obj, StringRef FileName, Elf_Dyn_Range DynTable,
Elf_Sym_Range DynSyms);
bool hasGot() const { return !GotEntries.empty(); }
bool hasPlt() const { return !PltEntries.empty(); }
uint64_t getGp() const;
const Entry *getGotLazyResolver() const;
const Entry *getGotModulePointer() const;
const Entry *getPltLazyResolver() const;
const Entry *getPltModulePointer() const;
Entries getLocalEntries() const;
Entries getGlobalEntries() const;
Entries getOtherEntries() const;
Entries getPltEntries() const;
uint64_t getGotAddress(const Entry * E) const;
int64_t getGotOffset(const Entry * E) const;
const Elf_Sym *getGotSym(const Entry *E) const;
uint64_t getPltAddress(const Entry * E) const;
const Elf_Sym *getPltSym(const Entry *E) const;
StringRef getPltStrTable() const { return PltStrTable; }
private:
const Elf_Shdr *GotSec;
size_t LocalNum;
size_t GlobalNum;
const Elf_Shdr *PltSec;
const Elf_Shdr *PltRelSec;
const Elf_Shdr *PltSymTable;
StringRef FileName;
Elf_Sym_Range GotDynSyms;
StringRef PltStrTable;
Entries GotEntries;
Entries PltEntries;
};
} // end anonymous namespace
template <class ELFT>
MipsGOTParser<ELFT>::MipsGOTParser(const ELFO *Obj, StringRef FileName,
Elf_Dyn_Range DynTable,
Elf_Sym_Range DynSyms)
: IsStatic(DynTable.empty()), Obj(Obj), GotSec(nullptr), LocalNum(0),
GlobalNum(0), PltSec(nullptr), PltRelSec(nullptr), PltSymTable(nullptr),
FileName(FileName) {
// 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
// Find static GOT secton.
if (IsStatic) {
GotSec = findSectionByName(*Obj, FileName, ".got");
if (!GotSec)
return;
ArrayRef<uint8_t> Content =
unwrapOrError(FileName, Obj->getSectionContents(GotSec));