blob: cba21b07d4c687f7adfc710a7106d003234f5f1b [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"
Benjamin Kramer5acab502011-09-15 02:12:05 +000020#include "llvm/Support/raw_ostream.h"
Benjamin Kramera57c46a2011-09-15 02:19:33 +000021#include <algorithm>
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000022#include <cassert>
23#include <cinttypes>
24#include <cstdint>
25#include <cstdio>
26#include <utility>
27
Benjamin Kramer5acab502011-09-15 02:12:05 +000028using namespace llvm;
29using namespace dwarf;
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000030
Eugene Zelenko2db0cfa2017-06-23 21:57:40 +000031using FileLineInfoKind = DILineInfoSpecifier::FileLineInfoKind;
32
Paul Robinson2bc38732017-05-02 21:40:47 +000033namespace {
Eugene Zelenko2db0cfa2017-06-23 21:57:40 +000034
Paul Robinson2bc38732017-05-02 21:40:47 +000035struct ContentDescriptor {
36 dwarf::LineNumberEntryFormat Type;
37 dwarf::Form Form;
38};
Eugene Zelenko2db0cfa2017-06-23 21:57:40 +000039
40using ContentDescriptors = SmallVector<ContentDescriptor, 4>;
41
Paul Robinson2bc38732017-05-02 21:40:47 +000042} // end anonmyous namespace
Benjamin Kramer5acab502011-09-15 02:12:05 +000043
Scott Linder16c7bda2018-02-23 23:01:06 +000044void DWARFDebugLine::ContentTypeTracker::trackContentType(
45 dwarf::LineNumberEntryFormat ContentType) {
46 switch (ContentType) {
47 case dwarf::DW_LNCT_timestamp:
48 HasModTime = true;
49 break;
50 case dwarf::DW_LNCT_size:
51 HasLength = true;
52 break;
53 case dwarf::DW_LNCT_MD5:
54 HasMD5 = true;
55 break;
56 case dwarf::DW_LNCT_LLVM_source:
57 HasSource = true;
58 break;
59 default:
60 // We only care about values we consider optional, and new values may be
61 // added in the vendor extension range, so we do not match exhaustively.
62 break;
63 }
64}
65
Dehao Chen1b54fce2016-04-28 22:09:37 +000066DWARFDebugLine::Prologue::Prologue() { clear(); }
Alexey Samsonov836b1ae2014-04-29 21:28:13 +000067
68void DWARFDebugLine::Prologue::clear() {
Paul Robinson75c068c2017-06-26 18:43:01 +000069 TotalLength = PrologueLength = 0;
70 SegSelectorSize = 0;
Alexey Samsonov836b1ae2014-04-29 21:28:13 +000071 MinInstLength = MaxOpsPerInst = DefaultIsStmt = LineBase = LineRange = 0;
72 OpcodeBase = 0;
Pavel Labath322711f2018-03-14 09:39:54 +000073 FormParams = dwarf::FormParams({0, 0, DWARF32});
Scott Linder16c7bda2018-02-23 23:01:06 +000074 ContentTypes = ContentTypeTracker();
Alexey Samsonov836b1ae2014-04-29 21:28:13 +000075 StandardOpcodeLengths.clear();
76 IncludeDirectories.clear();
77 FileNames.clear();
78}
79
Paul Robinson0a227092018-02-05 20:43:15 +000080void DWARFDebugLine::Prologue::dump(raw_ostream &OS,
81 DIDumpOptions DumpOptions) const {
Benjamin Kramer5acab502011-09-15 02:12:05 +000082 OS << "Line table prologue:\n"
Ed Maste6d0bee52015-05-28 15:38:17 +000083 << format(" total_length: 0x%8.8" PRIx64 "\n", TotalLength)
Paul Robinson75c068c2017-06-26 18:43:01 +000084 << format(" version: %u\n", getVersion());
85 if (getVersion() >= 5)
86 OS << format(" address_size: %u\n", getAddressSize())
87 << format(" seg_select_size: %u\n", SegSelectorSize);
88 OS << format(" prologue_length: 0x%8.8" PRIx64 "\n", PrologueLength)
David Blaikie1d4736e2014-02-24 23:58:54 +000089 << format(" min_inst_length: %u\n", MinInstLength)
Paul Robinson75c068c2017-06-26 18:43:01 +000090 << format(getVersion() >= 4 ? "max_ops_per_inst: %u\n" : "", MaxOpsPerInst)
David Blaikie1d4736e2014-02-24 23:58:54 +000091 << format(" default_is_stmt: %u\n", DefaultIsStmt)
92 << format(" line_base: %i\n", LineBase)
93 << format(" line_range: %u\n", LineRange)
94 << format(" opcode_base: %u\n", OpcodeBase);
Benjamin Kramer5acab502011-09-15 02:12:05 +000095
Paul Robinson9d4eb692017-05-01 23:27:55 +000096 for (uint32_t I = 0; I != StandardOpcodeLengths.size(); ++I)
Mehdi Amini149f6ea2016-10-05 05:59:29 +000097 OS << format("standard_opcode_lengths[%s] = %u\n",
Paul Robinson9d4eb692017-05-01 23:27:55 +000098 LNStandardString(I + 1).data(), StandardOpcodeLengths[I]);
Benjamin Kramer5acab502011-09-15 02:12:05 +000099
Paul Robinson8181d232018-01-18 20:33:35 +0000100 if (!IncludeDirectories.empty()) {
101 // DWARF v5 starts directory indexes at 0.
102 uint32_t DirBase = getVersion() >= 5 ? 0 : 1;
Paul Robinson0a227092018-02-05 20:43:15 +0000103 for (uint32_t I = 0; I != IncludeDirectories.size(); ++I) {
104 OS << format("include_directories[%3u] = ", I + DirBase);
105 IncludeDirectories[I].dump(OS, DumpOptions);
106 OS << '\n';
107 }
Paul Robinson8181d232018-01-18 20:33:35 +0000108 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000109
110 if (!FileNames.empty()) {
Paul Robinsonceafcd42018-02-08 23:08:02 +0000111 // DWARF v5 starts file indexes at 0.
112 uint32_t FileBase = getVersion() >= 5 ? 0 : 1;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000113 for (uint32_t I = 0; I != FileNames.size(); ++I) {
114 const FileNameEntry &FileEntry = FileNames[I];
Scott Linder16c7bda2018-02-23 23:01:06 +0000115 OS << format("file_names[%3u]:\n", I + FileBase);
116 OS << " name: ";
Paul Robinson0a227092018-02-05 20:43:15 +0000117 FileEntry.Name.dump(OS, DumpOptions);
Scott Linder16c7bda2018-02-23 23:01:06 +0000118 OS << '\n'
119 << format(" dir_index: %" PRIu64 "\n", FileEntry.DirIdx);
120 if (ContentTypes.HasMD5)
121 OS << " md5_checksum: " << FileEntry.Checksum.digest() << '\n';
122 if (ContentTypes.HasModTime)
123 OS << format(" mod_time: 0x%8.8" PRIx64 "\n", FileEntry.ModTime);
124 if (ContentTypes.HasLength)
125 OS << format(" length: 0x%8.8" PRIx64 "\n", FileEntry.Length);
126 if (ContentTypes.HasSource) {
127 OS << " source: ";
128 FileEntry.Source.dump(OS, DumpOptions);
129 OS << '\n';
130 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000131 }
132 }
133}
134
Paul Robinson2bc38732017-05-02 21:40:47 +0000135// Parse v2-v4 directory and file tables.
136static void
Paul Robinson17536b92017-06-29 16:52:08 +0000137parseV2DirFileTables(const DWARFDataExtractor &DebugLineData,
138 uint32_t *OffsetPtr, uint64_t EndPrologueOffset,
Scott Linder16c7bda2018-02-23 23:01:06 +0000139 DWARFDebugLine::ContentTypeTracker &ContentTypes,
Paul Robinson0a227092018-02-05 20:43:15 +0000140 std::vector<DWARFFormValue> &IncludeDirectories,
Paul Robinson2bc38732017-05-02 21:40:47 +0000141 std::vector<DWARFDebugLine::FileNameEntry> &FileNames) {
142 while (*OffsetPtr < EndPrologueOffset) {
143 StringRef S = DebugLineData.getCStrRef(OffsetPtr);
144 if (S.empty())
145 break;
Paul Robinson0a227092018-02-05 20:43:15 +0000146 DWARFFormValue Dir(dwarf::DW_FORM_string);
147 Dir.setPValue(S.data());
148 IncludeDirectories.push_back(Dir);
Paul Robinson2bc38732017-05-02 21:40:47 +0000149 }
150
151 while (*OffsetPtr < EndPrologueOffset) {
152 StringRef Name = DebugLineData.getCStrRef(OffsetPtr);
153 if (Name.empty())
154 break;
155 DWARFDebugLine::FileNameEntry FileEntry;
Paul Robinson0a227092018-02-05 20:43:15 +0000156 FileEntry.Name.setForm(dwarf::DW_FORM_string);
157 FileEntry.Name.setPValue(Name.data());
Paul Robinson2bc38732017-05-02 21:40:47 +0000158 FileEntry.DirIdx = DebugLineData.getULEB128(OffsetPtr);
159 FileEntry.ModTime = DebugLineData.getULEB128(OffsetPtr);
160 FileEntry.Length = DebugLineData.getULEB128(OffsetPtr);
161 FileNames.push_back(FileEntry);
162 }
Scott Linder16c7bda2018-02-23 23:01:06 +0000163
164 ContentTypes.HasModTime = true;
165 ContentTypes.HasLength = true;
Paul Robinson2bc38732017-05-02 21:40:47 +0000166}
167
168// Parse v5 directory/file entry content descriptions.
169// Returns the descriptors, or an empty vector if we did not find a path or
170// ran off the end of the prologue.
171static ContentDescriptors
Scott Linder16c7bda2018-02-23 23:01:06 +0000172parseV5EntryFormat(const DWARFDataExtractor &DebugLineData, uint32_t
173 *OffsetPtr, uint64_t EndPrologueOffset, DWARFDebugLine::ContentTypeTracker
174 *ContentTypes) {
Paul Robinson2bc38732017-05-02 21:40:47 +0000175 ContentDescriptors Descriptors;
176 int FormatCount = DebugLineData.getU8(OffsetPtr);
177 bool HasPath = false;
178 for (int I = 0; I != FormatCount; ++I) {
179 if (*OffsetPtr >= EndPrologueOffset)
180 return ContentDescriptors();
181 ContentDescriptor Descriptor;
182 Descriptor.Type =
183 dwarf::LineNumberEntryFormat(DebugLineData.getULEB128(OffsetPtr));
184 Descriptor.Form = dwarf::Form(DebugLineData.getULEB128(OffsetPtr));
185 if (Descriptor.Type == dwarf::DW_LNCT_path)
186 HasPath = true;
Scott Linder16c7bda2018-02-23 23:01:06 +0000187 if (ContentTypes)
188 ContentTypes->trackContentType(Descriptor.Type);
Paul Robinson2bc38732017-05-02 21:40:47 +0000189 Descriptors.push_back(Descriptor);
190 }
191 return HasPath ? Descriptors : ContentDescriptors();
192}
193
194static bool
Paul Robinson17536b92017-06-29 16:52:08 +0000195parseV5DirFileTables(const DWARFDataExtractor &DebugLineData,
196 uint32_t *OffsetPtr, uint64_t EndPrologueOffset,
Pavel Labath322711f2018-03-14 09:39:54 +0000197 const dwarf::FormParams &FormParams,
198 const DWARFContext &Ctx, const DWARFUnit *U,
Scott Linder16c7bda2018-02-23 23:01:06 +0000199 DWARFDebugLine::ContentTypeTracker &ContentTypes,
Paul Robinson0a227092018-02-05 20:43:15 +0000200 std::vector<DWARFFormValue> &IncludeDirectories,
Paul Robinson2bc38732017-05-02 21:40:47 +0000201 std::vector<DWARFDebugLine::FileNameEntry> &FileNames) {
202 // Get the directory entry description.
203 ContentDescriptors DirDescriptors =
Paul Robinsona06f8dc2017-12-18 19:08:35 +0000204 parseV5EntryFormat(DebugLineData, OffsetPtr, EndPrologueOffset, nullptr);
Paul Robinson2bc38732017-05-02 21:40:47 +0000205 if (DirDescriptors.empty())
206 return false;
207
208 // Get the directory entries, according to the format described above.
209 int DirEntryCount = DebugLineData.getU8(OffsetPtr);
210 for (int I = 0; I != DirEntryCount; ++I) {
211 if (*OffsetPtr >= EndPrologueOffset)
212 return false;
213 for (auto Descriptor : DirDescriptors) {
214 DWARFFormValue Value(Descriptor.Form);
215 switch (Descriptor.Type) {
216 case DW_LNCT_path:
Paul Robinsonbf750c82018-01-29 20:57:43 +0000217 if (!Value.extractValue(DebugLineData, OffsetPtr, FormParams, &Ctx, U))
Paul Robinson2bc38732017-05-02 21:40:47 +0000218 return false;
Paul Robinson0a227092018-02-05 20:43:15 +0000219 IncludeDirectories.push_back(Value);
Paul Robinson2bc38732017-05-02 21:40:47 +0000220 break;
221 default:
Paul Robinson75c068c2017-06-26 18:43:01 +0000222 if (!Value.skipValue(DebugLineData, OffsetPtr, FormParams))
Paul Robinson2bc38732017-05-02 21:40:47 +0000223 return false;
224 }
225 }
226 }
227
228 // Get the file entry description.
229 ContentDescriptors FileDescriptors =
Scott Linder16c7bda2018-02-23 23:01:06 +0000230 parseV5EntryFormat(DebugLineData, OffsetPtr, EndPrologueOffset,
231 &ContentTypes);
Paul Robinson2bc38732017-05-02 21:40:47 +0000232 if (FileDescriptors.empty())
233 return false;
234
235 // Get the file entries, according to the format described above.
236 int FileEntryCount = DebugLineData.getU8(OffsetPtr);
237 for (int I = 0; I != FileEntryCount; ++I) {
238 if (*OffsetPtr >= EndPrologueOffset)
239 return false;
240 DWARFDebugLine::FileNameEntry FileEntry;
241 for (auto Descriptor : FileDescriptors) {
242 DWARFFormValue Value(Descriptor.Form);
Paul Robinsonbf750c82018-01-29 20:57:43 +0000243 if (!Value.extractValue(DebugLineData, OffsetPtr, FormParams, &Ctx, U))
Paul Robinson2bc38732017-05-02 21:40:47 +0000244 return false;
245 switch (Descriptor.Type) {
246 case DW_LNCT_path:
Paul Robinson0a227092018-02-05 20:43:15 +0000247 FileEntry.Name = Value;
Paul Robinson2bc38732017-05-02 21:40:47 +0000248 break;
Scott Linder16c7bda2018-02-23 23:01:06 +0000249 case DW_LNCT_LLVM_source:
250 FileEntry.Source = Value;
251 break;
Paul Robinson2bc38732017-05-02 21:40:47 +0000252 case DW_LNCT_directory_index:
253 FileEntry.DirIdx = Value.getAsUnsignedConstant().getValue();
254 break;
255 case DW_LNCT_timestamp:
256 FileEntry.ModTime = Value.getAsUnsignedConstant().getValue();
257 break;
258 case DW_LNCT_size:
259 FileEntry.Length = Value.getAsUnsignedConstant().getValue();
260 break;
Paul Robinsona06f8dc2017-12-18 19:08:35 +0000261 case DW_LNCT_MD5:
262 assert(Value.getAsBlock().getValue().size() == 16);
263 std::uninitialized_copy_n(Value.getAsBlock().getValue().begin(), 16,
264 FileEntry.Checksum.Bytes.begin());
265 break;
Paul Robinson2bc38732017-05-02 21:40:47 +0000266 default:
267 break;
268 }
269 }
270 FileNames.push_back(FileEntry);
271 }
272 return true;
273}
274
Paul Robinson17536b92017-06-29 16:52:08 +0000275bool DWARFDebugLine::Prologue::parse(const DWARFDataExtractor &DebugLineData,
Paul Robinsonbf750c82018-01-29 20:57:43 +0000276 uint32_t *OffsetPtr,
277 const DWARFContext &Ctx,
278 const DWARFUnit *U) {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000279 const uint64_t PrologueOffset = *OffsetPtr;
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000280
281 clear();
Paul Robinson9d4eb692017-05-01 23:27:55 +0000282 TotalLength = DebugLineData.getU32(OffsetPtr);
Ed Maste6d0bee52015-05-28 15:38:17 +0000283 if (TotalLength == UINT32_MAX) {
Paul Robinson75c068c2017-06-26 18:43:01 +0000284 FormParams.Format = dwarf::DWARF64;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000285 TotalLength = DebugLineData.getU64(OffsetPtr);
Paul Robinson75c068c2017-06-26 18:43:01 +0000286 } else if (TotalLength >= 0xffffff00) {
Ed Maste6d0bee52015-05-28 15:38:17 +0000287 return false;
288 }
Paul Robinson75c068c2017-06-26 18:43:01 +0000289 FormParams.Version = DebugLineData.getU16(OffsetPtr);
290 if (getVersion() < 2)
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000291 return false;
292
Paul Robinson75c068c2017-06-26 18:43:01 +0000293 if (getVersion() >= 5) {
294 FormParams.AddrSize = DebugLineData.getU8(OffsetPtr);
Paul Robinson63811a42017-11-22 15:33:17 +0000295 assert((DebugLineData.getAddressSize() == 0 ||
296 DebugLineData.getAddressSize() == getAddressSize()) &&
Paul Robinson75c068c2017-06-26 18:43:01 +0000297 "Line table header and data extractor disagree");
Paul Robinson2bc38732017-05-02 21:40:47 +0000298 SegSelectorSize = DebugLineData.getU8(OffsetPtr);
299 }
300
Paul Robinson9d4eb692017-05-01 23:27:55 +0000301 PrologueLength = DebugLineData.getUnsigned(OffsetPtr, sizeofPrologueLength());
302 const uint64_t EndPrologueOffset = PrologueLength + *OffsetPtr;
303 MinInstLength = DebugLineData.getU8(OffsetPtr);
Paul Robinson75c068c2017-06-26 18:43:01 +0000304 if (getVersion() >= 4)
Paul Robinson9d4eb692017-05-01 23:27:55 +0000305 MaxOpsPerInst = DebugLineData.getU8(OffsetPtr);
306 DefaultIsStmt = DebugLineData.getU8(OffsetPtr);
307 LineBase = DebugLineData.getU8(OffsetPtr);
308 LineRange = DebugLineData.getU8(OffsetPtr);
309 OpcodeBase = DebugLineData.getU8(OffsetPtr);
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000310
311 StandardOpcodeLengths.reserve(OpcodeBase - 1);
Paul Robinson9d4eb692017-05-01 23:27:55 +0000312 for (uint32_t I = 1; I < OpcodeBase; ++I) {
313 uint8_t OpLen = DebugLineData.getU8(OffsetPtr);
314 StandardOpcodeLengths.push_back(OpLen);
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000315 }
316
Paul Robinson75c068c2017-06-26 18:43:01 +0000317 if (getVersion() >= 5) {
Paul Robinson2bc38732017-05-02 21:40:47 +0000318 if (!parseV5DirFileTables(DebugLineData, OffsetPtr, EndPrologueOffset,
Scott Linder16c7bda2018-02-23 23:01:06 +0000319 FormParams, Ctx, U, ContentTypes,
320 IncludeDirectories, FileNames)) {
Paul Robinson2bc38732017-05-02 21:40:47 +0000321 fprintf(stderr,
322 "warning: parsing line table prologue at 0x%8.8" PRIx64
323 " found an invalid directory or file table description at"
324 " 0x%8.8" PRIx64 "\n", PrologueOffset, (uint64_t)*OffsetPtr);
325 return false;
326 }
327 } else
328 parseV2DirFileTables(DebugLineData, OffsetPtr, EndPrologueOffset,
Scott Linder16c7bda2018-02-23 23:01:06 +0000329 ContentTypes, IncludeDirectories, FileNames);
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000330
Paul Robinson9d4eb692017-05-01 23:27:55 +0000331 if (*OffsetPtr != EndPrologueOffset) {
332 fprintf(stderr,
333 "warning: parsing line table prologue at 0x%8.8" PRIx64
334 " should have ended at 0x%8.8" PRIx64
335 " but it ended at 0x%8.8" PRIx64 "\n",
336 PrologueOffset, EndPrologueOffset, (uint64_t)*OffsetPtr);
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000337 return false;
338 }
339 return true;
340}
341
Paul Robinson9d4eb692017-05-01 23:27:55 +0000342DWARFDebugLine::Row::Row(bool DefaultIsStmt) { reset(DefaultIsStmt); }
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000343
Benjamin Kramer5acab502011-09-15 02:12:05 +0000344void DWARFDebugLine::Row::postAppend() {
345 BasicBlock = false;
346 PrologueEnd = false;
347 EpilogueBegin = false;
348}
349
Paul Robinson9d4eb692017-05-01 23:27:55 +0000350void DWARFDebugLine::Row::reset(bool DefaultIsStmt) {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000351 Address = 0;
352 Line = 1;
353 Column = 0;
354 File = 1;
355 Isa = 0;
Diego Novillo5b5cf502014-02-14 19:27:53 +0000356 Discriminator = 0;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000357 IsStmt = DefaultIsStmt;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000358 BasicBlock = false;
359 EndSequence = false;
360 PrologueEnd = false;
361 EpilogueBegin = false;
362}
363
Greg Clayton67070462017-05-02 22:48:52 +0000364void DWARFDebugLine::Row::dumpTableHeader(raw_ostream &OS) {
365 OS << "Address Line Column File ISA Discriminator Flags\n"
366 << "------------------ ------ ------ ------ --- ------------- "
367 "-------------\n";
368}
369
Benjamin Kramer5acab502011-09-15 02:12:05 +0000370void DWARFDebugLine::Row::dump(raw_ostream &OS) const {
Benjamin Kramerf3da5292011-11-05 08:57:40 +0000371 OS << format("0x%16.16" PRIx64 " %6u %6u", Address, Line, Column)
Diego Novillo5b5cf502014-02-14 19:27:53 +0000372 << format(" %6u %3u %13u ", File, Isa, Discriminator)
Dehao Chen1b54fce2016-04-28 22:09:37 +0000373 << (IsStmt ? " is_stmt" : "") << (BasicBlock ? " basic_block" : "")
Benjamin Kramer5acab502011-09-15 02:12:05 +0000374 << (PrologueEnd ? " prologue_end" : "")
375 << (EpilogueBegin ? " epilogue_begin" : "")
Dehao Chen1b54fce2016-04-28 22:09:37 +0000376 << (EndSequence ? " end_sequence" : "") << '\n';
Benjamin Kramer5acab502011-09-15 02:12:05 +0000377}
378
Dehao Chen1b54fce2016-04-28 22:09:37 +0000379DWARFDebugLine::Sequence::Sequence() { reset(); }
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000380
381void DWARFDebugLine::Sequence::reset() {
382 LowPC = 0;
383 HighPC = 0;
384 FirstRowIndex = 0;
385 LastRowIndex = 0;
386 Empty = true;
387}
388
Dehao Chen1b54fce2016-04-28 22:09:37 +0000389DWARFDebugLine::LineTable::LineTable() { clear(); }
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000390
Paul Robinson0a227092018-02-05 20:43:15 +0000391void DWARFDebugLine::LineTable::dump(raw_ostream &OS,
392 DIDumpOptions DumpOptions) const {
393 Prologue.dump(OS, DumpOptions);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000394 OS << '\n';
395
396 if (!Rows.empty()) {
Greg Clayton67070462017-05-02 22:48:52 +0000397 Row::dumpTableHeader(OS);
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000398 for (const Row &R : Rows) {
399 R.dump(OS);
400 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000401 }
402}
403
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000404void DWARFDebugLine::LineTable::clear() {
405 Prologue.clear();
406 Rows.clear();
407 Sequences.clear();
408}
409
Alexey Samsonov110d5952014-04-30 00:09:19 +0000410DWARFDebugLine::ParsingState::ParsingState(struct LineTable *LT)
Eugene Zelenko2db0cfa2017-06-23 21:57:40 +0000411 : LineTable(LT) {
Alexey Samsonov110d5952014-04-30 00:09:19 +0000412 resetRowAndSequence();
413}
Nick Lewycky4d044922011-09-15 03:41:51 +0000414
Alexey Samsonov110d5952014-04-30 00:09:19 +0000415void DWARFDebugLine::ParsingState::resetRowAndSequence() {
416 Row.reset(LineTable->Prologue.DefaultIsStmt);
417 Sequence.reset();
418}
419
Paul Robinson9d4eb692017-05-01 23:27:55 +0000420void DWARFDebugLine::ParsingState::appendRowToMatrix(uint32_t Offset) {
Alexey Samsonov110d5952014-04-30 00:09:19 +0000421 if (Sequence.Empty) {
Alexey Samsonov947228c2012-08-07 11:46:57 +0000422 // Record the beginning of instruction sequence.
Alexey Samsonov110d5952014-04-30 00:09:19 +0000423 Sequence.Empty = false;
424 Sequence.LowPC = Row.Address;
425 Sequence.FirstRowIndex = RowNumber;
Alexey Samsonov947228c2012-08-07 11:46:57 +0000426 }
Alexey Samsonov110d5952014-04-30 00:09:19 +0000427 ++RowNumber;
428 LineTable->appendRow(Row);
429 if (Row.EndSequence) {
Alexey Samsonov947228c2012-08-07 11:46:57 +0000430 // Record the end of instruction sequence.
Alexey Samsonov110d5952014-04-30 00:09:19 +0000431 Sequence.HighPC = Row.Address;
432 Sequence.LastRowIndex = RowNumber;
433 if (Sequence.isValid())
434 LineTable->appendSequence(Sequence);
435 Sequence.reset();
Alexey Samsonov947228c2012-08-07 11:46:57 +0000436 }
Alexey Samsonov110d5952014-04-30 00:09:19 +0000437 Row.postAppend();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000438}
439
Benjamin Kramer5acab502011-09-15 02:12:05 +0000440const DWARFDebugLine::LineTable *
Paul Robinson9d4eb692017-05-01 23:27:55 +0000441DWARFDebugLine::getLineTable(uint32_t Offset) const {
442 LineTableConstIter Pos = LineTableMap.find(Offset);
443 if (Pos != LineTableMap.end())
444 return &Pos->second;
Craig Topper2617dcc2014-04-15 06:32:26 +0000445 return nullptr;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000446}
447
Benjamin Kramer679e1752011-09-15 20:43:18 +0000448const DWARFDebugLine::LineTable *
Paul Robinson511b54c2017-11-22 15:48:30 +0000449DWARFDebugLine::getOrParseLineTable(DWARFDataExtractor &DebugLineData,
Paul Robinsonbf750c82018-01-29 20:57:43 +0000450 uint32_t Offset, const DWARFContext &Ctx,
451 const DWARFUnit *U) {
James Henderson66702622018-03-08 10:53:34 +0000452 if (!DebugLineData.isValidOffset(Offset))
453 return nullptr;
454
Paul Robinson9d4eb692017-05-01 23:27:55 +0000455 std::pair<LineTableIter, bool> Pos =
456 LineTableMap.insert(LineTableMapTy::value_type(Offset, LineTable()));
457 LineTable *LT = &Pos.first->second;
458 if (Pos.second) {
Paul Robinsonbf750c82018-01-29 20:57:43 +0000459 if (!LT->parse(DebugLineData, &Offset, Ctx, U))
Craig Topper2617dcc2014-04-15 06:32:26 +0000460 return nullptr;
Benjamin Kramer679e1752011-09-15 20:43:18 +0000461 }
Alexey Samsonov110d5952014-04-30 00:09:19 +0000462 return LT;
Benjamin Kramer679e1752011-09-15 20:43:18 +0000463}
464
Paul Robinson511b54c2017-11-22 15:48:30 +0000465bool DWARFDebugLine::LineTable::parse(DWARFDataExtractor &DebugLineData,
Paul Robinsonbf750c82018-01-29 20:57:43 +0000466 uint32_t *OffsetPtr,
467 const DWARFContext &Ctx,
468 const DWARFUnit *U, raw_ostream *OS) {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000469 const uint32_t DebugLineOffset = *OffsetPtr;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000470
Alexey Samsonov110d5952014-04-30 00:09:19 +0000471 clear();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000472
Paul Robinsonbf750c82018-01-29 20:57:43 +0000473 if (!Prologue.parse(DebugLineData, OffsetPtr, Ctx, U)) {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000474 // Restore our offset and return false to indicate failure!
Paul Robinson9d4eb692017-05-01 23:27:55 +0000475 *OffsetPtr = DebugLineOffset;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000476 return false;
477 }
478
Paul Robinson0a227092018-02-05 20:43:15 +0000479 if (OS) {
480 // The presence of OS signals verbose dumping.
481 DIDumpOptions DumpOptions;
482 DumpOptions.Verbose = true;
483 Prologue.dump(*OS, DumpOptions);
484 }
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000485
Paul Robinson9d4eb692017-05-01 23:27:55 +0000486 const uint32_t EndOffset =
487 DebugLineOffset + Prologue.TotalLength + Prologue.sizeofTotalLength();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000488
Paul Robinson511b54c2017-11-22 15:48:30 +0000489 // See if we should tell the data extractor the address size.
490 if (DebugLineData.getAddressSize() == 0)
491 DebugLineData.setAddressSize(Prologue.getAddressSize());
492 else
493 assert(Prologue.getAddressSize() == 0 ||
494 Prologue.getAddressSize() == DebugLineData.getAddressSize());
495
Alexey Samsonov110d5952014-04-30 00:09:19 +0000496 ParsingState State(this);
Benjamin Kramer112ec172011-09-15 21:59:13 +0000497
Paul Robinson9d4eb692017-05-01 23:27:55 +0000498 while (*OffsetPtr < EndOffset) {
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000499 if (OS)
500 *OS << format("0x%08.08" PRIx32 ": ", *OffsetPtr);
501
Paul Robinson9d4eb692017-05-01 23:27:55 +0000502 uint8_t Opcode = DebugLineData.getU8(OffsetPtr);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000503
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000504 if (OS)
505 *OS << format("%02.02" PRIx8 " ", Opcode);
506
Paul Robinson9d4eb692017-05-01 23:27:55 +0000507 if (Opcode == 0) {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000508 // Extended Opcodes always start with a zero opcode followed by
509 // a uleb128 length so you can skip ones you don't know about
Paul Robinson9d4eb692017-05-01 23:27:55 +0000510 uint64_t Len = DebugLineData.getULEB128(OffsetPtr);
Paul Robinsone0833342017-11-22 15:14:49 +0000511 uint32_t ExtOffset = *OffsetPtr;
512
513 // Tolerate zero-length; assume length is correct and soldier on.
514 if (Len == 0) {
515 if (OS)
516 *OS << "Badly formed extended line op (length 0)\n";
517 continue;
518 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000519
Paul Robinson9d4eb692017-05-01 23:27:55 +0000520 uint8_t SubOpcode = DebugLineData.getU8(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000521 if (OS)
522 *OS << LNExtendedString(SubOpcode);
Paul Robinson9d4eb692017-05-01 23:27:55 +0000523 switch (SubOpcode) {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000524 case DW_LNE_end_sequence:
525 // Set the end_sequence register of the state machine to true and
526 // append a row to the matrix using the current values of the
527 // state-machine registers. Then reset the registers to the initial
528 // values specified above. Every statement program sequence must end
529 // with a DW_LNE_end_sequence instruction which creates a row whose
530 // address is that of the byte after the last target machine instruction
531 // of the sequence.
Alexey Samsonov110d5952014-04-30 00:09:19 +0000532 State.Row.EndSequence = true;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000533 State.appendRowToMatrix(*OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000534 if (OS) {
535 *OS << "\n";
536 OS->indent(12);
537 State.Row.dump(*OS);
538 }
Alexey Samsonov110d5952014-04-30 00:09:19 +0000539 State.resetRowAndSequence();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000540 break;
541
542 case DW_LNE_set_address:
543 // Takes a single relocatable address as an operand. The size of the
544 // operand is the size appropriate to hold an address on the target
545 // machine. Set the address register to the value given by the
546 // relocatable address. All of the other statement program opcodes
547 // that affect the address register add a delta to it. This instruction
548 // stores a relocatable value into it instead.
Paul Robinson511b54c2017-11-22 15:48:30 +0000549 //
550 // Make sure the extractor knows the address size. If not, infer it
551 // from the size of the operand.
552 if (DebugLineData.getAddressSize() == 0)
553 DebugLineData.setAddressSize(Len - 1);
Paul Robinson79474682018-03-22 19:37:56 +0000554 else if (DebugLineData.getAddressSize() != Len - 1) {
555 fprintf(stderr, "Mismatching address size at offset 0x%8.8" PRIx32
Paul Robinson82e48642018-03-26 19:55:01 +0000556 " expected 0x%2.2" PRIx8 " found 0x%2.2" PRIx64 "\n",
Paul Robinson79474682018-03-22 19:37:56 +0000557 ExtOffset, DebugLineData.getAddressSize(), Len - 1);
558 // Skip the rest of the line-number program.
559 *OffsetPtr = EndOffset;
560 return false;
561 }
Paul Robinson17536b92017-06-29 16:52:08 +0000562 State.Row.Address = DebugLineData.getRelocatedAddress(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000563 if (OS)
564 *OS << format(" (0x%16.16" PRIx64 ")", State.Row.Address);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000565 break;
566
567 case DW_LNE_define_file:
568 // Takes 4 arguments. The first is a null terminated string containing
569 // a source file name. The second is an unsigned LEB128 number
570 // representing the directory index of the directory in which the file
571 // was found. The third is an unsigned LEB128 number representing the
572 // time of last modification of the file. The fourth is an unsigned
573 // LEB128 number representing the length in bytes of the file. The time
574 // and length fields may contain LEB128(0) if the information is not
575 // available.
576 //
577 // The directory index represents an entry in the include_directories
578 // section of the statement program prologue. The index is LEB128(0)
579 // if the file was found in the current directory of the compilation,
580 // LEB128(1) if it was found in the first directory in the
581 // include_directories section, and so on. The directory index is
582 // ignored for file names that represent full path names.
583 //
584 // The files are numbered, starting at 1, in the order in which they
585 // appear; the names in the prologue come before names defined by
586 // the DW_LNE_define_file instruction. These numbers are used in the
587 // the file register of the state machine.
588 {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000589 FileNameEntry FileEntry;
Paul Robinson0a227092018-02-05 20:43:15 +0000590 const char *Name = DebugLineData.getCStr(OffsetPtr);
591 FileEntry.Name.setForm(dwarf::DW_FORM_string);
592 FileEntry.Name.setPValue(Name);
Paul Robinson9d4eb692017-05-01 23:27:55 +0000593 FileEntry.DirIdx = DebugLineData.getULEB128(OffsetPtr);
594 FileEntry.ModTime = DebugLineData.getULEB128(OffsetPtr);
595 FileEntry.Length = DebugLineData.getULEB128(OffsetPtr);
596 Prologue.FileNames.push_back(FileEntry);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000597 if (OS)
Paul Robinson0a227092018-02-05 20:43:15 +0000598 *OS << " (" << Name << ", dir=" << FileEntry.DirIdx << ", mod_time="
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000599 << format("(0x%16.16" PRIx64 ")", FileEntry.ModTime)
600 << ", length=" << FileEntry.Length << ")";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000601 }
602 break;
603
Diego Novillo5b5cf502014-02-14 19:27:53 +0000604 case DW_LNE_set_discriminator:
Paul Robinson9d4eb692017-05-01 23:27:55 +0000605 State.Row.Discriminator = DebugLineData.getULEB128(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000606 if (OS)
607 *OS << " (" << State.Row.Discriminator << ")";
Diego Novillo5b5cf502014-02-14 19:27:53 +0000608 break;
609
Benjamin Kramer5acab502011-09-15 02:12:05 +0000610 default:
Paul Robinsone0833342017-11-22 15:14:49 +0000611 if (OS)
612 *OS << format("Unrecognized extended op 0x%02.02" PRIx8, SubOpcode)
613 << format(" length %" PRIx64, Len);
614 // Len doesn't include the zero opcode byte or the length itself, but
615 // it does include the sub_opcode, so we have to adjust for that.
616 (*OffsetPtr) += Len - 1;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000617 break;
618 }
Paul Robinsone0833342017-11-22 15:14:49 +0000619 // Make sure the stated and parsed lengths are the same.
620 // Otherwise we have an unparseable line-number program.
621 if (*OffsetPtr - ExtOffset != Len) {
622 fprintf(stderr, "Unexpected line op length at offset 0x%8.8" PRIx32
623 " expected 0x%2.2" PRIx64 " found 0x%2.2" PRIx32 "\n",
624 ExtOffset, Len, *OffsetPtr - ExtOffset);
625 // Skip the rest of the line-number program.
626 *OffsetPtr = EndOffset;
627 return false;
628 }
Paul Robinson9d4eb692017-05-01 23:27:55 +0000629 } else if (Opcode < Prologue.OpcodeBase) {
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000630 if (OS)
631 *OS << LNStandardString(Opcode);
Paul Robinson9d4eb692017-05-01 23:27:55 +0000632 switch (Opcode) {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000633 // Standard Opcodes
634 case DW_LNS_copy:
635 // Takes no arguments. Append a row to the matrix using the
636 // current values of the state-machine registers. Then set
637 // the basic_block register to false.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000638 State.appendRowToMatrix(*OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000639 if (OS) {
640 *OS << "\n";
641 OS->indent(12);
642 State.Row.dump(*OS);
643 *OS << "\n";
644 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000645 break;
646
647 case DW_LNS_advance_pc:
648 // Takes a single unsigned LEB128 operand, multiplies it by the
649 // min_inst_length field of the prologue, and adds the
650 // result to the address register of the state machine.
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000651 {
652 uint64_t AddrOffset =
653 DebugLineData.getULEB128(OffsetPtr) * Prologue.MinInstLength;
654 State.Row.Address += AddrOffset;
655 if (OS)
656 *OS << " (" << AddrOffset << ")";
657 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000658 break;
659
660 case DW_LNS_advance_line:
661 // Takes a single signed LEB128 operand and adds that value to
662 // the line register of the state machine.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000663 State.Row.Line += DebugLineData.getSLEB128(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000664 if (OS)
665 *OS << " (" << State.Row.Line << ")";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000666 break;
667
668 case DW_LNS_set_file:
669 // Takes a single unsigned LEB128 operand and stores it in the file
670 // register of the state machine.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000671 State.Row.File = DebugLineData.getULEB128(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000672 if (OS)
673 *OS << " (" << State.Row.File << ")";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000674 break;
675
676 case DW_LNS_set_column:
677 // Takes a single unsigned LEB128 operand and stores it in the
678 // column register of the state machine.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000679 State.Row.Column = DebugLineData.getULEB128(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000680 if (OS)
681 *OS << " (" << State.Row.Column << ")";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000682 break;
683
684 case DW_LNS_negate_stmt:
685 // Takes no arguments. Set the is_stmt register of the state
686 // machine to the logical negation of its current value.
Alexey Samsonov110d5952014-04-30 00:09:19 +0000687 State.Row.IsStmt = !State.Row.IsStmt;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000688 break;
689
690 case DW_LNS_set_basic_block:
691 // Takes no arguments. Set the basic_block register of the
692 // state machine to true
Alexey Samsonov110d5952014-04-30 00:09:19 +0000693 State.Row.BasicBlock = true;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000694 break;
695
696 case DW_LNS_const_add_pc:
697 // Takes no arguments. Add to the address register of the state
698 // machine the address increment value corresponding to special
699 // opcode 255. The motivation for DW_LNS_const_add_pc is this:
700 // when the statement program needs to advance the address by a
701 // small amount, it can use a single special opcode, which occupies
702 // a single byte. When it needs to advance the address by up to
703 // twice the range of the last special opcode, it can use
704 // DW_LNS_const_add_pc followed by a special opcode, for a total
705 // of two bytes. Only if it needs to advance the address by more
706 // than twice that range will it need to use both DW_LNS_advance_pc
707 // and a special opcode, requiring three or more bytes.
708 {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000709 uint8_t AdjustOpcode = 255 - Prologue.OpcodeBase;
710 uint64_t AddrOffset =
711 (AdjustOpcode / Prologue.LineRange) * Prologue.MinInstLength;
712 State.Row.Address += AddrOffset;
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000713 if (OS)
714 *OS
715 << format(" (0x%16.16" PRIx64 ")", AddrOffset);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000716 }
717 break;
718
719 case DW_LNS_fixed_advance_pc:
720 // Takes a single uhalf operand. Add to the address register of
721 // the state machine the value of the (unencoded) operand. This
722 // is the only extended opcode that takes an argument that is not
723 // a variable length number. The motivation for DW_LNS_fixed_advance_pc
724 // is this: existing assemblers cannot emit DW_LNS_advance_pc or
725 // special opcodes because they cannot encode LEB128 numbers or
726 // judge when the computation of a special opcode overflows and
727 // requires the use of DW_LNS_advance_pc. Such assemblers, however,
728 // can use DW_LNS_fixed_advance_pc instead, sacrificing compression.
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000729 {
730 uint16_t PCOffset = DebugLineData.getU16(OffsetPtr);
731 State.Row.Address += PCOffset;
732 if (OS)
733 *OS
734 << format(" (0x%16.16" PRIx64 ")", PCOffset);
735 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000736 break;
737
738 case DW_LNS_set_prologue_end:
739 // Takes no arguments. Set the prologue_end register of the
740 // state machine to true
Alexey Samsonov110d5952014-04-30 00:09:19 +0000741 State.Row.PrologueEnd = true;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000742 break;
743
744 case DW_LNS_set_epilogue_begin:
745 // Takes no arguments. Set the basic_block register of the
746 // state machine to true
Alexey Samsonov110d5952014-04-30 00:09:19 +0000747 State.Row.EpilogueBegin = true;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000748 break;
749
750 case DW_LNS_set_isa:
751 // Takes a single unsigned LEB128 operand and stores it in the
752 // column register of the state machine.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000753 State.Row.Isa = DebugLineData.getULEB128(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000754 if (OS)
755 *OS << " (" << State.Row.Isa << ")";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000756 break;
757
758 default:
759 // Handle any unknown standard opcodes here. We know the lengths
760 // of such opcodes because they are specified in the prologue
761 // as a multiple of LEB128 operands for each opcode.
762 {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000763 assert(Opcode - 1U < Prologue.StandardOpcodeLengths.size());
764 uint8_t OpcodeLength = Prologue.StandardOpcodeLengths[Opcode - 1];
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000765 for (uint8_t I = 0; I < OpcodeLength; ++I) {
766 uint64_t Value = DebugLineData.getULEB128(OffsetPtr);
767 if (OS)
768 *OS << format("Skipping ULEB128 value: 0x%16.16" PRIx64 ")\n",
769 Value);
770 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000771 }
772 break;
773 }
774 } else {
775 // Special Opcodes
776
777 // A special opcode value is chosen based on the amount that needs
778 // to be added to the line and address registers. The maximum line
779 // increment for a special opcode is the value of the line_base
780 // field in the header, plus the value of the line_range field,
781 // minus 1 (line base + line range - 1). If the desired line
782 // increment is greater than the maximum line increment, a standard
NAKAMURA Takumif9959852011-10-08 11:22:47 +0000783 // opcode must be used instead of a special opcode. The "address
784 // advance" is calculated by dividing the desired address increment
Benjamin Kramer5acab502011-09-15 02:12:05 +0000785 // by the minimum_instruction_length field from the header. The
786 // special opcode is then calculated using the following formula:
787 //
788 // opcode = (desired line increment - line_base) +
789 // (line_range * address advance) + opcode_base
790 //
791 // If the resulting opcode is greater than 255, a standard opcode
792 // must be used instead.
793 //
794 // To decode a special opcode, subtract the opcode_base from the
795 // opcode itself to give the adjusted opcode. The amount to
796 // increment the address register is the result of the adjusted
797 // opcode divided by the line_range multiplied by the
798 // minimum_instruction_length field from the header. That is:
799 //
800 // address increment = (adjusted opcode / line_range) *
801 // minimum_instruction_length
802 //
803 // The amount to increment the line register is the line_base plus
804 // the result of the adjusted opcode modulo the line_range. That is:
805 //
806 // line increment = line_base + (adjusted opcode % line_range)
807
Paul Robinson9d4eb692017-05-01 23:27:55 +0000808 uint8_t AdjustOpcode = Opcode - Prologue.OpcodeBase;
809 uint64_t AddrOffset =
810 (AdjustOpcode / Prologue.LineRange) * Prologue.MinInstLength;
811 int32_t LineOffset =
812 Prologue.LineBase + (AdjustOpcode % Prologue.LineRange);
813 State.Row.Line += LineOffset;
814 State.Row.Address += AddrOffset;
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000815
816 if (OS) {
817 *OS << "address += " << ((uint32_t)AdjustOpcode)
818 << ", line += " << LineOffset << "\n";
819 OS->indent(12);
820 State.Row.dump(*OS);
821 }
822
Paul Robinson9d4eb692017-05-01 23:27:55 +0000823 State.appendRowToMatrix(*OffsetPtr);
Dehao Chen1b54fce2016-04-28 22:09:37 +0000824 // Reset discriminator to 0.
825 State.Row.Discriminator = 0;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000826 }
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000827 if(OS)
828 *OS << "\n";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000829 }
830
Alexey Samsonov110d5952014-04-30 00:09:19 +0000831 if (!State.Sequence.Empty) {
832 fprintf(stderr, "warning: last sequence in debug line table is not"
833 "terminated!\n");
834 }
835
836 // Sort all sequences so that address lookup will work faster.
837 if (!Sequences.empty()) {
Mandeep Singh Grangfe1d28e2018-04-01 16:18:49 +0000838 llvm::sort(Sequences.begin(), Sequences.end(), Sequence::orderByLowPC);
Alexey Samsonov110d5952014-04-30 00:09:19 +0000839 // Note: actually, instruction address ranges of sequences should not
840 // overlap (in shared objects and executables). If they do, the address
841 // lookup would still work, though, but result would be ambiguous.
842 // We don't report warning in this case. For example,
843 // sometimes .so compiled from multiple object files contains a few
844 // rudimentary sequences for address ranges [0x0, 0xsomething).
845 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000846
Paul Robinson9d4eb692017-05-01 23:27:55 +0000847 return EndOffset;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000848}
849
Keno Fischerc2c60182015-05-31 23:37:04 +0000850uint32_t
Paul Robinson9d4eb692017-05-01 23:27:55 +0000851DWARFDebugLine::LineTable::findRowInSeq(const DWARFDebugLine::Sequence &Seq,
852 uint64_t Address) const {
853 if (!Seq.containsPC(Address))
Keno Fischerc2c60182015-05-31 23:37:04 +0000854 return UnknownRowIndex;
855 // Search for instruction address in the rows describing the sequence.
856 // Rows are stored in a vector, so we may use arithmetical operations with
857 // iterators.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000858 DWARFDebugLine::Row Row;
859 Row.Address = Address;
860 RowIter FirstRow = Rows.begin() + Seq.FirstRowIndex;
861 RowIter LastRow = Rows.begin() + Seq.LastRowIndex;
862 LineTable::RowIter RowPos = std::lower_bound(
863 FirstRow, LastRow, Row, DWARFDebugLine::Row::orderByAddress);
864 if (RowPos == LastRow) {
865 return Seq.LastRowIndex - 1;
Keno Fischerc2c60182015-05-31 23:37:04 +0000866 }
Paul Robinson9d4eb692017-05-01 23:27:55 +0000867 uint32_t Index = Seq.FirstRowIndex + (RowPos - FirstRow);
868 if (RowPos->Address > Address) {
869 if (RowPos == FirstRow)
Keno Fischerc2c60182015-05-31 23:37:04 +0000870 return UnknownRowIndex;
871 else
Paul Robinson9d4eb692017-05-01 23:27:55 +0000872 Index--;
Keno Fischerc2c60182015-05-31 23:37:04 +0000873 }
Paul Robinson9d4eb692017-05-01 23:27:55 +0000874 return Index;
Keno Fischerc2c60182015-05-31 23:37:04 +0000875}
876
Paul Robinson9d4eb692017-05-01 23:27:55 +0000877uint32_t DWARFDebugLine::LineTable::lookupAddress(uint64_t Address) const {
Alexey Samsonov947228c2012-08-07 11:46:57 +0000878 if (Sequences.empty())
Keno Fischerc2c60182015-05-31 23:37:04 +0000879 return UnknownRowIndex;
Alexey Samsonov947228c2012-08-07 11:46:57 +0000880 // First, find an instruction sequence containing the given address.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000881 DWARFDebugLine::Sequence Sequence;
882 Sequence.LowPC = Address;
883 SequenceIter FirstSeq = Sequences.begin();
884 SequenceIter LastSeq = Sequences.end();
885 SequenceIter SeqPos = std::lower_bound(
886 FirstSeq, LastSeq, Sequence, DWARFDebugLine::Sequence::orderByLowPC);
887 DWARFDebugLine::Sequence FoundSeq;
888 if (SeqPos == LastSeq) {
889 FoundSeq = Sequences.back();
890 } else if (SeqPos->LowPC == Address) {
891 FoundSeq = *SeqPos;
Alexey Samsonov947228c2012-08-07 11:46:57 +0000892 } else {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000893 if (SeqPos == FirstSeq)
Keno Fischerc2c60182015-05-31 23:37:04 +0000894 return UnknownRowIndex;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000895 FoundSeq = *(SeqPos - 1);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000896 }
Paul Robinson9d4eb692017-05-01 23:27:55 +0000897 return findRowInSeq(FoundSeq, Address);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000898}
Alexey Samsonov45be7932012-08-30 07:49:50 +0000899
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000900bool DWARFDebugLine::LineTable::lookupAddressRange(
Paul Robinson9d4eb692017-05-01 23:27:55 +0000901 uint64_t Address, uint64_t Size, std::vector<uint32_t> &Result) const {
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000902 if (Sequences.empty())
903 return false;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000904 uint64_t EndAddr = Address + Size;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000905 // First, find an instruction sequence containing the given address.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000906 DWARFDebugLine::Sequence Sequence;
907 Sequence.LowPC = Address;
908 SequenceIter FirstSeq = Sequences.begin();
909 SequenceIter LastSeq = Sequences.end();
910 SequenceIter SeqPos = std::lower_bound(
911 FirstSeq, LastSeq, Sequence, DWARFDebugLine::Sequence::orderByLowPC);
912 if (SeqPos == LastSeq || SeqPos->LowPC != Address) {
913 if (SeqPos == FirstSeq)
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000914 return false;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000915 SeqPos--;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000916 }
Paul Robinson9d4eb692017-05-01 23:27:55 +0000917 if (!SeqPos->containsPC(Address))
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000918 return false;
919
Paul Robinson9d4eb692017-05-01 23:27:55 +0000920 SequenceIter StartPos = SeqPos;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000921
922 // Add the rows from the first sequence to the vector, starting with the
923 // index we just calculated
924
Paul Robinson9d4eb692017-05-01 23:27:55 +0000925 while (SeqPos != LastSeq && SeqPos->LowPC < EndAddr) {
926 const DWARFDebugLine::Sequence &CurSeq = *SeqPos;
Keno Fischerc2c60182015-05-31 23:37:04 +0000927 // For the first sequence, we need to find which row in the sequence is the
928 // first in our range.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000929 uint32_t FirstRowIndex = CurSeq.FirstRowIndex;
930 if (SeqPos == StartPos)
931 FirstRowIndex = findRowInSeq(CurSeq, Address);
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000932
Keno Fischerc2c60182015-05-31 23:37:04 +0000933 // Figure out the last row in the range.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000934 uint32_t LastRowIndex = findRowInSeq(CurSeq, EndAddr - 1);
935 if (LastRowIndex == UnknownRowIndex)
936 LastRowIndex = CurSeq.LastRowIndex - 1;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000937
Paul Robinson9d4eb692017-05-01 23:27:55 +0000938 assert(FirstRowIndex != UnknownRowIndex);
939 assert(LastRowIndex != UnknownRowIndex);
Keno Fischerc2c60182015-05-31 23:37:04 +0000940
Paul Robinson9d4eb692017-05-01 23:27:55 +0000941 for (uint32_t I = FirstRowIndex; I <= LastRowIndex; ++I) {
942 Result.push_back(I);
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000943 }
944
Paul Robinson9d4eb692017-05-01 23:27:55 +0000945 ++SeqPos;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000946 }
NAKAMURA Takumi4b86cdb2013-01-26 01:45:06 +0000947
948 return true;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000949}
950
Paul Robinson9d4eb692017-05-01 23:27:55 +0000951bool DWARFDebugLine::LineTable::hasFileAtIndex(uint64_t FileIndex) const {
Pete Cooperb2ba7762016-07-22 01:41:32 +0000952 return FileIndex != 0 && FileIndex <= Prologue.FileNames.size();
953}
954
Scott Linder16c7bda2018-02-23 23:01:06 +0000955Optional<StringRef> DWARFDebugLine::LineTable::getSourceByIndex(uint64_t FileIndex,
956 FileLineInfoKind Kind) const {
957 if (Kind == FileLineInfoKind::None || !hasFileAtIndex(FileIndex))
958 return None;
959 const FileNameEntry &Entry = Prologue.FileNames[FileIndex - 1];
960 if (Optional<const char *> source = Entry.Source.getAsCString())
961 return StringRef(*source);
962 return None;
963}
964
Eugene Zemtsov82d60d62018-03-13 17:54:29 +0000965static bool isPathAbsoluteOnWindowsOrPosix(const Twine &Path) {
966 // Debug info can contain paths from any OS, not necessarily
967 // an OS we're currently running on. Moreover different compilation units can
968 // be compiled on different operating systems and linked together later.
969 return sys::path::is_absolute(Path, sys::path::Style::posix) ||
970 sys::path::is_absolute(Path, sys::path::Style::windows);
971}
972
Paul Robinson9d4eb692017-05-01 23:27:55 +0000973bool DWARFDebugLine::LineTable::getFileNameByIndex(uint64_t FileIndex,
974 const char *CompDir,
975 FileLineInfoKind Kind,
976 std::string &Result) const {
Pete Cooperb2ba7762016-07-22 01:41:32 +0000977 if (Kind == FileLineInfoKind::None || !hasFileAtIndex(FileIndex))
Alexey Samsonov45be7932012-08-30 07:49:50 +0000978 return false;
979 const FileNameEntry &Entry = Prologue.FileNames[FileIndex - 1];
Paul Robinson0a227092018-02-05 20:43:15 +0000980 StringRef FileName = Entry.Name.getAsCString().getValue();
Alexey Samsonovdce67342014-05-15 21:24:32 +0000981 if (Kind != FileLineInfoKind::AbsoluteFilePath ||
Eugene Zemtsov82d60d62018-03-13 17:54:29 +0000982 isPathAbsoluteOnWindowsOrPosix(FileName)) {
Alexey Samsonov45be7932012-08-30 07:49:50 +0000983 Result = FileName;
984 return true;
985 }
Frederic Riss101b5e22014-09-19 15:11:51 +0000986
Alexey Samsonov45be7932012-08-30 07:49:50 +0000987 SmallString<16> FilePath;
988 uint64_t IncludeDirIndex = Entry.DirIdx;
Paul Robinsonba1c9152017-05-02 17:37:32 +0000989 StringRef IncludeDir;
Alexey Samsonov45be7932012-08-30 07:49:50 +0000990 // Be defensive about the contents of Entry.
991 if (IncludeDirIndex > 0 &&
Frederic Riss101b5e22014-09-19 15:11:51 +0000992 IncludeDirIndex <= Prologue.IncludeDirectories.size())
Paul Robinson0a227092018-02-05 20:43:15 +0000993 IncludeDir = Prologue.IncludeDirectories[IncludeDirIndex - 1]
994 .getAsCString()
995 .getValue();
Frederic Riss101b5e22014-09-19 15:11:51 +0000996
997 // We may still need to append compilation directory of compile unit.
998 // We know that FileName is not absolute, the only way to have an
999 // absolute path at this point would be if IncludeDir is absolute.
1000 if (CompDir && Kind == FileLineInfoKind::AbsoluteFilePath &&
Eugene Zemtsov82d60d62018-03-13 17:54:29 +00001001 !isPathAbsoluteOnWindowsOrPosix(IncludeDir))
Frederic Riss101b5e22014-09-19 15:11:51 +00001002 sys::path::append(FilePath, CompDir);
1003
1004 // sys::path::append skips empty strings.
1005 sys::path::append(FilePath, IncludeDir, FileName);
Alexey Samsonov45be7932012-08-30 07:49:50 +00001006 Result = FilePath.str();
1007 return true;
1008}
Frederic Riss101b5e22014-09-19 15:11:51 +00001009
Dehao Chen1b54fce2016-04-28 22:09:37 +00001010bool DWARFDebugLine::LineTable::getFileLineInfoForAddress(
1011 uint64_t Address, const char *CompDir, FileLineInfoKind Kind,
1012 DILineInfo &Result) const {
Frederic Riss101b5e22014-09-19 15:11:51 +00001013 // Get the index of row we're looking for in the line table.
1014 uint32_t RowIndex = lookupAddress(Address);
1015 if (RowIndex == -1U)
1016 return false;
1017 // Take file number and line/column from the row.
1018 const auto &Row = Rows[RowIndex];
1019 if (!getFileNameByIndex(Row.File, CompDir, Kind, Result.FileName))
1020 return false;
1021 Result.Line = Row.Line;
1022 Result.Column = Row.Column;
Eric Christopherba1024c2016-12-14 18:29:39 +00001023 Result.Discriminator = Row.Discriminator;
Scott Linder16c7bda2018-02-23 23:01:06 +00001024 Result.Source = getSourceByIndex(Row.File, Kind);
Frederic Riss101b5e22014-09-19 15:11:51 +00001025 return true;
1026}