Eugene Zelenko | 4fcfc19 | 2017-06-30 23:06:03 +0000 | [diff] [blame] | 1 | //===- NamedStreamMap.cpp - PDB Named Stream Map --------------------------===// |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 2 | // |
| 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 | |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 10 | #include "llvm/DebugInfo/PDB/Native/NamedStreamMap.h" |
Eugene Zelenko | 570e39a | 2016-11-23 23:16:32 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/StringMap.h" |
| 12 | #include "llvm/ADT/StringRef.h" |
| 13 | #include "llvm/ADT/iterator_range.h" |
Zachary Turner | cafd476 | 2018-02-16 20:46:04 +0000 | [diff] [blame] | 14 | #include "llvm/DebugInfo/PDB/Native/Hash.h" |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 15 | #include "llvm/DebugInfo/PDB/Native/HashTable.h" |
| 16 | #include "llvm/DebugInfo/PDB/Native/RawError.h" |
Zachary Turner | d9dc282 | 2017-03-02 20:52:51 +0000 | [diff] [blame] | 17 | #include "llvm/Support/BinaryStreamReader.h" |
Eugene Zelenko | 4fcfc19 | 2017-06-30 23:06:03 +0000 | [diff] [blame] | 18 | #include "llvm/Support/BinaryStreamRef.h" |
| 19 | #include "llvm/Support/BinaryStreamWriter.h" |
| 20 | #include "llvm/Support/Endian.h" |
Eugene Zelenko | 570e39a | 2016-11-23 23:16:32 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Error.h" |
| 22 | #include <algorithm> |
Eugene Zelenko | 4fcfc19 | 2017-06-30 23:06:03 +0000 | [diff] [blame] | 23 | #include <cassert> |
Eugene Zelenko | 570e39a | 2016-11-23 23:16:32 +0000 | [diff] [blame] | 24 | #include <cstdint> |
Eugene Zelenko | 4fcfc19 | 2017-06-30 23:06:03 +0000 | [diff] [blame] | 25 | #include <tuple> |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 26 | |
| 27 | using namespace llvm; |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 28 | using namespace llvm::pdb; |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 29 | |
Zachary Turner | ebf03f6 | 2018-03-15 17:38:26 +0000 | [diff] [blame] | 30 | NamedStreamMapTraits::NamedStreamMapTraits(NamedStreamMap &NS) : NS(&NS) {} |
Zachary Turner | 1affd80 | 2017-06-25 03:51:42 +0000 | [diff] [blame] | 31 | |
Zachary Turner | ebf03f6 | 2018-03-15 17:38:26 +0000 | [diff] [blame] | 32 | uint16_t NamedStreamMapTraits::hashLookupKey(StringRef S) const { |
| 33 | // In the reference implementation, this uses |
| 34 | // HASH Hasher<ULONG*, USHORT*>::hashPbCb(PB pb, size_t cb, ULONG ulMod). |
| 35 | // Here, the type HASH is a typedef of unsigned short. |
| 36 | // ** It is not a bug that we truncate the result of hashStringV1, in fact |
| 37 | // it is a bug if we do not! ** |
| 38 | return static_cast<uint16_t>(hashStringV1(S)); |
| 39 | } |
| 40 | |
| 41 | StringRef NamedStreamMapTraits::storageKeyToLookupKey(uint32_t Offset) const { |
| 42 | return NS->getString(Offset); |
| 43 | } |
| 44 | |
| 45 | uint32_t NamedStreamMapTraits::lookupKeyToStorageKey(StringRef S) { |
| 46 | return NS->appendStringData(S); |
| 47 | } |
| 48 | |
| 49 | NamedStreamMap::NamedStreamMap() |
| 50 | : HashTraits(*this), OffsetIndexMap(HashTraits) {} |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 51 | |
Zachary Turner | 120faca | 2017-02-27 22:11:43 +0000 | [diff] [blame] | 52 | Error NamedStreamMap::load(BinaryStreamReader &Stream) { |
Zachary Turner | 11036a9 | 2017-01-19 23:31:24 +0000 | [diff] [blame] | 53 | uint32_t StringBufferSize; |
Zachary Turner | 695ed56 | 2017-02-28 00:04:07 +0000 | [diff] [blame] | 54 | if (auto EC = Stream.readInteger(StringBufferSize)) |
David Majnemer | 836937e | 2016-05-27 16:16:56 +0000 | [diff] [blame] | 55 | return joinErrors(std::move(EC), |
| 56 | make_error<RawError>(raw_error_code::corrupt_file, |
Zachary Turner | 11036a9 | 2017-01-19 23:31:24 +0000 | [diff] [blame] | 57 | "Expected string buffer size")); |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 58 | |
Zachary Turner | cafd476 | 2018-02-16 20:46:04 +0000 | [diff] [blame] | 59 | StringRef Buffer; |
| 60 | if (auto EC = Stream.readFixedString(Buffer, StringBufferSize)) |
Zachary Turner | 11036a9 | 2017-01-19 23:31:24 +0000 | [diff] [blame] | 61 | return EC; |
Zachary Turner | cafd476 | 2018-02-16 20:46:04 +0000 | [diff] [blame] | 62 | NamesBuffer.assign(Buffer.begin(), Buffer.end()); |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 63 | |
Zachary Turner | cafd476 | 2018-02-16 20:46:04 +0000 | [diff] [blame] | 64 | return OffsetIndexMap.load(Stream); |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Zachary Turner | 120faca | 2017-02-27 22:11:43 +0000 | [diff] [blame] | 67 | Error NamedStreamMap::commit(BinaryStreamWriter &Writer) const { |
Zachary Turner | 60667ca | 2017-01-20 22:41:40 +0000 | [diff] [blame] | 68 | // The first field is the number of bytes of string data. |
Zachary Turner | cafd476 | 2018-02-16 20:46:04 +0000 | [diff] [blame] | 69 | if (auto EC = Writer.writeInteger<uint32_t>(NamesBuffer.size())) |
Zachary Turner | 60667ca | 2017-01-20 22:41:40 +0000 | [diff] [blame] | 70 | return EC; |
| 71 | |
Zachary Turner | cafd476 | 2018-02-16 20:46:04 +0000 | [diff] [blame] | 72 | // Then the actual string data. |
| 73 | StringRef Data(NamesBuffer.data(), NamesBuffer.size()); |
| 74 | if (auto EC = Writer.writeFixedString(Data)) |
| 75 | return EC; |
Zachary Turner | 60667ca | 2017-01-20 22:41:40 +0000 | [diff] [blame] | 76 | |
| 77 | // And finally the Offset Index map. |
Zachary Turner | cafd476 | 2018-02-16 20:46:04 +0000 | [diff] [blame] | 78 | if (auto EC = OffsetIndexMap.commit(Writer)) |
Zachary Turner | 60667ca | 2017-01-20 22:41:40 +0000 | [diff] [blame] | 79 | return EC; |
| 80 | |
| 81 | return Error::success(); |
| 82 | } |
| 83 | |
Zachary Turner | cafd476 | 2018-02-16 20:46:04 +0000 | [diff] [blame] | 84 | uint32_t NamedStreamMap::calculateSerializedLength() const { |
| 85 | return sizeof(uint32_t) // String data size |
| 86 | + NamesBuffer.size() // String data |
| 87 | + OffsetIndexMap.calculateSerializedLength(); // Offset Index Map |
Zachary Turner | 60667ca | 2017-01-20 22:41:40 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Zachary Turner | cafd476 | 2018-02-16 20:46:04 +0000 | [diff] [blame] | 90 | uint32_t NamedStreamMap::size() const { return OffsetIndexMap.size(); } |
| 91 | |
| 92 | StringRef NamedStreamMap::getString(uint32_t Offset) const { |
| 93 | assert(NamesBuffer.size() > Offset); |
| 94 | return StringRef(NamesBuffer.data() + Offset); |
Zachary Turner | 85ed80b | 2016-05-25 03:43:17 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Zachary Turner | cafd476 | 2018-02-16 20:46:04 +0000 | [diff] [blame] | 97 | uint32_t NamedStreamMap::hashString(uint32_t Offset) const { |
| 98 | return hashStringV1(getString(Offset)); |
| 99 | } |
Zachary Turner | 02278ce | 2017-03-16 20:18:41 +0000 | [diff] [blame] | 100 | |
Zachary Turner | 60667ca | 2017-01-20 22:41:40 +0000 | [diff] [blame] | 101 | bool NamedStreamMap::get(StringRef Stream, uint32_t &StreamNo) const { |
Zachary Turner | ebf03f6 | 2018-03-15 17:38:26 +0000 | [diff] [blame] | 102 | auto Iter = OffsetIndexMap.find_as(Stream); |
Zachary Turner | cafd476 | 2018-02-16 20:46:04 +0000 | [diff] [blame] | 103 | if (Iter == OffsetIndexMap.end()) |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 104 | return false; |
Zachary Turner | cafd476 | 2018-02-16 20:46:04 +0000 | [diff] [blame] | 105 | StreamNo = (*Iter).second; |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 106 | return true; |
| 107 | } |
Zachary Turner | 60667ca | 2017-01-20 22:41:40 +0000 | [diff] [blame] | 108 | |
Zachary Turner | cafd476 | 2018-02-16 20:46:04 +0000 | [diff] [blame] | 109 | StringMap<uint32_t> NamedStreamMap::entries() const { |
| 110 | StringMap<uint32_t> Result; |
| 111 | for (const auto &Entry : OffsetIndexMap) { |
| 112 | StringRef Stream(NamesBuffer.data() + Entry.first); |
| 113 | Result.try_emplace(Stream, Entry.second); |
| 114 | } |
| 115 | return Result; |
Zachary Turner | 60667ca | 2017-01-20 22:41:40 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Zachary Turner | cafd476 | 2018-02-16 20:46:04 +0000 | [diff] [blame] | 118 | uint32_t NamedStreamMap::appendStringData(StringRef S) { |
| 119 | uint32_t Offset = NamesBuffer.size(); |
| 120 | NamesBuffer.insert(NamesBuffer.end(), S.begin(), S.end()); |
| 121 | NamesBuffer.push_back('\0'); |
| 122 | return Offset; |
| 123 | } |
| 124 | |
| 125 | void NamedStreamMap::set(StringRef Stream, uint32_t StreamNo) { |
Zachary Turner | ebf03f6 | 2018-03-15 17:38:26 +0000 | [diff] [blame] | 126 | OffsetIndexMap.set_as(Stream, support::ulittle32_t(StreamNo)); |
Zachary Turner | 60667ca | 2017-01-20 22:41:40 +0000 | [diff] [blame] | 127 | } |