xref: /freebsd/contrib/llvm-project/llvm/lib/DebugInfo/CodeView/SymbolSerializer.cpp (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
10b57cec5SDimitry Andric //===- SymbolSerializer.cpp -----------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "llvm/DebugInfo/CodeView/SymbolSerializer.h"
100b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h"
110b57cec5SDimitry Andric #include "llvm/Support/Error.h"
1281ad6265SDimitry Andric #include "llvm/Support/ErrorHandling.h"
130b57cec5SDimitry Andric #include <cassert>
140b57cec5SDimitry Andric #include <cstdint>
150b57cec5SDimitry Andric #include <cstring>
160b57cec5SDimitry Andric 
170b57cec5SDimitry Andric using namespace llvm;
180b57cec5SDimitry Andric using namespace llvm::codeview;
190b57cec5SDimitry Andric 
200b57cec5SDimitry Andric SymbolSerializer::SymbolSerializer(BumpPtrAllocator &Allocator,
210b57cec5SDimitry Andric                                    CodeViewContainer Container)
22*5f757f3fSDimitry Andric     : Storage(Allocator), Stream(RecordBuffer, llvm::endianness::little),
23*5f757f3fSDimitry Andric       Writer(Stream), Mapping(Writer, Container) {}
240b57cec5SDimitry Andric 
250b57cec5SDimitry Andric Error SymbolSerializer::visitSymbolBegin(CVSymbol &Record) {
2681ad6265SDimitry Andric   assert(!CurrentSymbol && "Already in a symbol mapping!");
270b57cec5SDimitry Andric 
280b57cec5SDimitry Andric   Writer.setOffset(0);
290b57cec5SDimitry Andric 
300b57cec5SDimitry Andric   if (auto EC = writeRecordPrefix(Record.kind()))
310b57cec5SDimitry Andric     return EC;
320b57cec5SDimitry Andric 
330b57cec5SDimitry Andric   CurrentSymbol = Record.kind();
340b57cec5SDimitry Andric   if (auto EC = Mapping.visitSymbolBegin(Record))
350b57cec5SDimitry Andric     return EC;
360b57cec5SDimitry Andric 
370b57cec5SDimitry Andric   return Error::success();
380b57cec5SDimitry Andric }
390b57cec5SDimitry Andric 
400b57cec5SDimitry Andric Error SymbolSerializer::visitSymbolEnd(CVSymbol &Record) {
4181ad6265SDimitry Andric   assert(CurrentSymbol && "Not in a symbol mapping!");
420b57cec5SDimitry Andric 
430b57cec5SDimitry Andric   if (auto EC = Mapping.visitSymbolEnd(Record))
440b57cec5SDimitry Andric     return EC;
450b57cec5SDimitry Andric 
460b57cec5SDimitry Andric   uint32_t RecordEnd = Writer.getOffset();
470b57cec5SDimitry Andric   uint16_t Length = RecordEnd - 2;
480b57cec5SDimitry Andric   Writer.setOffset(0);
490b57cec5SDimitry Andric   if (auto EC = Writer.writeInteger(Length))
500b57cec5SDimitry Andric     return EC;
510b57cec5SDimitry Andric 
520b57cec5SDimitry Andric   uint8_t *StableStorage = Storage.Allocate<uint8_t>(RecordEnd);
530b57cec5SDimitry Andric   ::memcpy(StableStorage, &RecordBuffer[0], RecordEnd);
540b57cec5SDimitry Andric   Record.RecordData = ArrayRef<uint8_t>(StableStorage, RecordEnd);
550b57cec5SDimitry Andric   CurrentSymbol.reset();
560b57cec5SDimitry Andric 
570b57cec5SDimitry Andric   return Error::success();
580b57cec5SDimitry Andric }
59