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