blob: 7c620e484a9196202d5cbbba9bba2cedbdcd3d6b [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.
Rui Ueyamae1b48e02017-07-26 23:05:24 +0000110 const CVIndexMap &mergeDebugT(ObjFile *File, CVIndexMap &ObjectIndexMap);
Reid Kleckner651db912017-07-18 00:21:25 +0000111
Rui Ueyamae1b48e02017-07-26 23:05:24 +0000112 const CVIndexMap &maybeMergeTypeServerPDB(ObjFile *File,
Reid Kleckner651db912017-07-18 00:21:25 +0000113 TypeServer2Record &TS);
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000114
115 /// Add the section map and section contributions to the PDB.
Reid Kleckner175af4b2017-08-03 21:15:09 +0000116 void addSections(ArrayRef<OutputSection *> OutputSections,
117 ArrayRef<uint8_t> SectionTable);
118
119 void addSectionContrib(pdb::DbiModuleDescriptorBuilder &LinkerModule,
120 OutputSection *OS, Chunk *C);
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000121
122 /// Write the PDB to disk.
123 void commit();
124
125private:
126 BumpPtrAllocator Alloc;
127
128 SymbolTable *Symtab;
129
130 pdb::PDBFileBuilder Builder;
131
132 /// Type records that will go into the PDB TPI stream.
Zachary Turnerca6dbf12017-11-30 18:39:50 +0000133 MergingTypeTableBuilder TypeTable;
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000134
135 /// Item records that will go into the PDB IPI stream.
Zachary Turnerca6dbf12017-11-30 18:39:50 +0000136 MergingTypeTableBuilder IDTable;
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000137
Zachary Turner0d07a8e2017-12-14 18:07:04 +0000138 /// Type records that will go into the PDB TPI stream (for /DEBUG:GHASH)
139 GlobalTypeTableBuilder GlobalTypeTable;
140
141 /// Item records that will go into the PDB IPI stream (for /DEBUG:GHASH)
142 GlobalTypeTableBuilder GlobalIDTable;
143
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000144 /// PDBs use a single global string table for filenames in the file checksum
145 /// table.
146 DebugStringTableSubsection PDBStrTab;
147
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000148 llvm::SmallString<128> NativePath;
149
150 std::vector<pdb::SecMapEntry> SectionMap;
Reid Kleckner651db912017-07-18 00:21:25 +0000151
152 /// Type index mappings of type server PDBs that we've loaded so far.
153 std::map<GUID, CVIndexMap> TypeServerIndexMappings;
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000154};
155}
156
Sam Clegg0fb6faa2017-12-08 01:09:21 +0000157static SectionChunk *findByName(ArrayRef<SectionChunk *> Sections,
Rui Ueyamabe939b32016-11-21 17:22:35 +0000158 StringRef Name) {
159 for (SectionChunk *C : Sections)
160 if (C->getSectionName() == Name)
161 return C;
162 return nullptr;
163}
164
Reid Kleckner44cdb102017-06-19 17:21:45 +0000165static ArrayRef<uint8_t> consumeDebugMagic(ArrayRef<uint8_t> Data,
166 StringRef SecName) {
Rui Ueyamac5cb7372016-12-01 01:22:48 +0000167 // First 4 bytes are section magic.
Rui Ueyamac5cb7372016-12-01 01:22:48 +0000168 if (Data.size() < 4)
Rui Ueyama52896622017-01-12 03:09:25 +0000169 fatal(SecName + " too short");
Reid Kleckner44cdb102017-06-19 17:21:45 +0000170 if (support::endian::read32le(Data.data()) != COFF::DEBUG_SECTION_MAGIC)
Rui Ueyama52896622017-01-12 03:09:25 +0000171 fatal(SecName + " has an invalid magic");
Rui Ueyama26186c72016-12-09 04:46:54 +0000172 return Data.slice(4);
173}
Rui Ueyamac5cb7372016-12-01 01:22:48 +0000174
Rui Ueyamae1b48e02017-07-26 23:05:24 +0000175static ArrayRef<uint8_t> getDebugSection(ObjFile *File, StringRef SecName) {
Reid Kleckner44cdb102017-06-19 17:21:45 +0000176 if (SectionChunk *Sec = findByName(File->getDebugChunks(), SecName))
177 return consumeDebugMagic(Sec->getContents(), SecName);
178 return {};
179}
180
Zachary Turner0d07a8e2017-12-14 18:07:04 +0000181// A COFF .debug$H section is currently a clang extension. This function checks
182// if a .debug$H section is in a format that we expect / understand, so that we
183// can ignore any sections which are coincidentally also named .debug$H but do
184// not contain a format we recognize.
185static bool canUseDebugH(ArrayRef<uint8_t> DebugH) {
186 if (DebugH.size() < sizeof(object::debug_h_header))
187 return false;
188 auto *Header =
189 reinterpret_cast<const object::debug_h_header *>(DebugH.data());
190 DebugH = DebugH.drop_front(sizeof(object::debug_h_header));
191 return Header->Magic == COFF::DEBUG_HASHES_SECTION_MAGIC &&
192 Header->Version == 0 &&
193 Header->HashAlgorithm == uint16_t(GlobalTypeHashAlg::SHA1) &&
194 (DebugH.size() % 20 == 0);
195}
196
197static Optional<ArrayRef<uint8_t>> getDebugH(ObjFile *File) {
198 SectionChunk *Sec = findByName(File->getDebugChunks(), ".debug$H");
199 if (!Sec)
200 return llvm::None;
201 ArrayRef<uint8_t> Contents = Sec->getContents();
202 if (!canUseDebugH(Contents))
203 return None;
204 return Contents;
205}
206
207static ArrayRef<GloballyHashedType>
208getHashesFromDebugH(ArrayRef<uint8_t> DebugH) {
209 assert(canUseDebugH(DebugH));
210
211 DebugH = DebugH.drop_front(sizeof(object::debug_h_header));
212 uint32_t Count = DebugH.size() / sizeof(GloballyHashedType);
213 return {reinterpret_cast<const GloballyHashedType *>(DebugH.data()), Count};
214}
215
Reid Kleckner5d577522017-03-24 17:26:38 +0000216static void addTypeInfo(pdb::TpiStreamBuilder &TpiBuilder,
Zachary Turner3e3936d2017-11-29 19:35:21 +0000217 TypeCollection &TypeTable) {
Reid Kleckner5d577522017-03-24 17:26:38 +0000218 // Start the TPI or IPI stream header.
219 TpiBuilder.setVersionHeader(pdb::PdbTpiV80);
Rui Ueyama52896622017-01-12 03:09:25 +0000220
Reid Klecknerd91ca762017-07-19 17:26:07 +0000221 // Flatten the in memory type table and hash each type.
Zachary Turner3e3936d2017-11-29 19:35:21 +0000222 TypeTable.ForEachRecord([&](TypeIndex TI, const CVType &Type) {
Reid Klecknerd91ca762017-07-19 17:26:07 +0000223 auto Hash = pdb::hashTypeRecord(Type);
224 if (auto E = Hash.takeError())
225 fatal("type hashing error");
Zachary Turner3e3936d2017-11-29 19:35:21 +0000226 TpiBuilder.addTypeRecord(Type.RecordData, *Hash);
Reid Kleckner5d577522017-03-24 17:26:38 +0000227 });
Reid Kleckner5d577522017-03-24 17:26:38 +0000228}
229
Reid Kleckner651db912017-07-18 00:21:25 +0000230static Optional<TypeServer2Record>
231maybeReadTypeServerRecord(CVTypeArray &Types) {
232 auto I = Types.begin();
233 if (I == Types.end())
234 return None;
235 const CVType &Type = *I;
236 if (Type.kind() != LF_TYPESERVER2)
237 return None;
238 TypeServer2Record TS;
239 if (auto EC = TypeDeserializer::deserializeAs(const_cast<CVType &>(Type), TS))
Bob Haarmanb8a59c82017-10-25 22:28:38 +0000240 fatal("error reading type server record: " + toString(std::move(EC)));
Reid Kleckner651db912017-07-18 00:21:25 +0000241 return std::move(TS);
242}
243
Rui Ueyamae1b48e02017-07-26 23:05:24 +0000244const CVIndexMap &PDBLinker::mergeDebugT(ObjFile *File,
Reid Kleckner651db912017-07-18 00:21:25 +0000245 CVIndexMap &ObjectIndexMap) {
Zachary Turner727f1532018-01-17 19:16:26 +0000246 ScopedTimer T(TypeMergingTimer);
247
Reid Kleckner44cdb102017-06-19 17:21:45 +0000248 ArrayRef<uint8_t> Data = getDebugSection(File, ".debug$T");
249 if (Data.empty())
Reid Kleckner651db912017-07-18 00:21:25 +0000250 return ObjectIndexMap;
Reid Kleckner6597c282017-07-13 20:12:23 +0000251
Reid Kleckner44cdb102017-06-19 17:21:45 +0000252 BinaryByteStream Stream(Data, support::little);
253 CVTypeArray Types;
254 BinaryStreamReader Reader(Stream);
Reid Kleckner44cdb102017-06-19 17:21:45 +0000255 if (auto EC = Reader.readArray(Types, Reader.getLength()))
Bob Haarmanb8a59c82017-10-25 22:28:38 +0000256 fatal("Reader::readArray failed: " + toString(std::move(EC)));
Reid Kleckner651db912017-07-18 00:21:25 +0000257
258 // Look through type servers. If we've already seen this type server, don't
259 // merge any type information.
260 if (Optional<TypeServer2Record> TS = maybeReadTypeServerRecord(Types))
261 return maybeMergeTypeServerPDB(File, *TS);
262
263 // This is a /Z7 object. Fill in the temporary, caller-provided
264 // ObjectIndexMap.
Zachary Turner0d07a8e2017-12-14 18:07:04 +0000265 if (Config->DebugGHashes) {
266 ArrayRef<GloballyHashedType> Hashes;
267 std::vector<GloballyHashedType> OwnedHashes;
268 if (Optional<ArrayRef<uint8_t>> DebugH = getDebugH(File))
269 Hashes = getHashesFromDebugH(*DebugH);
270 else {
271 OwnedHashes = GloballyHashedType::hashTypes(Types);
272 Hashes = OwnedHashes;
273 }
274
275 if (auto Err = mergeTypeAndIdRecords(GlobalIDTable, GlobalTypeTable,
276 ObjectIndexMap.TPIMap, Types, Hashes))
277 fatal("codeview::mergeTypeAndIdRecords failed: " +
278 toString(std::move(Err)));
279 } else {
280 if (auto Err = mergeTypeAndIdRecords(IDTable, TypeTable,
281 ObjectIndexMap.TPIMap, Types))
282 fatal("codeview::mergeTypeAndIdRecords failed: " +
283 toString(std::move(Err)));
284 }
Reid Kleckner651db912017-07-18 00:21:25 +0000285 return ObjectIndexMap;
286}
287
288static Expected<std::unique_ptr<pdb::NativeSession>>
289tryToLoadPDB(const GUID &GuidFromObj, StringRef TSPath) {
Peter Collingbourne75257bc2017-10-20 19:48:26 +0000290 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile(
291 TSPath, /*FileSize=*/-1, /*RequiresNullTerminator=*/false);
292 if (!MBOrErr)
293 return errorCodeToError(MBOrErr.getError());
294
Reid Kleckner651db912017-07-18 00:21:25 +0000295 std::unique_ptr<pdb::IPDBSession> ThisSession;
Peter Collingbourne75257bc2017-10-20 19:48:26 +0000296 if (auto EC = pdb::NativeSession::createFromPdb(
297 MemoryBuffer::getMemBuffer(Driver->takeBuffer(std::move(*MBOrErr)),
298 /*RequiresNullTerminator=*/false),
299 ThisSession))
Reid Kleckner651db912017-07-18 00:21:25 +0000300 return std::move(EC);
301
302 std::unique_ptr<pdb::NativeSession> NS(
303 static_cast<pdb::NativeSession *>(ThisSession.release()));
304 pdb::PDBFile &File = NS->getPDBFile();
305 auto ExpectedInfo = File.getPDBInfoStream();
306 // All PDB Files should have an Info stream.
307 if (!ExpectedInfo)
308 return ExpectedInfo.takeError();
309
310 // Just because a file with a matching name was found and it was an actual
311 // PDB file doesn't mean it matches. For it to match the InfoStream's GUID
312 // must match the GUID specified in the TypeServer2 record.
313 if (ExpectedInfo->getGuid() != GuidFromObj)
314 return make_error<pdb::GenericError>(
315 pdb::generic_error_code::type_server_not_found, TSPath);
316
317 return std::move(NS);
318}
319
Rui Ueyamae1b48e02017-07-26 23:05:24 +0000320const CVIndexMap &PDBLinker::maybeMergeTypeServerPDB(ObjFile *File,
Reid Kleckner651db912017-07-18 00:21:25 +0000321 TypeServer2Record &TS) {
322 // First, check if we already loaded a PDB with this GUID. Return the type
323 // index mapping if we have it.
324 auto Insertion = TypeServerIndexMappings.insert({TS.getGuid(), CVIndexMap()});
325 CVIndexMap &IndexMap = Insertion.first->second;
326 if (!Insertion.second)
327 return IndexMap;
328
329 // Mark this map as a type server map.
330 IndexMap.IsTypeServerMap = true;
331
332 // Check for a PDB at:
333 // 1. The given file path
334 // 2. Next to the object file or archive file
335 auto ExpectedSession = tryToLoadPDB(TS.getGuid(), TS.getName());
336 if (!ExpectedSession) {
337 consumeError(ExpectedSession.takeError());
338 StringRef LocalPath =
339 !File->ParentName.empty() ? File->ParentName : File->getName();
340 SmallString<128> Path = sys::path::parent_path(LocalPath);
Reid Klecknerf8522d82017-07-18 00:33:53 +0000341 sys::path::append(
342 Path, sys::path::filename(TS.getName(), sys::path::Style::windows));
Reid Kleckner651db912017-07-18 00:21:25 +0000343 ExpectedSession = tryToLoadPDB(TS.getGuid(), Path);
344 }
345 if (auto E = ExpectedSession.takeError())
Bob Haarmanb8a59c82017-10-25 22:28:38 +0000346 fatal("Type server PDB was not found: " + toString(std::move(E)));
Reid Kleckner651db912017-07-18 00:21:25 +0000347
Reid Kleckner651db912017-07-18 00:21:25 +0000348 auto ExpectedTpi = (*ExpectedSession)->getPDBFile().getPDBTpiStream();
349 if (auto E = ExpectedTpi.takeError())
Bob Haarmanb8a59c82017-10-25 22:28:38 +0000350 fatal("Type server does not have TPI stream: " + toString(std::move(E)));
Reid Kleckner651db912017-07-18 00:21:25 +0000351 auto ExpectedIpi = (*ExpectedSession)->getPDBFile().getPDBIpiStream();
352 if (auto E = ExpectedIpi.takeError())
Bob Haarmanb8a59c82017-10-25 22:28:38 +0000353 fatal("Type server does not have TPI stream: " + toString(std::move(E)));
Zachary Turner0d07a8e2017-12-14 18:07:04 +0000354
355 if (Config->DebugGHashes) {
356 // PDBs do not actually store global hashes, so when merging a type server
357 // PDB we have to synthesize global hashes. To do this, we first synthesize
358 // global hashes for the TPI stream, since it is independent, then we
359 // synthesize hashes for the IPI stream, using the hashes for the TPI stream
360 // as inputs.
361 auto TpiHashes = GloballyHashedType::hashTypes(ExpectedTpi->typeArray());
362 auto IpiHashes =
363 GloballyHashedType::hashIds(ExpectedIpi->typeArray(), TpiHashes);
364
365 // Merge TPI first, because the IPI stream will reference type indices.
366 if (auto Err = mergeTypeRecords(GlobalTypeTable, IndexMap.TPIMap,
367 ExpectedTpi->typeArray(), TpiHashes))
368 fatal("codeview::mergeTypeRecords failed: " + toString(std::move(Err)));
369
370 // Merge IPI.
371 if (auto Err =
372 mergeIdRecords(GlobalIDTable, IndexMap.TPIMap, IndexMap.IPIMap,
373 ExpectedIpi->typeArray(), IpiHashes))
374 fatal("codeview::mergeIdRecords failed: " + toString(std::move(Err)));
375 } else {
376 // Merge TPI first, because the IPI stream will reference type indices.
377 if (auto Err = mergeTypeRecords(TypeTable, IndexMap.TPIMap,
378 ExpectedTpi->typeArray()))
379 fatal("codeview::mergeTypeRecords failed: " + toString(std::move(Err)));
380
381 // Merge IPI.
382 if (auto Err = mergeIdRecords(IDTable, IndexMap.TPIMap, IndexMap.IPIMap,
383 ExpectedIpi->typeArray()))
384 fatal("codeview::mergeIdRecords failed: " + toString(std::move(Err)));
385 }
Reid Kleckner651db912017-07-18 00:21:25 +0000386
387 return IndexMap;
Reid Kleckner44cdb102017-06-19 17:21:45 +0000388}
389
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000390static bool remapTypeIndex(TypeIndex &TI, ArrayRef<TypeIndex> TypeIndexMap) {
391 if (TI.isSimple())
392 return true;
393 if (TI.toArrayIndex() >= TypeIndexMap.size())
394 return false;
395 TI = TypeIndexMap[TI.toArrayIndex()];
396 return true;
397}
398
Reid Kleckner8aa32ff2017-10-24 17:02:40 +0000399static void remapTypesInSymbolRecord(ObjFile *File, SymbolKind SymKind,
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000400 MutableArrayRef<uint8_t> Contents,
Reid Kleckner651db912017-07-18 00:21:25 +0000401 const CVIndexMap &IndexMap,
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000402 ArrayRef<TiReference> TypeRefs) {
403 for (const TiReference &Ref : TypeRefs) {
404 unsigned ByteSize = Ref.Count * sizeof(TypeIndex);
Reid Kleckner03b5baf2017-07-12 18:49:43 +0000405 if (Contents.size() < Ref.Offset + ByteSize)
406 fatal("symbol record too short");
Reid Kleckner651db912017-07-18 00:21:25 +0000407
408 // This can be an item index or a type index. Choose the appropriate map.
409 ArrayRef<TypeIndex> TypeOrItemMap = IndexMap.TPIMap;
Reid Kleckner8aa32ff2017-10-24 17:02:40 +0000410 bool IsItemIndex = Ref.Kind == TiRefKind::IndexRef;
411 if (IsItemIndex && IndexMap.IsTypeServerMap)
Reid Kleckner651db912017-07-18 00:21:25 +0000412 TypeOrItemMap = IndexMap.IPIMap;
413
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000414 MutableArrayRef<TypeIndex> TIs(
415 reinterpret_cast<TypeIndex *>(Contents.data() + Ref.Offset), Ref.Count);
Reid Kleckner03b5baf2017-07-12 18:49:43 +0000416 for (TypeIndex &TI : TIs) {
Reid Kleckner651db912017-07-18 00:21:25 +0000417 if (!remapTypeIndex(TI, TypeOrItemMap)) {
Reid Kleckner8aa32ff2017-10-24 17:02:40 +0000418 log("ignoring symbol record of kind 0x" + utohexstr(SymKind) + " in " +
419 File->getName() + " with bad " + (IsItemIndex ? "item" : "type") +
420 " index 0x" + utohexstr(TI.getIndex()));
Reid Kleckner5d82f682017-10-23 22:44:51 +0000421 TI = TypeIndex(SimpleTypeKind::NotTranslated);
Reid Kleckner03b5baf2017-07-12 18:49:43 +0000422 continue;
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000423 }
Reid Kleckner03b5baf2017-07-12 18:49:43 +0000424 }
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000425 }
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000426}
427
Zachary Turner60478582018-01-05 19:12:40 +0000428static void
429recordStringTableReferenceAtOffset(MutableArrayRef<uint8_t> Contents,
430 uint32_t Offset,
431 std::vector<ulittle32_t *> &StrTableRefs) {
432 Contents =
433 Contents.drop_front(Offset).take_front(sizeof(support::ulittle32_t));
434 ulittle32_t *Index = reinterpret_cast<ulittle32_t *>(Contents.data());
435 StrTableRefs.push_back(Index);
436}
437
438static void
439recordStringTableReferences(SymbolKind Kind, MutableArrayRef<uint8_t> Contents,
440 std::vector<ulittle32_t *> &StrTableRefs) {
441 // For now we only handle S_FILESTATIC, but we may need the same logic for
442 // S_DEFRANGE and S_DEFRANGE_SUBFIELD. However, I cannot seem to generate any
443 // PDBs that contain these types of records, so because of the uncertainty
444 // they are omitted here until we can prove that it's necessary.
445 switch (Kind) {
446 case SymbolKind::S_FILESTATIC:
447 // FileStaticSym::ModFileOffset
448 recordStringTableReferenceAtOffset(Contents, 4, StrTableRefs);
449 break;
450 case SymbolKind::S_DEFRANGE:
451 case SymbolKind::S_DEFRANGE_SUBFIELD:
452 log("Not fixing up string table reference in S_DEFRANGE / "
453 "S_DEFRANGE_SUBFIELD record");
454 break;
Zachary Turner9e52e502018-01-05 19:28:39 +0000455 default:
456 break;
Zachary Turner60478582018-01-05 19:12:40 +0000457 }
458}
459
Zachary Turner59e3ae82017-08-08 18:34:44 +0000460static SymbolKind symbolKind(ArrayRef<uint8_t> RecordData) {
461 const RecordPrefix *Prefix =
462 reinterpret_cast<const RecordPrefix *>(RecordData.data());
463 return static_cast<SymbolKind>(uint16_t(Prefix->RecordKind));
464}
465
466/// MSVC translates S_PROC_ID_END to S_END, and S_[LG]PROC32_ID to S_[LG]PROC32
467static void translateIdSymbols(MutableArrayRef<uint8_t> &RecordData,
Zachary Turner3e3936d2017-11-29 19:35:21 +0000468 TypeCollection &IDTable) {
Zachary Turner59e3ae82017-08-08 18:34:44 +0000469 RecordPrefix *Prefix = reinterpret_cast<RecordPrefix *>(RecordData.data());
470
471 SymbolKind Kind = symbolKind(RecordData);
472
473 if (Kind == SymbolKind::S_PROC_ID_END) {
474 Prefix->RecordKind = SymbolKind::S_END;
475 return;
476 }
477
478 // In an object file, GPROC32_ID has an embedded reference which refers to the
479 // single object file type index namespace. This has already been translated
480 // to the PDB file's ID stream index space, but we need to convert this to a
481 // symbol that refers to the type stream index space. So we remap again from
482 // ID index space to type index space.
483 if (Kind == SymbolKind::S_GPROC32_ID || Kind == SymbolKind::S_LPROC32_ID) {
484 SmallVector<TiReference, 1> Refs;
485 auto Content = RecordData.drop_front(sizeof(RecordPrefix));
486 CVSymbol Sym(Kind, RecordData);
487 discoverTypeIndicesInSymbol(Sym, Refs);
488 assert(Refs.size() == 1);
489 assert(Refs.front().Count == 1);
490
491 TypeIndex *TI =
492 reinterpret_cast<TypeIndex *>(Content.data() + Refs[0].Offset);
493 // `TI` is the index of a FuncIdRecord or MemberFuncIdRecord which lives in
494 // the IPI stream, whose `FunctionType` member refers to the TPI stream.
495 // Note that LF_FUNC_ID and LF_MEMFUNC_ID have the same record layout, and
496 // in both cases we just need the second type index.
497 if (!TI->isSimple() && !TI->isNoneType()) {
Zachary Turner3e3936d2017-11-29 19:35:21 +0000498 CVType FuncIdData = IDTable.getType(*TI);
Zachary Turner59e3ae82017-08-08 18:34:44 +0000499 SmallVector<TypeIndex, 2> Indices;
500 discoverTypeIndices(FuncIdData, Indices);
501 assert(Indices.size() == 2);
502 *TI = Indices[1];
503 }
504
505 Kind = (Kind == SymbolKind::S_GPROC32_ID) ? SymbolKind::S_GPROC32
506 : SymbolKind::S_LPROC32;
507 Prefix->RecordKind = uint16_t(Kind);
508 }
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000509}
510
511/// Copy the symbol record. In a PDB, symbol records must be 4 byte aligned.
512/// The object file may not be aligned.
513static MutableArrayRef<uint8_t> copySymbolForPdb(const CVSymbol &Sym,
514 BumpPtrAllocator &Alloc) {
515 size_t Size = alignTo(Sym.length(), alignOf(CodeViewContainer::Pdb));
516 assert(Size >= 4 && "record too short");
517 assert(Size <= MaxRecordLength && "record too long");
518 void *Mem = Alloc.Allocate(Size, 4);
519
520 // Copy the symbol record and zero out any padding bytes.
521 MutableArrayRef<uint8_t> NewData(reinterpret_cast<uint8_t *>(Mem), Size);
522 memcpy(NewData.data(), Sym.data().data(), Sym.length());
523 memset(NewData.data() + Sym.length(), 0, Size - Sym.length());
524
525 // Update the record prefix length. It should point to the beginning of the
Zachary Turner59e3ae82017-08-08 18:34:44 +0000526 // next record.
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000527 auto *Prefix = reinterpret_cast<RecordPrefix *>(Mem);
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000528 Prefix->RecordLen = Size - 2;
529 return NewData;
530}
531
Reid Kleckner3f851922017-07-06 16:39:32 +0000532/// Return true if this symbol opens a scope. This implies that the symbol has
533/// "parent" and "end" fields, which contain the offset of the S_END or
534/// S_INLINESITE_END record.
535static bool symbolOpensScope(SymbolKind Kind) {
536 switch (Kind) {
537 case SymbolKind::S_GPROC32:
538 case SymbolKind::S_LPROC32:
539 case SymbolKind::S_LPROC32_ID:
540 case SymbolKind::S_GPROC32_ID:
541 case SymbolKind::S_BLOCK32:
542 case SymbolKind::S_SEPCODE:
543 case SymbolKind::S_THUNK32:
544 case SymbolKind::S_INLINESITE:
545 case SymbolKind::S_INLINESITE2:
546 return true;
547 default:
548 break;
549 }
550 return false;
551}
552
553static bool symbolEndsScope(SymbolKind Kind) {
554 switch (Kind) {
555 case SymbolKind::S_END:
556 case SymbolKind::S_PROC_ID_END:
557 case SymbolKind::S_INLINESITE_END:
558 return true;
559 default:
560 break;
561 }
562 return false;
563}
564
565struct ScopeRecord {
566 ulittle32_t PtrParent;
567 ulittle32_t PtrEnd;
568};
569
570struct SymbolScope {
571 ScopeRecord *OpeningRecord;
572 uint32_t ScopeOffset;
573};
574
575static void scopeStackOpen(SmallVectorImpl<SymbolScope> &Stack,
576 uint32_t CurOffset, CVSymbol &Sym) {
577 assert(symbolOpensScope(Sym.kind()));
578 SymbolScope S;
579 S.ScopeOffset = CurOffset;
580 S.OpeningRecord = const_cast<ScopeRecord *>(
581 reinterpret_cast<const ScopeRecord *>(Sym.content().data()));
582 S.OpeningRecord->PtrParent = Stack.empty() ? 0 : Stack.back().ScopeOffset;
583 Stack.push_back(S);
584}
585
586static void scopeStackClose(SmallVectorImpl<SymbolScope> &Stack,
Rui Ueyamae1b48e02017-07-26 23:05:24 +0000587 uint32_t CurOffset, ObjFile *File) {
Reid Kleckner3f851922017-07-06 16:39:32 +0000588 if (Stack.empty()) {
589 warn("symbol scopes are not balanced in " + File->getName());
590 return;
591 }
592 SymbolScope S = Stack.pop_back_val();
593 S.OpeningRecord->PtrEnd = CurOffset;
594}
595
Zachary Turneree9906d2017-08-11 19:00:03 +0000596static bool symbolGoesInModuleStream(const CVSymbol &Sym) {
597 switch (Sym.kind()) {
598 case SymbolKind::S_GDATA32:
599 case SymbolKind::S_CONSTANT:
600 case SymbolKind::S_UDT:
601 // We really should not be seeing S_PROCREF and S_LPROCREF in the first place
602 // since they are synthesized by the linker in response to S_GPROC32 and
603 // S_LPROC32, but if we do see them, don't put them in the module stream I
604 // guess.
605 case SymbolKind::S_PROCREF:
606 case SymbolKind::S_LPROCREF:
607 return false;
608 // S_GDATA32 does not go in the module stream, but S_LDATA32 does.
609 case SymbolKind::S_LDATA32:
610 default:
611 return true;
612 }
613}
614
615static bool symbolGoesInGlobalsStream(const CVSymbol &Sym) {
616 switch (Sym.kind()) {
617 case SymbolKind::S_CONSTANT:
Zachary Turneree9906d2017-08-11 19:00:03 +0000618 case SymbolKind::S_GDATA32:
619 // S_LDATA32 goes in both the module stream and the globals stream.
620 case SymbolKind::S_LDATA32:
621 case SymbolKind::S_GPROC32:
622 case SymbolKind::S_LPROC32:
623 // We really should not be seeing S_PROCREF and S_LPROCREF in the first place
624 // since they are synthesized by the linker in response to S_GPROC32 and
625 // S_LPROC32, but if we do see them, copy them straight through.
626 case SymbolKind::S_PROCREF:
627 case SymbolKind::S_LPROCREF:
628 return true;
Zachary Turner302dc8b2017-08-14 18:44:58 +0000629 // FIXME: For now, we drop all S_UDT symbols (i.e. they don't go in the
630 // globals stream or the modules stream). These have special handling which
631 // needs more investigation before we can get right, but by putting them all
632 // into the globals stream WinDbg fails to display local variables of class
633 // types saying that it cannot find the type Foo *. So as a stopgap just to
634 // keep things working, we drop them.
635 case SymbolKind::S_UDT:
Zachary Turneree9906d2017-08-11 19:00:03 +0000636 default:
637 return false;
638 }
639}
640
641static void addGlobalSymbol(pdb::GSIStreamBuilder &Builder, ObjFile &File,
642 const CVSymbol &Sym) {
643 switch (Sym.kind()) {
644 case SymbolKind::S_CONSTANT:
645 case SymbolKind::S_UDT:
646 case SymbolKind::S_GDATA32:
647 case SymbolKind::S_LDATA32:
648 case SymbolKind::S_PROCREF:
649 case SymbolKind::S_LPROCREF:
650 Builder.addGlobalSymbol(Sym);
651 break;
652 case SymbolKind::S_GPROC32:
653 case SymbolKind::S_LPROC32: {
654 SymbolRecordKind K = SymbolRecordKind::ProcRefSym;
655 if (Sym.kind() == SymbolKind::S_LPROC32)
656 K = SymbolRecordKind::LocalProcRef;
657 ProcRefSym PS(K);
658 PS.Module = static_cast<uint16_t>(File.ModuleDBI->getModuleIndex());
659 // For some reason, MSVC seems to add one to this value.
660 ++PS.Module;
661 PS.Name = getSymbolName(Sym);
662 PS.SumName = 0;
663 PS.SymOffset = File.ModuleDBI->getNextSymbolOffset();
664 Builder.addGlobalSymbol(PS);
665 break;
666 }
667 default:
668 llvm_unreachable("Invalid symbol kind!");
669 }
670}
671
Rui Ueyamae1b48e02017-07-26 23:05:24 +0000672static void mergeSymbolRecords(BumpPtrAllocator &Alloc, ObjFile *File,
Zachary Turneree9906d2017-08-11 19:00:03 +0000673 pdb::GSIStreamBuilder &GsiBuilder,
Reid Kleckner651db912017-07-18 00:21:25 +0000674 const CVIndexMap &IndexMap,
Zachary Turner3e3936d2017-11-29 19:35:21 +0000675 TypeCollection &IDTable,
Zachary Turner60478582018-01-05 19:12:40 +0000676 std::vector<ulittle32_t *> &StringTableRefs,
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000677 BinaryStreamRef SymData) {
678 // FIXME: Improve error recovery by warning and skipping records when
679 // possible.
680 CVSymbolArray Syms;
681 BinaryStreamReader Reader(SymData);
682 ExitOnErr(Reader.readArray(Syms, Reader.getLength()));
Reid Kleckner3f851922017-07-06 16:39:32 +0000683 SmallVector<SymbolScope, 4> Scopes;
Zachary Turner59e3ae82017-08-08 18:34:44 +0000684 for (CVSymbol Sym : Syms) {
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000685 // Discover type index references in the record. Skip it if we don't know
686 // where they are.
687 SmallVector<TiReference, 32> TypeRefs;
Zachary Turner59e3ae82017-08-08 18:34:44 +0000688 if (!discoverTypeIndicesInSymbol(Sym, TypeRefs)) {
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000689 log("ignoring unknown symbol record with kind 0x" + utohexstr(Sym.kind()));
690 continue;
691 }
692
693 // Copy the symbol record so we can mutate it.
694 MutableArrayRef<uint8_t> NewData = copySymbolForPdb(Sym, Alloc);
695
696 // Re-map all the type index references.
697 MutableArrayRef<uint8_t> Contents =
698 NewData.drop_front(sizeof(RecordPrefix));
Zachary Turner3e3936d2017-11-29 19:35:21 +0000699 remapTypesInSymbolRecord(File, Sym.kind(), Contents, IndexMap, TypeRefs);
Zachary Turner59e3ae82017-08-08 18:34:44 +0000700
701 // An object file may have S_xxx_ID symbols, but these get converted to
702 // "real" symbols in a PDB.
703 translateIdSymbols(NewData, IDTable);
704
Zachary Turner60478582018-01-05 19:12:40 +0000705 // If this record refers to an offset in the object file's string table,
706 // add that item to the global PDB string table and re-write the index.
707 recordStringTableReferences(Sym.kind(), Contents, StringTableRefs);
708
Zachary Turner59e3ae82017-08-08 18:34:44 +0000709 SymbolKind NewKind = symbolKind(NewData);
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000710
Reid Kleckner3f851922017-07-06 16:39:32 +0000711 // Fill in "Parent" and "End" fields by maintaining a stack of scopes.
Zachary Turner59e3ae82017-08-08 18:34:44 +0000712 CVSymbol NewSym(NewKind, NewData);
713 if (symbolOpensScope(NewKind))
Reid Kleckner3f851922017-07-06 16:39:32 +0000714 scopeStackOpen(Scopes, File->ModuleDBI->getNextSymbolOffset(), NewSym);
Zachary Turner59e3ae82017-08-08 18:34:44 +0000715 else if (symbolEndsScope(NewKind))
Reid Kleckner3f851922017-07-06 16:39:32 +0000716 scopeStackClose(Scopes, File->ModuleDBI->getNextSymbolOffset(), File);
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000717
Zachary Turneree9906d2017-08-11 19:00:03 +0000718 // Add the symbol to the globals stream if necessary. Do this before adding
719 // the symbol to the module since we may need to get the next symbol offset,
720 // and writing to the module's symbol stream will update that offset.
721 if (symbolGoesInGlobalsStream(NewSym))
722 addGlobalSymbol(GsiBuilder, *File, NewSym);
723
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000724 // Add the symbol to the module.
Zachary Turneree9906d2017-08-11 19:00:03 +0000725 if (symbolGoesInModuleStream(NewSym))
726 File->ModuleDBI->addSymbol(NewSym);
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000727 }
728}
729
Reid Kleckner44cdb102017-06-19 17:21:45 +0000730// Allocate memory for a .debug$S section and relocate it.
731static ArrayRef<uint8_t> relocateDebugChunk(BumpPtrAllocator &Alloc,
732 SectionChunk *DebugChunk) {
733 uint8_t *Buffer = Alloc.Allocate<uint8_t>(DebugChunk->getSize());
734 assert(DebugChunk->OutputSectionOff == 0 &&
735 "debug sections should not be in output sections");
736 DebugChunk->writeTo(Buffer);
737 return consumeDebugMagic(makeArrayRef(Buffer, DebugChunk->getSize()),
738 ".debug$S");
739}
740
Rui Ueyamae1b48e02017-07-26 23:05:24 +0000741void PDBLinker::addObjFile(ObjFile *File) {
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000742 // Add a module descriptor for every object file. We need to put an absolute
743 // path to the object into the PDB. If this is a plain object, we make its
744 // path absolute. If it's an object in an archive, we make the archive path
745 // absolute.
746 bool InArchive = !File->ParentName.empty();
747 SmallString<128> Path = InArchive ? File->ParentName : File->getName();
748 sys::fs::make_absolute(Path);
749 sys::path::native(Path, sys::path::Style::windows);
750 StringRef Name = InArchive ? File->getName() : StringRef(Path);
Zachary Turner2897e032017-05-25 21:16:03 +0000751
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000752 File->ModuleDBI = &ExitOnErr(Builder.getDbiBuilder().addModuleInfo(Name));
753 File->ModuleDBI->setObjFileName(Path);
Reid Kleckner44cdb102017-06-19 17:21:45 +0000754
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000755 // Before we can process symbol substreams from .debug$S, we need to process
756 // type information, file checksums, and the string table. Add type info to
757 // the PDB first, so that we can get the map from object file type and item
758 // indices to PDB type and item indices.
Reid Kleckner651db912017-07-18 00:21:25 +0000759 CVIndexMap ObjectIndexMap;
760 const CVIndexMap &IndexMap = mergeDebugT(File, ObjectIndexMap);
Zachary Turner448dea42017-07-07 18:46:14 +0000761
Zachary Turner727f1532018-01-17 19:16:26 +0000762 ScopedTimer T(SymbolMergingTimer);
763
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000764 // Now do all live .debug$S sections.
Zachary Turner60478582018-01-05 19:12:40 +0000765 DebugStringTableSubsectionRef CVStrTab;
766 DebugChecksumsSubsectionRef Checksums;
767 std::vector<ulittle32_t *> StringTableReferences;
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000768 for (SectionChunk *DebugChunk : File->getDebugChunks()) {
769 if (!DebugChunk->isLive() || DebugChunk->getSectionName() != ".debug$S")
770 continue;
Reid Kleckner8cbdd0c2017-06-13 15:49:13 +0000771
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000772 ArrayRef<uint8_t> RelocatedDebugContents =
773 relocateDebugChunk(Alloc, DebugChunk);
774 if (RelocatedDebugContents.empty())
775 continue;
Reid Kleckner8cbdd0c2017-06-13 15:49:13 +0000776
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000777 DebugSubsectionArray Subsections;
778 BinaryStreamReader Reader(RelocatedDebugContents, support::little);
779 ExitOnErr(Reader.readArray(Subsections, RelocatedDebugContents.size()));
Rui Ueyama52896622017-01-12 03:09:25 +0000780
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000781 for (const DebugSubsectionRecord &SS : Subsections) {
782 switch (SS.kind()) {
Zachary Turner60478582018-01-05 19:12:40 +0000783 case DebugSubsectionKind::StringTable: {
784 auto Data = SS.getRecordData();
785 ArrayRef<uint8_t> Buffer;
786 cantFail(Data.readLongestContiguousChunk(0, Buffer));
787 assert(!CVStrTab.valid() &&
788 "Encountered multiple string table subsections!");
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000789 ExitOnErr(CVStrTab.initialize(SS.getRecordData()));
790 break;
Zachary Turner60478582018-01-05 19:12:40 +0000791 }
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000792 case DebugSubsectionKind::FileChecksums:
Zachary Turner60478582018-01-05 19:12:40 +0000793 assert(!Checksums.valid() &&
794 "Encountered multiple checksum subsections!");
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000795 ExitOnErr(Checksums.initialize(SS.getRecordData()));
796 break;
797 case DebugSubsectionKind::Lines:
798 // We can add the relocated line table directly to the PDB without
799 // modification because the file checksum offsets will stay the same.
800 File->ModuleDBI->addDebugSubsection(SS);
801 break;
802 case DebugSubsectionKind::Symbols:
Zachary Turner0d07a8e2017-12-14 18:07:04 +0000803 if (Config->DebugGHashes) {
804 mergeSymbolRecords(Alloc, File, Builder.getGsiBuilder(), IndexMap,
Zachary Turner60478582018-01-05 19:12:40 +0000805 GlobalIDTable, StringTableReferences,
806 SS.getRecordData());
Zachary Turner0d07a8e2017-12-14 18:07:04 +0000807 } else {
808 mergeSymbolRecords(Alloc, File, Builder.getGsiBuilder(), IndexMap,
Zachary Turner60478582018-01-05 19:12:40 +0000809 IDTable, StringTableReferences,
810 SS.getRecordData());
Zachary Turner0d07a8e2017-12-14 18:07:04 +0000811 }
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000812 break;
813 default:
814 // FIXME: Process the rest of the subsections.
815 break;
Reid Kleckner44cdb102017-06-19 17:21:45 +0000816 }
817 }
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000818 }
Zachary Turner60478582018-01-05 19:12:40 +0000819
820 // We should have seen all debug subsections across the entire object file now
821 // which means that if a StringTable subsection and Checksums subsection were
822 // present, now is the time to handle them.
823 if (!CVStrTab.valid()) {
824 if (Checksums.valid())
825 fatal(".debug$S sections with a checksums subsection must also contain a "
826 "string table subsection");
827
828 if (!StringTableReferences.empty())
829 warn("No StringTable subsection was encountered, but there are string "
830 "table references");
831 return;
832 }
833
834 // Rewrite each string table reference based on the value that the string
835 // assumes in the final PDB.
836 for (ulittle32_t *Ref : StringTableReferences) {
837 auto ExpectedString = CVStrTab.getString(*Ref);
838 if (!ExpectedString) {
839 warn("Invalid string table reference");
840 consumeError(ExpectedString.takeError());
841 continue;
842 }
843
844 *Ref = PDBStrTab.insert(*ExpectedString);
845 }
846
847 // Make a new file checksum table that refers to offsets in the PDB-wide
848 // string table. Generally the string table subsection appears after the
849 // checksum table, so we have to do this after looping over all the
850 // subsections.
851 auto NewChecksums = make_unique<DebugChecksumsSubsection>(PDBStrTab);
852 for (FileChecksumEntry &FC : Checksums) {
853 StringRef FileName = ExitOnErr(CVStrTab.getString(FC.FileNameOffset));
854 ExitOnErr(Builder.getDbiBuilder().addModuleSourceFile(*File->ModuleDBI,
855 FileName));
856 NewChecksums->addChecksum(FileName, FC.Kind, FC.Checksum);
857 }
858 File->ModuleDBI->addDebugSubsection(std::move(NewChecksums));
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000859}
860
Reid Klecknereacdf042017-07-27 18:25:59 +0000861static PublicSym32 createPublic(Defined *Def) {
862 PublicSym32 Pub(SymbolKind::S_PUB32);
863 Pub.Name = Def->getName();
864 if (auto *D = dyn_cast<DefinedCOFF>(Def)) {
865 if (D->getCOFFSymbol().isFunctionDefinition())
866 Pub.Flags = PublicSymFlags::Function;
867 } else if (isa<DefinedImportThunk>(Def)) {
868 Pub.Flags = PublicSymFlags::Function;
869 }
870
871 OutputSection *OS = Def->getChunk()->getOutputSection();
872 assert(OS && "all publics should be in final image");
873 Pub.Offset = Def->getRVA() - OS->getRVA();
874 Pub.Segment = OS->SectionIndex;
875 return Pub;
876}
877
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000878// Add all object files to the PDB. Merge .debug$T sections into IpiData and
879// TpiData.
880void PDBLinker::addObjectsToPDB() {
Zachary Turner727f1532018-01-17 19:16:26 +0000881 ScopedTimer T1(AddObjectsTimer);
Rui Ueyamaacd632d2017-07-27 00:45:26 +0000882 for (ObjFile *File : ObjFile::Instances)
Rui Ueyamae1b48e02017-07-26 23:05:24 +0000883 addObjFile(File);
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000884
885 Builder.getStringTableBuilder().setStrings(PDBStrTab);
Zachary Turner727f1532018-01-17 19:16:26 +0000886 T1.stop();
Reid Kleckner44cdb102017-06-19 17:21:45 +0000887
Zachary Turner0d07a8e2017-12-14 18:07:04 +0000888 // Construct TPI and IPI stream contents.
Zachary Turner727f1532018-01-17 19:16:26 +0000889 ScopedTimer T2(TpiStreamLayoutTimer);
Zachary Turner0d07a8e2017-12-14 18:07:04 +0000890 if (Config->DebugGHashes) {
891 addTypeInfo(Builder.getTpiBuilder(), GlobalTypeTable);
892 addTypeInfo(Builder.getIpiBuilder(), GlobalIDTable);
893 } else {
894 addTypeInfo(Builder.getTpiBuilder(), TypeTable);
895 addTypeInfo(Builder.getIpiBuilder(), IDTable);
896 }
Zachary Turner727f1532018-01-17 19:16:26 +0000897 T2.stop();
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000898
Zachary Turner727f1532018-01-17 19:16:26 +0000899 ScopedTimer T3(GlobalsLayoutTimer);
Zachary Turneree9906d2017-08-11 19:00:03 +0000900 // Compute the public and global symbols.
901 auto &GsiBuilder = Builder.getGsiBuilder();
Reid Klecknereacdf042017-07-27 18:25:59 +0000902 std::vector<PublicSym32> Publics;
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000903 Symtab->forEachSymbol([&Publics](Symbol *S) {
Reid Klecknereacdf042017-07-27 18:25:59 +0000904 // Only emit defined, live symbols that have a chunk.
Rui Ueyama616cd992017-10-31 16:10:24 +0000905 auto *Def = dyn_cast<Defined>(S);
Reid Klecknereacdf042017-07-27 18:25:59 +0000906 if (Def && Def->isLive() && Def->getChunk())
907 Publics.push_back(createPublic(Def));
908 });
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000909
Reid Klecknereacdf042017-07-27 18:25:59 +0000910 if (!Publics.empty()) {
911 // Sort the public symbols and add them to the stream.
912 std::sort(Publics.begin(), Publics.end(),
913 [](const PublicSym32 &L, const PublicSym32 &R) {
914 return L.Name < R.Name;
915 });
Reid Klecknereacdf042017-07-27 18:25:59 +0000916 for (const PublicSym32 &Pub : Publics)
Zachary Turner946204c2017-08-09 04:23:25 +0000917 GsiBuilder.addPublicSymbol(Pub);
Reid Klecknereacdf042017-07-27 18:25:59 +0000918 }
Rui Ueyama52896622017-01-12 03:09:25 +0000919}
920
Zachary Turner28e31ee2017-08-11 20:46:28 +0000921static void addCommonLinkerModuleSymbols(StringRef Path,
922 pdb::DbiModuleDescriptorBuilder &Mod,
923 BumpPtrAllocator &Allocator) {
Zachary Turner28e31ee2017-08-11 20:46:28 +0000924 ObjNameSym ONS(SymbolRecordKind::ObjNameSym);
925 Compile3Sym CS(SymbolRecordKind::Compile3Sym);
926 EnvBlockSym EBS(SymbolRecordKind::EnvBlockSym);
Zachary Turner6708e0b2017-07-10 21:01:37 +0000927
928 ONS.Name = "* Linker *";
929 ONS.Signature = 0;
930
931 CS.Machine = Config->is64() ? CPUType::X64 : CPUType::Intel80386;
Zachary Turnerb57884e2017-08-11 21:14:01 +0000932 // Interestingly, if we set the string to 0.0.0.0, then when trying to view
933 // local variables WinDbg emits an error that private symbols are not present.
934 // By setting this to a valid MSVC linker version string, local variables are
935 // displayed properly. As such, even though it is not representative of
Zachary Turnerf6822c82017-08-11 20:46:47 +0000936 // LLVM's version information, we need this for compatibility.
Zachary Turner6708e0b2017-07-10 21:01:37 +0000937 CS.Flags = CompileSym3Flags::None;
Zachary Turnerf6822c82017-08-11 20:46:47 +0000938 CS.VersionBackendBuild = 25019;
939 CS.VersionBackendMajor = 14;
940 CS.VersionBackendMinor = 10;
Zachary Turner6708e0b2017-07-10 21:01:37 +0000941 CS.VersionBackendQFE = 0;
Zachary Turnerf6822c82017-08-11 20:46:47 +0000942
943 // MSVC also sets the frontend to 0.0.0.0 since this is specifically for the
944 // linker module (which is by definition a backend), so we don't need to do
945 // anything here. Also, it seems we can use "LLVM Linker" for the linker name
946 // without any problems. Only the backend version has to be hardcoded to a
947 // magic number.
Zachary Turner6708e0b2017-07-10 21:01:37 +0000948 CS.VersionFrontendBuild = 0;
949 CS.VersionFrontendMajor = 0;
950 CS.VersionFrontendMinor = 0;
951 CS.VersionFrontendQFE = 0;
952 CS.Version = "LLVM Linker";
953 CS.setLanguage(SourceLanguage::Link);
954
955 ArrayRef<StringRef> Args = makeArrayRef(Config->Argv).drop_front();
956 std::string ArgStr = llvm::join(Args, " ");
957 EBS.Fields.push_back("cwd");
958 SmallString<64> cwd;
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000959 sys::fs::current_path(cwd);
Zachary Turner6708e0b2017-07-10 21:01:37 +0000960 EBS.Fields.push_back(cwd);
961 EBS.Fields.push_back("exe");
Zachary Turnerf6822c82017-08-11 20:46:47 +0000962 SmallString<64> exe = Config->Argv[0];
963 llvm::sys::fs::make_absolute(exe);
964 EBS.Fields.push_back(exe);
Zachary Turner6708e0b2017-07-10 21:01:37 +0000965 EBS.Fields.push_back("pdb");
966 EBS.Fields.push_back(Path);
967 EBS.Fields.push_back("cmd");
968 EBS.Fields.push_back(ArgStr);
969 Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(
970 ONS, Allocator, CodeViewContainer::Pdb));
971 Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(
972 CS, Allocator, CodeViewContainer::Pdb));
973 Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(
974 EBS, Allocator, CodeViewContainer::Pdb));
975}
976
Zachary Turner28e31ee2017-08-11 20:46:28 +0000977static void addLinkerModuleSectionSymbol(pdb::DbiModuleDescriptorBuilder &Mod,
978 OutputSection &OS,
979 BumpPtrAllocator &Allocator) {
980 SectionSym Sym(SymbolRecordKind::SectionSym);
Zachary Turnerf6822c82017-08-11 20:46:47 +0000981 Sym.Alignment = 12; // 2^12 = 4KB
Zachary Turner28e31ee2017-08-11 20:46:28 +0000982 Sym.Characteristics = OS.getCharacteristics();
983 Sym.Length = OS.getVirtualSize();
984 Sym.Name = OS.getName();
985 Sym.Rva = OS.getRVA();
986 Sym.SectionNumber = OS.SectionIndex;
987 Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(
988 Sym, Allocator, CodeViewContainer::Pdb));
989}
990
Rui Ueyama09e0b5f2016-11-12 00:00:51 +0000991// Creates a PDB file.
Reid Kleckner175af4b2017-08-03 21:15:09 +0000992void coff::createPDB(SymbolTable *Symtab,
993 ArrayRef<OutputSection *> OutputSections,
994 ArrayRef<uint8_t> SectionTable,
Zachary Turner024323c2017-08-15 21:31:41 +0000995 const llvm::codeview::DebugInfo &BuildId) {
Zachary Turner727f1532018-01-17 19:16:26 +0000996 ScopedTimer T1(TotalPdbLinkTimer);
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000997 PDBLinker PDB(Symtab);
Zachary Turner024323c2017-08-15 21:31:41 +0000998 PDB.initialize(BuildId);
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000999 PDB.addObjectsToPDB();
Reid Kleckner175af4b2017-08-03 21:15:09 +00001000 PDB.addSections(OutputSections, SectionTable);
Zachary Turner727f1532018-01-17 19:16:26 +00001001
1002 ScopedTimer T2(DiskCommitTimer);
Reid Kleckner0faf6d72017-07-14 00:14:58 +00001003 PDB.commit();
1004}
1005
Zachary Turner024323c2017-08-15 21:31:41 +00001006void PDBLinker::initialize(const llvm::codeview::DebugInfo &BuildId) {
Rui Ueyama12979542016-09-30 20:53:45 +00001007 ExitOnErr(Builder.initialize(4096)); // 4096 is blocksize
Rui Ueyama7f382992016-09-15 18:55:18 +00001008
Rui Ueyama8d3fb5d2016-10-05 22:08:58 +00001009 // Create streams in MSF for predefined streams, namely
1010 // PDB, TPI, DBI and IPI.
1011 for (int I = 0; I < (int)pdb::kSpecialStreamCount; ++I)
1012 ExitOnErr(Builder.getMsfBuilder().addStream(0));
Rui Ueyama7f382992016-09-15 18:55:18 +00001013
Rui Ueyamabb542b32016-09-16 22:51:17 +00001014 // Add an Info stream.
1015 auto &InfoBuilder = Builder.getInfoBuilder();
Zachary Turner024323c2017-08-15 21:31:41 +00001016 InfoBuilder.setAge(BuildId.PDB70.Age);
Saleem Abdulrasool0acd6dd2017-02-07 04:28:02 +00001017
Zachary Turner024323c2017-08-15 21:31:41 +00001018 GUID uuid;
1019 memcpy(&uuid, &BuildId.PDB70.Signature, sizeof(uuid));
Saleem Abdulrasool0acd6dd2017-02-07 04:28:02 +00001020 InfoBuilder.setGuid(uuid);
Zachary Turner3a11fdf2017-07-07 20:25:39 +00001021 InfoBuilder.setSignature(time(nullptr));
Rui Ueyamabb542b32016-09-16 22:51:17 +00001022 InfoBuilder.setVersion(pdb::PdbRaw_ImplVer::PdbImplVC70);
Rui Ueyama7f382992016-09-15 18:55:18 +00001023
Zachary Turner6c4bfba2017-07-07 05:04:36 +00001024 // Add an empty DBI stream.
Reid Kleckner8cbdd0c2017-06-13 15:49:13 +00001025 pdb::DbiStreamBuilder &DbiBuilder = Builder.getDbiBuilder();
Zachary Turner024323c2017-08-15 21:31:41 +00001026 DbiBuilder.setAge(BuildId.PDB70.Age);
Zachary Turnerc1e93e52017-07-07 18:45:56 +00001027 DbiBuilder.setVersionHeader(pdb::PdbDbiV70);
Zachary Turner3a11fdf2017-07-07 20:25:39 +00001028 ExitOnErr(DbiBuilder.addDbgStream(pdb::DbgHeaderType::NewFPO, {}));
Reid Kleckner0faf6d72017-07-14 00:14:58 +00001029}
Rui Ueyama1343fac2016-10-06 22:52:01 +00001030
Reid Kleckner175af4b2017-08-03 21:15:09 +00001031void PDBLinker::addSectionContrib(pdb::DbiModuleDescriptorBuilder &LinkerModule,
1032 OutputSection *OS, Chunk *C) {
1033 pdb::SectionContrib SC;
1034 memset(&SC, 0, sizeof(SC));
1035 SC.ISect = OS->SectionIndex;
1036 SC.Off = C->getRVA() - OS->getRVA();
1037 SC.Size = C->getSize();
1038 if (auto *SecChunk = dyn_cast<SectionChunk>(C)) {
1039 SC.Characteristics = SecChunk->Header->Characteristics;
1040 SC.Imod = SecChunk->File->ModuleDBI->getModuleIndex();
Zachary Turner676386ff2017-08-07 20:23:45 +00001041 ArrayRef<uint8_t> Contents = SecChunk->getContents();
1042 JamCRC CRC(0);
1043 ArrayRef<char> CharContents = makeArrayRef(
1044 reinterpret_cast<const char *>(Contents.data()), Contents.size());
1045 CRC.update(CharContents);
1046 SC.DataCrc = CRC.getCRC();
Reid Kleckner175af4b2017-08-03 21:15:09 +00001047 } else {
1048 SC.Characteristics = OS->getCharacteristics();
1049 // FIXME: When we start creating DBI for import libraries, use those here.
1050 SC.Imod = LinkerModule.getModuleIndex();
1051 }
Reid Kleckner175af4b2017-08-03 21:15:09 +00001052 SC.RelocCrc = 0; // FIXME
1053 Builder.getDbiBuilder().addSectionContrib(SC);
1054}
Rui Ueyama09e0b5f2016-11-12 00:00:51 +00001055
Reid Kleckner175af4b2017-08-03 21:15:09 +00001056void PDBLinker::addSections(ArrayRef<OutputSection *> OutputSections,
1057 ArrayRef<uint8_t> SectionTable) {
Reid Kleckner0faf6d72017-07-14 00:14:58 +00001058 // It's not entirely clear what this is, but the * Linker * module uses it.
Reid Kleckner175af4b2017-08-03 21:15:09 +00001059 pdb::DbiStreamBuilder &DbiBuilder = Builder.getDbiBuilder();
Reid Kleckner0faf6d72017-07-14 00:14:58 +00001060 NativePath = Config->PDBPath;
1061 sys::fs::make_absolute(NativePath);
1062 sys::path::native(NativePath, sys::path::Style::windows);
1063 uint32_t PdbFilePathNI = DbiBuilder.addECName(NativePath);
Zachary Turner6c4bfba2017-07-07 05:04:36 +00001064 auto &LinkerModule = ExitOnErr(DbiBuilder.addModuleInfo("* Linker *"));
1065 LinkerModule.setPdbFilePathNI(PdbFilePathNI);
Zachary Turner28e31ee2017-08-11 20:46:28 +00001066 addCommonLinkerModuleSymbols(NativePath, LinkerModule, Alloc);
Rui Ueyamac91f7162016-11-16 01:10:46 +00001067
Reid Kleckner175af4b2017-08-03 21:15:09 +00001068 // Add section contributions. They must be ordered by ascending RVA.
Zachary Turner28e31ee2017-08-11 20:46:28 +00001069 for (OutputSection *OS : OutputSections) {
1070 addLinkerModuleSectionSymbol(LinkerModule, *OS, Alloc);
Reid Kleckner175af4b2017-08-03 21:15:09 +00001071 for (Chunk *C : OS->getChunks())
1072 addSectionContrib(LinkerModule, OS, C);
Zachary Turner28e31ee2017-08-11 20:46:28 +00001073 }
Reid Kleckner175af4b2017-08-03 21:15:09 +00001074
1075 // Add Section Map stream.
1076 ArrayRef<object::coff_section> Sections = {
1077 (const object::coff_section *)SectionTable.data(),
1078 SectionTable.size() / sizeof(object::coff_section)};
1079 SectionMap = pdb::DbiStreamBuilder::createSectionMap(Sections);
1080 DbiBuilder.setSectionMap(SectionMap);
1081
Rui Ueyama9f66f822016-10-11 19:45:07 +00001082 // Add COFF section header stream.
1083 ExitOnErr(
1084 DbiBuilder.addDbgStream(pdb::DbgHeaderType::SectionHdr, SectionTable));
Reid Kleckner0faf6d72017-07-14 00:14:58 +00001085}
Rui Ueyama9f66f822016-10-11 19:45:07 +00001086
Reid Kleckner0faf6d72017-07-14 00:14:58 +00001087void PDBLinker::commit() {
Rui Ueyama3e9d6bb2016-09-26 23:53:55 +00001088 // Write to a file.
Zachary Turner6708e0b2017-07-10 21:01:37 +00001089 ExitOnErr(Builder.commit(Config->PDBPath));
Rui Ueyamae7378242015-12-04 23:11:05 +00001090}