blob: 3d16e740f9f1f91c12281d8c6cd3056083e54e35 [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"
Victor Leschukcba595d2018-08-20 09:59:08 +000018#include "llvm/Support/Errc.h"
Benjamin Kramer5acab502011-09-15 02:12:05 +000019#include "llvm/Support/Format.h"
Alexey Samsonov45be7932012-08-30 07:49:50 +000020#include "llvm/Support/Path.h"
Jonas Devlieghere84e99262018-04-14 22:07:23 +000021#include "llvm/Support/WithColor.h"
Benjamin Kramer5acab502011-09-15 02:12:05 +000022#include "llvm/Support/raw_ostream.h"
Benjamin Kramera57c46a2011-09-15 02:19:33 +000023#include <algorithm>
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000024#include <cassert>
25#include <cinttypes>
26#include <cstdint>
27#include <cstdio>
28#include <utility>
29
Benjamin Kramer5acab502011-09-15 02:12:05 +000030using namespace llvm;
31using namespace dwarf;
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000032
Eugene Zelenko2db0cfa2017-06-23 21:57:40 +000033using FileLineInfoKind = DILineInfoSpecifier::FileLineInfoKind;
34
Paul Robinson2bc38732017-05-02 21:40:47 +000035namespace {
Eugene Zelenko2db0cfa2017-06-23 21:57:40 +000036
Paul Robinson2bc38732017-05-02 21:40:47 +000037struct ContentDescriptor {
38 dwarf::LineNumberEntryFormat Type;
39 dwarf::Form Form;
40};
Eugene Zelenko2db0cfa2017-06-23 21:57:40 +000041
42using ContentDescriptors = SmallVector<ContentDescriptor, 4>;
43
Paul Robinson2bc38732017-05-02 21:40:47 +000044} // end anonmyous namespace
Benjamin Kramer5acab502011-09-15 02:12:05 +000045
Scott Linder16c7bda2018-02-23 23:01:06 +000046void DWARFDebugLine::ContentTypeTracker::trackContentType(
47 dwarf::LineNumberEntryFormat ContentType) {
48 switch (ContentType) {
49 case dwarf::DW_LNCT_timestamp:
50 HasModTime = true;
51 break;
52 case dwarf::DW_LNCT_size:
53 HasLength = true;
54 break;
55 case dwarf::DW_LNCT_MD5:
56 HasMD5 = true;
57 break;
58 case dwarf::DW_LNCT_LLVM_source:
59 HasSource = true;
60 break;
61 default:
62 // We only care about values we consider optional, and new values may be
63 // added in the vendor extension range, so we do not match exhaustively.
64 break;
65 }
66}
67
Dehao Chen1b54fce2016-04-28 22:09:37 +000068DWARFDebugLine::Prologue::Prologue() { clear(); }
Alexey Samsonov836b1ae2014-04-29 21:28:13 +000069
70void DWARFDebugLine::Prologue::clear() {
Paul Robinson75c068c2017-06-26 18:43:01 +000071 TotalLength = PrologueLength = 0;
72 SegSelectorSize = 0;
Alexey Samsonov836b1ae2014-04-29 21:28:13 +000073 MinInstLength = MaxOpsPerInst = DefaultIsStmt = LineBase = LineRange = 0;
74 OpcodeBase = 0;
Pavel Labath322711f2018-03-14 09:39:54 +000075 FormParams = dwarf::FormParams({0, 0, DWARF32});
Scott Linder16c7bda2018-02-23 23:01:06 +000076 ContentTypes = ContentTypeTracker();
Alexey Samsonov836b1ae2014-04-29 21:28:13 +000077 StandardOpcodeLengths.clear();
78 IncludeDirectories.clear();
79 FileNames.clear();
80}
81
Paul Robinson0a227092018-02-05 20:43:15 +000082void DWARFDebugLine::Prologue::dump(raw_ostream &OS,
83 DIDumpOptions DumpOptions) const {
Benjamin Kramer5acab502011-09-15 02:12:05 +000084 OS << "Line table prologue:\n"
Ed Maste6d0bee52015-05-28 15:38:17 +000085 << format(" total_length: 0x%8.8" PRIx64 "\n", TotalLength)
Paul Robinson75c068c2017-06-26 18:43:01 +000086 << format(" version: %u\n", getVersion());
87 if (getVersion() >= 5)
88 OS << format(" address_size: %u\n", getAddressSize())
89 << format(" seg_select_size: %u\n", SegSelectorSize);
90 OS << format(" prologue_length: 0x%8.8" PRIx64 "\n", PrologueLength)
David Blaikie1d4736e2014-02-24 23:58:54 +000091 << format(" min_inst_length: %u\n", MinInstLength)
Paul Robinson75c068c2017-06-26 18:43:01 +000092 << format(getVersion() >= 4 ? "max_ops_per_inst: %u\n" : "", MaxOpsPerInst)
David Blaikie1d4736e2014-02-24 23:58:54 +000093 << format(" default_is_stmt: %u\n", DefaultIsStmt)
94 << format(" line_base: %i\n", LineBase)
95 << format(" line_range: %u\n", LineRange)
96 << format(" opcode_base: %u\n", OpcodeBase);
Benjamin Kramer5acab502011-09-15 02:12:05 +000097
Paul Robinson9d4eb692017-05-01 23:27:55 +000098 for (uint32_t I = 0; I != StandardOpcodeLengths.size(); ++I)
Mehdi Amini149f6ea2016-10-05 05:59:29 +000099 OS << format("standard_opcode_lengths[%s] = %u\n",
Paul Robinson9d4eb692017-05-01 23:27:55 +0000100 LNStandardString(I + 1).data(), StandardOpcodeLengths[I]);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000101
Paul Robinson8181d232018-01-18 20:33:35 +0000102 if (!IncludeDirectories.empty()) {
103 // DWARF v5 starts directory indexes at 0.
104 uint32_t DirBase = getVersion() >= 5 ? 0 : 1;
Paul Robinson0a227092018-02-05 20:43:15 +0000105 for (uint32_t I = 0; I != IncludeDirectories.size(); ++I) {
106 OS << format("include_directories[%3u] = ", I + DirBase);
107 IncludeDirectories[I].dump(OS, DumpOptions);
108 OS << '\n';
109 }
Paul Robinson8181d232018-01-18 20:33:35 +0000110 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000111
112 if (!FileNames.empty()) {
Paul Robinsonceafcd42018-02-08 23:08:02 +0000113 // DWARF v5 starts file indexes at 0.
114 uint32_t FileBase = getVersion() >= 5 ? 0 : 1;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000115 for (uint32_t I = 0; I != FileNames.size(); ++I) {
116 const FileNameEntry &FileEntry = FileNames[I];
Scott Linder16c7bda2018-02-23 23:01:06 +0000117 OS << format("file_names[%3u]:\n", I + FileBase);
118 OS << " name: ";
Paul Robinson0a227092018-02-05 20:43:15 +0000119 FileEntry.Name.dump(OS, DumpOptions);
Scott Linder16c7bda2018-02-23 23:01:06 +0000120 OS << '\n'
121 << format(" dir_index: %" PRIu64 "\n", FileEntry.DirIdx);
122 if (ContentTypes.HasMD5)
123 OS << " md5_checksum: " << FileEntry.Checksum.digest() << '\n';
124 if (ContentTypes.HasModTime)
125 OS << format(" mod_time: 0x%8.8" PRIx64 "\n", FileEntry.ModTime);
126 if (ContentTypes.HasLength)
127 OS << format(" length: 0x%8.8" PRIx64 "\n", FileEntry.Length);
128 if (ContentTypes.HasSource) {
129 OS << " source: ";
130 FileEntry.Source.dump(OS, DumpOptions);
131 OS << '\n';
132 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000133 }
134 }
135}
136
Paul Robinson2bc38732017-05-02 21:40:47 +0000137// Parse v2-v4 directory and file tables.
138static void
Paul Robinson17536b92017-06-29 16:52:08 +0000139parseV2DirFileTables(const DWARFDataExtractor &DebugLineData,
140 uint32_t *OffsetPtr, uint64_t EndPrologueOffset,
Scott Linder16c7bda2018-02-23 23:01:06 +0000141 DWARFDebugLine::ContentTypeTracker &ContentTypes,
Paul Robinson0a227092018-02-05 20:43:15 +0000142 std::vector<DWARFFormValue> &IncludeDirectories,
Paul Robinson2bc38732017-05-02 21:40:47 +0000143 std::vector<DWARFDebugLine::FileNameEntry> &FileNames) {
144 while (*OffsetPtr < EndPrologueOffset) {
145 StringRef S = DebugLineData.getCStrRef(OffsetPtr);
146 if (S.empty())
147 break;
Paul Robinson0a227092018-02-05 20:43:15 +0000148 DWARFFormValue Dir(dwarf::DW_FORM_string);
149 Dir.setPValue(S.data());
150 IncludeDirectories.push_back(Dir);
Paul Robinson2bc38732017-05-02 21:40:47 +0000151 }
152
153 while (*OffsetPtr < EndPrologueOffset) {
154 StringRef Name = DebugLineData.getCStrRef(OffsetPtr);
155 if (Name.empty())
156 break;
157 DWARFDebugLine::FileNameEntry FileEntry;
Paul Robinson0a227092018-02-05 20:43:15 +0000158 FileEntry.Name.setForm(dwarf::DW_FORM_string);
159 FileEntry.Name.setPValue(Name.data());
Paul Robinson2bc38732017-05-02 21:40:47 +0000160 FileEntry.DirIdx = DebugLineData.getULEB128(OffsetPtr);
161 FileEntry.ModTime = DebugLineData.getULEB128(OffsetPtr);
162 FileEntry.Length = DebugLineData.getULEB128(OffsetPtr);
163 FileNames.push_back(FileEntry);
164 }
Scott Linder16c7bda2018-02-23 23:01:06 +0000165
166 ContentTypes.HasModTime = true;
167 ContentTypes.HasLength = true;
Paul Robinson2bc38732017-05-02 21:40:47 +0000168}
169
170// Parse v5 directory/file entry content descriptions.
171// Returns the descriptors, or an empty vector if we did not find a path or
172// ran off the end of the prologue.
173static ContentDescriptors
Scott Linder16c7bda2018-02-23 23:01:06 +0000174parseV5EntryFormat(const DWARFDataExtractor &DebugLineData, uint32_t
175 *OffsetPtr, uint64_t EndPrologueOffset, DWARFDebugLine::ContentTypeTracker
176 *ContentTypes) {
Paul Robinson2bc38732017-05-02 21:40:47 +0000177 ContentDescriptors Descriptors;
178 int FormatCount = DebugLineData.getU8(OffsetPtr);
179 bool HasPath = false;
180 for (int I = 0; I != FormatCount; ++I) {
181 if (*OffsetPtr >= EndPrologueOffset)
182 return ContentDescriptors();
183 ContentDescriptor Descriptor;
184 Descriptor.Type =
185 dwarf::LineNumberEntryFormat(DebugLineData.getULEB128(OffsetPtr));
186 Descriptor.Form = dwarf::Form(DebugLineData.getULEB128(OffsetPtr));
187 if (Descriptor.Type == dwarf::DW_LNCT_path)
188 HasPath = true;
Scott Linder16c7bda2018-02-23 23:01:06 +0000189 if (ContentTypes)
190 ContentTypes->trackContentType(Descriptor.Type);
Paul Robinson2bc38732017-05-02 21:40:47 +0000191 Descriptors.push_back(Descriptor);
192 }
193 return HasPath ? Descriptors : ContentDescriptors();
194}
195
196static bool
Paul Robinson17536b92017-06-29 16:52:08 +0000197parseV5DirFileTables(const DWARFDataExtractor &DebugLineData,
198 uint32_t *OffsetPtr, uint64_t EndPrologueOffset,
Pavel Labath322711f2018-03-14 09:39:54 +0000199 const dwarf::FormParams &FormParams,
200 const DWARFContext &Ctx, const DWARFUnit *U,
Scott Linder16c7bda2018-02-23 23:01:06 +0000201 DWARFDebugLine::ContentTypeTracker &ContentTypes,
Paul Robinson0a227092018-02-05 20:43:15 +0000202 std::vector<DWARFFormValue> &IncludeDirectories,
Paul Robinson2bc38732017-05-02 21:40:47 +0000203 std::vector<DWARFDebugLine::FileNameEntry> &FileNames) {
204 // Get the directory entry description.
205 ContentDescriptors DirDescriptors =
Paul Robinsona06f8dc2017-12-18 19:08:35 +0000206 parseV5EntryFormat(DebugLineData, OffsetPtr, EndPrologueOffset, nullptr);
Paul Robinson2bc38732017-05-02 21:40:47 +0000207 if (DirDescriptors.empty())
208 return false;
209
210 // Get the directory entries, according to the format described above.
211 int DirEntryCount = DebugLineData.getU8(OffsetPtr);
212 for (int I = 0; I != DirEntryCount; ++I) {
213 if (*OffsetPtr >= EndPrologueOffset)
214 return false;
215 for (auto Descriptor : DirDescriptors) {
216 DWARFFormValue Value(Descriptor.Form);
217 switch (Descriptor.Type) {
218 case DW_LNCT_path:
Paul Robinsonbf750c82018-01-29 20:57:43 +0000219 if (!Value.extractValue(DebugLineData, OffsetPtr, FormParams, &Ctx, U))
Paul Robinson2bc38732017-05-02 21:40:47 +0000220 return false;
Paul Robinson0a227092018-02-05 20:43:15 +0000221 IncludeDirectories.push_back(Value);
Paul Robinson2bc38732017-05-02 21:40:47 +0000222 break;
223 default:
Paul Robinson75c068c2017-06-26 18:43:01 +0000224 if (!Value.skipValue(DebugLineData, OffsetPtr, FormParams))
Paul Robinson2bc38732017-05-02 21:40:47 +0000225 return false;
226 }
227 }
228 }
229
230 // Get the file entry description.
231 ContentDescriptors FileDescriptors =
Scott Linder16c7bda2018-02-23 23:01:06 +0000232 parseV5EntryFormat(DebugLineData, OffsetPtr, EndPrologueOffset,
233 &ContentTypes);
Paul Robinson2bc38732017-05-02 21:40:47 +0000234 if (FileDescriptors.empty())
235 return false;
236
237 // Get the file entries, according to the format described above.
238 int FileEntryCount = DebugLineData.getU8(OffsetPtr);
239 for (int I = 0; I != FileEntryCount; ++I) {
240 if (*OffsetPtr >= EndPrologueOffset)
241 return false;
242 DWARFDebugLine::FileNameEntry FileEntry;
243 for (auto Descriptor : FileDescriptors) {
244 DWARFFormValue Value(Descriptor.Form);
Paul Robinsonbf750c82018-01-29 20:57:43 +0000245 if (!Value.extractValue(DebugLineData, OffsetPtr, FormParams, &Ctx, U))
Paul Robinson2bc38732017-05-02 21:40:47 +0000246 return false;
247 switch (Descriptor.Type) {
248 case DW_LNCT_path:
Paul Robinson0a227092018-02-05 20:43:15 +0000249 FileEntry.Name = Value;
Paul Robinson2bc38732017-05-02 21:40:47 +0000250 break;
Scott Linder16c7bda2018-02-23 23:01:06 +0000251 case DW_LNCT_LLVM_source:
252 FileEntry.Source = Value;
253 break;
Paul Robinson2bc38732017-05-02 21:40:47 +0000254 case DW_LNCT_directory_index:
255 FileEntry.DirIdx = Value.getAsUnsignedConstant().getValue();
256 break;
257 case DW_LNCT_timestamp:
258 FileEntry.ModTime = Value.getAsUnsignedConstant().getValue();
259 break;
260 case DW_LNCT_size:
261 FileEntry.Length = Value.getAsUnsignedConstant().getValue();
262 break;
Paul Robinsona06f8dc2017-12-18 19:08:35 +0000263 case DW_LNCT_MD5:
264 assert(Value.getAsBlock().getValue().size() == 16);
265 std::uninitialized_copy_n(Value.getAsBlock().getValue().begin(), 16,
266 FileEntry.Checksum.Bytes.begin());
267 break;
Paul Robinson2bc38732017-05-02 21:40:47 +0000268 default:
269 break;
270 }
271 }
272 FileNames.push_back(FileEntry);
273 }
274 return true;
275}
276
James Hendersona3acf992018-05-10 10:51:33 +0000277Error DWARFDebugLine::Prologue::parse(const DWARFDataExtractor &DebugLineData,
278 uint32_t *OffsetPtr,
279 const DWARFContext &Ctx,
280 const DWARFUnit *U) {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000281 const uint64_t PrologueOffset = *OffsetPtr;
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000282
283 clear();
Paul Robinson9d4eb692017-05-01 23:27:55 +0000284 TotalLength = DebugLineData.getU32(OffsetPtr);
Ed Maste6d0bee52015-05-28 15:38:17 +0000285 if (TotalLength == UINT32_MAX) {
Paul Robinson75c068c2017-06-26 18:43:01 +0000286 FormParams.Format = dwarf::DWARF64;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000287 TotalLength = DebugLineData.getU64(OffsetPtr);
Paul Robinson75c068c2017-06-26 18:43:01 +0000288 } else if (TotalLength >= 0xffffff00) {
Victor Leschukcba595d2018-08-20 09:59:08 +0000289 return createStringError(errc::invalid_argument,
James Hendersona3acf992018-05-10 10:51:33 +0000290 "parsing line table prologue at offset 0x%8.8" PRIx64
291 " unsupported reserved unit length found of value 0x%8.8" PRIx64,
292 PrologueOffset, TotalLength);
Ed Maste6d0bee52015-05-28 15:38:17 +0000293 }
Paul Robinson75c068c2017-06-26 18:43:01 +0000294 FormParams.Version = DebugLineData.getU16(OffsetPtr);
295 if (getVersion() < 2)
Victor Leschukcba595d2018-08-20 09:59:08 +0000296 return createStringError(errc::not_supported,
297 "parsing line table prologue at offset 0x%8.8" PRIx64
James Hendersona3acf992018-05-10 10:51:33 +0000298 " found unsupported version 0x%2.2" PRIx16,
299 PrologueOffset, getVersion());
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000300
Paul Robinson75c068c2017-06-26 18:43:01 +0000301 if (getVersion() >= 5) {
302 FormParams.AddrSize = DebugLineData.getU8(OffsetPtr);
Paul Robinson63811a42017-11-22 15:33:17 +0000303 assert((DebugLineData.getAddressSize() == 0 ||
304 DebugLineData.getAddressSize() == getAddressSize()) &&
Paul Robinson75c068c2017-06-26 18:43:01 +0000305 "Line table header and data extractor disagree");
Paul Robinson2bc38732017-05-02 21:40:47 +0000306 SegSelectorSize = DebugLineData.getU8(OffsetPtr);
307 }
308
Paul Robinson9d4eb692017-05-01 23:27:55 +0000309 PrologueLength = DebugLineData.getUnsigned(OffsetPtr, sizeofPrologueLength());
310 const uint64_t EndPrologueOffset = PrologueLength + *OffsetPtr;
311 MinInstLength = DebugLineData.getU8(OffsetPtr);
Paul Robinson75c068c2017-06-26 18:43:01 +0000312 if (getVersion() >= 4)
Paul Robinson9d4eb692017-05-01 23:27:55 +0000313 MaxOpsPerInst = DebugLineData.getU8(OffsetPtr);
314 DefaultIsStmt = DebugLineData.getU8(OffsetPtr);
315 LineBase = DebugLineData.getU8(OffsetPtr);
316 LineRange = DebugLineData.getU8(OffsetPtr);
317 OpcodeBase = DebugLineData.getU8(OffsetPtr);
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000318
319 StandardOpcodeLengths.reserve(OpcodeBase - 1);
Paul Robinson9d4eb692017-05-01 23:27:55 +0000320 for (uint32_t I = 1; I < OpcodeBase; ++I) {
321 uint8_t OpLen = DebugLineData.getU8(OffsetPtr);
322 StandardOpcodeLengths.push_back(OpLen);
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000323 }
324
Paul Robinson75c068c2017-06-26 18:43:01 +0000325 if (getVersion() >= 5) {
Paul Robinson2bc38732017-05-02 21:40:47 +0000326 if (!parseV5DirFileTables(DebugLineData, OffsetPtr, EndPrologueOffset,
Scott Linder16c7bda2018-02-23 23:01:06 +0000327 FormParams, Ctx, U, ContentTypes,
328 IncludeDirectories, FileNames)) {
Victor Leschukcba595d2018-08-20 09:59:08 +0000329 return createStringError(errc::invalid_argument,
Jonas Devlieghere84e99262018-04-14 22:07:23 +0000330 "parsing line table prologue at 0x%8.8" PRIx64
331 " found an invalid directory or file table description at"
James Hendersona3acf992018-05-10 10:51:33 +0000332 " 0x%8.8" PRIx64,
Jonas Devlieghere84e99262018-04-14 22:07:23 +0000333 PrologueOffset, (uint64_t)*OffsetPtr);
Paul Robinson2bc38732017-05-02 21:40:47 +0000334 }
335 } else
336 parseV2DirFileTables(DebugLineData, OffsetPtr, EndPrologueOffset,
Scott Linder16c7bda2018-02-23 23:01:06 +0000337 ContentTypes, IncludeDirectories, FileNames);
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000338
James Hendersona3acf992018-05-10 10:51:33 +0000339 if (*OffsetPtr != EndPrologueOffset)
Victor Leschukcba595d2018-08-20 09:59:08 +0000340 return createStringError(errc::invalid_argument,
341 "parsing line table prologue at 0x%8.8" PRIx64
James Hendersona3acf992018-05-10 10:51:33 +0000342 " should have ended at 0x%8.8" PRIx64
343 " but it ended at 0x%8.8" PRIx64,
344 PrologueOffset, EndPrologueOffset, (uint64_t)*OffsetPtr);
345 return Error::success();
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000346}
347
Paul Robinson9d4eb692017-05-01 23:27:55 +0000348DWARFDebugLine::Row::Row(bool DefaultIsStmt) { reset(DefaultIsStmt); }
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000349
Benjamin Kramer5acab502011-09-15 02:12:05 +0000350void DWARFDebugLine::Row::postAppend() {
351 BasicBlock = false;
352 PrologueEnd = false;
353 EpilogueBegin = false;
354}
355
Paul Robinson9d4eb692017-05-01 23:27:55 +0000356void DWARFDebugLine::Row::reset(bool DefaultIsStmt) {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000357 Address = 0;
358 Line = 1;
359 Column = 0;
360 File = 1;
361 Isa = 0;
Diego Novillo5b5cf502014-02-14 19:27:53 +0000362 Discriminator = 0;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000363 IsStmt = DefaultIsStmt;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000364 BasicBlock = false;
365 EndSequence = false;
366 PrologueEnd = false;
367 EpilogueBegin = false;
368}
369
Greg Clayton67070462017-05-02 22:48:52 +0000370void DWARFDebugLine::Row::dumpTableHeader(raw_ostream &OS) {
371 OS << "Address Line Column File ISA Discriminator Flags\n"
372 << "------------------ ------ ------ ------ --- ------------- "
373 "-------------\n";
374}
375
Benjamin Kramer5acab502011-09-15 02:12:05 +0000376void DWARFDebugLine::Row::dump(raw_ostream &OS) const {
Benjamin Kramerf3da5292011-11-05 08:57:40 +0000377 OS << format("0x%16.16" PRIx64 " %6u %6u", Address, Line, Column)
Diego Novillo5b5cf502014-02-14 19:27:53 +0000378 << format(" %6u %3u %13u ", File, Isa, Discriminator)
Dehao Chen1b54fce2016-04-28 22:09:37 +0000379 << (IsStmt ? " is_stmt" : "") << (BasicBlock ? " basic_block" : "")
Benjamin Kramer5acab502011-09-15 02:12:05 +0000380 << (PrologueEnd ? " prologue_end" : "")
381 << (EpilogueBegin ? " epilogue_begin" : "")
Dehao Chen1b54fce2016-04-28 22:09:37 +0000382 << (EndSequence ? " end_sequence" : "") << '\n';
Benjamin Kramer5acab502011-09-15 02:12:05 +0000383}
384
Dehao Chen1b54fce2016-04-28 22:09:37 +0000385DWARFDebugLine::Sequence::Sequence() { reset(); }
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000386
387void DWARFDebugLine::Sequence::reset() {
388 LowPC = 0;
389 HighPC = 0;
390 FirstRowIndex = 0;
391 LastRowIndex = 0;
392 Empty = true;
393}
394
Dehao Chen1b54fce2016-04-28 22:09:37 +0000395DWARFDebugLine::LineTable::LineTable() { clear(); }
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000396
Paul Robinson0a227092018-02-05 20:43:15 +0000397void DWARFDebugLine::LineTable::dump(raw_ostream &OS,
398 DIDumpOptions DumpOptions) const {
399 Prologue.dump(OS, DumpOptions);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000400 OS << '\n';
401
402 if (!Rows.empty()) {
Greg Clayton67070462017-05-02 22:48:52 +0000403 Row::dumpTableHeader(OS);
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000404 for (const Row &R : Rows) {
405 R.dump(OS);
406 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000407 }
408}
409
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000410void DWARFDebugLine::LineTable::clear() {
411 Prologue.clear();
412 Rows.clear();
413 Sequences.clear();
414}
415
Alexey Samsonov110d5952014-04-30 00:09:19 +0000416DWARFDebugLine::ParsingState::ParsingState(struct LineTable *LT)
Eugene Zelenko2db0cfa2017-06-23 21:57:40 +0000417 : LineTable(LT) {
Alexey Samsonov110d5952014-04-30 00:09:19 +0000418 resetRowAndSequence();
419}
Nick Lewycky4d044922011-09-15 03:41:51 +0000420
Alexey Samsonov110d5952014-04-30 00:09:19 +0000421void DWARFDebugLine::ParsingState::resetRowAndSequence() {
422 Row.reset(LineTable->Prologue.DefaultIsStmt);
423 Sequence.reset();
424}
425
Paul Robinson9d4eb692017-05-01 23:27:55 +0000426void DWARFDebugLine::ParsingState::appendRowToMatrix(uint32_t Offset) {
Alexey Samsonov110d5952014-04-30 00:09:19 +0000427 if (Sequence.Empty) {
Alexey Samsonov947228c2012-08-07 11:46:57 +0000428 // Record the beginning of instruction sequence.
Alexey Samsonov110d5952014-04-30 00:09:19 +0000429 Sequence.Empty = false;
430 Sequence.LowPC = Row.Address;
431 Sequence.FirstRowIndex = RowNumber;
Alexey Samsonov947228c2012-08-07 11:46:57 +0000432 }
Alexey Samsonov110d5952014-04-30 00:09:19 +0000433 ++RowNumber;
434 LineTable->appendRow(Row);
435 if (Row.EndSequence) {
Alexey Samsonov947228c2012-08-07 11:46:57 +0000436 // Record the end of instruction sequence.
Alexey Samsonov110d5952014-04-30 00:09:19 +0000437 Sequence.HighPC = Row.Address;
438 Sequence.LastRowIndex = RowNumber;
439 if (Sequence.isValid())
440 LineTable->appendSequence(Sequence);
441 Sequence.reset();
Alexey Samsonov947228c2012-08-07 11:46:57 +0000442 }
Alexey Samsonov110d5952014-04-30 00:09:19 +0000443 Row.postAppend();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000444}
445
Benjamin Kramer5acab502011-09-15 02:12:05 +0000446const DWARFDebugLine::LineTable *
Paul Robinson9d4eb692017-05-01 23:27:55 +0000447DWARFDebugLine::getLineTable(uint32_t Offset) const {
448 LineTableConstIter Pos = LineTableMap.find(Offset);
449 if (Pos != LineTableMap.end())
450 return &Pos->second;
Craig Topper2617dcc2014-04-15 06:32:26 +0000451 return nullptr;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000452}
453
James Hendersona3acf992018-05-10 10:51:33 +0000454Expected<const DWARFDebugLine::LineTable *> DWARFDebugLine::getOrParseLineTable(
455 DWARFDataExtractor &DebugLineData, uint32_t Offset, const DWARFContext &Ctx,
James Henderson004b7292018-05-21 15:30:54 +0000456 const DWARFUnit *U, std::function<void(Error)> RecoverableErrorCallback) {
James Henderson66702622018-03-08 10:53:34 +0000457 if (!DebugLineData.isValidOffset(Offset))
Victor Leschukcba595d2018-08-20 09:59:08 +0000458 return createStringError(errc::invalid_argument, "offset 0x%8.8" PRIx32
James Hendersona3acf992018-05-10 10:51:33 +0000459 " is not a valid debug line section offset",
460 Offset);
James Henderson66702622018-03-08 10:53:34 +0000461
Paul Robinson9d4eb692017-05-01 23:27:55 +0000462 std::pair<LineTableIter, bool> Pos =
463 LineTableMap.insert(LineTableMapTy::value_type(Offset, LineTable()));
464 LineTable *LT = &Pos.first->second;
465 if (Pos.second) {
James Henderson004b7292018-05-21 15:30:54 +0000466 if (Error Err =
467 LT->parse(DebugLineData, &Offset, Ctx, U, RecoverableErrorCallback))
James Hendersona3acf992018-05-10 10:51:33 +0000468 return std::move(Err);
469 return LT;
Benjamin Kramer679e1752011-09-15 20:43:18 +0000470 }
Alexey Samsonov110d5952014-04-30 00:09:19 +0000471 return LT;
Benjamin Kramer679e1752011-09-15 20:43:18 +0000472}
473
James Hendersona3acf992018-05-10 10:51:33 +0000474Error DWARFDebugLine::LineTable::parse(
475 DWARFDataExtractor &DebugLineData, uint32_t *OffsetPtr,
476 const DWARFContext &Ctx, const DWARFUnit *U,
James Henderson004b7292018-05-21 15:30:54 +0000477 std::function<void(Error)> RecoverableErrorCallback, raw_ostream *OS) {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000478 const uint32_t DebugLineOffset = *OffsetPtr;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000479
Alexey Samsonov110d5952014-04-30 00:09:19 +0000480 clear();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000481
James Hendersona3acf992018-05-10 10:51:33 +0000482 Error PrologueErr = Prologue.parse(DebugLineData, OffsetPtr, Ctx, U);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000483
Paul Robinson0a227092018-02-05 20:43:15 +0000484 if (OS) {
485 // The presence of OS signals verbose dumping.
486 DIDumpOptions DumpOptions;
487 DumpOptions.Verbose = true;
488 Prologue.dump(*OS, DumpOptions);
489 }
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000490
James Hendersona3acf992018-05-10 10:51:33 +0000491 if (PrologueErr)
492 return PrologueErr;
493
Paul Robinson9d4eb692017-05-01 23:27:55 +0000494 const uint32_t EndOffset =
495 DebugLineOffset + Prologue.TotalLength + Prologue.sizeofTotalLength();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000496
Paul Robinson511b54c2017-11-22 15:48:30 +0000497 // See if we should tell the data extractor the address size.
498 if (DebugLineData.getAddressSize() == 0)
499 DebugLineData.setAddressSize(Prologue.getAddressSize());
500 else
501 assert(Prologue.getAddressSize() == 0 ||
502 Prologue.getAddressSize() == DebugLineData.getAddressSize());
503
Alexey Samsonov110d5952014-04-30 00:09:19 +0000504 ParsingState State(this);
Benjamin Kramer112ec172011-09-15 21:59:13 +0000505
Paul Robinson9d4eb692017-05-01 23:27:55 +0000506 while (*OffsetPtr < EndOffset) {
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000507 if (OS)
508 *OS << format("0x%08.08" PRIx32 ": ", *OffsetPtr);
509
Paul Robinson9d4eb692017-05-01 23:27:55 +0000510 uint8_t Opcode = DebugLineData.getU8(OffsetPtr);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000511
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000512 if (OS)
513 *OS << format("%02.02" PRIx8 " ", Opcode);
514
Paul Robinson9d4eb692017-05-01 23:27:55 +0000515 if (Opcode == 0) {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000516 // Extended Opcodes always start with a zero opcode followed by
517 // a uleb128 length so you can skip ones you don't know about
Paul Robinson9d4eb692017-05-01 23:27:55 +0000518 uint64_t Len = DebugLineData.getULEB128(OffsetPtr);
Paul Robinsone0833342017-11-22 15:14:49 +0000519 uint32_t ExtOffset = *OffsetPtr;
520
521 // Tolerate zero-length; assume length is correct and soldier on.
522 if (Len == 0) {
523 if (OS)
524 *OS << "Badly formed extended line op (length 0)\n";
525 continue;
526 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000527
Paul Robinson9d4eb692017-05-01 23:27:55 +0000528 uint8_t SubOpcode = DebugLineData.getU8(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000529 if (OS)
530 *OS << LNExtendedString(SubOpcode);
Paul Robinson9d4eb692017-05-01 23:27:55 +0000531 switch (SubOpcode) {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000532 case DW_LNE_end_sequence:
533 // Set the end_sequence register of the state machine to true and
534 // append a row to the matrix using the current values of the
535 // state-machine registers. Then reset the registers to the initial
536 // values specified above. Every statement program sequence must end
537 // with a DW_LNE_end_sequence instruction which creates a row whose
538 // address is that of the byte after the last target machine instruction
539 // of the sequence.
Alexey Samsonov110d5952014-04-30 00:09:19 +0000540 State.Row.EndSequence = true;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000541 State.appendRowToMatrix(*OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000542 if (OS) {
543 *OS << "\n";
544 OS->indent(12);
545 State.Row.dump(*OS);
546 }
Alexey Samsonov110d5952014-04-30 00:09:19 +0000547 State.resetRowAndSequence();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000548 break;
549
550 case DW_LNE_set_address:
551 // Takes a single relocatable address as an operand. The size of the
552 // operand is the size appropriate to hold an address on the target
553 // machine. Set the address register to the value given by the
554 // relocatable address. All of the other statement program opcodes
555 // that affect the address register add a delta to it. This instruction
556 // stores a relocatable value into it instead.
Paul Robinson511b54c2017-11-22 15:48:30 +0000557 //
558 // Make sure the extractor knows the address size. If not, infer it
559 // from the size of the operand.
560 if (DebugLineData.getAddressSize() == 0)
561 DebugLineData.setAddressSize(Len - 1);
Paul Robinson79474682018-03-22 19:37:56 +0000562 else if (DebugLineData.getAddressSize() != Len - 1) {
Victor Leschukcba595d2018-08-20 09:59:08 +0000563 return createStringError(errc::invalid_argument,
564 "mismatching address size at offset 0x%8.8" PRIx32
James Hendersona3acf992018-05-10 10:51:33 +0000565 " expected 0x%2.2" PRIx8 " found 0x%2.2" PRIx64,
566 ExtOffset, DebugLineData.getAddressSize(),
567 Len - 1);
Paul Robinson79474682018-03-22 19:37:56 +0000568 }
Paul Robinson17536b92017-06-29 16:52:08 +0000569 State.Row.Address = DebugLineData.getRelocatedAddress(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000570 if (OS)
571 *OS << format(" (0x%16.16" PRIx64 ")", State.Row.Address);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000572 break;
573
574 case DW_LNE_define_file:
575 // Takes 4 arguments. The first is a null terminated string containing
576 // a source file name. The second is an unsigned LEB128 number
577 // representing the directory index of the directory in which the file
578 // was found. The third is an unsigned LEB128 number representing the
579 // time of last modification of the file. The fourth is an unsigned
580 // LEB128 number representing the length in bytes of the file. The time
581 // and length fields may contain LEB128(0) if the information is not
582 // available.
583 //
584 // The directory index represents an entry in the include_directories
585 // section of the statement program prologue. The index is LEB128(0)
586 // if the file was found in the current directory of the compilation,
587 // LEB128(1) if it was found in the first directory in the
588 // include_directories section, and so on. The directory index is
589 // ignored for file names that represent full path names.
590 //
591 // The files are numbered, starting at 1, in the order in which they
592 // appear; the names in the prologue come before names defined by
593 // the DW_LNE_define_file instruction. These numbers are used in the
594 // the file register of the state machine.
595 {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000596 FileNameEntry FileEntry;
Paul Robinson0a227092018-02-05 20:43:15 +0000597 const char *Name = DebugLineData.getCStr(OffsetPtr);
598 FileEntry.Name.setForm(dwarf::DW_FORM_string);
599 FileEntry.Name.setPValue(Name);
Paul Robinson9d4eb692017-05-01 23:27:55 +0000600 FileEntry.DirIdx = DebugLineData.getULEB128(OffsetPtr);
601 FileEntry.ModTime = DebugLineData.getULEB128(OffsetPtr);
602 FileEntry.Length = DebugLineData.getULEB128(OffsetPtr);
603 Prologue.FileNames.push_back(FileEntry);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000604 if (OS)
Paul Robinson0a227092018-02-05 20:43:15 +0000605 *OS << " (" << Name << ", dir=" << FileEntry.DirIdx << ", mod_time="
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000606 << format("(0x%16.16" PRIx64 ")", FileEntry.ModTime)
607 << ", length=" << FileEntry.Length << ")";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000608 }
609 break;
610
Diego Novillo5b5cf502014-02-14 19:27:53 +0000611 case DW_LNE_set_discriminator:
Paul Robinson9d4eb692017-05-01 23:27:55 +0000612 State.Row.Discriminator = DebugLineData.getULEB128(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000613 if (OS)
614 *OS << " (" << State.Row.Discriminator << ")";
Diego Novillo5b5cf502014-02-14 19:27:53 +0000615 break;
616
Benjamin Kramer5acab502011-09-15 02:12:05 +0000617 default:
Paul Robinsone0833342017-11-22 15:14:49 +0000618 if (OS)
619 *OS << format("Unrecognized extended op 0x%02.02" PRIx8, SubOpcode)
620 << format(" length %" PRIx64, Len);
621 // Len doesn't include the zero opcode byte or the length itself, but
622 // it does include the sub_opcode, so we have to adjust for that.
623 (*OffsetPtr) += Len - 1;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000624 break;
625 }
Paul Robinsone0833342017-11-22 15:14:49 +0000626 // Make sure the stated and parsed lengths are the same.
627 // Otherwise we have an unparseable line-number program.
James Hendersona3acf992018-05-10 10:51:33 +0000628 if (*OffsetPtr - ExtOffset != Len)
Victor Leschukcba595d2018-08-20 09:59:08 +0000629 return createStringError(errc::illegal_byte_sequence,
630 "unexpected line op length at offset 0x%8.8" PRIx32
James Hendersona3acf992018-05-10 10:51:33 +0000631 " expected 0x%2.2" PRIx64 " found 0x%2.2" PRIx32,
632 ExtOffset, Len, *OffsetPtr - ExtOffset);
Paul Robinson9d4eb692017-05-01 23:27:55 +0000633 } else if (Opcode < Prologue.OpcodeBase) {
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000634 if (OS)
635 *OS << LNStandardString(Opcode);
Paul Robinson9d4eb692017-05-01 23:27:55 +0000636 switch (Opcode) {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000637 // Standard Opcodes
638 case DW_LNS_copy:
639 // Takes no arguments. Append a row to the matrix using the
640 // current values of the state-machine registers. Then set
641 // the basic_block register to false.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000642 State.appendRowToMatrix(*OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000643 if (OS) {
644 *OS << "\n";
645 OS->indent(12);
646 State.Row.dump(*OS);
647 *OS << "\n";
648 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000649 break;
650
651 case DW_LNS_advance_pc:
652 // Takes a single unsigned LEB128 operand, multiplies it by the
653 // min_inst_length field of the prologue, and adds the
654 // result to the address register of the state machine.
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000655 {
656 uint64_t AddrOffset =
657 DebugLineData.getULEB128(OffsetPtr) * Prologue.MinInstLength;
658 State.Row.Address += AddrOffset;
659 if (OS)
660 *OS << " (" << AddrOffset << ")";
661 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000662 break;
663
664 case DW_LNS_advance_line:
665 // Takes a single signed LEB128 operand and adds that value to
666 // the line register of the state machine.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000667 State.Row.Line += DebugLineData.getSLEB128(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000668 if (OS)
669 *OS << " (" << State.Row.Line << ")";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000670 break;
671
672 case DW_LNS_set_file:
673 // Takes a single unsigned LEB128 operand and stores it in the file
674 // register of the state machine.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000675 State.Row.File = DebugLineData.getULEB128(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000676 if (OS)
677 *OS << " (" << State.Row.File << ")";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000678 break;
679
680 case DW_LNS_set_column:
681 // Takes a single unsigned LEB128 operand and stores it in the
682 // column register of the state machine.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000683 State.Row.Column = DebugLineData.getULEB128(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000684 if (OS)
685 *OS << " (" << State.Row.Column << ")";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000686 break;
687
688 case DW_LNS_negate_stmt:
689 // Takes no arguments. Set the is_stmt register of the state
690 // machine to the logical negation of its current value.
Alexey Samsonov110d5952014-04-30 00:09:19 +0000691 State.Row.IsStmt = !State.Row.IsStmt;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000692 break;
693
694 case DW_LNS_set_basic_block:
695 // Takes no arguments. Set the basic_block register of the
696 // state machine to true
Alexey Samsonov110d5952014-04-30 00:09:19 +0000697 State.Row.BasicBlock = true;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000698 break;
699
700 case DW_LNS_const_add_pc:
701 // Takes no arguments. Add to the address register of the state
702 // machine the address increment value corresponding to special
703 // opcode 255. The motivation for DW_LNS_const_add_pc is this:
704 // when the statement program needs to advance the address by a
705 // small amount, it can use a single special opcode, which occupies
706 // a single byte. When it needs to advance the address by up to
707 // twice the range of the last special opcode, it can use
708 // DW_LNS_const_add_pc followed by a special opcode, for a total
709 // of two bytes. Only if it needs to advance the address by more
710 // than twice that range will it need to use both DW_LNS_advance_pc
711 // and a special opcode, requiring three or more bytes.
712 {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000713 uint8_t AdjustOpcode = 255 - Prologue.OpcodeBase;
714 uint64_t AddrOffset =
715 (AdjustOpcode / Prologue.LineRange) * Prologue.MinInstLength;
716 State.Row.Address += AddrOffset;
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000717 if (OS)
718 *OS
719 << format(" (0x%16.16" PRIx64 ")", AddrOffset);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000720 }
721 break;
722
723 case DW_LNS_fixed_advance_pc:
724 // Takes a single uhalf operand. Add to the address register of
725 // the state machine the value of the (unencoded) operand. This
726 // is the only extended opcode that takes an argument that is not
727 // a variable length number. The motivation for DW_LNS_fixed_advance_pc
728 // is this: existing assemblers cannot emit DW_LNS_advance_pc or
729 // special opcodes because they cannot encode LEB128 numbers or
730 // judge when the computation of a special opcode overflows and
731 // requires the use of DW_LNS_advance_pc. Such assemblers, however,
732 // can use DW_LNS_fixed_advance_pc instead, sacrificing compression.
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000733 {
734 uint16_t PCOffset = DebugLineData.getU16(OffsetPtr);
735 State.Row.Address += PCOffset;
736 if (OS)
737 *OS
738 << format(" (0x%16.16" PRIx64 ")", PCOffset);
739 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000740 break;
741
742 case DW_LNS_set_prologue_end:
743 // Takes no arguments. Set the prologue_end register of the
744 // state machine to true
Alexey Samsonov110d5952014-04-30 00:09:19 +0000745 State.Row.PrologueEnd = true;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000746 break;
747
748 case DW_LNS_set_epilogue_begin:
749 // Takes no arguments. Set the basic_block register of the
750 // state machine to true
Alexey Samsonov110d5952014-04-30 00:09:19 +0000751 State.Row.EpilogueBegin = true;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000752 break;
753
754 case DW_LNS_set_isa:
755 // Takes a single unsigned LEB128 operand and stores it in the
756 // column register of the state machine.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000757 State.Row.Isa = DebugLineData.getULEB128(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000758 if (OS)
759 *OS << " (" << State.Row.Isa << ")";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000760 break;
761
762 default:
763 // Handle any unknown standard opcodes here. We know the lengths
764 // of such opcodes because they are specified in the prologue
765 // as a multiple of LEB128 operands for each opcode.
766 {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000767 assert(Opcode - 1U < Prologue.StandardOpcodeLengths.size());
768 uint8_t OpcodeLength = Prologue.StandardOpcodeLengths[Opcode - 1];
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000769 for (uint8_t I = 0; I < OpcodeLength; ++I) {
770 uint64_t Value = DebugLineData.getULEB128(OffsetPtr);
771 if (OS)
772 *OS << format("Skipping ULEB128 value: 0x%16.16" PRIx64 ")\n",
773 Value);
774 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000775 }
776 break;
777 }
778 } else {
779 // Special Opcodes
780
781 // A special opcode value is chosen based on the amount that needs
782 // to be added to the line and address registers. The maximum line
783 // increment for a special opcode is the value of the line_base
784 // field in the header, plus the value of the line_range field,
785 // minus 1 (line base + line range - 1). If the desired line
786 // increment is greater than the maximum line increment, a standard
NAKAMURA Takumif9959852011-10-08 11:22:47 +0000787 // opcode must be used instead of a special opcode. The "address
788 // advance" is calculated by dividing the desired address increment
Benjamin Kramer5acab502011-09-15 02:12:05 +0000789 // by the minimum_instruction_length field from the header. The
790 // special opcode is then calculated using the following formula:
791 //
792 // opcode = (desired line increment - line_base) +
793 // (line_range * address advance) + opcode_base
794 //
795 // If the resulting opcode is greater than 255, a standard opcode
796 // must be used instead.
797 //
798 // To decode a special opcode, subtract the opcode_base from the
799 // opcode itself to give the adjusted opcode. The amount to
800 // increment the address register is the result of the adjusted
801 // opcode divided by the line_range multiplied by the
802 // minimum_instruction_length field from the header. That is:
803 //
804 // address increment = (adjusted opcode / line_range) *
805 // minimum_instruction_length
806 //
807 // The amount to increment the line register is the line_base plus
808 // the result of the adjusted opcode modulo the line_range. That is:
809 //
810 // line increment = line_base + (adjusted opcode % line_range)
811
Paul Robinson9d4eb692017-05-01 23:27:55 +0000812 uint8_t AdjustOpcode = Opcode - Prologue.OpcodeBase;
813 uint64_t AddrOffset =
814 (AdjustOpcode / Prologue.LineRange) * Prologue.MinInstLength;
815 int32_t LineOffset =
816 Prologue.LineBase + (AdjustOpcode % Prologue.LineRange);
817 State.Row.Line += LineOffset;
818 State.Row.Address += AddrOffset;
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000819
820 if (OS) {
821 *OS << "address += " << ((uint32_t)AdjustOpcode)
822 << ", line += " << LineOffset << "\n";
823 OS->indent(12);
824 State.Row.dump(*OS);
825 }
826
Paul Robinson9d4eb692017-05-01 23:27:55 +0000827 State.appendRowToMatrix(*OffsetPtr);
Dehao Chen1b54fce2016-04-28 22:09:37 +0000828 // Reset discriminator to 0.
829 State.Row.Discriminator = 0;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000830 }
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000831 if(OS)
832 *OS << "\n";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000833 }
834
Jonas Devlieghere84e99262018-04-14 22:07:23 +0000835 if (!State.Sequence.Empty)
James Henderson004b7292018-05-21 15:30:54 +0000836 RecoverableErrorCallback(
Victor Leschukcba595d2018-08-20 09:59:08 +0000837 createStringError(errc::illegal_byte_sequence,
838 "last sequence in debug line table is not terminated!"));
Alexey Samsonov110d5952014-04-30 00:09:19 +0000839
840 // Sort all sequences so that address lookup will work faster.
841 if (!Sequences.empty()) {
Mandeep Singh Grangfe1d28e2018-04-01 16:18:49 +0000842 llvm::sort(Sequences.begin(), Sequences.end(), Sequence::orderByLowPC);
Alexey Samsonov110d5952014-04-30 00:09:19 +0000843 // Note: actually, instruction address ranges of sequences should not
844 // overlap (in shared objects and executables). If they do, the address
845 // lookup would still work, though, but result would be ambiguous.
846 // We don't report warning in this case. For example,
847 // sometimes .so compiled from multiple object files contains a few
848 // rudimentary sequences for address ranges [0x0, 0xsomething).
849 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000850
James Hendersona3acf992018-05-10 10:51:33 +0000851 return Error::success();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000852}
853
Keno Fischerc2c60182015-05-31 23:37:04 +0000854uint32_t
Paul Robinson9d4eb692017-05-01 23:27:55 +0000855DWARFDebugLine::LineTable::findRowInSeq(const DWARFDebugLine::Sequence &Seq,
856 uint64_t Address) const {
857 if (!Seq.containsPC(Address))
Keno Fischerc2c60182015-05-31 23:37:04 +0000858 return UnknownRowIndex;
859 // Search for instruction address in the rows describing the sequence.
860 // Rows are stored in a vector, so we may use arithmetical operations with
861 // iterators.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000862 DWARFDebugLine::Row Row;
863 Row.Address = Address;
864 RowIter FirstRow = Rows.begin() + Seq.FirstRowIndex;
865 RowIter LastRow = Rows.begin() + Seq.LastRowIndex;
866 LineTable::RowIter RowPos = std::lower_bound(
867 FirstRow, LastRow, Row, DWARFDebugLine::Row::orderByAddress);
868 if (RowPos == LastRow) {
869 return Seq.LastRowIndex - 1;
Keno Fischerc2c60182015-05-31 23:37:04 +0000870 }
Paul Robinson9d4eb692017-05-01 23:27:55 +0000871 uint32_t Index = Seq.FirstRowIndex + (RowPos - FirstRow);
872 if (RowPos->Address > Address) {
873 if (RowPos == FirstRow)
Keno Fischerc2c60182015-05-31 23:37:04 +0000874 return UnknownRowIndex;
875 else
Paul Robinson9d4eb692017-05-01 23:27:55 +0000876 Index--;
Keno Fischerc2c60182015-05-31 23:37:04 +0000877 }
Paul Robinson9d4eb692017-05-01 23:27:55 +0000878 return Index;
Keno Fischerc2c60182015-05-31 23:37:04 +0000879}
880
Paul Robinson9d4eb692017-05-01 23:27:55 +0000881uint32_t DWARFDebugLine::LineTable::lookupAddress(uint64_t Address) const {
Alexey Samsonov947228c2012-08-07 11:46:57 +0000882 if (Sequences.empty())
Keno Fischerc2c60182015-05-31 23:37:04 +0000883 return UnknownRowIndex;
Alexey Samsonov947228c2012-08-07 11:46:57 +0000884 // First, find an instruction sequence containing the given address.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000885 DWARFDebugLine::Sequence Sequence;
886 Sequence.LowPC = Address;
887 SequenceIter FirstSeq = Sequences.begin();
888 SequenceIter LastSeq = Sequences.end();
889 SequenceIter SeqPos = std::lower_bound(
890 FirstSeq, LastSeq, Sequence, DWARFDebugLine::Sequence::orderByLowPC);
891 DWARFDebugLine::Sequence FoundSeq;
892 if (SeqPos == LastSeq) {
893 FoundSeq = Sequences.back();
894 } else if (SeqPos->LowPC == Address) {
895 FoundSeq = *SeqPos;
Alexey Samsonov947228c2012-08-07 11:46:57 +0000896 } else {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000897 if (SeqPos == FirstSeq)
Keno Fischerc2c60182015-05-31 23:37:04 +0000898 return UnknownRowIndex;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000899 FoundSeq = *(SeqPos - 1);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000900 }
Paul Robinson9d4eb692017-05-01 23:27:55 +0000901 return findRowInSeq(FoundSeq, Address);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000902}
Alexey Samsonov45be7932012-08-30 07:49:50 +0000903
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000904bool DWARFDebugLine::LineTable::lookupAddressRange(
Paul Robinson9d4eb692017-05-01 23:27:55 +0000905 uint64_t Address, uint64_t Size, std::vector<uint32_t> &Result) const {
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000906 if (Sequences.empty())
907 return false;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000908 uint64_t EndAddr = Address + Size;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000909 // First, find an instruction sequence containing the given address.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000910 DWARFDebugLine::Sequence Sequence;
911 Sequence.LowPC = Address;
912 SequenceIter FirstSeq = Sequences.begin();
913 SequenceIter LastSeq = Sequences.end();
914 SequenceIter SeqPos = std::lower_bound(
915 FirstSeq, LastSeq, Sequence, DWARFDebugLine::Sequence::orderByLowPC);
916 if (SeqPos == LastSeq || SeqPos->LowPC != Address) {
917 if (SeqPos == FirstSeq)
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000918 return false;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000919 SeqPos--;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000920 }
Paul Robinson9d4eb692017-05-01 23:27:55 +0000921 if (!SeqPos->containsPC(Address))
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000922 return false;
923
Paul Robinson9d4eb692017-05-01 23:27:55 +0000924 SequenceIter StartPos = SeqPos;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000925
926 // Add the rows from the first sequence to the vector, starting with the
927 // index we just calculated
928
Paul Robinson9d4eb692017-05-01 23:27:55 +0000929 while (SeqPos != LastSeq && SeqPos->LowPC < EndAddr) {
930 const DWARFDebugLine::Sequence &CurSeq = *SeqPos;
Keno Fischerc2c60182015-05-31 23:37:04 +0000931 // For the first sequence, we need to find which row in the sequence is the
932 // first in our range.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000933 uint32_t FirstRowIndex = CurSeq.FirstRowIndex;
934 if (SeqPos == StartPos)
935 FirstRowIndex = findRowInSeq(CurSeq, Address);
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000936
Keno Fischerc2c60182015-05-31 23:37:04 +0000937 // Figure out the last row in the range.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000938 uint32_t LastRowIndex = findRowInSeq(CurSeq, EndAddr - 1);
939 if (LastRowIndex == UnknownRowIndex)
940 LastRowIndex = CurSeq.LastRowIndex - 1;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000941
Paul Robinson9d4eb692017-05-01 23:27:55 +0000942 assert(FirstRowIndex != UnknownRowIndex);
943 assert(LastRowIndex != UnknownRowIndex);
Keno Fischerc2c60182015-05-31 23:37:04 +0000944
Paul Robinson9d4eb692017-05-01 23:27:55 +0000945 for (uint32_t I = FirstRowIndex; I <= LastRowIndex; ++I) {
946 Result.push_back(I);
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000947 }
948
Paul Robinson9d4eb692017-05-01 23:27:55 +0000949 ++SeqPos;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000950 }
NAKAMURA Takumi4b86cdb2013-01-26 01:45:06 +0000951
952 return true;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000953}
954
Paul Robinson9d4eb692017-05-01 23:27:55 +0000955bool DWARFDebugLine::LineTable::hasFileAtIndex(uint64_t FileIndex) const {
Pete Cooperb2ba7762016-07-22 01:41:32 +0000956 return FileIndex != 0 && FileIndex <= Prologue.FileNames.size();
957}
958
Scott Linder16c7bda2018-02-23 23:01:06 +0000959Optional<StringRef> DWARFDebugLine::LineTable::getSourceByIndex(uint64_t FileIndex,
960 FileLineInfoKind Kind) const {
961 if (Kind == FileLineInfoKind::None || !hasFileAtIndex(FileIndex))
962 return None;
963 const FileNameEntry &Entry = Prologue.FileNames[FileIndex - 1];
964 if (Optional<const char *> source = Entry.Source.getAsCString())
965 return StringRef(*source);
966 return None;
967}
968
Eugene Zemtsov82d60d62018-03-13 17:54:29 +0000969static bool isPathAbsoluteOnWindowsOrPosix(const Twine &Path) {
970 // Debug info can contain paths from any OS, not necessarily
971 // an OS we're currently running on. Moreover different compilation units can
972 // be compiled on different operating systems and linked together later.
973 return sys::path::is_absolute(Path, sys::path::Style::posix) ||
974 sys::path::is_absolute(Path, sys::path::Style::windows);
975}
976
Paul Robinson9d4eb692017-05-01 23:27:55 +0000977bool DWARFDebugLine::LineTable::getFileNameByIndex(uint64_t FileIndex,
978 const char *CompDir,
979 FileLineInfoKind Kind,
980 std::string &Result) const {
Pete Cooperb2ba7762016-07-22 01:41:32 +0000981 if (Kind == FileLineInfoKind::None || !hasFileAtIndex(FileIndex))
Alexey Samsonov45be7932012-08-30 07:49:50 +0000982 return false;
983 const FileNameEntry &Entry = Prologue.FileNames[FileIndex - 1];
Paul Robinson0a227092018-02-05 20:43:15 +0000984 StringRef FileName = Entry.Name.getAsCString().getValue();
Alexey Samsonovdce67342014-05-15 21:24:32 +0000985 if (Kind != FileLineInfoKind::AbsoluteFilePath ||
Eugene Zemtsov82d60d62018-03-13 17:54:29 +0000986 isPathAbsoluteOnWindowsOrPosix(FileName)) {
Alexey Samsonov45be7932012-08-30 07:49:50 +0000987 Result = FileName;
988 return true;
989 }
Frederic Riss101b5e22014-09-19 15:11:51 +0000990
Alexey Samsonov45be7932012-08-30 07:49:50 +0000991 SmallString<16> FilePath;
992 uint64_t IncludeDirIndex = Entry.DirIdx;
Paul Robinsonba1c9152017-05-02 17:37:32 +0000993 StringRef IncludeDir;
Alexey Samsonov45be7932012-08-30 07:49:50 +0000994 // Be defensive about the contents of Entry.
995 if (IncludeDirIndex > 0 &&
Frederic Riss101b5e22014-09-19 15:11:51 +0000996 IncludeDirIndex <= Prologue.IncludeDirectories.size())
Paul Robinson0a227092018-02-05 20:43:15 +0000997 IncludeDir = Prologue.IncludeDirectories[IncludeDirIndex - 1]
998 .getAsCString()
999 .getValue();
Frederic Riss101b5e22014-09-19 15:11:51 +00001000
1001 // We may still need to append compilation directory of compile unit.
1002 // We know that FileName is not absolute, the only way to have an
1003 // absolute path at this point would be if IncludeDir is absolute.
1004 if (CompDir && Kind == FileLineInfoKind::AbsoluteFilePath &&
Eugene Zemtsov82d60d62018-03-13 17:54:29 +00001005 !isPathAbsoluteOnWindowsOrPosix(IncludeDir))
Frederic Riss101b5e22014-09-19 15:11:51 +00001006 sys::path::append(FilePath, CompDir);
1007
1008 // sys::path::append skips empty strings.
1009 sys::path::append(FilePath, IncludeDir, FileName);
Alexey Samsonov45be7932012-08-30 07:49:50 +00001010 Result = FilePath.str();
1011 return true;
1012}
Frederic Riss101b5e22014-09-19 15:11:51 +00001013
Dehao Chen1b54fce2016-04-28 22:09:37 +00001014bool DWARFDebugLine::LineTable::getFileLineInfoForAddress(
1015 uint64_t Address, const char *CompDir, FileLineInfoKind Kind,
1016 DILineInfo &Result) const {
Frederic Riss101b5e22014-09-19 15:11:51 +00001017 // Get the index of row we're looking for in the line table.
1018 uint32_t RowIndex = lookupAddress(Address);
1019 if (RowIndex == -1U)
1020 return false;
1021 // Take file number and line/column from the row.
1022 const auto &Row = Rows[RowIndex];
1023 if (!getFileNameByIndex(Row.File, CompDir, Kind, Result.FileName))
1024 return false;
1025 Result.Line = Row.Line;
1026 Result.Column = Row.Column;
Eric Christopherba1024c2016-12-14 18:29:39 +00001027 Result.Discriminator = Row.Discriminator;
Scott Linder16c7bda2018-02-23 23:01:06 +00001028 Result.Source = getSourceByIndex(Row.File, Kind);
Frederic Riss101b5e22014-09-19 15:11:51 +00001029 return true;
1030}
James Hendersona3acf992018-05-10 10:51:33 +00001031
1032// We want to supply the Unit associated with a .debug_line[.dwo] table when
1033// we dump it, if possible, but still dump the table even if there isn't a Unit.
1034// Therefore, collect up handles on all the Units that point into the
1035// line-table section.
1036static DWARFDebugLine::SectionParser::LineToUnitMap
1037buildLineToUnitMap(DWARFDebugLine::SectionParser::cu_range CUs,
Paul Robinson7f330942018-08-01 20:46:46 +00001038 DWARFDebugLine::SectionParser::tu_range TUs) {
James Hendersona3acf992018-05-10 10:51:33 +00001039 DWARFDebugLine::SectionParser::LineToUnitMap LineToUnit;
1040 for (const auto &CU : CUs)
1041 if (auto CUDIE = CU->getUnitDIE())
1042 if (auto StmtOffset = toSectionOffset(CUDIE.find(DW_AT_stmt_list)))
1043 LineToUnit.insert(std::make_pair(*StmtOffset, &*CU));
Paul Robinson7f330942018-08-01 20:46:46 +00001044 for (const auto &TU : TUs)
1045 if (auto TUDIE = TU->getUnitDIE())
1046 if (auto StmtOffset = toSectionOffset(TUDIE.find(DW_AT_stmt_list)))
1047 LineToUnit.insert(std::make_pair(*StmtOffset, &*TU));
James Hendersona3acf992018-05-10 10:51:33 +00001048 return LineToUnit;
1049}
1050
1051DWARFDebugLine::SectionParser::SectionParser(DWARFDataExtractor &Data,
1052 const DWARFContext &C,
1053 cu_range CUs, tu_range TUs)
1054 : DebugLineData(Data), Context(C) {
1055 LineToUnit = buildLineToUnitMap(CUs, TUs);
1056 if (!DebugLineData.isValidOffset(Offset))
1057 Done = true;
1058}
1059
1060bool DWARFDebugLine::Prologue::totalLengthIsValid() const {
1061 return TotalLength == 0xffffffff || TotalLength < 0xffffff00;
1062}
1063
1064DWARFDebugLine::LineTable DWARFDebugLine::SectionParser::parseNext(
James Henderson004b7292018-05-21 15:30:54 +00001065 function_ref<void(Error)> RecoverableErrorCallback,
1066 function_ref<void(Error)> UnrecoverableErrorCallback, raw_ostream *OS) {
James Hendersona3acf992018-05-10 10:51:33 +00001067 assert(DebugLineData.isValidOffset(Offset) &&
1068 "parsing should have terminated");
1069 DWARFUnit *U = prepareToParse(Offset);
1070 uint32_t OldOffset = Offset;
1071 LineTable LT;
James Henderson004b7292018-05-21 15:30:54 +00001072 if (Error Err = LT.parse(DebugLineData, &Offset, Context, U,
1073 RecoverableErrorCallback, OS))
1074 UnrecoverableErrorCallback(std::move(Err));
James Hendersona3acf992018-05-10 10:51:33 +00001075 moveToNextTable(OldOffset, LT.Prologue);
1076 return LT;
1077}
1078
1079void DWARFDebugLine::SectionParser::skip(
1080 function_ref<void(Error)> ErrorCallback) {
1081 assert(DebugLineData.isValidOffset(Offset) &&
1082 "parsing should have terminated");
1083 DWARFUnit *U = prepareToParse(Offset);
1084 uint32_t OldOffset = Offset;
1085 LineTable LT;
James Henderson004b7292018-05-21 15:30:54 +00001086 if (Error Err = LT.Prologue.parse(DebugLineData, &Offset, Context, U))
1087 ErrorCallback(std::move(Err));
James Hendersona3acf992018-05-10 10:51:33 +00001088 moveToNextTable(OldOffset, LT.Prologue);
1089}
1090
1091DWARFUnit *DWARFDebugLine::SectionParser::prepareToParse(uint32_t Offset) {
1092 DWARFUnit *U = nullptr;
1093 auto It = LineToUnit.find(Offset);
1094 if (It != LineToUnit.end())
1095 U = It->second;
1096 DebugLineData.setAddressSize(U ? U->getAddressByteSize() : 0);
1097 return U;
1098}
1099
1100void DWARFDebugLine::SectionParser::moveToNextTable(uint32_t OldOffset,
1101 const Prologue &P) {
1102 // If the length field is not valid, we don't know where the next table is, so
1103 // cannot continue to parse. Mark the parser as done, and leave the Offset
1104 // value as it currently is. This will be the end of the bad length field.
1105 if (!P.totalLengthIsValid()) {
1106 Done = true;
1107 return;
1108 }
1109
1110 Offset = OldOffset + P.TotalLength + P.sizeofTotalLength();
1111 if (!DebugLineData.isValidOffset(Offset)) {
1112 Done = true;
1113 }
1114}
1115
James Henderson004b7292018-05-21 15:30:54 +00001116void DWARFDebugLine::warn(Error Err) {
1117 handleAllErrors(std::move(Err), [](ErrorInfoBase &Info) {
1118 WithColor::warning() << Info.message() << '\n';
1119 });
James Hendersona3acf992018-05-10 10:51:33 +00001120}