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