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;
|
|
|
|
void dumpHeader(const object::coff_file_header *Header);
|
|
|
|
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
|
|
|
static void check(error_code ec) {
|
|
|
|
if (ec)
|
|
|
|
report_fatal_error(ec.message());
|
2012-06-19 18:02:35 +00:00
|
|
|
}
|
|
|
|
|
2013-05-17 22:58:42 +00:00
|
|
|
COFFDumper::COFFDumper(const object::COFFObjectFile &Obj) : Obj(Obj) {
|
|
|
|
const object::coff_file_header *Header;
|
2013-06-12 19:10:33 +00:00
|
|
|
check(Obj.getCOFFHeader(Header));
|
2013-05-17 22:58:42 +00:00
|
|
|
dumpHeader(Header);
|
|
|
|
dumpSections(Header->NumberOfSections);
|
|
|
|
dumpSymbols(Header->NumberOfSymbols);
|
2012-06-19 18:02:35 +00:00
|
|
|
}
|
|
|
|
|
2013-05-17 22:58:42 +00:00
|
|
|
void COFFDumper::dumpHeader(const object::coff_file_header *Header) {
|
|
|
|
YAMLObj.Header.Machine = Header->Machine;
|
|
|
|
YAMLObj.Header.Characteristics = Header->Characteristics;
|
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;
|
2013-04-20 02:55:00 +00:00
|
|
|
error_code ec;
|
|
|
|
for (object::section_iterator iter = Obj.begin_sections();
|
|
|
|
iter != Obj.end_sections(); iter.increment(ec)) {
|
2013-05-17 22:58:42 +00:00
|
|
|
check(ec);
|
|
|
|
const object::coff_section *Sect = Obj.getCOFFSection(iter);
|
|
|
|
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-19 18:02:35 +00:00
|
|
|
|
2013-04-20 02:55:00 +00:00
|
|
|
ArrayRef<uint8_t> sectionData;
|
2013-05-17 22:58:42 +00:00
|
|
|
Obj.getSectionContents(Sect, sectionData);
|
2013-06-05 02:32:26 +00:00
|
|
|
Sec.SectionData = object::yaml::BinaryRef(sectionData);
|
2013-05-17 22:58:42 +00:00
|
|
|
|
2013-06-06 13:06:17 +00:00
|
|
|
std::vector<COFFYAML::Relocation> Relocations;
|
2013-04-20 02:55:00 +00:00
|
|
|
for (object::relocation_iterator rIter = iter->begin_relocations();
|
2012-06-19 18:02:35 +00:00
|
|
|
rIter != iter->end_relocations(); rIter.increment(ec)) {
|
2013-04-20 02:55:00 +00:00
|
|
|
const object::coff_relocation *reloc = Obj.getCOFFRelocation(rIter);
|
2013-06-06 13:06:17 +00:00
|
|
|
COFFYAML::Relocation Rel;
|
|
|
|
object::symbol_iterator Sym = rIter->getSymbol();
|
|
|
|
StringRef Name;
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-05-17 22:58:42 +00:00
|
|
|
void COFFDumper::dumpSymbols(unsigned NumSymbols) {
|
2013-04-20 02:55:00 +00:00
|
|
|
error_code ec;
|
2013-05-17 22:58:42 +00:00
|
|
|
std::vector<COFFYAML::Symbol> &Symbols = YAMLObj.Symbols;
|
2013-04-20 02:55:00 +00:00
|
|
|
for (object::symbol_iterator iter = Obj.begin_symbols();
|
|
|
|
iter != Obj.end_symbols(); iter.increment(ec)) {
|
2013-05-17 22:58:42 +00:00
|
|
|
check(ec);
|
|
|
|
const object::coff_symbol *Symbol = Obj.getCOFFSymbol(iter);
|
|
|
|
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;
|
2013-06-05 02:32:26 +00:00
|
|
|
Sym.AuxiliaryData = object::yaml::BinaryRef(Obj.getSymbolAuxData(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
|
|
|
|
2013-05-17 22:58:42 +00:00
|
|
|
error_code coff2yaml(raw_ostream &Out, MemoryBuffer *Buff) {
|
2013-04-20 02:55:00 +00:00
|
|
|
error_code ec;
|
2013-05-17 22:58:42 +00:00
|
|
|
object::COFFObjectFile Obj(Buff, ec);
|
|
|
|
check(ec);
|
|
|
|
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
|
|
|
}
|