1 //===- DebugSubsectionRecord.cpp ------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/DebugInfo/CodeView/DebugSubsectionRecord.h"
10 #include "llvm/DebugInfo/CodeView/CodeView.h"
11 #include "llvm/DebugInfo/CodeView/DebugSubsection.h"
12 #include "llvm/Support/BinaryStreamReader.h"
13 #include "llvm/Support/BinaryStreamWriter.h"
14 #include "llvm/Support/Error.h"
15 #include "llvm/Support/MathExtras.h"
21 using namespace llvm::codeview
;
23 DebugSubsectionRecord::DebugSubsectionRecord() = default;
25 DebugSubsectionRecord::DebugSubsectionRecord(DebugSubsectionKind Kind
,
27 CodeViewContainer Container
)
28 : Container(Container
), Kind(Kind
), Data(Data
) {}
30 Error
DebugSubsectionRecord::initialize(BinaryStreamRef Stream
,
31 DebugSubsectionRecord
&Info
,
32 CodeViewContainer Container
) {
33 const DebugSubsectionHeader
*Header
;
34 BinaryStreamReader
Reader(Stream
);
35 if (auto EC
= Reader
.readObject(Header
))
38 DebugSubsectionKind Kind
=
39 static_cast<DebugSubsectionKind
>(uint32_t(Header
->Kind
));
40 if (auto EC
= Reader
.readStreamRef(Info
.Data
, Header
->Length
))
42 Info
.Container
= Container
;
44 return Error::success();
47 uint32_t DebugSubsectionRecord::getRecordLength() const {
48 return sizeof(DebugSubsectionHeader
) + Data
.getLength();
51 DebugSubsectionKind
DebugSubsectionRecord::kind() const { return Kind
; }
53 BinaryStreamRef
DebugSubsectionRecord::getRecordData() const { return Data
; }
55 DebugSubsectionRecordBuilder::DebugSubsectionRecordBuilder(
56 std::shared_ptr
<DebugSubsection
> Subsection
, CodeViewContainer Container
)
57 : Subsection(std::move(Subsection
)), Container(Container
) {}
59 DebugSubsectionRecordBuilder::DebugSubsectionRecordBuilder(
60 const DebugSubsectionRecord
&Contents
, CodeViewContainer Container
)
61 : Contents(Contents
), Container(Container
) {}
63 uint32_t DebugSubsectionRecordBuilder::calculateSerializedLength() {
64 uint32_t DataSize
= Subsection
? Subsection
->calculateSerializedSize()
65 : Contents
.getRecordData().getLength();
66 // The length of the entire subsection is always padded to 4 bytes,
67 // regardless of the container kind.
68 return sizeof(DebugSubsectionHeader
) + alignTo(DataSize
, 4);
71 Error
DebugSubsectionRecordBuilder::commit(BinaryStreamWriter
&Writer
) const {
72 assert(Writer
.getOffset() % alignOf(Container
) == 0 &&
73 "Debug Subsection not properly aligned");
75 DebugSubsectionHeader Header
;
76 Header
.Kind
= uint32_t(Subsection
? Subsection
->kind() : Contents
.kind());
77 // The value written into the Header's Length field is only padded to the
78 // container's alignment
79 uint32_t DataSize
= Subsection
? Subsection
->calculateSerializedSize()
80 : Contents
.getRecordData().getLength();
81 Header
.Length
= alignTo(DataSize
, alignOf(Container
));
83 if (auto EC
= Writer
.writeObject(Header
))
86 if (auto EC
= Subsection
->commit(Writer
))
89 if (auto EC
= Writer
.writeStreamRef(Contents
.getRecordData()))
92 if (auto EC
= Writer
.padToAlignment(4))
95 return Error::success();