blob: 60e69c1153bc28e97d06a2301daad81464dd2baa [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"
Peter Collingbourne75257bc2017-10-20 19:48:26 +000013#include "Driver.h"
Rui Ueyama09e0b5f2016-11-12 00:00:51 +000014#include "SymbolTable.h"
15#include "Symbols.h"
Reid Klecknereacdf042017-07-27 18:25:59 +000016#include "Writer.h"
Bob Haarmanb8a59c82017-10-25 22:28:38 +000017#include "lld/Common/ErrorHandler.h"
Zachary Turner727f1532018-01-17 19:16:26 +000018#include "lld/Common/Timer.h"
Saleem Abdulrasooldf8a13b2017-01-04 17:56:54 +000019#include "llvm/DebugInfo/CodeView/CVDebugRecord.h"
Reid Kleckner44cdb102017-06-19 17:21:45 +000020#include "llvm/DebugInfo/CodeView/DebugSubsectionRecord.h"
Zachary Turner0d07a8e2017-12-14 18:07:04 +000021#include "llvm/DebugInfo/CodeView/GlobalTypeTableBuilder.h"
Zachary Turner526f4f22017-05-19 19:26:58 +000022#include "llvm/DebugInfo/CodeView/LazyRandomTypeCollection.h"
Zachary Turnerca6dbf12017-11-30 18:39:50 +000023#include "llvm/DebugInfo/CodeView/MergingTypeTableBuilder.h"
Zachary Turneree9906d2017-08-11 19:00:03 +000024#include "llvm/DebugInfo/CodeView/RecordName.h"
Zachary Turner59e3ae82017-08-08 18:34:44 +000025#include "llvm/DebugInfo/CodeView/SymbolDeserializer.h"
Zachary Turner6708e0b2017-07-10 21:01:37 +000026#include "llvm/DebugInfo/CodeView/SymbolSerializer.h"
Reid Kleckner651db912017-07-18 00:21:25 +000027#include "llvm/DebugInfo/CodeView/TypeDeserializer.h"
Zachary Turner629cb7d2017-01-11 23:24:22 +000028#include "llvm/DebugInfo/CodeView/TypeDumpVisitor.h"
Reid Klecknerd0e6e242017-06-21 17:25:56 +000029#include "llvm/DebugInfo/CodeView/TypeIndexDiscovery.h"
Rui Ueyama52896622017-01-12 03:09:25 +000030#include "llvm/DebugInfo/CodeView/TypeStreamMerger.h"
Rui Ueyamab28c6d42016-09-16 04:32:33 +000031#include "llvm/DebugInfo/MSF/MSFBuilder.h"
Rui Ueyama7f382992016-09-15 18:55:18 +000032#include "llvm/DebugInfo/MSF/MSFCommon.h"
Reid Kleckner651db912017-07-18 00:21:25 +000033#include "llvm/DebugInfo/PDB/GenericError.h"
Zachary Turner6708e0b2017-07-10 21:01:37 +000034#include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptorBuilder.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000035#include "llvm/DebugInfo/PDB/Native/DbiStream.h"
36#include "llvm/DebugInfo/PDB/Native/DbiStreamBuilder.h"
Zachary Turner946204c2017-08-09 04:23:25 +000037#include "llvm/DebugInfo/PDB/Native/GSIStreamBuilder.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000038#include "llvm/DebugInfo/PDB/Native/InfoStream.h"
39#include "llvm/DebugInfo/PDB/Native/InfoStreamBuilder.h"
Reid Kleckner651db912017-07-18 00:21:25 +000040#include "llvm/DebugInfo/PDB/Native/NativeSession.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000041#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
42#include "llvm/DebugInfo/PDB/Native/PDBFileBuilder.h"
Rafael Espindolaa0f30da2017-05-02 20:19:42 +000043#include "llvm/DebugInfo/PDB/Native/PDBStringTableBuilder.h"
Reid Klecknerd91ca762017-07-19 17:26:07 +000044#include "llvm/DebugInfo/PDB/Native/TpiHashing.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000045#include "llvm/DebugInfo/PDB/Native/TpiStream.h"
46#include "llvm/DebugInfo/PDB/Native/TpiStreamBuilder.h"
Reid Kleckner651db912017-07-18 00:21:25 +000047#include "llvm/DebugInfo/PDB/PDB.h"
Rui Ueyama20df4ec2016-10-31 21:09:21 +000048#include "llvm/Object/COFF.h"
Zachary Turnerd9dc2822017-03-02 20:52:51 +000049#include "llvm/Support/BinaryByteStream.h"
Rui Ueyama1763c0d2015-12-08 18:39:55 +000050#include "llvm/Support/Endian.h"
Zachary Turner60478582018-01-05 19:12:40 +000051#include "llvm/Support/FormatVariadic.h"
Zachary Turner676386ff2017-08-07 20:23:45 +000052#include "llvm/Support/JamCRC.h"
Zachary Turner7b327d02017-02-16 23:35:45 +000053#include "llvm/Support/Path.h"
Rui Ueyamabe939b32016-11-21 17:22:35 +000054#include "llvm/Support/ScopedPrinter.h"
Rui Ueyamae7378242015-12-04 23:11:05 +000055#include <memory>
56
Rui Ueyama1d99ab32016-09-15 22:24:51 +000057using namespace lld;
Rui Ueyama09e0b5f2016-11-12 00:00:51 +000058using namespace lld::coff;
Rui Ueyamae7378242015-12-04 23:11:05 +000059using namespace llvm;
Rui Ueyamabe939b32016-11-21 17:22:35 +000060using namespace llvm::codeview;
Rui Ueyamae7378242015-12-04 23:11:05 +000061
Rui Ueyama09e0b5f2016-11-12 00:00:51 +000062using llvm::object::coff_section;
63
Rui Ueyamab28c6d42016-09-16 04:32:33 +000064static ExitOnError ExitOnErr;
65
Zachary Turner727f1532018-01-17 19:16:26 +000066static Timer TotalPdbLinkTimer("PDB Emission (Cumulative)", Timer::root());
67
68static Timer AddObjectsTimer("Add Objects", TotalPdbLinkTimer);
69static Timer TypeMergingTimer("Type Merging", AddObjectsTimer);
70static Timer SymbolMergingTimer("Symbol Merging", AddObjectsTimer);
71static Timer GlobalsLayoutTimer("Globals Stream Layout", TotalPdbLinkTimer);
72static Timer TpiStreamLayoutTimer("TPI Stream Layout", TotalPdbLinkTimer);
73static Timer DiskCommitTimer("Commit to Disk", TotalPdbLinkTimer);
74
Reid Kleckner0faf6d72017-07-14 00:14:58 +000075namespace {
Reid Kleckner651db912017-07-18 00:21:25 +000076/// Map from type index and item index in a type server PDB to the
77/// corresponding index in the destination PDB.
78struct CVIndexMap {
79 SmallVector<TypeIndex, 0> TPIMap;
80 SmallVector<TypeIndex, 0> IPIMap;
81 bool IsTypeServerMap = false;
82};
83
Reid Kleckner0faf6d72017-07-14 00:14:58 +000084class PDBLinker {
85public:
86 PDBLinker(SymbolTable *Symtab)
Sam Cleggea244bf2017-12-14 21:09:31 +000087 : Alloc(), Symtab(Symtab), Builder(Alloc), TypeTable(Alloc),
88 IDTable(Alloc), GlobalTypeTable(Alloc), GlobalIDTable(Alloc) {}
Reid Kleckner0faf6d72017-07-14 00:14:58 +000089
90 /// Emit the basic PDB structure: initial streams, headers, etc.
Zachary Turner024323c2017-08-15 21:31:41 +000091 void initialize(const llvm::codeview::DebugInfo &BuildId);
Reid Kleckner0faf6d72017-07-14 00:14:58 +000092
93 /// Link CodeView from each object file in the symbol table into the PDB.
94 void addObjectsToPDB();
95
96 /// Link CodeView from a single object file into the PDB.
Rui Ueyamae1b48e02017-07-26 23:05:24 +000097 void addObjFile(ObjFile *File);
Reid Kleckner0faf6d72017-07-14 00:14:58 +000098
Reid Kleckner651db912017-07-18 00:21:25 +000099 /// Produce a mapping from the type and item indices used in the object
100 /// file to those in the destination PDB.
101 ///
102 /// If the object file uses a type server PDB (compiled with /Zi), merge TPI
103 /// and IPI from the type server PDB and return a map for it. Each unique type
104 /// server PDB is merged at most once, so this may return an existing index
105 /// mapping.
106 ///
107 /// If the object does not use a type server PDB (compiled with /Z7), we merge
108 /// all the type and item records from the .debug$S stream and fill in the
109 /// caller-provided ObjectIndexMap.
Colden Cullenb9b6ed92018-01-31 17:48:04 +0000110 Expected<const CVIndexMap&> mergeDebugT(ObjFile *File,
111 CVIndexMap &ObjectIndexMap);
Reid Kleckner651db912017-07-18 00:21:25 +0000112
Colden Cullenb9b6ed92018-01-31 17:48:04 +0000113 Expected<const CVIndexMap&> maybeMergeTypeServerPDB(ObjFile *File,
114 TypeServer2Record &TS);
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000115
116 /// Add the section map and section contributions to the PDB.
Reid Kleckner175af4b2017-08-03 21:15:09 +0000117 void addSections(ArrayRef<OutputSection *> OutputSections,
118 ArrayRef<uint8_t> SectionTable);
119
120 void addSectionContrib(pdb::DbiModuleDescriptorBuilder &LinkerModule,
121 OutputSection *OS, Chunk *C);
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000122
123 /// Write the PDB to disk.
124 void commit();
125
126private:
127 BumpPtrAllocator Alloc;
128
129 SymbolTable *Symtab;
130
131 pdb::PDBFileBuilder Builder;
132
133 /// Type records that will go into the PDB TPI stream.
Zachary Turnerca6dbf12017-11-30 18:39:50 +0000134 MergingTypeTableBuilder TypeTable;
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000135
136 /// Item records that will go into the PDB IPI stream.
Zachary Turnerca6dbf12017-11-30 18:39:50 +0000137 MergingTypeTableBuilder IDTable;
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000138
Zachary Turner0d07a8e2017-12-14 18:07:04 +0000139 /// Type records that will go into the PDB TPI stream (for /DEBUG:GHASH)
140 GlobalTypeTableBuilder GlobalTypeTable;
141
142 /// Item records that will go into the PDB IPI stream (for /DEBUG:GHASH)
143 GlobalTypeTableBuilder GlobalIDTable;
144
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000145 /// PDBs use a single global string table for filenames in the file checksum
146 /// table.
147 DebugStringTableSubsection PDBStrTab;
148
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000149 llvm::SmallString<128> NativePath;
150
Zachary Turner3868cfd2018-02-28 18:09:18 +0000151 /// A list of other PDBs which are loaded during the linking process and which
152 /// we need to keep around since the linking operation may reference pointers
153 /// inside of these PDBs.
154 llvm::SmallVector<std::unique_ptr<pdb::NativeSession>, 2> LoadedPDBs;
155
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000156 std::vector<pdb::SecMapEntry> SectionMap;
Reid Kleckner651db912017-07-18 00:21:25 +0000157
158 /// Type index mappings of type server PDBs that we've loaded so far.
159 std::map<GUID, CVIndexMap> TypeServerIndexMappings;
Colden Cullenb9b6ed92018-01-31 17:48:04 +0000160
161 /// List of TypeServer PDBs which cannot be loaded.
162 /// Cached to prevent repeated load attempts.
163 std::set<GUID> MissingTypeServerPDBs;
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000164};
165}
166
Sam Clegg0fb6faa2017-12-08 01:09:21 +0000167static SectionChunk *findByName(ArrayRef<SectionChunk *> Sections,
Rui Ueyamabe939b32016-11-21 17:22:35 +0000168 StringRef Name) {
169 for (SectionChunk *C : Sections)
170 if (C->getSectionName() == Name)
171 return C;
172 return nullptr;
173}
174
Reid Kleckner44cdb102017-06-19 17:21:45 +0000175static ArrayRef<uint8_t> consumeDebugMagic(ArrayRef<uint8_t> Data,
176 StringRef SecName) {
Rui Ueyamac5cb7372016-12-01 01:22:48 +0000177 // First 4 bytes are section magic.
Rui Ueyamac5cb7372016-12-01 01:22:48 +0000178 if (Data.size() < 4)
Rui Ueyama52896622017-01-12 03:09:25 +0000179 fatal(SecName + " too short");
Reid Kleckner44cdb102017-06-19 17:21:45 +0000180 if (support::endian::read32le(Data.data()) != COFF::DEBUG_SECTION_MAGIC)
Rui Ueyama52896622017-01-12 03:09:25 +0000181 fatal(SecName + " has an invalid magic");
Rui Ueyama26186c72016-12-09 04:46:54 +0000182 return Data.slice(4);
183}
Rui Ueyamac5cb7372016-12-01 01:22:48 +0000184
Rui Ueyamae1b48e02017-07-26 23:05:24 +0000185static ArrayRef<uint8_t> getDebugSection(ObjFile *File, StringRef SecName) {
Reid Kleckner44cdb102017-06-19 17:21:45 +0000186 if (SectionChunk *Sec = findByName(File->getDebugChunks(), SecName))
187 return consumeDebugMagic(Sec->getContents(), SecName);
188 return {};
189}
190
Zachary Turner0d07a8e2017-12-14 18:07:04 +0000191// A COFF .debug$H section is currently a clang extension. This function checks
192// if a .debug$H section is in a format that we expect / understand, so that we
193// can ignore any sections which are coincidentally also named .debug$H but do
194// not contain a format we recognize.
195static bool canUseDebugH(ArrayRef<uint8_t> DebugH) {
196 if (DebugH.size() < sizeof(object::debug_h_header))
197 return false;
198 auto *Header =
199 reinterpret_cast<const object::debug_h_header *>(DebugH.data());
200 DebugH = DebugH.drop_front(sizeof(object::debug_h_header));
201 return Header->Magic == COFF::DEBUG_HASHES_SECTION_MAGIC &&
202 Header->Version == 0 &&
203 Header->HashAlgorithm == uint16_t(GlobalTypeHashAlg::SHA1) &&
204 (DebugH.size() % 20 == 0);
205}
206
207static Optional<ArrayRef<uint8_t>> getDebugH(ObjFile *File) {
208 SectionChunk *Sec = findByName(File->getDebugChunks(), ".debug$H");
209 if (!Sec)
210 return llvm::None;
211 ArrayRef<uint8_t> Contents = Sec->getContents();
212 if (!canUseDebugH(Contents))
213 return None;
214 return Contents;
215}
216
217static ArrayRef<GloballyHashedType>
218getHashesFromDebugH(ArrayRef<uint8_t> DebugH) {
219 assert(canUseDebugH(DebugH));
220
221 DebugH = DebugH.drop_front(sizeof(object::debug_h_header));
222 uint32_t Count = DebugH.size() / sizeof(GloballyHashedType);
223 return {reinterpret_cast<const GloballyHashedType *>(DebugH.data()), Count};
224}
225
Reid Kleckner5d577522017-03-24 17:26:38 +0000226static void addTypeInfo(pdb::TpiStreamBuilder &TpiBuilder,
Zachary Turner3e3936d2017-11-29 19:35:21 +0000227 TypeCollection &TypeTable) {
Reid Kleckner5d577522017-03-24 17:26:38 +0000228 // Start the TPI or IPI stream header.
229 TpiBuilder.setVersionHeader(pdb::PdbTpiV80);
Rui Ueyama52896622017-01-12 03:09:25 +0000230
Reid Klecknerd91ca762017-07-19 17:26:07 +0000231 // Flatten the in memory type table and hash each type.
Zachary Turner3e3936d2017-11-29 19:35:21 +0000232 TypeTable.ForEachRecord([&](TypeIndex TI, const CVType &Type) {
Reid Klecknerd91ca762017-07-19 17:26:07 +0000233 auto Hash = pdb::hashTypeRecord(Type);
234 if (auto E = Hash.takeError())
235 fatal("type hashing error");
Zachary Turner3e3936d2017-11-29 19:35:21 +0000236 TpiBuilder.addTypeRecord(Type.RecordData, *Hash);
Reid Kleckner5d577522017-03-24 17:26:38 +0000237 });
Reid Kleckner5d577522017-03-24 17:26:38 +0000238}
239
Reid Kleckner651db912017-07-18 00:21:25 +0000240static Optional<TypeServer2Record>
241maybeReadTypeServerRecord(CVTypeArray &Types) {
242 auto I = Types.begin();
243 if (I == Types.end())
244 return None;
245 const CVType &Type = *I;
246 if (Type.kind() != LF_TYPESERVER2)
247 return None;
248 TypeServer2Record TS;
249 if (auto EC = TypeDeserializer::deserializeAs(const_cast<CVType &>(Type), TS))
Bob Haarmanb8a59c82017-10-25 22:28:38 +0000250 fatal("error reading type server record: " + toString(std::move(EC)));
Reid Kleckner651db912017-07-18 00:21:25 +0000251 return std::move(TS);
252}
253
Colden Cullenb9b6ed92018-01-31 17:48:04 +0000254Expected<const CVIndexMap&> PDBLinker::mergeDebugT(ObjFile *File,
255 CVIndexMap &ObjectIndexMap) {
Zachary Turner727f1532018-01-17 19:16:26 +0000256 ScopedTimer T(TypeMergingTimer);
257
Reid Kleckner44cdb102017-06-19 17:21:45 +0000258 ArrayRef<uint8_t> Data = getDebugSection(File, ".debug$T");
259 if (Data.empty())
Reid Kleckner651db912017-07-18 00:21:25 +0000260 return ObjectIndexMap;
Reid Kleckner6597c282017-07-13 20:12:23 +0000261
Reid Kleckner44cdb102017-06-19 17:21:45 +0000262 BinaryByteStream Stream(Data, support::little);
263 CVTypeArray Types;
264 BinaryStreamReader Reader(Stream);
Reid Kleckner44cdb102017-06-19 17:21:45 +0000265 if (auto EC = Reader.readArray(Types, Reader.getLength()))
Bob Haarmanb8a59c82017-10-25 22:28:38 +0000266 fatal("Reader::readArray failed: " + toString(std::move(EC)));
Reid Kleckner651db912017-07-18 00:21:25 +0000267
268 // Look through type servers. If we've already seen this type server, don't
269 // merge any type information.
270 if (Optional<TypeServer2Record> TS = maybeReadTypeServerRecord(Types))
271 return maybeMergeTypeServerPDB(File, *TS);
272
273 // This is a /Z7 object. Fill in the temporary, caller-provided
274 // ObjectIndexMap.
Zachary Turner0d07a8e2017-12-14 18:07:04 +0000275 if (Config->DebugGHashes) {
276 ArrayRef<GloballyHashedType> Hashes;
277 std::vector<GloballyHashedType> OwnedHashes;
278 if (Optional<ArrayRef<uint8_t>> DebugH = getDebugH(File))
279 Hashes = getHashesFromDebugH(*DebugH);
280 else {
281 OwnedHashes = GloballyHashedType::hashTypes(Types);
282 Hashes = OwnedHashes;
283 }
284
285 if (auto Err = mergeTypeAndIdRecords(GlobalIDTable, GlobalTypeTable,
286 ObjectIndexMap.TPIMap, Types, Hashes))
287 fatal("codeview::mergeTypeAndIdRecords failed: " +
288 toString(std::move(Err)));
289 } else {
290 if (auto Err = mergeTypeAndIdRecords(IDTable, TypeTable,
291 ObjectIndexMap.TPIMap, Types))
292 fatal("codeview::mergeTypeAndIdRecords failed: " +
293 toString(std::move(Err)));
294 }
Reid Kleckner651db912017-07-18 00:21:25 +0000295 return ObjectIndexMap;
296}
297
298static Expected<std::unique_ptr<pdb::NativeSession>>
299tryToLoadPDB(const GUID &GuidFromObj, StringRef TSPath) {
Peter Collingbourne75257bc2017-10-20 19:48:26 +0000300 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile(
301 TSPath, /*FileSize=*/-1, /*RequiresNullTerminator=*/false);
302 if (!MBOrErr)
303 return errorCodeToError(MBOrErr.getError());
304
Reid Kleckner651db912017-07-18 00:21:25 +0000305 std::unique_ptr<pdb::IPDBSession> ThisSession;
Peter Collingbourne75257bc2017-10-20 19:48:26 +0000306 if (auto EC = pdb::NativeSession::createFromPdb(
307 MemoryBuffer::getMemBuffer(Driver->takeBuffer(std::move(*MBOrErr)),
308 /*RequiresNullTerminator=*/false),
309 ThisSession))
Reid Kleckner651db912017-07-18 00:21:25 +0000310 return std::move(EC);
311
312 std::unique_ptr<pdb::NativeSession> NS(
313 static_cast<pdb::NativeSession *>(ThisSession.release()));
314 pdb::PDBFile &File = NS->getPDBFile();
315 auto ExpectedInfo = File.getPDBInfoStream();
316 // All PDB Files should have an Info stream.
317 if (!ExpectedInfo)
318 return ExpectedInfo.takeError();
319
320 // Just because a file with a matching name was found and it was an actual
321 // PDB file doesn't mean it matches. For it to match the InfoStream's GUID
322 // must match the GUID specified in the TypeServer2 record.
323 if (ExpectedInfo->getGuid() != GuidFromObj)
324 return make_error<pdb::GenericError>(
325 pdb::generic_error_code::type_server_not_found, TSPath);
326
327 return std::move(NS);
328}
329
Colden Cullenb9b6ed92018-01-31 17:48:04 +0000330Expected<const CVIndexMap&> PDBLinker::maybeMergeTypeServerPDB(ObjFile *File,
331 TypeServer2Record &TS) {
332 const GUID& TSId = TS.getGuid();
333 StringRef TSPath = TS.getName();
334
335 // First, check if the PDB has previously failed to load.
336 if (MissingTypeServerPDBs.count(TSId))
337 return make_error<pdb::GenericError>(
338 pdb::generic_error_code::type_server_not_found, TSPath);
339
340 // Second, check if we already loaded a PDB with this GUID. Return the type
Reid Kleckner651db912017-07-18 00:21:25 +0000341 // index mapping if we have it.
Colden Cullenb9b6ed92018-01-31 17:48:04 +0000342 auto Insertion = TypeServerIndexMappings.insert({TSId, CVIndexMap()});
Reid Kleckner651db912017-07-18 00:21:25 +0000343 CVIndexMap &IndexMap = Insertion.first->second;
344 if (!Insertion.second)
345 return IndexMap;
346
347 // Mark this map as a type server map.
348 IndexMap.IsTypeServerMap = true;
349
350 // Check for a PDB at:
351 // 1. The given file path
352 // 2. Next to the object file or archive file
Colden Cullenb9b6ed92018-01-31 17:48:04 +0000353 auto ExpectedSession = tryToLoadPDB(TSId, TSPath);
Reid Kleckner651db912017-07-18 00:21:25 +0000354 if (!ExpectedSession) {
355 consumeError(ExpectedSession.takeError());
356 StringRef LocalPath =
357 !File->ParentName.empty() ? File->ParentName : File->getName();
358 SmallString<128> Path = sys::path::parent_path(LocalPath);
Reid Klecknerf8522d82017-07-18 00:33:53 +0000359 sys::path::append(
Colden Cullenb9b6ed92018-01-31 17:48:04 +0000360 Path, sys::path::filename(TSPath, sys::path::Style::windows));
361 ExpectedSession = tryToLoadPDB(TSId, Path);
Reid Kleckner651db912017-07-18 00:21:25 +0000362 }
Colden Cullenb9b6ed92018-01-31 17:48:04 +0000363 if (auto E = ExpectedSession.takeError()) {
364 TypeServerIndexMappings.erase(TSId);
365 MissingTypeServerPDBs.emplace(TSId);
366 return std::move(E);
367 }
Reid Kleckner651db912017-07-18 00:21:25 +0000368
Zachary Turner3868cfd2018-02-28 18:09:18 +0000369 pdb::NativeSession *Session = ExpectedSession->get();
370
371 // Keep a strong reference to this PDB, so that it's safe to hold pointers
372 // into the file.
373 LoadedPDBs.push_back(std::move(*ExpectedSession));
374
375 auto ExpectedTpi = Session->getPDBFile().getPDBTpiStream();
Reid Kleckner651db912017-07-18 00:21:25 +0000376 if (auto E = ExpectedTpi.takeError())
Bob Haarmanb8a59c82017-10-25 22:28:38 +0000377 fatal("Type server does not have TPI stream: " + toString(std::move(E)));
Zachary Turner3868cfd2018-02-28 18:09:18 +0000378 auto ExpectedIpi = Session->getPDBFile().getPDBIpiStream();
Reid Kleckner651db912017-07-18 00:21:25 +0000379 if (auto E = ExpectedIpi.takeError())
Bob Haarmanb8a59c82017-10-25 22:28:38 +0000380 fatal("Type server does not have TPI stream: " + toString(std::move(E)));
Zachary Turner0d07a8e2017-12-14 18:07:04 +0000381
382 if (Config->DebugGHashes) {
383 // PDBs do not actually store global hashes, so when merging a type server
384 // PDB we have to synthesize global hashes. To do this, we first synthesize
385 // global hashes for the TPI stream, since it is independent, then we
386 // synthesize hashes for the IPI stream, using the hashes for the TPI stream
387 // as inputs.
388 auto TpiHashes = GloballyHashedType::hashTypes(ExpectedTpi->typeArray());
389 auto IpiHashes =
390 GloballyHashedType::hashIds(ExpectedIpi->typeArray(), TpiHashes);
391
392 // Merge TPI first, because the IPI stream will reference type indices.
393 if (auto Err = mergeTypeRecords(GlobalTypeTable, IndexMap.TPIMap,
394 ExpectedTpi->typeArray(), TpiHashes))
395 fatal("codeview::mergeTypeRecords failed: " + toString(std::move(Err)));
396
397 // Merge IPI.
398 if (auto Err =
399 mergeIdRecords(GlobalIDTable, IndexMap.TPIMap, IndexMap.IPIMap,
400 ExpectedIpi->typeArray(), IpiHashes))
401 fatal("codeview::mergeIdRecords failed: " + toString(std::move(Err)));
402 } else {
403 // Merge TPI first, because the IPI stream will reference type indices.
404 if (auto Err = mergeTypeRecords(TypeTable, IndexMap.TPIMap,
405 ExpectedTpi->typeArray()))
406 fatal("codeview::mergeTypeRecords failed: " + toString(std::move(Err)));
407
408 // Merge IPI.
409 if (auto Err = mergeIdRecords(IDTable, IndexMap.TPIMap, IndexMap.IPIMap,
410 ExpectedIpi->typeArray()))
411 fatal("codeview::mergeIdRecords failed: " + toString(std::move(Err)));
412 }
Reid Kleckner651db912017-07-18 00:21:25 +0000413
414 return IndexMap;
Reid Kleckner44cdb102017-06-19 17:21:45 +0000415}
416
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000417static bool remapTypeIndex(TypeIndex &TI, ArrayRef<TypeIndex> TypeIndexMap) {
418 if (TI.isSimple())
419 return true;
420 if (TI.toArrayIndex() >= TypeIndexMap.size())
421 return false;
422 TI = TypeIndexMap[TI.toArrayIndex()];
423 return true;
424}
425
Reid Kleckner8aa32ff2017-10-24 17:02:40 +0000426static void remapTypesInSymbolRecord(ObjFile *File, SymbolKind SymKind,
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000427 MutableArrayRef<uint8_t> Contents,
Reid Kleckner651db912017-07-18 00:21:25 +0000428 const CVIndexMap &IndexMap,
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000429 ArrayRef<TiReference> TypeRefs) {
430 for (const TiReference &Ref : TypeRefs) {
431 unsigned ByteSize = Ref.Count * sizeof(TypeIndex);
Reid Kleckner03b5baf2017-07-12 18:49:43 +0000432 if (Contents.size() < Ref.Offset + ByteSize)
433 fatal("symbol record too short");
Reid Kleckner651db912017-07-18 00:21:25 +0000434
435 // This can be an item index or a type index. Choose the appropriate map.
436 ArrayRef<TypeIndex> TypeOrItemMap = IndexMap.TPIMap;
Reid Kleckner8aa32ff2017-10-24 17:02:40 +0000437 bool IsItemIndex = Ref.Kind == TiRefKind::IndexRef;
438 if (IsItemIndex && IndexMap.IsTypeServerMap)
Reid Kleckner651db912017-07-18 00:21:25 +0000439 TypeOrItemMap = IndexMap.IPIMap;
440
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000441 MutableArrayRef<TypeIndex> TIs(
442 reinterpret_cast<TypeIndex *>(Contents.data() + Ref.Offset), Ref.Count);
Reid Kleckner03b5baf2017-07-12 18:49:43 +0000443 for (TypeIndex &TI : TIs) {
Reid Kleckner651db912017-07-18 00:21:25 +0000444 if (!remapTypeIndex(TI, TypeOrItemMap)) {
Reid Kleckner8aa32ff2017-10-24 17:02:40 +0000445 log("ignoring symbol record of kind 0x" + utohexstr(SymKind) + " in " +
446 File->getName() + " with bad " + (IsItemIndex ? "item" : "type") +
447 " index 0x" + utohexstr(TI.getIndex()));
Reid Kleckner5d82f682017-10-23 22:44:51 +0000448 TI = TypeIndex(SimpleTypeKind::NotTranslated);
Reid Kleckner03b5baf2017-07-12 18:49:43 +0000449 continue;
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000450 }
Reid Kleckner03b5baf2017-07-12 18:49:43 +0000451 }
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000452 }
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000453}
454
Zachary Turner60478582018-01-05 19:12:40 +0000455static void
456recordStringTableReferenceAtOffset(MutableArrayRef<uint8_t> Contents,
457 uint32_t Offset,
458 std::vector<ulittle32_t *> &StrTableRefs) {
459 Contents =
460 Contents.drop_front(Offset).take_front(sizeof(support::ulittle32_t));
461 ulittle32_t *Index = reinterpret_cast<ulittle32_t *>(Contents.data());
462 StrTableRefs.push_back(Index);
463}
464
465static void
466recordStringTableReferences(SymbolKind Kind, MutableArrayRef<uint8_t> Contents,
467 std::vector<ulittle32_t *> &StrTableRefs) {
468 // For now we only handle S_FILESTATIC, but we may need the same logic for
469 // S_DEFRANGE and S_DEFRANGE_SUBFIELD. However, I cannot seem to generate any
470 // PDBs that contain these types of records, so because of the uncertainty
471 // they are omitted here until we can prove that it's necessary.
472 switch (Kind) {
473 case SymbolKind::S_FILESTATIC:
474 // FileStaticSym::ModFileOffset
475 recordStringTableReferenceAtOffset(Contents, 4, StrTableRefs);
476 break;
477 case SymbolKind::S_DEFRANGE:
478 case SymbolKind::S_DEFRANGE_SUBFIELD:
479 log("Not fixing up string table reference in S_DEFRANGE / "
480 "S_DEFRANGE_SUBFIELD record");
481 break;
Zachary Turner9e52e502018-01-05 19:28:39 +0000482 default:
483 break;
Zachary Turner60478582018-01-05 19:12:40 +0000484 }
485}
486
Zachary Turner59e3ae82017-08-08 18:34:44 +0000487static SymbolKind symbolKind(ArrayRef<uint8_t> RecordData) {
488 const RecordPrefix *Prefix =
489 reinterpret_cast<const RecordPrefix *>(RecordData.data());
490 return static_cast<SymbolKind>(uint16_t(Prefix->RecordKind));
491}
492
493/// MSVC translates S_PROC_ID_END to S_END, and S_[LG]PROC32_ID to S_[LG]PROC32
494static void translateIdSymbols(MutableArrayRef<uint8_t> &RecordData,
Zachary Turner3e3936d2017-11-29 19:35:21 +0000495 TypeCollection &IDTable) {
Zachary Turner59e3ae82017-08-08 18:34:44 +0000496 RecordPrefix *Prefix = reinterpret_cast<RecordPrefix *>(RecordData.data());
497
498 SymbolKind Kind = symbolKind(RecordData);
499
500 if (Kind == SymbolKind::S_PROC_ID_END) {
501 Prefix->RecordKind = SymbolKind::S_END;
502 return;
503 }
504
505 // In an object file, GPROC32_ID has an embedded reference which refers to the
506 // single object file type index namespace. This has already been translated
507 // to the PDB file's ID stream index space, but we need to convert this to a
508 // symbol that refers to the type stream index space. So we remap again from
509 // ID index space to type index space.
510 if (Kind == SymbolKind::S_GPROC32_ID || Kind == SymbolKind::S_LPROC32_ID) {
511 SmallVector<TiReference, 1> Refs;
512 auto Content = RecordData.drop_front(sizeof(RecordPrefix));
513 CVSymbol Sym(Kind, RecordData);
514 discoverTypeIndicesInSymbol(Sym, Refs);
515 assert(Refs.size() == 1);
516 assert(Refs.front().Count == 1);
517
518 TypeIndex *TI =
519 reinterpret_cast<TypeIndex *>(Content.data() + Refs[0].Offset);
520 // `TI` is the index of a FuncIdRecord or MemberFuncIdRecord which lives in
521 // the IPI stream, whose `FunctionType` member refers to the TPI stream.
522 // Note that LF_FUNC_ID and LF_MEMFUNC_ID have the same record layout, and
523 // in both cases we just need the second type index.
524 if (!TI->isSimple() && !TI->isNoneType()) {
Zachary Turner3e3936d2017-11-29 19:35:21 +0000525 CVType FuncIdData = IDTable.getType(*TI);
Zachary Turner59e3ae82017-08-08 18:34:44 +0000526 SmallVector<TypeIndex, 2> Indices;
527 discoverTypeIndices(FuncIdData, Indices);
528 assert(Indices.size() == 2);
529 *TI = Indices[1];
530 }
531
532 Kind = (Kind == SymbolKind::S_GPROC32_ID) ? SymbolKind::S_GPROC32
533 : SymbolKind::S_LPROC32;
534 Prefix->RecordKind = uint16_t(Kind);
535 }
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000536}
537
538/// Copy the symbol record. In a PDB, symbol records must be 4 byte aligned.
539/// The object file may not be aligned.
540static MutableArrayRef<uint8_t> copySymbolForPdb(const CVSymbol &Sym,
541 BumpPtrAllocator &Alloc) {
542 size_t Size = alignTo(Sym.length(), alignOf(CodeViewContainer::Pdb));
543 assert(Size >= 4 && "record too short");
544 assert(Size <= MaxRecordLength && "record too long");
545 void *Mem = Alloc.Allocate(Size, 4);
546
547 // Copy the symbol record and zero out any padding bytes.
548 MutableArrayRef<uint8_t> NewData(reinterpret_cast<uint8_t *>(Mem), Size);
549 memcpy(NewData.data(), Sym.data().data(), Sym.length());
550 memset(NewData.data() + Sym.length(), 0, Size - Sym.length());
551
552 // Update the record prefix length. It should point to the beginning of the
Zachary Turner59e3ae82017-08-08 18:34:44 +0000553 // next record.
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000554 auto *Prefix = reinterpret_cast<RecordPrefix *>(Mem);
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000555 Prefix->RecordLen = Size - 2;
556 return NewData;
557}
558
Reid Kleckner3f851922017-07-06 16:39:32 +0000559/// Return true if this symbol opens a scope. This implies that the symbol has
560/// "parent" and "end" fields, which contain the offset of the S_END or
561/// S_INLINESITE_END record.
562static bool symbolOpensScope(SymbolKind Kind) {
563 switch (Kind) {
564 case SymbolKind::S_GPROC32:
565 case SymbolKind::S_LPROC32:
566 case SymbolKind::S_LPROC32_ID:
567 case SymbolKind::S_GPROC32_ID:
568 case SymbolKind::S_BLOCK32:
569 case SymbolKind::S_SEPCODE:
570 case SymbolKind::S_THUNK32:
571 case SymbolKind::S_INLINESITE:
572 case SymbolKind::S_INLINESITE2:
573 return true;
574 default:
575 break;
576 }
577 return false;
578}
579
580static bool symbolEndsScope(SymbolKind Kind) {
581 switch (Kind) {
582 case SymbolKind::S_END:
583 case SymbolKind::S_PROC_ID_END:
584 case SymbolKind::S_INLINESITE_END:
585 return true;
586 default:
587 break;
588 }
589 return false;
590}
591
592struct ScopeRecord {
593 ulittle32_t PtrParent;
594 ulittle32_t PtrEnd;
595};
596
597struct SymbolScope {
598 ScopeRecord *OpeningRecord;
599 uint32_t ScopeOffset;
600};
601
602static void scopeStackOpen(SmallVectorImpl<SymbolScope> &Stack,
603 uint32_t CurOffset, CVSymbol &Sym) {
604 assert(symbolOpensScope(Sym.kind()));
605 SymbolScope S;
606 S.ScopeOffset = CurOffset;
607 S.OpeningRecord = const_cast<ScopeRecord *>(
608 reinterpret_cast<const ScopeRecord *>(Sym.content().data()));
609 S.OpeningRecord->PtrParent = Stack.empty() ? 0 : Stack.back().ScopeOffset;
610 Stack.push_back(S);
611}
612
613static void scopeStackClose(SmallVectorImpl<SymbolScope> &Stack,
Rui Ueyamae1b48e02017-07-26 23:05:24 +0000614 uint32_t CurOffset, ObjFile *File) {
Reid Kleckner3f851922017-07-06 16:39:32 +0000615 if (Stack.empty()) {
616 warn("symbol scopes are not balanced in " + File->getName());
617 return;
618 }
619 SymbolScope S = Stack.pop_back_val();
620 S.OpeningRecord->PtrEnd = CurOffset;
621}
622
Zachary Turneree9906d2017-08-11 19:00:03 +0000623static bool symbolGoesInModuleStream(const CVSymbol &Sym) {
624 switch (Sym.kind()) {
625 case SymbolKind::S_GDATA32:
626 case SymbolKind::S_CONSTANT:
627 case SymbolKind::S_UDT:
628 // We really should not be seeing S_PROCREF and S_LPROCREF in the first place
629 // since they are synthesized by the linker in response to S_GPROC32 and
630 // S_LPROC32, but if we do see them, don't put them in the module stream I
631 // guess.
632 case SymbolKind::S_PROCREF:
633 case SymbolKind::S_LPROCREF:
634 return false;
635 // S_GDATA32 does not go in the module stream, but S_LDATA32 does.
636 case SymbolKind::S_LDATA32:
637 default:
638 return true;
639 }
640}
641
642static bool symbolGoesInGlobalsStream(const CVSymbol &Sym) {
643 switch (Sym.kind()) {
644 case SymbolKind::S_CONSTANT:
Zachary Turneree9906d2017-08-11 19:00:03 +0000645 case SymbolKind::S_GDATA32:
646 // S_LDATA32 goes in both the module stream and the globals stream.
647 case SymbolKind::S_LDATA32:
648 case SymbolKind::S_GPROC32:
649 case SymbolKind::S_LPROC32:
650 // We really should not be seeing S_PROCREF and S_LPROCREF in the first place
651 // since they are synthesized by the linker in response to S_GPROC32 and
652 // S_LPROC32, but if we do see them, copy them straight through.
653 case SymbolKind::S_PROCREF:
654 case SymbolKind::S_LPROCREF:
655 return true;
Zachary Turner302dc8b2017-08-14 18:44:58 +0000656 // FIXME: For now, we drop all S_UDT symbols (i.e. they don't go in the
657 // globals stream or the modules stream). These have special handling which
658 // needs more investigation before we can get right, but by putting them all
659 // into the globals stream WinDbg fails to display local variables of class
660 // types saying that it cannot find the type Foo *. So as a stopgap just to
661 // keep things working, we drop them.
662 case SymbolKind::S_UDT:
Zachary Turneree9906d2017-08-11 19:00:03 +0000663 default:
664 return false;
665 }
666}
667
668static void addGlobalSymbol(pdb::GSIStreamBuilder &Builder, ObjFile &File,
669 const CVSymbol &Sym) {
670 switch (Sym.kind()) {
671 case SymbolKind::S_CONSTANT:
672 case SymbolKind::S_UDT:
673 case SymbolKind::S_GDATA32:
674 case SymbolKind::S_LDATA32:
675 case SymbolKind::S_PROCREF:
676 case SymbolKind::S_LPROCREF:
677 Builder.addGlobalSymbol(Sym);
678 break;
679 case SymbolKind::S_GPROC32:
680 case SymbolKind::S_LPROC32: {
681 SymbolRecordKind K = SymbolRecordKind::ProcRefSym;
682 if (Sym.kind() == SymbolKind::S_LPROC32)
683 K = SymbolRecordKind::LocalProcRef;
684 ProcRefSym PS(K);
685 PS.Module = static_cast<uint16_t>(File.ModuleDBI->getModuleIndex());
686 // For some reason, MSVC seems to add one to this value.
687 ++PS.Module;
688 PS.Name = getSymbolName(Sym);
689 PS.SumName = 0;
690 PS.SymOffset = File.ModuleDBI->getNextSymbolOffset();
691 Builder.addGlobalSymbol(PS);
692 break;
693 }
694 default:
695 llvm_unreachable("Invalid symbol kind!");
696 }
697}
698
Rui Ueyamae1b48e02017-07-26 23:05:24 +0000699static void mergeSymbolRecords(BumpPtrAllocator &Alloc, ObjFile *File,
Zachary Turneree9906d2017-08-11 19:00:03 +0000700 pdb::GSIStreamBuilder &GsiBuilder,
Reid Kleckner651db912017-07-18 00:21:25 +0000701 const CVIndexMap &IndexMap,
Zachary Turner3e3936d2017-11-29 19:35:21 +0000702 TypeCollection &IDTable,
Zachary Turner60478582018-01-05 19:12:40 +0000703 std::vector<ulittle32_t *> &StringTableRefs,
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000704 BinaryStreamRef SymData) {
705 // FIXME: Improve error recovery by warning and skipping records when
706 // possible.
Zachary Turner1bc2ce62018-01-18 18:35:01 +0000707 ArrayRef<uint8_t> SymsBuffer;
708 cantFail(SymData.readBytes(0, SymData.getLength(), SymsBuffer));
Reid Kleckner3f851922017-07-06 16:39:32 +0000709 SmallVector<SymbolScope, 4> Scopes;
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000710
Zachary Turner1bc2ce62018-01-18 18:35:01 +0000711 auto EC = forEachCodeViewRecord<CVSymbol>(
712 SymsBuffer, [&](const CVSymbol &Sym) -> llvm::Error {
713 // Discover type index references in the record. Skip it if we don't
714 // know where they are.
715 SmallVector<TiReference, 32> TypeRefs;
716 if (!discoverTypeIndicesInSymbol(Sym, TypeRefs)) {
717 log("ignoring unknown symbol record with kind 0x" +
718 utohexstr(Sym.kind()));
719 return Error::success();
720 }
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000721
Zachary Turner1bc2ce62018-01-18 18:35:01 +0000722 // Copy the symbol record so we can mutate it.
723 MutableArrayRef<uint8_t> NewData = copySymbolForPdb(Sym, Alloc);
Zachary Turner59e3ae82017-08-08 18:34:44 +0000724
Zachary Turner1bc2ce62018-01-18 18:35:01 +0000725 // Re-map all the type index references.
726 MutableArrayRef<uint8_t> Contents =
727 NewData.drop_front(sizeof(RecordPrefix));
728 remapTypesInSymbolRecord(File, Sym.kind(), Contents, IndexMap,
729 TypeRefs);
Zachary Turner59e3ae82017-08-08 18:34:44 +0000730
Zachary Turner1bc2ce62018-01-18 18:35:01 +0000731 // An object file may have S_xxx_ID symbols, but these get converted to
732 // "real" symbols in a PDB.
733 translateIdSymbols(NewData, IDTable);
Zachary Turner60478582018-01-05 19:12:40 +0000734
Zachary Turner1bc2ce62018-01-18 18:35:01 +0000735 // If this record refers to an offset in the object file's string table,
736 // add that item to the global PDB string table and re-write the index.
737 recordStringTableReferences(Sym.kind(), Contents, StringTableRefs);
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000738
Zachary Turner1bc2ce62018-01-18 18:35:01 +0000739 SymbolKind NewKind = symbolKind(NewData);
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000740
Zachary Turner1bc2ce62018-01-18 18:35:01 +0000741 // Fill in "Parent" and "End" fields by maintaining a stack of scopes.
742 CVSymbol NewSym(NewKind, NewData);
743 if (symbolOpensScope(NewKind))
744 scopeStackOpen(Scopes, File->ModuleDBI->getNextSymbolOffset(),
745 NewSym);
746 else if (symbolEndsScope(NewKind))
747 scopeStackClose(Scopes, File->ModuleDBI->getNextSymbolOffset(), File);
Zachary Turneree9906d2017-08-11 19:00:03 +0000748
Zachary Turner1bc2ce62018-01-18 18:35:01 +0000749 // Add the symbol to the globals stream if necessary. Do this before
750 // adding the symbol to the module since we may need to get the next
751 // symbol offset, and writing to the module's symbol stream will update
752 // that offset.
753 if (symbolGoesInGlobalsStream(NewSym))
754 addGlobalSymbol(GsiBuilder, *File, NewSym);
755
756 // Add the symbol to the module.
757 if (symbolGoesInModuleStream(NewSym))
758 File->ModuleDBI->addSymbol(NewSym);
759 return Error::success();
760 });
761 cantFail(std::move(EC));
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000762}
763
Reid Kleckner44cdb102017-06-19 17:21:45 +0000764// Allocate memory for a .debug$S section and relocate it.
765static ArrayRef<uint8_t> relocateDebugChunk(BumpPtrAllocator &Alloc,
766 SectionChunk *DebugChunk) {
767 uint8_t *Buffer = Alloc.Allocate<uint8_t>(DebugChunk->getSize());
768 assert(DebugChunk->OutputSectionOff == 0 &&
769 "debug sections should not be in output sections");
770 DebugChunk->writeTo(Buffer);
771 return consumeDebugMagic(makeArrayRef(Buffer, DebugChunk->getSize()),
772 ".debug$S");
773}
774
Rui Ueyamae1b48e02017-07-26 23:05:24 +0000775void PDBLinker::addObjFile(ObjFile *File) {
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000776 // Add a module descriptor for every object file. We need to put an absolute
777 // path to the object into the PDB. If this is a plain object, we make its
778 // path absolute. If it's an object in an archive, we make the archive path
779 // absolute.
780 bool InArchive = !File->ParentName.empty();
781 SmallString<128> Path = InArchive ? File->ParentName : File->getName();
782 sys::fs::make_absolute(Path);
783 sys::path::native(Path, sys::path::Style::windows);
784 StringRef Name = InArchive ? File->getName() : StringRef(Path);
Zachary Turner2897e032017-05-25 21:16:03 +0000785
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000786 File->ModuleDBI = &ExitOnErr(Builder.getDbiBuilder().addModuleInfo(Name));
787 File->ModuleDBI->setObjFileName(Path);
Reid Kleckner44cdb102017-06-19 17:21:45 +0000788
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000789 // Before we can process symbol substreams from .debug$S, we need to process
790 // type information, file checksums, and the string table. Add type info to
791 // the PDB first, so that we can get the map from object file type and item
792 // indices to PDB type and item indices.
Reid Kleckner651db912017-07-18 00:21:25 +0000793 CVIndexMap ObjectIndexMap;
Colden Cullenb9b6ed92018-01-31 17:48:04 +0000794 auto IndexMapResult = mergeDebugT(File, ObjectIndexMap);
795
796 // If the .debug$T sections fail to merge, assume there is no debug info.
797 if (!IndexMapResult) {
798 warn("Type server PDB for " + Name + " is invalid, ignoring debug info. " +
799 toString(IndexMapResult.takeError()));
800 return;
801 }
802
803 const CVIndexMap &IndexMap = *IndexMapResult;
Zachary Turner448dea42017-07-07 18:46:14 +0000804
Zachary Turner727f1532018-01-17 19:16:26 +0000805 ScopedTimer T(SymbolMergingTimer);
806
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000807 // Now do all live .debug$S sections.
Zachary Turner60478582018-01-05 19:12:40 +0000808 DebugStringTableSubsectionRef CVStrTab;
809 DebugChecksumsSubsectionRef Checksums;
810 std::vector<ulittle32_t *> StringTableReferences;
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000811 for (SectionChunk *DebugChunk : File->getDebugChunks()) {
812 if (!DebugChunk->isLive() || DebugChunk->getSectionName() != ".debug$S")
813 continue;
Reid Kleckner8cbdd0c2017-06-13 15:49:13 +0000814
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000815 ArrayRef<uint8_t> RelocatedDebugContents =
816 relocateDebugChunk(Alloc, DebugChunk);
817 if (RelocatedDebugContents.empty())
818 continue;
Reid Kleckner8cbdd0c2017-06-13 15:49:13 +0000819
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000820 DebugSubsectionArray Subsections;
821 BinaryStreamReader Reader(RelocatedDebugContents, support::little);
822 ExitOnErr(Reader.readArray(Subsections, RelocatedDebugContents.size()));
Rui Ueyama52896622017-01-12 03:09:25 +0000823
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000824 for (const DebugSubsectionRecord &SS : Subsections) {
825 switch (SS.kind()) {
Zachary Turner60478582018-01-05 19:12:40 +0000826 case DebugSubsectionKind::StringTable: {
827 auto Data = SS.getRecordData();
828 ArrayRef<uint8_t> Buffer;
829 cantFail(Data.readLongestContiguousChunk(0, Buffer));
830 assert(!CVStrTab.valid() &&
831 "Encountered multiple string table subsections!");
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000832 ExitOnErr(CVStrTab.initialize(SS.getRecordData()));
833 break;
Zachary Turner60478582018-01-05 19:12:40 +0000834 }
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000835 case DebugSubsectionKind::FileChecksums:
Zachary Turner60478582018-01-05 19:12:40 +0000836 assert(!Checksums.valid() &&
837 "Encountered multiple checksum subsections!");
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000838 ExitOnErr(Checksums.initialize(SS.getRecordData()));
839 break;
840 case DebugSubsectionKind::Lines:
841 // We can add the relocated line table directly to the PDB without
842 // modification because the file checksum offsets will stay the same.
843 File->ModuleDBI->addDebugSubsection(SS);
844 break;
845 case DebugSubsectionKind::Symbols:
Zachary Turner0d07a8e2017-12-14 18:07:04 +0000846 if (Config->DebugGHashes) {
847 mergeSymbolRecords(Alloc, File, Builder.getGsiBuilder(), IndexMap,
Zachary Turner60478582018-01-05 19:12:40 +0000848 GlobalIDTable, StringTableReferences,
849 SS.getRecordData());
Zachary Turner0d07a8e2017-12-14 18:07:04 +0000850 } else {
851 mergeSymbolRecords(Alloc, File, Builder.getGsiBuilder(), IndexMap,
Zachary Turner60478582018-01-05 19:12:40 +0000852 IDTable, StringTableReferences,
853 SS.getRecordData());
Zachary Turner0d07a8e2017-12-14 18:07:04 +0000854 }
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000855 break;
856 default:
857 // FIXME: Process the rest of the subsections.
858 break;
Reid Kleckner44cdb102017-06-19 17:21:45 +0000859 }
860 }
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000861 }
Zachary Turner60478582018-01-05 19:12:40 +0000862
863 // We should have seen all debug subsections across the entire object file now
864 // which means that if a StringTable subsection and Checksums subsection were
865 // present, now is the time to handle them.
866 if (!CVStrTab.valid()) {
867 if (Checksums.valid())
868 fatal(".debug$S sections with a checksums subsection must also contain a "
869 "string table subsection");
870
871 if (!StringTableReferences.empty())
872 warn("No StringTable subsection was encountered, but there are string "
873 "table references");
874 return;
875 }
876
877 // Rewrite each string table reference based on the value that the string
878 // assumes in the final PDB.
879 for (ulittle32_t *Ref : StringTableReferences) {
880 auto ExpectedString = CVStrTab.getString(*Ref);
881 if (!ExpectedString) {
882 warn("Invalid string table reference");
883 consumeError(ExpectedString.takeError());
884 continue;
885 }
886
887 *Ref = PDBStrTab.insert(*ExpectedString);
888 }
889
890 // Make a new file checksum table that refers to offsets in the PDB-wide
891 // string table. Generally the string table subsection appears after the
892 // checksum table, so we have to do this after looping over all the
893 // subsections.
894 auto NewChecksums = make_unique<DebugChecksumsSubsection>(PDBStrTab);
895 for (FileChecksumEntry &FC : Checksums) {
896 StringRef FileName = ExitOnErr(CVStrTab.getString(FC.FileNameOffset));
897 ExitOnErr(Builder.getDbiBuilder().addModuleSourceFile(*File->ModuleDBI,
898 FileName));
899 NewChecksums->addChecksum(FileName, FC.Kind, FC.Checksum);
900 }
901 File->ModuleDBI->addDebugSubsection(std::move(NewChecksums));
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000902}
903
Reid Klecknereacdf042017-07-27 18:25:59 +0000904static PublicSym32 createPublic(Defined *Def) {
905 PublicSym32 Pub(SymbolKind::S_PUB32);
906 Pub.Name = Def->getName();
907 if (auto *D = dyn_cast<DefinedCOFF>(Def)) {
908 if (D->getCOFFSymbol().isFunctionDefinition())
909 Pub.Flags = PublicSymFlags::Function;
910 } else if (isa<DefinedImportThunk>(Def)) {
911 Pub.Flags = PublicSymFlags::Function;
912 }
913
914 OutputSection *OS = Def->getChunk()->getOutputSection();
915 assert(OS && "all publics should be in final image");
916 Pub.Offset = Def->getRVA() - OS->getRVA();
917 Pub.Segment = OS->SectionIndex;
918 return Pub;
919}
920
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000921// Add all object files to the PDB. Merge .debug$T sections into IpiData and
922// TpiData.
923void PDBLinker::addObjectsToPDB() {
Zachary Turner727f1532018-01-17 19:16:26 +0000924 ScopedTimer T1(AddObjectsTimer);
Rui Ueyamaacd632d2017-07-27 00:45:26 +0000925 for (ObjFile *File : ObjFile::Instances)
Rui Ueyamae1b48e02017-07-26 23:05:24 +0000926 addObjFile(File);
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000927
928 Builder.getStringTableBuilder().setStrings(PDBStrTab);
Zachary Turner727f1532018-01-17 19:16:26 +0000929 T1.stop();
Reid Kleckner44cdb102017-06-19 17:21:45 +0000930
Zachary Turner0d07a8e2017-12-14 18:07:04 +0000931 // Construct TPI and IPI stream contents.
Zachary Turner727f1532018-01-17 19:16:26 +0000932 ScopedTimer T2(TpiStreamLayoutTimer);
Zachary Turner0d07a8e2017-12-14 18:07:04 +0000933 if (Config->DebugGHashes) {
934 addTypeInfo(Builder.getTpiBuilder(), GlobalTypeTable);
935 addTypeInfo(Builder.getIpiBuilder(), GlobalIDTable);
936 } else {
937 addTypeInfo(Builder.getTpiBuilder(), TypeTable);
938 addTypeInfo(Builder.getIpiBuilder(), IDTable);
939 }
Zachary Turner727f1532018-01-17 19:16:26 +0000940 T2.stop();
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000941
Zachary Turner727f1532018-01-17 19:16:26 +0000942 ScopedTimer T3(GlobalsLayoutTimer);
Zachary Turneree9906d2017-08-11 19:00:03 +0000943 // Compute the public and global symbols.
944 auto &GsiBuilder = Builder.getGsiBuilder();
Reid Klecknereacdf042017-07-27 18:25:59 +0000945 std::vector<PublicSym32> Publics;
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000946 Symtab->forEachSymbol([&Publics](Symbol *S) {
Reid Klecknereacdf042017-07-27 18:25:59 +0000947 // Only emit defined, live symbols that have a chunk.
Rui Ueyama616cd992017-10-31 16:10:24 +0000948 auto *Def = dyn_cast<Defined>(S);
Reid Klecknereacdf042017-07-27 18:25:59 +0000949 if (Def && Def->isLive() && Def->getChunk())
950 Publics.push_back(createPublic(Def));
951 });
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000952
Reid Klecknereacdf042017-07-27 18:25:59 +0000953 if (!Publics.empty()) {
954 // Sort the public symbols and add them to the stream.
955 std::sort(Publics.begin(), Publics.end(),
956 [](const PublicSym32 &L, const PublicSym32 &R) {
957 return L.Name < R.Name;
958 });
Reid Klecknereacdf042017-07-27 18:25:59 +0000959 for (const PublicSym32 &Pub : Publics)
Zachary Turner946204c2017-08-09 04:23:25 +0000960 GsiBuilder.addPublicSymbol(Pub);
Reid Klecknereacdf042017-07-27 18:25:59 +0000961 }
Rui Ueyama52896622017-01-12 03:09:25 +0000962}
963
Zachary Turner28e31ee2017-08-11 20:46:28 +0000964static void addCommonLinkerModuleSymbols(StringRef Path,
965 pdb::DbiModuleDescriptorBuilder &Mod,
966 BumpPtrAllocator &Allocator) {
Zachary Turner28e31ee2017-08-11 20:46:28 +0000967 ObjNameSym ONS(SymbolRecordKind::ObjNameSym);
968 Compile3Sym CS(SymbolRecordKind::Compile3Sym);
969 EnvBlockSym EBS(SymbolRecordKind::EnvBlockSym);
Zachary Turner6708e0b2017-07-10 21:01:37 +0000970
971 ONS.Name = "* Linker *";
972 ONS.Signature = 0;
973
974 CS.Machine = Config->is64() ? CPUType::X64 : CPUType::Intel80386;
Zachary Turnerb57884e2017-08-11 21:14:01 +0000975 // Interestingly, if we set the string to 0.0.0.0, then when trying to view
976 // local variables WinDbg emits an error that private symbols are not present.
977 // By setting this to a valid MSVC linker version string, local variables are
978 // displayed properly. As such, even though it is not representative of
Zachary Turnerf6822c82017-08-11 20:46:47 +0000979 // LLVM's version information, we need this for compatibility.
Zachary Turner6708e0b2017-07-10 21:01:37 +0000980 CS.Flags = CompileSym3Flags::None;
Zachary Turnerf6822c82017-08-11 20:46:47 +0000981 CS.VersionBackendBuild = 25019;
982 CS.VersionBackendMajor = 14;
983 CS.VersionBackendMinor = 10;
Zachary Turner6708e0b2017-07-10 21:01:37 +0000984 CS.VersionBackendQFE = 0;
Zachary Turnerf6822c82017-08-11 20:46:47 +0000985
986 // MSVC also sets the frontend to 0.0.0.0 since this is specifically for the
987 // linker module (which is by definition a backend), so we don't need to do
988 // anything here. Also, it seems we can use "LLVM Linker" for the linker name
989 // without any problems. Only the backend version has to be hardcoded to a
990 // magic number.
Zachary Turner6708e0b2017-07-10 21:01:37 +0000991 CS.VersionFrontendBuild = 0;
992 CS.VersionFrontendMajor = 0;
993 CS.VersionFrontendMinor = 0;
994 CS.VersionFrontendQFE = 0;
995 CS.Version = "LLVM Linker";
996 CS.setLanguage(SourceLanguage::Link);
997
998 ArrayRef<StringRef> Args = makeArrayRef(Config->Argv).drop_front();
999 std::string ArgStr = llvm::join(Args, " ");
1000 EBS.Fields.push_back("cwd");
1001 SmallString<64> cwd;
Reid Kleckner0faf6d72017-07-14 00:14:58 +00001002 sys::fs::current_path(cwd);
Zachary Turner6708e0b2017-07-10 21:01:37 +00001003 EBS.Fields.push_back(cwd);
1004 EBS.Fields.push_back("exe");
Zachary Turnerf6822c82017-08-11 20:46:47 +00001005 SmallString<64> exe = Config->Argv[0];
1006 llvm::sys::fs::make_absolute(exe);
1007 EBS.Fields.push_back(exe);
Zachary Turner6708e0b2017-07-10 21:01:37 +00001008 EBS.Fields.push_back("pdb");
1009 EBS.Fields.push_back(Path);
1010 EBS.Fields.push_back("cmd");
1011 EBS.Fields.push_back(ArgStr);
1012 Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(
1013 ONS, Allocator, CodeViewContainer::Pdb));
1014 Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(
1015 CS, Allocator, CodeViewContainer::Pdb));
1016 Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(
1017 EBS, Allocator, CodeViewContainer::Pdb));
1018}
1019
Zachary Turner28e31ee2017-08-11 20:46:28 +00001020static void addLinkerModuleSectionSymbol(pdb::DbiModuleDescriptorBuilder &Mod,
1021 OutputSection &OS,
1022 BumpPtrAllocator &Allocator) {
1023 SectionSym Sym(SymbolRecordKind::SectionSym);
Zachary Turnerf6822c82017-08-11 20:46:47 +00001024 Sym.Alignment = 12; // 2^12 = 4KB
Zachary Turner28e31ee2017-08-11 20:46:28 +00001025 Sym.Characteristics = OS.getCharacteristics();
1026 Sym.Length = OS.getVirtualSize();
1027 Sym.Name = OS.getName();
1028 Sym.Rva = OS.getRVA();
1029 Sym.SectionNumber = OS.SectionIndex;
1030 Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(
1031 Sym, Allocator, CodeViewContainer::Pdb));
1032}
1033
Rui Ueyama09e0b5f2016-11-12 00:00:51 +00001034// Creates a PDB file.
Reid Kleckner175af4b2017-08-03 21:15:09 +00001035void coff::createPDB(SymbolTable *Symtab,
1036 ArrayRef<OutputSection *> OutputSections,
1037 ArrayRef<uint8_t> SectionTable,
Zachary Turner024323c2017-08-15 21:31:41 +00001038 const llvm::codeview::DebugInfo &BuildId) {
Zachary Turner727f1532018-01-17 19:16:26 +00001039 ScopedTimer T1(TotalPdbLinkTimer);
Reid Kleckner0faf6d72017-07-14 00:14:58 +00001040 PDBLinker PDB(Symtab);
Zachary Turner024323c2017-08-15 21:31:41 +00001041 PDB.initialize(BuildId);
Reid Kleckner0faf6d72017-07-14 00:14:58 +00001042 PDB.addObjectsToPDB();
Reid Kleckner175af4b2017-08-03 21:15:09 +00001043 PDB.addSections(OutputSections, SectionTable);
Zachary Turner727f1532018-01-17 19:16:26 +00001044
1045 ScopedTimer T2(DiskCommitTimer);
Reid Kleckner0faf6d72017-07-14 00:14:58 +00001046 PDB.commit();
1047}
1048
Zachary Turner024323c2017-08-15 21:31:41 +00001049void PDBLinker::initialize(const llvm::codeview::DebugInfo &BuildId) {
Rui Ueyama12979542016-09-30 20:53:45 +00001050 ExitOnErr(Builder.initialize(4096)); // 4096 is blocksize
Rui Ueyama7f382992016-09-15 18:55:18 +00001051
Rui Ueyama8d3fb5d2016-10-05 22:08:58 +00001052 // Create streams in MSF for predefined streams, namely
1053 // PDB, TPI, DBI and IPI.
1054 for (int I = 0; I < (int)pdb::kSpecialStreamCount; ++I)
1055 ExitOnErr(Builder.getMsfBuilder().addStream(0));
Rui Ueyama7f382992016-09-15 18:55:18 +00001056
Rui Ueyamabb542b32016-09-16 22:51:17 +00001057 // Add an Info stream.
1058 auto &InfoBuilder = Builder.getInfoBuilder();
Zachary Turner024323c2017-08-15 21:31:41 +00001059 InfoBuilder.setAge(BuildId.PDB70.Age);
Saleem Abdulrasool0acd6dd2017-02-07 04:28:02 +00001060
Zachary Turner024323c2017-08-15 21:31:41 +00001061 GUID uuid;
1062 memcpy(&uuid, &BuildId.PDB70.Signature, sizeof(uuid));
Saleem Abdulrasool0acd6dd2017-02-07 04:28:02 +00001063 InfoBuilder.setGuid(uuid);
Zachary Turner3a11fdf2017-07-07 20:25:39 +00001064 InfoBuilder.setSignature(time(nullptr));
Rui Ueyamabb542b32016-09-16 22:51:17 +00001065 InfoBuilder.setVersion(pdb::PdbRaw_ImplVer::PdbImplVC70);
Rui Ueyama7f382992016-09-15 18:55:18 +00001066
Zachary Turner6c4bfba2017-07-07 05:04:36 +00001067 // Add an empty DBI stream.
Reid Kleckner8cbdd0c2017-06-13 15:49:13 +00001068 pdb::DbiStreamBuilder &DbiBuilder = Builder.getDbiBuilder();
Zachary Turner024323c2017-08-15 21:31:41 +00001069 DbiBuilder.setAge(BuildId.PDB70.Age);
Zachary Turnerc1e93e52017-07-07 18:45:56 +00001070 DbiBuilder.setVersionHeader(pdb::PdbDbiV70);
Zachary Turner3a11fdf2017-07-07 20:25:39 +00001071 ExitOnErr(DbiBuilder.addDbgStream(pdb::DbgHeaderType::NewFPO, {}));
Reid Kleckner0faf6d72017-07-14 00:14:58 +00001072}
Rui Ueyama1343fac2016-10-06 22:52:01 +00001073
Reid Kleckner175af4b2017-08-03 21:15:09 +00001074void PDBLinker::addSectionContrib(pdb::DbiModuleDescriptorBuilder &LinkerModule,
1075 OutputSection *OS, Chunk *C) {
1076 pdb::SectionContrib SC;
1077 memset(&SC, 0, sizeof(SC));
1078 SC.ISect = OS->SectionIndex;
1079 SC.Off = C->getRVA() - OS->getRVA();
1080 SC.Size = C->getSize();
1081 if (auto *SecChunk = dyn_cast<SectionChunk>(C)) {
1082 SC.Characteristics = SecChunk->Header->Characteristics;
1083 SC.Imod = SecChunk->File->ModuleDBI->getModuleIndex();
Zachary Turner676386ff2017-08-07 20:23:45 +00001084 ArrayRef<uint8_t> Contents = SecChunk->getContents();
1085 JamCRC CRC(0);
1086 ArrayRef<char> CharContents = makeArrayRef(
1087 reinterpret_cast<const char *>(Contents.data()), Contents.size());
1088 CRC.update(CharContents);
1089 SC.DataCrc = CRC.getCRC();
Reid Kleckner175af4b2017-08-03 21:15:09 +00001090 } else {
1091 SC.Characteristics = OS->getCharacteristics();
1092 // FIXME: When we start creating DBI for import libraries, use those here.
1093 SC.Imod = LinkerModule.getModuleIndex();
1094 }
Reid Kleckner175af4b2017-08-03 21:15:09 +00001095 SC.RelocCrc = 0; // FIXME
1096 Builder.getDbiBuilder().addSectionContrib(SC);
1097}
Rui Ueyama09e0b5f2016-11-12 00:00:51 +00001098
Reid Kleckner175af4b2017-08-03 21:15:09 +00001099void PDBLinker::addSections(ArrayRef<OutputSection *> OutputSections,
1100 ArrayRef<uint8_t> SectionTable) {
Reid Kleckner0faf6d72017-07-14 00:14:58 +00001101 // It's not entirely clear what this is, but the * Linker * module uses it.
Reid Kleckner175af4b2017-08-03 21:15:09 +00001102 pdb::DbiStreamBuilder &DbiBuilder = Builder.getDbiBuilder();
Reid Kleckner0faf6d72017-07-14 00:14:58 +00001103 NativePath = Config->PDBPath;
1104 sys::fs::make_absolute(NativePath);
1105 sys::path::native(NativePath, sys::path::Style::windows);
1106 uint32_t PdbFilePathNI = DbiBuilder.addECName(NativePath);
Zachary Turner6c4bfba2017-07-07 05:04:36 +00001107 auto &LinkerModule = ExitOnErr(DbiBuilder.addModuleInfo("* Linker *"));
1108 LinkerModule.setPdbFilePathNI(PdbFilePathNI);
Zachary Turner28e31ee2017-08-11 20:46:28 +00001109 addCommonLinkerModuleSymbols(NativePath, LinkerModule, Alloc);
Rui Ueyamac91f7162016-11-16 01:10:46 +00001110
Reid Kleckner175af4b2017-08-03 21:15:09 +00001111 // Add section contributions. They must be ordered by ascending RVA.
Zachary Turner28e31ee2017-08-11 20:46:28 +00001112 for (OutputSection *OS : OutputSections) {
1113 addLinkerModuleSectionSymbol(LinkerModule, *OS, Alloc);
Reid Kleckner175af4b2017-08-03 21:15:09 +00001114 for (Chunk *C : OS->getChunks())
1115 addSectionContrib(LinkerModule, OS, C);
Zachary Turner28e31ee2017-08-11 20:46:28 +00001116 }
Reid Kleckner175af4b2017-08-03 21:15:09 +00001117
1118 // Add Section Map stream.
1119 ArrayRef<object::coff_section> Sections = {
1120 (const object::coff_section *)SectionTable.data(),
1121 SectionTable.size() / sizeof(object::coff_section)};
1122 SectionMap = pdb::DbiStreamBuilder::createSectionMap(Sections);
1123 DbiBuilder.setSectionMap(SectionMap);
1124
Rui Ueyama9f66f822016-10-11 19:45:07 +00001125 // Add COFF section header stream.
1126 ExitOnErr(
1127 DbiBuilder.addDbgStream(pdb::DbgHeaderType::SectionHdr, SectionTable));
Reid Kleckner0faf6d72017-07-14 00:14:58 +00001128}
Rui Ueyama9f66f822016-10-11 19:45:07 +00001129
Reid Kleckner0faf6d72017-07-14 00:14:58 +00001130void PDBLinker::commit() {
Rui Ueyama3e9d6bb2016-09-26 23:53:55 +00001131 // Write to a file.
Zachary Turner6708e0b2017-07-10 21:01:37 +00001132 ExitOnErr(Builder.commit(Config->PDBPath));
Rui Ueyamae7378242015-12-04 23:11:05 +00001133}