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