1 //===- CodeViewYAMLDebugSections.cpp - CodeView YAMLIO debug sections -----===//
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 // This file defines classes for handling the YAML representation of CodeView
11 // Debug Info.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "llvm/ObjectYAML/CodeViewYAMLDebugSections.h"
16 
17 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/ADT/StringSwitch.h"
19 #include "llvm/DebugInfo/CodeView/CodeViewError.h"
20 #include "llvm/DebugInfo/CodeView/DebugChecksumsSubsection.h"
21 #include "llvm/DebugInfo/CodeView/DebugCrossExSubsection.h"
22 #include "llvm/DebugInfo/CodeView/DebugCrossImpSubsection.h"
23 #include "llvm/DebugInfo/CodeView/DebugFrameDataSubsection.h"
24 #include "llvm/DebugInfo/CodeView/DebugInlineeLinesSubsection.h"
25 #include "llvm/DebugInfo/CodeView/DebugLinesSubsection.h"
26 #include "llvm/DebugInfo/CodeView/DebugStringTableSubsection.h"
27 #include "llvm/DebugInfo/CodeView/DebugSubsectionVisitor.h"
28 #include "llvm/DebugInfo/CodeView/DebugSymbolRVASubsection.h"
29 #include "llvm/DebugInfo/CodeView/DebugSymbolsSubsection.h"
30 #include "llvm/DebugInfo/CodeView/EnumTables.h"
31 #include "llvm/DebugInfo/CodeView/StringsAndChecksums.h"
32 #include "llvm/DebugInfo/CodeView/SymbolRecord.h"
33 #include "llvm/DebugInfo/CodeView/SymbolSerializer.h"
34 #include "llvm/ObjectYAML/CodeViewYAMLSymbols.h"
35 #include "llvm/Support/BinaryStreamWriter.h"
36 using namespace llvm;
37 using namespace llvm::codeview;
38 using namespace llvm::CodeViewYAML;
39 using namespace llvm::CodeViewYAML::detail;
40 using namespace llvm::yaml;
41 
42 LLVM_YAML_IS_SEQUENCE_VECTOR(SourceFileChecksumEntry)
43 LLVM_YAML_IS_SEQUENCE_VECTOR(SourceLineEntry)
44 LLVM_YAML_IS_SEQUENCE_VECTOR(SourceColumnEntry)
45 LLVM_YAML_IS_SEQUENCE_VECTOR(SourceLineBlock)
46 LLVM_YAML_IS_SEQUENCE_VECTOR(SourceLineInfo)
47 LLVM_YAML_IS_SEQUENCE_VECTOR(InlineeSite)
48 LLVM_YAML_IS_SEQUENCE_VECTOR(InlineeInfo)
49 LLVM_YAML_IS_SEQUENCE_VECTOR(CrossModuleExport)
50 LLVM_YAML_IS_SEQUENCE_VECTOR(YAMLCrossModuleImport)
51 LLVM_YAML_IS_SEQUENCE_VECTOR(StringRef)
52 LLVM_YAML_IS_SEQUENCE_VECTOR(YAMLFrameData)
53 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(uint32_t)
54 
55 LLVM_YAML_DECLARE_SCALAR_TRAITS(HexFormattedString, false)
56 LLVM_YAML_DECLARE_ENUM_TRAITS(DebugSubsectionKind)
57 LLVM_YAML_DECLARE_ENUM_TRAITS(FileChecksumKind)
58 LLVM_YAML_DECLARE_BITSET_TRAITS(LineFlags)
59 
60 LLVM_YAML_DECLARE_MAPPING_TRAITS(CrossModuleExport)
61 LLVM_YAML_DECLARE_MAPPING_TRAITS(YAMLFrameData)
62 LLVM_YAML_DECLARE_MAPPING_TRAITS(YAMLCrossModuleImport)
63 LLVM_YAML_DECLARE_MAPPING_TRAITS(CrossModuleImportItem)
64 LLVM_YAML_DECLARE_MAPPING_TRAITS(SourceLineEntry)
65 LLVM_YAML_DECLARE_MAPPING_TRAITS(SourceColumnEntry)
66 LLVM_YAML_DECLARE_MAPPING_TRAITS(SourceFileChecksumEntry)
67 LLVM_YAML_DECLARE_MAPPING_TRAITS(SourceLineBlock)
68 LLVM_YAML_DECLARE_MAPPING_TRAITS(InlineeSite)
69 
70 namespace llvm {
71 namespace CodeViewYAML {
72 namespace detail {
73 struct YAMLSubsectionBase {
74   explicit YAMLSubsectionBase(DebugSubsectionKind Kind) : Kind(Kind) {}
75   DebugSubsectionKind Kind;
76   virtual ~YAMLSubsectionBase() {}
77 
78   virtual void map(IO &IO) = 0;
79   virtual std::shared_ptr<DebugSubsection>
80   toCodeViewSubsection(BumpPtrAllocator &Allocator,
81                        const codeview::StringsAndChecksums &SC) const = 0;
82 };
83 }
84 }
85 }
86 
87 namespace {
88 struct YAMLChecksumsSubsection : public YAMLSubsectionBase {
89   YAMLChecksumsSubsection()
90       : YAMLSubsectionBase(DebugSubsectionKind::FileChecksums) {}
91 
92   void map(IO &IO) override;
93   std::shared_ptr<DebugSubsection>
94   toCodeViewSubsection(BumpPtrAllocator &Allocator,
95                        const codeview::StringsAndChecksums &SC) const override;
96   static Expected<std::shared_ptr<YAMLChecksumsSubsection>>
97   fromCodeViewSubsection(const DebugStringTableSubsectionRef &Strings,
98                          const DebugChecksumsSubsectionRef &FC);
99 
100   std::vector<SourceFileChecksumEntry> Checksums;
101 };
102 
103 struct YAMLLinesSubsection : public YAMLSubsectionBase {
104   YAMLLinesSubsection() : YAMLSubsectionBase(DebugSubsectionKind::Lines) {}
105 
106   void map(IO &IO) override;
107   std::shared_ptr<DebugSubsection>
108   toCodeViewSubsection(BumpPtrAllocator &Allocator,
109                        const codeview::StringsAndChecksums &SC) const override;
110   static Expected<std::shared_ptr<YAMLLinesSubsection>>
111   fromCodeViewSubsection(const DebugStringTableSubsectionRef &Strings,
112                          const DebugChecksumsSubsectionRef &Checksums,
113                          const DebugLinesSubsectionRef &Lines);
114 
115   SourceLineInfo Lines;
116 };
117 
118 struct YAMLInlineeLinesSubsection : public YAMLSubsectionBase {
119   YAMLInlineeLinesSubsection()
120       : YAMLSubsectionBase(DebugSubsectionKind::InlineeLines) {}
121 
122   void map(IO &IO) override;
123   std::shared_ptr<DebugSubsection>
124   toCodeViewSubsection(BumpPtrAllocator &Allocator,
125                        const codeview::StringsAndChecksums &SC) const override;
126   static Expected<std::shared_ptr<YAMLInlineeLinesSubsection>>
127   fromCodeViewSubsection(const DebugStringTableSubsectionRef &Strings,
128                          const DebugChecksumsSubsectionRef &Checksums,
129                          const DebugInlineeLinesSubsectionRef &Lines);
130 
131   InlineeInfo InlineeLines;
132 };
133 
134 struct YAMLCrossModuleExportsSubsection : public YAMLSubsectionBase {
135   YAMLCrossModuleExportsSubsection()
136       : YAMLSubsectionBase(DebugSubsectionKind::CrossScopeExports) {}
137 
138   void map(IO &IO) override;
139   std::shared_ptr<DebugSubsection>
140   toCodeViewSubsection(BumpPtrAllocator &Allocator,
141                        const codeview::StringsAndChecksums &SC) const override;
142   static Expected<std::shared_ptr<YAMLCrossModuleExportsSubsection>>
143   fromCodeViewSubsection(const DebugCrossModuleExportsSubsectionRef &Exports);
144 
145   std::vector<CrossModuleExport> Exports;
146 };
147 
148 struct YAMLCrossModuleImportsSubsection : public YAMLSubsectionBase {
149   YAMLCrossModuleImportsSubsection()
150       : YAMLSubsectionBase(DebugSubsectionKind::CrossScopeImports) {}
151 
152   void map(IO &IO) override;
153   std::shared_ptr<DebugSubsection>
154   toCodeViewSubsection(BumpPtrAllocator &Allocator,
155                        const codeview::StringsAndChecksums &SC) const override;
156   static Expected<std::shared_ptr<YAMLCrossModuleImportsSubsection>>
157   fromCodeViewSubsection(const DebugStringTableSubsectionRef &Strings,
158                          const DebugCrossModuleImportsSubsectionRef &Imports);
159 
160   std::vector<YAMLCrossModuleImport> Imports;
161 };
162 
163 struct YAMLSymbolsSubsection : public YAMLSubsectionBase {
164   YAMLSymbolsSubsection() : YAMLSubsectionBase(DebugSubsectionKind::Symbols) {}
165 
166   void map(IO &IO) override;
167   std::shared_ptr<DebugSubsection>
168   toCodeViewSubsection(BumpPtrAllocator &Allocator,
169                        const codeview::StringsAndChecksums &SC) const override;
170   static Expected<std::shared_ptr<YAMLSymbolsSubsection>>
171   fromCodeViewSubsection(const DebugSymbolsSubsectionRef &Symbols);
172 
173   std::vector<CodeViewYAML::SymbolRecord> Symbols;
174 };
175 
176 struct YAMLStringTableSubsection : public YAMLSubsectionBase {
177   YAMLStringTableSubsection()
178       : YAMLSubsectionBase(DebugSubsectionKind::StringTable) {}
179 
180   void map(IO &IO) override;
181   std::shared_ptr<DebugSubsection>
182   toCodeViewSubsection(BumpPtrAllocator &Allocator,
183                        const codeview::StringsAndChecksums &SC) const override;
184   static Expected<std::shared_ptr<YAMLStringTableSubsection>>
185   fromCodeViewSubsection(const DebugStringTableSubsectionRef &Strings);
186 
187   std::vector<StringRef> Strings;
188 };
189 
190 struct YAMLFrameDataSubsection : public YAMLSubsectionBase {
191   YAMLFrameDataSubsection()
192       : YAMLSubsectionBase(DebugSubsectionKind::FrameData) {}
193 
194   void map(IO &IO) override;
195   std::shared_ptr<DebugSubsection>
196   toCodeViewSubsection(BumpPtrAllocator &Allocator,
197                        const codeview::StringsAndChecksums &SC) const override;
198   static Expected<std::shared_ptr<YAMLFrameDataSubsection>>
199   fromCodeViewSubsection(const DebugStringTableSubsectionRef &Strings,
200                          const DebugFrameDataSubsectionRef &Frames);
201 
202   std::vector<YAMLFrameData> Frames;
203 };
204 
205 struct YAMLCoffSymbolRVASubsection : public YAMLSubsectionBase {
206   YAMLCoffSymbolRVASubsection()
207       : YAMLSubsectionBase(DebugSubsectionKind::CoffSymbolRVA) {}
208 
209   void map(IO &IO) override;
210   std::shared_ptr<DebugSubsection>
211   toCodeViewSubsection(BumpPtrAllocator &Allocator,
212                        const codeview::StringsAndChecksums &SC) const override;
213   static Expected<std::shared_ptr<YAMLCoffSymbolRVASubsection>>
214   fromCodeViewSubsection(const DebugSymbolRVASubsectionRef &RVAs);
215 
216   std::vector<uint32_t> RVAs;
217 };
218 }
219 
220 void ScalarBitSetTraits<LineFlags>::bitset(IO &io, LineFlags &Flags) {
221   io.bitSetCase(Flags, "HasColumnInfo", LF_HaveColumns);
222   io.enumFallback<Hex16>(Flags);
223 }
224 
225 void ScalarEnumerationTraits<FileChecksumKind>::enumeration(
226     IO &io, FileChecksumKind &Kind) {
227   io.enumCase(Kind, "None", FileChecksumKind::None);
228   io.enumCase(Kind, "MD5", FileChecksumKind::MD5);
229   io.enumCase(Kind, "SHA1", FileChecksumKind::SHA1);
230   io.enumCase(Kind, "SHA256", FileChecksumKind::SHA256);
231 }
232 
233 void ScalarTraits<HexFormattedString>::output(const HexFormattedString &Value,
234                                               void *ctx, raw_ostream &Out) {
235   StringRef Bytes(reinterpret_cast<const char *>(Value.Bytes.data()),
236                   Value.Bytes.size());
237   Out << toHex(Bytes);
238 }
239 
240 StringRef ScalarTraits<HexFormattedString>::input(StringRef Scalar, void *ctxt,
241                                                   HexFormattedString &Value) {
242   std::string H = fromHex(Scalar);
243   Value.Bytes.assign(H.begin(), H.end());
244   return StringRef();
245 }
246 
247 void MappingTraits<SourceLineEntry>::mapping(IO &IO, SourceLineEntry &Obj) {
248   IO.mapRequired("Offset", Obj.Offset);
249   IO.mapRequired("LineStart", Obj.LineStart);
250   IO.mapRequired("IsStatement", Obj.IsStatement);
251   IO.mapRequired("EndDelta", Obj.EndDelta);
252 }
253 
254 void MappingTraits<SourceColumnEntry>::mapping(IO &IO, SourceColumnEntry &Obj) {
255   IO.mapRequired("StartColumn", Obj.StartColumn);
256   IO.mapRequired("EndColumn", Obj.EndColumn);
257 }
258 
259 void MappingTraits<SourceLineBlock>::mapping(IO &IO, SourceLineBlock &Obj) {
260   IO.mapRequired("FileName", Obj.FileName);
261   IO.mapRequired("Lines", Obj.Lines);
262   IO.mapRequired("Columns", Obj.Columns);
263 }
264 
265 void MappingTraits<CrossModuleExport>::mapping(IO &IO, CrossModuleExport &Obj) {
266   IO.mapRequired("LocalId", Obj.Local);
267   IO.mapRequired("GlobalId", Obj.Global);
268 }
269 
270 void MappingTraits<YAMLCrossModuleImport>::mapping(IO &IO,
271                                                    YAMLCrossModuleImport &Obj) {
272   IO.mapRequired("Module", Obj.ModuleName);
273   IO.mapRequired("Imports", Obj.ImportIds);
274 }
275 
276 void MappingTraits<SourceFileChecksumEntry>::mapping(
277     IO &IO, SourceFileChecksumEntry &Obj) {
278   IO.mapRequired("FileName", Obj.FileName);
279   IO.mapRequired("Kind", Obj.Kind);
280   IO.mapRequired("Checksum", Obj.ChecksumBytes);
281 }
282 
283 void MappingTraits<InlineeSite>::mapping(IO &IO, InlineeSite &Obj) {
284   IO.mapRequired("FileName", Obj.FileName);
285   IO.mapRequired("LineNum", Obj.SourceLineNum);
286   IO.mapRequired("Inlinee", Obj.Inlinee);
287   IO.mapOptional("ExtraFiles", Obj.ExtraFiles);
288 }
289 
290 void MappingTraits<YAMLFrameData>::mapping(IO &IO, YAMLFrameData &Obj) {
291   IO.mapRequired("CodeSize", Obj.CodeSize);
292   IO.mapRequired("FrameFunc", Obj.FrameFunc);
293   IO.mapRequired("LocalSize", Obj.LocalSize);
294   IO.mapOptional("MaxStackSize", Obj.MaxStackSize);
295   IO.mapOptional("ParamsSize", Obj.ParamsSize);
296   IO.mapOptional("PrologSize", Obj.PrologSize);
297   IO.mapOptional("RvaStart", Obj.RvaStart);
298   IO.mapOptional("SavedRegsSize", Obj.SavedRegsSize);
299 }
300 
301 void YAMLChecksumsSubsection::map(IO &IO) {
302   IO.mapTag("!FileChecksums", true);
303   IO.mapRequired("Checksums", Checksums);
304 }
305 
306 void YAMLLinesSubsection::map(IO &IO) {
307   IO.mapTag("!Lines", true);
308   IO.mapRequired("CodeSize", Lines.CodeSize);
309 
310   IO.mapRequired("Flags", Lines.Flags);
311   IO.mapRequired("RelocOffset", Lines.RelocOffset);
312   IO.mapRequired("RelocSegment", Lines.RelocSegment);
313   IO.mapRequired("Blocks", Lines.Blocks);
314 }
315 
316 void YAMLInlineeLinesSubsection::map(IO &IO) {
317   IO.mapTag("!InlineeLines", true);
318   IO.mapRequired("HasExtraFiles", InlineeLines.HasExtraFiles);
319   IO.mapRequired("Sites", InlineeLines.Sites);
320 }
321 
322 void YAMLCrossModuleExportsSubsection::map(IO &IO) {
323   IO.mapTag("!CrossModuleExports", true);
324   IO.mapOptional("Exports", Exports);
325 }
326 
327 void YAMLCrossModuleImportsSubsection::map(IO &IO) {
328   IO.mapTag("!CrossModuleImports", true);
329   IO.mapOptional("Imports", Imports);
330 }
331 
332 void YAMLSymbolsSubsection::map(IO &IO) {
333   IO.mapTag("!Symbols", true);
334   IO.mapRequired("Records", Symbols);
335 }
336 
337 void YAMLStringTableSubsection::map(IO &IO) {
338   IO.mapTag("!StringTable", true);
339   IO.mapRequired("Strings", Strings);
340 }
341 
342 void YAMLFrameDataSubsection::map(IO &IO) {
343   IO.mapTag("!FrameData", true);
344   IO.mapRequired("Frames", Frames);
345 }
346 
347 void YAMLCoffSymbolRVASubsection::map(IO &IO) {
348   IO.mapTag("!COFFSymbolRVAs", true);
349   IO.mapRequired("RVAs", RVAs);
350 }
351 
352 void MappingTraits<YAMLDebugSubsection>::mapping(
353     IO &IO, YAMLDebugSubsection &Subsection) {
354   if (!IO.outputting()) {
355     if (IO.mapTag("!FileChecksums")) {
356       auto SS = std::make_shared<YAMLChecksumsSubsection>();
357       Subsection.Subsection = SS;
358     } else if (IO.mapTag("!Lines")) {
359       Subsection.Subsection = std::make_shared<YAMLLinesSubsection>();
360     } else if (IO.mapTag("!InlineeLines")) {
361       Subsection.Subsection = std::make_shared<YAMLInlineeLinesSubsection>();
362     } else if (IO.mapTag("!CrossModuleExports")) {
363       Subsection.Subsection =
364           std::make_shared<YAMLCrossModuleExportsSubsection>();
365     } else if (IO.mapTag("!CrossModuleImports")) {
366       Subsection.Subsection =
367           std::make_shared<YAMLCrossModuleImportsSubsection>();
368     } else if (IO.mapTag("!Symbols")) {
369       Subsection.Subsection = std::make_shared<YAMLSymbolsSubsection>();
370     } else if (IO.mapTag("!StringTable")) {
371       Subsection.Subsection = std::make_shared<YAMLStringTableSubsection>();
372     } else if (IO.mapTag("!FrameData")) {
373       Subsection.Subsection = std::make_shared<YAMLFrameDataSubsection>();
374     } else if (IO.mapTag("!COFFSymbolRVAs")) {
375       Subsection.Subsection = std::make_shared<YAMLCoffSymbolRVASubsection>();
376     } else {
377       llvm_unreachable("Unexpected subsection tag!");
378     }
379   }
380   Subsection.Subsection->map(IO);
381 }
382 
383 static std::shared_ptr<YAMLChecksumsSubsection>
384 findChecksums(ArrayRef<YAMLDebugSubsection> Subsections) {
385   for (const auto &SS : Subsections) {
386     if (SS.Subsection->Kind == DebugSubsectionKind::FileChecksums) {
387       return std::static_pointer_cast<YAMLChecksumsSubsection>(SS.Subsection);
388     }
389   }
390 
391   return nullptr;
392 }
393 
394 std::shared_ptr<DebugSubsection> YAMLChecksumsSubsection::toCodeViewSubsection(
395     BumpPtrAllocator &Allocator,
396     const codeview::StringsAndChecksums &SC) const {
397   assert(SC.hasStrings());
398   auto Result = std::make_shared<DebugChecksumsSubsection>(*SC.strings());
399   for (const auto &CS : Checksums) {
400     Result->addChecksum(CS.FileName, CS.Kind, CS.ChecksumBytes.Bytes);
401   }
402   return std::move(Result);
403 }
404 
405 std::shared_ptr<DebugSubsection> YAMLLinesSubsection::toCodeViewSubsection(
406     BumpPtrAllocator &Allocator,
407     const codeview::StringsAndChecksums &SC) const {
408   assert(SC.hasStrings() && SC.hasChecksums());
409   auto Result =
410       std::make_shared<DebugLinesSubsection>(*SC.checksums(), *SC.strings());
411   Result->setCodeSize(Lines.CodeSize);
412   Result->setRelocationAddress(Lines.RelocSegment, Lines.RelocOffset);
413   Result->setFlags(Lines.Flags);
414   for (const auto &LC : Lines.Blocks) {
415     Result->createBlock(LC.FileName);
416     if (Result->hasColumnInfo()) {
417       for (const auto &Item : zip(LC.Lines, LC.Columns)) {
418         auto &L = std::get<0>(Item);
419         auto &C = std::get<1>(Item);
420         uint32_t LE = L.LineStart + L.EndDelta;
421         Result->addLineAndColumnInfo(L.Offset,
422                                      LineInfo(L.LineStart, LE, L.IsStatement),
423                                      C.StartColumn, C.EndColumn);
424       }
425     } else {
426       for (const auto &L : LC.Lines) {
427         uint32_t LE = L.LineStart + L.EndDelta;
428         Result->addLineInfo(L.Offset, LineInfo(L.LineStart, LE, L.IsStatement));
429       }
430     }
431   }
432   return std::move(Result);
433 }
434 
435 std::shared_ptr<DebugSubsection>
436 YAMLInlineeLinesSubsection::toCodeViewSubsection(
437     BumpPtrAllocator &Allocator,
438     const codeview::StringsAndChecksums &SC) const {
439   assert(SC.hasChecksums());
440   auto Result = std::make_shared<DebugInlineeLinesSubsection>(
441       *SC.checksums(), InlineeLines.HasExtraFiles);
442 
443   for (const auto &Site : InlineeLines.Sites) {
444     Result->addInlineSite(TypeIndex(Site.Inlinee), Site.FileName,
445                           Site.SourceLineNum);
446     if (!InlineeLines.HasExtraFiles)
447       continue;
448 
449     for (auto EF : Site.ExtraFiles) {
450       Result->addExtraFile(EF);
451     }
452   }
453   return std::move(Result);
454 }
455 
456 std::shared_ptr<DebugSubsection>
457 YAMLCrossModuleExportsSubsection::toCodeViewSubsection(
458     BumpPtrAllocator &Allocator,
459     const codeview::StringsAndChecksums &SC) const {
460   auto Result = std::make_shared<DebugCrossModuleExportsSubsection>();
461   for (const auto &M : Exports)
462     Result->addMapping(M.Local, M.Global);
463   return std::move(Result);
464 }
465 
466 std::shared_ptr<DebugSubsection>
467 YAMLCrossModuleImportsSubsection::toCodeViewSubsection(
468     BumpPtrAllocator &Allocator,
469     const codeview::StringsAndChecksums &SC) const {
470   assert(SC.hasStrings());
471 
472   auto Result =
473       std::make_shared<DebugCrossModuleImportsSubsection>(*SC.strings());
474   for (const auto &M : Imports) {
475     for (const auto Id : M.ImportIds)
476       Result->addImport(M.ModuleName, Id);
477   }
478   return std::move(Result);
479 }
480 
481 std::shared_ptr<DebugSubsection> YAMLSymbolsSubsection::toCodeViewSubsection(
482     BumpPtrAllocator &Allocator,
483     const codeview::StringsAndChecksums &SC) const {
484   auto Result = std::make_shared<DebugSymbolsSubsection>();
485   for (const auto &Sym : Symbols)
486     Result->addSymbol(
487         Sym.toCodeViewSymbol(Allocator, CodeViewContainer::ObjectFile));
488   return std::move(Result);
489 }
490 
491 std::shared_ptr<DebugSubsection>
492 YAMLStringTableSubsection::toCodeViewSubsection(
493     BumpPtrAllocator &Allocator,
494     const codeview::StringsAndChecksums &SC) const {
495   auto Result = std::make_shared<DebugStringTableSubsection>();
496   for (const auto &Str : this->Strings)
497     Result->insert(Str);
498   return std::move(Result);
499 }
500 
501 std::shared_ptr<DebugSubsection> YAMLFrameDataSubsection::toCodeViewSubsection(
502     BumpPtrAllocator &Allocator,
503     const codeview::StringsAndChecksums &SC) const {
504   assert(SC.hasStrings());
505 
506   auto Result = llvm::make_unique<DebugFrameDataSubsection>();
507   for (const auto &YF : Frames) {
508     codeview::FrameData F;
509     F.CodeSize = YF.CodeSize;
510     F.Flags = YF.Flags;
511     F.LocalSize = YF.LocalSize;
512     F.MaxStackSize = YF.MaxStackSize;
513     F.ParamsSize = YF.ParamsSize;
514     F.PrologSize = YF.PrologSize;
515     F.RvaStart = YF.RvaStart;
516     F.SavedRegsSize = YF.SavedRegsSize;
517     F.FrameFunc = SC.strings()->insert(YF.FrameFunc);
518     Result->addFrameData(F);
519   }
520   return std::move(Result);
521 }
522 
523 std::shared_ptr<DebugSubsection>
524 YAMLCoffSymbolRVASubsection::toCodeViewSubsection(
525     BumpPtrAllocator &Allocator,
526     const codeview::StringsAndChecksums &SC) const {
527   auto Result = llvm::make_unique<DebugSymbolRVASubsection>();
528   for (const auto &RVA : RVAs)
529     Result->addRVA(RVA);
530   return std::move(Result);
531 }
532 
533 static Expected<SourceFileChecksumEntry>
534 convertOneChecksum(const DebugStringTableSubsectionRef &Strings,
535                    const FileChecksumEntry &CS) {
536   auto ExpectedString = Strings.getString(CS.FileNameOffset);
537   if (!ExpectedString)
538     return ExpectedString.takeError();
539 
540   SourceFileChecksumEntry Result;
541   Result.ChecksumBytes.Bytes = CS.Checksum;
542   Result.Kind = CS.Kind;
543   Result.FileName = *ExpectedString;
544   return Result;
545 }
546 
547 static Expected<StringRef>
548 getFileName(const DebugStringTableSubsectionRef &Strings,
549             const DebugChecksumsSubsectionRef &Checksums, uint32_t FileID) {
550   auto Iter = Checksums.getArray().at(FileID);
551   if (Iter == Checksums.getArray().end())
552     return make_error<CodeViewError>(cv_error_code::no_records);
553   uint32_t Offset = Iter->FileNameOffset;
554   return Strings.getString(Offset);
555 }
556 
557 Expected<std::shared_ptr<YAMLChecksumsSubsection>>
558 YAMLChecksumsSubsection::fromCodeViewSubsection(
559     const DebugStringTableSubsectionRef &Strings,
560     const DebugChecksumsSubsectionRef &FC) {
561   auto Result = std::make_shared<YAMLChecksumsSubsection>();
562 
563   for (const auto &CS : FC) {
564     auto ConvertedCS = convertOneChecksum(Strings, CS);
565     if (!ConvertedCS)
566       return ConvertedCS.takeError();
567     Result->Checksums.push_back(*ConvertedCS);
568   }
569   return Result;
570 }
571 
572 Expected<std::shared_ptr<YAMLLinesSubsection>>
573 YAMLLinesSubsection::fromCodeViewSubsection(
574     const DebugStringTableSubsectionRef &Strings,
575     const DebugChecksumsSubsectionRef &Checksums,
576     const DebugLinesSubsectionRef &Lines) {
577   auto Result = std::make_shared<YAMLLinesSubsection>();
578   Result->Lines.CodeSize = Lines.header()->CodeSize;
579   Result->Lines.RelocOffset = Lines.header()->RelocOffset;
580   Result->Lines.RelocSegment = Lines.header()->RelocSegment;
581   Result->Lines.Flags = static_cast<LineFlags>(uint16_t(Lines.header()->Flags));
582   for (const auto &L : Lines) {
583     SourceLineBlock Block;
584     auto EF = getFileName(Strings, Checksums, L.NameIndex);
585     if (!EF)
586       return EF.takeError();
587     Block.FileName = *EF;
588     if (Lines.hasColumnInfo()) {
589       for (const auto &C : L.Columns) {
590         SourceColumnEntry SCE;
591         SCE.EndColumn = C.EndColumn;
592         SCE.StartColumn = C.StartColumn;
593         Block.Columns.push_back(SCE);
594       }
595     }
596     for (const auto &LN : L.LineNumbers) {
597       SourceLineEntry SLE;
598       LineInfo LI(LN.Flags);
599       SLE.Offset = LN.Offset;
600       SLE.LineStart = LI.getStartLine();
601       SLE.EndDelta = LI.getLineDelta();
602       SLE.IsStatement = LI.isStatement();
603       Block.Lines.push_back(SLE);
604     }
605     Result->Lines.Blocks.push_back(Block);
606   }
607   return Result;
608 }
609 
610 Expected<std::shared_ptr<YAMLInlineeLinesSubsection>>
611 YAMLInlineeLinesSubsection::fromCodeViewSubsection(
612     const DebugStringTableSubsectionRef &Strings,
613     const DebugChecksumsSubsectionRef &Checksums,
614     const DebugInlineeLinesSubsectionRef &Lines) {
615   auto Result = std::make_shared<YAMLInlineeLinesSubsection>();
616 
617   Result->InlineeLines.HasExtraFiles = Lines.hasExtraFiles();
618   for (const auto &IL : Lines) {
619     InlineeSite Site;
620     auto ExpF = getFileName(Strings, Checksums, IL.Header->FileID);
621     if (!ExpF)
622       return ExpF.takeError();
623     Site.FileName = *ExpF;
624     Site.Inlinee = IL.Header->Inlinee.getIndex();
625     Site.SourceLineNum = IL.Header->SourceLineNum;
626     if (Lines.hasExtraFiles()) {
627       for (const auto EF : IL.ExtraFiles) {
628         auto ExpF2 = getFileName(Strings, Checksums, EF);
629         if (!ExpF2)
630           return ExpF2.takeError();
631         Site.ExtraFiles.push_back(*ExpF2);
632       }
633     }
634     Result->InlineeLines.Sites.push_back(Site);
635   }
636   return Result;
637 }
638 
639 Expected<std::shared_ptr<YAMLCrossModuleExportsSubsection>>
640 YAMLCrossModuleExportsSubsection::fromCodeViewSubsection(
641     const DebugCrossModuleExportsSubsectionRef &Exports) {
642   auto Result = std::make_shared<YAMLCrossModuleExportsSubsection>();
643   Result->Exports.assign(Exports.begin(), Exports.end());
644   return Result;
645 }
646 
647 Expected<std::shared_ptr<YAMLCrossModuleImportsSubsection>>
648 YAMLCrossModuleImportsSubsection::fromCodeViewSubsection(
649     const DebugStringTableSubsectionRef &Strings,
650     const DebugCrossModuleImportsSubsectionRef &Imports) {
651   auto Result = std::make_shared<YAMLCrossModuleImportsSubsection>();
652   for (const auto &CMI : Imports) {
653     YAMLCrossModuleImport YCMI;
654     auto ExpectedStr = Strings.getString(CMI.Header->ModuleNameOffset);
655     if (!ExpectedStr)
656       return ExpectedStr.takeError();
657     YCMI.ModuleName = *ExpectedStr;
658     YCMI.ImportIds.assign(CMI.Imports.begin(), CMI.Imports.end());
659     Result->Imports.push_back(YCMI);
660   }
661   return Result;
662 }
663 
664 Expected<std::shared_ptr<YAMLSymbolsSubsection>>
665 YAMLSymbolsSubsection::fromCodeViewSubsection(
666     const DebugSymbolsSubsectionRef &Symbols) {
667   auto Result = std::make_shared<YAMLSymbolsSubsection>();
668   for (const auto &Sym : Symbols) {
669     auto S = CodeViewYAML::SymbolRecord::fromCodeViewSymbol(Sym);
670     if (!S)
671       return joinErrors(make_error<CodeViewError>(
672                             cv_error_code::corrupt_record,
673                             "Invalid CodeView Symbol Record in SymbolRecord "
674                             "subsection of .debug$S while converting to YAML!"),
675                         S.takeError());
676 
677     Result->Symbols.push_back(*S);
678   }
679   return Result;
680 }
681 
682 Expected<std::shared_ptr<YAMLStringTableSubsection>>
683 YAMLStringTableSubsection::fromCodeViewSubsection(
684     const DebugStringTableSubsectionRef &Strings) {
685   auto Result = std::make_shared<YAMLStringTableSubsection>();
686   BinaryStreamReader Reader(Strings.getBuffer());
687   StringRef S;
688   // First item is a single null string, skip it.
689   if (auto EC = Reader.readCString(S))
690     return std::move(EC);
691   assert(S.empty());
692   while (Reader.bytesRemaining() > 0) {
693     if (auto EC = Reader.readCString(S))
694       return std::move(EC);
695     Result->Strings.push_back(S);
696   }
697   return Result;
698 }
699 
700 Expected<std::shared_ptr<YAMLFrameDataSubsection>>
701 YAMLFrameDataSubsection::fromCodeViewSubsection(
702     const DebugStringTableSubsectionRef &Strings,
703     const DebugFrameDataSubsectionRef &Frames) {
704   auto Result = std::make_shared<YAMLFrameDataSubsection>();
705   for (const auto &F : Frames) {
706     YAMLFrameData YF;
707     YF.CodeSize = F.CodeSize;
708     YF.Flags = F.Flags;
709     YF.LocalSize = F.LocalSize;
710     YF.MaxStackSize = F.MaxStackSize;
711     YF.ParamsSize = F.ParamsSize;
712     YF.PrologSize = F.PrologSize;
713     YF.RvaStart = F.RvaStart;
714     YF.SavedRegsSize = F.SavedRegsSize;
715 
716     auto ES = Strings.getString(F.FrameFunc);
717     if (!ES)
718       return joinErrors(
719           make_error<CodeViewError>(
720               cv_error_code::no_records,
721               "Could not find string for string id while mapping FrameData!"),
722           ES.takeError());
723     YF.FrameFunc = *ES;
724     Result->Frames.push_back(YF);
725   }
726   return Result;
727 }
728 
729 Expected<std::shared_ptr<YAMLCoffSymbolRVASubsection>>
730 YAMLCoffSymbolRVASubsection::fromCodeViewSubsection(
731     const DebugSymbolRVASubsectionRef &Section) {
732   auto Result = std::make_shared<YAMLCoffSymbolRVASubsection>();
733   for (const auto &RVA : Section) {
734     Result->RVAs.push_back(RVA);
735   }
736   return Result;
737 }
738 
739 Expected<std::vector<std::shared_ptr<DebugSubsection>>>
740 llvm::CodeViewYAML::toCodeViewSubsectionList(
741     BumpPtrAllocator &Allocator, ArrayRef<YAMLDebugSubsection> Subsections,
742     const codeview::StringsAndChecksums &SC) {
743   std::vector<std::shared_ptr<DebugSubsection>> Result;
744   if (Subsections.empty())
745     return std::move(Result);
746 
747   for (const auto &SS : Subsections) {
748     std::shared_ptr<DebugSubsection> CVS;
749     CVS = SS.Subsection->toCodeViewSubsection(Allocator, SC);
750     assert(CVS != nullptr);
751     Result.push_back(std::move(CVS));
752   }
753   return std::move(Result);
754 }
755 
756 namespace {
757 struct SubsectionConversionVisitor : public DebugSubsectionVisitor {
758   SubsectionConversionVisitor() {}
759 
760   Error visitUnknown(DebugUnknownSubsectionRef &Unknown) override;
761   Error visitLines(DebugLinesSubsectionRef &Lines,
762                    const StringsAndChecksumsRef &State) override;
763   Error visitFileChecksums(DebugChecksumsSubsectionRef &Checksums,
764                            const StringsAndChecksumsRef &State) override;
765   Error visitInlineeLines(DebugInlineeLinesSubsectionRef &Inlinees,
766                           const StringsAndChecksumsRef &State) override;
767   Error visitCrossModuleExports(DebugCrossModuleExportsSubsectionRef &Checksums,
768                                 const StringsAndChecksumsRef &State) override;
769   Error visitCrossModuleImports(DebugCrossModuleImportsSubsectionRef &Inlinees,
770                                 const StringsAndChecksumsRef &State) override;
771   Error visitStringTable(DebugStringTableSubsectionRef &ST,
772                          const StringsAndChecksumsRef &State) override;
773   Error visitSymbols(DebugSymbolsSubsectionRef &Symbols,
774                      const StringsAndChecksumsRef &State) override;
775   Error visitFrameData(DebugFrameDataSubsectionRef &Symbols,
776                        const StringsAndChecksumsRef &State) override;
777   Error visitCOFFSymbolRVAs(DebugSymbolRVASubsectionRef &Symbols,
778                             const StringsAndChecksumsRef &State) override;
779 
780   YAMLDebugSubsection Subsection;
781 };
782 
783 Error SubsectionConversionVisitor::visitUnknown(
784     DebugUnknownSubsectionRef &Unknown) {
785   return make_error<CodeViewError>(cv_error_code::operation_unsupported);
786 }
787 
788 Error SubsectionConversionVisitor::visitLines(
789     DebugLinesSubsectionRef &Lines, const StringsAndChecksumsRef &State) {
790   auto Result = YAMLLinesSubsection::fromCodeViewSubsection(
791       State.strings(), State.checksums(), Lines);
792   if (!Result)
793     return Result.takeError();
794   Subsection.Subsection = *Result;
795   return Error::success();
796 }
797 
798 Error SubsectionConversionVisitor::visitFileChecksums(
799     DebugChecksumsSubsectionRef &Checksums,
800     const StringsAndChecksumsRef &State) {
801   auto Result = YAMLChecksumsSubsection::fromCodeViewSubsection(State.strings(),
802                                                                 Checksums);
803   if (!Result)
804     return Result.takeError();
805   Subsection.Subsection = *Result;
806   return Error::success();
807 }
808 
809 Error SubsectionConversionVisitor::visitInlineeLines(
810     DebugInlineeLinesSubsectionRef &Inlinees,
811     const StringsAndChecksumsRef &State) {
812   auto Result = YAMLInlineeLinesSubsection::fromCodeViewSubsection(
813       State.strings(), State.checksums(), Inlinees);
814   if (!Result)
815     return Result.takeError();
816   Subsection.Subsection = *Result;
817   return Error::success();
818 }
819 
820 Error SubsectionConversionVisitor::visitCrossModuleExports(
821     DebugCrossModuleExportsSubsectionRef &Exports,
822     const StringsAndChecksumsRef &State) {
823   auto Result =
824       YAMLCrossModuleExportsSubsection::fromCodeViewSubsection(Exports);
825   if (!Result)
826     return Result.takeError();
827   Subsection.Subsection = *Result;
828   return Error::success();
829 }
830 
831 Error SubsectionConversionVisitor::visitCrossModuleImports(
832     DebugCrossModuleImportsSubsectionRef &Imports,
833     const StringsAndChecksumsRef &State) {
834   auto Result = YAMLCrossModuleImportsSubsection::fromCodeViewSubsection(
835       State.strings(), Imports);
836   if (!Result)
837     return Result.takeError();
838   Subsection.Subsection = *Result;
839   return Error::success();
840 }
841 
842 Error SubsectionConversionVisitor::visitStringTable(
843     DebugStringTableSubsectionRef &Strings,
844     const StringsAndChecksumsRef &State) {
845   auto Result = YAMLStringTableSubsection::fromCodeViewSubsection(Strings);
846   if (!Result)
847     return Result.takeError();
848   Subsection.Subsection = *Result;
849   return Error::success();
850 }
851 
852 Error SubsectionConversionVisitor::visitSymbols(
853     DebugSymbolsSubsectionRef &Symbols, const StringsAndChecksumsRef &State) {
854   auto Result = YAMLSymbolsSubsection::fromCodeViewSubsection(Symbols);
855   if (!Result)
856     return Result.takeError();
857   Subsection.Subsection = *Result;
858   return Error::success();
859 }
860 
861 Error SubsectionConversionVisitor::visitFrameData(
862     DebugFrameDataSubsectionRef &Frames, const StringsAndChecksumsRef &State) {
863   auto Result =
864       YAMLFrameDataSubsection::fromCodeViewSubsection(State.strings(), Frames);
865   if (!Result)
866     return Result.takeError();
867   Subsection.Subsection = *Result;
868   return Error::success();
869 }
870 
871 Error SubsectionConversionVisitor::visitCOFFSymbolRVAs(
872     DebugSymbolRVASubsectionRef &RVAs, const StringsAndChecksumsRef &State) {
873   auto Result = YAMLCoffSymbolRVASubsection::fromCodeViewSubsection(RVAs);
874   if (!Result)
875     return Result.takeError();
876   Subsection.Subsection = *Result;
877   return Error::success();
878 }
879 }
880 
881 Expected<YAMLDebugSubsection>
882 YAMLDebugSubsection::fromCodeViewSubection(const StringsAndChecksumsRef &SC,
883                                            const DebugSubsectionRecord &SS) {
884   SubsectionConversionVisitor V;
885   if (auto EC = visitDebugSubsection(SS, V, SC))
886     return std::move(EC);
887 
888   return V.Subsection;
889 }
890 
891 std::vector<YAMLDebugSubsection>
892 llvm::CodeViewYAML::fromDebugS(ArrayRef<uint8_t> Data,
893                                const StringsAndChecksumsRef &SC) {
894   BinaryStreamReader Reader(Data, support::little);
895   uint32_t Magic;
896 
897   ExitOnError Err("Invalid .debug$S section!");
898   Err(Reader.readInteger(Magic));
899   assert(Magic == COFF::DEBUG_SECTION_MAGIC && "Invalid .debug$S section!");
900 
901   DebugSubsectionArray Subsections;
902   Err(Reader.readArray(Subsections, Reader.bytesRemaining()));
903 
904   std::vector<YAMLDebugSubsection> Result;
905 
906   for (const auto &SS : Subsections) {
907     auto YamlSS = Err(YAMLDebugSubsection::fromCodeViewSubection(SC, SS));
908     Result.push_back(YamlSS);
909   }
910   return std::move(Result);
911 }
912 
913 void llvm::CodeViewYAML::initializeStringsAndChecksums(
914     ArrayRef<YAMLDebugSubsection> Sections, codeview::StringsAndChecksums &SC) {
915   // String Table and Checksums subsections don't use the allocator.
916   BumpPtrAllocator Allocator;
917 
918   // It's possible for checksums and strings to even appear in different debug$S
919   // sections, so we have to make this a stateful function that can build up
920   // the strings and checksums field over multiple iterations.
921 
922   // File Checksums require the string table, but may become before it, so we
923   // have to scan for strings first, then scan for checksums again from the
924   // beginning.
925   if (!SC.hasStrings()) {
926     for (const auto &SS : Sections) {
927       if (SS.Subsection->Kind != DebugSubsectionKind::StringTable)
928         continue;
929 
930       auto Result = SS.Subsection->toCodeViewSubsection(Allocator, SC);
931       SC.setStrings(
932           std::static_pointer_cast<DebugStringTableSubsection>(Result));
933       break;
934     }
935   }
936 
937   if (SC.hasStrings() && !SC.hasChecksums()) {
938     for (const auto &SS : Sections) {
939       if (SS.Subsection->Kind != DebugSubsectionKind::FileChecksums)
940         continue;
941 
942       auto Result = SS.Subsection->toCodeViewSubsection(Allocator, SC);
943       SC.setChecksums(
944           std::static_pointer_cast<DebugChecksumsSubsection>(Result));
945       break;
946     }
947   }
948 }
949