blob: bf3f83741ae6e70ad46f2c2237a20bbeb0625d75 [file] [log] [blame]
Zachary Turner67c56012017-04-27 16:11:19 +00001//===- DbiModuleDescriptorBuilder.cpp - PDB Mod Info Creation ---*- C++ -*-===//
Zachary Turnerea4e6072017-03-15 22:18:53 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Zachary Turner67c56012017-04-27 16:11:19 +000010#include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptorBuilder.h"
Zachary Turnerea4e6072017-03-15 22:18:53 +000011
12#include "llvm/ADT/ArrayRef.h"
Zachary Turner8c099fe2017-05-30 16:36:15 +000013#include "llvm/DebugInfo/CodeView/DebugSubsectionRecord.h"
Zachary Turnerea4e6072017-03-15 22:18:53 +000014#include "llvm/DebugInfo/MSF/MSFBuilder.h"
15#include "llvm/DebugInfo/MSF/MSFCommon.h"
16#include "llvm/DebugInfo/MSF/MappedBlockStream.h"
Zachary Turner67c56012017-04-27 16:11:19 +000017#include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptor.h"
Zachary Turnerea4e6072017-03-15 22:18:53 +000018#include "llvm/DebugInfo/PDB/Native/RawConstants.h"
19#include "llvm/DebugInfo/PDB/Native/RawError.h"
20#include "llvm/Support/BinaryItemStream.h"
21#include "llvm/Support/BinaryStreamWriter.h"
22#include "llvm/Support/COFF.h"
23
24using namespace llvm;
25using namespace llvm::codeview;
26using namespace llvm::msf;
27using namespace llvm::pdb;
28
29namespace llvm {
30template <> struct BinaryItemTraits<CVSymbol> {
31 static size_t length(const CVSymbol &Item) { return Item.RecordData.size(); }
32
33 static ArrayRef<uint8_t> bytes(const CVSymbol &Item) {
34 return Item.RecordData;
35 }
36};
37}
38
Zachary Turner8a2ebfb2017-05-01 23:27:42 +000039static uint32_t calculateDiSymbolStreamSize(uint32_t SymbolByteSize,
40 uint32_t C13Size) {
Zachary Turnerea4e6072017-03-15 22:18:53 +000041 uint32_t Size = sizeof(uint32_t); // Signature
42 Size += SymbolByteSize; // Symbol Data
Zachary Turner8a2ebfb2017-05-01 23:27:42 +000043 Size += 0; // TODO: Layout.C11Bytes
44 Size += C13Size; // C13 Debug Info Size
Zachary Turnerea4e6072017-03-15 22:18:53 +000045 Size += sizeof(uint32_t); // GlobalRefs substream size (always 0)
46 Size += 0; // GlobalRefs substream bytes
47 return Size;
48}
49
Zachary Turner67c56012017-04-27 16:11:19 +000050DbiModuleDescriptorBuilder::DbiModuleDescriptorBuilder(StringRef ModuleName,
51 uint32_t ModIndex,
52 msf::MSFBuilder &Msf)
Zachary Turnerea4e6072017-03-15 22:18:53 +000053 : MSF(Msf), ModuleName(ModuleName) {
54 Layout.Mod = ModIndex;
55}
56
Zachary Turner8a2ebfb2017-05-01 23:27:42 +000057DbiModuleDescriptorBuilder::~DbiModuleDescriptorBuilder() {}
58
Zachary Turner67c56012017-04-27 16:11:19 +000059uint16_t DbiModuleDescriptorBuilder::getStreamIndex() const {
60 return Layout.ModDiStream;
61}
Zachary Turnerea4e6072017-03-15 22:18:53 +000062
Zachary Turner67c56012017-04-27 16:11:19 +000063void DbiModuleDescriptorBuilder::setObjFileName(StringRef Name) {
64 ObjFileName = Name;
65}
Zachary Turnerea4e6072017-03-15 22:18:53 +000066
Zachary Turner67c56012017-04-27 16:11:19 +000067void DbiModuleDescriptorBuilder::addSymbol(CVSymbol Symbol) {
Zachary Turnerea4e6072017-03-15 22:18:53 +000068 Symbols.push_back(Symbol);
Zachary Turnerebd3ae82017-06-01 21:52:41 +000069 // Symbols written to a PDB file are required to be 4 byte aligned. The same
70 // is not true of object files.
71 assert(Symbol.length() % alignOf(CodeViewContainer::Pdb) == 0 &&
72 "Invalid Symbol alignment!");
73 SymbolByteSize += Symbol.length();
Zachary Turnerea4e6072017-03-15 22:18:53 +000074}
75
Zachary Turner67c56012017-04-27 16:11:19 +000076void DbiModuleDescriptorBuilder::addSourceFile(StringRef Path) {
Zachary Turnerea4e6072017-03-15 22:18:53 +000077 SourceFiles.push_back(Path);
78}
79
Zachary Turner8a2ebfb2017-05-01 23:27:42 +000080uint32_t DbiModuleDescriptorBuilder::calculateC13DebugInfoSize() const {
81 uint32_t Result = 0;
82 for (const auto &Builder : C13Builders) {
83 assert(Builder && "Empty C13 Fragment Builder!");
84 Result += Builder->calculateSerializedLength();
85 }
86 return Result;
87}
88
Zachary Turner67c56012017-04-27 16:11:19 +000089uint32_t DbiModuleDescriptorBuilder::calculateSerializedLength() const {
Zachary Turnerea4e6072017-03-15 22:18:53 +000090 uint32_t L = sizeof(Layout);
91 uint32_t M = ModuleName.size() + 1;
92 uint32_t O = ObjFileName.size() + 1;
93 return alignTo(L + M + O, sizeof(uint32_t));
94}
95
Zachary Turner59e83892017-05-03 05:34:00 +000096template <typename T> struct Foo {
97 explicit Foo(T &&Answer) : Answer(Answer) {}
98
99 T Answer;
100};
101
102template <typename T> Foo<T> makeFoo(T &&t) { return Foo<T>(std::move(t)); }
103
Zachary Turner67c56012017-04-27 16:11:19 +0000104void DbiModuleDescriptorBuilder::finalize() {
Zachary Turnerea4e6072017-03-15 22:18:53 +0000105 Layout.FileNameOffs = 0; // TODO: Fix this
106 Layout.Flags = 0; // TODO: Fix this
Zachary Turner5b6e4e02017-04-29 01:13:21 +0000107 Layout.C11Bytes = 0;
Zachary Turner8a2ebfb2017-05-01 23:27:42 +0000108 Layout.C13Bytes = calculateC13DebugInfoSize();
Zachary Turnerea4e6072017-03-15 22:18:53 +0000109 (void)Layout.Mod; // Set in constructor
110 (void)Layout.ModDiStream; // Set in finalizeMsfLayout
111 Layout.NumFiles = SourceFiles.size();
112 Layout.PdbFilePathNI = 0;
113 Layout.SrcFileNameNI = 0;
114
115 // This value includes both the signature field as well as the record bytes
116 // from the symbol stream.
117 Layout.SymBytes = SymbolByteSize + sizeof(uint32_t);
118}
119
Zachary Turner67c56012017-04-27 16:11:19 +0000120Error DbiModuleDescriptorBuilder::finalizeMsfLayout() {
Zachary Turnerea4e6072017-03-15 22:18:53 +0000121 this->Layout.ModDiStream = kInvalidStreamIndex;
Zachary Turner8a2ebfb2017-05-01 23:27:42 +0000122 uint32_t C13Size = calculateC13DebugInfoSize();
123 auto ExpectedSN =
124 MSF.addStream(calculateDiSymbolStreamSize(SymbolByteSize, C13Size));
Zachary Turnerea4e6072017-03-15 22:18:53 +0000125 if (!ExpectedSN)
126 return ExpectedSN.takeError();
127 Layout.ModDiStream = *ExpectedSN;
128 return Error::success();
129}
130
Zachary Turner67c56012017-04-27 16:11:19 +0000131Error DbiModuleDescriptorBuilder::commit(BinaryStreamWriter &ModiWriter,
132 const msf::MSFLayout &MsfLayout,
133 WritableBinaryStreamRef MsfBuffer) {
Zachary Turnerea4e6072017-03-15 22:18:53 +0000134 // We write the Modi record to the `ModiWriter`, but we additionally write its
135 // symbol stream to a brand new stream.
136 if (auto EC = ModiWriter.writeObject(Layout))
137 return EC;
138 if (auto EC = ModiWriter.writeCString(ModuleName))
139 return EC;
140 if (auto EC = ModiWriter.writeCString(ObjFileName))
141 return EC;
142 if (auto EC = ModiWriter.padToAlignment(sizeof(uint32_t)))
143 return EC;
144
145 if (Layout.ModDiStream != kInvalidStreamIndex) {
146 auto NS = WritableMappedBlockStream::createIndexedStream(
147 MsfLayout, MsfBuffer, Layout.ModDiStream);
148 WritableBinaryStreamRef Ref(*NS);
149 BinaryStreamWriter SymbolWriter(Ref);
150 // Write the symbols.
151 if (auto EC =
152 SymbolWriter.writeInteger<uint32_t>(COFF::DEBUG_SECTION_MAGIC))
153 return EC;
154 BinaryItemStream<CVSymbol> Records(llvm::support::endianness::little);
155 Records.setItems(Symbols);
156 BinaryStreamRef RecordsRef(Records);
157 if (auto EC = SymbolWriter.writeStreamRef(RecordsRef))
158 return EC;
159 // TODO: Write C11 Line data
Zachary Turnerebd3ae82017-06-01 21:52:41 +0000160 assert(SymbolWriter.getOffset() % alignOf(CodeViewContainer::Pdb) == 0 &&
161 "Invalid debug section alignment!");
Zachary Turner8a2ebfb2017-05-01 23:27:42 +0000162 for (const auto &Builder : C13Builders) {
163 assert(Builder && "Empty C13 Fragment Builder!");
164 if (auto EC = Builder->commit(SymbolWriter))
165 return EC;
166 }
167
Zachary Turnerea4e6072017-03-15 22:18:53 +0000168 // TODO: Figure out what GlobalRefs substream actually is and populate it.
169 if (auto EC = SymbolWriter.writeInteger<uint32_t>(0))
170 return EC;
171 if (SymbolWriter.bytesRemaining() > 0)
172 return make_error<RawError>(raw_error_code::stream_too_long);
173 }
174 return Error::success();
175}
Zachary Turner8a2ebfb2017-05-01 23:27:42 +0000176
Zachary Turneredef1452017-05-02 16:56:09 +0000177void DbiModuleDescriptorBuilder::addC13Fragment(
Zachary Turner8c099fe2017-05-30 16:36:15 +0000178 std::unique_ptr<DebugLinesSubsection> Lines) {
179 DebugLinesSubsection &Frag = *Lines;
Zachary Turner8a2ebfb2017-05-01 23:27:42 +0000180
181 // File Checksums have to come first, so push an empty entry on if this
182 // is the first.
183 if (C13Builders.empty())
184 C13Builders.push_back(nullptr);
185
186 this->LineInfo.push_back(std::move(Lines));
Zachary Turnerebd3ae82017-06-01 21:52:41 +0000187 C13Builders.push_back(llvm::make_unique<DebugSubsectionRecordBuilder>(
188 Frag.kind(), Frag, CodeViewContainer::Pdb));
Zachary Turner8a2ebfb2017-05-01 23:27:42 +0000189}
190
Zachary Turneredef1452017-05-02 16:56:09 +0000191void DbiModuleDescriptorBuilder::addC13Fragment(
Zachary Turner8c099fe2017-05-30 16:36:15 +0000192 std::unique_ptr<codeview::DebugInlineeLinesSubsection> Inlinees) {
193 DebugInlineeLinesSubsection &Frag = *Inlinees;
Zachary Turneredef1452017-05-02 16:56:09 +0000194
195 // File Checksums have to come first, so push an empty entry on if this
196 // is the first.
197 if (C13Builders.empty())
198 C13Builders.push_back(nullptr);
199
200 this->Inlinees.push_back(std::move(Inlinees));
Zachary Turnerebd3ae82017-06-01 21:52:41 +0000201 C13Builders.push_back(llvm::make_unique<DebugSubsectionRecordBuilder>(
202 Frag.kind(), Frag, CodeViewContainer::Pdb));
Zachary Turneredef1452017-05-02 16:56:09 +0000203}
204
Zachary Turner8a2ebfb2017-05-01 23:27:42 +0000205void DbiModuleDescriptorBuilder::setC13FileChecksums(
Zachary Turner8c099fe2017-05-30 16:36:15 +0000206 std::unique_ptr<DebugChecksumsSubsection> Checksums) {
Zachary Turner8a2ebfb2017-05-01 23:27:42 +0000207 assert(!ChecksumInfo && "Can't have more than one checksum info!");
208
209 if (C13Builders.empty())
210 C13Builders.push_back(nullptr);
211
212 ChecksumInfo = std::move(Checksums);
Zachary Turner8c099fe2017-05-30 16:36:15 +0000213 C13Builders[0] = llvm::make_unique<DebugSubsectionRecordBuilder>(
Zachary Turnerebd3ae82017-06-01 21:52:41 +0000214 ChecksumInfo->kind(), *ChecksumInfo, CodeViewContainer::Pdb);
Zachary Turner8a2ebfb2017-05-01 23:27:42 +0000215}