blob: 12df042c4eb532ee1bc08ca27bdf43c5e161d116 [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 Turner264b5d92017-06-07 03:48:56 +000013#include "llvm/BinaryFormat/COFF.h"
Zachary Turner8c099fe2017-05-30 16:36:15 +000014#include "llvm/DebugInfo/CodeView/DebugSubsectionRecord.h"
Zachary Turnerea4e6072017-03-15 22:18:53 +000015#include "llvm/DebugInfo/MSF/MSFBuilder.h"
16#include "llvm/DebugInfo/MSF/MSFCommon.h"
17#include "llvm/DebugInfo/MSF/MappedBlockStream.h"
Zachary Turner67c56012017-04-27 16:11:19 +000018#include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptor.h"
Zachary Turner946204c2017-08-09 04:23:25 +000019#include "llvm/DebugInfo/PDB/Native/GSIStreamBuilder.h"
Zachary Turnerea4e6072017-03-15 22:18:53 +000020#include "llvm/DebugInfo/PDB/Native/RawConstants.h"
21#include "llvm/DebugInfo/PDB/Native/RawError.h"
Zachary Turnerea4e6072017-03-15 22:18:53 +000022#include "llvm/Support/BinaryStreamWriter.h"
Zachary Turnerea4e6072017-03-15 22:18:53 +000023
24using namespace llvm;
25using namespace llvm::codeview;
26using namespace llvm::msf;
27using namespace llvm::pdb;
28
Zachary Turner8a2ebfb2017-05-01 23:27:42 +000029static uint32_t calculateDiSymbolStreamSize(uint32_t SymbolByteSize,
30 uint32_t C13Size) {
Zachary Turner1bfb9f42017-06-06 23:54:23 +000031 uint32_t Size = sizeof(uint32_t); // Signature
32 Size += alignTo(SymbolByteSize, 4); // Symbol Data
33 Size += 0; // TODO: Layout.C11Bytes
34 Size += C13Size; // C13 Debug Info Size
35 Size += sizeof(uint32_t); // GlobalRefs substream size (always 0)
36 Size += 0; // GlobalRefs substream bytes
Zachary Turnerea4e6072017-03-15 22:18:53 +000037 return Size;
38}
39
Zachary Turner67c56012017-04-27 16:11:19 +000040DbiModuleDescriptorBuilder::DbiModuleDescriptorBuilder(StringRef ModuleName,
41 uint32_t ModIndex,
42 msf::MSFBuilder &Msf)
Zachary Turnerea4e6072017-03-15 22:18:53 +000043 : MSF(Msf), ModuleName(ModuleName) {
Zachary Turner297b6eb2017-06-20 18:50:55 +000044 ::memset(&Layout, 0, sizeof(Layout));
Zachary Turnerea4e6072017-03-15 22:18:53 +000045 Layout.Mod = ModIndex;
46}
47
Zachary Turner8a2ebfb2017-05-01 23:27:42 +000048DbiModuleDescriptorBuilder::~DbiModuleDescriptorBuilder() {}
49
Zachary Turner67c56012017-04-27 16:11:19 +000050uint16_t DbiModuleDescriptorBuilder::getStreamIndex() const {
51 return Layout.ModDiStream;
52}
Zachary Turnerea4e6072017-03-15 22:18:53 +000053
Zachary Turner67c56012017-04-27 16:11:19 +000054void DbiModuleDescriptorBuilder::setObjFileName(StringRef Name) {
55 ObjFileName = Name;
56}
Zachary Turnerea4e6072017-03-15 22:18:53 +000057
Zachary Turner6c4bfba2017-07-07 05:04:36 +000058void DbiModuleDescriptorBuilder::setPdbFilePathNI(uint32_t NI) {
59 PdbFilePathNI = NI;
60}
61
Zachary Turner194be872018-04-20 18:00:46 +000062void DbiModuleDescriptorBuilder::setFirstSectionContrib(
63 const SectionContrib &SC) {
64 Layout.SC = SC;
65}
66
Zachary Turner67c56012017-04-27 16:11:19 +000067void DbiModuleDescriptorBuilder::addSymbol(CVSymbol Symbol) {
Reid Kleckner291d0152018-11-27 19:00:23 +000068 // Defer to the bulk API. It does the same thing.
69 addSymbolsInBulk(Symbol.data());
70}
71
72void DbiModuleDescriptorBuilder::addSymbolsInBulk(
73 ArrayRef<uint8_t> BulkSymbols) {
74 // Do nothing for empty runs of symbols.
75 if (BulkSymbols.empty())
76 return;
77
78 Symbols.push_back(BulkSymbols);
79 // Symbols written to a PDB file are required to be 4 byte aligned. The same
Zachary Turnerebd3ae82017-06-01 21:52:41 +000080 // is not true of object files.
Reid Kleckner291d0152018-11-27 19:00:23 +000081 assert(BulkSymbols.size() % alignOf(CodeViewContainer::Pdb) == 0 &&
Zachary Turnerebd3ae82017-06-01 21:52:41 +000082 "Invalid Symbol alignment!");
Reid Kleckner291d0152018-11-27 19:00:23 +000083 SymbolByteSize += BulkSymbols.size();
Zachary Turnerea4e6072017-03-15 22:18:53 +000084}
85
Zachary Turner67c56012017-04-27 16:11:19 +000086void DbiModuleDescriptorBuilder::addSourceFile(StringRef Path) {
Zachary Turnerea4e6072017-03-15 22:18:53 +000087 SourceFiles.push_back(Path);
88}
89
Zachary Turner8a2ebfb2017-05-01 23:27:42 +000090uint32_t DbiModuleDescriptorBuilder::calculateC13DebugInfoSize() const {
91 uint32_t Result = 0;
92 for (const auto &Builder : C13Builders) {
93 assert(Builder && "Empty C13 Fragment Builder!");
94 Result += Builder->calculateSerializedLength();
95 }
96 return Result;
97}
98
Zachary Turner67c56012017-04-27 16:11:19 +000099uint32_t DbiModuleDescriptorBuilder::calculateSerializedLength() const {
Zachary Turnerea4e6072017-03-15 22:18:53 +0000100 uint32_t L = sizeof(Layout);
101 uint32_t M = ModuleName.size() + 1;
102 uint32_t O = ObjFileName.size() + 1;
103 return alignTo(L + M + O, sizeof(uint32_t));
104}
105
Zachary Turner67c56012017-04-27 16:11:19 +0000106void DbiModuleDescriptorBuilder::finalize() {
Zachary Turnerbee6c222018-04-17 20:06:43 +0000107 Layout.SC.Imod = Layout.Mod;
Zachary Turnerea4e6072017-03-15 22:18:53 +0000108 Layout.FileNameOffs = 0; // TODO: Fix this
109 Layout.Flags = 0; // TODO: Fix this
Zachary Turner5b6e4e02017-04-29 01:13:21 +0000110 Layout.C11Bytes = 0;
Zachary Turner8a2ebfb2017-05-01 23:27:42 +0000111 Layout.C13Bytes = calculateC13DebugInfoSize();
Zachary Turnerea4e6072017-03-15 22:18:53 +0000112 (void)Layout.Mod; // Set in constructor
113 (void)Layout.ModDiStream; // Set in finalizeMsfLayout
114 Layout.NumFiles = SourceFiles.size();
Zachary Turner6c4bfba2017-07-07 05:04:36 +0000115 Layout.PdbFilePathNI = PdbFilePathNI;
Zachary Turnerea4e6072017-03-15 22:18:53 +0000116 Layout.SrcFileNameNI = 0;
117
118 // This value includes both the signature field as well as the record bytes
119 // from the symbol stream.
120 Layout.SymBytes = SymbolByteSize + sizeof(uint32_t);
121}
122
Zachary Turner67c56012017-04-27 16:11:19 +0000123Error DbiModuleDescriptorBuilder::finalizeMsfLayout() {
Zachary Turnerea4e6072017-03-15 22:18:53 +0000124 this->Layout.ModDiStream = kInvalidStreamIndex;
Zachary Turner8a2ebfb2017-05-01 23:27:42 +0000125 uint32_t C13Size = calculateC13DebugInfoSize();
126 auto ExpectedSN =
127 MSF.addStream(calculateDiSymbolStreamSize(SymbolByteSize, C13Size));
Zachary Turnerea4e6072017-03-15 22:18:53 +0000128 if (!ExpectedSN)
129 return ExpectedSN.takeError();
130 Layout.ModDiStream = *ExpectedSN;
131 return Error::success();
132}
133
Zachary Turner67c56012017-04-27 16:11:19 +0000134Error DbiModuleDescriptorBuilder::commit(BinaryStreamWriter &ModiWriter,
135 const msf::MSFLayout &MsfLayout,
136 WritableBinaryStreamRef MsfBuffer) {
Zachary Turnerea4e6072017-03-15 22:18:53 +0000137 // We write the Modi record to the `ModiWriter`, but we additionally write its
138 // symbol stream to a brand new stream.
139 if (auto EC = ModiWriter.writeObject(Layout))
140 return EC;
141 if (auto EC = ModiWriter.writeCString(ModuleName))
142 return EC;
143 if (auto EC = ModiWriter.writeCString(ObjFileName))
144 return EC;
145 if (auto EC = ModiWriter.padToAlignment(sizeof(uint32_t)))
146 return EC;
147
148 if (Layout.ModDiStream != kInvalidStreamIndex) {
149 auto NS = WritableMappedBlockStream::createIndexedStream(
Zachary Turner5b74ff32017-06-03 00:33:35 +0000150 MsfLayout, MsfBuffer, Layout.ModDiStream, MSF.getAllocator());
Zachary Turnerea4e6072017-03-15 22:18:53 +0000151 WritableBinaryStreamRef Ref(*NS);
152 BinaryStreamWriter SymbolWriter(Ref);
153 // Write the symbols.
154 if (auto EC =
155 SymbolWriter.writeInteger<uint32_t>(COFF::DEBUG_SECTION_MAGIC))
156 return EC;
Reid Kleckner291d0152018-11-27 19:00:23 +0000157 for (ArrayRef<uint8_t> Syms : Symbols)
158 SymbolWriter.writeBytes(Syms);
Zachary Turnerebd3ae82017-06-01 21:52:41 +0000159 assert(SymbolWriter.getOffset() % alignOf(CodeViewContainer::Pdb) == 0 &&
160 "Invalid debug section alignment!");
Reid Kleckner291d0152018-11-27 19:00:23 +0000161 // TODO: Write C11 Line data
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 Turner92dcdda2017-06-02 19:49:14 +0000177void DbiModuleDescriptorBuilder::addDebugSubsection(
Zachary Turnera8cfc292017-06-14 15:59:27 +0000178 std::shared_ptr<DebugSubsection> Subsection) {
Zachary Turner92dcdda2017-06-02 19:49:14 +0000179 assert(Subsection);
Zachary Turnerebd3ae82017-06-01 21:52:41 +0000180 C13Builders.push_back(llvm::make_unique<DebugSubsectionRecordBuilder>(
Zachary Turner92dcdda2017-06-02 19:49:14 +0000181 std::move(Subsection), CodeViewContainer::Pdb));
Zachary Turner8a2ebfb2017-05-01 23:27:42 +0000182}
Reid Kleckner44cdb102017-06-19 17:21:45 +0000183
184void DbiModuleDescriptorBuilder::addDebugSubsection(
185 const DebugSubsectionRecord &SubsectionContents) {
186 C13Builders.push_back(llvm::make_unique<DebugSubsectionRecordBuilder>(
187 SubsectionContents, CodeViewContainer::Pdb));
188}