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/DebugInfo/CodeView/DebugChecksumsSubsection.h"
12 #include "llvm/DebugInfo/CodeView/DebugStringTableSubsection.h"
13 #include "llvm/DebugInfo/CodeView/StringsAndChecksums.h"
14 #include "llvm/Object/COFF.h"
15 #include "llvm/ObjectYAML/COFFYAML.h"
16 #include "llvm/ObjectYAML/CodeViewYAMLTypes.h"
17 #include "llvm/Support/ErrorHandling.h"
18 #include "llvm/Support/YAMLTraits.h"
19
20 using namespace llvm;
21
22 namespace {
23
24 class COFFDumper {
25 const object::COFFObjectFile &Obj;
26 COFFYAML::Object YAMLObj;
27 template <typename T>
28 void dumpOptionalHeader(T OptionalHeader);
29 void dumpHeader();
30 void dumpSections(unsigned numSections);
31 void dumpSymbols(unsigned numSymbols);
32
33 public:
34 COFFDumper(const object::COFFObjectFile &Obj);
35 COFFYAML::Object &getYAMLObj();
36 };
37
38 }
39
COFFDumper(const object::COFFObjectFile & Obj)40 COFFDumper::COFFDumper(const object::COFFObjectFile &Obj) : Obj(Obj) {
41 const object::pe32_header *PE32Header = nullptr;
42 Obj.getPE32Header(PE32Header);
43 if (PE32Header) {
44 dumpOptionalHeader(PE32Header);
45 } else {
46 const object::pe32plus_header *PE32PlusHeader = nullptr;
47 Obj.getPE32PlusHeader(PE32PlusHeader);
48 if (PE32PlusHeader) {
49 dumpOptionalHeader(PE32PlusHeader);
50 }
51 }
52 dumpHeader();
53 dumpSections(Obj.getNumberOfSections());
54 dumpSymbols(Obj.getNumberOfSymbols());
55 }
56
dumpOptionalHeader(T OptionalHeader)57 template <typename T> void COFFDumper::dumpOptionalHeader(T OptionalHeader) {
58 YAMLObj.OptionalHeader = COFFYAML::PEHeader();
59 YAMLObj.OptionalHeader->Header.AddressOfEntryPoint =
60 OptionalHeader->AddressOfEntryPoint;
61 YAMLObj.OptionalHeader->Header.AddressOfEntryPoint =
62 OptionalHeader->AddressOfEntryPoint;
63 YAMLObj.OptionalHeader->Header.ImageBase = OptionalHeader->ImageBase;
64 YAMLObj.OptionalHeader->Header.SectionAlignment =
65 OptionalHeader->SectionAlignment;
66 YAMLObj.OptionalHeader->Header.FileAlignment = OptionalHeader->FileAlignment;
67 YAMLObj.OptionalHeader->Header.MajorOperatingSystemVersion =
68 OptionalHeader->MajorOperatingSystemVersion;
69 YAMLObj.OptionalHeader->Header.MinorOperatingSystemVersion =
70 OptionalHeader->MinorOperatingSystemVersion;
71 YAMLObj.OptionalHeader->Header.MajorImageVersion =
72 OptionalHeader->MajorImageVersion;
73 YAMLObj.OptionalHeader->Header.MinorImageVersion =
74 OptionalHeader->MinorImageVersion;
75 YAMLObj.OptionalHeader->Header.MajorSubsystemVersion =
76 OptionalHeader->MajorSubsystemVersion;
77 YAMLObj.OptionalHeader->Header.MinorSubsystemVersion =
78 OptionalHeader->MinorSubsystemVersion;
79 YAMLObj.OptionalHeader->Header.Subsystem = OptionalHeader->Subsystem;
80 YAMLObj.OptionalHeader->Header.DLLCharacteristics =
81 OptionalHeader->DLLCharacteristics;
82 YAMLObj.OptionalHeader->Header.SizeOfStackReserve =
83 OptionalHeader->SizeOfStackReserve;
84 YAMLObj.OptionalHeader->Header.SizeOfStackCommit =
85 OptionalHeader->SizeOfStackCommit;
86 YAMLObj.OptionalHeader->Header.SizeOfHeapReserve =
87 OptionalHeader->SizeOfHeapReserve;
88 YAMLObj.OptionalHeader->Header.SizeOfHeapCommit =
89 OptionalHeader->SizeOfHeapCommit;
90 unsigned I = 0;
91 for (auto &DestDD : YAMLObj.OptionalHeader->DataDirectories) {
92 const object::data_directory *DD;
93 if (Obj.getDataDirectory(I++, DD))
94 continue;
95 DestDD = COFF::DataDirectory();
96 DestDD->RelativeVirtualAddress = DD->RelativeVirtualAddress;
97 DestDD->Size = DD->Size;
98 }
99 }
100
dumpHeader()101 void COFFDumper::dumpHeader() {
102 YAMLObj.Header.Machine = Obj.getMachine();
103 YAMLObj.Header.Characteristics = Obj.getCharacteristics();
104 }
105
106 static void
initializeFileAndStringTable(const llvm::object::COFFObjectFile & Obj,codeview::StringsAndChecksumsRef & SC)107 initializeFileAndStringTable(const llvm::object::COFFObjectFile &Obj,
108 codeview::StringsAndChecksumsRef &SC) {
109
110 ExitOnError Err("Invalid .debug$S section!");
111 // Iterate all .debug$S sections looking for the checksums and string table.
112 // Exit as soon as both sections are found.
113 for (const auto &S : Obj.sections()) {
114 if (SC.hasStrings() && SC.hasChecksums())
115 break;
116
117 StringRef SectionName;
118 S.getName(SectionName);
119 ArrayRef<uint8_t> sectionData;
120 if (SectionName != ".debug$S")
121 continue;
122
123 const object::coff_section *COFFSection = Obj.getCOFFSection(S);
124
125 Obj.getSectionContents(COFFSection, sectionData);
126
127 BinaryStreamReader Reader(sectionData, support::little);
128 uint32_t Magic;
129
130 Err(Reader.readInteger(Magic));
131 assert(Magic == COFF::DEBUG_SECTION_MAGIC && "Invalid .debug$S section!");
132
133 codeview::DebugSubsectionArray Subsections;
134 Err(Reader.readArray(Subsections, Reader.bytesRemaining()));
135
136 SC.initialize(Subsections);
137 }
138 }
139
dumpSections(unsigned NumSections)140 void COFFDumper::dumpSections(unsigned NumSections) {
141 std::vector<COFFYAML::Section> &YAMLSections = YAMLObj.Sections;
142 codeview::StringsAndChecksumsRef SC;
143 initializeFileAndStringTable(Obj, SC);
144
145 for (const auto &ObjSection : Obj.sections()) {
146 const object::coff_section *COFFSection = Obj.getCOFFSection(ObjSection);
147 COFFYAML::Section NewYAMLSection;
148 ObjSection.getName(NewYAMLSection.Name);
149 NewYAMLSection.Header.Characteristics = COFFSection->Characteristics;
150 NewYAMLSection.Header.VirtualAddress = ObjSection.getAddress();
151 NewYAMLSection.Header.VirtualSize = COFFSection->VirtualSize;
152 NewYAMLSection.Header.NumberOfLineNumbers =
153 COFFSection->NumberOfLinenumbers;
154 NewYAMLSection.Header.NumberOfRelocations =
155 COFFSection->NumberOfRelocations;
156 NewYAMLSection.Header.PointerToLineNumbers =
157 COFFSection->PointerToLinenumbers;
158 NewYAMLSection.Header.PointerToRawData = COFFSection->PointerToRawData;
159 NewYAMLSection.Header.PointerToRelocations =
160 COFFSection->PointerToRelocations;
161 NewYAMLSection.Header.SizeOfRawData = COFFSection->SizeOfRawData;
162 uint32_t Shift = (COFFSection->Characteristics >> 20) & 0xF;
163 NewYAMLSection.Alignment = (1U << Shift) >> 1;
164 assert(NewYAMLSection.Alignment <= 8192);
165
166 ArrayRef<uint8_t> sectionData;
167 if (!ObjSection.isBSS())
168 Obj.getSectionContents(COFFSection, sectionData);
169 NewYAMLSection.SectionData = yaml::BinaryRef(sectionData);
170
171 if (NewYAMLSection.Name == ".debug$S")
172 NewYAMLSection.DebugS = CodeViewYAML::fromDebugS(sectionData, SC);
173 else if (NewYAMLSection.Name == ".debug$T")
174 NewYAMLSection.DebugT = CodeViewYAML::fromDebugT(sectionData,
175 NewYAMLSection.Name);
176 else if (NewYAMLSection.Name == ".debug$P")
177 NewYAMLSection.DebugP = CodeViewYAML::fromDebugT(sectionData,
178 NewYAMLSection.Name);
179 else if (NewYAMLSection.Name == ".debug$H")
180 NewYAMLSection.DebugH = CodeViewYAML::fromDebugH(sectionData);
181
182 std::vector<COFFYAML::Relocation> Relocations;
183 for (const auto &Reloc : ObjSection.relocations()) {
184 const object::coff_relocation *reloc = Obj.getCOFFRelocation(Reloc);
185 COFFYAML::Relocation Rel;
186 object::symbol_iterator Sym = Reloc.getSymbol();
187 Expected<StringRef> SymbolNameOrErr = Sym->getName();
188 if (!SymbolNameOrErr) {
189 std::string Buf;
190 raw_string_ostream OS(Buf);
191 logAllUnhandledErrors(SymbolNameOrErr.takeError(), OS, "");
192 OS.flush();
193 report_fatal_error(Buf);
194 }
195 Rel.SymbolName = *SymbolNameOrErr;
196 Rel.VirtualAddress = reloc->VirtualAddress;
197 Rel.Type = reloc->Type;
198 Relocations.push_back(Rel);
199 }
200 NewYAMLSection.Relocations = Relocations;
201 YAMLSections.push_back(NewYAMLSection);
202 }
203 }
204
205 static void
dumpFunctionDefinition(COFFYAML::Symbol * Sym,const object::coff_aux_function_definition * ObjFD)206 dumpFunctionDefinition(COFFYAML::Symbol *Sym,
207 const object::coff_aux_function_definition *ObjFD) {
208 COFF::AuxiliaryFunctionDefinition YAMLFD;
209 YAMLFD.TagIndex = ObjFD->TagIndex;
210 YAMLFD.TotalSize = ObjFD->TotalSize;
211 YAMLFD.PointerToLinenumber = ObjFD->PointerToLinenumber;
212 YAMLFD.PointerToNextFunction = ObjFD->PointerToNextFunction;
213
214 Sym->FunctionDefinition = YAMLFD;
215 }
216
217 static void
dumpbfAndEfLineInfo(COFFYAML::Symbol * Sym,const object::coff_aux_bf_and_ef_symbol * ObjBES)218 dumpbfAndEfLineInfo(COFFYAML::Symbol *Sym,
219 const object::coff_aux_bf_and_ef_symbol *ObjBES) {
220 COFF::AuxiliarybfAndefSymbol YAMLAAS;
221 YAMLAAS.Linenumber = ObjBES->Linenumber;
222 YAMLAAS.PointerToNextFunction = ObjBES->PointerToNextFunction;
223
224 Sym->bfAndefSymbol = YAMLAAS;
225 }
226
dumpWeakExternal(COFFYAML::Symbol * Sym,const object::coff_aux_weak_external * ObjWE)227 static void dumpWeakExternal(COFFYAML::Symbol *Sym,
228 const object::coff_aux_weak_external *ObjWE) {
229 COFF::AuxiliaryWeakExternal YAMLWE;
230 YAMLWE.TagIndex = ObjWE->TagIndex;
231 YAMLWE.Characteristics = ObjWE->Characteristics;
232
233 Sym->WeakExternal = YAMLWE;
234 }
235
236 static void
dumpSectionDefinition(COFFYAML::Symbol * Sym,const object::coff_aux_section_definition * ObjSD,bool IsBigObj)237 dumpSectionDefinition(COFFYAML::Symbol *Sym,
238 const object::coff_aux_section_definition *ObjSD,
239 bool IsBigObj) {
240 COFF::AuxiliarySectionDefinition YAMLASD;
241 int32_t AuxNumber = ObjSD->getNumber(IsBigObj);
242 YAMLASD.Length = ObjSD->Length;
243 YAMLASD.NumberOfRelocations = ObjSD->NumberOfRelocations;
244 YAMLASD.NumberOfLinenumbers = ObjSD->NumberOfLinenumbers;
245 YAMLASD.CheckSum = ObjSD->CheckSum;
246 YAMLASD.Number = AuxNumber;
247 YAMLASD.Selection = ObjSD->Selection;
248
249 Sym->SectionDefinition = YAMLASD;
250 }
251
252 static void
dumpCLRTokenDefinition(COFFYAML::Symbol * Sym,const object::coff_aux_clr_token * ObjCLRToken)253 dumpCLRTokenDefinition(COFFYAML::Symbol *Sym,
254 const object::coff_aux_clr_token *ObjCLRToken) {
255 COFF::AuxiliaryCLRToken YAMLCLRToken;
256 YAMLCLRToken.AuxType = ObjCLRToken->AuxType;
257 YAMLCLRToken.SymbolTableIndex = ObjCLRToken->SymbolTableIndex;
258
259 Sym->CLRToken = YAMLCLRToken;
260 }
261
dumpSymbols(unsigned NumSymbols)262 void COFFDumper::dumpSymbols(unsigned NumSymbols) {
263 std::vector<COFFYAML::Symbol> &Symbols = YAMLObj.Symbols;
264 for (const auto &S : Obj.symbols()) {
265 object::COFFSymbolRef Symbol = Obj.getCOFFSymbol(S);
266 COFFYAML::Symbol Sym;
267 Obj.getSymbolName(Symbol, Sym.Name);
268 Sym.SimpleType = COFF::SymbolBaseType(Symbol.getBaseType());
269 Sym.ComplexType = COFF::SymbolComplexType(Symbol.getComplexType());
270 Sym.Header.StorageClass = Symbol.getStorageClass();
271 Sym.Header.Value = Symbol.getValue();
272 Sym.Header.SectionNumber = Symbol.getSectionNumber();
273 Sym.Header.NumberOfAuxSymbols = Symbol.getNumberOfAuxSymbols();
274
275 if (Symbol.getNumberOfAuxSymbols() > 0) {
276 ArrayRef<uint8_t> AuxData = Obj.getSymbolAuxData(Symbol);
277 if (Symbol.isFunctionDefinition()) {
278 // This symbol represents a function definition.
279 assert(Symbol.getNumberOfAuxSymbols() == 1 &&
280 "Expected a single aux symbol to describe this function!");
281
282 const object::coff_aux_function_definition *ObjFD =
283 reinterpret_cast<const object::coff_aux_function_definition *>(
284 AuxData.data());
285 dumpFunctionDefinition(&Sym, ObjFD);
286 } else if (Symbol.isFunctionLineInfo()) {
287 // This symbol describes function line number information.
288 assert(Symbol.getNumberOfAuxSymbols() == 1 &&
289 "Expected a single aux symbol to describe this function!");
290
291 const object::coff_aux_bf_and_ef_symbol *ObjBES =
292 reinterpret_cast<const object::coff_aux_bf_and_ef_symbol *>(
293 AuxData.data());
294 dumpbfAndEfLineInfo(&Sym, ObjBES);
295 } else if (Symbol.isAnyUndefined()) {
296 // This symbol represents a weak external definition.
297 assert(Symbol.getNumberOfAuxSymbols() == 1 &&
298 "Expected a single aux symbol to describe this weak symbol!");
299
300 const object::coff_aux_weak_external *ObjWE =
301 reinterpret_cast<const object::coff_aux_weak_external *>(
302 AuxData.data());
303 dumpWeakExternal(&Sym, ObjWE);
304 } else if (Symbol.isFileRecord()) {
305 // This symbol represents a file record.
306 Sym.File = StringRef(reinterpret_cast<const char *>(AuxData.data()),
307 Symbol.getNumberOfAuxSymbols() *
308 Obj.getSymbolTableEntrySize())
309 .rtrim(StringRef("\0", /*length=*/1));
310 } else if (Symbol.isSectionDefinition()) {
311 // This symbol represents a section definition.
312 assert(Symbol.getNumberOfAuxSymbols() == 1 &&
313 "Expected a single aux symbol to describe this section!");
314
315 const object::coff_aux_section_definition *ObjSD =
316 reinterpret_cast<const object::coff_aux_section_definition *>(
317 AuxData.data());
318 dumpSectionDefinition(&Sym, ObjSD, Symbol.isBigObj());
319 } else if (Symbol.isCLRToken()) {
320 // This symbol represents a CLR token definition.
321 assert(Symbol.getNumberOfAuxSymbols() == 1 &&
322 "Expected a single aux symbol to describe this CLR Token!");
323
324 const object::coff_aux_clr_token *ObjCLRToken =
325 reinterpret_cast<const object::coff_aux_clr_token *>(
326 AuxData.data());
327 dumpCLRTokenDefinition(&Sym, ObjCLRToken);
328 } else {
329 llvm_unreachable("Unhandled auxiliary symbol!");
330 }
331 }
332 Symbols.push_back(Sym);
333 }
334 }
335
getYAMLObj()336 COFFYAML::Object &COFFDumper::getYAMLObj() {
337 return YAMLObj;
338 }
339
coff2yaml(raw_ostream & Out,const object::COFFObjectFile & Obj)340 std::error_code coff2yaml(raw_ostream &Out, const object::COFFObjectFile &Obj) {
341 COFFDumper Dumper(Obj);
342
343 yaml::Output Yout(Out);
344 Yout << Dumper.getYAMLObj();
345
346 return std::error_code();
347 }
348