NamedStreamMap.cpp 4.2 KB

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