1 //===- PDB.cpp ------------------------------------------------------------===// 2 // 3 // The LLVM Linker 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "PDB.h" 11 #include "Error.h" 12 #include "llvm/DebugInfo/MSF/MSFBuilder.h" 13 #include "llvm/DebugInfo/MSF/MSFCommon.h" 14 #include "llvm/DebugInfo/PDB/Raw/DbiStream.h" 15 #include "llvm/DebugInfo/PDB/Raw/DbiStreamBuilder.h" 16 #include "llvm/DebugInfo/PDB/Raw/InfoStream.h" 17 #include "llvm/DebugInfo/PDB/Raw/InfoStreamBuilder.h" 18 #include "llvm/DebugInfo/PDB/Raw/PDBFile.h" 19 #include "llvm/DebugInfo/PDB/Raw/PDBFileBuilder.h" 20 #include "llvm/DebugInfo/PDB/Raw/TpiStream.h" 21 #include "llvm/DebugInfo/PDB/Raw/TpiStreamBuilder.h" 22 #include "llvm/Support/Endian.h" 23 #include "llvm/Support/FileOutputBuffer.h" 24 #include <memory> 25 26 using namespace lld; 27 using namespace llvm; 28 using namespace llvm::support; 29 using namespace llvm::support::endian; 30 31 static ExitOnError ExitOnErr; 32 33 void coff::createPDB(StringRef Path, ArrayRef<uint8_t> SectionTable) { 34 BumpPtrAllocator Alloc; 35 pdb::PDBFileBuilder Builder(Alloc); 36 ExitOnErr(Builder.initialize(4096)); // 4096 is blocksize 37 38 // Create streams in MSF for predefined streams, namely 39 // PDB, TPI, DBI and IPI. 40 for (int I = 0; I < (int)pdb::kSpecialStreamCount; ++I) 41 ExitOnErr(Builder.getMsfBuilder().addStream(0)); 42 43 // Add an Info stream. 44 auto &InfoBuilder = Builder.getInfoBuilder(); 45 InfoBuilder.setAge(1); 46 47 // Should be a random number, 0 for now. 48 InfoBuilder.setGuid({}); 49 50 // Should be the current time, but set 0 for reproducibilty. 51 InfoBuilder.setSignature(0); 52 InfoBuilder.setVersion(pdb::PdbRaw_ImplVer::PdbImplVC70); 53 54 // Add an empty DPI stream. 55 auto &DbiBuilder = Builder.getDbiBuilder(); 56 DbiBuilder.setVersionHeader(pdb::PdbDbiV110); 57 58 // Add an empty TPI stream. 59 auto &TpiBuilder = Builder.getTpiBuilder(); 60 TpiBuilder.setVersionHeader(pdb::PdbTpiV80); 61 62 // Add an empty IPI stream. 63 auto &IpiBuilder = Builder.getIpiBuilder(); 64 IpiBuilder.setVersionHeader(pdb::PdbTpiV80); 65 66 // Add COFF section header stream. 67 ExitOnErr( 68 DbiBuilder.addDbgStream(pdb::DbgHeaderType::SectionHdr, SectionTable)); 69 70 // Write to a file. 71 ExitOnErr(Builder.commit(Path)); 72 } 73