TpiStreamBuilder.cpp 7.5 KB

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