1*0b57cec5SDimitry Andric //===- DebugSubsectionRecord.cpp ------------------------------------------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric
9*0b57cec5SDimitry Andric #include "llvm/DebugInfo/CodeView/DebugSubsectionRecord.h"
10*0b57cec5SDimitry Andric #include "llvm/DebugInfo/CodeView/CodeView.h"
11*0b57cec5SDimitry Andric #include "llvm/DebugInfo/CodeView/DebugSubsection.h"
12*0b57cec5SDimitry Andric #include "llvm/Support/BinaryStreamReader.h"
13*0b57cec5SDimitry Andric #include "llvm/Support/BinaryStreamWriter.h"
14*0b57cec5SDimitry Andric #include "llvm/Support/Error.h"
15*0b57cec5SDimitry Andric #include "llvm/Support/MathExtras.h"
16*0b57cec5SDimitry Andric #include <cassert>
17*0b57cec5SDimitry Andric #include <cstdint>
18*0b57cec5SDimitry Andric
19*0b57cec5SDimitry Andric using namespace llvm;
20*0b57cec5SDimitry Andric using namespace llvm::codeview;
21*0b57cec5SDimitry Andric
22*0b57cec5SDimitry Andric DebugSubsectionRecord::DebugSubsectionRecord() = default;
23*0b57cec5SDimitry Andric
DebugSubsectionRecord(DebugSubsectionKind Kind,BinaryStreamRef Data)24*0b57cec5SDimitry Andric DebugSubsectionRecord::DebugSubsectionRecord(DebugSubsectionKind Kind,
25*0b57cec5SDimitry Andric BinaryStreamRef Data)
26*0b57cec5SDimitry Andric : Kind(Kind), Data(Data) {}
27*0b57cec5SDimitry Andric
initialize(BinaryStreamRef Stream,DebugSubsectionRecord & Info)28*0b57cec5SDimitry Andric Error DebugSubsectionRecord::initialize(BinaryStreamRef Stream,
29*0b57cec5SDimitry Andric DebugSubsectionRecord &Info) {
30*0b57cec5SDimitry Andric const DebugSubsectionHeader *Header;
31*0b57cec5SDimitry Andric BinaryStreamReader Reader(Stream);
32*0b57cec5SDimitry Andric if (auto EC = Reader.readObject(Header))
33*0b57cec5SDimitry Andric return EC;
34*0b57cec5SDimitry Andric
35*0b57cec5SDimitry Andric DebugSubsectionKind Kind =
36*0b57cec5SDimitry Andric static_cast<DebugSubsectionKind>(uint32_t(Header->Kind));
37*0b57cec5SDimitry Andric if (auto EC = Reader.readStreamRef(Info.Data, Header->Length))
38*0b57cec5SDimitry Andric return EC;
39*0b57cec5SDimitry Andric Info.Kind = Kind;
40*0b57cec5SDimitry Andric return Error::success();
41*0b57cec5SDimitry Andric }
42*0b57cec5SDimitry Andric
getRecordLength() const43*0b57cec5SDimitry Andric uint32_t DebugSubsectionRecord::getRecordLength() const {
44*0b57cec5SDimitry Andric return sizeof(DebugSubsectionHeader) + Data.getLength();
45*0b57cec5SDimitry Andric }
46*0b57cec5SDimitry Andric
kind() const47*0b57cec5SDimitry Andric DebugSubsectionKind DebugSubsectionRecord::kind() const { return Kind; }
48*0b57cec5SDimitry Andric
getRecordData() const49*0b57cec5SDimitry Andric BinaryStreamRef DebugSubsectionRecord::getRecordData() const { return Data; }
50*0b57cec5SDimitry Andric
DebugSubsectionRecordBuilder(std::shared_ptr<DebugSubsection> Subsection)51*0b57cec5SDimitry Andric DebugSubsectionRecordBuilder::DebugSubsectionRecordBuilder(
52*0b57cec5SDimitry Andric std::shared_ptr<DebugSubsection> Subsection)
53*0b57cec5SDimitry Andric : Subsection(std::move(Subsection)) {}
54*0b57cec5SDimitry Andric
DebugSubsectionRecordBuilder(const DebugSubsectionRecord & Contents)55*0b57cec5SDimitry Andric DebugSubsectionRecordBuilder::DebugSubsectionRecordBuilder(
56*0b57cec5SDimitry Andric const DebugSubsectionRecord &Contents)
57*0b57cec5SDimitry Andric : Contents(Contents) {}
58*0b57cec5SDimitry Andric
calculateSerializedLength() const59*0b57cec5SDimitry Andric uint32_t DebugSubsectionRecordBuilder::calculateSerializedLength() const {
60*0b57cec5SDimitry Andric uint32_t DataSize = Subsection ? Subsection->calculateSerializedSize()
61*0b57cec5SDimitry Andric : Contents.getRecordData().getLength();
62*0b57cec5SDimitry Andric // The length of the entire subsection is always padded to 4 bytes,
63*0b57cec5SDimitry Andric // regardless of the container kind.
64*0b57cec5SDimitry Andric return sizeof(DebugSubsectionHeader) + alignTo(DataSize, 4);
65*0b57cec5SDimitry Andric }
66*0b57cec5SDimitry Andric
commit(BinaryStreamWriter & Writer,CodeViewContainer Container) const67*0b57cec5SDimitry Andric Error DebugSubsectionRecordBuilder::commit(BinaryStreamWriter &Writer,
68*0b57cec5SDimitry Andric CodeViewContainer Container) const {
69*0b57cec5SDimitry Andric assert(Writer.getOffset() % alignOf(Container) == 0 &&
70*0b57cec5SDimitry Andric "Debug Subsection not properly aligned");
71*0b57cec5SDimitry Andric
72*0b57cec5SDimitry Andric DebugSubsectionHeader Header;
73*0b57cec5SDimitry Andric Header.Kind = uint32_t(Subsection ? Subsection->kind() : Contents.kind());
74*0b57cec5SDimitry Andric // The value written into the Header's Length field is only padded to the
75*0b57cec5SDimitry Andric // container's alignment
76*0b57cec5SDimitry Andric uint32_t DataSize = Subsection ? Subsection->calculateSerializedSize()
77*0b57cec5SDimitry Andric : Contents.getRecordData().getLength();
78*0b57cec5SDimitry Andric Header.Length = alignTo(DataSize, alignOf(Container));
79*0b57cec5SDimitry Andric
80*0b57cec5SDimitry Andric if (auto EC = Writer.writeObject(Header))
81*0b57cec5SDimitry Andric return EC;
82*0b57cec5SDimitry Andric if (Subsection) {
83*0b57cec5SDimitry Andric if (auto EC = Subsection->commit(Writer))
84*0b57cec5SDimitry Andric return EC;
85*0b57cec5SDimitry Andric } else {
86*0b57cec5SDimitry Andric if (auto EC = Writer.writeStreamRef(Contents.getRecordData()))
87*0b57cec5SDimitry Andric return EC;
88*0b57cec5SDimitry Andric }
89*0b57cec5SDimitry Andric if (auto EC = Writer.padToAlignment(4))
90*0b57cec5SDimitry Andric return EC;
91*0b57cec5SDimitry Andric
92*0b57cec5SDimitry Andric return Error::success();
93*0b57cec5SDimitry Andric }
94*0b57cec5SDimitry Andric