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