blob: 0266148cc6c9069bf2192088e07f5f44618e4edc [file] [log] [blame]
Rui Ueyamae7378242015-12-04 23:11:05 +00001//===- 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
Rui Ueyama1d99ab32016-09-15 22:24:51 +000010#include "PDB.h"
Rui Ueyama09e0b5f2016-11-12 00:00:51 +000011#include "Chunks.h"
Rui Ueyamabe939b32016-11-21 17:22:35 +000012#include "Config.h"
Rui Ueyamae7378242015-12-04 23:11:05 +000013#include "Error.h"
Rui Ueyama09e0b5f2016-11-12 00:00:51 +000014#include "SymbolTable.h"
15#include "Symbols.h"
Saleem Abdulrasooldf8a13b2017-01-04 17:56:54 +000016#include "llvm/DebugInfo/CodeView/CVDebugRecord.h"
Zachary Turner526f4f22017-05-19 19:26:58 +000017#include "llvm/DebugInfo/CodeView/CVTypeVisitor.h"
18#include "llvm/DebugInfo/CodeView/LazyRandomTypeCollection.h"
Rui Ueyama24625fd2016-11-22 23:51:34 +000019#include "llvm/DebugInfo/CodeView/SymbolDumper.h"
Zachary Turner629cb7d2017-01-11 23:24:22 +000020#include "llvm/DebugInfo/CodeView/TypeDatabase.h"
21#include "llvm/DebugInfo/CodeView/TypeDumpVisitor.h"
Rui Ueyama52896622017-01-12 03:09:25 +000022#include "llvm/DebugInfo/CodeView/TypeStreamMerger.h"
23#include "llvm/DebugInfo/CodeView/TypeTableBuilder.h"
Rui Ueyamab28c6d42016-09-16 04:32:33 +000024#include "llvm/DebugInfo/MSF/MSFBuilder.h"
Rui Ueyama7f382992016-09-15 18:55:18 +000025#include "llvm/DebugInfo/MSF/MSFCommon.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000026#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"
Rafael Espindolaa0f30da2017-05-02 20:19:42 +000032#include "llvm/DebugInfo/PDB/Native/PDBStringTableBuilder.h"
Zachary Turner7b327d02017-02-16 23:35:45 +000033#include "llvm/DebugInfo/PDB/Native/PDBTypeServerHandler.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000034#include "llvm/DebugInfo/PDB/Native/TpiStream.h"
35#include "llvm/DebugInfo/PDB/Native/TpiStreamBuilder.h"
Rui Ueyama20df4ec2016-10-31 21:09:21 +000036#include "llvm/Object/COFF.h"
Zachary Turnerd9dc2822017-03-02 20:52:51 +000037#include "llvm/Support/BinaryByteStream.h"
Rui Ueyama1763c0d2015-12-08 18:39:55 +000038#include "llvm/Support/Endian.h"
Rui Ueyamae7378242015-12-04 23:11:05 +000039#include "llvm/Support/FileOutputBuffer.h"
Zachary Turner7b327d02017-02-16 23:35:45 +000040#include "llvm/Support/Path.h"
Rui Ueyamabe939b32016-11-21 17:22:35 +000041#include "llvm/Support/ScopedPrinter.h"
Rui Ueyamae7378242015-12-04 23:11:05 +000042#include <memory>
43
Rui Ueyama1d99ab32016-09-15 22:24:51 +000044using namespace lld;
Rui Ueyama09e0b5f2016-11-12 00:00:51 +000045using namespace lld::coff;
Rui Ueyamae7378242015-12-04 23:11:05 +000046using namespace llvm;
Rui Ueyamabe939b32016-11-21 17:22:35 +000047using namespace llvm::codeview;
Rui Ueyama1763c0d2015-12-08 18:39:55 +000048using namespace llvm::support;
49using namespace llvm::support::endian;
Rui Ueyamae7378242015-12-04 23:11:05 +000050
Rui Ueyama09e0b5f2016-11-12 00:00:51 +000051using llvm::object::coff_section;
52
Rui Ueyamab28c6d42016-09-16 04:32:33 +000053static ExitOnError ExitOnErr;
54
Rui Ueyama09e0b5f2016-11-12 00:00:51 +000055// Returns a list of all SectionChunks.
56static 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
Rui Ueyamabe939b32016-11-21 17:22:35 +000064static 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
Rui Ueyama52896622017-01-12 03:09:25 +000072static ArrayRef<uint8_t> getDebugSection(ObjectFile *File, StringRef SecName) {
73 SectionChunk *Sec = findByName(File->getDebugChunks(), SecName);
Rui Ueyamac5cb7372016-12-01 01:22:48 +000074 if (!Sec)
Rui Ueyama26186c72016-12-09 04:46:54 +000075 return {};
Rui Ueyamac5cb7372016-12-01 01:22:48 +000076
77 // First 4 bytes are section magic.
78 ArrayRef<uint8_t> Data = Sec->getContents();
79 if (Data.size() < 4)
Rui Ueyama52896622017-01-12 03:09:25 +000080 fatal(SecName + " too short");
Rui Ueyamac5cb7372016-12-01 01:22:48 +000081 if (read32le(Data.data()) != COFF::DEBUG_SECTION_MAGIC)
Rui Ueyama52896622017-01-12 03:09:25 +000082 fatal(SecName + " has an invalid magic");
Rui Ueyama26186c72016-12-09 04:46:54 +000083 return Data.slice(4);
84}
Rui Ueyamac5cb7372016-12-01 01:22:48 +000085
Reid Kleckner5d577522017-03-24 17:26:38 +000086static void addTypeInfo(pdb::TpiStreamBuilder &TpiBuilder,
Reid Kleckner13fc4112017-04-04 00:56:34 +000087 codeview::TypeTableBuilder &TypeTable) {
Reid Kleckner5d577522017-03-24 17:26:38 +000088 // Start the TPI or IPI stream header.
89 TpiBuilder.setVersionHeader(pdb::PdbTpiV80);
Rui Ueyama52896622017-01-12 03:09:25 +000090
Reid Kleckner5d577522017-03-24 17:26:38 +000091 // Flatten the in memory type table.
Reid Kleckner5d577522017-03-24 17:26:38 +000092 TypeTable.ForEachRecord([&](TypeIndex TI, ArrayRef<uint8_t> Rec) {
Reid Kleckner13fc4112017-04-04 00:56:34 +000093 // FIXME: Hash types.
94 TpiBuilder.addTypeRecord(Rec, None);
Reid Kleckner5d577522017-03-24 17:26:38 +000095 });
Reid Kleckner5d577522017-03-24 17:26:38 +000096}
97
98// Merge .debug$T sections into IpiData and TpiData.
99static void mergeDebugT(SymbolTable *Symtab, pdb::PDBFileBuilder &Builder,
Reid Kleckner13fc4112017-04-04 00:56:34 +0000100 codeview::TypeTableBuilder &TypeTable,
101 codeview::TypeTableBuilder &IDTable) {
Rui Ueyama52896622017-01-12 03:09:25 +0000102 // Visit all .debug$T sections to add them to Builder.
Rui Ueyama52896622017-01-12 03:09:25 +0000103 for (ObjectFile *File : Symtab->ObjectFiles) {
104 ArrayRef<uint8_t> Data = getDebugSection(File, ".debug$T");
105 if (Data.empty())
106 continue;
107
Reid Kleckner5d577522017-03-24 17:26:38 +0000108 BinaryByteStream Stream(Data, support::little);
Rui Ueyama52896622017-01-12 03:09:25 +0000109 codeview::CVTypeArray Types;
Zachary Turner120faca2017-02-27 22:11:43 +0000110 BinaryStreamReader Reader(Stream);
Zachary Turnerb32ec022017-05-18 23:04:08 +0000111 SmallVector<TypeIndex, 128> SourceToDest;
Zachary Turner7b327d02017-02-16 23:35:45 +0000112 // Follow type servers. If the same type server is encountered more than
113 // once for this instance of `PDBTypeServerHandler` (for example if many
114 // object files reference the same TypeServer), the types from the
115 // TypeServer will only be visited once.
116 pdb::PDBTypeServerHandler Handler;
117 Handler.addSearchPath(llvm::sys::path::parent_path(File->getName()));
Rui Ueyama52896622017-01-12 03:09:25 +0000118 if (auto EC = Reader.readArray(Types, Reader.getLength()))
119 fatal(EC, "Reader::readArray failed");
Zachary Turnerb32ec022017-05-18 23:04:08 +0000120 if (auto Err = codeview::mergeTypeStreams(IDTable, TypeTable, SourceToDest,
121 &Handler, Types))
Rui Ueyamaa9b29612017-02-02 00:47:10 +0000122 fatal(Err, "codeview::mergeTypeStreams failed");
Rui Ueyama52896622017-01-12 03:09:25 +0000123 }
124
Reid Kleckner5d577522017-03-24 17:26:38 +0000125 // Construct TPI stream contents.
Reid Kleckner13fc4112017-04-04 00:56:34 +0000126 addTypeInfo(Builder.getTpiBuilder(), TypeTable);
Reid Kleckner5d577522017-03-24 17:26:38 +0000127
128 // Construct IPI stream contents.
Reid Kleckner13fc4112017-04-04 00:56:34 +0000129 addTypeInfo(Builder.getIpiBuilder(), IDTable);
Rui Ueyama52896622017-01-12 03:09:25 +0000130}
131
Rui Ueyama26186c72016-12-09 04:46:54 +0000132static void dumpDebugT(ScopedPrinter &W, ObjectFile *File) {
Rui Ueyama52896622017-01-12 03:09:25 +0000133 ListScope LS(W, "DebugT");
134 ArrayRef<uint8_t> Data = getDebugSection(File, ".debug$T");
Rui Ueyama26186c72016-12-09 04:46:54 +0000135 if (Data.empty())
136 return;
137
Zachary Turner526f4f22017-05-19 19:26:58 +0000138 LazyRandomTypeCollection Types(Data, 100);
139 TypeDumpVisitor TDV(Types, &W, false);
Zachary Turner7b327d02017-02-16 23:35:45 +0000140 // Use a default implementation that does not follow type servers and instead
141 // just dumps the contents of the TypeServer2 record.
Zachary Turner526f4f22017-05-19 19:26:58 +0000142 if (auto EC = codeview::visitTypeStream(Types, TDV))
Rui Ueyamac5cb7372016-12-01 01:22:48 +0000143 fatal(EC, "CVTypeDumper::dump failed");
144}
145
146static void dumpDebugS(ScopedPrinter &W, ObjectFile *File) {
Rui Ueyama52896622017-01-12 03:09:25 +0000147 ListScope LS(W, "DebugS");
148 ArrayRef<uint8_t> Data = getDebugSection(File, ".debug$S");
149 if (Data.empty())
Rui Ueyamac5cb7372016-12-01 01:22:48 +0000150 return;
151
Zachary Turner695ed562017-02-28 00:04:07 +0000152 BinaryByteStream Stream(Data, llvm::support::little);
Rui Ueyamac5cb7372016-12-01 01:22:48 +0000153 CVSymbolArray Symbols;
Zachary Turner120faca2017-02-27 22:11:43 +0000154 BinaryStreamReader Reader(Stream);
Rui Ueyamac5cb7372016-12-01 01:22:48 +0000155 if (auto EC = Reader.readArray(Symbols, Reader.getLength()))
156 fatal(EC, "StreamReader.readArray<CVSymbolArray> failed");
157
Zachary Turner5634ccf2017-05-05 22:06:06 +0000158 TypeDatabase TDB(0);
Zachary Turner629cb7d2017-01-11 23:24:22 +0000159 CVSymbolDumper SymbolDumper(W, TDB, nullptr, false);
Rui Ueyamac5cb7372016-12-01 01:22:48 +0000160 if (auto EC = SymbolDumper.dump(Symbols))
161 fatal(EC, "CVSymbolDumper::dump failed");
162}
163
Rui Ueyamabe939b32016-11-21 17:22:35 +0000164// Dump CodeView debug info. This is for debugging.
165static void dumpCodeView(SymbolTable *Symtab) {
166 ScopedPrinter W(outs());
167
168 for (ObjectFile *File : Symtab->ObjectFiles) {
Rui Ueyamac5cb7372016-12-01 01:22:48 +0000169 dumpDebugT(W, File);
170 dumpDebugS(W, File);
Rui Ueyamabe939b32016-11-21 17:22:35 +0000171 }
172}
173
Rui Ueyama09e0b5f2016-11-12 00:00:51 +0000174// Creates a PDB file.
175void coff::createPDB(StringRef Path, SymbolTable *Symtab,
Saleem Abdulrasooldf8a13b2017-01-04 17:56:54 +0000176 ArrayRef<uint8_t> SectionTable,
177 const llvm::codeview::DebugInfo *DI) {
Rui Ueyamabe939b32016-11-21 17:22:35 +0000178 if (Config->DumpPdb)
179 dumpCodeView(Symtab);
180
Rui Ueyamab28c6d42016-09-16 04:32:33 +0000181 BumpPtrAllocator Alloc;
182 pdb::PDBFileBuilder Builder(Alloc);
Rui Ueyama12979542016-09-30 20:53:45 +0000183 ExitOnErr(Builder.initialize(4096)); // 4096 is blocksize
Rui Ueyama7f382992016-09-15 18:55:18 +0000184
Rui Ueyama8d3fb5d2016-10-05 22:08:58 +0000185 // Create streams in MSF for predefined streams, namely
186 // PDB, TPI, DBI and IPI.
187 for (int I = 0; I < (int)pdb::kSpecialStreamCount; ++I)
188 ExitOnErr(Builder.getMsfBuilder().addStream(0));
Rui Ueyama7f382992016-09-15 18:55:18 +0000189
Rui Ueyamabb542b32016-09-16 22:51:17 +0000190 // Add an Info stream.
191 auto &InfoBuilder = Builder.getInfoBuilder();
Saleem Abdulrasool0acd6dd2017-02-07 04:28:02 +0000192 InfoBuilder.setAge(DI ? DI->PDB70.Age : 0);
193
194 pdb::PDB_UniqueId uuid{};
195 if (DI)
196 memcpy(&uuid, &DI->PDB70.Signature, sizeof(uuid));
197 InfoBuilder.setGuid(uuid);
Rui Ueyamabb542b32016-09-16 22:51:17 +0000198 // Should be the current time, but set 0 for reproducibilty.
199 InfoBuilder.setSignature(0);
Rui Ueyamabb542b32016-09-16 22:51:17 +0000200 InfoBuilder.setVersion(pdb::PdbRaw_ImplVer::PdbImplVC70);
Rui Ueyama7f382992016-09-15 18:55:18 +0000201
Rui Ueyama1343fac2016-10-06 22:52:01 +0000202 // Add an empty DPI stream.
203 auto &DbiBuilder = Builder.getDbiBuilder();
204 DbiBuilder.setVersionHeader(pdb::PdbDbiV110);
205
Reid Kleckner13fc4112017-04-04 00:56:34 +0000206 codeview::TypeTableBuilder TypeTable(BAlloc);
207 codeview::TypeTableBuilder IDTable(BAlloc);
208 mergeDebugT(Symtab, Builder, TypeTable, IDTable);
Rui Ueyamad381c982016-10-05 21:37:25 +0000209
Rui Ueyama09e0b5f2016-11-12 00:00:51 +0000210 // Add Section Contributions.
Rui Ueyama09e0b5f2016-11-12 00:00:51 +0000211 std::vector<pdb::SectionContrib> Contribs =
Vitaly Buka4cf112c2016-11-14 20:21:41 +0000212 pdb::DbiStreamBuilder::createSectionContribs(getInputSections(Symtab));
Rui Ueyama09e0b5f2016-11-12 00:00:51 +0000213 DbiBuilder.setSectionContribs(Contribs);
214
Rui Ueyama20df4ec2016-10-31 21:09:21 +0000215 // Add Section Map stream.
216 ArrayRef<object::coff_section> Sections = {
Rui Ueyama6294a242016-11-04 17:41:29 +0000217 (const object::coff_section *)SectionTable.data(),
Rui Ueyama20df4ec2016-10-31 21:09:21 +0000218 SectionTable.size() / sizeof(object::coff_section)};
219 std::vector<pdb::SecMapEntry> SectionMap =
220 pdb::DbiStreamBuilder::createSectionMap(Sections);
221 DbiBuilder.setSectionMap(SectionMap);
222
Zachary Turnerea4e6072017-03-15 22:18:53 +0000223 ExitOnErr(DbiBuilder.addModuleInfo("* Linker *"));
Rui Ueyamac91f7162016-11-16 01:10:46 +0000224
Rui Ueyama9f66f822016-10-11 19:45:07 +0000225 // Add COFF section header stream.
226 ExitOnErr(
227 DbiBuilder.addDbgStream(pdb::DbgHeaderType::SectionHdr, SectionTable));
228
Rui Ueyama3e9d6bb2016-09-26 23:53:55 +0000229 // Write to a file.
Rui Ueyama5c82eea2016-09-30 20:39:04 +0000230 ExitOnErr(Builder.commit(Path));
Rui Ueyamae7378242015-12-04 23:11:05 +0000231}