Eugene Zelenko | 8456b16 | 2017-06-29 00:05:44 +0000 | [diff] [blame] | 1 | //===- DebugLinesSubsection.cpp -------------------------------------------===// |
Zachary Turner | c37cb0c | 2017-04-27 16:12:16 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Zachary Turner | 8c099fe | 2017-05-30 16:36:15 +0000 | [diff] [blame] | 10 | #include "llvm/DebugInfo/CodeView/DebugLinesSubsection.h" |
Eugene Zelenko | 8456b16 | 2017-06-29 00:05:44 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/ArrayRef.h" |
| 12 | #include "llvm/DebugInfo/CodeView/CodeView.h" |
Zachary Turner | c37cb0c | 2017-04-27 16:12:16 +0000 | [diff] [blame] | 13 | #include "llvm/DebugInfo/CodeView/CodeViewError.h" |
Zachary Turner | 8c099fe | 2017-05-30 16:36:15 +0000 | [diff] [blame] | 14 | #include "llvm/DebugInfo/CodeView/DebugChecksumsSubsection.h" |
Eugene Zelenko | 8456b16 | 2017-06-29 00:05:44 +0000 | [diff] [blame] | 15 | #include "llvm/Support/BinaryStreamReader.h" |
| 16 | #include "llvm/Support/BinaryStreamWriter.h" |
| 17 | #include "llvm/Support/Error.h" |
| 18 | #include <cassert> |
| 19 | #include <cstdint> |
Zachary Turner | c37cb0c | 2017-04-27 16:12:16 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace llvm; |
| 22 | using namespace llvm::codeview; |
| 23 | |
Zachary Turner | 7e62cd1 | 2017-06-09 17:54:36 +0000 | [diff] [blame] | 24 | Error LineColumnExtractor::operator()(BinaryStreamRef Stream, uint32_t &Len, |
| 25 | LineColumnEntry &Item) { |
Zachary Turner | c37cb0c | 2017-04-27 16:12:16 +0000 | [diff] [blame] | 26 | const LineBlockFragmentHeader *BlockHeader; |
| 27 | BinaryStreamReader Reader(Stream); |
| 28 | if (auto EC = Reader.readObject(BlockHeader)) |
| 29 | return EC; |
Zachary Turner | 5b6e4e0 | 2017-04-29 01:13:21 +0000 | [diff] [blame] | 30 | bool HasColumn = Header->Flags & uint16_t(LF_HaveColumns); |
Zachary Turner | c37cb0c | 2017-04-27 16:12:16 +0000 | [diff] [blame] | 31 | uint32_t LineInfoSize = |
| 32 | BlockHeader->NumLines * |
| 33 | (sizeof(LineNumberEntry) + (HasColumn ? sizeof(ColumnNumberEntry) : 0)); |
| 34 | if (BlockHeader->BlockSize < sizeof(LineBlockFragmentHeader)) |
| 35 | return make_error<CodeViewError>(cv_error_code::corrupt_record, |
| 36 | "Invalid line block record size"); |
| 37 | uint32_t Size = BlockHeader->BlockSize - sizeof(LineBlockFragmentHeader); |
| 38 | if (LineInfoSize > Size) |
| 39 | return make_error<CodeViewError>(cv_error_code::corrupt_record, |
| 40 | "Invalid line block record size"); |
| 41 | // The value recorded in BlockHeader->BlockSize includes the size of |
| 42 | // LineBlockFragmentHeader. |
| 43 | Len = BlockHeader->BlockSize; |
| 44 | Item.NameIndex = BlockHeader->NameIndex; |
| 45 | if (auto EC = Reader.readArray(Item.LineNumbers, BlockHeader->NumLines)) |
| 46 | return EC; |
| 47 | if (HasColumn) { |
| 48 | if (auto EC = Reader.readArray(Item.Columns, BlockHeader->NumLines)) |
| 49 | return EC; |
| 50 | } |
| 51 | return Error::success(); |
| 52 | } |
| 53 | |
Zachary Turner | 8c099fe | 2017-05-30 16:36:15 +0000 | [diff] [blame] | 54 | DebugLinesSubsectionRef::DebugLinesSubsectionRef() |
| 55 | : DebugSubsectionRef(DebugSubsectionKind::Lines) {} |
Zachary Turner | c37cb0c | 2017-04-27 16:12:16 +0000 | [diff] [blame] | 56 | |
Zachary Turner | 8c099fe | 2017-05-30 16:36:15 +0000 | [diff] [blame] | 57 | Error DebugLinesSubsectionRef::initialize(BinaryStreamReader Reader) { |
Zachary Turner | c37cb0c | 2017-04-27 16:12:16 +0000 | [diff] [blame] | 58 | if (auto EC = Reader.readObject(Header)) |
| 59 | return EC; |
| 60 | |
Zachary Turner | 7e62cd1 | 2017-06-09 17:54:36 +0000 | [diff] [blame] | 61 | LinesAndColumns.getExtractor().Header = Header; |
| 62 | if (auto EC = Reader.readArray(LinesAndColumns, Reader.bytesRemaining())) |
Zachary Turner | c37cb0c | 2017-04-27 16:12:16 +0000 | [diff] [blame] | 63 | return EC; |
| 64 | |
| 65 | return Error::success(); |
| 66 | } |
Zachary Turner | 5b6e4e0 | 2017-04-29 01:13:21 +0000 | [diff] [blame] | 67 | |
Zachary Turner | 8c099fe | 2017-05-30 16:36:15 +0000 | [diff] [blame] | 68 | bool DebugLinesSubsectionRef::hasColumnInfo() const { |
Zachary Turner | 7cc13e5 | 2017-05-01 16:46:39 +0000 | [diff] [blame] | 69 | return !!(Header->Flags & LF_HaveColumns); |
Zachary Turner | 5b6e4e0 | 2017-04-29 01:13:21 +0000 | [diff] [blame] | 70 | } |
Zachary Turner | 8a2ebfb | 2017-05-01 23:27:42 +0000 | [diff] [blame] | 71 | |
Zachary Turner | 8c099fe | 2017-05-30 16:36:15 +0000 | [diff] [blame] | 72 | DebugLinesSubsection::DebugLinesSubsection(DebugChecksumsSubsection &Checksums, |
Zachary Turner | 591312c | 2017-05-30 17:13:33 +0000 | [diff] [blame] | 73 | DebugStringTableSubsection &Strings) |
Zachary Turner | 8c099fe | 2017-05-30 16:36:15 +0000 | [diff] [blame] | 74 | : DebugSubsection(DebugSubsectionKind::Lines), Checksums(Checksums) {} |
Zachary Turner | 8a2ebfb | 2017-05-01 23:27:42 +0000 | [diff] [blame] | 75 | |
Zachary Turner | 8c099fe | 2017-05-30 16:36:15 +0000 | [diff] [blame] | 76 | void DebugLinesSubsection::createBlock(StringRef FileName) { |
Zachary Turner | cf468d8 | 2017-05-03 17:11:40 +0000 | [diff] [blame] | 77 | uint32_t Offset = Checksums.mapChecksumOffset(FileName); |
| 78 | |
| 79 | Blocks.emplace_back(Offset); |
Zachary Turner | 8a2ebfb | 2017-05-01 23:27:42 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Zachary Turner | 8c099fe | 2017-05-30 16:36:15 +0000 | [diff] [blame] | 82 | void DebugLinesSubsection::addLineInfo(uint32_t Offset, const LineInfo &Line) { |
Zachary Turner | 8a2ebfb | 2017-05-01 23:27:42 +0000 | [diff] [blame] | 83 | Block &B = Blocks.back(); |
| 84 | LineNumberEntry LNE; |
| 85 | LNE.Flags = Line.getRawData(); |
| 86 | LNE.Offset = Offset; |
| 87 | B.Lines.push_back(LNE); |
| 88 | } |
| 89 | |
Zachary Turner | 8c099fe | 2017-05-30 16:36:15 +0000 | [diff] [blame] | 90 | void DebugLinesSubsection::addLineAndColumnInfo(uint32_t Offset, |
| 91 | const LineInfo &Line, |
| 92 | uint32_t ColStart, |
| 93 | uint32_t ColEnd) { |
Zachary Turner | 8a2ebfb | 2017-05-01 23:27:42 +0000 | [diff] [blame] | 94 | Block &B = Blocks.back(); |
| 95 | assert(B.Lines.size() == B.Columns.size()); |
| 96 | |
| 97 | addLineInfo(Offset, Line); |
| 98 | ColumnNumberEntry CNE; |
| 99 | CNE.StartColumn = ColStart; |
| 100 | CNE.EndColumn = ColEnd; |
| 101 | B.Columns.push_back(CNE); |
| 102 | } |
| 103 | |
Zachary Turner | 591312c | 2017-05-30 17:13:33 +0000 | [diff] [blame] | 104 | Error DebugLinesSubsection::commit(BinaryStreamWriter &Writer) const { |
Zachary Turner | 8a2ebfb | 2017-05-01 23:27:42 +0000 | [diff] [blame] | 105 | LineFragmentHeader Header; |
| 106 | Header.CodeSize = CodeSize; |
| 107 | Header.Flags = hasColumnInfo() ? LF_HaveColumns : 0; |
| 108 | Header.RelocOffset = RelocOffset; |
| 109 | Header.RelocSegment = RelocSegment; |
| 110 | |
| 111 | if (auto EC = Writer.writeObject(Header)) |
| 112 | return EC; |
| 113 | |
| 114 | for (const auto &B : Blocks) { |
| 115 | LineBlockFragmentHeader BlockHeader; |
| 116 | assert(B.Lines.size() == B.Columns.size() || B.Columns.empty()); |
| 117 | |
| 118 | BlockHeader.NumLines = B.Lines.size(); |
| 119 | BlockHeader.BlockSize = sizeof(LineBlockFragmentHeader); |
| 120 | BlockHeader.BlockSize += BlockHeader.NumLines * sizeof(LineNumberEntry); |
| 121 | if (hasColumnInfo()) |
| 122 | BlockHeader.BlockSize += BlockHeader.NumLines * sizeof(ColumnNumberEntry); |
| 123 | BlockHeader.NameIndex = B.ChecksumBufferOffset; |
| 124 | if (auto EC = Writer.writeObject(BlockHeader)) |
| 125 | return EC; |
| 126 | |
| 127 | if (auto EC = Writer.writeArray(makeArrayRef(B.Lines))) |
| 128 | return EC; |
| 129 | |
| 130 | if (hasColumnInfo()) { |
| 131 | if (auto EC = Writer.writeArray(makeArrayRef(B.Columns))) |
| 132 | return EC; |
| 133 | } |
| 134 | } |
| 135 | return Error::success(); |
| 136 | } |
| 137 | |
Zachary Turner | 591312c | 2017-05-30 17:13:33 +0000 | [diff] [blame] | 138 | uint32_t DebugLinesSubsection::calculateSerializedSize() const { |
Zachary Turner | 8a2ebfb | 2017-05-01 23:27:42 +0000 | [diff] [blame] | 139 | uint32_t Size = sizeof(LineFragmentHeader); |
| 140 | for (const auto &B : Blocks) { |
| 141 | Size += sizeof(LineBlockFragmentHeader); |
| 142 | Size += B.Lines.size() * sizeof(LineNumberEntry); |
| 143 | if (hasColumnInfo()) |
| 144 | Size += B.Columns.size() * sizeof(ColumnNumberEntry); |
| 145 | } |
| 146 | return Size; |
| 147 | } |
| 148 | |
Zachary Turner | 8c099fe | 2017-05-30 16:36:15 +0000 | [diff] [blame] | 149 | void DebugLinesSubsection::setRelocationAddress(uint16_t Segment, |
Bob Haarman | fdf499b | 2017-06-09 01:18:10 +0000 | [diff] [blame] | 150 | uint32_t Offset) { |
Zachary Turner | 8a2ebfb | 2017-05-01 23:27:42 +0000 | [diff] [blame] | 151 | RelocOffset = Offset; |
| 152 | RelocSegment = Segment; |
| 153 | } |
| 154 | |
Zachary Turner | 8c099fe | 2017-05-30 16:36:15 +0000 | [diff] [blame] | 155 | void DebugLinesSubsection::setCodeSize(uint32_t Size) { CodeSize = Size; } |
Zachary Turner | 8a2ebfb | 2017-05-01 23:27:42 +0000 | [diff] [blame] | 156 | |
Zachary Turner | 8c099fe | 2017-05-30 16:36:15 +0000 | [diff] [blame] | 157 | void DebugLinesSubsection::setFlags(LineFlags Flags) { this->Flags = Flags; } |
Zachary Turner | 8a2ebfb | 2017-05-01 23:27:42 +0000 | [diff] [blame] | 158 | |
Zachary Turner | 8c099fe | 2017-05-30 16:36:15 +0000 | [diff] [blame] | 159 | bool DebugLinesSubsection::hasColumnInfo() const { |
Zachary Turner | 8a2ebfb | 2017-05-01 23:27:42 +0000 | [diff] [blame] | 160 | return Flags & LF_HaveColumns; |
| 161 | } |