blob: 1a6516b59ee45ba19cfbb65a4b2fd1bc826d4f74 [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 Turner676386ff2017-08-07 20:23:45 +000050#include "llvm/Support/JamCRC.h"
Zachary Turner7b327d02017-02-16 23:35:45 +000051#include "llvm/Support/Path.h"
Rui Ueyamabe939b32016-11-21 17:22:35 +000052#include "llvm/Support/ScopedPrinter.h"
Rui Ueyamae7378242015-12-04 23:11:05 +000053#include <memory>
54
Rui Ueyama1d99ab32016-09-15 22:24:51 +000055using namespace lld;
Rui Ueyama09e0b5f2016-11-12 00:00:51 +000056using namespace lld::coff;
Rui Ueyamae7378242015-12-04 23:11:05 +000057using namespace llvm;
Rui Ueyamabe939b32016-11-21 17:22:35 +000058using namespace llvm::codeview;
Rui Ueyamae7378242015-12-04 23:11:05 +000059
Rui Ueyama09e0b5f2016-11-12 00:00:51 +000060using llvm::object::coff_section;
61
Rui Ueyamab28c6d42016-09-16 04:32:33 +000062static ExitOnError ExitOnErr;
63
Reid Kleckner0faf6d72017-07-14 00:14:58 +000064namespace {
Reid Kleckner651db912017-07-18 00:21:25 +000065/// Map from type index and item index in a type server PDB to the
66/// corresponding index in the destination PDB.
67struct CVIndexMap {
68 SmallVector<TypeIndex, 0> TPIMap;
69 SmallVector<TypeIndex, 0> IPIMap;
70 bool IsTypeServerMap = false;
71};
72
Reid Kleckner0faf6d72017-07-14 00:14:58 +000073class PDBLinker {
74public:
75 PDBLinker(SymbolTable *Symtab)
Zachary Turner0d07a8e2017-12-14 18:07:04 +000076 : Alloc(), Symtab(Symtab), Builder(Alloc), GlobalTypeTable(Alloc),
77 GlobalIDTable(Alloc), TypeTable(Alloc), IDTable(Alloc) {}
Reid Kleckner0faf6d72017-07-14 00:14:58 +000078
79 /// Emit the basic PDB structure: initial streams, headers, etc.
Zachary Turner024323c2017-08-15 21:31:41 +000080 void initialize(const llvm::codeview::DebugInfo &BuildId);
Reid Kleckner0faf6d72017-07-14 00:14:58 +000081
82 /// Link CodeView from each object file in the symbol table into the PDB.
83 void addObjectsToPDB();
84
85 /// Link CodeView from a single object file into the PDB.
Rui Ueyamae1b48e02017-07-26 23:05:24 +000086 void addObjFile(ObjFile *File);
Reid Kleckner0faf6d72017-07-14 00:14:58 +000087
Reid Kleckner651db912017-07-18 00:21:25 +000088 /// Produce a mapping from the type and item indices used in the object
89 /// file to those in the destination PDB.
90 ///
91 /// If the object file uses a type server PDB (compiled with /Zi), merge TPI
92 /// and IPI from the type server PDB and return a map for it. Each unique type
93 /// server PDB is merged at most once, so this may return an existing index
94 /// mapping.
95 ///
96 /// If the object does not use a type server PDB (compiled with /Z7), we merge
97 /// all the type and item records from the .debug$S stream and fill in the
98 /// caller-provided ObjectIndexMap.
Rui Ueyamae1b48e02017-07-26 23:05:24 +000099 const CVIndexMap &mergeDebugT(ObjFile *File, CVIndexMap &ObjectIndexMap);
Reid Kleckner651db912017-07-18 00:21:25 +0000100
Rui Ueyamae1b48e02017-07-26 23:05:24 +0000101 const CVIndexMap &maybeMergeTypeServerPDB(ObjFile *File,
Reid Kleckner651db912017-07-18 00:21:25 +0000102 TypeServer2Record &TS);
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000103
104 /// Add the section map and section contributions to the PDB.
Reid Kleckner175af4b2017-08-03 21:15:09 +0000105 void addSections(ArrayRef<OutputSection *> OutputSections,
106 ArrayRef<uint8_t> SectionTable);
107
108 void addSectionContrib(pdb::DbiModuleDescriptorBuilder &LinkerModule,
109 OutputSection *OS, Chunk *C);
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000110
111 /// Write the PDB to disk.
112 void commit();
113
114private:
115 BumpPtrAllocator Alloc;
116
117 SymbolTable *Symtab;
118
119 pdb::PDBFileBuilder Builder;
120
121 /// Type records that will go into the PDB TPI stream.
Zachary Turnerca6dbf12017-11-30 18:39:50 +0000122 MergingTypeTableBuilder TypeTable;
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000123
124 /// Item records that will go into the PDB IPI stream.
Zachary Turnerca6dbf12017-11-30 18:39:50 +0000125 MergingTypeTableBuilder IDTable;
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000126
Zachary Turner0d07a8e2017-12-14 18:07:04 +0000127 /// Type records that will go into the PDB TPI stream (for /DEBUG:GHASH)
128 GlobalTypeTableBuilder GlobalTypeTable;
129
130 /// Item records that will go into the PDB IPI stream (for /DEBUG:GHASH)
131 GlobalTypeTableBuilder GlobalIDTable;
132
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000133 /// PDBs use a single global string table for filenames in the file checksum
134 /// table.
135 DebugStringTableSubsection PDBStrTab;
136
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000137 llvm::SmallString<128> NativePath;
138
139 std::vector<pdb::SecMapEntry> SectionMap;
Reid Kleckner651db912017-07-18 00:21:25 +0000140
141 /// Type index mappings of type server PDBs that we've loaded so far.
142 std::map<GUID, CVIndexMap> TypeServerIndexMappings;
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000143};
144}
145
Sam Clegg0fb6faa2017-12-08 01:09:21 +0000146static SectionChunk *findByName(ArrayRef<SectionChunk *> Sections,
Rui Ueyamabe939b32016-11-21 17:22:35 +0000147 StringRef Name) {
148 for (SectionChunk *C : Sections)
149 if (C->getSectionName() == Name)
150 return C;
151 return nullptr;
152}
153
Reid Kleckner44cdb102017-06-19 17:21:45 +0000154static ArrayRef<uint8_t> consumeDebugMagic(ArrayRef<uint8_t> Data,
155 StringRef SecName) {
Rui Ueyamac5cb7372016-12-01 01:22:48 +0000156 // First 4 bytes are section magic.
Rui Ueyamac5cb7372016-12-01 01:22:48 +0000157 if (Data.size() < 4)
Rui Ueyama52896622017-01-12 03:09:25 +0000158 fatal(SecName + " too short");
Reid Kleckner44cdb102017-06-19 17:21:45 +0000159 if (support::endian::read32le(Data.data()) != COFF::DEBUG_SECTION_MAGIC)
Rui Ueyama52896622017-01-12 03:09:25 +0000160 fatal(SecName + " has an invalid magic");
Rui Ueyama26186c72016-12-09 04:46:54 +0000161 return Data.slice(4);
162}
Rui Ueyamac5cb7372016-12-01 01:22:48 +0000163
Rui Ueyamae1b48e02017-07-26 23:05:24 +0000164static ArrayRef<uint8_t> getDebugSection(ObjFile *File, StringRef SecName) {
Reid Kleckner44cdb102017-06-19 17:21:45 +0000165 if (SectionChunk *Sec = findByName(File->getDebugChunks(), SecName))
166 return consumeDebugMagic(Sec->getContents(), SecName);
167 return {};
168}
169
Zachary Turner0d07a8e2017-12-14 18:07:04 +0000170// A COFF .debug$H section is currently a clang extension. This function checks
171// if a .debug$H section is in a format that we expect / understand, so that we
172// can ignore any sections which are coincidentally also named .debug$H but do
173// not contain a format we recognize.
174static bool canUseDebugH(ArrayRef<uint8_t> DebugH) {
175 if (DebugH.size() < sizeof(object::debug_h_header))
176 return false;
177 auto *Header =
178 reinterpret_cast<const object::debug_h_header *>(DebugH.data());
179 DebugH = DebugH.drop_front(sizeof(object::debug_h_header));
180 return Header->Magic == COFF::DEBUG_HASHES_SECTION_MAGIC &&
181 Header->Version == 0 &&
182 Header->HashAlgorithm == uint16_t(GlobalTypeHashAlg::SHA1) &&
183 (DebugH.size() % 20 == 0);
184}
185
186static Optional<ArrayRef<uint8_t>> getDebugH(ObjFile *File) {
187 SectionChunk *Sec = findByName(File->getDebugChunks(), ".debug$H");
188 if (!Sec)
189 return llvm::None;
190 ArrayRef<uint8_t> Contents = Sec->getContents();
191 if (!canUseDebugH(Contents))
192 return None;
193 return Contents;
194}
195
196static ArrayRef<GloballyHashedType>
197getHashesFromDebugH(ArrayRef<uint8_t> DebugH) {
198 assert(canUseDebugH(DebugH));
199
200 DebugH = DebugH.drop_front(sizeof(object::debug_h_header));
201 uint32_t Count = DebugH.size() / sizeof(GloballyHashedType);
202 return {reinterpret_cast<const GloballyHashedType *>(DebugH.data()), Count};
203}
204
Reid Kleckner5d577522017-03-24 17:26:38 +0000205static void addTypeInfo(pdb::TpiStreamBuilder &TpiBuilder,
Zachary Turner3e3936d2017-11-29 19:35:21 +0000206 TypeCollection &TypeTable) {
Reid Kleckner5d577522017-03-24 17:26:38 +0000207 // Start the TPI or IPI stream header.
208 TpiBuilder.setVersionHeader(pdb::PdbTpiV80);
Rui Ueyama52896622017-01-12 03:09:25 +0000209
Reid Klecknerd91ca762017-07-19 17:26:07 +0000210 // Flatten the in memory type table and hash each type.
Zachary Turner3e3936d2017-11-29 19:35:21 +0000211 TypeTable.ForEachRecord([&](TypeIndex TI, const CVType &Type) {
Reid Klecknerd91ca762017-07-19 17:26:07 +0000212 auto Hash = pdb::hashTypeRecord(Type);
213 if (auto E = Hash.takeError())
214 fatal("type hashing error");
Zachary Turner3e3936d2017-11-29 19:35:21 +0000215 TpiBuilder.addTypeRecord(Type.RecordData, *Hash);
Reid Kleckner5d577522017-03-24 17:26:38 +0000216 });
Reid Kleckner5d577522017-03-24 17:26:38 +0000217}
218
Reid Kleckner651db912017-07-18 00:21:25 +0000219static Optional<TypeServer2Record>
220maybeReadTypeServerRecord(CVTypeArray &Types) {
221 auto I = Types.begin();
222 if (I == Types.end())
223 return None;
224 const CVType &Type = *I;
225 if (Type.kind() != LF_TYPESERVER2)
226 return None;
227 TypeServer2Record TS;
228 if (auto EC = TypeDeserializer::deserializeAs(const_cast<CVType &>(Type), TS))
Bob Haarmanb8a59c82017-10-25 22:28:38 +0000229 fatal("error reading type server record: " + toString(std::move(EC)));
Reid Kleckner651db912017-07-18 00:21:25 +0000230 return std::move(TS);
231}
232
Rui Ueyamae1b48e02017-07-26 23:05:24 +0000233const CVIndexMap &PDBLinker::mergeDebugT(ObjFile *File,
Reid Kleckner651db912017-07-18 00:21:25 +0000234 CVIndexMap &ObjectIndexMap) {
Reid Kleckner44cdb102017-06-19 17:21:45 +0000235 ArrayRef<uint8_t> Data = getDebugSection(File, ".debug$T");
236 if (Data.empty())
Reid Kleckner651db912017-07-18 00:21:25 +0000237 return ObjectIndexMap;
Reid Kleckner6597c282017-07-13 20:12:23 +0000238
Reid Kleckner44cdb102017-06-19 17:21:45 +0000239 BinaryByteStream Stream(Data, support::little);
240 CVTypeArray Types;
241 BinaryStreamReader Reader(Stream);
Reid Kleckner44cdb102017-06-19 17:21:45 +0000242 if (auto EC = Reader.readArray(Types, Reader.getLength()))
Bob Haarmanb8a59c82017-10-25 22:28:38 +0000243 fatal("Reader::readArray failed: " + toString(std::move(EC)));
Reid Kleckner651db912017-07-18 00:21:25 +0000244
245 // Look through type servers. If we've already seen this type server, don't
246 // merge any type information.
247 if (Optional<TypeServer2Record> TS = maybeReadTypeServerRecord(Types))
248 return maybeMergeTypeServerPDB(File, *TS);
249
250 // This is a /Z7 object. Fill in the temporary, caller-provided
251 // ObjectIndexMap.
Zachary Turner0d07a8e2017-12-14 18:07:04 +0000252 if (Config->DebugGHashes) {
253 ArrayRef<GloballyHashedType> Hashes;
254 std::vector<GloballyHashedType> OwnedHashes;
255 if (Optional<ArrayRef<uint8_t>> DebugH = getDebugH(File))
256 Hashes = getHashesFromDebugH(*DebugH);
257 else {
258 OwnedHashes = GloballyHashedType::hashTypes(Types);
259 Hashes = OwnedHashes;
260 }
261
262 if (auto Err = mergeTypeAndIdRecords(GlobalIDTable, GlobalTypeTable,
263 ObjectIndexMap.TPIMap, Types, Hashes))
264 fatal("codeview::mergeTypeAndIdRecords failed: " +
265 toString(std::move(Err)));
266 } else {
267 if (auto Err = mergeTypeAndIdRecords(IDTable, TypeTable,
268 ObjectIndexMap.TPIMap, Types))
269 fatal("codeview::mergeTypeAndIdRecords failed: " +
270 toString(std::move(Err)));
271 }
Reid Kleckner651db912017-07-18 00:21:25 +0000272 return ObjectIndexMap;
273}
274
275static Expected<std::unique_ptr<pdb::NativeSession>>
276tryToLoadPDB(const GUID &GuidFromObj, StringRef TSPath) {
Peter Collingbourne75257bc2017-10-20 19:48:26 +0000277 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile(
278 TSPath, /*FileSize=*/-1, /*RequiresNullTerminator=*/false);
279 if (!MBOrErr)
280 return errorCodeToError(MBOrErr.getError());
281
Reid Kleckner651db912017-07-18 00:21:25 +0000282 std::unique_ptr<pdb::IPDBSession> ThisSession;
Peter Collingbourne75257bc2017-10-20 19:48:26 +0000283 if (auto EC = pdb::NativeSession::createFromPdb(
284 MemoryBuffer::getMemBuffer(Driver->takeBuffer(std::move(*MBOrErr)),
285 /*RequiresNullTerminator=*/false),
286 ThisSession))
Reid Kleckner651db912017-07-18 00:21:25 +0000287 return std::move(EC);
288
289 std::unique_ptr<pdb::NativeSession> NS(
290 static_cast<pdb::NativeSession *>(ThisSession.release()));
291 pdb::PDBFile &File = NS->getPDBFile();
292 auto ExpectedInfo = File.getPDBInfoStream();
293 // All PDB Files should have an Info stream.
294 if (!ExpectedInfo)
295 return ExpectedInfo.takeError();
296
297 // Just because a file with a matching name was found and it was an actual
298 // PDB file doesn't mean it matches. For it to match the InfoStream's GUID
299 // must match the GUID specified in the TypeServer2 record.
300 if (ExpectedInfo->getGuid() != GuidFromObj)
301 return make_error<pdb::GenericError>(
302 pdb::generic_error_code::type_server_not_found, TSPath);
303
304 return std::move(NS);
305}
306
Rui Ueyamae1b48e02017-07-26 23:05:24 +0000307const CVIndexMap &PDBLinker::maybeMergeTypeServerPDB(ObjFile *File,
Reid Kleckner651db912017-07-18 00:21:25 +0000308 TypeServer2Record &TS) {
309 // First, check if we already loaded a PDB with this GUID. Return the type
310 // index mapping if we have it.
311 auto Insertion = TypeServerIndexMappings.insert({TS.getGuid(), CVIndexMap()});
312 CVIndexMap &IndexMap = Insertion.first->second;
313 if (!Insertion.second)
314 return IndexMap;
315
316 // Mark this map as a type server map.
317 IndexMap.IsTypeServerMap = true;
318
319 // Check for a PDB at:
320 // 1. The given file path
321 // 2. Next to the object file or archive file
322 auto ExpectedSession = tryToLoadPDB(TS.getGuid(), TS.getName());
323 if (!ExpectedSession) {
324 consumeError(ExpectedSession.takeError());
325 StringRef LocalPath =
326 !File->ParentName.empty() ? File->ParentName : File->getName();
327 SmallString<128> Path = sys::path::parent_path(LocalPath);
Reid Klecknerf8522d82017-07-18 00:33:53 +0000328 sys::path::append(
329 Path, sys::path::filename(TS.getName(), sys::path::Style::windows));
Reid Kleckner651db912017-07-18 00:21:25 +0000330 ExpectedSession = tryToLoadPDB(TS.getGuid(), Path);
331 }
332 if (auto E = ExpectedSession.takeError())
Bob Haarmanb8a59c82017-10-25 22:28:38 +0000333 fatal("Type server PDB was not found: " + toString(std::move(E)));
Reid Kleckner651db912017-07-18 00:21:25 +0000334
Reid Kleckner651db912017-07-18 00:21:25 +0000335 auto ExpectedTpi = (*ExpectedSession)->getPDBFile().getPDBTpiStream();
336 if (auto E = ExpectedTpi.takeError())
Bob Haarmanb8a59c82017-10-25 22:28:38 +0000337 fatal("Type server does not have TPI stream: " + toString(std::move(E)));
Reid Kleckner651db912017-07-18 00:21:25 +0000338 auto ExpectedIpi = (*ExpectedSession)->getPDBFile().getPDBIpiStream();
339 if (auto E = ExpectedIpi.takeError())
Bob Haarmanb8a59c82017-10-25 22:28:38 +0000340 fatal("Type server does not have TPI stream: " + toString(std::move(E)));
Zachary Turner0d07a8e2017-12-14 18:07:04 +0000341
342 if (Config->DebugGHashes) {
343 // PDBs do not actually store global hashes, so when merging a type server
344 // PDB we have to synthesize global hashes. To do this, we first synthesize
345 // global hashes for the TPI stream, since it is independent, then we
346 // synthesize hashes for the IPI stream, using the hashes for the TPI stream
347 // as inputs.
348 auto TpiHashes = GloballyHashedType::hashTypes(ExpectedTpi->typeArray());
349 auto IpiHashes =
350 GloballyHashedType::hashIds(ExpectedIpi->typeArray(), TpiHashes);
351
352 // Merge TPI first, because the IPI stream will reference type indices.
353 if (auto Err = mergeTypeRecords(GlobalTypeTable, IndexMap.TPIMap,
354 ExpectedTpi->typeArray(), TpiHashes))
355 fatal("codeview::mergeTypeRecords failed: " + toString(std::move(Err)));
356
357 // Merge IPI.
358 if (auto Err =
359 mergeIdRecords(GlobalIDTable, IndexMap.TPIMap, IndexMap.IPIMap,
360 ExpectedIpi->typeArray(), IpiHashes))
361 fatal("codeview::mergeIdRecords failed: " + toString(std::move(Err)));
362 } else {
363 // Merge TPI first, because the IPI stream will reference type indices.
364 if (auto Err = mergeTypeRecords(TypeTable, IndexMap.TPIMap,
365 ExpectedTpi->typeArray()))
366 fatal("codeview::mergeTypeRecords failed: " + toString(std::move(Err)));
367
368 // Merge IPI.
369 if (auto Err = mergeIdRecords(IDTable, IndexMap.TPIMap, IndexMap.IPIMap,
370 ExpectedIpi->typeArray()))
371 fatal("codeview::mergeIdRecords failed: " + toString(std::move(Err)));
372 }
Reid Kleckner651db912017-07-18 00:21:25 +0000373
374 return IndexMap;
Reid Kleckner44cdb102017-06-19 17:21:45 +0000375}
376
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000377static bool remapTypeIndex(TypeIndex &TI, ArrayRef<TypeIndex> TypeIndexMap) {
378 if (TI.isSimple())
379 return true;
380 if (TI.toArrayIndex() >= TypeIndexMap.size())
381 return false;
382 TI = TypeIndexMap[TI.toArrayIndex()];
383 return true;
384}
385
Reid Kleckner8aa32ff2017-10-24 17:02:40 +0000386static void remapTypesInSymbolRecord(ObjFile *File, SymbolKind SymKind,
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000387 MutableArrayRef<uint8_t> Contents,
Reid Kleckner651db912017-07-18 00:21:25 +0000388 const CVIndexMap &IndexMap,
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000389 ArrayRef<TiReference> TypeRefs) {
390 for (const TiReference &Ref : TypeRefs) {
391 unsigned ByteSize = Ref.Count * sizeof(TypeIndex);
Reid Kleckner03b5baf2017-07-12 18:49:43 +0000392 if (Contents.size() < Ref.Offset + ByteSize)
393 fatal("symbol record too short");
Reid Kleckner651db912017-07-18 00:21:25 +0000394
395 // This can be an item index or a type index. Choose the appropriate map.
396 ArrayRef<TypeIndex> TypeOrItemMap = IndexMap.TPIMap;
Reid Kleckner8aa32ff2017-10-24 17:02:40 +0000397 bool IsItemIndex = Ref.Kind == TiRefKind::IndexRef;
398 if (IsItemIndex && IndexMap.IsTypeServerMap)
Reid Kleckner651db912017-07-18 00:21:25 +0000399 TypeOrItemMap = IndexMap.IPIMap;
400
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000401 MutableArrayRef<TypeIndex> TIs(
402 reinterpret_cast<TypeIndex *>(Contents.data() + Ref.Offset), Ref.Count);
Reid Kleckner03b5baf2017-07-12 18:49:43 +0000403 for (TypeIndex &TI : TIs) {
Reid Kleckner651db912017-07-18 00:21:25 +0000404 if (!remapTypeIndex(TI, TypeOrItemMap)) {
Reid Kleckner8aa32ff2017-10-24 17:02:40 +0000405 log("ignoring symbol record of kind 0x" + utohexstr(SymKind) + " in " +
406 File->getName() + " with bad " + (IsItemIndex ? "item" : "type") +
407 " index 0x" + utohexstr(TI.getIndex()));
Reid Kleckner5d82f682017-10-23 22:44:51 +0000408 TI = TypeIndex(SimpleTypeKind::NotTranslated);
Reid Kleckner03b5baf2017-07-12 18:49:43 +0000409 continue;
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000410 }
Reid Kleckner03b5baf2017-07-12 18:49:43 +0000411 }
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000412 }
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000413}
414
Zachary Turner59e3ae82017-08-08 18:34:44 +0000415static SymbolKind symbolKind(ArrayRef<uint8_t> RecordData) {
416 const RecordPrefix *Prefix =
417 reinterpret_cast<const RecordPrefix *>(RecordData.data());
418 return static_cast<SymbolKind>(uint16_t(Prefix->RecordKind));
419}
420
421/// MSVC translates S_PROC_ID_END to S_END, and S_[LG]PROC32_ID to S_[LG]PROC32
422static void translateIdSymbols(MutableArrayRef<uint8_t> &RecordData,
Zachary Turner3e3936d2017-11-29 19:35:21 +0000423 TypeCollection &IDTable) {
Zachary Turner59e3ae82017-08-08 18:34:44 +0000424 RecordPrefix *Prefix = reinterpret_cast<RecordPrefix *>(RecordData.data());
425
426 SymbolKind Kind = symbolKind(RecordData);
427
428 if (Kind == SymbolKind::S_PROC_ID_END) {
429 Prefix->RecordKind = SymbolKind::S_END;
430 return;
431 }
432
433 // In an object file, GPROC32_ID has an embedded reference which refers to the
434 // single object file type index namespace. This has already been translated
435 // to the PDB file's ID stream index space, but we need to convert this to a
436 // symbol that refers to the type stream index space. So we remap again from
437 // ID index space to type index space.
438 if (Kind == SymbolKind::S_GPROC32_ID || Kind == SymbolKind::S_LPROC32_ID) {
439 SmallVector<TiReference, 1> Refs;
440 auto Content = RecordData.drop_front(sizeof(RecordPrefix));
441 CVSymbol Sym(Kind, RecordData);
442 discoverTypeIndicesInSymbol(Sym, Refs);
443 assert(Refs.size() == 1);
444 assert(Refs.front().Count == 1);
445
446 TypeIndex *TI =
447 reinterpret_cast<TypeIndex *>(Content.data() + Refs[0].Offset);
448 // `TI` is the index of a FuncIdRecord or MemberFuncIdRecord which lives in
449 // the IPI stream, whose `FunctionType` member refers to the TPI stream.
450 // Note that LF_FUNC_ID and LF_MEMFUNC_ID have the same record layout, and
451 // in both cases we just need the second type index.
452 if (!TI->isSimple() && !TI->isNoneType()) {
Zachary Turner3e3936d2017-11-29 19:35:21 +0000453 CVType FuncIdData = IDTable.getType(*TI);
Zachary Turner59e3ae82017-08-08 18:34:44 +0000454 SmallVector<TypeIndex, 2> Indices;
455 discoverTypeIndices(FuncIdData, Indices);
456 assert(Indices.size() == 2);
457 *TI = Indices[1];
458 }
459
460 Kind = (Kind == SymbolKind::S_GPROC32_ID) ? SymbolKind::S_GPROC32
461 : SymbolKind::S_LPROC32;
462 Prefix->RecordKind = uint16_t(Kind);
463 }
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000464}
465
466/// Copy the symbol record. In a PDB, symbol records must be 4 byte aligned.
467/// The object file may not be aligned.
468static MutableArrayRef<uint8_t> copySymbolForPdb(const CVSymbol &Sym,
469 BumpPtrAllocator &Alloc) {
470 size_t Size = alignTo(Sym.length(), alignOf(CodeViewContainer::Pdb));
471 assert(Size >= 4 && "record too short");
472 assert(Size <= MaxRecordLength && "record too long");
473 void *Mem = Alloc.Allocate(Size, 4);
474
475 // Copy the symbol record and zero out any padding bytes.
476 MutableArrayRef<uint8_t> NewData(reinterpret_cast<uint8_t *>(Mem), Size);
477 memcpy(NewData.data(), Sym.data().data(), Sym.length());
478 memset(NewData.data() + Sym.length(), 0, Size - Sym.length());
479
480 // Update the record prefix length. It should point to the beginning of the
Zachary Turner59e3ae82017-08-08 18:34:44 +0000481 // next record.
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000482 auto *Prefix = reinterpret_cast<RecordPrefix *>(Mem);
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000483 Prefix->RecordLen = Size - 2;
484 return NewData;
485}
486
Reid Kleckner3f851922017-07-06 16:39:32 +0000487/// Return true if this symbol opens a scope. This implies that the symbol has
488/// "parent" and "end" fields, which contain the offset of the S_END or
489/// S_INLINESITE_END record.
490static bool symbolOpensScope(SymbolKind Kind) {
491 switch (Kind) {
492 case SymbolKind::S_GPROC32:
493 case SymbolKind::S_LPROC32:
494 case SymbolKind::S_LPROC32_ID:
495 case SymbolKind::S_GPROC32_ID:
496 case SymbolKind::S_BLOCK32:
497 case SymbolKind::S_SEPCODE:
498 case SymbolKind::S_THUNK32:
499 case SymbolKind::S_INLINESITE:
500 case SymbolKind::S_INLINESITE2:
501 return true;
502 default:
503 break;
504 }
505 return false;
506}
507
508static bool symbolEndsScope(SymbolKind Kind) {
509 switch (Kind) {
510 case SymbolKind::S_END:
511 case SymbolKind::S_PROC_ID_END:
512 case SymbolKind::S_INLINESITE_END:
513 return true;
514 default:
515 break;
516 }
517 return false;
518}
519
520struct ScopeRecord {
521 ulittle32_t PtrParent;
522 ulittle32_t PtrEnd;
523};
524
525struct SymbolScope {
526 ScopeRecord *OpeningRecord;
527 uint32_t ScopeOffset;
528};
529
530static void scopeStackOpen(SmallVectorImpl<SymbolScope> &Stack,
531 uint32_t CurOffset, CVSymbol &Sym) {
532 assert(symbolOpensScope(Sym.kind()));
533 SymbolScope S;
534 S.ScopeOffset = CurOffset;
535 S.OpeningRecord = const_cast<ScopeRecord *>(
536 reinterpret_cast<const ScopeRecord *>(Sym.content().data()));
537 S.OpeningRecord->PtrParent = Stack.empty() ? 0 : Stack.back().ScopeOffset;
538 Stack.push_back(S);
539}
540
541static void scopeStackClose(SmallVectorImpl<SymbolScope> &Stack,
Rui Ueyamae1b48e02017-07-26 23:05:24 +0000542 uint32_t CurOffset, ObjFile *File) {
Reid Kleckner3f851922017-07-06 16:39:32 +0000543 if (Stack.empty()) {
544 warn("symbol scopes are not balanced in " + File->getName());
545 return;
546 }
547 SymbolScope S = Stack.pop_back_val();
548 S.OpeningRecord->PtrEnd = CurOffset;
549}
550
Zachary Turneree9906d2017-08-11 19:00:03 +0000551static bool symbolGoesInModuleStream(const CVSymbol &Sym) {
552 switch (Sym.kind()) {
553 case SymbolKind::S_GDATA32:
554 case SymbolKind::S_CONSTANT:
555 case SymbolKind::S_UDT:
556 // We really should not be seeing S_PROCREF and S_LPROCREF in the first place
557 // since they are synthesized by the linker in response to S_GPROC32 and
558 // S_LPROC32, but if we do see them, don't put them in the module stream I
559 // guess.
560 case SymbolKind::S_PROCREF:
561 case SymbolKind::S_LPROCREF:
562 return false;
563 // S_GDATA32 does not go in the module stream, but S_LDATA32 does.
564 case SymbolKind::S_LDATA32:
565 default:
566 return true;
567 }
568}
569
570static bool symbolGoesInGlobalsStream(const CVSymbol &Sym) {
571 switch (Sym.kind()) {
572 case SymbolKind::S_CONSTANT:
Zachary Turneree9906d2017-08-11 19:00:03 +0000573 case SymbolKind::S_GDATA32:
574 // S_LDATA32 goes in both the module stream and the globals stream.
575 case SymbolKind::S_LDATA32:
576 case SymbolKind::S_GPROC32:
577 case SymbolKind::S_LPROC32:
578 // We really should not be seeing S_PROCREF and S_LPROCREF in the first place
579 // since they are synthesized by the linker in response to S_GPROC32 and
580 // S_LPROC32, but if we do see them, copy them straight through.
581 case SymbolKind::S_PROCREF:
582 case SymbolKind::S_LPROCREF:
583 return true;
Zachary Turner302dc8b2017-08-14 18:44:58 +0000584 // FIXME: For now, we drop all S_UDT symbols (i.e. they don't go in the
585 // globals stream or the modules stream). These have special handling which
586 // needs more investigation before we can get right, but by putting them all
587 // into the globals stream WinDbg fails to display local variables of class
588 // types saying that it cannot find the type Foo *. So as a stopgap just to
589 // keep things working, we drop them.
590 case SymbolKind::S_UDT:
Zachary Turneree9906d2017-08-11 19:00:03 +0000591 default:
592 return false;
593 }
594}
595
596static void addGlobalSymbol(pdb::GSIStreamBuilder &Builder, ObjFile &File,
597 const CVSymbol &Sym) {
598 switch (Sym.kind()) {
599 case SymbolKind::S_CONSTANT:
600 case SymbolKind::S_UDT:
601 case SymbolKind::S_GDATA32:
602 case SymbolKind::S_LDATA32:
603 case SymbolKind::S_PROCREF:
604 case SymbolKind::S_LPROCREF:
605 Builder.addGlobalSymbol(Sym);
606 break;
607 case SymbolKind::S_GPROC32:
608 case SymbolKind::S_LPROC32: {
609 SymbolRecordKind K = SymbolRecordKind::ProcRefSym;
610 if (Sym.kind() == SymbolKind::S_LPROC32)
611 K = SymbolRecordKind::LocalProcRef;
612 ProcRefSym PS(K);
613 PS.Module = static_cast<uint16_t>(File.ModuleDBI->getModuleIndex());
614 // For some reason, MSVC seems to add one to this value.
615 ++PS.Module;
616 PS.Name = getSymbolName(Sym);
617 PS.SumName = 0;
618 PS.SymOffset = File.ModuleDBI->getNextSymbolOffset();
619 Builder.addGlobalSymbol(PS);
620 break;
621 }
622 default:
623 llvm_unreachable("Invalid symbol kind!");
624 }
625}
626
Rui Ueyamae1b48e02017-07-26 23:05:24 +0000627static void mergeSymbolRecords(BumpPtrAllocator &Alloc, ObjFile *File,
Zachary Turneree9906d2017-08-11 19:00:03 +0000628 pdb::GSIStreamBuilder &GsiBuilder,
Reid Kleckner651db912017-07-18 00:21:25 +0000629 const CVIndexMap &IndexMap,
Zachary Turner3e3936d2017-11-29 19:35:21 +0000630 TypeCollection &IDTable,
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000631 BinaryStreamRef SymData) {
632 // FIXME: Improve error recovery by warning and skipping records when
633 // possible.
634 CVSymbolArray Syms;
635 BinaryStreamReader Reader(SymData);
636 ExitOnErr(Reader.readArray(Syms, Reader.getLength()));
Reid Kleckner3f851922017-07-06 16:39:32 +0000637 SmallVector<SymbolScope, 4> Scopes;
Zachary Turner59e3ae82017-08-08 18:34:44 +0000638 for (CVSymbol Sym : Syms) {
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000639 // Discover type index references in the record. Skip it if we don't know
640 // where they are.
641 SmallVector<TiReference, 32> TypeRefs;
Zachary Turner59e3ae82017-08-08 18:34:44 +0000642 if (!discoverTypeIndicesInSymbol(Sym, TypeRefs)) {
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000643 log("ignoring unknown symbol record with kind 0x" + utohexstr(Sym.kind()));
644 continue;
645 }
646
647 // Copy the symbol record so we can mutate it.
648 MutableArrayRef<uint8_t> NewData = copySymbolForPdb(Sym, Alloc);
649
650 // Re-map all the type index references.
651 MutableArrayRef<uint8_t> Contents =
652 NewData.drop_front(sizeof(RecordPrefix));
Zachary Turner3e3936d2017-11-29 19:35:21 +0000653 remapTypesInSymbolRecord(File, Sym.kind(), Contents, IndexMap, TypeRefs);
Zachary Turner59e3ae82017-08-08 18:34:44 +0000654
655 // An object file may have S_xxx_ID symbols, but these get converted to
656 // "real" symbols in a PDB.
657 translateIdSymbols(NewData, IDTable);
658
659 SymbolKind NewKind = symbolKind(NewData);
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000660
Reid Kleckner3f851922017-07-06 16:39:32 +0000661 // Fill in "Parent" and "End" fields by maintaining a stack of scopes.
Zachary Turner59e3ae82017-08-08 18:34:44 +0000662 CVSymbol NewSym(NewKind, NewData);
663 if (symbolOpensScope(NewKind))
Reid Kleckner3f851922017-07-06 16:39:32 +0000664 scopeStackOpen(Scopes, File->ModuleDBI->getNextSymbolOffset(), NewSym);
Zachary Turner59e3ae82017-08-08 18:34:44 +0000665 else if (symbolEndsScope(NewKind))
Reid Kleckner3f851922017-07-06 16:39:32 +0000666 scopeStackClose(Scopes, File->ModuleDBI->getNextSymbolOffset(), File);
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000667
Zachary Turneree9906d2017-08-11 19:00:03 +0000668 // Add the symbol to the globals stream if necessary. Do this before adding
669 // the symbol to the module since we may need to get the next symbol offset,
670 // and writing to the module's symbol stream will update that offset.
671 if (symbolGoesInGlobalsStream(NewSym))
672 addGlobalSymbol(GsiBuilder, *File, NewSym);
673
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000674 // Add the symbol to the module.
Zachary Turneree9906d2017-08-11 19:00:03 +0000675 if (symbolGoesInModuleStream(NewSym))
676 File->ModuleDBI->addSymbol(NewSym);
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000677 }
678}
679
Reid Kleckner44cdb102017-06-19 17:21:45 +0000680// Allocate memory for a .debug$S section and relocate it.
681static ArrayRef<uint8_t> relocateDebugChunk(BumpPtrAllocator &Alloc,
682 SectionChunk *DebugChunk) {
683 uint8_t *Buffer = Alloc.Allocate<uint8_t>(DebugChunk->getSize());
684 assert(DebugChunk->OutputSectionOff == 0 &&
685 "debug sections should not be in output sections");
686 DebugChunk->writeTo(Buffer);
687 return consumeDebugMagic(makeArrayRef(Buffer, DebugChunk->getSize()),
688 ".debug$S");
689}
690
Rui Ueyamae1b48e02017-07-26 23:05:24 +0000691void PDBLinker::addObjFile(ObjFile *File) {
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000692 // Add a module descriptor for every object file. We need to put an absolute
693 // path to the object into the PDB. If this is a plain object, we make its
694 // path absolute. If it's an object in an archive, we make the archive path
695 // absolute.
696 bool InArchive = !File->ParentName.empty();
697 SmallString<128> Path = InArchive ? File->ParentName : File->getName();
698 sys::fs::make_absolute(Path);
699 sys::path::native(Path, sys::path::Style::windows);
700 StringRef Name = InArchive ? File->getName() : StringRef(Path);
Zachary Turner2897e032017-05-25 21:16:03 +0000701
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000702 File->ModuleDBI = &ExitOnErr(Builder.getDbiBuilder().addModuleInfo(Name));
703 File->ModuleDBI->setObjFileName(Path);
Reid Kleckner44cdb102017-06-19 17:21:45 +0000704
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000705 // Before we can process symbol substreams from .debug$S, we need to process
706 // type information, file checksums, and the string table. Add type info to
707 // the PDB first, so that we can get the map from object file type and item
708 // indices to PDB type and item indices.
Reid Kleckner651db912017-07-18 00:21:25 +0000709 CVIndexMap ObjectIndexMap;
710 const CVIndexMap &IndexMap = mergeDebugT(File, ObjectIndexMap);
Zachary Turner448dea42017-07-07 18:46:14 +0000711
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000712 // Now do all live .debug$S sections.
713 for (SectionChunk *DebugChunk : File->getDebugChunks()) {
714 if (!DebugChunk->isLive() || DebugChunk->getSectionName() != ".debug$S")
715 continue;
Reid Kleckner8cbdd0c2017-06-13 15:49:13 +0000716
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000717 ArrayRef<uint8_t> RelocatedDebugContents =
718 relocateDebugChunk(Alloc, DebugChunk);
719 if (RelocatedDebugContents.empty())
720 continue;
Reid Kleckner8cbdd0c2017-06-13 15:49:13 +0000721
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000722 DebugSubsectionArray Subsections;
723 BinaryStreamReader Reader(RelocatedDebugContents, support::little);
724 ExitOnErr(Reader.readArray(Subsections, RelocatedDebugContents.size()));
Rui Ueyama52896622017-01-12 03:09:25 +0000725
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000726 DebugStringTableSubsectionRef CVStrTab;
727 DebugChecksumsSubsectionRef Checksums;
728 for (const DebugSubsectionRecord &SS : Subsections) {
729 switch (SS.kind()) {
730 case DebugSubsectionKind::StringTable:
731 ExitOnErr(CVStrTab.initialize(SS.getRecordData()));
732 break;
733 case DebugSubsectionKind::FileChecksums:
734 ExitOnErr(Checksums.initialize(SS.getRecordData()));
735 break;
736 case DebugSubsectionKind::Lines:
737 // We can add the relocated line table directly to the PDB without
738 // modification because the file checksum offsets will stay the same.
739 File->ModuleDBI->addDebugSubsection(SS);
740 break;
741 case DebugSubsectionKind::Symbols:
Zachary Turner0d07a8e2017-12-14 18:07:04 +0000742 if (Config->DebugGHashes) {
743 mergeSymbolRecords(Alloc, File, Builder.getGsiBuilder(), IndexMap,
744 GlobalIDTable, SS.getRecordData());
745 } else {
746 mergeSymbolRecords(Alloc, File, Builder.getGsiBuilder(), IndexMap,
747 IDTable, SS.getRecordData());
748 }
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000749 break;
750 default:
751 // FIXME: Process the rest of the subsections.
752 break;
Reid Kleckner44cdb102017-06-19 17:21:45 +0000753 }
754 }
Rui Ueyama52896622017-01-12 03:09:25 +0000755
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000756 if (Checksums.valid()) {
757 // Make a new file checksum table that refers to offsets in the PDB-wide
758 // string table. Generally the string table subsection appears after the
759 // checksum table, so we have to do this after looping over all the
760 // subsections.
761 if (!CVStrTab.valid())
762 fatal(".debug$S sections must have both a string table subsection "
763 "and a checksum subsection table or neither");
764 auto NewChecksums = make_unique<DebugChecksumsSubsection>(PDBStrTab);
765 for (FileChecksumEntry &FC : Checksums) {
766 StringRef FileName = ExitOnErr(CVStrTab.getString(FC.FileNameOffset));
767 ExitOnErr(Builder.getDbiBuilder().addModuleSourceFile(*File->ModuleDBI,
768 FileName));
769 NewChecksums->addChecksum(FileName, FC.Kind, FC.Checksum);
770 }
771 File->ModuleDBI->addDebugSubsection(std::move(NewChecksums));
772 }
773 }
774}
775
Reid Klecknereacdf042017-07-27 18:25:59 +0000776static PublicSym32 createPublic(Defined *Def) {
777 PublicSym32 Pub(SymbolKind::S_PUB32);
778 Pub.Name = Def->getName();
779 if (auto *D = dyn_cast<DefinedCOFF>(Def)) {
780 if (D->getCOFFSymbol().isFunctionDefinition())
781 Pub.Flags = PublicSymFlags::Function;
782 } else if (isa<DefinedImportThunk>(Def)) {
783 Pub.Flags = PublicSymFlags::Function;
784 }
785
786 OutputSection *OS = Def->getChunk()->getOutputSection();
787 assert(OS && "all publics should be in final image");
788 Pub.Offset = Def->getRVA() - OS->getRVA();
789 Pub.Segment = OS->SectionIndex;
790 return Pub;
791}
792
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000793// Add all object files to the PDB. Merge .debug$T sections into IpiData and
794// TpiData.
795void PDBLinker::addObjectsToPDB() {
Rui Ueyamaacd632d2017-07-27 00:45:26 +0000796 for (ObjFile *File : ObjFile::Instances)
Rui Ueyamae1b48e02017-07-26 23:05:24 +0000797 addObjFile(File);
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000798
799 Builder.getStringTableBuilder().setStrings(PDBStrTab);
Reid Kleckner44cdb102017-06-19 17:21:45 +0000800
Zachary Turner0d07a8e2017-12-14 18:07:04 +0000801 // Construct TPI and IPI stream contents.
802 if (Config->DebugGHashes) {
803 addTypeInfo(Builder.getTpiBuilder(), GlobalTypeTable);
804 addTypeInfo(Builder.getIpiBuilder(), GlobalIDTable);
805 } else {
806 addTypeInfo(Builder.getTpiBuilder(), TypeTable);
807 addTypeInfo(Builder.getIpiBuilder(), IDTable);
808 }
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000809
Zachary Turneree9906d2017-08-11 19:00:03 +0000810 // Compute the public and global symbols.
811 auto &GsiBuilder = Builder.getGsiBuilder();
Reid Klecknereacdf042017-07-27 18:25:59 +0000812 std::vector<PublicSym32> Publics;
Rui Ueyamaf52496e2017-11-03 21:21:47 +0000813 Symtab->forEachSymbol([&Publics](Symbol *S) {
Reid Klecknereacdf042017-07-27 18:25:59 +0000814 // Only emit defined, live symbols that have a chunk.
Rui Ueyama616cd992017-10-31 16:10:24 +0000815 auto *Def = dyn_cast<Defined>(S);
Reid Klecknereacdf042017-07-27 18:25:59 +0000816 if (Def && Def->isLive() && Def->getChunk())
817 Publics.push_back(createPublic(Def));
818 });
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000819
Reid Klecknereacdf042017-07-27 18:25:59 +0000820 if (!Publics.empty()) {
821 // Sort the public symbols and add them to the stream.
822 std::sort(Publics.begin(), Publics.end(),
823 [](const PublicSym32 &L, const PublicSym32 &R) {
824 return L.Name < R.Name;
825 });
Reid Klecknereacdf042017-07-27 18:25:59 +0000826 for (const PublicSym32 &Pub : Publics)
Zachary Turner946204c2017-08-09 04:23:25 +0000827 GsiBuilder.addPublicSymbol(Pub);
Reid Klecknereacdf042017-07-27 18:25:59 +0000828 }
Rui Ueyama52896622017-01-12 03:09:25 +0000829}
830
Zachary Turner28e31ee2017-08-11 20:46:28 +0000831static void addCommonLinkerModuleSymbols(StringRef Path,
832 pdb::DbiModuleDescriptorBuilder &Mod,
833 BumpPtrAllocator &Allocator) {
Zachary Turner28e31ee2017-08-11 20:46:28 +0000834 ObjNameSym ONS(SymbolRecordKind::ObjNameSym);
835 Compile3Sym CS(SymbolRecordKind::Compile3Sym);
836 EnvBlockSym EBS(SymbolRecordKind::EnvBlockSym);
Zachary Turner6708e0b2017-07-10 21:01:37 +0000837
838 ONS.Name = "* Linker *";
839 ONS.Signature = 0;
840
841 CS.Machine = Config->is64() ? CPUType::X64 : CPUType::Intel80386;
Zachary Turnerb57884e2017-08-11 21:14:01 +0000842 // Interestingly, if we set the string to 0.0.0.0, then when trying to view
843 // local variables WinDbg emits an error that private symbols are not present.
844 // By setting this to a valid MSVC linker version string, local variables are
845 // displayed properly. As such, even though it is not representative of
Zachary Turnerf6822c82017-08-11 20:46:47 +0000846 // LLVM's version information, we need this for compatibility.
Zachary Turner6708e0b2017-07-10 21:01:37 +0000847 CS.Flags = CompileSym3Flags::None;
Zachary Turnerf6822c82017-08-11 20:46:47 +0000848 CS.VersionBackendBuild = 25019;
849 CS.VersionBackendMajor = 14;
850 CS.VersionBackendMinor = 10;
Zachary Turner6708e0b2017-07-10 21:01:37 +0000851 CS.VersionBackendQFE = 0;
Zachary Turnerf6822c82017-08-11 20:46:47 +0000852
853 // MSVC also sets the frontend to 0.0.0.0 since this is specifically for the
854 // linker module (which is by definition a backend), so we don't need to do
855 // anything here. Also, it seems we can use "LLVM Linker" for the linker name
856 // without any problems. Only the backend version has to be hardcoded to a
857 // magic number.
Zachary Turner6708e0b2017-07-10 21:01:37 +0000858 CS.VersionFrontendBuild = 0;
859 CS.VersionFrontendMajor = 0;
860 CS.VersionFrontendMinor = 0;
861 CS.VersionFrontendQFE = 0;
862 CS.Version = "LLVM Linker";
863 CS.setLanguage(SourceLanguage::Link);
864
865 ArrayRef<StringRef> Args = makeArrayRef(Config->Argv).drop_front();
866 std::string ArgStr = llvm::join(Args, " ");
867 EBS.Fields.push_back("cwd");
868 SmallString<64> cwd;
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000869 sys::fs::current_path(cwd);
Zachary Turner6708e0b2017-07-10 21:01:37 +0000870 EBS.Fields.push_back(cwd);
871 EBS.Fields.push_back("exe");
Zachary Turnerf6822c82017-08-11 20:46:47 +0000872 SmallString<64> exe = Config->Argv[0];
873 llvm::sys::fs::make_absolute(exe);
874 EBS.Fields.push_back(exe);
Zachary Turner6708e0b2017-07-10 21:01:37 +0000875 EBS.Fields.push_back("pdb");
876 EBS.Fields.push_back(Path);
877 EBS.Fields.push_back("cmd");
878 EBS.Fields.push_back(ArgStr);
879 Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(
880 ONS, Allocator, CodeViewContainer::Pdb));
881 Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(
882 CS, Allocator, CodeViewContainer::Pdb));
883 Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(
884 EBS, Allocator, CodeViewContainer::Pdb));
885}
886
Zachary Turner28e31ee2017-08-11 20:46:28 +0000887static void addLinkerModuleSectionSymbol(pdb::DbiModuleDescriptorBuilder &Mod,
888 OutputSection &OS,
889 BumpPtrAllocator &Allocator) {
890 SectionSym Sym(SymbolRecordKind::SectionSym);
Zachary Turnerf6822c82017-08-11 20:46:47 +0000891 Sym.Alignment = 12; // 2^12 = 4KB
Zachary Turner28e31ee2017-08-11 20:46:28 +0000892 Sym.Characteristics = OS.getCharacteristics();
893 Sym.Length = OS.getVirtualSize();
894 Sym.Name = OS.getName();
895 Sym.Rva = OS.getRVA();
896 Sym.SectionNumber = OS.SectionIndex;
897 Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(
898 Sym, Allocator, CodeViewContainer::Pdb));
899}
900
Rui Ueyama09e0b5f2016-11-12 00:00:51 +0000901// Creates a PDB file.
Reid Kleckner175af4b2017-08-03 21:15:09 +0000902void coff::createPDB(SymbolTable *Symtab,
903 ArrayRef<OutputSection *> OutputSections,
904 ArrayRef<uint8_t> SectionTable,
Zachary Turner024323c2017-08-15 21:31:41 +0000905 const llvm::codeview::DebugInfo &BuildId) {
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000906 PDBLinker PDB(Symtab);
Zachary Turner024323c2017-08-15 21:31:41 +0000907 PDB.initialize(BuildId);
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000908 PDB.addObjectsToPDB();
Reid Kleckner175af4b2017-08-03 21:15:09 +0000909 PDB.addSections(OutputSections, SectionTable);
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000910 PDB.commit();
911}
912
Zachary Turner024323c2017-08-15 21:31:41 +0000913void PDBLinker::initialize(const llvm::codeview::DebugInfo &BuildId) {
Rui Ueyama12979542016-09-30 20:53:45 +0000914 ExitOnErr(Builder.initialize(4096)); // 4096 is blocksize
Rui Ueyama7f382992016-09-15 18:55:18 +0000915
Rui Ueyama8d3fb5d2016-10-05 22:08:58 +0000916 // Create streams in MSF for predefined streams, namely
917 // PDB, TPI, DBI and IPI.
918 for (int I = 0; I < (int)pdb::kSpecialStreamCount; ++I)
919 ExitOnErr(Builder.getMsfBuilder().addStream(0));
Rui Ueyama7f382992016-09-15 18:55:18 +0000920
Rui Ueyamabb542b32016-09-16 22:51:17 +0000921 // Add an Info stream.
922 auto &InfoBuilder = Builder.getInfoBuilder();
Zachary Turner024323c2017-08-15 21:31:41 +0000923 InfoBuilder.setAge(BuildId.PDB70.Age);
Saleem Abdulrasool0acd6dd2017-02-07 04:28:02 +0000924
Zachary Turner024323c2017-08-15 21:31:41 +0000925 GUID uuid;
926 memcpy(&uuid, &BuildId.PDB70.Signature, sizeof(uuid));
Saleem Abdulrasool0acd6dd2017-02-07 04:28:02 +0000927 InfoBuilder.setGuid(uuid);
Zachary Turner3a11fdf2017-07-07 20:25:39 +0000928 InfoBuilder.setSignature(time(nullptr));
Rui Ueyamabb542b32016-09-16 22:51:17 +0000929 InfoBuilder.setVersion(pdb::PdbRaw_ImplVer::PdbImplVC70);
Rui Ueyama7f382992016-09-15 18:55:18 +0000930
Zachary Turner6c4bfba2017-07-07 05:04:36 +0000931 // Add an empty DBI stream.
Reid Kleckner8cbdd0c2017-06-13 15:49:13 +0000932 pdb::DbiStreamBuilder &DbiBuilder = Builder.getDbiBuilder();
Zachary Turner024323c2017-08-15 21:31:41 +0000933 DbiBuilder.setAge(BuildId.PDB70.Age);
Zachary Turnerc1e93e52017-07-07 18:45:56 +0000934 DbiBuilder.setVersionHeader(pdb::PdbDbiV70);
Zachary Turner3a11fdf2017-07-07 20:25:39 +0000935 ExitOnErr(DbiBuilder.addDbgStream(pdb::DbgHeaderType::NewFPO, {}));
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000936}
Rui Ueyama1343fac2016-10-06 22:52:01 +0000937
Reid Kleckner175af4b2017-08-03 21:15:09 +0000938void PDBLinker::addSectionContrib(pdb::DbiModuleDescriptorBuilder &LinkerModule,
939 OutputSection *OS, Chunk *C) {
940 pdb::SectionContrib SC;
941 memset(&SC, 0, sizeof(SC));
942 SC.ISect = OS->SectionIndex;
943 SC.Off = C->getRVA() - OS->getRVA();
944 SC.Size = C->getSize();
945 if (auto *SecChunk = dyn_cast<SectionChunk>(C)) {
946 SC.Characteristics = SecChunk->Header->Characteristics;
947 SC.Imod = SecChunk->File->ModuleDBI->getModuleIndex();
Zachary Turner676386ff2017-08-07 20:23:45 +0000948 ArrayRef<uint8_t> Contents = SecChunk->getContents();
949 JamCRC CRC(0);
950 ArrayRef<char> CharContents = makeArrayRef(
951 reinterpret_cast<const char *>(Contents.data()), Contents.size());
952 CRC.update(CharContents);
953 SC.DataCrc = CRC.getCRC();
Reid Kleckner175af4b2017-08-03 21:15:09 +0000954 } else {
955 SC.Characteristics = OS->getCharacteristics();
956 // FIXME: When we start creating DBI for import libraries, use those here.
957 SC.Imod = LinkerModule.getModuleIndex();
958 }
Reid Kleckner175af4b2017-08-03 21:15:09 +0000959 SC.RelocCrc = 0; // FIXME
960 Builder.getDbiBuilder().addSectionContrib(SC);
961}
Rui Ueyama09e0b5f2016-11-12 00:00:51 +0000962
Reid Kleckner175af4b2017-08-03 21:15:09 +0000963void PDBLinker::addSections(ArrayRef<OutputSection *> OutputSections,
964 ArrayRef<uint8_t> SectionTable) {
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000965 // It's not entirely clear what this is, but the * Linker * module uses it.
Reid Kleckner175af4b2017-08-03 21:15:09 +0000966 pdb::DbiStreamBuilder &DbiBuilder = Builder.getDbiBuilder();
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000967 NativePath = Config->PDBPath;
968 sys::fs::make_absolute(NativePath);
969 sys::path::native(NativePath, sys::path::Style::windows);
970 uint32_t PdbFilePathNI = DbiBuilder.addECName(NativePath);
Zachary Turner6c4bfba2017-07-07 05:04:36 +0000971 auto &LinkerModule = ExitOnErr(DbiBuilder.addModuleInfo("* Linker *"));
972 LinkerModule.setPdbFilePathNI(PdbFilePathNI);
Zachary Turner28e31ee2017-08-11 20:46:28 +0000973 addCommonLinkerModuleSymbols(NativePath, LinkerModule, Alloc);
Rui Ueyamac91f7162016-11-16 01:10:46 +0000974
Reid Kleckner175af4b2017-08-03 21:15:09 +0000975 // Add section contributions. They must be ordered by ascending RVA.
Zachary Turner28e31ee2017-08-11 20:46:28 +0000976 for (OutputSection *OS : OutputSections) {
977 addLinkerModuleSectionSymbol(LinkerModule, *OS, Alloc);
Reid Kleckner175af4b2017-08-03 21:15:09 +0000978 for (Chunk *C : OS->getChunks())
979 addSectionContrib(LinkerModule, OS, C);
Zachary Turner28e31ee2017-08-11 20:46:28 +0000980 }
Reid Kleckner175af4b2017-08-03 21:15:09 +0000981
982 // Add Section Map stream.
983 ArrayRef<object::coff_section> Sections = {
984 (const object::coff_section *)SectionTable.data(),
985 SectionTable.size() / sizeof(object::coff_section)};
986 SectionMap = pdb::DbiStreamBuilder::createSectionMap(Sections);
987 DbiBuilder.setSectionMap(SectionMap);
988
Rui Ueyama9f66f822016-10-11 19:45:07 +0000989 // Add COFF section header stream.
990 ExitOnErr(
991 DbiBuilder.addDbgStream(pdb::DbgHeaderType::SectionHdr, SectionTable));
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000992}
Rui Ueyama9f66f822016-10-11 19:45:07 +0000993
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000994void PDBLinker::commit() {
Rui Ueyama3e9d6bb2016-09-26 23:53:55 +0000995 // Write to a file.
Zachary Turner6708e0b2017-07-10 21:01:37 +0000996 ExitOnErr(Builder.commit(Config->PDBPath));
Rui Ueyamae7378242015-12-04 23:11:05 +0000997}