blob: de9bb42b17987336f85559369a117ef6cb720769 [file] [log] [blame]
Eugene Zelenko4fcfc192017-06-30 23:06:03 +00001//===- SymbolSerializer.cpp -----------------------------------------------===//
Zachary Turner407dec52017-03-13 14:57:45 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Zachary Turner407dec52017-03-13 14:57:45 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/DebugInfo/CodeView/SymbolSerializer.h"
Eugene Zelenko4fcfc192017-06-30 23:06:03 +000010#include "llvm/ADT/ArrayRef.h"
11#include "llvm/DebugInfo/CodeView/SymbolRecord.h"
12#include "llvm/Support/Endian.h"
13#include "llvm/Support/Error.h"
14#include <cassert>
15#include <cstdint>
16#include <cstring>
Zachary Turner407dec52017-03-13 14:57:45 +000017
18using namespace llvm;
19using namespace llvm::codeview;
20
Zachary Turnerebd3ae82017-06-01 21:52:41 +000021SymbolSerializer::SymbolSerializer(BumpPtrAllocator &Allocator,
22 CodeViewContainer Container)
Zachary Turner5641c072017-08-21 20:17:19 +000023 : Storage(Allocator), Stream(RecordBuffer, support::little), Writer(Stream),
Zachary Turnerebd3ae82017-06-01 21:52:41 +000024 Mapping(Writer, Container) {}
Zachary Turner407dec52017-03-13 14:57:45 +000025
26Error SymbolSerializer::visitSymbolBegin(CVSymbol &Record) {
27 assert(!CurrentSymbol.hasValue() && "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
41Error SymbolSerializer::visitSymbolEnd(CVSymbol &Record) {
42 assert(CurrentSymbol.hasValue() && "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}