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/CVTypeDumper.h"
18 #include "llvm/DebugInfo/CodeView/SymbolDumper.h"
19 #include "llvm/DebugInfo/CodeView/TypeDatabase.h"
20 #include "llvm/DebugInfo/CodeView/TypeDumpVisitor.h"
21 #include "llvm/DebugInfo/CodeView/TypeStreamMerger.h"
22 #include "llvm/DebugInfo/CodeView/TypeTableBuilder.h"
23 #include "llvm/DebugInfo/MSF/MSFBuilder.h"
24 #include "llvm/DebugInfo/MSF/MSFCommon.h"
25 #include "llvm/DebugInfo/PDB/Native/DbiStream.h"
26 #include "llvm/DebugInfo/PDB/Native/DbiStreamBuilder.h"
27 #include "llvm/DebugInfo/PDB/Native/InfoStream.h"
28 #include "llvm/DebugInfo/PDB/Native/InfoStreamBuilder.h"
29 #include "llvm/DebugInfo/PDB/Native/PDBFile.h"
30 #include "llvm/DebugInfo/PDB/Native/PDBFileBuilder.h"
31 #include "llvm/DebugInfo/PDB/Native/PDBStringTableBuilder.h"
32 #include "llvm/DebugInfo/PDB/Native/PDBTypeServerHandler.h"
33 #include "llvm/DebugInfo/PDB/Native/TpiStream.h"
34 #include "llvm/DebugInfo/PDB/Native/TpiStreamBuilder.h"
35 #include "llvm/Object/COFF.h"
36 #include "llvm/Support/BinaryByteStream.h"
37 #include "llvm/Support/Endian.h"
38 #include "llvm/Support/FileOutputBuffer.h"
39 #include "llvm/Support/Path.h"
40 #include "llvm/Support/ScopedPrinter.h"
44 using namespace lld::coff;
46 using namespace llvm::codeview;
47 using namespace llvm::support;
48 using namespace llvm::support::endian;
50 using llvm::object::coff_section;
52 static ExitOnError ExitOnErr;
54 // Returns a list of all SectionChunks.
55 static std::vector<coff_section> getInputSections(SymbolTable *Symtab) {
56 std::vector<coff_section> V;
57 for (Chunk *C : Symtab->getChunks())
58 if (auto *SC = dyn_cast<SectionChunk>(C))
59 V.push_back(*SC->Header);
63 static SectionChunk *findByName(std::vector<SectionChunk *> &Sections,
65 for (SectionChunk *C : Sections)
66 if (C->getSectionName() == Name)
71 static ArrayRef<uint8_t> getDebugSection(ObjectFile *File, StringRef SecName) {
72 SectionChunk *Sec = findByName(File->getDebugChunks(), SecName);
76 // First 4 bytes are section magic.
77 ArrayRef<uint8_t> Data = Sec->getContents();
79 fatal(SecName + " too short");
80 if (read32le(Data.data()) != COFF::DEBUG_SECTION_MAGIC)
81 fatal(SecName + " has an invalid magic");
85 static void addTypeInfo(pdb::TpiStreamBuilder &TpiBuilder,
86 codeview::TypeTableBuilder &TypeTable) {
87 // Start the TPI or IPI stream header.
88 TpiBuilder.setVersionHeader(pdb::PdbTpiV80);
90 // Flatten the in memory type table.
91 TypeTable.ForEachRecord([&](TypeIndex TI, ArrayRef<uint8_t> Rec) {
93 TpiBuilder.addTypeRecord(Rec, None);
97 // Merge .debug$T sections into IpiData and TpiData.
98 static void mergeDebugT(SymbolTable *Symtab, pdb::PDBFileBuilder &Builder,
99 codeview::TypeTableBuilder &TypeTable,
100 codeview::TypeTableBuilder &IDTable) {
101 // Visit all .debug$T sections to add them to Builder.
102 for (ObjectFile *File : Symtab->ObjectFiles) {
103 ArrayRef<uint8_t> Data = getDebugSection(File, ".debug$T");
107 BinaryByteStream Stream(Data, support::little);
108 codeview::CVTypeArray Types;
109 BinaryStreamReader Reader(Stream);
110 // Follow type servers. If the same type server is encountered more than
111 // once for this instance of `PDBTypeServerHandler` (for example if many
112 // object files reference the same TypeServer), the types from the
113 // TypeServer will only be visited once.
114 pdb::PDBTypeServerHandler Handler;
115 Handler.addSearchPath(llvm::sys::path::parent_path(File->getName()));
116 if (auto EC = Reader.readArray(Types, Reader.getLength()))
117 fatal(EC, "Reader::readArray failed");
119 codeview::mergeTypeStreams(IDTable, TypeTable, &Handler, Types))
120 fatal(Err, "codeview::mergeTypeStreams failed");
123 // Construct TPI stream contents.
124 addTypeInfo(Builder.getTpiBuilder(), TypeTable);
126 // Construct IPI stream contents.
127 addTypeInfo(Builder.getIpiBuilder(), IDTable);
130 static void dumpDebugT(ScopedPrinter &W, ObjectFile *File) {
131 ListScope LS(W, "DebugT");
132 ArrayRef<uint8_t> Data = getDebugSection(File, ".debug$T");
137 TypeDumpVisitor TDV(TDB, &W, false);
138 // Use a default implementation that does not follow type servers and instead
139 // just dumps the contents of the TypeServer2 record.
140 CVTypeDumper TypeDumper(TDB);
141 if (auto EC = TypeDumper.dump(Data, TDV))
142 fatal(EC, "CVTypeDumper::dump failed");
145 static void dumpDebugS(ScopedPrinter &W, ObjectFile *File) {
146 ListScope LS(W, "DebugS");
147 ArrayRef<uint8_t> Data = getDebugSection(File, ".debug$S");
151 BinaryByteStream Stream(Data, llvm::support::little);
152 CVSymbolArray Symbols;
153 BinaryStreamReader Reader(Stream);
154 if (auto EC = Reader.readArray(Symbols, Reader.getLength()))
155 fatal(EC, "StreamReader.readArray<CVSymbolArray> failed");
158 CVSymbolDumper SymbolDumper(W, TDB, nullptr, false);
159 if (auto EC = SymbolDumper.dump(Symbols))
160 fatal(EC, "CVSymbolDumper::dump failed");
163 // Dump CodeView debug info. This is for debugging.
164 static void dumpCodeView(SymbolTable *Symtab) {
165 ScopedPrinter W(outs());
167 for (ObjectFile *File : Symtab->ObjectFiles) {
173 // Creates a PDB file.
174 void coff::createPDB(StringRef Path, SymbolTable *Symtab,
175 ArrayRef<uint8_t> SectionTable,
176 const llvm::codeview::DebugInfo *DI) {
178 dumpCodeView(Symtab);
180 BumpPtrAllocator Alloc;
181 pdb::PDBFileBuilder Builder(Alloc);
182 ExitOnErr(Builder.initialize(4096)); // 4096 is blocksize
184 // Create streams in MSF for predefined streams, namely
185 // PDB, TPI, DBI and IPI.
186 for (int I = 0; I < (int)pdb::kSpecialStreamCount; ++I)
187 ExitOnErr(Builder.getMsfBuilder().addStream(0));
189 // Add an Info stream.
190 auto &InfoBuilder = Builder.getInfoBuilder();
191 InfoBuilder.setAge(DI ? DI->PDB70.Age : 0);
193 pdb::PDB_UniqueId uuid{};
195 memcpy(&uuid, &DI->PDB70.Signature, sizeof(uuid));
196 InfoBuilder.setGuid(uuid);
197 // Should be the current time, but set 0 for reproducibilty.
198 InfoBuilder.setSignature(0);
199 InfoBuilder.setVersion(pdb::PdbRaw_ImplVer::PdbImplVC70);
201 // Add an empty DPI stream.
202 auto &DbiBuilder = Builder.getDbiBuilder();
203 DbiBuilder.setVersionHeader(pdb::PdbDbiV110);
205 codeview::TypeTableBuilder TypeTable(BAlloc);
206 codeview::TypeTableBuilder IDTable(BAlloc);
207 mergeDebugT(Symtab, Builder, TypeTable, IDTable);
209 // Add Section Contributions.
210 std::vector<pdb::SectionContrib> Contribs =
211 pdb::DbiStreamBuilder::createSectionContribs(getInputSections(Symtab));
212 DbiBuilder.setSectionContribs(Contribs);
214 // Add Section Map stream.
215 ArrayRef<object::coff_section> Sections = {
216 (const object::coff_section *)SectionTable.data(),
217 SectionTable.size() / sizeof(object::coff_section)};
218 std::vector<pdb::SecMapEntry> SectionMap =
219 pdb::DbiStreamBuilder::createSectionMap(Sections);
220 DbiBuilder.setSectionMap(SectionMap);
222 ExitOnErr(DbiBuilder.addModuleInfo("* Linker *"));
224 // Add COFF section header stream.
226 DbiBuilder.addDbgStream(pdb::DbgHeaderType::SectionHdr, SectionTable));
229 ExitOnErr(Builder.commit(Path));