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     : Kind(DebugSubsectionKind::None) {}
20 
21 DebugSubsectionRecord::DebugSubsectionRecord(DebugSubsectionKind Kind,
22                                              BinaryStreamRef Data)
23     : Kind(Kind), Data(Data) {}
24 
25 Error DebugSubsectionRecord::initialize(BinaryStreamRef Stream,
26                                         DebugSubsectionRecord &Info) {
27   const DebugSubsectionHeader *Header;
28   BinaryStreamReader Reader(Stream);
29   if (auto EC = Reader.readObject(Header))
30     return EC;
31 
32   DebugSubsectionKind Kind =
33       static_cast<DebugSubsectionKind>(uint32_t(Header->Kind));
34   switch (Kind) {
35   case DebugSubsectionKind::FileChecksums:
36   case DebugSubsectionKind::Lines:
37   case DebugSubsectionKind::InlineeLines:
38     break;
39   default:
40     llvm_unreachable("Unexpected debug fragment kind!");
41   }
42   if (auto EC = Reader.readStreamRef(Info.Data, Header->Length))
43     return EC;
44   Info.Kind = Kind;
45   return Error::success();
46 }
47 
48 uint32_t DebugSubsectionRecord::getRecordLength() const {
49   uint32_t Result = sizeof(DebugSubsectionHeader) + Data.getLength();
50   assert(Result % 4 == 0);
51   return Result;
52 }
53 
54 DebugSubsectionKind DebugSubsectionRecord::kind() const { return Kind; }
55 
56 BinaryStreamRef DebugSubsectionRecord::getRecordData() const { return Data; }
57 
58 DebugSubsectionRecordBuilder::DebugSubsectionRecordBuilder(
59     DebugSubsectionKind Kind, DebugSubsection &Frag)
60     : Kind(Kind), Frag(Frag) {}
61 
62 uint32_t DebugSubsectionRecordBuilder::calculateSerializedLength() {
63   uint32_t Size = sizeof(DebugSubsectionHeader) +
64                   alignTo(Frag.calculateSerializedLength(), 4);
65   return Size;
66 }
67 
68 Error DebugSubsectionRecordBuilder::commit(BinaryStreamWriter &Writer) {
69   DebugSubsectionHeader Header;
70   Header.Kind = uint32_t(Kind);
71   Header.Length = calculateSerializedLength() - sizeof(DebugSubsectionHeader);
72 
73   if (auto EC = Writer.writeObject(Header))
74     return EC;
75   if (auto EC = Frag.commit(Writer))
76     return EC;
77   if (auto EC = Writer.padToAlignment(4))
78     return EC;
79 
80   return Error::success();
81 }
82