blob: b22ed1487ae8a2995d9ded93edf569c60033d59a [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"
Zachary Turner42e7cc1b2018-09-11 22:35:01 +000019#include "llvm/DebugInfo/CodeView/DebugFrameDataSubsection.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"
David Blaikieddcd8562018-03-26 23:43:29 +000049#include "llvm/Object/CVDebugRecord.h"
Zachary Turnerd9dc2822017-03-02 20:52:51 +000050#include "llvm/Support/BinaryByteStream.h"
Rui Ueyama1763c0d2015-12-08 18:39:55 +000051#include "llvm/Support/Endian.h"
Zachary Turner60478582018-01-05 19:12:40 +000052#include "llvm/Support/FormatVariadic.h"
Zachary Turner676386ff2017-08-07 20:23:45 +000053#include "llvm/Support/JamCRC.h"
Zachary Turner7b327d02017-02-16 23:35:45 +000054#include "llvm/Support/Path.h"
Rui Ueyamabe939b32016-11-21 17:22:35 +000055#include "llvm/Support/ScopedPrinter.h"
Rui Ueyamae7378242015-12-04 23:11:05 +000056#include <memory>
57
Rui Ueyama1d99ab32016-09-15 22:24:51 +000058using namespace lld;
Rui Ueyama09e0b5f2016-11-12 00:00:51 +000059using namespace lld::coff;
Rui Ueyamae7378242015-12-04 23:11:05 +000060using namespace llvm;
Rui Ueyamabe939b32016-11-21 17:22:35 +000061using namespace llvm::codeview;
Rui Ueyamae7378242015-12-04 23:11:05 +000062
Rui Ueyama09e0b5f2016-11-12 00:00:51 +000063using llvm::object::coff_section;
64
Rui Ueyamab28c6d42016-09-16 04:32:33 +000065static ExitOnError ExitOnErr;
66
Zachary Turner727f1532018-01-17 19:16:26 +000067static Timer TotalPdbLinkTimer("PDB Emission (Cumulative)", Timer::root());
68
69static Timer AddObjectsTimer("Add Objects", TotalPdbLinkTimer);
70static Timer TypeMergingTimer("Type Merging", AddObjectsTimer);
71static Timer SymbolMergingTimer("Symbol Merging", AddObjectsTimer);
72static Timer GlobalsLayoutTimer("Globals Stream Layout", TotalPdbLinkTimer);
73static Timer TpiStreamLayoutTimer("TPI Stream Layout", TotalPdbLinkTimer);
74static Timer DiskCommitTimer("Commit to Disk", TotalPdbLinkTimer);
75
Reid Kleckner0faf6d72017-07-14 00:14:58 +000076namespace {
Reid Kleckner651db912017-07-18 00:21:25 +000077/// Map from type index and item index in a type server PDB to the
78/// corresponding index in the destination PDB.
79struct CVIndexMap {
80 SmallVector<TypeIndex, 0> TPIMap;
81 SmallVector<TypeIndex, 0> IPIMap;
82 bool IsTypeServerMap = false;
83};
84
Zachary Turnera1f85f82018-09-12 21:02:01 +000085class DebugSHandler;
86
Reid Kleckner0faf6d72017-07-14 00:14:58 +000087class PDBLinker {
Zachary Turnera1f85f82018-09-12 21:02:01 +000088 friend DebugSHandler;
89
Reid Kleckner0faf6d72017-07-14 00:14:58 +000090public:
91 PDBLinker(SymbolTable *Symtab)
Sam Cleggea244bf2017-12-14 21:09:31 +000092 : Alloc(), Symtab(Symtab), Builder(Alloc), TypeTable(Alloc),
Zachary Turnera6fb5362018-03-23 18:43:39 +000093 IDTable(Alloc), GlobalTypeTable(Alloc), GlobalIDTable(Alloc) {
94 // This isn't strictly necessary, but link.exe usually puts an empty string
95 // as the first "valid" string in the string table, so we do the same in
96 // order to maintain as much byte-for-byte compatibility as possible.
97 PDBStrTab.insert("");
98 }
Reid Kleckner0faf6d72017-07-14 00:14:58 +000099
100 /// Emit the basic PDB structure: initial streams, headers, etc.
Nico Weber0bd2d302018-09-15 18:37:22 +0000101 void initialize(llvm::codeview::DebugInfo *BuildId);
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000102
Zachary Turnerf2282762018-03-23 19:57:25 +0000103 /// Add natvis files specified on the command line.
104 void addNatvisFiles();
105
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000106 /// Link CodeView from each object file in the symbol table into the PDB.
107 void addObjectsToPDB();
108
109 /// Link CodeView from a single object file into the PDB.
Rui Ueyamae1b48e02017-07-26 23:05:24 +0000110 void addObjFile(ObjFile *File);
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000111
Reid Kleckner651db912017-07-18 00:21:25 +0000112 /// Produce a mapping from the type and item indices used in the object
113 /// file to those in the destination PDB.
114 ///
115 /// If the object file uses a type server PDB (compiled with /Zi), merge TPI
116 /// and IPI from the type server PDB and return a map for it. Each unique type
117 /// server PDB is merged at most once, so this may return an existing index
118 /// mapping.
119 ///
120 /// If the object does not use a type server PDB (compiled with /Z7), we merge
121 /// all the type and item records from the .debug$S stream and fill in the
122 /// caller-provided ObjectIndexMap.
Colden Cullenb9b6ed92018-01-31 17:48:04 +0000123 Expected<const CVIndexMap&> mergeDebugT(ObjFile *File,
124 CVIndexMap &ObjectIndexMap);
Reid Kleckner651db912017-07-18 00:21:25 +0000125
Colden Cullenb9b6ed92018-01-31 17:48:04 +0000126 Expected<const CVIndexMap&> maybeMergeTypeServerPDB(ObjFile *File,
127 TypeServer2Record &TS);
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000128
129 /// Add the section map and section contributions to the PDB.
Reid Kleckner175af4b2017-08-03 21:15:09 +0000130 void addSections(ArrayRef<OutputSection *> OutputSections,
131 ArrayRef<uint8_t> SectionTable);
132
Nico Weber0bd2d302018-09-15 18:37:22 +0000133 /// Write the PDB to disk and store the Guid generated for it in *Guid.
134 void commit(codeview::GUID *Guid);
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000135
136private:
137 BumpPtrAllocator Alloc;
138
139 SymbolTable *Symtab;
140
141 pdb::PDBFileBuilder Builder;
142
143 /// Type records that will go into the PDB TPI stream.
Zachary Turnerca6dbf12017-11-30 18:39:50 +0000144 MergingTypeTableBuilder TypeTable;
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000145
146 /// Item records that will go into the PDB IPI stream.
Zachary Turnerca6dbf12017-11-30 18:39:50 +0000147 MergingTypeTableBuilder IDTable;
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000148
Zachary Turner0d07a8e2017-12-14 18:07:04 +0000149 /// Type records that will go into the PDB TPI stream (for /DEBUG:GHASH)
150 GlobalTypeTableBuilder GlobalTypeTable;
151
152 /// Item records that will go into the PDB IPI stream (for /DEBUG:GHASH)
153 GlobalTypeTableBuilder GlobalIDTable;
154
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000155 /// PDBs use a single global string table for filenames in the file checksum
156 /// table.
157 DebugStringTableSubsection PDBStrTab;
158
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000159 llvm::SmallString<128> NativePath;
160
Zachary Turner3868cfd2018-02-28 18:09:18 +0000161 /// A list of other PDBs which are loaded during the linking process and which
162 /// we need to keep around since the linking operation may reference pointers
163 /// inside of these PDBs.
164 llvm::SmallVector<std::unique_ptr<pdb::NativeSession>, 2> LoadedPDBs;
165
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000166 std::vector<pdb::SecMapEntry> SectionMap;
Reid Kleckner651db912017-07-18 00:21:25 +0000167
168 /// Type index mappings of type server PDBs that we've loaded so far.
169 std::map<GUID, CVIndexMap> TypeServerIndexMappings;
Colden Cullenb9b6ed92018-01-31 17:48:04 +0000170
171 /// List of TypeServer PDBs which cannot be loaded.
172 /// Cached to prevent repeated load attempts.
Alexandre Ganead93b07f2018-09-10 13:51:21 +0000173 std::map<GUID, std::string> MissingTypeServerPDBs;
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000174};
Zachary Turnera1f85f82018-09-12 21:02:01 +0000175
176class DebugSHandler {
177 PDBLinker &Linker;
178
179 /// The object file whose .debug$S sections we're processing.
180 ObjFile &File;
181
182 /// The result of merging type indices.
183 const CVIndexMap &IndexMap;
184
185 /// The DEBUG_S_STRINGTABLE subsection. These strings are referred to by
186 /// index from other records in the .debug$S section. All of these strings
187 /// need to be added to the global PDB string table, and all references to
188 /// these strings need to have their indices re-written to refer to the
189 /// global PDB string table.
190 DebugStringTableSubsectionRef CVStrTab;
191
192 /// The DEBUG_S_FILECHKSMS subsection. As above, these are referred to
193 /// by other records in the .debug$S section and need to be merged into the
194 /// PDB.
195 DebugChecksumsSubsectionRef Checksums;
196
197 /// The DEBUG_S_FRAMEDATA subsection(s). There can be more than one of
198 /// these and they need not appear in any specific order. However, they
199 /// contain string table references which need to be re-written, so we
200 /// collect them all here and re-write them after all subsections have been
201 /// discovered and processed.
202 std::vector<DebugFrameDataSubsectionRef> NewFpoFrames;
203
204 /// Pointers to raw memory that we determine have string table references
205 /// that need to be re-written. We first process all .debug$S subsections
206 /// to ensure that we can handle subsections written in any order, building
207 /// up this list as we go. At the end, we use the string table (which must
208 /// have been discovered by now else it is an error) to re-write these
209 /// references.
210 std::vector<ulittle32_t *> StringTableReferences;
211
212public:
213 DebugSHandler(PDBLinker &Linker, ObjFile &File, const CVIndexMap &IndexMap)
214 : Linker(Linker), File(File), IndexMap(IndexMap) {}
215
216 void handleDebugS(lld::coff::SectionChunk &DebugS);
217 void finish();
218};
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000219}
220
Sam Clegg0fb6faa2017-12-08 01:09:21 +0000221static SectionChunk *findByName(ArrayRef<SectionChunk *> Sections,
Rui Ueyamabe939b32016-11-21 17:22:35 +0000222 StringRef Name) {
223 for (SectionChunk *C : Sections)
224 if (C->getSectionName() == Name)
225 return C;
226 return nullptr;
227}
228
Reid Kleckner44cdb102017-06-19 17:21:45 +0000229static ArrayRef<uint8_t> consumeDebugMagic(ArrayRef<uint8_t> Data,
230 StringRef SecName) {
Rui Ueyamac5cb7372016-12-01 01:22:48 +0000231 // First 4 bytes are section magic.
Rui Ueyamac5cb7372016-12-01 01:22:48 +0000232 if (Data.size() < 4)
Rui Ueyama52896622017-01-12 03:09:25 +0000233 fatal(SecName + " too short");
Reid Kleckner44cdb102017-06-19 17:21:45 +0000234 if (support::endian::read32le(Data.data()) != COFF::DEBUG_SECTION_MAGIC)
Rui Ueyama52896622017-01-12 03:09:25 +0000235 fatal(SecName + " has an invalid magic");
Rui Ueyama26186c72016-12-09 04:46:54 +0000236 return Data.slice(4);
237}
Rui Ueyamac5cb7372016-12-01 01:22:48 +0000238
Rui Ueyamae1b48e02017-07-26 23:05:24 +0000239static ArrayRef<uint8_t> getDebugSection(ObjFile *File, StringRef SecName) {
Reid Kleckner44cdb102017-06-19 17:21:45 +0000240 if (SectionChunk *Sec = findByName(File->getDebugChunks(), SecName))
241 return consumeDebugMagic(Sec->getContents(), SecName);
242 return {};
243}
244
Zachary Turner0d07a8e2017-12-14 18:07:04 +0000245// A COFF .debug$H section is currently a clang extension. This function checks
246// if a .debug$H section is in a format that we expect / understand, so that we
247// can ignore any sections which are coincidentally also named .debug$H but do
248// not contain a format we recognize.
249static bool canUseDebugH(ArrayRef<uint8_t> DebugH) {
250 if (DebugH.size() < sizeof(object::debug_h_header))
251 return false;
252 auto *Header =
253 reinterpret_cast<const object::debug_h_header *>(DebugH.data());
254 DebugH = DebugH.drop_front(sizeof(object::debug_h_header));
255 return Header->Magic == COFF::DEBUG_HASHES_SECTION_MAGIC &&
256 Header->Version == 0 &&
Zachary Turnerc7626662018-05-17 22:55:15 +0000257 Header->HashAlgorithm == uint16_t(GlobalTypeHashAlg::SHA1_8) &&
258 (DebugH.size() % 8 == 0);
Zachary Turner0d07a8e2017-12-14 18:07:04 +0000259}
260
261static Optional<ArrayRef<uint8_t>> getDebugH(ObjFile *File) {
262 SectionChunk *Sec = findByName(File->getDebugChunks(), ".debug$H");
263 if (!Sec)
264 return llvm::None;
265 ArrayRef<uint8_t> Contents = Sec->getContents();
266 if (!canUseDebugH(Contents))
267 return None;
268 return Contents;
269}
270
271static ArrayRef<GloballyHashedType>
272getHashesFromDebugH(ArrayRef<uint8_t> DebugH) {
273 assert(canUseDebugH(DebugH));
274
275 DebugH = DebugH.drop_front(sizeof(object::debug_h_header));
276 uint32_t Count = DebugH.size() / sizeof(GloballyHashedType);
277 return {reinterpret_cast<const GloballyHashedType *>(DebugH.data()), Count};
278}
279
Reid Kleckner5d577522017-03-24 17:26:38 +0000280static void addTypeInfo(pdb::TpiStreamBuilder &TpiBuilder,
Zachary Turner3e3936d2017-11-29 19:35:21 +0000281 TypeCollection &TypeTable) {
Reid Kleckner5d577522017-03-24 17:26:38 +0000282 // Start the TPI or IPI stream header.
283 TpiBuilder.setVersionHeader(pdb::PdbTpiV80);
Rui Ueyama52896622017-01-12 03:09:25 +0000284
Reid Klecknerd91ca762017-07-19 17:26:07 +0000285 // Flatten the in memory type table and hash each type.
Zachary Turner3e3936d2017-11-29 19:35:21 +0000286 TypeTable.ForEachRecord([&](TypeIndex TI, const CVType &Type) {
Reid Klecknerd91ca762017-07-19 17:26:07 +0000287 auto Hash = pdb::hashTypeRecord(Type);
288 if (auto E = Hash.takeError())
289 fatal("type hashing error");
Zachary Turner3e3936d2017-11-29 19:35:21 +0000290 TpiBuilder.addTypeRecord(Type.RecordData, *Hash);
Reid Kleckner5d577522017-03-24 17:26:38 +0000291 });
Reid Kleckner5d577522017-03-24 17:26:38 +0000292}
293
Reid Kleckner651db912017-07-18 00:21:25 +0000294static Optional<TypeServer2Record>
295maybeReadTypeServerRecord(CVTypeArray &Types) {
296 auto I = Types.begin();
297 if (I == Types.end())
298 return None;
299 const CVType &Type = *I;
300 if (Type.kind() != LF_TYPESERVER2)
301 return None;
302 TypeServer2Record TS;
303 if (auto EC = TypeDeserializer::deserializeAs(const_cast<CVType &>(Type), TS))
Bob Haarmanb8a59c82017-10-25 22:28:38 +0000304 fatal("error reading type server record: " + toString(std::move(EC)));
Reid Kleckner651db912017-07-18 00:21:25 +0000305 return std::move(TS);
306}
307
Colden Cullenb9b6ed92018-01-31 17:48:04 +0000308Expected<const CVIndexMap&> PDBLinker::mergeDebugT(ObjFile *File,
309 CVIndexMap &ObjectIndexMap) {
Zachary Turner727f1532018-01-17 19:16:26 +0000310 ScopedTimer T(TypeMergingTimer);
311
Reid Kleckner44cdb102017-06-19 17:21:45 +0000312 ArrayRef<uint8_t> Data = getDebugSection(File, ".debug$T");
313 if (Data.empty())
Reid Kleckner651db912017-07-18 00:21:25 +0000314 return ObjectIndexMap;
Reid Kleckner6597c282017-07-13 20:12:23 +0000315
Reid Kleckner44cdb102017-06-19 17:21:45 +0000316 BinaryByteStream Stream(Data, support::little);
317 CVTypeArray Types;
318 BinaryStreamReader Reader(Stream);
Reid Kleckner44cdb102017-06-19 17:21:45 +0000319 if (auto EC = Reader.readArray(Types, Reader.getLength()))
Bob Haarmanb8a59c82017-10-25 22:28:38 +0000320 fatal("Reader::readArray failed: " + toString(std::move(EC)));
Reid Kleckner651db912017-07-18 00:21:25 +0000321
322 // Look through type servers. If we've already seen this type server, don't
323 // merge any type information.
324 if (Optional<TypeServer2Record> TS = maybeReadTypeServerRecord(Types))
325 return maybeMergeTypeServerPDB(File, *TS);
326
327 // This is a /Z7 object. Fill in the temporary, caller-provided
328 // ObjectIndexMap.
Zachary Turner0d07a8e2017-12-14 18:07:04 +0000329 if (Config->DebugGHashes) {
330 ArrayRef<GloballyHashedType> Hashes;
331 std::vector<GloballyHashedType> OwnedHashes;
332 if (Optional<ArrayRef<uint8_t>> DebugH = getDebugH(File))
333 Hashes = getHashesFromDebugH(*DebugH);
334 else {
335 OwnedHashes = GloballyHashedType::hashTypes(Types);
336 Hashes = OwnedHashes;
337 }
338
339 if (auto Err = mergeTypeAndIdRecords(GlobalIDTable, GlobalTypeTable,
340 ObjectIndexMap.TPIMap, Types, Hashes))
341 fatal("codeview::mergeTypeAndIdRecords failed: " +
342 toString(std::move(Err)));
343 } else {
344 if (auto Err = mergeTypeAndIdRecords(IDTable, TypeTable,
345 ObjectIndexMap.TPIMap, Types))
346 fatal("codeview::mergeTypeAndIdRecords failed: " +
347 toString(std::move(Err)));
348 }
Reid Kleckner651db912017-07-18 00:21:25 +0000349 return ObjectIndexMap;
350}
351
352static Expected<std::unique_ptr<pdb::NativeSession>>
353tryToLoadPDB(const GUID &GuidFromObj, StringRef TSPath) {
Peter Collingbourne75257bc2017-10-20 19:48:26 +0000354 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = MemoryBuffer::getFile(
355 TSPath, /*FileSize=*/-1, /*RequiresNullTerminator=*/false);
356 if (!MBOrErr)
357 return errorCodeToError(MBOrErr.getError());
358
Reid Kleckner651db912017-07-18 00:21:25 +0000359 std::unique_ptr<pdb::IPDBSession> ThisSession;
Peter Collingbourne75257bc2017-10-20 19:48:26 +0000360 if (auto EC = pdb::NativeSession::createFromPdb(
361 MemoryBuffer::getMemBuffer(Driver->takeBuffer(std::move(*MBOrErr)),
362 /*RequiresNullTerminator=*/false),
363 ThisSession))
Reid Kleckner651db912017-07-18 00:21:25 +0000364 return std::move(EC);
365
366 std::unique_ptr<pdb::NativeSession> NS(
367 static_cast<pdb::NativeSession *>(ThisSession.release()));
368 pdb::PDBFile &File = NS->getPDBFile();
369 auto ExpectedInfo = File.getPDBInfoStream();
370 // All PDB Files should have an Info stream.
371 if (!ExpectedInfo)
372 return ExpectedInfo.takeError();
373
374 // Just because a file with a matching name was found and it was an actual
375 // PDB file doesn't mean it matches. For it to match the InfoStream's GUID
376 // must match the GUID specified in the TypeServer2 record.
377 if (ExpectedInfo->getGuid() != GuidFromObj)
Alexandre Ganead93b07f2018-09-10 13:51:21 +0000378 return make_error<pdb::PDBError>(pdb::pdb_error_code::signature_out_of_date);
Reid Kleckner651db912017-07-18 00:21:25 +0000379
380 return std::move(NS);
381}
382
Nico Weber0bd2d302018-09-15 18:37:22 +0000383Expected<const CVIndexMap &>
384PDBLinker::maybeMergeTypeServerPDB(ObjFile *File, TypeServer2Record &TS) {
Alexandre Ganead93b07f2018-09-10 13:51:21 +0000385 const GUID &TSId = TS.getGuid();
Colden Cullenb9b6ed92018-01-31 17:48:04 +0000386 StringRef TSPath = TS.getName();
387
388 // First, check if the PDB has previously failed to load.
Alexandre Ganead93b07f2018-09-10 13:51:21 +0000389 auto PrevErr = MissingTypeServerPDBs.find(TSId);
390 if (PrevErr != MissingTypeServerPDBs.end())
391 return createFileError(
Alexandre Ganea472e9b0a2018-09-10 14:07:11 +0000392 TSPath,
393 make_error<StringError>(PrevErr->second, inconvertibleErrorCode()));
Colden Cullenb9b6ed92018-01-31 17:48:04 +0000394
395 // Second, check if we already loaded a PDB with this GUID. Return the type
Reid Kleckner651db912017-07-18 00:21:25 +0000396 // index mapping if we have it.
Colden Cullenb9b6ed92018-01-31 17:48:04 +0000397 auto Insertion = TypeServerIndexMappings.insert({TSId, CVIndexMap()});
Reid Kleckner651db912017-07-18 00:21:25 +0000398 CVIndexMap &IndexMap = Insertion.first->second;
399 if (!Insertion.second)
400 return IndexMap;
401
402 // Mark this map as a type server map.
403 IndexMap.IsTypeServerMap = true;
404
405 // Check for a PDB at:
406 // 1. The given file path
407 // 2. Next to the object file or archive file
Alexandre Ganead93b07f2018-09-10 13:51:21 +0000408 auto ExpectedSession = handleExpected(
409 tryToLoadPDB(TSId, TSPath),
410 [&]() {
411 StringRef LocalPath =
412 !File->ParentName.empty() ? File->ParentName : File->getName();
413 SmallString<128> Path = sys::path::parent_path(LocalPath);
414 sys::path::append(
415 Path, sys::path::filename(TSPath, sys::path::Style::windows));
416 return tryToLoadPDB(TSId, Path);
417 },
418 [&](std::unique_ptr<ECError> EC) -> Error {
419 auto SysErr = EC->convertToErrorCode();
420 // Only re-try loading if the previous error was "No such file or
421 // directory"
422 if (SysErr.category() == std::generic_category() &&
423 SysErr.value() == ENOENT)
424 return Error::success();
425 return Error(std::move(EC));
426 });
427
Colden Cullenb9b6ed92018-01-31 17:48:04 +0000428 if (auto E = ExpectedSession.takeError()) {
429 TypeServerIndexMappings.erase(TSId);
Alexandre Ganead93b07f2018-09-10 13:51:21 +0000430
431 // Flatten the error to a string, for later display, if the error occurs
432 // again on the same PDB.
433 std::string ErrMsg;
434 raw_string_ostream S(ErrMsg);
435 S << E;
Alexandre Ganea472e9b0a2018-09-10 14:07:11 +0000436 MissingTypeServerPDBs.emplace(TSId, S.str());
Alexandre Ganead93b07f2018-09-10 13:51:21 +0000437
438 return createFileError(TSPath, std::move(E));
Colden Cullenb9b6ed92018-01-31 17:48:04 +0000439 }
Reid Kleckner651db912017-07-18 00:21:25 +0000440
Zachary Turner3868cfd2018-02-28 18:09:18 +0000441 pdb::NativeSession *Session = ExpectedSession->get();
442
443 // Keep a strong reference to this PDB, so that it's safe to hold pointers
444 // into the file.
445 LoadedPDBs.push_back(std::move(*ExpectedSession));
446
447 auto ExpectedTpi = Session->getPDBFile().getPDBTpiStream();
Reid Kleckner651db912017-07-18 00:21:25 +0000448 if (auto E = ExpectedTpi.takeError())
Bob Haarmanb8a59c82017-10-25 22:28:38 +0000449 fatal("Type server does not have TPI stream: " + toString(std::move(E)));
Zachary Turner3868cfd2018-02-28 18:09:18 +0000450 auto ExpectedIpi = Session->getPDBFile().getPDBIpiStream();
Reid Kleckner651db912017-07-18 00:21:25 +0000451 if (auto E = ExpectedIpi.takeError())
Bob Haarmanb8a59c82017-10-25 22:28:38 +0000452 fatal("Type server does not have TPI stream: " + toString(std::move(E)));
Zachary Turner0d07a8e2017-12-14 18:07:04 +0000453
454 if (Config->DebugGHashes) {
455 // PDBs do not actually store global hashes, so when merging a type server
456 // PDB we have to synthesize global hashes. To do this, we first synthesize
457 // global hashes for the TPI stream, since it is independent, then we
458 // synthesize hashes for the IPI stream, using the hashes for the TPI stream
459 // as inputs.
460 auto TpiHashes = GloballyHashedType::hashTypes(ExpectedTpi->typeArray());
461 auto IpiHashes =
462 GloballyHashedType::hashIds(ExpectedIpi->typeArray(), TpiHashes);
463
464 // Merge TPI first, because the IPI stream will reference type indices.
465 if (auto Err = mergeTypeRecords(GlobalTypeTable, IndexMap.TPIMap,
466 ExpectedTpi->typeArray(), TpiHashes))
467 fatal("codeview::mergeTypeRecords failed: " + toString(std::move(Err)));
468
469 // Merge IPI.
470 if (auto Err =
471 mergeIdRecords(GlobalIDTable, IndexMap.TPIMap, IndexMap.IPIMap,
472 ExpectedIpi->typeArray(), IpiHashes))
473 fatal("codeview::mergeIdRecords failed: " + toString(std::move(Err)));
474 } else {
475 // Merge TPI first, because the IPI stream will reference type indices.
476 if (auto Err = mergeTypeRecords(TypeTable, IndexMap.TPIMap,
477 ExpectedTpi->typeArray()))
478 fatal("codeview::mergeTypeRecords failed: " + toString(std::move(Err)));
479
480 // Merge IPI.
481 if (auto Err = mergeIdRecords(IDTable, IndexMap.TPIMap, IndexMap.IPIMap,
482 ExpectedIpi->typeArray()))
483 fatal("codeview::mergeIdRecords failed: " + toString(std::move(Err)));
484 }
Reid Kleckner651db912017-07-18 00:21:25 +0000485
486 return IndexMap;
Reid Kleckner44cdb102017-06-19 17:21:45 +0000487}
488
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000489static bool remapTypeIndex(TypeIndex &TI, ArrayRef<TypeIndex> TypeIndexMap) {
490 if (TI.isSimple())
491 return true;
492 if (TI.toArrayIndex() >= TypeIndexMap.size())
493 return false;
494 TI = TypeIndexMap[TI.toArrayIndex()];
495 return true;
496}
497
Reid Kleckner8aa32ff2017-10-24 17:02:40 +0000498static void remapTypesInSymbolRecord(ObjFile *File, SymbolKind SymKind,
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000499 MutableArrayRef<uint8_t> Contents,
Reid Kleckner651db912017-07-18 00:21:25 +0000500 const CVIndexMap &IndexMap,
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000501 ArrayRef<TiReference> TypeRefs) {
502 for (const TiReference &Ref : TypeRefs) {
503 unsigned ByteSize = Ref.Count * sizeof(TypeIndex);
Reid Kleckner03b5baf2017-07-12 18:49:43 +0000504 if (Contents.size() < Ref.Offset + ByteSize)
505 fatal("symbol record too short");
Reid Kleckner651db912017-07-18 00:21:25 +0000506
507 // This can be an item index or a type index. Choose the appropriate map.
508 ArrayRef<TypeIndex> TypeOrItemMap = IndexMap.TPIMap;
Reid Kleckner8aa32ff2017-10-24 17:02:40 +0000509 bool IsItemIndex = Ref.Kind == TiRefKind::IndexRef;
510 if (IsItemIndex && IndexMap.IsTypeServerMap)
Reid Kleckner651db912017-07-18 00:21:25 +0000511 TypeOrItemMap = IndexMap.IPIMap;
512
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000513 MutableArrayRef<TypeIndex> TIs(
514 reinterpret_cast<TypeIndex *>(Contents.data() + Ref.Offset), Ref.Count);
Reid Kleckner03b5baf2017-07-12 18:49:43 +0000515 for (TypeIndex &TI : TIs) {
Reid Kleckner651db912017-07-18 00:21:25 +0000516 if (!remapTypeIndex(TI, TypeOrItemMap)) {
Reid Kleckner8aa32ff2017-10-24 17:02:40 +0000517 log("ignoring symbol record of kind 0x" + utohexstr(SymKind) + " in " +
518 File->getName() + " with bad " + (IsItemIndex ? "item" : "type") +
519 " index 0x" + utohexstr(TI.getIndex()));
Reid Kleckner5d82f682017-10-23 22:44:51 +0000520 TI = TypeIndex(SimpleTypeKind::NotTranslated);
Reid Kleckner03b5baf2017-07-12 18:49:43 +0000521 continue;
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000522 }
Reid Kleckner03b5baf2017-07-12 18:49:43 +0000523 }
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000524 }
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000525}
526
Zachary Turner60478582018-01-05 19:12:40 +0000527static void
528recordStringTableReferenceAtOffset(MutableArrayRef<uint8_t> Contents,
529 uint32_t Offset,
530 std::vector<ulittle32_t *> &StrTableRefs) {
531 Contents =
532 Contents.drop_front(Offset).take_front(sizeof(support::ulittle32_t));
533 ulittle32_t *Index = reinterpret_cast<ulittle32_t *>(Contents.data());
534 StrTableRefs.push_back(Index);
535}
536
537static void
538recordStringTableReferences(SymbolKind Kind, MutableArrayRef<uint8_t> Contents,
539 std::vector<ulittle32_t *> &StrTableRefs) {
540 // For now we only handle S_FILESTATIC, but we may need the same logic for
541 // S_DEFRANGE and S_DEFRANGE_SUBFIELD. However, I cannot seem to generate any
542 // PDBs that contain these types of records, so because of the uncertainty
543 // they are omitted here until we can prove that it's necessary.
544 switch (Kind) {
545 case SymbolKind::S_FILESTATIC:
546 // FileStaticSym::ModFileOffset
547 recordStringTableReferenceAtOffset(Contents, 4, StrTableRefs);
548 break;
549 case SymbolKind::S_DEFRANGE:
550 case SymbolKind::S_DEFRANGE_SUBFIELD:
551 log("Not fixing up string table reference in S_DEFRANGE / "
552 "S_DEFRANGE_SUBFIELD record");
553 break;
Zachary Turner9e52e502018-01-05 19:28:39 +0000554 default:
555 break;
Zachary Turner60478582018-01-05 19:12:40 +0000556 }
557}
558
Zachary Turner59e3ae82017-08-08 18:34:44 +0000559static SymbolKind symbolKind(ArrayRef<uint8_t> RecordData) {
560 const RecordPrefix *Prefix =
561 reinterpret_cast<const RecordPrefix *>(RecordData.data());
562 return static_cast<SymbolKind>(uint16_t(Prefix->RecordKind));
563}
564
565/// MSVC translates S_PROC_ID_END to S_END, and S_[LG]PROC32_ID to S_[LG]PROC32
566static void translateIdSymbols(MutableArrayRef<uint8_t> &RecordData,
Zachary Turner3e3936d2017-11-29 19:35:21 +0000567 TypeCollection &IDTable) {
Zachary Turner59e3ae82017-08-08 18:34:44 +0000568 RecordPrefix *Prefix = reinterpret_cast<RecordPrefix *>(RecordData.data());
569
570 SymbolKind Kind = symbolKind(RecordData);
571
572 if (Kind == SymbolKind::S_PROC_ID_END) {
573 Prefix->RecordKind = SymbolKind::S_END;
574 return;
575 }
576
577 // In an object file, GPROC32_ID has an embedded reference which refers to the
578 // single object file type index namespace. This has already been translated
579 // to the PDB file's ID stream index space, but we need to convert this to a
580 // symbol that refers to the type stream index space. So we remap again from
581 // ID index space to type index space.
582 if (Kind == SymbolKind::S_GPROC32_ID || Kind == SymbolKind::S_LPROC32_ID) {
583 SmallVector<TiReference, 1> Refs;
584 auto Content = RecordData.drop_front(sizeof(RecordPrefix));
585 CVSymbol Sym(Kind, RecordData);
586 discoverTypeIndicesInSymbol(Sym, Refs);
587 assert(Refs.size() == 1);
588 assert(Refs.front().Count == 1);
589
590 TypeIndex *TI =
591 reinterpret_cast<TypeIndex *>(Content.data() + Refs[0].Offset);
592 // `TI` is the index of a FuncIdRecord or MemberFuncIdRecord which lives in
593 // the IPI stream, whose `FunctionType` member refers to the TPI stream.
594 // Note that LF_FUNC_ID and LF_MEMFUNC_ID have the same record layout, and
595 // in both cases we just need the second type index.
596 if (!TI->isSimple() && !TI->isNoneType()) {
Zachary Turner3e3936d2017-11-29 19:35:21 +0000597 CVType FuncIdData = IDTable.getType(*TI);
Zachary Turner59e3ae82017-08-08 18:34:44 +0000598 SmallVector<TypeIndex, 2> Indices;
599 discoverTypeIndices(FuncIdData, Indices);
600 assert(Indices.size() == 2);
601 *TI = Indices[1];
602 }
603
604 Kind = (Kind == SymbolKind::S_GPROC32_ID) ? SymbolKind::S_GPROC32
605 : SymbolKind::S_LPROC32;
606 Prefix->RecordKind = uint16_t(Kind);
607 }
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000608}
609
610/// Copy the symbol record. In a PDB, symbol records must be 4 byte aligned.
611/// The object file may not be aligned.
612static MutableArrayRef<uint8_t> copySymbolForPdb(const CVSymbol &Sym,
613 BumpPtrAllocator &Alloc) {
614 size_t Size = alignTo(Sym.length(), alignOf(CodeViewContainer::Pdb));
615 assert(Size >= 4 && "record too short");
616 assert(Size <= MaxRecordLength && "record too long");
617 void *Mem = Alloc.Allocate(Size, 4);
618
619 // Copy the symbol record and zero out any padding bytes.
620 MutableArrayRef<uint8_t> NewData(reinterpret_cast<uint8_t *>(Mem), Size);
621 memcpy(NewData.data(), Sym.data().data(), Sym.length());
622 memset(NewData.data() + Sym.length(), 0, Size - Sym.length());
623
624 // Update the record prefix length. It should point to the beginning of the
Zachary Turner59e3ae82017-08-08 18:34:44 +0000625 // next record.
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000626 auto *Prefix = reinterpret_cast<RecordPrefix *>(Mem);
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000627 Prefix->RecordLen = Size - 2;
628 return NewData;
629}
630
Reid Kleckner3f851922017-07-06 16:39:32 +0000631/// Return true if this symbol opens a scope. This implies that the symbol has
632/// "parent" and "end" fields, which contain the offset of the S_END or
633/// S_INLINESITE_END record.
634static bool symbolOpensScope(SymbolKind Kind) {
635 switch (Kind) {
636 case SymbolKind::S_GPROC32:
637 case SymbolKind::S_LPROC32:
638 case SymbolKind::S_LPROC32_ID:
639 case SymbolKind::S_GPROC32_ID:
640 case SymbolKind::S_BLOCK32:
641 case SymbolKind::S_SEPCODE:
642 case SymbolKind::S_THUNK32:
643 case SymbolKind::S_INLINESITE:
644 case SymbolKind::S_INLINESITE2:
645 return true;
646 default:
647 break;
648 }
649 return false;
650}
651
652static bool symbolEndsScope(SymbolKind Kind) {
653 switch (Kind) {
654 case SymbolKind::S_END:
655 case SymbolKind::S_PROC_ID_END:
656 case SymbolKind::S_INLINESITE_END:
657 return true;
658 default:
659 break;
660 }
661 return false;
662}
663
664struct ScopeRecord {
665 ulittle32_t PtrParent;
666 ulittle32_t PtrEnd;
667};
668
669struct SymbolScope {
670 ScopeRecord *OpeningRecord;
671 uint32_t ScopeOffset;
672};
673
674static void scopeStackOpen(SmallVectorImpl<SymbolScope> &Stack,
675 uint32_t CurOffset, CVSymbol &Sym) {
676 assert(symbolOpensScope(Sym.kind()));
677 SymbolScope S;
678 S.ScopeOffset = CurOffset;
679 S.OpeningRecord = const_cast<ScopeRecord *>(
680 reinterpret_cast<const ScopeRecord *>(Sym.content().data()));
681 S.OpeningRecord->PtrParent = Stack.empty() ? 0 : Stack.back().ScopeOffset;
682 Stack.push_back(S);
683}
684
685static void scopeStackClose(SmallVectorImpl<SymbolScope> &Stack,
Rui Ueyamae1b48e02017-07-26 23:05:24 +0000686 uint32_t CurOffset, ObjFile *File) {
Reid Kleckner3f851922017-07-06 16:39:32 +0000687 if (Stack.empty()) {
688 warn("symbol scopes are not balanced in " + File->getName());
689 return;
690 }
691 SymbolScope S = Stack.pop_back_val();
692 S.OpeningRecord->PtrEnd = CurOffset;
693}
694
Zachary Turneree9906d2017-08-11 19:00:03 +0000695static bool symbolGoesInModuleStream(const CVSymbol &Sym) {
696 switch (Sym.kind()) {
697 case SymbolKind::S_GDATA32:
698 case SymbolKind::S_CONSTANT:
699 case SymbolKind::S_UDT:
700 // We really should not be seeing S_PROCREF and S_LPROCREF in the first place
701 // since they are synthesized by the linker in response to S_GPROC32 and
702 // S_LPROC32, but if we do see them, don't put them in the module stream I
703 // guess.
704 case SymbolKind::S_PROCREF:
705 case SymbolKind::S_LPROCREF:
706 return false;
707 // S_GDATA32 does not go in the module stream, but S_LDATA32 does.
708 case SymbolKind::S_LDATA32:
709 default:
710 return true;
711 }
712}
713
714static bool symbolGoesInGlobalsStream(const CVSymbol &Sym) {
715 switch (Sym.kind()) {
716 case SymbolKind::S_CONSTANT:
Zachary Turneree9906d2017-08-11 19:00:03 +0000717 case SymbolKind::S_GDATA32:
718 // S_LDATA32 goes in both the module stream and the globals stream.
719 case SymbolKind::S_LDATA32:
720 case SymbolKind::S_GPROC32:
721 case SymbolKind::S_LPROC32:
722 // We really should not be seeing S_PROCREF and S_LPROCREF in the first place
723 // since they are synthesized by the linker in response to S_GPROC32 and
724 // S_LPROC32, but if we do see them, copy them straight through.
725 case SymbolKind::S_PROCREF:
726 case SymbolKind::S_LPROCREF:
727 return true;
Zachary Turner302dc8b2017-08-14 18:44:58 +0000728 // FIXME: For now, we drop all S_UDT symbols (i.e. they don't go in the
729 // globals stream or the modules stream). These have special handling which
730 // needs more investigation before we can get right, but by putting them all
731 // into the globals stream WinDbg fails to display local variables of class
732 // types saying that it cannot find the type Foo *. So as a stopgap just to
733 // keep things working, we drop them.
734 case SymbolKind::S_UDT:
Zachary Turneree9906d2017-08-11 19:00:03 +0000735 default:
736 return false;
737 }
738}
739
740static void addGlobalSymbol(pdb::GSIStreamBuilder &Builder, ObjFile &File,
741 const CVSymbol &Sym) {
742 switch (Sym.kind()) {
743 case SymbolKind::S_CONSTANT:
744 case SymbolKind::S_UDT:
745 case SymbolKind::S_GDATA32:
746 case SymbolKind::S_LDATA32:
747 case SymbolKind::S_PROCREF:
748 case SymbolKind::S_LPROCREF:
749 Builder.addGlobalSymbol(Sym);
750 break;
751 case SymbolKind::S_GPROC32:
752 case SymbolKind::S_LPROC32: {
753 SymbolRecordKind K = SymbolRecordKind::ProcRefSym;
754 if (Sym.kind() == SymbolKind::S_LPROC32)
755 K = SymbolRecordKind::LocalProcRef;
756 ProcRefSym PS(K);
757 PS.Module = static_cast<uint16_t>(File.ModuleDBI->getModuleIndex());
758 // For some reason, MSVC seems to add one to this value.
759 ++PS.Module;
760 PS.Name = getSymbolName(Sym);
761 PS.SumName = 0;
762 PS.SymOffset = File.ModuleDBI->getNextSymbolOffset();
763 Builder.addGlobalSymbol(PS);
764 break;
765 }
766 default:
767 llvm_unreachable("Invalid symbol kind!");
768 }
769}
770
Rui Ueyamae1b48e02017-07-26 23:05:24 +0000771static void mergeSymbolRecords(BumpPtrAllocator &Alloc, ObjFile *File,
Zachary Turneree9906d2017-08-11 19:00:03 +0000772 pdb::GSIStreamBuilder &GsiBuilder,
Reid Kleckner651db912017-07-18 00:21:25 +0000773 const CVIndexMap &IndexMap,
Zachary Turner3e3936d2017-11-29 19:35:21 +0000774 TypeCollection &IDTable,
Zachary Turner60478582018-01-05 19:12:40 +0000775 std::vector<ulittle32_t *> &StringTableRefs,
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000776 BinaryStreamRef SymData) {
777 // FIXME: Improve error recovery by warning and skipping records when
778 // possible.
Zachary Turner1bc2ce62018-01-18 18:35:01 +0000779 ArrayRef<uint8_t> SymsBuffer;
780 cantFail(SymData.readBytes(0, SymData.getLength(), SymsBuffer));
Reid Kleckner3f851922017-07-06 16:39:32 +0000781 SmallVector<SymbolScope, 4> Scopes;
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000782
Zachary Turner1bc2ce62018-01-18 18:35:01 +0000783 auto EC = forEachCodeViewRecord<CVSymbol>(
784 SymsBuffer, [&](const CVSymbol &Sym) -> llvm::Error {
785 // Discover type index references in the record. Skip it if we don't
786 // know where they are.
787 SmallVector<TiReference, 32> TypeRefs;
788 if (!discoverTypeIndicesInSymbol(Sym, TypeRefs)) {
789 log("ignoring unknown symbol record with kind 0x" +
790 utohexstr(Sym.kind()));
791 return Error::success();
792 }
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000793
Zachary Turner1bc2ce62018-01-18 18:35:01 +0000794 // Copy the symbol record so we can mutate it.
795 MutableArrayRef<uint8_t> NewData = copySymbolForPdb(Sym, Alloc);
Zachary Turner59e3ae82017-08-08 18:34:44 +0000796
Zachary Turner1bc2ce62018-01-18 18:35:01 +0000797 // Re-map all the type index references.
798 MutableArrayRef<uint8_t> Contents =
799 NewData.drop_front(sizeof(RecordPrefix));
800 remapTypesInSymbolRecord(File, Sym.kind(), Contents, IndexMap,
801 TypeRefs);
Zachary Turner59e3ae82017-08-08 18:34:44 +0000802
Zachary Turner1bc2ce62018-01-18 18:35:01 +0000803 // An object file may have S_xxx_ID symbols, but these get converted to
804 // "real" symbols in a PDB.
805 translateIdSymbols(NewData, IDTable);
Zachary Turner60478582018-01-05 19:12:40 +0000806
Zachary Turner1bc2ce62018-01-18 18:35:01 +0000807 // If this record refers to an offset in the object file's string table,
808 // add that item to the global PDB string table and re-write the index.
809 recordStringTableReferences(Sym.kind(), Contents, StringTableRefs);
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000810
Zachary Turner1bc2ce62018-01-18 18:35:01 +0000811 SymbolKind NewKind = symbolKind(NewData);
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000812
Zachary Turner1bc2ce62018-01-18 18:35:01 +0000813 // Fill in "Parent" and "End" fields by maintaining a stack of scopes.
814 CVSymbol NewSym(NewKind, NewData);
815 if (symbolOpensScope(NewKind))
816 scopeStackOpen(Scopes, File->ModuleDBI->getNextSymbolOffset(),
817 NewSym);
818 else if (symbolEndsScope(NewKind))
819 scopeStackClose(Scopes, File->ModuleDBI->getNextSymbolOffset(), File);
Zachary Turneree9906d2017-08-11 19:00:03 +0000820
Zachary Turner1bc2ce62018-01-18 18:35:01 +0000821 // Add the symbol to the globals stream if necessary. Do this before
822 // adding the symbol to the module since we may need to get the next
823 // symbol offset, and writing to the module's symbol stream will update
824 // that offset.
825 if (symbolGoesInGlobalsStream(NewSym))
826 addGlobalSymbol(GsiBuilder, *File, NewSym);
827
828 // Add the symbol to the module.
829 if (symbolGoesInModuleStream(NewSym))
830 File->ModuleDBI->addSymbol(NewSym);
831 return Error::success();
832 });
833 cantFail(std::move(EC));
Reid Klecknerd0e6e242017-06-21 17:25:56 +0000834}
835
Zachary Turnera1f85f82018-09-12 21:02:01 +0000836// Allocate memory for a .debug$S / .debug$F section and relocate it.
Reid Kleckner44cdb102017-06-19 17:21:45 +0000837static ArrayRef<uint8_t> relocateDebugChunk(BumpPtrAllocator &Alloc,
Zachary Turnera1f85f82018-09-12 21:02:01 +0000838 SectionChunk &DebugChunk) {
839 uint8_t *Buffer = Alloc.Allocate<uint8_t>(DebugChunk.getSize());
840 assert(DebugChunk.OutputSectionOff == 0 &&
Reid Kleckner44cdb102017-06-19 17:21:45 +0000841 "debug sections should not be in output sections");
Zachary Turnera1f85f82018-09-12 21:02:01 +0000842 DebugChunk.writeTo(Buffer);
843 return makeArrayRef(Buffer, DebugChunk.getSize());
Reid Kleckner44cdb102017-06-19 17:21:45 +0000844}
845
Zachary Turner194be872018-04-20 18:00:46 +0000846static pdb::SectionContrib createSectionContrib(const Chunk *C, uint32_t Modi) {
847 OutputSection *OS = C->getOutputSection();
848 pdb::SectionContrib SC;
849 memset(&SC, 0, sizeof(SC));
850 SC.ISect = OS->SectionIndex;
851 SC.Off = C->getRVA() - OS->getRVA();
852 SC.Size = C->getSize();
853 if (auto *SecChunk = dyn_cast<SectionChunk>(C)) {
854 SC.Characteristics = SecChunk->Header->Characteristics;
855 SC.Imod = SecChunk->File->ModuleDBI->getModuleIndex();
856 ArrayRef<uint8_t> Contents = SecChunk->getContents();
857 JamCRC CRC(0);
858 ArrayRef<char> CharContents = makeArrayRef(
859 reinterpret_cast<const char *>(Contents.data()), Contents.size());
860 CRC.update(CharContents);
861 SC.DataCrc = CRC.getCRC();
862 } else {
863 SC.Characteristics = OS->Header.Characteristics;
864 // FIXME: When we start creating DBI for import libraries, use those here.
865 SC.Imod = Modi;
866 }
867 SC.RelocCrc = 0; // FIXME
868
869 return SC;
870}
871
Zachary Turner42e7cc1b2018-09-11 22:35:01 +0000872static uint32_t
873translateStringTableIndex(uint32_t ObjIndex,
874 const DebugStringTableSubsectionRef &ObjStrTable,
875 DebugStringTableSubsection &PdbStrTable) {
876 auto ExpectedString = ObjStrTable.getString(ObjIndex);
877 if (!ExpectedString) {
878 warn("Invalid string table reference");
879 consumeError(ExpectedString.takeError());
880 return 0;
881 }
882
883 return PdbStrTable.insert(*ExpectedString);
884}
885
Zachary Turnera1f85f82018-09-12 21:02:01 +0000886void DebugSHandler::handleDebugS(lld::coff::SectionChunk &DebugS) {
887 DebugSubsectionArray Subsections;
888
889 ArrayRef<uint8_t> RelocatedDebugContents = consumeDebugMagic(
890 relocateDebugChunk(Linker.Alloc, DebugS), DebugS.getSectionName());
891
892 BinaryStreamReader Reader(RelocatedDebugContents, support::little);
893 ExitOnErr(Reader.readArray(Subsections, RelocatedDebugContents.size()));
894
895 for (const DebugSubsectionRecord &SS : Subsections) {
896 switch (SS.kind()) {
897 case DebugSubsectionKind::StringTable: {
898 assert(!CVStrTab.valid() &&
899 "Encountered multiple string table subsections!");
900 ExitOnErr(CVStrTab.initialize(SS.getRecordData()));
901 break;
902 }
903 case DebugSubsectionKind::FileChecksums:
904 assert(!Checksums.valid() &&
905 "Encountered multiple checksum subsections!");
906 ExitOnErr(Checksums.initialize(SS.getRecordData()));
907 break;
908 case DebugSubsectionKind::Lines:
909 // We can add the relocated line table directly to the PDB without
910 // modification because the file checksum offsets will stay the same.
911 File.ModuleDBI->addDebugSubsection(SS);
912 break;
913 case DebugSubsectionKind::FrameData: {
914 // We need to re-write string table indices here, so save off all
915 // frame data subsections until we've processed the entire list of
916 // subsections so that we can be sure we have the string table.
917 DebugFrameDataSubsectionRef FDS;
918 ExitOnErr(FDS.initialize(SS.getRecordData()));
919 NewFpoFrames.push_back(std::move(FDS));
920 break;
921 }
922 case DebugSubsectionKind::Symbols:
923 if (Config->DebugGHashes) {
924 mergeSymbolRecords(Linker.Alloc, &File, Linker.Builder.getGsiBuilder(),
925 IndexMap, Linker.GlobalIDTable,
926 StringTableReferences, SS.getRecordData());
927 } else {
928 mergeSymbolRecords(Linker.Alloc, &File, Linker.Builder.getGsiBuilder(),
929 IndexMap, Linker.IDTable, StringTableReferences,
930 SS.getRecordData());
931 }
932 break;
933 default:
934 // FIXME: Process the rest of the subsections.
935 break;
936 }
937 }
938}
939
940void DebugSHandler::finish() {
941 pdb::DbiStreamBuilder &DbiBuilder = Linker.Builder.getDbiBuilder();
942
943 // We should have seen all debug subsections across the entire object file now
944 // which means that if a StringTable subsection and Checksums subsection were
945 // present, now is the time to handle them.
946 if (!CVStrTab.valid()) {
947 if (Checksums.valid())
948 fatal(".debug$S sections with a checksums subsection must also contain a "
949 "string table subsection");
950
951 if (!StringTableReferences.empty())
952 warn("No StringTable subsection was encountered, but there are string "
953 "table references");
954 return;
955 }
956
957 // Rewrite string table indices in the Fpo Data and symbol records to refer to
958 // the global PDB string table instead of the object file string table.
959 for (DebugFrameDataSubsectionRef &FDS : NewFpoFrames) {
960 const uint32_t *Reloc = FDS.getRelocPtr();
961 for (codeview::FrameData FD : FDS) {
962 FD.RvaStart += *Reloc;
963 FD.FrameFunc =
964 translateStringTableIndex(FD.FrameFunc, CVStrTab, Linker.PDBStrTab);
965 DbiBuilder.addNewFpoData(FD);
966 }
967 }
968
969 for (ulittle32_t *Ref : StringTableReferences)
970 *Ref = translateStringTableIndex(*Ref, CVStrTab, Linker.PDBStrTab);
971
972 // Make a new file checksum table that refers to offsets in the PDB-wide
973 // string table. Generally the string table subsection appears after the
974 // checksum table, so we have to do this after looping over all the
975 // subsections.
976 auto NewChecksums = make_unique<DebugChecksumsSubsection>(Linker.PDBStrTab);
977 for (FileChecksumEntry &FC : Checksums) {
978 SmallString<128> FileName =
979 ExitOnErr(CVStrTab.getString(FC.FileNameOffset));
980 if (!sys::path::is_absolute(FileName) && !Config->PDBSourcePath.empty()) {
981 SmallString<128> AbsoluteFileName = Config->PDBSourcePath;
982 sys::path::append(AbsoluteFileName, FileName);
983 sys::path::native(AbsoluteFileName);
984 sys::path::remove_dots(AbsoluteFileName, /*remove_dot_dots=*/true);
985 FileName = std::move(AbsoluteFileName);
986 }
987 ExitOnErr(Linker.Builder.getDbiBuilder().addModuleSourceFile(
988 *File.ModuleDBI, FileName));
989 NewChecksums->addChecksum(FileName, FC.Kind, FC.Checksum);
990 }
991 File.ModuleDBI->addDebugSubsection(std::move(NewChecksums));
992}
993
Rui Ueyamae1b48e02017-07-26 23:05:24 +0000994void PDBLinker::addObjFile(ObjFile *File) {
Reid Kleckner0faf6d72017-07-14 00:14:58 +0000995 // Add a module descriptor for every object file. We need to put an absolute
996 // path to the object into the PDB. If this is a plain object, we make its
997 // path absolute. If it's an object in an archive, we make the archive path
998 // absolute.
999 bool InArchive = !File->ParentName.empty();
1000 SmallString<128> Path = InArchive ? File->ParentName : File->getName();
1001 sys::fs::make_absolute(Path);
1002 sys::path::native(Path, sys::path::Style::windows);
1003 StringRef Name = InArchive ? File->getName() : StringRef(Path);
Zachary Turner2897e032017-05-25 21:16:03 +00001004
Zachary Turner42e7cc1b2018-09-11 22:35:01 +00001005 pdb::DbiStreamBuilder &DbiBuilder = Builder.getDbiBuilder();
1006 File->ModuleDBI = &ExitOnErr(DbiBuilder.addModuleInfo(Name));
Reid Kleckner0faf6d72017-07-14 00:14:58 +00001007 File->ModuleDBI->setObjFileName(Path);
Reid Kleckner44cdb102017-06-19 17:21:45 +00001008
Zachary Turner194be872018-04-20 18:00:46 +00001009 auto Chunks = File->getChunks();
Zachary Turner194be872018-04-20 18:00:46 +00001010 uint32_t Modi = File->ModuleDBI->getModuleIndex();
1011 for (Chunk *C : Chunks) {
1012 auto *SecChunk = dyn_cast<SectionChunk>(C);
Martin Storsjo802fcb42018-08-31 07:45:20 +00001013 if (!SecChunk || !SecChunk->Live)
Zachary Turner194be872018-04-20 18:00:46 +00001014 continue;
1015 pdb::SectionContrib SC = createSectionContrib(SecChunk, Modi);
1016 File->ModuleDBI->setFirstSectionContrib(SC);
1017 break;
1018 }
1019
Reid Kleckner0faf6d72017-07-14 00:14:58 +00001020 // Before we can process symbol substreams from .debug$S, we need to process
1021 // type information, file checksums, and the string table. Add type info to
1022 // the PDB first, so that we can get the map from object file type and item
1023 // indices to PDB type and item indices.
Reid Kleckner651db912017-07-18 00:21:25 +00001024 CVIndexMap ObjectIndexMap;
Colden Cullenb9b6ed92018-01-31 17:48:04 +00001025 auto IndexMapResult = mergeDebugT(File, ObjectIndexMap);
1026
1027 // If the .debug$T sections fail to merge, assume there is no debug info.
1028 if (!IndexMapResult) {
Alexandre Ganead93b07f2018-09-10 13:51:21 +00001029 auto FileName = sys::path::filename(Path);
1030 warn("Cannot use debug info for '" + FileName + "'\n" +
1031 ">>> failed to load reference " +
1032 StringRef(toString(IndexMapResult.takeError())));
Colden Cullenb9b6ed92018-01-31 17:48:04 +00001033 return;
1034 }
1035
Zachary Turner727f1532018-01-17 19:16:26 +00001036 ScopedTimer T(SymbolMergingTimer);
1037
Zachary Turnera1f85f82018-09-12 21:02:01 +00001038 DebugSHandler DSH(*this, *File, *IndexMapResult);
1039 // Now do all live .debug$S and .debug$F sections.
Reid Kleckner0faf6d72017-07-14 00:14:58 +00001040 for (SectionChunk *DebugChunk : File->getDebugChunks()) {
Zachary Turnera1f85f82018-09-12 21:02:01 +00001041 if (!DebugChunk->Live || DebugChunk->getSize() == 0)
Reid Kleckner0faf6d72017-07-14 00:14:58 +00001042 continue;
Reid Kleckner8cbdd0c2017-06-13 15:49:13 +00001043
Zachary Turnera1f85f82018-09-12 21:02:01 +00001044 if (DebugChunk->getSectionName() == ".debug$S") {
1045 DSH.handleDebugS(*DebugChunk);
Reid Kleckner0faf6d72017-07-14 00:14:58 +00001046 continue;
Zachary Turnera1f85f82018-09-12 21:02:01 +00001047 }
Reid Kleckner8cbdd0c2017-06-13 15:49:13 +00001048
Zachary Turnera1f85f82018-09-12 21:02:01 +00001049 if (DebugChunk->getSectionName() == ".debug$F") {
1050 ArrayRef<uint8_t> RelocatedDebugContents =
1051 relocateDebugChunk(Alloc, *DebugChunk);
Rui Ueyama52896622017-01-12 03:09:25 +00001052
Zachary Turnera1f85f82018-09-12 21:02:01 +00001053 FixedStreamArray<object::FpoData> FpoRecords;
1054 BinaryStreamReader Reader(RelocatedDebugContents, support::little);
1055 uint32_t Count = RelocatedDebugContents.size() / sizeof(object::FpoData);
1056 ExitOnErr(Reader.readArray(FpoRecords, Count));
1057
1058 // These are already relocated and don't refer to the string table, so we
1059 // can just copy it.
1060 for (const object::FpoData &FD : FpoRecords)
1061 DbiBuilder.addOldFpoData(FD);
1062 continue;
Reid Kleckner44cdb102017-06-19 17:21:45 +00001063 }
Reid Kleckner0faf6d72017-07-14 00:14:58 +00001064 }
Zachary Turner60478582018-01-05 19:12:40 +00001065
Zachary Turnera1f85f82018-09-12 21:02:01 +00001066 // Do any post-processing now that all .debug$S sections have been processed.
1067 DSH.finish();
Reid Kleckner0faf6d72017-07-14 00:14:58 +00001068}
1069
Reid Klecknereacdf042017-07-27 18:25:59 +00001070static PublicSym32 createPublic(Defined *Def) {
1071 PublicSym32 Pub(SymbolKind::S_PUB32);
1072 Pub.Name = Def->getName();
1073 if (auto *D = dyn_cast<DefinedCOFF>(Def)) {
1074 if (D->getCOFFSymbol().isFunctionDefinition())
1075 Pub.Flags = PublicSymFlags::Function;
1076 } else if (isa<DefinedImportThunk>(Def)) {
1077 Pub.Flags = PublicSymFlags::Function;
1078 }
1079
1080 OutputSection *OS = Def->getChunk()->getOutputSection();
1081 assert(OS && "all publics should be in final image");
1082 Pub.Offset = Def->getRVA() - OS->getRVA();
1083 Pub.Segment = OS->SectionIndex;
1084 return Pub;
1085}
1086
Reid Kleckner0faf6d72017-07-14 00:14:58 +00001087// Add all object files to the PDB. Merge .debug$T sections into IpiData and
1088// TpiData.
1089void PDBLinker::addObjectsToPDB() {
Zachary Turner727f1532018-01-17 19:16:26 +00001090 ScopedTimer T1(AddObjectsTimer);
Rui Ueyamaacd632d2017-07-27 00:45:26 +00001091 for (ObjFile *File : ObjFile::Instances)
Rui Ueyamae1b48e02017-07-26 23:05:24 +00001092 addObjFile(File);
Reid Kleckner0faf6d72017-07-14 00:14:58 +00001093
1094 Builder.getStringTableBuilder().setStrings(PDBStrTab);
Zachary Turner727f1532018-01-17 19:16:26 +00001095 T1.stop();
Reid Kleckner44cdb102017-06-19 17:21:45 +00001096
Zachary Turner0d07a8e2017-12-14 18:07:04 +00001097 // Construct TPI and IPI stream contents.
Zachary Turner727f1532018-01-17 19:16:26 +00001098 ScopedTimer T2(TpiStreamLayoutTimer);
Zachary Turner0d07a8e2017-12-14 18:07:04 +00001099 if (Config->DebugGHashes) {
1100 addTypeInfo(Builder.getTpiBuilder(), GlobalTypeTable);
1101 addTypeInfo(Builder.getIpiBuilder(), GlobalIDTable);
1102 } else {
1103 addTypeInfo(Builder.getTpiBuilder(), TypeTable);
1104 addTypeInfo(Builder.getIpiBuilder(), IDTable);
1105 }
Zachary Turner727f1532018-01-17 19:16:26 +00001106 T2.stop();
Reid Kleckner0faf6d72017-07-14 00:14:58 +00001107
Zachary Turner727f1532018-01-17 19:16:26 +00001108 ScopedTimer T3(GlobalsLayoutTimer);
Zachary Turneree9906d2017-08-11 19:00:03 +00001109 // Compute the public and global symbols.
1110 auto &GsiBuilder = Builder.getGsiBuilder();
Reid Klecknereacdf042017-07-27 18:25:59 +00001111 std::vector<PublicSym32> Publics;
Rui Ueyamaf52496e2017-11-03 21:21:47 +00001112 Symtab->forEachSymbol([&Publics](Symbol *S) {
Reid Klecknereacdf042017-07-27 18:25:59 +00001113 // Only emit defined, live symbols that have a chunk.
Rui Ueyama616cd992017-10-31 16:10:24 +00001114 auto *Def = dyn_cast<Defined>(S);
Reid Klecknereacdf042017-07-27 18:25:59 +00001115 if (Def && Def->isLive() && Def->getChunk())
1116 Publics.push_back(createPublic(Def));
1117 });
Reid Kleckner0faf6d72017-07-14 00:14:58 +00001118
Reid Klecknereacdf042017-07-27 18:25:59 +00001119 if (!Publics.empty()) {
1120 // Sort the public symbols and add them to the stream.
1121 std::sort(Publics.begin(), Publics.end(),
1122 [](const PublicSym32 &L, const PublicSym32 &R) {
1123 return L.Name < R.Name;
1124 });
Reid Klecknereacdf042017-07-27 18:25:59 +00001125 for (const PublicSym32 &Pub : Publics)
Zachary Turner946204c2017-08-09 04:23:25 +00001126 GsiBuilder.addPublicSymbol(Pub);
Reid Klecknereacdf042017-07-27 18:25:59 +00001127 }
Rui Ueyama52896622017-01-12 03:09:25 +00001128}
1129
Zachary Turnerf2282762018-03-23 19:57:25 +00001130void PDBLinker::addNatvisFiles() {
1131 for (StringRef File : Config->NatvisFiles) {
1132 ErrorOr<std::unique_ptr<MemoryBuffer>> DataOrErr =
1133 MemoryBuffer::getFile(File);
1134 if (!DataOrErr) {
1135 warn("Cannot open input file: " + File);
1136 continue;
1137 }
1138 Builder.addInjectedSource(File, std::move(*DataOrErr));
1139 }
1140}
1141
Zachary Turnere3fe6692018-04-16 18:17:13 +00001142static codeview::CPUType toCodeViewMachine(COFF::MachineTypes Machine) {
1143 switch (Machine) {
1144 case COFF::IMAGE_FILE_MACHINE_AMD64:
1145 return codeview::CPUType::X64;
1146 case COFF::IMAGE_FILE_MACHINE_ARM:
1147 return codeview::CPUType::ARM7;
1148 case COFF::IMAGE_FILE_MACHINE_ARM64:
1149 return codeview::CPUType::ARM64;
1150 case COFF::IMAGE_FILE_MACHINE_ARMNT:
1151 return codeview::CPUType::ARMNT;
1152 case COFF::IMAGE_FILE_MACHINE_I386:
1153 return codeview::CPUType::Intel80386;
1154 default:
1155 llvm_unreachable("Unsupported CPU Type");
1156 }
1157}
1158
Zachary Turner28e31ee2017-08-11 20:46:28 +00001159static void addCommonLinkerModuleSymbols(StringRef Path,
1160 pdb::DbiModuleDescriptorBuilder &Mod,
1161 BumpPtrAllocator &Allocator) {
Zachary Turner28e31ee2017-08-11 20:46:28 +00001162 ObjNameSym ONS(SymbolRecordKind::ObjNameSym);
1163 Compile3Sym CS(SymbolRecordKind::Compile3Sym);
1164 EnvBlockSym EBS(SymbolRecordKind::EnvBlockSym);
Zachary Turner6708e0b2017-07-10 21:01:37 +00001165
1166 ONS.Name = "* Linker *";
1167 ONS.Signature = 0;
1168
Zachary Turnere3fe6692018-04-16 18:17:13 +00001169 CS.Machine = toCodeViewMachine(Config->Machine);
Zachary Turnerb57884e2017-08-11 21:14:01 +00001170 // Interestingly, if we set the string to 0.0.0.0, then when trying to view
1171 // local variables WinDbg emits an error that private symbols are not present.
1172 // By setting this to a valid MSVC linker version string, local variables are
1173 // displayed properly. As such, even though it is not representative of
Zachary Turnerf6822c82017-08-11 20:46:47 +00001174 // LLVM's version information, we need this for compatibility.
Zachary Turner6708e0b2017-07-10 21:01:37 +00001175 CS.Flags = CompileSym3Flags::None;
Zachary Turnerf6822c82017-08-11 20:46:47 +00001176 CS.VersionBackendBuild = 25019;
1177 CS.VersionBackendMajor = 14;
1178 CS.VersionBackendMinor = 10;
Zachary Turner6708e0b2017-07-10 21:01:37 +00001179 CS.VersionBackendQFE = 0;
Zachary Turnerf6822c82017-08-11 20:46:47 +00001180
1181 // MSVC also sets the frontend to 0.0.0.0 since this is specifically for the
1182 // linker module (which is by definition a backend), so we don't need to do
1183 // anything here. Also, it seems we can use "LLVM Linker" for the linker name
1184 // without any problems. Only the backend version has to be hardcoded to a
1185 // magic number.
Zachary Turner6708e0b2017-07-10 21:01:37 +00001186 CS.VersionFrontendBuild = 0;
1187 CS.VersionFrontendMajor = 0;
1188 CS.VersionFrontendMinor = 0;
1189 CS.VersionFrontendQFE = 0;
1190 CS.Version = "LLVM Linker";
1191 CS.setLanguage(SourceLanguage::Link);
1192
1193 ArrayRef<StringRef> Args = makeArrayRef(Config->Argv).drop_front();
1194 std::string ArgStr = llvm::join(Args, " ");
1195 EBS.Fields.push_back("cwd");
1196 SmallString<64> cwd;
Reid Kleckner0faf6d72017-07-14 00:14:58 +00001197 sys::fs::current_path(cwd);
Zachary Turner6708e0b2017-07-10 21:01:37 +00001198 EBS.Fields.push_back(cwd);
1199 EBS.Fields.push_back("exe");
Zachary Turnerf6822c82017-08-11 20:46:47 +00001200 SmallString<64> exe = Config->Argv[0];
1201 llvm::sys::fs::make_absolute(exe);
1202 EBS.Fields.push_back(exe);
Zachary Turner6708e0b2017-07-10 21:01:37 +00001203 EBS.Fields.push_back("pdb");
1204 EBS.Fields.push_back(Path);
1205 EBS.Fields.push_back("cmd");
1206 EBS.Fields.push_back(ArgStr);
1207 Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(
1208 ONS, Allocator, CodeViewContainer::Pdb));
1209 Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(
1210 CS, Allocator, CodeViewContainer::Pdb));
1211 Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(
1212 EBS, Allocator, CodeViewContainer::Pdb));
1213}
1214
Zachary Turner28e31ee2017-08-11 20:46:28 +00001215static void addLinkerModuleSectionSymbol(pdb::DbiModuleDescriptorBuilder &Mod,
1216 OutputSection &OS,
1217 BumpPtrAllocator &Allocator) {
1218 SectionSym Sym(SymbolRecordKind::SectionSym);
Zachary Turnerf6822c82017-08-11 20:46:47 +00001219 Sym.Alignment = 12; // 2^12 = 4KB
Peter Collingbournebe084ec2018-04-19 21:48:37 +00001220 Sym.Characteristics = OS.Header.Characteristics;
Zachary Turner28e31ee2017-08-11 20:46:28 +00001221 Sym.Length = OS.getVirtualSize();
Peter Collingbourne435b09912018-03-15 21:13:46 +00001222 Sym.Name = OS.Name;
Zachary Turner28e31ee2017-08-11 20:46:28 +00001223 Sym.Rva = OS.getRVA();
1224 Sym.SectionNumber = OS.SectionIndex;
1225 Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(
1226 Sym, Allocator, CodeViewContainer::Pdb));
1227}
1228
Rui Ueyama09e0b5f2016-11-12 00:00:51 +00001229// Creates a PDB file.
Reid Kleckner175af4b2017-08-03 21:15:09 +00001230void coff::createPDB(SymbolTable *Symtab,
1231 ArrayRef<OutputSection *> OutputSections,
1232 ArrayRef<uint8_t> SectionTable,
Nico Weber0bd2d302018-09-15 18:37:22 +00001233 llvm::codeview::DebugInfo *BuildId) {
Zachary Turner727f1532018-01-17 19:16:26 +00001234 ScopedTimer T1(TotalPdbLinkTimer);
Reid Kleckner0faf6d72017-07-14 00:14:58 +00001235 PDBLinker PDB(Symtab);
Zachary Turnerf2282762018-03-23 19:57:25 +00001236
Zachary Turner024323c2017-08-15 21:31:41 +00001237 PDB.initialize(BuildId);
Reid Kleckner0faf6d72017-07-14 00:14:58 +00001238 PDB.addObjectsToPDB();
Reid Kleckner175af4b2017-08-03 21:15:09 +00001239 PDB.addSections(OutputSections, SectionTable);
Zachary Turnerf2282762018-03-23 19:57:25 +00001240 PDB.addNatvisFiles();
Zachary Turner727f1532018-01-17 19:16:26 +00001241
1242 ScopedTimer T2(DiskCommitTimer);
Nico Weber0bd2d302018-09-15 18:37:22 +00001243 codeview::GUID Guid;
1244 PDB.commit(&Guid);
1245 memcpy(&BuildId->PDB70.Signature, &Guid, 16);
Reid Kleckner0faf6d72017-07-14 00:14:58 +00001246}
1247
Nico Weber0bd2d302018-09-15 18:37:22 +00001248void PDBLinker::initialize(llvm::codeview::DebugInfo *BuildId) {
Rui Ueyama12979542016-09-30 20:53:45 +00001249 ExitOnErr(Builder.initialize(4096)); // 4096 is blocksize
Rui Ueyama7f382992016-09-15 18:55:18 +00001250
Nico Weber0bd2d302018-09-15 18:37:22 +00001251 BuildId->Signature.CVSignature = OMF::Signature::PDB70;
1252 // Signature is set to a hash of the PDB contents when the PDB is done.
1253 memset(BuildId->PDB70.Signature, 0, 16);
1254 BuildId->PDB70.Age = 1;
1255
Rui Ueyama8d3fb5d2016-10-05 22:08:58 +00001256 // Create streams in MSF for predefined streams, namely
1257 // PDB, TPI, DBI and IPI.
1258 for (int I = 0; I < (int)pdb::kSpecialStreamCount; ++I)
1259 ExitOnErr(Builder.getMsfBuilder().addStream(0));
Rui Ueyama7f382992016-09-15 18:55:18 +00001260
Rui Ueyamabb542b32016-09-16 22:51:17 +00001261 // Add an Info stream.
1262 auto &InfoBuilder = Builder.getInfoBuilder();
Rui Ueyamabb542b32016-09-16 22:51:17 +00001263 InfoBuilder.setVersion(pdb::PdbRaw_ImplVer::PdbImplVC70);
Nico Weber0bd2d302018-09-15 18:37:22 +00001264 InfoBuilder.setHashPDBContentsToGUID(true);
Rui Ueyama7f382992016-09-15 18:55:18 +00001265
Zachary Turner6c4bfba2017-07-07 05:04:36 +00001266 // Add an empty DBI stream.
Reid Kleckner8cbdd0c2017-06-13 15:49:13 +00001267 pdb::DbiStreamBuilder &DbiBuilder = Builder.getDbiBuilder();
Nico Weber0bd2d302018-09-15 18:37:22 +00001268 DbiBuilder.setAge(BuildId->PDB70.Age);
Zachary Turnerc1e93e52017-07-07 18:45:56 +00001269 DbiBuilder.setVersionHeader(pdb::PdbDbiV70);
Zachary Turnerd8d97de2018-04-16 20:42:06 +00001270 DbiBuilder.setMachineType(Config->Machine);
Zachary Turnere3fe6692018-04-16 18:17:13 +00001271 // Technically we are not link.exe 14.11, but there are known cases where
1272 // debugging tools on Windows expect Microsoft-specific version numbers or
1273 // they fail to work at all. Since we know we produce PDBs that are
1274 // compatible with LINK 14.11, we set that version number here.
1275 DbiBuilder.setBuildNumber(14, 11);
Reid Kleckner0faf6d72017-07-14 00:14:58 +00001276}
Rui Ueyama1343fac2016-10-06 22:52:01 +00001277
Reid Kleckner175af4b2017-08-03 21:15:09 +00001278void PDBLinker::addSections(ArrayRef<OutputSection *> OutputSections,
1279 ArrayRef<uint8_t> SectionTable) {
Reid Kleckner0faf6d72017-07-14 00:14:58 +00001280 // It's not entirely clear what this is, but the * Linker * module uses it.
Reid Kleckner175af4b2017-08-03 21:15:09 +00001281 pdb::DbiStreamBuilder &DbiBuilder = Builder.getDbiBuilder();
Reid Kleckner0faf6d72017-07-14 00:14:58 +00001282 NativePath = Config->PDBPath;
1283 sys::fs::make_absolute(NativePath);
1284 sys::path::native(NativePath, sys::path::Style::windows);
1285 uint32_t PdbFilePathNI = DbiBuilder.addECName(NativePath);
Zachary Turner6c4bfba2017-07-07 05:04:36 +00001286 auto &LinkerModule = ExitOnErr(DbiBuilder.addModuleInfo("* Linker *"));
1287 LinkerModule.setPdbFilePathNI(PdbFilePathNI);
Zachary Turner28e31ee2017-08-11 20:46:28 +00001288 addCommonLinkerModuleSymbols(NativePath, LinkerModule, Alloc);
Rui Ueyamac91f7162016-11-16 01:10:46 +00001289
Reid Kleckner175af4b2017-08-03 21:15:09 +00001290 // Add section contributions. They must be ordered by ascending RVA.
Zachary Turner28e31ee2017-08-11 20:46:28 +00001291 for (OutputSection *OS : OutputSections) {
1292 addLinkerModuleSectionSymbol(LinkerModule, *OS, Alloc);
Zachary Turner194be872018-04-20 18:00:46 +00001293 for (Chunk *C : OS->getChunks()) {
1294 pdb::SectionContrib SC =
1295 createSectionContrib(C, LinkerModule.getModuleIndex());
1296 Builder.getDbiBuilder().addSectionContrib(SC);
1297 }
Zachary Turner28e31ee2017-08-11 20:46:28 +00001298 }
Reid Kleckner175af4b2017-08-03 21:15:09 +00001299
1300 // Add Section Map stream.
1301 ArrayRef<object::coff_section> Sections = {
1302 (const object::coff_section *)SectionTable.data(),
1303 SectionTable.size() / sizeof(object::coff_section)};
1304 SectionMap = pdb::DbiStreamBuilder::createSectionMap(Sections);
1305 DbiBuilder.setSectionMap(SectionMap);
1306
Rui Ueyama9f66f822016-10-11 19:45:07 +00001307 // Add COFF section header stream.
1308 ExitOnErr(
1309 DbiBuilder.addDbgStream(pdb::DbgHeaderType::SectionHdr, SectionTable));
Reid Kleckner0faf6d72017-07-14 00:14:58 +00001310}
Rui Ueyama9f66f822016-10-11 19:45:07 +00001311
Nico Weber0bd2d302018-09-15 18:37:22 +00001312void PDBLinker::commit(codeview::GUID *Guid) {
Rui Ueyama3e9d6bb2016-09-26 23:53:55 +00001313 // Write to a file.
Nico Weber0bd2d302018-09-15 18:37:22 +00001314 ExitOnErr(Builder.commit(Config->PDBPath, Guid));
Rui Ueyamae7378242015-12-04 23:11:05 +00001315}
Peter Collingbourne31088022018-04-17 23:32:33 +00001316
1317static Expected<StringRef>
1318getFileName(const DebugStringTableSubsectionRef &Strings,
1319 const DebugChecksumsSubsectionRef &Checksums, uint32_t FileID) {
1320 auto Iter = Checksums.getArray().at(FileID);
1321 if (Iter == Checksums.getArray().end())
1322 return make_error<CodeViewError>(cv_error_code::no_records);
1323 uint32_t Offset = Iter->FileNameOffset;
1324 return Strings.getString(Offset);
1325}
1326
1327static uint32_t getSecrelReloc() {
1328 switch (Config->Machine) {
1329 case AMD64:
1330 return COFF::IMAGE_REL_AMD64_SECREL;
1331 case I386:
1332 return COFF::IMAGE_REL_I386_SECREL;
1333 case ARMNT:
1334 return COFF::IMAGE_REL_ARM_SECREL;
1335 case ARM64:
1336 return COFF::IMAGE_REL_ARM64_SECREL;
1337 default:
1338 llvm_unreachable("unknown machine type");
1339 }
1340}
1341
1342// Try to find a line table for the given offset Addr into the given chunk C.
1343// If a line table was found, the line table, the string and checksum tables
1344// that are used to interpret the line table, and the offset of Addr in the line
1345// table are stored in the output arguments. Returns whether a line table was
1346// found.
1347static bool findLineTable(const SectionChunk *C, uint32_t Addr,
1348 DebugStringTableSubsectionRef &CVStrTab,
1349 DebugChecksumsSubsectionRef &Checksums,
1350 DebugLinesSubsectionRef &Lines,
1351 uint32_t &OffsetInLinetable) {
1352 ExitOnError ExitOnErr;
1353 uint32_t SecrelReloc = getSecrelReloc();
1354
1355 for (SectionChunk *DbgC : C->File->getDebugChunks()) {
1356 if (DbgC->getSectionName() != ".debug$S")
1357 continue;
1358
1359 // Build a mapping of SECREL relocations in DbgC that refer to C.
1360 DenseMap<uint32_t, uint32_t> Secrels;
1361 for (const coff_relocation &R : DbgC->Relocs) {
1362 if (R.Type != SecrelReloc)
1363 continue;
1364
1365 if (auto *S = dyn_cast_or_null<DefinedRegular>(
1366 C->File->getSymbols()[R.SymbolTableIndex]))
1367 if (S->getChunk() == C)
1368 Secrels[R.VirtualAddress] = S->getValue();
1369 }
1370
1371 ArrayRef<uint8_t> Contents =
1372 consumeDebugMagic(DbgC->getContents(), ".debug$S");
1373 DebugSubsectionArray Subsections;
1374 BinaryStreamReader Reader(Contents, support::little);
1375 ExitOnErr(Reader.readArray(Subsections, Contents.size()));
1376
1377 for (const DebugSubsectionRecord &SS : Subsections) {
1378 switch (SS.kind()) {
1379 case DebugSubsectionKind::StringTable: {
1380 assert(!CVStrTab.valid() &&
1381 "Encountered multiple string table subsections!");
1382 ExitOnErr(CVStrTab.initialize(SS.getRecordData()));
1383 break;
1384 }
1385 case DebugSubsectionKind::FileChecksums:
1386 assert(!Checksums.valid() &&
1387 "Encountered multiple checksum subsections!");
1388 ExitOnErr(Checksums.initialize(SS.getRecordData()));
1389 break;
1390 case DebugSubsectionKind::Lines: {
1391 ArrayRef<uint8_t> Bytes;
1392 auto Ref = SS.getRecordData();
1393 ExitOnErr(Ref.readLongestContiguousChunk(0, Bytes));
1394 size_t OffsetInDbgC = Bytes.data() - DbgC->getContents().data();
1395
1396 // Check whether this line table refers to C.
1397 auto I = Secrels.find(OffsetInDbgC);
1398 if (I == Secrels.end())
1399 break;
1400
1401 // Check whether this line table covers Addr in C.
1402 DebugLinesSubsectionRef LinesTmp;
1403 ExitOnErr(LinesTmp.initialize(BinaryStreamReader(Ref)));
1404 uint32_t OffsetInC = I->second + LinesTmp.header()->RelocOffset;
1405 if (Addr < OffsetInC || Addr >= OffsetInC + LinesTmp.header()->CodeSize)
1406 break;
1407
1408 assert(!Lines.header() &&
1409 "Encountered multiple line tables for function!");
1410 ExitOnErr(Lines.initialize(BinaryStreamReader(Ref)));
1411 OffsetInLinetable = Addr - OffsetInC;
1412 break;
1413 }
1414 default:
1415 break;
1416 }
1417
1418 if (CVStrTab.valid() && Checksums.valid() && Lines.header())
1419 return true;
1420 }
1421 }
1422
1423 return false;
1424}
1425
1426// Use CodeView line tables to resolve a file and line number for the given
1427// offset into the given chunk and return them, or {"", 0} if a line table was
1428// not found.
1429std::pair<StringRef, uint32_t> coff::getFileLine(const SectionChunk *C,
1430 uint32_t Addr) {
1431 ExitOnError ExitOnErr;
1432
1433 DebugStringTableSubsectionRef CVStrTab;
1434 DebugChecksumsSubsectionRef Checksums;
1435 DebugLinesSubsectionRef Lines;
1436 uint32_t OffsetInLinetable;
1437
1438 if (!findLineTable(C, Addr, CVStrTab, Checksums, Lines, OffsetInLinetable))
1439 return {"", 0};
1440
1441 uint32_t NameIndex;
1442 uint32_t LineNumber;
1443 for (LineColumnEntry &Entry : Lines) {
1444 for (const LineNumberEntry &LN : Entry.LineNumbers) {
1445 if (LN.Offset > OffsetInLinetable) {
1446 StringRef Filename =
1447 ExitOnErr(getFileName(CVStrTab, Checksums, NameIndex));
1448 return {Filename, LineNumber};
1449 }
1450 LineInfo LI(LN.Flags);
1451 NameIndex = Entry.NameIndex;
1452 LineNumber = LI.getStartLine();
1453 }
1454 }
1455 StringRef Filename = ExitOnErr(getFileName(CVStrTab, Checksums, NameIndex));
1456 return {Filename, LineNumber};
1457}