DebugLinesSubsection.cpp 5.5 KB

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