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);
28 COFFDumper(const object::COFFObjectFile &Obj);
29 COFFYAML::Object &getYAMLObj();
34 static void check(error_code ec) {
36 report_fatal_error(ec.message());
39 COFFDumper::COFFDumper(const object::COFFObjectFile &Obj) : Obj(Obj) {
40 const object::coff_file_header *Header;
41 check(Obj.getCOFFHeader(Header));
43 dumpSections(Header->NumberOfSections);
44 dumpSymbols(Header->NumberOfSymbols);
47 void COFFDumper::dumpHeader(const object::coff_file_header *Header) {
48 YAMLObj.Header.Machine = Header->Machine;
49 YAMLObj.Header.Characteristics = Header->Characteristics;
52 void COFFDumper::dumpSections(unsigned NumSections) {
53 std::vector<COFFYAML::Section> &Sections = YAMLObj.Sections;
54 for (const auto &Section : Obj.sections()) {
55 const object::coff_section *Sect = Obj.getCOFFSection(Section);
56 COFFYAML::Section Sec;
57 Sec.Name = Sect->Name; // FIXME: check the null termination!
58 uint32_t Characteristics = Sect->Characteristics;
59 Sec.Header.Characteristics = Characteristics;
60 Sec.Alignment = 1 << (((Characteristics >> 20) & 0xf) - 1);
62 ArrayRef<uint8_t> sectionData;
63 Obj.getSectionContents(Sect, sectionData);
64 Sec.SectionData = object::yaml::BinaryRef(sectionData);
66 std::vector<COFFYAML::Relocation> Relocations;
67 for (const auto &Reloc : Section.relocations()) {
68 const object::coff_relocation *reloc = Obj.getCOFFRelocation(Reloc);
69 COFFYAML::Relocation Rel;
70 object::symbol_iterator Sym = Reloc.getSymbol();
71 Sym->getName(Rel.SymbolName);
72 Rel.VirtualAddress = reloc->VirtualAddress;
73 Rel.Type = reloc->Type;
74 Relocations.push_back(Rel);
76 Sec.Relocations = Relocations;
77 Sections.push_back(Sec);
81 void COFFDumper::dumpSymbols(unsigned NumSymbols) {
82 std::vector<COFFYAML::Symbol> &Symbols = YAMLObj.Symbols;
83 for (const auto &S : Obj.symbols()) {
84 const object::coff_symbol *Symbol = Obj.getCOFFSymbol(S);
86 Obj.getSymbolName(Symbol, Sym.Name);
87 Sym.SimpleType = COFF::SymbolBaseType(Symbol->getBaseType());
88 Sym.ComplexType = COFF::SymbolComplexType(Symbol->getComplexType());
89 Sym.Header.StorageClass = Symbol->StorageClass;
90 Sym.Header.Value = Symbol->Value;
91 Sym.Header.SectionNumber = Symbol->SectionNumber;
92 Sym.Header.NumberOfAuxSymbols = Symbol->NumberOfAuxSymbols;
93 Sym.AuxiliaryData = object::yaml::BinaryRef(Obj.getSymbolAuxData(Symbol));
94 Symbols.push_back(Sym);
98 COFFYAML::Object &COFFDumper::getYAMLObj() {
102 error_code coff2yaml(raw_ostream &Out, MemoryBuffer *Buff) {
104 object::COFFObjectFile Obj(Buff, ec);
106 COFFDumper Dumper(Obj);
108 yaml::Output Yout(Out);
109 Yout << Dumper.getYAMLObj();
111 return object::object_error::success;