19df3605dSDimitry Andric //===- DebugLinesSubsection.cpp -------------------------------------------===//
2a303c417SDimitry Andric //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6a303c417SDimitry Andric //
7a303c417SDimitry Andric //===----------------------------------------------------------------------===//
8a303c417SDimitry Andric
9ee2f195dSDimitry Andric #include "llvm/DebugInfo/CodeView/DebugLinesSubsection.h"
109df3605dSDimitry Andric #include "llvm/ADT/ArrayRef.h"
119df3605dSDimitry Andric #include "llvm/DebugInfo/CodeView/CodeView.h"
12a303c417SDimitry Andric #include "llvm/DebugInfo/CodeView/CodeViewError.h"
13ee2f195dSDimitry Andric #include "llvm/DebugInfo/CodeView/DebugChecksumsSubsection.h"
149df3605dSDimitry Andric #include "llvm/Support/BinaryStreamReader.h"
159df3605dSDimitry Andric #include "llvm/Support/BinaryStreamWriter.h"
169df3605dSDimitry Andric #include "llvm/Support/Error.h"
179df3605dSDimitry Andric #include <cassert>
189df3605dSDimitry Andric #include <cstdint>
19a303c417SDimitry Andric
20a303c417SDimitry Andric using namespace llvm;
21a303c417SDimitry Andric using namespace llvm::codeview;
22a303c417SDimitry Andric
operator ()(BinaryStreamRef Stream,uint32_t & Len,LineColumnEntry & Item)237ab83427SDimitry Andric Error LineColumnExtractor::operator()(BinaryStreamRef Stream, uint32_t &Len,
247ab83427SDimitry Andric LineColumnEntry &Item) {
25a303c417SDimitry Andric const LineBlockFragmentHeader *BlockHeader;
26a303c417SDimitry Andric BinaryStreamReader Reader(Stream);
27a303c417SDimitry Andric if (auto EC = Reader.readObject(BlockHeader))
28a303c417SDimitry Andric return EC;
29a303c417SDimitry Andric bool HasColumn = Header->Flags & uint16_t(LF_HaveColumns);
30a303c417SDimitry Andric uint32_t LineInfoSize =
31a303c417SDimitry Andric BlockHeader->NumLines *
32a303c417SDimitry Andric (sizeof(LineNumberEntry) + (HasColumn ? sizeof(ColumnNumberEntry) : 0));
33a303c417SDimitry Andric if (BlockHeader->BlockSize < sizeof(LineBlockFragmentHeader))
34a303c417SDimitry Andric return make_error<CodeViewError>(cv_error_code::corrupt_record,
35a303c417SDimitry Andric "Invalid line block record size");
36a303c417SDimitry Andric uint32_t Size = BlockHeader->BlockSize - sizeof(LineBlockFragmentHeader);
37a303c417SDimitry Andric if (LineInfoSize > Size)
38a303c417SDimitry Andric return make_error<CodeViewError>(cv_error_code::corrupt_record,
39a303c417SDimitry Andric "Invalid line block record size");
40a303c417SDimitry Andric // The value recorded in BlockHeader->BlockSize includes the size of
41a303c417SDimitry Andric // LineBlockFragmentHeader.
42a303c417SDimitry Andric Len = BlockHeader->BlockSize;
43a303c417SDimitry Andric Item.NameIndex = BlockHeader->NameIndex;
44a303c417SDimitry Andric if (auto EC = Reader.readArray(Item.LineNumbers, BlockHeader->NumLines))
45a303c417SDimitry Andric return EC;
46a303c417SDimitry Andric if (HasColumn) {
47a303c417SDimitry Andric if (auto EC = Reader.readArray(Item.Columns, BlockHeader->NumLines))
48a303c417SDimitry Andric return EC;
49a303c417SDimitry Andric }
50a303c417SDimitry Andric return Error::success();
51a303c417SDimitry Andric }
52a303c417SDimitry Andric
DebugLinesSubsectionRef()53ee2f195dSDimitry Andric DebugLinesSubsectionRef::DebugLinesSubsectionRef()
54ee2f195dSDimitry Andric : DebugSubsectionRef(DebugSubsectionKind::Lines) {}
55a303c417SDimitry Andric
initialize(BinaryStreamReader Reader)56ee2f195dSDimitry Andric Error DebugLinesSubsectionRef::initialize(BinaryStreamReader Reader) {
57a303c417SDimitry Andric if (auto EC = Reader.readObject(Header))
58a303c417SDimitry Andric return EC;
59a303c417SDimitry Andric
607ab83427SDimitry Andric LinesAndColumns.getExtractor().Header = Header;
617ab83427SDimitry Andric if (auto EC = Reader.readArray(LinesAndColumns, Reader.bytesRemaining()))
62a303c417SDimitry Andric return EC;
63a303c417SDimitry Andric
64a303c417SDimitry Andric return Error::success();
65a303c417SDimitry Andric }
66a303c417SDimitry Andric
hasColumnInfo() const67ee2f195dSDimitry Andric bool DebugLinesSubsectionRef::hasColumnInfo() const {
68a303c417SDimitry Andric return !!(Header->Flags & LF_HaveColumns);
69a303c417SDimitry Andric }
70a303c417SDimitry Andric
DebugLinesSubsection(DebugChecksumsSubsection & Checksums,DebugStringTableSubsection & Strings)71ee2f195dSDimitry Andric DebugLinesSubsection::DebugLinesSubsection(DebugChecksumsSubsection &Checksums,
72ee2f195dSDimitry Andric DebugStringTableSubsection &Strings)
73ee2f195dSDimitry Andric : DebugSubsection(DebugSubsectionKind::Lines), Checksums(Checksums) {}
74a303c417SDimitry Andric
createBlock(StringRef FileName)75ee2f195dSDimitry Andric void DebugLinesSubsection::createBlock(StringRef FileName) {
76148779dfSDimitry Andric uint32_t Offset = Checksums.mapChecksumOffset(FileName);
77148779dfSDimitry Andric
78148779dfSDimitry Andric Blocks.emplace_back(Offset);
79a303c417SDimitry Andric }
80a303c417SDimitry Andric
addLineInfo(uint32_t Offset,const LineInfo & Line)81ee2f195dSDimitry Andric void DebugLinesSubsection::addLineInfo(uint32_t Offset, const LineInfo &Line) {
82a303c417SDimitry Andric Block &B = Blocks.back();
83a303c417SDimitry Andric LineNumberEntry LNE;
84a303c417SDimitry Andric LNE.Flags = Line.getRawData();
85a303c417SDimitry Andric LNE.Offset = Offset;
86a303c417SDimitry Andric B.Lines.push_back(LNE);
87a303c417SDimitry Andric }
88a303c417SDimitry Andric
addLineAndColumnInfo(uint32_t Offset,const LineInfo & Line,uint32_t ColStart,uint32_t ColEnd)89ee2f195dSDimitry Andric void DebugLinesSubsection::addLineAndColumnInfo(uint32_t Offset,
90a303c417SDimitry Andric const LineInfo &Line,
91a303c417SDimitry Andric uint32_t ColStart,
92a303c417SDimitry Andric uint32_t ColEnd) {
93a303c417SDimitry Andric Block &B = Blocks.back();
94a303c417SDimitry Andric assert(B.Lines.size() == B.Columns.size());
95a303c417SDimitry Andric
96a303c417SDimitry Andric addLineInfo(Offset, Line);
97a303c417SDimitry Andric ColumnNumberEntry CNE;
98a303c417SDimitry Andric CNE.StartColumn = ColStart;
99a303c417SDimitry Andric CNE.EndColumn = ColEnd;
100a303c417SDimitry Andric B.Columns.push_back(CNE);
101a303c417SDimitry Andric }
102a303c417SDimitry Andric
commit(BinaryStreamWriter & Writer) const103ee2f195dSDimitry Andric Error DebugLinesSubsection::commit(BinaryStreamWriter &Writer) const {
104a303c417SDimitry Andric LineFragmentHeader Header;
105a303c417SDimitry Andric Header.CodeSize = CodeSize;
106a303c417SDimitry Andric Header.Flags = hasColumnInfo() ? LF_HaveColumns : 0;
107a303c417SDimitry Andric Header.RelocOffset = RelocOffset;
108a303c417SDimitry Andric Header.RelocSegment = RelocSegment;
109a303c417SDimitry Andric
110a303c417SDimitry Andric if (auto EC = Writer.writeObject(Header))
111a303c417SDimitry Andric return EC;
112a303c417SDimitry Andric
113a303c417SDimitry Andric for (const auto &B : Blocks) {
114a303c417SDimitry Andric LineBlockFragmentHeader BlockHeader;
115a303c417SDimitry Andric assert(B.Lines.size() == B.Columns.size() || B.Columns.empty());
116a303c417SDimitry Andric
117a303c417SDimitry Andric BlockHeader.NumLines = B.Lines.size();
118a303c417SDimitry Andric BlockHeader.BlockSize = sizeof(LineBlockFragmentHeader);
119a303c417SDimitry Andric BlockHeader.BlockSize += BlockHeader.NumLines * sizeof(LineNumberEntry);
120a303c417SDimitry Andric if (hasColumnInfo())
121a303c417SDimitry Andric BlockHeader.BlockSize += BlockHeader.NumLines * sizeof(ColumnNumberEntry);
122a303c417SDimitry Andric BlockHeader.NameIndex = B.ChecksumBufferOffset;
123a303c417SDimitry Andric if (auto EC = Writer.writeObject(BlockHeader))
124a303c417SDimitry Andric return EC;
125a303c417SDimitry Andric
126e3b55780SDimitry Andric if (auto EC = Writer.writeArray(ArrayRef(B.Lines)))
127a303c417SDimitry Andric return EC;
128a303c417SDimitry Andric
129a303c417SDimitry Andric if (hasColumnInfo()) {
130e3b55780SDimitry Andric if (auto EC = Writer.writeArray(ArrayRef(B.Columns)))
131a303c417SDimitry Andric return EC;
132a303c417SDimitry Andric }
133a303c417SDimitry Andric }
134a303c417SDimitry Andric return Error::success();
135a303c417SDimitry Andric }
136a303c417SDimitry Andric
calculateSerializedSize() const137ee2f195dSDimitry Andric uint32_t DebugLinesSubsection::calculateSerializedSize() const {
138a303c417SDimitry Andric uint32_t Size = sizeof(LineFragmentHeader);
139a303c417SDimitry Andric for (const auto &B : Blocks) {
140a303c417SDimitry Andric Size += sizeof(LineBlockFragmentHeader);
141a303c417SDimitry Andric Size += B.Lines.size() * sizeof(LineNumberEntry);
142a303c417SDimitry Andric if (hasColumnInfo())
143a303c417SDimitry Andric Size += B.Columns.size() * sizeof(ColumnNumberEntry);
144a303c417SDimitry Andric }
145a303c417SDimitry Andric return Size;
146a303c417SDimitry Andric }
147a303c417SDimitry Andric
setRelocationAddress(uint16_t Segment,uint32_t Offset)148ee2f195dSDimitry Andric void DebugLinesSubsection::setRelocationAddress(uint16_t Segment,
1497ab83427SDimitry Andric uint32_t Offset) {
150a303c417SDimitry Andric RelocOffset = Offset;
151a303c417SDimitry Andric RelocSegment = Segment;
152a303c417SDimitry Andric }
153a303c417SDimitry Andric
setCodeSize(uint32_t Size)154ee2f195dSDimitry Andric void DebugLinesSubsection::setCodeSize(uint32_t Size) { CodeSize = Size; }
155a303c417SDimitry Andric
setFlags(LineFlags Flags)156ee2f195dSDimitry Andric void DebugLinesSubsection::setFlags(LineFlags Flags) { this->Flags = Flags; }
157a303c417SDimitry Andric
hasColumnInfo() const158ee2f195dSDimitry Andric bool DebugLinesSubsection::hasColumnInfo() const {
159a303c417SDimitry Andric return Flags & LF_HaveColumns;
160a303c417SDimitry Andric }
161