blob: 81eea32f689c6b86723a5309c3d1d42055d5965f [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 Ueyama24625fd2016-11-22 23:51:34 +000016#include "llvm/DebugInfo/CodeView/SymbolDumper.h"
Rui Ueyamabe939b32016-11-21 17:22:35 +000017#include "llvm/DebugInfo/CodeView/TypeDumper.h"
Rui Ueyama24625fd2016-11-22 23:51:34 +000018#include "llvm/DebugInfo/MSF/ByteStream.h"
Rui Ueyamab28c6d42016-09-16 04:32:33 +000019#include "llvm/DebugInfo/MSF/MSFBuilder.h"
Rui Ueyama7f382992016-09-15 18:55:18 +000020#include "llvm/DebugInfo/MSF/MSFCommon.h"
Rui Ueyamab28c6d42016-09-16 04:32:33 +000021#include "llvm/DebugInfo/PDB/Raw/DbiStream.h"
22#include "llvm/DebugInfo/PDB/Raw/DbiStreamBuilder.h"
23#include "llvm/DebugInfo/PDB/Raw/InfoStream.h"
24#include "llvm/DebugInfo/PDB/Raw/InfoStreamBuilder.h"
25#include "llvm/DebugInfo/PDB/Raw/PDBFile.h"
26#include "llvm/DebugInfo/PDB/Raw/PDBFileBuilder.h"
27#include "llvm/DebugInfo/PDB/Raw/TpiStream.h"
28#include "llvm/DebugInfo/PDB/Raw/TpiStreamBuilder.h"
Rui Ueyama20df4ec2016-10-31 21:09:21 +000029#include "llvm/Object/COFF.h"
Rui Ueyama1763c0d2015-12-08 18:39:55 +000030#include "llvm/Support/Endian.h"
Rui Ueyamae7378242015-12-04 23:11:05 +000031#include "llvm/Support/FileOutputBuffer.h"
Rui Ueyamabe939b32016-11-21 17:22:35 +000032#include "llvm/Support/ScopedPrinter.h"
Rui Ueyamae7378242015-12-04 23:11:05 +000033#include <memory>
34
Rui Ueyama1d99ab32016-09-15 22:24:51 +000035using namespace lld;
Rui Ueyama09e0b5f2016-11-12 00:00:51 +000036using namespace lld::coff;
Rui Ueyamae7378242015-12-04 23:11:05 +000037using namespace llvm;
Rui Ueyamabe939b32016-11-21 17:22:35 +000038using namespace llvm::codeview;
Rui Ueyama1763c0d2015-12-08 18:39:55 +000039using namespace llvm::support;
40using namespace llvm::support::endian;
Rui Ueyamae7378242015-12-04 23:11:05 +000041
Rui Ueyama09e0b5f2016-11-12 00:00:51 +000042using llvm::object::coff_section;
43
Rui Ueyamab28c6d42016-09-16 04:32:33 +000044static ExitOnError ExitOnErr;
45
Rui Ueyama09e0b5f2016-11-12 00:00:51 +000046// Returns a list of all SectionChunks.
47static std::vector<coff_section> getInputSections(SymbolTable *Symtab) {
48 std::vector<coff_section> V;
49 for (Chunk *C : Symtab->getChunks())
50 if (auto *SC = dyn_cast<SectionChunk>(C))
51 V.push_back(*SC->Header);
52 return V;
53}
54
Rui Ueyamabe939b32016-11-21 17:22:35 +000055static SectionChunk *findByName(std::vector<SectionChunk *> &Sections,
56 StringRef Name) {
57 for (SectionChunk *C : Sections)
58 if (C->getSectionName() == Name)
59 return C;
60 return nullptr;
61}
62
63// Dump CodeView debug info. This is for debugging.
64static void dumpCodeView(SymbolTable *Symtab) {
65 ScopedPrinter W(outs());
66
67 for (ObjectFile *File : Symtab->ObjectFiles) {
Rui Ueyama24625fd2016-11-22 23:51:34 +000068 SectionChunk *DebugT = findByName(File->getDebugChunks(), ".debug$T");
69 if (!DebugT)
Rui Ueyamabe939b32016-11-21 17:22:35 +000070 continue;
71
72 CVTypeDumper TypeDumper(&W, false);
Rui Ueyama24625fd2016-11-22 23:51:34 +000073 if (auto EC = TypeDumper.dump(DebugT->getContents()))
Rui Ueyamabe939b32016-11-21 17:22:35 +000074 fatal(EC, "CVTypeDumper::dump failed");
Rui Ueyama24625fd2016-11-22 23:51:34 +000075
76 SectionChunk *DebugS = findByName(File->getDebugChunks(), ".debug$S");
77 if (!DebugS)
78 continue;
79
80 msf::ByteStream Stream(DebugS->getContents());
81 CVSymbolArray Symbols;
82 msf::StreamReader Reader(Stream);
83 if (auto EC = Reader.readArray(Symbols, Reader.getLength()))
84 fatal(EC, "StreamReader.readArray<CVSymbolArray> failed");
85
86 CVSymbolDumper SymbolDumper(W, TypeDumper, nullptr, false);
87 if (auto EC = SymbolDumper.dump(Symbols))
88 fatal(EC, "CVSymbolDumper::dump failed");
Rui Ueyamabe939b32016-11-21 17:22:35 +000089 }
90}
91
Rui Ueyama09e0b5f2016-11-12 00:00:51 +000092// Creates a PDB file.
93void coff::createPDB(StringRef Path, SymbolTable *Symtab,
94 ArrayRef<uint8_t> SectionTable) {
Rui Ueyamabe939b32016-11-21 17:22:35 +000095 if (Config->DumpPdb)
96 dumpCodeView(Symtab);
97
Rui Ueyamab28c6d42016-09-16 04:32:33 +000098 BumpPtrAllocator Alloc;
99 pdb::PDBFileBuilder Builder(Alloc);
Rui Ueyama12979542016-09-30 20:53:45 +0000100 ExitOnErr(Builder.initialize(4096)); // 4096 is blocksize
Rui Ueyama7f382992016-09-15 18:55:18 +0000101
Rui Ueyama8d3fb5d2016-10-05 22:08:58 +0000102 // Create streams in MSF for predefined streams, namely
103 // PDB, TPI, DBI and IPI.
104 for (int I = 0; I < (int)pdb::kSpecialStreamCount; ++I)
105 ExitOnErr(Builder.getMsfBuilder().addStream(0));
Rui Ueyama7f382992016-09-15 18:55:18 +0000106
Rui Ueyamabb542b32016-09-16 22:51:17 +0000107 // Add an Info stream.
108 auto &InfoBuilder = Builder.getInfoBuilder();
109 InfoBuilder.setAge(1);
110
111 // Should be a random number, 0 for now.
112 InfoBuilder.setGuid({});
113
114 // Should be the current time, but set 0 for reproducibilty.
115 InfoBuilder.setSignature(0);
Rui Ueyamabb542b32016-09-16 22:51:17 +0000116 InfoBuilder.setVersion(pdb::PdbRaw_ImplVer::PdbImplVC70);
Rui Ueyama7f382992016-09-15 18:55:18 +0000117
Rui Ueyama1343fac2016-10-06 22:52:01 +0000118 // Add an empty DPI stream.
119 auto &DbiBuilder = Builder.getDbiBuilder();
120 DbiBuilder.setVersionHeader(pdb::PdbDbiV110);
121
Rui Ueyamab28c6d42016-09-16 04:32:33 +0000122 // Add an empty TPI stream.
123 auto &TpiBuilder = Builder.getTpiBuilder();
124 TpiBuilder.setVersionHeader(pdb::PdbTpiV80);
Rui Ueyama1763c0d2015-12-08 18:39:55 +0000125
Rui Ueyamad381c982016-10-05 21:37:25 +0000126 // Add an empty IPI stream.
127 auto &IpiBuilder = Builder.getIpiBuilder();
128 IpiBuilder.setVersionHeader(pdb::PdbTpiV80);
129
Rui Ueyama09e0b5f2016-11-12 00:00:51 +0000130 // Add Section Contributions.
Rui Ueyama09e0b5f2016-11-12 00:00:51 +0000131 std::vector<pdb::SectionContrib> Contribs =
Vitaly Buka4cf112c2016-11-14 20:21:41 +0000132 pdb::DbiStreamBuilder::createSectionContribs(getInputSections(Symtab));
Rui Ueyama09e0b5f2016-11-12 00:00:51 +0000133 DbiBuilder.setSectionContribs(Contribs);
134
Rui Ueyama20df4ec2016-10-31 21:09:21 +0000135 // Add Section Map stream.
136 ArrayRef<object::coff_section> Sections = {
Rui Ueyama6294a242016-11-04 17:41:29 +0000137 (const object::coff_section *)SectionTable.data(),
Rui Ueyama20df4ec2016-10-31 21:09:21 +0000138 SectionTable.size() / sizeof(object::coff_section)};
139 std::vector<pdb::SecMapEntry> SectionMap =
140 pdb::DbiStreamBuilder::createSectionMap(Sections);
141 DbiBuilder.setSectionMap(SectionMap);
142
Rui Ueyamac91f7162016-11-16 01:10:46 +0000143 ExitOnErr(DbiBuilder.addModuleInfo("", "* Linker *"));
144
Rui Ueyama9f66f822016-10-11 19:45:07 +0000145 // Add COFF section header stream.
146 ExitOnErr(
147 DbiBuilder.addDbgStream(pdb::DbgHeaderType::SectionHdr, SectionTable));
148
Rui Ueyama3e9d6bb2016-09-26 23:53:55 +0000149 // Write to a file.
Rui Ueyama5c82eea2016-09-30 20:39:04 +0000150 ExitOnErr(Builder.commit(Path));
Rui Ueyamae7378242015-12-04 23:11:05 +0000151}