1 //===------ utils/obj2yaml.cpp - obj2yaml conversion tool -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 #include "llvm/Object/COFF.h"
12 #include "llvm/Object/COFFYaml.h"
13 #include "llvm/Support/ErrorHandling.h"
14 #include "llvm/Support/YAMLTraits.h"
23 const object::COFFObjectFile &Obj;
24 COFFYAML::Object YAMLObj;
25 void dumpHeader(const object::coff_file_header *Header);
26 void dumpSections(unsigned numSections);
27 void dumpSymbols(unsigned numSymbols);
28 StringRef getHexString(ArrayRef<uint8_t> Data);
29 std::list<std::string> Strings;
32 COFFDumper(const object::COFFObjectFile &Obj);
33 COFFYAML::Object &getYAMLObj();
38 static void check(error_code ec) {
40 report_fatal_error(ec.message());
43 COFFDumper::COFFDumper(const object::COFFObjectFile &Obj) : Obj(Obj) {
44 const object::coff_file_header *Header;
45 check(Obj.getHeader(Header));
47 dumpSections(Header->NumberOfSections);
48 dumpSymbols(Header->NumberOfSymbols);
51 void COFFDumper::dumpHeader(const object::coff_file_header *Header) {
52 YAMLObj.Header.Machine = Header->Machine;
53 YAMLObj.Header.Characteristics = Header->Characteristics;
56 void COFFDumper::dumpSections(unsigned NumSections) {
57 std::vector<COFFYAML::Section> &Sections = YAMLObj.Sections;
59 for (object::section_iterator iter = Obj.begin_sections();
60 iter != Obj.end_sections(); iter.increment(ec)) {
62 const object::coff_section *Sect = Obj.getCOFFSection(iter);
63 COFFYAML::Section Sec;
64 Sec.Name = Sect->Name; // FIXME: check the null termination!
65 uint32_t Characteristics = Sect->Characteristics;
66 Sec.Header.Characteristics = Characteristics;
67 Sec.Alignment = 1 << (((Characteristics >> 20) & 0xf) - 1);
69 ArrayRef<uint8_t> sectionData;
70 Obj.getSectionContents(Sect, sectionData);
71 Sec.SectionData = getHexString(sectionData);
73 std::vector<COFF::relocation> Relocations;
74 for (object::relocation_iterator rIter = iter->begin_relocations();
75 rIter != iter->end_relocations(); rIter.increment(ec)) {
76 const object::coff_relocation *reloc = Obj.getCOFFRelocation(rIter);
78 Rel.VirtualAddress = reloc->VirtualAddress;
79 Rel.SymbolTableIndex = reloc->SymbolTableIndex;
80 Rel.Type = reloc->Type;
81 Relocations.push_back(Rel);
83 Sec.Relocations = Relocations;
84 Sections.push_back(Sec);
88 void COFFDumper::dumpSymbols(unsigned NumSymbols) {
90 std::vector<COFFYAML::Symbol> &Symbols = YAMLObj.Symbols;
91 for (object::symbol_iterator iter = Obj.begin_symbols();
92 iter != Obj.end_symbols(); iter.increment(ec)) {
94 const object::coff_symbol *Symbol = Obj.getCOFFSymbol(iter);
96 Obj.getSymbolName(Symbol, Sym.Name);
97 Sym.SimpleType = COFF::SymbolBaseType(Symbol->getBaseType());
98 Sym.ComplexType = COFF::SymbolComplexType(Symbol->getComplexType());
99 Sym.Header.StorageClass = Symbol->StorageClass;
100 Sym.Header.Value = Symbol->Value;
101 Sym.Header.SectionNumber = Symbol->SectionNumber;
102 Sym.Header.NumberOfAuxSymbols = Symbol->NumberOfAuxSymbols;
103 Sym.AuxiliaryData = getHexString(Obj.getSymbolAuxData(Symbol));
104 Symbols.push_back(Sym);
108 StringRef COFFDumper::getHexString(ArrayRef<uint8_t> Data) {
110 for (ArrayRef<uint8_t>::iterator I = Data.begin(), E = Data.end(); I != E;
113 S.push_back(hexdigit(Byte >> 4));
114 S.push_back(hexdigit(Byte & 0xf));
116 Strings.push_back(S);
117 return Strings.back();
120 COFFYAML::Object &COFFDumper::getYAMLObj() {
124 error_code coff2yaml(raw_ostream &Out, MemoryBuffer *Buff) {
126 object::COFFObjectFile Obj(Buff, ec);
128 COFFDumper Dumper(Obj);
130 yaml::Output Yout(Out);
131 Yout << Dumper.getYAMLObj();
133 return object::object_error::success;