typedef iterator_range<const Elf_Sym *> Elf_Sym_Range;
+ const uint8_t *base() const {
+ return reinterpret_cast<const uint8_t *>(Buf.data());
+ }
+
private:
typedef SmallVector<const Elf_Shdr *, 2> Sections_t;
typedef DenseMap<unsigned, unsigned> IndexMap_t;
StringRef Buf;
- const uint8_t *base() const {
- return reinterpret_cast<const uint8_t *>(Buf.data());
- }
-
const Elf_Ehdr *Header;
const Elf_Shdr *SectionHeaderTable = nullptr;
StringRef DotShstrtab; // Section header string table.
StringRef DotStrtab; // Symbol header string table.
const Elf_Shdr *dot_symtab_sec = nullptr; // Symbol table section.
const Elf_Shdr *DotDynSymSec = nullptr; // Dynamic symbol table section.
- const Elf_Hash *HashTable = nullptr;
const Elf_Shdr *SymbolTableSectionHeaderIndex = nullptr;
DenseMap<const Elf_Sym *, ELF::Elf64_Word> ExtendedSymbolTable;
const Elf_Shdr *dot_gnu_version_r_sec = nullptr; // .gnu.version_r
const Elf_Shdr *dot_gnu_version_d_sec = nullptr; // .gnu.version_d
- /// \brief Represents a region described by entries in the .dynamic table.
- struct DynRegionInfo {
- DynRegionInfo() : Addr(nullptr), Size(0), EntSize(0) {}
- /// \brief Address in current address space.
- const void *Addr;
- /// \brief Size in bytes of the region.
- uintX_t Size;
- /// \brief Size of each entity in the region.
- uintX_t EntSize;
- };
-
- DynRegionInfo DynamicRegion;
- DynRegionInfo DynStrRegion;
- DynRegionInfo DynRelaRegion;
-
- // SONAME entry in dynamic string table
- StringRef DTSoname;
-
// 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> {
void LoadVersionNeeds(const Elf_Shdr *ec) const;
void LoadVersionMap() const;
- void scanDynamicTable();
-
public:
template<typename T>
const T *getEntry(uint32_t Section, uint32_t Entry) const;
const Elf_Shdr *getDotSymtabSec() const { return dot_symtab_sec; }
const Elf_Shdr *getDotDynSymSec() const { return DotDynSymSec; }
- const Elf_Hash *getHashTable() const { return HashTable; }
- StringRef getDynamicStringTable() const {
- return StringRef((const char *)DynStrRegion.Addr, DynStrRegion.Size);
- }
ErrorOr<StringRef> getStringTable(const Elf_Shdr *Section) const;
ErrorOr<StringRef> getStringTableForSymtab(const Elf_Shdr &Section) const;
- const char *getDynamicString(uintX_t Offset) const;
- ErrorOr<StringRef> getSymbolVersion(const Elf_Sym *Symb,
+ ErrorOr<StringRef> getSymbolVersion(StringRef StrTab, const Elf_Sym *Symb,
bool &IsDefault) const;
void VerifyStrTab(const Elf_Shdr *sh) const;
return make_range(symbol_begin(), symbol_end());
}
- const Elf_Dyn *dynamic_table_begin() const;
- const Elf_Dyn *dynamic_table_end() const;
- Elf_Dyn_Range dynamic_table() const {
- return make_range(dynamic_table_begin(), dynamic_table_end());
- }
-
const Elf_Sym *dynamic_symbol_begin() const {
if (!DotDynSymSec)
return nullptr;
return make_range(dynamic_symbol_begin(), dynamic_symbol_end());
}
- const Elf_Rela *dyn_rela_begin() const {
- if (DynRelaRegion.Size && DynRelaRegion.EntSize != sizeof(Elf_Rela))
- report_fatal_error("Invalid relocation entry size");
- return reinterpret_cast<const Elf_Rela *>(DynRelaRegion.Addr);
- }
-
- const Elf_Rela *dyn_rela_end() const {
- uint64_t Size = DynRelaRegion.Size;
- if (Size % sizeof(Elf_Rela))
- report_fatal_error("Invalid relocation table size");
- return dyn_rela_begin() + Size / sizeof(Elf_Rela);
- }
-
typedef iterator_range<const Elf_Rela *> Elf_Rela_Range;
- Elf_Rela_Range dyn_relas() const {
- return make_range(dyn_rela_begin(), dyn_rela_end());
- }
-
const Elf_Rela *rela_begin(const Elf_Shdr *sec) const {
if (sec->sh_entsize != sizeof(Elf_Rela))
report_fatal_error("Invalid relocation entry size");
ErrorOr<StringRef> getSectionName(const Elf_Shdr *Section) const;
ErrorOr<ArrayRef<uint8_t> > getSectionContents(const Elf_Shdr *Sec) const;
- StringRef getLoadName() const;
};
typedef ELFFile<ELFType<support::little, false>> ELF32LEFile;
Header = reinterpret_cast<const Elf_Ehdr *>(base());
- if (Header->e_shoff == 0) {
- scanDynamicTable();
+ if (Header->e_shoff == 0)
return;
- }
const uint64_t SectionTableOffset = Header->e_shoff;
for (const Elf_Shdr &Sec : sections()) {
switch (Sec.sh_type) {
- case ELF::SHT_HASH:
- if (HashTable) {
- EC = object_error::parse_failed;
- return;
- }
- HashTable = reinterpret_cast<const Elf_Hash *>(base() + Sec.sh_offset);
- break;
case ELF::SHT_SYMTAB_SHNDX:
if (SymbolTableSectionHeaderIndex) {
// More than one .symtab_shndx!
}
}
- scanDynamicTable();
-
EC = std::error_code();
}
return VAddr < Phdr->p_vaddr;
}
-template <class ELFT> void ELFFile<ELFT>::scanDynamicTable() {
- SmallVector<const Elf_Phdr *, 4> LoadSegments;
- for (const Elf_Phdr &Phdr : program_headers()) {
- if (Phdr.p_type == ELF::PT_DYNAMIC) {
- DynamicRegion.Addr = base() + Phdr.p_offset;
- DynamicRegion.Size = Phdr.p_filesz;
- continue;
- }
- if (Phdr.p_type != ELF::PT_LOAD || Phdr.p_filesz == 0)
- continue;
- LoadSegments.push_back(&Phdr);
- }
-
- auto toMappedAddr = [&](uint64_t VAddr) -> const uint8_t * {
- const Elf_Phdr **I = std::upper_bound(
- LoadSegments.begin(), LoadSegments.end(), VAddr, compareAddr<ELFT>);
- 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 this->base() + Phdr.p_offset + Delta;
- };
-
- uint64_t SONameOffset = 0;
- for (const Elf_Dyn &Dyn : dynamic_table()) {
- switch (Dyn.d_tag) {
- case ELF::DT_HASH:
- if (HashTable)
- continue;
- HashTable =
- reinterpret_cast<const Elf_Hash *>(toMappedAddr(Dyn.getPtr()));
- break;
- case ELF::DT_STRTAB:
- if (!DynStrRegion.Addr)
- DynStrRegion.Addr = toMappedAddr(Dyn.getPtr());
- break;
- case ELF::DT_STRSZ:
- if (!DynStrRegion.Size)
- DynStrRegion.Size = Dyn.getVal();
- break;
- case ELF::DT_RELA:
- if (!DynRelaRegion.Addr)
- 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;
- }
- }
- if (SONameOffset)
- DTSoname = getDynamicString(SONameOffset);
-}
-
template <class ELFT>
const typename ELFFile<ELFT>::Elf_Shdr *ELFFile<ELFT>::section_begin() const {
if (Header->e_shentsize != sizeof(Elf_Shdr))
}
template <class ELFT>
-const typename ELFFile<ELFT>::Elf_Dyn *
-ELFFile<ELFT>::dynamic_table_begin() const {
- return reinterpret_cast<const Elf_Dyn *>(DynamicRegion.Addr);
-}
-
-template <class ELFT>
-const typename ELFFile<ELFT>::Elf_Dyn *
-ELFFile<ELFT>::dynamic_table_end() const {
- uint64_t Size = DynamicRegion.Size;
- if (Size % sizeof(Elf_Dyn))
- report_fatal_error("Invalid dynamic table size");
-
- return dynamic_table_begin() + Size / sizeof(Elf_Dyn);
-}
-
-template <class ELFT>
-StringRef ELFFile<ELFT>::getLoadName() const {
- return DTSoname;
-}
-
-template <class ELFT>
template <typename T>
const T *ELFFile<ELFT>::getEntry(uint32_t Section, uint32_t Entry) const {
ErrorOr<const Elf_Shdr *> Sec = getSection(Section);
}
template <class ELFT>
-const char *ELFFile<ELFT>::getDynamicString(uintX_t Offset) const {
- if (Offset >= DynStrRegion.Size)
- return nullptr;
- return (const char *)DynStrRegion.Addr + Offset;
-}
-
-template <class ELFT>
ErrorOr<StringRef>
ELFFile<ELFT>::getSectionName(const Elf_Shdr *Section) const {
uint32_t Offset = Section->sh_name;
}
template <class ELFT>
-ErrorOr<StringRef> ELFFile<ELFT>::getSymbolVersion(const Elf_Sym *symb,
+ErrorOr<StringRef> ELFFile<ELFT>::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) {
IsDefault = false;
}
- if (name_offset >= DynStrRegion.Size)
+ if (name_offset >= StrTab.size())
return object_error::parse_failed;
- return StringRef(getDynamicString(name_offset));
+ return StringRef(StrTab.data() + name_offset);
}
/// This function returns the hash value for a symbol in the .dynsym section
template<typename ELFT>
class ELFDumper : public ObjDumper {
public:
- ELFDumper(const ELFFile<ELFT> *Obj, StreamWriter &Writer)
- : ObjDumper(Writer), Obj(Obj) {}
+ ELFDumper(const ELFFile<ELFT> *Obj, StreamWriter &Writer);
void printFileHeaders() override;
void printSections() override;
typedef ELFFile<ELFT> 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_Rela Elf_Rela;
+ typedef typename ELFO::Elf_Rela_Range Elf_Rela_Range;
+ typedef typename ELFO::Elf_Phdr Elf_Phdr;
+ typedef typename ELFO::Elf_Hash Elf_Hash;
+ typedef typename ELFO::uintX_t uintX_t;
+
+ /// \brief Represents a region described by entries in the .dynamic table.
+ struct DynRegionInfo {
+ DynRegionInfo() : Addr(nullptr), Size(0), EntSize(0) {}
+ /// \brief Address in current address space.
+ const void *Addr;
+ /// \brief Size in bytes of the region.
+ uintX_t Size;
+ /// \brief Size of each entity in the region.
+ uintX_t EntSize;
+ };
void printSymbol(const Elf_Sym *Symbol, StringRef StrTable, bool IsDynamic);
void printRelocation(const Elf_Shdr *Sec, typename ELFO::Elf_Rela Rel);
void printValue(uint64_t Type, uint64_t Value);
+ const Elf_Rela *dyn_rela_begin() const;
+ const Elf_Rela *dyn_rela_end() const;
+ Elf_Rela_Range dyn_relas() const;
+ StringRef getDynamicString(uint64_t Offset) const;
+ const Elf_Dyn *dynamic_table_begin() const;
+ const Elf_Dyn *dynamic_table_end() const;
+ Elf_Dyn_Range dynamic_table() const {
+ return make_range(dynamic_table_begin(), dynamic_table_end());
+ }
+
const ELFO *Obj;
+ DynRegionInfo DynRelaRegion;
+ DynRegionInfo DynamicRegion;
+ StringRef DynamicStringTable;
+ StringRef SOName;
+ const Elf_Hash *HashTable = nullptr;
};
template <class T> T errorOrDefault(ErrorOr<T> Val, T Default = T()) {
std::string FullSymbolName(SymbolName);
bool IsDefault;
- ErrorOr<StringRef> Version = Obj.getSymbolVersion(&*Symbol, IsDefault);
+ ErrorOr<StringRef> Version =
+ Obj.getSymbolVersion(StrTable, &*Symbol, IsDefault);
if (Version) {
FullSymbolName += (IsDefault ? "@@" : "@");
FullSymbolName += *Version;
LLVM_READOBJ_ENUM_ENT(ELF, EF_MIPS_ARCH_64R6)
};
+template <typename ELFT>
+ELFDumper<ELFT>::ELFDumper(const ELFFile<ELFT> *Obj, StreamWriter &Writer)
+ : ObjDumper(Writer), Obj(Obj) {
+
+ SmallVector<const Elf_Phdr *, 4> LoadSegments;
+ for (const Elf_Phdr &Phdr : Obj->program_headers()) {
+ if (Phdr.p_type == ELF::PT_DYNAMIC) {
+ DynamicRegion.Addr = Obj->base() + Phdr.p_offset;
+ uint64_t Size = Phdr.p_filesz;
+ if (Size % sizeof(Elf_Dyn))
+ report_fatal_error("Invalid dynamic table size");
+ DynamicRegion.Size = Phdr.p_filesz;
+ continue;
+ }
+ if (Phdr.p_type != ELF::PT_LOAD || Phdr.p_filesz == 0)
+ continue;
+ LoadSegments.push_back(&Phdr);
+ }
+
+ auto toMappedAddr = [&](uint64_t VAddr) -> const uint8_t * {
+ const Elf_Phdr **I = std::upper_bound(
+ LoadSegments.begin(), LoadSegments.end(), VAddr, compareAddr<ELFT>);
+ 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<const Elf_Hash *>(toMappedAddr(Dyn.getPtr()));
+ 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_STRTAB:
+ StringTableBegin = (const char *)toMappedAddr(Dyn.getPtr());
+ break;
+ case ELF::DT_STRSZ:
+ StringTableSize = Dyn.getVal();
+ break;
+ }
+ }
+ if (StringTableBegin)
+ DynamicStringTable = StringRef(StringTableBegin, StringTableSize);
+ if (SONameOffset)
+ SOName = getDynamicString(SONameOffset);
+}
+
+template <typename ELFT>
+const typename ELFDumper<ELFT>::Elf_Rela *
+ELFDumper<ELFT>::dyn_rela_begin() const {
+ if (DynRelaRegion.Size && DynRelaRegion.EntSize != sizeof(Elf_Rela))
+ report_fatal_error("Invalid relocation entry size");
+ return reinterpret_cast<const Elf_Rela *>(DynRelaRegion.Addr);
+}
+
+template <typename ELFT>
+const typename ELFDumper<ELFT>::Elf_Rela *
+ELFDumper<ELFT>::dyn_rela_end() const {
+ uint64_t Size = DynRelaRegion.Size;
+ if (Size % sizeof(Elf_Rela))
+ report_fatal_error("Invalid relocation table size");
+ return dyn_rela_begin() + Size / sizeof(Elf_Rela);
+}
+
+template <typename ELFT>
+typename ELFDumper<ELFT>::Elf_Rela_Range ELFDumper<ELFT>::dyn_relas() const {
+ return make_range(dyn_rela_begin(), dyn_rela_end());
+}
+
+template <typename ELFT>
+const typename ELFDumper<ELFT>::Elf_Dyn *
+ELFDumper<ELFT>::dynamic_table_begin() const {
+ return reinterpret_cast<const Elf_Dyn *>(DynamicRegion.Addr);
+}
+
+template <typename ELFT>
+const typename ELFDumper<ELFT>::Elf_Dyn *
+ELFDumper<ELFT>::dynamic_table_end() const {
+ uint64_t Size = DynamicRegion.Size;
+ return dynamic_table_begin() + Size / sizeof(Elf_Dyn);
+}
+
template<class ELFT>
void ELFDumper<ELFT>::printFileHeaders() {
const typename ELFO::Elf_Ehdr *Header = Obj->getHeader();
void ELFDumper<ELFT>::printDynamicRelocations() {
W.startLine() << "Dynamic Relocations {\n";
W.indent();
- StringRef StringTable = Obj->getDynamicStringTable();
- for (const typename ELFO::Elf_Rela &Rel : Obj->dyn_relas()) {
+ for (const typename ELFO::Elf_Rela &Rel : dyn_relas()) {
SmallString<32> RelocName;
Obj->getRelocationTypeName(Rel.getType(Obj->isMips64EL()), RelocName);
StringRef SymbolName;
uint32_t SymIndex = Rel.getSymbol(Obj->isMips64EL());
const typename ELFO::Elf_Sym *Sym = Obj->dynamic_symbol_begin() + SymIndex;
- SymbolName = errorOrDefault(Sym->getName(StringTable));
+ SymbolName = errorOrDefault(Sym->getName(DynamicStringTable));
if (opts::ExpandRelocs) {
DictScope Group(W, "Relocation");
W.printHex("Offset", Rel.r_offset);
}
template <class ELFT>
-static const char *getDynamicString(const ELFFile<ELFT> &O, uint64_t Value) {
- const char *Ret = O.getDynamicString(Value);
- if (!Ret)
+StringRef ELFDumper<ELFT>::getDynamicString(uint64_t Value) const {
+ if (Value >= DynamicStringTable.size())
reportError("Invalid dynamic string table reference");
- return Ret;
+ return StringRef(DynamicStringTable.data() + Value);
}
template <class ELFT>
OS << Value << " (bytes)";
break;
case DT_NEEDED:
- OS << "SharedLibrary (" << getDynamicString(*Obj, Value) << ")";
+ OS << "SharedLibrary (" << getDynamicString(Value) << ")";
break;
case DT_SONAME:
- OS << "LibrarySoname (" << getDynamicString(*Obj, Value) << ")";
+ OS << "LibrarySoname (" << getDynamicString(Value) << ")";
break;
case DT_RPATH:
case DT_RUNPATH:
- OS << getDynamicString(*Obj, Value);
+ OS << getDynamicString(Value);
break;
case DT_MIPS_FLAGS:
printFlags(Value, makeArrayRef(ElfDynamicDTMipsFlags), OS);
template<class ELFT>
void ELFDumper<ELFT>::printDynamicTable() {
- auto I = Obj->dynamic_table_begin();
- auto E = Obj->dynamic_table_end();
+ auto I = dynamic_table_begin();
+ auto E = dynamic_table_end();
if (I == E)
return;
typedef std::vector<StringRef> LibsTy;
LibsTy Libs;
- for (const auto &Entry : Obj->dynamic_table())
+ for (const auto &Entry : dynamic_table())
if (Entry.d_tag == ELF::DT_NEEDED)
- Libs.push_back(getDynamicString(*Obj, Entry.d_un.d_val));
+ Libs.push_back(getDynamicString(Entry.d_un.d_val));
std::stable_sort(Libs.begin(), Libs.end());
template <typename ELFT>
void ELFDumper<ELFT>::printHashTable() {
DictScope D(W, "HashTable");
- auto HT = Obj->getHashTable();
- if (!HT)
+ if (!HashTable)
return;
- W.printNumber("Num Buckets", HT->nbucket);
- W.printNumber("Num Chains", HT->nchain);
- W.printList("Buckets", HT->buckets());
- W.printList("Chains", HT->chains());
+ 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>::printLoadName() {
- outs() << "LoadName: " << Obj->getLoadName() << '\n';
+ outs() << "LoadName: " << SOName << '\n';
}
template <class ELFT>
typedef object::ELFFile<ELFT> ObjectFile;
typedef typename ObjectFile::Elf_Shdr Elf_Shdr;
typedef typename ObjectFile::Elf_Sym Elf_Sym;
+ typedef typename ObjectFile::Elf_Dyn_Range Elf_Dyn_Range;
- MipsGOTParser(const ObjectFile *Obj, StreamWriter &W);
+ MipsGOTParser(const ObjectFile *Obj, Elf_Dyn_Range DynTable, StreamWriter &W);
void parseGOT();
void parsePLT();
}
template <class ELFT>
-MipsGOTParser<ELFT>::MipsGOTParser(const ObjectFile *Obj, StreamWriter &W)
+MipsGOTParser<ELFT>::MipsGOTParser(const ObjectFile *Obj,
+ Elf_Dyn_Range DynTable, StreamWriter &W)
: Obj(Obj), W(W) {
- for (const auto &Entry : Obj->dynamic_table()) {
+ for (const auto &Entry : DynTable) {
switch (Entry.getTag()) {
case ELF::DT_PLTGOT:
DtPltGot = Entry.getVal();
return;
}
- MipsGOTParser<ELFT> GOTParser(Obj, W);
+ MipsGOTParser<ELFT> GOTParser(Obj, dynamic_table(), W);
GOTParser.parseGOT();
GOTParser.parsePLT();
}