blob: 97b7f691dd70428c3299b5a8a9b8ecd4c5be54f9 [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"
Rui Ueyamabe939b32016-11-21 17:22:35 +000016#include "llvm/DebugInfo/CodeView/TypeDumper.h"
Rui Ueyamab28c6d42016-09-16 04:32:33 +000017#include "llvm/DebugInfo/MSF/MSFBuilder.h"
Rui Ueyama7f382992016-09-15 18:55:18 +000018#include "llvm/DebugInfo/MSF/MSFCommon.h"
Rui Ueyamab28c6d42016-09-16 04:32:33 +000019#include "llvm/DebugInfo/PDB/Raw/DbiStream.h"
20#include "llvm/DebugInfo/PDB/Raw/DbiStreamBuilder.h"
21#include "llvm/DebugInfo/PDB/Raw/InfoStream.h"
22#include "llvm/DebugInfo/PDB/Raw/InfoStreamBuilder.h"
23#include "llvm/DebugInfo/PDB/Raw/PDBFile.h"
24#include "llvm/DebugInfo/PDB/Raw/PDBFileBuilder.h"
25#include "llvm/DebugInfo/PDB/Raw/TpiStream.h"
26#include "llvm/DebugInfo/PDB/Raw/TpiStreamBuilder.h"
Rui Ueyama20df4ec2016-10-31 21:09:21 +000027#include "llvm/Object/COFF.h"
Rui Ueyama1763c0d2015-12-08 18:39:55 +000028#include "llvm/Support/Endian.h"
Rui Ueyamae7378242015-12-04 23:11:05 +000029#include "llvm/Support/FileOutputBuffer.h"
Rui Ueyamabe939b32016-11-21 17:22:35 +000030#include "llvm/Support/ScopedPrinter.h"
Rui Ueyamae7378242015-12-04 23:11:05 +000031#include <memory>
32
Rui Ueyama1d99ab32016-09-15 22:24:51 +000033using namespace lld;
Rui Ueyama09e0b5f2016-11-12 00:00:51 +000034using namespace lld::coff;
Rui Ueyamae7378242015-12-04 23:11:05 +000035using namespace llvm;
Rui Ueyamabe939b32016-11-21 17:22:35 +000036using namespace llvm::codeview;
Rui Ueyama1763c0d2015-12-08 18:39:55 +000037using namespace llvm::support;
38using namespace llvm::support::endian;
Rui Ueyamae7378242015-12-04 23:11:05 +000039
Rui Ueyama09e0b5f2016-11-12 00:00:51 +000040using llvm::object::coff_section;
41
Rui Ueyamab28c6d42016-09-16 04:32:33 +000042static ExitOnError ExitOnErr;
43
Rui Ueyama09e0b5f2016-11-12 00:00:51 +000044// Returns a list of all SectionChunks.
45static std::vector<coff_section> getInputSections(SymbolTable *Symtab) {
46 std::vector<coff_section> V;
47 for (Chunk *C : Symtab->getChunks())
48 if (auto *SC = dyn_cast<SectionChunk>(C))
49 V.push_back(*SC->Header);
50 return V;
51}
52
Rui Ueyamabe939b32016-11-21 17:22:35 +000053static SectionChunk *findByName(std::vector<SectionChunk *> &Sections,
54 StringRef Name) {
55 for (SectionChunk *C : Sections)
56 if (C->getSectionName() == Name)
57 return C;
58 return nullptr;
59}
60
61// Dump CodeView debug info. This is for debugging.
62static void dumpCodeView(SymbolTable *Symtab) {
63 ScopedPrinter W(outs());
64
65 for (ObjectFile *File : Symtab->ObjectFiles) {
66 SectionChunk *C = findByName(File->getDebugChunks(), ".debug$T");
67 if (!C)
68 continue;
69
70 CVTypeDumper TypeDumper(&W, false);
71 if (auto EC = TypeDumper.dump(C->getContents()))
72 fatal(EC, "CVTypeDumper::dump failed");
73 }
74}
75
Rui Ueyama09e0b5f2016-11-12 00:00:51 +000076// Creates a PDB file.
77void coff::createPDB(StringRef Path, SymbolTable *Symtab,
78 ArrayRef<uint8_t> SectionTable) {
Rui Ueyamabe939b32016-11-21 17:22:35 +000079 if (Config->DumpPdb)
80 dumpCodeView(Symtab);
81
Rui Ueyamab28c6d42016-09-16 04:32:33 +000082 BumpPtrAllocator Alloc;
83 pdb::PDBFileBuilder Builder(Alloc);
Rui Ueyama12979542016-09-30 20:53:45 +000084 ExitOnErr(Builder.initialize(4096)); // 4096 is blocksize
Rui Ueyama7f382992016-09-15 18:55:18 +000085
Rui Ueyama8d3fb5d2016-10-05 22:08:58 +000086 // Create streams in MSF for predefined streams, namely
87 // PDB, TPI, DBI and IPI.
88 for (int I = 0; I < (int)pdb::kSpecialStreamCount; ++I)
89 ExitOnErr(Builder.getMsfBuilder().addStream(0));
Rui Ueyama7f382992016-09-15 18:55:18 +000090
Rui Ueyamabb542b32016-09-16 22:51:17 +000091 // Add an Info stream.
92 auto &InfoBuilder = Builder.getInfoBuilder();
93 InfoBuilder.setAge(1);
94
95 // Should be a random number, 0 for now.
96 InfoBuilder.setGuid({});
97
98 // Should be the current time, but set 0 for reproducibilty.
99 InfoBuilder.setSignature(0);
Rui Ueyamabb542b32016-09-16 22:51:17 +0000100 InfoBuilder.setVersion(pdb::PdbRaw_ImplVer::PdbImplVC70);
Rui Ueyama7f382992016-09-15 18:55:18 +0000101
Rui Ueyama1343fac2016-10-06 22:52:01 +0000102 // Add an empty DPI stream.
103 auto &DbiBuilder = Builder.getDbiBuilder();
104 DbiBuilder.setVersionHeader(pdb::PdbDbiV110);
105
Rui Ueyamab28c6d42016-09-16 04:32:33 +0000106 // Add an empty TPI stream.
107 auto &TpiBuilder = Builder.getTpiBuilder();
108 TpiBuilder.setVersionHeader(pdb::PdbTpiV80);
Rui Ueyama1763c0d2015-12-08 18:39:55 +0000109
Rui Ueyamad381c982016-10-05 21:37:25 +0000110 // Add an empty IPI stream.
111 auto &IpiBuilder = Builder.getIpiBuilder();
112 IpiBuilder.setVersionHeader(pdb::PdbTpiV80);
113
Rui Ueyama09e0b5f2016-11-12 00:00:51 +0000114 // Add Section Contributions.
Rui Ueyama09e0b5f2016-11-12 00:00:51 +0000115 std::vector<pdb::SectionContrib> Contribs =
Vitaly Buka4cf112c2016-11-14 20:21:41 +0000116 pdb::DbiStreamBuilder::createSectionContribs(getInputSections(Symtab));
Rui Ueyama09e0b5f2016-11-12 00:00:51 +0000117 DbiBuilder.setSectionContribs(Contribs);
118
Rui Ueyama20df4ec2016-10-31 21:09:21 +0000119 // Add Section Map stream.
120 ArrayRef<object::coff_section> Sections = {
Rui Ueyama6294a242016-11-04 17:41:29 +0000121 (const object::coff_section *)SectionTable.data(),
Rui Ueyama20df4ec2016-10-31 21:09:21 +0000122 SectionTable.size() / sizeof(object::coff_section)};
123 std::vector<pdb::SecMapEntry> SectionMap =
124 pdb::DbiStreamBuilder::createSectionMap(Sections);
125 DbiBuilder.setSectionMap(SectionMap);
126
Rui Ueyamac91f7162016-11-16 01:10:46 +0000127 ExitOnErr(DbiBuilder.addModuleInfo("", "* Linker *"));
128
Rui Ueyama9f66f822016-10-11 19:45:07 +0000129 // Add COFF section header stream.
130 ExitOnErr(
131 DbiBuilder.addDbgStream(pdb::DbgHeaderType::SectionHdr, SectionTable));
132
Rui Ueyama3e9d6bb2016-09-26 23:53:55 +0000133 // Write to a file.
Rui Ueyama5c82eea2016-09-30 20:39:04 +0000134 ExitOnErr(Builder.commit(Path));
Rui Ueyamae7378242015-12-04 23:11:05 +0000135}