Eugene Zelenko | 4fcfc19 | 2017-06-30 23:06:03 +0000 | [diff] [blame] | 1 | //===- SymbolSerializer.cpp -----------------------------------------------===// |
Zachary Turner | 407dec5 | 2017-03-13 14:57:45 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Zachary Turner | 407dec5 | 2017-03-13 14:57:45 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "llvm/DebugInfo/CodeView/SymbolSerializer.h" |
Eugene Zelenko | 4fcfc19 | 2017-06-30 23:06:03 +0000 | [diff] [blame] | 10 | #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 Turner | 407dec5 | 2017-03-13 14:57:45 +0000 | [diff] [blame] | 17 | |
| 18 | using namespace llvm; |
| 19 | using namespace llvm::codeview; |
| 20 | |
Zachary Turner | ebd3ae8 | 2017-06-01 21:52:41 +0000 | [diff] [blame] | 21 | SymbolSerializer::SymbolSerializer(BumpPtrAllocator &Allocator, |
| 22 | CodeViewContainer Container) |
Zachary Turner | 5641c07 | 2017-08-21 20:17:19 +0000 | [diff] [blame] | 23 | : Storage(Allocator), Stream(RecordBuffer, support::little), Writer(Stream), |
Zachary Turner | ebd3ae8 | 2017-06-01 21:52:41 +0000 | [diff] [blame] | 24 | Mapping(Writer, Container) {} |
Zachary Turner | 407dec5 | 2017-03-13 14:57:45 +0000 | [diff] [blame] | 25 | |
| 26 | Error 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 | |
| 41 | Error 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 | } |