1 //===- PDB.cpp ------------------------------------------------------------===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
14 #include "SymbolTable.h"
16 #include "llvm/DebugInfo/CodeView/CVDebugRecord.h"
17 #include "llvm/DebugInfo/CodeView/SymbolDumper.h"
18 #include "llvm/DebugInfo/CodeView/TypeDumper.h"
19 #include "llvm/DebugInfo/MSF/ByteStream.h"
20 #include "llvm/DebugInfo/MSF/MSFBuilder.h"
21 #include "llvm/DebugInfo/MSF/MSFCommon.h"
22 #include "llvm/DebugInfo/PDB/Raw/DbiStream.h"
23 #include "llvm/DebugInfo/PDB/Raw/DbiStreamBuilder.h"
24 #include "llvm/DebugInfo/PDB/Raw/InfoStream.h"
25 #include "llvm/DebugInfo/PDB/Raw/InfoStreamBuilder.h"
26 #include "llvm/DebugInfo/PDB/Raw/PDBFile.h"
27 #include "llvm/DebugInfo/PDB/Raw/PDBFileBuilder.h"
28 #include "llvm/DebugInfo/PDB/Raw/TpiStream.h"
29 #include "llvm/DebugInfo/PDB/Raw/TpiStreamBuilder.h"
30 #include "llvm/Object/COFF.h"
31 #include "llvm/Support/Endian.h"
32 #include "llvm/Support/FileOutputBuffer.h"
33 #include "llvm/Support/ScopedPrinter.h"
37 using namespace lld::coff;
39 using namespace llvm::codeview;
40 using namespace llvm::support;
41 using namespace llvm::support::endian;
43 using llvm::object::coff_section;
45 static ExitOnError ExitOnErr;
47 // Returns a list of all SectionChunks.
48 static std::vector<coff_section> getInputSections(SymbolTable *Symtab) {
49 std::vector<coff_section> V;
50 for (Chunk *C : Symtab->getChunks())
51 if (auto *SC = dyn_cast<SectionChunk>(C))
52 V.push_back(*SC->Header);
56 static SectionChunk *findByName(std::vector<SectionChunk *> &Sections,
58 for (SectionChunk *C : Sections)
59 if (C->getSectionName() == Name)
64 static ArrayRef<uint8_t> getDebugT(ObjectFile *File) {
65 SectionChunk *Sec = findByName(File->getDebugChunks(), ".debug$T");
69 // First 4 bytes are section magic.
70 ArrayRef<uint8_t> Data = Sec->getContents();
72 fatal(".debug$T too short");
73 if (read32le(Data.data()) != COFF::DEBUG_SECTION_MAGIC)
74 fatal(".debug$T has an invalid magic");
78 static void dumpDebugT(ScopedPrinter &W, ObjectFile *File) {
79 ArrayRef<uint8_t> Data = getDebugT(File);
83 msf::ByteStream Stream(Data);
84 CVTypeDumper TypeDumper(&W, false);
85 if (auto EC = TypeDumper.dump(Data))
86 fatal(EC, "CVTypeDumper::dump failed");
89 static void dumpDebugS(ScopedPrinter &W, ObjectFile *File) {
90 SectionChunk *Sec = findByName(File->getDebugChunks(), ".debug$S");
94 msf::ByteStream Stream(Sec->getContents());
95 CVSymbolArray Symbols;
96 msf::StreamReader Reader(Stream);
97 if (auto EC = Reader.readArray(Symbols, Reader.getLength()))
98 fatal(EC, "StreamReader.readArray<CVSymbolArray> failed");
100 CVTypeDumper TypeDumper(&W, false);
101 CVSymbolDumper SymbolDumper(W, TypeDumper, nullptr, false);
102 if (auto EC = SymbolDumper.dump(Symbols))
103 fatal(EC, "CVSymbolDumper::dump failed");
106 // Dump CodeView debug info. This is for debugging.
107 static void dumpCodeView(SymbolTable *Symtab) {
108 ScopedPrinter W(outs());
110 for (ObjectFile *File : Symtab->ObjectFiles) {
116 static void addTypeInfo(SymbolTable *Symtab,
117 pdb::TpiStreamBuilder &TpiBuilder) {
118 for (ObjectFile *File : Symtab->ObjectFiles) {
119 ArrayRef<uint8_t> Data = getDebugT(File);
123 msf::ByteStream Stream(Data);
124 codeview::CVTypeArray Records;
125 msf::StreamReader Reader(Stream);
126 if (auto EC = Reader.readArray(Records, Reader.getLength()))
127 fatal(EC, "Reader.readArray failed");
128 for (const codeview::CVType &Rec : Records)
129 TpiBuilder.addTypeRecord(Rec);
133 // Creates a PDB file.
134 void coff::createPDB(StringRef Path, SymbolTable *Symtab,
135 ArrayRef<uint8_t> SectionTable,
136 const llvm::codeview::DebugInfo *DI) {
138 dumpCodeView(Symtab);
140 BumpPtrAllocator Alloc;
141 pdb::PDBFileBuilder Builder(Alloc);
142 ExitOnErr(Builder.initialize(4096)); // 4096 is blocksize
144 // Create streams in MSF for predefined streams, namely
145 // PDB, TPI, DBI and IPI.
146 for (int I = 0; I < (int)pdb::kSpecialStreamCount; ++I)
147 ExitOnErr(Builder.getMsfBuilder().addStream(0));
149 // Add an Info stream.
150 auto &InfoBuilder = Builder.getInfoBuilder();
151 InfoBuilder.setAge(DI->PDB70.Age);
153 *reinterpret_cast<const pdb::PDB_UniqueId *>(&DI->PDB70.Signature));
154 // Should be the current time, but set 0 for reproducibilty.
155 InfoBuilder.setSignature(0);
156 InfoBuilder.setVersion(pdb::PdbRaw_ImplVer::PdbImplVC70);
158 // Add an empty DPI stream.
159 auto &DbiBuilder = Builder.getDbiBuilder();
160 DbiBuilder.setVersionHeader(pdb::PdbDbiV110);
162 // Add an empty TPI stream.
163 auto &TpiBuilder = Builder.getTpiBuilder();
164 TpiBuilder.setVersionHeader(pdb::PdbTpiV80);
165 if (Config->DebugPdb)
166 addTypeInfo(Symtab, TpiBuilder);
168 // Add an empty IPI stream.
169 auto &IpiBuilder = Builder.getIpiBuilder();
170 IpiBuilder.setVersionHeader(pdb::PdbTpiV80);
172 // Add Section Contributions.
173 std::vector<pdb::SectionContrib> Contribs =
174 pdb::DbiStreamBuilder::createSectionContribs(getInputSections(Symtab));
175 DbiBuilder.setSectionContribs(Contribs);
177 // Add Section Map stream.
178 ArrayRef<object::coff_section> Sections = {
179 (const object::coff_section *)SectionTable.data(),
180 SectionTable.size() / sizeof(object::coff_section)};
181 std::vector<pdb::SecMapEntry> SectionMap =
182 pdb::DbiStreamBuilder::createSectionMap(Sections);
183 DbiBuilder.setSectionMap(SectionMap);
185 ExitOnErr(DbiBuilder.addModuleInfo("", "* Linker *"));
187 // Add COFF section header stream.
189 DbiBuilder.addDbgStream(pdb::DbgHeaderType::SectionHdr, SectionTable));
192 ExitOnErr(Builder.commit(Path));