blob: c97384520c8fe47ef6ad2769b06ea315e910c352 [file] [log] [blame]
Zachary Turner8c099fe2017-05-30 16:36:15 +00001//===- DebugLinesSubsection.cpp -------------------------------*- C++-*-===//
Zachary Turnerc37cb0c2017-04-27 16:12:16 +00002//
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 Turner8c099fe2017-05-30 16:36:15 +000010#include "llvm/DebugInfo/CodeView/DebugLinesSubsection.h"
Zachary Turnerc37cb0c2017-04-27 16:12:16 +000011
12#include "llvm/DebugInfo/CodeView/CodeViewError.h"
Zachary Turner8c099fe2017-05-30 16:36:15 +000013#include "llvm/DebugInfo/CodeView/DebugChecksumsSubsection.h"
14#include "llvm/DebugInfo/CodeView/DebugSubsectionRecord.h"
Zachary Turnercf468d82017-05-03 17:11:40 +000015#include "llvm/DebugInfo/CodeView/StringTable.h"
Zachary Turnerc37cb0c2017-04-27 16:12:16 +000016
17using namespace llvm;
18using namespace llvm::codeview;
19
20Error LineColumnExtractor::extract(BinaryStreamRef Stream, uint32_t &Len,
21 LineColumnEntry &Item,
22 const LineFragmentHeader *Header) {
23 using namespace codeview;
24 const LineBlockFragmentHeader *BlockHeader;
25 BinaryStreamReader Reader(Stream);
26 if (auto EC = Reader.readObject(BlockHeader))
27 return EC;
Zachary Turner5b6e4e02017-04-29 01:13:21 +000028 bool HasColumn = Header->Flags & uint16_t(LF_HaveColumns);
Zachary Turnerc37cb0c2017-04-27 16:12:16 +000029 uint32_t LineInfoSize =
30 BlockHeader->NumLines *
31 (sizeof(LineNumberEntry) + (HasColumn ? sizeof(ColumnNumberEntry) : 0));
32 if (BlockHeader->BlockSize < sizeof(LineBlockFragmentHeader))
33 return make_error<CodeViewError>(cv_error_code::corrupt_record,
34 "Invalid line block record size");
35 uint32_t Size = BlockHeader->BlockSize - sizeof(LineBlockFragmentHeader);
36 if (LineInfoSize > Size)
37 return make_error<CodeViewError>(cv_error_code::corrupt_record,
38 "Invalid line block record size");
39 // The value recorded in BlockHeader->BlockSize includes the size of
40 // LineBlockFragmentHeader.
41 Len = BlockHeader->BlockSize;
42 Item.NameIndex = BlockHeader->NameIndex;
43 if (auto EC = Reader.readArray(Item.LineNumbers, BlockHeader->NumLines))
44 return EC;
45 if (HasColumn) {
46 if (auto EC = Reader.readArray(Item.Columns, BlockHeader->NumLines))
47 return EC;
48 }
49 return Error::success();
50}
51
Zachary Turner8c099fe2017-05-30 16:36:15 +000052DebugLinesSubsectionRef::DebugLinesSubsectionRef()
53 : DebugSubsectionRef(DebugSubsectionKind::Lines) {}
Zachary Turnerc37cb0c2017-04-27 16:12:16 +000054
Zachary Turner8c099fe2017-05-30 16:36:15 +000055Error DebugLinesSubsectionRef::initialize(BinaryStreamReader Reader) {
Zachary Turnerc37cb0c2017-04-27 16:12:16 +000056 if (auto EC = Reader.readObject(Header))
57 return EC;
58
59 if (auto EC =
60 Reader.readArray(LinesAndColumns, Reader.bytesRemaining(), Header))
61 return EC;
62
63 return Error::success();
64}
Zachary Turner5b6e4e02017-04-29 01:13:21 +000065
Zachary Turner8c099fe2017-05-30 16:36:15 +000066bool DebugLinesSubsectionRef::hasColumnInfo() const {
Zachary Turner7cc13e52017-05-01 16:46:39 +000067 return !!(Header->Flags & LF_HaveColumns);
Zachary Turner5b6e4e02017-04-29 01:13:21 +000068}
Zachary Turner8a2ebfb2017-05-01 23:27:42 +000069
Zachary Turner8c099fe2017-05-30 16:36:15 +000070DebugLinesSubsection::DebugLinesSubsection(DebugChecksumsSubsection &Checksums,
71 StringTable &Strings)
72 : DebugSubsection(DebugSubsectionKind::Lines), Checksums(Checksums) {}
Zachary Turner8a2ebfb2017-05-01 23:27:42 +000073
Zachary Turner8c099fe2017-05-30 16:36:15 +000074void DebugLinesSubsection::createBlock(StringRef FileName) {
Zachary Turnercf468d82017-05-03 17:11:40 +000075 uint32_t Offset = Checksums.mapChecksumOffset(FileName);
76
77 Blocks.emplace_back(Offset);
Zachary Turner8a2ebfb2017-05-01 23:27:42 +000078}
79
Zachary Turner8c099fe2017-05-30 16:36:15 +000080void DebugLinesSubsection::addLineInfo(uint32_t Offset, const LineInfo &Line) {
Zachary Turner8a2ebfb2017-05-01 23:27:42 +000081 Block &B = Blocks.back();
82 LineNumberEntry LNE;
83 LNE.Flags = Line.getRawData();
84 LNE.Offset = Offset;
85 B.Lines.push_back(LNE);
86}
87
Zachary Turner8c099fe2017-05-30 16:36:15 +000088void DebugLinesSubsection::addLineAndColumnInfo(uint32_t Offset,
89 const LineInfo &Line,
90 uint32_t ColStart,
91 uint32_t ColEnd) {
Zachary Turner8a2ebfb2017-05-01 23:27:42 +000092 Block &B = Blocks.back();
93 assert(B.Lines.size() == B.Columns.size());
94
95 addLineInfo(Offset, Line);
96 ColumnNumberEntry CNE;
97 CNE.StartColumn = ColStart;
98 CNE.EndColumn = ColEnd;
99 B.Columns.push_back(CNE);
100}
101
Zachary Turner8c099fe2017-05-30 16:36:15 +0000102Error DebugLinesSubsection::commit(BinaryStreamWriter &Writer) {
Zachary Turner8a2ebfb2017-05-01 23:27:42 +0000103 LineFragmentHeader Header;
104 Header.CodeSize = CodeSize;
105 Header.Flags = hasColumnInfo() ? LF_HaveColumns : 0;
106 Header.RelocOffset = RelocOffset;
107 Header.RelocSegment = RelocSegment;
108
109 if (auto EC = Writer.writeObject(Header))
110 return EC;
111
112 for (const auto &B : Blocks) {
113 LineBlockFragmentHeader BlockHeader;
114 assert(B.Lines.size() == B.Columns.size() || B.Columns.empty());
115
116 BlockHeader.NumLines = B.Lines.size();
117 BlockHeader.BlockSize = sizeof(LineBlockFragmentHeader);
118 BlockHeader.BlockSize += BlockHeader.NumLines * sizeof(LineNumberEntry);
119 if (hasColumnInfo())
120 BlockHeader.BlockSize += BlockHeader.NumLines * sizeof(ColumnNumberEntry);
121 BlockHeader.NameIndex = B.ChecksumBufferOffset;
122 if (auto EC = Writer.writeObject(BlockHeader))
123 return EC;
124
125 if (auto EC = Writer.writeArray(makeArrayRef(B.Lines)))
126 return EC;
127
128 if (hasColumnInfo()) {
129 if (auto EC = Writer.writeArray(makeArrayRef(B.Columns)))
130 return EC;
131 }
132 }
133 return Error::success();
134}
135
Zachary Turner8c099fe2017-05-30 16:36:15 +0000136uint32_t DebugLinesSubsection::calculateSerializedLength() {
Zachary Turner8a2ebfb2017-05-01 23:27:42 +0000137 uint32_t Size = sizeof(LineFragmentHeader);
138 for (const auto &B : Blocks) {
139 Size += sizeof(LineBlockFragmentHeader);
140 Size += B.Lines.size() * sizeof(LineNumberEntry);
141 if (hasColumnInfo())
142 Size += B.Columns.size() * sizeof(ColumnNumberEntry);
143 }
144 return Size;
145}
146
Zachary Turner8c099fe2017-05-30 16:36:15 +0000147void DebugLinesSubsection::setRelocationAddress(uint16_t Segment,
148 uint16_t Offset) {
Zachary Turner8a2ebfb2017-05-01 23:27:42 +0000149 RelocOffset = Offset;
150 RelocSegment = Segment;
151}
152
Zachary Turner8c099fe2017-05-30 16:36:15 +0000153void DebugLinesSubsection::setCodeSize(uint32_t Size) { CodeSize = Size; }
Zachary Turner8a2ebfb2017-05-01 23:27:42 +0000154
Zachary Turner8c099fe2017-05-30 16:36:15 +0000155void DebugLinesSubsection::setFlags(LineFlags Flags) { this->Flags = Flags; }
Zachary Turner8a2ebfb2017-05-01 23:27:42 +0000156
Zachary Turner8c099fe2017-05-30 16:36:15 +0000157bool DebugLinesSubsection::hasColumnInfo() const {
Zachary Turner8a2ebfb2017-05-01 23:27:42 +0000158 return Flags & LF_HaveColumns;
159}