blob: 5376b465a491f216488ed8b872c442ad69a736e2 [file] [log] [blame]
Eugene Zelenkoe94042c2017-02-27 23:43:14 +00001//===- DWARFDebugLine.cpp -------------------------------------------------===//
Benjamin Kramer5acab502011-09-15 02:12:05 +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
Paul Robinson9d4eb692017-05-01 23:27:55 +000010#include "llvm/DebugInfo/DWARF/DWARFDebugLine.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000011#include "llvm/ADT/SmallString.h"
Eugene Zelenko2db0cfa2017-06-23 21:57:40 +000012#include "llvm/ADT/SmallVector.h"
13#include "llvm/ADT/StringRef.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000014#include "llvm/BinaryFormat/Dwarf.h"
Paul Robinson2bc38732017-05-02 21:40:47 +000015#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000016#include "llvm/DebugInfo/DWARF/DWARFRelocMap.h"
Benjamin Kramer5acab502011-09-15 02:12:05 +000017#include "llvm/Support/Format.h"
Alexey Samsonov45be7932012-08-30 07:49:50 +000018#include "llvm/Support/Path.h"
Benjamin Kramer5acab502011-09-15 02:12:05 +000019#include "llvm/Support/raw_ostream.h"
Benjamin Kramera57c46a2011-09-15 02:19:33 +000020#include <algorithm>
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000021#include <cassert>
22#include <cinttypes>
23#include <cstdint>
24#include <cstdio>
25#include <utility>
26
Benjamin Kramer5acab502011-09-15 02:12:05 +000027using namespace llvm;
28using namespace dwarf;
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000029
Eugene Zelenko2db0cfa2017-06-23 21:57:40 +000030using FileLineInfoKind = DILineInfoSpecifier::FileLineInfoKind;
31
Paul Robinson2bc38732017-05-02 21:40:47 +000032namespace {
Eugene Zelenko2db0cfa2017-06-23 21:57:40 +000033
Paul Robinson2bc38732017-05-02 21:40:47 +000034struct ContentDescriptor {
35 dwarf::LineNumberEntryFormat Type;
36 dwarf::Form Form;
37};
Eugene Zelenko2db0cfa2017-06-23 21:57:40 +000038
39using ContentDescriptors = SmallVector<ContentDescriptor, 4>;
40
Paul Robinson2bc38732017-05-02 21:40:47 +000041} // end anonmyous namespace
Benjamin Kramer5acab502011-09-15 02:12:05 +000042
Dehao Chen1b54fce2016-04-28 22:09:37 +000043DWARFDebugLine::Prologue::Prologue() { clear(); }
Alexey Samsonov836b1ae2014-04-29 21:28:13 +000044
45void DWARFDebugLine::Prologue::clear() {
Paul Robinson75c068c2017-06-26 18:43:01 +000046 TotalLength = PrologueLength = 0;
47 SegSelectorSize = 0;
Alexey Samsonov836b1ae2014-04-29 21:28:13 +000048 MinInstLength = MaxOpsPerInst = DefaultIsStmt = LineBase = LineRange = 0;
49 OpcodeBase = 0;
Paul Robinson75c068c2017-06-26 18:43:01 +000050 FormParams = DWARFFormParams({0, 0, DWARF32});
Alexey Samsonov836b1ae2014-04-29 21:28:13 +000051 StandardOpcodeLengths.clear();
52 IncludeDirectories.clear();
53 FileNames.clear();
54}
55
Benjamin Kramer5acab502011-09-15 02:12:05 +000056void DWARFDebugLine::Prologue::dump(raw_ostream &OS) const {
57 OS << "Line table prologue:\n"
Ed Maste6d0bee52015-05-28 15:38:17 +000058 << format(" total_length: 0x%8.8" PRIx64 "\n", TotalLength)
Paul Robinson75c068c2017-06-26 18:43:01 +000059 << format(" version: %u\n", getVersion());
60 if (getVersion() >= 5)
61 OS << format(" address_size: %u\n", getAddressSize())
62 << format(" seg_select_size: %u\n", SegSelectorSize);
63 OS << format(" prologue_length: 0x%8.8" PRIx64 "\n", PrologueLength)
David Blaikie1d4736e2014-02-24 23:58:54 +000064 << format(" min_inst_length: %u\n", MinInstLength)
Paul Robinson75c068c2017-06-26 18:43:01 +000065 << format(getVersion() >= 4 ? "max_ops_per_inst: %u\n" : "", MaxOpsPerInst)
David Blaikie1d4736e2014-02-24 23:58:54 +000066 << format(" default_is_stmt: %u\n", DefaultIsStmt)
67 << format(" line_base: %i\n", LineBase)
68 << format(" line_range: %u\n", LineRange)
69 << format(" opcode_base: %u\n", OpcodeBase);
Benjamin Kramer5acab502011-09-15 02:12:05 +000070
Paul Robinson9d4eb692017-05-01 23:27:55 +000071 for (uint32_t I = 0; I != StandardOpcodeLengths.size(); ++I)
Mehdi Amini149f6ea2016-10-05 05:59:29 +000072 OS << format("standard_opcode_lengths[%s] = %u\n",
Paul Robinson9d4eb692017-05-01 23:27:55 +000073 LNStandardString(I + 1).data(), StandardOpcodeLengths[I]);
Benjamin Kramer5acab502011-09-15 02:12:05 +000074
75 if (!IncludeDirectories.empty())
Paul Robinson9d4eb692017-05-01 23:27:55 +000076 for (uint32_t I = 0; I != IncludeDirectories.size(); ++I)
77 OS << format("include_directories[%3u] = '", I + 1)
78 << IncludeDirectories[I] << "'\n";
Benjamin Kramer5acab502011-09-15 02:12:05 +000079
80 if (!FileNames.empty()) {
Paul Robinson6d0484f2017-12-15 23:21:52 +000081 OS << " Dir Mod Time File Len File Name\n"
82 << " ---- ---------- ---------- -----------"
83 "----------------\n";
Paul Robinson9d4eb692017-05-01 23:27:55 +000084 for (uint32_t I = 0; I != FileNames.size(); ++I) {
85 const FileNameEntry &FileEntry = FileNames[I];
Paul Robinson6d0484f2017-12-15 23:21:52 +000086 OS << format("file_names[%3u] %4" PRIu64 " ", I + 1, FileEntry.DirIdx)
87 << format("0x%8.8" PRIx64 " 0x%8.8" PRIx64 " ", FileEntry.ModTime,
88 FileEntry.Length)
89 << FileEntry.Name << '\n';
Benjamin Kramer5acab502011-09-15 02:12:05 +000090 }
91 }
92}
93
Paul Robinson2bc38732017-05-02 21:40:47 +000094// Parse v2-v4 directory and file tables.
95static void
Paul Robinson17536b92017-06-29 16:52:08 +000096parseV2DirFileTables(const DWARFDataExtractor &DebugLineData,
97 uint32_t *OffsetPtr, uint64_t EndPrologueOffset,
Paul Robinson2bc38732017-05-02 21:40:47 +000098 std::vector<StringRef> &IncludeDirectories,
99 std::vector<DWARFDebugLine::FileNameEntry> &FileNames) {
100 while (*OffsetPtr < EndPrologueOffset) {
101 StringRef S = DebugLineData.getCStrRef(OffsetPtr);
102 if (S.empty())
103 break;
104 IncludeDirectories.push_back(S);
105 }
106
107 while (*OffsetPtr < EndPrologueOffset) {
108 StringRef Name = DebugLineData.getCStrRef(OffsetPtr);
109 if (Name.empty())
110 break;
111 DWARFDebugLine::FileNameEntry FileEntry;
112 FileEntry.Name = Name;
113 FileEntry.DirIdx = DebugLineData.getULEB128(OffsetPtr);
114 FileEntry.ModTime = DebugLineData.getULEB128(OffsetPtr);
115 FileEntry.Length = DebugLineData.getULEB128(OffsetPtr);
116 FileNames.push_back(FileEntry);
117 }
118}
119
120// Parse v5 directory/file entry content descriptions.
121// Returns the descriptors, or an empty vector if we did not find a path or
122// ran off the end of the prologue.
123static ContentDescriptors
Paul Robinson17536b92017-06-29 16:52:08 +0000124parseV5EntryFormat(const DWARFDataExtractor &DebugLineData, uint32_t *OffsetPtr,
Paul Robinson6d0484f2017-12-15 23:21:52 +0000125 uint64_t EndPrologueOffset) {
Paul Robinson2bc38732017-05-02 21:40:47 +0000126 ContentDescriptors Descriptors;
127 int FormatCount = DebugLineData.getU8(OffsetPtr);
128 bool HasPath = false;
129 for (int I = 0; I != FormatCount; ++I) {
130 if (*OffsetPtr >= EndPrologueOffset)
131 return ContentDescriptors();
132 ContentDescriptor Descriptor;
133 Descriptor.Type =
134 dwarf::LineNumberEntryFormat(DebugLineData.getULEB128(OffsetPtr));
135 Descriptor.Form = dwarf::Form(DebugLineData.getULEB128(OffsetPtr));
136 if (Descriptor.Type == dwarf::DW_LNCT_path)
137 HasPath = true;
138 Descriptors.push_back(Descriptor);
139 }
140 return HasPath ? Descriptors : ContentDescriptors();
141}
142
143static bool
Paul Robinson17536b92017-06-29 16:52:08 +0000144parseV5DirFileTables(const DWARFDataExtractor &DebugLineData,
145 uint32_t *OffsetPtr, uint64_t EndPrologueOffset,
Paul Robinsone5400f82017-11-07 19:57:12 +0000146 const DWARFFormParams &FormParams, const DWARFUnit *U,
Paul Robinson6d0484f2017-12-15 23:21:52 +0000147 std::vector<StringRef> &IncludeDirectories,
Paul Robinson2bc38732017-05-02 21:40:47 +0000148 std::vector<DWARFDebugLine::FileNameEntry> &FileNames) {
149 // Get the directory entry description.
150 ContentDescriptors DirDescriptors =
Paul Robinson6d0484f2017-12-15 23:21:52 +0000151 parseV5EntryFormat(DebugLineData, OffsetPtr, EndPrologueOffset);
Paul Robinson2bc38732017-05-02 21:40:47 +0000152 if (DirDescriptors.empty())
153 return false;
154
155 // Get the directory entries, according to the format described above.
156 int DirEntryCount = DebugLineData.getU8(OffsetPtr);
157 for (int I = 0; I != DirEntryCount; ++I) {
158 if (*OffsetPtr >= EndPrologueOffset)
159 return false;
160 for (auto Descriptor : DirDescriptors) {
161 DWARFFormValue Value(Descriptor.Form);
162 switch (Descriptor.Type) {
163 case DW_LNCT_path:
Paul Robinsone5400f82017-11-07 19:57:12 +0000164 if (!Value.extractValue(DebugLineData, OffsetPtr, FormParams, U))
Paul Robinson2bc38732017-05-02 21:40:47 +0000165 return false;
166 IncludeDirectories.push_back(Value.getAsCString().getValue());
167 break;
168 default:
Paul Robinson75c068c2017-06-26 18:43:01 +0000169 if (!Value.skipValue(DebugLineData, OffsetPtr, FormParams))
Paul Robinson2bc38732017-05-02 21:40:47 +0000170 return false;
171 }
172 }
173 }
174
175 // Get the file entry description.
176 ContentDescriptors FileDescriptors =
Paul Robinson6d0484f2017-12-15 23:21:52 +0000177 parseV5EntryFormat(DebugLineData, OffsetPtr, EndPrologueOffset);
Paul Robinson2bc38732017-05-02 21:40:47 +0000178 if (FileDescriptors.empty())
179 return false;
180
181 // Get the file entries, according to the format described above.
182 int FileEntryCount = DebugLineData.getU8(OffsetPtr);
183 for (int I = 0; I != FileEntryCount; ++I) {
184 if (*OffsetPtr >= EndPrologueOffset)
185 return false;
186 DWARFDebugLine::FileNameEntry FileEntry;
187 for (auto Descriptor : FileDescriptors) {
188 DWARFFormValue Value(Descriptor.Form);
Paul Robinsone5400f82017-11-07 19:57:12 +0000189 if (!Value.extractValue(DebugLineData, OffsetPtr, FormParams, U))
Paul Robinson2bc38732017-05-02 21:40:47 +0000190 return false;
191 switch (Descriptor.Type) {
192 case DW_LNCT_path:
193 FileEntry.Name = Value.getAsCString().getValue();
194 break;
195 case DW_LNCT_directory_index:
196 FileEntry.DirIdx = Value.getAsUnsignedConstant().getValue();
197 break;
198 case DW_LNCT_timestamp:
199 FileEntry.ModTime = Value.getAsUnsignedConstant().getValue();
200 break;
201 case DW_LNCT_size:
202 FileEntry.Length = Value.getAsUnsignedConstant().getValue();
203 break;
Paul Robinson6d0484f2017-12-15 23:21:52 +0000204 // FIXME: Add MD5
Paul Robinson2bc38732017-05-02 21:40:47 +0000205 default:
206 break;
207 }
208 }
209 FileNames.push_back(FileEntry);
210 }
211 return true;
212}
213
Paul Robinson17536b92017-06-29 16:52:08 +0000214bool DWARFDebugLine::Prologue::parse(const DWARFDataExtractor &DebugLineData,
Paul Robinsone5400f82017-11-07 19:57:12 +0000215 uint32_t *OffsetPtr, const DWARFUnit *U) {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000216 const uint64_t PrologueOffset = *OffsetPtr;
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000217
218 clear();
Paul Robinson9d4eb692017-05-01 23:27:55 +0000219 TotalLength = DebugLineData.getU32(OffsetPtr);
Ed Maste6d0bee52015-05-28 15:38:17 +0000220 if (TotalLength == UINT32_MAX) {
Paul Robinson75c068c2017-06-26 18:43:01 +0000221 FormParams.Format = dwarf::DWARF64;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000222 TotalLength = DebugLineData.getU64(OffsetPtr);
Paul Robinson75c068c2017-06-26 18:43:01 +0000223 } else if (TotalLength >= 0xffffff00) {
Ed Maste6d0bee52015-05-28 15:38:17 +0000224 return false;
225 }
Paul Robinson75c068c2017-06-26 18:43:01 +0000226 FormParams.Version = DebugLineData.getU16(OffsetPtr);
227 if (getVersion() < 2)
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000228 return false;
229
Paul Robinson75c068c2017-06-26 18:43:01 +0000230 if (getVersion() >= 5) {
231 FormParams.AddrSize = DebugLineData.getU8(OffsetPtr);
Paul Robinson63811a42017-11-22 15:33:17 +0000232 assert((DebugLineData.getAddressSize() == 0 ||
233 DebugLineData.getAddressSize() == getAddressSize()) &&
Paul Robinson75c068c2017-06-26 18:43:01 +0000234 "Line table header and data extractor disagree");
Paul Robinson2bc38732017-05-02 21:40:47 +0000235 SegSelectorSize = DebugLineData.getU8(OffsetPtr);
236 }
237
Paul Robinson9d4eb692017-05-01 23:27:55 +0000238 PrologueLength = DebugLineData.getUnsigned(OffsetPtr, sizeofPrologueLength());
239 const uint64_t EndPrologueOffset = PrologueLength + *OffsetPtr;
240 MinInstLength = DebugLineData.getU8(OffsetPtr);
Paul Robinson75c068c2017-06-26 18:43:01 +0000241 if (getVersion() >= 4)
Paul Robinson9d4eb692017-05-01 23:27:55 +0000242 MaxOpsPerInst = DebugLineData.getU8(OffsetPtr);
243 DefaultIsStmt = DebugLineData.getU8(OffsetPtr);
244 LineBase = DebugLineData.getU8(OffsetPtr);
245 LineRange = DebugLineData.getU8(OffsetPtr);
246 OpcodeBase = DebugLineData.getU8(OffsetPtr);
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000247
248 StandardOpcodeLengths.reserve(OpcodeBase - 1);
Paul Robinson9d4eb692017-05-01 23:27:55 +0000249 for (uint32_t I = 1; I < OpcodeBase; ++I) {
250 uint8_t OpLen = DebugLineData.getU8(OffsetPtr);
251 StandardOpcodeLengths.push_back(OpLen);
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000252 }
253
Paul Robinson75c068c2017-06-26 18:43:01 +0000254 if (getVersion() >= 5) {
Paul Robinson2bc38732017-05-02 21:40:47 +0000255 if (!parseV5DirFileTables(DebugLineData, OffsetPtr, EndPrologueOffset,
Paul Robinson6d0484f2017-12-15 23:21:52 +0000256 getFormParams(), U, IncludeDirectories,
Paul Robinsone5400f82017-11-07 19:57:12 +0000257 FileNames)) {
Paul Robinson2bc38732017-05-02 21:40:47 +0000258 fprintf(stderr,
259 "warning: parsing line table prologue at 0x%8.8" PRIx64
260 " found an invalid directory or file table description at"
261 " 0x%8.8" PRIx64 "\n", PrologueOffset, (uint64_t)*OffsetPtr);
262 return false;
263 }
264 } else
265 parseV2DirFileTables(DebugLineData, OffsetPtr, EndPrologueOffset,
266 IncludeDirectories, FileNames);
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000267
Paul Robinson9d4eb692017-05-01 23:27:55 +0000268 if (*OffsetPtr != EndPrologueOffset) {
269 fprintf(stderr,
270 "warning: parsing line table prologue at 0x%8.8" PRIx64
271 " should have ended at 0x%8.8" PRIx64
272 " but it ended at 0x%8.8" PRIx64 "\n",
273 PrologueOffset, EndPrologueOffset, (uint64_t)*OffsetPtr);
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000274 return false;
275 }
276 return true;
277}
278
Paul Robinson9d4eb692017-05-01 23:27:55 +0000279DWARFDebugLine::Row::Row(bool DefaultIsStmt) { reset(DefaultIsStmt); }
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000280
Benjamin Kramer5acab502011-09-15 02:12:05 +0000281void DWARFDebugLine::Row::postAppend() {
282 BasicBlock = false;
283 PrologueEnd = false;
284 EpilogueBegin = false;
285}
286
Paul Robinson9d4eb692017-05-01 23:27:55 +0000287void DWARFDebugLine::Row::reset(bool DefaultIsStmt) {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000288 Address = 0;
289 Line = 1;
290 Column = 0;
291 File = 1;
292 Isa = 0;
Diego Novillo5b5cf502014-02-14 19:27:53 +0000293 Discriminator = 0;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000294 IsStmt = DefaultIsStmt;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000295 BasicBlock = false;
296 EndSequence = false;
297 PrologueEnd = false;
298 EpilogueBegin = false;
299}
300
Greg Clayton67070462017-05-02 22:48:52 +0000301void DWARFDebugLine::Row::dumpTableHeader(raw_ostream &OS) {
302 OS << "Address Line Column File ISA Discriminator Flags\n"
303 << "------------------ ------ ------ ------ --- ------------- "
304 "-------------\n";
305}
306
Benjamin Kramer5acab502011-09-15 02:12:05 +0000307void DWARFDebugLine::Row::dump(raw_ostream &OS) const {
Benjamin Kramerf3da5292011-11-05 08:57:40 +0000308 OS << format("0x%16.16" PRIx64 " %6u %6u", Address, Line, Column)
Diego Novillo5b5cf502014-02-14 19:27:53 +0000309 << format(" %6u %3u %13u ", File, Isa, Discriminator)
Dehao Chen1b54fce2016-04-28 22:09:37 +0000310 << (IsStmt ? " is_stmt" : "") << (BasicBlock ? " basic_block" : "")
Benjamin Kramer5acab502011-09-15 02:12:05 +0000311 << (PrologueEnd ? " prologue_end" : "")
312 << (EpilogueBegin ? " epilogue_begin" : "")
Dehao Chen1b54fce2016-04-28 22:09:37 +0000313 << (EndSequence ? " end_sequence" : "") << '\n';
Benjamin Kramer5acab502011-09-15 02:12:05 +0000314}
315
Dehao Chen1b54fce2016-04-28 22:09:37 +0000316DWARFDebugLine::Sequence::Sequence() { reset(); }
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000317
318void DWARFDebugLine::Sequence::reset() {
319 LowPC = 0;
320 HighPC = 0;
321 FirstRowIndex = 0;
322 LastRowIndex = 0;
323 Empty = true;
324}
325
Dehao Chen1b54fce2016-04-28 22:09:37 +0000326DWARFDebugLine::LineTable::LineTable() { clear(); }
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000327
Benjamin Kramer5acab502011-09-15 02:12:05 +0000328void DWARFDebugLine::LineTable::dump(raw_ostream &OS) const {
329 Prologue.dump(OS);
330 OS << '\n';
331
332 if (!Rows.empty()) {
Greg Clayton67070462017-05-02 22:48:52 +0000333 Row::dumpTableHeader(OS);
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000334 for (const Row &R : Rows) {
335 R.dump(OS);
336 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000337 }
338}
339
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000340void DWARFDebugLine::LineTable::clear() {
341 Prologue.clear();
342 Rows.clear();
343 Sequences.clear();
344}
345
Alexey Samsonov110d5952014-04-30 00:09:19 +0000346DWARFDebugLine::ParsingState::ParsingState(struct LineTable *LT)
Eugene Zelenko2db0cfa2017-06-23 21:57:40 +0000347 : LineTable(LT) {
Alexey Samsonov110d5952014-04-30 00:09:19 +0000348 resetRowAndSequence();
349}
Nick Lewycky4d044922011-09-15 03:41:51 +0000350
Alexey Samsonov110d5952014-04-30 00:09:19 +0000351void DWARFDebugLine::ParsingState::resetRowAndSequence() {
352 Row.reset(LineTable->Prologue.DefaultIsStmt);
353 Sequence.reset();
354}
355
Paul Robinson9d4eb692017-05-01 23:27:55 +0000356void DWARFDebugLine::ParsingState::appendRowToMatrix(uint32_t Offset) {
Alexey Samsonov110d5952014-04-30 00:09:19 +0000357 if (Sequence.Empty) {
Alexey Samsonov947228c2012-08-07 11:46:57 +0000358 // Record the beginning of instruction sequence.
Alexey Samsonov110d5952014-04-30 00:09:19 +0000359 Sequence.Empty = false;
360 Sequence.LowPC = Row.Address;
361 Sequence.FirstRowIndex = RowNumber;
Alexey Samsonov947228c2012-08-07 11:46:57 +0000362 }
Alexey Samsonov110d5952014-04-30 00:09:19 +0000363 ++RowNumber;
364 LineTable->appendRow(Row);
365 if (Row.EndSequence) {
Alexey Samsonov947228c2012-08-07 11:46:57 +0000366 // Record the end of instruction sequence.
Alexey Samsonov110d5952014-04-30 00:09:19 +0000367 Sequence.HighPC = Row.Address;
368 Sequence.LastRowIndex = RowNumber;
369 if (Sequence.isValid())
370 LineTable->appendSequence(Sequence);
371 Sequence.reset();
Alexey Samsonov947228c2012-08-07 11:46:57 +0000372 }
Alexey Samsonov110d5952014-04-30 00:09:19 +0000373 Row.postAppend();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000374}
375
Benjamin Kramer5acab502011-09-15 02:12:05 +0000376const DWARFDebugLine::LineTable *
Paul Robinson9d4eb692017-05-01 23:27:55 +0000377DWARFDebugLine::getLineTable(uint32_t Offset) const {
378 LineTableConstIter Pos = LineTableMap.find(Offset);
379 if (Pos != LineTableMap.end())
380 return &Pos->second;
Craig Topper2617dcc2014-04-15 06:32:26 +0000381 return nullptr;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000382}
383
Benjamin Kramer679e1752011-09-15 20:43:18 +0000384const DWARFDebugLine::LineTable *
Paul Robinson511b54c2017-11-22 15:48:30 +0000385DWARFDebugLine::getOrParseLineTable(DWARFDataExtractor &DebugLineData,
Paul Robinsone5400f82017-11-07 19:57:12 +0000386 uint32_t Offset, const DWARFUnit *U) {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000387 std::pair<LineTableIter, bool> Pos =
388 LineTableMap.insert(LineTableMapTy::value_type(Offset, LineTable()));
389 LineTable *LT = &Pos.first->second;
390 if (Pos.second) {
Paul Robinsone5400f82017-11-07 19:57:12 +0000391 if (!LT->parse(DebugLineData, &Offset, U))
Craig Topper2617dcc2014-04-15 06:32:26 +0000392 return nullptr;
Benjamin Kramer679e1752011-09-15 20:43:18 +0000393 }
Alexey Samsonov110d5952014-04-30 00:09:19 +0000394 return LT;
Benjamin Kramer679e1752011-09-15 20:43:18 +0000395}
396
Paul Robinson511b54c2017-11-22 15:48:30 +0000397bool DWARFDebugLine::LineTable::parse(DWARFDataExtractor &DebugLineData,
Paul Robinsone5400f82017-11-07 19:57:12 +0000398 uint32_t *OffsetPtr, const DWARFUnit *U,
399 raw_ostream *OS) {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000400 const uint32_t DebugLineOffset = *OffsetPtr;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000401
Alexey Samsonov110d5952014-04-30 00:09:19 +0000402 clear();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000403
Paul Robinsone5400f82017-11-07 19:57:12 +0000404 if (!Prologue.parse(DebugLineData, OffsetPtr, U)) {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000405 // Restore our offset and return false to indicate failure!
Paul Robinson9d4eb692017-05-01 23:27:55 +0000406 *OffsetPtr = DebugLineOffset;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000407 return false;
408 }
409
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000410 if (OS)
411 Prologue.dump(*OS);
412
Paul Robinson9d4eb692017-05-01 23:27:55 +0000413 const uint32_t EndOffset =
414 DebugLineOffset + Prologue.TotalLength + Prologue.sizeofTotalLength();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000415
Paul Robinson511b54c2017-11-22 15:48:30 +0000416 // See if we should tell the data extractor the address size.
417 if (DebugLineData.getAddressSize() == 0)
418 DebugLineData.setAddressSize(Prologue.getAddressSize());
419 else
420 assert(Prologue.getAddressSize() == 0 ||
421 Prologue.getAddressSize() == DebugLineData.getAddressSize());
422
Alexey Samsonov110d5952014-04-30 00:09:19 +0000423 ParsingState State(this);
Benjamin Kramer112ec172011-09-15 21:59:13 +0000424
Paul Robinson9d4eb692017-05-01 23:27:55 +0000425 while (*OffsetPtr < EndOffset) {
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000426 if (OS)
427 *OS << format("0x%08.08" PRIx32 ": ", *OffsetPtr);
428
Paul Robinson9d4eb692017-05-01 23:27:55 +0000429 uint8_t Opcode = DebugLineData.getU8(OffsetPtr);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000430
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000431 if (OS)
432 *OS << format("%02.02" PRIx8 " ", Opcode);
433
Paul Robinson9d4eb692017-05-01 23:27:55 +0000434 if (Opcode == 0) {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000435 // Extended Opcodes always start with a zero opcode followed by
436 // a uleb128 length so you can skip ones you don't know about
Paul Robinson9d4eb692017-05-01 23:27:55 +0000437 uint64_t Len = DebugLineData.getULEB128(OffsetPtr);
Paul Robinsone0833342017-11-22 15:14:49 +0000438 uint32_t ExtOffset = *OffsetPtr;
439
440 // Tolerate zero-length; assume length is correct and soldier on.
441 if (Len == 0) {
442 if (OS)
443 *OS << "Badly formed extended line op (length 0)\n";
444 continue;
445 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000446
Paul Robinson9d4eb692017-05-01 23:27:55 +0000447 uint8_t SubOpcode = DebugLineData.getU8(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000448 if (OS)
449 *OS << LNExtendedString(SubOpcode);
Paul Robinson9d4eb692017-05-01 23:27:55 +0000450 switch (SubOpcode) {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000451 case DW_LNE_end_sequence:
452 // Set the end_sequence register of the state machine to true and
453 // append a row to the matrix using the current values of the
454 // state-machine registers. Then reset the registers to the initial
455 // values specified above. Every statement program sequence must end
456 // with a DW_LNE_end_sequence instruction which creates a row whose
457 // address is that of the byte after the last target machine instruction
458 // of the sequence.
Alexey Samsonov110d5952014-04-30 00:09:19 +0000459 State.Row.EndSequence = true;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000460 State.appendRowToMatrix(*OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000461 if (OS) {
462 *OS << "\n";
463 OS->indent(12);
464 State.Row.dump(*OS);
465 }
Alexey Samsonov110d5952014-04-30 00:09:19 +0000466 State.resetRowAndSequence();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000467 break;
468
469 case DW_LNE_set_address:
470 // Takes a single relocatable address as an operand. The size of the
471 // operand is the size appropriate to hold an address on the target
472 // machine. Set the address register to the value given by the
473 // relocatable address. All of the other statement program opcodes
474 // that affect the address register add a delta to it. This instruction
475 // stores a relocatable value into it instead.
Paul Robinson511b54c2017-11-22 15:48:30 +0000476 //
477 // Make sure the extractor knows the address size. If not, infer it
478 // from the size of the operand.
479 if (DebugLineData.getAddressSize() == 0)
480 DebugLineData.setAddressSize(Len - 1);
481 else
482 assert(DebugLineData.getAddressSize() == Len - 1);
Paul Robinson17536b92017-06-29 16:52:08 +0000483 State.Row.Address = DebugLineData.getRelocatedAddress(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000484 if (OS)
485 *OS << format(" (0x%16.16" PRIx64 ")", State.Row.Address);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000486 break;
487
488 case DW_LNE_define_file:
489 // Takes 4 arguments. The first is a null terminated string containing
490 // a source file name. The second is an unsigned LEB128 number
491 // representing the directory index of the directory in which the file
492 // was found. The third is an unsigned LEB128 number representing the
493 // time of last modification of the file. The fourth is an unsigned
494 // LEB128 number representing the length in bytes of the file. The time
495 // and length fields may contain LEB128(0) if the information is not
496 // available.
497 //
498 // The directory index represents an entry in the include_directories
499 // section of the statement program prologue. The index is LEB128(0)
500 // if the file was found in the current directory of the compilation,
501 // LEB128(1) if it was found in the first directory in the
502 // include_directories section, and so on. The directory index is
503 // ignored for file names that represent full path names.
504 //
505 // The files are numbered, starting at 1, in the order in which they
506 // appear; the names in the prologue come before names defined by
507 // the DW_LNE_define_file instruction. These numbers are used in the
508 // the file register of the state machine.
509 {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000510 FileNameEntry FileEntry;
511 FileEntry.Name = DebugLineData.getCStr(OffsetPtr);
512 FileEntry.DirIdx = DebugLineData.getULEB128(OffsetPtr);
513 FileEntry.ModTime = DebugLineData.getULEB128(OffsetPtr);
514 FileEntry.Length = DebugLineData.getULEB128(OffsetPtr);
515 Prologue.FileNames.push_back(FileEntry);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000516 if (OS)
517 *OS << " (" << FileEntry.Name.str()
518 << ", dir=" << FileEntry.DirIdx << ", mod_time="
519 << format("(0x%16.16" PRIx64 ")", FileEntry.ModTime)
520 << ", length=" << FileEntry.Length << ")";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000521 }
522 break;
523
Diego Novillo5b5cf502014-02-14 19:27:53 +0000524 case DW_LNE_set_discriminator:
Paul Robinson9d4eb692017-05-01 23:27:55 +0000525 State.Row.Discriminator = DebugLineData.getULEB128(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000526 if (OS)
527 *OS << " (" << State.Row.Discriminator << ")";
Diego Novillo5b5cf502014-02-14 19:27:53 +0000528 break;
529
Benjamin Kramer5acab502011-09-15 02:12:05 +0000530 default:
Paul Robinsone0833342017-11-22 15:14:49 +0000531 if (OS)
532 *OS << format("Unrecognized extended op 0x%02.02" PRIx8, SubOpcode)
533 << format(" length %" PRIx64, Len);
534 // Len doesn't include the zero opcode byte or the length itself, but
535 // it does include the sub_opcode, so we have to adjust for that.
536 (*OffsetPtr) += Len - 1;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000537 break;
538 }
Paul Robinsone0833342017-11-22 15:14:49 +0000539 // Make sure the stated and parsed lengths are the same.
540 // Otherwise we have an unparseable line-number program.
541 if (*OffsetPtr - ExtOffset != Len) {
542 fprintf(stderr, "Unexpected line op length at offset 0x%8.8" PRIx32
543 " expected 0x%2.2" PRIx64 " found 0x%2.2" PRIx32 "\n",
544 ExtOffset, Len, *OffsetPtr - ExtOffset);
545 // Skip the rest of the line-number program.
546 *OffsetPtr = EndOffset;
547 return false;
548 }
Paul Robinson9d4eb692017-05-01 23:27:55 +0000549 } else if (Opcode < Prologue.OpcodeBase) {
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000550 if (OS)
551 *OS << LNStandardString(Opcode);
Paul Robinson9d4eb692017-05-01 23:27:55 +0000552 switch (Opcode) {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000553 // Standard Opcodes
554 case DW_LNS_copy:
555 // Takes no arguments. Append a row to the matrix using the
556 // current values of the state-machine registers. Then set
557 // the basic_block register to false.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000558 State.appendRowToMatrix(*OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000559 if (OS) {
560 *OS << "\n";
561 OS->indent(12);
562 State.Row.dump(*OS);
563 *OS << "\n";
564 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000565 break;
566
567 case DW_LNS_advance_pc:
568 // Takes a single unsigned LEB128 operand, multiplies it by the
569 // min_inst_length field of the prologue, and adds the
570 // result to the address register of the state machine.
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000571 {
572 uint64_t AddrOffset =
573 DebugLineData.getULEB128(OffsetPtr) * Prologue.MinInstLength;
574 State.Row.Address += AddrOffset;
575 if (OS)
576 *OS << " (" << AddrOffset << ")";
577 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000578 break;
579
580 case DW_LNS_advance_line:
581 // Takes a single signed LEB128 operand and adds that value to
582 // the line register of the state machine.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000583 State.Row.Line += DebugLineData.getSLEB128(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000584 if (OS)
585 *OS << " (" << State.Row.Line << ")";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000586 break;
587
588 case DW_LNS_set_file:
589 // Takes a single unsigned LEB128 operand and stores it in the file
590 // register of the state machine.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000591 State.Row.File = DebugLineData.getULEB128(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000592 if (OS)
593 *OS << " (" << State.Row.File << ")";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000594 break;
595
596 case DW_LNS_set_column:
597 // Takes a single unsigned LEB128 operand and stores it in the
598 // column register of the state machine.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000599 State.Row.Column = DebugLineData.getULEB128(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000600 if (OS)
601 *OS << " (" << State.Row.Column << ")";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000602 break;
603
604 case DW_LNS_negate_stmt:
605 // Takes no arguments. Set the is_stmt register of the state
606 // machine to the logical negation of its current value.
Alexey Samsonov110d5952014-04-30 00:09:19 +0000607 State.Row.IsStmt = !State.Row.IsStmt;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000608 break;
609
610 case DW_LNS_set_basic_block:
611 // Takes no arguments. Set the basic_block register of the
612 // state machine to true
Alexey Samsonov110d5952014-04-30 00:09:19 +0000613 State.Row.BasicBlock = true;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000614 break;
615
616 case DW_LNS_const_add_pc:
617 // Takes no arguments. Add to the address register of the state
618 // machine the address increment value corresponding to special
619 // opcode 255. The motivation for DW_LNS_const_add_pc is this:
620 // when the statement program needs to advance the address by a
621 // small amount, it can use a single special opcode, which occupies
622 // a single byte. When it needs to advance the address by up to
623 // twice the range of the last special opcode, it can use
624 // DW_LNS_const_add_pc followed by a special opcode, for a total
625 // of two bytes. Only if it needs to advance the address by more
626 // than twice that range will it need to use both DW_LNS_advance_pc
627 // and a special opcode, requiring three or more bytes.
628 {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000629 uint8_t AdjustOpcode = 255 - Prologue.OpcodeBase;
630 uint64_t AddrOffset =
631 (AdjustOpcode / Prologue.LineRange) * Prologue.MinInstLength;
632 State.Row.Address += AddrOffset;
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000633 if (OS)
634 *OS
635 << format(" (0x%16.16" PRIx64 ")", AddrOffset);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000636 }
637 break;
638
639 case DW_LNS_fixed_advance_pc:
640 // Takes a single uhalf operand. Add to the address register of
641 // the state machine the value of the (unencoded) operand. This
642 // is the only extended opcode that takes an argument that is not
643 // a variable length number. The motivation for DW_LNS_fixed_advance_pc
644 // is this: existing assemblers cannot emit DW_LNS_advance_pc or
645 // special opcodes because they cannot encode LEB128 numbers or
646 // judge when the computation of a special opcode overflows and
647 // requires the use of DW_LNS_advance_pc. Such assemblers, however,
648 // can use DW_LNS_fixed_advance_pc instead, sacrificing compression.
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000649 {
650 uint16_t PCOffset = DebugLineData.getU16(OffsetPtr);
651 State.Row.Address += PCOffset;
652 if (OS)
653 *OS
654 << format(" (0x%16.16" PRIx64 ")", PCOffset);
655 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000656 break;
657
658 case DW_LNS_set_prologue_end:
659 // Takes no arguments. Set the prologue_end register of the
660 // state machine to true
Alexey Samsonov110d5952014-04-30 00:09:19 +0000661 State.Row.PrologueEnd = true;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000662 break;
663
664 case DW_LNS_set_epilogue_begin:
665 // Takes no arguments. Set the basic_block register of the
666 // state machine to true
Alexey Samsonov110d5952014-04-30 00:09:19 +0000667 State.Row.EpilogueBegin = true;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000668 break;
669
670 case DW_LNS_set_isa:
671 // Takes a single unsigned LEB128 operand and stores it in the
672 // column register of the state machine.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000673 State.Row.Isa = DebugLineData.getULEB128(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000674 if (OS)
675 *OS << " (" << State.Row.Isa << ")";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000676 break;
677
678 default:
679 // Handle any unknown standard opcodes here. We know the lengths
680 // of such opcodes because they are specified in the prologue
681 // as a multiple of LEB128 operands for each opcode.
682 {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000683 assert(Opcode - 1U < Prologue.StandardOpcodeLengths.size());
684 uint8_t OpcodeLength = Prologue.StandardOpcodeLengths[Opcode - 1];
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000685 for (uint8_t I = 0; I < OpcodeLength; ++I) {
686 uint64_t Value = DebugLineData.getULEB128(OffsetPtr);
687 if (OS)
688 *OS << format("Skipping ULEB128 value: 0x%16.16" PRIx64 ")\n",
689 Value);
690 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000691 }
692 break;
693 }
694 } else {
695 // Special Opcodes
696
697 // A special opcode value is chosen based on the amount that needs
698 // to be added to the line and address registers. The maximum line
699 // increment for a special opcode is the value of the line_base
700 // field in the header, plus the value of the line_range field,
701 // minus 1 (line base + line range - 1). If the desired line
702 // increment is greater than the maximum line increment, a standard
NAKAMURA Takumif9959852011-10-08 11:22:47 +0000703 // opcode must be used instead of a special opcode. The "address
704 // advance" is calculated by dividing the desired address increment
Benjamin Kramer5acab502011-09-15 02:12:05 +0000705 // by the minimum_instruction_length field from the header. The
706 // special opcode is then calculated using the following formula:
707 //
708 // opcode = (desired line increment - line_base) +
709 // (line_range * address advance) + opcode_base
710 //
711 // If the resulting opcode is greater than 255, a standard opcode
712 // must be used instead.
713 //
714 // To decode a special opcode, subtract the opcode_base from the
715 // opcode itself to give the adjusted opcode. The amount to
716 // increment the address register is the result of the adjusted
717 // opcode divided by the line_range multiplied by the
718 // minimum_instruction_length field from the header. That is:
719 //
720 // address increment = (adjusted opcode / line_range) *
721 // minimum_instruction_length
722 //
723 // The amount to increment the line register is the line_base plus
724 // the result of the adjusted opcode modulo the line_range. That is:
725 //
726 // line increment = line_base + (adjusted opcode % line_range)
727
Paul Robinson9d4eb692017-05-01 23:27:55 +0000728 uint8_t AdjustOpcode = Opcode - Prologue.OpcodeBase;
729 uint64_t AddrOffset =
730 (AdjustOpcode / Prologue.LineRange) * Prologue.MinInstLength;
731 int32_t LineOffset =
732 Prologue.LineBase + (AdjustOpcode % Prologue.LineRange);
733 State.Row.Line += LineOffset;
734 State.Row.Address += AddrOffset;
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000735
736 if (OS) {
737 *OS << "address += " << ((uint32_t)AdjustOpcode)
738 << ", line += " << LineOffset << "\n";
739 OS->indent(12);
740 State.Row.dump(*OS);
741 }
742
Paul Robinson9d4eb692017-05-01 23:27:55 +0000743 State.appendRowToMatrix(*OffsetPtr);
Dehao Chen1b54fce2016-04-28 22:09:37 +0000744 // Reset discriminator to 0.
745 State.Row.Discriminator = 0;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000746 }
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000747 if(OS)
748 *OS << "\n";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000749 }
750
Alexey Samsonov110d5952014-04-30 00:09:19 +0000751 if (!State.Sequence.Empty) {
752 fprintf(stderr, "warning: last sequence in debug line table is not"
753 "terminated!\n");
754 }
755
756 // Sort all sequences so that address lookup will work faster.
757 if (!Sequences.empty()) {
758 std::sort(Sequences.begin(), Sequences.end(), Sequence::orderByLowPC);
759 // Note: actually, instruction address ranges of sequences should not
760 // overlap (in shared objects and executables). If they do, the address
761 // lookup would still work, though, but result would be ambiguous.
762 // We don't report warning in this case. For example,
763 // sometimes .so compiled from multiple object files contains a few
764 // rudimentary sequences for address ranges [0x0, 0xsomething).
765 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000766
Paul Robinson9d4eb692017-05-01 23:27:55 +0000767 return EndOffset;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000768}
769
Keno Fischerc2c60182015-05-31 23:37:04 +0000770uint32_t
Paul Robinson9d4eb692017-05-01 23:27:55 +0000771DWARFDebugLine::LineTable::findRowInSeq(const DWARFDebugLine::Sequence &Seq,
772 uint64_t Address) const {
773 if (!Seq.containsPC(Address))
Keno Fischerc2c60182015-05-31 23:37:04 +0000774 return UnknownRowIndex;
775 // Search for instruction address in the rows describing the sequence.
776 // Rows are stored in a vector, so we may use arithmetical operations with
777 // iterators.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000778 DWARFDebugLine::Row Row;
779 Row.Address = Address;
780 RowIter FirstRow = Rows.begin() + Seq.FirstRowIndex;
781 RowIter LastRow = Rows.begin() + Seq.LastRowIndex;
782 LineTable::RowIter RowPos = std::lower_bound(
783 FirstRow, LastRow, Row, DWARFDebugLine::Row::orderByAddress);
784 if (RowPos == LastRow) {
785 return Seq.LastRowIndex - 1;
Keno Fischerc2c60182015-05-31 23:37:04 +0000786 }
Paul Robinson9d4eb692017-05-01 23:27:55 +0000787 uint32_t Index = Seq.FirstRowIndex + (RowPos - FirstRow);
788 if (RowPos->Address > Address) {
789 if (RowPos == FirstRow)
Keno Fischerc2c60182015-05-31 23:37:04 +0000790 return UnknownRowIndex;
791 else
Paul Robinson9d4eb692017-05-01 23:27:55 +0000792 Index--;
Keno Fischerc2c60182015-05-31 23:37:04 +0000793 }
Paul Robinson9d4eb692017-05-01 23:27:55 +0000794 return Index;
Keno Fischerc2c60182015-05-31 23:37:04 +0000795}
796
Paul Robinson9d4eb692017-05-01 23:27:55 +0000797uint32_t DWARFDebugLine::LineTable::lookupAddress(uint64_t Address) const {
Alexey Samsonov947228c2012-08-07 11:46:57 +0000798 if (Sequences.empty())
Keno Fischerc2c60182015-05-31 23:37:04 +0000799 return UnknownRowIndex;
Alexey Samsonov947228c2012-08-07 11:46:57 +0000800 // First, find an instruction sequence containing the given address.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000801 DWARFDebugLine::Sequence Sequence;
802 Sequence.LowPC = Address;
803 SequenceIter FirstSeq = Sequences.begin();
804 SequenceIter LastSeq = Sequences.end();
805 SequenceIter SeqPos = std::lower_bound(
806 FirstSeq, LastSeq, Sequence, DWARFDebugLine::Sequence::orderByLowPC);
807 DWARFDebugLine::Sequence FoundSeq;
808 if (SeqPos == LastSeq) {
809 FoundSeq = Sequences.back();
810 } else if (SeqPos->LowPC == Address) {
811 FoundSeq = *SeqPos;
Alexey Samsonov947228c2012-08-07 11:46:57 +0000812 } else {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000813 if (SeqPos == FirstSeq)
Keno Fischerc2c60182015-05-31 23:37:04 +0000814 return UnknownRowIndex;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000815 FoundSeq = *(SeqPos - 1);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000816 }
Paul Robinson9d4eb692017-05-01 23:27:55 +0000817 return findRowInSeq(FoundSeq, Address);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000818}
Alexey Samsonov45be7932012-08-30 07:49:50 +0000819
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000820bool DWARFDebugLine::LineTable::lookupAddressRange(
Paul Robinson9d4eb692017-05-01 23:27:55 +0000821 uint64_t Address, uint64_t Size, std::vector<uint32_t> &Result) const {
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000822 if (Sequences.empty())
823 return false;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000824 uint64_t EndAddr = Address + Size;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000825 // First, find an instruction sequence containing the given address.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000826 DWARFDebugLine::Sequence Sequence;
827 Sequence.LowPC = Address;
828 SequenceIter FirstSeq = Sequences.begin();
829 SequenceIter LastSeq = Sequences.end();
830 SequenceIter SeqPos = std::lower_bound(
831 FirstSeq, LastSeq, Sequence, DWARFDebugLine::Sequence::orderByLowPC);
832 if (SeqPos == LastSeq || SeqPos->LowPC != Address) {
833 if (SeqPos == FirstSeq)
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000834 return false;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000835 SeqPos--;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000836 }
Paul Robinson9d4eb692017-05-01 23:27:55 +0000837 if (!SeqPos->containsPC(Address))
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000838 return false;
839
Paul Robinson9d4eb692017-05-01 23:27:55 +0000840 SequenceIter StartPos = SeqPos;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000841
842 // Add the rows from the first sequence to the vector, starting with the
843 // index we just calculated
844
Paul Robinson9d4eb692017-05-01 23:27:55 +0000845 while (SeqPos != LastSeq && SeqPos->LowPC < EndAddr) {
846 const DWARFDebugLine::Sequence &CurSeq = *SeqPos;
Keno Fischerc2c60182015-05-31 23:37:04 +0000847 // For the first sequence, we need to find which row in the sequence is the
848 // first in our range.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000849 uint32_t FirstRowIndex = CurSeq.FirstRowIndex;
850 if (SeqPos == StartPos)
851 FirstRowIndex = findRowInSeq(CurSeq, Address);
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000852
Keno Fischerc2c60182015-05-31 23:37:04 +0000853 // Figure out the last row in the range.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000854 uint32_t LastRowIndex = findRowInSeq(CurSeq, EndAddr - 1);
855 if (LastRowIndex == UnknownRowIndex)
856 LastRowIndex = CurSeq.LastRowIndex - 1;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000857
Paul Robinson9d4eb692017-05-01 23:27:55 +0000858 assert(FirstRowIndex != UnknownRowIndex);
859 assert(LastRowIndex != UnknownRowIndex);
Keno Fischerc2c60182015-05-31 23:37:04 +0000860
Paul Robinson9d4eb692017-05-01 23:27:55 +0000861 for (uint32_t I = FirstRowIndex; I <= LastRowIndex; ++I) {
862 Result.push_back(I);
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000863 }
864
Paul Robinson9d4eb692017-05-01 23:27:55 +0000865 ++SeqPos;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000866 }
NAKAMURA Takumi4b86cdb2013-01-26 01:45:06 +0000867
868 return true;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000869}
870
Paul Robinson9d4eb692017-05-01 23:27:55 +0000871bool DWARFDebugLine::LineTable::hasFileAtIndex(uint64_t FileIndex) const {
Pete Cooperb2ba7762016-07-22 01:41:32 +0000872 return FileIndex != 0 && FileIndex <= Prologue.FileNames.size();
873}
874
Paul Robinson9d4eb692017-05-01 23:27:55 +0000875bool DWARFDebugLine::LineTable::getFileNameByIndex(uint64_t FileIndex,
876 const char *CompDir,
877 FileLineInfoKind Kind,
878 std::string &Result) const {
Pete Cooperb2ba7762016-07-22 01:41:32 +0000879 if (Kind == FileLineInfoKind::None || !hasFileAtIndex(FileIndex))
Alexey Samsonov45be7932012-08-30 07:49:50 +0000880 return false;
881 const FileNameEntry &Entry = Prologue.FileNames[FileIndex - 1];
Paul Robinsonba1c9152017-05-02 17:37:32 +0000882 StringRef FileName = Entry.Name;
Alexey Samsonovdce67342014-05-15 21:24:32 +0000883 if (Kind != FileLineInfoKind::AbsoluteFilePath ||
Alexey Samsonov45be7932012-08-30 07:49:50 +0000884 sys::path::is_absolute(FileName)) {
885 Result = FileName;
886 return true;
887 }
Frederic Riss101b5e22014-09-19 15:11:51 +0000888
Alexey Samsonov45be7932012-08-30 07:49:50 +0000889 SmallString<16> FilePath;
890 uint64_t IncludeDirIndex = Entry.DirIdx;
Paul Robinsonba1c9152017-05-02 17:37:32 +0000891 StringRef IncludeDir;
Alexey Samsonov45be7932012-08-30 07:49:50 +0000892 // Be defensive about the contents of Entry.
893 if (IncludeDirIndex > 0 &&
Frederic Riss101b5e22014-09-19 15:11:51 +0000894 IncludeDirIndex <= Prologue.IncludeDirectories.size())
895 IncludeDir = Prologue.IncludeDirectories[IncludeDirIndex - 1];
896
897 // We may still need to append compilation directory of compile unit.
898 // We know that FileName is not absolute, the only way to have an
899 // absolute path at this point would be if IncludeDir is absolute.
900 if (CompDir && Kind == FileLineInfoKind::AbsoluteFilePath &&
901 sys::path::is_relative(IncludeDir))
902 sys::path::append(FilePath, CompDir);
903
904 // sys::path::append skips empty strings.
905 sys::path::append(FilePath, IncludeDir, FileName);
Alexey Samsonov45be7932012-08-30 07:49:50 +0000906 Result = FilePath.str();
907 return true;
908}
Frederic Riss101b5e22014-09-19 15:11:51 +0000909
Dehao Chen1b54fce2016-04-28 22:09:37 +0000910bool DWARFDebugLine::LineTable::getFileLineInfoForAddress(
911 uint64_t Address, const char *CompDir, FileLineInfoKind Kind,
912 DILineInfo &Result) const {
Frederic Riss101b5e22014-09-19 15:11:51 +0000913 // Get the index of row we're looking for in the line table.
914 uint32_t RowIndex = lookupAddress(Address);
915 if (RowIndex == -1U)
916 return false;
917 // Take file number and line/column from the row.
918 const auto &Row = Rows[RowIndex];
919 if (!getFileNameByIndex(Row.File, CompDir, Kind, Result.FileName))
920 return false;
921 Result.Line = Row.Line;
922 Result.Column = Row.Column;
Eric Christopherba1024c2016-12-14 18:29:39 +0000923 Result.Discriminator = Row.Discriminator;
Frederic Riss101b5e22014-09-19 15:11:51 +0000924 return true;
925}