xref: /llvm-project/llvm/lib/DebugInfo/CodeView/DebugFrameDataSubsection.cpp (revision 42e7cc1b0fdf3428ebd0dfd70a6e4efe162e1cd3)
1 //===- DebugFrameDataSubsection.cpp -----------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "llvm/DebugInfo/CodeView/DebugFrameDataSubsection.h"
11 #include "llvm/DebugInfo/CodeView/CodeViewError.h"
12 
13 using namespace llvm;
14 using namespace llvm::codeview;
15 
16 Error DebugFrameDataSubsectionRef::initialize(BinaryStreamReader Reader) {
17   if (Reader.bytesRemaining() % sizeof(FrameData) != 0) {
18     if (auto EC = Reader.readObject(RelocPtr))
19       return EC;
20   }
21 
22   if (Reader.bytesRemaining() % sizeof(FrameData) != 0)
23     return make_error<CodeViewError>(cv_error_code::corrupt_record,
24                                      "Invalid frame data record format!");
25 
26   uint32_t Count = Reader.bytesRemaining() / sizeof(FrameData);
27   if (auto EC = Reader.readArray(Frames, Count))
28     return EC;
29   return Error::success();
30 }
31 
32 Error DebugFrameDataSubsectionRef::initialize(BinaryStreamRef Section) {
33   BinaryStreamReader Reader(Section);
34   return initialize(Reader);
35 }
36 
37 uint32_t DebugFrameDataSubsection::calculateSerializedSize() const {
38   uint32_t Size = sizeof(FrameData) * Frames.size();
39   if (IncludeRelocPtr)
40     Size += sizeof(uint32_t);
41   return Size;
42 }
43 
44 Error DebugFrameDataSubsection::commit(BinaryStreamWriter &Writer) const {
45   if (IncludeRelocPtr) {
46     if (auto EC = Writer.writeInteger<uint32_t>(0))
47       return EC;
48   }
49 
50   std::vector<FrameData> SortedFrames(Frames.begin(), Frames.end());
51   std::sort(SortedFrames.begin(), SortedFrames.end(),
52             [](const FrameData &LHS, const FrameData &RHS) {
53               return LHS.RvaStart < RHS.RvaStart;
54             });
55   if (auto EC = Writer.writeArray(makeArrayRef(SortedFrames)))
56     return EC;
57   return Error::success();
58 }
59 
60 void DebugFrameDataSubsection::addFrameData(const FrameData &Frame) {
61   Frames.push_back(Frame);
62 }
63