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