blob: 283b75a9c20c19cf4a46dfcb3f89c47e6f818240 [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 Ueyamae7378242015-12-04 23:11:05 +000011#include "Error.h"
Rui Ueyamab28c6d42016-09-16 04:32:33 +000012#include "llvm/DebugInfo/MSF/MSFBuilder.h"
Rui Ueyama7f382992016-09-15 18:55:18 +000013#include "llvm/DebugInfo/MSF/MSFCommon.h"
Rui Ueyamab28c6d42016-09-16 04:32:33 +000014#include "llvm/DebugInfo/PDB/Raw/DbiStream.h"
15#include "llvm/DebugInfo/PDB/Raw/DbiStreamBuilder.h"
16#include "llvm/DebugInfo/PDB/Raw/InfoStream.h"
17#include "llvm/DebugInfo/PDB/Raw/InfoStreamBuilder.h"
18#include "llvm/DebugInfo/PDB/Raw/PDBFile.h"
19#include "llvm/DebugInfo/PDB/Raw/PDBFileBuilder.h"
20#include "llvm/DebugInfo/PDB/Raw/TpiStream.h"
21#include "llvm/DebugInfo/PDB/Raw/TpiStreamBuilder.h"
Rui Ueyama20df4ec2016-10-31 21:09:21 +000022#include "llvm/Object/COFF.h"
Rui Ueyama1763c0d2015-12-08 18:39:55 +000023#include "llvm/Support/Endian.h"
Rui Ueyamae7378242015-12-04 23:11:05 +000024#include "llvm/Support/FileOutputBuffer.h"
25#include <memory>
26
Rui Ueyama1d99ab32016-09-15 22:24:51 +000027using namespace lld;
Rui Ueyamae7378242015-12-04 23:11:05 +000028using namespace llvm;
Rui Ueyama1763c0d2015-12-08 18:39:55 +000029using namespace llvm::support;
30using namespace llvm::support::endian;
Rui Ueyamae7378242015-12-04 23:11:05 +000031
Rui Ueyamab28c6d42016-09-16 04:32:33 +000032static ExitOnError ExitOnErr;
33
Rui Ueyama9f66f822016-10-11 19:45:07 +000034void coff::createPDB(StringRef Path, ArrayRef<uint8_t> SectionTable) {
Rui Ueyamab28c6d42016-09-16 04:32:33 +000035 BumpPtrAllocator Alloc;
36 pdb::PDBFileBuilder Builder(Alloc);
Rui Ueyama12979542016-09-30 20:53:45 +000037 ExitOnErr(Builder.initialize(4096)); // 4096 is blocksize
Rui Ueyama7f382992016-09-15 18:55:18 +000038
Rui Ueyama8d3fb5d2016-10-05 22:08:58 +000039 // Create streams in MSF for predefined streams, namely
40 // PDB, TPI, DBI and IPI.
41 for (int I = 0; I < (int)pdb::kSpecialStreamCount; ++I)
42 ExitOnErr(Builder.getMsfBuilder().addStream(0));
Rui Ueyama7f382992016-09-15 18:55:18 +000043
Rui Ueyamabb542b32016-09-16 22:51:17 +000044 // Add an Info stream.
45 auto &InfoBuilder = Builder.getInfoBuilder();
46 InfoBuilder.setAge(1);
47
48 // Should be a random number, 0 for now.
49 InfoBuilder.setGuid({});
50
51 // Should be the current time, but set 0 for reproducibilty.
52 InfoBuilder.setSignature(0);
Rui Ueyamabb542b32016-09-16 22:51:17 +000053 InfoBuilder.setVersion(pdb::PdbRaw_ImplVer::PdbImplVC70);
Rui Ueyama7f382992016-09-15 18:55:18 +000054
Rui Ueyama1343fac2016-10-06 22:52:01 +000055 // Add an empty DPI stream.
56 auto &DbiBuilder = Builder.getDbiBuilder();
57 DbiBuilder.setVersionHeader(pdb::PdbDbiV110);
58
Rui Ueyamab28c6d42016-09-16 04:32:33 +000059 // Add an empty TPI stream.
60 auto &TpiBuilder = Builder.getTpiBuilder();
61 TpiBuilder.setVersionHeader(pdb::PdbTpiV80);
Rui Ueyama1763c0d2015-12-08 18:39:55 +000062
Rui Ueyamad381c982016-10-05 21:37:25 +000063 // Add an empty IPI stream.
64 auto &IpiBuilder = Builder.getIpiBuilder();
65 IpiBuilder.setVersionHeader(pdb::PdbTpiV80);
66
Rui Ueyama20df4ec2016-10-31 21:09:21 +000067 // Add Section Map stream.
68 ArrayRef<object::coff_section> Sections = {
69 (object::coff_section *)SectionTable.data(),
70 SectionTable.size() / sizeof(object::coff_section)};
71 std::vector<pdb::SecMapEntry> SectionMap =
72 pdb::DbiStreamBuilder::createSectionMap(Sections);
73 DbiBuilder.setSectionMap(SectionMap);
74
Rui Ueyama9f66f822016-10-11 19:45:07 +000075 // Add COFF section header stream.
76 ExitOnErr(
77 DbiBuilder.addDbgStream(pdb::DbgHeaderType::SectionHdr, SectionTable));
78
Rui Ueyama3e9d6bb2016-09-26 23:53:55 +000079 // Write to a file.
Rui Ueyama5c82eea2016-09-30 20:39:04 +000080 ExitOnErr(Builder.commit(Path));
Rui Ueyamae7378242015-12-04 23:11:05 +000081}