1//===-- PdbYaml.cpp ------------------------------------------- *- 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 "PdbYaml.h"
10
11#include "llvm/ADT/StringExtras.h"
12#include "llvm/DebugInfo/CodeView/CVTypeVisitor.h"
13#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
14#include "llvm/DebugInfo/PDB/Native/RawTypes.h"
15#include "llvm/DebugInfo/PDB/Native/TpiHashing.h"
16#include "llvm/DebugInfo/PDB/PDBTypes.h"
17#include "llvm/ObjectYAML/CodeViewYAMLDebugSections.h"
18#include "llvm/ObjectYAML/CodeViewYAMLTypes.h"
19
20using namespace llvm;
21using namespace llvm::pdb;
22using namespace llvm::pdb::yaml;
23using namespace llvm::yaml;
24
25LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::pdb::yaml::CoffSectionHeader)
26LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::pdb::yaml::NamedStreamMapping)
27LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::pdb::yaml::PdbDbiModuleInfo)
28LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::pdb::yaml::PdbDbiSectionContrib)
29LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::pdb::yaml::StreamBlockList)
30LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(llvm::pdb::PdbRaw_FeatureSig)
31
32namespace llvm {
33namespace yaml {
34
35template <> struct ScalarEnumerationTraits<llvm::pdb::PDB_Machine> {
36 static void enumeration(IO &io, llvm::pdb::PDB_Machine &Value) {
37 io.enumCase(Val&: Value, Str: "Invalid", ConstVal: PDB_Machine::Invalid);
38 io.enumCase(Val&: Value, Str: "Am33", ConstVal: PDB_Machine::Am33);
39 io.enumCase(Val&: Value, Str: "Amd64", ConstVal: PDB_Machine::Amd64);
40 io.enumCase(Val&: Value, Str: "Arm", ConstVal: PDB_Machine::Arm);
41 io.enumCase(Val&: Value, Str: "ArmNT", ConstVal: PDB_Machine::ArmNT);
42 io.enumCase(Val&: Value, Str: "Ebc", ConstVal: PDB_Machine::Ebc);
43 io.enumCase(Val&: Value, Str: "x86", ConstVal: PDB_Machine::x86);
44 io.enumCase(Val&: Value, Str: "Ia64", ConstVal: PDB_Machine::Ia64);
45 io.enumCase(Val&: Value, Str: "M32R", ConstVal: PDB_Machine::M32R);
46 io.enumCase(Val&: Value, Str: "Mips16", ConstVal: PDB_Machine::Mips16);
47 io.enumCase(Val&: Value, Str: "MipsFpu", ConstVal: PDB_Machine::MipsFpu);
48 io.enumCase(Val&: Value, Str: "MipsFpu16", ConstVal: PDB_Machine::MipsFpu16);
49 io.enumCase(Val&: Value, Str: "PowerPCFP", ConstVal: PDB_Machine::PowerPCFP);
50 io.enumCase(Val&: Value, Str: "R4000", ConstVal: PDB_Machine::R4000);
51 io.enumCase(Val&: Value, Str: "SH3", ConstVal: PDB_Machine::SH3);
52 io.enumCase(Val&: Value, Str: "SH3DSP", ConstVal: PDB_Machine::SH3DSP);
53 io.enumCase(Val&: Value, Str: "Thumb", ConstVal: PDB_Machine::Thumb);
54 io.enumCase(Val&: Value, Str: "WceMipsV2", ConstVal: PDB_Machine::WceMipsV2);
55 io.enumCase(Val&: Value, Str: "Arm64", ConstVal: PDB_Machine::Arm64);
56 }
57};
58
59template <> struct ScalarEnumerationTraits<llvm::pdb::PdbRaw_DbiVer> {
60 static void enumeration(IO &io, llvm::pdb::PdbRaw_DbiVer &Value) {
61 io.enumCase(Val&: Value, Str: "V41", ConstVal: llvm::pdb::PdbRaw_DbiVer::PdbDbiVC41);
62 io.enumCase(Val&: Value, Str: "V50", ConstVal: llvm::pdb::PdbRaw_DbiVer::PdbDbiV50);
63 io.enumCase(Val&: Value, Str: "V60", ConstVal: llvm::pdb::PdbRaw_DbiVer::PdbDbiV60);
64 io.enumCase(Val&: Value, Str: "V70", ConstVal: llvm::pdb::PdbRaw_DbiVer::PdbDbiV70);
65 io.enumCase(Val&: Value, Str: "V110", ConstVal: llvm::pdb::PdbRaw_DbiVer::PdbDbiV110);
66 }
67};
68
69template <> struct ScalarEnumerationTraits<llvm::pdb::PdbRaw_ImplVer> {
70 static void enumeration(IO &io, llvm::pdb::PdbRaw_ImplVer &Value) {
71 io.enumCase(Val&: Value, Str: "VC2", ConstVal: llvm::pdb::PdbRaw_ImplVer::PdbImplVC2);
72 io.enumCase(Val&: Value, Str: "VC4", ConstVal: llvm::pdb::PdbRaw_ImplVer::PdbImplVC4);
73 io.enumCase(Val&: Value, Str: "VC41", ConstVal: llvm::pdb::PdbRaw_ImplVer::PdbImplVC41);
74 io.enumCase(Val&: Value, Str: "VC50", ConstVal: llvm::pdb::PdbRaw_ImplVer::PdbImplVC50);
75 io.enumCase(Val&: Value, Str: "VC98", ConstVal: llvm::pdb::PdbRaw_ImplVer::PdbImplVC98);
76 io.enumCase(Val&: Value, Str: "VC70Dep", ConstVal: llvm::pdb::PdbRaw_ImplVer::PdbImplVC70Dep);
77 io.enumCase(Val&: Value, Str: "VC70", ConstVal: llvm::pdb::PdbRaw_ImplVer::PdbImplVC70);
78 io.enumCase(Val&: Value, Str: "VC80", ConstVal: llvm::pdb::PdbRaw_ImplVer::PdbImplVC80);
79 io.enumCase(Val&: Value, Str: "VC110", ConstVal: llvm::pdb::PdbRaw_ImplVer::PdbImplVC110);
80 io.enumCase(Val&: Value, Str: "VC140", ConstVal: llvm::pdb::PdbRaw_ImplVer::PdbImplVC140);
81 }
82};
83
84template <> struct ScalarEnumerationTraits<llvm::pdb::PdbRaw_TpiVer> {
85 static void enumeration(IO &io, llvm::pdb::PdbRaw_TpiVer &Value) {
86 io.enumCase(Val&: Value, Str: "VC40", ConstVal: llvm::pdb::PdbRaw_TpiVer::PdbTpiV40);
87 io.enumCase(Val&: Value, Str: "VC41", ConstVal: llvm::pdb::PdbRaw_TpiVer::PdbTpiV41);
88 io.enumCase(Val&: Value, Str: "VC50", ConstVal: llvm::pdb::PdbRaw_TpiVer::PdbTpiV50);
89 io.enumCase(Val&: Value, Str: "VC70", ConstVal: llvm::pdb::PdbRaw_TpiVer::PdbTpiV70);
90 io.enumCase(Val&: Value, Str: "VC80", ConstVal: llvm::pdb::PdbRaw_TpiVer::PdbTpiV80);
91 }
92};
93
94template <> struct ScalarEnumerationTraits<llvm::pdb::PdbRaw_FeatureSig> {
95 static void enumeration(IO &io, PdbRaw_FeatureSig &Features) {
96 io.enumCase(Val&: Features, Str: "MinimalDebugInfo",
97 ConstVal: PdbRaw_FeatureSig::MinimalDebugInfo);
98 io.enumCase(Val&: Features, Str: "NoTypeMerge", ConstVal: PdbRaw_FeatureSig::NoTypeMerge);
99 io.enumCase(Val&: Features, Str: "VC110", ConstVal: PdbRaw_FeatureSig::VC110);
100 io.enumCase(Val&: Features, Str: "VC140", ConstVal: PdbRaw_FeatureSig::VC140);
101 }
102};
103
104template <> struct ScalarEnumerationTraits<llvm::pdb::PdbRaw_DbiSecContribVer> {
105 static void enumeration(IO &IO, PdbRaw_DbiSecContribVer &Ver) {
106 IO.enumCase(Val&: Ver, Str: "Ver60", ConstVal: PdbRaw_DbiSecContribVer::DbiSecContribVer60);
107 IO.enumCase(Val&: Ver, Str: "V2", ConstVal: PdbRaw_DbiSecContribVer::DbiSecContribV2);
108 }
109};
110}
111}
112
113void MappingTraits<PdbObject>::mapping(IO &IO, PdbObject &Obj) {
114 IO.mapOptional(Key: "MSF", Val&: Obj.Headers);
115 IO.mapOptional(Key: "StreamSizes", Val&: Obj.StreamSizes);
116 IO.mapOptional(Key: "StreamMap", Val&: Obj.StreamMap);
117 IO.mapOptional(Key: "StringTable", Val&: Obj.StringTable);
118 IO.mapOptional(Key: "PdbStream", Val&: Obj.PdbStream);
119 IO.mapOptional(Key: "DbiStream", Val&: Obj.DbiStream);
120 IO.mapOptional(Key: "TpiStream", Val&: Obj.TpiStream);
121 IO.mapOptional(Key: "IpiStream", Val&: Obj.IpiStream);
122 IO.mapOptional(Key: "PublicsStream", Val&: Obj.PublicsStream);
123 IO.mapOptional(Key: "DXContainerStream", Val&: Obj.DXContainerStream);
124}
125
126void MappingTraits<MSFHeaders>::mapping(IO &IO, MSFHeaders &Obj) {
127 IO.mapOptional(Key: "SuperBlock", Val&: Obj.SuperBlock);
128 IO.mapOptional(Key: "NumDirectoryBlocks", Val&: Obj.NumDirectoryBlocks);
129 IO.mapOptional(Key: "DirectoryBlocks", Val&: Obj.DirectoryBlocks);
130 IO.mapOptional(Key: "NumStreams", Val&: Obj.NumStreams);
131 IO.mapOptional(Key: "FileSize", Val&: Obj.FileSize);
132}
133
134void MappingTraits<msf::SuperBlock>::mapping(IO &IO, msf::SuperBlock &SB) {
135 if (!IO.outputting()) {
136 ::memcpy(dest: SB.MagicBytes, src: msf::Magic, n: sizeof(msf::Magic));
137 }
138
139 using u32 = support::ulittle32_t;
140 IO.mapOptional(Key: "BlockSize", Val&: SB.BlockSize, Default: u32(4096U));
141 IO.mapOptional(Key: "FreeBlockMap", Val&: SB.FreeBlockMapBlock, Default: u32(0U));
142 IO.mapOptional(Key: "NumBlocks", Val&: SB.NumBlocks, Default: u32(0U));
143 IO.mapOptional(Key: "NumDirectoryBytes", Val&: SB.NumDirectoryBytes, Default: u32(0U));
144 IO.mapOptional(Key: "Unknown1", Val&: SB.Unknown1, Default: u32(0U));
145 IO.mapOptional(Key: "BlockMapAddr", Val&: SB.BlockMapAddr, Default: u32(0U));
146}
147
148CoffSectionHeader::CoffSectionHeader() = default;
149
150CoffSectionHeader::CoffSectionHeader(const object::coff_section &Section)
151 : Name(Section.Name), VirtualSize(Section.VirtualSize),
152 VirtualAddress(Section.VirtualAddress),
153 SizeOfRawData(Section.SizeOfRawData),
154 PointerToRawData(Section.PointerToRawData),
155 PointerToRelocations(Section.PointerToRelocations),
156 PointerToLinenumbers(Section.PointerToLinenumbers),
157 NumberOfRelocations(Section.NumberOfRelocations),
158 NumberOfLinenumbers(Section.NumberOfLinenumbers),
159 Characteristics(Section.Characteristics) {}
160
161object::coff_section CoffSectionHeader::toCoffSection() const {
162 object::coff_section Sec;
163 std::memset(s: Sec.Name, c: 0, n: COFF::NameSize);
164 std::memcpy(dest: Sec.Name, src: Name.data(),
165 n: std::min(a: static_cast<size_t>(COFF::NameSize), b: Name.size()));
166 Sec.VirtualSize = VirtualSize;
167 Sec.VirtualAddress = VirtualAddress;
168 Sec.SizeOfRawData = SizeOfRawData;
169 Sec.PointerToRawData = PointerToRawData;
170 Sec.PointerToRelocations = PointerToRelocations;
171 Sec.PointerToLinenumbers = PointerToLinenumbers;
172 Sec.NumberOfRelocations = NumberOfRelocations;
173 Sec.NumberOfLinenumbers = NumberOfLinenumbers;
174 Sec.Characteristics = Characteristics;
175 return Sec;
176}
177
178void MappingTraits<CoffSectionHeader>::mapping(IO &IO, CoffSectionHeader &Obj) {
179 IO.mapRequired(Key: "Name", Val&: Obj.Name);
180 IO.mapOptional(Key: "VirtualSize", Val&: Obj.VirtualSize);
181 IO.mapOptional(Key: "VirtualAddress", Val&: Obj.VirtualAddress);
182 IO.mapOptional(Key: "SizeOfRawData", Val&: Obj.SizeOfRawData);
183 IO.mapOptional(Key: "PointerToRawData", Val&: Obj.PointerToRawData);
184 IO.mapOptional(Key: "PointerToRelocations", Val&: Obj.PointerToRelocations);
185 IO.mapOptional(Key: "PointerToLinenumbers", Val&: Obj.PointerToLinenumbers);
186 IO.mapOptional(Key: "NumberOfRelocations", Val&: Obj.NumberOfRelocations);
187 IO.mapOptional(Key: "NumberOfLinenumbers", Val&: Obj.NumberOfLinenumbers);
188 IO.mapOptional(Key: "Characteristics", Val&: Obj.Characteristics);
189}
190
191void MappingTraits<StreamBlockList>::mapping(IO &IO, StreamBlockList &SB) {
192 IO.mapRequired(Key: "Stream", Val&: SB.Blocks);
193}
194
195void MappingTraits<PdbInfoStream>::mapping(IO &IO, PdbInfoStream &Obj) {
196 IO.mapOptional(Key: "Age", Val&: Obj.Age, Default: 1U);
197 IO.mapOptional(Key: "Guid", Val&: Obj.Guid);
198 IO.mapOptional(Key: "Signature", Val&: Obj.Signature, Default: 0U);
199 IO.mapOptional(Key: "Features", Val&: Obj.Features);
200 IO.mapOptional(Key: "Version", Val&: Obj.Version, Default: PdbImplVC70);
201}
202
203void MappingTraits<PdbDbiSectionContribs>::mapping(IO &IO,
204 PdbDbiSectionContribs &Obj) {
205 IO.mapOptional(Key: "Version", Val&: Obj.Version,
206 Default: PdbRaw_DbiSecContribVer::DbiSecContribVer60);
207 IO.mapOptional(Key: "Items", Val&: Obj.Items);
208}
209
210void MappingTraits<PdbDbiSectionContrib>::mapping(IO &IO,
211 PdbDbiSectionContrib &Obj) {
212 IO.mapOptional(Key: "ISect", Val&: Obj.ISect);
213 IO.mapOptional(Key: "Offset", Val&: Obj.Off);
214 IO.mapOptional(Key: "Size", Val&: Obj.Size);
215 IO.mapOptional(Key: "Characteristics", Val&: Obj.Characteristics);
216 IO.mapOptional(Key: "Imod", Val&: Obj.Imod);
217 IO.mapOptional(Key: "DataCrc", Val&: Obj.DataCrc);
218 IO.mapOptional(Key: "RelocCrc", Val&: Obj.RelocCrc);
219 IO.mapOptional(Key: "ISectCoff", Val&: Obj.ISectCoff);
220}
221
222void MappingTraits<PdbDbiStream>::mapping(IO &IO, PdbDbiStream &Obj) {
223 IO.mapOptional(Key: "VerHeader", Val&: Obj.VerHeader, Default: PdbDbiV70);
224 IO.mapOptional(Key: "Age", Val&: Obj.Age, Default: 1U);
225 IO.mapOptional(Key: "BuildNumber", Val&: Obj.BuildNumber, Default: uint16_t(0U));
226 IO.mapOptional(Key: "PdbDllVersion", Val&: Obj.PdbDllVersion, Default: 0U);
227 IO.mapOptional(Key: "PdbDllRbld", Val&: Obj.PdbDllRbld, Default: uint16_t(0U));
228 IO.mapOptional(Key: "Flags", Val&: Obj.Flags, Default: uint16_t(1U));
229 IO.mapOptional(Key: "MachineType", Val&: Obj.MachineType, Default: PDB_Machine::x86);
230 // This is a workaround for IO not having document context with the
231 // machine type. The machine type is needed to properly parse Register enums
232 // in the PDB.
233 if (!IO.getContext()) {
234 Obj.FakeHeader.Machine = static_cast<uint16_t>(Obj.MachineType);
235 IO.setContext(&Obj.FakeHeader);
236 }
237 IO.mapOptional(Key: "Modules", Val&: Obj.ModInfos);
238 IO.mapOptional(Key: "SectionHeaders", Val&: Obj.SectionHeaders);
239 IO.mapOptional(Key: "SectionContribs", Val&: Obj.SectionContribs);
240}
241
242void MappingTraits<PdbTpiStream>::mapping(IO &IO,
243 pdb::yaml::PdbTpiStream &Obj) {
244 IO.mapOptional(Key: "Version", Val&: Obj.Version, Default: PdbTpiV80);
245 IO.mapRequired(Key: "Records", Val&: Obj.Records);
246}
247
248void MappingTraits<PdbPublicsStream>::mapping(
249 IO &IO, pdb::yaml::PdbPublicsStream &Obj) {
250 IO.mapRequired(Key: "Records", Val&: Obj.PubSyms);
251}
252
253void MappingTraits<NamedStreamMapping>::mapping(IO &IO,
254 NamedStreamMapping &Obj) {
255 IO.mapRequired(Key: "Name", Val&: Obj.StreamName);
256 IO.mapRequired(Key: "StreamNum", Val&: Obj.StreamNumber);
257}
258
259void MappingTraits<PdbModiStream>::mapping(IO &IO, PdbModiStream &Obj) {
260 IO.mapOptional(Key: "Signature", Val&: Obj.Signature, Default: 4U);
261 IO.mapRequired(Key: "Records", Val&: Obj.Symbols);
262}
263
264void MappingTraits<PdbDbiModuleInfo>::mapping(IO &IO, PdbDbiModuleInfo &Obj) {
265 IO.mapRequired(Key: "Module", Val&: Obj.Mod);
266 IO.mapOptional(Key: "ObjFile", Val&: Obj.Obj, Default: Obj.Mod);
267 IO.mapOptional(Key: "SourceFiles", Val&: Obj.SourceFiles);
268 IO.mapOptional(Key: "Subsections", Val&: Obj.Subsections);
269 IO.mapOptional(Key: "Modi", Val&: Obj.Modi);
270}
271
272void MappingTraits<PdbDXContainerStream>::mapping(
273 IO &IO, pdb::yaml::PdbDXContainerStream &Obj) {
274 IO.mapRequired(Key: "DXContainer", Val&: Obj.DXC);
275}
276