SymbolSerializer.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. //===- SymbolSerializer.cpp -----------------------------------------------===//
  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/CodeView/SymbolSerializer.h"
  9. #include "llvm/ADT/ArrayRef.h"
  10. #include "llvm/DebugInfo/CodeView/SymbolRecord.h"
  11. #include "llvm/Support/Endian.h"
  12. #include "llvm/Support/Error.h"
  13. #include <cassert>
  14. #include <cstdint>
  15. #include <cstring>
  16. using namespace llvm;
  17. using namespace llvm::codeview;
  18. SymbolSerializer::SymbolSerializer(BumpPtrAllocator &Allocator,
  19. CodeViewContainer Container)
  20. : Storage(Allocator), Stream(RecordBuffer, support::little), Writer(Stream),
  21. Mapping(Writer, Container) {}
  22. Error SymbolSerializer::visitSymbolBegin(CVSymbol &Record) {
  23. assert(!CurrentSymbol.hasValue() && "Already in a symbol mapping!");
  24. Writer.setOffset(0);
  25. if (auto EC = writeRecordPrefix(Record.kind()))
  26. return EC;
  27. CurrentSymbol = Record.kind();
  28. if (auto EC = Mapping.visitSymbolBegin(Record))
  29. return EC;
  30. return Error::success();
  31. }
  32. Error SymbolSerializer::visitSymbolEnd(CVSymbol &Record) {
  33. assert(CurrentSymbol.hasValue() && "Not in a symbol mapping!");
  34. if (auto EC = Mapping.visitSymbolEnd(Record))
  35. return EC;
  36. uint32_t RecordEnd = Writer.getOffset();
  37. uint16_t Length = RecordEnd - 2;
  38. Writer.setOffset(0);
  39. if (auto EC = Writer.writeInteger(Length))
  40. return EC;
  41. uint8_t *StableStorage = Storage.Allocate<uint8_t>(RecordEnd);
  42. ::memcpy(StableStorage, &RecordBuffer[0], RecordEnd);
  43. Record.RecordData = ArrayRef<uint8_t>(StableStorage, RecordEnd);
  44. CurrentSymbol.reset();
  45. return Error::success();
  46. }