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 
COFFDumper(const object::COFFObjectFile & Obj)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 
dumpOptionalHeader(T OptionalHeader)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 = Obj.getDataDirectory(I++);
86     if (!DD)
87       continue;
88     DestDD = COFF::DataDirectory();
89     DestDD->RelativeVirtualAddress = DD->RelativeVirtualAddress;
90     DestDD->Size = DD->Size;
91   }
92 }
93 
dumpHeader()94 void COFFDumper::dumpHeader() {
95   YAMLObj.Header.Machine = Obj.getMachine();
96   YAMLObj.Header.Characteristics = Obj.getCharacteristics();
97 }
98 
99 static void
initializeFileAndStringTable(const llvm::object::COFFObjectFile & Obj,codeview::StringsAndChecksumsRef & SC)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 
dumpSections(unsigned NumSections)137 void COFFDumper::dumpSections(unsigned NumSections) {
138   std::vector<COFFYAML::Section> &YAMLSections = YAMLObj.Sections;
139   codeview::StringsAndChecksumsRef SC;
140   initializeFileAndStringTable(Obj, SC);
141 
142   ExitOnError Err("invalid section table");
143   StringMap<bool> SymbolUnique;
144   for (const auto &S : Obj.symbols()) {
145     StringRef Name = Err(Obj.getSymbolName(Obj.getCOFFSymbol(S)));
146     StringMap<bool>::iterator It;
147     bool Inserted;
148     std::tie(It, Inserted) = SymbolUnique.insert(std::make_pair(Name, true));
149     if (!Inserted)
150       It->second = false;
151   }
152 
153   for (const auto &ObjSection : Obj.sections()) {
154     const object::coff_section *COFFSection = Obj.getCOFFSection(ObjSection);
155     COFFYAML::Section NewYAMLSection;
156 
157     if (Expected<StringRef> NameOrErr = ObjSection.getName())
158       NewYAMLSection.Name = *NameOrErr;
159     else
160       consumeError(NameOrErr.takeError());
161 
162     NewYAMLSection.Header.Characteristics = COFFSection->Characteristics;
163     NewYAMLSection.Header.VirtualAddress = COFFSection->VirtualAddress;
164     NewYAMLSection.Header.VirtualSize = COFFSection->VirtualSize;
165     NewYAMLSection.Header.NumberOfLineNumbers =
166         COFFSection->NumberOfLinenumbers;
167     NewYAMLSection.Header.NumberOfRelocations =
168         COFFSection->NumberOfRelocations;
169     NewYAMLSection.Header.PointerToLineNumbers =
170         COFFSection->PointerToLinenumbers;
171     NewYAMLSection.Header.PointerToRawData = COFFSection->PointerToRawData;
172     NewYAMLSection.Header.PointerToRelocations =
173         COFFSection->PointerToRelocations;
174     NewYAMLSection.Header.SizeOfRawData = COFFSection->SizeOfRawData;
175     uint32_t Shift = (COFFSection->Characteristics >> 20) & 0xF;
176     NewYAMLSection.Alignment = (1U << Shift) >> 1;
177     assert(NewYAMLSection.Alignment <= 8192);
178 
179     ArrayRef<uint8_t> sectionData;
180     if (!ObjSection.isBSS())
181       cantFail(Obj.getSectionContents(COFFSection, sectionData));
182     NewYAMLSection.SectionData = yaml::BinaryRef(sectionData);
183 
184     if (NewYAMLSection.Name == ".debug$S")
185       NewYAMLSection.DebugS = CodeViewYAML::fromDebugS(sectionData, SC);
186     else if (NewYAMLSection.Name == ".debug$T")
187       NewYAMLSection.DebugT = CodeViewYAML::fromDebugT(sectionData,
188                                                        NewYAMLSection.Name);
189     else if (NewYAMLSection.Name == ".debug$P")
190       NewYAMLSection.DebugP = CodeViewYAML::fromDebugT(sectionData,
191                                                        NewYAMLSection.Name);
192     else if (NewYAMLSection.Name == ".debug$H")
193       NewYAMLSection.DebugH = CodeViewYAML::fromDebugH(sectionData);
194 
195     std::vector<COFFYAML::Relocation> Relocations;
196     for (const auto &Reloc : ObjSection.relocations()) {
197       const object::coff_relocation *reloc = Obj.getCOFFRelocation(Reloc);
198       COFFYAML::Relocation Rel;
199       object::symbol_iterator Sym = Reloc.getSymbol();
200       Expected<StringRef> SymbolNameOrErr = Sym->getName();
201       if (!SymbolNameOrErr) {
202        std::string Buf;
203        raw_string_ostream OS(Buf);
204        logAllUnhandledErrors(SymbolNameOrErr.takeError(), OS);
205        OS.flush();
206        report_fatal_error(Buf);
207       }
208       if (SymbolUnique.lookup(*SymbolNameOrErr))
209         Rel.SymbolName = *SymbolNameOrErr;
210       else
211         Rel.SymbolTableIndex = reloc->SymbolTableIndex;
212       Rel.VirtualAddress = reloc->VirtualAddress;
213       Rel.Type = reloc->Type;
214       Relocations.push_back(Rel);
215     }
216     NewYAMLSection.Relocations = Relocations;
217     YAMLSections.push_back(NewYAMLSection);
218   }
219 }
220 
221 static void
dumpFunctionDefinition(COFFYAML::Symbol * Sym,const object::coff_aux_function_definition * ObjFD)222 dumpFunctionDefinition(COFFYAML::Symbol *Sym,
223                        const object::coff_aux_function_definition *ObjFD) {
224   COFF::AuxiliaryFunctionDefinition YAMLFD;
225   YAMLFD.TagIndex = ObjFD->TagIndex;
226   YAMLFD.TotalSize = ObjFD->TotalSize;
227   YAMLFD.PointerToLinenumber = ObjFD->PointerToLinenumber;
228   YAMLFD.PointerToNextFunction = ObjFD->PointerToNextFunction;
229 
230   Sym->FunctionDefinition = YAMLFD;
231 }
232 
233 static void
dumpbfAndEfLineInfo(COFFYAML::Symbol * Sym,const object::coff_aux_bf_and_ef_symbol * ObjBES)234 dumpbfAndEfLineInfo(COFFYAML::Symbol *Sym,
235                     const object::coff_aux_bf_and_ef_symbol *ObjBES) {
236   COFF::AuxiliarybfAndefSymbol YAMLAAS;
237   YAMLAAS.Linenumber = ObjBES->Linenumber;
238   YAMLAAS.PointerToNextFunction = ObjBES->PointerToNextFunction;
239 
240   Sym->bfAndefSymbol = YAMLAAS;
241 }
242 
dumpWeakExternal(COFFYAML::Symbol * Sym,const object::coff_aux_weak_external * ObjWE)243 static void dumpWeakExternal(COFFYAML::Symbol *Sym,
244                              const object::coff_aux_weak_external *ObjWE) {
245   COFF::AuxiliaryWeakExternal YAMLWE;
246   YAMLWE.TagIndex = ObjWE->TagIndex;
247   YAMLWE.Characteristics = ObjWE->Characteristics;
248 
249   Sym->WeakExternal = YAMLWE;
250 }
251 
252 static void
dumpSectionDefinition(COFFYAML::Symbol * Sym,const object::coff_aux_section_definition * ObjSD,bool IsBigObj)253 dumpSectionDefinition(COFFYAML::Symbol *Sym,
254                       const object::coff_aux_section_definition *ObjSD,
255                       bool IsBigObj) {
256   COFF::AuxiliarySectionDefinition YAMLASD;
257   int32_t AuxNumber = ObjSD->getNumber(IsBigObj);
258   YAMLASD.Length = ObjSD->Length;
259   YAMLASD.NumberOfRelocations = ObjSD->NumberOfRelocations;
260   YAMLASD.NumberOfLinenumbers = ObjSD->NumberOfLinenumbers;
261   YAMLASD.CheckSum = ObjSD->CheckSum;
262   YAMLASD.Number = AuxNumber;
263   YAMLASD.Selection = ObjSD->Selection;
264 
265   Sym->SectionDefinition = YAMLASD;
266 }
267 
268 static void
dumpCLRTokenDefinition(COFFYAML::Symbol * Sym,const object::coff_aux_clr_token * ObjCLRToken)269 dumpCLRTokenDefinition(COFFYAML::Symbol *Sym,
270                        const object::coff_aux_clr_token *ObjCLRToken) {
271   COFF::AuxiliaryCLRToken YAMLCLRToken;
272   YAMLCLRToken.AuxType = ObjCLRToken->AuxType;
273   YAMLCLRToken.SymbolTableIndex = ObjCLRToken->SymbolTableIndex;
274 
275   Sym->CLRToken = YAMLCLRToken;
276 }
277 
dumpSymbols(unsigned NumSymbols)278 void COFFDumper::dumpSymbols(unsigned NumSymbols) {
279   ExitOnError Err("invalid symbol table");
280 
281   std::vector<COFFYAML::Symbol> &Symbols = YAMLObj.Symbols;
282   for (const auto &S : Obj.symbols()) {
283     object::COFFSymbolRef Symbol = Obj.getCOFFSymbol(S);
284     COFFYAML::Symbol Sym;
285     Sym.Name = Err(Obj.getSymbolName(Symbol));
286     Sym.SimpleType = COFF::SymbolBaseType(Symbol.getBaseType());
287     Sym.ComplexType = COFF::SymbolComplexType(Symbol.getComplexType());
288     Sym.Header.StorageClass = Symbol.getStorageClass();
289     Sym.Header.Value = Symbol.getValue();
290     Sym.Header.SectionNumber = Symbol.getSectionNumber();
291     Sym.Header.NumberOfAuxSymbols = Symbol.getNumberOfAuxSymbols();
292 
293     if (Symbol.getNumberOfAuxSymbols() > 0) {
294       ArrayRef<uint8_t> AuxData = Obj.getSymbolAuxData(Symbol);
295       if (Symbol.isFunctionDefinition()) {
296         // This symbol represents a function definition.
297         assert(Symbol.getNumberOfAuxSymbols() == 1 &&
298                "Expected a single aux symbol to describe this function!");
299 
300         const object::coff_aux_function_definition *ObjFD =
301             reinterpret_cast<const object::coff_aux_function_definition *>(
302                 AuxData.data());
303         dumpFunctionDefinition(&Sym, ObjFD);
304       } else if (Symbol.isFunctionLineInfo()) {
305         // This symbol describes function line number information.
306         assert(Symbol.getNumberOfAuxSymbols() == 1 &&
307                "Expected a single aux symbol to describe this function!");
308 
309         const object::coff_aux_bf_and_ef_symbol *ObjBES =
310             reinterpret_cast<const object::coff_aux_bf_and_ef_symbol *>(
311                 AuxData.data());
312         dumpbfAndEfLineInfo(&Sym, ObjBES);
313       } else if (Symbol.isAnyUndefined()) {
314         // This symbol represents a weak external definition.
315         assert(Symbol.getNumberOfAuxSymbols() == 1 &&
316                "Expected a single aux symbol to describe this weak symbol!");
317 
318         const object::coff_aux_weak_external *ObjWE =
319             reinterpret_cast<const object::coff_aux_weak_external *>(
320                 AuxData.data());
321         dumpWeakExternal(&Sym, ObjWE);
322       } else if (Symbol.isFileRecord()) {
323         // This symbol represents a file record.
324         Sym.File = StringRef(reinterpret_cast<const char *>(AuxData.data()),
325                              Symbol.getNumberOfAuxSymbols() *
326                                  Obj.getSymbolTableEntrySize())
327                        .rtrim(StringRef("\0", /*length=*/1));
328       } else if (Symbol.isSectionDefinition()) {
329         // This symbol represents a section definition.
330         assert(Symbol.getNumberOfAuxSymbols() == 1 &&
331                "Expected a single aux symbol to describe this section!");
332 
333         const object::coff_aux_section_definition *ObjSD =
334             reinterpret_cast<const object::coff_aux_section_definition *>(
335                 AuxData.data());
336         dumpSectionDefinition(&Sym, ObjSD, Symbol.isBigObj());
337       } else if (Symbol.isCLRToken()) {
338         // This symbol represents a CLR token definition.
339         assert(Symbol.getNumberOfAuxSymbols() == 1 &&
340                "Expected a single aux symbol to describe this CLR Token!");
341 
342         const object::coff_aux_clr_token *ObjCLRToken =
343             reinterpret_cast<const object::coff_aux_clr_token *>(
344                 AuxData.data());
345         dumpCLRTokenDefinition(&Sym, ObjCLRToken);
346       } else {
347         llvm_unreachable("Unhandled auxiliary symbol!");
348       }
349     }
350     Symbols.push_back(Sym);
351   }
352 }
353 
getYAMLObj()354 COFFYAML::Object &COFFDumper::getYAMLObj() {
355   return YAMLObj;
356 }
357 
coff2yaml(raw_ostream & Out,const object::COFFObjectFile & Obj)358 std::error_code coff2yaml(raw_ostream &Out, const object::COFFObjectFile &Obj) {
359   COFFDumper Dumper(Obj);
360 
361   yaml::Output Yout(Out);
362   Yout << Dumper.getYAMLObj();
363 
364   return std::error_code();
365 }
366