blob: 38272b6afdfee1c0673ad4d4bfe63c7811a5ccd8 [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 Robinsona06f8dc2017-12-18 19:08:35 +000051 HasMD5 = false;
Alexey Samsonov836b1ae2014-04-29 21:28:13 +000052 StandardOpcodeLengths.clear();
53 IncludeDirectories.clear();
54 FileNames.clear();
55}
56
Paul Robinson0a227092018-02-05 20:43:15 +000057void DWARFDebugLine::Prologue::dump(raw_ostream &OS,
58 DIDumpOptions DumpOptions) const {
Benjamin Kramer5acab502011-09-15 02:12:05 +000059 OS << "Line table prologue:\n"
Ed Maste6d0bee52015-05-28 15:38:17 +000060 << format(" total_length: 0x%8.8" PRIx64 "\n", TotalLength)
Paul Robinson75c068c2017-06-26 18:43:01 +000061 << format(" version: %u\n", getVersion());
62 if (getVersion() >= 5)
63 OS << format(" address_size: %u\n", getAddressSize())
64 << format(" seg_select_size: %u\n", SegSelectorSize);
65 OS << format(" prologue_length: 0x%8.8" PRIx64 "\n", PrologueLength)
David Blaikie1d4736e2014-02-24 23:58:54 +000066 << format(" min_inst_length: %u\n", MinInstLength)
Paul Robinson75c068c2017-06-26 18:43:01 +000067 << format(getVersion() >= 4 ? "max_ops_per_inst: %u\n" : "", MaxOpsPerInst)
David Blaikie1d4736e2014-02-24 23:58:54 +000068 << format(" default_is_stmt: %u\n", DefaultIsStmt)
69 << format(" line_base: %i\n", LineBase)
70 << format(" line_range: %u\n", LineRange)
71 << format(" opcode_base: %u\n", OpcodeBase);
Benjamin Kramer5acab502011-09-15 02:12:05 +000072
Paul Robinson9d4eb692017-05-01 23:27:55 +000073 for (uint32_t I = 0; I != StandardOpcodeLengths.size(); ++I)
Mehdi Amini149f6ea2016-10-05 05:59:29 +000074 OS << format("standard_opcode_lengths[%s] = %u\n",
Paul Robinson9d4eb692017-05-01 23:27:55 +000075 LNStandardString(I + 1).data(), StandardOpcodeLengths[I]);
Benjamin Kramer5acab502011-09-15 02:12:05 +000076
Paul Robinson8181d232018-01-18 20:33:35 +000077 if (!IncludeDirectories.empty()) {
78 // DWARF v5 starts directory indexes at 0.
79 uint32_t DirBase = getVersion() >= 5 ? 0 : 1;
Paul Robinson0a227092018-02-05 20:43:15 +000080 for (uint32_t I = 0; I != IncludeDirectories.size(); ++I) {
81 OS << format("include_directories[%3u] = ", I + DirBase);
82 IncludeDirectories[I].dump(OS, DumpOptions);
83 OS << '\n';
84 }
Paul Robinson8181d232018-01-18 20:33:35 +000085 }
Benjamin Kramer5acab502011-09-15 02:12:05 +000086
87 if (!FileNames.empty()) {
Paul Robinsona06f8dc2017-12-18 19:08:35 +000088 if (HasMD5)
89 OS << " Dir MD5 Checksum File Name\n"
90 << " ---- -------------------------------- -----------"
91 "---------------\n";
92 else
93 OS << " Dir Mod Time File Len File Name\n"
94 << " ---- ---------- ---------- -----------"
95 "----------------\n";
Paul Robinson9d4eb692017-05-01 23:27:55 +000096 for (uint32_t I = 0; I != FileNames.size(); ++I) {
97 const FileNameEntry &FileEntry = FileNames[I];
Paul Robinsona06f8dc2017-12-18 19:08:35 +000098 OS << format("file_names[%3u] %4" PRIu64 " ", I + 1, FileEntry.DirIdx);
99 if (HasMD5)
100 OS << FileEntry.Checksum.digest();
101 else
102 OS << format("0x%8.8" PRIx64 " 0x%8.8" PRIx64, FileEntry.ModTime,
103 FileEntry.Length);
Paul Robinson0a227092018-02-05 20:43:15 +0000104 OS << ' ';
105 FileEntry.Name.dump(OS, DumpOptions);
106 OS << '\n';
Benjamin Kramer5acab502011-09-15 02:12:05 +0000107 }
108 }
109}
110
Paul Robinson2bc38732017-05-02 21:40:47 +0000111// Parse v2-v4 directory and file tables.
112static void
Paul Robinson17536b92017-06-29 16:52:08 +0000113parseV2DirFileTables(const DWARFDataExtractor &DebugLineData,
114 uint32_t *OffsetPtr, uint64_t EndPrologueOffset,
Paul Robinson0a227092018-02-05 20:43:15 +0000115 std::vector<DWARFFormValue> &IncludeDirectories,
Paul Robinson2bc38732017-05-02 21:40:47 +0000116 std::vector<DWARFDebugLine::FileNameEntry> &FileNames) {
117 while (*OffsetPtr < EndPrologueOffset) {
118 StringRef S = DebugLineData.getCStrRef(OffsetPtr);
119 if (S.empty())
120 break;
Paul Robinson0a227092018-02-05 20:43:15 +0000121 DWARFFormValue Dir(dwarf::DW_FORM_string);
122 Dir.setPValue(S.data());
123 IncludeDirectories.push_back(Dir);
Paul Robinson2bc38732017-05-02 21:40:47 +0000124 }
125
126 while (*OffsetPtr < EndPrologueOffset) {
127 StringRef Name = DebugLineData.getCStrRef(OffsetPtr);
128 if (Name.empty())
129 break;
130 DWARFDebugLine::FileNameEntry FileEntry;
Paul Robinson0a227092018-02-05 20:43:15 +0000131 FileEntry.Name.setForm(dwarf::DW_FORM_string);
132 FileEntry.Name.setPValue(Name.data());
Paul Robinson2bc38732017-05-02 21:40:47 +0000133 FileEntry.DirIdx = DebugLineData.getULEB128(OffsetPtr);
134 FileEntry.ModTime = DebugLineData.getULEB128(OffsetPtr);
135 FileEntry.Length = DebugLineData.getULEB128(OffsetPtr);
136 FileNames.push_back(FileEntry);
137 }
138}
139
140// Parse v5 directory/file entry content descriptions.
141// Returns the descriptors, or an empty vector if we did not find a path or
142// ran off the end of the prologue.
143static ContentDescriptors
Paul Robinson17536b92017-06-29 16:52:08 +0000144parseV5EntryFormat(const DWARFDataExtractor &DebugLineData, uint32_t *OffsetPtr,
Paul Robinsona06f8dc2017-12-18 19:08:35 +0000145 uint64_t EndPrologueOffset, bool *HasMD5) {
Paul Robinson2bc38732017-05-02 21:40:47 +0000146 ContentDescriptors Descriptors;
147 int FormatCount = DebugLineData.getU8(OffsetPtr);
148 bool HasPath = false;
149 for (int I = 0; I != FormatCount; ++I) {
150 if (*OffsetPtr >= EndPrologueOffset)
151 return ContentDescriptors();
152 ContentDescriptor Descriptor;
153 Descriptor.Type =
154 dwarf::LineNumberEntryFormat(DebugLineData.getULEB128(OffsetPtr));
155 Descriptor.Form = dwarf::Form(DebugLineData.getULEB128(OffsetPtr));
156 if (Descriptor.Type == dwarf::DW_LNCT_path)
157 HasPath = true;
Paul Robinsona06f8dc2017-12-18 19:08:35 +0000158 else if (Descriptor.Type == dwarf::DW_LNCT_MD5 && HasMD5)
159 *HasMD5 = true;
Paul Robinson2bc38732017-05-02 21:40:47 +0000160 Descriptors.push_back(Descriptor);
161 }
162 return HasPath ? Descriptors : ContentDescriptors();
163}
164
165static bool
Paul Robinson17536b92017-06-29 16:52:08 +0000166parseV5DirFileTables(const DWARFDataExtractor &DebugLineData,
167 uint32_t *OffsetPtr, uint64_t EndPrologueOffset,
Paul Robinsonbf750c82018-01-29 20:57:43 +0000168 const DWARFFormParams &FormParams, const DWARFContext &Ctx,
169 const DWARFUnit *U, bool &HasMD5,
Paul Robinson0a227092018-02-05 20:43:15 +0000170 std::vector<DWARFFormValue> &IncludeDirectories,
Paul Robinson2bc38732017-05-02 21:40:47 +0000171 std::vector<DWARFDebugLine::FileNameEntry> &FileNames) {
172 // Get the directory entry description.
173 ContentDescriptors DirDescriptors =
Paul Robinsona06f8dc2017-12-18 19:08:35 +0000174 parseV5EntryFormat(DebugLineData, OffsetPtr, EndPrologueOffset, nullptr);
Paul Robinson2bc38732017-05-02 21:40:47 +0000175 if (DirDescriptors.empty())
176 return false;
177
178 // Get the directory entries, according to the format described above.
179 int DirEntryCount = DebugLineData.getU8(OffsetPtr);
180 for (int I = 0; I != DirEntryCount; ++I) {
181 if (*OffsetPtr >= EndPrologueOffset)
182 return false;
183 for (auto Descriptor : DirDescriptors) {
184 DWARFFormValue Value(Descriptor.Form);
185 switch (Descriptor.Type) {
186 case DW_LNCT_path:
Paul Robinsonbf750c82018-01-29 20:57:43 +0000187 if (!Value.extractValue(DebugLineData, OffsetPtr, FormParams, &Ctx, U))
Paul Robinson2bc38732017-05-02 21:40:47 +0000188 return false;
Paul Robinson0a227092018-02-05 20:43:15 +0000189 IncludeDirectories.push_back(Value);
Paul Robinson2bc38732017-05-02 21:40:47 +0000190 break;
191 default:
Paul Robinson75c068c2017-06-26 18:43:01 +0000192 if (!Value.skipValue(DebugLineData, OffsetPtr, FormParams))
Paul Robinson2bc38732017-05-02 21:40:47 +0000193 return false;
194 }
195 }
196 }
197
198 // Get the file entry description.
199 ContentDescriptors FileDescriptors =
Paul Robinsona06f8dc2017-12-18 19:08:35 +0000200 parseV5EntryFormat(DebugLineData, OffsetPtr, EndPrologueOffset, &HasMD5);
Paul Robinson2bc38732017-05-02 21:40:47 +0000201 if (FileDescriptors.empty())
202 return false;
203
204 // Get the file entries, according to the format described above.
205 int FileEntryCount = DebugLineData.getU8(OffsetPtr);
206 for (int I = 0; I != FileEntryCount; ++I) {
207 if (*OffsetPtr >= EndPrologueOffset)
208 return false;
209 DWARFDebugLine::FileNameEntry FileEntry;
210 for (auto Descriptor : FileDescriptors) {
211 DWARFFormValue Value(Descriptor.Form);
Paul Robinsonbf750c82018-01-29 20:57:43 +0000212 if (!Value.extractValue(DebugLineData, OffsetPtr, FormParams, &Ctx, U))
Paul Robinson2bc38732017-05-02 21:40:47 +0000213 return false;
214 switch (Descriptor.Type) {
215 case DW_LNCT_path:
Paul Robinson0a227092018-02-05 20:43:15 +0000216 FileEntry.Name = Value;
Paul Robinson2bc38732017-05-02 21:40:47 +0000217 break;
218 case DW_LNCT_directory_index:
219 FileEntry.DirIdx = Value.getAsUnsignedConstant().getValue();
220 break;
221 case DW_LNCT_timestamp:
222 FileEntry.ModTime = Value.getAsUnsignedConstant().getValue();
223 break;
224 case DW_LNCT_size:
225 FileEntry.Length = Value.getAsUnsignedConstant().getValue();
226 break;
Paul Robinsona06f8dc2017-12-18 19:08:35 +0000227 case DW_LNCT_MD5:
228 assert(Value.getAsBlock().getValue().size() == 16);
229 std::uninitialized_copy_n(Value.getAsBlock().getValue().begin(), 16,
230 FileEntry.Checksum.Bytes.begin());
231 break;
Paul Robinson2bc38732017-05-02 21:40:47 +0000232 default:
233 break;
234 }
235 }
236 FileNames.push_back(FileEntry);
237 }
238 return true;
239}
240
Paul Robinson17536b92017-06-29 16:52:08 +0000241bool DWARFDebugLine::Prologue::parse(const DWARFDataExtractor &DebugLineData,
Paul Robinsonbf750c82018-01-29 20:57:43 +0000242 uint32_t *OffsetPtr,
243 const DWARFContext &Ctx,
244 const DWARFUnit *U) {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000245 const uint64_t PrologueOffset = *OffsetPtr;
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000246
247 clear();
Paul Robinson9d4eb692017-05-01 23:27:55 +0000248 TotalLength = DebugLineData.getU32(OffsetPtr);
Ed Maste6d0bee52015-05-28 15:38:17 +0000249 if (TotalLength == UINT32_MAX) {
Paul Robinson75c068c2017-06-26 18:43:01 +0000250 FormParams.Format = dwarf::DWARF64;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000251 TotalLength = DebugLineData.getU64(OffsetPtr);
Paul Robinson75c068c2017-06-26 18:43:01 +0000252 } else if (TotalLength >= 0xffffff00) {
Ed Maste6d0bee52015-05-28 15:38:17 +0000253 return false;
254 }
Paul Robinson75c068c2017-06-26 18:43:01 +0000255 FormParams.Version = DebugLineData.getU16(OffsetPtr);
256 if (getVersion() < 2)
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000257 return false;
258
Paul Robinson75c068c2017-06-26 18:43:01 +0000259 if (getVersion() >= 5) {
260 FormParams.AddrSize = DebugLineData.getU8(OffsetPtr);
Paul Robinson63811a42017-11-22 15:33:17 +0000261 assert((DebugLineData.getAddressSize() == 0 ||
262 DebugLineData.getAddressSize() == getAddressSize()) &&
Paul Robinson75c068c2017-06-26 18:43:01 +0000263 "Line table header and data extractor disagree");
Paul Robinson2bc38732017-05-02 21:40:47 +0000264 SegSelectorSize = DebugLineData.getU8(OffsetPtr);
265 }
266
Paul Robinson9d4eb692017-05-01 23:27:55 +0000267 PrologueLength = DebugLineData.getUnsigned(OffsetPtr, sizeofPrologueLength());
268 const uint64_t EndPrologueOffset = PrologueLength + *OffsetPtr;
269 MinInstLength = DebugLineData.getU8(OffsetPtr);
Paul Robinson75c068c2017-06-26 18:43:01 +0000270 if (getVersion() >= 4)
Paul Robinson9d4eb692017-05-01 23:27:55 +0000271 MaxOpsPerInst = DebugLineData.getU8(OffsetPtr);
272 DefaultIsStmt = DebugLineData.getU8(OffsetPtr);
273 LineBase = DebugLineData.getU8(OffsetPtr);
274 LineRange = DebugLineData.getU8(OffsetPtr);
275 OpcodeBase = DebugLineData.getU8(OffsetPtr);
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000276
277 StandardOpcodeLengths.reserve(OpcodeBase - 1);
Paul Robinson9d4eb692017-05-01 23:27:55 +0000278 for (uint32_t I = 1; I < OpcodeBase; ++I) {
279 uint8_t OpLen = DebugLineData.getU8(OffsetPtr);
280 StandardOpcodeLengths.push_back(OpLen);
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000281 }
282
Paul Robinson75c068c2017-06-26 18:43:01 +0000283 if (getVersion() >= 5) {
Paul Robinson2bc38732017-05-02 21:40:47 +0000284 if (!parseV5DirFileTables(DebugLineData, OffsetPtr, EndPrologueOffset,
Paul Robinsonbf750c82018-01-29 20:57:43 +0000285 FormParams, Ctx, U, HasMD5, IncludeDirectories,
Paul Robinsone5400f82017-11-07 19:57:12 +0000286 FileNames)) {
Paul Robinson2bc38732017-05-02 21:40:47 +0000287 fprintf(stderr,
288 "warning: parsing line table prologue at 0x%8.8" PRIx64
289 " found an invalid directory or file table description at"
290 " 0x%8.8" PRIx64 "\n", PrologueOffset, (uint64_t)*OffsetPtr);
291 return false;
292 }
293 } else
294 parseV2DirFileTables(DebugLineData, OffsetPtr, EndPrologueOffset,
295 IncludeDirectories, FileNames);
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000296
Paul Robinson9d4eb692017-05-01 23:27:55 +0000297 if (*OffsetPtr != EndPrologueOffset) {
298 fprintf(stderr,
299 "warning: parsing line table prologue at 0x%8.8" PRIx64
300 " should have ended at 0x%8.8" PRIx64
301 " but it ended at 0x%8.8" PRIx64 "\n",
302 PrologueOffset, EndPrologueOffset, (uint64_t)*OffsetPtr);
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000303 return false;
304 }
305 return true;
306}
307
Paul Robinson9d4eb692017-05-01 23:27:55 +0000308DWARFDebugLine::Row::Row(bool DefaultIsStmt) { reset(DefaultIsStmt); }
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000309
Benjamin Kramer5acab502011-09-15 02:12:05 +0000310void DWARFDebugLine::Row::postAppend() {
311 BasicBlock = false;
312 PrologueEnd = false;
313 EpilogueBegin = false;
314}
315
Paul Robinson9d4eb692017-05-01 23:27:55 +0000316void DWARFDebugLine::Row::reset(bool DefaultIsStmt) {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000317 Address = 0;
318 Line = 1;
319 Column = 0;
320 File = 1;
321 Isa = 0;
Diego Novillo5b5cf502014-02-14 19:27:53 +0000322 Discriminator = 0;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000323 IsStmt = DefaultIsStmt;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000324 BasicBlock = false;
325 EndSequence = false;
326 PrologueEnd = false;
327 EpilogueBegin = false;
328}
329
Greg Clayton67070462017-05-02 22:48:52 +0000330void DWARFDebugLine::Row::dumpTableHeader(raw_ostream &OS) {
331 OS << "Address Line Column File ISA Discriminator Flags\n"
332 << "------------------ ------ ------ ------ --- ------------- "
333 "-------------\n";
334}
335
Benjamin Kramer5acab502011-09-15 02:12:05 +0000336void DWARFDebugLine::Row::dump(raw_ostream &OS) const {
Benjamin Kramerf3da5292011-11-05 08:57:40 +0000337 OS << format("0x%16.16" PRIx64 " %6u %6u", Address, Line, Column)
Diego Novillo5b5cf502014-02-14 19:27:53 +0000338 << format(" %6u %3u %13u ", File, Isa, Discriminator)
Dehao Chen1b54fce2016-04-28 22:09:37 +0000339 << (IsStmt ? " is_stmt" : "") << (BasicBlock ? " basic_block" : "")
Benjamin Kramer5acab502011-09-15 02:12:05 +0000340 << (PrologueEnd ? " prologue_end" : "")
341 << (EpilogueBegin ? " epilogue_begin" : "")
Dehao Chen1b54fce2016-04-28 22:09:37 +0000342 << (EndSequence ? " end_sequence" : "") << '\n';
Benjamin Kramer5acab502011-09-15 02:12:05 +0000343}
344
Dehao Chen1b54fce2016-04-28 22:09:37 +0000345DWARFDebugLine::Sequence::Sequence() { reset(); }
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000346
347void DWARFDebugLine::Sequence::reset() {
348 LowPC = 0;
349 HighPC = 0;
350 FirstRowIndex = 0;
351 LastRowIndex = 0;
352 Empty = true;
353}
354
Dehao Chen1b54fce2016-04-28 22:09:37 +0000355DWARFDebugLine::LineTable::LineTable() { clear(); }
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000356
Paul Robinson0a227092018-02-05 20:43:15 +0000357void DWARFDebugLine::LineTable::dump(raw_ostream &OS,
358 DIDumpOptions DumpOptions) const {
359 Prologue.dump(OS, DumpOptions);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000360 OS << '\n';
361
362 if (!Rows.empty()) {
Greg Clayton67070462017-05-02 22:48:52 +0000363 Row::dumpTableHeader(OS);
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000364 for (const Row &R : Rows) {
365 R.dump(OS);
366 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000367 }
368}
369
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000370void DWARFDebugLine::LineTable::clear() {
371 Prologue.clear();
372 Rows.clear();
373 Sequences.clear();
374}
375
Alexey Samsonov110d5952014-04-30 00:09:19 +0000376DWARFDebugLine::ParsingState::ParsingState(struct LineTable *LT)
Eugene Zelenko2db0cfa2017-06-23 21:57:40 +0000377 : LineTable(LT) {
Alexey Samsonov110d5952014-04-30 00:09:19 +0000378 resetRowAndSequence();
379}
Nick Lewycky4d044922011-09-15 03:41:51 +0000380
Alexey Samsonov110d5952014-04-30 00:09:19 +0000381void DWARFDebugLine::ParsingState::resetRowAndSequence() {
382 Row.reset(LineTable->Prologue.DefaultIsStmt);
383 Sequence.reset();
384}
385
Paul Robinson9d4eb692017-05-01 23:27:55 +0000386void DWARFDebugLine::ParsingState::appendRowToMatrix(uint32_t Offset) {
Alexey Samsonov110d5952014-04-30 00:09:19 +0000387 if (Sequence.Empty) {
Alexey Samsonov947228c2012-08-07 11:46:57 +0000388 // Record the beginning of instruction sequence.
Alexey Samsonov110d5952014-04-30 00:09:19 +0000389 Sequence.Empty = false;
390 Sequence.LowPC = Row.Address;
391 Sequence.FirstRowIndex = RowNumber;
Alexey Samsonov947228c2012-08-07 11:46:57 +0000392 }
Alexey Samsonov110d5952014-04-30 00:09:19 +0000393 ++RowNumber;
394 LineTable->appendRow(Row);
395 if (Row.EndSequence) {
Alexey Samsonov947228c2012-08-07 11:46:57 +0000396 // Record the end of instruction sequence.
Alexey Samsonov110d5952014-04-30 00:09:19 +0000397 Sequence.HighPC = Row.Address;
398 Sequence.LastRowIndex = RowNumber;
399 if (Sequence.isValid())
400 LineTable->appendSequence(Sequence);
401 Sequence.reset();
Alexey Samsonov947228c2012-08-07 11:46:57 +0000402 }
Alexey Samsonov110d5952014-04-30 00:09:19 +0000403 Row.postAppend();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000404}
405
Benjamin Kramer5acab502011-09-15 02:12:05 +0000406const DWARFDebugLine::LineTable *
Paul Robinson9d4eb692017-05-01 23:27:55 +0000407DWARFDebugLine::getLineTable(uint32_t Offset) const {
408 LineTableConstIter Pos = LineTableMap.find(Offset);
409 if (Pos != LineTableMap.end())
410 return &Pos->second;
Craig Topper2617dcc2014-04-15 06:32:26 +0000411 return nullptr;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000412}
413
Benjamin Kramer679e1752011-09-15 20:43:18 +0000414const DWARFDebugLine::LineTable *
Paul Robinson511b54c2017-11-22 15:48:30 +0000415DWARFDebugLine::getOrParseLineTable(DWARFDataExtractor &DebugLineData,
Paul Robinsonbf750c82018-01-29 20:57:43 +0000416 uint32_t Offset, const DWARFContext &Ctx,
417 const DWARFUnit *U) {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000418 std::pair<LineTableIter, bool> Pos =
419 LineTableMap.insert(LineTableMapTy::value_type(Offset, LineTable()));
420 LineTable *LT = &Pos.first->second;
421 if (Pos.second) {
Paul Robinsonbf750c82018-01-29 20:57:43 +0000422 if (!LT->parse(DebugLineData, &Offset, Ctx, U))
Craig Topper2617dcc2014-04-15 06:32:26 +0000423 return nullptr;
Benjamin Kramer679e1752011-09-15 20:43:18 +0000424 }
Alexey Samsonov110d5952014-04-30 00:09:19 +0000425 return LT;
Benjamin Kramer679e1752011-09-15 20:43:18 +0000426}
427
Paul Robinson511b54c2017-11-22 15:48:30 +0000428bool DWARFDebugLine::LineTable::parse(DWARFDataExtractor &DebugLineData,
Paul Robinsonbf750c82018-01-29 20:57:43 +0000429 uint32_t *OffsetPtr,
430 const DWARFContext &Ctx,
431 const DWARFUnit *U, raw_ostream *OS) {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000432 const uint32_t DebugLineOffset = *OffsetPtr;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000433
Alexey Samsonov110d5952014-04-30 00:09:19 +0000434 clear();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000435
Paul Robinsonbf750c82018-01-29 20:57:43 +0000436 if (!Prologue.parse(DebugLineData, OffsetPtr, Ctx, U)) {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000437 // Restore our offset and return false to indicate failure!
Paul Robinson9d4eb692017-05-01 23:27:55 +0000438 *OffsetPtr = DebugLineOffset;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000439 return false;
440 }
441
Paul Robinson0a227092018-02-05 20:43:15 +0000442 if (OS) {
443 // The presence of OS signals verbose dumping.
444 DIDumpOptions DumpOptions;
445 DumpOptions.Verbose = true;
446 Prologue.dump(*OS, DumpOptions);
447 }
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000448
Paul Robinson9d4eb692017-05-01 23:27:55 +0000449 const uint32_t EndOffset =
450 DebugLineOffset + Prologue.TotalLength + Prologue.sizeofTotalLength();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000451
Paul Robinson511b54c2017-11-22 15:48:30 +0000452 // See if we should tell the data extractor the address size.
453 if (DebugLineData.getAddressSize() == 0)
454 DebugLineData.setAddressSize(Prologue.getAddressSize());
455 else
456 assert(Prologue.getAddressSize() == 0 ||
457 Prologue.getAddressSize() == DebugLineData.getAddressSize());
458
Alexey Samsonov110d5952014-04-30 00:09:19 +0000459 ParsingState State(this);
Benjamin Kramer112ec172011-09-15 21:59:13 +0000460
Paul Robinson9d4eb692017-05-01 23:27:55 +0000461 while (*OffsetPtr < EndOffset) {
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000462 if (OS)
463 *OS << format("0x%08.08" PRIx32 ": ", *OffsetPtr);
464
Paul Robinson9d4eb692017-05-01 23:27:55 +0000465 uint8_t Opcode = DebugLineData.getU8(OffsetPtr);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000466
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000467 if (OS)
468 *OS << format("%02.02" PRIx8 " ", Opcode);
469
Paul Robinson9d4eb692017-05-01 23:27:55 +0000470 if (Opcode == 0) {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000471 // Extended Opcodes always start with a zero opcode followed by
472 // a uleb128 length so you can skip ones you don't know about
Paul Robinson9d4eb692017-05-01 23:27:55 +0000473 uint64_t Len = DebugLineData.getULEB128(OffsetPtr);
Paul Robinsone0833342017-11-22 15:14:49 +0000474 uint32_t ExtOffset = *OffsetPtr;
475
476 // Tolerate zero-length; assume length is correct and soldier on.
477 if (Len == 0) {
478 if (OS)
479 *OS << "Badly formed extended line op (length 0)\n";
480 continue;
481 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000482
Paul Robinson9d4eb692017-05-01 23:27:55 +0000483 uint8_t SubOpcode = DebugLineData.getU8(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000484 if (OS)
485 *OS << LNExtendedString(SubOpcode);
Paul Robinson9d4eb692017-05-01 23:27:55 +0000486 switch (SubOpcode) {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000487 case DW_LNE_end_sequence:
488 // Set the end_sequence register of the state machine to true and
489 // append a row to the matrix using the current values of the
490 // state-machine registers. Then reset the registers to the initial
491 // values specified above. Every statement program sequence must end
492 // with a DW_LNE_end_sequence instruction which creates a row whose
493 // address is that of the byte after the last target machine instruction
494 // of the sequence.
Alexey Samsonov110d5952014-04-30 00:09:19 +0000495 State.Row.EndSequence = true;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000496 State.appendRowToMatrix(*OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000497 if (OS) {
498 *OS << "\n";
499 OS->indent(12);
500 State.Row.dump(*OS);
501 }
Alexey Samsonov110d5952014-04-30 00:09:19 +0000502 State.resetRowAndSequence();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000503 break;
504
505 case DW_LNE_set_address:
506 // Takes a single relocatable address as an operand. The size of the
507 // operand is the size appropriate to hold an address on the target
508 // machine. Set the address register to the value given by the
509 // relocatable address. All of the other statement program opcodes
510 // that affect the address register add a delta to it. This instruction
511 // stores a relocatable value into it instead.
Paul Robinson511b54c2017-11-22 15:48:30 +0000512 //
513 // Make sure the extractor knows the address size. If not, infer it
514 // from the size of the operand.
515 if (DebugLineData.getAddressSize() == 0)
516 DebugLineData.setAddressSize(Len - 1);
517 else
518 assert(DebugLineData.getAddressSize() == Len - 1);
Paul Robinson17536b92017-06-29 16:52:08 +0000519 State.Row.Address = DebugLineData.getRelocatedAddress(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000520 if (OS)
521 *OS << format(" (0x%16.16" PRIx64 ")", State.Row.Address);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000522 break;
523
524 case DW_LNE_define_file:
525 // Takes 4 arguments. The first is a null terminated string containing
526 // a source file name. The second is an unsigned LEB128 number
527 // representing the directory index of the directory in which the file
528 // was found. The third is an unsigned LEB128 number representing the
529 // time of last modification of the file. The fourth is an unsigned
530 // LEB128 number representing the length in bytes of the file. The time
531 // and length fields may contain LEB128(0) if the information is not
532 // available.
533 //
534 // The directory index represents an entry in the include_directories
535 // section of the statement program prologue. The index is LEB128(0)
536 // if the file was found in the current directory of the compilation,
537 // LEB128(1) if it was found in the first directory in the
538 // include_directories section, and so on. The directory index is
539 // ignored for file names that represent full path names.
540 //
541 // The files are numbered, starting at 1, in the order in which they
542 // appear; the names in the prologue come before names defined by
543 // the DW_LNE_define_file instruction. These numbers are used in the
544 // the file register of the state machine.
545 {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000546 FileNameEntry FileEntry;
Paul Robinson0a227092018-02-05 20:43:15 +0000547 const char *Name = DebugLineData.getCStr(OffsetPtr);
548 FileEntry.Name.setForm(dwarf::DW_FORM_string);
549 FileEntry.Name.setPValue(Name);
Paul Robinson9d4eb692017-05-01 23:27:55 +0000550 FileEntry.DirIdx = DebugLineData.getULEB128(OffsetPtr);
551 FileEntry.ModTime = DebugLineData.getULEB128(OffsetPtr);
552 FileEntry.Length = DebugLineData.getULEB128(OffsetPtr);
553 Prologue.FileNames.push_back(FileEntry);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000554 if (OS)
Paul Robinson0a227092018-02-05 20:43:15 +0000555 *OS << " (" << Name << ", dir=" << FileEntry.DirIdx << ", mod_time="
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000556 << format("(0x%16.16" PRIx64 ")", FileEntry.ModTime)
557 << ", length=" << FileEntry.Length << ")";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000558 }
559 break;
560
Diego Novillo5b5cf502014-02-14 19:27:53 +0000561 case DW_LNE_set_discriminator:
Paul Robinson9d4eb692017-05-01 23:27:55 +0000562 State.Row.Discriminator = DebugLineData.getULEB128(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000563 if (OS)
564 *OS << " (" << State.Row.Discriminator << ")";
Diego Novillo5b5cf502014-02-14 19:27:53 +0000565 break;
566
Benjamin Kramer5acab502011-09-15 02:12:05 +0000567 default:
Paul Robinsone0833342017-11-22 15:14:49 +0000568 if (OS)
569 *OS << format("Unrecognized extended op 0x%02.02" PRIx8, SubOpcode)
570 << format(" length %" PRIx64, Len);
571 // Len doesn't include the zero opcode byte or the length itself, but
572 // it does include the sub_opcode, so we have to adjust for that.
573 (*OffsetPtr) += Len - 1;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000574 break;
575 }
Paul Robinsone0833342017-11-22 15:14:49 +0000576 // Make sure the stated and parsed lengths are the same.
577 // Otherwise we have an unparseable line-number program.
578 if (*OffsetPtr - ExtOffset != Len) {
579 fprintf(stderr, "Unexpected line op length at offset 0x%8.8" PRIx32
580 " expected 0x%2.2" PRIx64 " found 0x%2.2" PRIx32 "\n",
581 ExtOffset, Len, *OffsetPtr - ExtOffset);
582 // Skip the rest of the line-number program.
583 *OffsetPtr = EndOffset;
584 return false;
585 }
Paul Robinson9d4eb692017-05-01 23:27:55 +0000586 } else if (Opcode < Prologue.OpcodeBase) {
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000587 if (OS)
588 *OS << LNStandardString(Opcode);
Paul Robinson9d4eb692017-05-01 23:27:55 +0000589 switch (Opcode) {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000590 // Standard Opcodes
591 case DW_LNS_copy:
592 // Takes no arguments. Append a row to the matrix using the
593 // current values of the state-machine registers. Then set
594 // the basic_block register to false.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000595 State.appendRowToMatrix(*OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000596 if (OS) {
597 *OS << "\n";
598 OS->indent(12);
599 State.Row.dump(*OS);
600 *OS << "\n";
601 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000602 break;
603
604 case DW_LNS_advance_pc:
605 // Takes a single unsigned LEB128 operand, multiplies it by the
606 // min_inst_length field of the prologue, and adds the
607 // result to the address register of the state machine.
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000608 {
609 uint64_t AddrOffset =
610 DebugLineData.getULEB128(OffsetPtr) * Prologue.MinInstLength;
611 State.Row.Address += AddrOffset;
612 if (OS)
613 *OS << " (" << AddrOffset << ")";
614 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000615 break;
616
617 case DW_LNS_advance_line:
618 // Takes a single signed LEB128 operand and adds that value to
619 // the line register of the state machine.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000620 State.Row.Line += DebugLineData.getSLEB128(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000621 if (OS)
622 *OS << " (" << State.Row.Line << ")";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000623 break;
624
625 case DW_LNS_set_file:
626 // Takes a single unsigned LEB128 operand and stores it in the file
627 // register of the state machine.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000628 State.Row.File = DebugLineData.getULEB128(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000629 if (OS)
630 *OS << " (" << State.Row.File << ")";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000631 break;
632
633 case DW_LNS_set_column:
634 // Takes a single unsigned LEB128 operand and stores it in the
635 // column register of the state machine.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000636 State.Row.Column = DebugLineData.getULEB128(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000637 if (OS)
638 *OS << " (" << State.Row.Column << ")";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000639 break;
640
641 case DW_LNS_negate_stmt:
642 // Takes no arguments. Set the is_stmt register of the state
643 // machine to the logical negation of its current value.
Alexey Samsonov110d5952014-04-30 00:09:19 +0000644 State.Row.IsStmt = !State.Row.IsStmt;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000645 break;
646
647 case DW_LNS_set_basic_block:
648 // Takes no arguments. Set the basic_block register of the
649 // state machine to true
Alexey Samsonov110d5952014-04-30 00:09:19 +0000650 State.Row.BasicBlock = true;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000651 break;
652
653 case DW_LNS_const_add_pc:
654 // Takes no arguments. Add to the address register of the state
655 // machine the address increment value corresponding to special
656 // opcode 255. The motivation for DW_LNS_const_add_pc is this:
657 // when the statement program needs to advance the address by a
658 // small amount, it can use a single special opcode, which occupies
659 // a single byte. When it needs to advance the address by up to
660 // twice the range of the last special opcode, it can use
661 // DW_LNS_const_add_pc followed by a special opcode, for a total
662 // of two bytes. Only if it needs to advance the address by more
663 // than twice that range will it need to use both DW_LNS_advance_pc
664 // and a special opcode, requiring three or more bytes.
665 {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000666 uint8_t AdjustOpcode = 255 - Prologue.OpcodeBase;
667 uint64_t AddrOffset =
668 (AdjustOpcode / Prologue.LineRange) * Prologue.MinInstLength;
669 State.Row.Address += AddrOffset;
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000670 if (OS)
671 *OS
672 << format(" (0x%16.16" PRIx64 ")", AddrOffset);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000673 }
674 break;
675
676 case DW_LNS_fixed_advance_pc:
677 // Takes a single uhalf operand. Add to the address register of
678 // the state machine the value of the (unencoded) operand. This
679 // is the only extended opcode that takes an argument that is not
680 // a variable length number. The motivation for DW_LNS_fixed_advance_pc
681 // is this: existing assemblers cannot emit DW_LNS_advance_pc or
682 // special opcodes because they cannot encode LEB128 numbers or
683 // judge when the computation of a special opcode overflows and
684 // requires the use of DW_LNS_advance_pc. Such assemblers, however,
685 // can use DW_LNS_fixed_advance_pc instead, sacrificing compression.
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000686 {
687 uint16_t PCOffset = DebugLineData.getU16(OffsetPtr);
688 State.Row.Address += PCOffset;
689 if (OS)
690 *OS
691 << format(" (0x%16.16" PRIx64 ")", PCOffset);
692 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000693 break;
694
695 case DW_LNS_set_prologue_end:
696 // Takes no arguments. Set the prologue_end register of the
697 // state machine to true
Alexey Samsonov110d5952014-04-30 00:09:19 +0000698 State.Row.PrologueEnd = true;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000699 break;
700
701 case DW_LNS_set_epilogue_begin:
702 // Takes no arguments. Set the basic_block register of the
703 // state machine to true
Alexey Samsonov110d5952014-04-30 00:09:19 +0000704 State.Row.EpilogueBegin = true;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000705 break;
706
707 case DW_LNS_set_isa:
708 // Takes a single unsigned LEB128 operand and stores it in the
709 // column register of the state machine.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000710 State.Row.Isa = DebugLineData.getULEB128(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000711 if (OS)
712 *OS << " (" << State.Row.Isa << ")";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000713 break;
714
715 default:
716 // Handle any unknown standard opcodes here. We know the lengths
717 // of such opcodes because they are specified in the prologue
718 // as a multiple of LEB128 operands for each opcode.
719 {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000720 assert(Opcode - 1U < Prologue.StandardOpcodeLengths.size());
721 uint8_t OpcodeLength = Prologue.StandardOpcodeLengths[Opcode - 1];
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000722 for (uint8_t I = 0; I < OpcodeLength; ++I) {
723 uint64_t Value = DebugLineData.getULEB128(OffsetPtr);
724 if (OS)
725 *OS << format("Skipping ULEB128 value: 0x%16.16" PRIx64 ")\n",
726 Value);
727 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000728 }
729 break;
730 }
731 } else {
732 // Special Opcodes
733
734 // A special opcode value is chosen based on the amount that needs
735 // to be added to the line and address registers. The maximum line
736 // increment for a special opcode is the value of the line_base
737 // field in the header, plus the value of the line_range field,
738 // minus 1 (line base + line range - 1). If the desired line
739 // increment is greater than the maximum line increment, a standard
NAKAMURA Takumif9959852011-10-08 11:22:47 +0000740 // opcode must be used instead of a special opcode. The "address
741 // advance" is calculated by dividing the desired address increment
Benjamin Kramer5acab502011-09-15 02:12:05 +0000742 // by the minimum_instruction_length field from the header. The
743 // special opcode is then calculated using the following formula:
744 //
745 // opcode = (desired line increment - line_base) +
746 // (line_range * address advance) + opcode_base
747 //
748 // If the resulting opcode is greater than 255, a standard opcode
749 // must be used instead.
750 //
751 // To decode a special opcode, subtract the opcode_base from the
752 // opcode itself to give the adjusted opcode. The amount to
753 // increment the address register is the result of the adjusted
754 // opcode divided by the line_range multiplied by the
755 // minimum_instruction_length field from the header. That is:
756 //
757 // address increment = (adjusted opcode / line_range) *
758 // minimum_instruction_length
759 //
760 // The amount to increment the line register is the line_base plus
761 // the result of the adjusted opcode modulo the line_range. That is:
762 //
763 // line increment = line_base + (adjusted opcode % line_range)
764
Paul Robinson9d4eb692017-05-01 23:27:55 +0000765 uint8_t AdjustOpcode = Opcode - Prologue.OpcodeBase;
766 uint64_t AddrOffset =
767 (AdjustOpcode / Prologue.LineRange) * Prologue.MinInstLength;
768 int32_t LineOffset =
769 Prologue.LineBase + (AdjustOpcode % Prologue.LineRange);
770 State.Row.Line += LineOffset;
771 State.Row.Address += AddrOffset;
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000772
773 if (OS) {
774 *OS << "address += " << ((uint32_t)AdjustOpcode)
775 << ", line += " << LineOffset << "\n";
776 OS->indent(12);
777 State.Row.dump(*OS);
778 }
779
Paul Robinson9d4eb692017-05-01 23:27:55 +0000780 State.appendRowToMatrix(*OffsetPtr);
Dehao Chen1b54fce2016-04-28 22:09:37 +0000781 // Reset discriminator to 0.
782 State.Row.Discriminator = 0;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000783 }
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000784 if(OS)
785 *OS << "\n";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000786 }
787
Alexey Samsonov110d5952014-04-30 00:09:19 +0000788 if (!State.Sequence.Empty) {
789 fprintf(stderr, "warning: last sequence in debug line table is not"
790 "terminated!\n");
791 }
792
793 // Sort all sequences so that address lookup will work faster.
794 if (!Sequences.empty()) {
795 std::sort(Sequences.begin(), Sequences.end(), Sequence::orderByLowPC);
796 // Note: actually, instruction address ranges of sequences should not
797 // overlap (in shared objects and executables). If they do, the address
798 // lookup would still work, though, but result would be ambiguous.
799 // We don't report warning in this case. For example,
800 // sometimes .so compiled from multiple object files contains a few
801 // rudimentary sequences for address ranges [0x0, 0xsomething).
802 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000803
Paul Robinson9d4eb692017-05-01 23:27:55 +0000804 return EndOffset;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000805}
806
Keno Fischerc2c60182015-05-31 23:37:04 +0000807uint32_t
Paul Robinson9d4eb692017-05-01 23:27:55 +0000808DWARFDebugLine::LineTable::findRowInSeq(const DWARFDebugLine::Sequence &Seq,
809 uint64_t Address) const {
810 if (!Seq.containsPC(Address))
Keno Fischerc2c60182015-05-31 23:37:04 +0000811 return UnknownRowIndex;
812 // Search for instruction address in the rows describing the sequence.
813 // Rows are stored in a vector, so we may use arithmetical operations with
814 // iterators.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000815 DWARFDebugLine::Row Row;
816 Row.Address = Address;
817 RowIter FirstRow = Rows.begin() + Seq.FirstRowIndex;
818 RowIter LastRow = Rows.begin() + Seq.LastRowIndex;
819 LineTable::RowIter RowPos = std::lower_bound(
820 FirstRow, LastRow, Row, DWARFDebugLine::Row::orderByAddress);
821 if (RowPos == LastRow) {
822 return Seq.LastRowIndex - 1;
Keno Fischerc2c60182015-05-31 23:37:04 +0000823 }
Paul Robinson9d4eb692017-05-01 23:27:55 +0000824 uint32_t Index = Seq.FirstRowIndex + (RowPos - FirstRow);
825 if (RowPos->Address > Address) {
826 if (RowPos == FirstRow)
Keno Fischerc2c60182015-05-31 23:37:04 +0000827 return UnknownRowIndex;
828 else
Paul Robinson9d4eb692017-05-01 23:27:55 +0000829 Index--;
Keno Fischerc2c60182015-05-31 23:37:04 +0000830 }
Paul Robinson9d4eb692017-05-01 23:27:55 +0000831 return Index;
Keno Fischerc2c60182015-05-31 23:37:04 +0000832}
833
Paul Robinson9d4eb692017-05-01 23:27:55 +0000834uint32_t DWARFDebugLine::LineTable::lookupAddress(uint64_t Address) const {
Alexey Samsonov947228c2012-08-07 11:46:57 +0000835 if (Sequences.empty())
Keno Fischerc2c60182015-05-31 23:37:04 +0000836 return UnknownRowIndex;
Alexey Samsonov947228c2012-08-07 11:46:57 +0000837 // First, find an instruction sequence containing the given address.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000838 DWARFDebugLine::Sequence Sequence;
839 Sequence.LowPC = Address;
840 SequenceIter FirstSeq = Sequences.begin();
841 SequenceIter LastSeq = Sequences.end();
842 SequenceIter SeqPos = std::lower_bound(
843 FirstSeq, LastSeq, Sequence, DWARFDebugLine::Sequence::orderByLowPC);
844 DWARFDebugLine::Sequence FoundSeq;
845 if (SeqPos == LastSeq) {
846 FoundSeq = Sequences.back();
847 } else if (SeqPos->LowPC == Address) {
848 FoundSeq = *SeqPos;
Alexey Samsonov947228c2012-08-07 11:46:57 +0000849 } else {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000850 if (SeqPos == FirstSeq)
Keno Fischerc2c60182015-05-31 23:37:04 +0000851 return UnknownRowIndex;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000852 FoundSeq = *(SeqPos - 1);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000853 }
Paul Robinson9d4eb692017-05-01 23:27:55 +0000854 return findRowInSeq(FoundSeq, Address);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000855}
Alexey Samsonov45be7932012-08-30 07:49:50 +0000856
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000857bool DWARFDebugLine::LineTable::lookupAddressRange(
Paul Robinson9d4eb692017-05-01 23:27:55 +0000858 uint64_t Address, uint64_t Size, std::vector<uint32_t> &Result) const {
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000859 if (Sequences.empty())
860 return false;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000861 uint64_t EndAddr = Address + Size;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000862 // First, find an instruction sequence containing the given address.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000863 DWARFDebugLine::Sequence Sequence;
864 Sequence.LowPC = Address;
865 SequenceIter FirstSeq = Sequences.begin();
866 SequenceIter LastSeq = Sequences.end();
867 SequenceIter SeqPos = std::lower_bound(
868 FirstSeq, LastSeq, Sequence, DWARFDebugLine::Sequence::orderByLowPC);
869 if (SeqPos == LastSeq || SeqPos->LowPC != Address) {
870 if (SeqPos == FirstSeq)
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000871 return false;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000872 SeqPos--;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000873 }
Paul Robinson9d4eb692017-05-01 23:27:55 +0000874 if (!SeqPos->containsPC(Address))
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000875 return false;
876
Paul Robinson9d4eb692017-05-01 23:27:55 +0000877 SequenceIter StartPos = SeqPos;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000878
879 // Add the rows from the first sequence to the vector, starting with the
880 // index we just calculated
881
Paul Robinson9d4eb692017-05-01 23:27:55 +0000882 while (SeqPos != LastSeq && SeqPos->LowPC < EndAddr) {
883 const DWARFDebugLine::Sequence &CurSeq = *SeqPos;
Keno Fischerc2c60182015-05-31 23:37:04 +0000884 // For the first sequence, we need to find which row in the sequence is the
885 // first in our range.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000886 uint32_t FirstRowIndex = CurSeq.FirstRowIndex;
887 if (SeqPos == StartPos)
888 FirstRowIndex = findRowInSeq(CurSeq, Address);
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000889
Keno Fischerc2c60182015-05-31 23:37:04 +0000890 // Figure out the last row in the range.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000891 uint32_t LastRowIndex = findRowInSeq(CurSeq, EndAddr - 1);
892 if (LastRowIndex == UnknownRowIndex)
893 LastRowIndex = CurSeq.LastRowIndex - 1;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000894
Paul Robinson9d4eb692017-05-01 23:27:55 +0000895 assert(FirstRowIndex != UnknownRowIndex);
896 assert(LastRowIndex != UnknownRowIndex);
Keno Fischerc2c60182015-05-31 23:37:04 +0000897
Paul Robinson9d4eb692017-05-01 23:27:55 +0000898 for (uint32_t I = FirstRowIndex; I <= LastRowIndex; ++I) {
899 Result.push_back(I);
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000900 }
901
Paul Robinson9d4eb692017-05-01 23:27:55 +0000902 ++SeqPos;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000903 }
NAKAMURA Takumi4b86cdb2013-01-26 01:45:06 +0000904
905 return true;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000906}
907
Paul Robinson9d4eb692017-05-01 23:27:55 +0000908bool DWARFDebugLine::LineTable::hasFileAtIndex(uint64_t FileIndex) const {
Pete Cooperb2ba7762016-07-22 01:41:32 +0000909 return FileIndex != 0 && FileIndex <= Prologue.FileNames.size();
910}
911
Paul Robinson9d4eb692017-05-01 23:27:55 +0000912bool DWARFDebugLine::LineTable::getFileNameByIndex(uint64_t FileIndex,
913 const char *CompDir,
914 FileLineInfoKind Kind,
915 std::string &Result) const {
Pete Cooperb2ba7762016-07-22 01:41:32 +0000916 if (Kind == FileLineInfoKind::None || !hasFileAtIndex(FileIndex))
Alexey Samsonov45be7932012-08-30 07:49:50 +0000917 return false;
918 const FileNameEntry &Entry = Prologue.FileNames[FileIndex - 1];
Paul Robinson0a227092018-02-05 20:43:15 +0000919 StringRef FileName = Entry.Name.getAsCString().getValue();
Alexey Samsonovdce67342014-05-15 21:24:32 +0000920 if (Kind != FileLineInfoKind::AbsoluteFilePath ||
Alexey Samsonov45be7932012-08-30 07:49:50 +0000921 sys::path::is_absolute(FileName)) {
922 Result = FileName;
923 return true;
924 }
Frederic Riss101b5e22014-09-19 15:11:51 +0000925
Alexey Samsonov45be7932012-08-30 07:49:50 +0000926 SmallString<16> FilePath;
927 uint64_t IncludeDirIndex = Entry.DirIdx;
Paul Robinsonba1c9152017-05-02 17:37:32 +0000928 StringRef IncludeDir;
Alexey Samsonov45be7932012-08-30 07:49:50 +0000929 // Be defensive about the contents of Entry.
930 if (IncludeDirIndex > 0 &&
Frederic Riss101b5e22014-09-19 15:11:51 +0000931 IncludeDirIndex <= Prologue.IncludeDirectories.size())
Paul Robinson0a227092018-02-05 20:43:15 +0000932 IncludeDir = Prologue.IncludeDirectories[IncludeDirIndex - 1]
933 .getAsCString()
934 .getValue();
Frederic Riss101b5e22014-09-19 15:11:51 +0000935
936 // We may still need to append compilation directory of compile unit.
937 // We know that FileName is not absolute, the only way to have an
938 // absolute path at this point would be if IncludeDir is absolute.
939 if (CompDir && Kind == FileLineInfoKind::AbsoluteFilePath &&
940 sys::path::is_relative(IncludeDir))
941 sys::path::append(FilePath, CompDir);
942
943 // sys::path::append skips empty strings.
944 sys::path::append(FilePath, IncludeDir, FileName);
Alexey Samsonov45be7932012-08-30 07:49:50 +0000945 Result = FilePath.str();
946 return true;
947}
Frederic Riss101b5e22014-09-19 15:11:51 +0000948
Dehao Chen1b54fce2016-04-28 22:09:37 +0000949bool DWARFDebugLine::LineTable::getFileLineInfoForAddress(
950 uint64_t Address, const char *CompDir, FileLineInfoKind Kind,
951 DILineInfo &Result) const {
Frederic Riss101b5e22014-09-19 15:11:51 +0000952 // Get the index of row we're looking for in the line table.
953 uint32_t RowIndex = lookupAddress(Address);
954 if (RowIndex == -1U)
955 return false;
956 // Take file number and line/column from the row.
957 const auto &Row = Rows[RowIndex];
958 if (!getFileNameByIndex(Row.File, CompDir, Kind, Result.FileName))
959 return false;
960 Result.Line = Row.Line;
961 Result.Column = Row.Column;
Eric Christopherba1024c2016-12-14 18:29:39 +0000962 Result.Discriminator = Row.Discriminator;
Frederic Riss101b5e22014-09-19 15:11:51 +0000963 return true;
964}