1 //===- DebugSubsectionRecord.cpp -----------------------------*- C++-*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "llvm/DebugInfo/CodeView/DebugSubsectionRecord.h"
11 #include "llvm/DebugInfo/CodeView/DebugSubsection.h"
12 
13 #include "llvm/Support/BinaryStreamReader.h"
14 
15 using namespace llvm;
16 using namespace llvm::codeview;
17 
18 DebugSubsectionRecord::DebugSubsectionRecord()
19     : Container(CodeViewContainer::ObjectFile),
20       Kind(DebugSubsectionKind::None) {}
21 
22 DebugSubsectionRecord::DebugSubsectionRecord(DebugSubsectionKind Kind,
23                                              BinaryStreamRef Data,
24                                              CodeViewContainer Container)
25     : Container(Container), Kind(Kind), Data(Data) {}
26 
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))
33     return EC;
34 
35   DebugSubsectionKind Kind =
36       static_cast<DebugSubsectionKind>(uint32_t(Header->Kind));
37   switch (Kind) {
38   case DebugSubsectionKind::FileChecksums:
39   case DebugSubsectionKind::Lines:
40   case DebugSubsectionKind::InlineeLines:
41   case DebugSubsectionKind::CrossScopeExports:
42   case DebugSubsectionKind::CrossScopeImports:
43   case DebugSubsectionKind::Symbols:
44   case DebugSubsectionKind::StringTable:
45   case DebugSubsectionKind::FrameData:
46     break;
47   default:
48     llvm_unreachable("Unexpected debug fragment kind!");
49   }
50   if (auto EC = Reader.readStreamRef(Info.Data, Header->Length))
51     return EC;
52   Info.Container = Container;
53   Info.Kind = Kind;
54   return Error::success();
55 }
56 
57 uint32_t DebugSubsectionRecord::getRecordLength() const {
58   return sizeof(DebugSubsectionHeader) + Data.getLength();
59 }
60 
61 DebugSubsectionKind DebugSubsectionRecord::kind() const { return Kind; }
62 
63 BinaryStreamRef DebugSubsectionRecord::getRecordData() const { return Data; }
64 
65 DebugSubsectionRecordBuilder::DebugSubsectionRecordBuilder(
66     std::unique_ptr<DebugSubsection> Subsection, CodeViewContainer Container)
67     : Subsection(std::move(Subsection)), Container(Container) {}
68 
69 uint32_t DebugSubsectionRecordBuilder::calculateSerializedLength() {
70   // The length of the entire subsection is always padded to 4 bytes, regardless
71   // of the container kind.
72   uint32_t Size = sizeof(DebugSubsectionHeader) +
73                   alignTo(Subsection->calculateSerializedSize(), 4);
74   return Size;
75 }
76 
77 Error DebugSubsectionRecordBuilder::commit(BinaryStreamWriter &Writer) const {
78   assert(Writer.getOffset() % alignOf(Container) == 0 &&
79          "Debug Subsection not properly aligned");
80 
81   DebugSubsectionHeader Header;
82   Header.Kind = uint32_t(Subsection->kind());
83   // The value written into the Header's Length field is only padded to the
84   // container's alignment
85   Header.Length =
86       alignTo(Subsection->calculateSerializedSize(), alignOf(Container));
87 
88   if (auto EC = Writer.writeObject(Header))
89     return EC;
90   if (auto EC = Subsection->commit(Writer))
91     return EC;
92   if (auto EC = Writer.padToAlignment(4))
93     return EC;
94 
95   return Error::success();
96 }
97