]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/llvm-pdbdump/PdbYaml.cpp
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r301441, and update
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / llvm-pdbdump / PdbYaml.cpp
1 //===- PdbYAML.cpp -------------------------------------------- *- 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 "PdbYaml.h"
11
12 #include "YamlSerializationContext.h"
13 #include "YamlSymbolDumper.h"
14 #include "YamlTypeDumper.h"
15
16 #include "llvm/ADT/StringExtras.h"
17 #include "llvm/DebugInfo/CodeView/CVSymbolVisitor.h"
18 #include "llvm/DebugInfo/CodeView/CVTypeVisitor.h"
19 #include "llvm/DebugInfo/CodeView/SymbolDeserializer.h"
20 #include "llvm/DebugInfo/CodeView/SymbolSerializer.h"
21 #include "llvm/DebugInfo/CodeView/SymbolVisitorCallbackPipeline.h"
22 #include "llvm/DebugInfo/CodeView/TypeDeserializer.h"
23 #include "llvm/DebugInfo/CodeView/TypeSerializer.h"
24 #include "llvm/DebugInfo/CodeView/TypeVisitorCallbackPipeline.h"
25 #include "llvm/DebugInfo/PDB/Native/PDBFile.h"
26 #include "llvm/DebugInfo/PDB/Native/TpiHashing.h"
27 #include "llvm/DebugInfo/PDB/PDBExtras.h"
28 #include "llvm/DebugInfo/PDB/PDBTypes.h"
29
30 using namespace llvm;
31 using namespace llvm::pdb;
32 using namespace llvm::pdb::yaml;
33 using namespace llvm::yaml;
34
35 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(uint32_t)
36 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::StringRef)
37 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::pdb::yaml::NamedStreamMapping)
38 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::pdb::yaml::PdbDbiModuleInfo)
39 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::pdb::yaml::PdbSourceFileChecksumEntry)
40 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::pdb::yaml::PdbSourceLineEntry)
41 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::pdb::yaml::PdbSourceColumnEntry)
42 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::pdb::yaml::PdbSourceLineBlock)
43 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::pdb::yaml::PdbSymbolRecord)
44 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::pdb::yaml::PdbTpiRecord)
45 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::pdb::yaml::StreamBlockList)
46 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(llvm::pdb::PdbRaw_FeatureSig)
47
48 namespace llvm {
49 namespace yaml {
50
51 template <> struct ScalarTraits<llvm::pdb::PDB_UniqueId> {
52   static void output(const llvm::pdb::PDB_UniqueId &S, void *,
53                      llvm::raw_ostream &OS) {
54     OS << S;
55   }
56
57   static StringRef input(StringRef Scalar, void *Ctx,
58                          llvm::pdb::PDB_UniqueId &S) {
59     if (Scalar.size() != 38)
60       return "GUID strings are 38 characters long";
61     if (Scalar[0] != '{' || Scalar[37] != '}')
62       return "GUID is not enclosed in {}";
63     if (Scalar[9] != '-' || Scalar[14] != '-' || Scalar[19] != '-' ||
64         Scalar[24] != '-')
65       return "GUID sections are not properly delineated with dashes";
66
67     uint8_t *OutBuffer = S.Guid;
68     for (auto Iter = Scalar.begin(); Iter != Scalar.end();) {
69       if (*Iter == '-' || *Iter == '{' || *Iter == '}') {
70         ++Iter;
71         continue;
72       }
73       uint8_t Value = (llvm::hexDigitValue(*Iter) << 4);
74       ++Iter;
75       Value |= llvm::hexDigitValue(*Iter);
76       ++Iter;
77       *OutBuffer++ = Value;
78     }
79
80     return "";
81   }
82
83   static bool mustQuote(StringRef Scalar) { return needsQuotes(Scalar); }
84 };
85
86 template <> struct ScalarEnumerationTraits<llvm::pdb::PDB_Machine> {
87   static void enumeration(IO &io, llvm::pdb::PDB_Machine &Value) {
88     io.enumCase(Value, "Invalid", PDB_Machine::Invalid);
89     io.enumCase(Value, "Am33", PDB_Machine::Am33);
90     io.enumCase(Value, "Amd64", PDB_Machine::Amd64);
91     io.enumCase(Value, "Arm", PDB_Machine::Arm);
92     io.enumCase(Value, "ArmNT", PDB_Machine::ArmNT);
93     io.enumCase(Value, "Ebc", PDB_Machine::Ebc);
94     io.enumCase(Value, "x86", PDB_Machine::x86);
95     io.enumCase(Value, "Ia64", PDB_Machine::Ia64);
96     io.enumCase(Value, "M32R", PDB_Machine::M32R);
97     io.enumCase(Value, "Mips16", PDB_Machine::Mips16);
98     io.enumCase(Value, "MipsFpu", PDB_Machine::MipsFpu);
99     io.enumCase(Value, "MipsFpu16", PDB_Machine::MipsFpu16);
100     io.enumCase(Value, "PowerPCFP", PDB_Machine::PowerPCFP);
101     io.enumCase(Value, "R4000", PDB_Machine::R4000);
102     io.enumCase(Value, "SH3", PDB_Machine::SH3);
103     io.enumCase(Value, "SH3DSP", PDB_Machine::SH3DSP);
104     io.enumCase(Value, "Thumb", PDB_Machine::Thumb);
105     io.enumCase(Value, "WceMipsV2", PDB_Machine::WceMipsV2);
106   }
107 };
108
109 template <> struct ScalarEnumerationTraits<llvm::pdb::PdbRaw_DbiVer> {
110   static void enumeration(IO &io, llvm::pdb::PdbRaw_DbiVer &Value) {
111     io.enumCase(Value, "V41", llvm::pdb::PdbRaw_DbiVer::PdbDbiVC41);
112     io.enumCase(Value, "V50", llvm::pdb::PdbRaw_DbiVer::PdbDbiV50);
113     io.enumCase(Value, "V60", llvm::pdb::PdbRaw_DbiVer::PdbDbiV60);
114     io.enumCase(Value, "V70", llvm::pdb::PdbRaw_DbiVer::PdbDbiV70);
115     io.enumCase(Value, "V110", llvm::pdb::PdbRaw_DbiVer::PdbDbiV110);
116   }
117 };
118
119 template <> struct ScalarEnumerationTraits<llvm::pdb::PdbRaw_ImplVer> {
120   static void enumeration(IO &io, llvm::pdb::PdbRaw_ImplVer &Value) {
121     io.enumCase(Value, "VC2", llvm::pdb::PdbRaw_ImplVer::PdbImplVC2);
122     io.enumCase(Value, "VC4", llvm::pdb::PdbRaw_ImplVer::PdbImplVC4);
123     io.enumCase(Value, "VC41", llvm::pdb::PdbRaw_ImplVer::PdbImplVC41);
124     io.enumCase(Value, "VC50", llvm::pdb::PdbRaw_ImplVer::PdbImplVC50);
125     io.enumCase(Value, "VC98", llvm::pdb::PdbRaw_ImplVer::PdbImplVC98);
126     io.enumCase(Value, "VC70Dep", llvm::pdb::PdbRaw_ImplVer::PdbImplVC70Dep);
127     io.enumCase(Value, "VC70", llvm::pdb::PdbRaw_ImplVer::PdbImplVC70);
128     io.enumCase(Value, "VC80", llvm::pdb::PdbRaw_ImplVer::PdbImplVC80);
129     io.enumCase(Value, "VC110", llvm::pdb::PdbRaw_ImplVer::PdbImplVC110);
130     io.enumCase(Value, "VC140", llvm::pdb::PdbRaw_ImplVer::PdbImplVC140);
131   }
132 };
133
134 template <> struct ScalarEnumerationTraits<llvm::pdb::PdbRaw_TpiVer> {
135   static void enumeration(IO &io, llvm::pdb::PdbRaw_TpiVer &Value) {
136     io.enumCase(Value, "VC40", llvm::pdb::PdbRaw_TpiVer::PdbTpiV40);
137     io.enumCase(Value, "VC41", llvm::pdb::PdbRaw_TpiVer::PdbTpiV41);
138     io.enumCase(Value, "VC50", llvm::pdb::PdbRaw_TpiVer::PdbTpiV50);
139     io.enumCase(Value, "VC70", llvm::pdb::PdbRaw_TpiVer::PdbTpiV70);
140     io.enumCase(Value, "VC80", llvm::pdb::PdbRaw_TpiVer::PdbTpiV80);
141   }
142 };
143
144 template <> struct ScalarEnumerationTraits<llvm::pdb::PdbRaw_FeatureSig> {
145   static void enumeration(IO &io, PdbRaw_FeatureSig &Features) {
146     io.enumCase(Features, "MinimalDebugInfo",
147                 PdbRaw_FeatureSig::MinimalDebugInfo);
148     io.enumCase(Features, "NoTypeMerge", PdbRaw_FeatureSig::NoTypeMerge);
149     io.enumCase(Features, "VC110", PdbRaw_FeatureSig::VC110);
150     io.enumCase(Features, "VC140", PdbRaw_FeatureSig::VC140);
151   }
152 };
153
154 template <> struct ScalarEnumerationTraits<llvm::codeview::FileChecksumKind> {
155   static void enumeration(IO &io, llvm::codeview::FileChecksumKind &Kind) {
156     io.enumCase(Kind, "None", llvm::codeview::FileChecksumKind::None);
157     io.enumCase(Kind, "MD5", llvm::codeview::FileChecksumKind::MD5);
158     io.enumCase(Kind, "SHA1", llvm::codeview::FileChecksumKind::SHA1);
159     io.enumCase(Kind, "SHA256", llvm::codeview::FileChecksumKind::SHA256);
160   }
161 };
162
163 template <> struct ScalarBitSetTraits<llvm::codeview::LineFlags> {
164   static void bitset(IO &io, llvm::codeview::LineFlags &Flags) {
165     io.bitSetCase(Flags, "HasColumnInfo",
166                   llvm::codeview::LineFlags::HaveColumns);
167     io.enumFallback<Hex16>(Flags);
168   }
169 };
170 }
171 }
172
173 void ScalarTraits<HexFormattedString>::output(const HexFormattedString &Value,
174                                               void *ctx, raw_ostream &Out) {
175   StringRef Bytes(reinterpret_cast<const char *>(Value.Bytes.data()),
176                   Value.Bytes.size());
177   Out << toHex(Bytes);
178 }
179
180 StringRef ScalarTraits<HexFormattedString>::input(StringRef Scalar, void *ctxt,
181                                                   HexFormattedString &Value) {
182   std::string H = fromHex(Scalar);
183   Value.Bytes.assign(H.begin(), H.end());
184   return StringRef();
185 }
186
187 void MappingTraits<PdbObject>::mapping(IO &IO, PdbObject &Obj) {
188   // Create a single serialization context that will be passed through the
189   // entire process of serializing / deserializing a Tpi Stream.  This is
190   // especially important when we are going from Pdb -> Yaml because we need
191   // to maintain state in a TypeTableBuilder across mappings, and at the end of
192   // the entire process, we need to have one TypeTableBuilder that has every
193   // record.
194   pdb::yaml::SerializationContext Context(IO, Obj.Allocator);
195
196
197   IO.mapOptional("MSF", Obj.Headers);
198   IO.mapOptional("StreamSizes", Obj.StreamSizes);
199   IO.mapOptional("StreamMap", Obj.StreamMap);
200   IO.mapOptional("StringTable", Obj.StringTable);
201   IO.mapOptional("PdbStream", Obj.PdbStream);
202   IO.mapOptionalWithContext("DbiStream", Obj.DbiStream, Context);
203   IO.mapOptionalWithContext("TpiStream", Obj.TpiStream, Context);
204   IO.mapOptionalWithContext("IpiStream", Obj.IpiStream, Context);
205 }
206
207 void MappingTraits<MSFHeaders>::mapping(IO &IO, MSFHeaders &Obj) {
208   IO.mapOptional("SuperBlock", Obj.SuperBlock);
209   IO.mapOptional("NumDirectoryBlocks", Obj.NumDirectoryBlocks);
210   IO.mapOptional("DirectoryBlocks", Obj.DirectoryBlocks);
211   IO.mapOptional("NumStreams", Obj.NumStreams);
212   IO.mapOptional("FileSize", Obj.FileSize);
213 }
214
215 void MappingTraits<msf::SuperBlock>::mapping(IO &IO, msf::SuperBlock &SB) {
216   if (!IO.outputting()) {
217     ::memcpy(SB.MagicBytes, msf::Magic, sizeof(msf::Magic));
218   }
219
220   using u32 = support::ulittle32_t;
221   IO.mapOptional("BlockSize", SB.BlockSize, u32(4096U));
222   IO.mapOptional("FreeBlockMap", SB.FreeBlockMapBlock, u32(0U));
223   IO.mapOptional("NumBlocks", SB.NumBlocks, u32(0U));
224   IO.mapOptional("NumDirectoryBytes", SB.NumDirectoryBytes, u32(0U));
225   IO.mapOptional("Unknown1", SB.Unknown1, u32(0U));
226   IO.mapOptional("BlockMapAddr", SB.BlockMapAddr, u32(0U));
227 }
228
229 void MappingTraits<StreamBlockList>::mapping(IO &IO, StreamBlockList &SB) {
230   IO.mapRequired("Stream", SB.Blocks);
231 }
232
233 void MappingTraits<PdbInfoStream>::mapping(IO &IO, PdbInfoStream &Obj) {
234   IO.mapOptional("Age", Obj.Age, 1U);
235   IO.mapOptional("Guid", Obj.Guid);
236   IO.mapOptional("Signature", Obj.Signature, 0U);
237   IO.mapOptional("Features", Obj.Features);
238   IO.mapOptional("Version", Obj.Version, PdbImplVC70);
239 }
240
241 void MappingContextTraits<PdbDbiStream, pdb::yaml::SerializationContext>::mapping(IO &IO, PdbDbiStream &Obj, pdb::yaml::SerializationContext &Context) {
242   IO.mapOptional("VerHeader", Obj.VerHeader, PdbDbiV70);
243   IO.mapOptional("Age", Obj.Age, 1U);
244   IO.mapOptional("BuildNumber", Obj.BuildNumber, uint16_t(0U));
245   IO.mapOptional("PdbDllVersion", Obj.PdbDllVersion, 0U);
246   IO.mapOptional("PdbDllRbld", Obj.PdbDllRbld, uint16_t(0U));
247   IO.mapOptional("Flags", Obj.Flags, uint16_t(1U));
248   IO.mapOptional("MachineType", Obj.MachineType, PDB_Machine::x86);
249   IO.mapOptionalWithContext("Modules", Obj.ModInfos, Context);
250 }
251
252 void MappingContextTraits<PdbTpiStream, pdb::yaml::SerializationContext>::mapping(
253     IO &IO, pdb::yaml::PdbTpiStream &Obj, pdb::yaml::SerializationContext &Context) {
254   IO.mapOptional("Version", Obj.Version, PdbTpiV80);
255   IO.mapRequired("Records", Obj.Records, Context);
256 }
257
258 void MappingTraits<NamedStreamMapping>::mapping(IO &IO,
259                                                 NamedStreamMapping &Obj) {
260   IO.mapRequired("Name", Obj.StreamName);
261   IO.mapRequired("StreamNum", Obj.StreamNumber);
262 }
263
264 void MappingContextTraits<PdbSymbolRecord, pdb::yaml::SerializationContext>::mapping(IO &IO, PdbSymbolRecord &Obj, pdb::yaml::SerializationContext &Context) {
265   codeview::SymbolVisitorCallbackPipeline Pipeline;
266   codeview::SymbolSerializer Serializer(Context.Allocator);
267   codeview::SymbolDeserializer Deserializer(nullptr);
268   codeview::yaml::YamlSymbolDumper Dumper(IO);
269
270   if (IO.outputting()) {
271     // For PDB to Yaml, deserialize into a high level record type, then dump it.
272     Pipeline.addCallbackToPipeline(Deserializer);
273     Pipeline.addCallbackToPipeline(Dumper);
274   } else {
275     // For the other way around, dump it into a concrete structure, and then
276     // serialize it into the CVRecord.
277     Pipeline.addCallbackToPipeline(Dumper);
278     Pipeline.addCallbackToPipeline(Serializer);
279   }
280
281   codeview::CVSymbolVisitor Visitor(Pipeline);
282   consumeError(Visitor.visitSymbolRecord(Obj.Record));
283 }
284
285 void MappingContextTraits<PdbModiStream, pdb::yaml::SerializationContext>::mapping(IO &IO, PdbModiStream &Obj, pdb::yaml::SerializationContext &Context) {
286   IO.mapOptional("Signature", Obj.Signature, 4U);
287   IO.mapRequired("Records", Obj.Symbols, Context);
288 }
289
290 void MappingContextTraits<PdbDbiModuleInfo, pdb::yaml::SerializationContext>::mapping(IO &IO, PdbDbiModuleInfo &Obj, pdb::yaml::SerializationContext &Context) {
291   IO.mapRequired("Module", Obj.Mod);
292   IO.mapOptional("ObjFile", Obj.Obj, Obj.Mod);
293   IO.mapOptional("SourceFiles", Obj.SourceFiles);
294   IO.mapOptionalWithContext("LineInfo", Obj.FileLineInfo, Context);
295   IO.mapOptionalWithContext("Modi", Obj.Modi, Context);
296 }
297
298 void MappingContextTraits<pdb::yaml::PdbSourceLineEntry,
299                           pdb::yaml::SerializationContext>::
300     mapping(IO &IO, PdbSourceLineEntry &Obj,
301             pdb::yaml::SerializationContext &Context) {
302   IO.mapRequired("Offset", Obj.Offset);
303   IO.mapRequired("LineStart", Obj.LineStart);
304   IO.mapRequired("IsStatement", Obj.IsStatement);
305   IO.mapRequired("EndDelta", Obj.EndDelta);
306 }
307
308 void MappingContextTraits<pdb::yaml::PdbSourceColumnEntry,
309                           pdb::yaml::SerializationContext>::
310     mapping(IO &IO, PdbSourceColumnEntry &Obj,
311             pdb::yaml::SerializationContext &Context) {
312   IO.mapRequired("StartColumn", Obj.StartColumn);
313   IO.mapRequired("EndColumn", Obj.EndColumn);
314 };
315
316 void MappingContextTraits<pdb::yaml::PdbSourceLineBlock,
317                           pdb::yaml::SerializationContext>::
318     mapping(IO &IO, PdbSourceLineBlock &Obj,
319             pdb::yaml::SerializationContext &Context) {
320   IO.mapRequired("FileName", Obj.FileName);
321   IO.mapRequired("Lines", Obj.Lines, Context);
322   IO.mapRequired("Columns", Obj.Columns, Context);
323 };
324
325 void MappingContextTraits<pdb::yaml::PdbSourceFileChecksumEntry,
326                           pdb::yaml::SerializationContext>::
327     mapping(IO &IO, PdbSourceFileChecksumEntry &Obj,
328             pdb::yaml::SerializationContext &Context) {
329   IO.mapRequired("FileName", Obj.FileName);
330   IO.mapRequired("Kind", Obj.Kind);
331   IO.mapRequired("Checksum", Obj.ChecksumBytes);
332 };
333
334 void MappingContextTraits<pdb::yaml::PdbSourceLineInfo,
335                           pdb::yaml::SerializationContext>::
336     mapping(IO &IO, PdbSourceLineInfo &Obj,
337             pdb::yaml::SerializationContext &Context) {
338   IO.mapRequired("CodeSize", Obj.CodeSize);
339   IO.mapRequired("Flags", Obj.Flags);
340   IO.mapRequired("RelocOffset", Obj.RelocOffset);
341   IO.mapRequired("RelocSegment", Obj.RelocSegment);
342   IO.mapRequired("LineInfo", Obj.LineInfo, Context);
343 };
344
345 void MappingContextTraits<pdb::yaml::PdbSourceFileInfo,
346                           pdb::yaml::SerializationContext>::
347     mapping(IO &IO, PdbSourceFileInfo &Obj,
348             pdb::yaml::SerializationContext &Context) {
349   IO.mapOptionalWithContext("Lines", Obj.Lines, Context);
350   IO.mapOptionalWithContext("Checksums", Obj.FileChecksums, Context);
351 };
352
353 void MappingContextTraits<PdbTpiRecord, pdb::yaml::SerializationContext>::
354     mapping(IO &IO, pdb::yaml::PdbTpiRecord &Obj,
355             pdb::yaml::SerializationContext &Context) {
356   codeview::TypeVisitorCallbackPipeline Pipeline;
357   codeview::TypeDeserializer Deserializer;
358   codeview::TypeSerializer Serializer(Context.Allocator);
359   pdb::TpiHashUpdater Hasher;
360
361   if (IO.outputting()) {
362     // For PDB to Yaml, deserialize into a high level record type, then dump it.
363     Pipeline.addCallbackToPipeline(Deserializer);
364     Pipeline.addCallbackToPipeline(Context.Dumper);
365   } else {
366     // For Yaml to PDB, extract from the high level record type, then write it
367     // to bytes.
368
369     // This might be interpreted as a hack, but serializing FieldList
370     // sub-records requires having access to the same serializer being used by
371     // the FieldList itself.
372     Context.ActiveSerializer = &Serializer;
373     Pipeline.addCallbackToPipeline(Context.Dumper);
374     Pipeline.addCallbackToPipeline(Serializer);
375     Pipeline.addCallbackToPipeline(Hasher);
376   }
377
378   codeview::CVTypeVisitor Visitor(Pipeline);
379   consumeError(Visitor.visitTypeRecord(Obj.Record));
380   Context.ActiveSerializer = nullptr;
381 }