InfoStream.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //===- InfoStream.cpp - PDB Info Stream (Stream 1) Access -------*- 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/InfoStream.h"
  9. #include "llvm/ADT/BitVector.h"
  10. #include "llvm/ADT/SmallVector.h"
  11. #include "llvm/DebugInfo/PDB/Native/RawConstants.h"
  12. #include "llvm/DebugInfo/PDB/Native/RawError.h"
  13. #include "llvm/DebugInfo/PDB/Native/RawTypes.h"
  14. #include "llvm/Support/BinaryStreamReader.h"
  15. using namespace llvm;
  16. using namespace llvm::codeview;
  17. using namespace llvm::msf;
  18. using namespace llvm::pdb;
  19. InfoStream::InfoStream(std::unique_ptr<BinaryStream> Stream)
  20. : Stream(std::move(Stream)), Header(nullptr) {}
  21. Error InfoStream::reload() {
  22. BinaryStreamReader Reader(*Stream);
  23. if (auto EC = Reader.readObject(Header))
  24. return joinErrors(
  25. std::move(EC),
  26. make_error<RawError>(raw_error_code::corrupt_file,
  27. "PDB Stream does not contain a header."));
  28. switch (Header->Version) {
  29. case PdbImplVC70:
  30. case PdbImplVC80:
  31. case PdbImplVC110:
  32. case PdbImplVC140:
  33. break;
  34. default:
  35. return make_error<RawError>(raw_error_code::corrupt_file,
  36. "Unsupported PDB stream version.");
  37. }
  38. uint32_t Offset = Reader.getOffset();
  39. if (auto EC = NamedStreams.load(Reader))
  40. return EC;
  41. uint32_t NewOffset = Reader.getOffset();
  42. NamedStreamMapByteSize = NewOffset - Offset;
  43. Reader.setOffset(Offset);
  44. if (auto EC = Reader.readSubstream(SubNamedStreams, NamedStreamMapByteSize))
  45. return EC;
  46. bool Stop = false;
  47. while (!Stop && !Reader.empty()) {
  48. PdbRaw_FeatureSig Sig;
  49. if (auto EC = Reader.readEnum(Sig))
  50. return EC;
  51. // Since this value comes from a file, it's possible we have some strange
  52. // value which doesn't correspond to any value. We don't want to warn on
  53. // -Wcovered-switch-default in this case, so switch on the integral value
  54. // instead of the enumeration value.
  55. switch (uint32_t(Sig)) {
  56. case uint32_t(PdbRaw_FeatureSig::VC110):
  57. // No other flags for VC110 PDB.
  58. Stop = true;
  59. LLVM_FALLTHROUGH;
  60. case uint32_t(PdbRaw_FeatureSig::VC140):
  61. Features |= PdbFeatureContainsIdStream;
  62. break;
  63. case uint32_t(PdbRaw_FeatureSig::NoTypeMerge):
  64. Features |= PdbFeatureNoTypeMerging;
  65. break;
  66. case uint32_t(PdbRaw_FeatureSig::MinimalDebugInfo):
  67. Features |= PdbFeatureMinimalDebugInfo;
  68. break;
  69. default:
  70. continue;
  71. }
  72. FeatureSignatures.push_back(Sig);
  73. }
  74. return Error::success();
  75. }
  76. uint32_t InfoStream::getStreamSize() const { return Stream->getLength(); }
  77. Expected<uint32_t> InfoStream::getNamedStreamIndex(llvm::StringRef Name) const {
  78. uint32_t Result;
  79. if (!NamedStreams.get(Name, Result))
  80. return make_error<RawError>(raw_error_code::no_stream);
  81. return Result;
  82. }
  83. StringMap<uint32_t> InfoStream::named_streams() const {
  84. return NamedStreams.entries();
  85. }
  86. bool InfoStream::containsIdStream() const {
  87. return !!(Features & PdbFeatureContainsIdStream);
  88. }
  89. PdbRaw_ImplVer InfoStream::getVersion() const {
  90. return static_cast<PdbRaw_ImplVer>(uint32_t(Header->Version));
  91. }
  92. uint32_t InfoStream::getSignature() const {
  93. return uint32_t(Header->Signature);
  94. }
  95. uint32_t InfoStream::getAge() const { return uint32_t(Header->Age); }
  96. GUID InfoStream::getGuid() const { return Header->Guid; }
  97. uint32_t InfoStream::getNamedStreamMapByteSize() const {
  98. return NamedStreamMapByteSize;
  99. }
  100. PdbRaw_Features InfoStream::getFeatures() const { return Features; }
  101. ArrayRef<PdbRaw_FeatureSig> InfoStream::getFeatureSignatures() const {
  102. return FeatureSignatures;
  103. }
  104. const NamedStreamMap &InfoStream::getNamedStreams() const {
  105. return NamedStreams;
  106. }
  107. BinarySubstreamRef InfoStream::getNamedStreamsBuffer() const {
  108. return SubNamedStreams;
  109. }