2014-05-14 13:07:47 +08:00
|
|
|
//===------ utils/elf2yaml.cpp - obj2yaml conversion tool -------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Error.h"
|
|
|
|
#include "obj2yaml.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/Object/ELFObjectFile.h"
|
2016-03-02 03:15:06 +08:00
|
|
|
#include "llvm/ObjectYAML/ELFYAML.h"
|
2014-05-14 13:07:47 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/YAMLTraits.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
class ELFDumper {
|
2015-02-21 12:28:26 +08:00
|
|
|
typedef object::Elf_Sym_Impl<ELFT> Elf_Sym;
|
2014-05-14 13:07:47 +08:00
|
|
|
typedef typename object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
|
2015-02-21 12:28:26 +08:00
|
|
|
typedef typename object::ELFFile<ELFT>::Elf_Word Elf_Word;
|
2014-05-14 13:07:47 +08:00
|
|
|
|
|
|
|
const object::ELFFile<ELFT> &Obj;
|
2015-08-11 05:29:35 +08:00
|
|
|
ArrayRef<Elf_Word> ShndxTable;
|
2014-05-14 13:07:47 +08:00
|
|
|
|
2015-08-11 05:29:35 +08:00
|
|
|
std::error_code dumpSymbol(const Elf_Sym *Sym, const Elf_Shdr *SymTab,
|
|
|
|
StringRef StrTable, ELFYAML::Symbol &S);
|
2014-06-13 11:07:50 +08:00
|
|
|
std::error_code dumpCommonSection(const Elf_Shdr *Shdr, ELFYAML::Section &S);
|
|
|
|
std::error_code dumpCommonRelocationSection(const Elf_Shdr *Shdr,
|
|
|
|
ELFYAML::RelocationSection &S);
|
2014-05-14 13:07:47 +08:00
|
|
|
template <class RelT>
|
2015-09-02 23:07:39 +08:00
|
|
|
std::error_code dumpRelocation(const RelT *Rel, const Elf_Shdr *SymTab,
|
2014-06-13 11:07:50 +08:00
|
|
|
ELFYAML::Relocation &R);
|
2014-05-14 13:07:47 +08:00
|
|
|
|
|
|
|
ErrorOr<ELFYAML::RelocationSection *> dumpRelSection(const Elf_Shdr *Shdr);
|
|
|
|
ErrorOr<ELFYAML::RelocationSection *> dumpRelaSection(const Elf_Shdr *Shdr);
|
|
|
|
ErrorOr<ELFYAML::RawContentSection *>
|
|
|
|
dumpContentSection(const Elf_Shdr *Shdr);
|
2015-07-04 07:00:54 +08:00
|
|
|
ErrorOr<ELFYAML::NoBitsSection *> dumpNoBitsSection(const Elf_Shdr *Shdr);
|
2015-02-21 12:28:26 +08:00
|
|
|
ErrorOr<ELFYAML::Group *> dumpGroup(const Elf_Shdr *Shdr);
|
2015-05-07 23:40:48 +08:00
|
|
|
ErrorOr<ELFYAML::MipsABIFlags *> dumpMipsABIFlags(const Elf_Shdr *Shdr);
|
2014-05-14 13:07:47 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
ELFDumper(const object::ELFFile<ELFT> &O);
|
|
|
|
ErrorOr<ELFYAML::Object *> dump();
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
ELFDumper<ELFT>::ELFDumper(const object::ELFFile<ELFT> &O)
|
|
|
|
: Obj(O) {}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
ErrorOr<ELFYAML::Object *> ELFDumper<ELFT>::dump() {
|
|
|
|
auto Y = make_unique<ELFYAML::Object>();
|
|
|
|
|
|
|
|
// Dump header
|
|
|
|
Y->Header.Class = ELFYAML::ELF_ELFCLASS(Obj.getHeader()->getFileClass());
|
|
|
|
Y->Header.Data = ELFYAML::ELF_ELFDATA(Obj.getHeader()->getDataEncoding());
|
|
|
|
Y->Header.OSABI = Obj.getHeader()->e_ident[ELF::EI_OSABI];
|
|
|
|
Y->Header.Type = Obj.getHeader()->e_type;
|
|
|
|
Y->Header.Machine = Obj.getHeader()->e_machine;
|
|
|
|
Y->Header.Flags = Obj.getHeader()->e_flags;
|
|
|
|
Y->Header.Entry = Obj.getHeader()->e_entry;
|
|
|
|
|
2015-08-10 22:27:50 +08:00
|
|
|
const Elf_Shdr *Symtab = nullptr;
|
|
|
|
|
2014-05-14 13:07:47 +08:00
|
|
|
// Dump sections
|
|
|
|
for (const Elf_Shdr &Sec : Obj.sections()) {
|
|
|
|
switch (Sec.sh_type) {
|
|
|
|
case ELF::SHT_NULL:
|
|
|
|
case ELF::SHT_DYNSYM:
|
|
|
|
case ELF::SHT_STRTAB:
|
|
|
|
// Do not dump these sections.
|
|
|
|
break;
|
2015-08-10 22:27:50 +08:00
|
|
|
case ELF::SHT_SYMTAB:
|
|
|
|
Symtab = &Sec;
|
|
|
|
break;
|
2015-08-11 05:29:35 +08:00
|
|
|
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;
|
|
|
|
}
|
2014-05-14 13:07:47 +08:00
|
|
|
case ELF::SHT_RELA: {
|
|
|
|
ErrorOr<ELFYAML::RelocationSection *> S = dumpRelaSection(&Sec);
|
2014-06-13 11:07:50 +08:00
|
|
|
if (std::error_code EC = S.getError())
|
2014-05-14 13:07:47 +08:00
|
|
|
return EC;
|
|
|
|
Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(S.get()));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ELF::SHT_REL: {
|
|
|
|
ErrorOr<ELFYAML::RelocationSection *> S = dumpRelSection(&Sec);
|
2014-06-13 11:07:50 +08:00
|
|
|
if (std::error_code EC = S.getError())
|
2014-05-14 13:07:47 +08:00
|
|
|
return EC;
|
|
|
|
Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(S.get()));
|
|
|
|
break;
|
|
|
|
}
|
2015-02-21 12:28:26 +08:00
|
|
|
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;
|
|
|
|
}
|
2015-05-07 23:40:48 +08:00
|
|
|
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;
|
|
|
|
}
|
2015-07-04 07:00:54 +08:00
|
|
|
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;
|
|
|
|
}
|
2014-05-14 13:07:47 +08:00
|
|
|
default: {
|
|
|
|
ErrorOr<ELFYAML::RawContentSection *> S = dumpContentSection(&Sec);
|
2014-06-13 11:07:50 +08:00
|
|
|
if (std::error_code EC = S.getError())
|
2014-05-14 13:07:47 +08:00
|
|
|
return EC;
|
|
|
|
Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(S.get()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dump symbols
|
2015-07-22 02:04:29 +08:00
|
|
|
ErrorOr<StringRef> StrTableOrErr = Obj.getStringTableForSymtab(*Symtab);
|
|
|
|
if (std::error_code EC = StrTableOrErr.getError())
|
|
|
|
return EC;
|
|
|
|
StringRef StrTable = *StrTableOrErr;
|
|
|
|
|
2014-05-14 13:07:47 +08:00
|
|
|
bool IsFirstSym = true;
|
2015-08-10 22:27:50 +08:00
|
|
|
for (const Elf_Sym &Sym : Obj.symbols(Symtab)) {
|
2014-05-14 13:07:47 +08:00
|
|
|
if (IsFirstSym) {
|
|
|
|
IsFirstSym = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ELFYAML::Symbol S;
|
2015-08-11 05:29:35 +08:00
|
|
|
if (std::error_code EC =
|
|
|
|
ELFDumper<ELFT>::dumpSymbol(&Sym, Symtab, StrTable, S))
|
2014-05-14 13:07:47 +08:00
|
|
|
return EC;
|
|
|
|
|
2015-07-22 00:40:14 +08:00
|
|
|
switch (Sym.getBinding())
|
2014-05-14 13:07:47 +08:00
|
|
|
{
|
|
|
|
case ELF::STB_LOCAL:
|
|
|
|
Y->Symbols.Local.push_back(S);
|
|
|
|
break;
|
|
|
|
case ELF::STB_GLOBAL:
|
|
|
|
Y->Symbols.Global.push_back(S);
|
|
|
|
break;
|
|
|
|
case ELF::STB_WEAK:
|
|
|
|
Y->Symbols.Weak.push_back(S);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Unknown ELF symbol binding");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Y.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
2015-08-11 05:29:35 +08:00
|
|
|
std::error_code
|
|
|
|
ELFDumper<ELFT>::dumpSymbol(const Elf_Sym *Sym, const Elf_Shdr *SymTab,
|
|
|
|
StringRef StrTable, ELFYAML::Symbol &S) {
|
2014-05-14 13:07:47 +08:00
|
|
|
S.Type = Sym->getType();
|
|
|
|
S.Value = Sym->st_value;
|
|
|
|
S.Size = Sym->st_size;
|
2014-11-07 06:46:24 +08:00
|
|
|
S.Other = Sym->st_other;
|
2014-05-14 13:07:47 +08:00
|
|
|
|
2016-04-21 05:24:34 +08:00
|
|
|
Expected<StringRef> SymbolNameOrErr = Sym->getName(StrTable);
|
|
|
|
if (!SymbolNameOrErr)
|
|
|
|
return errorToErrorCode(SymbolNameOrErr.takeError());
|
|
|
|
S.Name = SymbolNameOrErr.get();
|
2014-05-14 13:07:47 +08:00
|
|
|
|
2015-08-11 05:29:35 +08:00
|
|
|
ErrorOr<const Elf_Shdr *> ShdrOrErr = Obj.getSection(Sym, SymTab, ShndxTable);
|
2015-07-01 20:56:27 +08:00
|
|
|
if (std::error_code EC = ShdrOrErr.getError())
|
|
|
|
return EC;
|
|
|
|
const Elf_Shdr *Shdr = *ShdrOrErr;
|
2014-05-14 13:07:47 +08:00
|
|
|
if (!Shdr)
|
|
|
|
return obj2yaml_error::success;
|
|
|
|
|
2016-04-21 05:24:34 +08:00
|
|
|
ErrorOr<StringRef> NameOrErr = Obj.getSectionName(Shdr);
|
2014-06-13 11:07:50 +08:00
|
|
|
if (std::error_code EC = NameOrErr.getError())
|
2014-05-14 13:07:47 +08:00
|
|
|
return EC;
|
|
|
|
S.Section = NameOrErr.get();
|
|
|
|
|
|
|
|
return obj2yaml_error::success;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
template <class RelT>
|
2015-09-02 23:07:39 +08:00
|
|
|
std::error_code ELFDumper<ELFT>::dumpRelocation(const RelT *Rel,
|
|
|
|
const Elf_Shdr *SymTab,
|
2014-06-13 11:07:50 +08:00
|
|
|
ELFYAML::Relocation &R) {
|
2014-05-14 13:07:47 +08:00
|
|
|
R.Type = Rel->getType(Obj.isMips64EL());
|
|
|
|
R.Offset = Rel->r_offset;
|
|
|
|
R.Addend = 0;
|
|
|
|
|
2015-09-02 23:07:39 +08:00
|
|
|
const Elf_Sym *Sym = Obj.getRelocationSymbol(Rel, SymTab);
|
2015-07-01 20:56:27 +08:00
|
|
|
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);
|
2015-06-29 22:39:25 +08:00
|
|
|
if (std::error_code EC = StrTabOrErr.getError())
|
|
|
|
return EC;
|
|
|
|
StringRef StrTab = *StrTabOrErr;
|
2015-06-29 20:38:31 +08:00
|
|
|
|
2016-04-21 05:24:34 +08:00
|
|
|
Expected<StringRef> NameOrErr = Sym->getName(StrTab);
|
|
|
|
if (!NameOrErr)
|
|
|
|
return errorToErrorCode(NameOrErr.takeError());
|
2014-05-14 13:07:47 +08:00
|
|
|
R.Symbol = NameOrErr.get();
|
|
|
|
|
|
|
|
return obj2yaml_error::success;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
2014-06-13 11:07:50 +08:00
|
|
|
std::error_code ELFDumper<ELFT>::dumpCommonSection(const Elf_Shdr *Shdr,
|
|
|
|
ELFYAML::Section &S) {
|
2014-05-14 13:07:47 +08:00
|
|
|
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);
|
2014-06-13 11:07:50 +08:00
|
|
|
if (std::error_code EC = NameOrErr.getError())
|
2014-05-14 13:07:47 +08:00
|
|
|
return EC;
|
|
|
|
S.Name = NameOrErr.get();
|
|
|
|
|
|
|
|
if (Shdr->sh_link != ELF::SHN_UNDEF) {
|
2015-07-01 20:56:27 +08:00
|
|
|
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();
|
2014-05-14 13:07:47 +08:00
|
|
|
}
|
2014-05-29 19:05:31 +08:00
|
|
|
|
|
|
|
return obj2yaml_error::success;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
2014-06-13 11:07:50 +08:00
|
|
|
std::error_code
|
2014-05-29 19:05:31 +08:00
|
|
|
ELFDumper<ELFT>::dumpCommonRelocationSection(const Elf_Shdr *Shdr,
|
|
|
|
ELFYAML::RelocationSection &S) {
|
2014-06-13 11:07:50 +08:00
|
|
|
if (std::error_code EC = dumpCommonSection(Shdr, S))
|
2014-05-29 19:05:31 +08:00
|
|
|
return EC;
|
|
|
|
|
2015-07-01 20:56:27 +08:00
|
|
|
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();
|
2014-05-29 19:05:31 +08:00
|
|
|
|
2014-05-14 13:07:47 +08:00
|
|
|
return obj2yaml_error::success;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
ErrorOr<ELFYAML::RelocationSection *>
|
|
|
|
ELFDumper<ELFT>::dumpRelSection(const Elf_Shdr *Shdr) {
|
|
|
|
assert(Shdr->sh_type == ELF::SHT_REL && "Section type is not SHT_REL");
|
|
|
|
auto S = make_unique<ELFYAML::RelocationSection>();
|
|
|
|
|
2014-06-13 11:07:50 +08:00
|
|
|
if (std::error_code EC = dumpCommonRelocationSection(Shdr, *S))
|
2014-05-14 13:07:47 +08:00
|
|
|
return EC;
|
|
|
|
|
2015-09-02 23:07:39 +08:00
|
|
|
ErrorOr<const Elf_Shdr *> SymTabOrErr = Obj.getSection(Shdr->sh_link);
|
|
|
|
if (std::error_code EC = SymTabOrErr.getError())
|
|
|
|
return EC;
|
|
|
|
const Elf_Shdr *SymTab = *SymTabOrErr;
|
|
|
|
|
2015-06-30 23:33:44 +08:00
|
|
|
for (auto RI = Obj.rel_begin(Shdr), RE = Obj.rel_end(Shdr); RI != RE; ++RI) {
|
2014-05-14 13:07:47 +08:00
|
|
|
ELFYAML::Relocation R;
|
2015-09-02 23:07:39 +08:00
|
|
|
if (std::error_code EC = dumpRelocation(&*RI, SymTab, R))
|
2014-05-14 13:07:47 +08:00
|
|
|
return EC;
|
|
|
|
S->Relocations.push_back(R);
|
|
|
|
}
|
|
|
|
|
|
|
|
return S.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
ErrorOr<ELFYAML::RelocationSection *>
|
|
|
|
ELFDumper<ELFT>::dumpRelaSection(const Elf_Shdr *Shdr) {
|
|
|
|
assert(Shdr->sh_type == ELF::SHT_RELA && "Section type is not SHT_RELA");
|
|
|
|
auto S = make_unique<ELFYAML::RelocationSection>();
|
|
|
|
|
2014-06-13 11:07:50 +08:00
|
|
|
if (std::error_code EC = dumpCommonRelocationSection(Shdr, *S))
|
2014-05-14 13:07:47 +08:00
|
|
|
return EC;
|
|
|
|
|
2015-09-02 23:07:39 +08:00
|
|
|
ErrorOr<const Elf_Shdr *> SymTabOrErr = Obj.getSection(Shdr->sh_link);
|
|
|
|
if (std::error_code EC = SymTabOrErr.getError())
|
|
|
|
return EC;
|
|
|
|
const Elf_Shdr *SymTab = *SymTabOrErr;
|
|
|
|
|
2015-06-30 23:33:44 +08:00
|
|
|
for (auto RI = Obj.rela_begin(Shdr), RE = Obj.rela_end(Shdr); RI != RE;
|
2014-05-14 13:07:47 +08:00
|
|
|
++RI) {
|
|
|
|
ELFYAML::Relocation R;
|
2015-09-02 23:07:39 +08:00
|
|
|
if (std::error_code EC = dumpRelocation(&*RI, SymTab, R))
|
2014-05-14 13:07:47 +08:00
|
|
|
return EC;
|
|
|
|
R.Addend = RI->r_addend;
|
|
|
|
S->Relocations.push_back(R);
|
|
|
|
}
|
|
|
|
|
|
|
|
return S.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
ErrorOr<ELFYAML::RawContentSection *>
|
|
|
|
ELFDumper<ELFT>::dumpContentSection(const Elf_Shdr *Shdr) {
|
|
|
|
auto S = make_unique<ELFYAML::RawContentSection>();
|
|
|
|
|
2014-06-13 11:07:50 +08:00
|
|
|
if (std::error_code EC = dumpCommonSection(Shdr, *S))
|
2014-05-14 13:07:47 +08:00
|
|
|
return EC;
|
|
|
|
|
|
|
|
ErrorOr<ArrayRef<uint8_t>> ContentOrErr = Obj.getSectionContents(Shdr);
|
2014-06-13 11:07:50 +08:00
|
|
|
if (std::error_code EC = ContentOrErr.getError())
|
2014-05-14 13:07:47 +08:00
|
|
|
return EC;
|
2014-07-03 10:01:39 +08:00
|
|
|
S->Content = yaml::BinaryRef(ContentOrErr.get());
|
2014-05-17 00:01:00 +08:00
|
|
|
S->Size = S->Content.binary_size();
|
2014-05-14 13:07:47 +08:00
|
|
|
|
|
|
|
return S.release();
|
|
|
|
}
|
|
|
|
|
2015-07-04 07:00:54 +08:00
|
|
|
template <class ELFT>
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2015-02-21 12:28:26 +08:00
|
|
|
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.
|
2015-07-23 20:49:40 +08:00
|
|
|
ErrorOr<const Elf_Shdr *> SymtabOrErr = Obj.getSection(Shdr->sh_link);
|
|
|
|
if (std::error_code EC = SymtabOrErr.getError())
|
2015-07-01 20:56:27 +08:00
|
|
|
return EC;
|
2015-07-23 20:49:40 +08:00
|
|
|
const Elf_Shdr *Symtab = *SymtabOrErr;
|
|
|
|
const Elf_Sym *symbol = Obj.getSymbol(Symtab, Shdr->sh_info);
|
2015-07-23 20:51:44 +08:00
|
|
|
ErrorOr<StringRef> StrTabOrErr = Obj.getStringTableForSymtab(*Symtab);
|
2015-06-29 22:39:25 +08:00
|
|
|
if (std::error_code EC = StrTabOrErr.getError())
|
|
|
|
return EC;
|
|
|
|
StringRef StrTab = *StrTabOrErr;
|
2015-02-21 12:28:26 +08:00
|
|
|
auto sectionContents = Obj.getSectionContents(Shdr);
|
|
|
|
if (std::error_code ec = sectionContents.getError())
|
|
|
|
return ec;
|
2016-04-21 05:24:34 +08:00
|
|
|
Expected<StringRef> symbolName = symbol->getName(StrTab);
|
|
|
|
if (!symbolName)
|
|
|
|
return errorToErrorCode(symbolName.takeError());
|
2015-02-21 12:28:26 +08:00
|
|
|
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 {
|
2015-07-01 20:56:27 +08:00
|
|
|
ErrorOr<const Elf_Shdr *> sHdr = Obj.getSection(groupMembers[i]);
|
|
|
|
if (std::error_code EC = sHdr.getError())
|
|
|
|
return EC;
|
|
|
|
ErrorOr<StringRef> sectionName = Obj.getSectionName(*sHdr);
|
2015-02-21 12:28:26 +08:00
|
|
|
if (std::error_code ec = sectionName.getError())
|
|
|
|
return ec;
|
|
|
|
s.sectionNameOrType = *sectionName;
|
|
|
|
}
|
|
|
|
S->Members.push_back(s);
|
|
|
|
}
|
|
|
|
return S.release();
|
|
|
|
}
|
|
|
|
|
2015-05-07 23:40:48 +08:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2014-05-14 13:07:47 +08:00
|
|
|
template <class ELFT>
|
2014-06-13 11:07:50 +08:00
|
|
|
static std::error_code elf2yaml(raw_ostream &Out,
|
|
|
|
const object::ELFFile<ELFT> &Obj) {
|
2014-05-14 13:07:47 +08:00
|
|
|
ELFDumper<ELFT> Dumper(Obj);
|
|
|
|
ErrorOr<ELFYAML::Object *> YAMLOrErr = Dumper.dump();
|
2014-06-13 11:07:50 +08:00
|
|
|
if (std::error_code EC = YAMLOrErr.getError())
|
2014-05-14 13:07:47 +08:00
|
|
|
return EC;
|
|
|
|
|
|
|
|
std::unique_ptr<ELFYAML::Object> YAML(YAMLOrErr.get());
|
|
|
|
yaml::Output Yout(Out);
|
|
|
|
Yout << *YAML;
|
|
|
|
|
2015-06-09 23:20:42 +08:00
|
|
|
return std::error_code();
|
2014-05-14 13:07:47 +08:00
|
|
|
}
|
|
|
|
|
2014-06-13 11:07:50 +08:00
|
|
|
std::error_code elf2yaml(raw_ostream &Out, const object::ObjectFile &Obj) {
|
2014-05-14 13:07:47 +08:00
|
|
|
if (const auto *ELFObj = dyn_cast<object::ELF32LEObjectFile>(&Obj))
|
|
|
|
return elf2yaml(Out, *ELFObj->getELFFile());
|
|
|
|
|
|
|
|
if (const auto *ELFObj = dyn_cast<object::ELF32BEObjectFile>(&Obj))
|
|
|
|
return elf2yaml(Out, *ELFObj->getELFFile());
|
|
|
|
|
|
|
|
if (const auto *ELFObj = dyn_cast<object::ELF64LEObjectFile>(&Obj))
|
|
|
|
return elf2yaml(Out, *ELFObj->getELFFile());
|
|
|
|
|
|
|
|
if (const auto *ELFObj = dyn_cast<object::ELF64BEObjectFile>(&Obj))
|
|
|
|
return elf2yaml(Out, *ELFObj->getELFFile());
|
|
|
|
|
|
|
|
return obj2yaml_error::unsupported_obj_file_format;
|
|
|
|
}
|