PDBStringTable.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //===- PDBStringTable.cpp - PDB String Table ---------------------*- C++-*-===//
  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/PDBStringTable.h"
  9. #include "llvm/DebugInfo/PDB/Native/Hash.h"
  10. #include "llvm/DebugInfo/PDB/Native/RawError.h"
  11. #include "llvm/DebugInfo/PDB/Native/RawTypes.h"
  12. #include "llvm/Support/BinaryStreamReader.h"
  13. #include "llvm/Support/Endian.h"
  14. using namespace llvm;
  15. using namespace llvm::support;
  16. using namespace llvm::pdb;
  17. uint32_t PDBStringTable::getByteSize() const { return Header->ByteSize; }
  18. uint32_t PDBStringTable::getNameCount() const { return NameCount; }
  19. uint32_t PDBStringTable::getHashVersion() const { return Header->HashVersion; }
  20. uint32_t PDBStringTable::getSignature() const { return Header->Signature; }
  21. Error PDBStringTable::readHeader(BinaryStreamReader &Reader) {
  22. if (auto EC = Reader.readObject(Header))
  23. return EC;
  24. if (Header->Signature != PDBStringTableSignature)
  25. return make_error<RawError>(raw_error_code::corrupt_file,
  26. "Invalid hash table signature");
  27. if (Header->HashVersion != 1 && Header->HashVersion != 2)
  28. return make_error<RawError>(raw_error_code::corrupt_file,
  29. "Unsupported hash version");
  30. assert(Reader.bytesRemaining() == 0);
  31. return Error::success();
  32. }
  33. Error PDBStringTable::readStrings(BinaryStreamReader &Reader) {
  34. BinaryStreamRef Stream;
  35. if (auto EC = Reader.readStreamRef(Stream))
  36. return EC;
  37. if (auto EC = Strings.initialize(Stream)) {
  38. return joinErrors(std::move(EC),
  39. make_error<RawError>(raw_error_code::corrupt_file,
  40. "Invalid hash table byte length"));
  41. }
  42. assert(Reader.bytesRemaining() == 0);
  43. return Error::success();
  44. }
  45. const codeview::DebugStringTableSubsectionRef &
  46. PDBStringTable::getStringTable() const {
  47. return Strings;
  48. }
  49. Error PDBStringTable::readHashTable(BinaryStreamReader &Reader) {
  50. const support::ulittle32_t *HashCount;
  51. if (auto EC = Reader.readObject(HashCount))
  52. return EC;
  53. if (auto EC = Reader.readArray(IDs, *HashCount)) {
  54. return joinErrors(std::move(EC),
  55. make_error<RawError>(raw_error_code::corrupt_file,
  56. "Could not read bucket array"));
  57. }
  58. return Error::success();
  59. }
  60. Error PDBStringTable::readEpilogue(BinaryStreamReader &Reader) {
  61. if (auto EC = Reader.readInteger(NameCount))
  62. return EC;
  63. assert(Reader.bytesRemaining() == 0);
  64. return Error::success();
  65. }
  66. Error PDBStringTable::reload(BinaryStreamReader &Reader) {
  67. BinaryStreamReader SectionReader;
  68. std::tie(SectionReader, Reader) = Reader.split(sizeof(PDBStringTableHeader));
  69. if (auto EC = readHeader(SectionReader))
  70. return EC;
  71. std::tie(SectionReader, Reader) = Reader.split(Header->ByteSize);
  72. if (auto EC = readStrings(SectionReader))
  73. return EC;
  74. // We don't know how long the hash table is until we parse it, so let the
  75. // function responsible for doing that figure it out.
  76. if (auto EC = readHashTable(Reader))
  77. return EC;
  78. std::tie(SectionReader, Reader) = Reader.split(sizeof(uint32_t));
  79. if (auto EC = readEpilogue(SectionReader))
  80. return EC;
  81. assert(Reader.bytesRemaining() == 0);
  82. return Error::success();
  83. }
  84. Expected<StringRef> PDBStringTable::getStringForID(uint32_t ID) const {
  85. return Strings.getString(ID);
  86. }
  87. Expected<uint32_t> PDBStringTable::getIDForString(StringRef Str) const {
  88. uint32_t Hash =
  89. (Header->HashVersion == 1) ? hashStringV1(Str) : hashStringV2(Str);
  90. size_t Count = IDs.size();
  91. uint32_t Start = Hash % Count;
  92. for (size_t I = 0; I < Count; ++I) {
  93. // The hash is just a starting point for the search, but if it
  94. // doesn't work we should find the string no matter what, because
  95. // we iterate the entire array.
  96. uint32_t Index = (Start + I) % Count;
  97. // If we find 0, it means the item isn't in the hash table.
  98. uint32_t ID = IDs[Index];
  99. if (ID == 0)
  100. return make_error<RawError>(raw_error_code::no_entry);
  101. auto ExpectedStr = getStringForID(ID);
  102. if (!ExpectedStr)
  103. return ExpectedStr.takeError();
  104. if (*ExpectedStr == Str)
  105. return ID;
  106. }
  107. return make_error<RawError>(raw_error_code::no_entry);
  108. }
  109. FixedStreamArray<support::ulittle32_t> PDBStringTable::name_ids() const {
  110. return IDs;
  111. }