blob: 8986af26c03cf94a134988ca4aa520dcc2437020 [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 Robinsonceafcd42018-02-08 23:08:02 +000096 // DWARF v5 starts file indexes at 0.
97 uint32_t FileBase = getVersion() >= 5 ? 0 : 1;
Paul Robinson9d4eb692017-05-01 23:27:55 +000098 for (uint32_t I = 0; I != FileNames.size(); ++I) {
99 const FileNameEntry &FileEntry = FileNames[I];
Paul Robinsonceafcd42018-02-08 23:08:02 +0000100 OS << format("file_names[%3u] %4" PRIu64 " ", I + FileBase,
101 FileEntry.DirIdx);
Paul Robinsona06f8dc2017-12-18 19:08:35 +0000102 if (HasMD5)
103 OS << FileEntry.Checksum.digest();
104 else
105 OS << format("0x%8.8" PRIx64 " 0x%8.8" PRIx64, FileEntry.ModTime,
106 FileEntry.Length);
Paul Robinson0a227092018-02-05 20:43:15 +0000107 OS << ' ';
108 FileEntry.Name.dump(OS, DumpOptions);
109 OS << '\n';
Benjamin Kramer5acab502011-09-15 02:12:05 +0000110 }
111 }
112}
113
Paul Robinson2bc38732017-05-02 21:40:47 +0000114// Parse v2-v4 directory and file tables.
115static void
Paul Robinson17536b92017-06-29 16:52:08 +0000116parseV2DirFileTables(const DWARFDataExtractor &DebugLineData,
117 uint32_t *OffsetPtr, uint64_t EndPrologueOffset,
Paul Robinson0a227092018-02-05 20:43:15 +0000118 std::vector<DWARFFormValue> &IncludeDirectories,
Paul Robinson2bc38732017-05-02 21:40:47 +0000119 std::vector<DWARFDebugLine::FileNameEntry> &FileNames) {
120 while (*OffsetPtr < EndPrologueOffset) {
121 StringRef S = DebugLineData.getCStrRef(OffsetPtr);
122 if (S.empty())
123 break;
Paul Robinson0a227092018-02-05 20:43:15 +0000124 DWARFFormValue Dir(dwarf::DW_FORM_string);
125 Dir.setPValue(S.data());
126 IncludeDirectories.push_back(Dir);
Paul Robinson2bc38732017-05-02 21:40:47 +0000127 }
128
129 while (*OffsetPtr < EndPrologueOffset) {
130 StringRef Name = DebugLineData.getCStrRef(OffsetPtr);
131 if (Name.empty())
132 break;
133 DWARFDebugLine::FileNameEntry FileEntry;
Paul Robinson0a227092018-02-05 20:43:15 +0000134 FileEntry.Name.setForm(dwarf::DW_FORM_string);
135 FileEntry.Name.setPValue(Name.data());
Paul Robinson2bc38732017-05-02 21:40:47 +0000136 FileEntry.DirIdx = DebugLineData.getULEB128(OffsetPtr);
137 FileEntry.ModTime = DebugLineData.getULEB128(OffsetPtr);
138 FileEntry.Length = DebugLineData.getULEB128(OffsetPtr);
139 FileNames.push_back(FileEntry);
140 }
141}
142
143// Parse v5 directory/file entry content descriptions.
144// Returns the descriptors, or an empty vector if we did not find a path or
145// ran off the end of the prologue.
146static ContentDescriptors
Paul Robinson17536b92017-06-29 16:52:08 +0000147parseV5EntryFormat(const DWARFDataExtractor &DebugLineData, uint32_t *OffsetPtr,
Paul Robinsona06f8dc2017-12-18 19:08:35 +0000148 uint64_t EndPrologueOffset, bool *HasMD5) {
Paul Robinson2bc38732017-05-02 21:40:47 +0000149 ContentDescriptors Descriptors;
150 int FormatCount = DebugLineData.getU8(OffsetPtr);
151 bool HasPath = false;
152 for (int I = 0; I != FormatCount; ++I) {
153 if (*OffsetPtr >= EndPrologueOffset)
154 return ContentDescriptors();
155 ContentDescriptor Descriptor;
156 Descriptor.Type =
157 dwarf::LineNumberEntryFormat(DebugLineData.getULEB128(OffsetPtr));
158 Descriptor.Form = dwarf::Form(DebugLineData.getULEB128(OffsetPtr));
159 if (Descriptor.Type == dwarf::DW_LNCT_path)
160 HasPath = true;
Paul Robinsona06f8dc2017-12-18 19:08:35 +0000161 else if (Descriptor.Type == dwarf::DW_LNCT_MD5 && HasMD5)
162 *HasMD5 = true;
Paul Robinson2bc38732017-05-02 21:40:47 +0000163 Descriptors.push_back(Descriptor);
164 }
165 return HasPath ? Descriptors : ContentDescriptors();
166}
167
168static bool
Paul Robinson17536b92017-06-29 16:52:08 +0000169parseV5DirFileTables(const DWARFDataExtractor &DebugLineData,
170 uint32_t *OffsetPtr, uint64_t EndPrologueOffset,
Paul Robinsonbf750c82018-01-29 20:57:43 +0000171 const DWARFFormParams &FormParams, const DWARFContext &Ctx,
172 const DWARFUnit *U, bool &HasMD5,
Paul Robinson0a227092018-02-05 20:43:15 +0000173 std::vector<DWARFFormValue> &IncludeDirectories,
Paul Robinson2bc38732017-05-02 21:40:47 +0000174 std::vector<DWARFDebugLine::FileNameEntry> &FileNames) {
175 // Get the directory entry description.
176 ContentDescriptors DirDescriptors =
Paul Robinsona06f8dc2017-12-18 19:08:35 +0000177 parseV5EntryFormat(DebugLineData, OffsetPtr, EndPrologueOffset, nullptr);
Paul Robinson2bc38732017-05-02 21:40:47 +0000178 if (DirDescriptors.empty())
179 return false;
180
181 // Get the directory entries, according to the format described above.
182 int DirEntryCount = DebugLineData.getU8(OffsetPtr);
183 for (int I = 0; I != DirEntryCount; ++I) {
184 if (*OffsetPtr >= EndPrologueOffset)
185 return false;
186 for (auto Descriptor : DirDescriptors) {
187 DWARFFormValue Value(Descriptor.Form);
188 switch (Descriptor.Type) {
189 case DW_LNCT_path:
Paul Robinsonbf750c82018-01-29 20:57:43 +0000190 if (!Value.extractValue(DebugLineData, OffsetPtr, FormParams, &Ctx, U))
Paul Robinson2bc38732017-05-02 21:40:47 +0000191 return false;
Paul Robinson0a227092018-02-05 20:43:15 +0000192 IncludeDirectories.push_back(Value);
Paul Robinson2bc38732017-05-02 21:40:47 +0000193 break;
194 default:
Paul Robinson75c068c2017-06-26 18:43:01 +0000195 if (!Value.skipValue(DebugLineData, OffsetPtr, FormParams))
Paul Robinson2bc38732017-05-02 21:40:47 +0000196 return false;
197 }
198 }
199 }
200
201 // Get the file entry description.
202 ContentDescriptors FileDescriptors =
Paul Robinsona06f8dc2017-12-18 19:08:35 +0000203 parseV5EntryFormat(DebugLineData, OffsetPtr, EndPrologueOffset, &HasMD5);
Paul Robinson2bc38732017-05-02 21:40:47 +0000204 if (FileDescriptors.empty())
205 return false;
206
207 // Get the file entries, according to the format described above.
208 int FileEntryCount = DebugLineData.getU8(OffsetPtr);
209 for (int I = 0; I != FileEntryCount; ++I) {
210 if (*OffsetPtr >= EndPrologueOffset)
211 return false;
212 DWARFDebugLine::FileNameEntry FileEntry;
213 for (auto Descriptor : FileDescriptors) {
214 DWARFFormValue Value(Descriptor.Form);
Paul Robinsonbf750c82018-01-29 20:57:43 +0000215 if (!Value.extractValue(DebugLineData, OffsetPtr, FormParams, &Ctx, U))
Paul Robinson2bc38732017-05-02 21:40:47 +0000216 return false;
217 switch (Descriptor.Type) {
218 case DW_LNCT_path:
Paul Robinson0a227092018-02-05 20:43:15 +0000219 FileEntry.Name = Value;
Paul Robinson2bc38732017-05-02 21:40:47 +0000220 break;
221 case DW_LNCT_directory_index:
222 FileEntry.DirIdx = Value.getAsUnsignedConstant().getValue();
223 break;
224 case DW_LNCT_timestamp:
225 FileEntry.ModTime = Value.getAsUnsignedConstant().getValue();
226 break;
227 case DW_LNCT_size:
228 FileEntry.Length = Value.getAsUnsignedConstant().getValue();
229 break;
Paul Robinsona06f8dc2017-12-18 19:08:35 +0000230 case DW_LNCT_MD5:
231 assert(Value.getAsBlock().getValue().size() == 16);
232 std::uninitialized_copy_n(Value.getAsBlock().getValue().begin(), 16,
233 FileEntry.Checksum.Bytes.begin());
234 break;
Paul Robinson2bc38732017-05-02 21:40:47 +0000235 default:
236 break;
237 }
238 }
239 FileNames.push_back(FileEntry);
240 }
241 return true;
242}
243
Paul Robinson17536b92017-06-29 16:52:08 +0000244bool DWARFDebugLine::Prologue::parse(const DWARFDataExtractor &DebugLineData,
Paul Robinsonbf750c82018-01-29 20:57:43 +0000245 uint32_t *OffsetPtr,
246 const DWARFContext &Ctx,
247 const DWARFUnit *U) {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000248 const uint64_t PrologueOffset = *OffsetPtr;
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000249
250 clear();
Paul Robinson9d4eb692017-05-01 23:27:55 +0000251 TotalLength = DebugLineData.getU32(OffsetPtr);
Ed Maste6d0bee52015-05-28 15:38:17 +0000252 if (TotalLength == UINT32_MAX) {
Paul Robinson75c068c2017-06-26 18:43:01 +0000253 FormParams.Format = dwarf::DWARF64;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000254 TotalLength = DebugLineData.getU64(OffsetPtr);
Paul Robinson75c068c2017-06-26 18:43:01 +0000255 } else if (TotalLength >= 0xffffff00) {
Ed Maste6d0bee52015-05-28 15:38:17 +0000256 return false;
257 }
Paul Robinson75c068c2017-06-26 18:43:01 +0000258 FormParams.Version = DebugLineData.getU16(OffsetPtr);
259 if (getVersion() < 2)
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000260 return false;
261
Paul Robinson75c068c2017-06-26 18:43:01 +0000262 if (getVersion() >= 5) {
263 FormParams.AddrSize = DebugLineData.getU8(OffsetPtr);
Paul Robinson63811a42017-11-22 15:33:17 +0000264 assert((DebugLineData.getAddressSize() == 0 ||
265 DebugLineData.getAddressSize() == getAddressSize()) &&
Paul Robinson75c068c2017-06-26 18:43:01 +0000266 "Line table header and data extractor disagree");
Paul Robinson2bc38732017-05-02 21:40:47 +0000267 SegSelectorSize = DebugLineData.getU8(OffsetPtr);
268 }
269
Paul Robinson9d4eb692017-05-01 23:27:55 +0000270 PrologueLength = DebugLineData.getUnsigned(OffsetPtr, sizeofPrologueLength());
271 const uint64_t EndPrologueOffset = PrologueLength + *OffsetPtr;
272 MinInstLength = DebugLineData.getU8(OffsetPtr);
Paul Robinson75c068c2017-06-26 18:43:01 +0000273 if (getVersion() >= 4)
Paul Robinson9d4eb692017-05-01 23:27:55 +0000274 MaxOpsPerInst = DebugLineData.getU8(OffsetPtr);
275 DefaultIsStmt = DebugLineData.getU8(OffsetPtr);
276 LineBase = DebugLineData.getU8(OffsetPtr);
277 LineRange = DebugLineData.getU8(OffsetPtr);
278 OpcodeBase = DebugLineData.getU8(OffsetPtr);
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000279
280 StandardOpcodeLengths.reserve(OpcodeBase - 1);
Paul Robinson9d4eb692017-05-01 23:27:55 +0000281 for (uint32_t I = 1; I < OpcodeBase; ++I) {
282 uint8_t OpLen = DebugLineData.getU8(OffsetPtr);
283 StandardOpcodeLengths.push_back(OpLen);
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000284 }
285
Paul Robinson75c068c2017-06-26 18:43:01 +0000286 if (getVersion() >= 5) {
Paul Robinson2bc38732017-05-02 21:40:47 +0000287 if (!parseV5DirFileTables(DebugLineData, OffsetPtr, EndPrologueOffset,
Paul Robinsonbf750c82018-01-29 20:57:43 +0000288 FormParams, Ctx, U, HasMD5, IncludeDirectories,
Paul Robinsone5400f82017-11-07 19:57:12 +0000289 FileNames)) {
Paul Robinson2bc38732017-05-02 21:40:47 +0000290 fprintf(stderr,
291 "warning: parsing line table prologue at 0x%8.8" PRIx64
292 " found an invalid directory or file table description at"
293 " 0x%8.8" PRIx64 "\n", PrologueOffset, (uint64_t)*OffsetPtr);
294 return false;
295 }
296 } else
297 parseV2DirFileTables(DebugLineData, OffsetPtr, EndPrologueOffset,
298 IncludeDirectories, FileNames);
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000299
Paul Robinson9d4eb692017-05-01 23:27:55 +0000300 if (*OffsetPtr != EndPrologueOffset) {
301 fprintf(stderr,
302 "warning: parsing line table prologue at 0x%8.8" PRIx64
303 " should have ended at 0x%8.8" PRIx64
304 " but it ended at 0x%8.8" PRIx64 "\n",
305 PrologueOffset, EndPrologueOffset, (uint64_t)*OffsetPtr);
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000306 return false;
307 }
308 return true;
309}
310
Paul Robinson9d4eb692017-05-01 23:27:55 +0000311DWARFDebugLine::Row::Row(bool DefaultIsStmt) { reset(DefaultIsStmt); }
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000312
Benjamin Kramer5acab502011-09-15 02:12:05 +0000313void DWARFDebugLine::Row::postAppend() {
314 BasicBlock = false;
315 PrologueEnd = false;
316 EpilogueBegin = false;
317}
318
Paul Robinson9d4eb692017-05-01 23:27:55 +0000319void DWARFDebugLine::Row::reset(bool DefaultIsStmt) {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000320 Address = 0;
321 Line = 1;
322 Column = 0;
323 File = 1;
324 Isa = 0;
Diego Novillo5b5cf502014-02-14 19:27:53 +0000325 Discriminator = 0;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000326 IsStmt = DefaultIsStmt;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000327 BasicBlock = false;
328 EndSequence = false;
329 PrologueEnd = false;
330 EpilogueBegin = false;
331}
332
Greg Clayton67070462017-05-02 22:48:52 +0000333void DWARFDebugLine::Row::dumpTableHeader(raw_ostream &OS) {
334 OS << "Address Line Column File ISA Discriminator Flags\n"
335 << "------------------ ------ ------ ------ --- ------------- "
336 "-------------\n";
337}
338
Benjamin Kramer5acab502011-09-15 02:12:05 +0000339void DWARFDebugLine::Row::dump(raw_ostream &OS) const {
Benjamin Kramerf3da5292011-11-05 08:57:40 +0000340 OS << format("0x%16.16" PRIx64 " %6u %6u", Address, Line, Column)
Diego Novillo5b5cf502014-02-14 19:27:53 +0000341 << format(" %6u %3u %13u ", File, Isa, Discriminator)
Dehao Chen1b54fce2016-04-28 22:09:37 +0000342 << (IsStmt ? " is_stmt" : "") << (BasicBlock ? " basic_block" : "")
Benjamin Kramer5acab502011-09-15 02:12:05 +0000343 << (PrologueEnd ? " prologue_end" : "")
344 << (EpilogueBegin ? " epilogue_begin" : "")
Dehao Chen1b54fce2016-04-28 22:09:37 +0000345 << (EndSequence ? " end_sequence" : "") << '\n';
Benjamin Kramer5acab502011-09-15 02:12:05 +0000346}
347
Dehao Chen1b54fce2016-04-28 22:09:37 +0000348DWARFDebugLine::Sequence::Sequence() { reset(); }
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000349
350void DWARFDebugLine::Sequence::reset() {
351 LowPC = 0;
352 HighPC = 0;
353 FirstRowIndex = 0;
354 LastRowIndex = 0;
355 Empty = true;
356}
357
Dehao Chen1b54fce2016-04-28 22:09:37 +0000358DWARFDebugLine::LineTable::LineTable() { clear(); }
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000359
Paul Robinson0a227092018-02-05 20:43:15 +0000360void DWARFDebugLine::LineTable::dump(raw_ostream &OS,
361 DIDumpOptions DumpOptions) const {
362 Prologue.dump(OS, DumpOptions);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000363 OS << '\n';
364
365 if (!Rows.empty()) {
Greg Clayton67070462017-05-02 22:48:52 +0000366 Row::dumpTableHeader(OS);
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000367 for (const Row &R : Rows) {
368 R.dump(OS);
369 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000370 }
371}
372
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000373void DWARFDebugLine::LineTable::clear() {
374 Prologue.clear();
375 Rows.clear();
376 Sequences.clear();
377}
378
Alexey Samsonov110d5952014-04-30 00:09:19 +0000379DWARFDebugLine::ParsingState::ParsingState(struct LineTable *LT)
Eugene Zelenko2db0cfa2017-06-23 21:57:40 +0000380 : LineTable(LT) {
Alexey Samsonov110d5952014-04-30 00:09:19 +0000381 resetRowAndSequence();
382}
Nick Lewycky4d044922011-09-15 03:41:51 +0000383
Alexey Samsonov110d5952014-04-30 00:09:19 +0000384void DWARFDebugLine::ParsingState::resetRowAndSequence() {
385 Row.reset(LineTable->Prologue.DefaultIsStmt);
386 Sequence.reset();
387}
388
Paul Robinson9d4eb692017-05-01 23:27:55 +0000389void DWARFDebugLine::ParsingState::appendRowToMatrix(uint32_t Offset) {
Alexey Samsonov110d5952014-04-30 00:09:19 +0000390 if (Sequence.Empty) {
Alexey Samsonov947228c2012-08-07 11:46:57 +0000391 // Record the beginning of instruction sequence.
Alexey Samsonov110d5952014-04-30 00:09:19 +0000392 Sequence.Empty = false;
393 Sequence.LowPC = Row.Address;
394 Sequence.FirstRowIndex = RowNumber;
Alexey Samsonov947228c2012-08-07 11:46:57 +0000395 }
Alexey Samsonov110d5952014-04-30 00:09:19 +0000396 ++RowNumber;
397 LineTable->appendRow(Row);
398 if (Row.EndSequence) {
Alexey Samsonov947228c2012-08-07 11:46:57 +0000399 // Record the end of instruction sequence.
Alexey Samsonov110d5952014-04-30 00:09:19 +0000400 Sequence.HighPC = Row.Address;
401 Sequence.LastRowIndex = RowNumber;
402 if (Sequence.isValid())
403 LineTable->appendSequence(Sequence);
404 Sequence.reset();
Alexey Samsonov947228c2012-08-07 11:46:57 +0000405 }
Alexey Samsonov110d5952014-04-30 00:09:19 +0000406 Row.postAppend();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000407}
408
Benjamin Kramer5acab502011-09-15 02:12:05 +0000409const DWARFDebugLine::LineTable *
Paul Robinson9d4eb692017-05-01 23:27:55 +0000410DWARFDebugLine::getLineTable(uint32_t Offset) const {
411 LineTableConstIter Pos = LineTableMap.find(Offset);
412 if (Pos != LineTableMap.end())
413 return &Pos->second;
Craig Topper2617dcc2014-04-15 06:32:26 +0000414 return nullptr;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000415}
416
Benjamin Kramer679e1752011-09-15 20:43:18 +0000417const DWARFDebugLine::LineTable *
Paul Robinson511b54c2017-11-22 15:48:30 +0000418DWARFDebugLine::getOrParseLineTable(DWARFDataExtractor &DebugLineData,
Paul Robinsonbf750c82018-01-29 20:57:43 +0000419 uint32_t Offset, const DWARFContext &Ctx,
420 const DWARFUnit *U) {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000421 std::pair<LineTableIter, bool> Pos =
422 LineTableMap.insert(LineTableMapTy::value_type(Offset, LineTable()));
423 LineTable *LT = &Pos.first->second;
424 if (Pos.second) {
Paul Robinsonbf750c82018-01-29 20:57:43 +0000425 if (!LT->parse(DebugLineData, &Offset, Ctx, U))
Craig Topper2617dcc2014-04-15 06:32:26 +0000426 return nullptr;
Benjamin Kramer679e1752011-09-15 20:43:18 +0000427 }
Alexey Samsonov110d5952014-04-30 00:09:19 +0000428 return LT;
Benjamin Kramer679e1752011-09-15 20:43:18 +0000429}
430
Paul Robinson511b54c2017-11-22 15:48:30 +0000431bool DWARFDebugLine::LineTable::parse(DWARFDataExtractor &DebugLineData,
Paul Robinsonbf750c82018-01-29 20:57:43 +0000432 uint32_t *OffsetPtr,
433 const DWARFContext &Ctx,
434 const DWARFUnit *U, raw_ostream *OS) {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000435 const uint32_t DebugLineOffset = *OffsetPtr;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000436
Alexey Samsonov110d5952014-04-30 00:09:19 +0000437 clear();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000438
Paul Robinsonbf750c82018-01-29 20:57:43 +0000439 if (!Prologue.parse(DebugLineData, OffsetPtr, Ctx, U)) {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000440 // Restore our offset and return false to indicate failure!
Paul Robinson9d4eb692017-05-01 23:27:55 +0000441 *OffsetPtr = DebugLineOffset;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000442 return false;
443 }
444
Paul Robinson0a227092018-02-05 20:43:15 +0000445 if (OS) {
446 // The presence of OS signals verbose dumping.
447 DIDumpOptions DumpOptions;
448 DumpOptions.Verbose = true;
449 Prologue.dump(*OS, DumpOptions);
450 }
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000451
Paul Robinson9d4eb692017-05-01 23:27:55 +0000452 const uint32_t EndOffset =
453 DebugLineOffset + Prologue.TotalLength + Prologue.sizeofTotalLength();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000454
Paul Robinson511b54c2017-11-22 15:48:30 +0000455 // See if we should tell the data extractor the address size.
456 if (DebugLineData.getAddressSize() == 0)
457 DebugLineData.setAddressSize(Prologue.getAddressSize());
458 else
459 assert(Prologue.getAddressSize() == 0 ||
460 Prologue.getAddressSize() == DebugLineData.getAddressSize());
461
Alexey Samsonov110d5952014-04-30 00:09:19 +0000462 ParsingState State(this);
Benjamin Kramer112ec172011-09-15 21:59:13 +0000463
Paul Robinson9d4eb692017-05-01 23:27:55 +0000464 while (*OffsetPtr < EndOffset) {
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000465 if (OS)
466 *OS << format("0x%08.08" PRIx32 ": ", *OffsetPtr);
467
Paul Robinson9d4eb692017-05-01 23:27:55 +0000468 uint8_t Opcode = DebugLineData.getU8(OffsetPtr);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000469
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000470 if (OS)
471 *OS << format("%02.02" PRIx8 " ", Opcode);
472
Paul Robinson9d4eb692017-05-01 23:27:55 +0000473 if (Opcode == 0) {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000474 // Extended Opcodes always start with a zero opcode followed by
475 // a uleb128 length so you can skip ones you don't know about
Paul Robinson9d4eb692017-05-01 23:27:55 +0000476 uint64_t Len = DebugLineData.getULEB128(OffsetPtr);
Paul Robinsone0833342017-11-22 15:14:49 +0000477 uint32_t ExtOffset = *OffsetPtr;
478
479 // Tolerate zero-length; assume length is correct and soldier on.
480 if (Len == 0) {
481 if (OS)
482 *OS << "Badly formed extended line op (length 0)\n";
483 continue;
484 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000485
Paul Robinson9d4eb692017-05-01 23:27:55 +0000486 uint8_t SubOpcode = DebugLineData.getU8(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000487 if (OS)
488 *OS << LNExtendedString(SubOpcode);
Paul Robinson9d4eb692017-05-01 23:27:55 +0000489 switch (SubOpcode) {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000490 case DW_LNE_end_sequence:
491 // Set the end_sequence register of the state machine to true and
492 // append a row to the matrix using the current values of the
493 // state-machine registers. Then reset the registers to the initial
494 // values specified above. Every statement program sequence must end
495 // with a DW_LNE_end_sequence instruction which creates a row whose
496 // address is that of the byte after the last target machine instruction
497 // of the sequence.
Alexey Samsonov110d5952014-04-30 00:09:19 +0000498 State.Row.EndSequence = true;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000499 State.appendRowToMatrix(*OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000500 if (OS) {
501 *OS << "\n";
502 OS->indent(12);
503 State.Row.dump(*OS);
504 }
Alexey Samsonov110d5952014-04-30 00:09:19 +0000505 State.resetRowAndSequence();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000506 break;
507
508 case DW_LNE_set_address:
509 // Takes a single relocatable address as an operand. The size of the
510 // operand is the size appropriate to hold an address on the target
511 // machine. Set the address register to the value given by the
512 // relocatable address. All of the other statement program opcodes
513 // that affect the address register add a delta to it. This instruction
514 // stores a relocatable value into it instead.
Paul Robinson511b54c2017-11-22 15:48:30 +0000515 //
516 // Make sure the extractor knows the address size. If not, infer it
517 // from the size of the operand.
518 if (DebugLineData.getAddressSize() == 0)
519 DebugLineData.setAddressSize(Len - 1);
520 else
521 assert(DebugLineData.getAddressSize() == Len - 1);
Paul Robinson17536b92017-06-29 16:52:08 +0000522 State.Row.Address = DebugLineData.getRelocatedAddress(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000523 if (OS)
524 *OS << format(" (0x%16.16" PRIx64 ")", State.Row.Address);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000525 break;
526
527 case DW_LNE_define_file:
528 // Takes 4 arguments. The first is a null terminated string containing
529 // a source file name. The second is an unsigned LEB128 number
530 // representing the directory index of the directory in which the file
531 // was found. The third is an unsigned LEB128 number representing the
532 // time of last modification of the file. The fourth is an unsigned
533 // LEB128 number representing the length in bytes of the file. The time
534 // and length fields may contain LEB128(0) if the information is not
535 // available.
536 //
537 // The directory index represents an entry in the include_directories
538 // section of the statement program prologue. The index is LEB128(0)
539 // if the file was found in the current directory of the compilation,
540 // LEB128(1) if it was found in the first directory in the
541 // include_directories section, and so on. The directory index is
542 // ignored for file names that represent full path names.
543 //
544 // The files are numbered, starting at 1, in the order in which they
545 // appear; the names in the prologue come before names defined by
546 // the DW_LNE_define_file instruction. These numbers are used in the
547 // the file register of the state machine.
548 {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000549 FileNameEntry FileEntry;
Paul Robinson0a227092018-02-05 20:43:15 +0000550 const char *Name = DebugLineData.getCStr(OffsetPtr);
551 FileEntry.Name.setForm(dwarf::DW_FORM_string);
552 FileEntry.Name.setPValue(Name);
Paul Robinson9d4eb692017-05-01 23:27:55 +0000553 FileEntry.DirIdx = DebugLineData.getULEB128(OffsetPtr);
554 FileEntry.ModTime = DebugLineData.getULEB128(OffsetPtr);
555 FileEntry.Length = DebugLineData.getULEB128(OffsetPtr);
556 Prologue.FileNames.push_back(FileEntry);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000557 if (OS)
Paul Robinson0a227092018-02-05 20:43:15 +0000558 *OS << " (" << Name << ", dir=" << FileEntry.DirIdx << ", mod_time="
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000559 << format("(0x%16.16" PRIx64 ")", FileEntry.ModTime)
560 << ", length=" << FileEntry.Length << ")";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000561 }
562 break;
563
Diego Novillo5b5cf502014-02-14 19:27:53 +0000564 case DW_LNE_set_discriminator:
Paul Robinson9d4eb692017-05-01 23:27:55 +0000565 State.Row.Discriminator = DebugLineData.getULEB128(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000566 if (OS)
567 *OS << " (" << State.Row.Discriminator << ")";
Diego Novillo5b5cf502014-02-14 19:27:53 +0000568 break;
569
Benjamin Kramer5acab502011-09-15 02:12:05 +0000570 default:
Paul Robinsone0833342017-11-22 15:14:49 +0000571 if (OS)
572 *OS << format("Unrecognized extended op 0x%02.02" PRIx8, SubOpcode)
573 << format(" length %" PRIx64, Len);
574 // Len doesn't include the zero opcode byte or the length itself, but
575 // it does include the sub_opcode, so we have to adjust for that.
576 (*OffsetPtr) += Len - 1;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000577 break;
578 }
Paul Robinsone0833342017-11-22 15:14:49 +0000579 // Make sure the stated and parsed lengths are the same.
580 // Otherwise we have an unparseable line-number program.
581 if (*OffsetPtr - ExtOffset != Len) {
582 fprintf(stderr, "Unexpected line op length at offset 0x%8.8" PRIx32
583 " expected 0x%2.2" PRIx64 " found 0x%2.2" PRIx32 "\n",
584 ExtOffset, Len, *OffsetPtr - ExtOffset);
585 // Skip the rest of the line-number program.
586 *OffsetPtr = EndOffset;
587 return false;
588 }
Paul Robinson9d4eb692017-05-01 23:27:55 +0000589 } else if (Opcode < Prologue.OpcodeBase) {
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000590 if (OS)
591 *OS << LNStandardString(Opcode);
Paul Robinson9d4eb692017-05-01 23:27:55 +0000592 switch (Opcode) {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000593 // Standard Opcodes
594 case DW_LNS_copy:
595 // Takes no arguments. Append a row to the matrix using the
596 // current values of the state-machine registers. Then set
597 // the basic_block register to false.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000598 State.appendRowToMatrix(*OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000599 if (OS) {
600 *OS << "\n";
601 OS->indent(12);
602 State.Row.dump(*OS);
603 *OS << "\n";
604 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000605 break;
606
607 case DW_LNS_advance_pc:
608 // Takes a single unsigned LEB128 operand, multiplies it by the
609 // min_inst_length field of the prologue, and adds the
610 // result to the address register of the state machine.
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000611 {
612 uint64_t AddrOffset =
613 DebugLineData.getULEB128(OffsetPtr) * Prologue.MinInstLength;
614 State.Row.Address += AddrOffset;
615 if (OS)
616 *OS << " (" << AddrOffset << ")";
617 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000618 break;
619
620 case DW_LNS_advance_line:
621 // Takes a single signed LEB128 operand and adds that value to
622 // the line register of the state machine.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000623 State.Row.Line += DebugLineData.getSLEB128(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000624 if (OS)
625 *OS << " (" << State.Row.Line << ")";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000626 break;
627
628 case DW_LNS_set_file:
629 // Takes a single unsigned LEB128 operand and stores it in the file
630 // register of the state machine.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000631 State.Row.File = DebugLineData.getULEB128(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000632 if (OS)
633 *OS << " (" << State.Row.File << ")";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000634 break;
635
636 case DW_LNS_set_column:
637 // Takes a single unsigned LEB128 operand and stores it in the
638 // column register of the state machine.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000639 State.Row.Column = DebugLineData.getULEB128(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000640 if (OS)
641 *OS << " (" << State.Row.Column << ")";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000642 break;
643
644 case DW_LNS_negate_stmt:
645 // Takes no arguments. Set the is_stmt register of the state
646 // machine to the logical negation of its current value.
Alexey Samsonov110d5952014-04-30 00:09:19 +0000647 State.Row.IsStmt = !State.Row.IsStmt;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000648 break;
649
650 case DW_LNS_set_basic_block:
651 // Takes no arguments. Set the basic_block register of the
652 // state machine to true
Alexey Samsonov110d5952014-04-30 00:09:19 +0000653 State.Row.BasicBlock = true;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000654 break;
655
656 case DW_LNS_const_add_pc:
657 // Takes no arguments. Add to the address register of the state
658 // machine the address increment value corresponding to special
659 // opcode 255. The motivation for DW_LNS_const_add_pc is this:
660 // when the statement program needs to advance the address by a
661 // small amount, it can use a single special opcode, which occupies
662 // a single byte. When it needs to advance the address by up to
663 // twice the range of the last special opcode, it can use
664 // DW_LNS_const_add_pc followed by a special opcode, for a total
665 // of two bytes. Only if it needs to advance the address by more
666 // than twice that range will it need to use both DW_LNS_advance_pc
667 // and a special opcode, requiring three or more bytes.
668 {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000669 uint8_t AdjustOpcode = 255 - Prologue.OpcodeBase;
670 uint64_t AddrOffset =
671 (AdjustOpcode / Prologue.LineRange) * Prologue.MinInstLength;
672 State.Row.Address += AddrOffset;
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000673 if (OS)
674 *OS
675 << format(" (0x%16.16" PRIx64 ")", AddrOffset);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000676 }
677 break;
678
679 case DW_LNS_fixed_advance_pc:
680 // Takes a single uhalf operand. Add to the address register of
681 // the state machine the value of the (unencoded) operand. This
682 // is the only extended opcode that takes an argument that is not
683 // a variable length number. The motivation for DW_LNS_fixed_advance_pc
684 // is this: existing assemblers cannot emit DW_LNS_advance_pc or
685 // special opcodes because they cannot encode LEB128 numbers or
686 // judge when the computation of a special opcode overflows and
687 // requires the use of DW_LNS_advance_pc. Such assemblers, however,
688 // can use DW_LNS_fixed_advance_pc instead, sacrificing compression.
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000689 {
690 uint16_t PCOffset = DebugLineData.getU16(OffsetPtr);
691 State.Row.Address += PCOffset;
692 if (OS)
693 *OS
694 << format(" (0x%16.16" PRIx64 ")", PCOffset);
695 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000696 break;
697
698 case DW_LNS_set_prologue_end:
699 // Takes no arguments. Set the prologue_end register of the
700 // state machine to true
Alexey Samsonov110d5952014-04-30 00:09:19 +0000701 State.Row.PrologueEnd = true;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000702 break;
703
704 case DW_LNS_set_epilogue_begin:
705 // Takes no arguments. Set the basic_block register of the
706 // state machine to true
Alexey Samsonov110d5952014-04-30 00:09:19 +0000707 State.Row.EpilogueBegin = true;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000708 break;
709
710 case DW_LNS_set_isa:
711 // Takes a single unsigned LEB128 operand and stores it in the
712 // column register of the state machine.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000713 State.Row.Isa = DebugLineData.getULEB128(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000714 if (OS)
715 *OS << " (" << State.Row.Isa << ")";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000716 break;
717
718 default:
719 // Handle any unknown standard opcodes here. We know the lengths
720 // of such opcodes because they are specified in the prologue
721 // as a multiple of LEB128 operands for each opcode.
722 {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000723 assert(Opcode - 1U < Prologue.StandardOpcodeLengths.size());
724 uint8_t OpcodeLength = Prologue.StandardOpcodeLengths[Opcode - 1];
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000725 for (uint8_t I = 0; I < OpcodeLength; ++I) {
726 uint64_t Value = DebugLineData.getULEB128(OffsetPtr);
727 if (OS)
728 *OS << format("Skipping ULEB128 value: 0x%16.16" PRIx64 ")\n",
729 Value);
730 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000731 }
732 break;
733 }
734 } else {
735 // Special Opcodes
736
737 // A special opcode value is chosen based on the amount that needs
738 // to be added to the line and address registers. The maximum line
739 // increment for a special opcode is the value of the line_base
740 // field in the header, plus the value of the line_range field,
741 // minus 1 (line base + line range - 1). If the desired line
742 // increment is greater than the maximum line increment, a standard
NAKAMURA Takumif9959852011-10-08 11:22:47 +0000743 // opcode must be used instead of a special opcode. The "address
744 // advance" is calculated by dividing the desired address increment
Benjamin Kramer5acab502011-09-15 02:12:05 +0000745 // by the minimum_instruction_length field from the header. The
746 // special opcode is then calculated using the following formula:
747 //
748 // opcode = (desired line increment - line_base) +
749 // (line_range * address advance) + opcode_base
750 //
751 // If the resulting opcode is greater than 255, a standard opcode
752 // must be used instead.
753 //
754 // To decode a special opcode, subtract the opcode_base from the
755 // opcode itself to give the adjusted opcode. The amount to
756 // increment the address register is the result of the adjusted
757 // opcode divided by the line_range multiplied by the
758 // minimum_instruction_length field from the header. That is:
759 //
760 // address increment = (adjusted opcode / line_range) *
761 // minimum_instruction_length
762 //
763 // The amount to increment the line register is the line_base plus
764 // the result of the adjusted opcode modulo the line_range. That is:
765 //
766 // line increment = line_base + (adjusted opcode % line_range)
767
Paul Robinson9d4eb692017-05-01 23:27:55 +0000768 uint8_t AdjustOpcode = Opcode - Prologue.OpcodeBase;
769 uint64_t AddrOffset =
770 (AdjustOpcode / Prologue.LineRange) * Prologue.MinInstLength;
771 int32_t LineOffset =
772 Prologue.LineBase + (AdjustOpcode % Prologue.LineRange);
773 State.Row.Line += LineOffset;
774 State.Row.Address += AddrOffset;
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000775
776 if (OS) {
777 *OS << "address += " << ((uint32_t)AdjustOpcode)
778 << ", line += " << LineOffset << "\n";
779 OS->indent(12);
780 State.Row.dump(*OS);
781 }
782
Paul Robinson9d4eb692017-05-01 23:27:55 +0000783 State.appendRowToMatrix(*OffsetPtr);
Dehao Chen1b54fce2016-04-28 22:09:37 +0000784 // Reset discriminator to 0.
785 State.Row.Discriminator = 0;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000786 }
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000787 if(OS)
788 *OS << "\n";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000789 }
790
Alexey Samsonov110d5952014-04-30 00:09:19 +0000791 if (!State.Sequence.Empty) {
792 fprintf(stderr, "warning: last sequence in debug line table is not"
793 "terminated!\n");
794 }
795
796 // Sort all sequences so that address lookup will work faster.
797 if (!Sequences.empty()) {
798 std::sort(Sequences.begin(), Sequences.end(), Sequence::orderByLowPC);
799 // Note: actually, instruction address ranges of sequences should not
800 // overlap (in shared objects and executables). If they do, the address
801 // lookup would still work, though, but result would be ambiguous.
802 // We don't report warning in this case. For example,
803 // sometimes .so compiled from multiple object files contains a few
804 // rudimentary sequences for address ranges [0x0, 0xsomething).
805 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000806
Paul Robinson9d4eb692017-05-01 23:27:55 +0000807 return EndOffset;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000808}
809
Keno Fischerc2c60182015-05-31 23:37:04 +0000810uint32_t
Paul Robinson9d4eb692017-05-01 23:27:55 +0000811DWARFDebugLine::LineTable::findRowInSeq(const DWARFDebugLine::Sequence &Seq,
812 uint64_t Address) const {
813 if (!Seq.containsPC(Address))
Keno Fischerc2c60182015-05-31 23:37:04 +0000814 return UnknownRowIndex;
815 // Search for instruction address in the rows describing the sequence.
816 // Rows are stored in a vector, so we may use arithmetical operations with
817 // iterators.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000818 DWARFDebugLine::Row Row;
819 Row.Address = Address;
820 RowIter FirstRow = Rows.begin() + Seq.FirstRowIndex;
821 RowIter LastRow = Rows.begin() + Seq.LastRowIndex;
822 LineTable::RowIter RowPos = std::lower_bound(
823 FirstRow, LastRow, Row, DWARFDebugLine::Row::orderByAddress);
824 if (RowPos == LastRow) {
825 return Seq.LastRowIndex - 1;
Keno Fischerc2c60182015-05-31 23:37:04 +0000826 }
Paul Robinson9d4eb692017-05-01 23:27:55 +0000827 uint32_t Index = Seq.FirstRowIndex + (RowPos - FirstRow);
828 if (RowPos->Address > Address) {
829 if (RowPos == FirstRow)
Keno Fischerc2c60182015-05-31 23:37:04 +0000830 return UnknownRowIndex;
831 else
Paul Robinson9d4eb692017-05-01 23:27:55 +0000832 Index--;
Keno Fischerc2c60182015-05-31 23:37:04 +0000833 }
Paul Robinson9d4eb692017-05-01 23:27:55 +0000834 return Index;
Keno Fischerc2c60182015-05-31 23:37:04 +0000835}
836
Paul Robinson9d4eb692017-05-01 23:27:55 +0000837uint32_t DWARFDebugLine::LineTable::lookupAddress(uint64_t Address) const {
Alexey Samsonov947228c2012-08-07 11:46:57 +0000838 if (Sequences.empty())
Keno Fischerc2c60182015-05-31 23:37:04 +0000839 return UnknownRowIndex;
Alexey Samsonov947228c2012-08-07 11:46:57 +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 DWARFDebugLine::Sequence FoundSeq;
848 if (SeqPos == LastSeq) {
849 FoundSeq = Sequences.back();
850 } else if (SeqPos->LowPC == Address) {
851 FoundSeq = *SeqPos;
Alexey Samsonov947228c2012-08-07 11:46:57 +0000852 } else {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000853 if (SeqPos == FirstSeq)
Keno Fischerc2c60182015-05-31 23:37:04 +0000854 return UnknownRowIndex;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000855 FoundSeq = *(SeqPos - 1);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000856 }
Paul Robinson9d4eb692017-05-01 23:27:55 +0000857 return findRowInSeq(FoundSeq, Address);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000858}
Alexey Samsonov45be7932012-08-30 07:49:50 +0000859
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000860bool DWARFDebugLine::LineTable::lookupAddressRange(
Paul Robinson9d4eb692017-05-01 23:27:55 +0000861 uint64_t Address, uint64_t Size, std::vector<uint32_t> &Result) const {
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000862 if (Sequences.empty())
863 return false;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000864 uint64_t EndAddr = Address + Size;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000865 // First, find an instruction sequence containing the given address.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000866 DWARFDebugLine::Sequence Sequence;
867 Sequence.LowPC = Address;
868 SequenceIter FirstSeq = Sequences.begin();
869 SequenceIter LastSeq = Sequences.end();
870 SequenceIter SeqPos = std::lower_bound(
871 FirstSeq, LastSeq, Sequence, DWARFDebugLine::Sequence::orderByLowPC);
872 if (SeqPos == LastSeq || SeqPos->LowPC != Address) {
873 if (SeqPos == FirstSeq)
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000874 return false;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000875 SeqPos--;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000876 }
Paul Robinson9d4eb692017-05-01 23:27:55 +0000877 if (!SeqPos->containsPC(Address))
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000878 return false;
879
Paul Robinson9d4eb692017-05-01 23:27:55 +0000880 SequenceIter StartPos = SeqPos;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000881
882 // Add the rows from the first sequence to the vector, starting with the
883 // index we just calculated
884
Paul Robinson9d4eb692017-05-01 23:27:55 +0000885 while (SeqPos != LastSeq && SeqPos->LowPC < EndAddr) {
886 const DWARFDebugLine::Sequence &CurSeq = *SeqPos;
Keno Fischerc2c60182015-05-31 23:37:04 +0000887 // For the first sequence, we need to find which row in the sequence is the
888 // first in our range.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000889 uint32_t FirstRowIndex = CurSeq.FirstRowIndex;
890 if (SeqPos == StartPos)
891 FirstRowIndex = findRowInSeq(CurSeq, Address);
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000892
Keno Fischerc2c60182015-05-31 23:37:04 +0000893 // Figure out the last row in the range.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000894 uint32_t LastRowIndex = findRowInSeq(CurSeq, EndAddr - 1);
895 if (LastRowIndex == UnknownRowIndex)
896 LastRowIndex = CurSeq.LastRowIndex - 1;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000897
Paul Robinson9d4eb692017-05-01 23:27:55 +0000898 assert(FirstRowIndex != UnknownRowIndex);
899 assert(LastRowIndex != UnknownRowIndex);
Keno Fischerc2c60182015-05-31 23:37:04 +0000900
Paul Robinson9d4eb692017-05-01 23:27:55 +0000901 for (uint32_t I = FirstRowIndex; I <= LastRowIndex; ++I) {
902 Result.push_back(I);
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000903 }
904
Paul Robinson9d4eb692017-05-01 23:27:55 +0000905 ++SeqPos;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000906 }
NAKAMURA Takumi4b86cdb2013-01-26 01:45:06 +0000907
908 return true;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000909}
910
Paul Robinson9d4eb692017-05-01 23:27:55 +0000911bool DWARFDebugLine::LineTable::hasFileAtIndex(uint64_t FileIndex) const {
Pete Cooperb2ba7762016-07-22 01:41:32 +0000912 return FileIndex != 0 && FileIndex <= Prologue.FileNames.size();
913}
914
Paul Robinson9d4eb692017-05-01 23:27:55 +0000915bool DWARFDebugLine::LineTable::getFileNameByIndex(uint64_t FileIndex,
916 const char *CompDir,
917 FileLineInfoKind Kind,
918 std::string &Result) const {
Pete Cooperb2ba7762016-07-22 01:41:32 +0000919 if (Kind == FileLineInfoKind::None || !hasFileAtIndex(FileIndex))
Alexey Samsonov45be7932012-08-30 07:49:50 +0000920 return false;
921 const FileNameEntry &Entry = Prologue.FileNames[FileIndex - 1];
Paul Robinson0a227092018-02-05 20:43:15 +0000922 StringRef FileName = Entry.Name.getAsCString().getValue();
Alexey Samsonovdce67342014-05-15 21:24:32 +0000923 if (Kind != FileLineInfoKind::AbsoluteFilePath ||
Alexey Samsonov45be7932012-08-30 07:49:50 +0000924 sys::path::is_absolute(FileName)) {
925 Result = FileName;
926 return true;
927 }
Frederic Riss101b5e22014-09-19 15:11:51 +0000928
Alexey Samsonov45be7932012-08-30 07:49:50 +0000929 SmallString<16> FilePath;
930 uint64_t IncludeDirIndex = Entry.DirIdx;
Paul Robinsonba1c9152017-05-02 17:37:32 +0000931 StringRef IncludeDir;
Alexey Samsonov45be7932012-08-30 07:49:50 +0000932 // Be defensive about the contents of Entry.
933 if (IncludeDirIndex > 0 &&
Frederic Riss101b5e22014-09-19 15:11:51 +0000934 IncludeDirIndex <= Prologue.IncludeDirectories.size())
Paul Robinson0a227092018-02-05 20:43:15 +0000935 IncludeDir = Prologue.IncludeDirectories[IncludeDirIndex - 1]
936 .getAsCString()
937 .getValue();
Frederic Riss101b5e22014-09-19 15:11:51 +0000938
939 // We may still need to append compilation directory of compile unit.
940 // We know that FileName is not absolute, the only way to have an
941 // absolute path at this point would be if IncludeDir is absolute.
942 if (CompDir && Kind == FileLineInfoKind::AbsoluteFilePath &&
943 sys::path::is_relative(IncludeDir))
944 sys::path::append(FilePath, CompDir);
945
946 // sys::path::append skips empty strings.
947 sys::path::append(FilePath, IncludeDir, FileName);
Alexey Samsonov45be7932012-08-30 07:49:50 +0000948 Result = FilePath.str();
949 return true;
950}
Frederic Riss101b5e22014-09-19 15:11:51 +0000951
Dehao Chen1b54fce2016-04-28 22:09:37 +0000952bool DWARFDebugLine::LineTable::getFileLineInfoForAddress(
953 uint64_t Address, const char *CompDir, FileLineInfoKind Kind,
954 DILineInfo &Result) const {
Frederic Riss101b5e22014-09-19 15:11:51 +0000955 // Get the index of row we're looking for in the line table.
956 uint32_t RowIndex = lookupAddress(Address);
957 if (RowIndex == -1U)
958 return false;
959 // Take file number and line/column from the row.
960 const auto &Row = Rows[RowIndex];
961 if (!getFileNameByIndex(Row.File, CompDir, Kind, Result.FileName))
962 return false;
963 Result.Line = Row.Line;
964 Result.Column = Row.Column;
Eric Christopherba1024c2016-12-14 18:29:39 +0000965 Result.Discriminator = Row.Discriminator;
Frederic Riss101b5e22014-09-19 15:11:51 +0000966 return true;
967}