1 //===- DebugSubsectionRecord.cpp ------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/DebugInfo/CodeView/DebugSubsectionRecord.h"
11 #include "llvm/DebugInfo/CodeView/CodeView.h"
12 #include "llvm/DebugInfo/CodeView/DebugSubsection.h"
13 #include "llvm/Support/BinaryStreamReader.h"
14 #include "llvm/Support/BinaryStreamWriter.h"
15 #include "llvm/Support/Error.h"
16 #include "llvm/Support/MathExtras.h"
22 using namespace llvm::codeview
;
24 DebugSubsectionRecord::DebugSubsectionRecord() = default;
26 DebugSubsectionRecord::DebugSubsectionRecord(DebugSubsectionKind Kind
,
28 CodeViewContainer Container
)
29 : Container(Container
), Kind(Kind
), Data(Data
) {}
31 Error
DebugSubsectionRecord::initialize(BinaryStreamRef Stream
,
32 DebugSubsectionRecord
&Info
,
33 CodeViewContainer Container
) {
34 const DebugSubsectionHeader
*Header
;
35 BinaryStreamReader
Reader(Stream
);
36 if (auto EC
= Reader
.readObject(Header
))
39 DebugSubsectionKind Kind
=
40 static_cast<DebugSubsectionKind
>(uint32_t(Header
->Kind
));
41 if (auto EC
= Reader
.readStreamRef(Info
.Data
, Header
->Length
))
43 Info
.Container
= Container
;
45 return Error::success();
48 uint32_t DebugSubsectionRecord::getRecordLength() const {
49 return sizeof(DebugSubsectionHeader
) + Data
.getLength();
52 DebugSubsectionKind
DebugSubsectionRecord::kind() const { return Kind
; }
54 BinaryStreamRef
DebugSubsectionRecord::getRecordData() const { return Data
; }
56 DebugSubsectionRecordBuilder::DebugSubsectionRecordBuilder(
57 std::shared_ptr
<DebugSubsection
> Subsection
, CodeViewContainer Container
)
58 : Subsection(std::move(Subsection
)), Container(Container
) {}
60 DebugSubsectionRecordBuilder::DebugSubsectionRecordBuilder(
61 const DebugSubsectionRecord
&Contents
, CodeViewContainer Container
)
62 : Contents(Contents
), Container(Container
) {}
64 uint32_t DebugSubsectionRecordBuilder::calculateSerializedLength() {
65 uint32_t DataSize
= Subsection
? Subsection
->calculateSerializedSize()
66 : Contents
.getRecordData().getLength();
67 // The length of the entire subsection is always padded to 4 bytes,
68 // regardless of the container kind.
69 return sizeof(DebugSubsectionHeader
) + alignTo(DataSize
, 4);
72 Error
DebugSubsectionRecordBuilder::commit(BinaryStreamWriter
&Writer
) const {
73 assert(Writer
.getOffset() % alignOf(Container
) == 0 &&
74 "Debug Subsection not properly aligned");
76 DebugSubsectionHeader Header
;
77 Header
.Kind
= uint32_t(Subsection
? Subsection
->kind() : Contents
.kind());
78 // The value written into the Header's Length field is only padded to the
79 // container's alignment
80 uint32_t DataSize
= Subsection
? Subsection
->calculateSerializedSize()
81 : Contents
.getRecordData().getLength();
82 Header
.Length
= alignTo(DataSize
, alignOf(Container
));
84 if (auto EC
= Writer
.writeObject(Header
))
87 if (auto EC
= Subsection
->commit(Writer
))
90 if (auto EC
= Writer
.writeStreamRef(Contents
.getRecordData()))
93 if (auto EC
= Writer
.padToAlignment(4))
96 return Error::success();