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