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