NamedStreamMap.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //===- NamedStreamMap.cpp - PDB Named Stream Map --------------------------===//
  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/NamedStreamMap.h"
  9. #include "llvm/ADT/SparseBitVector.h"
  10. #include "llvm/ADT/StringMap.h"
  11. #include "llvm/ADT/StringRef.h"
  12. #include "llvm/DebugInfo/PDB/Native/Hash.h"
  13. #include "llvm/DebugInfo/PDB/Native/HashTable.h"
  14. #include "llvm/DebugInfo/PDB/Native/RawError.h"
  15. #include "llvm/Support/BinaryStreamReader.h"
  16. #include "llvm/Support/BinaryStreamWriter.h"
  17. #include "llvm/Support/Endian.h"
  18. #include "llvm/Support/Error.h"
  19. #include <algorithm>
  20. #include <cassert>
  21. #include <cstdint>
  22. using namespace llvm;
  23. using namespace llvm::pdb;
  24. NamedStreamMapTraits::NamedStreamMapTraits(NamedStreamMap &NS) : NS(&NS) {}
  25. uint16_t NamedStreamMapTraits::hashLookupKey(StringRef S) const {
  26. // In the reference implementation, this uses
  27. // HASH Hasher<ULONG*, USHORT*>::hashPbCb(PB pb, size_t cb, ULONG ulMod).
  28. // Here, the type HASH is a typedef of unsigned short.
  29. // ** It is not a bug that we truncate the result of hashStringV1, in fact
  30. // it is a bug if we do not! **
  31. // See NMTNI::hash() in the reference implementation.
  32. return static_cast<uint16_t>(hashStringV1(S));
  33. }
  34. StringRef NamedStreamMapTraits::storageKeyToLookupKey(uint32_t Offset) const {
  35. return NS->getString(Offset);
  36. }
  37. uint32_t NamedStreamMapTraits::lookupKeyToStorageKey(StringRef S) {
  38. return NS->appendStringData(S);
  39. }
  40. NamedStreamMap::NamedStreamMap() : HashTraits(*this), OffsetIndexMap(1) {}
  41. Error NamedStreamMap::load(BinaryStreamReader &Stream) {
  42. uint32_t StringBufferSize;
  43. if (auto EC = Stream.readInteger(StringBufferSize))
  44. return joinErrors(std::move(EC),
  45. make_error<RawError>(raw_error_code::corrupt_file,
  46. "Expected string buffer size"));
  47. StringRef Buffer;
  48. if (auto EC = Stream.readFixedString(Buffer, StringBufferSize))
  49. return EC;
  50. NamesBuffer.assign(Buffer.begin(), Buffer.end());
  51. return OffsetIndexMap.load(Stream);
  52. }
  53. Error NamedStreamMap::commit(BinaryStreamWriter &Writer) const {
  54. // The first field is the number of bytes of string data.
  55. if (auto EC = Writer.writeInteger<uint32_t>(NamesBuffer.size()))
  56. return EC;
  57. // Then the actual string data.
  58. StringRef Data(NamesBuffer.data(), NamesBuffer.size());
  59. if (auto EC = Writer.writeFixedString(Data))
  60. return EC;
  61. // And finally the Offset Index map.
  62. if (auto EC = OffsetIndexMap.commit(Writer))
  63. return EC;
  64. return Error::success();
  65. }
  66. uint32_t NamedStreamMap::calculateSerializedLength() const {
  67. return sizeof(uint32_t) // String data size
  68. + NamesBuffer.size() // String data
  69. + OffsetIndexMap.calculateSerializedLength(); // Offset Index Map
  70. }
  71. uint32_t NamedStreamMap::size() const { return OffsetIndexMap.size(); }
  72. StringRef NamedStreamMap::getString(uint32_t Offset) const {
  73. assert(NamesBuffer.size() > Offset);
  74. return StringRef(NamesBuffer.data() + Offset);
  75. }
  76. uint32_t NamedStreamMap::hashString(uint32_t Offset) const {
  77. return hashStringV1(getString(Offset));
  78. }
  79. bool NamedStreamMap::get(StringRef Stream, uint32_t &StreamNo) const {
  80. auto Iter = OffsetIndexMap.find_as(Stream, HashTraits);
  81. if (Iter == OffsetIndexMap.end())
  82. return false;
  83. StreamNo = (*Iter).second;
  84. return true;
  85. }
  86. StringMap<uint32_t> NamedStreamMap::entries() const {
  87. StringMap<uint32_t> Result;
  88. for (const auto &Entry : OffsetIndexMap) {
  89. StringRef Stream(NamesBuffer.data() + Entry.first);
  90. Result.try_emplace(Stream, Entry.second);
  91. }
  92. return Result;
  93. }
  94. uint32_t NamedStreamMap::appendStringData(StringRef S) {
  95. uint32_t Offset = NamesBuffer.size();
  96. llvm::append_range(NamesBuffer, S);
  97. NamesBuffer.push_back('\0');
  98. return Offset;
  99. }
  100. void NamedStreamMap::set(StringRef Stream, uint32_t StreamNo) {
  101. OffsetIndexMap.set_as(Stream, support::ulittle32_t(StreamNo), HashTraits);
  102. }