SymbolDeserializer.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //===- SymbolDeserializer.h -------------------------------------*- 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. #ifndef LLVM_DEBUGINFO_CODEVIEW_SYMBOLDESERIALIZER_H
  9. #define LLVM_DEBUGINFO_CODEVIEW_SYMBOLDESERIALIZER_H
  10. #include "llvm/ADT/ArrayRef.h"
  11. #include "llvm/DebugInfo/CodeView/SymbolRecord.h"
  12. #include "llvm/DebugInfo/CodeView/SymbolRecordMapping.h"
  13. #include "llvm/DebugInfo/CodeView/SymbolVisitorCallbacks.h"
  14. #include "llvm/DebugInfo/CodeView/SymbolVisitorDelegate.h"
  15. #include "llvm/Support/BinaryByteStream.h"
  16. #include "llvm/Support/BinaryStreamReader.h"
  17. #include "llvm/Support/Error.h"
  18. namespace llvm {
  19. namespace codeview {
  20. class SymbolVisitorDelegate;
  21. class SymbolDeserializer : public SymbolVisitorCallbacks {
  22. struct MappingInfo {
  23. MappingInfo(ArrayRef<uint8_t> RecordData, CodeViewContainer Container)
  24. : Stream(RecordData, llvm::support::little), Reader(Stream),
  25. Mapping(Reader, Container) {}
  26. BinaryByteStream Stream;
  27. BinaryStreamReader Reader;
  28. SymbolRecordMapping Mapping;
  29. };
  30. public:
  31. template <typename T> static Error deserializeAs(CVSymbol Symbol, T &Record) {
  32. // If we're just deserializing one record, then don't worry about alignment
  33. // as there's nothing that comes after.
  34. SymbolDeserializer S(nullptr, CodeViewContainer::ObjectFile);
  35. if (auto EC = S.visitSymbolBegin(Symbol))
  36. return EC;
  37. if (auto EC = S.visitKnownRecord(Symbol, Record))
  38. return EC;
  39. if (auto EC = S.visitSymbolEnd(Symbol))
  40. return EC;
  41. return Error::success();
  42. }
  43. template <typename T> static Expected<T> deserializeAs(CVSymbol Symbol) {
  44. T Record(static_cast<SymbolRecordKind>(Symbol.kind()));
  45. if (auto EC = deserializeAs<T>(Symbol, Record))
  46. return std::move(EC);
  47. return Record;
  48. }
  49. explicit SymbolDeserializer(SymbolVisitorDelegate *Delegate,
  50. CodeViewContainer Container)
  51. : Delegate(Delegate), Container(Container) {}
  52. Error visitSymbolBegin(CVSymbol &Record, uint32_t Offset) override {
  53. return visitSymbolBegin(Record);
  54. }
  55. Error visitSymbolBegin(CVSymbol &Record) override {
  56. assert(!Mapping && "Already in a symbol mapping!");
  57. Mapping = std::make_unique<MappingInfo>(Record.content(), Container);
  58. return Mapping->Mapping.visitSymbolBegin(Record);
  59. }
  60. Error visitSymbolEnd(CVSymbol &Record) override {
  61. assert(Mapping && "Not in a symbol mapping!");
  62. auto EC = Mapping->Mapping.visitSymbolEnd(Record);
  63. Mapping.reset();
  64. return EC;
  65. }
  66. #define SYMBOL_RECORD(EnumName, EnumVal, Name) \
  67. Error visitKnownRecord(CVSymbol &CVR, Name &Record) override { \
  68. return visitKnownRecordImpl(CVR, Record); \
  69. }
  70. #define SYMBOL_RECORD_ALIAS(EnumName, EnumVal, Name, AliasName)
  71. #include "llvm/DebugInfo/CodeView/CodeViewSymbols.def"
  72. private:
  73. template <typename T> Error visitKnownRecordImpl(CVSymbol &CVR, T &Record) {
  74. Record.RecordOffset =
  75. Delegate ? Delegate->getRecordOffset(Mapping->Reader) : 0;
  76. if (auto EC = Mapping->Mapping.visitKnownRecord(CVR, Record))
  77. return EC;
  78. return Error::success();
  79. }
  80. SymbolVisitorDelegate *Delegate;
  81. CodeViewContainer Container;
  82. std::unique_ptr<MappingInfo> Mapping;
  83. };
  84. }
  85. }
  86. #endif