TpiStreamBuilder.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. //===- TpiStreamBuilder.cpp - -------------------------------------------===//
  2. //
  3. // 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
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "llvm/DebugInfo/PDB/Native/TpiStreamBuilder.h"
  9. #include "llvm/ADT/ArrayRef.h"
  10. #include "llvm/ADT/STLExtras.h"
  11. #include "llvm/DebugInfo/CodeView/RecordSerialization.h"
  12. #include "llvm/DebugInfo/CodeView/TypeIndex.h"
  13. #include "llvm/DebugInfo/MSF/MSFBuilder.h"
  14. #include "llvm/DebugInfo/MSF/MappedBlockStream.h"
  15. #include "llvm/DebugInfo/PDB/Native/RawTypes.h"
  16. #include "llvm/Support/Allocator.h"
  17. #include "llvm/Support/BinaryByteStream.h"
  18. #include "llvm/Support/BinaryStreamWriter.h"
  19. #include "llvm/Support/Endian.h"
  20. #include "llvm/Support/Error.h"
  21. #include <algorithm>
  22. #include <cstdint>
  23. #include <numeric>
  24. using namespace llvm;
  25. using namespace llvm::msf;
  26. using namespace llvm::pdb;
  27. using namespace llvm::support;
  28. TpiStreamBuilder::TpiStreamBuilder(MSFBuilder &Msf, uint32_t StreamIdx)
  29. : Msf(Msf), Allocator(Msf.getAllocator()), Header(nullptr), Idx(StreamIdx) {
  30. }
  31. TpiStreamBuilder::~TpiStreamBuilder() = default;
  32. void TpiStreamBuilder::setVersionHeader(PdbRaw_TpiVer Version) {
  33. VerHeader = Version;
  34. }
  35. void TpiStreamBuilder::updateTypeIndexOffsets(ArrayRef<uint16_t> Sizes) {
  36. // If we just crossed an 8KB threshold, add a type index offset.
  37. for (uint16_t Size : Sizes) {
  38. size_t NewSize = TypeRecordBytes + Size;
  39. constexpr size_t EightKB = 8 * 1024;
  40. if (NewSize / EightKB > TypeRecordBytes / EightKB || TypeRecordCount == 0) {
  41. TypeIndexOffsets.push_back(
  42. {codeview::TypeIndex(codeview::TypeIndex::FirstNonSimpleIndex +
  43. TypeRecordCount),
  44. ulittle32_t(TypeRecordBytes)});
  45. }
  46. ++TypeRecordCount;
  47. TypeRecordBytes = NewSize;
  48. }
  49. }
  50. void TpiStreamBuilder::addTypeRecord(ArrayRef<uint8_t> Record,
  51. std::optional<uint32_t> Hash) {
  52. assert(((Record.size() & 3) == 0) &&
  53. "The type record's size is not a multiple of 4 bytes which will "
  54. "cause misalignment in the output TPI stream!");
  55. assert(Record.size() <= codeview::MaxRecordLength);
  56. uint16_t OneSize = (uint16_t)Record.size();
  57. updateTypeIndexOffsets(ArrayRef(&OneSize, 1));
  58. TypeRecBuffers.push_back(Record);
  59. // FIXME: Require it.
  60. if (Hash)
  61. TypeHashes.push_back(*Hash);
  62. }
  63. void TpiStreamBuilder::addTypeRecords(ArrayRef<uint8_t> Types,
  64. ArrayRef<uint16_t> Sizes,
  65. ArrayRef<uint32_t> Hashes) {
  66. // Ignore empty type buffers. There should be no hashes or sizes in this case.
  67. if (Types.empty()) {
  68. assert(Sizes.empty() && Hashes.empty());
  69. return;
  70. }
  71. assert(((Types.size() & 3) == 0) &&
  72. "The type record's size is not a multiple of 4 bytes which will "
  73. "cause misalignment in the output TPI stream!");
  74. assert(Sizes.size() == Hashes.size() && "sizes and hashes should be in sync");
  75. assert(std::accumulate(Sizes.begin(), Sizes.end(), 0U) == Types.size() &&
  76. "sizes of type records should sum to the size of the types");
  77. updateTypeIndexOffsets(Sizes);
  78. TypeRecBuffers.push_back(Types);
  79. llvm::append_range(TypeHashes, Hashes);
  80. }
  81. Error TpiStreamBuilder::finalize() {
  82. if (Header)
  83. return Error::success();
  84. TpiStreamHeader *H = Allocator.Allocate<TpiStreamHeader>();
  85. H->Version = VerHeader;
  86. H->HeaderSize = sizeof(TpiStreamHeader);
  87. H->TypeIndexBegin = codeview::TypeIndex::FirstNonSimpleIndex;
  88. H->TypeIndexEnd = H->TypeIndexBegin + TypeRecordCount;
  89. H->TypeRecordBytes = TypeRecordBytes;
  90. H->HashStreamIndex = HashStreamIndex;
  91. H->HashAuxStreamIndex = kInvalidStreamIndex;
  92. H->HashKeySize = sizeof(ulittle32_t);
  93. H->NumHashBuckets = MaxTpiHashBuckets - 1;
  94. // Recall that hash values go into a completely different stream identified by
  95. // the `HashStreamIndex` field of the `TpiStreamHeader`. Therefore, the data
  96. // begins at offset 0 of this independent stream.
  97. H->HashValueBuffer.Off = 0;
  98. H->HashValueBuffer.Length = calculateHashBufferSize();
  99. // We never write any adjustments into our PDBs, so this is usually some
  100. // offset with zero length.
  101. H->HashAdjBuffer.Off = H->HashValueBuffer.Off + H->HashValueBuffer.Length;
  102. H->HashAdjBuffer.Length = 0;
  103. H->IndexOffsetBuffer.Off = H->HashAdjBuffer.Off + H->HashAdjBuffer.Length;
  104. H->IndexOffsetBuffer.Length = calculateIndexOffsetSize();
  105. Header = H;
  106. return Error::success();
  107. }
  108. uint32_t TpiStreamBuilder::calculateSerializedLength() {
  109. return sizeof(TpiStreamHeader) + TypeRecordBytes;
  110. }
  111. uint32_t TpiStreamBuilder::calculateHashBufferSize() const {
  112. assert((TypeRecordCount == TypeHashes.size() || TypeHashes.empty()) &&
  113. "either all or no type records should have hashes");
  114. return TypeHashes.size() * sizeof(ulittle32_t);
  115. }
  116. uint32_t TpiStreamBuilder::calculateIndexOffsetSize() const {
  117. return TypeIndexOffsets.size() * sizeof(codeview::TypeIndexOffset);
  118. }
  119. Error TpiStreamBuilder::finalizeMsfLayout() {
  120. uint32_t Length = calculateSerializedLength();
  121. if (auto EC = Msf.setStreamSize(Idx, Length))
  122. return EC;
  123. uint32_t HashStreamSize =
  124. calculateHashBufferSize() + calculateIndexOffsetSize();
  125. if (HashStreamSize == 0)
  126. return Error::success();
  127. auto ExpectedIndex = Msf.addStream(HashStreamSize);
  128. if (!ExpectedIndex)
  129. return ExpectedIndex.takeError();
  130. HashStreamIndex = *ExpectedIndex;
  131. if (!TypeHashes.empty()) {
  132. ulittle32_t *H = Allocator.Allocate<ulittle32_t>(TypeHashes.size());
  133. MutableArrayRef<ulittle32_t> HashBuffer(H, TypeHashes.size());
  134. for (uint32_t I = 0; I < TypeHashes.size(); ++I) {
  135. HashBuffer[I] = TypeHashes[I] % (MaxTpiHashBuckets - 1);
  136. }
  137. ArrayRef<uint8_t> Bytes(
  138. reinterpret_cast<const uint8_t *>(HashBuffer.data()),
  139. calculateHashBufferSize());
  140. HashValueStream =
  141. std::make_unique<BinaryByteStream>(Bytes, llvm::support::little);
  142. }
  143. return Error::success();
  144. }
  145. Error TpiStreamBuilder::commit(const msf::MSFLayout &Layout,
  146. WritableBinaryStreamRef Buffer) {
  147. if (auto EC = finalize())
  148. return EC;
  149. auto InfoS = WritableMappedBlockStream::createIndexedStream(Layout, Buffer,
  150. Idx, Allocator);
  151. BinaryStreamWriter Writer(*InfoS);
  152. if (auto EC = Writer.writeObject(*Header))
  153. return EC;
  154. for (auto Rec : TypeRecBuffers) {
  155. assert(!Rec.empty() && "Attempting to write an empty type record shifts "
  156. "all offsets in the TPI stream!");
  157. assert(((Rec.size() & 3) == 0) &&
  158. "The type record's size is not a multiple of 4 bytes which will "
  159. "cause misalignment in the output TPI stream!");
  160. if (auto EC = Writer.writeBytes(Rec))
  161. return EC;
  162. }
  163. if (HashStreamIndex != kInvalidStreamIndex) {
  164. auto HVS = WritableMappedBlockStream::createIndexedStream(
  165. Layout, Buffer, HashStreamIndex, Allocator);
  166. BinaryStreamWriter HW(*HVS);
  167. if (HashValueStream) {
  168. if (auto EC = HW.writeStreamRef(*HashValueStream))
  169. return EC;
  170. }
  171. for (auto &IndexOffset : TypeIndexOffsets) {
  172. if (auto EC = HW.writeObject(IndexOffset))
  173. return EC;
  174. }
  175. }
  176. return Error::success();
  177. }