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