SymbolDeserializer.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- SymbolDeserializer.h -------------------------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_DEBUGINFO_CODEVIEW_SYMBOLDESERIALIZER_H
  14. #define LLVM_DEBUGINFO_CODEVIEW_SYMBOLDESERIALIZER_H
  15. #include "llvm/ADT/ArrayRef.h"
  16. #include "llvm/DebugInfo/CodeView/SymbolRecord.h"
  17. #include "llvm/DebugInfo/CodeView/SymbolRecordMapping.h"
  18. #include "llvm/DebugInfo/CodeView/SymbolVisitorCallbacks.h"
  19. #include "llvm/DebugInfo/CodeView/SymbolVisitorDelegate.h"
  20. #include "llvm/Support/BinaryByteStream.h"
  21. #include "llvm/Support/BinaryStreamReader.h"
  22. #include "llvm/Support/Error.h"
  23. namespace llvm {
  24. namespace codeview {
  25. class SymbolVisitorDelegate;
  26. class SymbolDeserializer : public SymbolVisitorCallbacks {
  27. struct MappingInfo {
  28. MappingInfo(ArrayRef<uint8_t> RecordData, CodeViewContainer Container)
  29. : Stream(RecordData, llvm::support::little), Reader(Stream),
  30. Mapping(Reader, Container) {}
  31. BinaryByteStream Stream;
  32. BinaryStreamReader Reader;
  33. SymbolRecordMapping Mapping;
  34. };
  35. public:
  36. template <typename T> static Error deserializeAs(CVSymbol Symbol, T &Record) {
  37. // If we're just deserializing one record, then don't worry about alignment
  38. // as there's nothing that comes after.
  39. SymbolDeserializer S(nullptr, CodeViewContainer::ObjectFile);
  40. if (auto EC = S.visitSymbolBegin(Symbol))
  41. return EC;
  42. if (auto EC = S.visitKnownRecord(Symbol, Record))
  43. return EC;
  44. if (auto EC = S.visitSymbolEnd(Symbol))
  45. return EC;
  46. return Error::success();
  47. }
  48. template <typename T> static Expected<T> deserializeAs(CVSymbol Symbol) {
  49. T Record(static_cast<SymbolRecordKind>(Symbol.kind()));
  50. if (auto EC = deserializeAs<T>(Symbol, Record))
  51. return std::move(EC);
  52. return Record;
  53. }
  54. explicit SymbolDeserializer(SymbolVisitorDelegate *Delegate,
  55. CodeViewContainer Container)
  56. : Delegate(Delegate), Container(Container) {}
  57. Error visitSymbolBegin(CVSymbol &Record, uint32_t Offset) override {
  58. return visitSymbolBegin(Record);
  59. }
  60. Error visitSymbolBegin(CVSymbol &Record) override {
  61. assert(!Mapping && "Already in a symbol mapping!");
  62. Mapping = std::make_unique<MappingInfo>(Record.content(), Container);
  63. return Mapping->Mapping.visitSymbolBegin(Record);
  64. }
  65. Error visitSymbolEnd(CVSymbol &Record) override {
  66. assert(Mapping && "Not in a symbol mapping!");
  67. auto EC = Mapping->Mapping.visitSymbolEnd(Record);
  68. Mapping.reset();
  69. return EC;
  70. }
  71. #define SYMBOL_RECORD(EnumName, EnumVal, Name) \
  72. Error visitKnownRecord(CVSymbol &CVR, Name &Record) override { \
  73. return visitKnownRecordImpl(CVR, Record); \
  74. }
  75. #define SYMBOL_RECORD_ALIAS(EnumName, EnumVal, Name, AliasName)
  76. #include "llvm/DebugInfo/CodeView/CodeViewSymbols.def"
  77. private:
  78. template <typename T> Error visitKnownRecordImpl(CVSymbol &CVR, T &Record) {
  79. Record.RecordOffset =
  80. Delegate ? Delegate->getRecordOffset(Mapping->Reader) : 0;
  81. if (auto EC = Mapping->Mapping.visitKnownRecord(CVR, Record))
  82. return EC;
  83. return Error::success();
  84. }
  85. SymbolVisitorDelegate *Delegate;
  86. CodeViewContainer Container;
  87. std::unique_ptr<MappingInfo> Mapping;
  88. };
  89. }
  90. }
  91. #endif
  92. #ifdef __GNUC__
  93. #pragma GCC diagnostic pop
  94. #endif