1 //===- DebugSubsectionRecord.cpp -----------------------------*- C++-*-===//
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/DebugSubsection.h"
13 #include "llvm/Support/BinaryStreamReader.h"
16 using namespace llvm::codeview;
18 DebugSubsectionRecord::DebugSubsectionRecord()
19 : Container(CodeViewContainer::ObjectFile),
20 Kind(DebugSubsectionKind::None) {}
22 DebugSubsectionRecord::DebugSubsectionRecord(DebugSubsectionKind Kind,
24 CodeViewContainer Container)
25 : Container(Container), Kind(Kind), Data(Data) {}
27 Error DebugSubsectionRecord::initialize(BinaryStreamRef Stream,
28 DebugSubsectionRecord &Info,
29 CodeViewContainer Container) {
30 const DebugSubsectionHeader *Header;
31 BinaryStreamReader Reader(Stream);
32 if (auto EC = Reader.readObject(Header))
35 DebugSubsectionKind Kind =
36 static_cast<DebugSubsectionKind>(uint32_t(Header->Kind));
37 if (auto EC = Reader.readStreamRef(Info.Data, Header->Length))
39 Info.Container = Container;
41 return Error::success();
44 uint32_t DebugSubsectionRecord::getRecordLength() const {
45 return sizeof(DebugSubsectionHeader) + Data.getLength();
48 DebugSubsectionKind DebugSubsectionRecord::kind() const { return Kind; }
50 BinaryStreamRef DebugSubsectionRecord::getRecordData() const { return Data; }
52 DebugSubsectionRecordBuilder::DebugSubsectionRecordBuilder(
53 std::shared_ptr<DebugSubsection> Subsection, CodeViewContainer Container)
54 : Subsection(std::move(Subsection)), Container(Container) {}
56 uint32_t DebugSubsectionRecordBuilder::calculateSerializedLength() {
57 // The length of the entire subsection is always padded to 4 bytes, regardless
58 // of the container kind.
59 uint32_t Size = sizeof(DebugSubsectionHeader) +
60 alignTo(Subsection->calculateSerializedSize(), 4);
64 Error DebugSubsectionRecordBuilder::commit(BinaryStreamWriter &Writer) const {
65 assert(Writer.getOffset() % alignOf(Container) == 0 &&
66 "Debug Subsection not properly aligned");
68 DebugSubsectionHeader Header;
69 Header.Kind = uint32_t(Subsection->kind());
70 // The value written into the Header's Length field is only padded to the
71 // container's alignment
73 alignTo(Subsection->calculateSerializedSize(), alignOf(Container));
75 if (auto EC = Writer.writeObject(Header))
77 if (auto EC = Subsection->commit(Writer))
79 if (auto EC = Writer.padToAlignment(4))
82 return Error::success();