10b57cec5SDimitry Andric //===- DebugLinesSubsection.cpp -------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "llvm/DebugInfo/CodeView/DebugLinesSubsection.h"
100b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h"
110b57cec5SDimitry Andric #include "llvm/DebugInfo/CodeView/CodeView.h"
120b57cec5SDimitry Andric #include "llvm/DebugInfo/CodeView/CodeViewError.h"
130b57cec5SDimitry Andric #include "llvm/DebugInfo/CodeView/DebugChecksumsSubsection.h"
140b57cec5SDimitry Andric #include "llvm/Support/BinaryStreamReader.h"
150b57cec5SDimitry Andric #include "llvm/Support/BinaryStreamWriter.h"
160b57cec5SDimitry Andric #include "llvm/Support/Error.h"
170b57cec5SDimitry Andric #include <cassert>
180b57cec5SDimitry Andric #include <cstdint>
190b57cec5SDimitry Andric 
200b57cec5SDimitry Andric using namespace llvm;
210b57cec5SDimitry Andric using namespace llvm::codeview;
220b57cec5SDimitry Andric 
operator ()(BinaryStreamRef Stream,uint32_t & Len,LineColumnEntry & Item)230b57cec5SDimitry Andric Error LineColumnExtractor::operator()(BinaryStreamRef Stream, uint32_t &Len,
240b57cec5SDimitry Andric                                       LineColumnEntry &Item) {
250b57cec5SDimitry Andric   const LineBlockFragmentHeader *BlockHeader;
260b57cec5SDimitry Andric   BinaryStreamReader Reader(Stream);
270b57cec5SDimitry Andric   if (auto EC = Reader.readObject(BlockHeader))
280b57cec5SDimitry Andric     return EC;
290b57cec5SDimitry Andric   bool HasColumn = Header->Flags & uint16_t(LF_HaveColumns);
300b57cec5SDimitry Andric   uint32_t LineInfoSize =
310b57cec5SDimitry Andric       BlockHeader->NumLines *
320b57cec5SDimitry Andric       (sizeof(LineNumberEntry) + (HasColumn ? sizeof(ColumnNumberEntry) : 0));
330b57cec5SDimitry Andric   if (BlockHeader->BlockSize < sizeof(LineBlockFragmentHeader))
340b57cec5SDimitry Andric     return make_error<CodeViewError>(cv_error_code::corrupt_record,
350b57cec5SDimitry Andric                                      "Invalid line block record size");
360b57cec5SDimitry Andric   uint32_t Size = BlockHeader->BlockSize - sizeof(LineBlockFragmentHeader);
370b57cec5SDimitry Andric   if (LineInfoSize > Size)
380b57cec5SDimitry Andric     return make_error<CodeViewError>(cv_error_code::corrupt_record,
390b57cec5SDimitry Andric                                      "Invalid line block record size");
400b57cec5SDimitry Andric   // The value recorded in BlockHeader->BlockSize includes the size of
410b57cec5SDimitry Andric   // LineBlockFragmentHeader.
420b57cec5SDimitry Andric   Len = BlockHeader->BlockSize;
430b57cec5SDimitry Andric   Item.NameIndex = BlockHeader->NameIndex;
440b57cec5SDimitry Andric   if (auto EC = Reader.readArray(Item.LineNumbers, BlockHeader->NumLines))
450b57cec5SDimitry Andric     return EC;
460b57cec5SDimitry Andric   if (HasColumn) {
470b57cec5SDimitry Andric     if (auto EC = Reader.readArray(Item.Columns, BlockHeader->NumLines))
480b57cec5SDimitry Andric       return EC;
490b57cec5SDimitry Andric   }
500b57cec5SDimitry Andric   return Error::success();
510b57cec5SDimitry Andric }
520b57cec5SDimitry Andric 
DebugLinesSubsectionRef()530b57cec5SDimitry Andric DebugLinesSubsectionRef::DebugLinesSubsectionRef()
540b57cec5SDimitry Andric     : DebugSubsectionRef(DebugSubsectionKind::Lines) {}
550b57cec5SDimitry Andric 
initialize(BinaryStreamReader Reader)560b57cec5SDimitry Andric Error DebugLinesSubsectionRef::initialize(BinaryStreamReader Reader) {
570b57cec5SDimitry Andric   if (auto EC = Reader.readObject(Header))
580b57cec5SDimitry Andric     return EC;
590b57cec5SDimitry Andric 
600b57cec5SDimitry Andric   LinesAndColumns.getExtractor().Header = Header;
610b57cec5SDimitry Andric   if (auto EC = Reader.readArray(LinesAndColumns, Reader.bytesRemaining()))
620b57cec5SDimitry Andric     return EC;
630b57cec5SDimitry Andric 
640b57cec5SDimitry Andric   return Error::success();
650b57cec5SDimitry Andric }
660b57cec5SDimitry Andric 
hasColumnInfo() const670b57cec5SDimitry Andric bool DebugLinesSubsectionRef::hasColumnInfo() const {
680b57cec5SDimitry Andric   return !!(Header->Flags & LF_HaveColumns);
690b57cec5SDimitry Andric }
700b57cec5SDimitry Andric 
DebugLinesSubsection(DebugChecksumsSubsection & Checksums,DebugStringTableSubsection & Strings)710b57cec5SDimitry Andric DebugLinesSubsection::DebugLinesSubsection(DebugChecksumsSubsection &Checksums,
720b57cec5SDimitry Andric                                            DebugStringTableSubsection &Strings)
730b57cec5SDimitry Andric     : DebugSubsection(DebugSubsectionKind::Lines), Checksums(Checksums) {}
740b57cec5SDimitry Andric 
createBlock(StringRef FileName)750b57cec5SDimitry Andric void DebugLinesSubsection::createBlock(StringRef FileName) {
760b57cec5SDimitry Andric   uint32_t Offset = Checksums.mapChecksumOffset(FileName);
770b57cec5SDimitry Andric 
780b57cec5SDimitry Andric   Blocks.emplace_back(Offset);
790b57cec5SDimitry Andric }
800b57cec5SDimitry Andric 
addLineInfo(uint32_t Offset,const LineInfo & Line)810b57cec5SDimitry Andric void DebugLinesSubsection::addLineInfo(uint32_t Offset, const LineInfo &Line) {
820b57cec5SDimitry Andric   Block &B = Blocks.back();
830b57cec5SDimitry Andric   LineNumberEntry LNE;
840b57cec5SDimitry Andric   LNE.Flags = Line.getRawData();
850b57cec5SDimitry Andric   LNE.Offset = Offset;
860b57cec5SDimitry Andric   B.Lines.push_back(LNE);
870b57cec5SDimitry Andric }
880b57cec5SDimitry Andric 
addLineAndColumnInfo(uint32_t Offset,const LineInfo & Line,uint32_t ColStart,uint32_t ColEnd)890b57cec5SDimitry Andric void DebugLinesSubsection::addLineAndColumnInfo(uint32_t Offset,
900b57cec5SDimitry Andric                                                 const LineInfo &Line,
910b57cec5SDimitry Andric                                                 uint32_t ColStart,
920b57cec5SDimitry Andric                                                 uint32_t ColEnd) {
930b57cec5SDimitry Andric   Block &B = Blocks.back();
940b57cec5SDimitry Andric   assert(B.Lines.size() == B.Columns.size());
950b57cec5SDimitry Andric 
960b57cec5SDimitry Andric   addLineInfo(Offset, Line);
970b57cec5SDimitry Andric   ColumnNumberEntry CNE;
980b57cec5SDimitry Andric   CNE.StartColumn = ColStart;
990b57cec5SDimitry Andric   CNE.EndColumn = ColEnd;
1000b57cec5SDimitry Andric   B.Columns.push_back(CNE);
1010b57cec5SDimitry Andric }
1020b57cec5SDimitry Andric 
commit(BinaryStreamWriter & Writer) const1030b57cec5SDimitry Andric Error DebugLinesSubsection::commit(BinaryStreamWriter &Writer) const {
1040b57cec5SDimitry Andric   LineFragmentHeader Header;
1050b57cec5SDimitry Andric   Header.CodeSize = CodeSize;
1060b57cec5SDimitry Andric   Header.Flags = hasColumnInfo() ? LF_HaveColumns : 0;
1070b57cec5SDimitry Andric   Header.RelocOffset = RelocOffset;
1080b57cec5SDimitry Andric   Header.RelocSegment = RelocSegment;
1090b57cec5SDimitry Andric 
1100b57cec5SDimitry Andric   if (auto EC = Writer.writeObject(Header))
1110b57cec5SDimitry Andric     return EC;
1120b57cec5SDimitry Andric 
1130b57cec5SDimitry Andric   for (const auto &B : Blocks) {
1140b57cec5SDimitry Andric     LineBlockFragmentHeader BlockHeader;
1150b57cec5SDimitry Andric     assert(B.Lines.size() == B.Columns.size() || B.Columns.empty());
1160b57cec5SDimitry Andric 
1170b57cec5SDimitry Andric     BlockHeader.NumLines = B.Lines.size();
1180b57cec5SDimitry Andric     BlockHeader.BlockSize = sizeof(LineBlockFragmentHeader);
1190b57cec5SDimitry Andric     BlockHeader.BlockSize += BlockHeader.NumLines * sizeof(LineNumberEntry);
1200b57cec5SDimitry Andric     if (hasColumnInfo())
1210b57cec5SDimitry Andric       BlockHeader.BlockSize += BlockHeader.NumLines * sizeof(ColumnNumberEntry);
1220b57cec5SDimitry Andric     BlockHeader.NameIndex = B.ChecksumBufferOffset;
1230b57cec5SDimitry Andric     if (auto EC = Writer.writeObject(BlockHeader))
1240b57cec5SDimitry Andric       return EC;
1250b57cec5SDimitry Andric 
126bdd1243dSDimitry Andric     if (auto EC = Writer.writeArray(ArrayRef(B.Lines)))
1270b57cec5SDimitry Andric       return EC;
1280b57cec5SDimitry Andric 
1290b57cec5SDimitry Andric     if (hasColumnInfo()) {
130bdd1243dSDimitry Andric       if (auto EC = Writer.writeArray(ArrayRef(B.Columns)))
1310b57cec5SDimitry Andric         return EC;
1320b57cec5SDimitry Andric     }
1330b57cec5SDimitry Andric   }
1340b57cec5SDimitry Andric   return Error::success();
1350b57cec5SDimitry Andric }
1360b57cec5SDimitry Andric 
calculateSerializedSize() const1370b57cec5SDimitry Andric uint32_t DebugLinesSubsection::calculateSerializedSize() const {
1380b57cec5SDimitry Andric   uint32_t Size = sizeof(LineFragmentHeader);
1390b57cec5SDimitry Andric   for (const auto &B : Blocks) {
1400b57cec5SDimitry Andric     Size += sizeof(LineBlockFragmentHeader);
1410b57cec5SDimitry Andric     Size += B.Lines.size() * sizeof(LineNumberEntry);
1420b57cec5SDimitry Andric     if (hasColumnInfo())
1430b57cec5SDimitry Andric       Size += B.Columns.size() * sizeof(ColumnNumberEntry);
1440b57cec5SDimitry Andric   }
1450b57cec5SDimitry Andric   return Size;
1460b57cec5SDimitry Andric }
1470b57cec5SDimitry Andric 
setRelocationAddress(uint16_t Segment,uint32_t Offset)1480b57cec5SDimitry Andric void DebugLinesSubsection::setRelocationAddress(uint16_t Segment,
1490b57cec5SDimitry Andric                                                 uint32_t Offset) {
1500b57cec5SDimitry Andric   RelocOffset = Offset;
1510b57cec5SDimitry Andric   RelocSegment = Segment;
1520b57cec5SDimitry Andric }
1530b57cec5SDimitry Andric 
setCodeSize(uint32_t Size)1540b57cec5SDimitry Andric void DebugLinesSubsection::setCodeSize(uint32_t Size) { CodeSize = Size; }
1550b57cec5SDimitry Andric 
setFlags(LineFlags Flags)1560b57cec5SDimitry Andric void DebugLinesSubsection::setFlags(LineFlags Flags) { this->Flags = Flags; }
1570b57cec5SDimitry Andric 
hasColumnInfo() const1580b57cec5SDimitry Andric bool DebugLinesSubsection::hasColumnInfo() const {
1590b57cec5SDimitry Andric   return Flags & LF_HaveColumns;
1600b57cec5SDimitry Andric }
161