blob: 5f230566fe880ce8da6308f3b583aa9f2056d90b [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"
Saleem Abdulrasooldf8a13b2017-01-04 17:56:54 +000018#include "llvm/DebugInfo/CodeView/CVDebugRecord.h"
Reid Kleckner44cdb102017-06-19 17:21:45 +000019#include "llvm/DebugInfo/CodeView/DebugSubsectionRecord.h"
Zachary Turner0d07a8e2017-12-14 18:07:04 +000020#include "llvm/DebugInfo/CodeView/GlobalTypeTableBuilder.h"
Zachary Turner526f4f22017-05-19 19:26:58 +000021#include "llvm/DebugInfo/CodeView/LazyRandomTypeCollection.h"
Zachary Turnerca6dbf12017-11-30 18:39:50 +000022#include "llvm/DebugInfo/CodeView/MergingTypeTableBuilder.h"
Zachary Turneree9906d2017-08-11 19:00:03 +000023#include "llvm/DebugInfo/CodeView/RecordName.h"
Zachary Turner59e3ae82017-08-08 18:34:44 +000024#include "llvm/DebugInfo/CodeView/SymbolDeserializer.h"
Zachary Turner6708e0b2017-07-10 21:01:37 +000025#include "llvm/DebugInfo/CodeView/SymbolSerializer.h"
Reid Kleckner651db912017-07-18 00:21:25 +000026#include "llvm/DebugInfo/CodeView/TypeDeserializer.h"
Zachary Turner629cb7d2017-01-11 23:24:22 +000027#include "llvm/DebugInfo/CodeView/TypeDumpVisitor.h"
Reid Klecknerd0e6e242017-06-21 17:25:56 +000028#include "llvm/DebugInfo/CodeView/TypeIndexDiscovery.h"
Rui Ueyama52896622017-01-12 03:09:25 +000029#include "llvm/DebugInfo/CodeView/TypeStreamMerger.h"
Rui Ueyamab28c6d42016-09-16 04:32:33 +000030#include "llvm/DebugInfo/MSF/MSFBuilder.h"
Rui Ueyama7f382992016-09-15 18:55:18 +000031#include "llvm/DebugInfo/MSF/MSFCommon.h"
Reid Kleckner651db912017-07-18 00:21:25 +000032#include "llvm/DebugInfo/PDB/GenericError.h"
Zachary Turner6708e0b2017-07-10 21:01:37 +000033#include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptorBuilder.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000034#include "llvm/DebugInfo/PDB/Native/DbiStream.h"
35#include "llvm/DebugInfo/PDB/Native/DbiStreamBuilder.h"
Zachary Turner946204c2017-08-09 04:23:25 +000036#include "llvm/DebugInfo/PDB/Native/GSIStreamBuilder.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000037#include "llvm/DebugInfo/PDB/Native/InfoStream.h"
38#include "llvm/DebugInfo/PDB/Native/InfoStreamBuilder.h"
Reid Kleckner651db912017-07-18 00:21:25 +000039#include "llvm/DebugInfo/PDB/Native/NativeSession.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000040#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
41#include "llvm/DebugInfo/PDB/Native/PDBFileBuilder.h"
Rafael Espindolaa0f30da2017-05-02 20:19:42 +000042#include "llvm/DebugInfo/PDB/Native/PDBStringTableBuilder.h"
Reid Klecknerd91ca762017-07-19 17:26:07 +000043#include "llvm/DebugInfo/PDB/Native/TpiHashing.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000044#include "llvm/DebugInfo/PDB/Native/TpiStream.h"
45#include "llvm/DebugInfo/PDB/Native/TpiStreamBuilder.h"
Reid Kleckner651db912017-07-18 00:21:25 +000046#include "llvm/DebugInfo/PDB/PDB.h"
Rui Ueyama20df4ec2016-10-31 21:09:21 +000047#include "llvm/Object/COFF.h"
Zachary Turnerd9dc2822017-03-02 20:52:51 +000048#include "llvm/Support/BinaryByteStream.h"
Rui Ueyama1763c0d2015-12-08 18:39:55 +000049#include "llvm/Support/Endian.h"
Zachary Turner60478582018-01-05 19:12:40 +000050#include "llvm/Support/FormatVariadic.h"
Zachary Turner676386ff2017-08-07 20:23:45 +000051#include "llvm/Support/JamCRC.h"
Zachary Turner7b327d02017-02-16 23:35:45 +000052#include "llvm/Support/Path.h"
Rui Ueyamabe939b32016-11-21 17:22:35 +000053#include "llvm/Support/ScopedPrinter.h"
Rui Ueyamae7378242015-12-04 23:11:05 +000054#include <memory>
55
Rui Ueyama1d99ab32016-09-15 22:24:51 +000056using namespace lld;
Rui Ueyama09e0b5f2016-11-12 00:00:51 +000057using namespace lld::coff;
Rui Ueyamae7378242015-12-04 23:11:05 +000058using namespace llvm;
Rui Ueyamabe939b32016-11-21 17:22:35 +000059using namespace llvm::codeview;
Rui Ueyamae7378242015-12-04 23:11:05 +000060
Rui Ueyama09e0b5f2016-11-12 00:00:51 +000061using llvm::object::coff_section;
62
Rui Ueyamab28c6d42016-09-16 04:32:33 +000063static ExitOnError ExitOnErr;
64
Reid Kleckner0faf6d72017-07-14 00:14:58 +000065namespace {
Reid Kleckner651db912017-07-18 00:21:25 +000066/// Map from type index and item index in a type server PDB to the
67/// corresponding index in the destination PDB.
68struct CVIndexMap {
69 SmallVector<TypeIndex, 0> TPIMap;
70 SmallVector<TypeIndex, 0> IPIMap;
71 bool IsTypeServerMap = false;
72};
73
Reid Kleckner0faf6d72017-07-14 00:14:58 +000074class PDBLinker {
75public:
76 PDBLinker(SymbolTable *Symtab)
Sam Cleggea244bf2017-12-14 21:09:31 +000077 : Alloc(), Symtab(Symtab), Builder(Alloc), TypeTable(Alloc),
78 IDTable(Alloc), GlobalTypeTable(Alloc), GlobalIDTable(Alloc) {}
Reid Kleckner0faf6d72017-07-14 00:14:58 +000079
80 /// Emit the basic PDB structure: initial streams, headers, etc.
Zachary Turner024323c2017-08-15 21:31:41 +000081 void initialize(const llvm::codeview::DebugInfo &BuildId);
Reid Kleckner0faf6d72017-07-14 00:14:58 +000082
83 /// Link CodeView from each object file in the symbol table into the PDB.
84 void addObjectsToPDB();
85
86 /// Link CodeView from a single object file into the PDB.
Rui Ueyamae1b48e02017-07-26 23:05:24 +000087 void addObjFile(ObjFile *File);
Reid Kleckner0faf6d72017-07-14 00:14:58 +000088
Reid Kleckner651db912017-07-18 00:21:25 +000089 /// Produce a mapping from the type and item indices used in the object
90 /// file to those in the destination PDB.
91 ///
92 /// If the object file uses a type server PDB (compiled with /Zi), merge TPI
93 /// and IPI from the type server PDB and return a map for it. Each unique type
94 /// server PDB is merged at most once, so this may return an existing index
95 /// mapping.
96 ///
97 /// If the object does not use a type server PDB (compiled with /Z7), we merge
98 /// all the type and item records from the .debug$S stream and fill in the
99 /// caller-provided ObjectIndexMap.
Rui Ueyamae1b48e02017-07-26 23:05:24 +0000100 const CVIndexMap &mergeDebugT(ObjFile *File, CVIndexMap &ObjectIndexMap);
Reid Kleckner651db912017-07-18 00:21:25 +0000101
Rui Ueyamae1b48e02017-07-26 23:05:24 +0000102 const CVIndexMap &maybeMergeTypeServerPDB(ObjFile *File,
Reid Kleckner651db912017-07-18 00:21:25 +0000103 TypeServer2Record &TS);
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000104
105 /// Add the section map and section contributions to the PDB.
Reid Kleckner175af4b2017-08-03 21:15:09 +0000106 void addSections(ArrayRef<OutputSection *> OutputSections,
107 ArrayRef<uint8_t> SectionTable);
108
109 void addSectionContrib(pdb::DbiModuleDescriptorBuilder &LinkerModule,
110 OutputSection *OS, Chunk *C);
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000111
112 /// Write the PDB to disk.
113 void commit();
114
115private:
116 BumpPtrAllocator Alloc;
117
118 SymbolTable *Symtab;
119
120 pdb::PDBFileBuilder Builder;
121
122 /// Type records that will go into the PDB TPI stream.
Zachary Turnerca6dbf12017-11-30 18:39:50 +0000123 MergingTypeTableBuilder TypeTable;
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000124
125 /// Item records that will go into the PDB IPI stream.
Zachary Turnerca6dbf12017-11-30 18:39:50 +0000126 MergingTypeTableBuilder IDTable;
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000127
Zachary Turner0d07a8e2017-12-14 18:07:04 +0000128 /// Type records that will go into the PDB TPI stream (for /DEBUG:GHASH)
129 GlobalTypeTableBuilder GlobalTypeTable;
130
131 /// Item records that will go into the PDB IPI stream (for /DEBUG:GHASH)
132 GlobalTypeTableBuilder GlobalIDTable;
133
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000134 /// PDBs use a single global string table for filenames in the file checksum
135 /// table.
136 DebugStringTableSubsection PDBStrTab;
137
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000138 llvm::SmallString<128> NativePath;
139
140 std::vector<pdb::SecMapEntry> SectionMap;
Reid Kleckner651db912017-07-18 00:21:25 +0000141
142 /// Type index mappings of type server PDBs that we've loaded so far.
143 std::map<GUID, CVIndexMap> TypeServerIndexMappings;
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000144};
145}
146
Sam Clegg0fb6faa2017-12-08 01:09:21 +0000147static SectionChunk *findByName(ArrayRef<SectionChunk *> Sections,
Rui Ueyamabe939b32016-11-21 17:22:35 +0000148 StringRef Name) {
149 for (SectionChunk *C : Sections)
150 if (C->getSectionName() == Name)
151 return C;
152 return nullptr;
153}
154
Reid Kleckner44cdb102017-06-19 17:21:45 +0000155static ArrayRef<uint8_t> consumeDebugMagic(ArrayRef<uint8_t> Data,
156 StringRef SecName) {
Rui Ueyamac5cb7372016-12-01 01:22:48 +0000157 // First 4 bytes are section magic.
Rui Ueyamac5cb7372016-12-01 01:22:48 +0000158 if (Data.size() < 4)
Rui Ueyama52896622017-01-12 03:09:25 +0000159 fatal(SecName + " too short");
Reid Kleckner44cdb102017-06-19 17:21:45 +0000160 if (support::endian::read32le(Data.data()) != COFF::DEBUG_SECTION_MAGIC)
Rui Ueyama52896622017-01-12 03:09:25 +0000161 fatal(SecName + " has an invalid magic");
Rui Ueyama26186c72016-12-09 04:46:54 +0000162 return Data.slice(4);
163}
Rui Ueyamac5cb7372016-12-01 01:22:48 +0000164
Rui Ueyamae1b48e02017-07-26 23:05:24 +0000165static ArrayRef<uint8_t> getDebugSection(ObjFile *File, StringRef SecName) {
Reid Kleckner44cdb102017-06-19 17:21:45 +0000166 if (SectionChunk *Sec = findByName(File->getDebugChunks(), SecName))
167 return consumeDebugMagic(Sec->getContents(), SecName);
168 return {};
169}
170
Zachary Turner0d07a8e2017-12-14 18:07:04 +0000171// A COFF .debug$H section is currently a clang extension. This function checks
172// if a .debug$H section is in a format that we expect / understand, so that we
173// can ignore any sections which are coincidentally also named .debug$H but do
174// not contain a format we recognize.
175static bool canUseDebugH(ArrayRef<uint8_t> DebugH) {
176 if (DebugH.size() < sizeof(object::debug_h_header))
177 return false;
178 auto *Header =
179 reinterpret_cast<const object::debug_h_header *>(DebugH.data());
180 DebugH = DebugH.drop_front(sizeof(object::debug_h_header));
181 return Header->Magic == COFF::DEBUG_HASHES_SECTION_MAGIC &&
182 Header->Version == 0 &&
183 Header->HashAlgorithm == uint16_t(GlobalTypeHashAlg::SHA1) &&
184 (DebugH.size() % 20 == 0);
185}
186
187static Optional<ArrayRef<uint8_t>> getDebugH(ObjFile *File) {
188 SectionChunk *Sec = findByName(File->getDebugChunks(), ".debug$H");
189 if (!Sec)
190 return llvm::None;
191 ArrayRef<uint8_t> Contents = Sec->getContents();
192 if (!canUseDebugH(Contents))
193 return None;
194 return Contents;
195}
196
197static ArrayRef<GloballyHashedType>
198getHashesFromDebugH(ArrayRef<uint8_t> DebugH) {
199 assert(canUseDebugH(DebugH));
200
201 DebugH = DebugH.drop_front(sizeof(object::debug_h_header));
202 uint32_t Count = DebugH.size() / sizeof(GloballyHashedType);
203 return {reinterpret_cast<const GloballyHashedType *>(DebugH.data()), Count};
204}
205
Reid Kleckner5d577522017-03-24 17:26:38 +0000206static void addTypeInfo(pdb::TpiStreamBuilder &TpiBuilder,
Zachary Turner3e3936d2017-11-29 19:35:21 +0000207 TypeCollection &TypeTable) {
Reid Kleckner5d577522017-03-24 17:26:38 +0000208 // Start the TPI or IPI stream header.
209 TpiBuilder.setVersionHeader(pdb::PdbTpiV80);
Rui Ueyama52896622017-01-12 03:09:25 +0000210
Reid Klecknerd91ca762017-07-19 17:26:07 +0000211 // Flatten the in memory type table and hash each type.
Zachary Turner3e3936d2017-11-29 19:35:21 +0000212 TypeTable.ForEachRecord([&](TypeIndex TI, const CVType &Type) {
Reid Klecknerd91ca762017-07-19 17:26:07 +0000213 auto Hash = pdb::hashTypeRecord(Type);
214 if (auto E = Hash.takeError())
215 fatal("type hashing error");
Zachary Turner3e3936d2017-11-29 19:35:21 +0000216 TpiBuilder.addTypeRecord(Type.RecordData, *Hash);
Reid Kleckner5d577522017-03-24 17:26:38 +0000217 });
Reid Kleckner5d577522017-03-24 17:26:38 +0000218}
219
Reid Kleckner651db912017-07-18 00:21:25 +0000220static Optional<TypeServer2Record>
221maybeReadTypeServerRecord(CVTypeArray &Types) {
222 auto I = Types.begin();
223 if (I == Types.end())
224 return None;
225 const CVType &Type = *I;
226 if (Type.kind() != LF_TYPESERVER2)
227 return None;
228 TypeServer2Record TS;
229 if (auto EC = TypeDeserializer::deserializeAs(const_cast<CVType &>(Type), TS))
Bob Haarmanb8a59c82017-10-25 22:28:38 +0000230 fatal("error reading type server record: " + toString(std::move(EC)));
Reid Kleckner651db912017-07-18 00:21:25 +0000231 return std::move(TS);
232}
233
Rui Ueyamae1b48e02017-07-26 23:05:24 +0000234const CVIndexMap &PDBLinker::mergeDebugT(ObjFile *File,
Reid Kleckner651db912017-07-18 00:21:25 +0000235 CVIndexMap &ObjectIndexMap) {
Reid Kleckner44cdb102017-06-19 17:21:45 +0000236 ArrayRef<uint8_t> Data = getDebugSection(File, ".debug$T");
237 if (Data.empty())
Reid Kleckner651db912017-07-18 00:21:25 +0000238 return ObjectIndexMap;
Reid Kleckner6597c282017-07-13 20:12:23 +0000239
Reid Kleckner44cdb102017-06-19 17:21:45 +0000240 BinaryByteStream Stream(Data, support::little);
241 CVTypeArray Types;
242 BinaryStreamReader Reader(Stream);
Reid Kleckner44cdb102017-06-19 17:21:45 +0000243 if (auto EC = Reader.readArray(Types, Reader.getLength()))
Bob Haarmanb8a59c82017-10-25 22:28:38 +0000244 fatal("Reader::readArray failed: " + toString(std::move(EC)));
Reid Kleckner651db912017-07-18 00:21:25 +0000245
246 // Look through type servers. If we've already seen this type server, don't
247 // merge any type information.
248 if (Optional<TypeServer2Record> TS = maybeReadTypeServerRecord(Types))
249 return maybeMergeTypeServerPDB(File, *TS);
250
251 // This is a /Z7 object. Fill in the temporary, caller-provided
252 // ObjectIndexMap.
Zachary Turner0d07a8e2017-12-14 18:07:04 +0000253 if (Config->DebugGHashes) {
254 ArrayRef<GloballyHashedType> Hashes;
255 std::vector<GloballyHashedType> OwnedHashes;
256 if (Optional<ArrayRef<uint8_t>> DebugH = getDebugH(File))
257 Hashes = getHashesFromDebugH(*DebugH);
258 else {
259 OwnedHashes = GloballyHashedType::hashTypes(Types);
260 Hashes = OwnedHashes;
261 }
262
263 if (auto Err = mergeTypeAndIdRecords(GlobalIDTable, GlobalTypeTable,
264 ObjectIndexMap.TPIMap, Types, Hashes))
265 fatal("codeview::mergeTypeAndIdRecords failed: " +
266 toString(std::move(Err)));
267 } else {
268 if (auto Err = mergeTypeAndIdRecords(IDTable, TypeTable,
269 ObjectIndexMap.TPIMap, Types))
270 fatal("codeview::mergeTypeAndIdRecords failed: " +
271 toString(std::move(Err)));
272 }
Reid Kleckner651db912017-07-18 00:21:25 +0000273 return ObjectIndexMap;
274}
275
276static Expected<std::unique_ptr<pdb::NativeSession>>
277tryToLoadPDB(const GUID &GuidFromObj, StringRef TSPath) {
Peter Collingbourne75257bc2017-10-20 19:48:26 +0000278 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile(
279 TSPath, /*FileSize=*/-1, /*RequiresNullTerminator=*/false);
280 if (!MBOrErr)
281 return errorCodeToError(MBOrErr.getError());
282
Reid Kleckner651db912017-07-18 00:21:25 +0000283 std::unique_ptr<pdb::IPDBSession> ThisSession;
Peter Collingbourne75257bc2017-10-20 19:48:26 +0000284 if (auto EC = pdb::NativeSession::createFromPdb(
285 MemoryBuffer::getMemBuffer(Driver->takeBuffer(std::move(*MBOrErr)),
286 /*RequiresNullTerminator=*/false),
287 ThisSession))
Reid Kleckner651db912017-07-18 00:21:25 +0000288 return std::move(EC);
289
290 std::unique_ptr<pdb::NativeSession> NS(
291 static_cast<pdb::NativeSession *>(ThisSession.release()));
292 pdb::PDBFile &File = NS->getPDBFile();
293 auto ExpectedInfo = File.getPDBInfoStream();
294 // All PDB Files should have an Info stream.
295 if (!ExpectedInfo)
296 return ExpectedInfo.takeError();
297
298 // Just because a file with a matching name was found and it was an actual
299 // PDB file doesn't mean it matches. For it to match the InfoStream's GUID
300 // must match the GUID specified in the TypeServer2 record.
301 if (ExpectedInfo->getGuid() != GuidFromObj)
302 return make_error<pdb::GenericError>(
303 pdb::generic_error_code::type_server_not_found, TSPath);
304
305 return std::move(NS);
306}
307
Rui Ueyamae1b48e02017-07-26 23:05:24 +0000308const CVIndexMap &PDBLinker::maybeMergeTypeServerPDB(ObjFile *File,
Reid Kleckner651db912017-07-18 00:21:25 +0000309 TypeServer2Record &TS) {
310 // First, check if we already loaded a PDB with this GUID. Return the type
311 // index mapping if we have it.
312 auto Insertion = TypeServerIndexMappings.insert({TS.getGuid(), CVIndexMap()});
313 CVIndexMap &IndexMap = Insertion.first->second;
314 if (!Insertion.second)
315 return IndexMap;
316
317 // Mark this map as a type server map.
318 IndexMap.IsTypeServerMap = true;
319
320 // Check for a PDB at:
321 // 1. The given file path
322 // 2. Next to the object file or archive file
323 auto ExpectedSession = tryToLoadPDB(TS.getGuid(), TS.getName());
324 if (!ExpectedSession) {
325 consumeError(ExpectedSession.takeError());
326 StringRef LocalPath =
327 !File->ParentName.empty() ? File->ParentName : File->getName();
328 SmallString<128> Path = sys::path::parent_path(LocalPath);
Reid Klecknerf8522d82017-07-18 00:33:53 +0000329 sys::path::append(
330 Path, sys::path::filename(TS.getName(), sys::path::Style::windows));
Reid Kleckner651db912017-07-18 00:21:25 +0000331 ExpectedSession = tryToLoadPDB(TS.getGuid(), Path);
332 }
333 if (auto E = ExpectedSession.takeError())
Bob Haarmanb8a59c82017-10-25 22:28:38 +0000334 fatal("Type server PDB was not found: " + toString(std::move(E)));
Reid Kleckner651db912017-07-18 00:21:25 +0000335
Reid Kleckner651db912017-07-18 00:21:25 +0000336 auto ExpectedTpi = (*ExpectedSession)->getPDBFile().getPDBTpiStream();
337 if (auto E = ExpectedTpi.takeError())
Bob Haarmanb8a59c82017-10-25 22:28:38 +0000338 fatal("Type server does not have TPI stream: " + toString(std::move(E)));
Reid Kleckner651db912017-07-18 00:21:25 +0000339 auto ExpectedIpi = (*ExpectedSession)->getPDBFile().getPDBIpiStream();
340 if (auto E = ExpectedIpi.takeError())
Bob Haarmanb8a59c82017-10-25 22:28:38 +0000341 fatal("Type server does not have TPI stream: " + toString(std::move(E)));
Zachary Turner0d07a8e2017-12-14 18:07:04 +0000342
343 if (Config->DebugGHashes) {
344 // PDBs do not actually store global hashes, so when merging a type server
345 // PDB we have to synthesize global hashes. To do this, we first synthesize
346 // global hashes for the TPI stream, since it is independent, then we
347 // synthesize hashes for the IPI stream, using the hashes for the TPI stream
348 // as inputs.
349 auto TpiHashes = GloballyHashedType::hashTypes(ExpectedTpi->typeArray());
350 auto IpiHashes =
351 GloballyHashedType::hashIds(ExpectedIpi->typeArray(), TpiHashes);
352
353 // Merge TPI first, because the IPI stream will reference type indices.
354 if (auto Err = mergeTypeRecords(GlobalTypeTable, IndexMap.TPIMap,
355 ExpectedTpi->typeArray(), TpiHashes))
356 fatal("codeview::mergeTypeRecords failed: " + toString(std::move(Err)));
357
358 // Merge IPI.
359 if (auto Err =
360 mergeIdRecords(GlobalIDTable, IndexMap.TPIMap, IndexMap.IPIMap,
361 ExpectedIpi->typeArray(), IpiHashes))
362 fatal("codeview::mergeIdRecords failed: " + toString(std::move(Err)));
363 } else {
364 // Merge TPI first, because the IPI stream will reference type indices.
365 if (auto Err = mergeTypeRecords(TypeTable, IndexMap.TPIMap,
366 ExpectedTpi->typeArray()))
367 fatal("codeview::mergeTypeRecords failed: " + toString(std::move(Err)));
368
369 // Merge IPI.
370 if (auto Err = mergeIdRecords(IDTable, IndexMap.TPIMap, IndexMap.IPIMap,
371 ExpectedIpi->typeArray()))
372 fatal("codeview::mergeIdRecords failed: " + toString(std::move(Err)));
373 }
Reid Kleckner651db912017-07-18 00:21:25 +0000374
375 return IndexMap;
Reid Kleckner44cdb102017-06-19 17:21:45 +0000376}
377
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000378static bool remapTypeIndex(TypeIndex &TI, ArrayRef<TypeIndex> TypeIndexMap) {
379 if (TI.isSimple())
380 return true;
381 if (TI.toArrayIndex() >= TypeIndexMap.size())
382 return false;
383 TI = TypeIndexMap[TI.toArrayIndex()];
384 return true;
385}
386
Reid Kleckner8aa32ff2017-10-24 17:02:40 +0000387static void remapTypesInSymbolRecord(ObjFile *File, SymbolKind SymKind,
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000388 MutableArrayRef<uint8_t> Contents,
Reid Kleckner651db912017-07-18 00:21:25 +0000389 const CVIndexMap &IndexMap,
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000390 ArrayRef<TiReference> TypeRefs) {
391 for (const TiReference &Ref : TypeRefs) {
392 unsigned ByteSize = Ref.Count * sizeof(TypeIndex);
Reid Kleckner03b5baf2017-07-12 18:49:43 +0000393 if (Contents.size() < Ref.Offset + ByteSize)
394 fatal("symbol record too short");
Reid Kleckner651db912017-07-18 00:21:25 +0000395
396 // This can be an item index or a type index. Choose the appropriate map.
397 ArrayRef<TypeIndex> TypeOrItemMap = IndexMap.TPIMap;
Reid Kleckner8aa32ff2017-10-24 17:02:40 +0000398 bool IsItemIndex = Ref.Kind == TiRefKind::IndexRef;
399 if (IsItemIndex && IndexMap.IsTypeServerMap)
Reid Kleckner651db912017-07-18 00:21:25 +0000400 TypeOrItemMap = IndexMap.IPIMap;
401
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000402 MutableArrayRef<TypeIndex> TIs(
403 reinterpret_cast<TypeIndex *>(Contents.data() + Ref.Offset), Ref.Count);
Reid Kleckner03b5baf2017-07-12 18:49:43 +0000404 for (TypeIndex &TI : TIs) {
Reid Kleckner651db912017-07-18 00:21:25 +0000405 if (!remapTypeIndex(TI, TypeOrItemMap)) {
Reid Kleckner8aa32ff2017-10-24 17:02:40 +0000406 log("ignoring symbol record of kind 0x" + utohexstr(SymKind) + " in " +
407 File->getName() + " with bad " + (IsItemIndex ? "item" : "type") +
408 " index 0x" + utohexstr(TI.getIndex()));
Reid Kleckner5d82f682017-10-23 22:44:51 +0000409 TI = TypeIndex(SimpleTypeKind::NotTranslated);
Reid Kleckner03b5baf2017-07-12 18:49:43 +0000410 continue;
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000411 }
Reid Kleckner03b5baf2017-07-12 18:49:43 +0000412 }
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000413 }
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000414}
415
Zachary Turner60478582018-01-05 19:12:40 +0000416static void
417recordStringTableReferenceAtOffset(MutableArrayRef<uint8_t> Contents,
418 uint32_t Offset,
419 std::vector<ulittle32_t *> &StrTableRefs) {
420 Contents =
421 Contents.drop_front(Offset).take_front(sizeof(support::ulittle32_t));
422 ulittle32_t *Index = reinterpret_cast<ulittle32_t *>(Contents.data());
423 StrTableRefs.push_back(Index);
424}
425
426static void
427recordStringTableReferences(SymbolKind Kind, MutableArrayRef<uint8_t> Contents,
428 std::vector<ulittle32_t *> &StrTableRefs) {
429 // For now we only handle S_FILESTATIC, but we may need the same logic for
430 // S_DEFRANGE and S_DEFRANGE_SUBFIELD. However, I cannot seem to generate any
431 // PDBs that contain these types of records, so because of the uncertainty
432 // they are omitted here until we can prove that it's necessary.
433 switch (Kind) {
434 case SymbolKind::S_FILESTATIC:
435 // FileStaticSym::ModFileOffset
436 recordStringTableReferenceAtOffset(Contents, 4, StrTableRefs);
437 break;
438 case SymbolKind::S_DEFRANGE:
439 case SymbolKind::S_DEFRANGE_SUBFIELD:
440 log("Not fixing up string table reference in S_DEFRANGE / "
441 "S_DEFRANGE_SUBFIELD record");
442 break;
443 }
444}
445
Zachary Turner59e3ae82017-08-08 18:34:44 +0000446static SymbolKind symbolKind(ArrayRef<uint8_t> RecordData) {
447 const RecordPrefix *Prefix =
448 reinterpret_cast<const RecordPrefix *>(RecordData.data());
449 return static_cast<SymbolKind>(uint16_t(Prefix->RecordKind));
450}
451
452/// MSVC translates S_PROC_ID_END to S_END, and S_[LG]PROC32_ID to S_[LG]PROC32
453static void translateIdSymbols(MutableArrayRef<uint8_t> &RecordData,
Zachary Turner3e3936d2017-11-29 19:35:21 +0000454 TypeCollection &IDTable) {
Zachary Turner59e3ae82017-08-08 18:34:44 +0000455 RecordPrefix *Prefix = reinterpret_cast<RecordPrefix *>(RecordData.data());
456
457 SymbolKind Kind = symbolKind(RecordData);
458
459 if (Kind == SymbolKind::S_PROC_ID_END) {
460 Prefix->RecordKind = SymbolKind::S_END;
461 return;
462 }
463
464 // In an object file, GPROC32_ID has an embedded reference which refers to the
465 // single object file type index namespace. This has already been translated
466 // to the PDB file's ID stream index space, but we need to convert this to a
467 // symbol that refers to the type stream index space. So we remap again from
468 // ID index space to type index space.
469 if (Kind == SymbolKind::S_GPROC32_ID || Kind == SymbolKind::S_LPROC32_ID) {
470 SmallVector<TiReference, 1> Refs;
471 auto Content = RecordData.drop_front(sizeof(RecordPrefix));
472 CVSymbol Sym(Kind, RecordData);
473 discoverTypeIndicesInSymbol(Sym, Refs);
474 assert(Refs.size() == 1);
475 assert(Refs.front().Count == 1);
476
477 TypeIndex *TI =
478 reinterpret_cast<TypeIndex *>(Content.data() + Refs[0].Offset);
479 // `TI` is the index of a FuncIdRecord or MemberFuncIdRecord which lives in
480 // the IPI stream, whose `FunctionType` member refers to the TPI stream.
481 // Note that LF_FUNC_ID and LF_MEMFUNC_ID have the same record layout, and
482 // in both cases we just need the second type index.
483 if (!TI->isSimple() && !TI->isNoneType()) {
Zachary Turner3e3936d2017-11-29 19:35:21 +0000484 CVType FuncIdData = IDTable.getType(*TI);
Zachary Turner59e3ae82017-08-08 18:34:44 +0000485 SmallVector<TypeIndex, 2> Indices;
486 discoverTypeIndices(FuncIdData, Indices);
487 assert(Indices.size() == 2);
488 *TI = Indices[1];
489 }
490
491 Kind = (Kind == SymbolKind::S_GPROC32_ID) ? SymbolKind::S_GPROC32
492 : SymbolKind::S_LPROC32;
493 Prefix->RecordKind = uint16_t(Kind);
494 }
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000495}
496
497/// Copy the symbol record. In a PDB, symbol records must be 4 byte aligned.
498/// The object file may not be aligned.
499static MutableArrayRef<uint8_t> copySymbolForPdb(const CVSymbol &Sym,
500 BumpPtrAllocator &Alloc) {
501 size_t Size = alignTo(Sym.length(), alignOf(CodeViewContainer::Pdb));
502 assert(Size >= 4 && "record too short");
503 assert(Size <= MaxRecordLength && "record too long");
504 void *Mem = Alloc.Allocate(Size, 4);
505
506 // Copy the symbol record and zero out any padding bytes.
507 MutableArrayRef<uint8_t> NewData(reinterpret_cast<uint8_t *>(Mem), Size);
508 memcpy(NewData.data(), Sym.data().data(), Sym.length());
509 memset(NewData.data() + Sym.length(), 0, Size - Sym.length());
510
511 // Update the record prefix length. It should point to the beginning of the
Zachary Turner59e3ae82017-08-08 18:34:44 +0000512 // next record.
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000513 auto *Prefix = reinterpret_cast<RecordPrefix *>(Mem);
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000514 Prefix->RecordLen = Size - 2;
515 return NewData;
516}
517
Reid Kleckner3f851922017-07-06 16:39:32 +0000518/// Return true if this symbol opens a scope. This implies that the symbol has
519/// "parent" and "end" fields, which contain the offset of the S_END or
520/// S_INLINESITE_END record.
521static bool symbolOpensScope(SymbolKind Kind) {
522 switch (Kind) {
523 case SymbolKind::S_GPROC32:
524 case SymbolKind::S_LPROC32:
525 case SymbolKind::S_LPROC32_ID:
526 case SymbolKind::S_GPROC32_ID:
527 case SymbolKind::S_BLOCK32:
528 case SymbolKind::S_SEPCODE:
529 case SymbolKind::S_THUNK32:
530 case SymbolKind::S_INLINESITE:
531 case SymbolKind::S_INLINESITE2:
532 return true;
533 default:
534 break;
535 }
536 return false;
537}
538
539static bool symbolEndsScope(SymbolKind Kind) {
540 switch (Kind) {
541 case SymbolKind::S_END:
542 case SymbolKind::S_PROC_ID_END:
543 case SymbolKind::S_INLINESITE_END:
544 return true;
545 default:
546 break;
547 }
548 return false;
549}
550
551struct ScopeRecord {
552 ulittle32_t PtrParent;
553 ulittle32_t PtrEnd;
554};
555
556struct SymbolScope {
557 ScopeRecord *OpeningRecord;
558 uint32_t ScopeOffset;
559};
560
561static void scopeStackOpen(SmallVectorImpl<SymbolScope> &Stack,
562 uint32_t CurOffset, CVSymbol &Sym) {
563 assert(symbolOpensScope(Sym.kind()));
564 SymbolScope S;
565 S.ScopeOffset = CurOffset;
566 S.OpeningRecord = const_cast<ScopeRecord *>(
567 reinterpret_cast<const ScopeRecord *>(Sym.content().data()));
568 S.OpeningRecord->PtrParent = Stack.empty() ? 0 : Stack.back().ScopeOffset;
569 Stack.push_back(S);
570}
571
572static void scopeStackClose(SmallVectorImpl<SymbolScope> &Stack,
Rui Ueyamae1b48e02017-07-26 23:05:24 +0000573 uint32_t CurOffset, ObjFile *File) {
Reid Kleckner3f851922017-07-06 16:39:32 +0000574 if (Stack.empty()) {
575 warn("symbol scopes are not balanced in " + File->getName());
576 return;
577 }
578 SymbolScope S = Stack.pop_back_val();
579 S.OpeningRecord->PtrEnd = CurOffset;
580}
581
Zachary Turneree9906d2017-08-11 19:00:03 +0000582static bool symbolGoesInModuleStream(const CVSymbol &Sym) {
583 switch (Sym.kind()) {
584 case SymbolKind::S_GDATA32:
585 case SymbolKind::S_CONSTANT:
586 case SymbolKind::S_UDT:
587 // We really should not be seeing S_PROCREF and S_LPROCREF in the first place
588 // since they are synthesized by the linker in response to S_GPROC32 and
589 // S_LPROC32, but if we do see them, don't put them in the module stream I
590 // guess.
591 case SymbolKind::S_PROCREF:
592 case SymbolKind::S_LPROCREF:
593 return false;
594 // S_GDATA32 does not go in the module stream, but S_LDATA32 does.
595 case SymbolKind::S_LDATA32:
596 default:
597 return true;
598 }
599}
600
601static bool symbolGoesInGlobalsStream(const CVSymbol &Sym) {
602 switch (Sym.kind()) {
603 case SymbolKind::S_CONSTANT:
Zachary Turneree9906d2017-08-11 19:00:03 +0000604 case SymbolKind::S_GDATA32:
605 // S_LDATA32 goes in both the module stream and the globals stream.
606 case SymbolKind::S_LDATA32:
607 case SymbolKind::S_GPROC32:
608 case SymbolKind::S_LPROC32:
609 // We really should not be seeing S_PROCREF and S_LPROCREF in the first place
610 // since they are synthesized by the linker in response to S_GPROC32 and
611 // S_LPROC32, but if we do see them, copy them straight through.
612 case SymbolKind::S_PROCREF:
613 case SymbolKind::S_LPROCREF:
614 return true;
Zachary Turner302dc8b2017-08-14 18:44:58 +0000615 // FIXME: For now, we drop all S_UDT symbols (i.e. they don't go in the
616 // globals stream or the modules stream). These have special handling which
617 // needs more investigation before we can get right, but by putting them all
618 // into the globals stream WinDbg fails to display local variables of class
619 // types saying that it cannot find the type Foo *. So as a stopgap just to
620 // keep things working, we drop them.
621 case SymbolKind::S_UDT:
Zachary Turneree9906d2017-08-11 19:00:03 +0000622 default:
623 return false;
624 }
625}
626
627static void addGlobalSymbol(pdb::GSIStreamBuilder &Builder, ObjFile &File,
628 const CVSymbol &Sym) {
629 switch (Sym.kind()) {
630 case SymbolKind::S_CONSTANT:
631 case SymbolKind::S_UDT:
632 case SymbolKind::S_GDATA32:
633 case SymbolKind::S_LDATA32:
634 case SymbolKind::S_PROCREF:
635 case SymbolKind::S_LPROCREF:
636 Builder.addGlobalSymbol(Sym);
637 break;
638 case SymbolKind::S_GPROC32:
639 case SymbolKind::S_LPROC32: {
640 SymbolRecordKind K = SymbolRecordKind::ProcRefSym;
641 if (Sym.kind() == SymbolKind::S_LPROC32)
642 K = SymbolRecordKind::LocalProcRef;
643 ProcRefSym PS(K);
644 PS.Module = static_cast<uint16_t>(File.ModuleDBI->getModuleIndex());
645 // For some reason, MSVC seems to add one to this value.
646 ++PS.Module;
647 PS.Name = getSymbolName(Sym);
648 PS.SumName = 0;
649 PS.SymOffset = File.ModuleDBI->getNextSymbolOffset();
650 Builder.addGlobalSymbol(PS);
651 break;
652 }
653 default:
654 llvm_unreachable("Invalid symbol kind!");
655 }
656}
657
Rui Ueyamae1b48e02017-07-26 23:05:24 +0000658static void mergeSymbolRecords(BumpPtrAllocator &Alloc, ObjFile *File,
Zachary Turneree9906d2017-08-11 19:00:03 +0000659 pdb::GSIStreamBuilder &GsiBuilder,
Reid Kleckner651db912017-07-18 00:21:25 +0000660 const CVIndexMap &IndexMap,
Zachary Turner3e3936d2017-11-29 19:35:21 +0000661 TypeCollection &IDTable,
Zachary Turner60478582018-01-05 19:12:40 +0000662 std::vector<ulittle32_t *> &StringTableRefs,
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000663 BinaryStreamRef SymData) {
664 // FIXME: Improve error recovery by warning and skipping records when
665 // possible.
666 CVSymbolArray Syms;
667 BinaryStreamReader Reader(SymData);
668 ExitOnErr(Reader.readArray(Syms, Reader.getLength()));
Reid Kleckner3f851922017-07-06 16:39:32 +0000669 SmallVector<SymbolScope, 4> Scopes;
Zachary Turner59e3ae82017-08-08 18:34:44 +0000670 for (CVSymbol Sym : Syms) {
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000671 // Discover type index references in the record. Skip it if we don't know
672 // where they are.
673 SmallVector<TiReference, 32> TypeRefs;
Zachary Turner59e3ae82017-08-08 18:34:44 +0000674 if (!discoverTypeIndicesInSymbol(Sym, TypeRefs)) {
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000675 log("ignoring unknown symbol record with kind 0x" + utohexstr(Sym.kind()));
676 continue;
677 }
678
679 // Copy the symbol record so we can mutate it.
680 MutableArrayRef<uint8_t> NewData = copySymbolForPdb(Sym, Alloc);
681
682 // Re-map all the type index references.
683 MutableArrayRef<uint8_t> Contents =
684 NewData.drop_front(sizeof(RecordPrefix));
Zachary Turner3e3936d2017-11-29 19:35:21 +0000685 remapTypesInSymbolRecord(File, Sym.kind(), Contents, IndexMap, TypeRefs);
Zachary Turner59e3ae82017-08-08 18:34:44 +0000686
687 // An object file may have S_xxx_ID symbols, but these get converted to
688 // "real" symbols in a PDB.
689 translateIdSymbols(NewData, IDTable);
690
Zachary Turner60478582018-01-05 19:12:40 +0000691 // If this record refers to an offset in the object file's string table,
692 // add that item to the global PDB string table and re-write the index.
693 recordStringTableReferences(Sym.kind(), Contents, StringTableRefs);
694
Zachary Turner59e3ae82017-08-08 18:34:44 +0000695 SymbolKind NewKind = symbolKind(NewData);
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000696
Reid Kleckner3f851922017-07-06 16:39:32 +0000697 // Fill in "Parent" and "End" fields by maintaining a stack of scopes.
Zachary Turner59e3ae82017-08-08 18:34:44 +0000698 CVSymbol NewSym(NewKind, NewData);
699 if (symbolOpensScope(NewKind))
Reid Kleckner3f851922017-07-06 16:39:32 +0000700 scopeStackOpen(Scopes, File->ModuleDBI->getNextSymbolOffset(), NewSym);
Zachary Turner59e3ae82017-08-08 18:34:44 +0000701 else if (symbolEndsScope(NewKind))
Reid Kleckner3f851922017-07-06 16:39:32 +0000702 scopeStackClose(Scopes, File->ModuleDBI->getNextSymbolOffset(), File);
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000703
Zachary Turneree9906d2017-08-11 19:00:03 +0000704 // Add the symbol to the globals stream if necessary. Do this before adding
705 // the symbol to the module since we may need to get the next symbol offset,
706 // and writing to the module's symbol stream will update that offset.
707 if (symbolGoesInGlobalsStream(NewSym))
708 addGlobalSymbol(GsiBuilder, *File, NewSym);
709
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000710 // Add the symbol to the module.
Zachary Turneree9906d2017-08-11 19:00:03 +0000711 if (symbolGoesInModuleStream(NewSym))
712 File->ModuleDBI->addSymbol(NewSym);
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000713 }
714}
715
Reid Kleckner44cdb102017-06-19 17:21:45 +0000716// Allocate memory for a .debug$S section and relocate it.
717static ArrayRef<uint8_t> relocateDebugChunk(BumpPtrAllocator &Alloc,
718 SectionChunk *DebugChunk) {
719 uint8_t *Buffer = Alloc.Allocate<uint8_t>(DebugChunk->getSize());
720 assert(DebugChunk->OutputSectionOff == 0 &&
721 "debug sections should not be in output sections");
722 DebugChunk->writeTo(Buffer);
723 return consumeDebugMagic(makeArrayRef(Buffer, DebugChunk->getSize()),
724 ".debug$S");
725}
726
Rui Ueyamae1b48e02017-07-26 23:05:24 +0000727void PDBLinker::addObjFile(ObjFile *File) {
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000728 // Add a module descriptor for every object file. We need to put an absolute
729 // path to the object into the PDB. If this is a plain object, we make its
730 // path absolute. If it's an object in an archive, we make the archive path
731 // absolute.
732 bool InArchive = !File->ParentName.empty();
733 SmallString<128> Path = InArchive ? File->ParentName : File->getName();
734 sys::fs::make_absolute(Path);
735 sys::path::native(Path, sys::path::Style::windows);
736 StringRef Name = InArchive ? File->getName() : StringRef(Path);
Zachary Turner2897e032017-05-25 21:16:03 +0000737
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000738 File->ModuleDBI = &ExitOnErr(Builder.getDbiBuilder().addModuleInfo(Name));
739 File->ModuleDBI->setObjFileName(Path);
Reid Kleckner44cdb102017-06-19 17:21:45 +0000740
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000741 // Before we can process symbol substreams from .debug$S, we need to process
742 // type information, file checksums, and the string table. Add type info to
743 // the PDB first, so that we can get the map from object file type and item
744 // indices to PDB type and item indices.
Reid Kleckner651db912017-07-18 00:21:25 +0000745 CVIndexMap ObjectIndexMap;
746 const CVIndexMap &IndexMap = mergeDebugT(File, ObjectIndexMap);
Zachary Turner448dea42017-07-07 18:46:14 +0000747
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000748 // Now do all live .debug$S sections.
Zachary Turner60478582018-01-05 19:12:40 +0000749 DebugStringTableSubsectionRef CVStrTab;
750 DebugChecksumsSubsectionRef Checksums;
751 std::vector<ulittle32_t *> StringTableReferences;
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000752 for (SectionChunk *DebugChunk : File->getDebugChunks()) {
753 if (!DebugChunk->isLive() || DebugChunk->getSectionName() != ".debug$S")
754 continue;
Reid Kleckner8cbdd0c2017-06-13 15:49:13 +0000755
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000756 ArrayRef<uint8_t> RelocatedDebugContents =
757 relocateDebugChunk(Alloc, DebugChunk);
758 if (RelocatedDebugContents.empty())
759 continue;
Reid Kleckner8cbdd0c2017-06-13 15:49:13 +0000760
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000761 DebugSubsectionArray Subsections;
762 BinaryStreamReader Reader(RelocatedDebugContents, support::little);
763 ExitOnErr(Reader.readArray(Subsections, RelocatedDebugContents.size()));
Rui Ueyama52896622017-01-12 03:09:25 +0000764
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000765 for (const DebugSubsectionRecord &SS : Subsections) {
766 switch (SS.kind()) {
Zachary Turner60478582018-01-05 19:12:40 +0000767 case DebugSubsectionKind::StringTable: {
768 auto Data = SS.getRecordData();
769 ArrayRef<uint8_t> Buffer;
770 cantFail(Data.readLongestContiguousChunk(0, Buffer));
771 assert(!CVStrTab.valid() &&
772 "Encountered multiple string table subsections!");
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000773 ExitOnErr(CVStrTab.initialize(SS.getRecordData()));
774 break;
Zachary Turner60478582018-01-05 19:12:40 +0000775 }
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000776 case DebugSubsectionKind::FileChecksums:
Zachary Turner60478582018-01-05 19:12:40 +0000777 assert(!Checksums.valid() &&
778 "Encountered multiple checksum subsections!");
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000779 ExitOnErr(Checksums.initialize(SS.getRecordData()));
780 break;
781 case DebugSubsectionKind::Lines:
782 // We can add the relocated line table directly to the PDB without
783 // modification because the file checksum offsets will stay the same.
784 File->ModuleDBI->addDebugSubsection(SS);
785 break;
786 case DebugSubsectionKind::Symbols:
Zachary Turner0d07a8e2017-12-14 18:07:04 +0000787 if (Config->DebugGHashes) {
788 mergeSymbolRecords(Alloc, File, Builder.getGsiBuilder(), IndexMap,
Zachary Turner60478582018-01-05 19:12:40 +0000789 GlobalIDTable, StringTableReferences,
790 SS.getRecordData());
Zachary Turner0d07a8e2017-12-14 18:07:04 +0000791 } else {
792 mergeSymbolRecords(Alloc, File, Builder.getGsiBuilder(), IndexMap,
Zachary Turner60478582018-01-05 19:12:40 +0000793 IDTable, StringTableReferences,
794 SS.getRecordData());
Zachary Turner0d07a8e2017-12-14 18:07:04 +0000795 }
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000796 break;
797 default:
798 // FIXME: Process the rest of the subsections.
799 break;
Reid Kleckner44cdb102017-06-19 17:21:45 +0000800 }
801 }
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000802 }
Zachary Turner60478582018-01-05 19:12:40 +0000803
804 // We should have seen all debug subsections across the entire object file now
805 // which means that if a StringTable subsection and Checksums subsection were
806 // present, now is the time to handle them.
807 if (!CVStrTab.valid()) {
808 if (Checksums.valid())
809 fatal(".debug$S sections with a checksums subsection must also contain a "
810 "string table subsection");
811
812 if (!StringTableReferences.empty())
813 warn("No StringTable subsection was encountered, but there are string "
814 "table references");
815 return;
816 }
817
818 // Rewrite each string table reference based on the value that the string
819 // assumes in the final PDB.
820 for (ulittle32_t *Ref : StringTableReferences) {
821 auto ExpectedString = CVStrTab.getString(*Ref);
822 if (!ExpectedString) {
823 warn("Invalid string table reference");
824 consumeError(ExpectedString.takeError());
825 continue;
826 }
827
828 *Ref = PDBStrTab.insert(*ExpectedString);
829 }
830
831 // Make a new file checksum table that refers to offsets in the PDB-wide
832 // string table. Generally the string table subsection appears after the
833 // checksum table, so we have to do this after looping over all the
834 // subsections.
835 auto NewChecksums = make_unique<DebugChecksumsSubsection>(PDBStrTab);
836 for (FileChecksumEntry &FC : Checksums) {
837 StringRef FileName = ExitOnErr(CVStrTab.getString(FC.FileNameOffset));
838 ExitOnErr(Builder.getDbiBuilder().addModuleSourceFile(*File->ModuleDBI,
839 FileName));
840 NewChecksums->addChecksum(FileName, FC.Kind, FC.Checksum);
841 }
842 File->ModuleDBI->addDebugSubsection(std::move(NewChecksums));
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000843}
844
Reid Klecknereacdf042017-07-27 18:25:59 +0000845static PublicSym32 createPublic(Defined *Def) {
846 PublicSym32 Pub(SymbolKind::S_PUB32);
847 Pub.Name = Def->getName();
848 if (auto *D = dyn_cast<DefinedCOFF>(Def)) {
849 if (D->getCOFFSymbol().isFunctionDefinition())
850 Pub.Flags = PublicSymFlags::Function;
851 } else if (isa<DefinedImportThunk>(Def)) {
852 Pub.Flags = PublicSymFlags::Function;
853 }
854
855 OutputSection *OS = Def->getChunk()->getOutputSection();
856 assert(OS && "all publics should be in final image");
857 Pub.Offset = Def->getRVA() - OS->getRVA();
858 Pub.Segment = OS->SectionIndex;
859 return Pub;
860}
861
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000862// Add all object files to the PDB. Merge .debug$T sections into IpiData and
863// TpiData.
864void PDBLinker::addObjectsToPDB() {
Rui Ueyamaacd632d2017-07-27 00:45:26 +0000865 for (ObjFile *File : ObjFile::Instances)
Rui Ueyamae1b48e02017-07-26 23:05:24 +0000866 addObjFile(File);
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000867
868 Builder.getStringTableBuilder().setStrings(PDBStrTab);
Reid Kleckner44cdb102017-06-19 17:21:45 +0000869
Zachary Turner0d07a8e2017-12-14 18:07:04 +0000870 // Construct TPI and IPI stream contents.
871 if (Config->DebugGHashes) {
872 addTypeInfo(Builder.getTpiBuilder(), GlobalTypeTable);
873 addTypeInfo(Builder.getIpiBuilder(), GlobalIDTable);
874 } else {
875 addTypeInfo(Builder.getTpiBuilder(), TypeTable);
876 addTypeInfo(Builder.getIpiBuilder(), IDTable);
877 }
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000878
Zachary Turneree9906d2017-08-11 19:00:03 +0000879 // Compute the public and global symbols.
880 auto &GsiBuilder = Builder.getGsiBuilder();
Reid Klecknereacdf042017-07-27 18:25:59 +0000881 std::vector<PublicSym32> Publics;
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000882 Symtab->forEachSymbol([&Publics](Symbol *S) {
Reid Klecknereacdf042017-07-27 18:25:59 +0000883 // Only emit defined, live symbols that have a chunk.
Rui Ueyama616cd992017-10-31 16:10:24 +0000884 auto *Def = dyn_cast<Defined>(S);
Reid Klecknereacdf042017-07-27 18:25:59 +0000885 if (Def && Def->isLive() && Def->getChunk())
886 Publics.push_back(createPublic(Def));
887 });
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000888
Reid Klecknereacdf042017-07-27 18:25:59 +0000889 if (!Publics.empty()) {
890 // Sort the public symbols and add them to the stream.
891 std::sort(Publics.begin(), Publics.end(),
892 [](const PublicSym32 &L, const PublicSym32 &R) {
893 return L.Name < R.Name;
894 });
Reid Klecknereacdf042017-07-27 18:25:59 +0000895 for (const PublicSym32 &Pub : Publics)
Zachary Turner946204c2017-08-09 04:23:25 +0000896 GsiBuilder.addPublicSymbol(Pub);
Reid Klecknereacdf042017-07-27 18:25:59 +0000897 }
Rui Ueyama52896622017-01-12 03:09:25 +0000898}
899
Zachary Turner28e31ee2017-08-11 20:46:28 +0000900static void addCommonLinkerModuleSymbols(StringRef Path,
901 pdb::DbiModuleDescriptorBuilder &Mod,
902 BumpPtrAllocator &Allocator) {
Zachary Turner28e31ee2017-08-11 20:46:28 +0000903 ObjNameSym ONS(SymbolRecordKind::ObjNameSym);
904 Compile3Sym CS(SymbolRecordKind::Compile3Sym);
905 EnvBlockSym EBS(SymbolRecordKind::EnvBlockSym);
Zachary Turner6708e0b2017-07-10 21:01:37 +0000906
907 ONS.Name = "* Linker *";
908 ONS.Signature = 0;
909
910 CS.Machine = Config->is64() ? CPUType::X64 : CPUType::Intel80386;
Zachary Turnerb57884e2017-08-11 21:14:01 +0000911 // Interestingly, if we set the string to 0.0.0.0, then when trying to view
912 // local variables WinDbg emits an error that private symbols are not present.
913 // By setting this to a valid MSVC linker version string, local variables are
914 // displayed properly. As such, even though it is not representative of
Zachary Turnerf6822c82017-08-11 20:46:47 +0000915 // LLVM's version information, we need this for compatibility.
Zachary Turner6708e0b2017-07-10 21:01:37 +0000916 CS.Flags = CompileSym3Flags::None;
Zachary Turnerf6822c82017-08-11 20:46:47 +0000917 CS.VersionBackendBuild = 25019;
918 CS.VersionBackendMajor = 14;
919 CS.VersionBackendMinor = 10;
Zachary Turner6708e0b2017-07-10 21:01:37 +0000920 CS.VersionBackendQFE = 0;
Zachary Turnerf6822c82017-08-11 20:46:47 +0000921
922 // MSVC also sets the frontend to 0.0.0.0 since this is specifically for the
923 // linker module (which is by definition a backend), so we don't need to do
924 // anything here. Also, it seems we can use "LLVM Linker" for the linker name
925 // without any problems. Only the backend version has to be hardcoded to a
926 // magic number.
Zachary Turner6708e0b2017-07-10 21:01:37 +0000927 CS.VersionFrontendBuild = 0;
928 CS.VersionFrontendMajor = 0;
929 CS.VersionFrontendMinor = 0;
930 CS.VersionFrontendQFE = 0;
931 CS.Version = "LLVM Linker";
932 CS.setLanguage(SourceLanguage::Link);
933
934 ArrayRef<StringRef> Args = makeArrayRef(Config->Argv).drop_front();
935 std::string ArgStr = llvm::join(Args, " ");
936 EBS.Fields.push_back("cwd");
937 SmallString<64> cwd;
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000938 sys::fs::current_path(cwd);
Zachary Turner6708e0b2017-07-10 21:01:37 +0000939 EBS.Fields.push_back(cwd);
940 EBS.Fields.push_back("exe");
Zachary Turnerf6822c82017-08-11 20:46:47 +0000941 SmallString<64> exe = Config->Argv[0];
942 llvm::sys::fs::make_absolute(exe);
943 EBS.Fields.push_back(exe);
Zachary Turner6708e0b2017-07-10 21:01:37 +0000944 EBS.Fields.push_back("pdb");
945 EBS.Fields.push_back(Path);
946 EBS.Fields.push_back("cmd");
947 EBS.Fields.push_back(ArgStr);
948 Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(
949 ONS, Allocator, CodeViewContainer::Pdb));
950 Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(
951 CS, Allocator, CodeViewContainer::Pdb));
952 Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(
953 EBS, Allocator, CodeViewContainer::Pdb));
954}
955
Zachary Turner28e31ee2017-08-11 20:46:28 +0000956static void addLinkerModuleSectionSymbol(pdb::DbiModuleDescriptorBuilder &Mod,
957 OutputSection &OS,
958 BumpPtrAllocator &Allocator) {
959 SectionSym Sym(SymbolRecordKind::SectionSym);
Zachary Turnerf6822c82017-08-11 20:46:47 +0000960 Sym.Alignment = 12; // 2^12 = 4KB
Zachary Turner28e31ee2017-08-11 20:46:28 +0000961 Sym.Characteristics = OS.getCharacteristics();
962 Sym.Length = OS.getVirtualSize();
963 Sym.Name = OS.getName();
964 Sym.Rva = OS.getRVA();
965 Sym.SectionNumber = OS.SectionIndex;
966 Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(
967 Sym, Allocator, CodeViewContainer::Pdb));
968}
969
Rui Ueyama09e0b5f2016-11-12 00:00:51 +0000970// Creates a PDB file.
Reid Kleckner175af4b2017-08-03 21:15:09 +0000971void coff::createPDB(SymbolTable *Symtab,
972 ArrayRef<OutputSection *> OutputSections,
973 ArrayRef<uint8_t> SectionTable,
Zachary Turner024323c2017-08-15 21:31:41 +0000974 const llvm::codeview::DebugInfo &BuildId) {
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000975 PDBLinker PDB(Symtab);
Zachary Turner024323c2017-08-15 21:31:41 +0000976 PDB.initialize(BuildId);
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000977 PDB.addObjectsToPDB();
Reid Kleckner175af4b2017-08-03 21:15:09 +0000978 PDB.addSections(OutputSections, SectionTable);
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000979 PDB.commit();
980}
981
Zachary Turner024323c2017-08-15 21:31:41 +0000982void PDBLinker::initialize(const llvm::codeview::DebugInfo &BuildId) {
Rui Ueyama12979542016-09-30 20:53:45 +0000983 ExitOnErr(Builder.initialize(4096)); // 4096 is blocksize
Rui Ueyama7f382992016-09-15 18:55:18 +0000984
Rui Ueyama8d3fb5d2016-10-05 22:08:58 +0000985 // Create streams in MSF for predefined streams, namely
986 // PDB, TPI, DBI and IPI.
987 for (int I = 0; I < (int)pdb::kSpecialStreamCount; ++I)
988 ExitOnErr(Builder.getMsfBuilder().addStream(0));
Rui Ueyama7f382992016-09-15 18:55:18 +0000989
Rui Ueyamabb542b32016-09-16 22:51:17 +0000990 // Add an Info stream.
991 auto &InfoBuilder = Builder.getInfoBuilder();
Zachary Turner024323c2017-08-15 21:31:41 +0000992 InfoBuilder.setAge(BuildId.PDB70.Age);
Saleem Abdulrasool0acd6dd2017-02-07 04:28:02 +0000993
Zachary Turner024323c2017-08-15 21:31:41 +0000994 GUID uuid;
995 memcpy(&uuid, &BuildId.PDB70.Signature, sizeof(uuid));
Saleem Abdulrasool0acd6dd2017-02-07 04:28:02 +0000996 InfoBuilder.setGuid(uuid);
Zachary Turner3a11fdf2017-07-07 20:25:39 +0000997 InfoBuilder.setSignature(time(nullptr));
Rui Ueyamabb542b32016-09-16 22:51:17 +0000998 InfoBuilder.setVersion(pdb::PdbRaw_ImplVer::PdbImplVC70);
Rui Ueyama7f382992016-09-15 18:55:18 +0000999
Zachary Turner6c4bfba2017-07-07 05:04:36 +00001000 // Add an empty DBI stream.
Reid Kleckner8cbdd0c2017-06-13 15:49:13 +00001001 pdb::DbiStreamBuilder &DbiBuilder = Builder.getDbiBuilder();
Zachary Turner024323c2017-08-15 21:31:41 +00001002 DbiBuilder.setAge(BuildId.PDB70.Age);
Zachary Turnerc1e93e52017-07-07 18:45:56 +00001003 DbiBuilder.setVersionHeader(pdb::PdbDbiV70);
Zachary Turner3a11fdf2017-07-07 20:25:39 +00001004 ExitOnErr(DbiBuilder.addDbgStream(pdb::DbgHeaderType::NewFPO, {}));
Reid Kleckner0faf6d72017-07-14 00:14:58 +00001005}
Rui Ueyama1343fac2016-10-06 22:52:01 +00001006
Reid Kleckner175af4b2017-08-03 21:15:09 +00001007void PDBLinker::addSectionContrib(pdb::DbiModuleDescriptorBuilder &LinkerModule,
1008 OutputSection *OS, Chunk *C) {
1009 pdb::SectionContrib SC;
1010 memset(&SC, 0, sizeof(SC));
1011 SC.ISect = OS->SectionIndex;
1012 SC.Off = C->getRVA() - OS->getRVA();
1013 SC.Size = C->getSize();
1014 if (auto *SecChunk = dyn_cast<SectionChunk>(C)) {
1015 SC.Characteristics = SecChunk->Header->Characteristics;
1016 SC.Imod = SecChunk->File->ModuleDBI->getModuleIndex();
Zachary Turner676386ff2017-08-07 20:23:45 +00001017 ArrayRef<uint8_t> Contents = SecChunk->getContents();
1018 JamCRC CRC(0);
1019 ArrayRef<char> CharContents = makeArrayRef(
1020 reinterpret_cast<const char *>(Contents.data()), Contents.size());
1021 CRC.update(CharContents);
1022 SC.DataCrc = CRC.getCRC();
Reid Kleckner175af4b2017-08-03 21:15:09 +00001023 } else {
1024 SC.Characteristics = OS->getCharacteristics();
1025 // FIXME: When we start creating DBI for import libraries, use those here.
1026 SC.Imod = LinkerModule.getModuleIndex();
1027 }
Reid Kleckner175af4b2017-08-03 21:15:09 +00001028 SC.RelocCrc = 0; // FIXME
1029 Builder.getDbiBuilder().addSectionContrib(SC);
1030}
Rui Ueyama09e0b5f2016-11-12 00:00:51 +00001031
Reid Kleckner175af4b2017-08-03 21:15:09 +00001032void PDBLinker::addSections(ArrayRef<OutputSection *> OutputSections,
1033 ArrayRef<uint8_t> SectionTable) {
Reid Kleckner0faf6d72017-07-14 00:14:58 +00001034 // It's not entirely clear what this is, but the * Linker * module uses it.
Reid Kleckner175af4b2017-08-03 21:15:09 +00001035 pdb::DbiStreamBuilder &DbiBuilder = Builder.getDbiBuilder();
Reid Kleckner0faf6d72017-07-14 00:14:58 +00001036 NativePath = Config->PDBPath;
1037 sys::fs::make_absolute(NativePath);
1038 sys::path::native(NativePath, sys::path::Style::windows);
1039 uint32_t PdbFilePathNI = DbiBuilder.addECName(NativePath);
Zachary Turner6c4bfba2017-07-07 05:04:36 +00001040 auto &LinkerModule = ExitOnErr(DbiBuilder.addModuleInfo("* Linker *"));
1041 LinkerModule.setPdbFilePathNI(PdbFilePathNI);
Zachary Turner28e31ee2017-08-11 20:46:28 +00001042 addCommonLinkerModuleSymbols(NativePath, LinkerModule, Alloc);
Rui Ueyamac91f7162016-11-16 01:10:46 +00001043
Reid Kleckner175af4b2017-08-03 21:15:09 +00001044 // Add section contributions. They must be ordered by ascending RVA.
Zachary Turner28e31ee2017-08-11 20:46:28 +00001045 for (OutputSection *OS : OutputSections) {
1046 addLinkerModuleSectionSymbol(LinkerModule, *OS, Alloc);
Reid Kleckner175af4b2017-08-03 21:15:09 +00001047 for (Chunk *C : OS->getChunks())
1048 addSectionContrib(LinkerModule, OS, C);
Zachary Turner28e31ee2017-08-11 20:46:28 +00001049 }
Reid Kleckner175af4b2017-08-03 21:15:09 +00001050
1051 // Add Section Map stream.
1052 ArrayRef<object::coff_section> Sections = {
1053 (const object::coff_section *)SectionTable.data(),
1054 SectionTable.size() / sizeof(object::coff_section)};
1055 SectionMap = pdb::DbiStreamBuilder::createSectionMap(Sections);
1056 DbiBuilder.setSectionMap(SectionMap);
1057
Rui Ueyama9f66f822016-10-11 19:45:07 +00001058 // Add COFF section header stream.
1059 ExitOnErr(
1060 DbiBuilder.addDbgStream(pdb::DbgHeaderType::SectionHdr, SectionTable));
Reid Kleckner0faf6d72017-07-14 00:14:58 +00001061}
Rui Ueyama9f66f822016-10-11 19:45:07 +00001062
Reid Kleckner0faf6d72017-07-14 00:14:58 +00001063void PDBLinker::commit() {
Rui Ueyama3e9d6bb2016-09-26 23:53:55 +00001064 // Write to a file.
Zachary Turner6708e0b2017-07-10 21:01:37 +00001065 ExitOnErr(Builder.commit(Config->PDBPath));
Rui Ueyamae7378242015-12-04 23:11:05 +00001066}