blob: b62b3fb968faec40772e07a6df15c20b9e59b49d [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 Turnera9054dd2017-01-11 00:35:43 +000017#include "llvm/DebugInfo/CodeView/CVTypeDumper.h"
Rui Ueyama24625fd2016-11-22 23:51:34 +000018#include "llvm/DebugInfo/CodeView/SymbolDumper.h"
Zachary Turner629cb7d2017-01-11 23:24:22 +000019#include "llvm/DebugInfo/CodeView/TypeDatabase.h"
20#include "llvm/DebugInfo/CodeView/TypeDumpVisitor.h"
Rui Ueyama52896622017-01-12 03:09:25 +000021#include "llvm/DebugInfo/CodeView/TypeStreamMerger.h"
22#include "llvm/DebugInfo/CodeView/TypeTableBuilder.h"
Rui Ueyama24625fd2016-11-22 23:51:34 +000023#include "llvm/DebugInfo/MSF/ByteStream.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"
32#include "llvm/DebugInfo/PDB/Native/StringTableBuilder.h"
33#include "llvm/DebugInfo/PDB/Native/TpiStream.h"
34#include "llvm/DebugInfo/PDB/Native/TpiStreamBuilder.h"
Rui Ueyama20df4ec2016-10-31 21:09:21 +000035#include "llvm/Object/COFF.h"
Rui Ueyama1763c0d2015-12-08 18:39:55 +000036#include "llvm/Support/Endian.h"
Rui Ueyamae7378242015-12-04 23:11:05 +000037#include "llvm/Support/FileOutputBuffer.h"
Rui Ueyamabe939b32016-11-21 17:22:35 +000038#include "llvm/Support/ScopedPrinter.h"
Rui Ueyamae7378242015-12-04 23:11:05 +000039#include <memory>
40
Rui Ueyama1d99ab32016-09-15 22:24:51 +000041using namespace lld;
Rui Ueyama09e0b5f2016-11-12 00:00:51 +000042using namespace lld::coff;
Rui Ueyamae7378242015-12-04 23:11:05 +000043using namespace llvm;
Rui Ueyamabe939b32016-11-21 17:22:35 +000044using namespace llvm::codeview;
Rui Ueyama1763c0d2015-12-08 18:39:55 +000045using namespace llvm::support;
46using namespace llvm::support::endian;
Rui Ueyamae7378242015-12-04 23:11:05 +000047
Rui Ueyama09e0b5f2016-11-12 00:00:51 +000048using llvm::object::coff_section;
49
Rui Ueyamab28c6d42016-09-16 04:32:33 +000050static ExitOnError ExitOnErr;
51
Rui Ueyama09e0b5f2016-11-12 00:00:51 +000052// Returns a list of all SectionChunks.
53static std::vector<coff_section> getInputSections(SymbolTable *Symtab) {
54 std::vector<coff_section> V;
55 for (Chunk *C : Symtab->getChunks())
56 if (auto *SC = dyn_cast<SectionChunk>(C))
57 V.push_back(*SC->Header);
58 return V;
59}
60
Rui Ueyamabe939b32016-11-21 17:22:35 +000061static SectionChunk *findByName(std::vector<SectionChunk *> &Sections,
62 StringRef Name) {
63 for (SectionChunk *C : Sections)
64 if (C->getSectionName() == Name)
65 return C;
66 return nullptr;
67}
68
Rui Ueyama52896622017-01-12 03:09:25 +000069static ArrayRef<uint8_t> getDebugSection(ObjectFile *File, StringRef SecName) {
70 SectionChunk *Sec = findByName(File->getDebugChunks(), SecName);
Rui Ueyamac5cb7372016-12-01 01:22:48 +000071 if (!Sec)
Rui Ueyama26186c72016-12-09 04:46:54 +000072 return {};
Rui Ueyamac5cb7372016-12-01 01:22:48 +000073
74 // First 4 bytes are section magic.
75 ArrayRef<uint8_t> Data = Sec->getContents();
76 if (Data.size() < 4)
Rui Ueyama52896622017-01-12 03:09:25 +000077 fatal(SecName + " too short");
Rui Ueyamac5cb7372016-12-01 01:22:48 +000078 if (read32le(Data.data()) != COFF::DEBUG_SECTION_MAGIC)
Rui Ueyama52896622017-01-12 03:09:25 +000079 fatal(SecName + " has an invalid magic");
Rui Ueyama26186c72016-12-09 04:46:54 +000080 return Data.slice(4);
81}
Rui Ueyamac5cb7372016-12-01 01:22:48 +000082
Rui Ueyama52896622017-01-12 03:09:25 +000083// Merge .debug$T sections and returns it.
84static std::vector<uint8_t> mergeDebugT(SymbolTable *Symtab) {
85 ScopedPrinter W(outs());
86
87 // Visit all .debug$T sections to add them to Builder.
88 codeview::TypeTableBuilder Builder(BAlloc);
89 for (ObjectFile *File : Symtab->ObjectFiles) {
90 ArrayRef<uint8_t> Data = getDebugSection(File, ".debug$T");
91 if (Data.empty())
92 continue;
93
94 msf::ByteStream Stream(Data);
95 codeview::CVTypeArray Types;
96 msf::StreamReader Reader(Stream);
97 if (auto EC = Reader.readArray(Types, Reader.getLength()))
98 fatal(EC, "Reader::readArray failed");
99 if (!codeview::mergeTypeStreams(Builder, Types))
100 fatal("codeview::mergeTypeStreams failed");
101 }
102
103 // Construct section contents.
104 std::vector<uint8_t> V;
105 Builder.ForEachRecord([&](TypeIndex TI, ArrayRef<uint8_t> Rec) {
106 V.insert(V.end(), Rec.begin(), Rec.end());
107 });
108 return V;
109}
110
Rui Ueyama26186c72016-12-09 04:46:54 +0000111static void dumpDebugT(ScopedPrinter &W, ObjectFile *File) {
Rui Ueyama52896622017-01-12 03:09:25 +0000112 ListScope LS(W, "DebugT");
113 ArrayRef<uint8_t> Data = getDebugSection(File, ".debug$T");
Rui Ueyama26186c72016-12-09 04:46:54 +0000114 if (Data.empty())
115 return;
116
Zachary Turner629cb7d2017-01-11 23:24:22 +0000117 TypeDatabase TDB;
118 TypeDumpVisitor TDV(TDB, &W, false);
119 CVTypeDumper TypeDumper(TDB);
120 if (auto EC = TypeDumper.dump(Data, TDV))
Rui Ueyamac5cb7372016-12-01 01:22:48 +0000121 fatal(EC, "CVTypeDumper::dump failed");
122}
123
124static void dumpDebugS(ScopedPrinter &W, ObjectFile *File) {
Rui Ueyama52896622017-01-12 03:09:25 +0000125 ListScope LS(W, "DebugS");
126 ArrayRef<uint8_t> Data = getDebugSection(File, ".debug$S");
127 if (Data.empty())
Rui Ueyamac5cb7372016-12-01 01:22:48 +0000128 return;
129
Rui Ueyama52896622017-01-12 03:09:25 +0000130 msf::ByteStream Stream(Data);
Rui Ueyamac5cb7372016-12-01 01:22:48 +0000131 CVSymbolArray Symbols;
132 msf::StreamReader Reader(Stream);
133 if (auto EC = Reader.readArray(Symbols, Reader.getLength()))
134 fatal(EC, "StreamReader.readArray<CVSymbolArray> failed");
135
Zachary Turner629cb7d2017-01-11 23:24:22 +0000136 TypeDatabase TDB;
137 CVSymbolDumper SymbolDumper(W, TDB, nullptr, false);
Rui Ueyamac5cb7372016-12-01 01:22:48 +0000138 if (auto EC = SymbolDumper.dump(Symbols))
139 fatal(EC, "CVSymbolDumper::dump failed");
140}
141
Rui Ueyamabe939b32016-11-21 17:22:35 +0000142// Dump CodeView debug info. This is for debugging.
143static void dumpCodeView(SymbolTable *Symtab) {
144 ScopedPrinter W(outs());
145
146 for (ObjectFile *File : Symtab->ObjectFiles) {
Rui Ueyamac5cb7372016-12-01 01:22:48 +0000147 dumpDebugT(W, File);
148 dumpDebugS(W, File);
Rui Ueyamabe939b32016-11-21 17:22:35 +0000149 }
150}
151
Rui Ueyama52896622017-01-12 03:09:25 +0000152static void addTypeInfo(pdb::TpiStreamBuilder &TpiBuilder,
153 ArrayRef<uint8_t> Data) {
154 msf::ByteStream Stream(Data);
155 codeview::CVTypeArray Records;
156 msf::StreamReader Reader(Stream);
157 if (auto EC = Reader.readArray(Records, Reader.getLength()))
158 fatal(EC, "Reader.readArray failed");
159 for (const codeview::CVType &Rec : Records)
160 TpiBuilder.addTypeRecord(Rec);
Rui Ueyama26186c72016-12-09 04:46:54 +0000161}
162
Rui Ueyama09e0b5f2016-11-12 00:00:51 +0000163// Creates a PDB file.
164void coff::createPDB(StringRef Path, SymbolTable *Symtab,
Saleem Abdulrasooldf8a13b2017-01-04 17:56:54 +0000165 ArrayRef<uint8_t> SectionTable,
166 const llvm::codeview::DebugInfo *DI) {
Rui Ueyamabe939b32016-11-21 17:22:35 +0000167 if (Config->DumpPdb)
168 dumpCodeView(Symtab);
169
Rui Ueyamab28c6d42016-09-16 04:32:33 +0000170 BumpPtrAllocator Alloc;
171 pdb::PDBFileBuilder Builder(Alloc);
Rui Ueyama12979542016-09-30 20:53:45 +0000172 ExitOnErr(Builder.initialize(4096)); // 4096 is blocksize
Rui Ueyama7f382992016-09-15 18:55:18 +0000173
Rui Ueyama8d3fb5d2016-10-05 22:08:58 +0000174 // Create streams in MSF for predefined streams, namely
175 // PDB, TPI, DBI and IPI.
176 for (int I = 0; I < (int)pdb::kSpecialStreamCount; ++I)
177 ExitOnErr(Builder.getMsfBuilder().addStream(0));
Rui Ueyama7f382992016-09-15 18:55:18 +0000178
Rui Ueyamabb542b32016-09-16 22:51:17 +0000179 // Add an Info stream.
180 auto &InfoBuilder = Builder.getInfoBuilder();
Saleem Abdulrasooldf8a13b2017-01-04 17:56:54 +0000181 InfoBuilder.setAge(DI->PDB70.Age);
182 InfoBuilder.setGuid(
183 *reinterpret_cast<const pdb::PDB_UniqueId *>(&DI->PDB70.Signature));
Rui Ueyamabb542b32016-09-16 22:51:17 +0000184 // Should be the current time, but set 0 for reproducibilty.
185 InfoBuilder.setSignature(0);
Rui Ueyamabb542b32016-09-16 22:51:17 +0000186 InfoBuilder.setVersion(pdb::PdbRaw_ImplVer::PdbImplVC70);
Rui Ueyama7f382992016-09-15 18:55:18 +0000187
Rui Ueyama1343fac2016-10-06 22:52:01 +0000188 // Add an empty DPI stream.
189 auto &DbiBuilder = Builder.getDbiBuilder();
190 DbiBuilder.setVersionHeader(pdb::PdbDbiV110);
191
Rui Ueyamab28c6d42016-09-16 04:32:33 +0000192 // Add an empty TPI stream.
193 auto &TpiBuilder = Builder.getTpiBuilder();
194 TpiBuilder.setVersionHeader(pdb::PdbTpiV80);
Rui Ueyama52896622017-01-12 03:09:25 +0000195 std::vector<uint8_t> TpiData;
196 if (Config->DebugPdb) {
197 TpiData = mergeDebugT(Symtab);
198 addTypeInfo(TpiBuilder, TpiData);
199 }
Rui Ueyama1763c0d2015-12-08 18:39:55 +0000200
Rui Ueyamad381c982016-10-05 21:37:25 +0000201 // Add an empty IPI stream.
202 auto &IpiBuilder = Builder.getIpiBuilder();
203 IpiBuilder.setVersionHeader(pdb::PdbTpiV80);
204
Rui Ueyama09e0b5f2016-11-12 00:00:51 +0000205 // Add Section Contributions.
Rui Ueyama09e0b5f2016-11-12 00:00:51 +0000206 std::vector<pdb::SectionContrib> Contribs =
Vitaly Buka4cf112c2016-11-14 20:21:41 +0000207 pdb::DbiStreamBuilder::createSectionContribs(getInputSections(Symtab));
Rui Ueyama09e0b5f2016-11-12 00:00:51 +0000208 DbiBuilder.setSectionContribs(Contribs);
209
Rui Ueyama20df4ec2016-10-31 21:09:21 +0000210 // Add Section Map stream.
211 ArrayRef<object::coff_section> Sections = {
Rui Ueyama6294a242016-11-04 17:41:29 +0000212 (const object::coff_section *)SectionTable.data(),
Rui Ueyama20df4ec2016-10-31 21:09:21 +0000213 SectionTable.size() / sizeof(object::coff_section)};
214 std::vector<pdb::SecMapEntry> SectionMap =
215 pdb::DbiStreamBuilder::createSectionMap(Sections);
216 DbiBuilder.setSectionMap(SectionMap);
217
Rui Ueyamac91f7162016-11-16 01:10:46 +0000218 ExitOnErr(DbiBuilder.addModuleInfo("", "* Linker *"));
219
Rui Ueyama9f66f822016-10-11 19:45:07 +0000220 // Add COFF section header stream.
221 ExitOnErr(
222 DbiBuilder.addDbgStream(pdb::DbgHeaderType::SectionHdr, SectionTable));
223
Rui Ueyama3e9d6bb2016-09-26 23:53:55 +0000224 // Write to a file.
Rui Ueyama5c82eea2016-09-30 20:39:04 +0000225 ExitOnErr(Builder.commit(Path));
Rui Ueyamae7378242015-12-04 23:11:05 +0000226}