PublicsStream.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //===- PublicsStream.cpp - PDB Public Symbol Stream -----------------------===//
  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. //
  9. // The data structures defined in this file are based on the reference
  10. // implementation which is available at
  11. // https://github.com/Microsoft/microsoft-pdb/blob/master/PDB/dbi/gsi.h
  12. //
  13. // When you are reading the reference source code, you'd find the
  14. // information below useful.
  15. //
  16. // - ppdb1->m_fMinimalDbgInfo seems to be always true.
  17. // - SMALLBUCKETS macro is defined.
  18. //
  19. // The reference doesn't compile, so I learned just by reading code.
  20. // It's not guaranteed to be correct.
  21. //
  22. //===----------------------------------------------------------------------===//
  23. #include "llvm/DebugInfo/PDB/Native/PublicsStream.h"
  24. #include "llvm/ADT/iterator_range.h"
  25. #include "llvm/DebugInfo/CodeView/SymbolRecord.h"
  26. #include "llvm/DebugInfo/MSF/MappedBlockStream.h"
  27. #include "llvm/DebugInfo/PDB/Native/RawError.h"
  28. #include "llvm/Support/BinaryStreamReader.h"
  29. #include "llvm/Support/Endian.h"
  30. #include "llvm/Support/Error.h"
  31. #include <algorithm>
  32. #include <cstdint>
  33. using namespace llvm;
  34. using namespace llvm::msf;
  35. using namespace llvm::support;
  36. using namespace llvm::pdb;
  37. PublicsStream::PublicsStream(std::unique_ptr<MappedBlockStream> Stream)
  38. : Stream(std::move(Stream)) {}
  39. PublicsStream::~PublicsStream() = default;
  40. uint32_t PublicsStream::getSymHash() const { return Header->SymHash; }
  41. uint16_t PublicsStream::getThunkTableSection() const {
  42. return Header->ISectThunkTable;
  43. }
  44. uint32_t PublicsStream::getThunkTableOffset() const {
  45. return Header->OffThunkTable;
  46. }
  47. // Publics stream contains fixed-size headers and a serialized hash table.
  48. // This implementation is not complete yet. It reads till the end of the
  49. // stream so that we verify the stream is at least not corrupted. However,
  50. // we skip over the hash table which we believe contains information about
  51. // public symbols.
  52. Error PublicsStream::reload() {
  53. BinaryStreamReader Reader(*Stream);
  54. // Check stream size.
  55. if (Reader.bytesRemaining() <
  56. sizeof(PublicsStreamHeader) + sizeof(GSIHashHeader))
  57. return make_error<RawError>(raw_error_code::corrupt_file,
  58. "Publics Stream does not contain a header.");
  59. // Read PSGSIHDR struct.
  60. if (Reader.readObject(Header))
  61. return make_error<RawError>(raw_error_code::corrupt_file,
  62. "Publics Stream does not contain a header.");
  63. // Read the hash table.
  64. if (auto E = PublicsTable.read(Reader))
  65. return E;
  66. // Something called "address map" follows.
  67. uint32_t NumAddressMapEntries = Header->AddrMap / sizeof(uint32_t);
  68. if (auto EC = Reader.readArray(AddressMap, NumAddressMapEntries))
  69. return joinErrors(std::move(EC),
  70. make_error<RawError>(raw_error_code::corrupt_file,
  71. "Could not read an address map."));
  72. // Something called "thunk map" follows.
  73. if (auto EC = Reader.readArray(ThunkMap, Header->NumThunks))
  74. return joinErrors(std::move(EC),
  75. make_error<RawError>(raw_error_code::corrupt_file,
  76. "Could not read a thunk map."));
  77. // Something called "section map" follows.
  78. if (Reader.bytesRemaining() > 0) {
  79. if (auto EC = Reader.readArray(SectionOffsets, Header->NumSections))
  80. return joinErrors(std::move(EC),
  81. make_error<RawError>(raw_error_code::corrupt_file,
  82. "Could not read a section map."));
  83. }
  84. if (Reader.bytesRemaining() > 0)
  85. return make_error<RawError>(raw_error_code::corrupt_file,
  86. "Corrupted publics stream.");
  87. return Error::success();
  88. }