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;
24 void dumpSections(unsigned numSections);
25 void dumpSymbols(unsigned numSymbols);
28 COFFDumper(const object::COFFObjectFile &Obj);
29 COFFYAML::Object &getYAMLObj();
34 COFFDumper::COFFDumper(const object::COFFObjectFile &Obj) : Obj(Obj) {
36 dumpSections(Obj.getNumberOfSections());
37 dumpSymbols(Obj.getNumberOfSymbols());
40 void COFFDumper::dumpHeader() {
41 YAMLObj.Header.Machine = Obj.getMachine();
42 YAMLObj.Header.Characteristics = Obj.getCharacteristics();
45 void COFFDumper::dumpSections(unsigned NumSections) {
46 std::vector<COFFYAML::Section> &Sections = YAMLObj.Sections;
47 for (const auto &Section : Obj.sections()) {
48 const object::coff_section *Sect = Obj.getCOFFSection(Section);
49 COFFYAML::Section Sec;
50 Section.getName(Sec.Name);
51 Sec.Header.Characteristics = Sect->Characteristics;
52 Sec.Alignment = Section.getAlignment();
54 ArrayRef<uint8_t> sectionData;
56 Obj.getSectionContents(Sect, sectionData);
57 Sec.SectionData = yaml::BinaryRef(sectionData);
59 std::vector<COFFYAML::Relocation> Relocations;
60 for (const auto &Reloc : Section.relocations()) {
61 const object::coff_relocation *reloc = Obj.getCOFFRelocation(Reloc);
62 COFFYAML::Relocation Rel;
63 object::symbol_iterator Sym = Reloc.getSymbol();
64 Sym->getName(Rel.SymbolName);
65 Rel.VirtualAddress = reloc->VirtualAddress;
66 Rel.Type = reloc->Type;
67 Relocations.push_back(Rel);
69 Sec.Relocations = Relocations;
70 Sections.push_back(Sec);
75 dumpFunctionDefinition(COFFYAML::Symbol *Sym,
76 const object::coff_aux_function_definition *ObjFD) {
77 COFF::AuxiliaryFunctionDefinition YAMLFD;
78 YAMLFD.TagIndex = ObjFD->TagIndex;
79 YAMLFD.TotalSize = ObjFD->TotalSize;
80 YAMLFD.PointerToLinenumber = ObjFD->PointerToLinenumber;
81 YAMLFD.PointerToNextFunction = ObjFD->PointerToNextFunction;
83 Sym->FunctionDefinition = YAMLFD;
87 dumpbfAndEfLineInfo(COFFYAML::Symbol *Sym,
88 const object::coff_aux_bf_and_ef_symbol *ObjBES) {
89 COFF::AuxiliarybfAndefSymbol YAMLAAS;
90 YAMLAAS.Linenumber = ObjBES->Linenumber;
91 YAMLAAS.PointerToNextFunction = ObjBES->PointerToNextFunction;
93 Sym->bfAndefSymbol = YAMLAAS;
96 static void dumpWeakExternal(COFFYAML::Symbol *Sym,
97 const object::coff_aux_weak_external *ObjWE) {
98 COFF::AuxiliaryWeakExternal YAMLWE;
99 YAMLWE.TagIndex = ObjWE->TagIndex;
100 YAMLWE.Characteristics = ObjWE->Characteristics;
102 Sym->WeakExternal = YAMLWE;
106 dumpSectionDefinition(COFFYAML::Symbol *Sym,
107 const object::coff_aux_section_definition *ObjSD,
109 COFF::AuxiliarySectionDefinition YAMLASD;
110 int32_t AuxNumber = ObjSD->getNumber(IsBigObj);
111 YAMLASD.Length = ObjSD->Length;
112 YAMLASD.NumberOfRelocations = ObjSD->NumberOfRelocations;
113 YAMLASD.NumberOfLinenumbers = ObjSD->NumberOfLinenumbers;
114 YAMLASD.CheckSum = ObjSD->CheckSum;
115 YAMLASD.Number = AuxNumber;
116 YAMLASD.Selection = ObjSD->Selection;
118 Sym->SectionDefinition = YAMLASD;
122 dumpCLRTokenDefinition(COFFYAML::Symbol *Sym,
123 const object::coff_aux_clr_token *ObjCLRToken) {
124 COFF::AuxiliaryCLRToken YAMLCLRToken;
125 YAMLCLRToken.AuxType = ObjCLRToken->AuxType;
126 YAMLCLRToken.SymbolTableIndex = ObjCLRToken->SymbolTableIndex;
128 Sym->CLRToken = YAMLCLRToken;
131 void COFFDumper::dumpSymbols(unsigned NumSymbols) {
132 std::vector<COFFYAML::Symbol> &Symbols = YAMLObj.Symbols;
133 for (const auto &S : Obj.symbols()) {
134 object::COFFSymbolRef Symbol = Obj.getCOFFSymbol(S);
135 COFFYAML::Symbol Sym;
136 Obj.getSymbolName(Symbol, Sym.Name);
137 Sym.SimpleType = COFF::SymbolBaseType(Symbol.getBaseType());
138 Sym.ComplexType = COFF::SymbolComplexType(Symbol.getComplexType());
139 Sym.Header.StorageClass = Symbol.getStorageClass();
140 Sym.Header.Value = Symbol.getValue();
141 Sym.Header.SectionNumber = Symbol.getSectionNumber();
142 Sym.Header.NumberOfAuxSymbols = Symbol.getNumberOfAuxSymbols();
144 if (Symbol.getNumberOfAuxSymbols() > 0) {
145 ArrayRef<uint8_t> AuxData = Obj.getSymbolAuxData(Symbol);
146 if (Symbol.isFunctionDefinition()) {
147 // This symbol represents a function definition.
148 assert(Symbol.getNumberOfAuxSymbols() == 1 &&
149 "Expected a single aux symbol to describe this function!");
151 const object::coff_aux_function_definition *ObjFD =
152 reinterpret_cast<const object::coff_aux_function_definition *>(
154 dumpFunctionDefinition(&Sym, ObjFD);
155 } else if (Symbol.isFunctionLineInfo()) {
156 // This symbol describes function line number information.
157 assert(Symbol.getNumberOfAuxSymbols() == 1 &&
158 "Expected a single aux symbol to describe this function!");
160 const object::coff_aux_bf_and_ef_symbol *ObjBES =
161 reinterpret_cast<const object::coff_aux_bf_and_ef_symbol *>(
163 dumpbfAndEfLineInfo(&Sym, ObjBES);
164 } else if (Symbol.isAnyUndefined()) {
165 // This symbol represents a weak external definition.
166 assert(Symbol.getNumberOfAuxSymbols() == 1 &&
167 "Expected a single aux symbol to describe this weak symbol!");
169 const object::coff_aux_weak_external *ObjWE =
170 reinterpret_cast<const object::coff_aux_weak_external *>(
172 dumpWeakExternal(&Sym, ObjWE);
173 } else if (Symbol.isFileRecord()) {
174 // This symbol represents a file record.
175 Sym.File = StringRef(reinterpret_cast<const char *>(AuxData.data()),
176 Symbol.getNumberOfAuxSymbols() *
177 Obj.getSymbolTableEntrySize())
178 .rtrim(StringRef("\0", /*length=*/1));
179 } else if (Symbol.isSectionDefinition()) {
180 // This symbol represents a section definition.
181 assert(Symbol.getNumberOfAuxSymbols() == 1 &&
182 "Expected a single aux symbol to describe this section!");
184 const object::coff_aux_section_definition *ObjSD =
185 reinterpret_cast<const object::coff_aux_section_definition *>(
187 dumpSectionDefinition(&Sym, ObjSD, Symbol.isBigObj());
188 } else if (Symbol.isCLRToken()) {
189 // This symbol represents a CLR token definition.
190 assert(Symbol.getNumberOfAuxSymbols() == 1 &&
191 "Expected a single aux symbol to describe this CLR Token!");
193 const object::coff_aux_clr_token *ObjCLRToken =
194 reinterpret_cast<const object::coff_aux_clr_token *>(
196 dumpCLRTokenDefinition(&Sym, ObjCLRToken);
198 llvm_unreachable("Unhandled auxiliary symbol!");
201 Symbols.push_back(Sym);
205 COFFYAML::Object &COFFDumper::getYAMLObj() {
209 std::error_code coff2yaml(raw_ostream &Out, const object::COFFObjectFile &Obj) {
210 COFFDumper Dumper(Obj);
212 yaml::Output Yout(Out);
213 Yout << Dumper.getYAMLObj();
215 return object::object_error::success;