Zachary Turner | 7120a47 | 2016-06-06 20:37:05 +0000 | [diff] [blame] | 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" |
Zachary Turner | 2f951ce | 2016-08-31 21:42:26 +0000 | [diff] [blame] | 11 | |
Zachary Turner | ac5763e | 2016-08-18 16:49:29 +0000 | [diff] [blame] | 12 | #include "CodeViewYaml.h" |
Zachary Turner | c6d54da | 2016-09-09 17:46:17 +0000 | [diff] [blame] | 13 | #include "YamlSerializationContext.h" |
Zachary Turner | 7120a47 | 2016-06-06 20:37:05 +0000 | [diff] [blame] | 14 | |
Zachary Turner | ac5763e | 2016-08-18 16:49:29 +0000 | [diff] [blame] | 15 | #include "llvm/DebugInfo/CodeView/CVTypeVisitor.h" |
| 16 | #include "llvm/DebugInfo/CodeView/TypeDeserializer.h" |
Zachary Turner | c6d54da | 2016-09-09 17:46:17 +0000 | [diff] [blame] | 17 | #include "llvm/DebugInfo/CodeView/TypeSerializationVisitor.h" |
Zachary Turner | 2f951ce | 2016-08-31 21:42:26 +0000 | [diff] [blame] | 18 | #include "llvm/DebugInfo/CodeView/TypeVisitorCallbackPipeline.h" |
Zachary Turner | 8848a7a | 2016-07-06 18:05:57 +0000 | [diff] [blame] | 19 | #include "llvm/DebugInfo/PDB/PDBExtras.h" |
Zachary Turner | ac5763e | 2016-08-18 16:49:29 +0000 | [diff] [blame] | 20 | #include "llvm/DebugInfo/PDB/PDBTypes.h" |
Zachary Turner | 7120a47 | 2016-06-06 20:37:05 +0000 | [diff] [blame] | 21 | #include "llvm/DebugInfo/PDB/Raw/PDBFile.h" |
Zachary Turner | 620961d | 2016-09-14 23:00:02 +0000 | [diff] [blame] | 22 | #include "llvm/DebugInfo/PDB/Raw/TpiHashing.h" |
Zachary Turner | 7120a47 | 2016-06-06 20:37:05 +0000 | [diff] [blame] | 23 | |
| 24 | using namespace llvm; |
| 25 | using namespace llvm::pdb; |
| 26 | using namespace llvm::pdb::yaml; |
Zachary Turner | ac5763e | 2016-08-18 16:49:29 +0000 | [diff] [blame] | 27 | using namespace llvm::yaml; |
| 28 | |
| 29 | LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(uint32_t) |
| 30 | LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::StringRef) |
| 31 | LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::pdb::yaml::NamedStreamMapping) |
| 32 | LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::pdb::yaml::PdbDbiModuleInfo) |
| 33 | LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::pdb::yaml::PdbTpiRecord) |
| 34 | LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::pdb::yaml::StreamBlockList) |
Zachary Turner | 7120a47 | 2016-06-06 20:37:05 +0000 | [diff] [blame] | 35 | |
Zachary Turner | 8848a7a | 2016-07-06 18:05:57 +0000 | [diff] [blame] | 36 | namespace llvm { |
| 37 | namespace yaml { |
Zachary Turner | ac5763e | 2016-08-18 16:49:29 +0000 | [diff] [blame] | 38 | |
Zachary Turner | 8848a7a | 2016-07-06 18:05:57 +0000 | [diff] [blame] | 39 | template <> struct ScalarTraits<llvm::pdb::PDB_UniqueId> { |
| 40 | static void output(const llvm::pdb::PDB_UniqueId &S, void *, |
| 41 | llvm::raw_ostream &OS) { |
| 42 | OS << S; |
| 43 | } |
| 44 | |
| 45 | static StringRef input(StringRef Scalar, void *Ctx, |
| 46 | llvm::pdb::PDB_UniqueId &S) { |
| 47 | if (Scalar.size() != 38) |
| 48 | return "GUID strings are 38 characters long"; |
| 49 | if (Scalar[0] != '{' || Scalar[37] != '}') |
| 50 | return "GUID is not enclosed in {}"; |
| 51 | if (Scalar[9] != '-' || Scalar[14] != '-' || Scalar[19] != '-' || |
| 52 | Scalar[24] != '-') |
| 53 | return "GUID sections are not properly delineated with dashes"; |
| 54 | |
| 55 | char *OutBuffer = S.Guid; |
| 56 | for (auto Iter = Scalar.begin(); Iter != Scalar.end();) { |
| 57 | if (*Iter == '-' || *Iter == '{' || *Iter == '}') { |
| 58 | ++Iter; |
| 59 | continue; |
| 60 | } |
| 61 | uint8_t Value = (llvm::hexDigitValue(*Iter) << 4); |
| 62 | ++Iter; |
| 63 | Value |= llvm::hexDigitValue(*Iter); |
| 64 | ++Iter; |
| 65 | *OutBuffer++ = Value; |
| 66 | } |
| 67 | |
| 68 | return ""; |
| 69 | } |
| 70 | |
| 71 | static bool mustQuote(StringRef Scalar) { return needsQuotes(Scalar); } |
| 72 | }; |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 73 | |
| 74 | template <> struct ScalarEnumerationTraits<llvm::pdb::PDB_Machine> { |
| 75 | static void enumeration(IO &io, llvm::pdb::PDB_Machine &Value) { |
| 76 | io.enumCase(Value, "Invalid", PDB_Machine::Invalid); |
| 77 | io.enumCase(Value, "Am33", PDB_Machine::Am33); |
| 78 | io.enumCase(Value, "Amd64", PDB_Machine::Amd64); |
| 79 | io.enumCase(Value, "Arm", PDB_Machine::Arm); |
| 80 | io.enumCase(Value, "ArmNT", PDB_Machine::ArmNT); |
| 81 | io.enumCase(Value, "Ebc", PDB_Machine::Ebc); |
| 82 | io.enumCase(Value, "x86", PDB_Machine::x86); |
| 83 | io.enumCase(Value, "Ia64", PDB_Machine::Ia64); |
| 84 | io.enumCase(Value, "M32R", PDB_Machine::M32R); |
| 85 | io.enumCase(Value, "Mips16", PDB_Machine::Mips16); |
| 86 | io.enumCase(Value, "MipsFpu", PDB_Machine::MipsFpu); |
| 87 | io.enumCase(Value, "MipsFpu16", PDB_Machine::MipsFpu16); |
| 88 | io.enumCase(Value, "PowerPCFP", PDB_Machine::PowerPCFP); |
| 89 | io.enumCase(Value, "R4000", PDB_Machine::R4000); |
| 90 | io.enumCase(Value, "SH3", PDB_Machine::SH3); |
| 91 | io.enumCase(Value, "SH3DSP", PDB_Machine::SH3DSP); |
| 92 | io.enumCase(Value, "Thumb", PDB_Machine::Thumb); |
| 93 | io.enumCase(Value, "WceMipsV2", PDB_Machine::WceMipsV2); |
| 94 | } |
| 95 | }; |
| 96 | |
| 97 | template <> struct ScalarEnumerationTraits<llvm::pdb::PdbRaw_DbiVer> { |
| 98 | static void enumeration(IO &io, llvm::pdb::PdbRaw_DbiVer &Value) { |
| 99 | io.enumCase(Value, "V41", llvm::pdb::PdbRaw_DbiVer::PdbDbiVC41); |
| 100 | io.enumCase(Value, "V50", llvm::pdb::PdbRaw_DbiVer::PdbDbiV50); |
| 101 | io.enumCase(Value, "V60", llvm::pdb::PdbRaw_DbiVer::PdbDbiV60); |
| 102 | io.enumCase(Value, "V70", llvm::pdb::PdbRaw_DbiVer::PdbDbiV70); |
| 103 | io.enumCase(Value, "V110", llvm::pdb::PdbRaw_DbiVer::PdbDbiV110); |
| 104 | } |
| 105 | }; |
| 106 | |
| 107 | template <> struct ScalarEnumerationTraits<llvm::pdb::PdbRaw_ImplVer> { |
| 108 | static void enumeration(IO &io, llvm::pdb::PdbRaw_ImplVer &Value) { |
| 109 | io.enumCase(Value, "VC2", llvm::pdb::PdbRaw_ImplVer::PdbImplVC2); |
| 110 | io.enumCase(Value, "VC4", llvm::pdb::PdbRaw_ImplVer::PdbImplVC4); |
| 111 | io.enumCase(Value, "VC41", llvm::pdb::PdbRaw_ImplVer::PdbImplVC41); |
| 112 | io.enumCase(Value, "VC50", llvm::pdb::PdbRaw_ImplVer::PdbImplVC50); |
| 113 | io.enumCase(Value, "VC98", llvm::pdb::PdbRaw_ImplVer::PdbImplVC98); |
| 114 | io.enumCase(Value, "VC70Dep", llvm::pdb::PdbRaw_ImplVer::PdbImplVC70Dep); |
| 115 | io.enumCase(Value, "VC70", llvm::pdb::PdbRaw_ImplVer::PdbImplVC70); |
| 116 | io.enumCase(Value, "VC80", llvm::pdb::PdbRaw_ImplVer::PdbImplVC80); |
| 117 | io.enumCase(Value, "VC110", llvm::pdb::PdbRaw_ImplVer::PdbImplVC110); |
| 118 | io.enumCase(Value, "VC140", llvm::pdb::PdbRaw_ImplVer::PdbImplVC140); |
| 119 | } |
| 120 | }; |
Zachary Turner | ac5763e | 2016-08-18 16:49:29 +0000 | [diff] [blame] | 121 | |
| 122 | template <> struct ScalarEnumerationTraits<llvm::pdb::PdbRaw_TpiVer> { |
| 123 | static void enumeration(IO &io, llvm::pdb::PdbRaw_TpiVer &Value) { |
| 124 | io.enumCase(Value, "VC40", llvm::pdb::PdbRaw_TpiVer::PdbTpiV40); |
| 125 | io.enumCase(Value, "VC41", llvm::pdb::PdbRaw_TpiVer::PdbTpiV41); |
| 126 | io.enumCase(Value, "VC50", llvm::pdb::PdbRaw_TpiVer::PdbTpiV50); |
| 127 | io.enumCase(Value, "VC70", llvm::pdb::PdbRaw_TpiVer::PdbTpiV70); |
| 128 | io.enumCase(Value, "VC80", llvm::pdb::PdbRaw_TpiVer::PdbTpiV80); |
| 129 | } |
| 130 | }; |
Zachary Turner | 8848a7a | 2016-07-06 18:05:57 +0000 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 134 | void MappingTraits<PdbObject>::mapping(IO &IO, PdbObject &Obj) { |
| 135 | IO.mapOptional("MSF", Obj.Headers); |
| 136 | IO.mapOptional("StreamSizes", Obj.StreamSizes); |
| 137 | IO.mapOptional("StreamMap", Obj.StreamMap); |
| 138 | IO.mapOptional("PdbStream", Obj.PdbStream); |
| 139 | IO.mapOptional("DbiStream", Obj.DbiStream); |
Zachary Turner | c6d54da | 2016-09-09 17:46:17 +0000 | [diff] [blame] | 140 | IO.mapOptionalWithContext("TpiStream", Obj.TpiStream, Obj.Allocator); |
Zachary Turner | de9ba15 | 2016-09-15 18:22:31 +0000 | [diff] [blame^] | 141 | IO.mapOptionalWithContext("IpiStream", Obj.IpiStream, Obj.Allocator); |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Zachary Turner | a3225b0 | 2016-07-29 20:56:36 +0000 | [diff] [blame] | 144 | void MappingTraits<MSFHeaders>::mapping(IO &IO, MSFHeaders &Obj) { |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 145 | IO.mapRequired("SuperBlock", Obj.SuperBlock); |
| 146 | IO.mapRequired("NumDirectoryBlocks", Obj.NumDirectoryBlocks); |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 147 | IO.mapRequired("DirectoryBlocks", Obj.DirectoryBlocks); |
| 148 | IO.mapRequired("NumStreams", Obj.NumStreams); |
| 149 | IO.mapRequired("FileSize", Obj.FileSize); |
| 150 | } |
| 151 | |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 152 | void MappingTraits<msf::SuperBlock>::mapping(IO &IO, msf::SuperBlock &SB) { |
Zachary Turner | 1dc9fd3 | 2016-06-14 20:48:36 +0000 | [diff] [blame] | 153 | if (!IO.outputting()) { |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 154 | ::memcpy(SB.MagicBytes, msf::Magic, sizeof(msf::Magic)); |
Zachary Turner | 1dc9fd3 | 2016-06-14 20:48:36 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | IO.mapRequired("BlockSize", SB.BlockSize); |
Zachary Turner | b927e02 | 2016-07-15 22:17:19 +0000 | [diff] [blame] | 158 | IO.mapRequired("FreeBlockMap", SB.FreeBlockMapBlock); |
Zachary Turner | 1dc9fd3 | 2016-06-14 20:48:36 +0000 | [diff] [blame] | 159 | IO.mapRequired("NumBlocks", SB.NumBlocks); |
| 160 | IO.mapRequired("NumDirectoryBytes", SB.NumDirectoryBytes); |
| 161 | IO.mapRequired("Unknown1", SB.Unknown1); |
| 162 | IO.mapRequired("BlockMapAddr", SB.BlockMapAddr); |
| 163 | } |
| 164 | |
| 165 | void MappingTraits<StreamBlockList>::mapping(IO &IO, StreamBlockList &SB) { |
| 166 | IO.mapRequired("Stream", SB.Blocks); |
| 167 | } |
| 168 | |
Zachary Turner | 8848a7a | 2016-07-06 18:05:57 +0000 | [diff] [blame] | 169 | void MappingTraits<PdbInfoStream>::mapping(IO &IO, PdbInfoStream &Obj) { |
| 170 | IO.mapRequired("Age", Obj.Age); |
| 171 | IO.mapRequired("Guid", Obj.Guid); |
| 172 | IO.mapRequired("Signature", Obj.Signature); |
| 173 | IO.mapRequired("Version", Obj.Version); |
Zachary Turner | 5e534c7 | 2016-07-15 22:17:08 +0000 | [diff] [blame] | 174 | IO.mapRequired("NamedStreams", Obj.NamedStreams); |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | void MappingTraits<PdbDbiStream>::mapping(IO &IO, PdbDbiStream &Obj) { |
| 178 | IO.mapRequired("VerHeader", Obj.VerHeader); |
| 179 | IO.mapRequired("Age", Obj.Age); |
| 180 | IO.mapRequired("BuildNumber", Obj.BuildNumber); |
| 181 | IO.mapRequired("PdbDllVersion", Obj.PdbDllVersion); |
| 182 | IO.mapRequired("PdbDllRbld", Obj.PdbDllRbld); |
| 183 | IO.mapRequired("Flags", Obj.Flags); |
| 184 | IO.mapRequired("MachineType", Obj.MachineType); |
Zachary Turner | d218c26 | 2016-07-22 15:46:37 +0000 | [diff] [blame] | 185 | IO.mapOptional("Modules", Obj.ModInfos); |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 186 | } |
Zachary Turner | 5e534c7 | 2016-07-15 22:17:08 +0000 | [diff] [blame] | 187 | |
Zachary Turner | c6d54da | 2016-09-09 17:46:17 +0000 | [diff] [blame] | 188 | void MappingContextTraits<PdbTpiStream, BumpPtrAllocator>::mapping( |
| 189 | IO &IO, pdb::yaml::PdbTpiStream &Obj, BumpPtrAllocator &Allocator) { |
| 190 | // Create a single serialization context that will be passed through the |
| 191 | // entire process of serializing / deserializing a Tpi Stream. This is |
| 192 | // especially important when we are going from Pdb -> Yaml because we need |
| 193 | // to maintain state in a TypeTableBuilder across mappings, and at the end of |
| 194 | // the entire process, we need to have one TypeTableBuilder that has every |
| 195 | // record. |
| 196 | pdb::yaml::SerializationContext Context(IO, Allocator); |
| 197 | |
Zachary Turner | ac5763e | 2016-08-18 16:49:29 +0000 | [diff] [blame] | 198 | IO.mapRequired("Version", Obj.Version); |
Zachary Turner | c6d54da | 2016-09-09 17:46:17 +0000 | [diff] [blame] | 199 | IO.mapRequired("Records", Obj.Records, Context); |
Zachary Turner | ac5763e | 2016-08-18 16:49:29 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Zachary Turner | 5e534c7 | 2016-07-15 22:17:08 +0000 | [diff] [blame] | 202 | void MappingTraits<NamedStreamMapping>::mapping(IO &IO, |
| 203 | NamedStreamMapping &Obj) { |
| 204 | IO.mapRequired("Name", Obj.StreamName); |
| 205 | IO.mapRequired("StreamNum", Obj.StreamNumber); |
| 206 | } |
Zachary Turner | d218c26 | 2016-07-22 15:46:37 +0000 | [diff] [blame] | 207 | |
| 208 | void MappingTraits<PdbDbiModuleInfo>::mapping(IO &IO, PdbDbiModuleInfo &Obj) { |
| 209 | IO.mapRequired("Module", Obj.Mod); |
| 210 | IO.mapRequired("ObjFile", Obj.Obj); |
| 211 | IO.mapOptional("SourceFiles", Obj.SourceFiles); |
| 212 | } |
Zachary Turner | ac5763e | 2016-08-18 16:49:29 +0000 | [diff] [blame] | 213 | |
Zachary Turner | c6d54da | 2016-09-09 17:46:17 +0000 | [diff] [blame] | 214 | void MappingContextTraits<PdbTpiRecord, pdb::yaml::SerializationContext>:: |
| 215 | mapping(IO &IO, pdb::yaml::PdbTpiRecord &Obj, |
| 216 | pdb::yaml::SerializationContext &Context) { |
| 217 | codeview::TypeVisitorCallbackPipeline Pipeline; |
| 218 | codeview::TypeDeserializer Deserializer; |
| 219 | codeview::TypeSerializationVisitor Serializer(Context.FieldListBuilder, |
| 220 | Context.TypeTableBuilder); |
Zachary Turner | 620961d | 2016-09-14 23:00:02 +0000 | [diff] [blame] | 221 | pdb::TpiHashUpdater Hasher; |
Zachary Turner | c6d54da | 2016-09-09 17:46:17 +0000 | [diff] [blame] | 222 | |
Zachary Turner | ac5763e | 2016-08-18 16:49:29 +0000 | [diff] [blame] | 223 | if (IO.outputting()) { |
Zachary Turner | c6d54da | 2016-09-09 17:46:17 +0000 | [diff] [blame] | 224 | // For PDB to Yaml, deserialize into a high level record type, then dump it. |
Zachary Turner | 2f951ce | 2016-08-31 21:42:26 +0000 | [diff] [blame] | 225 | Pipeline.addCallbackToPipeline(Deserializer); |
Zachary Turner | c6d54da | 2016-09-09 17:46:17 +0000 | [diff] [blame] | 226 | Pipeline.addCallbackToPipeline(Context.Dumper); |
Zachary Turner | ac5763e | 2016-08-18 16:49:29 +0000 | [diff] [blame] | 227 | } else { |
Zachary Turner | c6d54da | 2016-09-09 17:46:17 +0000 | [diff] [blame] | 228 | // For Yaml to PDB, extract from the high level record type, then write it |
| 229 | // to bytes. |
| 230 | Pipeline.addCallbackToPipeline(Context.Dumper); |
| 231 | Pipeline.addCallbackToPipeline(Serializer); |
Zachary Turner | 620961d | 2016-09-14 23:00:02 +0000 | [diff] [blame] | 232 | Pipeline.addCallbackToPipeline(Hasher); |
Zachary Turner | c6d54da | 2016-09-09 17:46:17 +0000 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | codeview::CVTypeVisitor Visitor(Pipeline); |
| 236 | consumeError(Visitor.visitTypeRecord(Obj.Record)); |
Zachary Turner | ac5763e | 2016-08-18 16:49:29 +0000 | [diff] [blame] | 237 | } |