blob: e5ef4eaceebefa56de0606d497092d42d88e34a9 [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});
Paul Robinson5c8f7d72017-12-15 22:57:17 +000051 HasMD5 = false;
Alexey Samsonov836b1ae2014-04-29 21:28:13 +000052 StandardOpcodeLengths.clear();
53 IncludeDirectories.clear();
54 FileNames.clear();
55}
56
Benjamin Kramer5acab502011-09-15 02:12:05 +000057void DWARFDebugLine::Prologue::dump(raw_ostream &OS) const {
58 OS << "Line table prologue:\n"
Ed Maste6d0bee52015-05-28 15:38:17 +000059 << format(" total_length: 0x%8.8" PRIx64 "\n", TotalLength)
Paul Robinson75c068c2017-06-26 18:43:01 +000060 << format(" version: %u\n", getVersion());
61 if (getVersion() >= 5)
62 OS << format(" address_size: %u\n", getAddressSize())
63 << format(" seg_select_size: %u\n", SegSelectorSize);
64 OS << format(" prologue_length: 0x%8.8" PRIx64 "\n", PrologueLength)
David Blaikie1d4736e2014-02-24 23:58:54 +000065 << format(" min_inst_length: %u\n", MinInstLength)
Paul Robinson75c068c2017-06-26 18:43:01 +000066 << format(getVersion() >= 4 ? "max_ops_per_inst: %u\n" : "", MaxOpsPerInst)
David Blaikie1d4736e2014-02-24 23:58:54 +000067 << format(" default_is_stmt: %u\n", DefaultIsStmt)
68 << format(" line_base: %i\n", LineBase)
69 << format(" line_range: %u\n", LineRange)
70 << format(" opcode_base: %u\n", OpcodeBase);
Benjamin Kramer5acab502011-09-15 02:12:05 +000071
Paul Robinson9d4eb692017-05-01 23:27:55 +000072 for (uint32_t I = 0; I != StandardOpcodeLengths.size(); ++I)
Mehdi Amini149f6ea2016-10-05 05:59:29 +000073 OS << format("standard_opcode_lengths[%s] = %u\n",
Paul Robinson9d4eb692017-05-01 23:27:55 +000074 LNStandardString(I + 1).data(), StandardOpcodeLengths[I]);
Benjamin Kramer5acab502011-09-15 02:12:05 +000075
76 if (!IncludeDirectories.empty())
Paul Robinson9d4eb692017-05-01 23:27:55 +000077 for (uint32_t I = 0; I != IncludeDirectories.size(); ++I)
78 OS << format("include_directories[%3u] = '", I + 1)
79 << IncludeDirectories[I] << "'\n";
Benjamin Kramer5acab502011-09-15 02:12:05 +000080
81 if (!FileNames.empty()) {
Paul Robinson5c8f7d72017-12-15 22:57:17 +000082 if (HasMD5)
83 OS << " Dir MD5 Checksum File Name\n"
84 << " ---- -------------------------------- -----------"
85 "---------------\n";
86 else
87 OS << " Dir Mod Time File Len File Name\n"
88 << " ---- ---------- ---------- -----------"
89 "----------------\n";
Paul Robinson9d4eb692017-05-01 23:27:55 +000090 for (uint32_t I = 0; I != FileNames.size(); ++I) {
91 const FileNameEntry &FileEntry = FileNames[I];
Paul Robinson5c8f7d72017-12-15 22:57:17 +000092 OS << format("file_names[%3u] %4" PRIu64 " ", I + 1, FileEntry.DirIdx);
93 if (HasMD5)
94 OS << FileEntry.Checksum.digest();
95 else
96 OS << format("0x%8.8" PRIx64 " 0x%8.8" PRIx64, FileEntry.ModTime,
97 FileEntry.Length);
98 OS << ' ' << FileEntry.Name << '\n';
Benjamin Kramer5acab502011-09-15 02:12:05 +000099 }
100 }
101}
102
Paul Robinson2bc38732017-05-02 21:40:47 +0000103// Parse v2-v4 directory and file tables.
104static void
Paul Robinson17536b92017-06-29 16:52:08 +0000105parseV2DirFileTables(const DWARFDataExtractor &DebugLineData,
106 uint32_t *OffsetPtr, uint64_t EndPrologueOffset,
Paul Robinson2bc38732017-05-02 21:40:47 +0000107 std::vector<StringRef> &IncludeDirectories,
108 std::vector<DWARFDebugLine::FileNameEntry> &FileNames) {
109 while (*OffsetPtr < EndPrologueOffset) {
110 StringRef S = DebugLineData.getCStrRef(OffsetPtr);
111 if (S.empty())
112 break;
113 IncludeDirectories.push_back(S);
114 }
115
116 while (*OffsetPtr < EndPrologueOffset) {
117 StringRef Name = DebugLineData.getCStrRef(OffsetPtr);
118 if (Name.empty())
119 break;
120 DWARFDebugLine::FileNameEntry FileEntry;
121 FileEntry.Name = Name;
122 FileEntry.DirIdx = DebugLineData.getULEB128(OffsetPtr);
123 FileEntry.ModTime = DebugLineData.getULEB128(OffsetPtr);
124 FileEntry.Length = DebugLineData.getULEB128(OffsetPtr);
125 FileNames.push_back(FileEntry);
126 }
127}
128
129// Parse v5 directory/file entry content descriptions.
130// Returns the descriptors, or an empty vector if we did not find a path or
131// ran off the end of the prologue.
132static ContentDescriptors
Paul Robinson17536b92017-06-29 16:52:08 +0000133parseV5EntryFormat(const DWARFDataExtractor &DebugLineData, uint32_t *OffsetPtr,
Paul Robinson5c8f7d72017-12-15 22:57:17 +0000134 uint64_t EndPrologueOffset, bool *HasMD5) {
Paul Robinson2bc38732017-05-02 21:40:47 +0000135 ContentDescriptors Descriptors;
136 int FormatCount = DebugLineData.getU8(OffsetPtr);
137 bool HasPath = false;
138 for (int I = 0; I != FormatCount; ++I) {
139 if (*OffsetPtr >= EndPrologueOffset)
140 return ContentDescriptors();
141 ContentDescriptor Descriptor;
142 Descriptor.Type =
143 dwarf::LineNumberEntryFormat(DebugLineData.getULEB128(OffsetPtr));
144 Descriptor.Form = dwarf::Form(DebugLineData.getULEB128(OffsetPtr));
145 if (Descriptor.Type == dwarf::DW_LNCT_path)
146 HasPath = true;
Paul Robinson5c8f7d72017-12-15 22:57:17 +0000147 else if (Descriptor.Type == dwarf::DW_LNCT_MD5 && HasMD5)
148 *HasMD5 = true;
Paul Robinson2bc38732017-05-02 21:40:47 +0000149 Descriptors.push_back(Descriptor);
150 }
151 return HasPath ? Descriptors : ContentDescriptors();
152}
153
154static bool
Paul Robinson17536b92017-06-29 16:52:08 +0000155parseV5DirFileTables(const DWARFDataExtractor &DebugLineData,
156 uint32_t *OffsetPtr, uint64_t EndPrologueOffset,
Paul Robinsone5400f82017-11-07 19:57:12 +0000157 const DWARFFormParams &FormParams, const DWARFUnit *U,
Paul Robinson5c8f7d72017-12-15 22:57:17 +0000158 bool &HasMD5, std::vector<StringRef> &IncludeDirectories,
Paul Robinson2bc38732017-05-02 21:40:47 +0000159 std::vector<DWARFDebugLine::FileNameEntry> &FileNames) {
160 // Get the directory entry description.
161 ContentDescriptors DirDescriptors =
Paul Robinson5c8f7d72017-12-15 22:57:17 +0000162 parseV5EntryFormat(DebugLineData, OffsetPtr, EndPrologueOffset, nullptr);
Paul Robinson2bc38732017-05-02 21:40:47 +0000163 if (DirDescriptors.empty())
164 return false;
165
166 // Get the directory entries, according to the format described above.
167 int DirEntryCount = DebugLineData.getU8(OffsetPtr);
168 for (int I = 0; I != DirEntryCount; ++I) {
169 if (*OffsetPtr >= EndPrologueOffset)
170 return false;
171 for (auto Descriptor : DirDescriptors) {
172 DWARFFormValue Value(Descriptor.Form);
173 switch (Descriptor.Type) {
174 case DW_LNCT_path:
Paul Robinsone5400f82017-11-07 19:57:12 +0000175 if (!Value.extractValue(DebugLineData, OffsetPtr, FormParams, U))
Paul Robinson2bc38732017-05-02 21:40:47 +0000176 return false;
177 IncludeDirectories.push_back(Value.getAsCString().getValue());
178 break;
179 default:
Paul Robinson75c068c2017-06-26 18:43:01 +0000180 if (!Value.skipValue(DebugLineData, OffsetPtr, FormParams))
Paul Robinson2bc38732017-05-02 21:40:47 +0000181 return false;
182 }
183 }
184 }
185
186 // Get the file entry description.
187 ContentDescriptors FileDescriptors =
Paul Robinson5c8f7d72017-12-15 22:57:17 +0000188 parseV5EntryFormat(DebugLineData, OffsetPtr, EndPrologueOffset, &HasMD5);
Paul Robinson2bc38732017-05-02 21:40:47 +0000189 if (FileDescriptors.empty())
190 return false;
191
192 // Get the file entries, according to the format described above.
193 int FileEntryCount = DebugLineData.getU8(OffsetPtr);
194 for (int I = 0; I != FileEntryCount; ++I) {
195 if (*OffsetPtr >= EndPrologueOffset)
196 return false;
197 DWARFDebugLine::FileNameEntry FileEntry;
198 for (auto Descriptor : FileDescriptors) {
199 DWARFFormValue Value(Descriptor.Form);
Paul Robinsone5400f82017-11-07 19:57:12 +0000200 if (!Value.extractValue(DebugLineData, OffsetPtr, FormParams, U))
Paul Robinson2bc38732017-05-02 21:40:47 +0000201 return false;
202 switch (Descriptor.Type) {
203 case DW_LNCT_path:
204 FileEntry.Name = Value.getAsCString().getValue();
205 break;
206 case DW_LNCT_directory_index:
207 FileEntry.DirIdx = Value.getAsUnsignedConstant().getValue();
208 break;
209 case DW_LNCT_timestamp:
210 FileEntry.ModTime = Value.getAsUnsignedConstant().getValue();
211 break;
212 case DW_LNCT_size:
213 FileEntry.Length = Value.getAsUnsignedConstant().getValue();
214 break;
Paul Robinson5c8f7d72017-12-15 22:57:17 +0000215 case DW_LNCT_MD5:
216 assert(Value.getAsBlock().getValue().size() == 16);
217 std::uninitialized_copy_n(Value.getAsBlock().getValue().begin(), 16,
218 FileEntry.Checksum.Bytes.begin());
219 break;
Paul Robinson2bc38732017-05-02 21:40:47 +0000220 default:
221 break;
222 }
223 }
224 FileNames.push_back(FileEntry);
225 }
226 return true;
227}
228
Paul Robinson17536b92017-06-29 16:52:08 +0000229bool DWARFDebugLine::Prologue::parse(const DWARFDataExtractor &DebugLineData,
Paul Robinsone5400f82017-11-07 19:57:12 +0000230 uint32_t *OffsetPtr, const DWARFUnit *U) {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000231 const uint64_t PrologueOffset = *OffsetPtr;
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000232
233 clear();
Paul Robinson9d4eb692017-05-01 23:27:55 +0000234 TotalLength = DebugLineData.getU32(OffsetPtr);
Ed Maste6d0bee52015-05-28 15:38:17 +0000235 if (TotalLength == UINT32_MAX) {
Paul Robinson75c068c2017-06-26 18:43:01 +0000236 FormParams.Format = dwarf::DWARF64;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000237 TotalLength = DebugLineData.getU64(OffsetPtr);
Paul Robinson75c068c2017-06-26 18:43:01 +0000238 } else if (TotalLength >= 0xffffff00) {
Ed Maste6d0bee52015-05-28 15:38:17 +0000239 return false;
240 }
Paul Robinson75c068c2017-06-26 18:43:01 +0000241 FormParams.Version = DebugLineData.getU16(OffsetPtr);
242 if (getVersion() < 2)
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000243 return false;
244
Paul Robinson75c068c2017-06-26 18:43:01 +0000245 if (getVersion() >= 5) {
246 FormParams.AddrSize = DebugLineData.getU8(OffsetPtr);
Paul Robinson63811a42017-11-22 15:33:17 +0000247 assert((DebugLineData.getAddressSize() == 0 ||
248 DebugLineData.getAddressSize() == getAddressSize()) &&
Paul Robinson75c068c2017-06-26 18:43:01 +0000249 "Line table header and data extractor disagree");
Paul Robinson2bc38732017-05-02 21:40:47 +0000250 SegSelectorSize = DebugLineData.getU8(OffsetPtr);
251 }
252
Paul Robinson9d4eb692017-05-01 23:27:55 +0000253 PrologueLength = DebugLineData.getUnsigned(OffsetPtr, sizeofPrologueLength());
254 const uint64_t EndPrologueOffset = PrologueLength + *OffsetPtr;
255 MinInstLength = DebugLineData.getU8(OffsetPtr);
Paul Robinson75c068c2017-06-26 18:43:01 +0000256 if (getVersion() >= 4)
Paul Robinson9d4eb692017-05-01 23:27:55 +0000257 MaxOpsPerInst = DebugLineData.getU8(OffsetPtr);
258 DefaultIsStmt = DebugLineData.getU8(OffsetPtr);
259 LineBase = DebugLineData.getU8(OffsetPtr);
260 LineRange = DebugLineData.getU8(OffsetPtr);
261 OpcodeBase = DebugLineData.getU8(OffsetPtr);
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000262
263 StandardOpcodeLengths.reserve(OpcodeBase - 1);
Paul Robinson9d4eb692017-05-01 23:27:55 +0000264 for (uint32_t I = 1; I < OpcodeBase; ++I) {
265 uint8_t OpLen = DebugLineData.getU8(OffsetPtr);
266 StandardOpcodeLengths.push_back(OpLen);
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000267 }
268
Paul Robinson75c068c2017-06-26 18:43:01 +0000269 if (getVersion() >= 5) {
Paul Robinson2bc38732017-05-02 21:40:47 +0000270 if (!parseV5DirFileTables(DebugLineData, OffsetPtr, EndPrologueOffset,
Paul Robinson5c8f7d72017-12-15 22:57:17 +0000271 getFormParams(), U, HasMD5, IncludeDirectories,
Paul Robinsone5400f82017-11-07 19:57:12 +0000272 FileNames)) {
Paul Robinson2bc38732017-05-02 21:40:47 +0000273 fprintf(stderr,
274 "warning: parsing line table prologue at 0x%8.8" PRIx64
275 " found an invalid directory or file table description at"
276 " 0x%8.8" PRIx64 "\n", PrologueOffset, (uint64_t)*OffsetPtr);
277 return false;
278 }
279 } else
280 parseV2DirFileTables(DebugLineData, OffsetPtr, EndPrologueOffset,
281 IncludeDirectories, FileNames);
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000282
Paul Robinson9d4eb692017-05-01 23:27:55 +0000283 if (*OffsetPtr != EndPrologueOffset) {
284 fprintf(stderr,
285 "warning: parsing line table prologue at 0x%8.8" PRIx64
286 " should have ended at 0x%8.8" PRIx64
287 " but it ended at 0x%8.8" PRIx64 "\n",
288 PrologueOffset, EndPrologueOffset, (uint64_t)*OffsetPtr);
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000289 return false;
290 }
291 return true;
292}
293
Paul Robinson9d4eb692017-05-01 23:27:55 +0000294DWARFDebugLine::Row::Row(bool DefaultIsStmt) { reset(DefaultIsStmt); }
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000295
Benjamin Kramer5acab502011-09-15 02:12:05 +0000296void DWARFDebugLine::Row::postAppend() {
297 BasicBlock = false;
298 PrologueEnd = false;
299 EpilogueBegin = false;
300}
301
Paul Robinson9d4eb692017-05-01 23:27:55 +0000302void DWARFDebugLine::Row::reset(bool DefaultIsStmt) {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000303 Address = 0;
304 Line = 1;
305 Column = 0;
306 File = 1;
307 Isa = 0;
Diego Novillo5b5cf502014-02-14 19:27:53 +0000308 Discriminator = 0;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000309 IsStmt = DefaultIsStmt;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000310 BasicBlock = false;
311 EndSequence = false;
312 PrologueEnd = false;
313 EpilogueBegin = false;
314}
315
Greg Clayton67070462017-05-02 22:48:52 +0000316void DWARFDebugLine::Row::dumpTableHeader(raw_ostream &OS) {
317 OS << "Address Line Column File ISA Discriminator Flags\n"
318 << "------------------ ------ ------ ------ --- ------------- "
319 "-------------\n";
320}
321
Benjamin Kramer5acab502011-09-15 02:12:05 +0000322void DWARFDebugLine::Row::dump(raw_ostream &OS) const {
Benjamin Kramerf3da5292011-11-05 08:57:40 +0000323 OS << format("0x%16.16" PRIx64 " %6u %6u", Address, Line, Column)
Diego Novillo5b5cf502014-02-14 19:27:53 +0000324 << format(" %6u %3u %13u ", File, Isa, Discriminator)
Dehao Chen1b54fce2016-04-28 22:09:37 +0000325 << (IsStmt ? " is_stmt" : "") << (BasicBlock ? " basic_block" : "")
Benjamin Kramer5acab502011-09-15 02:12:05 +0000326 << (PrologueEnd ? " prologue_end" : "")
327 << (EpilogueBegin ? " epilogue_begin" : "")
Dehao Chen1b54fce2016-04-28 22:09:37 +0000328 << (EndSequence ? " end_sequence" : "") << '\n';
Benjamin Kramer5acab502011-09-15 02:12:05 +0000329}
330
Dehao Chen1b54fce2016-04-28 22:09:37 +0000331DWARFDebugLine::Sequence::Sequence() { reset(); }
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000332
333void DWARFDebugLine::Sequence::reset() {
334 LowPC = 0;
335 HighPC = 0;
336 FirstRowIndex = 0;
337 LastRowIndex = 0;
338 Empty = true;
339}
340
Dehao Chen1b54fce2016-04-28 22:09:37 +0000341DWARFDebugLine::LineTable::LineTable() { clear(); }
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000342
Benjamin Kramer5acab502011-09-15 02:12:05 +0000343void DWARFDebugLine::LineTable::dump(raw_ostream &OS) const {
344 Prologue.dump(OS);
345 OS << '\n';
346
347 if (!Rows.empty()) {
Greg Clayton67070462017-05-02 22:48:52 +0000348 Row::dumpTableHeader(OS);
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000349 for (const Row &R : Rows) {
350 R.dump(OS);
351 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000352 }
353}
354
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000355void DWARFDebugLine::LineTable::clear() {
356 Prologue.clear();
357 Rows.clear();
358 Sequences.clear();
359}
360
Alexey Samsonov110d5952014-04-30 00:09:19 +0000361DWARFDebugLine::ParsingState::ParsingState(struct LineTable *LT)
Eugene Zelenko2db0cfa2017-06-23 21:57:40 +0000362 : LineTable(LT) {
Alexey Samsonov110d5952014-04-30 00:09:19 +0000363 resetRowAndSequence();
364}
Nick Lewycky4d044922011-09-15 03:41:51 +0000365
Alexey Samsonov110d5952014-04-30 00:09:19 +0000366void DWARFDebugLine::ParsingState::resetRowAndSequence() {
367 Row.reset(LineTable->Prologue.DefaultIsStmt);
368 Sequence.reset();
369}
370
Paul Robinson9d4eb692017-05-01 23:27:55 +0000371void DWARFDebugLine::ParsingState::appendRowToMatrix(uint32_t Offset) {
Alexey Samsonov110d5952014-04-30 00:09:19 +0000372 if (Sequence.Empty) {
Alexey Samsonov947228c2012-08-07 11:46:57 +0000373 // Record the beginning of instruction sequence.
Alexey Samsonov110d5952014-04-30 00:09:19 +0000374 Sequence.Empty = false;
375 Sequence.LowPC = Row.Address;
376 Sequence.FirstRowIndex = RowNumber;
Alexey Samsonov947228c2012-08-07 11:46:57 +0000377 }
Alexey Samsonov110d5952014-04-30 00:09:19 +0000378 ++RowNumber;
379 LineTable->appendRow(Row);
380 if (Row.EndSequence) {
Alexey Samsonov947228c2012-08-07 11:46:57 +0000381 // Record the end of instruction sequence.
Alexey Samsonov110d5952014-04-30 00:09:19 +0000382 Sequence.HighPC = Row.Address;
383 Sequence.LastRowIndex = RowNumber;
384 if (Sequence.isValid())
385 LineTable->appendSequence(Sequence);
386 Sequence.reset();
Alexey Samsonov947228c2012-08-07 11:46:57 +0000387 }
Alexey Samsonov110d5952014-04-30 00:09:19 +0000388 Row.postAppend();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000389}
390
Benjamin Kramer5acab502011-09-15 02:12:05 +0000391const DWARFDebugLine::LineTable *
Paul Robinson9d4eb692017-05-01 23:27:55 +0000392DWARFDebugLine::getLineTable(uint32_t Offset) const {
393 LineTableConstIter Pos = LineTableMap.find(Offset);
394 if (Pos != LineTableMap.end())
395 return &Pos->second;
Craig Topper2617dcc2014-04-15 06:32:26 +0000396 return nullptr;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000397}
398
Benjamin Kramer679e1752011-09-15 20:43:18 +0000399const DWARFDebugLine::LineTable *
Paul Robinson511b54c2017-11-22 15:48:30 +0000400DWARFDebugLine::getOrParseLineTable(DWARFDataExtractor &DebugLineData,
Paul Robinsone5400f82017-11-07 19:57:12 +0000401 uint32_t Offset, const DWARFUnit *U) {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000402 std::pair<LineTableIter, bool> Pos =
403 LineTableMap.insert(LineTableMapTy::value_type(Offset, LineTable()));
404 LineTable *LT = &Pos.first->second;
405 if (Pos.second) {
Paul Robinsone5400f82017-11-07 19:57:12 +0000406 if (!LT->parse(DebugLineData, &Offset, U))
Craig Topper2617dcc2014-04-15 06:32:26 +0000407 return nullptr;
Benjamin Kramer679e1752011-09-15 20:43:18 +0000408 }
Alexey Samsonov110d5952014-04-30 00:09:19 +0000409 return LT;
Benjamin Kramer679e1752011-09-15 20:43:18 +0000410}
411
Paul Robinson511b54c2017-11-22 15:48:30 +0000412bool DWARFDebugLine::LineTable::parse(DWARFDataExtractor &DebugLineData,
Paul Robinsone5400f82017-11-07 19:57:12 +0000413 uint32_t *OffsetPtr, const DWARFUnit *U,
414 raw_ostream *OS) {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000415 const uint32_t DebugLineOffset = *OffsetPtr;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000416
Alexey Samsonov110d5952014-04-30 00:09:19 +0000417 clear();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000418
Paul Robinsone5400f82017-11-07 19:57:12 +0000419 if (!Prologue.parse(DebugLineData, OffsetPtr, U)) {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000420 // Restore our offset and return false to indicate failure!
Paul Robinson9d4eb692017-05-01 23:27:55 +0000421 *OffsetPtr = DebugLineOffset;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000422 return false;
423 }
424
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000425 if (OS)
426 Prologue.dump(*OS);
427
Paul Robinson9d4eb692017-05-01 23:27:55 +0000428 const uint32_t EndOffset =
429 DebugLineOffset + Prologue.TotalLength + Prologue.sizeofTotalLength();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000430
Paul Robinson511b54c2017-11-22 15:48:30 +0000431 // See if we should tell the data extractor the address size.
432 if (DebugLineData.getAddressSize() == 0)
433 DebugLineData.setAddressSize(Prologue.getAddressSize());
434 else
435 assert(Prologue.getAddressSize() == 0 ||
436 Prologue.getAddressSize() == DebugLineData.getAddressSize());
437
Alexey Samsonov110d5952014-04-30 00:09:19 +0000438 ParsingState State(this);
Benjamin Kramer112ec172011-09-15 21:59:13 +0000439
Paul Robinson9d4eb692017-05-01 23:27:55 +0000440 while (*OffsetPtr < EndOffset) {
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000441 if (OS)
442 *OS << format("0x%08.08" PRIx32 ": ", *OffsetPtr);
443
Paul Robinson9d4eb692017-05-01 23:27:55 +0000444 uint8_t Opcode = DebugLineData.getU8(OffsetPtr);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000445
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000446 if (OS)
447 *OS << format("%02.02" PRIx8 " ", Opcode);
448
Paul Robinson9d4eb692017-05-01 23:27:55 +0000449 if (Opcode == 0) {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000450 // Extended Opcodes always start with a zero opcode followed by
451 // a uleb128 length so you can skip ones you don't know about
Paul Robinson9d4eb692017-05-01 23:27:55 +0000452 uint64_t Len = DebugLineData.getULEB128(OffsetPtr);
Paul Robinsone0833342017-11-22 15:14:49 +0000453 uint32_t ExtOffset = *OffsetPtr;
454
455 // Tolerate zero-length; assume length is correct and soldier on.
456 if (Len == 0) {
457 if (OS)
458 *OS << "Badly formed extended line op (length 0)\n";
459 continue;
460 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000461
Paul Robinson9d4eb692017-05-01 23:27:55 +0000462 uint8_t SubOpcode = DebugLineData.getU8(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000463 if (OS)
464 *OS << LNExtendedString(SubOpcode);
Paul Robinson9d4eb692017-05-01 23:27:55 +0000465 switch (SubOpcode) {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000466 case DW_LNE_end_sequence:
467 // Set the end_sequence register of the state machine to true and
468 // append a row to the matrix using the current values of the
469 // state-machine registers. Then reset the registers to the initial
470 // values specified above. Every statement program sequence must end
471 // with a DW_LNE_end_sequence instruction which creates a row whose
472 // address is that of the byte after the last target machine instruction
473 // of the sequence.
Alexey Samsonov110d5952014-04-30 00:09:19 +0000474 State.Row.EndSequence = true;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000475 State.appendRowToMatrix(*OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000476 if (OS) {
477 *OS << "\n";
478 OS->indent(12);
479 State.Row.dump(*OS);
480 }
Alexey Samsonov110d5952014-04-30 00:09:19 +0000481 State.resetRowAndSequence();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000482 break;
483
484 case DW_LNE_set_address:
485 // Takes a single relocatable address as an operand. The size of the
486 // operand is the size appropriate to hold an address on the target
487 // machine. Set the address register to the value given by the
488 // relocatable address. All of the other statement program opcodes
489 // that affect the address register add a delta to it. This instruction
490 // stores a relocatable value into it instead.
Paul Robinson511b54c2017-11-22 15:48:30 +0000491 //
492 // Make sure the extractor knows the address size. If not, infer it
493 // from the size of the operand.
494 if (DebugLineData.getAddressSize() == 0)
495 DebugLineData.setAddressSize(Len - 1);
496 else
497 assert(DebugLineData.getAddressSize() == Len - 1);
Paul Robinson17536b92017-06-29 16:52:08 +0000498 State.Row.Address = DebugLineData.getRelocatedAddress(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000499 if (OS)
500 *OS << format(" (0x%16.16" PRIx64 ")", State.Row.Address);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000501 break;
502
503 case DW_LNE_define_file:
504 // Takes 4 arguments. The first is a null terminated string containing
505 // a source file name. The second is an unsigned LEB128 number
506 // representing the directory index of the directory in which the file
507 // was found. The third is an unsigned LEB128 number representing the
508 // time of last modification of the file. The fourth is an unsigned
509 // LEB128 number representing the length in bytes of the file. The time
510 // and length fields may contain LEB128(0) if the information is not
511 // available.
512 //
513 // The directory index represents an entry in the include_directories
514 // section of the statement program prologue. The index is LEB128(0)
515 // if the file was found in the current directory of the compilation,
516 // LEB128(1) if it was found in the first directory in the
517 // include_directories section, and so on. The directory index is
518 // ignored for file names that represent full path names.
519 //
520 // The files are numbered, starting at 1, in the order in which they
521 // appear; the names in the prologue come before names defined by
522 // the DW_LNE_define_file instruction. These numbers are used in the
523 // the file register of the state machine.
524 {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000525 FileNameEntry FileEntry;
526 FileEntry.Name = DebugLineData.getCStr(OffsetPtr);
527 FileEntry.DirIdx = DebugLineData.getULEB128(OffsetPtr);
528 FileEntry.ModTime = DebugLineData.getULEB128(OffsetPtr);
529 FileEntry.Length = DebugLineData.getULEB128(OffsetPtr);
530 Prologue.FileNames.push_back(FileEntry);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000531 if (OS)
532 *OS << " (" << FileEntry.Name.str()
533 << ", dir=" << FileEntry.DirIdx << ", mod_time="
534 << format("(0x%16.16" PRIx64 ")", FileEntry.ModTime)
535 << ", length=" << FileEntry.Length << ")";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000536 }
537 break;
538
Diego Novillo5b5cf502014-02-14 19:27:53 +0000539 case DW_LNE_set_discriminator:
Paul Robinson9d4eb692017-05-01 23:27:55 +0000540 State.Row.Discriminator = DebugLineData.getULEB128(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000541 if (OS)
542 *OS << " (" << State.Row.Discriminator << ")";
Diego Novillo5b5cf502014-02-14 19:27:53 +0000543 break;
544
Benjamin Kramer5acab502011-09-15 02:12:05 +0000545 default:
Paul Robinsone0833342017-11-22 15:14:49 +0000546 if (OS)
547 *OS << format("Unrecognized extended op 0x%02.02" PRIx8, SubOpcode)
548 << format(" length %" PRIx64, Len);
549 // Len doesn't include the zero opcode byte or the length itself, but
550 // it does include the sub_opcode, so we have to adjust for that.
551 (*OffsetPtr) += Len - 1;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000552 break;
553 }
Paul Robinsone0833342017-11-22 15:14:49 +0000554 // Make sure the stated and parsed lengths are the same.
555 // Otherwise we have an unparseable line-number program.
556 if (*OffsetPtr - ExtOffset != Len) {
557 fprintf(stderr, "Unexpected line op length at offset 0x%8.8" PRIx32
558 " expected 0x%2.2" PRIx64 " found 0x%2.2" PRIx32 "\n",
559 ExtOffset, Len, *OffsetPtr - ExtOffset);
560 // Skip the rest of the line-number program.
561 *OffsetPtr = EndOffset;
562 return false;
563 }
Paul Robinson9d4eb692017-05-01 23:27:55 +0000564 } else if (Opcode < Prologue.OpcodeBase) {
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000565 if (OS)
566 *OS << LNStandardString(Opcode);
Paul Robinson9d4eb692017-05-01 23:27:55 +0000567 switch (Opcode) {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000568 // Standard Opcodes
569 case DW_LNS_copy:
570 // Takes no arguments. Append a row to the matrix using the
571 // current values of the state-machine registers. Then set
572 // the basic_block register to false.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000573 State.appendRowToMatrix(*OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000574 if (OS) {
575 *OS << "\n";
576 OS->indent(12);
577 State.Row.dump(*OS);
578 *OS << "\n";
579 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000580 break;
581
582 case DW_LNS_advance_pc:
583 // Takes a single unsigned LEB128 operand, multiplies it by the
584 // min_inst_length field of the prologue, and adds the
585 // result to the address register of the state machine.
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000586 {
587 uint64_t AddrOffset =
588 DebugLineData.getULEB128(OffsetPtr) * Prologue.MinInstLength;
589 State.Row.Address += AddrOffset;
590 if (OS)
591 *OS << " (" << AddrOffset << ")";
592 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000593 break;
594
595 case DW_LNS_advance_line:
596 // Takes a single signed LEB128 operand and adds that value to
597 // the line register of the state machine.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000598 State.Row.Line += DebugLineData.getSLEB128(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000599 if (OS)
600 *OS << " (" << State.Row.Line << ")";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000601 break;
602
603 case DW_LNS_set_file:
604 // Takes a single unsigned LEB128 operand and stores it in the file
605 // register of the state machine.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000606 State.Row.File = DebugLineData.getULEB128(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000607 if (OS)
608 *OS << " (" << State.Row.File << ")";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000609 break;
610
611 case DW_LNS_set_column:
612 // Takes a single unsigned LEB128 operand and stores it in the
613 // column register of the state machine.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000614 State.Row.Column = DebugLineData.getULEB128(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000615 if (OS)
616 *OS << " (" << State.Row.Column << ")";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000617 break;
618
619 case DW_LNS_negate_stmt:
620 // Takes no arguments. Set the is_stmt register of the state
621 // machine to the logical negation of its current value.
Alexey Samsonov110d5952014-04-30 00:09:19 +0000622 State.Row.IsStmt = !State.Row.IsStmt;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000623 break;
624
625 case DW_LNS_set_basic_block:
626 // Takes no arguments. Set the basic_block register of the
627 // state machine to true
Alexey Samsonov110d5952014-04-30 00:09:19 +0000628 State.Row.BasicBlock = true;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000629 break;
630
631 case DW_LNS_const_add_pc:
632 // Takes no arguments. Add to the address register of the state
633 // machine the address increment value corresponding to special
634 // opcode 255. The motivation for DW_LNS_const_add_pc is this:
635 // when the statement program needs to advance the address by a
636 // small amount, it can use a single special opcode, which occupies
637 // a single byte. When it needs to advance the address by up to
638 // twice the range of the last special opcode, it can use
639 // DW_LNS_const_add_pc followed by a special opcode, for a total
640 // of two bytes. Only if it needs to advance the address by more
641 // than twice that range will it need to use both DW_LNS_advance_pc
642 // and a special opcode, requiring three or more bytes.
643 {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000644 uint8_t AdjustOpcode = 255 - Prologue.OpcodeBase;
645 uint64_t AddrOffset =
646 (AdjustOpcode / Prologue.LineRange) * Prologue.MinInstLength;
647 State.Row.Address += AddrOffset;
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000648 if (OS)
649 *OS
650 << format(" (0x%16.16" PRIx64 ")", AddrOffset);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000651 }
652 break;
653
654 case DW_LNS_fixed_advance_pc:
655 // Takes a single uhalf operand. Add to the address register of
656 // the state machine the value of the (unencoded) operand. This
657 // is the only extended opcode that takes an argument that is not
658 // a variable length number. The motivation for DW_LNS_fixed_advance_pc
659 // is this: existing assemblers cannot emit DW_LNS_advance_pc or
660 // special opcodes because they cannot encode LEB128 numbers or
661 // judge when the computation of a special opcode overflows and
662 // requires the use of DW_LNS_advance_pc. Such assemblers, however,
663 // can use DW_LNS_fixed_advance_pc instead, sacrificing compression.
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000664 {
665 uint16_t PCOffset = DebugLineData.getU16(OffsetPtr);
666 State.Row.Address += PCOffset;
667 if (OS)
668 *OS
669 << format(" (0x%16.16" PRIx64 ")", PCOffset);
670 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000671 break;
672
673 case DW_LNS_set_prologue_end:
674 // Takes no arguments. Set the prologue_end register of the
675 // state machine to true
Alexey Samsonov110d5952014-04-30 00:09:19 +0000676 State.Row.PrologueEnd = true;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000677 break;
678
679 case DW_LNS_set_epilogue_begin:
680 // Takes no arguments. Set the basic_block register of the
681 // state machine to true
Alexey Samsonov110d5952014-04-30 00:09:19 +0000682 State.Row.EpilogueBegin = true;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000683 break;
684
685 case DW_LNS_set_isa:
686 // Takes a single unsigned LEB128 operand and stores it in the
687 // column register of the state machine.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000688 State.Row.Isa = DebugLineData.getULEB128(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000689 if (OS)
690 *OS << " (" << State.Row.Isa << ")";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000691 break;
692
693 default:
694 // Handle any unknown standard opcodes here. We know the lengths
695 // of such opcodes because they are specified in the prologue
696 // as a multiple of LEB128 operands for each opcode.
697 {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000698 assert(Opcode - 1U < Prologue.StandardOpcodeLengths.size());
699 uint8_t OpcodeLength = Prologue.StandardOpcodeLengths[Opcode - 1];
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000700 for (uint8_t I = 0; I < OpcodeLength; ++I) {
701 uint64_t Value = DebugLineData.getULEB128(OffsetPtr);
702 if (OS)
703 *OS << format("Skipping ULEB128 value: 0x%16.16" PRIx64 ")\n",
704 Value);
705 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000706 }
707 break;
708 }
709 } else {
710 // Special Opcodes
711
712 // A special opcode value is chosen based on the amount that needs
713 // to be added to the line and address registers. The maximum line
714 // increment for a special opcode is the value of the line_base
715 // field in the header, plus the value of the line_range field,
716 // minus 1 (line base + line range - 1). If the desired line
717 // increment is greater than the maximum line increment, a standard
NAKAMURA Takumif9959852011-10-08 11:22:47 +0000718 // opcode must be used instead of a special opcode. The "address
719 // advance" is calculated by dividing the desired address increment
Benjamin Kramer5acab502011-09-15 02:12:05 +0000720 // by the minimum_instruction_length field from the header. The
721 // special opcode is then calculated using the following formula:
722 //
723 // opcode = (desired line increment - line_base) +
724 // (line_range * address advance) + opcode_base
725 //
726 // If the resulting opcode is greater than 255, a standard opcode
727 // must be used instead.
728 //
729 // To decode a special opcode, subtract the opcode_base from the
730 // opcode itself to give the adjusted opcode. The amount to
731 // increment the address register is the result of the adjusted
732 // opcode divided by the line_range multiplied by the
733 // minimum_instruction_length field from the header. That is:
734 //
735 // address increment = (adjusted opcode / line_range) *
736 // minimum_instruction_length
737 //
738 // The amount to increment the line register is the line_base plus
739 // the result of the adjusted opcode modulo the line_range. That is:
740 //
741 // line increment = line_base + (adjusted opcode % line_range)
742
Paul Robinson9d4eb692017-05-01 23:27:55 +0000743 uint8_t AdjustOpcode = Opcode - Prologue.OpcodeBase;
744 uint64_t AddrOffset =
745 (AdjustOpcode / Prologue.LineRange) * Prologue.MinInstLength;
746 int32_t LineOffset =
747 Prologue.LineBase + (AdjustOpcode % Prologue.LineRange);
748 State.Row.Line += LineOffset;
749 State.Row.Address += AddrOffset;
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000750
751 if (OS) {
752 *OS << "address += " << ((uint32_t)AdjustOpcode)
753 << ", line += " << LineOffset << "\n";
754 OS->indent(12);
755 State.Row.dump(*OS);
756 }
757
Paul Robinson9d4eb692017-05-01 23:27:55 +0000758 State.appendRowToMatrix(*OffsetPtr);
Dehao Chen1b54fce2016-04-28 22:09:37 +0000759 // Reset discriminator to 0.
760 State.Row.Discriminator = 0;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000761 }
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000762 if(OS)
763 *OS << "\n";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000764 }
765
Alexey Samsonov110d5952014-04-30 00:09:19 +0000766 if (!State.Sequence.Empty) {
767 fprintf(stderr, "warning: last sequence in debug line table is not"
768 "terminated!\n");
769 }
770
771 // Sort all sequences so that address lookup will work faster.
772 if (!Sequences.empty()) {
773 std::sort(Sequences.begin(), Sequences.end(), Sequence::orderByLowPC);
774 // Note: actually, instruction address ranges of sequences should not
775 // overlap (in shared objects and executables). If they do, the address
776 // lookup would still work, though, but result would be ambiguous.
777 // We don't report warning in this case. For example,
778 // sometimes .so compiled from multiple object files contains a few
779 // rudimentary sequences for address ranges [0x0, 0xsomething).
780 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000781
Paul Robinson9d4eb692017-05-01 23:27:55 +0000782 return EndOffset;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000783}
784
Keno Fischerc2c60182015-05-31 23:37:04 +0000785uint32_t
Paul Robinson9d4eb692017-05-01 23:27:55 +0000786DWARFDebugLine::LineTable::findRowInSeq(const DWARFDebugLine::Sequence &Seq,
787 uint64_t Address) const {
788 if (!Seq.containsPC(Address))
Keno Fischerc2c60182015-05-31 23:37:04 +0000789 return UnknownRowIndex;
790 // Search for instruction address in the rows describing the sequence.
791 // Rows are stored in a vector, so we may use arithmetical operations with
792 // iterators.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000793 DWARFDebugLine::Row Row;
794 Row.Address = Address;
795 RowIter FirstRow = Rows.begin() + Seq.FirstRowIndex;
796 RowIter LastRow = Rows.begin() + Seq.LastRowIndex;
797 LineTable::RowIter RowPos = std::lower_bound(
798 FirstRow, LastRow, Row, DWARFDebugLine::Row::orderByAddress);
799 if (RowPos == LastRow) {
800 return Seq.LastRowIndex - 1;
Keno Fischerc2c60182015-05-31 23:37:04 +0000801 }
Paul Robinson9d4eb692017-05-01 23:27:55 +0000802 uint32_t Index = Seq.FirstRowIndex + (RowPos - FirstRow);
803 if (RowPos->Address > Address) {
804 if (RowPos == FirstRow)
Keno Fischerc2c60182015-05-31 23:37:04 +0000805 return UnknownRowIndex;
806 else
Paul Robinson9d4eb692017-05-01 23:27:55 +0000807 Index--;
Keno Fischerc2c60182015-05-31 23:37:04 +0000808 }
Paul Robinson9d4eb692017-05-01 23:27:55 +0000809 return Index;
Keno Fischerc2c60182015-05-31 23:37:04 +0000810}
811
Paul Robinson9d4eb692017-05-01 23:27:55 +0000812uint32_t DWARFDebugLine::LineTable::lookupAddress(uint64_t Address) const {
Alexey Samsonov947228c2012-08-07 11:46:57 +0000813 if (Sequences.empty())
Keno Fischerc2c60182015-05-31 23:37:04 +0000814 return UnknownRowIndex;
Alexey Samsonov947228c2012-08-07 11:46:57 +0000815 // First, find an instruction sequence containing the given address.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000816 DWARFDebugLine::Sequence Sequence;
817 Sequence.LowPC = Address;
818 SequenceIter FirstSeq = Sequences.begin();
819 SequenceIter LastSeq = Sequences.end();
820 SequenceIter SeqPos = std::lower_bound(
821 FirstSeq, LastSeq, Sequence, DWARFDebugLine::Sequence::orderByLowPC);
822 DWARFDebugLine::Sequence FoundSeq;
823 if (SeqPos == LastSeq) {
824 FoundSeq = Sequences.back();
825 } else if (SeqPos->LowPC == Address) {
826 FoundSeq = *SeqPos;
Alexey Samsonov947228c2012-08-07 11:46:57 +0000827 } else {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000828 if (SeqPos == FirstSeq)
Keno Fischerc2c60182015-05-31 23:37:04 +0000829 return UnknownRowIndex;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000830 FoundSeq = *(SeqPos - 1);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000831 }
Paul Robinson9d4eb692017-05-01 23:27:55 +0000832 return findRowInSeq(FoundSeq, Address);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000833}
Alexey Samsonov45be7932012-08-30 07:49:50 +0000834
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000835bool DWARFDebugLine::LineTable::lookupAddressRange(
Paul Robinson9d4eb692017-05-01 23:27:55 +0000836 uint64_t Address, uint64_t Size, std::vector<uint32_t> &Result) const {
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000837 if (Sequences.empty())
838 return false;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000839 uint64_t EndAddr = Address + Size;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000840 // First, find an instruction sequence containing the given address.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000841 DWARFDebugLine::Sequence Sequence;
842 Sequence.LowPC = Address;
843 SequenceIter FirstSeq = Sequences.begin();
844 SequenceIter LastSeq = Sequences.end();
845 SequenceIter SeqPos = std::lower_bound(
846 FirstSeq, LastSeq, Sequence, DWARFDebugLine::Sequence::orderByLowPC);
847 if (SeqPos == LastSeq || SeqPos->LowPC != Address) {
848 if (SeqPos == FirstSeq)
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000849 return false;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000850 SeqPos--;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000851 }
Paul Robinson9d4eb692017-05-01 23:27:55 +0000852 if (!SeqPos->containsPC(Address))
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000853 return false;
854
Paul Robinson9d4eb692017-05-01 23:27:55 +0000855 SequenceIter StartPos = SeqPos;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000856
857 // Add the rows from the first sequence to the vector, starting with the
858 // index we just calculated
859
Paul Robinson9d4eb692017-05-01 23:27:55 +0000860 while (SeqPos != LastSeq && SeqPos->LowPC < EndAddr) {
861 const DWARFDebugLine::Sequence &CurSeq = *SeqPos;
Keno Fischerc2c60182015-05-31 23:37:04 +0000862 // For the first sequence, we need to find which row in the sequence is the
863 // first in our range.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000864 uint32_t FirstRowIndex = CurSeq.FirstRowIndex;
865 if (SeqPos == StartPos)
866 FirstRowIndex = findRowInSeq(CurSeq, Address);
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000867
Keno Fischerc2c60182015-05-31 23:37:04 +0000868 // Figure out the last row in the range.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000869 uint32_t LastRowIndex = findRowInSeq(CurSeq, EndAddr - 1);
870 if (LastRowIndex == UnknownRowIndex)
871 LastRowIndex = CurSeq.LastRowIndex - 1;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000872
Paul Robinson9d4eb692017-05-01 23:27:55 +0000873 assert(FirstRowIndex != UnknownRowIndex);
874 assert(LastRowIndex != UnknownRowIndex);
Keno Fischerc2c60182015-05-31 23:37:04 +0000875
Paul Robinson9d4eb692017-05-01 23:27:55 +0000876 for (uint32_t I = FirstRowIndex; I <= LastRowIndex; ++I) {
877 Result.push_back(I);
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000878 }
879
Paul Robinson9d4eb692017-05-01 23:27:55 +0000880 ++SeqPos;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000881 }
NAKAMURA Takumi4b86cdb2013-01-26 01:45:06 +0000882
883 return true;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000884}
885
Paul Robinson9d4eb692017-05-01 23:27:55 +0000886bool DWARFDebugLine::LineTable::hasFileAtIndex(uint64_t FileIndex) const {
Pete Cooperb2ba7762016-07-22 01:41:32 +0000887 return FileIndex != 0 && FileIndex <= Prologue.FileNames.size();
888}
889
Paul Robinson9d4eb692017-05-01 23:27:55 +0000890bool DWARFDebugLine::LineTable::getFileNameByIndex(uint64_t FileIndex,
891 const char *CompDir,
892 FileLineInfoKind Kind,
893 std::string &Result) const {
Pete Cooperb2ba7762016-07-22 01:41:32 +0000894 if (Kind == FileLineInfoKind::None || !hasFileAtIndex(FileIndex))
Alexey Samsonov45be7932012-08-30 07:49:50 +0000895 return false;
896 const FileNameEntry &Entry = Prologue.FileNames[FileIndex - 1];
Paul Robinsonba1c9152017-05-02 17:37:32 +0000897 StringRef FileName = Entry.Name;
Alexey Samsonovdce67342014-05-15 21:24:32 +0000898 if (Kind != FileLineInfoKind::AbsoluteFilePath ||
Alexey Samsonov45be7932012-08-30 07:49:50 +0000899 sys::path::is_absolute(FileName)) {
900 Result = FileName;
901 return true;
902 }
Frederic Riss101b5e22014-09-19 15:11:51 +0000903
Alexey Samsonov45be7932012-08-30 07:49:50 +0000904 SmallString<16> FilePath;
905 uint64_t IncludeDirIndex = Entry.DirIdx;
Paul Robinsonba1c9152017-05-02 17:37:32 +0000906 StringRef IncludeDir;
Alexey Samsonov45be7932012-08-30 07:49:50 +0000907 // Be defensive about the contents of Entry.
908 if (IncludeDirIndex > 0 &&
Frederic Riss101b5e22014-09-19 15:11:51 +0000909 IncludeDirIndex <= Prologue.IncludeDirectories.size())
910 IncludeDir = Prologue.IncludeDirectories[IncludeDirIndex - 1];
911
912 // We may still need to append compilation directory of compile unit.
913 // We know that FileName is not absolute, the only way to have an
914 // absolute path at this point would be if IncludeDir is absolute.
915 if (CompDir && Kind == FileLineInfoKind::AbsoluteFilePath &&
916 sys::path::is_relative(IncludeDir))
917 sys::path::append(FilePath, CompDir);
918
919 // sys::path::append skips empty strings.
920 sys::path::append(FilePath, IncludeDir, FileName);
Alexey Samsonov45be7932012-08-30 07:49:50 +0000921 Result = FilePath.str();
922 return true;
923}
Frederic Riss101b5e22014-09-19 15:11:51 +0000924
Dehao Chen1b54fce2016-04-28 22:09:37 +0000925bool DWARFDebugLine::LineTable::getFileLineInfoForAddress(
926 uint64_t Address, const char *CompDir, FileLineInfoKind Kind,
927 DILineInfo &Result) const {
Frederic Riss101b5e22014-09-19 15:11:51 +0000928 // Get the index of row we're looking for in the line table.
929 uint32_t RowIndex = lookupAddress(Address);
930 if (RowIndex == -1U)
931 return false;
932 // Take file number and line/column from the row.
933 const auto &Row = Rows[RowIndex];
934 if (!getFileNameByIndex(Row.File, CompDir, Kind, Result.FileName))
935 return false;
936 Result.Line = Row.Line;
937 Result.Column = Row.Column;
Eric Christopherba1024c2016-12-14 18:29:39 +0000938 Result.Discriminator = Row.Discriminator;
Frederic Riss101b5e22014-09-19 15:11:51 +0000939 return true;
940}