Zachary Turner | 591312c | 2017-05-30 17:13:33 +0000 | [diff] [blame] | 1 | //===- DebugFrameDataSubsection.cpp -----------------------------*- C++ -*-===// |
| 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 | 591312c | 2017-05-30 17:13:33 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "llvm/DebugInfo/CodeView/DebugFrameDataSubsection.h" |
| 10 | #include "llvm/DebugInfo/CodeView/CodeViewError.h" |
| 11 | |
| 12 | using namespace llvm; |
| 13 | using namespace llvm::codeview; |
| 14 | |
| 15 | Error DebugFrameDataSubsectionRef::initialize(BinaryStreamReader Reader) { |
Zachary Turner | 42e7cc1b | 2018-09-11 22:35:01 +0000 | [diff] [blame] | 16 | if (Reader.bytesRemaining() % sizeof(FrameData) != 0) { |
| 17 | if (auto EC = Reader.readObject(RelocPtr)) |
| 18 | return EC; |
| 19 | } |
| 20 | |
Zachary Turner | 591312c | 2017-05-30 17:13:33 +0000 | [diff] [blame] | 21 | if (Reader.bytesRemaining() % sizeof(FrameData) != 0) |
| 22 | return make_error<CodeViewError>(cv_error_code::corrupt_record, |
| 23 | "Invalid frame data record format!"); |
| 24 | |
| 25 | uint32_t Count = Reader.bytesRemaining() / sizeof(FrameData); |
| 26 | if (auto EC = Reader.readArray(Frames, Count)) |
| 27 | return EC; |
| 28 | return Error::success(); |
| 29 | } |
| 30 | |
Zachary Turner | 42e7cc1b | 2018-09-11 22:35:01 +0000 | [diff] [blame] | 31 | Error DebugFrameDataSubsectionRef::initialize(BinaryStreamRef Section) { |
| 32 | BinaryStreamReader Reader(Section); |
| 33 | return initialize(Reader); |
| 34 | } |
| 35 | |
Zachary Turner | 591312c | 2017-05-30 17:13:33 +0000 | [diff] [blame] | 36 | uint32_t DebugFrameDataSubsection::calculateSerializedSize() const { |
Zachary Turner | 42e7cc1b | 2018-09-11 22:35:01 +0000 | [diff] [blame] | 37 | uint32_t Size = sizeof(FrameData) * Frames.size(); |
| 38 | if (IncludeRelocPtr) |
| 39 | Size += sizeof(uint32_t); |
| 40 | return Size; |
Zachary Turner | 591312c | 2017-05-30 17:13:33 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | Error DebugFrameDataSubsection::commit(BinaryStreamWriter &Writer) const { |
Zachary Turner | 42e7cc1b | 2018-09-11 22:35:01 +0000 | [diff] [blame] | 44 | if (IncludeRelocPtr) { |
| 45 | if (auto EC = Writer.writeInteger<uint32_t>(0)) |
| 46 | return EC; |
| 47 | } |
Zachary Turner | 591312c | 2017-05-30 17:13:33 +0000 | [diff] [blame] | 48 | |
Zachary Turner | 42e7cc1b | 2018-09-11 22:35:01 +0000 | [diff] [blame] | 49 | std::vector<FrameData> SortedFrames(Frames.begin(), Frames.end()); |
| 50 | std::sort(SortedFrames.begin(), SortedFrames.end(), |
| 51 | [](const FrameData &LHS, const FrameData &RHS) { |
| 52 | return LHS.RvaStart < RHS.RvaStart; |
| 53 | }); |
| 54 | if (auto EC = Writer.writeArray(makeArrayRef(SortedFrames))) |
Zachary Turner | 591312c | 2017-05-30 17:13:33 +0000 | [diff] [blame] | 55 | return EC; |
| 56 | return Error::success(); |
| 57 | } |
| 58 | |
| 59 | void DebugFrameDataSubsection::addFrameData(const FrameData &Frame) { |
| 60 | Frames.push_back(Frame); |
| 61 | } |