1 //===------ utils/obj2yaml.cpp - obj2yaml conversion tool -------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "obj2yaml.h"
11 #include "llvm/Object/COFF.h"
12 #include "llvm/Object/COFFYAML.h"
13 #include "llvm/Support/ErrorHandling.h"
14 #include "llvm/Support/YAMLTraits.h"
15
16 using namespace llvm;
17
18 namespace {
19
20 class COFFDumper {
21 const object::COFFObjectFile &Obj;
22 COFFYAML::Object YAMLObj;
23 template <typename T>
24 void dumpOptionalHeader(T OptionalHeader);
25 void dumpHeader();
26 void dumpSections(unsigned numSections);
27 void dumpSymbols(unsigned numSymbols);
28
29 public:
30 COFFDumper(const object::COFFObjectFile &Obj);
31 COFFYAML::Object &getYAMLObj();
32 };
33
34 }
35
COFFDumper(const object::COFFObjectFile & Obj)36 COFFDumper::COFFDumper(const object::COFFObjectFile &Obj) : Obj(Obj) {
37 const object::pe32_header *PE32Header = nullptr;
38 Obj.getPE32Header(PE32Header);
39 if (PE32Header) {
40 dumpOptionalHeader(PE32Header);
41 } else {
42 const object::pe32plus_header *PE32PlusHeader = nullptr;
43 Obj.getPE32PlusHeader(PE32PlusHeader);
44 if (PE32PlusHeader) {
45 dumpOptionalHeader(PE32PlusHeader);
46 }
47 }
48 dumpHeader();
49 dumpSections(Obj.getNumberOfSections());
50 dumpSymbols(Obj.getNumberOfSymbols());
51 }
52
dumpOptionalHeader(T OptionalHeader)53 template <typename T> void COFFDumper::dumpOptionalHeader(T OptionalHeader) {
54 YAMLObj.OptionalHeader = COFFYAML::PEHeader();
55 YAMLObj.OptionalHeader->Header.AddressOfEntryPoint =
56 OptionalHeader->AddressOfEntryPoint;
57 YAMLObj.OptionalHeader->Header.AddressOfEntryPoint =
58 OptionalHeader->AddressOfEntryPoint;
59 YAMLObj.OptionalHeader->Header.ImageBase = OptionalHeader->ImageBase;
60 YAMLObj.OptionalHeader->Header.SectionAlignment =
61 OptionalHeader->SectionAlignment;
62 YAMLObj.OptionalHeader->Header.FileAlignment = OptionalHeader->FileAlignment;
63 YAMLObj.OptionalHeader->Header.MajorOperatingSystemVersion =
64 OptionalHeader->MajorOperatingSystemVersion;
65 YAMLObj.OptionalHeader->Header.MinorOperatingSystemVersion =
66 OptionalHeader->MinorOperatingSystemVersion;
67 YAMLObj.OptionalHeader->Header.MajorImageVersion =
68 OptionalHeader->MajorImageVersion;
69 YAMLObj.OptionalHeader->Header.MinorImageVersion =
70 OptionalHeader->MinorImageVersion;
71 YAMLObj.OptionalHeader->Header.MajorSubsystemVersion =
72 OptionalHeader->MajorSubsystemVersion;
73 YAMLObj.OptionalHeader->Header.MinorSubsystemVersion =
74 OptionalHeader->MinorSubsystemVersion;
75 YAMLObj.OptionalHeader->Header.Subsystem = OptionalHeader->Subsystem;
76 YAMLObj.OptionalHeader->Header.DLLCharacteristics =
77 OptionalHeader->DLLCharacteristics;
78 YAMLObj.OptionalHeader->Header.SizeOfStackReserve =
79 OptionalHeader->SizeOfStackReserve;
80 YAMLObj.OptionalHeader->Header.SizeOfStackCommit =
81 OptionalHeader->SizeOfStackCommit;
82 YAMLObj.OptionalHeader->Header.SizeOfHeapReserve =
83 OptionalHeader->SizeOfHeapReserve;
84 YAMLObj.OptionalHeader->Header.SizeOfHeapCommit =
85 OptionalHeader->SizeOfHeapCommit;
86 unsigned I = 0;
87 for (auto &DestDD : YAMLObj.OptionalHeader->DataDirectories) {
88 const object::data_directory *DD;
89 if (Obj.getDataDirectory(I++, DD))
90 continue;
91 DestDD = COFF::DataDirectory();
92 DestDD->RelativeVirtualAddress = DD->RelativeVirtualAddress;
93 DestDD->Size = DD->Size;
94 }
95 }
96
dumpHeader()97 void COFFDumper::dumpHeader() {
98 YAMLObj.Header.Machine = Obj.getMachine();
99 YAMLObj.Header.Characteristics = Obj.getCharacteristics();
100 }
101
dumpSections(unsigned NumSections)102 void COFFDumper::dumpSections(unsigned NumSections) {
103 std::vector<COFFYAML::Section> &YAMLSections = YAMLObj.Sections;
104 for (const auto &ObjSection : Obj.sections()) {
105 const object::coff_section *COFFSection = Obj.getCOFFSection(ObjSection);
106 COFFYAML::Section NewYAMLSection;
107 ObjSection.getName(NewYAMLSection.Name);
108 NewYAMLSection.Header.Characteristics = COFFSection->Characteristics;
109 NewYAMLSection.Header.VirtualAddress = ObjSection.getAddress();
110 NewYAMLSection.Header.VirtualSize = COFFSection->VirtualSize;
111 NewYAMLSection.Alignment = ObjSection.getAlignment();
112
113 ArrayRef<uint8_t> sectionData;
114 if (!ObjSection.isBSS())
115 Obj.getSectionContents(COFFSection, sectionData);
116 NewYAMLSection.SectionData = yaml::BinaryRef(sectionData);
117
118 std::vector<COFFYAML::Relocation> Relocations;
119 for (const auto &Reloc : ObjSection.relocations()) {
120 const object::coff_relocation *reloc = Obj.getCOFFRelocation(Reloc);
121 COFFYAML::Relocation Rel;
122 object::symbol_iterator Sym = Reloc.getSymbol();
123 ErrorOr<StringRef> SymbolNameOrErr = Sym->getName();
124 if (std::error_code EC = SymbolNameOrErr.getError())
125 report_fatal_error(EC.message());
126 Rel.SymbolName = *SymbolNameOrErr;
127 Rel.VirtualAddress = reloc->VirtualAddress;
128 Rel.Type = reloc->Type;
129 Relocations.push_back(Rel);
130 }
131 NewYAMLSection.Relocations = Relocations;
132 YAMLSections.push_back(NewYAMLSection);
133 }
134 }
135
136 static void
dumpFunctionDefinition(COFFYAML::Symbol * Sym,const object::coff_aux_function_definition * ObjFD)137 dumpFunctionDefinition(COFFYAML::Symbol *Sym,
138 const object::coff_aux_function_definition *ObjFD) {
139 COFF::AuxiliaryFunctionDefinition YAMLFD;
140 YAMLFD.TagIndex = ObjFD->TagIndex;
141 YAMLFD.TotalSize = ObjFD->TotalSize;
142 YAMLFD.PointerToLinenumber = ObjFD->PointerToLinenumber;
143 YAMLFD.PointerToNextFunction = ObjFD->PointerToNextFunction;
144
145 Sym->FunctionDefinition = YAMLFD;
146 }
147
148 static void
dumpbfAndEfLineInfo(COFFYAML::Symbol * Sym,const object::coff_aux_bf_and_ef_symbol * ObjBES)149 dumpbfAndEfLineInfo(COFFYAML::Symbol *Sym,
150 const object::coff_aux_bf_and_ef_symbol *ObjBES) {
151 COFF::AuxiliarybfAndefSymbol YAMLAAS;
152 YAMLAAS.Linenumber = ObjBES->Linenumber;
153 YAMLAAS.PointerToNextFunction = ObjBES->PointerToNextFunction;
154
155 Sym->bfAndefSymbol = YAMLAAS;
156 }
157
dumpWeakExternal(COFFYAML::Symbol * Sym,const object::coff_aux_weak_external * ObjWE)158 static void dumpWeakExternal(COFFYAML::Symbol *Sym,
159 const object::coff_aux_weak_external *ObjWE) {
160 COFF::AuxiliaryWeakExternal YAMLWE;
161 YAMLWE.TagIndex = ObjWE->TagIndex;
162 YAMLWE.Characteristics = ObjWE->Characteristics;
163
164 Sym->WeakExternal = YAMLWE;
165 }
166
167 static void
dumpSectionDefinition(COFFYAML::Symbol * Sym,const object::coff_aux_section_definition * ObjSD,bool IsBigObj)168 dumpSectionDefinition(COFFYAML::Symbol *Sym,
169 const object::coff_aux_section_definition *ObjSD,
170 bool IsBigObj) {
171 COFF::AuxiliarySectionDefinition YAMLASD;
172 int32_t AuxNumber = ObjSD->getNumber(IsBigObj);
173 YAMLASD.Length = ObjSD->Length;
174 YAMLASD.NumberOfRelocations = ObjSD->NumberOfRelocations;
175 YAMLASD.NumberOfLinenumbers = ObjSD->NumberOfLinenumbers;
176 YAMLASD.CheckSum = ObjSD->CheckSum;
177 YAMLASD.Number = AuxNumber;
178 YAMLASD.Selection = ObjSD->Selection;
179
180 Sym->SectionDefinition = YAMLASD;
181 }
182
183 static void
dumpCLRTokenDefinition(COFFYAML::Symbol * Sym,const object::coff_aux_clr_token * ObjCLRToken)184 dumpCLRTokenDefinition(COFFYAML::Symbol *Sym,
185 const object::coff_aux_clr_token *ObjCLRToken) {
186 COFF::AuxiliaryCLRToken YAMLCLRToken;
187 YAMLCLRToken.AuxType = ObjCLRToken->AuxType;
188 YAMLCLRToken.SymbolTableIndex = ObjCLRToken->SymbolTableIndex;
189
190 Sym->CLRToken = YAMLCLRToken;
191 }
192
dumpSymbols(unsigned NumSymbols)193 void COFFDumper::dumpSymbols(unsigned NumSymbols) {
194 std::vector<COFFYAML::Symbol> &Symbols = YAMLObj.Symbols;
195 for (const auto &S : Obj.symbols()) {
196 object::COFFSymbolRef Symbol = Obj.getCOFFSymbol(S);
197 COFFYAML::Symbol Sym;
198 Obj.getSymbolName(Symbol, Sym.Name);
199 Sym.SimpleType = COFF::SymbolBaseType(Symbol.getBaseType());
200 Sym.ComplexType = COFF::SymbolComplexType(Symbol.getComplexType());
201 Sym.Header.StorageClass = Symbol.getStorageClass();
202 Sym.Header.Value = Symbol.getValue();
203 Sym.Header.SectionNumber = Symbol.getSectionNumber();
204 Sym.Header.NumberOfAuxSymbols = Symbol.getNumberOfAuxSymbols();
205
206 if (Symbol.getNumberOfAuxSymbols() > 0) {
207 ArrayRef<uint8_t> AuxData = Obj.getSymbolAuxData(Symbol);
208 if (Symbol.isFunctionDefinition()) {
209 // This symbol represents a function definition.
210 assert(Symbol.getNumberOfAuxSymbols() == 1 &&
211 "Expected a single aux symbol to describe this function!");
212
213 const object::coff_aux_function_definition *ObjFD =
214 reinterpret_cast<const object::coff_aux_function_definition *>(
215 AuxData.data());
216 dumpFunctionDefinition(&Sym, ObjFD);
217 } else if (Symbol.isFunctionLineInfo()) {
218 // This symbol describes function line number information.
219 assert(Symbol.getNumberOfAuxSymbols() == 1 &&
220 "Expected a single aux symbol to describe this function!");
221
222 const object::coff_aux_bf_and_ef_symbol *ObjBES =
223 reinterpret_cast<const object::coff_aux_bf_and_ef_symbol *>(
224 AuxData.data());
225 dumpbfAndEfLineInfo(&Sym, ObjBES);
226 } else if (Symbol.isAnyUndefined()) {
227 // This symbol represents a weak external definition.
228 assert(Symbol.getNumberOfAuxSymbols() == 1 &&
229 "Expected a single aux symbol to describe this weak symbol!");
230
231 const object::coff_aux_weak_external *ObjWE =
232 reinterpret_cast<const object::coff_aux_weak_external *>(
233 AuxData.data());
234 dumpWeakExternal(&Sym, ObjWE);
235 } else if (Symbol.isFileRecord()) {
236 // This symbol represents a file record.
237 Sym.File = StringRef(reinterpret_cast<const char *>(AuxData.data()),
238 Symbol.getNumberOfAuxSymbols() *
239 Obj.getSymbolTableEntrySize())
240 .rtrim(StringRef("\0", /*length=*/1));
241 } else if (Symbol.isSectionDefinition()) {
242 // This symbol represents a section definition.
243 assert(Symbol.getNumberOfAuxSymbols() == 1 &&
244 "Expected a single aux symbol to describe this section!");
245
246 const object::coff_aux_section_definition *ObjSD =
247 reinterpret_cast<const object::coff_aux_section_definition *>(
248 AuxData.data());
249 dumpSectionDefinition(&Sym, ObjSD, Symbol.isBigObj());
250 } else if (Symbol.isCLRToken()) {
251 // This symbol represents a CLR token definition.
252 assert(Symbol.getNumberOfAuxSymbols() == 1 &&
253 "Expected a single aux symbol to describe this CLR Token!");
254
255 const object::coff_aux_clr_token *ObjCLRToken =
256 reinterpret_cast<const object::coff_aux_clr_token *>(
257 AuxData.data());
258 dumpCLRTokenDefinition(&Sym, ObjCLRToken);
259 } else {
260 llvm_unreachable("Unhandled auxiliary symbol!");
261 }
262 }
263 Symbols.push_back(Sym);
264 }
265 }
266
getYAMLObj()267 COFFYAML::Object &COFFDumper::getYAMLObj() {
268 return YAMLObj;
269 }
270
coff2yaml(raw_ostream & Out,const object::COFFObjectFile & Obj)271 std::error_code coff2yaml(raw_ostream &Out, const object::COFFObjectFile &Obj) {
272 COFFDumper Dumper(Obj);
273
274 yaml::Output Yout(Out);
275 Yout << Dumper.getYAMLObj();
276
277 return std::error_code();
278 }
279