template <class ELFT>
class ELFDumper {
+ typedef object::Elf_Sym_Impl<ELFT> Elf_Sym;
typedef typename object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
- typedef typename object::ELFFile<ELFT>::Elf_Sym_Iter Elf_Sym_Iter;
+ typedef typename object::ELFFile<ELFT>::Elf_Word Elf_Word;
const object::ELFFile<ELFT> &Obj;
+ ArrayRef<Elf_Word> ShndxTable;
- error_code dumpSymbol(Elf_Sym_Iter Sym, ELFYAML::Symbol &S);
- error_code dumpCommonSection(const Elf_Shdr *Shdr, ELFYAML::Section &S);
+ std::error_code dumpSymbol(const Elf_Sym *Sym, const Elf_Shdr *SymTab,
+ StringRef StrTable, ELFYAML::Symbol &S);
+ std::error_code dumpCommonSection(const Elf_Shdr *Shdr, ELFYAML::Section &S);
+ std::error_code dumpCommonRelocationSection(const Elf_Shdr *Shdr,
+ ELFYAML::RelocationSection &S);
template <class RelT>
- error_code dumpRelocation(const Elf_Shdr *Shdr, const RelT *Rel,
- ELFYAML::Relocation &R);
+ std::error_code dumpRelocation(const RelT *Rel, const Elf_Shdr *SymTab,
+ ELFYAML::Relocation &R);
ErrorOr<ELFYAML::RelocationSection *> dumpRelSection(const Elf_Shdr *Shdr);
ErrorOr<ELFYAML::RelocationSection *> dumpRelaSection(const Elf_Shdr *Shdr);
ErrorOr<ELFYAML::RawContentSection *>
dumpContentSection(const Elf_Shdr *Shdr);
+ ErrorOr<ELFYAML::NoBitsSection *> dumpNoBitsSection(const Elf_Shdr *Shdr);
+ ErrorOr<ELFYAML::Group *> dumpGroup(const Elf_Shdr *Shdr);
+ ErrorOr<ELFYAML::MipsABIFlags *> dumpMipsABIFlags(const Elf_Shdr *Shdr);
public:
ELFDumper(const object::ELFFile<ELFT> &O);
Y->Header.Flags = Obj.getHeader()->e_flags;
Y->Header.Entry = Obj.getHeader()->e_entry;
+ const Elf_Shdr *Symtab = nullptr;
+
// Dump sections
for (const Elf_Shdr &Sec : Obj.sections()) {
switch (Sec.sh_type) {
case ELF::SHT_NULL:
- case ELF::SHT_SYMTAB:
case ELF::SHT_DYNSYM:
case ELF::SHT_STRTAB:
// Do not dump these sections.
break;
+ case ELF::SHT_SYMTAB:
+ Symtab = &Sec;
+ break;
+ case ELF::SHT_SYMTAB_SHNDX: {
+ ErrorOr<ArrayRef<Elf_Word>> TableOrErr = Obj.getSHNDXTable(Sec);
+ if (std::error_code EC = TableOrErr.getError())
+ return EC;
+ ShndxTable = *TableOrErr;
+ break;
+ }
case ELF::SHT_RELA: {
ErrorOr<ELFYAML::RelocationSection *> S = dumpRelaSection(&Sec);
- if (error_code EC = S.getError())
+ if (std::error_code EC = S.getError())
return EC;
Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(S.get()));
break;
}
case ELF::SHT_REL: {
ErrorOr<ELFYAML::RelocationSection *> S = dumpRelSection(&Sec);
- if (error_code EC = S.getError())
+ if (std::error_code EC = S.getError())
+ return EC;
+ Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(S.get()));
+ break;
+ }
+ case ELF::SHT_GROUP: {
+ ErrorOr<ELFYAML::Group *> G = dumpGroup(&Sec);
+ if (std::error_code EC = G.getError())
+ return EC;
+ Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(G.get()));
+ break;
+ }
+ case ELF::SHT_MIPS_ABIFLAGS: {
+ ErrorOr<ELFYAML::MipsABIFlags *> G = dumpMipsABIFlags(&Sec);
+ if (std::error_code EC = G.getError())
+ return EC;
+ Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(G.get()));
+ break;
+ }
+ case ELF::SHT_NOBITS: {
+ ErrorOr<ELFYAML::NoBitsSection *> S = dumpNoBitsSection(&Sec);
+ if (std::error_code EC = S.getError())
return EC;
Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(S.get()));
break;
}
default: {
ErrorOr<ELFYAML::RawContentSection *> S = dumpContentSection(&Sec);
- if (error_code EC = S.getError())
+ if (std::error_code EC = S.getError())
return EC;
Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(S.get()));
}
}
// Dump symbols
+ ErrorOr<StringRef> StrTableOrErr = Obj.getStringTableForSymtab(*Symtab);
+ if (std::error_code EC = StrTableOrErr.getError())
+ return EC;
+ StringRef StrTable = *StrTableOrErr;
+
bool IsFirstSym = true;
- for (auto SI = Obj.begin_symbols(), SE = Obj.end_symbols(); SI != SE; ++SI) {
+ for (const Elf_Sym &Sym : Obj.symbols(Symtab)) {
if (IsFirstSym) {
IsFirstSym = false;
continue;
}
ELFYAML::Symbol S;
- if (error_code EC = ELFDumper<ELFT>::dumpSymbol(SI, S))
+ if (std::error_code EC =
+ ELFDumper<ELFT>::dumpSymbol(&Sym, Symtab, StrTable, S))
return EC;
- switch (SI->getBinding())
+ switch (Sym.getBinding())
{
case ELF::STB_LOCAL:
Y->Symbols.Local.push_back(S);
}
template <class ELFT>
-error_code ELFDumper<ELFT>::dumpSymbol(Elf_Sym_Iter Sym, ELFYAML::Symbol &S) {
+std::error_code
+ELFDumper<ELFT>::dumpSymbol(const Elf_Sym *Sym, const Elf_Shdr *SymTab,
+ StringRef StrTable, ELFYAML::Symbol &S) {
S.Type = Sym->getType();
S.Value = Sym->st_value;
S.Size = Sym->st_size;
+ S.Other = Sym->st_other;
- ErrorOr<StringRef> NameOrErr = Obj.getSymbolName(Sym);
- if (error_code EC = NameOrErr.getError())
+ ErrorOr<StringRef> NameOrErr = Sym->getName(StrTable);
+ if (std::error_code EC = NameOrErr.getError())
return EC;
S.Name = NameOrErr.get();
- const Elf_Shdr *Shdr = Obj.getSection(&*Sym);
+ ErrorOr<const Elf_Shdr *> ShdrOrErr = Obj.getSection(Sym, SymTab, ShndxTable);
+ if (std::error_code EC = ShdrOrErr.getError())
+ return EC;
+ const Elf_Shdr *Shdr = *ShdrOrErr;
if (!Shdr)
return obj2yaml_error::success;
NameOrErr = Obj.getSectionName(Shdr);
- if (error_code EC = NameOrErr.getError())
+ if (std::error_code EC = NameOrErr.getError())
return EC;
S.Section = NameOrErr.get();
template <class ELFT>
template <class RelT>
-error_code ELFDumper<ELFT>::dumpRelocation(const Elf_Shdr *Shdr,
- const RelT *Rel,
- ELFYAML::Relocation &R) {
+std::error_code ELFDumper<ELFT>::dumpRelocation(const RelT *Rel,
+ const Elf_Shdr *SymTab,
+ ELFYAML::Relocation &R) {
R.Type = Rel->getType(Obj.isMips64EL());
R.Offset = Rel->r_offset;
R.Addend = 0;
- auto NamePair = Obj.getRelocationSymbol(Shdr, Rel);
- if (!NamePair.first)
- return obj2yaml_error::success;
+ const Elf_Sym *Sym = Obj.getRelocationSymbol(Rel, SymTab);
+ ErrorOr<const Elf_Shdr *> StrTabSec = Obj.getSection(SymTab->sh_link);
+ if (std::error_code EC = StrTabSec.getError())
+ return EC;
+ ErrorOr<StringRef> StrTabOrErr = Obj.getStringTable(*StrTabSec);
+ if (std::error_code EC = StrTabOrErr.getError())
+ return EC;
+ StringRef StrTab = *StrTabOrErr;
- ErrorOr<StringRef> NameOrErr =
- Obj.getSymbolName(NamePair.first, NamePair.second);
- if (error_code EC = NameOrErr.getError())
+ ErrorOr<StringRef> NameOrErr = Sym->getName(StrTab);
+ if (std::error_code EC = NameOrErr.getError())
return EC;
R.Symbol = NameOrErr.get();
}
template <class ELFT>
-error_code ELFDumper<ELFT>::dumpCommonSection(const Elf_Shdr *Shdr,
- ELFYAML::Section &S) {
+std::error_code ELFDumper<ELFT>::dumpCommonSection(const Elf_Shdr *Shdr,
+ ELFYAML::Section &S) {
S.Type = Shdr->sh_type;
S.Flags = Shdr->sh_flags;
S.Address = Shdr->sh_addr;
S.AddressAlign = Shdr->sh_addralign;
ErrorOr<StringRef> NameOrErr = Obj.getSectionName(Shdr);
- if (error_code EC = NameOrErr.getError())
+ if (std::error_code EC = NameOrErr.getError())
return EC;
S.Name = NameOrErr.get();
if (Shdr->sh_link != ELF::SHN_UNDEF) {
- if (const Elf_Shdr *LinkSection = Obj.getSection(Shdr->sh_link)) {
- NameOrErr = Obj.getSectionName(LinkSection);
- if (error_code EC = NameOrErr.getError())
- return EC;
- S.Link = NameOrErr.get();
- }
- }
- if (Shdr->sh_info != ELF::SHN_UNDEF) {
- if (const Elf_Shdr *InfoSection = Obj.getSection(Shdr->sh_info)) {
- NameOrErr = Obj.getSectionName(InfoSection);
- if (error_code EC = NameOrErr.getError())
- return EC;
- S.Info = NameOrErr.get();
- }
+ ErrorOr<const Elf_Shdr *> LinkSection = Obj.getSection(Shdr->sh_link);
+ if (std::error_code EC = LinkSection.getError())
+ return EC;
+ NameOrErr = Obj.getSectionName(*LinkSection);
+ if (std::error_code EC = NameOrErr.getError())
+ return EC;
+ S.Link = NameOrErr.get();
}
+
+ return obj2yaml_error::success;
+}
+
+template <class ELFT>
+std::error_code
+ELFDumper<ELFT>::dumpCommonRelocationSection(const Elf_Shdr *Shdr,
+ ELFYAML::RelocationSection &S) {
+ if (std::error_code EC = dumpCommonSection(Shdr, S))
+ return EC;
+
+ ErrorOr<const Elf_Shdr *> InfoSection = Obj.getSection(Shdr->sh_info);
+ if (std::error_code EC = InfoSection.getError())
+ return EC;
+
+ ErrorOr<StringRef> NameOrErr = Obj.getSectionName(*InfoSection);
+ if (std::error_code EC = NameOrErr.getError())
+ return EC;
+ S.Info = NameOrErr.get();
+
return obj2yaml_error::success;
}
assert(Shdr->sh_type == ELF::SHT_REL && "Section type is not SHT_REL");
auto S = make_unique<ELFYAML::RelocationSection>();
- if (error_code EC = dumpCommonSection(Shdr, *S))
+ if (std::error_code EC = dumpCommonRelocationSection(Shdr, *S))
return EC;
- for (auto RI = Obj.begin_rel(Shdr), RE = Obj.end_rel(Shdr); RI != RE;
- ++RI) {
+ ErrorOr<const Elf_Shdr *> SymTabOrErr = Obj.getSection(Shdr->sh_link);
+ if (std::error_code EC = SymTabOrErr.getError())
+ return EC;
+ const Elf_Shdr *SymTab = *SymTabOrErr;
+
+ for (auto RI = Obj.rel_begin(Shdr), RE = Obj.rel_end(Shdr); RI != RE; ++RI) {
ELFYAML::Relocation R;
- if (error_code EC = dumpRelocation(Shdr, &*RI, R))
+ if (std::error_code EC = dumpRelocation(&*RI, SymTab, R))
return EC;
S->Relocations.push_back(R);
}
assert(Shdr->sh_type == ELF::SHT_RELA && "Section type is not SHT_RELA");
auto S = make_unique<ELFYAML::RelocationSection>();
- if (error_code EC = dumpCommonSection(Shdr, *S))
+ if (std::error_code EC = dumpCommonRelocationSection(Shdr, *S))
return EC;
- for (auto RI = Obj.begin_rela(Shdr), RE = Obj.end_rela(Shdr); RI != RE;
+ ErrorOr<const Elf_Shdr *> SymTabOrErr = Obj.getSection(Shdr->sh_link);
+ if (std::error_code EC = SymTabOrErr.getError())
+ return EC;
+ const Elf_Shdr *SymTab = *SymTabOrErr;
+
+ for (auto RI = Obj.rela_begin(Shdr), RE = Obj.rela_end(Shdr); RI != RE;
++RI) {
ELFYAML::Relocation R;
- if (error_code EC = dumpRelocation(Shdr, &*RI, R))
+ if (std::error_code EC = dumpRelocation(&*RI, SymTab, R))
return EC;
R.Addend = RI->r_addend;
S->Relocations.push_back(R);
ELFDumper<ELFT>::dumpContentSection(const Elf_Shdr *Shdr) {
auto S = make_unique<ELFYAML::RawContentSection>();
- if (error_code EC = dumpCommonSection(Shdr, *S))
+ if (std::error_code EC = dumpCommonSection(Shdr, *S))
return EC;
ErrorOr<ArrayRef<uint8_t>> ContentOrErr = Obj.getSectionContents(Shdr);
- if (error_code EC = ContentOrErr.getError())
+ if (std::error_code EC = ContentOrErr.getError())
return EC;
- S->Content = object::yaml::BinaryRef(ContentOrErr.get());
+ S->Content = yaml::BinaryRef(ContentOrErr.get());
S->Size = S->Content.binary_size();
return S.release();
}
template <class ELFT>
-static error_code elf2yaml(raw_ostream &Out, const object::ELFFile<ELFT> &Obj) {
+ErrorOr<ELFYAML::NoBitsSection *>
+ELFDumper<ELFT>::dumpNoBitsSection(const Elf_Shdr *Shdr) {
+ auto S = make_unique<ELFYAML::NoBitsSection>();
+
+ if (std::error_code EC = dumpCommonSection(Shdr, *S))
+ return EC;
+ S->Size = Shdr->sh_size;
+
+ return S.release();
+}
+
+template <class ELFT>
+ErrorOr<ELFYAML::Group *> ELFDumper<ELFT>::dumpGroup(const Elf_Shdr *Shdr) {
+ auto S = make_unique<ELFYAML::Group>();
+
+ if (std::error_code EC = dumpCommonSection(Shdr, *S))
+ return EC;
+ // Get sh_info which is the signature.
+ ErrorOr<const Elf_Shdr *> SymtabOrErr = Obj.getSection(Shdr->sh_link);
+ if (std::error_code EC = SymtabOrErr.getError())
+ return EC;
+ const Elf_Shdr *Symtab = *SymtabOrErr;
+ const Elf_Sym *symbol = Obj.getSymbol(Symtab, Shdr->sh_info);
+ ErrorOr<StringRef> StrTabOrErr = Obj.getStringTableForSymtab(*Symtab);
+ if (std::error_code EC = StrTabOrErr.getError())
+ return EC;
+ StringRef StrTab = *StrTabOrErr;
+ auto sectionContents = Obj.getSectionContents(Shdr);
+ if (std::error_code ec = sectionContents.getError())
+ return ec;
+ ErrorOr<StringRef> symbolName = symbol->getName(StrTab);
+ if (std::error_code EC = symbolName.getError())
+ return EC;
+ S->Info = *symbolName;
+ const Elf_Word *groupMembers =
+ reinterpret_cast<const Elf_Word *>(sectionContents->data());
+ const long count = (Shdr->sh_size) / sizeof(Elf_Word);
+ ELFYAML::SectionOrType s;
+ for (int i = 0; i < count; i++) {
+ if (groupMembers[i] == llvm::ELF::GRP_COMDAT) {
+ s.sectionNameOrType = "GRP_COMDAT";
+ } else {
+ ErrorOr<const Elf_Shdr *> sHdr = Obj.getSection(groupMembers[i]);
+ if (std::error_code EC = sHdr.getError())
+ return EC;
+ ErrorOr<StringRef> sectionName = Obj.getSectionName(*sHdr);
+ if (std::error_code ec = sectionName.getError())
+ return ec;
+ s.sectionNameOrType = *sectionName;
+ }
+ S->Members.push_back(s);
+ }
+ return S.release();
+}
+
+template <class ELFT>
+ErrorOr<ELFYAML::MipsABIFlags *>
+ELFDumper<ELFT>::dumpMipsABIFlags(const Elf_Shdr *Shdr) {
+ assert(Shdr->sh_type == ELF::SHT_MIPS_ABIFLAGS &&
+ "Section type is not SHT_MIPS_ABIFLAGS");
+ auto S = make_unique<ELFYAML::MipsABIFlags>();
+ if (std::error_code EC = dumpCommonSection(Shdr, *S))
+ return EC;
+
+ ErrorOr<ArrayRef<uint8_t>> ContentOrErr = Obj.getSectionContents(Shdr);
+ if (std::error_code EC = ContentOrErr.getError())
+ return EC;
+
+ auto *Flags = reinterpret_cast<const object::Elf_Mips_ABIFlags<ELFT> *>(
+ ContentOrErr.get().data());
+ S->Version = Flags->version;
+ S->ISALevel = Flags->isa_level;
+ S->ISARevision = Flags->isa_rev;
+ S->GPRSize = Flags->gpr_size;
+ S->CPR1Size = Flags->cpr1_size;
+ S->CPR2Size = Flags->cpr2_size;
+ S->FpABI = Flags->fp_abi;
+ S->ISAExtension = Flags->isa_ext;
+ S->ASEs = Flags->ases;
+ S->Flags1 = Flags->flags1;
+ S->Flags2 = Flags->flags2;
+ return S.release();
+}
+
+template <class ELFT>
+static std::error_code elf2yaml(raw_ostream &Out,
+ const object::ELFFile<ELFT> &Obj) {
ELFDumper<ELFT> Dumper(Obj);
ErrorOr<ELFYAML::Object *> YAMLOrErr = Dumper.dump();
- if (error_code EC = YAMLOrErr.getError())
+ if (std::error_code EC = YAMLOrErr.getError())
return EC;
std::unique_ptr<ELFYAML::Object> YAML(YAMLOrErr.get());
yaml::Output Yout(Out);
Yout << *YAML;
- return object::object_error::success;
+ return std::error_code();
}
-error_code elf2yaml(raw_ostream &Out, const object::ObjectFile &Obj) {
+std::error_code elf2yaml(raw_ostream &Out, const object::ObjectFile &Obj) {
if (const auto *ELFObj = dyn_cast<object::ELF32LEObjectFile>(&Obj))
return elf2yaml(Out, *ELFObj->getELFFile());