xref: /llvm-project-15.0.7/lld/COFF/PDB.cpp (revision c255fa5e)
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/SymbolDumper.h"
17 #include "llvm/DebugInfo/CodeView/TypeDumper.h"
18 #include "llvm/DebugInfo/MSF/ByteStream.h"
19 #include "llvm/DebugInfo/MSF/MSFBuilder.h"
20 #include "llvm/DebugInfo/MSF/MSFCommon.h"
21 #include "llvm/DebugInfo/PDB/Raw/DbiStream.h"
22 #include "llvm/DebugInfo/PDB/Raw/DbiStreamBuilder.h"
23 #include "llvm/DebugInfo/PDB/Raw/InfoStream.h"
24 #include "llvm/DebugInfo/PDB/Raw/InfoStreamBuilder.h"
25 #include "llvm/DebugInfo/PDB/Raw/PDBFile.h"
26 #include "llvm/DebugInfo/PDB/Raw/PDBFileBuilder.h"
27 #include "llvm/DebugInfo/PDB/Raw/TpiStream.h"
28 #include "llvm/DebugInfo/PDB/Raw/TpiStreamBuilder.h"
29 #include "llvm/Object/COFF.h"
30 #include "llvm/Support/Endian.h"
31 #include "llvm/Support/FileOutputBuffer.h"
32 #include "llvm/Support/ScopedPrinter.h"
33 #include <memory>
34 
35 using namespace lld;
36 using namespace lld::coff;
37 using namespace llvm;
38 using namespace llvm::codeview;
39 using namespace llvm::support;
40 using namespace llvm::support::endian;
41 
42 using llvm::object::coff_section;
43 
44 static ExitOnError ExitOnErr;
45 
46 // Returns a list of all SectionChunks.
47 static std::vector<coff_section> getInputSections(SymbolTable *Symtab) {
48   std::vector<coff_section> V;
49   for (Chunk *C : Symtab->getChunks())
50     if (auto *SC = dyn_cast<SectionChunk>(C))
51       V.push_back(*SC->Header);
52   return V;
53 }
54 
55 static SectionChunk *findByName(std::vector<SectionChunk *> &Sections,
56                                 StringRef Name) {
57   for (SectionChunk *C : Sections)
58     if (C->getSectionName() == Name)
59       return C;
60   return nullptr;
61 }
62 
63 static void dumpDebugT(ScopedPrinter &W, ObjectFile *File) {
64   SectionChunk *Sec = findByName(File->getDebugChunks(), ".debug$T");
65   if (!Sec)
66     return;
67 
68   // First 4 bytes are section magic.
69   ArrayRef<uint8_t> Data = Sec->getContents();
70   if (Data.size() < 4)
71     fatal(".debug$T too short");
72   if (read32le(Data.data()) != COFF::DEBUG_SECTION_MAGIC)
73     fatal(".debug$T has an invalid magic");
74 
75   CVTypeDumper TypeDumper(&W, false);
76   if (auto EC = TypeDumper.dump(Data.slice(4)))
77     fatal(EC, "CVTypeDumper::dump failed");
78 }
79 
80 static void dumpDebugS(ScopedPrinter &W, ObjectFile *File) {
81   SectionChunk *Sec = findByName(File->getDebugChunks(), ".debug$S");
82   if (!Sec)
83     return;
84 
85   msf::ByteStream Stream(Sec->getContents());
86   CVSymbolArray Symbols;
87   msf::StreamReader Reader(Stream);
88   if (auto EC = Reader.readArray(Symbols, Reader.getLength()))
89     fatal(EC, "StreamReader.readArray<CVSymbolArray> failed");
90 
91   CVTypeDumper TypeDumper(&W, false);
92   CVSymbolDumper SymbolDumper(W, TypeDumper, nullptr, false);
93   if (auto EC = SymbolDumper.dump(Symbols))
94     fatal(EC, "CVSymbolDumper::dump failed");
95 }
96 
97 // Dump CodeView debug info. This is for debugging.
98 static void dumpCodeView(SymbolTable *Symtab) {
99   ScopedPrinter W(outs());
100 
101   for (ObjectFile *File : Symtab->ObjectFiles) {
102     dumpDebugT(W, File);
103     dumpDebugS(W, File);
104   }
105 }
106 
107 // Creates a PDB file.
108 void coff::createPDB(StringRef Path, SymbolTable *Symtab,
109                      ArrayRef<uint8_t> SectionTable) {
110   if (Config->DumpPdb)
111     dumpCodeView(Symtab);
112 
113   BumpPtrAllocator Alloc;
114   pdb::PDBFileBuilder Builder(Alloc);
115   ExitOnErr(Builder.initialize(4096)); // 4096 is blocksize
116 
117   // Create streams in MSF for predefined streams, namely
118   // PDB, TPI, DBI and IPI.
119   for (int I = 0; I < (int)pdb::kSpecialStreamCount; ++I)
120     ExitOnErr(Builder.getMsfBuilder().addStream(0));
121 
122   // Add an Info stream.
123   auto &InfoBuilder = Builder.getInfoBuilder();
124   InfoBuilder.setAge(1);
125 
126   // Should be a random number, 0 for now.
127   InfoBuilder.setGuid({});
128 
129   // Should be the current time, but set 0 for reproducibilty.
130   InfoBuilder.setSignature(0);
131   InfoBuilder.setVersion(pdb::PdbRaw_ImplVer::PdbImplVC70);
132 
133   // Add an empty DPI stream.
134   auto &DbiBuilder = Builder.getDbiBuilder();
135   DbiBuilder.setVersionHeader(pdb::PdbDbiV110);
136 
137   // Add an empty TPI stream.
138   auto &TpiBuilder = Builder.getTpiBuilder();
139   TpiBuilder.setVersionHeader(pdb::PdbTpiV80);
140 
141   // Add an empty IPI stream.
142   auto &IpiBuilder = Builder.getIpiBuilder();
143   IpiBuilder.setVersionHeader(pdb::PdbTpiV80);
144 
145   // Add Section Contributions.
146   std::vector<pdb::SectionContrib> Contribs =
147       pdb::DbiStreamBuilder::createSectionContribs(getInputSections(Symtab));
148   DbiBuilder.setSectionContribs(Contribs);
149 
150   // Add Section Map stream.
151   ArrayRef<object::coff_section> Sections = {
152       (const object::coff_section *)SectionTable.data(),
153       SectionTable.size() / sizeof(object::coff_section)};
154   std::vector<pdb::SecMapEntry> SectionMap =
155       pdb::DbiStreamBuilder::createSectionMap(Sections);
156   DbiBuilder.setSectionMap(SectionMap);
157 
158   ExitOnErr(DbiBuilder.addModuleInfo("", "* Linker *"));
159 
160   // Add COFF section header stream.
161   ExitOnErr(
162       DbiBuilder.addDbgStream(pdb::DbgHeaderType::SectionHdr, SectionTable));
163 
164   // Write to a file.
165   ExitOnErr(Builder.commit(Path));
166 }
167