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 if (auto EC = Reader.readStreamRef(Info.Data, Header->Length)) 38 return EC; 39 Info.Container = Container; 40 Info.Kind = Kind; 41 return Error::success(); 42 } 43 44 uint32_t DebugSubsectionRecord::getRecordLength() const { 45 return sizeof(DebugSubsectionHeader) + Data.getLength(); 46 } 47 48 DebugSubsectionKind DebugSubsectionRecord::kind() const { return Kind; } 49 50 BinaryStreamRef DebugSubsectionRecord::getRecordData() const { return Data; } 51 52 DebugSubsectionRecordBuilder::DebugSubsectionRecordBuilder( 53 std::shared_ptr<DebugSubsection> Subsection, CodeViewContainer Container) 54 : Subsection(std::move(Subsection)), Container(Container) {} 55 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); 61 return Size; 62 } 63 64 Error DebugSubsectionRecordBuilder::commit(BinaryStreamWriter &Writer) const { 65 assert(Writer.getOffset() % alignOf(Container) == 0 && 66 "Debug Subsection not properly aligned"); 67 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 72 Header.Length = 73 alignTo(Subsection->calculateSerializedSize(), alignOf(Container)); 74 75 if (auto EC = Writer.writeObject(Header)) 76 return EC; 77 if (auto EC = Subsection->commit(Writer)) 78 return EC; 79 if (auto EC = Writer.padToAlignment(4)) 80 return EC; 81 82 return Error::success(); 83 } 84