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 "Chunks.h" 12 #include "Config.h" 13 #include "Error.h" 14 #include "SymbolTable.h" 15 #include "Symbols.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" 42 #include <memory> 43 44 using namespace lld; 45 using namespace lld::coff; 46 using namespace llvm; 47 using namespace llvm::codeview; 48 using namespace llvm::support; 49 using namespace llvm::support::endian; 50 51 using llvm::object::coff_section; 52 53 static ExitOnError ExitOnErr; 54 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); 61 return V; 62 } 63 64 static SectionChunk *findByName(std::vector<SectionChunk *> &Sections, 65 StringRef Name) { 66 for (SectionChunk *C : Sections) 67 if (C->getSectionName() == Name) 68 return C; 69 return nullptr; 70 } 71 72 static ArrayRef<uint8_t> getDebugSection(ObjectFile *File, StringRef SecName) { 73 SectionChunk *Sec = findByName(File->getDebugChunks(), SecName); 74 if (!Sec) 75 return {}; 76 77 // First 4 bytes are section magic. 78 ArrayRef<uint8_t> Data = Sec->getContents(); 79 if (Data.size() < 4) 80 fatal(SecName + " too short"); 81 if (read32le(Data.data()) != COFF::DEBUG_SECTION_MAGIC) 82 fatal(SecName + " has an invalid magic"); 83 return Data.slice(4); 84 } 85 86 static void addTypeInfo(pdb::TpiStreamBuilder &TpiBuilder, 87 codeview::TypeTableBuilder &TypeTable) { 88 // Start the TPI or IPI stream header. 89 TpiBuilder.setVersionHeader(pdb::PdbTpiV80); 90 91 // Flatten the in memory type table. 92 TypeTable.ForEachRecord([&](TypeIndex TI, ArrayRef<uint8_t> Rec) { 93 // FIXME: Hash types. 94 TpiBuilder.addTypeRecord(Rec, None); 95 }); 96 } 97 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; 107 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"); 111 if (Data.empty()) 112 continue; 113 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"); 124 } 125 126 // Construct TPI stream contents. 127 addTypeInfo(Builder.getTpiBuilder(), TypeTable); 128 129 // Construct IPI stream contents. 130 addTypeInfo(Builder.getIpiBuilder(), IDTable); 131 } 132 133 static void dumpDebugT(ScopedPrinter &W, ObjectFile *File) { 134 ListScope LS(W, "DebugT"); 135 ArrayRef<uint8_t> Data = getDebugSection(File, ".debug$T"); 136 if (Data.empty()) 137 return; 138 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"); 145 } 146 147 static void dumpDebugS(ScopedPrinter &W, ObjectFile *File) { 148 ListScope LS(W, "DebugS"); 149 ArrayRef<uint8_t> Data = getDebugSection(File, ".debug$S"); 150 if (Data.empty()) 151 return; 152 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"); 158 159 TypeDatabase TDB(0); 160 CVSymbolDumper SymbolDumper(W, TDB, nullptr, false); 161 if (auto EC = SymbolDumper.dump(Symbols)) 162 fatal(EC, "CVSymbolDumper::dump failed"); 163 } 164 165 // Dump CodeView debug info. This is for debugging. 166 static void dumpCodeView(SymbolTable *Symtab) { 167 ScopedPrinter W(outs()); 168 169 for (ObjectFile *File : Symtab->ObjectFiles) { 170 dumpDebugT(W, File); 171 dumpDebugS(W, File); 172 } 173 } 174 175 // Creates a PDB file. 176 void coff::createPDB(StringRef Path, SymbolTable *Symtab, 177 ArrayRef<uint8_t> SectionTable, 178 const llvm::codeview::DebugInfo *DI) { 179 if (Config->DumpPdb) 180 dumpCodeView(Symtab); 181 182 BumpPtrAllocator Alloc; 183 pdb::PDBFileBuilder Builder(Alloc); 184 ExitOnErr(Builder.initialize(4096)); // 4096 is blocksize 185 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)); 190 191 // Add an Info stream. 192 auto &InfoBuilder = Builder.getInfoBuilder(); 193 InfoBuilder.setAge(DI ? DI->PDB70.Age : 0); 194 195 pdb::PDB_UniqueId uuid{}; 196 if (DI) 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); 202 203 // Add an empty DPI stream. 204 auto &DbiBuilder = Builder.getDbiBuilder(); 205 DbiBuilder.setVersionHeader(pdb::PdbDbiV110); 206 207 codeview::TypeTableBuilder TypeTable(BAlloc); 208 codeview::TypeTableBuilder IDTable(BAlloc); 209 mergeDebugT(Symtab, Builder, TypeTable, IDTable); 210 211 // Add Section Contributions. 212 std::vector<pdb::SectionContrib> Contribs = 213 pdb::DbiStreamBuilder::createSectionContribs(getInputSections(Symtab)); 214 DbiBuilder.setSectionContribs(Contribs); 215 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); 223 224 ExitOnErr(DbiBuilder.addModuleInfo("* Linker *")); 225 226 // Add COFF section header stream. 227 ExitOnErr( 228 DbiBuilder.addDbgStream(pdb::DbgHeaderType::SectionHdr, SectionTable)); 229 230 // Write to a file. 231 ExitOnErr(Builder.commit(Path)); 232 } 233