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, nullptr, false);
161 if (auto EC = SymbolDumper.dump(Symbols))
162 fatal(EC, "CVSymbolDumper::dump failed");
165 // Dump CodeView debug info. This is for debugging.
166 static void dumpCodeView(SymbolTable *Symtab) {
167 ScopedPrinter W(outs());
169 for (ObjectFile *File : Symtab->ObjectFiles) {
175 // Creates a PDB file.
176 void coff::createPDB(StringRef Path, SymbolTable *Symtab,
177 ArrayRef<uint8_t> SectionTable,
178 const llvm::codeview::DebugInfo *DI) {
180 dumpCodeView(Symtab);
182 BumpPtrAllocator Alloc;
183 pdb::PDBFileBuilder Builder(Alloc);
184 ExitOnErr(Builder.initialize(4096)); // 4096 is blocksize
186 // Create streams in MSF for predefined streams, namely
187 // PDB, TPI, DBI and IPI.
188 for (int I = 0; I < (int)pdb::kSpecialStreamCount; ++I)
189 ExitOnErr(Builder.getMsfBuilder().addStream(0));
191 // Add an Info stream.
192 auto &InfoBuilder = Builder.getInfoBuilder();
193 InfoBuilder.setAge(DI ? DI->PDB70.Age : 0);
195 pdb::PDB_UniqueId uuid{};
197 memcpy(&uuid, &DI->PDB70.Signature, sizeof(uuid));
198 InfoBuilder.setGuid(uuid);
199 // Should be the current time, but set 0 for reproducibilty.
200 InfoBuilder.setSignature(0);
201 InfoBuilder.setVersion(pdb::PdbRaw_ImplVer::PdbImplVC70);
203 // Add an empty DPI stream.
204 auto &DbiBuilder = Builder.getDbiBuilder();
205 DbiBuilder.setVersionHeader(pdb::PdbDbiV110);
207 codeview::TypeTableBuilder TypeTable(BAlloc);
208 codeview::TypeTableBuilder IDTable(BAlloc);
209 mergeDebugT(Symtab, Builder, TypeTable, IDTable);
211 // Add Section Contributions.
212 std::vector<pdb::SectionContrib> Contribs =
213 pdb::DbiStreamBuilder::createSectionContribs(getInputSections(Symtab));
214 DbiBuilder.setSectionContribs(Contribs);
216 // Add Section Map stream.
217 ArrayRef<object::coff_section> Sections = {
218 (const object::coff_section *)SectionTable.data(),
219 SectionTable.size() / sizeof(object::coff_section)};
220 std::vector<pdb::SecMapEntry> SectionMap =
221 pdb::DbiStreamBuilder::createSectionMap(Sections);
222 DbiBuilder.setSectionMap(SectionMap);
224 ExitOnErr(DbiBuilder.addModuleInfo("* Linker *"));
226 // Add COFF section header stream.
228 DbiBuilder.addDbgStream(pdb::DbgHeaderType::SectionHdr, SectionTable));
231 ExitOnErr(Builder.commit(Path));