blob: fbcad61d60a623d77bd77a08ac88a7814e1bdcb5 [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"
Zachary Turner591312c2017-05-30 17:13:33 +000014#include "llvm/DebugInfo/CodeView/DebugStringTableSubsection.h"
Zachary Turner8c099fe2017-05-30 16:36:15 +000015#include "llvm/DebugInfo/CodeView/DebugSubsectionRecord.h"
Zachary Turnerc37cb0c2017-04-27 16:12:16 +000016
17using namespace llvm;
18using namespace llvm::codeview;
19
Zachary Turner7e62cd12017-06-09 17:54:36 +000020Error LineColumnExtractor::operator()(BinaryStreamRef Stream, uint32_t &Len,
21 LineColumnEntry &Item) {
Zachary Turnerc37cb0c2017-04-27 16:12:16 +000022 using namespace codeview;
23 const LineBlockFragmentHeader *BlockHeader;
24 BinaryStreamReader Reader(Stream);
25 if (auto EC = Reader.readObject(BlockHeader))
26 return EC;
Zachary Turner5b6e4e02017-04-29 01:13:21 +000027 bool HasColumn = Header->Flags & uint16_t(LF_HaveColumns);
Zachary Turnerc37cb0c2017-04-27 16:12:16 +000028 uint32_t LineInfoSize =
29 BlockHeader->NumLines *
30 (sizeof(LineNumberEntry) + (HasColumn ? sizeof(ColumnNumberEntry) : 0));
31 if (BlockHeader->BlockSize < sizeof(LineBlockFragmentHeader))
32 return make_error<CodeViewError>(cv_error_code::corrupt_record,
33 "Invalid line block record size");
34 uint32_t Size = BlockHeader->BlockSize - sizeof(LineBlockFragmentHeader);
35 if (LineInfoSize > Size)
36 return make_error<CodeViewError>(cv_error_code::corrupt_record,
37 "Invalid line block record size");
38 // The value recorded in BlockHeader->BlockSize includes the size of
39 // LineBlockFragmentHeader.
40 Len = BlockHeader->BlockSize;
41 Item.NameIndex = BlockHeader->NameIndex;
42 if (auto EC = Reader.readArray(Item.LineNumbers, BlockHeader->NumLines))
43 return EC;
44 if (HasColumn) {
45 if (auto EC = Reader.readArray(Item.Columns, BlockHeader->NumLines))
46 return EC;
47 }
48 return Error::success();
49}
50
Zachary Turner8c099fe2017-05-30 16:36:15 +000051DebugLinesSubsectionRef::DebugLinesSubsectionRef()
52 : DebugSubsectionRef(DebugSubsectionKind::Lines) {}
Zachary Turnerc37cb0c2017-04-27 16:12:16 +000053
Zachary Turner8c099fe2017-05-30 16:36:15 +000054Error DebugLinesSubsectionRef::initialize(BinaryStreamReader Reader) {
Zachary Turnerc37cb0c2017-04-27 16:12:16 +000055 if (auto EC = Reader.readObject(Header))
56 return EC;
57
Zachary Turner7e62cd12017-06-09 17:54:36 +000058 LinesAndColumns.getExtractor().Header = Header;
59 if (auto EC = Reader.readArray(LinesAndColumns, Reader.bytesRemaining()))
Zachary Turnerc37cb0c2017-04-27 16:12:16 +000060 return EC;
61
62 return Error::success();
63}
Zachary Turner5b6e4e02017-04-29 01:13:21 +000064
Zachary Turner8c099fe2017-05-30 16:36:15 +000065bool DebugLinesSubsectionRef::hasColumnInfo() const {
Zachary Turner7cc13e52017-05-01 16:46:39 +000066 return !!(Header->Flags & LF_HaveColumns);
Zachary Turner5b6e4e02017-04-29 01:13:21 +000067}
Zachary Turner8a2ebfb2017-05-01 23:27:42 +000068
Zachary Turner8c099fe2017-05-30 16:36:15 +000069DebugLinesSubsection::DebugLinesSubsection(DebugChecksumsSubsection &Checksums,
Zachary Turner591312c2017-05-30 17:13:33 +000070 DebugStringTableSubsection &Strings)
Zachary Turner8c099fe2017-05-30 16:36:15 +000071 : DebugSubsection(DebugSubsectionKind::Lines), Checksums(Checksums) {}
Zachary Turner8a2ebfb2017-05-01 23:27:42 +000072
Zachary Turner8c099fe2017-05-30 16:36:15 +000073void DebugLinesSubsection::createBlock(StringRef FileName) {
Zachary Turnercf468d82017-05-03 17:11:40 +000074 uint32_t Offset = Checksums.mapChecksumOffset(FileName);
75
76 Blocks.emplace_back(Offset);
Zachary Turner8a2ebfb2017-05-01 23:27:42 +000077}
78
Zachary Turner8c099fe2017-05-30 16:36:15 +000079void DebugLinesSubsection::addLineInfo(uint32_t Offset, const LineInfo &Line) {
Zachary Turner8a2ebfb2017-05-01 23:27:42 +000080 Block &B = Blocks.back();
81 LineNumberEntry LNE;
82 LNE.Flags = Line.getRawData();
83 LNE.Offset = Offset;
84 B.Lines.push_back(LNE);
85}
86
Zachary Turner8c099fe2017-05-30 16:36:15 +000087void DebugLinesSubsection::addLineAndColumnInfo(uint32_t Offset,
88 const LineInfo &Line,
89 uint32_t ColStart,
90 uint32_t ColEnd) {
Zachary Turner8a2ebfb2017-05-01 23:27:42 +000091 Block &B = Blocks.back();
92 assert(B.Lines.size() == B.Columns.size());
93
94 addLineInfo(Offset, Line);
95 ColumnNumberEntry CNE;
96 CNE.StartColumn = ColStart;
97 CNE.EndColumn = ColEnd;
98 B.Columns.push_back(CNE);
99}
100
Zachary Turner591312c2017-05-30 17:13:33 +0000101Error DebugLinesSubsection::commit(BinaryStreamWriter &Writer) const {
Zachary Turner8a2ebfb2017-05-01 23:27:42 +0000102 LineFragmentHeader Header;
103 Header.CodeSize = CodeSize;
104 Header.Flags = hasColumnInfo() ? LF_HaveColumns : 0;
105 Header.RelocOffset = RelocOffset;
106 Header.RelocSegment = RelocSegment;
107
108 if (auto EC = Writer.writeObject(Header))
109 return EC;
110
111 for (const auto &B : Blocks) {
112 LineBlockFragmentHeader BlockHeader;
113 assert(B.Lines.size() == B.Columns.size() || B.Columns.empty());
114
115 BlockHeader.NumLines = B.Lines.size();
116 BlockHeader.BlockSize = sizeof(LineBlockFragmentHeader);
117 BlockHeader.BlockSize += BlockHeader.NumLines * sizeof(LineNumberEntry);
118 if (hasColumnInfo())
119 BlockHeader.BlockSize += BlockHeader.NumLines * sizeof(ColumnNumberEntry);
120 BlockHeader.NameIndex = B.ChecksumBufferOffset;
121 if (auto EC = Writer.writeObject(BlockHeader))
122 return EC;
123
124 if (auto EC = Writer.writeArray(makeArrayRef(B.Lines)))
125 return EC;
126
127 if (hasColumnInfo()) {
128 if (auto EC = Writer.writeArray(makeArrayRef(B.Columns)))
129 return EC;
130 }
131 }
132 return Error::success();
133}
134
Zachary Turner591312c2017-05-30 17:13:33 +0000135uint32_t DebugLinesSubsection::calculateSerializedSize() const {
Zachary Turner8a2ebfb2017-05-01 23:27:42 +0000136 uint32_t Size = sizeof(LineFragmentHeader);
137 for (const auto &B : Blocks) {
138 Size += sizeof(LineBlockFragmentHeader);
139 Size += B.Lines.size() * sizeof(LineNumberEntry);
140 if (hasColumnInfo())
141 Size += B.Columns.size() * sizeof(ColumnNumberEntry);
142 }
143 return Size;
144}
145
Zachary Turner8c099fe2017-05-30 16:36:15 +0000146void DebugLinesSubsection::setRelocationAddress(uint16_t Segment,
Bob Haarmanfdf499b2017-06-09 01:18:10 +0000147 uint32_t Offset) {
Zachary Turner8a2ebfb2017-05-01 23:27:42 +0000148 RelocOffset = Offset;
149 RelocSegment = Segment;
150}
151
Zachary Turner8c099fe2017-05-30 16:36:15 +0000152void DebugLinesSubsection::setCodeSize(uint32_t Size) { CodeSize = Size; }
Zachary Turner8a2ebfb2017-05-01 23:27:42 +0000153
Zachary Turner8c099fe2017-05-30 16:36:15 +0000154void DebugLinesSubsection::setFlags(LineFlags Flags) { this->Flags = Flags; }
Zachary Turner8a2ebfb2017-05-01 23:27:42 +0000155
Zachary Turner8c099fe2017-05-30 16:36:15 +0000156bool DebugLinesSubsection::hasColumnInfo() const {
Zachary Turner8a2ebfb2017-05-01 23:27:42 +0000157 return Flags & LF_HaveColumns;
158}