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"
21 const object::COFFObjectFile &Obj;
22 COFFYAML::Object YAMLObj;
23 void dumpHeader(const object::coff_file_header *Header);
24 void dumpSections(unsigned numSections);
25 void dumpSymbols(unsigned numSymbols);
26 StringRef getHexString(ArrayRef<uint8_t> Data);
27 std::vector<std::string> Strings;
30 COFFDumper(const object::COFFObjectFile &Obj);
31 COFFYAML::Object &getYAMLObj();
36 static void check(error_code ec) {
38 report_fatal_error(ec.message());
41 COFFDumper::COFFDumper(const object::COFFObjectFile &Obj) : Obj(Obj) {
42 const object::coff_file_header *Header;
43 check(Obj.getHeader(Header));
45 dumpSections(Header->NumberOfSections);
46 dumpSymbols(Header->NumberOfSymbols);
49 void COFFDumper::dumpHeader(const object::coff_file_header *Header) {
50 YAMLObj.Header.Machine = Header->Machine;
51 YAMLObj.Header.Characteristics = Header->Characteristics;
54 void COFFDumper::dumpSections(unsigned NumSections) {
55 std::vector<COFFYAML::Section> &Sections = YAMLObj.Sections;
57 for (object::section_iterator iter = Obj.begin_sections();
58 iter != Obj.end_sections(); iter.increment(ec)) {
60 const object::coff_section *Sect = Obj.getCOFFSection(iter);
61 COFFYAML::Section Sec;
62 Sec.Name = Sect->Name; // FIXME: check the null termination!
63 uint32_t Characteristics = Sect->Characteristics;
64 Sec.Header.Characteristics = Characteristics;
65 Sec.Alignment = 1 << (((Characteristics >> 20) & 0xf) - 1);
67 ArrayRef<uint8_t> sectionData;
68 Obj.getSectionContents(Sect, sectionData);
69 Sec.SectionData = getHexString(sectionData);
71 std::vector<COFF::relocation> Relocations;
72 for (object::relocation_iterator rIter = iter->begin_relocations();
73 rIter != iter->end_relocations(); rIter.increment(ec)) {
74 const object::coff_relocation *reloc = Obj.getCOFFRelocation(rIter);
76 Rel.VirtualAddress = reloc->VirtualAddress;
77 Rel.SymbolTableIndex = reloc->SymbolTableIndex;
78 Rel.Type = reloc->Type;
79 Relocations.push_back(Rel);
81 Sec.Relocations = Relocations;
82 Sections.push_back(Sec);
86 void COFFDumper::dumpSymbols(unsigned NumSymbols) {
88 std::vector<COFFYAML::Symbol> &Symbols = YAMLObj.Symbols;
89 for (object::symbol_iterator iter = Obj.begin_symbols();
90 iter != Obj.end_symbols(); iter.increment(ec)) {
92 const object::coff_symbol *Symbol = Obj.getCOFFSymbol(iter);
94 Obj.getSymbolName(Symbol, Sym.Name);
95 Sym.SimpleType = COFF::SymbolBaseType(Symbol->getBaseType());
96 Sym.ComplexType = COFF::SymbolComplexType(Symbol->getComplexType());
97 Sym.Header.StorageClass = Symbol->StorageClass;
98 Sym.Header.Value = Symbol->Value;
99 Sym.Header.SectionNumber = Symbol->SectionNumber;
100 Sym.Header.NumberOfAuxSymbols = Symbol->NumberOfAuxSymbols;
101 Sym.AuxiliaryData = getHexString(Obj.getSymbolAuxData(Symbol));
102 Symbols.push_back(Sym);
106 StringRef COFFDumper::getHexString(ArrayRef<uint8_t> Data) {
108 for (ArrayRef<uint8_t>::iterator I = Data.begin(), E = Data.end(); I != E;
111 S.push_back(hexdigit(Byte >> 4));
112 S.push_back(hexdigit(Byte & 0xf));
114 Strings.push_back(S);
115 return Strings.back();
118 COFFYAML::Object &COFFDumper::getYAMLObj() {
122 error_code coff2yaml(raw_ostream &Out, MemoryBuffer *Buff) {
124 object::COFFObjectFile Obj(Buff, ec);
126 COFFDumper Dumper(Obj);
128 yaml::Output Yout(Out);
129 Yout << Dumper.getYAMLObj();
131 return object::object_error::success;