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