PDBStringTable.cpp 4.5 KB

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