1//===- YAMLOutputStyle.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 "YAMLOutputStyle.h"
10
11#include "PdbYaml.h"
12#include "llvm-pdbutil.h"
13
14#include "llvm/BinaryFormat/COFF.h"
15#include "llvm/DebugInfo/CodeView/DebugChecksumsSubsection.h"
16#include "llvm/DebugInfo/CodeView/StringsAndChecksums.h"
17#include "llvm/DebugInfo/MSF/MappedBlockStream.h"
18#include "llvm/DebugInfo/PDB/Native/DbiStream.h"
19#include "llvm/DebugInfo/PDB/Native/GlobalsStream.h"
20#include "llvm/DebugInfo/PDB/Native/ISectionContribVisitor.h"
21#include "llvm/DebugInfo/PDB/Native/InfoStream.h"
22#include "llvm/DebugInfo/PDB/Native/ModuleDebugStream.h"
23#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
24#include "llvm/DebugInfo/PDB/Native/PublicsStream.h"
25#include "llvm/DebugInfo/PDB/Native/RawConstants.h"
26#include "llvm/DebugInfo/PDB/Native/SymbolStream.h"
27#include "llvm/DebugInfo/PDB/Native/TpiStream.h"
28
29using namespace llvm;
30using namespace llvm::codeview;
31using namespace llvm::pdb;
32
33static bool checkModuleSubsection(opts::ModuleSubsection MS) {
34 return any_of(Range&: opts::pdb2yaml::DumpModuleSubsections,
35 P: [=](opts::ModuleSubsection M) {
36 return M == MS || M == opts::ModuleSubsection::All;
37 });
38}
39
40YAMLOutputStyle::YAMLOutputStyle(PDBFile &File)
41 : File(File), Out(outs()), Obj(File.getAllocator()) {
42 Out.setWriteDefaultValues(!opts::pdb2yaml::Minimal);
43}
44
45Error YAMLOutputStyle::dump() {
46 if (opts::pdb2yaml::StreamDirectory)
47 opts::pdb2yaml::StreamMetadata = true;
48
49 if (auto EC = dumpFileHeaders())
50 return EC;
51
52 if (auto EC = dumpStreamMetadata())
53 return EC;
54
55 if (auto EC = dumpStreamDirectory())
56 return EC;
57
58 if (auto EC = dumpStringTable())
59 return EC;
60
61 if (auto EC = dumpPDBStream())
62 return EC;
63
64 if (auto EC = dumpDbiStream())
65 return EC;
66
67 if (auto EC = dumpTpiStream())
68 return EC;
69
70 if (auto EC = dumpIpiStream())
71 return EC;
72
73 if (auto EC = dumpPublics())
74 return EC;
75
76 if (auto EC = dumpDXContainer())
77 return EC;
78
79 // Fake Coff header for dumping register enumerations.
80 COFF::header Header;
81 auto MachineType =
82 Obj.DbiStream ? Obj.DbiStream->MachineType : PDB_Machine::Unknown;
83 Header.Machine = static_cast<uint16_t>(MachineType);
84 Out.setContext(&Header);
85 flush();
86 Out.setContext(nullptr);
87
88 return Error::success();
89}
90
91
92Error YAMLOutputStyle::dumpFileHeaders() {
93 if (opts::pdb2yaml::NoFileHeaders)
94 return Error::success();
95
96 Obj.Headers.emplace();
97 Obj.Headers->SuperBlock.NumBlocks = File.getBlockCount();
98 Obj.Headers->SuperBlock.BlockMapAddr = File.getBlockMapIndex();
99 Obj.Headers->SuperBlock.BlockSize = File.getBlockSize();
100 auto Blocks = File.getDirectoryBlockArray();
101 Obj.Headers->DirectoryBlocks.assign(first: Blocks.begin(), last: Blocks.end());
102 Obj.Headers->NumDirectoryBlocks = File.getNumDirectoryBlocks();
103 Obj.Headers->SuperBlock.NumDirectoryBytes = File.getNumDirectoryBytes();
104 Obj.Headers->NumStreams =
105 opts::pdb2yaml::StreamMetadata ? File.getNumStreams() : 0;
106 Obj.Headers->SuperBlock.FreeBlockMapBlock = File.getFreeBlockMapBlock();
107 Obj.Headers->SuperBlock.Unknown1 = File.getUnknown1();
108 Obj.Headers->FileSize = File.getFileSize();
109
110 return Error::success();
111}
112
113Error YAMLOutputStyle::dumpStringTable() {
114 bool RequiresStringTable = opts::pdb2yaml::DumpModuleFiles ||
115 !opts::pdb2yaml::DumpModuleSubsections.empty();
116 bool RequestedStringTable = opts::pdb2yaml::StringTable;
117 if (!RequiresStringTable && !RequestedStringTable)
118 return Error::success();
119
120 if (!File.hasPDBStringTable())
121 return Error::success();
122
123 auto ExpectedST = File.getStringTable();
124 if (!ExpectedST)
125 return ExpectedST.takeError();
126
127 Obj.StringTable.emplace();
128 const auto &ST = ExpectedST.get();
129 for (auto ID : ST.name_ids()) {
130 auto S = ST.getStringForID(ID);
131 if (!S)
132 return S.takeError();
133 if (S->empty())
134 continue;
135 Obj.StringTable->push_back(x: *S);
136 }
137 return Error::success();
138}
139
140Error YAMLOutputStyle::dumpStreamMetadata() {
141 if (!opts::pdb2yaml::StreamMetadata)
142 return Error::success();
143
144 Obj.StreamSizes.emplace();
145 Obj.StreamSizes->assign(first: File.getStreamSizes().begin(),
146 last: File.getStreamSizes().end());
147 return Error::success();
148}
149
150Error YAMLOutputStyle::dumpStreamDirectory() {
151 if (!opts::pdb2yaml::StreamDirectory)
152 return Error::success();
153
154 auto StreamMap = File.getStreamMap();
155 Obj.StreamMap.emplace();
156 for (auto &Stream : StreamMap) {
157 pdb::yaml::StreamBlockList BlockList;
158 BlockList.Blocks.assign(first: Stream.begin(), last: Stream.end());
159 Obj.StreamMap->push_back(x: BlockList);
160 }
161
162 return Error::success();
163}
164
165Error YAMLOutputStyle::dumpPDBStream() {
166 if (!opts::pdb2yaml::PdbStream)
167 return Error::success();
168
169 auto IS = File.getPDBInfoStream();
170 if (!IS)
171 return IS.takeError();
172
173 auto &InfoS = IS.get();
174 Obj.PdbStream.emplace();
175 Obj.PdbStream->Age = InfoS.getAge();
176 Obj.PdbStream->Guid = InfoS.getGuid();
177 Obj.PdbStream->Signature = InfoS.getSignature();
178 Obj.PdbStream->Version = InfoS.getVersion();
179 Obj.PdbStream->Features = InfoS.getFeatureSignatures();
180
181 return Error::success();
182}
183
184static opts::ModuleSubsection convertSubsectionKind(DebugSubsectionKind K) {
185 switch (K) {
186 case DebugSubsectionKind::CrossScopeExports:
187 return opts::ModuleSubsection::CrossScopeExports;
188 case DebugSubsectionKind::CrossScopeImports:
189 return opts::ModuleSubsection::CrossScopeImports;
190 case DebugSubsectionKind::FileChecksums:
191 return opts::ModuleSubsection::FileChecksums;
192 case DebugSubsectionKind::InlineeLines:
193 return opts::ModuleSubsection::InlineeLines;
194 case DebugSubsectionKind::Lines:
195 return opts::ModuleSubsection::Lines;
196 case DebugSubsectionKind::Symbols:
197 return opts::ModuleSubsection::Symbols;
198 case DebugSubsectionKind::StringTable:
199 return opts::ModuleSubsection::StringTable;
200 case DebugSubsectionKind::FrameData:
201 return opts::ModuleSubsection::FrameData;
202 default:
203 return opts::ModuleSubsection::Unknown;
204 }
205 llvm_unreachable("Unreachable!");
206}
207
208Error YAMLOutputStyle::dumpDbiStream() {
209 if (!opts::pdb2yaml::DbiStream)
210 return Error::success();
211
212 if (!File.hasPDBDbiStream())
213 return Error::success();
214
215 auto DbiS = File.getPDBDbiStream();
216 if (!DbiS)
217 return DbiS.takeError();
218
219 auto &DS = DbiS.get();
220 Obj.DbiStream.emplace();
221 Obj.DbiStream->Age = DS.getAge();
222 Obj.DbiStream->BuildNumber = DS.getBuildNumber();
223 Obj.DbiStream->Flags = DS.getFlags();
224 Obj.DbiStream->MachineType = DS.getMachineType();
225 Obj.DbiStream->PdbDllRbld = DS.getPdbDllRbld();
226 Obj.DbiStream->PdbDllVersion = DS.getPdbDllVersion();
227 Obj.DbiStream->VerHeader = DS.getDbiVersion();
228 if (opts::pdb2yaml::DumpModules) {
229 const auto &Modules = DS.modules();
230 for (uint32_t I = 0; I < Modules.getModuleCount(); ++I) {
231 DbiModuleDescriptor MI = Modules.getModuleDescriptor(Modi: I);
232
233 Obj.DbiStream->ModInfos.emplace_back();
234 yaml::PdbDbiModuleInfo &DMI = Obj.DbiStream->ModInfos.back();
235
236 DMI.Mod = MI.getModuleName();
237 DMI.Obj = MI.getObjFileName();
238 if (opts::pdb2yaml::DumpModuleFiles) {
239 auto Files = Modules.source_files(Modi: I);
240 DMI.SourceFiles.assign(first: Files.begin(), last: Files.end());
241 }
242
243 uint16_t ModiStream = MI.getModuleStreamIndex();
244 if (ModiStream == kInvalidStreamIndex)
245 continue;
246
247 auto ModStreamData = File.createIndexedStream(SN: ModiStream);
248 pdb::ModuleDebugStreamRef ModS(MI, std::move(ModStreamData));
249 if (auto EC = ModS.reload())
250 return EC;
251
252 auto ExpectedST = File.getStringTable();
253 if (!ExpectedST)
254 return ExpectedST.takeError();
255 if (!opts::pdb2yaml::DumpModuleSubsections.empty() &&
256 ModS.hasDebugSubsections()) {
257 auto ExpectedChecksums = ModS.findChecksumsSubsection();
258 if (!ExpectedChecksums)
259 return ExpectedChecksums.takeError();
260
261 StringsAndChecksumsRef SC(ExpectedST->getStringTable(),
262 *ExpectedChecksums);
263
264 for (const auto &SS : ModS.subsections()) {
265 opts::ModuleSubsection OptionKind = convertSubsectionKind(K: SS.kind());
266 if (!checkModuleSubsection(MS: OptionKind))
267 continue;
268
269 auto Converted =
270 CodeViewYAML::YAMLDebugSubsection::fromCodeViewSubection(SC, SS);
271 if (!Converted)
272 return Converted.takeError();
273 DMI.Subsections.push_back(x: *Converted);
274 }
275 }
276
277 if (opts::pdb2yaml::DumpModuleSyms) {
278 DMI.Modi.emplace();
279
280 DMI.Modi->Signature = ModS.signature();
281 bool HadError = false;
282 for (auto &Sym : ModS.symbols(HadError: &HadError)) {
283 auto ES = CodeViewYAML::SymbolRecord::fromCodeViewSymbol(Symbol: Sym);
284 if (!ES)
285 return ES.takeError();
286
287 DMI.Modi->Symbols.push_back(x: *ES);
288 }
289 }
290 }
291 }
292
293 if (opts::pdb2yaml::DumpSectionHeaders) {
294 for (const auto &Section : DS.getSectionHeaders()) {
295 yaml::CoffSectionHeader Hdr;
296 Hdr.Name = Section.Name;
297 Hdr.VirtualSize = Section.VirtualSize;
298 Hdr.VirtualAddress = Section.VirtualAddress;
299 Hdr.SizeOfRawData = Section.SizeOfRawData;
300 Hdr.PointerToRawData = Section.PointerToRawData;
301 Hdr.PointerToRelocations = Section.PointerToRelocations;
302 Hdr.PointerToLinenumbers = Section.PointerToLinenumbers;
303 Hdr.NumberOfRelocations = Section.NumberOfRelocations;
304 Hdr.NumberOfLinenumbers = Section.NumberOfLinenumbers;
305 Hdr.Characteristics = Section.Characteristics;
306 Obj.DbiStream->SectionHeaders.emplace_back(args&: Hdr);
307 }
308 }
309
310 if (opts::pdb2yaml::DumpSectionContribs) {
311 struct Visitor : public ISectionContribVisitor {
312 Visitor(std::vector<yaml::PdbDbiSectionContrib> &Out) : Out(Out) {}
313
314 static yaml::PdbDbiSectionContrib createFromSC(const SectionContrib &C) {
315 yaml::PdbDbiSectionContrib SC;
316 SC.ISect = C.ISect;
317 SC.Off = C.Off;
318 SC.Size = C.Size;
319 SC.Characteristics = C.Characteristics;
320 SC.Imod = C.Imod;
321 SC.DataCrc = C.DataCrc;
322 SC.RelocCrc = C.RelocCrc;
323 return SC;
324 }
325
326 void visit(const SectionContrib &C) override {
327 Out.emplace_back(args: createFromSC(C));
328 }
329 void visit(const SectionContrib2 &C) override {
330 yaml::PdbDbiSectionContrib SC = createFromSC(C: C.Base);
331 SC.ISectCoff = C.ISectCoff;
332 Out.emplace_back(args&: SC);
333 }
334
335 std::vector<yaml::PdbDbiSectionContrib> &Out;
336 };
337 Obj.DbiStream->SectionContribs.emplace(args: yaml::PdbDbiSectionContribs{
338 /*Version=*/DS.getSectionContributionsVersion(),
339 /*Items=*/{},
340 });
341 Visitor Vis(Obj.DbiStream->SectionContribs->Items);
342 DS.visitSectionContributions(Visitor&: Vis);
343 }
344
345 return Error::success();
346}
347
348Error YAMLOutputStyle::dumpTpiStream() {
349 if (!opts::pdb2yaml::TpiStream)
350 return Error::success();
351
352 if (!File.hasPDBTpiStream())
353 return Error::success();
354
355 auto TpiS = File.getPDBTpiStream();
356 if (!TpiS)
357 return TpiS.takeError();
358
359 auto &TS = TpiS.get();
360 Obj.TpiStream.emplace();
361 Obj.TpiStream->Version = TS.getTpiVersion();
362 for (auto &Record : TS.types(HadError: nullptr)) {
363 auto ExpectedRecord = CodeViewYAML::LeafRecord::fromCodeViewRecord(Type: Record);
364 if (!ExpectedRecord)
365 return ExpectedRecord.takeError();
366 Obj.TpiStream->Records.push_back(x: *ExpectedRecord);
367 }
368
369 return Error::success();
370}
371
372Error YAMLOutputStyle::dumpIpiStream() {
373 if (!opts::pdb2yaml::IpiStream)
374 return Error::success();
375
376 if (!File.hasPDBIpiStream())
377 return Error::success();
378
379 auto InfoS = File.getPDBInfoStream();
380 if (!InfoS)
381 return InfoS.takeError();
382 if (!InfoS->containsIdStream())
383 return Error::success();
384
385 auto IpiS = File.getPDBIpiStream();
386 if (!IpiS)
387 return IpiS.takeError();
388
389 auto &IS = IpiS.get();
390 Obj.IpiStream.emplace();
391 Obj.IpiStream->Version = IS.getTpiVersion();
392 for (auto &Record : IS.types(HadError: nullptr)) {
393 auto ExpectedRecord = CodeViewYAML::LeafRecord::fromCodeViewRecord(Type: Record);
394 if (!ExpectedRecord)
395 return ExpectedRecord.takeError();
396
397 Obj.IpiStream->Records.push_back(x: *ExpectedRecord);
398 }
399
400 return Error::success();
401}
402
403Error YAMLOutputStyle::dumpPublics() {
404 if (!opts::pdb2yaml::PublicsStream)
405 return Error::success();
406
407 Obj.PublicsStream.emplace();
408 auto ExpectedPublics = File.getPDBPublicsStream();
409 if (!ExpectedPublics) {
410 llvm::consumeError(Err: ExpectedPublics.takeError());
411 return Error::success();
412 }
413
414 PublicsStream &Publics = *ExpectedPublics;
415 const GSIHashTable &PublicsTable = Publics.getPublicsTable();
416
417 auto ExpectedSyms = File.getPDBSymbolStream();
418 if (!ExpectedSyms) {
419 llvm::consumeError(Err: ExpectedSyms.takeError());
420 return Error::success();
421 }
422
423 BinaryStreamRef SymStream =
424 ExpectedSyms->getSymbolArray().getUnderlyingStream();
425 for (uint32_t PubSymOff : PublicsTable) {
426 Expected<CVSymbol> Sym = readSymbolFromStream(Stream: SymStream, Offset: PubSymOff);
427 if (!Sym)
428 return Sym.takeError();
429 auto ES = CodeViewYAML::SymbolRecord::fromCodeViewSymbol(Symbol: *Sym);
430 if (!ES)
431 return ES.takeError();
432
433 Obj.PublicsStream->PubSyms.push_back(x: *ES);
434 }
435
436 return Error::success();
437}
438
439Error YAMLOutputStyle::dumpDXContainer() {
440 if (!opts::pdb2yaml::DXContainerStream)
441 return Error::success();
442
443 auto DxcS = File.getDXContainerStream();
444 if (!DxcS) {
445 // Not finding a DXContainer is not an error.
446 consumeError(Err: DxcS.takeError());
447 return Error::success();
448 }
449
450 auto DXCYaml = DXContainerYAML::fromDXContainer(DXC&: *DxcS);
451 if (!DXCYaml)
452 return DXCYaml.takeError();
453 Obj.DXContainerStream.emplace();
454 Obj.DXContainerStream->DXC = *DXCYaml->get();
455 return Error::success();
456}
457
458void YAMLOutputStyle::flush() {
459 Out << Obj;
460 outs().flush();
461}
462