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