Zachary Turner | c6d54da | 2016-09-09 17:46:17 +0000 | [diff] [blame] | 1 | #include "llvm/DebugInfo/PDB/Raw/TpiStreamBuilder.h" |
| 2 | |
| 3 | #include "llvm/DebugInfo/CodeView/TypeIndex.h" |
| 4 | #include "llvm/DebugInfo/CodeView/TypeRecord.h" |
Zachary Turner | 620961d | 2016-09-14 23:00:02 +0000 | [diff] [blame] | 5 | #include "llvm/DebugInfo/MSF/MSFBuilder.h" |
Zachary Turner | c6d54da | 2016-09-09 17:46:17 +0000 | [diff] [blame] | 6 | #include "llvm/DebugInfo/MSF/MappedBlockStream.h" |
| 7 | #include "llvm/DebugInfo/MSF/StreamWriter.h" |
| 8 | #include "llvm/DebugInfo/PDB/Raw/PDBFile.h" |
| 9 | #include "llvm/DebugInfo/PDB/Raw/RawError.h" |
| 10 | #include "llvm/DebugInfo/PDB/Raw/TpiStream.h" |
| 11 | #include "llvm/Support/Allocator.h" |
| 12 | |
| 13 | using namespace llvm; |
| 14 | using namespace llvm::msf; |
| 15 | using namespace llvm::pdb; |
| 16 | using namespace llvm::support; |
| 17 | |
Zachary Turner | 620961d | 2016-09-14 23:00:02 +0000 | [diff] [blame] | 18 | TpiStreamBuilder::TpiStreamBuilder(MSFBuilder &Msf) |
| 19 | : Msf(Msf), Allocator(Msf.getAllocator()), Header(nullptr) {} |
Zachary Turner | c6d54da | 2016-09-09 17:46:17 +0000 | [diff] [blame] | 20 | |
| 21 | TpiStreamBuilder::~TpiStreamBuilder() {} |
| 22 | |
| 23 | void TpiStreamBuilder::setVersionHeader(PdbRaw_TpiVer Version) { |
| 24 | VerHeader = Version; |
| 25 | } |
| 26 | |
| 27 | void TpiStreamBuilder::addTypeRecord(const codeview::CVType &Record) { |
| 28 | TypeRecords.push_back(Record); |
| 29 | TypeRecordStream.setItems(TypeRecords); |
| 30 | } |
| 31 | |
| 32 | Error TpiStreamBuilder::finalize() { |
| 33 | if (Header) |
| 34 | return Error::success(); |
| 35 | |
| 36 | TpiStreamHeader *H = Allocator.Allocate<TpiStreamHeader>(); |
| 37 | |
| 38 | uint32_t Count = TypeRecords.size(); |
Zachary Turner | 620961d | 2016-09-14 23:00:02 +0000 | [diff] [blame] | 39 | uint32_t HashBufferSize = calculateHashBufferSize(); |
Zachary Turner | c6d54da | 2016-09-09 17:46:17 +0000 | [diff] [blame] | 40 | |
| 41 | H->Version = *VerHeader; |
| 42 | H->HeaderSize = sizeof(TpiStreamHeader); |
| 43 | H->TypeIndexBegin = codeview::TypeIndex::FirstNonSimpleIndex; |
| 44 | H->TypeIndexEnd = H->TypeIndexBegin + Count; |
| 45 | H->TypeRecordBytes = TypeRecordStream.getLength(); |
| 46 | |
Zachary Turner | 620961d | 2016-09-14 23:00:02 +0000 | [diff] [blame] | 47 | H->HashStreamIndex = HashStreamIndex; |
Zachary Turner | c6d54da | 2016-09-09 17:46:17 +0000 | [diff] [blame] | 48 | H->HashAuxStreamIndex = kInvalidStreamIndex; |
| 49 | H->HashKeySize = sizeof(ulittle32_t); |
| 50 | H->NumHashBuckets = MinTpiHashBuckets; |
| 51 | |
Zachary Turner | 620961d | 2016-09-14 23:00:02 +0000 | [diff] [blame] | 52 | // Recall that hash values go into a completely different stream identified by |
| 53 | // the `HashStreamIndex` field of the `TpiStreamHeader`. Therefore, the data |
| 54 | // begins at offset 0 of this independent stream. |
| 55 | H->HashValueBuffer.Off = 0; |
| 56 | H->HashValueBuffer.Length = HashBufferSize; |
| 57 | H->HashAdjBuffer.Off = H->HashValueBuffer.Off + H->HashValueBuffer.Length; |
Zachary Turner | c6d54da | 2016-09-09 17:46:17 +0000 | [diff] [blame] | 58 | H->HashAdjBuffer.Length = 0; |
Zachary Turner | 620961d | 2016-09-14 23:00:02 +0000 | [diff] [blame] | 59 | H->IndexOffsetBuffer.Off = H->HashAdjBuffer.Off + H->HashAdjBuffer.Length; |
Zachary Turner | c6d54da | 2016-09-09 17:46:17 +0000 | [diff] [blame] | 60 | H->IndexOffsetBuffer.Length = 0; |
| 61 | |
| 62 | Header = H; |
| 63 | return Error::success(); |
| 64 | } |
| 65 | |
| 66 | uint32_t TpiStreamBuilder::calculateSerializedLength() const { |
Zachary Turner | a6cbfb5 | 2016-09-15 18:22:21 +0000 | [diff] [blame^] | 67 | return sizeof(TpiStreamHeader) + TypeRecordStream.getLength(); |
Zachary Turner | 620961d | 2016-09-14 23:00:02 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | uint32_t TpiStreamBuilder::calculateHashBufferSize() const { |
| 71 | if (TypeRecords.empty() || !TypeRecords[0].Hash.hasValue()) |
| 72 | return 0; |
| 73 | return TypeRecords.size() * sizeof(ulittle32_t); |
| 74 | } |
| 75 | |
| 76 | Error TpiStreamBuilder::finalizeMsfLayout() { |
| 77 | uint32_t Length = calculateSerializedLength(); |
| 78 | if (auto EC = Msf.setStreamSize(StreamTPI, Length)) |
| 79 | return EC; |
| 80 | |
| 81 | uint32_t HashBufferSize = calculateHashBufferSize(); |
| 82 | |
| 83 | if (HashBufferSize == 0) |
| 84 | return Error::success(); |
| 85 | |
| 86 | auto ExpectedIndex = Msf.addStream(HashBufferSize); |
| 87 | if (!ExpectedIndex) |
| 88 | return ExpectedIndex.takeError(); |
| 89 | HashStreamIndex = *ExpectedIndex; |
| 90 | ulittle32_t *H = Allocator.Allocate<ulittle32_t>(TypeRecords.size()); |
| 91 | MutableArrayRef<ulittle32_t> HashBuffer(H, TypeRecords.size()); |
| 92 | for (uint32_t I = 0; I < TypeRecords.size(); ++I) { |
| 93 | HashBuffer[I] = *TypeRecords[I].Hash % MinTpiHashBuckets; |
| 94 | } |
| 95 | ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(HashBuffer.data()), |
| 96 | HashBufferSize); |
| 97 | HashValueStream = llvm::make_unique<ByteStream>(Bytes); |
| 98 | return Error::success(); |
Zachary Turner | c6d54da | 2016-09-09 17:46:17 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | Expected<std::unique_ptr<TpiStream>> |
| 102 | TpiStreamBuilder::build(PDBFile &File, const msf::WritableStream &Buffer) { |
| 103 | if (!VerHeader.hasValue()) |
| 104 | return make_error<RawError>(raw_error_code::unspecified, |
| 105 | "Missing TPI Stream Version"); |
| 106 | if (auto EC = finalize()) |
| 107 | return std::move(EC); |
| 108 | |
| 109 | auto StreamData = MappedBlockStream::createIndexedStream(File.getMsfLayout(), |
| 110 | Buffer, StreamTPI); |
| 111 | auto Tpi = llvm::make_unique<TpiStream>(File, std::move(StreamData)); |
| 112 | Tpi->Header = Header; |
| 113 | Tpi->TypeRecords = VarStreamArray<codeview::CVType>(TypeRecordStream); |
Zachary Turner | 620961d | 2016-09-14 23:00:02 +0000 | [diff] [blame] | 114 | if (HashValueStream) { |
| 115 | Tpi->HashStream = std::move(HashValueStream); |
| 116 | StreamReader HSR(*Tpi->HashStream); |
| 117 | if (auto EC = HSR.readArray(Tpi->HashValues, TypeRecords.size())) |
| 118 | return std::move(EC); |
| 119 | } |
Zachary Turner | c6d54da | 2016-09-09 17:46:17 +0000 | [diff] [blame] | 120 | return std::move(Tpi); |
| 121 | } |
| 122 | |
| 123 | Error TpiStreamBuilder::commit(const msf::MSFLayout &Layout, |
| 124 | const msf::WritableStream &Buffer) { |
| 125 | if (auto EC = finalize()) |
| 126 | return EC; |
| 127 | |
| 128 | auto InfoS = |
| 129 | WritableMappedBlockStream::createIndexedStream(Layout, Buffer, StreamTPI); |
| 130 | |
| 131 | StreamWriter Writer(*InfoS); |
| 132 | if (auto EC = Writer.writeObject(*Header)) |
| 133 | return EC; |
| 134 | |
| 135 | auto RecordArray = VarStreamArray<codeview::CVType>(TypeRecordStream); |
| 136 | if (auto EC = Writer.writeArray(RecordArray)) |
| 137 | return EC; |
| 138 | |
Zachary Turner | 620961d | 2016-09-14 23:00:02 +0000 | [diff] [blame] | 139 | if (HashStreamIndex != kInvalidStreamIndex) { |
| 140 | auto HVS = WritableMappedBlockStream::createIndexedStream(Layout, Buffer, |
| 141 | HashStreamIndex); |
| 142 | StreamWriter HW(*HVS); |
| 143 | if (auto EC = HW.writeStreamRef(*HashValueStream)) |
| 144 | return EC; |
| 145 | } |
| 146 | |
Zachary Turner | c6d54da | 2016-09-09 17:46:17 +0000 | [diff] [blame] | 147 | return Error::success(); |
| 148 | } |