DebugLinesSubsection.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- DebugLinesSubsection.h -----------------------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_DEBUGINFO_CODEVIEW_DEBUGLINESSUBSECTION_H
  14. #define LLVM_DEBUGINFO_CODEVIEW_DEBUGLINESSUBSECTION_H
  15. #include "llvm/ADT/StringRef.h"
  16. #include "llvm/DebugInfo/CodeView/CodeView.h"
  17. #include "llvm/DebugInfo/CodeView/DebugSubsection.h"
  18. #include "llvm/DebugInfo/CodeView/Line.h"
  19. #include "llvm/Support/BinaryStreamArray.h"
  20. #include "llvm/Support/BinaryStreamReader.h"
  21. #include "llvm/Support/BinaryStreamRef.h"
  22. #include "llvm/Support/Endian.h"
  23. #include "llvm/Support/Error.h"
  24. #include <cstdint>
  25. #include <vector>
  26. namespace llvm {
  27. namespace codeview {
  28. class DebugChecksumsSubsection;
  29. class DebugStringTableSubsection;
  30. // Corresponds to the `CV_DebugSLinesHeader_t` structure.
  31. struct LineFragmentHeader {
  32. support::ulittle32_t RelocOffset; // Code offset of line contribution.
  33. support::ulittle16_t RelocSegment; // Code segment of line contribution.
  34. support::ulittle16_t Flags; // See LineFlags enumeration.
  35. support::ulittle32_t CodeSize; // Code size of this line contribution.
  36. };
  37. // Corresponds to the `CV_DebugSLinesFileBlockHeader_t` structure.
  38. struct LineBlockFragmentHeader {
  39. support::ulittle32_t NameIndex; // Offset of FileChecksum entry in File
  40. // checksums buffer. The checksum entry then
  41. // contains another offset into the string
  42. // table of the actual name.
  43. support::ulittle32_t NumLines; // Number of lines
  44. support::ulittle32_t BlockSize; // Code size of block, in bytes.
  45. // The following two variable length arrays appear immediately after the
  46. // header. The structure definitions follow.
  47. // LineNumberEntry Lines[NumLines];
  48. // ColumnNumberEntry Columns[NumLines];
  49. };
  50. // Corresponds to `CV_Line_t` structure
  51. struct LineNumberEntry {
  52. support::ulittle32_t Offset; // Offset to start of code bytes for line number
  53. support::ulittle32_t Flags; // Start:24, End:7, IsStatement:1
  54. };
  55. // Corresponds to `CV_Column_t` structure
  56. struct ColumnNumberEntry {
  57. support::ulittle16_t StartColumn;
  58. support::ulittle16_t EndColumn;
  59. };
  60. struct LineColumnEntry {
  61. support::ulittle32_t NameIndex;
  62. FixedStreamArray<LineNumberEntry> LineNumbers;
  63. FixedStreamArray<ColumnNumberEntry> Columns;
  64. };
  65. class LineColumnExtractor {
  66. public:
  67. Error operator()(BinaryStreamRef Stream, uint32_t &Len,
  68. LineColumnEntry &Item);
  69. const LineFragmentHeader *Header = nullptr;
  70. };
  71. class DebugLinesSubsectionRef final : public DebugSubsectionRef {
  72. friend class LineColumnExtractor;
  73. using LineInfoArray = VarStreamArray<LineColumnEntry, LineColumnExtractor>;
  74. using Iterator = LineInfoArray::Iterator;
  75. public:
  76. DebugLinesSubsectionRef();
  77. static bool classof(const DebugSubsectionRef *S) {
  78. return S->kind() == DebugSubsectionKind::Lines;
  79. }
  80. Error initialize(BinaryStreamReader Reader);
  81. Iterator begin() const { return LinesAndColumns.begin(); }
  82. Iterator end() const { return LinesAndColumns.end(); }
  83. const LineFragmentHeader *header() const { return Header; }
  84. bool hasColumnInfo() const;
  85. private:
  86. const LineFragmentHeader *Header = nullptr;
  87. LineInfoArray LinesAndColumns;
  88. };
  89. class DebugLinesSubsection final : public DebugSubsection {
  90. struct Block {
  91. Block(uint32_t ChecksumBufferOffset)
  92. : ChecksumBufferOffset(ChecksumBufferOffset) {}
  93. uint32_t ChecksumBufferOffset;
  94. std::vector<LineNumberEntry> Lines;
  95. std::vector<ColumnNumberEntry> Columns;
  96. };
  97. public:
  98. DebugLinesSubsection(DebugChecksumsSubsection &Checksums,
  99. DebugStringTableSubsection &Strings);
  100. static bool classof(const DebugSubsection *S) {
  101. return S->kind() == DebugSubsectionKind::Lines;
  102. }
  103. void createBlock(StringRef FileName);
  104. void addLineInfo(uint32_t Offset, const LineInfo &Line);
  105. void addLineAndColumnInfo(uint32_t Offset, const LineInfo &Line,
  106. uint32_t ColStart, uint32_t ColEnd);
  107. uint32_t calculateSerializedSize() const override;
  108. Error commit(BinaryStreamWriter &Writer) const override;
  109. void setRelocationAddress(uint16_t Segment, uint32_t Offset);
  110. void setCodeSize(uint32_t Size);
  111. void setFlags(LineFlags Flags);
  112. bool hasColumnInfo() const;
  113. private:
  114. DebugChecksumsSubsection &Checksums;
  115. uint32_t RelocOffset = 0;
  116. uint16_t RelocSegment = 0;
  117. uint32_t CodeSize = 0;
  118. LineFlags Flags = LF_None;
  119. std::vector<Block> Blocks;
  120. };
  121. } // end namespace codeview
  122. } // end namespace llvm
  123. #endif // LLVM_DEBUGINFO_CODEVIEW_DEBUGLINESSUBSECTION_H
  124. #ifdef __GNUC__
  125. #pragma GCC diagnostic pop
  126. #endif