blob: 419734771ccd55581e48411b42193d3dd34b9280 [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Zachary Turnerea4e6072017-03-15 22:18:53 +00006//
7//===----------------------------------------------------------------------===//
8
Zachary Turner67c56012017-04-27 16:11:19 +00009#include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptorBuilder.h"
Zachary Turnerea4e6072017-03-15 22:18:53 +000010
11#include "llvm/ADT/ArrayRef.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000012#include "llvm/BinaryFormat/COFF.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 Turner946204c2017-08-09 04:23:25 +000018#include "llvm/DebugInfo/PDB/Native/GSIStreamBuilder.h"
Zachary Turnerea4e6072017-03-15 22:18:53 +000019#include "llvm/DebugInfo/PDB/Native/RawConstants.h"
20#include "llvm/DebugInfo/PDB/Native/RawError.h"
Zachary Turnerea4e6072017-03-15 22:18:53 +000021#include "llvm/Support/BinaryStreamWriter.h"
Zachary Turnerea4e6072017-03-15 22:18:53 +000022
23using namespace llvm;
24using namespace llvm::codeview;
25using namespace llvm::msf;
26using namespace llvm::pdb;
27
Zachary Turner8a2ebfb2017-05-01 23:27:42 +000028static uint32_t calculateDiSymbolStreamSize(uint32_t SymbolByteSize,
29 uint32_t C13Size) {
Zachary Turner1bfb9f42017-06-06 23:54:23 +000030 uint32_t Size = sizeof(uint32_t); // Signature
31 Size += alignTo(SymbolByteSize, 4); // Symbol Data
32 Size += 0; // TODO: Layout.C11Bytes
33 Size += C13Size; // C13 Debug Info Size
34 Size += sizeof(uint32_t); // GlobalRefs substream size (always 0)
35 Size += 0; // GlobalRefs substream bytes
Zachary Turnerea4e6072017-03-15 22:18:53 +000036 return Size;
37}
38
Zachary Turner67c56012017-04-27 16:11:19 +000039DbiModuleDescriptorBuilder::DbiModuleDescriptorBuilder(StringRef ModuleName,
40 uint32_t ModIndex,
41 msf::MSFBuilder &Msf)
Zachary Turnerea4e6072017-03-15 22:18:53 +000042 : MSF(Msf), ModuleName(ModuleName) {
Zachary Turner297b6eb2017-06-20 18:50:55 +000043 ::memset(&Layout, 0, sizeof(Layout));
Zachary Turnerea4e6072017-03-15 22:18:53 +000044 Layout.Mod = ModIndex;
45}
46
Zachary Turner8a2ebfb2017-05-01 23:27:42 +000047DbiModuleDescriptorBuilder::~DbiModuleDescriptorBuilder() {}
48
Zachary Turner67c56012017-04-27 16:11:19 +000049uint16_t DbiModuleDescriptorBuilder::getStreamIndex() const {
50 return Layout.ModDiStream;
51}
Zachary Turnerea4e6072017-03-15 22:18:53 +000052
Zachary Turner67c56012017-04-27 16:11:19 +000053void DbiModuleDescriptorBuilder::setObjFileName(StringRef Name) {
54 ObjFileName = Name;
55}
Zachary Turnerea4e6072017-03-15 22:18:53 +000056
Zachary Turner6c4bfba2017-07-07 05:04:36 +000057void DbiModuleDescriptorBuilder::setPdbFilePathNI(uint32_t NI) {
58 PdbFilePathNI = NI;
59}
60
Zachary Turner194be872018-04-20 18:00:46 +000061void DbiModuleDescriptorBuilder::setFirstSectionContrib(
62 const SectionContrib &SC) {
63 Layout.SC = SC;
64}
65
Zachary Turner67c56012017-04-27 16:11:19 +000066void DbiModuleDescriptorBuilder::addSymbol(CVSymbol Symbol) {
Reid Kleckner291d0152018-11-27 19:00:23 +000067 // Defer to the bulk API. It does the same thing.
68 addSymbolsInBulk(Symbol.data());
69}
70
71void DbiModuleDescriptorBuilder::addSymbolsInBulk(
72 ArrayRef<uint8_t> BulkSymbols) {
73 // Do nothing for empty runs of symbols.
74 if (BulkSymbols.empty())
75 return;
76
77 Symbols.push_back(BulkSymbols);
78 // Symbols written to a PDB file are required to be 4 byte aligned. The same
Zachary Turnerebd3ae82017-06-01 21:52:41 +000079 // is not true of object files.
Reid Kleckner291d0152018-11-27 19:00:23 +000080 assert(BulkSymbols.size() % alignOf(CodeViewContainer::Pdb) == 0 &&
Zachary Turnerebd3ae82017-06-01 21:52:41 +000081 "Invalid Symbol alignment!");
Reid Kleckner291d0152018-11-27 19:00:23 +000082 SymbolByteSize += BulkSymbols.size();
Zachary Turnerea4e6072017-03-15 22:18:53 +000083}
84
Zachary Turner67c56012017-04-27 16:11:19 +000085void DbiModuleDescriptorBuilder::addSourceFile(StringRef Path) {
Zachary Turnerea4e6072017-03-15 22:18:53 +000086 SourceFiles.push_back(Path);
87}
88
Zachary Turner8a2ebfb2017-05-01 23:27:42 +000089uint32_t DbiModuleDescriptorBuilder::calculateC13DebugInfoSize() const {
90 uint32_t Result = 0;
91 for (const auto &Builder : C13Builders) {
92 assert(Builder && "Empty C13 Fragment Builder!");
93 Result += Builder->calculateSerializedLength();
94 }
95 return Result;
96}
97
Zachary Turner67c56012017-04-27 16:11:19 +000098uint32_t DbiModuleDescriptorBuilder::calculateSerializedLength() const {
Zachary Turnerea4e6072017-03-15 22:18:53 +000099 uint32_t L = sizeof(Layout);
100 uint32_t M = ModuleName.size() + 1;
101 uint32_t O = ObjFileName.size() + 1;
102 return alignTo(L + M + O, sizeof(uint32_t));
103}
104
Zachary Turner67c56012017-04-27 16:11:19 +0000105void DbiModuleDescriptorBuilder::finalize() {
Zachary Turnerea4e6072017-03-15 22:18:53 +0000106 Layout.FileNameOffs = 0; // TODO: Fix this
107 Layout.Flags = 0; // TODO: Fix this
Zachary Turner5b6e4e02017-04-29 01:13:21 +0000108 Layout.C11Bytes = 0;
Zachary Turner8a2ebfb2017-05-01 23:27:42 +0000109 Layout.C13Bytes = calculateC13DebugInfoSize();
Zachary Turnerea4e6072017-03-15 22:18:53 +0000110 (void)Layout.Mod; // Set in constructor
111 (void)Layout.ModDiStream; // Set in finalizeMsfLayout
112 Layout.NumFiles = SourceFiles.size();
Zachary Turner6c4bfba2017-07-07 05:04:36 +0000113 Layout.PdbFilePathNI = PdbFilePathNI;
Zachary Turnerea4e6072017-03-15 22:18:53 +0000114 Layout.SrcFileNameNI = 0;
115
116 // This value includes both the signature field as well as the record bytes
117 // from the symbol stream.
Alexandre Ganea4aeea4c2019-03-18 19:13:23 +0000118 Layout.SymBytes =
119 Layout.ModDiStream == kInvalidStreamIndex ? 0 : getNextSymbolOffset();
Zachary Turnerea4e6072017-03-15 22:18:53 +0000120}
121
Zachary Turner67c56012017-04-27 16:11:19 +0000122Error DbiModuleDescriptorBuilder::finalizeMsfLayout() {
Zachary Turnerea4e6072017-03-15 22:18:53 +0000123 this->Layout.ModDiStream = kInvalidStreamIndex;
Zachary Turner8a2ebfb2017-05-01 23:27:42 +0000124 uint32_t C13Size = calculateC13DebugInfoSize();
Alexandre Ganea4aeea4c2019-03-18 19:13:23 +0000125 if (!C13Size && !SymbolByteSize)
126 return Error::success();
Zachary Turner8a2ebfb2017-05-01 23:27:42 +0000127 auto ExpectedSN =
128 MSF.addStream(calculateDiSymbolStreamSize(SymbolByteSize, C13Size));
Zachary Turnerea4e6072017-03-15 22:18:53 +0000129 if (!ExpectedSN)
130 return ExpectedSN.takeError();
131 Layout.ModDiStream = *ExpectedSN;
132 return Error::success();
133}
134
Zachary Turner67c56012017-04-27 16:11:19 +0000135Error DbiModuleDescriptorBuilder::commit(BinaryStreamWriter &ModiWriter,
136 const msf::MSFLayout &MsfLayout,
137 WritableBinaryStreamRef MsfBuffer) {
Zachary Turnerea4e6072017-03-15 22:18:53 +0000138 // We write the Modi record to the `ModiWriter`, but we additionally write its
139 // symbol stream to a brand new stream.
140 if (auto EC = ModiWriter.writeObject(Layout))
141 return EC;
142 if (auto EC = ModiWriter.writeCString(ModuleName))
143 return EC;
144 if (auto EC = ModiWriter.writeCString(ObjFileName))
145 return EC;
146 if (auto EC = ModiWriter.padToAlignment(sizeof(uint32_t)))
147 return EC;
148
149 if (Layout.ModDiStream != kInvalidStreamIndex) {
150 auto NS = WritableMappedBlockStream::createIndexedStream(
Zachary Turner5b74ff32017-06-03 00:33:35 +0000151 MsfLayout, MsfBuffer, Layout.ModDiStream, MSF.getAllocator());
Zachary Turnerea4e6072017-03-15 22:18:53 +0000152 WritableBinaryStreamRef Ref(*NS);
153 BinaryStreamWriter SymbolWriter(Ref);
154 // Write the symbols.
155 if (auto EC =
156 SymbolWriter.writeInteger<uint32_t>(COFF::DEBUG_SECTION_MAGIC))
157 return EC;
Reid Klecknerffba5442018-11-27 19:14:11 +0000158 for (ArrayRef<uint8_t> Syms : Symbols) {
159 if (auto EC = SymbolWriter.writeBytes(Syms))
160 return EC;
161 }
Zachary Turnerebd3ae82017-06-01 21:52:41 +0000162 assert(SymbolWriter.getOffset() % alignOf(CodeViewContainer::Pdb) == 0 &&
163 "Invalid debug section alignment!");
Reid Kleckner291d0152018-11-27 19:00:23 +0000164 // TODO: Write C11 Line data
Zachary Turner8a2ebfb2017-05-01 23:27:42 +0000165 for (const auto &Builder : C13Builders) {
166 assert(Builder && "Empty C13 Fragment Builder!");
167 if (auto EC = Builder->commit(SymbolWriter))
168 return EC;
169 }
170
Zachary Turnerea4e6072017-03-15 22:18:53 +0000171 // TODO: Figure out what GlobalRefs substream actually is and populate it.
172 if (auto EC = SymbolWriter.writeInteger<uint32_t>(0))
173 return EC;
174 if (SymbolWriter.bytesRemaining() > 0)
175 return make_error<RawError>(raw_error_code::stream_too_long);
176 }
177 return Error::success();
178}
Zachary Turner8a2ebfb2017-05-01 23:27:42 +0000179
Zachary Turner92dcdda2017-06-02 19:49:14 +0000180void DbiModuleDescriptorBuilder::addDebugSubsection(
Zachary Turnera8cfc292017-06-14 15:59:27 +0000181 std::shared_ptr<DebugSubsection> Subsection) {
Zachary Turner92dcdda2017-06-02 19:49:14 +0000182 assert(Subsection);
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000183 C13Builders.push_back(std::make_unique<DebugSubsectionRecordBuilder>(
Zachary Turner92dcdda2017-06-02 19:49:14 +0000184 std::move(Subsection), CodeViewContainer::Pdb));
Zachary Turner8a2ebfb2017-05-01 23:27:42 +0000185}
Reid Kleckner44cdb102017-06-19 17:21:45 +0000186
187void DbiModuleDescriptorBuilder::addDebugSubsection(
188 const DebugSubsectionRecord &SubsectionContents) {
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000189 C13Builders.push_back(std::make_unique<DebugSubsectionRecordBuilder>(
Reid Kleckner44cdb102017-06-19 17:21:45 +0000190 SubsectionContents, CodeViewContainer::Pdb));
191}