blob: 2b9d4f246d30e7bb1af29eb09b07b5cf4070fcbb [file] [log] [blame]
Rui Ueyamae7378242015-12-04 23:11:05 +00001//===- PDB.cpp ------------------------------------------------------------===//
2//
3// The LLVM Linker
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Rui Ueyama1d99ab32016-09-15 22:24:51 +000010#include "PDB.h"
Rui Ueyama09e0b5f2016-11-12 00:00:51 +000011#include "Chunks.h"
Rui Ueyamabe939b32016-11-21 17:22:35 +000012#include "Config.h"
Rui Ueyamae7378242015-12-04 23:11:05 +000013#include "Error.h"
Rui Ueyama09e0b5f2016-11-12 00:00:51 +000014#include "SymbolTable.h"
15#include "Symbols.h"
Reid Klecknereacdf042017-07-27 18:25:59 +000016#include "Writer.h"
Saleem Abdulrasooldf8a13b2017-01-04 17:56:54 +000017#include "llvm/DebugInfo/CodeView/CVDebugRecord.h"
Reid Kleckner44cdb102017-06-19 17:21:45 +000018#include "llvm/DebugInfo/CodeView/DebugSubsectionRecord.h"
Zachary Turner526f4f22017-05-19 19:26:58 +000019#include "llvm/DebugInfo/CodeView/LazyRandomTypeCollection.h"
Zachary Turner6708e0b2017-07-10 21:01:37 +000020#include "llvm/DebugInfo/CodeView/SymbolSerializer.h"
Reid Kleckner651db912017-07-18 00:21:25 +000021#include "llvm/DebugInfo/CodeView/TypeDeserializer.h"
Zachary Turner629cb7d2017-01-11 23:24:22 +000022#include "llvm/DebugInfo/CodeView/TypeDumpVisitor.h"
Reid Klecknerd0e6e242017-06-21 17:25:56 +000023#include "llvm/DebugInfo/CodeView/TypeIndexDiscovery.h"
Rui Ueyama52896622017-01-12 03:09:25 +000024#include "llvm/DebugInfo/CodeView/TypeStreamMerger.h"
25#include "llvm/DebugInfo/CodeView/TypeTableBuilder.h"
Rui Ueyamab28c6d42016-09-16 04:32:33 +000026#include "llvm/DebugInfo/MSF/MSFBuilder.h"
Rui Ueyama7f382992016-09-15 18:55:18 +000027#include "llvm/DebugInfo/MSF/MSFCommon.h"
Reid Kleckner651db912017-07-18 00:21:25 +000028#include "llvm/DebugInfo/PDB/GenericError.h"
Zachary Turner6708e0b2017-07-10 21:01:37 +000029#include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptorBuilder.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000030#include "llvm/DebugInfo/PDB/Native/DbiStream.h"
31#include "llvm/DebugInfo/PDB/Native/DbiStreamBuilder.h"
32#include "llvm/DebugInfo/PDB/Native/InfoStream.h"
33#include "llvm/DebugInfo/PDB/Native/InfoStreamBuilder.h"
Reid Kleckner651db912017-07-18 00:21:25 +000034#include "llvm/DebugInfo/PDB/Native/NativeSession.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000035#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
36#include "llvm/DebugInfo/PDB/Native/PDBFileBuilder.h"
Rafael Espindolaa0f30da2017-05-02 20:19:42 +000037#include "llvm/DebugInfo/PDB/Native/PDBStringTableBuilder.h"
Reid Klecknereacdf042017-07-27 18:25:59 +000038#include "llvm/DebugInfo/PDB/Native/PublicsStreamBuilder.h"
Reid Klecknerd91ca762017-07-19 17:26:07 +000039#include "llvm/DebugInfo/PDB/Native/TpiHashing.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000040#include "llvm/DebugInfo/PDB/Native/TpiStream.h"
41#include "llvm/DebugInfo/PDB/Native/TpiStreamBuilder.h"
Reid Kleckner651db912017-07-18 00:21:25 +000042#include "llvm/DebugInfo/PDB/PDB.h"
Rui Ueyama20df4ec2016-10-31 21:09:21 +000043#include "llvm/Object/COFF.h"
Zachary Turnerd9dc2822017-03-02 20:52:51 +000044#include "llvm/Support/BinaryByteStream.h"
Rui Ueyama1763c0d2015-12-08 18:39:55 +000045#include "llvm/Support/Endian.h"
Rui Ueyamae7378242015-12-04 23:11:05 +000046#include "llvm/Support/FileOutputBuffer.h"
Zachary Turner7b327d02017-02-16 23:35:45 +000047#include "llvm/Support/Path.h"
Rui Ueyamabe939b32016-11-21 17:22:35 +000048#include "llvm/Support/ScopedPrinter.h"
Rui Ueyamae7378242015-12-04 23:11:05 +000049#include <memory>
50
Rui Ueyama1d99ab32016-09-15 22:24:51 +000051using namespace lld;
Rui Ueyama09e0b5f2016-11-12 00:00:51 +000052using namespace lld::coff;
Rui Ueyamae7378242015-12-04 23:11:05 +000053using namespace llvm;
Rui Ueyamabe939b32016-11-21 17:22:35 +000054using namespace llvm::codeview;
Rui Ueyamae7378242015-12-04 23:11:05 +000055
Rui Ueyama09e0b5f2016-11-12 00:00:51 +000056using llvm::object::coff_section;
57
Rui Ueyamab28c6d42016-09-16 04:32:33 +000058static ExitOnError ExitOnErr;
59
Reid Kleckner0faf6d72017-07-14 00:14:58 +000060namespace {
Reid Kleckner651db912017-07-18 00:21:25 +000061/// Map from type index and item index in a type server PDB to the
62/// corresponding index in the destination PDB.
63struct CVIndexMap {
64 SmallVector<TypeIndex, 0> TPIMap;
65 SmallVector<TypeIndex, 0> IPIMap;
66 bool IsTypeServerMap = false;
67};
68
Reid Kleckner0faf6d72017-07-14 00:14:58 +000069class PDBLinker {
70public:
71 PDBLinker(SymbolTable *Symtab)
72 : Alloc(), Symtab(Symtab), Builder(Alloc), TypeTable(Alloc),
73 IDTable(Alloc) {}
74
75 /// Emit the basic PDB structure: initial streams, headers, etc.
76 void initialize(const llvm::codeview::DebugInfo *DI);
77
78 /// Link CodeView from each object file in the symbol table into the PDB.
79 void addObjectsToPDB();
80
81 /// Link CodeView from a single object file into the PDB.
Rui Ueyamae1b48e02017-07-26 23:05:24 +000082 void addObjFile(ObjFile *File);
Reid Kleckner0faf6d72017-07-14 00:14:58 +000083
Reid Kleckner651db912017-07-18 00:21:25 +000084 /// Produce a mapping from the type and item indices used in the object
85 /// file to those in the destination PDB.
86 ///
87 /// If the object file uses a type server PDB (compiled with /Zi), merge TPI
88 /// and IPI from the type server PDB and return a map for it. Each unique type
89 /// server PDB is merged at most once, so this may return an existing index
90 /// mapping.
91 ///
92 /// If the object does not use a type server PDB (compiled with /Z7), we merge
93 /// all the type and item records from the .debug$S stream and fill in the
94 /// caller-provided ObjectIndexMap.
Rui Ueyamae1b48e02017-07-26 23:05:24 +000095 const CVIndexMap &mergeDebugT(ObjFile *File, CVIndexMap &ObjectIndexMap);
Reid Kleckner651db912017-07-18 00:21:25 +000096
Rui Ueyamae1b48e02017-07-26 23:05:24 +000097 const CVIndexMap &maybeMergeTypeServerPDB(ObjFile *File,
Reid Kleckner651db912017-07-18 00:21:25 +000098 TypeServer2Record &TS);
Reid Kleckner0faf6d72017-07-14 00:14:58 +000099
100 /// Add the section map and section contributions to the PDB.
Reid Kleckner175af4b2017-08-03 21:15:09 +0000101 void addSections(ArrayRef<OutputSection *> OutputSections,
102 ArrayRef<uint8_t> SectionTable);
103
104 void addSectionContrib(pdb::DbiModuleDescriptorBuilder &LinkerModule,
105 OutputSection *OS, Chunk *C);
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000106
107 /// Write the PDB to disk.
108 void commit();
109
110private:
111 BumpPtrAllocator Alloc;
112
113 SymbolTable *Symtab;
114
115 pdb::PDBFileBuilder Builder;
116
117 /// Type records that will go into the PDB TPI stream.
118 TypeTableBuilder TypeTable;
119
120 /// Item records that will go into the PDB IPI stream.
121 TypeTableBuilder IDTable;
122
123 /// PDBs use a single global string table for filenames in the file checksum
124 /// table.
125 DebugStringTableSubsection PDBStrTab;
126
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000127 llvm::SmallString<128> NativePath;
128
129 std::vector<pdb::SecMapEntry> SectionMap;
Reid Kleckner651db912017-07-18 00:21:25 +0000130
131 /// Type index mappings of type server PDBs that we've loaded so far.
132 std::map<GUID, CVIndexMap> TypeServerIndexMappings;
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000133};
134}
135
Rui Ueyamabe939b32016-11-21 17:22:35 +0000136static SectionChunk *findByName(std::vector<SectionChunk *> &Sections,
137 StringRef Name) {
138 for (SectionChunk *C : Sections)
139 if (C->getSectionName() == Name)
140 return C;
141 return nullptr;
142}
143
Reid Kleckner44cdb102017-06-19 17:21:45 +0000144static ArrayRef<uint8_t> consumeDebugMagic(ArrayRef<uint8_t> Data,
145 StringRef SecName) {
Rui Ueyamac5cb7372016-12-01 01:22:48 +0000146 // First 4 bytes are section magic.
Rui Ueyamac5cb7372016-12-01 01:22:48 +0000147 if (Data.size() < 4)
Rui Ueyama52896622017-01-12 03:09:25 +0000148 fatal(SecName + " too short");
Reid Kleckner44cdb102017-06-19 17:21:45 +0000149 if (support::endian::read32le(Data.data()) != COFF::DEBUG_SECTION_MAGIC)
Rui Ueyama52896622017-01-12 03:09:25 +0000150 fatal(SecName + " has an invalid magic");
Rui Ueyama26186c72016-12-09 04:46:54 +0000151 return Data.slice(4);
152}
Rui Ueyamac5cb7372016-12-01 01:22:48 +0000153
Rui Ueyamae1b48e02017-07-26 23:05:24 +0000154static ArrayRef<uint8_t> getDebugSection(ObjFile *File, StringRef SecName) {
Reid Kleckner44cdb102017-06-19 17:21:45 +0000155 if (SectionChunk *Sec = findByName(File->getDebugChunks(), SecName))
156 return consumeDebugMagic(Sec->getContents(), SecName);
157 return {};
158}
159
Reid Kleckner5d577522017-03-24 17:26:38 +0000160static void addTypeInfo(pdb::TpiStreamBuilder &TpiBuilder,
Reid Kleckner44cdb102017-06-19 17:21:45 +0000161 TypeTableBuilder &TypeTable) {
Reid Kleckner5d577522017-03-24 17:26:38 +0000162 // Start the TPI or IPI stream header.
163 TpiBuilder.setVersionHeader(pdb::PdbTpiV80);
Rui Ueyama52896622017-01-12 03:09:25 +0000164
Reid Klecknerd91ca762017-07-19 17:26:07 +0000165 // Flatten the in memory type table and hash each type.
Reid Kleckner5d577522017-03-24 17:26:38 +0000166 TypeTable.ForEachRecord([&](TypeIndex TI, ArrayRef<uint8_t> Rec) {
Reid Klecknerd91ca762017-07-19 17:26:07 +0000167 assert(Rec.size() >= sizeof(RecordPrefix));
168 const RecordPrefix *P = reinterpret_cast<const RecordPrefix *>(Rec.data());
169 CVType Type(static_cast<TypeLeafKind>(unsigned(P->RecordKind)), Rec);
170 auto Hash = pdb::hashTypeRecord(Type);
171 if (auto E = Hash.takeError())
172 fatal("type hashing error");
173 TpiBuilder.addTypeRecord(Rec, *Hash);
Reid Kleckner5d577522017-03-24 17:26:38 +0000174 });
Reid Kleckner5d577522017-03-24 17:26:38 +0000175}
176
Reid Kleckner651db912017-07-18 00:21:25 +0000177static Optional<TypeServer2Record>
178maybeReadTypeServerRecord(CVTypeArray &Types) {
179 auto I = Types.begin();
180 if (I == Types.end())
181 return None;
182 const CVType &Type = *I;
183 if (Type.kind() != LF_TYPESERVER2)
184 return None;
185 TypeServer2Record TS;
186 if (auto EC = TypeDeserializer::deserializeAs(const_cast<CVType &>(Type), TS))
187 fatal(EC, "error reading type server record");
188 return std::move(TS);
189}
190
Rui Ueyamae1b48e02017-07-26 23:05:24 +0000191const CVIndexMap &PDBLinker::mergeDebugT(ObjFile *File,
Reid Kleckner651db912017-07-18 00:21:25 +0000192 CVIndexMap &ObjectIndexMap) {
Reid Kleckner44cdb102017-06-19 17:21:45 +0000193 ArrayRef<uint8_t> Data = getDebugSection(File, ".debug$T");
194 if (Data.empty())
Reid Kleckner651db912017-07-18 00:21:25 +0000195 return ObjectIndexMap;
Reid Kleckner6597c282017-07-13 20:12:23 +0000196
Reid Kleckner44cdb102017-06-19 17:21:45 +0000197 BinaryByteStream Stream(Data, support::little);
198 CVTypeArray Types;
199 BinaryStreamReader Reader(Stream);
Reid Kleckner44cdb102017-06-19 17:21:45 +0000200 if (auto EC = Reader.readArray(Types, Reader.getLength()))
201 fatal(EC, "Reader::readArray failed");
Reid Kleckner651db912017-07-18 00:21:25 +0000202
203 // Look through type servers. If we've already seen this type server, don't
204 // merge any type information.
205 if (Optional<TypeServer2Record> TS = maybeReadTypeServerRecord(Types))
206 return maybeMergeTypeServerPDB(File, *TS);
207
208 // This is a /Z7 object. Fill in the temporary, caller-provided
209 // ObjectIndexMap.
210 if (auto Err = mergeTypeAndIdRecords(IDTable, TypeTable,
211 ObjectIndexMap.TPIMap, Types))
212 fatal(Err, "codeview::mergeTypeAndIdRecords failed");
213 return ObjectIndexMap;
214}
215
216static Expected<std::unique_ptr<pdb::NativeSession>>
217tryToLoadPDB(const GUID &GuidFromObj, StringRef TSPath) {
218 std::unique_ptr<pdb::IPDBSession> ThisSession;
219 if (auto EC =
220 pdb::loadDataForPDB(pdb::PDB_ReaderType::Native, TSPath, ThisSession))
221 return std::move(EC);
222
223 std::unique_ptr<pdb::NativeSession> NS(
224 static_cast<pdb::NativeSession *>(ThisSession.release()));
225 pdb::PDBFile &File = NS->getPDBFile();
226 auto ExpectedInfo = File.getPDBInfoStream();
227 // All PDB Files should have an Info stream.
228 if (!ExpectedInfo)
229 return ExpectedInfo.takeError();
230
231 // Just because a file with a matching name was found and it was an actual
232 // PDB file doesn't mean it matches. For it to match the InfoStream's GUID
233 // must match the GUID specified in the TypeServer2 record.
234 if (ExpectedInfo->getGuid() != GuidFromObj)
235 return make_error<pdb::GenericError>(
236 pdb::generic_error_code::type_server_not_found, TSPath);
237
238 return std::move(NS);
239}
240
Rui Ueyamae1b48e02017-07-26 23:05:24 +0000241const CVIndexMap &PDBLinker::maybeMergeTypeServerPDB(ObjFile *File,
Reid Kleckner651db912017-07-18 00:21:25 +0000242 TypeServer2Record &TS) {
243 // First, check if we already loaded a PDB with this GUID. Return the type
244 // index mapping if we have it.
245 auto Insertion = TypeServerIndexMappings.insert({TS.getGuid(), CVIndexMap()});
246 CVIndexMap &IndexMap = Insertion.first->second;
247 if (!Insertion.second)
248 return IndexMap;
249
250 // Mark this map as a type server map.
251 IndexMap.IsTypeServerMap = true;
252
253 // Check for a PDB at:
254 // 1. The given file path
255 // 2. Next to the object file or archive file
256 auto ExpectedSession = tryToLoadPDB(TS.getGuid(), TS.getName());
257 if (!ExpectedSession) {
258 consumeError(ExpectedSession.takeError());
259 StringRef LocalPath =
260 !File->ParentName.empty() ? File->ParentName : File->getName();
261 SmallString<128> Path = sys::path::parent_path(LocalPath);
Reid Klecknerf8522d82017-07-18 00:33:53 +0000262 sys::path::append(
263 Path, sys::path::filename(TS.getName(), sys::path::Style::windows));
Reid Kleckner651db912017-07-18 00:21:25 +0000264 ExpectedSession = tryToLoadPDB(TS.getGuid(), Path);
265 }
266 if (auto E = ExpectedSession.takeError())
267 fatal(E, "Type server PDB was not found");
268
269 // Merge TPI first, because the IPI stream will reference type indices.
270 auto ExpectedTpi = (*ExpectedSession)->getPDBFile().getPDBTpiStream();
271 if (auto E = ExpectedTpi.takeError())
272 fatal(E, "Type server does not have TPI stream");
273 if (auto Err = mergeTypeRecords(TypeTable, IndexMap.TPIMap,
274 ExpectedTpi->typeArray()))
275 fatal(Err, "codeview::mergeTypeRecords failed");
276
277 // Merge IPI.
278 auto ExpectedIpi = (*ExpectedSession)->getPDBFile().getPDBIpiStream();
279 if (auto E = ExpectedIpi.takeError())
280 fatal(E, "Type server does not have TPI stream");
281 if (auto Err = mergeIdRecords(IDTable, IndexMap.TPIMap, IndexMap.IPIMap,
282 ExpectedIpi->typeArray()))
283 fatal(Err, "codeview::mergeIdRecords failed");
284
285 return IndexMap;
Reid Kleckner44cdb102017-06-19 17:21:45 +0000286}
287
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000288static bool remapTypeIndex(TypeIndex &TI, ArrayRef<TypeIndex> TypeIndexMap) {
289 if (TI.isSimple())
290 return true;
291 if (TI.toArrayIndex() >= TypeIndexMap.size())
292 return false;
293 TI = TypeIndexMap[TI.toArrayIndex()];
294 return true;
295}
296
Rui Ueyamae1b48e02017-07-26 23:05:24 +0000297static void remapTypesInSymbolRecord(ObjFile *File,
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000298 MutableArrayRef<uint8_t> Contents,
Reid Kleckner651db912017-07-18 00:21:25 +0000299 const CVIndexMap &IndexMap,
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000300 ArrayRef<TiReference> TypeRefs) {
301 for (const TiReference &Ref : TypeRefs) {
302 unsigned ByteSize = Ref.Count * sizeof(TypeIndex);
Reid Kleckner03b5baf2017-07-12 18:49:43 +0000303 if (Contents.size() < Ref.Offset + ByteSize)
304 fatal("symbol record too short");
Reid Kleckner651db912017-07-18 00:21:25 +0000305
306 // This can be an item index or a type index. Choose the appropriate map.
307 ArrayRef<TypeIndex> TypeOrItemMap = IndexMap.TPIMap;
308 if (Ref.Kind == TiRefKind::IndexRef && IndexMap.IsTypeServerMap)
309 TypeOrItemMap = IndexMap.IPIMap;
310
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000311 MutableArrayRef<TypeIndex> TIs(
312 reinterpret_cast<TypeIndex *>(Contents.data() + Ref.Offset), Ref.Count);
Reid Kleckner03b5baf2017-07-12 18:49:43 +0000313 for (TypeIndex &TI : TIs) {
Reid Kleckner651db912017-07-18 00:21:25 +0000314 if (!remapTypeIndex(TI, TypeOrItemMap)) {
Reid Kleckner03b5baf2017-07-12 18:49:43 +0000315 TI = TypeIndex(SimpleTypeKind::NotTranslated);
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000316 log("ignoring symbol record in " + File->getName() +
317 " with bad type index 0x" + utohexstr(TI.getIndex()));
Reid Kleckner03b5baf2017-07-12 18:49:43 +0000318 continue;
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000319 }
Reid Kleckner03b5baf2017-07-12 18:49:43 +0000320 }
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000321 }
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000322}
323
324/// MSVC translates S_PROC_ID_END to S_END.
325uint16_t canonicalizeSymbolKind(SymbolKind Kind) {
326 if (Kind == SymbolKind::S_PROC_ID_END)
327 return SymbolKind::S_END;
328 return Kind;
329}
330
331/// Copy the symbol record. In a PDB, symbol records must be 4 byte aligned.
332/// The object file may not be aligned.
333static MutableArrayRef<uint8_t> copySymbolForPdb(const CVSymbol &Sym,
334 BumpPtrAllocator &Alloc) {
335 size_t Size = alignTo(Sym.length(), alignOf(CodeViewContainer::Pdb));
336 assert(Size >= 4 && "record too short");
337 assert(Size <= MaxRecordLength && "record too long");
338 void *Mem = Alloc.Allocate(Size, 4);
339
340 // Copy the symbol record and zero out any padding bytes.
341 MutableArrayRef<uint8_t> NewData(reinterpret_cast<uint8_t *>(Mem), Size);
342 memcpy(NewData.data(), Sym.data().data(), Sym.length());
343 memset(NewData.data() + Sym.length(), 0, Size - Sym.length());
344
345 // Update the record prefix length. It should point to the beginning of the
346 // next record. MSVC does some canonicalization of the record kind, so we do
347 // that as well.
348 auto *Prefix = reinterpret_cast<RecordPrefix *>(Mem);
349 Prefix->RecordKind = canonicalizeSymbolKind(Sym.kind());
350 Prefix->RecordLen = Size - 2;
351 return NewData;
352}
353
Reid Kleckner3f851922017-07-06 16:39:32 +0000354/// Return true if this symbol opens a scope. This implies that the symbol has
355/// "parent" and "end" fields, which contain the offset of the S_END or
356/// S_INLINESITE_END record.
357static bool symbolOpensScope(SymbolKind Kind) {
358 switch (Kind) {
359 case SymbolKind::S_GPROC32:
360 case SymbolKind::S_LPROC32:
361 case SymbolKind::S_LPROC32_ID:
362 case SymbolKind::S_GPROC32_ID:
363 case SymbolKind::S_BLOCK32:
364 case SymbolKind::S_SEPCODE:
365 case SymbolKind::S_THUNK32:
366 case SymbolKind::S_INLINESITE:
367 case SymbolKind::S_INLINESITE2:
368 return true;
369 default:
370 break;
371 }
372 return false;
373}
374
375static bool symbolEndsScope(SymbolKind Kind) {
376 switch (Kind) {
377 case SymbolKind::S_END:
378 case SymbolKind::S_PROC_ID_END:
379 case SymbolKind::S_INLINESITE_END:
380 return true;
381 default:
382 break;
383 }
384 return false;
385}
386
387struct ScopeRecord {
388 ulittle32_t PtrParent;
389 ulittle32_t PtrEnd;
390};
391
392struct SymbolScope {
393 ScopeRecord *OpeningRecord;
394 uint32_t ScopeOffset;
395};
396
397static void scopeStackOpen(SmallVectorImpl<SymbolScope> &Stack,
398 uint32_t CurOffset, CVSymbol &Sym) {
399 assert(symbolOpensScope(Sym.kind()));
400 SymbolScope S;
401 S.ScopeOffset = CurOffset;
402 S.OpeningRecord = const_cast<ScopeRecord *>(
403 reinterpret_cast<const ScopeRecord *>(Sym.content().data()));
404 S.OpeningRecord->PtrParent = Stack.empty() ? 0 : Stack.back().ScopeOffset;
405 Stack.push_back(S);
406}
407
408static void scopeStackClose(SmallVectorImpl<SymbolScope> &Stack,
Rui Ueyamae1b48e02017-07-26 23:05:24 +0000409 uint32_t CurOffset, ObjFile *File) {
Reid Kleckner3f851922017-07-06 16:39:32 +0000410 if (Stack.empty()) {
411 warn("symbol scopes are not balanced in " + File->getName());
412 return;
413 }
414 SymbolScope S = Stack.pop_back_val();
415 S.OpeningRecord->PtrEnd = CurOffset;
416}
417
Rui Ueyamae1b48e02017-07-26 23:05:24 +0000418static void mergeSymbolRecords(BumpPtrAllocator &Alloc, ObjFile *File,
Reid Kleckner651db912017-07-18 00:21:25 +0000419 const CVIndexMap &IndexMap,
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000420 BinaryStreamRef SymData) {
421 // FIXME: Improve error recovery by warning and skipping records when
422 // possible.
423 CVSymbolArray Syms;
424 BinaryStreamReader Reader(SymData);
425 ExitOnErr(Reader.readArray(Syms, Reader.getLength()));
Reid Kleckner3f851922017-07-06 16:39:32 +0000426 SmallVector<SymbolScope, 4> Scopes;
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000427 for (const CVSymbol &Sym : Syms) {
428 // Discover type index references in the record. Skip it if we don't know
429 // where they are.
430 SmallVector<TiReference, 32> TypeRefs;
431 if (!discoverTypeIndices(Sym, TypeRefs)) {
432 log("ignoring unknown symbol record with kind 0x" + utohexstr(Sym.kind()));
433 continue;
434 }
435
436 // Copy the symbol record so we can mutate it.
437 MutableArrayRef<uint8_t> NewData = copySymbolForPdb(Sym, Alloc);
438
439 // Re-map all the type index references.
440 MutableArrayRef<uint8_t> Contents =
441 NewData.drop_front(sizeof(RecordPrefix));
Reid Kleckner651db912017-07-18 00:21:25 +0000442 remapTypesInSymbolRecord(File, Contents, IndexMap, TypeRefs);
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000443
Reid Kleckner3f851922017-07-06 16:39:32 +0000444 // Fill in "Parent" and "End" fields by maintaining a stack of scopes.
445 CVSymbol NewSym(Sym.kind(), NewData);
446 if (symbolOpensScope(Sym.kind()))
447 scopeStackOpen(Scopes, File->ModuleDBI->getNextSymbolOffset(), NewSym);
448 else if (symbolEndsScope(Sym.kind()))
449 scopeStackClose(Scopes, File->ModuleDBI->getNextSymbolOffset(), File);
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000450
451 // Add the symbol to the module.
Reid Kleckner3f851922017-07-06 16:39:32 +0000452 File->ModuleDBI->addSymbol(NewSym);
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000453 }
454}
455
Reid Kleckner44cdb102017-06-19 17:21:45 +0000456// Allocate memory for a .debug$S section and relocate it.
457static ArrayRef<uint8_t> relocateDebugChunk(BumpPtrAllocator &Alloc,
458 SectionChunk *DebugChunk) {
459 uint8_t *Buffer = Alloc.Allocate<uint8_t>(DebugChunk->getSize());
460 assert(DebugChunk->OutputSectionOff == 0 &&
461 "debug sections should not be in output sections");
462 DebugChunk->writeTo(Buffer);
463 return consumeDebugMagic(makeArrayRef(Buffer, DebugChunk->getSize()),
464 ".debug$S");
465}
466
Rui Ueyamae1b48e02017-07-26 23:05:24 +0000467void PDBLinker::addObjFile(ObjFile *File) {
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000468 // Add a module descriptor for every object file. We need to put an absolute
469 // path to the object into the PDB. If this is a plain object, we make its
470 // path absolute. If it's an object in an archive, we make the archive path
471 // absolute.
472 bool InArchive = !File->ParentName.empty();
473 SmallString<128> Path = InArchive ? File->ParentName : File->getName();
474 sys::fs::make_absolute(Path);
475 sys::path::native(Path, sys::path::Style::windows);
476 StringRef Name = InArchive ? File->getName() : StringRef(Path);
Zachary Turner2897e032017-05-25 21:16:03 +0000477
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000478 File->ModuleDBI = &ExitOnErr(Builder.getDbiBuilder().addModuleInfo(Name));
479 File->ModuleDBI->setObjFileName(Path);
Reid Kleckner44cdb102017-06-19 17:21:45 +0000480
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000481 // Before we can process symbol substreams from .debug$S, we need to process
482 // type information, file checksums, and the string table. Add type info to
483 // the PDB first, so that we can get the map from object file type and item
484 // indices to PDB type and item indices.
Reid Kleckner651db912017-07-18 00:21:25 +0000485 CVIndexMap ObjectIndexMap;
486 const CVIndexMap &IndexMap = mergeDebugT(File, ObjectIndexMap);
Zachary Turner448dea42017-07-07 18:46:14 +0000487
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000488 // Now do all live .debug$S sections.
489 for (SectionChunk *DebugChunk : File->getDebugChunks()) {
490 if (!DebugChunk->isLive() || DebugChunk->getSectionName() != ".debug$S")
491 continue;
Reid Kleckner8cbdd0c2017-06-13 15:49:13 +0000492
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000493 ArrayRef<uint8_t> RelocatedDebugContents =
494 relocateDebugChunk(Alloc, DebugChunk);
495 if (RelocatedDebugContents.empty())
496 continue;
Reid Kleckner8cbdd0c2017-06-13 15:49:13 +0000497
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000498 DebugSubsectionArray Subsections;
499 BinaryStreamReader Reader(RelocatedDebugContents, support::little);
500 ExitOnErr(Reader.readArray(Subsections, RelocatedDebugContents.size()));
Rui Ueyama52896622017-01-12 03:09:25 +0000501
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000502 DebugStringTableSubsectionRef CVStrTab;
503 DebugChecksumsSubsectionRef Checksums;
504 for (const DebugSubsectionRecord &SS : Subsections) {
505 switch (SS.kind()) {
506 case DebugSubsectionKind::StringTable:
507 ExitOnErr(CVStrTab.initialize(SS.getRecordData()));
508 break;
509 case DebugSubsectionKind::FileChecksums:
510 ExitOnErr(Checksums.initialize(SS.getRecordData()));
511 break;
512 case DebugSubsectionKind::Lines:
513 // We can add the relocated line table directly to the PDB without
514 // modification because the file checksum offsets will stay the same.
515 File->ModuleDBI->addDebugSubsection(SS);
516 break;
517 case DebugSubsectionKind::Symbols:
Reid Kleckner651db912017-07-18 00:21:25 +0000518 mergeSymbolRecords(Alloc, File, IndexMap, SS.getRecordData());
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000519 break;
520 default:
521 // FIXME: Process the rest of the subsections.
522 break;
Reid Kleckner44cdb102017-06-19 17:21:45 +0000523 }
524 }
Rui Ueyama52896622017-01-12 03:09:25 +0000525
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000526 if (Checksums.valid()) {
527 // Make a new file checksum table that refers to offsets in the PDB-wide
528 // string table. Generally the string table subsection appears after the
529 // checksum table, so we have to do this after looping over all the
530 // subsections.
531 if (!CVStrTab.valid())
532 fatal(".debug$S sections must have both a string table subsection "
533 "and a checksum subsection table or neither");
534 auto NewChecksums = make_unique<DebugChecksumsSubsection>(PDBStrTab);
535 for (FileChecksumEntry &FC : Checksums) {
536 StringRef FileName = ExitOnErr(CVStrTab.getString(FC.FileNameOffset));
537 ExitOnErr(Builder.getDbiBuilder().addModuleSourceFile(*File->ModuleDBI,
538 FileName));
539 NewChecksums->addChecksum(FileName, FC.Kind, FC.Checksum);
540 }
541 File->ModuleDBI->addDebugSubsection(std::move(NewChecksums));
542 }
543 }
544}
545
Reid Klecknereacdf042017-07-27 18:25:59 +0000546static PublicSym32 createPublic(Defined *Def) {
547 PublicSym32 Pub(SymbolKind::S_PUB32);
548 Pub.Name = Def->getName();
549 if (auto *D = dyn_cast<DefinedCOFF>(Def)) {
550 if (D->getCOFFSymbol().isFunctionDefinition())
551 Pub.Flags = PublicSymFlags::Function;
552 } else if (isa<DefinedImportThunk>(Def)) {
553 Pub.Flags = PublicSymFlags::Function;
554 }
555
556 OutputSection *OS = Def->getChunk()->getOutputSection();
557 assert(OS && "all publics should be in final image");
558 Pub.Offset = Def->getRVA() - OS->getRVA();
559 Pub.Segment = OS->SectionIndex;
560 return Pub;
561}
562
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000563// Add all object files to the PDB. Merge .debug$T sections into IpiData and
564// TpiData.
565void PDBLinker::addObjectsToPDB() {
Rui Ueyamaacd632d2017-07-27 00:45:26 +0000566 for (ObjFile *File : ObjFile::Instances)
Rui Ueyamae1b48e02017-07-26 23:05:24 +0000567 addObjFile(File);
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000568
569 Builder.getStringTableBuilder().setStrings(PDBStrTab);
Reid Kleckner44cdb102017-06-19 17:21:45 +0000570
Reid Kleckner5d577522017-03-24 17:26:38 +0000571 // Construct TPI stream contents.
Reid Kleckner13fc4112017-04-04 00:56:34 +0000572 addTypeInfo(Builder.getTpiBuilder(), TypeTable);
Reid Kleckner5d577522017-03-24 17:26:38 +0000573
574 // Construct IPI stream contents.
Reid Kleckner13fc4112017-04-04 00:56:34 +0000575 addTypeInfo(Builder.getIpiBuilder(), IDTable);
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000576
Reid Klecknereacdf042017-07-27 18:25:59 +0000577 // Compute the public symbols.
578 std::vector<PublicSym32> Publics;
579 Symtab->forEachSymbol([&Publics](Symbol *S) {
580 // Only emit defined, live symbols that have a chunk.
581 auto *Def = dyn_cast<Defined>(S->body());
582 if (Def && Def->isLive() && Def->getChunk())
583 Publics.push_back(createPublic(Def));
584 });
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000585
Reid Klecknereacdf042017-07-27 18:25:59 +0000586 if (!Publics.empty()) {
587 // Sort the public symbols and add them to the stream.
588 std::sort(Publics.begin(), Publics.end(),
589 [](const PublicSym32 &L, const PublicSym32 &R) {
590 return L.Name < R.Name;
591 });
592 auto &PublicsBuilder = Builder.getPublicsBuilder();
593 for (const PublicSym32 &Pub : Publics)
594 PublicsBuilder.addPublicSymbol(Pub);
595 }
Zachary Turner8d927b62017-07-31 19:36:08 +0000596 // Add globals stream. For now we don't actually write any thing useful to
597 // the globals stream, but the act of "getting" it also creates it lazily so
598 // that we write an empty stream.
599 (void)Builder.getGlobalsBuilder();
Rui Ueyama52896622017-01-12 03:09:25 +0000600}
601
Zachary Turner6708e0b2017-07-10 21:01:37 +0000602static void addLinkerModuleSymbols(StringRef Path,
603 pdb::DbiModuleDescriptorBuilder &Mod,
604 BumpPtrAllocator &Allocator) {
605 codeview::SymbolSerializer Serializer(Allocator, CodeViewContainer::Pdb);
606 codeview::ObjNameSym ONS(SymbolRecordKind::ObjNameSym);
607 codeview::Compile3Sym CS(SymbolRecordKind::Compile3Sym);
608 codeview::EnvBlockSym EBS(SymbolRecordKind::EnvBlockSym);
609
610 ONS.Name = "* Linker *";
611 ONS.Signature = 0;
612
613 CS.Machine = Config->is64() ? CPUType::X64 : CPUType::Intel80386;
614 CS.Flags = CompileSym3Flags::None;
615 CS.VersionBackendBuild = 0;
616 CS.VersionBackendMajor = 0;
617 CS.VersionBackendMinor = 0;
618 CS.VersionBackendQFE = 0;
619 CS.VersionFrontendBuild = 0;
620 CS.VersionFrontendMajor = 0;
621 CS.VersionFrontendMinor = 0;
622 CS.VersionFrontendQFE = 0;
623 CS.Version = "LLVM Linker";
624 CS.setLanguage(SourceLanguage::Link);
625
626 ArrayRef<StringRef> Args = makeArrayRef(Config->Argv).drop_front();
627 std::string ArgStr = llvm::join(Args, " ");
628 EBS.Fields.push_back("cwd");
629 SmallString<64> cwd;
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000630 sys::fs::current_path(cwd);
Zachary Turner6708e0b2017-07-10 21:01:37 +0000631 EBS.Fields.push_back(cwd);
632 EBS.Fields.push_back("exe");
Zachary Turner77f23b92017-07-10 21:09:11 +0000633 EBS.Fields.push_back(Config->Argv[0]);
Zachary Turner6708e0b2017-07-10 21:01:37 +0000634 EBS.Fields.push_back("pdb");
635 EBS.Fields.push_back(Path);
636 EBS.Fields.push_back("cmd");
637 EBS.Fields.push_back(ArgStr);
638 Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(
639 ONS, Allocator, CodeViewContainer::Pdb));
640 Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(
641 CS, Allocator, CodeViewContainer::Pdb));
642 Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(
643 EBS, Allocator, CodeViewContainer::Pdb));
644}
645
Rui Ueyama09e0b5f2016-11-12 00:00:51 +0000646// Creates a PDB file.
Reid Kleckner175af4b2017-08-03 21:15:09 +0000647void coff::createPDB(SymbolTable *Symtab,
648 ArrayRef<OutputSection *> OutputSections,
649 ArrayRef<uint8_t> SectionTable,
Saleem Abdulrasooldf8a13b2017-01-04 17:56:54 +0000650 const llvm::codeview::DebugInfo *DI) {
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000651 PDBLinker PDB(Symtab);
652 PDB.initialize(DI);
653 PDB.addObjectsToPDB();
Reid Kleckner175af4b2017-08-03 21:15:09 +0000654 PDB.addSections(OutputSections, SectionTable);
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000655 PDB.commit();
656}
657
658void PDBLinker::initialize(const llvm::codeview::DebugInfo *DI) {
Rui Ueyama12979542016-09-30 20:53:45 +0000659 ExitOnErr(Builder.initialize(4096)); // 4096 is blocksize
Rui Ueyama7f382992016-09-15 18:55:18 +0000660
Rui Ueyama8d3fb5d2016-10-05 22:08:58 +0000661 // Create streams in MSF for predefined streams, namely
662 // PDB, TPI, DBI and IPI.
663 for (int I = 0; I < (int)pdb::kSpecialStreamCount; ++I)
664 ExitOnErr(Builder.getMsfBuilder().addStream(0));
Rui Ueyama7f382992016-09-15 18:55:18 +0000665
Rui Ueyamabb542b32016-09-16 22:51:17 +0000666 // Add an Info stream.
667 auto &InfoBuilder = Builder.getInfoBuilder();
Saleem Abdulrasool0acd6dd2017-02-07 04:28:02 +0000668 InfoBuilder.setAge(DI ? DI->PDB70.Age : 0);
669
Reid Kleckner67653ee2017-07-17 23:59:44 +0000670 GUID uuid{};
Saleem Abdulrasool0acd6dd2017-02-07 04:28:02 +0000671 if (DI)
672 memcpy(&uuid, &DI->PDB70.Signature, sizeof(uuid));
673 InfoBuilder.setGuid(uuid);
Zachary Turner3a11fdf2017-07-07 20:25:39 +0000674 InfoBuilder.setSignature(time(nullptr));
Rui Ueyamabb542b32016-09-16 22:51:17 +0000675 InfoBuilder.setVersion(pdb::PdbRaw_ImplVer::PdbImplVC70);
Rui Ueyama7f382992016-09-15 18:55:18 +0000676
Zachary Turner6c4bfba2017-07-07 05:04:36 +0000677 // Add an empty DBI stream.
Reid Kleckner8cbdd0c2017-06-13 15:49:13 +0000678 pdb::DbiStreamBuilder &DbiBuilder = Builder.getDbiBuilder();
Zachary Turnerc1e93e52017-07-07 18:45:56 +0000679 DbiBuilder.setVersionHeader(pdb::PdbDbiV70);
Zachary Turner3a11fdf2017-07-07 20:25:39 +0000680 ExitOnErr(DbiBuilder.addDbgStream(pdb::DbgHeaderType::NewFPO, {}));
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000681}
Rui Ueyama1343fac2016-10-06 22:52:01 +0000682
Reid Kleckner175af4b2017-08-03 21:15:09 +0000683void PDBLinker::addSectionContrib(pdb::DbiModuleDescriptorBuilder &LinkerModule,
684 OutputSection *OS, Chunk *C) {
685 pdb::SectionContrib SC;
686 memset(&SC, 0, sizeof(SC));
687 SC.ISect = OS->SectionIndex;
688 SC.Off = C->getRVA() - OS->getRVA();
689 SC.Size = C->getSize();
690 if (auto *SecChunk = dyn_cast<SectionChunk>(C)) {
691 SC.Characteristics = SecChunk->Header->Characteristics;
692 SC.Imod = SecChunk->File->ModuleDBI->getModuleIndex();
693 } else {
694 SC.Characteristics = OS->getCharacteristics();
695 // FIXME: When we start creating DBI for import libraries, use those here.
696 SC.Imod = LinkerModule.getModuleIndex();
697 }
698 SC.DataCrc = 0; // FIXME
699 SC.RelocCrc = 0; // FIXME
700 Builder.getDbiBuilder().addSectionContrib(SC);
701}
Rui Ueyama09e0b5f2016-11-12 00:00:51 +0000702
Reid Kleckner175af4b2017-08-03 21:15:09 +0000703void PDBLinker::addSections(ArrayRef<OutputSection *> OutputSections,
704 ArrayRef<uint8_t> SectionTable) {
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000705 // It's not entirely clear what this is, but the * Linker * module uses it.
Reid Kleckner175af4b2017-08-03 21:15:09 +0000706 pdb::DbiStreamBuilder &DbiBuilder = Builder.getDbiBuilder();
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000707 NativePath = Config->PDBPath;
708 sys::fs::make_absolute(NativePath);
709 sys::path::native(NativePath, sys::path::Style::windows);
710 uint32_t PdbFilePathNI = DbiBuilder.addECName(NativePath);
Zachary Turner6c4bfba2017-07-07 05:04:36 +0000711 auto &LinkerModule = ExitOnErr(DbiBuilder.addModuleInfo("* Linker *"));
712 LinkerModule.setPdbFilePathNI(PdbFilePathNI);
Zachary Turner6708e0b2017-07-10 21:01:37 +0000713 addLinkerModuleSymbols(NativePath, LinkerModule, Alloc);
Rui Ueyamac91f7162016-11-16 01:10:46 +0000714
Reid Kleckner175af4b2017-08-03 21:15:09 +0000715 // Add section contributions. They must be ordered by ascending RVA.
716 for (OutputSection *OS : OutputSections)
717 for (Chunk *C : OS->getChunks())
718 addSectionContrib(LinkerModule, OS, C);
719
720 // Add Section Map stream.
721 ArrayRef<object::coff_section> Sections = {
722 (const object::coff_section *)SectionTable.data(),
723 SectionTable.size() / sizeof(object::coff_section)};
724 SectionMap = pdb::DbiStreamBuilder::createSectionMap(Sections);
725 DbiBuilder.setSectionMap(SectionMap);
726
Rui Ueyama9f66f822016-10-11 19:45:07 +0000727 // Add COFF section header stream.
728 ExitOnErr(
729 DbiBuilder.addDbgStream(pdb::DbgHeaderType::SectionHdr, SectionTable));
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000730}
Rui Ueyama9f66f822016-10-11 19:45:07 +0000731
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000732void PDBLinker::commit() {
Rui Ueyama3e9d6bb2016-09-26 23:53:55 +0000733 // Write to a file.
Zachary Turner6708e0b2017-07-10 21:01:37 +0000734 ExitOnErr(Builder.commit(Config->PDBPath));
Rui Ueyamae7378242015-12-04 23:11:05 +0000735}