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 DebugSubsectionRecordBuilder::DebugSubsectionRecordBuilder( 57 const DebugSubsectionRecord &Contents, CodeViewContainer Container) 58 : Contents(Contents), Container(Container) {} 59 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); 66 } 67 68 Error DebugSubsectionRecordBuilder::commit(BinaryStreamWriter &Writer) const { 69 assert(Writer.getOffset() % alignOf(Container) == 0 && 70 "Debug Subsection not properly aligned"); 71 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)); 79 80 if (auto EC = Writer.writeObject(Header)) 81 return EC; 82 if (Subsection) { 83 if (auto EC = Subsection->commit(Writer)) 84 return EC; 85 } else { 86 if (auto EC = Writer.writeStreamRef(Contents.getRecordData())) 87 return EC; 88 } 89 if (auto EC = Writer.padToAlignment(4)) 90 return EC; 91 92 return Error::success(); 93 } 94