blob: bc31a5beb0ab321c04ad8d40fc73ca2cae8193bc [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 Ueyama24625fd2016-11-22 23:51:34 +000021#include "llvm/DebugInfo/MSF/ByteStream.h"
Rui Ueyamab28c6d42016-09-16 04:32:33 +000022#include "llvm/DebugInfo/MSF/MSFBuilder.h"
Rui Ueyama7f382992016-09-15 18:55:18 +000023#include "llvm/DebugInfo/MSF/MSFCommon.h"
Rui Ueyamab28c6d42016-09-16 04:32:33 +000024#include "llvm/DebugInfo/PDB/Raw/DbiStream.h"
25#include "llvm/DebugInfo/PDB/Raw/DbiStreamBuilder.h"
26#include "llvm/DebugInfo/PDB/Raw/InfoStream.h"
27#include "llvm/DebugInfo/PDB/Raw/InfoStreamBuilder.h"
28#include "llvm/DebugInfo/PDB/Raw/PDBFile.h"
29#include "llvm/DebugInfo/PDB/Raw/PDBFileBuilder.h"
30#include "llvm/DebugInfo/PDB/Raw/TpiStream.h"
31#include "llvm/DebugInfo/PDB/Raw/TpiStreamBuilder.h"
Rui Ueyama20df4ec2016-10-31 21:09:21 +000032#include "llvm/Object/COFF.h"
Rui Ueyama1763c0d2015-12-08 18:39:55 +000033#include "llvm/Support/Endian.h"
Rui Ueyamae7378242015-12-04 23:11:05 +000034#include "llvm/Support/FileOutputBuffer.h"
Rui Ueyamabe939b32016-11-21 17:22:35 +000035#include "llvm/Support/ScopedPrinter.h"
Rui Ueyamae7378242015-12-04 23:11:05 +000036#include <memory>
37
Rui Ueyama1d99ab32016-09-15 22:24:51 +000038using namespace lld;
Rui Ueyama09e0b5f2016-11-12 00:00:51 +000039using namespace lld::coff;
Rui Ueyamae7378242015-12-04 23:11:05 +000040using namespace llvm;
Rui Ueyamabe939b32016-11-21 17:22:35 +000041using namespace llvm::codeview;
Rui Ueyama1763c0d2015-12-08 18:39:55 +000042using namespace llvm::support;
43using namespace llvm::support::endian;
Rui Ueyamae7378242015-12-04 23:11:05 +000044
Rui Ueyama09e0b5f2016-11-12 00:00:51 +000045using llvm::object::coff_section;
46
Rui Ueyamab28c6d42016-09-16 04:32:33 +000047static ExitOnError ExitOnErr;
48
Rui Ueyama09e0b5f2016-11-12 00:00:51 +000049// Returns a list of all SectionChunks.
50static std::vector<coff_section> getInputSections(SymbolTable *Symtab) {
51 std::vector<coff_section> V;
52 for (Chunk *C : Symtab->getChunks())
53 if (auto *SC = dyn_cast<SectionChunk>(C))
54 V.push_back(*SC->Header);
55 return V;
56}
57
Rui Ueyamabe939b32016-11-21 17:22:35 +000058static SectionChunk *findByName(std::vector<SectionChunk *> &Sections,
59 StringRef Name) {
60 for (SectionChunk *C : Sections)
61 if (C->getSectionName() == Name)
62 return C;
63 return nullptr;
64}
65
Rui Ueyama26186c72016-12-09 04:46:54 +000066static ArrayRef<uint8_t> getDebugT(ObjectFile *File) {
Rui Ueyamac5cb7372016-12-01 01:22:48 +000067 SectionChunk *Sec = findByName(File->getDebugChunks(), ".debug$T");
68 if (!Sec)
Rui Ueyama26186c72016-12-09 04:46:54 +000069 return {};
Rui Ueyamac5cb7372016-12-01 01:22:48 +000070
71 // First 4 bytes are section magic.
72 ArrayRef<uint8_t> Data = Sec->getContents();
73 if (Data.size() < 4)
74 fatal(".debug$T too short");
75 if (read32le(Data.data()) != COFF::DEBUG_SECTION_MAGIC)
76 fatal(".debug$T has an invalid magic");
Rui Ueyama26186c72016-12-09 04:46:54 +000077 return Data.slice(4);
78}
Rui Ueyamac5cb7372016-12-01 01:22:48 +000079
Rui Ueyama26186c72016-12-09 04:46:54 +000080static void dumpDebugT(ScopedPrinter &W, ObjectFile *File) {
81 ArrayRef<uint8_t> Data = getDebugT(File);
82 if (Data.empty())
83 return;
84
Zachary Turner629cb7d2017-01-11 23:24:22 +000085 TypeDatabase TDB;
86 TypeDumpVisitor TDV(TDB, &W, false);
87 CVTypeDumper TypeDumper(TDB);
88 if (auto EC = TypeDumper.dump(Data, TDV))
Rui Ueyamac5cb7372016-12-01 01:22:48 +000089 fatal(EC, "CVTypeDumper::dump failed");
90}
91
92static void dumpDebugS(ScopedPrinter &W, ObjectFile *File) {
93 SectionChunk *Sec = findByName(File->getDebugChunks(), ".debug$S");
94 if (!Sec)
95 return;
96
97 msf::ByteStream Stream(Sec->getContents());
98 CVSymbolArray Symbols;
99 msf::StreamReader Reader(Stream);
100 if (auto EC = Reader.readArray(Symbols, Reader.getLength()))
101 fatal(EC, "StreamReader.readArray<CVSymbolArray> failed");
102
Zachary Turner629cb7d2017-01-11 23:24:22 +0000103 TypeDatabase TDB;
104 CVSymbolDumper SymbolDumper(W, TDB, nullptr, false);
Rui Ueyamac5cb7372016-12-01 01:22:48 +0000105 if (auto EC = SymbolDumper.dump(Symbols))
106 fatal(EC, "CVSymbolDumper::dump failed");
107}
108
Rui Ueyamabe939b32016-11-21 17:22:35 +0000109// Dump CodeView debug info. This is for debugging.
110static void dumpCodeView(SymbolTable *Symtab) {
111 ScopedPrinter W(outs());
112
113 for (ObjectFile *File : Symtab->ObjectFiles) {
Rui Ueyamac5cb7372016-12-01 01:22:48 +0000114 dumpDebugT(W, File);
115 dumpDebugS(W, File);
Rui Ueyamabe939b32016-11-21 17:22:35 +0000116 }
117}
118
Rui Ueyama26186c72016-12-09 04:46:54 +0000119static void addTypeInfo(SymbolTable *Symtab,
120 pdb::TpiStreamBuilder &TpiBuilder) {
121 for (ObjectFile *File : Symtab->ObjectFiles) {
122 ArrayRef<uint8_t> Data = getDebugT(File);
123 if (Data.empty())
124 continue;
125
126 msf::ByteStream Stream(Data);
127 codeview::CVTypeArray Records;
128 msf::StreamReader Reader(Stream);
129 if (auto EC = Reader.readArray(Records, Reader.getLength()))
130 fatal(EC, "Reader.readArray failed");
131 for (const codeview::CVType &Rec : Records)
132 TpiBuilder.addTypeRecord(Rec);
133 }
134}
135
Rui Ueyama09e0b5f2016-11-12 00:00:51 +0000136// Creates a PDB file.
137void coff::createPDB(StringRef Path, SymbolTable *Symtab,
Saleem Abdulrasooldf8a13b2017-01-04 17:56:54 +0000138 ArrayRef<uint8_t> SectionTable,
139 const llvm::codeview::DebugInfo *DI) {
Rui Ueyamabe939b32016-11-21 17:22:35 +0000140 if (Config->DumpPdb)
141 dumpCodeView(Symtab);
142
Rui Ueyamab28c6d42016-09-16 04:32:33 +0000143 BumpPtrAllocator Alloc;
144 pdb::PDBFileBuilder Builder(Alloc);
Rui Ueyama12979542016-09-30 20:53:45 +0000145 ExitOnErr(Builder.initialize(4096)); // 4096 is blocksize
Rui Ueyama7f382992016-09-15 18:55:18 +0000146
Rui Ueyama8d3fb5d2016-10-05 22:08:58 +0000147 // Create streams in MSF for predefined streams, namely
148 // PDB, TPI, DBI and IPI.
149 for (int I = 0; I < (int)pdb::kSpecialStreamCount; ++I)
150 ExitOnErr(Builder.getMsfBuilder().addStream(0));
Rui Ueyama7f382992016-09-15 18:55:18 +0000151
Rui Ueyamabb542b32016-09-16 22:51:17 +0000152 // Add an Info stream.
153 auto &InfoBuilder = Builder.getInfoBuilder();
Saleem Abdulrasooldf8a13b2017-01-04 17:56:54 +0000154 InfoBuilder.setAge(DI->PDB70.Age);
155 InfoBuilder.setGuid(
156 *reinterpret_cast<const pdb::PDB_UniqueId *>(&DI->PDB70.Signature));
Rui Ueyamabb542b32016-09-16 22:51:17 +0000157 // Should be the current time, but set 0 for reproducibilty.
158 InfoBuilder.setSignature(0);
Rui Ueyamabb542b32016-09-16 22:51:17 +0000159 InfoBuilder.setVersion(pdb::PdbRaw_ImplVer::PdbImplVC70);
Rui Ueyama7f382992016-09-15 18:55:18 +0000160
Rui Ueyama1343fac2016-10-06 22:52:01 +0000161 // Add an empty DPI stream.
162 auto &DbiBuilder = Builder.getDbiBuilder();
163 DbiBuilder.setVersionHeader(pdb::PdbDbiV110);
164
Rui Ueyamab28c6d42016-09-16 04:32:33 +0000165 // Add an empty TPI stream.
166 auto &TpiBuilder = Builder.getTpiBuilder();
167 TpiBuilder.setVersionHeader(pdb::PdbTpiV80);
Rui Ueyama327705d2016-12-10 17:23:23 +0000168 if (Config->DebugPdb)
169 addTypeInfo(Symtab, TpiBuilder);
Rui Ueyama1763c0d2015-12-08 18:39:55 +0000170
Rui Ueyamad381c982016-10-05 21:37:25 +0000171 // Add an empty IPI stream.
172 auto &IpiBuilder = Builder.getIpiBuilder();
173 IpiBuilder.setVersionHeader(pdb::PdbTpiV80);
174
Rui Ueyama09e0b5f2016-11-12 00:00:51 +0000175 // Add Section Contributions.
Rui Ueyama09e0b5f2016-11-12 00:00:51 +0000176 std::vector<pdb::SectionContrib> Contribs =
Vitaly Buka4cf112c2016-11-14 20:21:41 +0000177 pdb::DbiStreamBuilder::createSectionContribs(getInputSections(Symtab));
Rui Ueyama09e0b5f2016-11-12 00:00:51 +0000178 DbiBuilder.setSectionContribs(Contribs);
179
Rui Ueyama20df4ec2016-10-31 21:09:21 +0000180 // Add Section Map stream.
181 ArrayRef<object::coff_section> Sections = {
Rui Ueyama6294a242016-11-04 17:41:29 +0000182 (const object::coff_section *)SectionTable.data(),
Rui Ueyama20df4ec2016-10-31 21:09:21 +0000183 SectionTable.size() / sizeof(object::coff_section)};
184 std::vector<pdb::SecMapEntry> SectionMap =
185 pdb::DbiStreamBuilder::createSectionMap(Sections);
186 DbiBuilder.setSectionMap(SectionMap);
187
Rui Ueyamac91f7162016-11-16 01:10:46 +0000188 ExitOnErr(DbiBuilder.addModuleInfo("", "* Linker *"));
189
Rui Ueyama9f66f822016-10-11 19:45:07 +0000190 // Add COFF section header stream.
191 ExitOnErr(
192 DbiBuilder.addDbgStream(pdb::DbgHeaderType::SectionHdr, SectionTable));
193
Rui Ueyama3e9d6bb2016-09-26 23:53:55 +0000194 // Write to a file.
Rui Ueyama5c82eea2016-09-30 20:39:04 +0000195 ExitOnErr(Builder.commit(Path));
Rui Ueyamae7378242015-12-04 23:11:05 +0000196}