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/CVTypeVisitor.h"
18 #include "llvm/DebugInfo/CodeView/LazyRandomTypeCollection.h"
19 #include "llvm/DebugInfo/CodeView/SymbolDumper.h"
20 #include "llvm/DebugInfo/CodeView/TypeDatabase.h"
21 #include "llvm/DebugInfo/CodeView/TypeDumpVisitor.h"
22 #include "llvm/DebugInfo/CodeView/TypeStreamMerger.h"
23 #include "llvm/DebugInfo/CodeView/TypeTableBuilder.h"
24 #include "llvm/DebugInfo/MSF/MSFBuilder.h"
25 #include "llvm/DebugInfo/MSF/MSFCommon.h"
26 #include "llvm/DebugInfo/PDB/Native/DbiStream.h"
27 #include "llvm/DebugInfo/PDB/Native/DbiStreamBuilder.h"
28 #include "llvm/DebugInfo/PDB/Native/InfoStream.h"
29 #include "llvm/DebugInfo/PDB/Native/InfoStreamBuilder.h"
30 #include "llvm/DebugInfo/PDB/Native/PDBFile.h"
31 #include "llvm/DebugInfo/PDB/Native/PDBFileBuilder.h"
32 #include "llvm/DebugInfo/PDB/Native/PDBStringTableBuilder.h"
33 #include "llvm/DebugInfo/PDB/Native/PDBTypeServerHandler.h"
34 #include "llvm/DebugInfo/PDB/Native/TpiStream.h"
35 #include "llvm/DebugInfo/PDB/Native/TpiStreamBuilder.h"
36 #include "llvm/Object/COFF.h"
37 #include "llvm/Support/BinaryByteStream.h"
38 #include "llvm/Support/Endian.h"
39 #include "llvm/Support/FileOutputBuffer.h"
40 #include "llvm/Support/Path.h"
41 #include "llvm/Support/ScopedPrinter.h"
45 using namespace lld::coff;
47 using namespace llvm::codeview;
48 using namespace llvm::support;
49 using namespace llvm::support::endian;
51 using llvm::object::coff_section;
53 static ExitOnError ExitOnErr;
55 // Returns a list of all SectionChunks.
56 static std::vector<coff_section> getInputSections(SymbolTable *Symtab) {
57 std::vector<coff_section> V;
58 for (Chunk *C : Symtab->getChunks())
59 if (auto *SC = dyn_cast<SectionChunk>(C))
60 V.push_back(*SC->Header);
64 static SectionChunk *findByName(std::vector<SectionChunk *> &Sections,
66 for (SectionChunk *C : Sections)
67 if (C->getSectionName() == Name)
72 static ArrayRef<uint8_t> getDebugSection(ObjectFile *File, StringRef SecName) {
73 SectionChunk *Sec = findByName(File->getDebugChunks(), SecName);
77 // First 4 bytes are section magic.
78 ArrayRef<uint8_t> Data = Sec->getContents();
80 fatal(SecName + " too short");
81 if (read32le(Data.data()) != COFF::DEBUG_SECTION_MAGIC)
82 fatal(SecName + " has an invalid magic");
86 static void addTypeInfo(pdb::TpiStreamBuilder &TpiBuilder,
87 codeview::TypeTableBuilder &TypeTable) {
88 // Start the TPI or IPI stream header.
89 TpiBuilder.setVersionHeader(pdb::PdbTpiV80);
91 // Flatten the in memory type table.
92 TypeTable.ForEachRecord([&](TypeIndex TI, ArrayRef<uint8_t> Rec) {
94 TpiBuilder.addTypeRecord(Rec, None);
98 // Merge .debug$T sections into IpiData and TpiData.
99 static void mergeDebugT(SymbolTable *Symtab, pdb::PDBFileBuilder &Builder,
100 codeview::TypeTableBuilder &TypeTable,
101 codeview::TypeTableBuilder &IDTable) {
102 // Follow type servers. If the same type server is encountered more than
103 // once for this instance of `PDBTypeServerHandler` (for example if many
104 // object files reference the same TypeServer), the types from the
105 // TypeServer will only be visited once.
106 pdb::PDBTypeServerHandler Handler;
108 // Visit all .debug$T sections to add them to Builder.
109 for (ObjectFile *File : Symtab->ObjectFiles) {
110 ArrayRef<uint8_t> Data = getDebugSection(File, ".debug$T");
114 BinaryByteStream Stream(Data, support::little);
115 codeview::CVTypeArray Types;
116 BinaryStreamReader Reader(Stream);
117 SmallVector<TypeIndex, 128> SourceToDest;
118 Handler.addSearchPath(llvm::sys::path::parent_path(File->getName()));
119 if (auto EC = Reader.readArray(Types, Reader.getLength()))
120 fatal(EC, "Reader::readArray failed");
121 if (auto Err = codeview::mergeTypeAndIdRecords(
122 IDTable, TypeTable, SourceToDest, &Handler, Types))
123 fatal(Err, "codeview::mergeTypeStreams failed");
126 // Construct TPI stream contents.
127 addTypeInfo(Builder.getTpiBuilder(), TypeTable);
129 // Construct IPI stream contents.
130 addTypeInfo(Builder.getIpiBuilder(), IDTable);
133 static void dumpDebugT(ScopedPrinter &W, ObjectFile *File) {
134 ListScope LS(W, "DebugT");
135 ArrayRef<uint8_t> Data = getDebugSection(File, ".debug$T");
139 LazyRandomTypeCollection Types(Data, 100);
140 TypeDumpVisitor TDV(Types, &W, false);
141 // Use a default implementation that does not follow type servers and instead
142 // just dumps the contents of the TypeServer2 record.
143 if (auto EC = codeview::visitTypeStream(Types, TDV))
144 fatal(EC, "CVTypeDumper::dump failed");
147 static void dumpDebugS(ScopedPrinter &W, ObjectFile *File) {
148 ListScope LS(W, "DebugS");
149 ArrayRef<uint8_t> Data = getDebugSection(File, ".debug$S");
153 BinaryByteStream Stream(Data, llvm::support::little);
154 CVSymbolArray Symbols;
155 BinaryStreamReader Reader(Stream);
156 if (auto EC = Reader.readArray(Symbols, Reader.getLength()))
157 fatal(EC, "StreamReader.readArray<CVSymbolArray> failed");
160 CVSymbolDumper SymbolDumper(W, TDB, CodeViewContainer::ObjectFile, nullptr,
162 if (auto EC = SymbolDumper.dump(Symbols))
163 fatal(EC, "CVSymbolDumper::dump failed");
166 // Dump CodeView debug info. This is for debugging.
167 static void dumpCodeView(SymbolTable *Symtab) {
168 ScopedPrinter W(outs());
170 for (ObjectFile *File : Symtab->ObjectFiles) {
176 // Creates a PDB file.
177 void coff::createPDB(StringRef Path, SymbolTable *Symtab,
178 ArrayRef<uint8_t> SectionTable,
179 const llvm::codeview::DebugInfo *DI) {
181 dumpCodeView(Symtab);
183 BumpPtrAllocator Alloc;
184 pdb::PDBFileBuilder Builder(Alloc);
185 ExitOnErr(Builder.initialize(4096)); // 4096 is blocksize
187 // Create streams in MSF for predefined streams, namely
188 // PDB, TPI, DBI and IPI.
189 for (int I = 0; I < (int)pdb::kSpecialStreamCount; ++I)
190 ExitOnErr(Builder.getMsfBuilder().addStream(0));
192 // Add an Info stream.
193 auto &InfoBuilder = Builder.getInfoBuilder();
194 InfoBuilder.setAge(DI ? DI->PDB70.Age : 0);
196 pdb::PDB_UniqueId uuid{};
198 memcpy(&uuid, &DI->PDB70.Signature, sizeof(uuid));
199 InfoBuilder.setGuid(uuid);
200 // Should be the current time, but set 0 for reproducibilty.
201 InfoBuilder.setSignature(0);
202 InfoBuilder.setVersion(pdb::PdbRaw_ImplVer::PdbImplVC70);
204 // Add an empty DPI stream.
205 auto &DbiBuilder = Builder.getDbiBuilder();
206 DbiBuilder.setVersionHeader(pdb::PdbDbiV110);
208 codeview::TypeTableBuilder TypeTable(BAlloc);
209 codeview::TypeTableBuilder IDTable(BAlloc);
210 mergeDebugT(Symtab, Builder, TypeTable, IDTable);
212 // Add Section Contributions.
213 std::vector<pdb::SectionContrib> Contribs =
214 pdb::DbiStreamBuilder::createSectionContribs(getInputSections(Symtab));
215 DbiBuilder.setSectionContribs(Contribs);
217 // Add Section Map stream.
218 ArrayRef<object::coff_section> Sections = {
219 (const object::coff_section *)SectionTable.data(),
220 SectionTable.size() / sizeof(object::coff_section)};
221 std::vector<pdb::SecMapEntry> SectionMap =
222 pdb::DbiStreamBuilder::createSectionMap(Sections);
223 DbiBuilder.setSectionMap(SectionMap);
225 ExitOnErr(DbiBuilder.addModuleInfo("* Linker *"));
227 // Add COFF section header stream.
229 DbiBuilder.addDbgStream(pdb::DbgHeaderType::SectionHdr, SectionTable));
232 ExitOnErr(Builder.commit(Path));