blob: 380e3019d77eee067df282bb1540f576554744eb [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"
Saleem Abdulrasooldf8a13b2017-01-04 17:56:54 +000016#include "llvm/DebugInfo/CodeView/CVDebugRecord.h"
Zachary Turner526f4f22017-05-19 19:26:58 +000017#include "llvm/DebugInfo/CodeView/CVTypeVisitor.h"
Reid Kleckner44cdb102017-06-19 17:21:45 +000018#include "llvm/DebugInfo/CodeView/DebugSubsectionRecord.h"
19#include "llvm/DebugInfo/CodeView/DebugSubsectionVisitor.h"
Zachary Turner526f4f22017-05-19 19:26:58 +000020#include "llvm/DebugInfo/CodeView/LazyRandomTypeCollection.h"
Rui Ueyama24625fd2016-11-22 23:51:34 +000021#include "llvm/DebugInfo/CodeView/SymbolDumper.h"
Zachary Turner629cb7d2017-01-11 23:24:22 +000022#include "llvm/DebugInfo/CodeView/TypeDumpVisitor.h"
Rui Ueyama52896622017-01-12 03:09:25 +000023#include "llvm/DebugInfo/CodeView/TypeStreamMerger.h"
24#include "llvm/DebugInfo/CodeView/TypeTableBuilder.h"
Rui Ueyamab28c6d42016-09-16 04:32:33 +000025#include "llvm/DebugInfo/MSF/MSFBuilder.h"
Rui Ueyama7f382992016-09-15 18:55:18 +000026#include "llvm/DebugInfo/MSF/MSFCommon.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000027#include "llvm/DebugInfo/PDB/Native/DbiStream.h"
28#include "llvm/DebugInfo/PDB/Native/DbiStreamBuilder.h"
29#include "llvm/DebugInfo/PDB/Native/InfoStream.h"
30#include "llvm/DebugInfo/PDB/Native/InfoStreamBuilder.h"
31#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
32#include "llvm/DebugInfo/PDB/Native/PDBFileBuilder.h"
Reid Kleckner8cbdd0c2017-06-13 15:49:13 +000033#include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptorBuilder.h"
Rafael Espindolaa0f30da2017-05-02 20:19:42 +000034#include "llvm/DebugInfo/PDB/Native/PDBStringTableBuilder.h"
Zachary Turner7b327d02017-02-16 23:35:45 +000035#include "llvm/DebugInfo/PDB/Native/PDBTypeServerHandler.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000036#include "llvm/DebugInfo/PDB/Native/TpiStream.h"
37#include "llvm/DebugInfo/PDB/Native/TpiStreamBuilder.h"
Rui Ueyama20df4ec2016-10-31 21:09:21 +000038#include "llvm/Object/COFF.h"
Zachary Turnerd9dc2822017-03-02 20:52:51 +000039#include "llvm/Support/BinaryByteStream.h"
Rui Ueyama1763c0d2015-12-08 18:39:55 +000040#include "llvm/Support/Endian.h"
Rui Ueyamae7378242015-12-04 23:11:05 +000041#include "llvm/Support/FileOutputBuffer.h"
Zachary Turner7b327d02017-02-16 23:35:45 +000042#include "llvm/Support/Path.h"
Rui Ueyamabe939b32016-11-21 17:22:35 +000043#include "llvm/Support/ScopedPrinter.h"
Rui Ueyamae7378242015-12-04 23:11:05 +000044#include <memory>
45
Rui Ueyama1d99ab32016-09-15 22:24:51 +000046using namespace lld;
Rui Ueyama09e0b5f2016-11-12 00:00:51 +000047using namespace lld::coff;
Rui Ueyamae7378242015-12-04 23:11:05 +000048using namespace llvm;
Rui Ueyamabe939b32016-11-21 17:22:35 +000049using namespace llvm::codeview;
Rui Ueyamae7378242015-12-04 23:11:05 +000050
Rui Ueyama09e0b5f2016-11-12 00:00:51 +000051using llvm::object::coff_section;
52
Rui Ueyamab28c6d42016-09-16 04:32:33 +000053static ExitOnError ExitOnErr;
54
Rui Ueyama09e0b5f2016-11-12 00:00:51 +000055// Returns a list of all SectionChunks.
Reid Kleckner8cbdd0c2017-06-13 15:49:13 +000056static void addSectionContribs(SymbolTable *Symtab, pdb::DbiStreamBuilder &DbiBuilder) {
Rui Ueyama09e0b5f2016-11-12 00:00:51 +000057 for (Chunk *C : Symtab->getChunks())
58 if (auto *SC = dyn_cast<SectionChunk>(C))
Reid Kleckner8cbdd0c2017-06-13 15:49:13 +000059 DbiBuilder.addSectionContrib(SC->File->ModuleDBI, SC->Header);
Rui Ueyama09e0b5f2016-11-12 00:00:51 +000060}
61
Rui Ueyamabe939b32016-11-21 17:22:35 +000062static SectionChunk *findByName(std::vector<SectionChunk *> &Sections,
63 StringRef Name) {
64 for (SectionChunk *C : Sections)
65 if (C->getSectionName() == Name)
66 return C;
67 return nullptr;
68}
69
Reid Kleckner44cdb102017-06-19 17:21:45 +000070static ArrayRef<uint8_t> consumeDebugMagic(ArrayRef<uint8_t> Data,
71 StringRef SecName) {
Rui Ueyamac5cb7372016-12-01 01:22:48 +000072 // First 4 bytes are section magic.
Rui Ueyamac5cb7372016-12-01 01:22:48 +000073 if (Data.size() < 4)
Rui Ueyama52896622017-01-12 03:09:25 +000074 fatal(SecName + " too short");
Reid Kleckner44cdb102017-06-19 17:21:45 +000075 if (support::endian::read32le(Data.data()) != COFF::DEBUG_SECTION_MAGIC)
Rui Ueyama52896622017-01-12 03:09:25 +000076 fatal(SecName + " has an invalid magic");
Rui Ueyama26186c72016-12-09 04:46:54 +000077 return Data.slice(4);
78}
Rui Ueyamac5cb7372016-12-01 01:22:48 +000079
Reid Kleckner44cdb102017-06-19 17:21:45 +000080static ArrayRef<uint8_t> getDebugSection(ObjectFile *File, StringRef SecName) {
81 if (SectionChunk *Sec = findByName(File->getDebugChunks(), SecName))
82 return consumeDebugMagic(Sec->getContents(), SecName);
83 return {};
84}
85
Reid Kleckner5d577522017-03-24 17:26:38 +000086static void addTypeInfo(pdb::TpiStreamBuilder &TpiBuilder,
Reid Kleckner44cdb102017-06-19 17:21:45 +000087 TypeTableBuilder &TypeTable) {
Reid Kleckner5d577522017-03-24 17:26:38 +000088 // Start the TPI or IPI stream header.
89 TpiBuilder.setVersionHeader(pdb::PdbTpiV80);
Rui Ueyama52896622017-01-12 03:09:25 +000090
Reid Kleckner5d577522017-03-24 17:26:38 +000091 // Flatten the in memory type table.
Reid Kleckner5d577522017-03-24 17:26:38 +000092 TypeTable.ForEachRecord([&](TypeIndex TI, ArrayRef<uint8_t> Rec) {
Reid Kleckner13fc4112017-04-04 00:56:34 +000093 // FIXME: Hash types.
94 TpiBuilder.addTypeRecord(Rec, None);
Reid Kleckner5d577522017-03-24 17:26:38 +000095 });
Reid Kleckner5d577522017-03-24 17:26:38 +000096}
97
Reid Kleckner44cdb102017-06-19 17:21:45 +000098static void mergeDebugT(ObjectFile *File,
99 TypeTableBuilder &IDTable,
100 TypeTableBuilder &TypeTable,
101 SmallVectorImpl<TypeIndex> &TypeIndexMap,
102 pdb::PDBTypeServerHandler &Handler) {
103 ArrayRef<uint8_t> Data = getDebugSection(File, ".debug$T");
104 if (Data.empty())
105 return;
106
107 BinaryByteStream Stream(Data, support::little);
108 CVTypeArray Types;
109 BinaryStreamReader Reader(Stream);
110 Handler.addSearchPath(sys::path::parent_path(File->getName()));
111 if (auto EC = Reader.readArray(Types, Reader.getLength()))
112 fatal(EC, "Reader::readArray failed");
113 if (auto Err = mergeTypeAndIdRecords(IDTable, TypeTable,
114 TypeIndexMap, &Handler, Types))
115 fatal(Err, "codeview::mergeTypeStreams failed");
116}
117
118// Allocate memory for a .debug$S section and relocate it.
119static ArrayRef<uint8_t> relocateDebugChunk(BumpPtrAllocator &Alloc,
120 SectionChunk *DebugChunk) {
121 uint8_t *Buffer = Alloc.Allocate<uint8_t>(DebugChunk->getSize());
122 assert(DebugChunk->OutputSectionOff == 0 &&
123 "debug sections should not be in output sections");
124 DebugChunk->writeTo(Buffer);
125 return consumeDebugMagic(makeArrayRef(Buffer, DebugChunk->getSize()),
126 ".debug$S");
127}
128
Reid Kleckner8cbdd0c2017-06-13 15:49:13 +0000129// Add all object files to the PDB. Merge .debug$T sections into IpiData and
130// TpiData.
Reid Kleckner44cdb102017-06-19 17:21:45 +0000131static void addObjectsToPDB(BumpPtrAllocator &Alloc, SymbolTable *Symtab,
132 pdb::PDBFileBuilder &Builder,
133 TypeTableBuilder &TypeTable,
134 TypeTableBuilder &IDTable) {
Zachary Turner2897e032017-05-25 21:16:03 +0000135 // Follow type servers. If the same type server is encountered more than
136 // once for this instance of `PDBTypeServerHandler` (for example if many
137 // object files reference the same TypeServer), the types from the
138 // TypeServer will only be visited once.
139 pdb::PDBTypeServerHandler Handler;
140
Reid Kleckner44cdb102017-06-19 17:21:45 +0000141 // PDBs use a single global string table for filenames in the file checksum
142 // table.
143 auto PDBStrTab = std::make_shared<DebugStringTableSubsection>();
144
Rui Ueyama52896622017-01-12 03:09:25 +0000145 // Visit all .debug$T sections to add them to Builder.
Rui Ueyama52896622017-01-12 03:09:25 +0000146 for (ObjectFile *File : Symtab->ObjectFiles) {
Reid Kleckner8cbdd0c2017-06-13 15:49:13 +0000147 // Add a module descriptor for every object file. We need to put an absolute
148 // path to the object into the PDB. If this is a plain object, we make its
149 // path absolute. If it's an object in an archive, we make the archive path
150 // absolute.
151 bool InArchive = !File->ParentName.empty();
152 SmallString<128> Path = InArchive ? File->ParentName : File->getName();
153 sys::fs::make_absolute(Path);
154 StringRef Name = InArchive ? File->getName() : StringRef(Path);
155 File->ModuleDBI = &ExitOnErr(Builder.getDbiBuilder().addModuleInfo(Name));
156 File->ModuleDBI->setObjFileName(Path);
157
Reid Kleckner44cdb102017-06-19 17:21:45 +0000158 // Before we can process symbol substreams from .debug$S, we need to process
159 // type information, file checksums, and the string table. Add type info to
160 // the PDB first, so that we can get the map from object file type and item
161 // indices to PDB type and item indices.
162 SmallVector<TypeIndex, 128> TypeIndexMap;
163 mergeDebugT(File, IDTable, TypeTable, TypeIndexMap, Handler);
Reid Kleckner8cbdd0c2017-06-13 15:49:13 +0000164
Reid Kleckner44cdb102017-06-19 17:21:45 +0000165 // Now do all line info.
166 for (SectionChunk *DebugChunk : File->getDebugChunks()) {
167 // FIXME: Debug chunks do not have a correct isLive() bit.
168 // FIXME: When linker GC is off we need to ignore debug info whose
169 // associated symbol was discarded.
170 if (DebugChunk->getSectionName() != ".debug$S")
171 continue;
Rui Ueyama52896622017-01-12 03:09:25 +0000172
Reid Kleckner44cdb102017-06-19 17:21:45 +0000173 ArrayRef<uint8_t> RelocatedDebugContents =
174 relocateDebugChunk(Alloc, DebugChunk);
175 if (RelocatedDebugContents.empty())
176 continue;
177
178 DebugSubsectionArray Subsections;
179 BinaryStreamReader Reader(RelocatedDebugContents, support::little);
180 ExitOnErr(Reader.readArray(Subsections, RelocatedDebugContents.size()));
181
182 DebugStringTableSubsectionRef CVStrTab;
183 DebugChecksumsSubsectionRef Checksums;
184 for (const DebugSubsectionRecord &SS : Subsections) {
185 switch (SS.kind()) {
186 case DebugSubsectionKind::StringTable:
187 ExitOnErr(CVStrTab.initialize(SS.getRecordData()));
188 break;
189 case DebugSubsectionKind::FileChecksums:
190 ExitOnErr(Checksums.initialize(SS.getRecordData()));
191 break;
192 case DebugSubsectionKind::Lines:
193 // We can add the relocated line table directly to the PDB without
194 // modification because the file checksum offsets will stay the same.
195 File->ModuleDBI->addDebugSubsection(SS);
196 break;
197 default:
198 // FIXME: Process the rest of the subsections.
199 break;
200 }
201 }
202
203 if (Checksums.valid()) {
204 // Make a new file checksum table that refers to offsets in the PDB-wide
205 // string table. Generally the string table subsection appears after the
206 // checksum table, so we have to do this after looping over all the
207 // subsections.
208 if (!CVStrTab.valid())
209 fatal(".debug$S sections must have both a string table subsection "
210 "and a checksum subsection table or neither");
211 auto NewChecksums =
Reid Kleckneradea0ce2017-06-19 17:27:31 +0000212 make_unique<DebugChecksumsSubsection>(*PDBStrTab);
Reid Kleckner44cdb102017-06-19 17:21:45 +0000213 for (FileChecksumEntry &FC : Checksums) {
214 StringRef FileName = ExitOnErr(CVStrTab.getString(FC.FileNameOffset));
215 ExitOnErr(Builder.getDbiBuilder().addModuleSourceFile(
216 *File->ModuleDBI, FileName));
217 NewChecksums->addChecksum(FileName, FC.Kind, FC.Checksum);
218 }
219 File->ModuleDBI->addDebugSubsection(std::move(NewChecksums));
220 }
221 }
Rui Ueyama52896622017-01-12 03:09:25 +0000222 }
223
Reid Kleckner44cdb102017-06-19 17:21:45 +0000224 Builder.getStringTableBuilder().setStrings(*PDBStrTab);
225
Reid Kleckner5d577522017-03-24 17:26:38 +0000226 // Construct TPI stream contents.
Reid Kleckner13fc4112017-04-04 00:56:34 +0000227 addTypeInfo(Builder.getTpiBuilder(), TypeTable);
Reid Kleckner5d577522017-03-24 17:26:38 +0000228
229 // Construct IPI stream contents.
Reid Kleckner13fc4112017-04-04 00:56:34 +0000230 addTypeInfo(Builder.getIpiBuilder(), IDTable);
Rui Ueyama52896622017-01-12 03:09:25 +0000231}
232
Rui Ueyama09e0b5f2016-11-12 00:00:51 +0000233// Creates a PDB file.
234void coff::createPDB(StringRef Path, SymbolTable *Symtab,
Saleem Abdulrasooldf8a13b2017-01-04 17:56:54 +0000235 ArrayRef<uint8_t> SectionTable,
236 const llvm::codeview::DebugInfo *DI) {
Rui Ueyamab28c6d42016-09-16 04:32:33 +0000237 BumpPtrAllocator Alloc;
238 pdb::PDBFileBuilder Builder(Alloc);
Rui Ueyama12979542016-09-30 20:53:45 +0000239 ExitOnErr(Builder.initialize(4096)); // 4096 is blocksize
Rui Ueyama7f382992016-09-15 18:55:18 +0000240
Rui Ueyama8d3fb5d2016-10-05 22:08:58 +0000241 // Create streams in MSF for predefined streams, namely
242 // PDB, TPI, DBI and IPI.
243 for (int I = 0; I < (int)pdb::kSpecialStreamCount; ++I)
244 ExitOnErr(Builder.getMsfBuilder().addStream(0));
Rui Ueyama7f382992016-09-15 18:55:18 +0000245
Rui Ueyamabb542b32016-09-16 22:51:17 +0000246 // Add an Info stream.
247 auto &InfoBuilder = Builder.getInfoBuilder();
Saleem Abdulrasool0acd6dd2017-02-07 04:28:02 +0000248 InfoBuilder.setAge(DI ? DI->PDB70.Age : 0);
249
250 pdb::PDB_UniqueId uuid{};
251 if (DI)
252 memcpy(&uuid, &DI->PDB70.Signature, sizeof(uuid));
253 InfoBuilder.setGuid(uuid);
Rui Ueyamabb542b32016-09-16 22:51:17 +0000254 // Should be the current time, but set 0 for reproducibilty.
255 InfoBuilder.setSignature(0);
Rui Ueyamabb542b32016-09-16 22:51:17 +0000256 InfoBuilder.setVersion(pdb::PdbRaw_ImplVer::PdbImplVC70);
Rui Ueyama7f382992016-09-15 18:55:18 +0000257
Rui Ueyama1343fac2016-10-06 22:52:01 +0000258 // Add an empty DPI stream.
Reid Kleckner8cbdd0c2017-06-13 15:49:13 +0000259 pdb::DbiStreamBuilder &DbiBuilder = Builder.getDbiBuilder();
Rui Ueyama1343fac2016-10-06 22:52:01 +0000260 DbiBuilder.setVersionHeader(pdb::PdbDbiV110);
261
Reid Kleckner44cdb102017-06-19 17:21:45 +0000262 TypeTableBuilder TypeTable(BAlloc);
263 TypeTableBuilder IDTable(BAlloc);
264 addObjectsToPDB(Alloc, Symtab, Builder, TypeTable, IDTable);
Rui Ueyamad381c982016-10-05 21:37:25 +0000265
Rui Ueyama09e0b5f2016-11-12 00:00:51 +0000266 // Add Section Contributions.
Reid Kleckner8cbdd0c2017-06-13 15:49:13 +0000267 addSectionContribs(Symtab, DbiBuilder);
Rui Ueyama09e0b5f2016-11-12 00:00:51 +0000268
Rui Ueyama20df4ec2016-10-31 21:09:21 +0000269 // Add Section Map stream.
270 ArrayRef<object::coff_section> Sections = {
Rui Ueyama6294a242016-11-04 17:41:29 +0000271 (const object::coff_section *)SectionTable.data(),
Rui Ueyama20df4ec2016-10-31 21:09:21 +0000272 SectionTable.size() / sizeof(object::coff_section)};
273 std::vector<pdb::SecMapEntry> SectionMap =
274 pdb::DbiStreamBuilder::createSectionMap(Sections);
275 DbiBuilder.setSectionMap(SectionMap);
276
Zachary Turnerea4e6072017-03-15 22:18:53 +0000277 ExitOnErr(DbiBuilder.addModuleInfo("* Linker *"));
Rui Ueyamac91f7162016-11-16 01:10:46 +0000278
Rui Ueyama9f66f822016-10-11 19:45:07 +0000279 // Add COFF section header stream.
280 ExitOnErr(
281 DbiBuilder.addDbgStream(pdb::DbgHeaderType::SectionHdr, SectionTable));
282
Rui Ueyama3e9d6bb2016-09-26 23:53:55 +0000283 // Write to a file.
Rui Ueyama5c82eea2016-09-30 20:39:04 +0000284 ExitOnErr(Builder.commit(Path));
Rui Ueyamae7378242015-12-04 23:11:05 +0000285}