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 break; 44 default: 45 llvm_unreachable("Unexpected debug fragment kind!"); 46 } 47 if (auto EC = Reader.readStreamRef(Info.Data, Header->Length)) 48 return EC; 49 Info.Container = Container; 50 Info.Kind = Kind; 51 return Error::success(); 52 } 53 54 uint32_t DebugSubsectionRecord::getRecordLength() const { 55 uint32_t Result = sizeof(DebugSubsectionHeader) + Data.getLength(); 56 assert(Result % alignOf(Container) == 0); 57 return Result; 58 } 59 60 DebugSubsectionKind DebugSubsectionRecord::kind() const { return Kind; } 61 62 BinaryStreamRef DebugSubsectionRecord::getRecordData() const { return Data; } 63 64 DebugSubsectionRecordBuilder::DebugSubsectionRecordBuilder( 65 std::unique_ptr<DebugSubsection> Subsection, CodeViewContainer Container) 66 : Subsection(std::move(Subsection)), Container(Container) {} 67 68 uint32_t DebugSubsectionRecordBuilder::calculateSerializedLength() { 69 uint32_t Size = 70 sizeof(DebugSubsectionHeader) + 71 alignTo(Subsection->calculateSerializedSize(), alignOf(Container)); 72 return Size; 73 } 74 75 Error DebugSubsectionRecordBuilder::commit(BinaryStreamWriter &Writer) { 76 assert(Writer.getOffset() % alignOf(Container) == 0 && 77 "Debug Subsection not properly aligned"); 78 79 DebugSubsectionHeader Header; 80 Header.Kind = uint32_t(Subsection->kind()); 81 Header.Length = calculateSerializedLength() - sizeof(DebugSubsectionHeader); 82 83 if (auto EC = Writer.writeObject(Header)) 84 return EC; 85 if (auto EC = Subsection->commit(Writer)) 86 return EC; 87 if (auto EC = Writer.padToAlignment(alignOf(Container))) 88 return EC; 89 90 return Error::success(); 91 } 92