2012-06-19 18:02:35 +00: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-05-31 20:38:27 +00:00
|
|
|
#include "llvm/Object/COFFYAML.h"
|
2013-05-17 22:58:42 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/YAMLTraits.h"
|
2012-06-19 18:02:35 +00:00
|
|
|
|
2013-04-20 02:55:00 +00:00
|
|
|
using namespace llvm;
|
2012-06-19 18:02:35 +00:00
|
|
|
|
2013-05-17 22:58:42 +00:00
|
|
|
namespace {
|
2012-06-19 18:02:35 +00:00
|
|
|
|
2013-05-17 22:58:42 +00:00
|
|
|
class COFFDumper {
|
|
|
|
const object::COFFObjectFile &Obj;
|
|
|
|
COFFYAML::Object YAMLObj;
|
2014-09-10 12:51:52 +00:00
|
|
|
void dumpHeader();
|
2013-05-17 22:58:42 +00:00
|
|
|
void dumpSections(unsigned numSections);
|
|
|
|
void dumpSymbols(unsigned numSymbols);
|
2012-06-19 18:02:35 +00:00
|
|
|
|
2013-05-17 22:58:42 +00:00
|
|
|
public:
|
|
|
|
COFFDumper(const object::COFFObjectFile &Obj);
|
|
|
|
COFFYAML::Object &getYAMLObj();
|
2012-06-19 18:02:35 +00:00
|
|
|
};
|
2013-04-20 02:55:00 +00:00
|
|
|
|
2012-06-19 18:02:35 +00:00
|
|
|
}
|
|
|
|
|
2013-05-17 22:58:42 +00:00
|
|
|
COFFDumper::COFFDumper(const object::COFFObjectFile &Obj) : Obj(Obj) {
|
2014-09-10 12:51:52 +00:00
|
|
|
dumpHeader();
|
|
|
|
dumpSections(Obj.getNumberOfSections());
|
|
|
|
dumpSymbols(Obj.getNumberOfSymbols());
|
2012-06-19 18:02:35 +00:00
|
|
|
}
|
|
|
|
|
2014-09-10 12:51:52 +00:00
|
|
|
void COFFDumper::dumpHeader() {
|
|
|
|
YAMLObj.Header.Machine = Obj.getMachine();
|
|
|
|
YAMLObj.Header.Characteristics = Obj.getCharacteristics();
|
2012-06-19 18:02:35 +00:00
|
|
|
}
|
|
|
|
|
2013-05-17 22:58:42 +00:00
|
|
|
void COFFDumper::dumpSections(unsigned NumSections) {
|
|
|
|
std::vector<COFFYAML::Section> &Sections = YAMLObj.Sections;
|
2014-03-18 06:53:02 +00:00
|
|
|
for (const auto &Section : Obj.sections()) {
|
|
|
|
const object::coff_section *Sect = Obj.getCOFFSection(Section);
|
2013-05-17 22:58:42 +00:00
|
|
|
COFFYAML::Section Sec;
|
2014-10-10 00:17:57 +00:00
|
|
|
Section.getName(Sec.Name);
|
|
|
|
Sec.Header.Characteristics = Sect->Characteristics;
|
|
|
|
Sec.Alignment = Section.getAlignment();
|
2012-06-19 18:02:35 +00:00
|
|
|
|
2013-04-20 02:55:00 +00:00
|
|
|
ArrayRef<uint8_t> sectionData;
|
2014-10-10 00:17:57 +00:00
|
|
|
if (!Section.isBSS())
|
|
|
|
Obj.getSectionContents(Sect, sectionData);
|
2014-07-03 02:01:39 +00:00
|
|
|
Sec.SectionData = yaml::BinaryRef(sectionData);
|
2013-05-17 22:58:42 +00:00
|
|
|
|
2013-06-06 13:06:17 +00:00
|
|
|
std::vector<COFFYAML::Relocation> Relocations;
|
2014-03-18 06:53:02 +00:00
|
|
|
for (const auto &Reloc : Section.relocations()) {
|
|
|
|
const object::coff_relocation *reloc = Obj.getCOFFRelocation(Reloc);
|
2013-06-06 13:06:17 +00:00
|
|
|
COFFYAML::Relocation Rel;
|
2014-03-18 06:53:02 +00:00
|
|
|
object::symbol_iterator Sym = Reloc.getSymbol();
|
2013-06-06 13:06:17 +00:00
|
|
|
Sym->getName(Rel.SymbolName);
|
2013-05-17 22:58:42 +00:00
|
|
|
Rel.VirtualAddress = reloc->VirtualAddress;
|
|
|
|
Rel.Type = reloc->Type;
|
|
|
|
Relocations.push_back(Rel);
|
|
|
|
}
|
|
|
|
Sec.Relocations = Relocations;
|
|
|
|
Sections.push_back(Sec);
|
2013-04-20 02:55:00 +00:00
|
|
|
}
|
2012-06-19 18:02:35 +00:00
|
|
|
}
|
|
|
|
|
2014-03-19 04:47:47 +00: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-15 19:42:42 +00:00
|
|
|
const object::coff_aux_section_definition *ObjSD,
|
|
|
|
bool IsBigObj) {
|
2014-03-19 04:47:47 +00:00
|
|
|
COFF::AuxiliarySectionDefinition YAMLASD;
|
2014-09-15 19:42:42 +00:00
|
|
|
int32_t AuxNumber = ObjSD->getNumber(IsBigObj);
|
2014-03-19 04:47:47 +00:00
|
|
|
YAMLASD.Length = ObjSD->Length;
|
|
|
|
YAMLASD.NumberOfRelocations = ObjSD->NumberOfRelocations;
|
|
|
|
YAMLASD.NumberOfLinenumbers = ObjSD->NumberOfLinenumbers;
|
|
|
|
YAMLASD.CheckSum = ObjSD->CheckSum;
|
2014-09-15 19:42:42 +00:00
|
|
|
YAMLASD.Number = AuxNumber;
|
2014-03-19 04:47:47 +00: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-17 22:58:42 +00:00
|
|
|
void COFFDumper::dumpSymbols(unsigned NumSymbols) {
|
|
|
|
std::vector<COFFYAML::Symbol> &Symbols = YAMLObj.Symbols;
|
2014-03-18 06:53:02 +00:00
|
|
|
for (const auto &S : Obj.symbols()) {
|
2014-09-10 12:51:52 +00:00
|
|
|
object::COFFSymbolRef Symbol = Obj.getCOFFSymbol(S);
|
2013-05-17 22:58:42 +00:00
|
|
|
COFFYAML::Symbol Sym;
|
|
|
|
Obj.getSymbolName(Symbol, Sym.Name);
|
2014-09-10 12:51:52 +00: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 04:47:47 +00:00
|
|
|
ArrayRef<uint8_t> AuxData = Obj.getSymbolAuxData(Symbol);
|
2014-09-10 12:51:52 +00:00
|
|
|
if (Symbol.isFunctionDefinition()) {
|
2014-03-19 04:47:47 +00:00
|
|
|
// This symbol represents a function definition.
|
2014-09-10 12:51:52 +00:00
|
|
|
assert(Symbol.getNumberOfAuxSymbols() == 1 &&
|
2014-03-19 04:47:47 +00: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 12:51:52 +00:00
|
|
|
} else if (Symbol.isFunctionLineInfo()) {
|
2014-03-19 04:47:47 +00:00
|
|
|
// This symbol describes function line number information.
|
2014-09-10 12:51:52 +00:00
|
|
|
assert(Symbol.getNumberOfAuxSymbols() == 1 &&
|
|
|
|
"Expected a single aux symbol to describe this function!");
|
2014-03-19 04:47:47 +00: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-09-10 12:51:52 +00:00
|
|
|
} else if (Symbol.isWeakExternal()) {
|
2014-03-19 04:47:47 +00:00
|
|
|
// This symbol represents a weak external definition.
|
2014-09-10 12:51:52 +00:00
|
|
|
assert(Symbol.getNumberOfAuxSymbols() == 1 &&
|
|
|
|
"Expected a single aux symbol to describe this weak symbol!");
|
2014-03-19 04:47:47 +00:00
|
|
|
|
|
|
|
const object::coff_aux_weak_external *ObjWE =
|
|
|
|
reinterpret_cast<const object::coff_aux_weak_external *>(
|
|
|
|
AuxData.data());
|
|
|
|
dumpWeakExternal(&Sym, ObjWE);
|
2014-09-10 12:51:52 +00:00
|
|
|
} else if (Symbol.isFileRecord()) {
|
2014-03-19 04:47:47 +00:00
|
|
|
// This symbol represents a file record.
|
|
|
|
Sym.File = StringRef(reinterpret_cast<const char *>(AuxData.data()),
|
2014-09-10 12:51:52 +00:00
|
|
|
Symbol.getNumberOfAuxSymbols() *
|
|
|
|
Obj.getSymbolTableEntrySize())
|
2014-03-20 06:29:02 +00:00
|
|
|
.rtrim(StringRef("\0", /*length=*/1));
|
2014-09-10 12:51:52 +00:00
|
|
|
} else if (Symbol.isSectionDefinition()) {
|
2014-03-19 04:47:47 +00:00
|
|
|
// This symbol represents a section definition.
|
2014-09-10 12:51:52 +00:00
|
|
|
assert(Symbol.getNumberOfAuxSymbols() == 1 &&
|
2014-03-19 04:47:47 +00: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-15 19:42:42 +00:00
|
|
|
dumpSectionDefinition(&Sym, ObjSD, Symbol.isBigObj());
|
2014-09-10 12:51:52 +00:00
|
|
|
} else if (Symbol.isCLRToken()) {
|
2014-03-19 04:47:47 +00:00
|
|
|
// This symbol represents a CLR token definition.
|
2014-09-10 12:51:52 +00:00
|
|
|
assert(Symbol.getNumberOfAuxSymbols() == 1 &&
|
|
|
|
"Expected a single aux symbol to describe this CLR Token!");
|
2014-03-19 04:47:47 +00: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-17 22:58:42 +00:00
|
|
|
Symbols.push_back(Sym);
|
|
|
|
}
|
|
|
|
}
|
2013-04-20 02:55:00 +00:00
|
|
|
|
2013-05-17 22:58:42 +00:00
|
|
|
COFFYAML::Object &COFFDumper::getYAMLObj() {
|
|
|
|
return YAMLObj;
|
|
|
|
}
|
2012-06-19 18:02:35 +00:00
|
|
|
|
2014-06-13 03:07:50 +00:00
|
|
|
std::error_code coff2yaml(raw_ostream &Out, const object::COFFObjectFile &Obj) {
|
2013-05-17 22:58:42 +00:00
|
|
|
COFFDumper Dumper(Obj);
|
2013-04-20 02:55:00 +00:00
|
|
|
|
2013-05-17 22:58:42 +00:00
|
|
|
yaml::Output Yout(Out);
|
|
|
|
Yout << Dumper.getYAMLObj();
|
2013-04-20 02:55:00 +00:00
|
|
|
|
2013-05-17 22:58:42 +00:00
|
|
|
return object::object_error::success;
|
2012-06-19 18:02:35 +00:00
|
|
|
}
|