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"
|
2013-06-01 04:38:27 +08:00
|
|
|
#include "llvm/Object/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;
|
2014-06-13 05:46:39 +08:00
|
|
|
using std::error_code;
|
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;
|
|
|
|
void dumpHeader(const object::coff_file_header *Header);
|
|
|
|
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
|
|
|
static void check(error_code ec) {
|
|
|
|
if (ec)
|
|
|
|
report_fatal_error(ec.message());
|
2012-06-20 02:02:35 +08:00
|
|
|
}
|
|
|
|
|
2013-05-18 06:58:42 +08:00
|
|
|
COFFDumper::COFFDumper(const object::COFFObjectFile &Obj) : Obj(Obj) {
|
|
|
|
const object::coff_file_header *Header;
|
2013-06-13 03:10:33 +08:00
|
|
|
check(Obj.getCOFFHeader(Header));
|
2013-05-18 06:58:42 +08:00
|
|
|
dumpHeader(Header);
|
|
|
|
dumpSections(Header->NumberOfSections);
|
|
|
|
dumpSymbols(Header->NumberOfSymbols);
|
2012-06-20 02:02:35 +08:00
|
|
|
}
|
|
|
|
|
2013-05-18 06:58:42 +08:00
|
|
|
void COFFDumper::dumpHeader(const object::coff_file_header *Header) {
|
|
|
|
YAMLObj.Header.Machine = Header->Machine;
|
|
|
|
YAMLObj.Header.Characteristics = Header->Characteristics;
|
2012-06-20 02:02:35 +08:00
|
|
|
}
|
|
|
|
|
2013-05-18 06:58:42 +08:00
|
|
|
void COFFDumper::dumpSections(unsigned NumSections) {
|
|
|
|
std::vector<COFFYAML::Section> &Sections = YAMLObj.Sections;
|
2014-03-18 14:53:02 +08:00
|
|
|
for (const auto &Section : Obj.sections()) {
|
|
|
|
const object::coff_section *Sect = Obj.getCOFFSection(Section);
|
2013-05-18 06:58:42 +08:00
|
|
|
COFFYAML::Section Sec;
|
|
|
|
Sec.Name = Sect->Name; // FIXME: check the null termination!
|
|
|
|
uint32_t Characteristics = Sect->Characteristics;
|
|
|
|
Sec.Header.Characteristics = Characteristics;
|
|
|
|
Sec.Alignment = 1 << (((Characteristics >> 20) & 0xf) - 1);
|
2012-06-20 02:02:35 +08:00
|
|
|
|
2013-04-20 10:55:00 +08:00
|
|
|
ArrayRef<uint8_t> sectionData;
|
2013-05-18 06:58:42 +08:00
|
|
|
Obj.getSectionContents(Sect, sectionData);
|
2013-06-05 10:32:26 +08:00
|
|
|
Sec.SectionData = object::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-03-18 14:53:02 +08:00
|
|
|
for (const auto &Reloc : Section.relocations()) {
|
|
|
|
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();
|
2013-06-06 21:06:17 +08:00
|
|
|
Sym->getName(Rel.SymbolName);
|
2013-05-18 06:58:42 +08:00
|
|
|
Rel.VirtualAddress = reloc->VirtualAddress;
|
|
|
|
Rel.Type = reloc->Type;
|
|
|
|
Relocations.push_back(Rel);
|
|
|
|
}
|
|
|
|
Sec.Relocations = Relocations;
|
|
|
|
Sections.push_back(Sec);
|
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,
|
|
|
|
const object::coff_aux_section_definition *ObjSD) {
|
|
|
|
COFF::AuxiliarySectionDefinition YAMLASD;
|
|
|
|
YAMLASD.Length = ObjSD->Length;
|
|
|
|
YAMLASD.NumberOfRelocations = ObjSD->NumberOfRelocations;
|
|
|
|
YAMLASD.NumberOfLinenumbers = ObjSD->NumberOfLinenumbers;
|
|
|
|
YAMLASD.CheckSum = ObjSD->CheckSum;
|
|
|
|
YAMLASD.Number = ObjSD->Number;
|
|
|
|
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()) {
|
|
|
|
const object::coff_symbol *Symbol = Obj.getCOFFSymbol(S);
|
2013-05-18 06:58:42 +08:00
|
|
|
COFFYAML::Symbol Sym;
|
|
|
|
Obj.getSymbolName(Symbol, Sym.Name);
|
|
|
|
Sym.SimpleType = COFF::SymbolBaseType(Symbol->getBaseType());
|
|
|
|
Sym.ComplexType = COFF::SymbolComplexType(Symbol->getComplexType());
|
|
|
|
Sym.Header.StorageClass = Symbol->StorageClass;
|
|
|
|
Sym.Header.Value = Symbol->Value;
|
|
|
|
Sym.Header.SectionNumber = Symbol->SectionNumber;
|
|
|
|
Sym.Header.NumberOfAuxSymbols = Symbol->NumberOfAuxSymbols;
|
2014-03-19 12:47:47 +08:00
|
|
|
|
|
|
|
if (Symbol->NumberOfAuxSymbols > 0) {
|
|
|
|
ArrayRef<uint8_t> AuxData = Obj.getSymbolAuxData(Symbol);
|
|
|
|
if (Symbol->isFunctionDefinition()) {
|
|
|
|
// This symbol represents a function definition.
|
|
|
|
assert(Symbol->NumberOfAuxSymbols == 1 &&
|
|
|
|
"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-03-20 14:28:55 +08:00
|
|
|
} else if (Symbol->isFunctionLineInfo()) {
|
2014-03-19 12:47:47 +08:00
|
|
|
// This symbol describes function line number information.
|
|
|
|
assert(Symbol->NumberOfAuxSymbols == 1 &&
|
|
|
|
"Exepected a single aux symbol to describe this section!");
|
|
|
|
|
|
|
|
const object::coff_aux_bf_and_ef_symbol *ObjBES =
|
|
|
|
reinterpret_cast<const object::coff_aux_bf_and_ef_symbol *>(
|
|
|
|
AuxData.data());
|
|
|
|
dumpbfAndEfLineInfo(&Sym, ObjBES);
|
|
|
|
} else if (Symbol->isWeakExternal()) {
|
|
|
|
// This symbol represents a weak external definition.
|
|
|
|
assert(Symbol->NumberOfAuxSymbols == 1 &&
|
|
|
|
"Exepected a single aux symbol to describe this section!");
|
|
|
|
|
|
|
|
const object::coff_aux_weak_external *ObjWE =
|
|
|
|
reinterpret_cast<const object::coff_aux_weak_external *>(
|
|
|
|
AuxData.data());
|
|
|
|
dumpWeakExternal(&Sym, ObjWE);
|
|
|
|
} else if (Symbol->isFileRecord()) {
|
|
|
|
// This symbol represents a file record.
|
|
|
|
Sym.File = StringRef(reinterpret_cast<const char *>(AuxData.data()),
|
2014-03-20 14:29:02 +08:00
|
|
|
Symbol->NumberOfAuxSymbols * COFF::SymbolSize)
|
|
|
|
.rtrim(StringRef("\0", /*length=*/1));
|
2014-03-19 12:47:47 +08:00
|
|
|
} else if (Symbol->isSectionDefinition()) {
|
|
|
|
// This symbol represents a section definition.
|
|
|
|
assert(Symbol->NumberOfAuxSymbols == 1 &&
|
|
|
|
"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());
|
|
|
|
dumpSectionDefinition(&Sym, ObjSD);
|
|
|
|
} else if (Symbol->isCLRToken()) {
|
|
|
|
// This symbol represents a CLR token definition.
|
|
|
|
assert(Symbol->NumberOfAuxSymbols == 1 &&
|
|
|
|
"Expected a single aux symbol to describe this CLR Token");
|
|
|
|
|
|
|
|
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-05-07 13:18:51 +08:00
|
|
|
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
|
|
|
|
2013-05-18 06:58:42 +08:00
|
|
|
return object::object_error::success;
|
2012-06-20 02:02:35 +08:00
|
|
|
}
|