blob: 470c07ae51114b6e15ad1260d362323db2d2c788 [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 Ueyamae7378242015-12-04 23:11:05 +000012#include "Error.h"
Rui Ueyama09e0b5f2016-11-12 00:00:51 +000013#include "SymbolTable.h"
14#include "Symbols.h"
Rui Ueyamab28c6d42016-09-16 04:32:33 +000015#include "llvm/DebugInfo/MSF/MSFBuilder.h"
Rui Ueyama7f382992016-09-15 18:55:18 +000016#include "llvm/DebugInfo/MSF/MSFCommon.h"
Rui Ueyamab28c6d42016-09-16 04:32:33 +000017#include "llvm/DebugInfo/PDB/Raw/DbiStream.h"
18#include "llvm/DebugInfo/PDB/Raw/DbiStreamBuilder.h"
19#include "llvm/DebugInfo/PDB/Raw/InfoStream.h"
20#include "llvm/DebugInfo/PDB/Raw/InfoStreamBuilder.h"
21#include "llvm/DebugInfo/PDB/Raw/PDBFile.h"
22#include "llvm/DebugInfo/PDB/Raw/PDBFileBuilder.h"
23#include "llvm/DebugInfo/PDB/Raw/TpiStream.h"
24#include "llvm/DebugInfo/PDB/Raw/TpiStreamBuilder.h"
Rui Ueyama20df4ec2016-10-31 21:09:21 +000025#include "llvm/Object/COFF.h"
Rui Ueyama1763c0d2015-12-08 18:39:55 +000026#include "llvm/Support/Endian.h"
Rui Ueyamae7378242015-12-04 23:11:05 +000027#include "llvm/Support/FileOutputBuffer.h"
28#include <memory>
29
Rui Ueyama1d99ab32016-09-15 22:24:51 +000030using namespace lld;
Rui Ueyama09e0b5f2016-11-12 00:00:51 +000031using namespace lld::coff;
Rui Ueyamae7378242015-12-04 23:11:05 +000032using namespace llvm;
Rui Ueyama1763c0d2015-12-08 18:39:55 +000033using namespace llvm::support;
34using namespace llvm::support::endian;
Rui Ueyamae7378242015-12-04 23:11:05 +000035
Rui Ueyama09e0b5f2016-11-12 00:00:51 +000036using llvm::object::coff_section;
37
Rui Ueyamab28c6d42016-09-16 04:32:33 +000038static ExitOnError ExitOnErr;
39
Rui Ueyama09e0b5f2016-11-12 00:00:51 +000040// Returns a list of all SectionChunks.
41static std::vector<coff_section> getInputSections(SymbolTable *Symtab) {
42 std::vector<coff_section> V;
43 for (Chunk *C : Symtab->getChunks())
44 if (auto *SC = dyn_cast<SectionChunk>(C))
45 V.push_back(*SC->Header);
46 return V;
47}
48
49// Creates a PDB file.
50void coff::createPDB(StringRef Path, SymbolTable *Symtab,
51 ArrayRef<uint8_t> SectionTable) {
Rui Ueyamab28c6d42016-09-16 04:32:33 +000052 BumpPtrAllocator Alloc;
53 pdb::PDBFileBuilder Builder(Alloc);
Rui Ueyama12979542016-09-30 20:53:45 +000054 ExitOnErr(Builder.initialize(4096)); // 4096 is blocksize
Rui Ueyama7f382992016-09-15 18:55:18 +000055
Rui Ueyama8d3fb5d2016-10-05 22:08:58 +000056 // Create streams in MSF for predefined streams, namely
57 // PDB, TPI, DBI and IPI.
58 for (int I = 0; I < (int)pdb::kSpecialStreamCount; ++I)
59 ExitOnErr(Builder.getMsfBuilder().addStream(0));
Rui Ueyama7f382992016-09-15 18:55:18 +000060
Rui Ueyamabb542b32016-09-16 22:51:17 +000061 // Add an Info stream.
62 auto &InfoBuilder = Builder.getInfoBuilder();
63 InfoBuilder.setAge(1);
64
65 // Should be a random number, 0 for now.
66 InfoBuilder.setGuid({});
67
68 // Should be the current time, but set 0 for reproducibilty.
69 InfoBuilder.setSignature(0);
Rui Ueyamabb542b32016-09-16 22:51:17 +000070 InfoBuilder.setVersion(pdb::PdbRaw_ImplVer::PdbImplVC70);
Rui Ueyama7f382992016-09-15 18:55:18 +000071
Rui Ueyama1343fac2016-10-06 22:52:01 +000072 // Add an empty DPI stream.
73 auto &DbiBuilder = Builder.getDbiBuilder();
74 DbiBuilder.setVersionHeader(pdb::PdbDbiV110);
75
Rui Ueyamab28c6d42016-09-16 04:32:33 +000076 // Add an empty TPI stream.
77 auto &TpiBuilder = Builder.getTpiBuilder();
78 TpiBuilder.setVersionHeader(pdb::PdbTpiV80);
Rui Ueyama1763c0d2015-12-08 18:39:55 +000079
Rui Ueyamad381c982016-10-05 21:37:25 +000080 // Add an empty IPI stream.
81 auto &IpiBuilder = Builder.getIpiBuilder();
82 IpiBuilder.setVersionHeader(pdb::PdbTpiV80);
83
Rui Ueyama09e0b5f2016-11-12 00:00:51 +000084 // Add Section Contributions.
Rui Ueyama09e0b5f2016-11-12 00:00:51 +000085 std::vector<pdb::SectionContrib> Contribs =
Vitaly Buka4cf112c2016-11-14 20:21:41 +000086 pdb::DbiStreamBuilder::createSectionContribs(getInputSections(Symtab));
Rui Ueyama09e0b5f2016-11-12 00:00:51 +000087 DbiBuilder.setSectionContribs(Contribs);
88
Rui Ueyama20df4ec2016-10-31 21:09:21 +000089 // Add Section Map stream.
90 ArrayRef<object::coff_section> Sections = {
Rui Ueyama6294a242016-11-04 17:41:29 +000091 (const object::coff_section *)SectionTable.data(),
Rui Ueyama20df4ec2016-10-31 21:09:21 +000092 SectionTable.size() / sizeof(object::coff_section)};
93 std::vector<pdb::SecMapEntry> SectionMap =
94 pdb::DbiStreamBuilder::createSectionMap(Sections);
95 DbiBuilder.setSectionMap(SectionMap);
96
Rui Ueyamac91f7162016-11-16 01:10:46 +000097 ExitOnErr(DbiBuilder.addModuleInfo("", "* Linker *"));
98
Rui Ueyama9f66f822016-10-11 19:45:07 +000099 // Add COFF section header stream.
100 ExitOnErr(
101 DbiBuilder.addDbgStream(pdb::DbgHeaderType::SectionHdr, SectionTable));
102
Rui Ueyama3e9d6bb2016-09-26 23:53:55 +0000103 // Write to a file.
Rui Ueyama5c82eea2016-09-30 20:39:04 +0000104 ExitOnErr(Builder.commit(Path));
Rui Ueyamae7378242015-12-04 23:11:05 +0000105}