2012-06-20 02:02:35 +08:00
|
|
|
//===------ utils/obj2yaml.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 "obj2yaml.h"
|
|
|
|
#include "llvm/Object/COFF.h"
|
2016-03-02 03:15:06 +08:00
|
|
|
#include "llvm/ObjectYAML/COFFYAML.h"
|
2013-05-18 06:58:42 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/YAMLTraits.h"
|
2012-06-20 02:02:35 +08:00
|
|
|
|
2013-04-20 10:55:00 +08:00
|
|
|
using namespace llvm;
|
2012-06-20 02:02:35 +08:00
|
|
|
|
2013-05-18 06:58:42 +08:00
|
|
|
namespace {
|
2012-06-20 02:02:35 +08:00
|
|
|
|
2013-05-18 06:58:42 +08:00
|
|
|
class COFFDumper {
|
|
|
|
const object::COFFObjectFile &Obj;
|
|
|
|
COFFYAML::Object YAMLObj;
|
2014-11-14 16:15:42 +08:00
|
|
|
template <typename T>
|
|
|
|
void dumpOptionalHeader(T OptionalHeader);
|
2014-09-10 20:51:52 +08:00
|
|
|
void dumpHeader();
|
2013-05-18 06:58:42 +08:00
|
|
|
void dumpSections(unsigned numSections);
|
|
|
|
void dumpSymbols(unsigned numSymbols);
|
2012-06-20 02:02:35 +08:00
|
|
|
|
2013-05-18 06:58:42 +08:00
|
|
|
public:
|
|
|
|
COFFDumper(const object::COFFObjectFile &Obj);
|
|
|
|
COFFYAML::Object &getYAMLObj();
|
2012-06-20 02:02:35 +08:00
|
|
|
};
|
2013-04-20 10:55:00 +08:00
|
|
|
|
2012-06-20 02:02:35 +08:00
|
|
|
}
|
|
|
|
|
2013-05-18 06:58:42 +08:00
|
|
|
COFFDumper::COFFDumper(const object::COFFObjectFile &Obj) : Obj(Obj) {
|
2014-11-14 16:15:42 +08:00
|
|
|
const object::pe32_header *PE32Header = nullptr;
|
|
|
|
Obj.getPE32Header(PE32Header);
|
|
|
|
if (PE32Header) {
|
|
|
|
dumpOptionalHeader(PE32Header);
|
|
|
|
} else {
|
|
|
|
const object::pe32plus_header *PE32PlusHeader = nullptr;
|
|
|
|
Obj.getPE32PlusHeader(PE32PlusHeader);
|
|
|
|
if (PE32PlusHeader) {
|
|
|
|
dumpOptionalHeader(PE32PlusHeader);
|
|
|
|
}
|
|
|
|
}
|
2014-09-10 20:51:52 +08:00
|
|
|
dumpHeader();
|
|
|
|
dumpSections(Obj.getNumberOfSections());
|
|
|
|
dumpSymbols(Obj.getNumberOfSymbols());
|
2012-06-20 02:02:35 +08:00
|
|
|
}
|
|
|
|
|
2014-11-14 16:15:42 +08:00
|
|
|
template <typename T> void COFFDumper::dumpOptionalHeader(T OptionalHeader) {
|
|
|
|
YAMLObj.OptionalHeader = COFFYAML::PEHeader();
|
|
|
|
YAMLObj.OptionalHeader->Header.AddressOfEntryPoint =
|
|
|
|
OptionalHeader->AddressOfEntryPoint;
|
|
|
|
YAMLObj.OptionalHeader->Header.AddressOfEntryPoint =
|
|
|
|
OptionalHeader->AddressOfEntryPoint;
|
|
|
|
YAMLObj.OptionalHeader->Header.ImageBase = OptionalHeader->ImageBase;
|
|
|
|
YAMLObj.OptionalHeader->Header.SectionAlignment =
|
|
|
|
OptionalHeader->SectionAlignment;
|
|
|
|
YAMLObj.OptionalHeader->Header.FileAlignment = OptionalHeader->FileAlignment;
|
|
|
|
YAMLObj.OptionalHeader->Header.MajorOperatingSystemVersion =
|
|
|
|
OptionalHeader->MajorOperatingSystemVersion;
|
|
|
|
YAMLObj.OptionalHeader->Header.MinorOperatingSystemVersion =
|
|
|
|
OptionalHeader->MinorOperatingSystemVersion;
|
|
|
|
YAMLObj.OptionalHeader->Header.MajorImageVersion =
|
|
|
|
OptionalHeader->MajorImageVersion;
|
|
|
|
YAMLObj.OptionalHeader->Header.MinorImageVersion =
|
|
|
|
OptionalHeader->MinorImageVersion;
|
|
|
|
YAMLObj.OptionalHeader->Header.MajorSubsystemVersion =
|
|
|
|
OptionalHeader->MajorSubsystemVersion;
|
|
|
|
YAMLObj.OptionalHeader->Header.MinorSubsystemVersion =
|
|
|
|
OptionalHeader->MinorSubsystemVersion;
|
|
|
|
YAMLObj.OptionalHeader->Header.Subsystem = OptionalHeader->Subsystem;
|
|
|
|
YAMLObj.OptionalHeader->Header.DLLCharacteristics =
|
|
|
|
OptionalHeader->DLLCharacteristics;
|
|
|
|
YAMLObj.OptionalHeader->Header.SizeOfStackReserve =
|
|
|
|
OptionalHeader->SizeOfStackReserve;
|
|
|
|
YAMLObj.OptionalHeader->Header.SizeOfStackCommit =
|
|
|
|
OptionalHeader->SizeOfStackCommit;
|
|
|
|
YAMLObj.OptionalHeader->Header.SizeOfHeapReserve =
|
|
|
|
OptionalHeader->SizeOfHeapReserve;
|
|
|
|
YAMLObj.OptionalHeader->Header.SizeOfHeapCommit =
|
|
|
|
OptionalHeader->SizeOfHeapCommit;
|
|
|
|
unsigned I = 0;
|
|
|
|
for (auto &DestDD : YAMLObj.OptionalHeader->DataDirectories) {
|
|
|
|
const object::data_directory *DD;
|
|
|
|
if (Obj.getDataDirectory(I++, DD))
|
|
|
|
continue;
|
|
|
|
DestDD = COFF::DataDirectory();
|
|
|
|
DestDD->RelativeVirtualAddress = DD->RelativeVirtualAddress;
|
|
|
|
DestDD->Size = DD->Size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-10 20:51:52 +08:00
|
|
|
void COFFDumper::dumpHeader() {
|
|
|
|
YAMLObj.Header.Machine = Obj.getMachine();
|
|
|
|
YAMLObj.Header.Characteristics = Obj.getCharacteristics();
|
2012-06-20 02:02:35 +08:00
|
|
|
}
|
|
|
|
|
2013-05-18 06:58:42 +08:00
|
|
|
void COFFDumper::dumpSections(unsigned NumSections) {
|
2014-11-14 16:15:42 +08:00
|
|
|
std::vector<COFFYAML::Section> &YAMLSections = YAMLObj.Sections;
|
|
|
|
for (const auto &ObjSection : Obj.sections()) {
|
|
|
|
const object::coff_section *COFFSection = Obj.getCOFFSection(ObjSection);
|
|
|
|
COFFYAML::Section NewYAMLSection;
|
|
|
|
ObjSection.getName(NewYAMLSection.Name);
|
|
|
|
NewYAMLSection.Header.Characteristics = COFFSection->Characteristics;
|
|
|
|
NewYAMLSection.Header.VirtualAddress = ObjSection.getAddress();
|
|
|
|
NewYAMLSection.Header.VirtualSize = COFFSection->VirtualSize;
|
|
|
|
NewYAMLSection.Alignment = ObjSection.getAlignment();
|
2016-03-19 05:51:14 +08:00
|
|
|
assert(NewYAMLSection.Alignment <= 8192);
|
2012-06-20 02:02:35 +08:00
|
|
|
|
2013-04-20 10:55:00 +08:00
|
|
|
ArrayRef<uint8_t> sectionData;
|
2014-11-14 16:15:42 +08:00
|
|
|
if (!ObjSection.isBSS())
|
|
|
|
Obj.getSectionContents(COFFSection, sectionData);
|
|
|
|
NewYAMLSection.SectionData = yaml::BinaryRef(sectionData);
|
2013-05-18 06:58:42 +08:00
|
|
|
|
2013-06-06 21:06:17 +08:00
|
|
|
std::vector<COFFYAML::Relocation> Relocations;
|
2014-11-14 16:15:42 +08:00
|
|
|
for (const auto &Reloc : ObjSection.relocations()) {
|
2014-03-18 14:53:02 +08:00
|
|
|
const object::coff_relocation *reloc = Obj.getCOFFRelocation(Reloc);
|
2013-06-06 21:06:17 +08:00
|
|
|
COFFYAML::Relocation Rel;
|
2014-03-18 14:53:02 +08:00
|
|
|
object::symbol_iterator Sym = Reloc.getSymbol();
|
2016-04-21 05:24:34 +08:00
|
|
|
Expected<StringRef> SymbolNameOrErr = Sym->getName();
|
|
|
|
if (!SymbolNameOrErr) {
|
|
|
|
std::string Buf;
|
|
|
|
raw_string_ostream OS(Buf);
|
|
|
|
logAllUnhandledErrors(SymbolNameOrErr.takeError(), OS, "");
|
|
|
|
OS.flush();
|
|
|
|
report_fatal_error(Buf);
|
|
|
|
}
|
2015-07-03 04:55:21 +08:00
|
|
|
Rel.SymbolName = *SymbolNameOrErr;
|
2013-05-18 06:58:42 +08:00
|
|
|
Rel.VirtualAddress = reloc->VirtualAddress;
|
|
|
|
Rel.Type = reloc->Type;
|
|
|
|
Relocations.push_back(Rel);
|
|
|
|
}
|
2014-11-14 16:15:42 +08:00
|
|
|
NewYAMLSection.Relocations = Relocations;
|
|
|
|
YAMLSections.push_back(NewYAMLSection);
|
2013-04-20 10:55:00 +08:00
|
|
|
}
|
2012-06-20 02:02:35 +08:00
|
|
|
}
|
|
|
|
|
2014-03-19 12:47:47 +08:00
|
|
|
static void
|
|
|
|
dumpFunctionDefinition(COFFYAML::Symbol *Sym,
|
|
|
|
const object::coff_aux_function_definition *ObjFD) {
|
|
|
|
COFF::AuxiliaryFunctionDefinition YAMLFD;
|
|
|
|
YAMLFD.TagIndex = ObjFD->TagIndex;
|
|
|
|
YAMLFD.TotalSize = ObjFD->TotalSize;
|
|
|
|
YAMLFD.PointerToLinenumber = ObjFD->PointerToLinenumber;
|
|
|
|
YAMLFD.PointerToNextFunction = ObjFD->PointerToNextFunction;
|
|
|
|
|
|
|
|
Sym->FunctionDefinition = YAMLFD;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
dumpbfAndEfLineInfo(COFFYAML::Symbol *Sym,
|
|
|
|
const object::coff_aux_bf_and_ef_symbol *ObjBES) {
|
|
|
|
COFF::AuxiliarybfAndefSymbol YAMLAAS;
|
|
|
|
YAMLAAS.Linenumber = ObjBES->Linenumber;
|
|
|
|
YAMLAAS.PointerToNextFunction = ObjBES->PointerToNextFunction;
|
|
|
|
|
|
|
|
Sym->bfAndefSymbol = YAMLAAS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dumpWeakExternal(COFFYAML::Symbol *Sym,
|
|
|
|
const object::coff_aux_weak_external *ObjWE) {
|
|
|
|
COFF::AuxiliaryWeakExternal YAMLWE;
|
|
|
|
YAMLWE.TagIndex = ObjWE->TagIndex;
|
|
|
|
YAMLWE.Characteristics = ObjWE->Characteristics;
|
|
|
|
|
|
|
|
Sym->WeakExternal = YAMLWE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
dumpSectionDefinition(COFFYAML::Symbol *Sym,
|
2014-09-16 03:42:42 +08:00
|
|
|
const object::coff_aux_section_definition *ObjSD,
|
|
|
|
bool IsBigObj) {
|
2014-03-19 12:47:47 +08:00
|
|
|
COFF::AuxiliarySectionDefinition YAMLASD;
|
2014-09-16 03:42:42 +08:00
|
|
|
int32_t AuxNumber = ObjSD->getNumber(IsBigObj);
|
2014-03-19 12:47:47 +08:00
|
|
|
YAMLASD.Length = ObjSD->Length;
|
|
|
|
YAMLASD.NumberOfRelocations = ObjSD->NumberOfRelocations;
|
|
|
|
YAMLASD.NumberOfLinenumbers = ObjSD->NumberOfLinenumbers;
|
|
|
|
YAMLASD.CheckSum = ObjSD->CheckSum;
|
2014-09-16 03:42:42 +08:00
|
|
|
YAMLASD.Number = AuxNumber;
|
2014-03-19 12:47:47 +08:00
|
|
|
YAMLASD.Selection = ObjSD->Selection;
|
|
|
|
|
|
|
|
Sym->SectionDefinition = YAMLASD;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
dumpCLRTokenDefinition(COFFYAML::Symbol *Sym,
|
|
|
|
const object::coff_aux_clr_token *ObjCLRToken) {
|
|
|
|
COFF::AuxiliaryCLRToken YAMLCLRToken;
|
|
|
|
YAMLCLRToken.AuxType = ObjCLRToken->AuxType;
|
|
|
|
YAMLCLRToken.SymbolTableIndex = ObjCLRToken->SymbolTableIndex;
|
|
|
|
|
|
|
|
Sym->CLRToken = YAMLCLRToken;
|
|
|
|
}
|
|
|
|
|
2013-05-18 06:58:42 +08:00
|
|
|
void COFFDumper::dumpSymbols(unsigned NumSymbols) {
|
|
|
|
std::vector<COFFYAML::Symbol> &Symbols = YAMLObj.Symbols;
|
2014-03-18 14:53:02 +08:00
|
|
|
for (const auto &S : Obj.symbols()) {
|
2014-09-10 20:51:52 +08:00
|
|
|
object::COFFSymbolRef Symbol = Obj.getCOFFSymbol(S);
|
2013-05-18 06:58:42 +08:00
|
|
|
COFFYAML::Symbol Sym;
|
|
|
|
Obj.getSymbolName(Symbol, Sym.Name);
|
2014-09-10 20:51:52 +08:00
|
|
|
Sym.SimpleType = COFF::SymbolBaseType(Symbol.getBaseType());
|
|
|
|
Sym.ComplexType = COFF::SymbolComplexType(Symbol.getComplexType());
|
|
|
|
Sym.Header.StorageClass = Symbol.getStorageClass();
|
|
|
|
Sym.Header.Value = Symbol.getValue();
|
|
|
|
Sym.Header.SectionNumber = Symbol.getSectionNumber();
|
|
|
|
Sym.Header.NumberOfAuxSymbols = Symbol.getNumberOfAuxSymbols();
|
|
|
|
|
|
|
|
if (Symbol.getNumberOfAuxSymbols() > 0) {
|
2014-03-19 12:47:47 +08:00
|
|
|
ArrayRef<uint8_t> AuxData = Obj.getSymbolAuxData(Symbol);
|
2014-09-10 20:51:52 +08:00
|
|
|
if (Symbol.isFunctionDefinition()) {
|
2014-03-19 12:47:47 +08:00
|
|
|
// This symbol represents a function definition.
|
2014-09-10 20:51:52 +08:00
|
|
|
assert(Symbol.getNumberOfAuxSymbols() == 1 &&
|
2014-03-19 12:47:47 +08:00
|
|
|
"Expected a single aux symbol to describe this function!");
|
|
|
|
|
|
|
|
const object::coff_aux_function_definition *ObjFD =
|
|
|
|
reinterpret_cast<const object::coff_aux_function_definition *>(
|
|
|
|
AuxData.data());
|
|
|
|
dumpFunctionDefinition(&Sym, ObjFD);
|
2014-09-10 20:51:52 +08:00
|
|
|
} else if (Symbol.isFunctionLineInfo()) {
|
2014-03-19 12:47:47 +08:00
|
|
|
// This symbol describes function line number information.
|
2014-09-10 20:51:52 +08:00
|
|
|
assert(Symbol.getNumberOfAuxSymbols() == 1 &&
|
|
|
|
"Expected a single aux symbol to describe this function!");
|
2014-03-19 12:47:47 +08:00
|
|
|
|
|
|
|
const object::coff_aux_bf_and_ef_symbol *ObjBES =
|
|
|
|
reinterpret_cast<const object::coff_aux_bf_and_ef_symbol *>(
|
|
|
|
AuxData.data());
|
|
|
|
dumpbfAndEfLineInfo(&Sym, ObjBES);
|
2014-10-31 13:07:00 +08:00
|
|
|
} else if (Symbol.isAnyUndefined()) {
|
2014-03-19 12:47:47 +08:00
|
|
|
// This symbol represents a weak external definition.
|
2014-09-10 20:51:52 +08:00
|
|
|
assert(Symbol.getNumberOfAuxSymbols() == 1 &&
|
|
|
|
"Expected a single aux symbol to describe this weak symbol!");
|
2014-03-19 12:47:47 +08:00
|
|
|
|
|
|
|
const object::coff_aux_weak_external *ObjWE =
|
|
|
|
reinterpret_cast<const object::coff_aux_weak_external *>(
|
|
|
|
AuxData.data());
|
|
|
|
dumpWeakExternal(&Sym, ObjWE);
|
2014-09-10 20:51:52 +08:00
|
|
|
} else if (Symbol.isFileRecord()) {
|
2014-03-19 12:47:47 +08:00
|
|
|
// This symbol represents a file record.
|
|
|
|
Sym.File = StringRef(reinterpret_cast<const char *>(AuxData.data()),
|
2014-09-10 20:51:52 +08:00
|
|
|
Symbol.getNumberOfAuxSymbols() *
|
|
|
|
Obj.getSymbolTableEntrySize())
|
2014-03-20 14:29:02 +08:00
|
|
|
.rtrim(StringRef("\0", /*length=*/1));
|
2014-09-10 20:51:52 +08:00
|
|
|
} else if (Symbol.isSectionDefinition()) {
|
2014-03-19 12:47:47 +08:00
|
|
|
// This symbol represents a section definition.
|
2014-09-10 20:51:52 +08:00
|
|
|
assert(Symbol.getNumberOfAuxSymbols() == 1 &&
|
2014-03-19 12:47:47 +08:00
|
|
|
"Expected a single aux symbol to describe this section!");
|
|
|
|
|
|
|
|
const object::coff_aux_section_definition *ObjSD =
|
|
|
|
reinterpret_cast<const object::coff_aux_section_definition *>(
|
|
|
|
AuxData.data());
|
2014-09-16 03:42:42 +08:00
|
|
|
dumpSectionDefinition(&Sym, ObjSD, Symbol.isBigObj());
|
2014-09-10 20:51:52 +08:00
|
|
|
} else if (Symbol.isCLRToken()) {
|
2014-03-19 12:47:47 +08:00
|
|
|
// This symbol represents a CLR token definition.
|
2014-09-10 20:51:52 +08:00
|
|
|
assert(Symbol.getNumberOfAuxSymbols() == 1 &&
|
|
|
|
"Expected a single aux symbol to describe this CLR Token!");
|
2014-03-19 12:47:47 +08:00
|
|
|
|
|
|
|
const object::coff_aux_clr_token *ObjCLRToken =
|
|
|
|
reinterpret_cast<const object::coff_aux_clr_token *>(
|
|
|
|
AuxData.data());
|
|
|
|
dumpCLRTokenDefinition(&Sym, ObjCLRToken);
|
|
|
|
} else {
|
|
|
|
llvm_unreachable("Unhandled auxiliary symbol!");
|
|
|
|
}
|
|
|
|
}
|
2013-05-18 06:58:42 +08:00
|
|
|
Symbols.push_back(Sym);
|
|
|
|
}
|
|
|
|
}
|
2013-04-20 10:55:00 +08:00
|
|
|
|
2013-05-18 06:58:42 +08:00
|
|
|
COFFYAML::Object &COFFDumper::getYAMLObj() {
|
|
|
|
return YAMLObj;
|
|
|
|
}
|
2012-06-20 02:02:35 +08:00
|
|
|
|
2014-06-13 11:07:50 +08:00
|
|
|
std::error_code coff2yaml(raw_ostream &Out, const object::COFFObjectFile &Obj) {
|
2013-05-18 06:58:42 +08:00
|
|
|
COFFDumper Dumper(Obj);
|
2013-04-20 10:55:00 +08:00
|
|
|
|
2013-05-18 06:58:42 +08:00
|
|
|
yaml::Output Yout(Out);
|
|
|
|
Yout << Dumper.getYAMLObj();
|
2013-04-20 10:55:00 +08:00
|
|
|
|
2015-06-09 23:20:42 +08:00
|
|
|
return std::error_code();
|
2012-06-20 02:02:35 +08:00
|
|
|
}
|