blob: 36f02e5873f14f0faa227151fd7c1f2758637434 [file] [log] [blame]
Eugene Zelenko570e39a2016-11-23 23:16:32 +00001//===- TpiStreamBuilder.cpp - -------------------------------------------===//
2//
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
Eugene Zelenko570e39a2016-11-23 23:16:32 +00006//
7//===----------------------------------------------------------------------===//
Zachary Turnerc6d54da2016-09-09 17:46:17 +00008
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +00009#include "llvm/DebugInfo/PDB/Native/TpiStreamBuilder.h"
Eugene Zelenko570e39a2016-11-23 23:16:32 +000010#include "llvm/ADT/ArrayRef.h"
11#include "llvm/ADT/STLExtras.h"
Zachary Turnerc6d54da2016-09-09 17:46:17 +000012#include "llvm/DebugInfo/CodeView/TypeIndex.h"
13#include "llvm/DebugInfo/CodeView/TypeRecord.h"
Zachary Turner620961d2016-09-14 23:00:02 +000014#include "llvm/DebugInfo/MSF/MSFBuilder.h"
Zachary Turnerc6d54da2016-09-09 17:46:17 +000015#include "llvm/DebugInfo/MSF/MappedBlockStream.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000016#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
17#include "llvm/DebugInfo/PDB/Native/RawError.h"
18#include "llvm/DebugInfo/PDB/Native/RawTypes.h"
Zachary Turnerc6d54da2016-09-09 17:46:17 +000019#include "llvm/Support/Allocator.h"
Zachary Turnerd9dc2822017-03-02 20:52:51 +000020#include "llvm/Support/BinaryByteStream.h"
21#include "llvm/Support/BinaryStreamArray.h"
22#include "llvm/Support/BinaryStreamReader.h"
23#include "llvm/Support/BinaryStreamWriter.h"
Eugene Zelenko570e39a2016-11-23 23:16:32 +000024#include "llvm/Support/Endian.h"
25#include "llvm/Support/Error.h"
26#include <algorithm>
27#include <cstdint>
Zachary Turnerc6d54da2016-09-09 17:46:17 +000028
29using namespace llvm;
30using namespace llvm::msf;
31using namespace llvm::pdb;
32using namespace llvm::support;
33
Zachary Turnerde9ba152016-09-15 18:22:31 +000034TpiStreamBuilder::TpiStreamBuilder(MSFBuilder &Msf, uint32_t StreamIdx)
Reid Kleckner13fc4112017-04-04 00:56:34 +000035 : Msf(Msf), Allocator(Msf.getAllocator()), Header(nullptr), Idx(StreamIdx) {
Zachary Turnerde9ba152016-09-15 18:22:31 +000036}
Zachary Turnerc6d54da2016-09-09 17:46:17 +000037
Eugene Zelenko570e39a2016-11-23 23:16:32 +000038TpiStreamBuilder::~TpiStreamBuilder() = default;
Zachary Turnerc6d54da2016-09-09 17:46:17 +000039
40void TpiStreamBuilder::setVersionHeader(PdbRaw_TpiVer Version) {
41 VerHeader = Version;
42}
43
Reid Kleckner13fc4112017-04-04 00:56:34 +000044void TpiStreamBuilder::addTypeRecord(ArrayRef<uint8_t> Record,
45 Optional<uint32_t> Hash) {
Reid Kleckner6e545ff2017-04-11 16:26:15 +000046 // If we just crossed an 8KB threshold, add a type index offset.
47 size_t NewSize = TypeRecordBytes + Record.size();
48 constexpr size_t EightKB = 8 * 1024;
49 if (NewSize / EightKB > TypeRecordBytes / EightKB || TypeRecords.empty()) {
50 TypeIndexOffsets.push_back(
51 {codeview::TypeIndex(codeview::TypeIndex::FirstNonSimpleIndex +
52 TypeRecords.size()),
53 ulittle32_t(TypeRecordBytes)});
54 }
55 TypeRecordBytes = NewSize;
56
Zachary Turnerc6d54da2016-09-09 17:46:17 +000057 TypeRecords.push_back(Record);
Reid Kleckner13fc4112017-04-04 00:56:34 +000058 if (Hash)
59 TypeHashes.push_back(*Hash);
Zachary Turnerc6d54da2016-09-09 17:46:17 +000060}
61
62Error TpiStreamBuilder::finalize() {
63 if (Header)
64 return Error::success();
65
66 TpiStreamHeader *H = Allocator.Allocate<TpiStreamHeader>();
67
68 uint32_t Count = TypeRecords.size();
69
Zachary Turner8fb441a2017-05-18 23:03:41 +000070 H->Version = VerHeader;
Zachary Turnerc6d54da2016-09-09 17:46:17 +000071 H->HeaderSize = sizeof(TpiStreamHeader);
72 H->TypeIndexBegin = codeview::TypeIndex::FirstNonSimpleIndex;
73 H->TypeIndexEnd = H->TypeIndexBegin + Count;
Reid Kleckner13fc4112017-04-04 00:56:34 +000074 H->TypeRecordBytes = TypeRecordBytes;
Zachary Turnerc6d54da2016-09-09 17:46:17 +000075
Zachary Turner620961d2016-09-14 23:00:02 +000076 H->HashStreamIndex = HashStreamIndex;
Zachary Turnerc6d54da2016-09-09 17:46:17 +000077 H->HashAuxStreamIndex = kInvalidStreamIndex;
78 H->HashKeySize = sizeof(ulittle32_t);
79 H->NumHashBuckets = MinTpiHashBuckets;
80
Zachary Turner620961d2016-09-14 23:00:02 +000081 // Recall that hash values go into a completely different stream identified by
82 // the `HashStreamIndex` field of the `TpiStreamHeader`. Therefore, the data
83 // begins at offset 0 of this independent stream.
84 H->HashValueBuffer.Off = 0;
Reid Kleckner6e545ff2017-04-11 16:26:15 +000085 H->HashValueBuffer.Length = calculateHashBufferSize();
86
87 // We never write any adjustments into our PDBs, so this is usually some
88 // offset with zero length.
Zachary Turner620961d2016-09-14 23:00:02 +000089 H->HashAdjBuffer.Off = H->HashValueBuffer.Off + H->HashValueBuffer.Length;
Zachary Turnerc6d54da2016-09-09 17:46:17 +000090 H->HashAdjBuffer.Length = 0;
Reid Kleckner6e545ff2017-04-11 16:26:15 +000091
Zachary Turner620961d2016-09-14 23:00:02 +000092 H->IndexOffsetBuffer.Off = H->HashAdjBuffer.Off + H->HashAdjBuffer.Length;
Reid Kleckner6e545ff2017-04-11 16:26:15 +000093 H->IndexOffsetBuffer.Length = calculateIndexOffsetSize();
Zachary Turnerc6d54da2016-09-09 17:46:17 +000094
95 Header = H;
96 return Error::success();
97}
98
Zachary Turner120faca2017-02-27 22:11:43 +000099uint32_t TpiStreamBuilder::calculateSerializedLength() {
Reid Kleckner13fc4112017-04-04 00:56:34 +0000100 return sizeof(TpiStreamHeader) + TypeRecordBytes;
Zachary Turner620961d2016-09-14 23:00:02 +0000101}
102
103uint32_t TpiStreamBuilder::calculateHashBufferSize() const {
Reid Kleckner6e545ff2017-04-11 16:26:15 +0000104 assert((TypeRecords.size() == TypeHashes.size() || TypeHashes.empty()) &&
Reid Kleckner13fc4112017-04-04 00:56:34 +0000105 "either all or no type records should have hashes");
106 return TypeHashes.size() * sizeof(ulittle32_t);
Zachary Turner620961d2016-09-14 23:00:02 +0000107}
108
Reid Kleckner6e545ff2017-04-11 16:26:15 +0000109uint32_t TpiStreamBuilder::calculateIndexOffsetSize() const {
Zachary Turnerdd3a7392017-05-12 19:18:12 +0000110 return TypeIndexOffsets.size() * sizeof(codeview::TypeIndexOffset);
Reid Kleckner6e545ff2017-04-11 16:26:15 +0000111}
112
Zachary Turner620961d2016-09-14 23:00:02 +0000113Error TpiStreamBuilder::finalizeMsfLayout() {
114 uint32_t Length = calculateSerializedLength();
Zachary Turnerde9ba152016-09-15 18:22:31 +0000115 if (auto EC = Msf.setStreamSize(Idx, Length))
Zachary Turner620961d2016-09-14 23:00:02 +0000116 return EC;
117
Reid Kleckner6e545ff2017-04-11 16:26:15 +0000118 uint32_t HashStreamSize =
119 calculateHashBufferSize() + calculateIndexOffsetSize();
Zachary Turner620961d2016-09-14 23:00:02 +0000120
Reid Kleckner6e545ff2017-04-11 16:26:15 +0000121 if (HashStreamSize == 0)
Zachary Turner620961d2016-09-14 23:00:02 +0000122 return Error::success();
123
Reid Kleckner6e545ff2017-04-11 16:26:15 +0000124 auto ExpectedIndex = Msf.addStream(HashStreamSize);
Zachary Turner620961d2016-09-14 23:00:02 +0000125 if (!ExpectedIndex)
126 return ExpectedIndex.takeError();
127 HashStreamIndex = *ExpectedIndex;
Reid Kleckner6e545ff2017-04-11 16:26:15 +0000128 if (!TypeHashes.empty()) {
129 ulittle32_t *H = Allocator.Allocate<ulittle32_t>(TypeHashes.size());
130 MutableArrayRef<ulittle32_t> HashBuffer(H, TypeHashes.size());
131 for (uint32_t I = 0; I < TypeHashes.size(); ++I) {
132 HashBuffer[I] = TypeHashes[I] % MinTpiHashBuckets;
133 }
134 ArrayRef<uint8_t> Bytes(
135 reinterpret_cast<const uint8_t *>(HashBuffer.data()),
136 calculateHashBufferSize());
137 HashValueStream =
138 llvm::make_unique<BinaryByteStream>(Bytes, llvm::support::little);
Zachary Turner620961d2016-09-14 23:00:02 +0000139 }
Zachary Turner620961d2016-09-14 23:00:02 +0000140 return Error::success();
Zachary Turnerc6d54da2016-09-09 17:46:17 +0000141}
142
Zachary Turnerc6d54da2016-09-09 17:46:17 +0000143Error TpiStreamBuilder::commit(const msf::MSFLayout &Layout,
Zachary Turner120faca2017-02-27 22:11:43 +0000144 WritableBinaryStreamRef Buffer) {
Zachary Turnerc6d54da2016-09-09 17:46:17 +0000145 if (auto EC = finalize())
146 return EC;
147
Zachary Turner5b74ff32017-06-03 00:33:35 +0000148 auto InfoS = WritableMappedBlockStream::createIndexedStream(Layout, Buffer,
149 Idx, Allocator);
Zachary Turnerc6d54da2016-09-09 17:46:17 +0000150
Zachary Turner120faca2017-02-27 22:11:43 +0000151 BinaryStreamWriter Writer(*InfoS);
Zachary Turnerc6d54da2016-09-09 17:46:17 +0000152 if (auto EC = Writer.writeObject(*Header))
153 return EC;
154
Reid Kleckner13fc4112017-04-04 00:56:34 +0000155 for (auto Rec : TypeRecords)
156 if (auto EC = Writer.writeBytes(Rec))
157 return EC;
Zachary Turnerc6d54da2016-09-09 17:46:17 +0000158
Zachary Turner620961d2016-09-14 23:00:02 +0000159 if (HashStreamIndex != kInvalidStreamIndex) {
Zachary Turner5b74ff32017-06-03 00:33:35 +0000160 auto HVS = WritableMappedBlockStream::createIndexedStream(
161 Layout, Buffer, HashStreamIndex, Allocator);
Zachary Turner120faca2017-02-27 22:11:43 +0000162 BinaryStreamWriter HW(*HVS);
Reid Kleckner6e545ff2017-04-11 16:26:15 +0000163 if (HashValueStream) {
164 if (auto EC = HW.writeStreamRef(*HashValueStream))
165 return EC;
166 }
167
168 for (auto &IndexOffset : TypeIndexOffsets) {
169 if (auto EC = HW.writeObject(IndexOffset))
170 return EC;
171 }
Zachary Turner620961d2016-09-14 23:00:02 +0000172 }
173
Zachary Turnerc6d54da2016-09-09 17:46:17 +0000174 return Error::success();
175}