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 DebugSubsectionRecordBuilder::DebugSubsectionRecordBuilder(
57 const DebugSubsectionRecord &Contents, CodeViewContainer Container)
58 : Contents(Contents), Container(Container) {}
60 uint32_t DebugSubsectionRecordBuilder::calculateSerializedLength() {
61 uint32_t DataSize = Subsection ? Subsection->calculateSerializedSize()
62 : Contents.getRecordData().getLength();
63 // The length of the entire subsection is always padded to 4 bytes,
64 // regardless of the container kind.
65 return sizeof(DebugSubsectionHeader) + alignTo(DataSize, 4);
68 Error DebugSubsectionRecordBuilder::commit(BinaryStreamWriter &Writer) const {
69 assert(Writer.getOffset() % alignOf(Container) == 0 &&
70 "Debug Subsection not properly aligned");
72 DebugSubsectionHeader Header;
73 Header.Kind = uint32_t(Subsection ? Subsection->kind() : Contents.kind());
74 // The value written into the Header's Length field is only padded to the
75 // container's alignment
76 uint32_t DataSize = Subsection ? Subsection->calculateSerializedSize()
77 : Contents.getRecordData().getLength();
78 Header.Length = alignTo(DataSize, alignOf(Container));
80 if (auto EC = Writer.writeObject(Header))
83 if (auto EC = Subsection->commit(Writer))
86 if (auto EC = Writer.writeStreamRef(Contents.getRecordData()))
89 if (auto EC = Writer.padToAlignment(4))
92 return Error::success();