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/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/PDBTypeServerHandler.h" 32 #include "llvm/DebugInfo/PDB/Native/StringTableBuilder.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" 41 #include <memory> 42 43 using namespace lld; 44 using namespace lld::coff; 45 using namespace llvm; 46 using namespace llvm::codeview; 47 using namespace llvm::support; 48 using namespace llvm::support::endian; 49 50 using llvm::object::coff_section; 51 52 static ExitOnError ExitOnErr; 53 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); 60 return V; 61 } 62 63 static SectionChunk *findByName(std::vector<SectionChunk *> &Sections, 64 StringRef Name) { 65 for (SectionChunk *C : Sections) 66 if (C->getSectionName() == Name) 67 return C; 68 return nullptr; 69 } 70 71 static ArrayRef<uint8_t> getDebugSection(ObjectFile *File, StringRef SecName) { 72 SectionChunk *Sec = findByName(File->getDebugChunks(), SecName); 73 if (!Sec) 74 return {}; 75 76 // First 4 bytes are section magic. 77 ArrayRef<uint8_t> Data = Sec->getContents(); 78 if (Data.size() < 4) 79 fatal(SecName + " too short"); 80 if (read32le(Data.data()) != COFF::DEBUG_SECTION_MAGIC) 81 fatal(SecName + " has an invalid magic"); 82 return Data.slice(4); 83 } 84 85 // Merge .debug$T sections and returns it. 86 static std::vector<uint8_t> mergeDebugT(SymbolTable *Symtab) { 87 ScopedPrinter W(outs()); 88 89 // Visit all .debug$T sections to add them to Builder. 90 codeview::TypeTableBuilder Builder(BAlloc); 91 for (ObjectFile *File : Symtab->ObjectFiles) { 92 ArrayRef<uint8_t> Data = getDebugSection(File, ".debug$T"); 93 if (Data.empty()) 94 continue; 95 96 BinaryByteStream Stream(Data, llvm::support::little); 97 codeview::CVTypeArray Types; 98 BinaryStreamReader Reader(Stream); 99 // Follow type servers. If the same type server is encountered more than 100 // once for this instance of `PDBTypeServerHandler` (for example if many 101 // object files reference the same TypeServer), the types from the 102 // TypeServer will only be visited once. 103 pdb::PDBTypeServerHandler Handler; 104 Handler.addSearchPath(llvm::sys::path::parent_path(File->getName())); 105 if (auto EC = Reader.readArray(Types, Reader.getLength())) 106 fatal(EC, "Reader::readArray failed"); 107 if (auto Err = codeview::mergeTypeStreams(Builder, &Handler, Types)) 108 fatal(Err, "codeview::mergeTypeStreams failed"); 109 } 110 111 // Construct section contents. 112 std::vector<uint8_t> V; 113 Builder.ForEachRecord([&](TypeIndex TI, ArrayRef<uint8_t> Rec) { 114 V.insert(V.end(), Rec.begin(), Rec.end()); 115 }); 116 return V; 117 } 118 119 static void dumpDebugT(ScopedPrinter &W, ObjectFile *File) { 120 ListScope LS(W, "DebugT"); 121 ArrayRef<uint8_t> Data = getDebugSection(File, ".debug$T"); 122 if (Data.empty()) 123 return; 124 125 TypeDatabase TDB; 126 TypeDumpVisitor TDV(TDB, &W, false); 127 // Use a default implementation that does not follow type servers and instead 128 // just dumps the contents of the TypeServer2 record. 129 CVTypeDumper TypeDumper(TDB); 130 if (auto EC = TypeDumper.dump(Data, TDV)) 131 fatal(EC, "CVTypeDumper::dump failed"); 132 } 133 134 static void dumpDebugS(ScopedPrinter &W, ObjectFile *File) { 135 ListScope LS(W, "DebugS"); 136 ArrayRef<uint8_t> Data = getDebugSection(File, ".debug$S"); 137 if (Data.empty()) 138 return; 139 140 BinaryByteStream Stream(Data, llvm::support::little); 141 CVSymbolArray Symbols; 142 BinaryStreamReader Reader(Stream); 143 if (auto EC = Reader.readArray(Symbols, Reader.getLength())) 144 fatal(EC, "StreamReader.readArray<CVSymbolArray> failed"); 145 146 TypeDatabase TDB; 147 CVSymbolDumper SymbolDumper(W, TDB, nullptr, false); 148 if (auto EC = SymbolDumper.dump(Symbols)) 149 fatal(EC, "CVSymbolDumper::dump failed"); 150 } 151 152 // Dump CodeView debug info. This is for debugging. 153 static void dumpCodeView(SymbolTable *Symtab) { 154 ScopedPrinter W(outs()); 155 156 for (ObjectFile *File : Symtab->ObjectFiles) { 157 dumpDebugT(W, File); 158 dumpDebugS(W, File); 159 } 160 } 161 162 static void addTypeInfo(pdb::TpiStreamBuilder &TpiBuilder, 163 ArrayRef<uint8_t> Data) { 164 BinaryByteStream Stream(Data, llvm::support::little); 165 codeview::CVTypeArray Records; 166 BinaryStreamReader Reader(Stream); 167 if (auto EC = Reader.readArray(Records, Reader.getLength())) 168 fatal(EC, "Reader.readArray failed"); 169 for (const codeview::CVType &Rec : Records) 170 TpiBuilder.addTypeRecord(Rec); 171 } 172 173 // Creates a PDB file. 174 void coff::createPDB(StringRef Path, SymbolTable *Symtab, 175 ArrayRef<uint8_t> SectionTable, 176 const llvm::codeview::DebugInfo *DI) { 177 if (Config->DumpPdb) 178 dumpCodeView(Symtab); 179 180 BumpPtrAllocator Alloc; 181 pdb::PDBFileBuilder Builder(Alloc); 182 ExitOnErr(Builder.initialize(4096)); // 4096 is blocksize 183 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)); 188 189 // Add an Info stream. 190 auto &InfoBuilder = Builder.getInfoBuilder(); 191 InfoBuilder.setAge(DI ? DI->PDB70.Age : 0); 192 193 pdb::PDB_UniqueId uuid{}; 194 if (DI) 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); 200 201 // Add an empty DPI stream. 202 auto &DbiBuilder = Builder.getDbiBuilder(); 203 DbiBuilder.setVersionHeader(pdb::PdbDbiV110); 204 205 // Add an empty TPI stream. 206 auto &TpiBuilder = Builder.getTpiBuilder(); 207 TpiBuilder.setVersionHeader(pdb::PdbTpiV80); 208 std::vector<uint8_t> TpiData; 209 if (Config->DebugPdb) { 210 TpiData = mergeDebugT(Symtab); 211 addTypeInfo(TpiBuilder, TpiData); 212 } 213 214 // Add an empty IPI stream. 215 auto &IpiBuilder = Builder.getIpiBuilder(); 216 IpiBuilder.setVersionHeader(pdb::PdbTpiV80); 217 218 // Add Section Contributions. 219 std::vector<pdb::SectionContrib> Contribs = 220 pdb::DbiStreamBuilder::createSectionContribs(getInputSections(Symtab)); 221 DbiBuilder.setSectionContribs(Contribs); 222 223 // Add Section Map stream. 224 ArrayRef<object::coff_section> Sections = { 225 (const object::coff_section *)SectionTable.data(), 226 SectionTable.size() / sizeof(object::coff_section)}; 227 std::vector<pdb::SecMapEntry> SectionMap = 228 pdb::DbiStreamBuilder::createSectionMap(Sections); 229 DbiBuilder.setSectionMap(SectionMap); 230 231 ExitOnErr(DbiBuilder.addModuleInfo("", "* Linker *")); 232 233 // Add COFF section header stream. 234 ExitOnErr( 235 DbiBuilder.addDbgStream(pdb::DbgHeaderType::SectionHdr, SectionTable)); 236 237 // Write to a file. 238 ExitOnErr(Builder.commit(Path)); 239 } 240