blob: eb510e3d3d16b0a5e75a8bd9a60f774429ce8d77 [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"
Zachary Turnerd2684b72017-02-25 00:33:34 +000023#include "llvm/DebugInfo/MSF/BinaryByteStream.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"
Zachary Turner7b327d02017-02-16 23:35:45 +000032#include "llvm/DebugInfo/PDB/Native/PDBTypeServerHandler.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000033#include "llvm/DebugInfo/PDB/Native/StringTableBuilder.h"
34#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"
Rui Ueyama1763c0d2015-12-08 18:39:55 +000037#include "llvm/Support/Endian.h"
Rui Ueyamae7378242015-12-04 23:11:05 +000038#include "llvm/Support/FileOutputBuffer.h"
Zachary Turner7b327d02017-02-16 23:35:45 +000039#include "llvm/Support/Path.h"
Rui Ueyamabe939b32016-11-21 17:22:35 +000040#include "llvm/Support/ScopedPrinter.h"
Rui Ueyamae7378242015-12-04 23:11:05 +000041#include <memory>
42
Rui Ueyama1d99ab32016-09-15 22:24:51 +000043using namespace lld;
Rui Ueyama09e0b5f2016-11-12 00:00:51 +000044using namespace lld::coff;
Rui Ueyamae7378242015-12-04 23:11:05 +000045using namespace llvm;
Rui Ueyamabe939b32016-11-21 17:22:35 +000046using namespace llvm::codeview;
Rui Ueyama1763c0d2015-12-08 18:39:55 +000047using namespace llvm::support;
48using namespace llvm::support::endian;
Rui Ueyamae7378242015-12-04 23:11:05 +000049
Rui Ueyama09e0b5f2016-11-12 00:00:51 +000050using llvm::object::coff_section;
51
Rui Ueyamab28c6d42016-09-16 04:32:33 +000052static ExitOnError ExitOnErr;
53
Rui Ueyama09e0b5f2016-11-12 00:00:51 +000054// Returns a list of all SectionChunks.
55static 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
Rui Ueyamabe939b32016-11-21 17:22:35 +000063static 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
Rui Ueyama52896622017-01-12 03:09:25 +000071static ArrayRef<uint8_t> getDebugSection(ObjectFile *File, StringRef SecName) {
72 SectionChunk *Sec = findByName(File->getDebugChunks(), SecName);
Rui Ueyamac5cb7372016-12-01 01:22:48 +000073 if (!Sec)
Rui Ueyama26186c72016-12-09 04:46:54 +000074 return {};
Rui Ueyamac5cb7372016-12-01 01:22:48 +000075
76 // First 4 bytes are section magic.
77 ArrayRef<uint8_t> Data = Sec->getContents();
78 if (Data.size() < 4)
Rui Ueyama52896622017-01-12 03:09:25 +000079 fatal(SecName + " too short");
Rui Ueyamac5cb7372016-12-01 01:22:48 +000080 if (read32le(Data.data()) != COFF::DEBUG_SECTION_MAGIC)
Rui Ueyama52896622017-01-12 03:09:25 +000081 fatal(SecName + " has an invalid magic");
Rui Ueyama26186c72016-12-09 04:46:54 +000082 return Data.slice(4);
83}
Rui Ueyamac5cb7372016-12-01 01:22:48 +000084
Rui Ueyama52896622017-01-12 03:09:25 +000085// Merge .debug$T sections and returns it.
86static 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 msf::ByteStream Stream(Data);
97 codeview::CVTypeArray Types;
98 msf::StreamReader Reader(Stream);
Zachary Turner7b327d02017-02-16 23:35:45 +000099 // 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()));
Rui Ueyama52896622017-01-12 03:09:25 +0000105 if (auto EC = Reader.readArray(Types, Reader.getLength()))
106 fatal(EC, "Reader::readArray failed");
Zachary Turner7b327d02017-02-16 23:35:45 +0000107 if (auto Err = codeview::mergeTypeStreams(Builder, &Handler, Types))
Rui Ueyamaa9b29612017-02-02 00:47:10 +0000108 fatal(Err, "codeview::mergeTypeStreams failed");
Rui Ueyama52896622017-01-12 03:09:25 +0000109 }
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
Rui Ueyama26186c72016-12-09 04:46:54 +0000119static void dumpDebugT(ScopedPrinter &W, ObjectFile *File) {
Rui Ueyama52896622017-01-12 03:09:25 +0000120 ListScope LS(W, "DebugT");
121 ArrayRef<uint8_t> Data = getDebugSection(File, ".debug$T");
Rui Ueyama26186c72016-12-09 04:46:54 +0000122 if (Data.empty())
123 return;
124
Zachary Turner629cb7d2017-01-11 23:24:22 +0000125 TypeDatabase TDB;
126 TypeDumpVisitor TDV(TDB, &W, false);
Zachary Turner7b327d02017-02-16 23:35:45 +0000127 // Use a default implementation that does not follow type servers and instead
128 // just dumps the contents of the TypeServer2 record.
Zachary Turner629cb7d2017-01-11 23:24:22 +0000129 CVTypeDumper TypeDumper(TDB);
130 if (auto EC = TypeDumper.dump(Data, TDV))
Rui Ueyamac5cb7372016-12-01 01:22:48 +0000131 fatal(EC, "CVTypeDumper::dump failed");
132}
133
134static void dumpDebugS(ScopedPrinter &W, ObjectFile *File) {
Rui Ueyama52896622017-01-12 03:09:25 +0000135 ListScope LS(W, "DebugS");
136 ArrayRef<uint8_t> Data = getDebugSection(File, ".debug$S");
137 if (Data.empty())
Rui Ueyamac5cb7372016-12-01 01:22:48 +0000138 return;
139
Rui Ueyama52896622017-01-12 03:09:25 +0000140 msf::ByteStream Stream(Data);
Rui Ueyamac5cb7372016-12-01 01:22:48 +0000141 CVSymbolArray Symbols;
142 msf::StreamReader Reader(Stream);
143 if (auto EC = Reader.readArray(Symbols, Reader.getLength()))
144 fatal(EC, "StreamReader.readArray<CVSymbolArray> failed");
145
Zachary Turner629cb7d2017-01-11 23:24:22 +0000146 TypeDatabase TDB;
147 CVSymbolDumper SymbolDumper(W, TDB, nullptr, false);
Rui Ueyamac5cb7372016-12-01 01:22:48 +0000148 if (auto EC = SymbolDumper.dump(Symbols))
149 fatal(EC, "CVSymbolDumper::dump failed");
150}
151
Rui Ueyamabe939b32016-11-21 17:22:35 +0000152// Dump CodeView debug info. This is for debugging.
153static void dumpCodeView(SymbolTable *Symtab) {
154 ScopedPrinter W(outs());
155
156 for (ObjectFile *File : Symtab->ObjectFiles) {
Rui Ueyamac5cb7372016-12-01 01:22:48 +0000157 dumpDebugT(W, File);
158 dumpDebugS(W, File);
Rui Ueyamabe939b32016-11-21 17:22:35 +0000159 }
160}
161
Rui Ueyama52896622017-01-12 03:09:25 +0000162static void addTypeInfo(pdb::TpiStreamBuilder &TpiBuilder,
163 ArrayRef<uint8_t> Data) {
164 msf::ByteStream Stream(Data);
165 codeview::CVTypeArray Records;
166 msf::StreamReader 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);
Rui Ueyama26186c72016-12-09 04:46:54 +0000171}
172
Rui Ueyama09e0b5f2016-11-12 00:00:51 +0000173// Creates a PDB file.
174void coff::createPDB(StringRef Path, SymbolTable *Symtab,
Saleem Abdulrasooldf8a13b2017-01-04 17:56:54 +0000175 ArrayRef<uint8_t> SectionTable,
176 const llvm::codeview::DebugInfo *DI) {
Rui Ueyamabe939b32016-11-21 17:22:35 +0000177 if (Config->DumpPdb)
178 dumpCodeView(Symtab);
179
Rui Ueyamab28c6d42016-09-16 04:32:33 +0000180 BumpPtrAllocator Alloc;
181 pdb::PDBFileBuilder Builder(Alloc);
Rui Ueyama12979542016-09-30 20:53:45 +0000182 ExitOnErr(Builder.initialize(4096)); // 4096 is blocksize
Rui Ueyama7f382992016-09-15 18:55:18 +0000183
Rui Ueyama8d3fb5d2016-10-05 22:08:58 +0000184 // 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));
Rui Ueyama7f382992016-09-15 18:55:18 +0000188
Rui Ueyamabb542b32016-09-16 22:51:17 +0000189 // Add an Info stream.
190 auto &InfoBuilder = Builder.getInfoBuilder();
Saleem Abdulrasool0acd6dd2017-02-07 04:28:02 +0000191 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);
Rui Ueyamabb542b32016-09-16 22:51:17 +0000197 // Should be the current time, but set 0 for reproducibilty.
198 InfoBuilder.setSignature(0);
Rui Ueyamabb542b32016-09-16 22:51:17 +0000199 InfoBuilder.setVersion(pdb::PdbRaw_ImplVer::PdbImplVC70);
Rui Ueyama7f382992016-09-15 18:55:18 +0000200
Rui Ueyama1343fac2016-10-06 22:52:01 +0000201 // Add an empty DPI stream.
202 auto &DbiBuilder = Builder.getDbiBuilder();
203 DbiBuilder.setVersionHeader(pdb::PdbDbiV110);
204
Rui Ueyamab28c6d42016-09-16 04:32:33 +0000205 // Add an empty TPI stream.
206 auto &TpiBuilder = Builder.getTpiBuilder();
207 TpiBuilder.setVersionHeader(pdb::PdbTpiV80);
Rui Ueyama52896622017-01-12 03:09:25 +0000208 std::vector<uint8_t> TpiData;
209 if (Config->DebugPdb) {
210 TpiData = mergeDebugT(Symtab);
211 addTypeInfo(TpiBuilder, TpiData);
212 }
Rui Ueyama1763c0d2015-12-08 18:39:55 +0000213
Rui Ueyamad381c982016-10-05 21:37:25 +0000214 // Add an empty IPI stream.
215 auto &IpiBuilder = Builder.getIpiBuilder();
216 IpiBuilder.setVersionHeader(pdb::PdbTpiV80);
217
Rui Ueyama09e0b5f2016-11-12 00:00:51 +0000218 // Add Section Contributions.
Rui Ueyama09e0b5f2016-11-12 00:00:51 +0000219 std::vector<pdb::SectionContrib> Contribs =
Vitaly Buka4cf112c2016-11-14 20:21:41 +0000220 pdb::DbiStreamBuilder::createSectionContribs(getInputSections(Symtab));
Rui Ueyama09e0b5f2016-11-12 00:00:51 +0000221 DbiBuilder.setSectionContribs(Contribs);
222
Rui Ueyama20df4ec2016-10-31 21:09:21 +0000223 // Add Section Map stream.
224 ArrayRef<object::coff_section> Sections = {
Rui Ueyama6294a242016-11-04 17:41:29 +0000225 (const object::coff_section *)SectionTable.data(),
Rui Ueyama20df4ec2016-10-31 21:09:21 +0000226 SectionTable.size() / sizeof(object::coff_section)};
227 std::vector<pdb::SecMapEntry> SectionMap =
228 pdb::DbiStreamBuilder::createSectionMap(Sections);
229 DbiBuilder.setSectionMap(SectionMap);
230
Rui Ueyamac91f7162016-11-16 01:10:46 +0000231 ExitOnErr(DbiBuilder.addModuleInfo("", "* Linker *"));
232
Rui Ueyama9f66f822016-10-11 19:45:07 +0000233 // Add COFF section header stream.
234 ExitOnErr(
235 DbiBuilder.addDbgStream(pdb::DbgHeaderType::SectionHdr, SectionTable));
236
Rui Ueyama3e9d6bb2016-09-26 23:53:55 +0000237 // Write to a file.
Rui Ueyama5c82eea2016-09-30 20:39:04 +0000238 ExitOnErr(Builder.commit(Path));
Rui Ueyamae7378242015-12-04 23:11:05 +0000239}