blob: 538185771c2f6b603d12dc8c90c36e10b808dc47 [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
James Hendersona3acf992018-05-10 10:51:33 +0000276template <typename... Ts>
277static std::string formatErrorString(char const *Fmt, const Ts &... Vals) {
278 std::string Buffer;
279 raw_string_ostream Stream(Buffer);
280 Stream << format(Fmt, Vals...);
281 return Stream.str();
282}
283
284template <typename... Ts>
285static Error createError(char const *Fmt, const Ts &... Vals) {
286 return make_error<StringError>(formatErrorString(Fmt, Vals...),
287 inconvertibleErrorCode());
288}
289
James Henderson004b7292018-05-21 15:30:54 +0000290static Error createError(char const *Msg) {
291 return make_error<StringError>(Msg, inconvertibleErrorCode());
292}
293
James Hendersona3acf992018-05-10 10:51:33 +0000294Error DWARFDebugLine::Prologue::parse(const DWARFDataExtractor &DebugLineData,
295 uint32_t *OffsetPtr,
296 const DWARFContext &Ctx,
297 const DWARFUnit *U) {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000298 const uint64_t PrologueOffset = *OffsetPtr;
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000299
300 clear();
Paul Robinson9d4eb692017-05-01 23:27:55 +0000301 TotalLength = DebugLineData.getU32(OffsetPtr);
Ed Maste6d0bee52015-05-28 15:38:17 +0000302 if (TotalLength == UINT32_MAX) {
Paul Robinson75c068c2017-06-26 18:43:01 +0000303 FormParams.Format = dwarf::DWARF64;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000304 TotalLength = DebugLineData.getU64(OffsetPtr);
Paul Robinson75c068c2017-06-26 18:43:01 +0000305 } else if (TotalLength >= 0xffffff00) {
James Hendersona3acf992018-05-10 10:51:33 +0000306 return createError(
307 "parsing line table prologue at offset 0x%8.8" PRIx64
308 " unsupported reserved unit length found of value 0x%8.8" PRIx64,
309 PrologueOffset, TotalLength);
Ed Maste6d0bee52015-05-28 15:38:17 +0000310 }
Paul Robinson75c068c2017-06-26 18:43:01 +0000311 FormParams.Version = DebugLineData.getU16(OffsetPtr);
312 if (getVersion() < 2)
James Hendersona3acf992018-05-10 10:51:33 +0000313 return createError("parsing line table prologue at offset 0x%8.8" PRIx64
314 " found unsupported version 0x%2.2" PRIx16,
315 PrologueOffset, getVersion());
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000316
Paul Robinson75c068c2017-06-26 18:43:01 +0000317 if (getVersion() >= 5) {
318 FormParams.AddrSize = DebugLineData.getU8(OffsetPtr);
Paul Robinson63811a42017-11-22 15:33:17 +0000319 assert((DebugLineData.getAddressSize() == 0 ||
320 DebugLineData.getAddressSize() == getAddressSize()) &&
Paul Robinson75c068c2017-06-26 18:43:01 +0000321 "Line table header and data extractor disagree");
Paul Robinson2bc38732017-05-02 21:40:47 +0000322 SegSelectorSize = DebugLineData.getU8(OffsetPtr);
323 }
324
Paul Robinson9d4eb692017-05-01 23:27:55 +0000325 PrologueLength = DebugLineData.getUnsigned(OffsetPtr, sizeofPrologueLength());
326 const uint64_t EndPrologueOffset = PrologueLength + *OffsetPtr;
327 MinInstLength = DebugLineData.getU8(OffsetPtr);
Paul Robinson75c068c2017-06-26 18:43:01 +0000328 if (getVersion() >= 4)
Paul Robinson9d4eb692017-05-01 23:27:55 +0000329 MaxOpsPerInst = DebugLineData.getU8(OffsetPtr);
330 DefaultIsStmt = DebugLineData.getU8(OffsetPtr);
331 LineBase = DebugLineData.getU8(OffsetPtr);
332 LineRange = DebugLineData.getU8(OffsetPtr);
333 OpcodeBase = DebugLineData.getU8(OffsetPtr);
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000334
335 StandardOpcodeLengths.reserve(OpcodeBase - 1);
Paul Robinson9d4eb692017-05-01 23:27:55 +0000336 for (uint32_t I = 1; I < OpcodeBase; ++I) {
337 uint8_t OpLen = DebugLineData.getU8(OffsetPtr);
338 StandardOpcodeLengths.push_back(OpLen);
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000339 }
340
Paul Robinson75c068c2017-06-26 18:43:01 +0000341 if (getVersion() >= 5) {
Paul Robinson2bc38732017-05-02 21:40:47 +0000342 if (!parseV5DirFileTables(DebugLineData, OffsetPtr, EndPrologueOffset,
Scott Linder16c7bda2018-02-23 23:01:06 +0000343 FormParams, Ctx, U, ContentTypes,
344 IncludeDirectories, FileNames)) {
James Hendersona3acf992018-05-10 10:51:33 +0000345 return createError(
Jonas Devlieghere84e99262018-04-14 22:07:23 +0000346 "parsing line table prologue at 0x%8.8" PRIx64
347 " found an invalid directory or file table description at"
James Hendersona3acf992018-05-10 10:51:33 +0000348 " 0x%8.8" PRIx64,
Jonas Devlieghere84e99262018-04-14 22:07:23 +0000349 PrologueOffset, (uint64_t)*OffsetPtr);
Paul Robinson2bc38732017-05-02 21:40:47 +0000350 }
351 } else
352 parseV2DirFileTables(DebugLineData, OffsetPtr, EndPrologueOffset,
Scott Linder16c7bda2018-02-23 23:01:06 +0000353 ContentTypes, IncludeDirectories, FileNames);
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000354
James Hendersona3acf992018-05-10 10:51:33 +0000355 if (*OffsetPtr != EndPrologueOffset)
356 return createError("parsing line table prologue at 0x%8.8" PRIx64
357 " should have ended at 0x%8.8" PRIx64
358 " but it ended at 0x%8.8" PRIx64,
359 PrologueOffset, EndPrologueOffset, (uint64_t)*OffsetPtr);
360 return Error::success();
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000361}
362
Paul Robinson9d4eb692017-05-01 23:27:55 +0000363DWARFDebugLine::Row::Row(bool DefaultIsStmt) { reset(DefaultIsStmt); }
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000364
Benjamin Kramer5acab502011-09-15 02:12:05 +0000365void DWARFDebugLine::Row::postAppend() {
366 BasicBlock = false;
367 PrologueEnd = false;
368 EpilogueBegin = false;
369}
370
Paul Robinson9d4eb692017-05-01 23:27:55 +0000371void DWARFDebugLine::Row::reset(bool DefaultIsStmt) {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000372 Address = 0;
373 Line = 1;
374 Column = 0;
375 File = 1;
376 Isa = 0;
Diego Novillo5b5cf502014-02-14 19:27:53 +0000377 Discriminator = 0;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000378 IsStmt = DefaultIsStmt;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000379 BasicBlock = false;
380 EndSequence = false;
381 PrologueEnd = false;
382 EpilogueBegin = false;
383}
384
Greg Clayton67070462017-05-02 22:48:52 +0000385void DWARFDebugLine::Row::dumpTableHeader(raw_ostream &OS) {
386 OS << "Address Line Column File ISA Discriminator Flags\n"
387 << "------------------ ------ ------ ------ --- ------------- "
388 "-------------\n";
389}
390
Benjamin Kramer5acab502011-09-15 02:12:05 +0000391void DWARFDebugLine::Row::dump(raw_ostream &OS) const {
Benjamin Kramerf3da5292011-11-05 08:57:40 +0000392 OS << format("0x%16.16" PRIx64 " %6u %6u", Address, Line, Column)
Diego Novillo5b5cf502014-02-14 19:27:53 +0000393 << format(" %6u %3u %13u ", File, Isa, Discriminator)
Dehao Chen1b54fce2016-04-28 22:09:37 +0000394 << (IsStmt ? " is_stmt" : "") << (BasicBlock ? " basic_block" : "")
Benjamin Kramer5acab502011-09-15 02:12:05 +0000395 << (PrologueEnd ? " prologue_end" : "")
396 << (EpilogueBegin ? " epilogue_begin" : "")
Dehao Chen1b54fce2016-04-28 22:09:37 +0000397 << (EndSequence ? " end_sequence" : "") << '\n';
Benjamin Kramer5acab502011-09-15 02:12:05 +0000398}
399
Dehao Chen1b54fce2016-04-28 22:09:37 +0000400DWARFDebugLine::Sequence::Sequence() { reset(); }
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000401
402void DWARFDebugLine::Sequence::reset() {
403 LowPC = 0;
404 HighPC = 0;
405 FirstRowIndex = 0;
406 LastRowIndex = 0;
407 Empty = true;
408}
409
Dehao Chen1b54fce2016-04-28 22:09:37 +0000410DWARFDebugLine::LineTable::LineTable() { clear(); }
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000411
Paul Robinson0a227092018-02-05 20:43:15 +0000412void DWARFDebugLine::LineTable::dump(raw_ostream &OS,
413 DIDumpOptions DumpOptions) const {
414 Prologue.dump(OS, DumpOptions);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000415 OS << '\n';
416
417 if (!Rows.empty()) {
Greg Clayton67070462017-05-02 22:48:52 +0000418 Row::dumpTableHeader(OS);
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000419 for (const Row &R : Rows) {
420 R.dump(OS);
421 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000422 }
423}
424
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000425void DWARFDebugLine::LineTable::clear() {
426 Prologue.clear();
427 Rows.clear();
428 Sequences.clear();
429}
430
Alexey Samsonov110d5952014-04-30 00:09:19 +0000431DWARFDebugLine::ParsingState::ParsingState(struct LineTable *LT)
Eugene Zelenko2db0cfa2017-06-23 21:57:40 +0000432 : LineTable(LT) {
Alexey Samsonov110d5952014-04-30 00:09:19 +0000433 resetRowAndSequence();
434}
Nick Lewycky4d044922011-09-15 03:41:51 +0000435
Alexey Samsonov110d5952014-04-30 00:09:19 +0000436void DWARFDebugLine::ParsingState::resetRowAndSequence() {
437 Row.reset(LineTable->Prologue.DefaultIsStmt);
438 Sequence.reset();
439}
440
Paul Robinson9d4eb692017-05-01 23:27:55 +0000441void DWARFDebugLine::ParsingState::appendRowToMatrix(uint32_t Offset) {
Alexey Samsonov110d5952014-04-30 00:09:19 +0000442 if (Sequence.Empty) {
Alexey Samsonov947228c2012-08-07 11:46:57 +0000443 // Record the beginning of instruction sequence.
Alexey Samsonov110d5952014-04-30 00:09:19 +0000444 Sequence.Empty = false;
445 Sequence.LowPC = Row.Address;
446 Sequence.FirstRowIndex = RowNumber;
Alexey Samsonov947228c2012-08-07 11:46:57 +0000447 }
Alexey Samsonov110d5952014-04-30 00:09:19 +0000448 ++RowNumber;
449 LineTable->appendRow(Row);
450 if (Row.EndSequence) {
Alexey Samsonov947228c2012-08-07 11:46:57 +0000451 // Record the end of instruction sequence.
Alexey Samsonov110d5952014-04-30 00:09:19 +0000452 Sequence.HighPC = Row.Address;
453 Sequence.LastRowIndex = RowNumber;
454 if (Sequence.isValid())
455 LineTable->appendSequence(Sequence);
456 Sequence.reset();
Alexey Samsonov947228c2012-08-07 11:46:57 +0000457 }
Alexey Samsonov110d5952014-04-30 00:09:19 +0000458 Row.postAppend();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000459}
460
Benjamin Kramer5acab502011-09-15 02:12:05 +0000461const DWARFDebugLine::LineTable *
Paul Robinson9d4eb692017-05-01 23:27:55 +0000462DWARFDebugLine::getLineTable(uint32_t Offset) const {
463 LineTableConstIter Pos = LineTableMap.find(Offset);
464 if (Pos != LineTableMap.end())
465 return &Pos->second;
Craig Topper2617dcc2014-04-15 06:32:26 +0000466 return nullptr;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000467}
468
James Hendersona3acf992018-05-10 10:51:33 +0000469Expected<const DWARFDebugLine::LineTable *> DWARFDebugLine::getOrParseLineTable(
470 DWARFDataExtractor &DebugLineData, uint32_t Offset, const DWARFContext &Ctx,
James Henderson004b7292018-05-21 15:30:54 +0000471 const DWARFUnit *U, std::function<void(Error)> RecoverableErrorCallback) {
James Henderson66702622018-03-08 10:53:34 +0000472 if (!DebugLineData.isValidOffset(Offset))
James Henderson11a9de72018-05-10 12:15:43 +0000473 return createError("offset 0x%8.8" PRIx32
James Hendersona3acf992018-05-10 10:51:33 +0000474 " is not a valid debug line section offset",
475 Offset);
James Henderson66702622018-03-08 10:53:34 +0000476
Paul Robinson9d4eb692017-05-01 23:27:55 +0000477 std::pair<LineTableIter, bool> Pos =
478 LineTableMap.insert(LineTableMapTy::value_type(Offset, LineTable()));
479 LineTable *LT = &Pos.first->second;
480 if (Pos.second) {
James Henderson004b7292018-05-21 15:30:54 +0000481 if (Error Err =
482 LT->parse(DebugLineData, &Offset, Ctx, U, RecoverableErrorCallback))
James Hendersona3acf992018-05-10 10:51:33 +0000483 return std::move(Err);
484 return LT;
Benjamin Kramer679e1752011-09-15 20:43:18 +0000485 }
Alexey Samsonov110d5952014-04-30 00:09:19 +0000486 return LT;
Benjamin Kramer679e1752011-09-15 20:43:18 +0000487}
488
James Hendersona3acf992018-05-10 10:51:33 +0000489Error DWARFDebugLine::LineTable::parse(
490 DWARFDataExtractor &DebugLineData, uint32_t *OffsetPtr,
491 const DWARFContext &Ctx, const DWARFUnit *U,
James Henderson004b7292018-05-21 15:30:54 +0000492 std::function<void(Error)> RecoverableErrorCallback, raw_ostream *OS) {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000493 const uint32_t DebugLineOffset = *OffsetPtr;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000494
Alexey Samsonov110d5952014-04-30 00:09:19 +0000495 clear();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000496
James Hendersona3acf992018-05-10 10:51:33 +0000497 Error PrologueErr = Prologue.parse(DebugLineData, OffsetPtr, Ctx, U);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000498
Paul Robinson0a227092018-02-05 20:43:15 +0000499 if (OS) {
500 // The presence of OS signals verbose dumping.
501 DIDumpOptions DumpOptions;
502 DumpOptions.Verbose = true;
503 Prologue.dump(*OS, DumpOptions);
504 }
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000505
James Hendersona3acf992018-05-10 10:51:33 +0000506 if (PrologueErr)
507 return PrologueErr;
508
Paul Robinson9d4eb692017-05-01 23:27:55 +0000509 const uint32_t EndOffset =
510 DebugLineOffset + Prologue.TotalLength + Prologue.sizeofTotalLength();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000511
Paul Robinson511b54c2017-11-22 15:48:30 +0000512 // See if we should tell the data extractor the address size.
513 if (DebugLineData.getAddressSize() == 0)
514 DebugLineData.setAddressSize(Prologue.getAddressSize());
515 else
516 assert(Prologue.getAddressSize() == 0 ||
517 Prologue.getAddressSize() == DebugLineData.getAddressSize());
518
Alexey Samsonov110d5952014-04-30 00:09:19 +0000519 ParsingState State(this);
Benjamin Kramer112ec172011-09-15 21:59:13 +0000520
Paul Robinson9d4eb692017-05-01 23:27:55 +0000521 while (*OffsetPtr < EndOffset) {
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000522 if (OS)
523 *OS << format("0x%08.08" PRIx32 ": ", *OffsetPtr);
524
Paul Robinson9d4eb692017-05-01 23:27:55 +0000525 uint8_t Opcode = DebugLineData.getU8(OffsetPtr);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000526
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000527 if (OS)
528 *OS << format("%02.02" PRIx8 " ", Opcode);
529
Paul Robinson9d4eb692017-05-01 23:27:55 +0000530 if (Opcode == 0) {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000531 // Extended Opcodes always start with a zero opcode followed by
532 // a uleb128 length so you can skip ones you don't know about
Paul Robinson9d4eb692017-05-01 23:27:55 +0000533 uint64_t Len = DebugLineData.getULEB128(OffsetPtr);
Paul Robinsone0833342017-11-22 15:14:49 +0000534 uint32_t ExtOffset = *OffsetPtr;
535
536 // Tolerate zero-length; assume length is correct and soldier on.
537 if (Len == 0) {
538 if (OS)
539 *OS << "Badly formed extended line op (length 0)\n";
540 continue;
541 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000542
Paul Robinson9d4eb692017-05-01 23:27:55 +0000543 uint8_t SubOpcode = DebugLineData.getU8(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000544 if (OS)
545 *OS << LNExtendedString(SubOpcode);
Paul Robinson9d4eb692017-05-01 23:27:55 +0000546 switch (SubOpcode) {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000547 case DW_LNE_end_sequence:
548 // Set the end_sequence register of the state machine to true and
549 // append a row to the matrix using the current values of the
550 // state-machine registers. Then reset the registers to the initial
551 // values specified above. Every statement program sequence must end
552 // with a DW_LNE_end_sequence instruction which creates a row whose
553 // address is that of the byte after the last target machine instruction
554 // of the sequence.
Alexey Samsonov110d5952014-04-30 00:09:19 +0000555 State.Row.EndSequence = true;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000556 State.appendRowToMatrix(*OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000557 if (OS) {
558 *OS << "\n";
559 OS->indent(12);
560 State.Row.dump(*OS);
561 }
Alexey Samsonov110d5952014-04-30 00:09:19 +0000562 State.resetRowAndSequence();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000563 break;
564
565 case DW_LNE_set_address:
566 // Takes a single relocatable address as an operand. The size of the
567 // operand is the size appropriate to hold an address on the target
568 // machine. Set the address register to the value given by the
569 // relocatable address. All of the other statement program opcodes
570 // that affect the address register add a delta to it. This instruction
571 // stores a relocatable value into it instead.
Paul Robinson511b54c2017-11-22 15:48:30 +0000572 //
573 // Make sure the extractor knows the address size. If not, infer it
574 // from the size of the operand.
575 if (DebugLineData.getAddressSize() == 0)
576 DebugLineData.setAddressSize(Len - 1);
Paul Robinson79474682018-03-22 19:37:56 +0000577 else if (DebugLineData.getAddressSize() != Len - 1) {
James Hendersona3acf992018-05-10 10:51:33 +0000578 return createError("mismatching address size at offset 0x%8.8" PRIx32
579 " expected 0x%2.2" PRIx8 " found 0x%2.2" PRIx64,
580 ExtOffset, DebugLineData.getAddressSize(),
581 Len - 1);
Paul Robinson79474682018-03-22 19:37:56 +0000582 }
Paul Robinson17536b92017-06-29 16:52:08 +0000583 State.Row.Address = DebugLineData.getRelocatedAddress(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000584 if (OS)
585 *OS << format(" (0x%16.16" PRIx64 ")", State.Row.Address);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000586 break;
587
588 case DW_LNE_define_file:
589 // Takes 4 arguments. The first is a null terminated string containing
590 // a source file name. The second is an unsigned LEB128 number
591 // representing the directory index of the directory in which the file
592 // was found. The third is an unsigned LEB128 number representing the
593 // time of last modification of the file. The fourth is an unsigned
594 // LEB128 number representing the length in bytes of the file. The time
595 // and length fields may contain LEB128(0) if the information is not
596 // available.
597 //
598 // The directory index represents an entry in the include_directories
599 // section of the statement program prologue. The index is LEB128(0)
600 // if the file was found in the current directory of the compilation,
601 // LEB128(1) if it was found in the first directory in the
602 // include_directories section, and so on. The directory index is
603 // ignored for file names that represent full path names.
604 //
605 // The files are numbered, starting at 1, in the order in which they
606 // appear; the names in the prologue come before names defined by
607 // the DW_LNE_define_file instruction. These numbers are used in the
608 // the file register of the state machine.
609 {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000610 FileNameEntry FileEntry;
Paul Robinson0a227092018-02-05 20:43:15 +0000611 const char *Name = DebugLineData.getCStr(OffsetPtr);
612 FileEntry.Name.setForm(dwarf::DW_FORM_string);
613 FileEntry.Name.setPValue(Name);
Paul Robinson9d4eb692017-05-01 23:27:55 +0000614 FileEntry.DirIdx = DebugLineData.getULEB128(OffsetPtr);
615 FileEntry.ModTime = DebugLineData.getULEB128(OffsetPtr);
616 FileEntry.Length = DebugLineData.getULEB128(OffsetPtr);
617 Prologue.FileNames.push_back(FileEntry);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000618 if (OS)
Paul Robinson0a227092018-02-05 20:43:15 +0000619 *OS << " (" << Name << ", dir=" << FileEntry.DirIdx << ", mod_time="
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000620 << format("(0x%16.16" PRIx64 ")", FileEntry.ModTime)
621 << ", length=" << FileEntry.Length << ")";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000622 }
623 break;
624
Diego Novillo5b5cf502014-02-14 19:27:53 +0000625 case DW_LNE_set_discriminator:
Paul Robinson9d4eb692017-05-01 23:27:55 +0000626 State.Row.Discriminator = DebugLineData.getULEB128(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000627 if (OS)
628 *OS << " (" << State.Row.Discriminator << ")";
Diego Novillo5b5cf502014-02-14 19:27:53 +0000629 break;
630
Benjamin Kramer5acab502011-09-15 02:12:05 +0000631 default:
Paul Robinsone0833342017-11-22 15:14:49 +0000632 if (OS)
633 *OS << format("Unrecognized extended op 0x%02.02" PRIx8, SubOpcode)
634 << format(" length %" PRIx64, Len);
635 // Len doesn't include the zero opcode byte or the length itself, but
636 // it does include the sub_opcode, so we have to adjust for that.
637 (*OffsetPtr) += Len - 1;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000638 break;
639 }
Paul Robinsone0833342017-11-22 15:14:49 +0000640 // Make sure the stated and parsed lengths are the same.
641 // Otherwise we have an unparseable line-number program.
James Hendersona3acf992018-05-10 10:51:33 +0000642 if (*OffsetPtr - ExtOffset != Len)
643 return createError("unexpected line op length at offset 0x%8.8" PRIx32
644 " expected 0x%2.2" PRIx64 " found 0x%2.2" PRIx32,
645 ExtOffset, Len, *OffsetPtr - ExtOffset);
Paul Robinson9d4eb692017-05-01 23:27:55 +0000646 } else if (Opcode < Prologue.OpcodeBase) {
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000647 if (OS)
648 *OS << LNStandardString(Opcode);
Paul Robinson9d4eb692017-05-01 23:27:55 +0000649 switch (Opcode) {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000650 // Standard Opcodes
651 case DW_LNS_copy:
652 // Takes no arguments. Append a row to the matrix using the
653 // current values of the state-machine registers. Then set
654 // the basic_block register to false.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000655 State.appendRowToMatrix(*OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000656 if (OS) {
657 *OS << "\n";
658 OS->indent(12);
659 State.Row.dump(*OS);
660 *OS << "\n";
661 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000662 break;
663
664 case DW_LNS_advance_pc:
665 // Takes a single unsigned LEB128 operand, multiplies it by the
666 // min_inst_length field of the prologue, and adds the
667 // result to the address register of the state machine.
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000668 {
669 uint64_t AddrOffset =
670 DebugLineData.getULEB128(OffsetPtr) * Prologue.MinInstLength;
671 State.Row.Address += AddrOffset;
672 if (OS)
673 *OS << " (" << AddrOffset << ")";
674 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000675 break;
676
677 case DW_LNS_advance_line:
678 // Takes a single signed LEB128 operand and adds that value to
679 // the line register of the state machine.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000680 State.Row.Line += DebugLineData.getSLEB128(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000681 if (OS)
682 *OS << " (" << State.Row.Line << ")";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000683 break;
684
685 case DW_LNS_set_file:
686 // Takes a single unsigned LEB128 operand and stores it in the file
687 // register of the state machine.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000688 State.Row.File = DebugLineData.getULEB128(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000689 if (OS)
690 *OS << " (" << State.Row.File << ")";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000691 break;
692
693 case DW_LNS_set_column:
694 // Takes a single unsigned LEB128 operand and stores it in the
695 // column register of the state machine.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000696 State.Row.Column = DebugLineData.getULEB128(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000697 if (OS)
698 *OS << " (" << State.Row.Column << ")";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000699 break;
700
701 case DW_LNS_negate_stmt:
702 // Takes no arguments. Set the is_stmt register of the state
703 // machine to the logical negation of its current value.
Alexey Samsonov110d5952014-04-30 00:09:19 +0000704 State.Row.IsStmt = !State.Row.IsStmt;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000705 break;
706
707 case DW_LNS_set_basic_block:
708 // Takes no arguments. Set the basic_block register of the
709 // state machine to true
Alexey Samsonov110d5952014-04-30 00:09:19 +0000710 State.Row.BasicBlock = true;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000711 break;
712
713 case DW_LNS_const_add_pc:
714 // Takes no arguments. Add to the address register of the state
715 // machine the address increment value corresponding to special
716 // opcode 255. The motivation for DW_LNS_const_add_pc is this:
717 // when the statement program needs to advance the address by a
718 // small amount, it can use a single special opcode, which occupies
719 // a single byte. When it needs to advance the address by up to
720 // twice the range of the last special opcode, it can use
721 // DW_LNS_const_add_pc followed by a special opcode, for a total
722 // of two bytes. Only if it needs to advance the address by more
723 // than twice that range will it need to use both DW_LNS_advance_pc
724 // and a special opcode, requiring three or more bytes.
725 {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000726 uint8_t AdjustOpcode = 255 - Prologue.OpcodeBase;
727 uint64_t AddrOffset =
728 (AdjustOpcode / Prologue.LineRange) * Prologue.MinInstLength;
729 State.Row.Address += AddrOffset;
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000730 if (OS)
731 *OS
732 << format(" (0x%16.16" PRIx64 ")", AddrOffset);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000733 }
734 break;
735
736 case DW_LNS_fixed_advance_pc:
737 // Takes a single uhalf operand. Add to the address register of
738 // the state machine the value of the (unencoded) operand. This
739 // is the only extended opcode that takes an argument that is not
740 // a variable length number. The motivation for DW_LNS_fixed_advance_pc
741 // is this: existing assemblers cannot emit DW_LNS_advance_pc or
742 // special opcodes because they cannot encode LEB128 numbers or
743 // judge when the computation of a special opcode overflows and
744 // requires the use of DW_LNS_advance_pc. Such assemblers, however,
745 // can use DW_LNS_fixed_advance_pc instead, sacrificing compression.
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000746 {
747 uint16_t PCOffset = DebugLineData.getU16(OffsetPtr);
748 State.Row.Address += PCOffset;
749 if (OS)
750 *OS
751 << format(" (0x%16.16" PRIx64 ")", PCOffset);
752 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000753 break;
754
755 case DW_LNS_set_prologue_end:
756 // Takes no arguments. Set the prologue_end register of the
757 // state machine to true
Alexey Samsonov110d5952014-04-30 00:09:19 +0000758 State.Row.PrologueEnd = true;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000759 break;
760
761 case DW_LNS_set_epilogue_begin:
762 // Takes no arguments. Set the basic_block register of the
763 // state machine to true
Alexey Samsonov110d5952014-04-30 00:09:19 +0000764 State.Row.EpilogueBegin = true;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000765 break;
766
767 case DW_LNS_set_isa:
768 // Takes a single unsigned LEB128 operand and stores it in the
769 // column register of the state machine.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000770 State.Row.Isa = DebugLineData.getULEB128(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000771 if (OS)
772 *OS << " (" << State.Row.Isa << ")";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000773 break;
774
775 default:
776 // Handle any unknown standard opcodes here. We know the lengths
777 // of such opcodes because they are specified in the prologue
778 // as a multiple of LEB128 operands for each opcode.
779 {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000780 assert(Opcode - 1U < Prologue.StandardOpcodeLengths.size());
781 uint8_t OpcodeLength = Prologue.StandardOpcodeLengths[Opcode - 1];
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000782 for (uint8_t I = 0; I < OpcodeLength; ++I) {
783 uint64_t Value = DebugLineData.getULEB128(OffsetPtr);
784 if (OS)
785 *OS << format("Skipping ULEB128 value: 0x%16.16" PRIx64 ")\n",
786 Value);
787 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000788 }
789 break;
790 }
791 } else {
792 // Special Opcodes
793
794 // A special opcode value is chosen based on the amount that needs
795 // to be added to the line and address registers. The maximum line
796 // increment for a special opcode is the value of the line_base
797 // field in the header, plus the value of the line_range field,
798 // minus 1 (line base + line range - 1). If the desired line
799 // increment is greater than the maximum line increment, a standard
NAKAMURA Takumif9959852011-10-08 11:22:47 +0000800 // opcode must be used instead of a special opcode. The "address
801 // advance" is calculated by dividing the desired address increment
Benjamin Kramer5acab502011-09-15 02:12:05 +0000802 // by the minimum_instruction_length field from the header. The
803 // special opcode is then calculated using the following formula:
804 //
805 // opcode = (desired line increment - line_base) +
806 // (line_range * address advance) + opcode_base
807 //
808 // If the resulting opcode is greater than 255, a standard opcode
809 // must be used instead.
810 //
811 // To decode a special opcode, subtract the opcode_base from the
812 // opcode itself to give the adjusted opcode. The amount to
813 // increment the address register is the result of the adjusted
814 // opcode divided by the line_range multiplied by the
815 // minimum_instruction_length field from the header. That is:
816 //
817 // address increment = (adjusted opcode / line_range) *
818 // minimum_instruction_length
819 //
820 // The amount to increment the line register is the line_base plus
821 // the result of the adjusted opcode modulo the line_range. That is:
822 //
823 // line increment = line_base + (adjusted opcode % line_range)
824
Paul Robinson9d4eb692017-05-01 23:27:55 +0000825 uint8_t AdjustOpcode = Opcode - Prologue.OpcodeBase;
826 uint64_t AddrOffset =
827 (AdjustOpcode / Prologue.LineRange) * Prologue.MinInstLength;
828 int32_t LineOffset =
829 Prologue.LineBase + (AdjustOpcode % Prologue.LineRange);
830 State.Row.Line += LineOffset;
831 State.Row.Address += AddrOffset;
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000832
833 if (OS) {
834 *OS << "address += " << ((uint32_t)AdjustOpcode)
835 << ", line += " << LineOffset << "\n";
836 OS->indent(12);
837 State.Row.dump(*OS);
838 }
839
Paul Robinson9d4eb692017-05-01 23:27:55 +0000840 State.appendRowToMatrix(*OffsetPtr);
Dehao Chen1b54fce2016-04-28 22:09:37 +0000841 // Reset discriminator to 0.
842 State.Row.Discriminator = 0;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000843 }
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000844 if(OS)
845 *OS << "\n";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000846 }
847
Jonas Devlieghere84e99262018-04-14 22:07:23 +0000848 if (!State.Sequence.Empty)
James Henderson004b7292018-05-21 15:30:54 +0000849 RecoverableErrorCallback(
850 createError("last sequence in debug line table is not terminated!"));
Alexey Samsonov110d5952014-04-30 00:09:19 +0000851
852 // Sort all sequences so that address lookup will work faster.
853 if (!Sequences.empty()) {
Mandeep Singh Grangfe1d28e2018-04-01 16:18:49 +0000854 llvm::sort(Sequences.begin(), Sequences.end(), Sequence::orderByLowPC);
Alexey Samsonov110d5952014-04-30 00:09:19 +0000855 // Note: actually, instruction address ranges of sequences should not
856 // overlap (in shared objects and executables). If they do, the address
857 // lookup would still work, though, but result would be ambiguous.
858 // We don't report warning in this case. For example,
859 // sometimes .so compiled from multiple object files contains a few
860 // rudimentary sequences for address ranges [0x0, 0xsomething).
861 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000862
James Hendersona3acf992018-05-10 10:51:33 +0000863 return Error::success();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000864}
865
Keno Fischerc2c60182015-05-31 23:37:04 +0000866uint32_t
Paul Robinson9d4eb692017-05-01 23:27:55 +0000867DWARFDebugLine::LineTable::findRowInSeq(const DWARFDebugLine::Sequence &Seq,
868 uint64_t Address) const {
869 if (!Seq.containsPC(Address))
Keno Fischerc2c60182015-05-31 23:37:04 +0000870 return UnknownRowIndex;
871 // Search for instruction address in the rows describing the sequence.
872 // Rows are stored in a vector, so we may use arithmetical operations with
873 // iterators.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000874 DWARFDebugLine::Row Row;
875 Row.Address = Address;
876 RowIter FirstRow = Rows.begin() + Seq.FirstRowIndex;
877 RowIter LastRow = Rows.begin() + Seq.LastRowIndex;
878 LineTable::RowIter RowPos = std::lower_bound(
879 FirstRow, LastRow, Row, DWARFDebugLine::Row::orderByAddress);
880 if (RowPos == LastRow) {
881 return Seq.LastRowIndex - 1;
Keno Fischerc2c60182015-05-31 23:37:04 +0000882 }
Paul Robinson9d4eb692017-05-01 23:27:55 +0000883 uint32_t Index = Seq.FirstRowIndex + (RowPos - FirstRow);
884 if (RowPos->Address > Address) {
885 if (RowPos == FirstRow)
Keno Fischerc2c60182015-05-31 23:37:04 +0000886 return UnknownRowIndex;
887 else
Paul Robinson9d4eb692017-05-01 23:27:55 +0000888 Index--;
Keno Fischerc2c60182015-05-31 23:37:04 +0000889 }
Paul Robinson9d4eb692017-05-01 23:27:55 +0000890 return Index;
Keno Fischerc2c60182015-05-31 23:37:04 +0000891}
892
Paul Robinson9d4eb692017-05-01 23:27:55 +0000893uint32_t DWARFDebugLine::LineTable::lookupAddress(uint64_t Address) const {
Alexey Samsonov947228c2012-08-07 11:46:57 +0000894 if (Sequences.empty())
Keno Fischerc2c60182015-05-31 23:37:04 +0000895 return UnknownRowIndex;
Alexey Samsonov947228c2012-08-07 11:46:57 +0000896 // First, find an instruction sequence containing the given address.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000897 DWARFDebugLine::Sequence Sequence;
898 Sequence.LowPC = Address;
899 SequenceIter FirstSeq = Sequences.begin();
900 SequenceIter LastSeq = Sequences.end();
901 SequenceIter SeqPos = std::lower_bound(
902 FirstSeq, LastSeq, Sequence, DWARFDebugLine::Sequence::orderByLowPC);
903 DWARFDebugLine::Sequence FoundSeq;
904 if (SeqPos == LastSeq) {
905 FoundSeq = Sequences.back();
906 } else if (SeqPos->LowPC == Address) {
907 FoundSeq = *SeqPos;
Alexey Samsonov947228c2012-08-07 11:46:57 +0000908 } else {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000909 if (SeqPos == FirstSeq)
Keno Fischerc2c60182015-05-31 23:37:04 +0000910 return UnknownRowIndex;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000911 FoundSeq = *(SeqPos - 1);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000912 }
Paul Robinson9d4eb692017-05-01 23:27:55 +0000913 return findRowInSeq(FoundSeq, Address);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000914}
Alexey Samsonov45be7932012-08-30 07:49:50 +0000915
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000916bool DWARFDebugLine::LineTable::lookupAddressRange(
Paul Robinson9d4eb692017-05-01 23:27:55 +0000917 uint64_t Address, uint64_t Size, std::vector<uint32_t> &Result) const {
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000918 if (Sequences.empty())
919 return false;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000920 uint64_t EndAddr = Address + Size;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000921 // First, find an instruction sequence containing the given address.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000922 DWARFDebugLine::Sequence Sequence;
923 Sequence.LowPC = Address;
924 SequenceIter FirstSeq = Sequences.begin();
925 SequenceIter LastSeq = Sequences.end();
926 SequenceIter SeqPos = std::lower_bound(
927 FirstSeq, LastSeq, Sequence, DWARFDebugLine::Sequence::orderByLowPC);
928 if (SeqPos == LastSeq || SeqPos->LowPC != Address) {
929 if (SeqPos == FirstSeq)
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000930 return false;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000931 SeqPos--;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000932 }
Paul Robinson9d4eb692017-05-01 23:27:55 +0000933 if (!SeqPos->containsPC(Address))
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000934 return false;
935
Paul Robinson9d4eb692017-05-01 23:27:55 +0000936 SequenceIter StartPos = SeqPos;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000937
938 // Add the rows from the first sequence to the vector, starting with the
939 // index we just calculated
940
Paul Robinson9d4eb692017-05-01 23:27:55 +0000941 while (SeqPos != LastSeq && SeqPos->LowPC < EndAddr) {
942 const DWARFDebugLine::Sequence &CurSeq = *SeqPos;
Keno Fischerc2c60182015-05-31 23:37:04 +0000943 // For the first sequence, we need to find which row in the sequence is the
944 // first in our range.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000945 uint32_t FirstRowIndex = CurSeq.FirstRowIndex;
946 if (SeqPos == StartPos)
947 FirstRowIndex = findRowInSeq(CurSeq, Address);
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000948
Keno Fischerc2c60182015-05-31 23:37:04 +0000949 // Figure out the last row in the range.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000950 uint32_t LastRowIndex = findRowInSeq(CurSeq, EndAddr - 1);
951 if (LastRowIndex == UnknownRowIndex)
952 LastRowIndex = CurSeq.LastRowIndex - 1;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000953
Paul Robinson9d4eb692017-05-01 23:27:55 +0000954 assert(FirstRowIndex != UnknownRowIndex);
955 assert(LastRowIndex != UnknownRowIndex);
Keno Fischerc2c60182015-05-31 23:37:04 +0000956
Paul Robinson9d4eb692017-05-01 23:27:55 +0000957 for (uint32_t I = FirstRowIndex; I <= LastRowIndex; ++I) {
958 Result.push_back(I);
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000959 }
960
Paul Robinson9d4eb692017-05-01 23:27:55 +0000961 ++SeqPos;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000962 }
NAKAMURA Takumi4b86cdb2013-01-26 01:45:06 +0000963
964 return true;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000965}
966
Paul Robinson9d4eb692017-05-01 23:27:55 +0000967bool DWARFDebugLine::LineTable::hasFileAtIndex(uint64_t FileIndex) const {
Pete Cooperb2ba7762016-07-22 01:41:32 +0000968 return FileIndex != 0 && FileIndex <= Prologue.FileNames.size();
969}
970
Scott Linder16c7bda2018-02-23 23:01:06 +0000971Optional<StringRef> DWARFDebugLine::LineTable::getSourceByIndex(uint64_t FileIndex,
972 FileLineInfoKind Kind) const {
973 if (Kind == FileLineInfoKind::None || !hasFileAtIndex(FileIndex))
974 return None;
975 const FileNameEntry &Entry = Prologue.FileNames[FileIndex - 1];
976 if (Optional<const char *> source = Entry.Source.getAsCString())
977 return StringRef(*source);
978 return None;
979}
980
Eugene Zemtsov82d60d62018-03-13 17:54:29 +0000981static bool isPathAbsoluteOnWindowsOrPosix(const Twine &Path) {
982 // Debug info can contain paths from any OS, not necessarily
983 // an OS we're currently running on. Moreover different compilation units can
984 // be compiled on different operating systems and linked together later.
985 return sys::path::is_absolute(Path, sys::path::Style::posix) ||
986 sys::path::is_absolute(Path, sys::path::Style::windows);
987}
988
Paul Robinson9d4eb692017-05-01 23:27:55 +0000989bool DWARFDebugLine::LineTable::getFileNameByIndex(uint64_t FileIndex,
990 const char *CompDir,
991 FileLineInfoKind Kind,
992 std::string &Result) const {
Pete Cooperb2ba7762016-07-22 01:41:32 +0000993 if (Kind == FileLineInfoKind::None || !hasFileAtIndex(FileIndex))
Alexey Samsonov45be7932012-08-30 07:49:50 +0000994 return false;
995 const FileNameEntry &Entry = Prologue.FileNames[FileIndex - 1];
Paul Robinson0a227092018-02-05 20:43:15 +0000996 StringRef FileName = Entry.Name.getAsCString().getValue();
Alexey Samsonovdce67342014-05-15 21:24:32 +0000997 if (Kind != FileLineInfoKind::AbsoluteFilePath ||
Eugene Zemtsov82d60d62018-03-13 17:54:29 +0000998 isPathAbsoluteOnWindowsOrPosix(FileName)) {
Alexey Samsonov45be7932012-08-30 07:49:50 +0000999 Result = FileName;
1000 return true;
1001 }
Frederic Riss101b5e22014-09-19 15:11:51 +00001002
Alexey Samsonov45be7932012-08-30 07:49:50 +00001003 SmallString<16> FilePath;
1004 uint64_t IncludeDirIndex = Entry.DirIdx;
Paul Robinsonba1c9152017-05-02 17:37:32 +00001005 StringRef IncludeDir;
Alexey Samsonov45be7932012-08-30 07:49:50 +00001006 // Be defensive about the contents of Entry.
1007 if (IncludeDirIndex > 0 &&
Frederic Riss101b5e22014-09-19 15:11:51 +00001008 IncludeDirIndex <= Prologue.IncludeDirectories.size())
Paul Robinson0a227092018-02-05 20:43:15 +00001009 IncludeDir = Prologue.IncludeDirectories[IncludeDirIndex - 1]
1010 .getAsCString()
1011 .getValue();
Frederic Riss101b5e22014-09-19 15:11:51 +00001012
1013 // We may still need to append compilation directory of compile unit.
1014 // We know that FileName is not absolute, the only way to have an
1015 // absolute path at this point would be if IncludeDir is absolute.
1016 if (CompDir && Kind == FileLineInfoKind::AbsoluteFilePath &&
Eugene Zemtsov82d60d62018-03-13 17:54:29 +00001017 !isPathAbsoluteOnWindowsOrPosix(IncludeDir))
Frederic Riss101b5e22014-09-19 15:11:51 +00001018 sys::path::append(FilePath, CompDir);
1019
1020 // sys::path::append skips empty strings.
1021 sys::path::append(FilePath, IncludeDir, FileName);
Alexey Samsonov45be7932012-08-30 07:49:50 +00001022 Result = FilePath.str();
1023 return true;
1024}
Frederic Riss101b5e22014-09-19 15:11:51 +00001025
Dehao Chen1b54fce2016-04-28 22:09:37 +00001026bool DWARFDebugLine::LineTable::getFileLineInfoForAddress(
1027 uint64_t Address, const char *CompDir, FileLineInfoKind Kind,
1028 DILineInfo &Result) const {
Frederic Riss101b5e22014-09-19 15:11:51 +00001029 // Get the index of row we're looking for in the line table.
1030 uint32_t RowIndex = lookupAddress(Address);
1031 if (RowIndex == -1U)
1032 return false;
1033 // Take file number and line/column from the row.
1034 const auto &Row = Rows[RowIndex];
1035 if (!getFileNameByIndex(Row.File, CompDir, Kind, Result.FileName))
1036 return false;
1037 Result.Line = Row.Line;
1038 Result.Column = Row.Column;
Eric Christopherba1024c2016-12-14 18:29:39 +00001039 Result.Discriminator = Row.Discriminator;
Scott Linder16c7bda2018-02-23 23:01:06 +00001040 Result.Source = getSourceByIndex(Row.File, Kind);
Frederic Riss101b5e22014-09-19 15:11:51 +00001041 return true;
1042}
James Hendersona3acf992018-05-10 10:51:33 +00001043
1044// We want to supply the Unit associated with a .debug_line[.dwo] table when
1045// we dump it, if possible, but still dump the table even if there isn't a Unit.
1046// Therefore, collect up handles on all the Units that point into the
1047// line-table section.
1048static DWARFDebugLine::SectionParser::LineToUnitMap
1049buildLineToUnitMap(DWARFDebugLine::SectionParser::cu_range CUs,
Paul Robinson7f330942018-08-01 20:46:46 +00001050 DWARFDebugLine::SectionParser::tu_range TUs) {
James Hendersona3acf992018-05-10 10:51:33 +00001051 DWARFDebugLine::SectionParser::LineToUnitMap LineToUnit;
1052 for (const auto &CU : CUs)
1053 if (auto CUDIE = CU->getUnitDIE())
1054 if (auto StmtOffset = toSectionOffset(CUDIE.find(DW_AT_stmt_list)))
1055 LineToUnit.insert(std::make_pair(*StmtOffset, &*CU));
Paul Robinson7f330942018-08-01 20:46:46 +00001056 for (const auto &TU : TUs)
1057 if (auto TUDIE = TU->getUnitDIE())
1058 if (auto StmtOffset = toSectionOffset(TUDIE.find(DW_AT_stmt_list)))
1059 LineToUnit.insert(std::make_pair(*StmtOffset, &*TU));
James Hendersona3acf992018-05-10 10:51:33 +00001060 return LineToUnit;
1061}
1062
1063DWARFDebugLine::SectionParser::SectionParser(DWARFDataExtractor &Data,
1064 const DWARFContext &C,
1065 cu_range CUs, tu_range TUs)
1066 : DebugLineData(Data), Context(C) {
1067 LineToUnit = buildLineToUnitMap(CUs, TUs);
1068 if (!DebugLineData.isValidOffset(Offset))
1069 Done = true;
1070}
1071
1072bool DWARFDebugLine::Prologue::totalLengthIsValid() const {
1073 return TotalLength == 0xffffffff || TotalLength < 0xffffff00;
1074}
1075
1076DWARFDebugLine::LineTable DWARFDebugLine::SectionParser::parseNext(
James Henderson004b7292018-05-21 15:30:54 +00001077 function_ref<void(Error)> RecoverableErrorCallback,
1078 function_ref<void(Error)> UnrecoverableErrorCallback, raw_ostream *OS) {
James Hendersona3acf992018-05-10 10:51:33 +00001079 assert(DebugLineData.isValidOffset(Offset) &&
1080 "parsing should have terminated");
1081 DWARFUnit *U = prepareToParse(Offset);
1082 uint32_t OldOffset = Offset;
1083 LineTable LT;
James Henderson004b7292018-05-21 15:30:54 +00001084 if (Error Err = LT.parse(DebugLineData, &Offset, Context, U,
1085 RecoverableErrorCallback, OS))
1086 UnrecoverableErrorCallback(std::move(Err));
James Hendersona3acf992018-05-10 10:51:33 +00001087 moveToNextTable(OldOffset, LT.Prologue);
1088 return LT;
1089}
1090
1091void DWARFDebugLine::SectionParser::skip(
1092 function_ref<void(Error)> ErrorCallback) {
1093 assert(DebugLineData.isValidOffset(Offset) &&
1094 "parsing should have terminated");
1095 DWARFUnit *U = prepareToParse(Offset);
1096 uint32_t OldOffset = Offset;
1097 LineTable LT;
James Henderson004b7292018-05-21 15:30:54 +00001098 if (Error Err = LT.Prologue.parse(DebugLineData, &Offset, Context, U))
1099 ErrorCallback(std::move(Err));
James Hendersona3acf992018-05-10 10:51:33 +00001100 moveToNextTable(OldOffset, LT.Prologue);
1101}
1102
1103DWARFUnit *DWARFDebugLine::SectionParser::prepareToParse(uint32_t Offset) {
1104 DWARFUnit *U = nullptr;
1105 auto It = LineToUnit.find(Offset);
1106 if (It != LineToUnit.end())
1107 U = It->second;
1108 DebugLineData.setAddressSize(U ? U->getAddressByteSize() : 0);
1109 return U;
1110}
1111
1112void DWARFDebugLine::SectionParser::moveToNextTable(uint32_t OldOffset,
1113 const Prologue &P) {
1114 // If the length field is not valid, we don't know where the next table is, so
1115 // cannot continue to parse. Mark the parser as done, and leave the Offset
1116 // value as it currently is. This will be the end of the bad length field.
1117 if (!P.totalLengthIsValid()) {
1118 Done = true;
1119 return;
1120 }
1121
1122 Offset = OldOffset + P.TotalLength + P.sizeofTotalLength();
1123 if (!DebugLineData.isValidOffset(Offset)) {
1124 Done = true;
1125 }
1126}
1127
James Henderson004b7292018-05-21 15:30:54 +00001128void DWARFDebugLine::warn(Error Err) {
1129 handleAllErrors(std::move(Err), [](ErrorInfoBase &Info) {
1130 WithColor::warning() << Info.message() << '\n';
1131 });
James Hendersona3acf992018-05-10 10:51:33 +00001132}