blob: 89462da934546ea76493194b231c05e85c478327 [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"
Reid Kleckner44cdb102017-06-19 17:21:45 +000017#include "llvm/DebugInfo/CodeView/DebugSubsectionRecord.h"
Zachary Turner526f4f22017-05-19 19:26:58 +000018#include "llvm/DebugInfo/CodeView/LazyRandomTypeCollection.h"
Zachary Turner6708e0b2017-07-10 21:01:37 +000019#include "llvm/DebugInfo/CodeView/SymbolSerializer.h"
Reid Kleckner651db912017-07-18 00:21:25 +000020#include "llvm/DebugInfo/CodeView/TypeDeserializer.h"
Zachary Turner629cb7d2017-01-11 23:24:22 +000021#include "llvm/DebugInfo/CodeView/TypeDumpVisitor.h"
Reid Klecknerd0e6e242017-06-21 17:25:56 +000022#include "llvm/DebugInfo/CodeView/TypeIndexDiscovery.h"
Rui Ueyama52896622017-01-12 03:09:25 +000023#include "llvm/DebugInfo/CodeView/TypeStreamMerger.h"
24#include "llvm/DebugInfo/CodeView/TypeTableBuilder.h"
Rui Ueyamab28c6d42016-09-16 04:32:33 +000025#include "llvm/DebugInfo/MSF/MSFBuilder.h"
Rui Ueyama7f382992016-09-15 18:55:18 +000026#include "llvm/DebugInfo/MSF/MSFCommon.h"
Reid Kleckner651db912017-07-18 00:21:25 +000027#include "llvm/DebugInfo/PDB/GenericError.h"
Zachary Turner6708e0b2017-07-10 21:01:37 +000028#include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptorBuilder.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000029#include "llvm/DebugInfo/PDB/Native/DbiStream.h"
30#include "llvm/DebugInfo/PDB/Native/DbiStreamBuilder.h"
31#include "llvm/DebugInfo/PDB/Native/InfoStream.h"
32#include "llvm/DebugInfo/PDB/Native/InfoStreamBuilder.h"
Reid Kleckner651db912017-07-18 00:21:25 +000033#include "llvm/DebugInfo/PDB/Native/NativeSession.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000034#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
35#include "llvm/DebugInfo/PDB/Native/PDBFileBuilder.h"
Rafael Espindolaa0f30da2017-05-02 20:19:42 +000036#include "llvm/DebugInfo/PDB/Native/PDBStringTableBuilder.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000037#include "llvm/DebugInfo/PDB/Native/TpiStream.h"
38#include "llvm/DebugInfo/PDB/Native/TpiStreamBuilder.h"
Reid Kleckner651db912017-07-18 00:21:25 +000039#include "llvm/DebugInfo/PDB/PDB.h"
Rui Ueyama20df4ec2016-10-31 21:09:21 +000040#include "llvm/Object/COFF.h"
Zachary Turnerd9dc2822017-03-02 20:52:51 +000041#include "llvm/Support/BinaryByteStream.h"
Rui Ueyama1763c0d2015-12-08 18:39:55 +000042#include "llvm/Support/Endian.h"
Rui Ueyamae7378242015-12-04 23:11:05 +000043#include "llvm/Support/FileOutputBuffer.h"
Zachary Turner7b327d02017-02-16 23:35:45 +000044#include "llvm/Support/Path.h"
Rui Ueyamabe939b32016-11-21 17:22:35 +000045#include "llvm/Support/ScopedPrinter.h"
Rui Ueyamae7378242015-12-04 23:11:05 +000046#include <memory>
47
Rui Ueyama1d99ab32016-09-15 22:24:51 +000048using namespace lld;
Rui Ueyama09e0b5f2016-11-12 00:00:51 +000049using namespace lld::coff;
Rui Ueyamae7378242015-12-04 23:11:05 +000050using namespace llvm;
Rui Ueyamabe939b32016-11-21 17:22:35 +000051using namespace llvm::codeview;
Rui Ueyamae7378242015-12-04 23:11:05 +000052
Rui Ueyama09e0b5f2016-11-12 00:00:51 +000053using llvm::object::coff_section;
54
Rui Ueyamab28c6d42016-09-16 04:32:33 +000055static ExitOnError ExitOnErr;
56
Reid Kleckner0faf6d72017-07-14 00:14:58 +000057namespace {
Reid Kleckner651db912017-07-18 00:21:25 +000058/// Map from type index and item index in a type server PDB to the
59/// corresponding index in the destination PDB.
60struct CVIndexMap {
61 SmallVector<TypeIndex, 0> TPIMap;
62 SmallVector<TypeIndex, 0> IPIMap;
63 bool IsTypeServerMap = false;
64};
65
Reid Kleckner0faf6d72017-07-14 00:14:58 +000066class PDBLinker {
67public:
68 PDBLinker(SymbolTable *Symtab)
69 : Alloc(), Symtab(Symtab), Builder(Alloc), TypeTable(Alloc),
70 IDTable(Alloc) {}
71
72 /// Emit the basic PDB structure: initial streams, headers, etc.
73 void initialize(const llvm::codeview::DebugInfo *DI);
74
75 /// Link CodeView from each object file in the symbol table into the PDB.
76 void addObjectsToPDB();
77
78 /// Link CodeView from a single object file into the PDB.
79 void addObjectFile(ObjectFile *File);
80
Reid Kleckner651db912017-07-18 00:21:25 +000081 /// Produce a mapping from the type and item indices used in the object
82 /// file to those in the destination PDB.
83 ///
84 /// If the object file uses a type server PDB (compiled with /Zi), merge TPI
85 /// and IPI from the type server PDB and return a map for it. Each unique type
86 /// server PDB is merged at most once, so this may return an existing index
87 /// mapping.
88 ///
89 /// If the object does not use a type server PDB (compiled with /Z7), we merge
90 /// all the type and item records from the .debug$S stream and fill in the
91 /// caller-provided ObjectIndexMap.
92 const CVIndexMap &mergeDebugT(ObjectFile *File, CVIndexMap &ObjectIndexMap);
93
94 const CVIndexMap &maybeMergeTypeServerPDB(ObjectFile *File,
95 TypeServer2Record &TS);
Reid Kleckner0faf6d72017-07-14 00:14:58 +000096
97 /// Add the section map and section contributions to the PDB.
98 void addSections(ArrayRef<uint8_t> SectionTable);
99
100 /// Write the PDB to disk.
101 void commit();
102
103private:
104 BumpPtrAllocator Alloc;
105
106 SymbolTable *Symtab;
107
108 pdb::PDBFileBuilder Builder;
109
110 /// Type records that will go into the PDB TPI stream.
111 TypeTableBuilder TypeTable;
112
113 /// Item records that will go into the PDB IPI stream.
114 TypeTableBuilder IDTable;
115
116 /// PDBs use a single global string table for filenames in the file checksum
117 /// table.
118 DebugStringTableSubsection PDBStrTab;
119
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000120 llvm::SmallString<128> NativePath;
121
122 std::vector<pdb::SecMapEntry> SectionMap;
Reid Kleckner651db912017-07-18 00:21:25 +0000123
124 /// Type index mappings of type server PDBs that we've loaded so far.
125 std::map<GUID, CVIndexMap> TypeServerIndexMappings;
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000126};
127}
128
Rui Ueyama09e0b5f2016-11-12 00:00:51 +0000129// Returns a list of all SectionChunks.
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000130static void addSectionContribs(SymbolTable *Symtab,
131 pdb::DbiStreamBuilder &DbiBuilder) {
Rui Ueyama09e0b5f2016-11-12 00:00:51 +0000132 for (Chunk *C : Symtab->getChunks())
133 if (auto *SC = dyn_cast<SectionChunk>(C))
Reid Kleckner8cbdd0c2017-06-13 15:49:13 +0000134 DbiBuilder.addSectionContrib(SC->File->ModuleDBI, SC->Header);
Rui Ueyama09e0b5f2016-11-12 00:00:51 +0000135}
136
Rui Ueyamabe939b32016-11-21 17:22:35 +0000137static SectionChunk *findByName(std::vector<SectionChunk *> &Sections,
138 StringRef Name) {
139 for (SectionChunk *C : Sections)
140 if (C->getSectionName() == Name)
141 return C;
142 return nullptr;
143}
144
Reid Kleckner44cdb102017-06-19 17:21:45 +0000145static ArrayRef<uint8_t> consumeDebugMagic(ArrayRef<uint8_t> Data,
146 StringRef SecName) {
Rui Ueyamac5cb7372016-12-01 01:22:48 +0000147 // First 4 bytes are section magic.
Rui Ueyamac5cb7372016-12-01 01:22:48 +0000148 if (Data.size() < 4)
Rui Ueyama52896622017-01-12 03:09:25 +0000149 fatal(SecName + " too short");
Reid Kleckner44cdb102017-06-19 17:21:45 +0000150 if (support::endian::read32le(Data.data()) != COFF::DEBUG_SECTION_MAGIC)
Rui Ueyama52896622017-01-12 03:09:25 +0000151 fatal(SecName + " has an invalid magic");
Rui Ueyama26186c72016-12-09 04:46:54 +0000152 return Data.slice(4);
153}
Rui Ueyamac5cb7372016-12-01 01:22:48 +0000154
Reid Kleckner44cdb102017-06-19 17:21:45 +0000155static ArrayRef<uint8_t> getDebugSection(ObjectFile *File, StringRef SecName) {
156 if (SectionChunk *Sec = findByName(File->getDebugChunks(), SecName))
157 return consumeDebugMagic(Sec->getContents(), SecName);
158 return {};
159}
160
Reid Kleckner5d577522017-03-24 17:26:38 +0000161static void addTypeInfo(pdb::TpiStreamBuilder &TpiBuilder,
Reid Kleckner44cdb102017-06-19 17:21:45 +0000162 TypeTableBuilder &TypeTable) {
Reid Kleckner5d577522017-03-24 17:26:38 +0000163 // Start the TPI or IPI stream header.
164 TpiBuilder.setVersionHeader(pdb::PdbTpiV80);
Rui Ueyama52896622017-01-12 03:09:25 +0000165
Reid Kleckner5d577522017-03-24 17:26:38 +0000166 // Flatten the in memory type table.
Reid Kleckner5d577522017-03-24 17:26:38 +0000167 TypeTable.ForEachRecord([&](TypeIndex TI, ArrayRef<uint8_t> Rec) {
Reid Kleckner13fc4112017-04-04 00:56:34 +0000168 // FIXME: Hash types.
169 TpiBuilder.addTypeRecord(Rec, None);
Reid Kleckner5d577522017-03-24 17:26:38 +0000170 });
Reid Kleckner5d577522017-03-24 17:26:38 +0000171}
172
Reid Kleckner651db912017-07-18 00:21:25 +0000173static Optional<TypeServer2Record>
174maybeReadTypeServerRecord(CVTypeArray &Types) {
175 auto I = Types.begin();
176 if (I == Types.end())
177 return None;
178 const CVType &Type = *I;
179 if (Type.kind() != LF_TYPESERVER2)
180 return None;
181 TypeServer2Record TS;
182 if (auto EC = TypeDeserializer::deserializeAs(const_cast<CVType &>(Type), TS))
183 fatal(EC, "error reading type server record");
184 return std::move(TS);
185}
186
187const CVIndexMap &PDBLinker::mergeDebugT(ObjectFile *File,
188 CVIndexMap &ObjectIndexMap) {
Reid Kleckner44cdb102017-06-19 17:21:45 +0000189 ArrayRef<uint8_t> Data = getDebugSection(File, ".debug$T");
190 if (Data.empty())
Reid Kleckner651db912017-07-18 00:21:25 +0000191 return ObjectIndexMap;
Reid Kleckner6597c282017-07-13 20:12:23 +0000192
Reid Kleckner44cdb102017-06-19 17:21:45 +0000193 BinaryByteStream Stream(Data, support::little);
194 CVTypeArray Types;
195 BinaryStreamReader Reader(Stream);
Reid Kleckner44cdb102017-06-19 17:21:45 +0000196 if (auto EC = Reader.readArray(Types, Reader.getLength()))
197 fatal(EC, "Reader::readArray failed");
Reid Kleckner651db912017-07-18 00:21:25 +0000198
199 // Look through type servers. If we've already seen this type server, don't
200 // merge any type information.
201 if (Optional<TypeServer2Record> TS = maybeReadTypeServerRecord(Types))
202 return maybeMergeTypeServerPDB(File, *TS);
203
204 // This is a /Z7 object. Fill in the temporary, caller-provided
205 // ObjectIndexMap.
206 if (auto Err = mergeTypeAndIdRecords(IDTable, TypeTable,
207 ObjectIndexMap.TPIMap, Types))
208 fatal(Err, "codeview::mergeTypeAndIdRecords failed");
209 return ObjectIndexMap;
210}
211
212static Expected<std::unique_ptr<pdb::NativeSession>>
213tryToLoadPDB(const GUID &GuidFromObj, StringRef TSPath) {
214 std::unique_ptr<pdb::IPDBSession> ThisSession;
215 if (auto EC =
216 pdb::loadDataForPDB(pdb::PDB_ReaderType::Native, TSPath, ThisSession))
217 return std::move(EC);
218
219 std::unique_ptr<pdb::NativeSession> NS(
220 static_cast<pdb::NativeSession *>(ThisSession.release()));
221 pdb::PDBFile &File = NS->getPDBFile();
222 auto ExpectedInfo = File.getPDBInfoStream();
223 // All PDB Files should have an Info stream.
224 if (!ExpectedInfo)
225 return ExpectedInfo.takeError();
226
227 // Just because a file with a matching name was found and it was an actual
228 // PDB file doesn't mean it matches. For it to match the InfoStream's GUID
229 // must match the GUID specified in the TypeServer2 record.
230 if (ExpectedInfo->getGuid() != GuidFromObj)
231 return make_error<pdb::GenericError>(
232 pdb::generic_error_code::type_server_not_found, TSPath);
233
234 return std::move(NS);
235}
236
237const CVIndexMap &PDBLinker::maybeMergeTypeServerPDB(ObjectFile *File,
238 TypeServer2Record &TS) {
239 // First, check if we already loaded a PDB with this GUID. Return the type
240 // index mapping if we have it.
241 auto Insertion = TypeServerIndexMappings.insert({TS.getGuid(), CVIndexMap()});
242 CVIndexMap &IndexMap = Insertion.first->second;
243 if (!Insertion.second)
244 return IndexMap;
245
246 // Mark this map as a type server map.
247 IndexMap.IsTypeServerMap = true;
248
249 // Check for a PDB at:
250 // 1. The given file path
251 // 2. Next to the object file or archive file
252 auto ExpectedSession = tryToLoadPDB(TS.getGuid(), TS.getName());
253 if (!ExpectedSession) {
254 consumeError(ExpectedSession.takeError());
255 StringRef LocalPath =
256 !File->ParentName.empty() ? File->ParentName : File->getName();
257 SmallString<128> Path = sys::path::parent_path(LocalPath);
Reid Klecknerf8522d82017-07-18 00:33:53 +0000258 sys::path::append(
259 Path, sys::path::filename(TS.getName(), sys::path::Style::windows));
Reid Kleckner651db912017-07-18 00:21:25 +0000260 ExpectedSession = tryToLoadPDB(TS.getGuid(), Path);
261 }
262 if (auto E = ExpectedSession.takeError())
263 fatal(E, "Type server PDB was not found");
264
265 // Merge TPI first, because the IPI stream will reference type indices.
266 auto ExpectedTpi = (*ExpectedSession)->getPDBFile().getPDBTpiStream();
267 if (auto E = ExpectedTpi.takeError())
268 fatal(E, "Type server does not have TPI stream");
269 if (auto Err = mergeTypeRecords(TypeTable, IndexMap.TPIMap,
270 ExpectedTpi->typeArray()))
271 fatal(Err, "codeview::mergeTypeRecords failed");
272
273 // Merge IPI.
274 auto ExpectedIpi = (*ExpectedSession)->getPDBFile().getPDBIpiStream();
275 if (auto E = ExpectedIpi.takeError())
276 fatal(E, "Type server does not have TPI stream");
277 if (auto Err = mergeIdRecords(IDTable, IndexMap.TPIMap, IndexMap.IPIMap,
278 ExpectedIpi->typeArray()))
279 fatal(Err, "codeview::mergeIdRecords failed");
280
281 return IndexMap;
Reid Kleckner44cdb102017-06-19 17:21:45 +0000282}
283
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000284static bool remapTypeIndex(TypeIndex &TI, ArrayRef<TypeIndex> TypeIndexMap) {
285 if (TI.isSimple())
286 return true;
287 if (TI.toArrayIndex() >= TypeIndexMap.size())
288 return false;
289 TI = TypeIndexMap[TI.toArrayIndex()];
290 return true;
291}
292
Reid Kleckner03b5baf2017-07-12 18:49:43 +0000293static void remapTypesInSymbolRecord(ObjectFile *File,
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000294 MutableArrayRef<uint8_t> Contents,
Reid Kleckner651db912017-07-18 00:21:25 +0000295 const CVIndexMap &IndexMap,
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000296 ArrayRef<TiReference> TypeRefs) {
297 for (const TiReference &Ref : TypeRefs) {
298 unsigned ByteSize = Ref.Count * sizeof(TypeIndex);
Reid Kleckner03b5baf2017-07-12 18:49:43 +0000299 if (Contents.size() < Ref.Offset + ByteSize)
300 fatal("symbol record too short");
Reid Kleckner651db912017-07-18 00:21:25 +0000301
302 // This can be an item index or a type index. Choose the appropriate map.
303 ArrayRef<TypeIndex> TypeOrItemMap = IndexMap.TPIMap;
304 if (Ref.Kind == TiRefKind::IndexRef && IndexMap.IsTypeServerMap)
305 TypeOrItemMap = IndexMap.IPIMap;
306
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000307 MutableArrayRef<TypeIndex> TIs(
308 reinterpret_cast<TypeIndex *>(Contents.data() + Ref.Offset), Ref.Count);
Reid Kleckner03b5baf2017-07-12 18:49:43 +0000309 for (TypeIndex &TI : TIs) {
Reid Kleckner651db912017-07-18 00:21:25 +0000310 if (!remapTypeIndex(TI, TypeOrItemMap)) {
Reid Kleckner03b5baf2017-07-12 18:49:43 +0000311 TI = TypeIndex(SimpleTypeKind::NotTranslated);
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000312 log("ignoring symbol record in " + File->getName() +
313 " with bad type index 0x" + utohexstr(TI.getIndex()));
Reid Kleckner03b5baf2017-07-12 18:49:43 +0000314 continue;
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000315 }
Reid Kleckner03b5baf2017-07-12 18:49:43 +0000316 }
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000317 }
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000318}
319
320/// MSVC translates S_PROC_ID_END to S_END.
321uint16_t canonicalizeSymbolKind(SymbolKind Kind) {
322 if (Kind == SymbolKind::S_PROC_ID_END)
323 return SymbolKind::S_END;
324 return Kind;
325}
326
327/// Copy the symbol record. In a PDB, symbol records must be 4 byte aligned.
328/// The object file may not be aligned.
329static MutableArrayRef<uint8_t> copySymbolForPdb(const CVSymbol &Sym,
330 BumpPtrAllocator &Alloc) {
331 size_t Size = alignTo(Sym.length(), alignOf(CodeViewContainer::Pdb));
332 assert(Size >= 4 && "record too short");
333 assert(Size <= MaxRecordLength && "record too long");
334 void *Mem = Alloc.Allocate(Size, 4);
335
336 // Copy the symbol record and zero out any padding bytes.
337 MutableArrayRef<uint8_t> NewData(reinterpret_cast<uint8_t *>(Mem), Size);
338 memcpy(NewData.data(), Sym.data().data(), Sym.length());
339 memset(NewData.data() + Sym.length(), 0, Size - Sym.length());
340
341 // Update the record prefix length. It should point to the beginning of the
342 // next record. MSVC does some canonicalization of the record kind, so we do
343 // that as well.
344 auto *Prefix = reinterpret_cast<RecordPrefix *>(Mem);
345 Prefix->RecordKind = canonicalizeSymbolKind(Sym.kind());
346 Prefix->RecordLen = Size - 2;
347 return NewData;
348}
349
Reid Kleckner3f851922017-07-06 16:39:32 +0000350/// Return true if this symbol opens a scope. This implies that the symbol has
351/// "parent" and "end" fields, which contain the offset of the S_END or
352/// S_INLINESITE_END record.
353static bool symbolOpensScope(SymbolKind Kind) {
354 switch (Kind) {
355 case SymbolKind::S_GPROC32:
356 case SymbolKind::S_LPROC32:
357 case SymbolKind::S_LPROC32_ID:
358 case SymbolKind::S_GPROC32_ID:
359 case SymbolKind::S_BLOCK32:
360 case SymbolKind::S_SEPCODE:
361 case SymbolKind::S_THUNK32:
362 case SymbolKind::S_INLINESITE:
363 case SymbolKind::S_INLINESITE2:
364 return true;
365 default:
366 break;
367 }
368 return false;
369}
370
371static bool symbolEndsScope(SymbolKind Kind) {
372 switch (Kind) {
373 case SymbolKind::S_END:
374 case SymbolKind::S_PROC_ID_END:
375 case SymbolKind::S_INLINESITE_END:
376 return true;
377 default:
378 break;
379 }
380 return false;
381}
382
383struct ScopeRecord {
384 ulittle32_t PtrParent;
385 ulittle32_t PtrEnd;
386};
387
388struct SymbolScope {
389 ScopeRecord *OpeningRecord;
390 uint32_t ScopeOffset;
391};
392
393static void scopeStackOpen(SmallVectorImpl<SymbolScope> &Stack,
394 uint32_t CurOffset, CVSymbol &Sym) {
395 assert(symbolOpensScope(Sym.kind()));
396 SymbolScope S;
397 S.ScopeOffset = CurOffset;
398 S.OpeningRecord = const_cast<ScopeRecord *>(
399 reinterpret_cast<const ScopeRecord *>(Sym.content().data()));
400 S.OpeningRecord->PtrParent = Stack.empty() ? 0 : Stack.back().ScopeOffset;
401 Stack.push_back(S);
402}
403
404static void scopeStackClose(SmallVectorImpl<SymbolScope> &Stack,
405 uint32_t CurOffset, ObjectFile *File) {
406 if (Stack.empty()) {
407 warn("symbol scopes are not balanced in " + File->getName());
408 return;
409 }
410 SymbolScope S = Stack.pop_back_val();
411 S.OpeningRecord->PtrEnd = CurOffset;
412}
413
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000414static void mergeSymbolRecords(BumpPtrAllocator &Alloc, ObjectFile *File,
Reid Kleckner651db912017-07-18 00:21:25 +0000415 const CVIndexMap &IndexMap,
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000416 BinaryStreamRef SymData) {
417 // FIXME: Improve error recovery by warning and skipping records when
418 // possible.
419 CVSymbolArray Syms;
420 BinaryStreamReader Reader(SymData);
421 ExitOnErr(Reader.readArray(Syms, Reader.getLength()));
Reid Kleckner3f851922017-07-06 16:39:32 +0000422 SmallVector<SymbolScope, 4> Scopes;
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000423 for (const CVSymbol &Sym : Syms) {
424 // Discover type index references in the record. Skip it if we don't know
425 // where they are.
426 SmallVector<TiReference, 32> TypeRefs;
427 if (!discoverTypeIndices(Sym, TypeRefs)) {
428 log("ignoring unknown symbol record with kind 0x" + utohexstr(Sym.kind()));
429 continue;
430 }
431
432 // Copy the symbol record so we can mutate it.
433 MutableArrayRef<uint8_t> NewData = copySymbolForPdb(Sym, Alloc);
434
435 // Re-map all the type index references.
436 MutableArrayRef<uint8_t> Contents =
437 NewData.drop_front(sizeof(RecordPrefix));
Reid Kleckner651db912017-07-18 00:21:25 +0000438 remapTypesInSymbolRecord(File, Contents, IndexMap, TypeRefs);
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000439
Reid Kleckner3f851922017-07-06 16:39:32 +0000440 // Fill in "Parent" and "End" fields by maintaining a stack of scopes.
441 CVSymbol NewSym(Sym.kind(), NewData);
442 if (symbolOpensScope(Sym.kind()))
443 scopeStackOpen(Scopes, File->ModuleDBI->getNextSymbolOffset(), NewSym);
444 else if (symbolEndsScope(Sym.kind()))
445 scopeStackClose(Scopes, File->ModuleDBI->getNextSymbolOffset(), File);
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000446
447 // Add the symbol to the module.
Reid Kleckner3f851922017-07-06 16:39:32 +0000448 File->ModuleDBI->addSymbol(NewSym);
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000449 }
450}
451
Reid Kleckner44cdb102017-06-19 17:21:45 +0000452// Allocate memory for a .debug$S section and relocate it.
453static ArrayRef<uint8_t> relocateDebugChunk(BumpPtrAllocator &Alloc,
454 SectionChunk *DebugChunk) {
455 uint8_t *Buffer = Alloc.Allocate<uint8_t>(DebugChunk->getSize());
456 assert(DebugChunk->OutputSectionOff == 0 &&
457 "debug sections should not be in output sections");
458 DebugChunk->writeTo(Buffer);
459 return consumeDebugMagic(makeArrayRef(Buffer, DebugChunk->getSize()),
460 ".debug$S");
461}
462
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000463void PDBLinker::addObjectFile(ObjectFile *File) {
464 // Add a module descriptor for every object file. We need to put an absolute
465 // path to the object into the PDB. If this is a plain object, we make its
466 // path absolute. If it's an object in an archive, we make the archive path
467 // absolute.
468 bool InArchive = !File->ParentName.empty();
469 SmallString<128> Path = InArchive ? File->ParentName : File->getName();
470 sys::fs::make_absolute(Path);
471 sys::path::native(Path, sys::path::Style::windows);
472 StringRef Name = InArchive ? File->getName() : StringRef(Path);
Zachary Turner2897e032017-05-25 21:16:03 +0000473
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000474 File->ModuleDBI = &ExitOnErr(Builder.getDbiBuilder().addModuleInfo(Name));
475 File->ModuleDBI->setObjFileName(Path);
Reid Kleckner44cdb102017-06-19 17:21:45 +0000476
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000477 // Before we can process symbol substreams from .debug$S, we need to process
478 // type information, file checksums, and the string table. Add type info to
479 // the PDB first, so that we can get the map from object file type and item
480 // indices to PDB type and item indices.
Reid Kleckner651db912017-07-18 00:21:25 +0000481 CVIndexMap ObjectIndexMap;
482 const CVIndexMap &IndexMap = mergeDebugT(File, ObjectIndexMap);
Zachary Turner448dea42017-07-07 18:46:14 +0000483
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000484 // Now do all live .debug$S sections.
485 for (SectionChunk *DebugChunk : File->getDebugChunks()) {
486 if (!DebugChunk->isLive() || DebugChunk->getSectionName() != ".debug$S")
487 continue;
Reid Kleckner8cbdd0c2017-06-13 15:49:13 +0000488
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000489 ArrayRef<uint8_t> RelocatedDebugContents =
490 relocateDebugChunk(Alloc, DebugChunk);
491 if (RelocatedDebugContents.empty())
492 continue;
Reid Kleckner8cbdd0c2017-06-13 15:49:13 +0000493
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000494 DebugSubsectionArray Subsections;
495 BinaryStreamReader Reader(RelocatedDebugContents, support::little);
496 ExitOnErr(Reader.readArray(Subsections, RelocatedDebugContents.size()));
Rui Ueyama52896622017-01-12 03:09:25 +0000497
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000498 DebugStringTableSubsectionRef CVStrTab;
499 DebugChecksumsSubsectionRef Checksums;
500 for (const DebugSubsectionRecord &SS : Subsections) {
501 switch (SS.kind()) {
502 case DebugSubsectionKind::StringTable:
503 ExitOnErr(CVStrTab.initialize(SS.getRecordData()));
504 break;
505 case DebugSubsectionKind::FileChecksums:
506 ExitOnErr(Checksums.initialize(SS.getRecordData()));
507 break;
508 case DebugSubsectionKind::Lines:
509 // We can add the relocated line table directly to the PDB without
510 // modification because the file checksum offsets will stay the same.
511 File->ModuleDBI->addDebugSubsection(SS);
512 break;
513 case DebugSubsectionKind::Symbols:
Reid Kleckner651db912017-07-18 00:21:25 +0000514 mergeSymbolRecords(Alloc, File, IndexMap, SS.getRecordData());
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000515 break;
516 default:
517 // FIXME: Process the rest of the subsections.
518 break;
Reid Kleckner44cdb102017-06-19 17:21:45 +0000519 }
520 }
Rui Ueyama52896622017-01-12 03:09:25 +0000521
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000522 if (Checksums.valid()) {
523 // Make a new file checksum table that refers to offsets in the PDB-wide
524 // string table. Generally the string table subsection appears after the
525 // checksum table, so we have to do this after looping over all the
526 // subsections.
527 if (!CVStrTab.valid())
528 fatal(".debug$S sections must have both a string table subsection "
529 "and a checksum subsection table or neither");
530 auto NewChecksums = make_unique<DebugChecksumsSubsection>(PDBStrTab);
531 for (FileChecksumEntry &FC : Checksums) {
532 StringRef FileName = ExitOnErr(CVStrTab.getString(FC.FileNameOffset));
533 ExitOnErr(Builder.getDbiBuilder().addModuleSourceFile(*File->ModuleDBI,
534 FileName));
535 NewChecksums->addChecksum(FileName, FC.Kind, FC.Checksum);
536 }
537 File->ModuleDBI->addDebugSubsection(std::move(NewChecksums));
538 }
539 }
540}
541
542// Add all object files to the PDB. Merge .debug$T sections into IpiData and
543// TpiData.
544void PDBLinker::addObjectsToPDB() {
545 for (ObjectFile *File : Symtab->ObjectFiles)
546 addObjectFile(File);
547
548 Builder.getStringTableBuilder().setStrings(PDBStrTab);
Reid Kleckner44cdb102017-06-19 17:21:45 +0000549
Reid Kleckner5d577522017-03-24 17:26:38 +0000550 // Construct TPI stream contents.
Reid Kleckner13fc4112017-04-04 00:56:34 +0000551 addTypeInfo(Builder.getTpiBuilder(), TypeTable);
Reid Kleckner5d577522017-03-24 17:26:38 +0000552
553 // Construct IPI stream contents.
Reid Kleckner13fc4112017-04-04 00:56:34 +0000554 addTypeInfo(Builder.getIpiBuilder(), IDTable);
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000555
556 // Add public and symbol records stream.
557
558 // For now we don't actually write any thing useful to the publics stream, but
559 // the act of "getting" it also creates it lazily so that we write an empty
560 // stream.
561 (void)Builder.getPublicsBuilder();
Rui Ueyama52896622017-01-12 03:09:25 +0000562}
563
Zachary Turner6708e0b2017-07-10 21:01:37 +0000564static void addLinkerModuleSymbols(StringRef Path,
565 pdb::DbiModuleDescriptorBuilder &Mod,
566 BumpPtrAllocator &Allocator) {
567 codeview::SymbolSerializer Serializer(Allocator, CodeViewContainer::Pdb);
568 codeview::ObjNameSym ONS(SymbolRecordKind::ObjNameSym);
569 codeview::Compile3Sym CS(SymbolRecordKind::Compile3Sym);
570 codeview::EnvBlockSym EBS(SymbolRecordKind::EnvBlockSym);
571
572 ONS.Name = "* Linker *";
573 ONS.Signature = 0;
574
575 CS.Machine = Config->is64() ? CPUType::X64 : CPUType::Intel80386;
576 CS.Flags = CompileSym3Flags::None;
577 CS.VersionBackendBuild = 0;
578 CS.VersionBackendMajor = 0;
579 CS.VersionBackendMinor = 0;
580 CS.VersionBackendQFE = 0;
581 CS.VersionFrontendBuild = 0;
582 CS.VersionFrontendMajor = 0;
583 CS.VersionFrontendMinor = 0;
584 CS.VersionFrontendQFE = 0;
585 CS.Version = "LLVM Linker";
586 CS.setLanguage(SourceLanguage::Link);
587
588 ArrayRef<StringRef> Args = makeArrayRef(Config->Argv).drop_front();
589 std::string ArgStr = llvm::join(Args, " ");
590 EBS.Fields.push_back("cwd");
591 SmallString<64> cwd;
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000592 sys::fs::current_path(cwd);
Zachary Turner6708e0b2017-07-10 21:01:37 +0000593 EBS.Fields.push_back(cwd);
594 EBS.Fields.push_back("exe");
Zachary Turner77f23b92017-07-10 21:09:11 +0000595 EBS.Fields.push_back(Config->Argv[0]);
Zachary Turner6708e0b2017-07-10 21:01:37 +0000596 EBS.Fields.push_back("pdb");
597 EBS.Fields.push_back(Path);
598 EBS.Fields.push_back("cmd");
599 EBS.Fields.push_back(ArgStr);
600 Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(
601 ONS, Allocator, CodeViewContainer::Pdb));
602 Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(
603 CS, Allocator, CodeViewContainer::Pdb));
604 Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(
605 EBS, Allocator, CodeViewContainer::Pdb));
606}
607
Rui Ueyama09e0b5f2016-11-12 00:00:51 +0000608// Creates a PDB file.
Zachary Turner6708e0b2017-07-10 21:01:37 +0000609void coff::createPDB(SymbolTable *Symtab, ArrayRef<uint8_t> SectionTable,
Saleem Abdulrasooldf8a13b2017-01-04 17:56:54 +0000610 const llvm::codeview::DebugInfo *DI) {
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000611 PDBLinker PDB(Symtab);
612 PDB.initialize(DI);
613 PDB.addObjectsToPDB();
614 PDB.addSections(SectionTable);
615 PDB.commit();
616}
617
618void PDBLinker::initialize(const llvm::codeview::DebugInfo *DI) {
Rui Ueyama12979542016-09-30 20:53:45 +0000619 ExitOnErr(Builder.initialize(4096)); // 4096 is blocksize
Rui Ueyama7f382992016-09-15 18:55:18 +0000620
Rui Ueyama8d3fb5d2016-10-05 22:08:58 +0000621 // Create streams in MSF for predefined streams, namely
622 // PDB, TPI, DBI and IPI.
623 for (int I = 0; I < (int)pdb::kSpecialStreamCount; ++I)
624 ExitOnErr(Builder.getMsfBuilder().addStream(0));
Rui Ueyama7f382992016-09-15 18:55:18 +0000625
Rui Ueyamabb542b32016-09-16 22:51:17 +0000626 // Add an Info stream.
627 auto &InfoBuilder = Builder.getInfoBuilder();
Saleem Abdulrasool0acd6dd2017-02-07 04:28:02 +0000628 InfoBuilder.setAge(DI ? DI->PDB70.Age : 0);
629
Reid Kleckner67653ee2017-07-17 23:59:44 +0000630 GUID uuid{};
Saleem Abdulrasool0acd6dd2017-02-07 04:28:02 +0000631 if (DI)
632 memcpy(&uuid, &DI->PDB70.Signature, sizeof(uuid));
633 InfoBuilder.setGuid(uuid);
Zachary Turner3a11fdf2017-07-07 20:25:39 +0000634 InfoBuilder.setSignature(time(nullptr));
Rui Ueyamabb542b32016-09-16 22:51:17 +0000635 InfoBuilder.setVersion(pdb::PdbRaw_ImplVer::PdbImplVC70);
Rui Ueyama7f382992016-09-15 18:55:18 +0000636
Zachary Turner6c4bfba2017-07-07 05:04:36 +0000637 // Add an empty DBI stream.
Reid Kleckner8cbdd0c2017-06-13 15:49:13 +0000638 pdb::DbiStreamBuilder &DbiBuilder = Builder.getDbiBuilder();
Zachary Turnerc1e93e52017-07-07 18:45:56 +0000639 DbiBuilder.setVersionHeader(pdb::PdbDbiV70);
Zachary Turner3a11fdf2017-07-07 20:25:39 +0000640 ExitOnErr(DbiBuilder.addDbgStream(pdb::DbgHeaderType::NewFPO, {}));
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000641}
Rui Ueyama1343fac2016-10-06 22:52:01 +0000642
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000643void PDBLinker::addSections(ArrayRef<uint8_t> SectionTable) {
Rui Ueyama09e0b5f2016-11-12 00:00:51 +0000644 // Add Section Contributions.
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000645 pdb::DbiStreamBuilder &DbiBuilder = Builder.getDbiBuilder();
Reid Kleckner8cbdd0c2017-06-13 15:49:13 +0000646 addSectionContribs(Symtab, DbiBuilder);
Rui Ueyama09e0b5f2016-11-12 00:00:51 +0000647
Rui Ueyama20df4ec2016-10-31 21:09:21 +0000648 // Add Section Map stream.
649 ArrayRef<object::coff_section> Sections = {
Rui Ueyama6294a242016-11-04 17:41:29 +0000650 (const object::coff_section *)SectionTable.data(),
Rui Ueyama20df4ec2016-10-31 21:09:21 +0000651 SectionTable.size() / sizeof(object::coff_section)};
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000652 SectionMap = pdb::DbiStreamBuilder::createSectionMap(Sections);
Rui Ueyama20df4ec2016-10-31 21:09:21 +0000653 DbiBuilder.setSectionMap(SectionMap);
654
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000655 // It's not entirely clear what this is, but the * Linker * module uses it.
656 NativePath = Config->PDBPath;
657 sys::fs::make_absolute(NativePath);
658 sys::path::native(NativePath, sys::path::Style::windows);
659 uint32_t PdbFilePathNI = DbiBuilder.addECName(NativePath);
Zachary Turner6c4bfba2017-07-07 05:04:36 +0000660 auto &LinkerModule = ExitOnErr(DbiBuilder.addModuleInfo("* Linker *"));
661 LinkerModule.setPdbFilePathNI(PdbFilePathNI);
Zachary Turner6708e0b2017-07-10 21:01:37 +0000662 addLinkerModuleSymbols(NativePath, LinkerModule, Alloc);
Rui Ueyamac91f7162016-11-16 01:10:46 +0000663
Rui Ueyama9f66f822016-10-11 19:45:07 +0000664 // Add COFF section header stream.
665 ExitOnErr(
666 DbiBuilder.addDbgStream(pdb::DbgHeaderType::SectionHdr, SectionTable));
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000667}
Rui Ueyama9f66f822016-10-11 19:45:07 +0000668
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000669void PDBLinker::commit() {
Rui Ueyama3e9d6bb2016-09-26 23:53:55 +0000670 // Write to a file.
Zachary Turner6708e0b2017-07-10 21:01:37 +0000671 ExitOnErr(Builder.commit(Config->PDBPath));
Rui Ueyamae7378242015-12-04 23:11:05 +0000672}