blob: b97f1e90bcf83ead6e6d58f1e43e5b78fb940e9d [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"
22#include "llvm/Support/BinaryItemStream.h"
23#include "llvm/Support/BinaryStreamWriter.h"
Zachary Turnerea4e6072017-03-15 22:18:53 +000024
25using namespace llvm;
26using namespace llvm::codeview;
27using namespace llvm::msf;
28using namespace llvm::pdb;
29
Zachary Turner8a2ebfb2017-05-01 23:27:42 +000030static uint32_t calculateDiSymbolStreamSize(uint32_t SymbolByteSize,
31 uint32_t C13Size) {
Zachary Turner1bfb9f42017-06-06 23:54:23 +000032 uint32_t Size = sizeof(uint32_t); // Signature
33 Size += alignTo(SymbolByteSize, 4); // Symbol Data
34 Size += 0; // TODO: Layout.C11Bytes
35 Size += C13Size; // C13 Debug Info Size
36 Size += sizeof(uint32_t); // GlobalRefs substream size (always 0)
37 Size += 0; // GlobalRefs substream bytes
Zachary Turnerea4e6072017-03-15 22:18:53 +000038 return Size;
39}
40
Zachary Turner67c56012017-04-27 16:11:19 +000041DbiModuleDescriptorBuilder::DbiModuleDescriptorBuilder(StringRef ModuleName,
42 uint32_t ModIndex,
43 msf::MSFBuilder &Msf)
Zachary Turnerea4e6072017-03-15 22:18:53 +000044 : MSF(Msf), ModuleName(ModuleName) {
Zachary Turner297b6eb2017-06-20 18:50:55 +000045 ::memset(&Layout, 0, sizeof(Layout));
Zachary Turnerea4e6072017-03-15 22:18:53 +000046 Layout.Mod = ModIndex;
47}
48
Zachary Turner8a2ebfb2017-05-01 23:27:42 +000049DbiModuleDescriptorBuilder::~DbiModuleDescriptorBuilder() {}
50
Zachary Turner67c56012017-04-27 16:11:19 +000051uint16_t DbiModuleDescriptorBuilder::getStreamIndex() const {
52 return Layout.ModDiStream;
53}
Zachary Turnerea4e6072017-03-15 22:18:53 +000054
Zachary Turner67c56012017-04-27 16:11:19 +000055void DbiModuleDescriptorBuilder::setObjFileName(StringRef Name) {
56 ObjFileName = Name;
57}
Zachary Turnerea4e6072017-03-15 22:18:53 +000058
Zachary Turner6c4bfba2017-07-07 05:04:36 +000059void DbiModuleDescriptorBuilder::setPdbFilePathNI(uint32_t NI) {
60 PdbFilePathNI = NI;
61}
62
Zachary Turner194be872018-04-20 18:00:46 +000063void DbiModuleDescriptorBuilder::setFirstSectionContrib(
64 const SectionContrib &SC) {
65 Layout.SC = SC;
66}
67
Zachary Turner67c56012017-04-27 16:11:19 +000068void DbiModuleDescriptorBuilder::addSymbol(CVSymbol Symbol) {
Zachary Turnerea4e6072017-03-15 22:18:53 +000069 Symbols.push_back(Symbol);
Zachary Turnerebd3ae82017-06-01 21:52:41 +000070 // Symbols written to a PDB file are required to be 4 byte aligned. The same
71 // is not true of object files.
72 assert(Symbol.length() % alignOf(CodeViewContainer::Pdb) == 0 &&
73 "Invalid Symbol alignment!");
74 SymbolByteSize += Symbol.length();
Zachary Turnerea4e6072017-03-15 22:18:53 +000075}
76
Zachary Turner67c56012017-04-27 16:11:19 +000077void DbiModuleDescriptorBuilder::addSourceFile(StringRef Path) {
Zachary Turnerea4e6072017-03-15 22:18:53 +000078 SourceFiles.push_back(Path);
79}
80
Zachary Turner8a2ebfb2017-05-01 23:27:42 +000081uint32_t DbiModuleDescriptorBuilder::calculateC13DebugInfoSize() const {
82 uint32_t Result = 0;
83 for (const auto &Builder : C13Builders) {
84 assert(Builder && "Empty C13 Fragment Builder!");
85 Result += Builder->calculateSerializedLength();
86 }
87 return Result;
88}
89
Zachary Turner67c56012017-04-27 16:11:19 +000090uint32_t DbiModuleDescriptorBuilder::calculateSerializedLength() const {
Zachary Turnerea4e6072017-03-15 22:18:53 +000091 uint32_t L = sizeof(Layout);
92 uint32_t M = ModuleName.size() + 1;
93 uint32_t O = ObjFileName.size() + 1;
94 return alignTo(L + M + O, sizeof(uint32_t));
95}
96
Zachary Turner67c56012017-04-27 16:11:19 +000097void DbiModuleDescriptorBuilder::finalize() {
Zachary Turnerbee6c222018-04-17 20:06:43 +000098 Layout.SC.Imod = Layout.Mod;
Zachary Turnerea4e6072017-03-15 22:18:53 +000099 Layout.FileNameOffs = 0; // TODO: Fix this
100 Layout.Flags = 0; // TODO: Fix this
Zachary Turner5b6e4e02017-04-29 01:13:21 +0000101 Layout.C11Bytes = 0;
Zachary Turner8a2ebfb2017-05-01 23:27:42 +0000102 Layout.C13Bytes = calculateC13DebugInfoSize();
Zachary Turnerea4e6072017-03-15 22:18:53 +0000103 (void)Layout.Mod; // Set in constructor
104 (void)Layout.ModDiStream; // Set in finalizeMsfLayout
105 Layout.NumFiles = SourceFiles.size();
Zachary Turner6c4bfba2017-07-07 05:04:36 +0000106 Layout.PdbFilePathNI = PdbFilePathNI;
Zachary Turnerea4e6072017-03-15 22:18:53 +0000107 Layout.SrcFileNameNI = 0;
108
109 // This value includes both the signature field as well as the record bytes
110 // from the symbol stream.
111 Layout.SymBytes = SymbolByteSize + sizeof(uint32_t);
112}
113
Zachary Turner67c56012017-04-27 16:11:19 +0000114Error DbiModuleDescriptorBuilder::finalizeMsfLayout() {
Zachary Turnerea4e6072017-03-15 22:18:53 +0000115 this->Layout.ModDiStream = kInvalidStreamIndex;
Zachary Turner8a2ebfb2017-05-01 23:27:42 +0000116 uint32_t C13Size = calculateC13DebugInfoSize();
117 auto ExpectedSN =
118 MSF.addStream(calculateDiSymbolStreamSize(SymbolByteSize, C13Size));
Zachary Turnerea4e6072017-03-15 22:18:53 +0000119 if (!ExpectedSN)
120 return ExpectedSN.takeError();
121 Layout.ModDiStream = *ExpectedSN;
122 return Error::success();
123}
124
Zachary Turner67c56012017-04-27 16:11:19 +0000125Error DbiModuleDescriptorBuilder::commit(BinaryStreamWriter &ModiWriter,
126 const msf::MSFLayout &MsfLayout,
127 WritableBinaryStreamRef MsfBuffer) {
Zachary Turnerea4e6072017-03-15 22:18:53 +0000128 // We write the Modi record to the `ModiWriter`, but we additionally write its
129 // symbol stream to a brand new stream.
130 if (auto EC = ModiWriter.writeObject(Layout))
131 return EC;
132 if (auto EC = ModiWriter.writeCString(ModuleName))
133 return EC;
134 if (auto EC = ModiWriter.writeCString(ObjFileName))
135 return EC;
136 if (auto EC = ModiWriter.padToAlignment(sizeof(uint32_t)))
137 return EC;
138
139 if (Layout.ModDiStream != kInvalidStreamIndex) {
140 auto NS = WritableMappedBlockStream::createIndexedStream(
Zachary Turner5b74ff32017-06-03 00:33:35 +0000141 MsfLayout, MsfBuffer, Layout.ModDiStream, MSF.getAllocator());
Zachary Turnerea4e6072017-03-15 22:18:53 +0000142 WritableBinaryStreamRef Ref(*NS);
143 BinaryStreamWriter SymbolWriter(Ref);
144 // Write the symbols.
145 if (auto EC =
146 SymbolWriter.writeInteger<uint32_t>(COFF::DEBUG_SECTION_MAGIC))
147 return EC;
148 BinaryItemStream<CVSymbol> Records(llvm::support::endianness::little);
149 Records.setItems(Symbols);
150 BinaryStreamRef RecordsRef(Records);
151 if (auto EC = SymbolWriter.writeStreamRef(RecordsRef))
152 return EC;
Zachary Turner1bfb9f42017-06-06 23:54:23 +0000153 if (auto EC = SymbolWriter.padToAlignment(4))
154 return EC;
Zachary Turnerea4e6072017-03-15 22:18:53 +0000155 // TODO: Write C11 Line data
Zachary Turnerebd3ae82017-06-01 21:52:41 +0000156 assert(SymbolWriter.getOffset() % alignOf(CodeViewContainer::Pdb) == 0 &&
157 "Invalid debug section alignment!");
Zachary Turner8a2ebfb2017-05-01 23:27:42 +0000158 for (const auto &Builder : C13Builders) {
159 assert(Builder && "Empty C13 Fragment Builder!");
160 if (auto EC = Builder->commit(SymbolWriter))
161 return EC;
162 }
163
Zachary Turnerea4e6072017-03-15 22:18:53 +0000164 // TODO: Figure out what GlobalRefs substream actually is and populate it.
165 if (auto EC = SymbolWriter.writeInteger<uint32_t>(0))
166 return EC;
167 if (SymbolWriter.bytesRemaining() > 0)
168 return make_error<RawError>(raw_error_code::stream_too_long);
169 }
170 return Error::success();
171}
Zachary Turner8a2ebfb2017-05-01 23:27:42 +0000172
Zachary Turner92dcdda2017-06-02 19:49:14 +0000173void DbiModuleDescriptorBuilder::addDebugSubsection(
Zachary Turnera8cfc292017-06-14 15:59:27 +0000174 std::shared_ptr<DebugSubsection> Subsection) {
Zachary Turner92dcdda2017-06-02 19:49:14 +0000175 assert(Subsection);
Zachary Turnerebd3ae82017-06-01 21:52:41 +0000176 C13Builders.push_back(llvm::make_unique<DebugSubsectionRecordBuilder>(
Zachary Turner92dcdda2017-06-02 19:49:14 +0000177 std::move(Subsection), CodeViewContainer::Pdb));
Zachary Turner8a2ebfb2017-05-01 23:27:42 +0000178}
Reid Kleckner44cdb102017-06-19 17:21:45 +0000179
180void DbiModuleDescriptorBuilder::addDebugSubsection(
181 const DebugSubsectionRecord &SubsectionContents) {
182 C13Builders.push_back(llvm::make_unique<DebugSubsectionRecordBuilder>(
183 SubsectionContents, CodeViewContainer::Pdb));
184}