18456b16eSEugene Zelenko //===- DebugSubsectionRecord.cpp ------------------------------------------===// 28c099fe0SZachary Turner // 3*2946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*2946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 5*2946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 68c099fe0SZachary Turner // 78c099fe0SZachary Turner //===----------------------------------------------------------------------===// 88c099fe0SZachary Turner 98c099fe0SZachary Turner #include "llvm/DebugInfo/CodeView/DebugSubsectionRecord.h" 108456b16eSEugene Zelenko #include "llvm/DebugInfo/CodeView/CodeView.h" 118c099fe0SZachary Turner #include "llvm/DebugInfo/CodeView/DebugSubsection.h" 128c099fe0SZachary Turner #include "llvm/Support/BinaryStreamReader.h" 138456b16eSEugene Zelenko #include "llvm/Support/BinaryStreamWriter.h" 148456b16eSEugene Zelenko #include "llvm/Support/Error.h" 158456b16eSEugene Zelenko #include "llvm/Support/MathExtras.h" 168456b16eSEugene Zelenko #include <algorithm> 178456b16eSEugene Zelenko #include <cassert> 188456b16eSEugene Zelenko #include <cstdint> 198c099fe0SZachary Turner 208c099fe0SZachary Turner using namespace llvm; 218c099fe0SZachary Turner using namespace llvm::codeview; 228c099fe0SZachary Turner 238456b16eSEugene Zelenko DebugSubsectionRecord::DebugSubsectionRecord() = default; 248c099fe0SZachary Turner 258c099fe0SZachary Turner DebugSubsectionRecord::DebugSubsectionRecord(DebugSubsectionKind Kind, 26ebd3ae83SZachary Turner BinaryStreamRef Data, 27ebd3ae83SZachary Turner CodeViewContainer Container) 28afb81a83SZachary Turner : Container(Container), Kind(Kind), Data(Data) {} 298c099fe0SZachary Turner 308c099fe0SZachary Turner Error DebugSubsectionRecord::initialize(BinaryStreamRef Stream, 31ebd3ae83SZachary Turner DebugSubsectionRecord &Info, 32ebd3ae83SZachary Turner CodeViewContainer Container) { 338c099fe0SZachary Turner const DebugSubsectionHeader *Header; 348c099fe0SZachary Turner BinaryStreamReader Reader(Stream); 358c099fe0SZachary Turner if (auto EC = Reader.readObject(Header)) 368c099fe0SZachary Turner return EC; 378c099fe0SZachary Turner 388c099fe0SZachary Turner DebugSubsectionKind Kind = 398c099fe0SZachary Turner static_cast<DebugSubsectionKind>(uint32_t(Header->Kind)); 408c099fe0SZachary Turner if (auto EC = Reader.readStreamRef(Info.Data, Header->Length)) 418c099fe0SZachary Turner return EC; 42ebd3ae83SZachary Turner Info.Container = Container; 438c099fe0SZachary Turner Info.Kind = Kind; 448c099fe0SZachary Turner return Error::success(); 458c099fe0SZachary Turner } 468c099fe0SZachary Turner 478c099fe0SZachary Turner uint32_t DebugSubsectionRecord::getRecordLength() const { 48deb39130SZachary Turner return sizeof(DebugSubsectionHeader) + Data.getLength(); 498c099fe0SZachary Turner } 508c099fe0SZachary Turner 518c099fe0SZachary Turner DebugSubsectionKind DebugSubsectionRecord::kind() const { return Kind; } 528c099fe0SZachary Turner 538c099fe0SZachary Turner BinaryStreamRef DebugSubsectionRecord::getRecordData() const { return Data; } 548c099fe0SZachary Turner 558c099fe0SZachary Turner DebugSubsectionRecordBuilder::DebugSubsectionRecordBuilder( 56a8cfc29cSZachary Turner std::shared_ptr<DebugSubsection> Subsection, CodeViewContainer Container) 5792dcdda6SZachary Turner : Subsection(std::move(Subsection)), Container(Container) {} 588c099fe0SZachary Turner 5944cdb109SReid Kleckner DebugSubsectionRecordBuilder::DebugSubsectionRecordBuilder( 6044cdb109SReid Kleckner const DebugSubsectionRecord &Contents, CodeViewContainer Container) 6144cdb109SReid Kleckner : Contents(Contents), Container(Container) {} 6244cdb109SReid Kleckner 638c099fe0SZachary Turner uint32_t DebugSubsectionRecordBuilder::calculateSerializedLength() { 6444cdb109SReid Kleckner uint32_t DataSize = Subsection ? Subsection->calculateSerializedSize() 6544cdb109SReid Kleckner : Contents.getRecordData().getLength(); 6644cdb109SReid Kleckner // The length of the entire subsection is always padded to 4 bytes, 6744cdb109SReid Kleckner // regardless of the container kind. 6844cdb109SReid Kleckner return sizeof(DebugSubsectionHeader) + alignTo(DataSize, 4); 698c099fe0SZachary Turner } 708c099fe0SZachary Turner 71deb39130SZachary Turner Error DebugSubsectionRecordBuilder::commit(BinaryStreamWriter &Writer) const { 72ebd3ae83SZachary Turner assert(Writer.getOffset() % alignOf(Container) == 0 && 73ebd3ae83SZachary Turner "Debug Subsection not properly aligned"); 74ebd3ae83SZachary Turner 758c099fe0SZachary Turner DebugSubsectionHeader Header; 7644cdb109SReid Kleckner Header.Kind = uint32_t(Subsection ? Subsection->kind() : Contents.kind()); 77deb39130SZachary Turner // The value written into the Header's Length field is only padded to the 78deb39130SZachary Turner // container's alignment 7944cdb109SReid Kleckner uint32_t DataSize = Subsection ? Subsection->calculateSerializedSize() 8044cdb109SReid Kleckner : Contents.getRecordData().getLength(); 8144cdb109SReid Kleckner Header.Length = alignTo(DataSize, alignOf(Container)); 828c099fe0SZachary Turner 838c099fe0SZachary Turner if (auto EC = Writer.writeObject(Header)) 848c099fe0SZachary Turner return EC; 8544cdb109SReid Kleckner if (Subsection) { 8692dcdda6SZachary Turner if (auto EC = Subsection->commit(Writer)) 878c099fe0SZachary Turner return EC; 8844cdb109SReid Kleckner } else { 8944cdb109SReid Kleckner if (auto EC = Writer.writeStreamRef(Contents.getRecordData())) 9044cdb109SReid Kleckner return EC; 9144cdb109SReid Kleckner } 92deb39130SZachary Turner if (auto EC = Writer.padToAlignment(4)) 938c099fe0SZachary Turner return EC; 948c099fe0SZachary Turner 958c099fe0SZachary Turner return Error::success(); 968c099fe0SZachary Turner } 97