blob: dd74ea3a594f495aac8de205c954c9647d362b20 [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"
Scott Linder16c7bda2018-02-23 23:01:06 +000011#include "llvm/ADT/Optional.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000012#include "llvm/ADT/SmallString.h"
Eugene Zelenko2db0cfa2017-06-23 21:57:40 +000013#include "llvm/ADT/SmallVector.h"
14#include "llvm/ADT/StringRef.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000015#include "llvm/BinaryFormat/Dwarf.h"
Paul Robinson2bc38732017-05-02 21:40:47 +000016#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000017#include "llvm/DebugInfo/DWARF/DWARFRelocMap.h"
Benjamin Kramer5acab502011-09-15 02:12:05 +000018#include "llvm/Support/Format.h"
Alexey Samsonov45be7932012-08-30 07:49:50 +000019#include "llvm/Support/Path.h"
Jonas Devlieghere84e99262018-04-14 22:07:23 +000020#include "llvm/Support/WithColor.h"
Benjamin Kramer5acab502011-09-15 02:12:05 +000021#include "llvm/Support/raw_ostream.h"
Benjamin Kramera57c46a2011-09-15 02:19:33 +000022#include <algorithm>
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000023#include <cassert>
24#include <cinttypes>
25#include <cstdint>
26#include <cstdio>
27#include <utility>
28
Benjamin Kramer5acab502011-09-15 02:12:05 +000029using namespace llvm;
30using namespace dwarf;
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000031
Eugene Zelenko2db0cfa2017-06-23 21:57:40 +000032using FileLineInfoKind = DILineInfoSpecifier::FileLineInfoKind;
33
Paul Robinson2bc38732017-05-02 21:40:47 +000034namespace {
Eugene Zelenko2db0cfa2017-06-23 21:57:40 +000035
Paul Robinson2bc38732017-05-02 21:40:47 +000036struct ContentDescriptor {
37 dwarf::LineNumberEntryFormat Type;
38 dwarf::Form Form;
39};
Eugene Zelenko2db0cfa2017-06-23 21:57:40 +000040
41using ContentDescriptors = SmallVector<ContentDescriptor, 4>;
42
Paul Robinson2bc38732017-05-02 21:40:47 +000043} // end anonmyous namespace
Benjamin Kramer5acab502011-09-15 02:12:05 +000044
Scott Linder16c7bda2018-02-23 23:01:06 +000045void DWARFDebugLine::ContentTypeTracker::trackContentType(
46 dwarf::LineNumberEntryFormat ContentType) {
47 switch (ContentType) {
48 case dwarf::DW_LNCT_timestamp:
49 HasModTime = true;
50 break;
51 case dwarf::DW_LNCT_size:
52 HasLength = true;
53 break;
54 case dwarf::DW_LNCT_MD5:
55 HasMD5 = true;
56 break;
57 case dwarf::DW_LNCT_LLVM_source:
58 HasSource = true;
59 break;
60 default:
61 // We only care about values we consider optional, and new values may be
62 // added in the vendor extension range, so we do not match exhaustively.
63 break;
64 }
65}
66
Dehao Chen1b54fce2016-04-28 22:09:37 +000067DWARFDebugLine::Prologue::Prologue() { clear(); }
Alexey Samsonov836b1ae2014-04-29 21:28:13 +000068
69void DWARFDebugLine::Prologue::clear() {
Paul Robinson75c068c2017-06-26 18:43:01 +000070 TotalLength = PrologueLength = 0;
71 SegSelectorSize = 0;
Alexey Samsonov836b1ae2014-04-29 21:28:13 +000072 MinInstLength = MaxOpsPerInst = DefaultIsStmt = LineBase = LineRange = 0;
73 OpcodeBase = 0;
Pavel Labath322711f2018-03-14 09:39:54 +000074 FormParams = dwarf::FormParams({0, 0, DWARF32});
Scott Linder16c7bda2018-02-23 23:01:06 +000075 ContentTypes = ContentTypeTracker();
Alexey Samsonov836b1ae2014-04-29 21:28:13 +000076 StandardOpcodeLengths.clear();
77 IncludeDirectories.clear();
78 FileNames.clear();
79}
80
Paul Robinson0a227092018-02-05 20:43:15 +000081void DWARFDebugLine::Prologue::dump(raw_ostream &OS,
82 DIDumpOptions DumpOptions) const {
Benjamin Kramer5acab502011-09-15 02:12:05 +000083 OS << "Line table prologue:\n"
Ed Maste6d0bee52015-05-28 15:38:17 +000084 << format(" total_length: 0x%8.8" PRIx64 "\n", TotalLength)
Paul Robinson75c068c2017-06-26 18:43:01 +000085 << format(" version: %u\n", getVersion());
86 if (getVersion() >= 5)
87 OS << format(" address_size: %u\n", getAddressSize())
88 << format(" seg_select_size: %u\n", SegSelectorSize);
89 OS << format(" prologue_length: 0x%8.8" PRIx64 "\n", PrologueLength)
David Blaikie1d4736e2014-02-24 23:58:54 +000090 << format(" min_inst_length: %u\n", MinInstLength)
Paul Robinson75c068c2017-06-26 18:43:01 +000091 << format(getVersion() >= 4 ? "max_ops_per_inst: %u\n" : "", MaxOpsPerInst)
David Blaikie1d4736e2014-02-24 23:58:54 +000092 << format(" default_is_stmt: %u\n", DefaultIsStmt)
93 << format(" line_base: %i\n", LineBase)
94 << format(" line_range: %u\n", LineRange)
95 << format(" opcode_base: %u\n", OpcodeBase);
Benjamin Kramer5acab502011-09-15 02:12:05 +000096
Paul Robinson9d4eb692017-05-01 23:27:55 +000097 for (uint32_t I = 0; I != StandardOpcodeLengths.size(); ++I)
Mehdi Amini149f6ea2016-10-05 05:59:29 +000098 OS << format("standard_opcode_lengths[%s] = %u\n",
Paul Robinson9d4eb692017-05-01 23:27:55 +000099 LNStandardString(I + 1).data(), StandardOpcodeLengths[I]);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000100
Paul Robinson8181d232018-01-18 20:33:35 +0000101 if (!IncludeDirectories.empty()) {
102 // DWARF v5 starts directory indexes at 0.
103 uint32_t DirBase = getVersion() >= 5 ? 0 : 1;
Paul Robinson0a227092018-02-05 20:43:15 +0000104 for (uint32_t I = 0; I != IncludeDirectories.size(); ++I) {
105 OS << format("include_directories[%3u] = ", I + DirBase);
106 IncludeDirectories[I].dump(OS, DumpOptions);
107 OS << '\n';
108 }
Paul Robinson8181d232018-01-18 20:33:35 +0000109 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000110
111 if (!FileNames.empty()) {
Paul Robinsonceafcd42018-02-08 23:08:02 +0000112 // DWARF v5 starts file indexes at 0.
113 uint32_t FileBase = getVersion() >= 5 ? 0 : 1;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000114 for (uint32_t I = 0; I != FileNames.size(); ++I) {
115 const FileNameEntry &FileEntry = FileNames[I];
Scott Linder16c7bda2018-02-23 23:01:06 +0000116 OS << format("file_names[%3u]:\n", I + FileBase);
117 OS << " name: ";
Paul Robinson0a227092018-02-05 20:43:15 +0000118 FileEntry.Name.dump(OS, DumpOptions);
Scott Linder16c7bda2018-02-23 23:01:06 +0000119 OS << '\n'
120 << format(" dir_index: %" PRIu64 "\n", FileEntry.DirIdx);
121 if (ContentTypes.HasMD5)
122 OS << " md5_checksum: " << FileEntry.Checksum.digest() << '\n';
123 if (ContentTypes.HasModTime)
124 OS << format(" mod_time: 0x%8.8" PRIx64 "\n", FileEntry.ModTime);
125 if (ContentTypes.HasLength)
126 OS << format(" length: 0x%8.8" PRIx64 "\n", FileEntry.Length);
127 if (ContentTypes.HasSource) {
128 OS << " source: ";
129 FileEntry.Source.dump(OS, DumpOptions);
130 OS << '\n';
131 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000132 }
133 }
134}
135
Paul Robinson2bc38732017-05-02 21:40:47 +0000136// Parse v2-v4 directory and file tables.
137static void
Paul Robinson17536b92017-06-29 16:52:08 +0000138parseV2DirFileTables(const DWARFDataExtractor &DebugLineData,
139 uint32_t *OffsetPtr, uint64_t EndPrologueOffset,
Scott Linder16c7bda2018-02-23 23:01:06 +0000140 DWARFDebugLine::ContentTypeTracker &ContentTypes,
Paul Robinson0a227092018-02-05 20:43:15 +0000141 std::vector<DWARFFormValue> &IncludeDirectories,
Paul Robinson2bc38732017-05-02 21:40:47 +0000142 std::vector<DWARFDebugLine::FileNameEntry> &FileNames) {
143 while (*OffsetPtr < EndPrologueOffset) {
144 StringRef S = DebugLineData.getCStrRef(OffsetPtr);
145 if (S.empty())
146 break;
Paul Robinson0a227092018-02-05 20:43:15 +0000147 DWARFFormValue Dir(dwarf::DW_FORM_string);
148 Dir.setPValue(S.data());
149 IncludeDirectories.push_back(Dir);
Paul Robinson2bc38732017-05-02 21:40:47 +0000150 }
151
152 while (*OffsetPtr < EndPrologueOffset) {
153 StringRef Name = DebugLineData.getCStrRef(OffsetPtr);
154 if (Name.empty())
155 break;
156 DWARFDebugLine::FileNameEntry FileEntry;
Paul Robinson0a227092018-02-05 20:43:15 +0000157 FileEntry.Name.setForm(dwarf::DW_FORM_string);
158 FileEntry.Name.setPValue(Name.data());
Paul Robinson2bc38732017-05-02 21:40:47 +0000159 FileEntry.DirIdx = DebugLineData.getULEB128(OffsetPtr);
160 FileEntry.ModTime = DebugLineData.getULEB128(OffsetPtr);
161 FileEntry.Length = DebugLineData.getULEB128(OffsetPtr);
162 FileNames.push_back(FileEntry);
163 }
Scott Linder16c7bda2018-02-23 23:01:06 +0000164
165 ContentTypes.HasModTime = true;
166 ContentTypes.HasLength = true;
Paul Robinson2bc38732017-05-02 21:40:47 +0000167}
168
169// Parse v5 directory/file entry content descriptions.
170// Returns the descriptors, or an empty vector if we did not find a path or
171// ran off the end of the prologue.
172static ContentDescriptors
Scott Linder16c7bda2018-02-23 23:01:06 +0000173parseV5EntryFormat(const DWARFDataExtractor &DebugLineData, uint32_t
174 *OffsetPtr, uint64_t EndPrologueOffset, DWARFDebugLine::ContentTypeTracker
175 *ContentTypes) {
Paul Robinson2bc38732017-05-02 21:40:47 +0000176 ContentDescriptors Descriptors;
177 int FormatCount = DebugLineData.getU8(OffsetPtr);
178 bool HasPath = false;
179 for (int I = 0; I != FormatCount; ++I) {
180 if (*OffsetPtr >= EndPrologueOffset)
181 return ContentDescriptors();
182 ContentDescriptor Descriptor;
183 Descriptor.Type =
184 dwarf::LineNumberEntryFormat(DebugLineData.getULEB128(OffsetPtr));
185 Descriptor.Form = dwarf::Form(DebugLineData.getULEB128(OffsetPtr));
186 if (Descriptor.Type == dwarf::DW_LNCT_path)
187 HasPath = true;
Scott Linder16c7bda2018-02-23 23:01:06 +0000188 if (ContentTypes)
189 ContentTypes->trackContentType(Descriptor.Type);
Paul Robinson2bc38732017-05-02 21:40:47 +0000190 Descriptors.push_back(Descriptor);
191 }
192 return HasPath ? Descriptors : ContentDescriptors();
193}
194
195static bool
Paul Robinson17536b92017-06-29 16:52:08 +0000196parseV5DirFileTables(const DWARFDataExtractor &DebugLineData,
197 uint32_t *OffsetPtr, uint64_t EndPrologueOffset,
Pavel Labath322711f2018-03-14 09:39:54 +0000198 const dwarf::FormParams &FormParams,
199 const DWARFContext &Ctx, const DWARFUnit *U,
Scott Linder16c7bda2018-02-23 23:01:06 +0000200 DWARFDebugLine::ContentTypeTracker &ContentTypes,
Paul Robinson0a227092018-02-05 20:43:15 +0000201 std::vector<DWARFFormValue> &IncludeDirectories,
Paul Robinson2bc38732017-05-02 21:40:47 +0000202 std::vector<DWARFDebugLine::FileNameEntry> &FileNames) {
203 // Get the directory entry description.
204 ContentDescriptors DirDescriptors =
Paul Robinsona06f8dc2017-12-18 19:08:35 +0000205 parseV5EntryFormat(DebugLineData, OffsetPtr, EndPrologueOffset, nullptr);
Paul Robinson2bc38732017-05-02 21:40:47 +0000206 if (DirDescriptors.empty())
207 return false;
208
209 // Get the directory entries, according to the format described above.
210 int DirEntryCount = DebugLineData.getU8(OffsetPtr);
211 for (int I = 0; I != DirEntryCount; ++I) {
212 if (*OffsetPtr >= EndPrologueOffset)
213 return false;
214 for (auto Descriptor : DirDescriptors) {
215 DWARFFormValue Value(Descriptor.Form);
216 switch (Descriptor.Type) {
217 case DW_LNCT_path:
Paul Robinsonbf750c82018-01-29 20:57:43 +0000218 if (!Value.extractValue(DebugLineData, OffsetPtr, FormParams, &Ctx, U))
Paul Robinson2bc38732017-05-02 21:40:47 +0000219 return false;
Paul Robinson0a227092018-02-05 20:43:15 +0000220 IncludeDirectories.push_back(Value);
Paul Robinson2bc38732017-05-02 21:40:47 +0000221 break;
222 default:
Paul Robinson75c068c2017-06-26 18:43:01 +0000223 if (!Value.skipValue(DebugLineData, OffsetPtr, FormParams))
Paul Robinson2bc38732017-05-02 21:40:47 +0000224 return false;
225 }
226 }
227 }
228
229 // Get the file entry description.
230 ContentDescriptors FileDescriptors =
Scott Linder16c7bda2018-02-23 23:01:06 +0000231 parseV5EntryFormat(DebugLineData, OffsetPtr, EndPrologueOffset,
232 &ContentTypes);
Paul Robinson2bc38732017-05-02 21:40:47 +0000233 if (FileDescriptors.empty())
234 return false;
235
236 // Get the file entries, according to the format described above.
237 int FileEntryCount = DebugLineData.getU8(OffsetPtr);
238 for (int I = 0; I != FileEntryCount; ++I) {
239 if (*OffsetPtr >= EndPrologueOffset)
240 return false;
241 DWARFDebugLine::FileNameEntry FileEntry;
242 for (auto Descriptor : FileDescriptors) {
243 DWARFFormValue Value(Descriptor.Form);
Paul Robinsonbf750c82018-01-29 20:57:43 +0000244 if (!Value.extractValue(DebugLineData, OffsetPtr, FormParams, &Ctx, U))
Paul Robinson2bc38732017-05-02 21:40:47 +0000245 return false;
246 switch (Descriptor.Type) {
247 case DW_LNCT_path:
Paul Robinson0a227092018-02-05 20:43:15 +0000248 FileEntry.Name = Value;
Paul Robinson2bc38732017-05-02 21:40:47 +0000249 break;
Scott Linder16c7bda2018-02-23 23:01:06 +0000250 case DW_LNCT_LLVM_source:
251 FileEntry.Source = Value;
252 break;
Paul Robinson2bc38732017-05-02 21:40:47 +0000253 case DW_LNCT_directory_index:
254 FileEntry.DirIdx = Value.getAsUnsignedConstant().getValue();
255 break;
256 case DW_LNCT_timestamp:
257 FileEntry.ModTime = Value.getAsUnsignedConstant().getValue();
258 break;
259 case DW_LNCT_size:
260 FileEntry.Length = Value.getAsUnsignedConstant().getValue();
261 break;
Paul Robinsona06f8dc2017-12-18 19:08:35 +0000262 case DW_LNCT_MD5:
263 assert(Value.getAsBlock().getValue().size() == 16);
264 std::uninitialized_copy_n(Value.getAsBlock().getValue().begin(), 16,
265 FileEntry.Checksum.Bytes.begin());
266 break;
Paul Robinson2bc38732017-05-02 21:40:47 +0000267 default:
268 break;
269 }
270 }
271 FileNames.push_back(FileEntry);
272 }
273 return true;
274}
275
Paul Robinson17536b92017-06-29 16:52:08 +0000276bool DWARFDebugLine::Prologue::parse(const DWARFDataExtractor &DebugLineData,
Paul Robinsonbf750c82018-01-29 20:57:43 +0000277 uint32_t *OffsetPtr,
278 const DWARFContext &Ctx,
279 const DWARFUnit *U) {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000280 const uint64_t PrologueOffset = *OffsetPtr;
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000281
282 clear();
Paul Robinson9d4eb692017-05-01 23:27:55 +0000283 TotalLength = DebugLineData.getU32(OffsetPtr);
Ed Maste6d0bee52015-05-28 15:38:17 +0000284 if (TotalLength == UINT32_MAX) {
Paul Robinson75c068c2017-06-26 18:43:01 +0000285 FormParams.Format = dwarf::DWARF64;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000286 TotalLength = DebugLineData.getU64(OffsetPtr);
Paul Robinson75c068c2017-06-26 18:43:01 +0000287 } else if (TotalLength >= 0xffffff00) {
Ed Maste6d0bee52015-05-28 15:38:17 +0000288 return false;
289 }
Paul Robinson75c068c2017-06-26 18:43:01 +0000290 FormParams.Version = DebugLineData.getU16(OffsetPtr);
291 if (getVersion() < 2)
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000292 return false;
293
Paul Robinson75c068c2017-06-26 18:43:01 +0000294 if (getVersion() >= 5) {
295 FormParams.AddrSize = DebugLineData.getU8(OffsetPtr);
Paul Robinson63811a42017-11-22 15:33:17 +0000296 assert((DebugLineData.getAddressSize() == 0 ||
297 DebugLineData.getAddressSize() == getAddressSize()) &&
Paul Robinson75c068c2017-06-26 18:43:01 +0000298 "Line table header and data extractor disagree");
Paul Robinson2bc38732017-05-02 21:40:47 +0000299 SegSelectorSize = DebugLineData.getU8(OffsetPtr);
300 }
301
Paul Robinson9d4eb692017-05-01 23:27:55 +0000302 PrologueLength = DebugLineData.getUnsigned(OffsetPtr, sizeofPrologueLength());
303 const uint64_t EndPrologueOffset = PrologueLength + *OffsetPtr;
304 MinInstLength = DebugLineData.getU8(OffsetPtr);
Paul Robinson75c068c2017-06-26 18:43:01 +0000305 if (getVersion() >= 4)
Paul Robinson9d4eb692017-05-01 23:27:55 +0000306 MaxOpsPerInst = DebugLineData.getU8(OffsetPtr);
307 DefaultIsStmt = DebugLineData.getU8(OffsetPtr);
308 LineBase = DebugLineData.getU8(OffsetPtr);
309 LineRange = DebugLineData.getU8(OffsetPtr);
310 OpcodeBase = DebugLineData.getU8(OffsetPtr);
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000311
312 StandardOpcodeLengths.reserve(OpcodeBase - 1);
Paul Robinson9d4eb692017-05-01 23:27:55 +0000313 for (uint32_t I = 1; I < OpcodeBase; ++I) {
314 uint8_t OpLen = DebugLineData.getU8(OffsetPtr);
315 StandardOpcodeLengths.push_back(OpLen);
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000316 }
317
Paul Robinson75c068c2017-06-26 18:43:01 +0000318 if (getVersion() >= 5) {
Paul Robinson2bc38732017-05-02 21:40:47 +0000319 if (!parseV5DirFileTables(DebugLineData, OffsetPtr, EndPrologueOffset,
Scott Linder16c7bda2018-02-23 23:01:06 +0000320 FormParams, Ctx, U, ContentTypes,
321 IncludeDirectories, FileNames)) {
Jonas Devlieghere84e99262018-04-14 22:07:23 +0000322 WithColor::warning() << format(
323 "parsing line table prologue at 0x%8.8" PRIx64
324 " found an invalid directory or file table description at"
325 " 0x%8.8" PRIx64 "\n",
326 PrologueOffset, (uint64_t)*OffsetPtr);
Paul Robinson2bc38732017-05-02 21:40:47 +0000327 return false;
328 }
329 } else
330 parseV2DirFileTables(DebugLineData, OffsetPtr, EndPrologueOffset,
Scott Linder16c7bda2018-02-23 23:01:06 +0000331 ContentTypes, IncludeDirectories, FileNames);
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000332
Paul Robinson9d4eb692017-05-01 23:27:55 +0000333 if (*OffsetPtr != EndPrologueOffset) {
Jonas Devlieghere84e99262018-04-14 22:07:23 +0000334 WithColor::warning() << format(
335 "parsing line table prologue at 0x%8.8" PRIx64
336 " should have ended at 0x%8.8" PRIx64 " but it ended at 0x%8.8" PRIx64
337 "\n",
338 PrologueOffset, EndPrologueOffset, (uint64_t)*OffsetPtr);
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000339 return false;
340 }
341 return true;
342}
343
Paul Robinson9d4eb692017-05-01 23:27:55 +0000344DWARFDebugLine::Row::Row(bool DefaultIsStmt) { reset(DefaultIsStmt); }
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000345
Benjamin Kramer5acab502011-09-15 02:12:05 +0000346void DWARFDebugLine::Row::postAppend() {
347 BasicBlock = false;
348 PrologueEnd = false;
349 EpilogueBegin = false;
350}
351
Paul Robinson9d4eb692017-05-01 23:27:55 +0000352void DWARFDebugLine::Row::reset(bool DefaultIsStmt) {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000353 Address = 0;
354 Line = 1;
355 Column = 0;
356 File = 1;
357 Isa = 0;
Diego Novillo5b5cf502014-02-14 19:27:53 +0000358 Discriminator = 0;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000359 IsStmt = DefaultIsStmt;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000360 BasicBlock = false;
361 EndSequence = false;
362 PrologueEnd = false;
363 EpilogueBegin = false;
364}
365
Greg Clayton67070462017-05-02 22:48:52 +0000366void DWARFDebugLine::Row::dumpTableHeader(raw_ostream &OS) {
367 OS << "Address Line Column File ISA Discriminator Flags\n"
368 << "------------------ ------ ------ ------ --- ------------- "
369 "-------------\n";
370}
371
Benjamin Kramer5acab502011-09-15 02:12:05 +0000372void DWARFDebugLine::Row::dump(raw_ostream &OS) const {
Benjamin Kramerf3da5292011-11-05 08:57:40 +0000373 OS << format("0x%16.16" PRIx64 " %6u %6u", Address, Line, Column)
Diego Novillo5b5cf502014-02-14 19:27:53 +0000374 << format(" %6u %3u %13u ", File, Isa, Discriminator)
Dehao Chen1b54fce2016-04-28 22:09:37 +0000375 << (IsStmt ? " is_stmt" : "") << (BasicBlock ? " basic_block" : "")
Benjamin Kramer5acab502011-09-15 02:12:05 +0000376 << (PrologueEnd ? " prologue_end" : "")
377 << (EpilogueBegin ? " epilogue_begin" : "")
Dehao Chen1b54fce2016-04-28 22:09:37 +0000378 << (EndSequence ? " end_sequence" : "") << '\n';
Benjamin Kramer5acab502011-09-15 02:12:05 +0000379}
380
Dehao Chen1b54fce2016-04-28 22:09:37 +0000381DWARFDebugLine::Sequence::Sequence() { reset(); }
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000382
383void DWARFDebugLine::Sequence::reset() {
384 LowPC = 0;
385 HighPC = 0;
386 FirstRowIndex = 0;
387 LastRowIndex = 0;
388 Empty = true;
389}
390
Dehao Chen1b54fce2016-04-28 22:09:37 +0000391DWARFDebugLine::LineTable::LineTable() { clear(); }
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000392
Paul Robinson0a227092018-02-05 20:43:15 +0000393void DWARFDebugLine::LineTable::dump(raw_ostream &OS,
394 DIDumpOptions DumpOptions) const {
395 Prologue.dump(OS, DumpOptions);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000396 OS << '\n';
397
398 if (!Rows.empty()) {
Greg Clayton67070462017-05-02 22:48:52 +0000399 Row::dumpTableHeader(OS);
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000400 for (const Row &R : Rows) {
401 R.dump(OS);
402 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000403 }
404}
405
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000406void DWARFDebugLine::LineTable::clear() {
407 Prologue.clear();
408 Rows.clear();
409 Sequences.clear();
410}
411
Alexey Samsonov110d5952014-04-30 00:09:19 +0000412DWARFDebugLine::ParsingState::ParsingState(struct LineTable *LT)
Eugene Zelenko2db0cfa2017-06-23 21:57:40 +0000413 : LineTable(LT) {
Alexey Samsonov110d5952014-04-30 00:09:19 +0000414 resetRowAndSequence();
415}
Nick Lewycky4d044922011-09-15 03:41:51 +0000416
Alexey Samsonov110d5952014-04-30 00:09:19 +0000417void DWARFDebugLine::ParsingState::resetRowAndSequence() {
418 Row.reset(LineTable->Prologue.DefaultIsStmt);
419 Sequence.reset();
420}
421
Paul Robinson9d4eb692017-05-01 23:27:55 +0000422void DWARFDebugLine::ParsingState::appendRowToMatrix(uint32_t Offset) {
Alexey Samsonov110d5952014-04-30 00:09:19 +0000423 if (Sequence.Empty) {
Alexey Samsonov947228c2012-08-07 11:46:57 +0000424 // Record the beginning of instruction sequence.
Alexey Samsonov110d5952014-04-30 00:09:19 +0000425 Sequence.Empty = false;
426 Sequence.LowPC = Row.Address;
427 Sequence.FirstRowIndex = RowNumber;
Alexey Samsonov947228c2012-08-07 11:46:57 +0000428 }
Alexey Samsonov110d5952014-04-30 00:09:19 +0000429 ++RowNumber;
430 LineTable->appendRow(Row);
431 if (Row.EndSequence) {
Alexey Samsonov947228c2012-08-07 11:46:57 +0000432 // Record the end of instruction sequence.
Alexey Samsonov110d5952014-04-30 00:09:19 +0000433 Sequence.HighPC = Row.Address;
434 Sequence.LastRowIndex = RowNumber;
435 if (Sequence.isValid())
436 LineTable->appendSequence(Sequence);
437 Sequence.reset();
Alexey Samsonov947228c2012-08-07 11:46:57 +0000438 }
Alexey Samsonov110d5952014-04-30 00:09:19 +0000439 Row.postAppend();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000440}
441
Benjamin Kramer5acab502011-09-15 02:12:05 +0000442const DWARFDebugLine::LineTable *
Paul Robinson9d4eb692017-05-01 23:27:55 +0000443DWARFDebugLine::getLineTable(uint32_t Offset) const {
444 LineTableConstIter Pos = LineTableMap.find(Offset);
445 if (Pos != LineTableMap.end())
446 return &Pos->second;
Craig Topper2617dcc2014-04-15 06:32:26 +0000447 return nullptr;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000448}
449
Benjamin Kramer679e1752011-09-15 20:43:18 +0000450const DWARFDebugLine::LineTable *
Paul Robinson511b54c2017-11-22 15:48:30 +0000451DWARFDebugLine::getOrParseLineTable(DWARFDataExtractor &DebugLineData,
Paul Robinsonbf750c82018-01-29 20:57:43 +0000452 uint32_t Offset, const DWARFContext &Ctx,
453 const DWARFUnit *U) {
James Henderson66702622018-03-08 10:53:34 +0000454 if (!DebugLineData.isValidOffset(Offset))
455 return nullptr;
456
Paul Robinson9d4eb692017-05-01 23:27:55 +0000457 std::pair<LineTableIter, bool> Pos =
458 LineTableMap.insert(LineTableMapTy::value_type(Offset, LineTable()));
459 LineTable *LT = &Pos.first->second;
460 if (Pos.second) {
Paul Robinsonbf750c82018-01-29 20:57:43 +0000461 if (!LT->parse(DebugLineData, &Offset, Ctx, U))
Craig Topper2617dcc2014-04-15 06:32:26 +0000462 return nullptr;
Benjamin Kramer679e1752011-09-15 20:43:18 +0000463 }
Alexey Samsonov110d5952014-04-30 00:09:19 +0000464 return LT;
Benjamin Kramer679e1752011-09-15 20:43:18 +0000465}
466
Paul Robinson511b54c2017-11-22 15:48:30 +0000467bool DWARFDebugLine::LineTable::parse(DWARFDataExtractor &DebugLineData,
Paul Robinsonbf750c82018-01-29 20:57:43 +0000468 uint32_t *OffsetPtr,
469 const DWARFContext &Ctx,
470 const DWARFUnit *U, raw_ostream *OS) {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000471 const uint32_t DebugLineOffset = *OffsetPtr;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000472
Alexey Samsonov110d5952014-04-30 00:09:19 +0000473 clear();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000474
Paul Robinsonbf750c82018-01-29 20:57:43 +0000475 if (!Prologue.parse(DebugLineData, OffsetPtr, Ctx, U)) {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000476 // Restore our offset and return false to indicate failure!
Paul Robinson9d4eb692017-05-01 23:27:55 +0000477 *OffsetPtr = DebugLineOffset;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000478 return false;
479 }
480
Paul Robinson0a227092018-02-05 20:43:15 +0000481 if (OS) {
482 // The presence of OS signals verbose dumping.
483 DIDumpOptions DumpOptions;
484 DumpOptions.Verbose = true;
485 Prologue.dump(*OS, DumpOptions);
486 }
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000487
Paul Robinson9d4eb692017-05-01 23:27:55 +0000488 const uint32_t EndOffset =
489 DebugLineOffset + Prologue.TotalLength + Prologue.sizeofTotalLength();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000490
Paul Robinson511b54c2017-11-22 15:48:30 +0000491 // See if we should tell the data extractor the address size.
492 if (DebugLineData.getAddressSize() == 0)
493 DebugLineData.setAddressSize(Prologue.getAddressSize());
494 else
495 assert(Prologue.getAddressSize() == 0 ||
496 Prologue.getAddressSize() == DebugLineData.getAddressSize());
497
Alexey Samsonov110d5952014-04-30 00:09:19 +0000498 ParsingState State(this);
Benjamin Kramer112ec172011-09-15 21:59:13 +0000499
Paul Robinson9d4eb692017-05-01 23:27:55 +0000500 while (*OffsetPtr < EndOffset) {
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000501 if (OS)
502 *OS << format("0x%08.08" PRIx32 ": ", *OffsetPtr);
503
Paul Robinson9d4eb692017-05-01 23:27:55 +0000504 uint8_t Opcode = DebugLineData.getU8(OffsetPtr);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000505
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000506 if (OS)
507 *OS << format("%02.02" PRIx8 " ", Opcode);
508
Paul Robinson9d4eb692017-05-01 23:27:55 +0000509 if (Opcode == 0) {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000510 // Extended Opcodes always start with a zero opcode followed by
511 // a uleb128 length so you can skip ones you don't know about
Paul Robinson9d4eb692017-05-01 23:27:55 +0000512 uint64_t Len = DebugLineData.getULEB128(OffsetPtr);
Paul Robinsone0833342017-11-22 15:14:49 +0000513 uint32_t ExtOffset = *OffsetPtr;
514
515 // Tolerate zero-length; assume length is correct and soldier on.
516 if (Len == 0) {
517 if (OS)
518 *OS << "Badly formed extended line op (length 0)\n";
519 continue;
520 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000521
Paul Robinson9d4eb692017-05-01 23:27:55 +0000522 uint8_t SubOpcode = DebugLineData.getU8(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000523 if (OS)
524 *OS << LNExtendedString(SubOpcode);
Paul Robinson9d4eb692017-05-01 23:27:55 +0000525 switch (SubOpcode) {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000526 case DW_LNE_end_sequence:
527 // Set the end_sequence register of the state machine to true and
528 // append a row to the matrix using the current values of the
529 // state-machine registers. Then reset the registers to the initial
530 // values specified above. Every statement program sequence must end
531 // with a DW_LNE_end_sequence instruction which creates a row whose
532 // address is that of the byte after the last target machine instruction
533 // of the sequence.
Alexey Samsonov110d5952014-04-30 00:09:19 +0000534 State.Row.EndSequence = true;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000535 State.appendRowToMatrix(*OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000536 if (OS) {
537 *OS << "\n";
538 OS->indent(12);
539 State.Row.dump(*OS);
540 }
Alexey Samsonov110d5952014-04-30 00:09:19 +0000541 State.resetRowAndSequence();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000542 break;
543
544 case DW_LNE_set_address:
545 // Takes a single relocatable address as an operand. The size of the
546 // operand is the size appropriate to hold an address on the target
547 // machine. Set the address register to the value given by the
548 // relocatable address. All of the other statement program opcodes
549 // that affect the address register add a delta to it. This instruction
550 // stores a relocatable value into it instead.
Paul Robinson511b54c2017-11-22 15:48:30 +0000551 //
552 // Make sure the extractor knows the address size. If not, infer it
553 // from the size of the operand.
554 if (DebugLineData.getAddressSize() == 0)
555 DebugLineData.setAddressSize(Len - 1);
Paul Robinson79474682018-03-22 19:37:56 +0000556 else if (DebugLineData.getAddressSize() != Len - 1) {
557 fprintf(stderr, "Mismatching address size at offset 0x%8.8" PRIx32
Paul Robinson82e48642018-03-26 19:55:01 +0000558 " expected 0x%2.2" PRIx8 " found 0x%2.2" PRIx64 "\n",
Paul Robinson79474682018-03-22 19:37:56 +0000559 ExtOffset, DebugLineData.getAddressSize(), Len - 1);
560 // Skip the rest of the line-number program.
561 *OffsetPtr = EndOffset;
562 return false;
563 }
Paul Robinson17536b92017-06-29 16:52:08 +0000564 State.Row.Address = DebugLineData.getRelocatedAddress(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000565 if (OS)
566 *OS << format(" (0x%16.16" PRIx64 ")", State.Row.Address);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000567 break;
568
569 case DW_LNE_define_file:
570 // Takes 4 arguments. The first is a null terminated string containing
571 // a source file name. The second is an unsigned LEB128 number
572 // representing the directory index of the directory in which the file
573 // was found. The third is an unsigned LEB128 number representing the
574 // time of last modification of the file. The fourth is an unsigned
575 // LEB128 number representing the length in bytes of the file. The time
576 // and length fields may contain LEB128(0) if the information is not
577 // available.
578 //
579 // The directory index represents an entry in the include_directories
580 // section of the statement program prologue. The index is LEB128(0)
581 // if the file was found in the current directory of the compilation,
582 // LEB128(1) if it was found in the first directory in the
583 // include_directories section, and so on. The directory index is
584 // ignored for file names that represent full path names.
585 //
586 // The files are numbered, starting at 1, in the order in which they
587 // appear; the names in the prologue come before names defined by
588 // the DW_LNE_define_file instruction. These numbers are used in the
589 // the file register of the state machine.
590 {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000591 FileNameEntry FileEntry;
Paul Robinson0a227092018-02-05 20:43:15 +0000592 const char *Name = DebugLineData.getCStr(OffsetPtr);
593 FileEntry.Name.setForm(dwarf::DW_FORM_string);
594 FileEntry.Name.setPValue(Name);
Paul Robinson9d4eb692017-05-01 23:27:55 +0000595 FileEntry.DirIdx = DebugLineData.getULEB128(OffsetPtr);
596 FileEntry.ModTime = DebugLineData.getULEB128(OffsetPtr);
597 FileEntry.Length = DebugLineData.getULEB128(OffsetPtr);
598 Prologue.FileNames.push_back(FileEntry);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000599 if (OS)
Paul Robinson0a227092018-02-05 20:43:15 +0000600 *OS << " (" << Name << ", dir=" << FileEntry.DirIdx << ", mod_time="
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000601 << format("(0x%16.16" PRIx64 ")", FileEntry.ModTime)
602 << ", length=" << FileEntry.Length << ")";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000603 }
604 break;
605
Diego Novillo5b5cf502014-02-14 19:27:53 +0000606 case DW_LNE_set_discriminator:
Paul Robinson9d4eb692017-05-01 23:27:55 +0000607 State.Row.Discriminator = DebugLineData.getULEB128(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000608 if (OS)
609 *OS << " (" << State.Row.Discriminator << ")";
Diego Novillo5b5cf502014-02-14 19:27:53 +0000610 break;
611
Benjamin Kramer5acab502011-09-15 02:12:05 +0000612 default:
Paul Robinsone0833342017-11-22 15:14:49 +0000613 if (OS)
614 *OS << format("Unrecognized extended op 0x%02.02" PRIx8, SubOpcode)
615 << format(" length %" PRIx64, Len);
616 // Len doesn't include the zero opcode byte or the length itself, but
617 // it does include the sub_opcode, so we have to adjust for that.
618 (*OffsetPtr) += Len - 1;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000619 break;
620 }
Paul Robinsone0833342017-11-22 15:14:49 +0000621 // Make sure the stated and parsed lengths are the same.
622 // Otherwise we have an unparseable line-number program.
623 if (*OffsetPtr - ExtOffset != Len) {
624 fprintf(stderr, "Unexpected line op length at offset 0x%8.8" PRIx32
625 " expected 0x%2.2" PRIx64 " found 0x%2.2" PRIx32 "\n",
626 ExtOffset, Len, *OffsetPtr - ExtOffset);
627 // Skip the rest of the line-number program.
628 *OffsetPtr = EndOffset;
629 return false;
630 }
Paul Robinson9d4eb692017-05-01 23:27:55 +0000631 } else if (Opcode < Prologue.OpcodeBase) {
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000632 if (OS)
633 *OS << LNStandardString(Opcode);
Paul Robinson9d4eb692017-05-01 23:27:55 +0000634 switch (Opcode) {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000635 // Standard Opcodes
636 case DW_LNS_copy:
637 // Takes no arguments. Append a row to the matrix using the
638 // current values of the state-machine registers. Then set
639 // the basic_block register to false.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000640 State.appendRowToMatrix(*OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000641 if (OS) {
642 *OS << "\n";
643 OS->indent(12);
644 State.Row.dump(*OS);
645 *OS << "\n";
646 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000647 break;
648
649 case DW_LNS_advance_pc:
650 // Takes a single unsigned LEB128 operand, multiplies it by the
651 // min_inst_length field of the prologue, and adds the
652 // result to the address register of the state machine.
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000653 {
654 uint64_t AddrOffset =
655 DebugLineData.getULEB128(OffsetPtr) * Prologue.MinInstLength;
656 State.Row.Address += AddrOffset;
657 if (OS)
658 *OS << " (" << AddrOffset << ")";
659 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000660 break;
661
662 case DW_LNS_advance_line:
663 // Takes a single signed LEB128 operand and adds that value to
664 // the line register of the state machine.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000665 State.Row.Line += DebugLineData.getSLEB128(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000666 if (OS)
667 *OS << " (" << State.Row.Line << ")";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000668 break;
669
670 case DW_LNS_set_file:
671 // Takes a single unsigned LEB128 operand and stores it in the file
672 // register of the state machine.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000673 State.Row.File = DebugLineData.getULEB128(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000674 if (OS)
675 *OS << " (" << State.Row.File << ")";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000676 break;
677
678 case DW_LNS_set_column:
679 // Takes a single unsigned LEB128 operand and stores it in the
680 // column register of the state machine.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000681 State.Row.Column = DebugLineData.getULEB128(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000682 if (OS)
683 *OS << " (" << State.Row.Column << ")";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000684 break;
685
686 case DW_LNS_negate_stmt:
687 // Takes no arguments. Set the is_stmt register of the state
688 // machine to the logical negation of its current value.
Alexey Samsonov110d5952014-04-30 00:09:19 +0000689 State.Row.IsStmt = !State.Row.IsStmt;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000690 break;
691
692 case DW_LNS_set_basic_block:
693 // Takes no arguments. Set the basic_block register of the
694 // state machine to true
Alexey Samsonov110d5952014-04-30 00:09:19 +0000695 State.Row.BasicBlock = true;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000696 break;
697
698 case DW_LNS_const_add_pc:
699 // Takes no arguments. Add to the address register of the state
700 // machine the address increment value corresponding to special
701 // opcode 255. The motivation for DW_LNS_const_add_pc is this:
702 // when the statement program needs to advance the address by a
703 // small amount, it can use a single special opcode, which occupies
704 // a single byte. When it needs to advance the address by up to
705 // twice the range of the last special opcode, it can use
706 // DW_LNS_const_add_pc followed by a special opcode, for a total
707 // of two bytes. Only if it needs to advance the address by more
708 // than twice that range will it need to use both DW_LNS_advance_pc
709 // and a special opcode, requiring three or more bytes.
710 {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000711 uint8_t AdjustOpcode = 255 - Prologue.OpcodeBase;
712 uint64_t AddrOffset =
713 (AdjustOpcode / Prologue.LineRange) * Prologue.MinInstLength;
714 State.Row.Address += AddrOffset;
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000715 if (OS)
716 *OS
717 << format(" (0x%16.16" PRIx64 ")", AddrOffset);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000718 }
719 break;
720
721 case DW_LNS_fixed_advance_pc:
722 // Takes a single uhalf operand. Add to the address register of
723 // the state machine the value of the (unencoded) operand. This
724 // is the only extended opcode that takes an argument that is not
725 // a variable length number. The motivation for DW_LNS_fixed_advance_pc
726 // is this: existing assemblers cannot emit DW_LNS_advance_pc or
727 // special opcodes because they cannot encode LEB128 numbers or
728 // judge when the computation of a special opcode overflows and
729 // requires the use of DW_LNS_advance_pc. Such assemblers, however,
730 // can use DW_LNS_fixed_advance_pc instead, sacrificing compression.
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000731 {
732 uint16_t PCOffset = DebugLineData.getU16(OffsetPtr);
733 State.Row.Address += PCOffset;
734 if (OS)
735 *OS
736 << format(" (0x%16.16" PRIx64 ")", PCOffset);
737 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000738 break;
739
740 case DW_LNS_set_prologue_end:
741 // Takes no arguments. Set the prologue_end register of the
742 // state machine to true
Alexey Samsonov110d5952014-04-30 00:09:19 +0000743 State.Row.PrologueEnd = true;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000744 break;
745
746 case DW_LNS_set_epilogue_begin:
747 // Takes no arguments. Set the basic_block register of the
748 // state machine to true
Alexey Samsonov110d5952014-04-30 00:09:19 +0000749 State.Row.EpilogueBegin = true;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000750 break;
751
752 case DW_LNS_set_isa:
753 // Takes a single unsigned LEB128 operand and stores it in the
754 // column register of the state machine.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000755 State.Row.Isa = DebugLineData.getULEB128(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000756 if (OS)
757 *OS << " (" << State.Row.Isa << ")";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000758 break;
759
760 default:
761 // Handle any unknown standard opcodes here. We know the lengths
762 // of such opcodes because they are specified in the prologue
763 // as a multiple of LEB128 operands for each opcode.
764 {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000765 assert(Opcode - 1U < Prologue.StandardOpcodeLengths.size());
766 uint8_t OpcodeLength = Prologue.StandardOpcodeLengths[Opcode - 1];
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000767 for (uint8_t I = 0; I < OpcodeLength; ++I) {
768 uint64_t Value = DebugLineData.getULEB128(OffsetPtr);
769 if (OS)
770 *OS << format("Skipping ULEB128 value: 0x%16.16" PRIx64 ")\n",
771 Value);
772 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000773 }
774 break;
775 }
776 } else {
777 // Special Opcodes
778
779 // A special opcode value is chosen based on the amount that needs
780 // to be added to the line and address registers. The maximum line
781 // increment for a special opcode is the value of the line_base
782 // field in the header, plus the value of the line_range field,
783 // minus 1 (line base + line range - 1). If the desired line
784 // increment is greater than the maximum line increment, a standard
NAKAMURA Takumif9959852011-10-08 11:22:47 +0000785 // opcode must be used instead of a special opcode. The "address
786 // advance" is calculated by dividing the desired address increment
Benjamin Kramer5acab502011-09-15 02:12:05 +0000787 // by the minimum_instruction_length field from the header. The
788 // special opcode is then calculated using the following formula:
789 //
790 // opcode = (desired line increment - line_base) +
791 // (line_range * address advance) + opcode_base
792 //
793 // If the resulting opcode is greater than 255, a standard opcode
794 // must be used instead.
795 //
796 // To decode a special opcode, subtract the opcode_base from the
797 // opcode itself to give the adjusted opcode. The amount to
798 // increment the address register is the result of the adjusted
799 // opcode divided by the line_range multiplied by the
800 // minimum_instruction_length field from the header. That is:
801 //
802 // address increment = (adjusted opcode / line_range) *
803 // minimum_instruction_length
804 //
805 // The amount to increment the line register is the line_base plus
806 // the result of the adjusted opcode modulo the line_range. That is:
807 //
808 // line increment = line_base + (adjusted opcode % line_range)
809
Paul Robinson9d4eb692017-05-01 23:27:55 +0000810 uint8_t AdjustOpcode = Opcode - Prologue.OpcodeBase;
811 uint64_t AddrOffset =
812 (AdjustOpcode / Prologue.LineRange) * Prologue.MinInstLength;
813 int32_t LineOffset =
814 Prologue.LineBase + (AdjustOpcode % Prologue.LineRange);
815 State.Row.Line += LineOffset;
816 State.Row.Address += AddrOffset;
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000817
818 if (OS) {
819 *OS << "address += " << ((uint32_t)AdjustOpcode)
820 << ", line += " << LineOffset << "\n";
821 OS->indent(12);
822 State.Row.dump(*OS);
823 }
824
Paul Robinson9d4eb692017-05-01 23:27:55 +0000825 State.appendRowToMatrix(*OffsetPtr);
Dehao Chen1b54fce2016-04-28 22:09:37 +0000826 // Reset discriminator to 0.
827 State.Row.Discriminator = 0;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000828 }
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000829 if(OS)
830 *OS << "\n";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000831 }
832
Jonas Devlieghere84e99262018-04-14 22:07:23 +0000833 if (!State.Sequence.Empty)
834 WithColor::warning() << "last sequence in debug line table is not"
835 "terminated!\n";
Alexey Samsonov110d5952014-04-30 00:09:19 +0000836
837 // Sort all sequences so that address lookup will work faster.
838 if (!Sequences.empty()) {
Mandeep Singh Grangfe1d28e2018-04-01 16:18:49 +0000839 llvm::sort(Sequences.begin(), Sequences.end(), Sequence::orderByLowPC);
Alexey Samsonov110d5952014-04-30 00:09:19 +0000840 // Note: actually, instruction address ranges of sequences should not
841 // overlap (in shared objects and executables). If they do, the address
842 // lookup would still work, though, but result would be ambiguous.
843 // We don't report warning in this case. For example,
844 // sometimes .so compiled from multiple object files contains a few
845 // rudimentary sequences for address ranges [0x0, 0xsomething).
846 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000847
Paul Robinson9d4eb692017-05-01 23:27:55 +0000848 return EndOffset;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000849}
850
Keno Fischerc2c60182015-05-31 23:37:04 +0000851uint32_t
Paul Robinson9d4eb692017-05-01 23:27:55 +0000852DWARFDebugLine::LineTable::findRowInSeq(const DWARFDebugLine::Sequence &Seq,
853 uint64_t Address) const {
854 if (!Seq.containsPC(Address))
Keno Fischerc2c60182015-05-31 23:37:04 +0000855 return UnknownRowIndex;
856 // Search for instruction address in the rows describing the sequence.
857 // Rows are stored in a vector, so we may use arithmetical operations with
858 // iterators.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000859 DWARFDebugLine::Row Row;
860 Row.Address = Address;
861 RowIter FirstRow = Rows.begin() + Seq.FirstRowIndex;
862 RowIter LastRow = Rows.begin() + Seq.LastRowIndex;
863 LineTable::RowIter RowPos = std::lower_bound(
864 FirstRow, LastRow, Row, DWARFDebugLine::Row::orderByAddress);
865 if (RowPos == LastRow) {
866 return Seq.LastRowIndex - 1;
Keno Fischerc2c60182015-05-31 23:37:04 +0000867 }
Paul Robinson9d4eb692017-05-01 23:27:55 +0000868 uint32_t Index = Seq.FirstRowIndex + (RowPos - FirstRow);
869 if (RowPos->Address > Address) {
870 if (RowPos == FirstRow)
Keno Fischerc2c60182015-05-31 23:37:04 +0000871 return UnknownRowIndex;
872 else
Paul Robinson9d4eb692017-05-01 23:27:55 +0000873 Index--;
Keno Fischerc2c60182015-05-31 23:37:04 +0000874 }
Paul Robinson9d4eb692017-05-01 23:27:55 +0000875 return Index;
Keno Fischerc2c60182015-05-31 23:37:04 +0000876}
877
Paul Robinson9d4eb692017-05-01 23:27:55 +0000878uint32_t DWARFDebugLine::LineTable::lookupAddress(uint64_t Address) const {
Alexey Samsonov947228c2012-08-07 11:46:57 +0000879 if (Sequences.empty())
Keno Fischerc2c60182015-05-31 23:37:04 +0000880 return UnknownRowIndex;
Alexey Samsonov947228c2012-08-07 11:46:57 +0000881 // First, find an instruction sequence containing the given address.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000882 DWARFDebugLine::Sequence Sequence;
883 Sequence.LowPC = Address;
884 SequenceIter FirstSeq = Sequences.begin();
885 SequenceIter LastSeq = Sequences.end();
886 SequenceIter SeqPos = std::lower_bound(
887 FirstSeq, LastSeq, Sequence, DWARFDebugLine::Sequence::orderByLowPC);
888 DWARFDebugLine::Sequence FoundSeq;
889 if (SeqPos == LastSeq) {
890 FoundSeq = Sequences.back();
891 } else if (SeqPos->LowPC == Address) {
892 FoundSeq = *SeqPos;
Alexey Samsonov947228c2012-08-07 11:46:57 +0000893 } else {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000894 if (SeqPos == FirstSeq)
Keno Fischerc2c60182015-05-31 23:37:04 +0000895 return UnknownRowIndex;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000896 FoundSeq = *(SeqPos - 1);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000897 }
Paul Robinson9d4eb692017-05-01 23:27:55 +0000898 return findRowInSeq(FoundSeq, Address);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000899}
Alexey Samsonov45be7932012-08-30 07:49:50 +0000900
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000901bool DWARFDebugLine::LineTable::lookupAddressRange(
Paul Robinson9d4eb692017-05-01 23:27:55 +0000902 uint64_t Address, uint64_t Size, std::vector<uint32_t> &Result) const {
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000903 if (Sequences.empty())
904 return false;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000905 uint64_t EndAddr = Address + Size;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000906 // First, find an instruction sequence containing the given address.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000907 DWARFDebugLine::Sequence Sequence;
908 Sequence.LowPC = Address;
909 SequenceIter FirstSeq = Sequences.begin();
910 SequenceIter LastSeq = Sequences.end();
911 SequenceIter SeqPos = std::lower_bound(
912 FirstSeq, LastSeq, Sequence, DWARFDebugLine::Sequence::orderByLowPC);
913 if (SeqPos == LastSeq || SeqPos->LowPC != Address) {
914 if (SeqPos == FirstSeq)
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000915 return false;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000916 SeqPos--;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000917 }
Paul Robinson9d4eb692017-05-01 23:27:55 +0000918 if (!SeqPos->containsPC(Address))
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000919 return false;
920
Paul Robinson9d4eb692017-05-01 23:27:55 +0000921 SequenceIter StartPos = SeqPos;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000922
923 // Add the rows from the first sequence to the vector, starting with the
924 // index we just calculated
925
Paul Robinson9d4eb692017-05-01 23:27:55 +0000926 while (SeqPos != LastSeq && SeqPos->LowPC < EndAddr) {
927 const DWARFDebugLine::Sequence &CurSeq = *SeqPos;
Keno Fischerc2c60182015-05-31 23:37:04 +0000928 // For the first sequence, we need to find which row in the sequence is the
929 // first in our range.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000930 uint32_t FirstRowIndex = CurSeq.FirstRowIndex;
931 if (SeqPos == StartPos)
932 FirstRowIndex = findRowInSeq(CurSeq, Address);
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000933
Keno Fischerc2c60182015-05-31 23:37:04 +0000934 // Figure out the last row in the range.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000935 uint32_t LastRowIndex = findRowInSeq(CurSeq, EndAddr - 1);
936 if (LastRowIndex == UnknownRowIndex)
937 LastRowIndex = CurSeq.LastRowIndex - 1;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000938
Paul Robinson9d4eb692017-05-01 23:27:55 +0000939 assert(FirstRowIndex != UnknownRowIndex);
940 assert(LastRowIndex != UnknownRowIndex);
Keno Fischerc2c60182015-05-31 23:37:04 +0000941
Paul Robinson9d4eb692017-05-01 23:27:55 +0000942 for (uint32_t I = FirstRowIndex; I <= LastRowIndex; ++I) {
943 Result.push_back(I);
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000944 }
945
Paul Robinson9d4eb692017-05-01 23:27:55 +0000946 ++SeqPos;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000947 }
NAKAMURA Takumi4b86cdb2013-01-26 01:45:06 +0000948
949 return true;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000950}
951
Paul Robinson9d4eb692017-05-01 23:27:55 +0000952bool DWARFDebugLine::LineTable::hasFileAtIndex(uint64_t FileIndex) const {
Pete Cooperb2ba7762016-07-22 01:41:32 +0000953 return FileIndex != 0 && FileIndex <= Prologue.FileNames.size();
954}
955
Scott Linder16c7bda2018-02-23 23:01:06 +0000956Optional<StringRef> DWARFDebugLine::LineTable::getSourceByIndex(uint64_t FileIndex,
957 FileLineInfoKind Kind) const {
958 if (Kind == FileLineInfoKind::None || !hasFileAtIndex(FileIndex))
959 return None;
960 const FileNameEntry &Entry = Prologue.FileNames[FileIndex - 1];
961 if (Optional<const char *> source = Entry.Source.getAsCString())
962 return StringRef(*source);
963 return None;
964}
965
Eugene Zemtsov82d60d62018-03-13 17:54:29 +0000966static bool isPathAbsoluteOnWindowsOrPosix(const Twine &Path) {
967 // Debug info can contain paths from any OS, not necessarily
968 // an OS we're currently running on. Moreover different compilation units can
969 // be compiled on different operating systems and linked together later.
970 return sys::path::is_absolute(Path, sys::path::Style::posix) ||
971 sys::path::is_absolute(Path, sys::path::Style::windows);
972}
973
Paul Robinson9d4eb692017-05-01 23:27:55 +0000974bool DWARFDebugLine::LineTable::getFileNameByIndex(uint64_t FileIndex,
975 const char *CompDir,
976 FileLineInfoKind Kind,
977 std::string &Result) const {
Pete Cooperb2ba7762016-07-22 01:41:32 +0000978 if (Kind == FileLineInfoKind::None || !hasFileAtIndex(FileIndex))
Alexey Samsonov45be7932012-08-30 07:49:50 +0000979 return false;
980 const FileNameEntry &Entry = Prologue.FileNames[FileIndex - 1];
Paul Robinson0a227092018-02-05 20:43:15 +0000981 StringRef FileName = Entry.Name.getAsCString().getValue();
Alexey Samsonovdce67342014-05-15 21:24:32 +0000982 if (Kind != FileLineInfoKind::AbsoluteFilePath ||
Eugene Zemtsov82d60d62018-03-13 17:54:29 +0000983 isPathAbsoluteOnWindowsOrPosix(FileName)) {
Alexey Samsonov45be7932012-08-30 07:49:50 +0000984 Result = FileName;
985 return true;
986 }
Frederic Riss101b5e22014-09-19 15:11:51 +0000987
Alexey Samsonov45be7932012-08-30 07:49:50 +0000988 SmallString<16> FilePath;
989 uint64_t IncludeDirIndex = Entry.DirIdx;
Paul Robinsonba1c9152017-05-02 17:37:32 +0000990 StringRef IncludeDir;
Alexey Samsonov45be7932012-08-30 07:49:50 +0000991 // Be defensive about the contents of Entry.
992 if (IncludeDirIndex > 0 &&
Frederic Riss101b5e22014-09-19 15:11:51 +0000993 IncludeDirIndex <= Prologue.IncludeDirectories.size())
Paul Robinson0a227092018-02-05 20:43:15 +0000994 IncludeDir = Prologue.IncludeDirectories[IncludeDirIndex - 1]
995 .getAsCString()
996 .getValue();
Frederic Riss101b5e22014-09-19 15:11:51 +0000997
998 // We may still need to append compilation directory of compile unit.
999 // We know that FileName is not absolute, the only way to have an
1000 // absolute path at this point would be if IncludeDir is absolute.
1001 if (CompDir && Kind == FileLineInfoKind::AbsoluteFilePath &&
Eugene Zemtsov82d60d62018-03-13 17:54:29 +00001002 !isPathAbsoluteOnWindowsOrPosix(IncludeDir))
Frederic Riss101b5e22014-09-19 15:11:51 +00001003 sys::path::append(FilePath, CompDir);
1004
1005 // sys::path::append skips empty strings.
1006 sys::path::append(FilePath, IncludeDir, FileName);
Alexey Samsonov45be7932012-08-30 07:49:50 +00001007 Result = FilePath.str();
1008 return true;
1009}
Frederic Riss101b5e22014-09-19 15:11:51 +00001010
Dehao Chen1b54fce2016-04-28 22:09:37 +00001011bool DWARFDebugLine::LineTable::getFileLineInfoForAddress(
1012 uint64_t Address, const char *CompDir, FileLineInfoKind Kind,
1013 DILineInfo &Result) const {
Frederic Riss101b5e22014-09-19 15:11:51 +00001014 // Get the index of row we're looking for in the line table.
1015 uint32_t RowIndex = lookupAddress(Address);
1016 if (RowIndex == -1U)
1017 return false;
1018 // Take file number and line/column from the row.
1019 const auto &Row = Rows[RowIndex];
1020 if (!getFileNameByIndex(Row.File, CompDir, Kind, Result.FileName))
1021 return false;
1022 Result.Line = Row.Line;
1023 Result.Column = Row.Column;
Eric Christopherba1024c2016-12-14 18:29:39 +00001024 Result.Discriminator = Row.Discriminator;
Scott Linder16c7bda2018-02-23 23:01:06 +00001025 Result.Source = getSourceByIndex(Row.File, Kind);
Frederic Riss101b5e22014-09-19 15:11:51 +00001026 return true;
1027}