blob: a1cb1e8582eda22658090655a3fb27660af44dc8 [file] [log] [blame]
Eugene Zelenkoe94042c2017-02-27 23:43:14 +00001//===- DWARFDebugLine.cpp -------------------------------------------------===//
Benjamin Kramer5acab502011-09-15 02:12:05 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Benjamin Kramer5acab502011-09-15 02:12:05 +00006//
7//===----------------------------------------------------------------------===//
8
Paul Robinson9d4eb692017-05-01 23:27:55 +00009#include "llvm/DebugInfo/DWARF/DWARFDebugLine.h"
Scott Linder16c7bda2018-02-23 23:01:06 +000010#include "llvm/ADT/Optional.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000011#include "llvm/ADT/SmallString.h"
Eugene Zelenko2db0cfa2017-06-23 21:57:40 +000012#include "llvm/ADT/SmallVector.h"
13#include "llvm/ADT/StringRef.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000014#include "llvm/BinaryFormat/Dwarf.h"
Paul Robinson2bc38732017-05-02 21:40:47 +000015#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000016#include "llvm/DebugInfo/DWARF/DWARFRelocMap.h"
Victor Leschukcba595d2018-08-20 09:59:08 +000017#include "llvm/Support/Errc.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
Jonas Devlieghereca16d282019-07-16 01:21:25 +000069bool DWARFDebugLine::Prologue::hasFileAtIndex(uint64_t FileIndex) const {
70 uint16_t DwarfVersion = getVersion();
71 assert(DwarfVersion != 0 &&
72 "line table prologue has no dwarf version information");
73 if (DwarfVersion >= 5)
74 return FileIndex < FileNames.size();
75 return FileIndex != 0 && FileIndex <= FileNames.size();
76}
77
78const llvm::DWARFDebugLine::FileNameEntry &
79DWARFDebugLine::Prologue::getFileNameEntry(uint64_t Index) const {
80 uint16_t DwarfVersion = getVersion();
81 assert(DwarfVersion != 0 &&
82 "line table prologue has no dwarf version information");
83 // In DWARF v5 the file names are 0-indexed.
84 if (DwarfVersion >= 5)
85 return FileNames[Index];
86 return FileNames[Index - 1];
87}
88
Alexey Samsonov836b1ae2014-04-29 21:28:13 +000089void DWARFDebugLine::Prologue::clear() {
Paul Robinson75c068c2017-06-26 18:43:01 +000090 TotalLength = PrologueLength = 0;
91 SegSelectorSize = 0;
Alexey Samsonov836b1ae2014-04-29 21:28:13 +000092 MinInstLength = MaxOpsPerInst = DefaultIsStmt = LineBase = LineRange = 0;
93 OpcodeBase = 0;
Pavel Labath322711f2018-03-14 09:39:54 +000094 FormParams = dwarf::FormParams({0, 0, DWARF32});
Scott Linder16c7bda2018-02-23 23:01:06 +000095 ContentTypes = ContentTypeTracker();
Alexey Samsonov836b1ae2014-04-29 21:28:13 +000096 StandardOpcodeLengths.clear();
97 IncludeDirectories.clear();
98 FileNames.clear();
99}
100
Paul Robinson0a227092018-02-05 20:43:15 +0000101void DWARFDebugLine::Prologue::dump(raw_ostream &OS,
102 DIDumpOptions DumpOptions) const {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000103 OS << "Line table prologue:\n"
Ed Maste6d0bee52015-05-28 15:38:17 +0000104 << format(" total_length: 0x%8.8" PRIx64 "\n", TotalLength)
Paul Robinson75c068c2017-06-26 18:43:01 +0000105 << format(" version: %u\n", getVersion());
106 if (getVersion() >= 5)
107 OS << format(" address_size: %u\n", getAddressSize())
108 << format(" seg_select_size: %u\n", SegSelectorSize);
109 OS << format(" prologue_length: 0x%8.8" PRIx64 "\n", PrologueLength)
David Blaikie1d4736e2014-02-24 23:58:54 +0000110 << format(" min_inst_length: %u\n", MinInstLength)
Paul Robinson75c068c2017-06-26 18:43:01 +0000111 << format(getVersion() >= 4 ? "max_ops_per_inst: %u\n" : "", MaxOpsPerInst)
David Blaikie1d4736e2014-02-24 23:58:54 +0000112 << format(" default_is_stmt: %u\n", DefaultIsStmt)
113 << format(" line_base: %i\n", LineBase)
114 << format(" line_range: %u\n", LineRange)
115 << format(" opcode_base: %u\n", OpcodeBase);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000116
Paul Robinson9d4eb692017-05-01 23:27:55 +0000117 for (uint32_t I = 0; I != StandardOpcodeLengths.size(); ++I)
Mehdi Amini149f6ea2016-10-05 05:59:29 +0000118 OS << format("standard_opcode_lengths[%s] = %u\n",
Paul Robinson9d4eb692017-05-01 23:27:55 +0000119 LNStandardString(I + 1).data(), StandardOpcodeLengths[I]);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000120
Paul Robinson8181d232018-01-18 20:33:35 +0000121 if (!IncludeDirectories.empty()) {
122 // DWARF v5 starts directory indexes at 0.
123 uint32_t DirBase = getVersion() >= 5 ? 0 : 1;
Paul Robinson0a227092018-02-05 20:43:15 +0000124 for (uint32_t I = 0; I != IncludeDirectories.size(); ++I) {
125 OS << format("include_directories[%3u] = ", I + DirBase);
126 IncludeDirectories[I].dump(OS, DumpOptions);
127 OS << '\n';
128 }
Paul Robinson8181d232018-01-18 20:33:35 +0000129 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000130
131 if (!FileNames.empty()) {
Paul Robinsonceafcd42018-02-08 23:08:02 +0000132 // DWARF v5 starts file indexes at 0.
133 uint32_t FileBase = getVersion() >= 5 ? 0 : 1;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000134 for (uint32_t I = 0; I != FileNames.size(); ++I) {
135 const FileNameEntry &FileEntry = FileNames[I];
Scott Linder16c7bda2018-02-23 23:01:06 +0000136 OS << format("file_names[%3u]:\n", I + FileBase);
137 OS << " name: ";
Paul Robinson0a227092018-02-05 20:43:15 +0000138 FileEntry.Name.dump(OS, DumpOptions);
Scott Linder16c7bda2018-02-23 23:01:06 +0000139 OS << '\n'
140 << format(" dir_index: %" PRIu64 "\n", FileEntry.DirIdx);
141 if (ContentTypes.HasMD5)
142 OS << " md5_checksum: " << FileEntry.Checksum.digest() << '\n';
143 if (ContentTypes.HasModTime)
144 OS << format(" mod_time: 0x%8.8" PRIx64 "\n", FileEntry.ModTime);
145 if (ContentTypes.HasLength)
146 OS << format(" length: 0x%8.8" PRIx64 "\n", FileEntry.Length);
147 if (ContentTypes.HasSource) {
148 OS << " source: ";
149 FileEntry.Source.dump(OS, DumpOptions);
150 OS << '\n';
151 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000152 }
153 }
154}
155
Paul Robinson2bc38732017-05-02 21:40:47 +0000156// Parse v2-v4 directory and file tables.
157static void
Paul Robinson17536b92017-06-29 16:52:08 +0000158parseV2DirFileTables(const DWARFDataExtractor &DebugLineData,
159 uint32_t *OffsetPtr, uint64_t EndPrologueOffset,
Scott Linder16c7bda2018-02-23 23:01:06 +0000160 DWARFDebugLine::ContentTypeTracker &ContentTypes,
Paul Robinson0a227092018-02-05 20:43:15 +0000161 std::vector<DWARFFormValue> &IncludeDirectories,
Paul Robinson2bc38732017-05-02 21:40:47 +0000162 std::vector<DWARFDebugLine::FileNameEntry> &FileNames) {
163 while (*OffsetPtr < EndPrologueOffset) {
164 StringRef S = DebugLineData.getCStrRef(OffsetPtr);
165 if (S.empty())
166 break;
Jonas Devliegherebb111152019-02-27 00:58:09 +0000167 DWARFFormValue Dir =
168 DWARFFormValue::createFromPValue(dwarf::DW_FORM_string, S.data());
Paul Robinson0a227092018-02-05 20:43:15 +0000169 IncludeDirectories.push_back(Dir);
Paul Robinson2bc38732017-05-02 21:40:47 +0000170 }
171
172 while (*OffsetPtr < EndPrologueOffset) {
173 StringRef Name = DebugLineData.getCStrRef(OffsetPtr);
174 if (Name.empty())
175 break;
176 DWARFDebugLine::FileNameEntry FileEntry;
Jonas Devliegherebb111152019-02-27 00:58:09 +0000177 FileEntry.Name =
178 DWARFFormValue::createFromPValue(dwarf::DW_FORM_string, Name.data());
Paul Robinson2bc38732017-05-02 21:40:47 +0000179 FileEntry.DirIdx = DebugLineData.getULEB128(OffsetPtr);
180 FileEntry.ModTime = DebugLineData.getULEB128(OffsetPtr);
181 FileEntry.Length = DebugLineData.getULEB128(OffsetPtr);
182 FileNames.push_back(FileEntry);
183 }
Scott Linder16c7bda2018-02-23 23:01:06 +0000184
185 ContentTypes.HasModTime = true;
186 ContentTypes.HasLength = true;
Paul Robinson2bc38732017-05-02 21:40:47 +0000187}
188
189// Parse v5 directory/file entry content descriptions.
190// Returns the descriptors, or an empty vector if we did not find a path or
191// ran off the end of the prologue.
192static ContentDescriptors
Scott Linder16c7bda2018-02-23 23:01:06 +0000193parseV5EntryFormat(const DWARFDataExtractor &DebugLineData, uint32_t
194 *OffsetPtr, uint64_t EndPrologueOffset, DWARFDebugLine::ContentTypeTracker
195 *ContentTypes) {
Paul Robinson2bc38732017-05-02 21:40:47 +0000196 ContentDescriptors Descriptors;
197 int FormatCount = DebugLineData.getU8(OffsetPtr);
198 bool HasPath = false;
199 for (int I = 0; I != FormatCount; ++I) {
200 if (*OffsetPtr >= EndPrologueOffset)
201 return ContentDescriptors();
202 ContentDescriptor Descriptor;
203 Descriptor.Type =
204 dwarf::LineNumberEntryFormat(DebugLineData.getULEB128(OffsetPtr));
205 Descriptor.Form = dwarf::Form(DebugLineData.getULEB128(OffsetPtr));
206 if (Descriptor.Type == dwarf::DW_LNCT_path)
207 HasPath = true;
Scott Linder16c7bda2018-02-23 23:01:06 +0000208 if (ContentTypes)
209 ContentTypes->trackContentType(Descriptor.Type);
Paul Robinson2bc38732017-05-02 21:40:47 +0000210 Descriptors.push_back(Descriptor);
211 }
212 return HasPath ? Descriptors : ContentDescriptors();
213}
214
215static bool
Paul Robinson17536b92017-06-29 16:52:08 +0000216parseV5DirFileTables(const DWARFDataExtractor &DebugLineData,
217 uint32_t *OffsetPtr, uint64_t EndPrologueOffset,
Pavel Labath322711f2018-03-14 09:39:54 +0000218 const dwarf::FormParams &FormParams,
219 const DWARFContext &Ctx, const DWARFUnit *U,
Scott Linder16c7bda2018-02-23 23:01:06 +0000220 DWARFDebugLine::ContentTypeTracker &ContentTypes,
Paul Robinson0a227092018-02-05 20:43:15 +0000221 std::vector<DWARFFormValue> &IncludeDirectories,
Paul Robinson2bc38732017-05-02 21:40:47 +0000222 std::vector<DWARFDebugLine::FileNameEntry> &FileNames) {
223 // Get the directory entry description.
224 ContentDescriptors DirDescriptors =
Paul Robinsona06f8dc2017-12-18 19:08:35 +0000225 parseV5EntryFormat(DebugLineData, OffsetPtr, EndPrologueOffset, nullptr);
Paul Robinson2bc38732017-05-02 21:40:47 +0000226 if (DirDescriptors.empty())
227 return false;
228
229 // Get the directory entries, according to the format described above.
230 int DirEntryCount = DebugLineData.getU8(OffsetPtr);
231 for (int I = 0; I != DirEntryCount; ++I) {
232 if (*OffsetPtr >= EndPrologueOffset)
233 return false;
234 for (auto Descriptor : DirDescriptors) {
Vlad Tsyrklevich53a9f1d2019-03-02 01:10:00 +0000235 DWARFFormValue Value(Descriptor.Form);
Paul Robinson2bc38732017-05-02 21:40:47 +0000236 switch (Descriptor.Type) {
237 case DW_LNCT_path:
Vlad Tsyrklevich53a9f1d2019-03-02 01:10:00 +0000238 if (!Value.extractValue(DebugLineData, OffsetPtr, FormParams, &Ctx, U))
239 return false;
240 IncludeDirectories.push_back(Value);
Paul Robinson2bc38732017-05-02 21:40:47 +0000241 break;
242 default:
Vlad Tsyrklevich53a9f1d2019-03-02 01:10:00 +0000243 if (!Value.skipValue(DebugLineData, OffsetPtr, FormParams))
Paul Robinson2bc38732017-05-02 21:40:47 +0000244 return false;
245 }
246 }
247 }
248
249 // Get the file entry description.
250 ContentDescriptors FileDescriptors =
Scott Linder16c7bda2018-02-23 23:01:06 +0000251 parseV5EntryFormat(DebugLineData, OffsetPtr, EndPrologueOffset,
252 &ContentTypes);
Paul Robinson2bc38732017-05-02 21:40:47 +0000253 if (FileDescriptors.empty())
254 return false;
255
256 // Get the file entries, according to the format described above.
257 int FileEntryCount = DebugLineData.getU8(OffsetPtr);
258 for (int I = 0; I != FileEntryCount; ++I) {
259 if (*OffsetPtr >= EndPrologueOffset)
260 return false;
261 DWARFDebugLine::FileNameEntry FileEntry;
262 for (auto Descriptor : FileDescriptors) {
Vlad Tsyrklevich53a9f1d2019-03-02 01:10:00 +0000263 DWARFFormValue Value(Descriptor.Form);
264 if (!Value.extractValue(DebugLineData, OffsetPtr, FormParams, &Ctx, U))
265 return false;
Paul Robinson2bc38732017-05-02 21:40:47 +0000266 switch (Descriptor.Type) {
267 case DW_LNCT_path:
Paul Robinson0a227092018-02-05 20:43:15 +0000268 FileEntry.Name = Value;
Paul Robinson2bc38732017-05-02 21:40:47 +0000269 break;
Scott Linder16c7bda2018-02-23 23:01:06 +0000270 case DW_LNCT_LLVM_source:
271 FileEntry.Source = Value;
272 break;
Paul Robinson2bc38732017-05-02 21:40:47 +0000273 case DW_LNCT_directory_index:
274 FileEntry.DirIdx = Value.getAsUnsignedConstant().getValue();
275 break;
276 case DW_LNCT_timestamp:
277 FileEntry.ModTime = Value.getAsUnsignedConstant().getValue();
278 break;
279 case DW_LNCT_size:
280 FileEntry.Length = Value.getAsUnsignedConstant().getValue();
281 break;
Paul Robinsona06f8dc2017-12-18 19:08:35 +0000282 case DW_LNCT_MD5:
283 assert(Value.getAsBlock().getValue().size() == 16);
284 std::uninitialized_copy_n(Value.getAsBlock().getValue().begin(), 16,
285 FileEntry.Checksum.Bytes.begin());
286 break;
Paul Robinson2bc38732017-05-02 21:40:47 +0000287 default:
288 break;
289 }
290 }
291 FileNames.push_back(FileEntry);
292 }
293 return true;
294}
295
James Hendersona3acf992018-05-10 10:51:33 +0000296Error DWARFDebugLine::Prologue::parse(const DWARFDataExtractor &DebugLineData,
297 uint32_t *OffsetPtr,
298 const DWARFContext &Ctx,
299 const DWARFUnit *U) {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000300 const uint64_t PrologueOffset = *OffsetPtr;
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000301
302 clear();
Alex Bradbury44deaf72019-07-18 05:22:55 +0000303 TotalLength = DebugLineData.getRelocatedValue(4, OffsetPtr);
Ed Maste6d0bee52015-05-28 15:38:17 +0000304 if (TotalLength == UINT32_MAX) {
Paul Robinson75c068c2017-06-26 18:43:01 +0000305 FormParams.Format = dwarf::DWARF64;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000306 TotalLength = DebugLineData.getU64(OffsetPtr);
Igor Kudrinf48bc012019-07-16 07:01:08 +0000307 } else if (TotalLength >= 0xfffffff0) {
Victor Leschukcba595d2018-08-20 09:59:08 +0000308 return createStringError(errc::invalid_argument,
James Hendersona3acf992018-05-10 10:51:33 +0000309 "parsing line table prologue at offset 0x%8.8" PRIx64
310 " unsupported reserved unit length found of value 0x%8.8" PRIx64,
311 PrologueOffset, TotalLength);
Ed Maste6d0bee52015-05-28 15:38:17 +0000312 }
Paul Robinson75c068c2017-06-26 18:43:01 +0000313 FormParams.Version = DebugLineData.getU16(OffsetPtr);
314 if (getVersion() < 2)
Victor Leschukcba595d2018-08-20 09:59:08 +0000315 return createStringError(errc::not_supported,
316 "parsing line table prologue at offset 0x%8.8" PRIx64
James Hendersona3acf992018-05-10 10:51:33 +0000317 " found unsupported version 0x%2.2" PRIx16,
318 PrologueOffset, getVersion());
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000319
Paul Robinson75c068c2017-06-26 18:43:01 +0000320 if (getVersion() >= 5) {
321 FormParams.AddrSize = DebugLineData.getU8(OffsetPtr);
Paul Robinson63811a42017-11-22 15:33:17 +0000322 assert((DebugLineData.getAddressSize() == 0 ||
323 DebugLineData.getAddressSize() == getAddressSize()) &&
Paul Robinson75c068c2017-06-26 18:43:01 +0000324 "Line table header and data extractor disagree");
Paul Robinson2bc38732017-05-02 21:40:47 +0000325 SegSelectorSize = DebugLineData.getU8(OffsetPtr);
326 }
327
Alex Bradbury44deaf72019-07-18 05:22:55 +0000328 PrologueLength =
329 DebugLineData.getRelocatedValue(sizeofPrologueLength(), OffsetPtr);
Paul Robinson9d4eb692017-05-01 23:27:55 +0000330 const uint64_t EndPrologueOffset = PrologueLength + *OffsetPtr;
331 MinInstLength = DebugLineData.getU8(OffsetPtr);
Paul Robinson75c068c2017-06-26 18:43:01 +0000332 if (getVersion() >= 4)
Paul Robinson9d4eb692017-05-01 23:27:55 +0000333 MaxOpsPerInst = DebugLineData.getU8(OffsetPtr);
334 DefaultIsStmt = DebugLineData.getU8(OffsetPtr);
335 LineBase = DebugLineData.getU8(OffsetPtr);
336 LineRange = DebugLineData.getU8(OffsetPtr);
337 OpcodeBase = DebugLineData.getU8(OffsetPtr);
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000338
339 StandardOpcodeLengths.reserve(OpcodeBase - 1);
Paul Robinson9d4eb692017-05-01 23:27:55 +0000340 for (uint32_t I = 1; I < OpcodeBase; ++I) {
341 uint8_t OpLen = DebugLineData.getU8(OffsetPtr);
342 StandardOpcodeLengths.push_back(OpLen);
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000343 }
344
Paul Robinson75c068c2017-06-26 18:43:01 +0000345 if (getVersion() >= 5) {
Paul Robinson2bc38732017-05-02 21:40:47 +0000346 if (!parseV5DirFileTables(DebugLineData, OffsetPtr, EndPrologueOffset,
Scott Linder16c7bda2018-02-23 23:01:06 +0000347 FormParams, Ctx, U, ContentTypes,
348 IncludeDirectories, FileNames)) {
Victor Leschukcba595d2018-08-20 09:59:08 +0000349 return createStringError(errc::invalid_argument,
Jonas Devlieghere84e99262018-04-14 22:07:23 +0000350 "parsing line table prologue at 0x%8.8" PRIx64
351 " found an invalid directory or file table description at"
James Hendersona3acf992018-05-10 10:51:33 +0000352 " 0x%8.8" PRIx64,
Jonas Devlieghere84e99262018-04-14 22:07:23 +0000353 PrologueOffset, (uint64_t)*OffsetPtr);
Paul Robinson2bc38732017-05-02 21:40:47 +0000354 }
355 } else
356 parseV2DirFileTables(DebugLineData, OffsetPtr, EndPrologueOffset,
Scott Linder16c7bda2018-02-23 23:01:06 +0000357 ContentTypes, IncludeDirectories, FileNames);
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000358
James Hendersona3acf992018-05-10 10:51:33 +0000359 if (*OffsetPtr != EndPrologueOffset)
Victor Leschukcba595d2018-08-20 09:59:08 +0000360 return createStringError(errc::invalid_argument,
361 "parsing line table prologue at 0x%8.8" PRIx64
James Hendersona3acf992018-05-10 10:51:33 +0000362 " should have ended at 0x%8.8" PRIx64
363 " but it ended at 0x%8.8" PRIx64,
364 PrologueOffset, EndPrologueOffset, (uint64_t)*OffsetPtr);
365 return Error::success();
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000366}
367
Paul Robinson9d4eb692017-05-01 23:27:55 +0000368DWARFDebugLine::Row::Row(bool DefaultIsStmt) { reset(DefaultIsStmt); }
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000369
Benjamin Kramer5acab502011-09-15 02:12:05 +0000370void DWARFDebugLine::Row::postAppend() {
Fangrui Song6a285df2019-04-11 02:02:44 +0000371 Discriminator = 0;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000372 BasicBlock = false;
373 PrologueEnd = false;
374 EpilogueBegin = false;
375}
376
Paul Robinson9d4eb692017-05-01 23:27:55 +0000377void DWARFDebugLine::Row::reset(bool DefaultIsStmt) {
Alexey Lapshin77fc1f62019-02-27 13:17:36 +0000378 Address.Address = 0;
379 Address.SectionIndex = object::SectionedAddress::UndefSection;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000380 Line = 1;
381 Column = 0;
382 File = 1;
383 Isa = 0;
Diego Novillo5b5cf502014-02-14 19:27:53 +0000384 Discriminator = 0;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000385 IsStmt = DefaultIsStmt;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000386 BasicBlock = false;
387 EndSequence = false;
388 PrologueEnd = false;
389 EpilogueBegin = false;
390}
391
Greg Clayton67070462017-05-02 22:48:52 +0000392void DWARFDebugLine::Row::dumpTableHeader(raw_ostream &OS) {
393 OS << "Address Line Column File ISA Discriminator Flags\n"
394 << "------------------ ------ ------ ------ --- ------------- "
395 "-------------\n";
396}
397
Benjamin Kramer5acab502011-09-15 02:12:05 +0000398void DWARFDebugLine::Row::dump(raw_ostream &OS) const {
Alexey Lapshin77fc1f62019-02-27 13:17:36 +0000399 OS << format("0x%16.16" PRIx64 " %6u %6u", Address.Address, Line, Column)
Diego Novillo5b5cf502014-02-14 19:27:53 +0000400 << format(" %6u %3u %13u ", File, Isa, Discriminator)
Dehao Chen1b54fce2016-04-28 22:09:37 +0000401 << (IsStmt ? " is_stmt" : "") << (BasicBlock ? " basic_block" : "")
Benjamin Kramer5acab502011-09-15 02:12:05 +0000402 << (PrologueEnd ? " prologue_end" : "")
403 << (EpilogueBegin ? " epilogue_begin" : "")
Dehao Chen1b54fce2016-04-28 22:09:37 +0000404 << (EndSequence ? " end_sequence" : "") << '\n';
Benjamin Kramer5acab502011-09-15 02:12:05 +0000405}
406
Dehao Chen1b54fce2016-04-28 22:09:37 +0000407DWARFDebugLine::Sequence::Sequence() { reset(); }
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000408
409void DWARFDebugLine::Sequence::reset() {
410 LowPC = 0;
411 HighPC = 0;
Alexey Lapshin77fc1f62019-02-27 13:17:36 +0000412 SectionIndex = object::SectionedAddress::UndefSection;
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000413 FirstRowIndex = 0;
414 LastRowIndex = 0;
415 Empty = true;
416}
417
Dehao Chen1b54fce2016-04-28 22:09:37 +0000418DWARFDebugLine::LineTable::LineTable() { clear(); }
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000419
Paul Robinson0a227092018-02-05 20:43:15 +0000420void DWARFDebugLine::LineTable::dump(raw_ostream &OS,
421 DIDumpOptions DumpOptions) const {
422 Prologue.dump(OS, DumpOptions);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000423 OS << '\n';
424
425 if (!Rows.empty()) {
Greg Clayton67070462017-05-02 22:48:52 +0000426 Row::dumpTableHeader(OS);
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000427 for (const Row &R : Rows) {
428 R.dump(OS);
429 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000430 }
431}
432
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000433void DWARFDebugLine::LineTable::clear() {
434 Prologue.clear();
435 Rows.clear();
436 Sequences.clear();
437}
438
Alexey Samsonov110d5952014-04-30 00:09:19 +0000439DWARFDebugLine::ParsingState::ParsingState(struct LineTable *LT)
Eugene Zelenko2db0cfa2017-06-23 21:57:40 +0000440 : LineTable(LT) {
Alexey Samsonov110d5952014-04-30 00:09:19 +0000441 resetRowAndSequence();
442}
Nick Lewycky4d044922011-09-15 03:41:51 +0000443
Alexey Samsonov110d5952014-04-30 00:09:19 +0000444void DWARFDebugLine::ParsingState::resetRowAndSequence() {
445 Row.reset(LineTable->Prologue.DefaultIsStmt);
446 Sequence.reset();
447}
448
Fangrui Songc4c8bca2019-04-07 13:56:14 +0000449void DWARFDebugLine::ParsingState::appendRowToMatrix() {
Fangrui Song50a09672019-04-15 07:40:30 +0000450 unsigned RowNumber = LineTable->Rows.size();
Alexey Samsonov110d5952014-04-30 00:09:19 +0000451 if (Sequence.Empty) {
Alexey Samsonov947228c2012-08-07 11:46:57 +0000452 // Record the beginning of instruction sequence.
Alexey Samsonov110d5952014-04-30 00:09:19 +0000453 Sequence.Empty = false;
Alexey Lapshin77fc1f62019-02-27 13:17:36 +0000454 Sequence.LowPC = Row.Address.Address;
Alexey Samsonov110d5952014-04-30 00:09:19 +0000455 Sequence.FirstRowIndex = RowNumber;
Alexey Samsonov947228c2012-08-07 11:46:57 +0000456 }
Alexey Samsonov110d5952014-04-30 00:09:19 +0000457 LineTable->appendRow(Row);
458 if (Row.EndSequence) {
Alexey Samsonov947228c2012-08-07 11:46:57 +0000459 // Record the end of instruction sequence.
Alexey Lapshin77fc1f62019-02-27 13:17:36 +0000460 Sequence.HighPC = Row.Address.Address;
Fangrui Song50a09672019-04-15 07:40:30 +0000461 Sequence.LastRowIndex = RowNumber + 1;
Alexey Lapshin77fc1f62019-02-27 13:17:36 +0000462 Sequence.SectionIndex = Row.Address.SectionIndex;
Alexey Samsonov110d5952014-04-30 00:09:19 +0000463 if (Sequence.isValid())
464 LineTable->appendSequence(Sequence);
465 Sequence.reset();
Alexey Samsonov947228c2012-08-07 11:46:57 +0000466 }
Alexey Samsonov110d5952014-04-30 00:09:19 +0000467 Row.postAppend();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000468}
469
Benjamin Kramer5acab502011-09-15 02:12:05 +0000470const DWARFDebugLine::LineTable *
Paul Robinson9d4eb692017-05-01 23:27:55 +0000471DWARFDebugLine::getLineTable(uint32_t Offset) const {
472 LineTableConstIter Pos = LineTableMap.find(Offset);
473 if (Pos != LineTableMap.end())
474 return &Pos->second;
Craig Topper2617dcc2014-04-15 06:32:26 +0000475 return nullptr;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000476}
477
James Hendersona3acf992018-05-10 10:51:33 +0000478Expected<const DWARFDebugLine::LineTable *> DWARFDebugLine::getOrParseLineTable(
479 DWARFDataExtractor &DebugLineData, uint32_t Offset, const DWARFContext &Ctx,
James Henderson004b7292018-05-21 15:30:54 +0000480 const DWARFUnit *U, std::function<void(Error)> RecoverableErrorCallback) {
James Henderson66702622018-03-08 10:53:34 +0000481 if (!DebugLineData.isValidOffset(Offset))
Victor Leschukcba595d2018-08-20 09:59:08 +0000482 return createStringError(errc::invalid_argument, "offset 0x%8.8" PRIx32
James Hendersona3acf992018-05-10 10:51:33 +0000483 " is not a valid debug line section offset",
484 Offset);
James Henderson66702622018-03-08 10:53:34 +0000485
Paul Robinson9d4eb692017-05-01 23:27:55 +0000486 std::pair<LineTableIter, bool> Pos =
487 LineTableMap.insert(LineTableMapTy::value_type(Offset, LineTable()));
488 LineTable *LT = &Pos.first->second;
489 if (Pos.second) {
James Henderson004b7292018-05-21 15:30:54 +0000490 if (Error Err =
491 LT->parse(DebugLineData, &Offset, Ctx, U, RecoverableErrorCallback))
James Hendersona3acf992018-05-10 10:51:33 +0000492 return std::move(Err);
493 return LT;
Benjamin Kramer679e1752011-09-15 20:43:18 +0000494 }
Alexey Samsonov110d5952014-04-30 00:09:19 +0000495 return LT;
Benjamin Kramer679e1752011-09-15 20:43:18 +0000496}
497
James Hendersona3acf992018-05-10 10:51:33 +0000498Error DWARFDebugLine::LineTable::parse(
499 DWARFDataExtractor &DebugLineData, uint32_t *OffsetPtr,
500 const DWARFContext &Ctx, const DWARFUnit *U,
James Henderson004b7292018-05-21 15:30:54 +0000501 std::function<void(Error)> RecoverableErrorCallback, raw_ostream *OS) {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000502 const uint32_t DebugLineOffset = *OffsetPtr;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000503
Alexey Samsonov110d5952014-04-30 00:09:19 +0000504 clear();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000505
James Hendersona3acf992018-05-10 10:51:33 +0000506 Error PrologueErr = Prologue.parse(DebugLineData, OffsetPtr, Ctx, U);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000507
Paul Robinson0a227092018-02-05 20:43:15 +0000508 if (OS) {
509 // The presence of OS signals verbose dumping.
510 DIDumpOptions DumpOptions;
511 DumpOptions.Verbose = true;
512 Prologue.dump(*OS, DumpOptions);
513 }
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000514
James Hendersona3acf992018-05-10 10:51:33 +0000515 if (PrologueErr)
516 return PrologueErr;
517
Paul Robinson9d4eb692017-05-01 23:27:55 +0000518 const uint32_t EndOffset =
519 DebugLineOffset + Prologue.TotalLength + Prologue.sizeofTotalLength();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000520
Paul Robinson511b54c2017-11-22 15:48:30 +0000521 // See if we should tell the data extractor the address size.
522 if (DebugLineData.getAddressSize() == 0)
523 DebugLineData.setAddressSize(Prologue.getAddressSize());
524 else
525 assert(Prologue.getAddressSize() == 0 ||
526 Prologue.getAddressSize() == DebugLineData.getAddressSize());
527
Alexey Samsonov110d5952014-04-30 00:09:19 +0000528 ParsingState State(this);
Benjamin Kramer112ec172011-09-15 21:59:13 +0000529
Paul Robinson9d4eb692017-05-01 23:27:55 +0000530 while (*OffsetPtr < EndOffset) {
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000531 if (OS)
532 *OS << format("0x%08.08" PRIx32 ": ", *OffsetPtr);
533
Paul Robinson9d4eb692017-05-01 23:27:55 +0000534 uint8_t Opcode = DebugLineData.getU8(OffsetPtr);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000535
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000536 if (OS)
537 *OS << format("%02.02" PRIx8 " ", Opcode);
538
Paul Robinson9d4eb692017-05-01 23:27:55 +0000539 if (Opcode == 0) {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000540 // Extended Opcodes always start with a zero opcode followed by
541 // a uleb128 length so you can skip ones you don't know about
Paul Robinson9d4eb692017-05-01 23:27:55 +0000542 uint64_t Len = DebugLineData.getULEB128(OffsetPtr);
Paul Robinsone0833342017-11-22 15:14:49 +0000543 uint32_t ExtOffset = *OffsetPtr;
544
545 // Tolerate zero-length; assume length is correct and soldier on.
546 if (Len == 0) {
547 if (OS)
548 *OS << "Badly formed extended line op (length 0)\n";
549 continue;
550 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000551
Paul Robinson9d4eb692017-05-01 23:27:55 +0000552 uint8_t SubOpcode = DebugLineData.getU8(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000553 if (OS)
554 *OS << LNExtendedString(SubOpcode);
Paul Robinson9d4eb692017-05-01 23:27:55 +0000555 switch (SubOpcode) {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000556 case DW_LNE_end_sequence:
557 // Set the end_sequence register of the state machine to true and
558 // append a row to the matrix using the current values of the
559 // state-machine registers. Then reset the registers to the initial
560 // values specified above. Every statement program sequence must end
561 // with a DW_LNE_end_sequence instruction which creates a row whose
562 // address is that of the byte after the last target machine instruction
563 // of the sequence.
Alexey Samsonov110d5952014-04-30 00:09:19 +0000564 State.Row.EndSequence = true;
Fangrui Songc4c8bca2019-04-07 13:56:14 +0000565 State.appendRowToMatrix();
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000566 if (OS) {
567 *OS << "\n";
568 OS->indent(12);
569 State.Row.dump(*OS);
570 }
Alexey Samsonov110d5952014-04-30 00:09:19 +0000571 State.resetRowAndSequence();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000572 break;
573
574 case DW_LNE_set_address:
575 // Takes a single relocatable address as an operand. The size of the
576 // operand is the size appropriate to hold an address on the target
577 // machine. Set the address register to the value given by the
578 // relocatable address. All of the other statement program opcodes
579 // that affect the address register add a delta to it. This instruction
580 // stores a relocatable value into it instead.
Paul Robinson511b54c2017-11-22 15:48:30 +0000581 //
582 // Make sure the extractor knows the address size. If not, infer it
583 // from the size of the operand.
584 if (DebugLineData.getAddressSize() == 0)
585 DebugLineData.setAddressSize(Len - 1);
Paul Robinson79474682018-03-22 19:37:56 +0000586 else if (DebugLineData.getAddressSize() != Len - 1) {
Victor Leschukcba595d2018-08-20 09:59:08 +0000587 return createStringError(errc::invalid_argument,
588 "mismatching address size at offset 0x%8.8" PRIx32
James Hendersona3acf992018-05-10 10:51:33 +0000589 " expected 0x%2.2" PRIx8 " found 0x%2.2" PRIx64,
590 ExtOffset, DebugLineData.getAddressSize(),
591 Len - 1);
Paul Robinson79474682018-03-22 19:37:56 +0000592 }
Alexey Lapshin77fc1f62019-02-27 13:17:36 +0000593 State.Row.Address.Address = DebugLineData.getRelocatedAddress(
594 OffsetPtr, &State.Row.Address.SectionIndex);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000595 if (OS)
Alexey Lapshin77fc1f62019-02-27 13:17:36 +0000596 *OS << format(" (0x%16.16" PRIx64 ")", State.Row.Address.Address);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000597 break;
598
599 case DW_LNE_define_file:
600 // Takes 4 arguments. The first is a null terminated string containing
601 // a source file name. The second is an unsigned LEB128 number
602 // representing the directory index of the directory in which the file
603 // was found. The third is an unsigned LEB128 number representing the
604 // time of last modification of the file. The fourth is an unsigned
605 // LEB128 number representing the length in bytes of the file. The time
606 // and length fields may contain LEB128(0) if the information is not
607 // available.
608 //
609 // The directory index represents an entry in the include_directories
610 // section of the statement program prologue. The index is LEB128(0)
611 // if the file was found in the current directory of the compilation,
612 // LEB128(1) if it was found in the first directory in the
613 // include_directories section, and so on. The directory index is
614 // ignored for file names that represent full path names.
615 //
616 // The files are numbered, starting at 1, in the order in which they
617 // appear; the names in the prologue come before names defined by
618 // the DW_LNE_define_file instruction. These numbers are used in the
619 // the file register of the state machine.
620 {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000621 FileNameEntry FileEntry;
Paul Robinson0a227092018-02-05 20:43:15 +0000622 const char *Name = DebugLineData.getCStr(OffsetPtr);
Jonas Devliegherebb111152019-02-27 00:58:09 +0000623 FileEntry.Name =
624 DWARFFormValue::createFromPValue(dwarf::DW_FORM_string, Name);
Paul Robinson9d4eb692017-05-01 23:27:55 +0000625 FileEntry.DirIdx = DebugLineData.getULEB128(OffsetPtr);
626 FileEntry.ModTime = DebugLineData.getULEB128(OffsetPtr);
627 FileEntry.Length = DebugLineData.getULEB128(OffsetPtr);
628 Prologue.FileNames.push_back(FileEntry);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000629 if (OS)
Paul Robinson0a227092018-02-05 20:43:15 +0000630 *OS << " (" << Name << ", dir=" << FileEntry.DirIdx << ", mod_time="
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000631 << format("(0x%16.16" PRIx64 ")", FileEntry.ModTime)
632 << ", length=" << FileEntry.Length << ")";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000633 }
634 break;
635
Diego Novillo5b5cf502014-02-14 19:27:53 +0000636 case DW_LNE_set_discriminator:
Paul Robinson9d4eb692017-05-01 23:27:55 +0000637 State.Row.Discriminator = DebugLineData.getULEB128(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000638 if (OS)
639 *OS << " (" << State.Row.Discriminator << ")";
Diego Novillo5b5cf502014-02-14 19:27:53 +0000640 break;
641
Benjamin Kramer5acab502011-09-15 02:12:05 +0000642 default:
Paul Robinsone0833342017-11-22 15:14:49 +0000643 if (OS)
644 *OS << format("Unrecognized extended op 0x%02.02" PRIx8, SubOpcode)
645 << format(" length %" PRIx64, Len);
646 // Len doesn't include the zero opcode byte or the length itself, but
647 // it does include the sub_opcode, so we have to adjust for that.
648 (*OffsetPtr) += Len - 1;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000649 break;
650 }
Paul Robinsone0833342017-11-22 15:14:49 +0000651 // Make sure the stated and parsed lengths are the same.
652 // Otherwise we have an unparseable line-number program.
James Hendersona3acf992018-05-10 10:51:33 +0000653 if (*OffsetPtr - ExtOffset != Len)
Victor Leschukcba595d2018-08-20 09:59:08 +0000654 return createStringError(errc::illegal_byte_sequence,
655 "unexpected line op length at offset 0x%8.8" PRIx32
James Hendersona3acf992018-05-10 10:51:33 +0000656 " expected 0x%2.2" PRIx64 " found 0x%2.2" PRIx32,
657 ExtOffset, Len, *OffsetPtr - ExtOffset);
Paul Robinson9d4eb692017-05-01 23:27:55 +0000658 } else if (Opcode < Prologue.OpcodeBase) {
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000659 if (OS)
660 *OS << LNStandardString(Opcode);
Paul Robinson9d4eb692017-05-01 23:27:55 +0000661 switch (Opcode) {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000662 // Standard Opcodes
663 case DW_LNS_copy:
664 // Takes no arguments. Append a row to the matrix using the
Fangrui Song6a285df2019-04-11 02:02:44 +0000665 // current values of the state-machine registers.
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000666 if (OS) {
667 *OS << "\n";
668 OS->indent(12);
669 State.Row.dump(*OS);
670 *OS << "\n";
671 }
Fangrui Song6a285df2019-04-11 02:02:44 +0000672 State.appendRowToMatrix();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000673 break;
674
675 case DW_LNS_advance_pc:
676 // Takes a single unsigned LEB128 operand, multiplies it by the
677 // min_inst_length field of the prologue, and adds the
678 // result to the address register of the state machine.
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000679 {
680 uint64_t AddrOffset =
681 DebugLineData.getULEB128(OffsetPtr) * Prologue.MinInstLength;
Alexey Lapshin77fc1f62019-02-27 13:17:36 +0000682 State.Row.Address.Address += AddrOffset;
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000683 if (OS)
684 *OS << " (" << AddrOffset << ")";
685 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000686 break;
687
688 case DW_LNS_advance_line:
689 // Takes a single signed LEB128 operand and adds that value to
690 // the line register of the state machine.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000691 State.Row.Line += DebugLineData.getSLEB128(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000692 if (OS)
693 *OS << " (" << State.Row.Line << ")";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000694 break;
695
696 case DW_LNS_set_file:
697 // Takes a single unsigned LEB128 operand and stores it in the file
698 // register of the state machine.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000699 State.Row.File = DebugLineData.getULEB128(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000700 if (OS)
701 *OS << " (" << State.Row.File << ")";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000702 break;
703
704 case DW_LNS_set_column:
705 // Takes a single unsigned LEB128 operand and stores it in the
706 // column register of the state machine.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000707 State.Row.Column = DebugLineData.getULEB128(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000708 if (OS)
709 *OS << " (" << State.Row.Column << ")";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000710 break;
711
712 case DW_LNS_negate_stmt:
713 // Takes no arguments. Set the is_stmt register of the state
714 // machine to the logical negation of its current value.
Alexey Samsonov110d5952014-04-30 00:09:19 +0000715 State.Row.IsStmt = !State.Row.IsStmt;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000716 break;
717
718 case DW_LNS_set_basic_block:
719 // Takes no arguments. Set the basic_block register of the
720 // state machine to true
Alexey Samsonov110d5952014-04-30 00:09:19 +0000721 State.Row.BasicBlock = true;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000722 break;
723
724 case DW_LNS_const_add_pc:
725 // Takes no arguments. Add to the address register of the state
726 // machine the address increment value corresponding to special
727 // opcode 255. The motivation for DW_LNS_const_add_pc is this:
728 // when the statement program needs to advance the address by a
729 // small amount, it can use a single special opcode, which occupies
730 // a single byte. When it needs to advance the address by up to
731 // twice the range of the last special opcode, it can use
732 // DW_LNS_const_add_pc followed by a special opcode, for a total
733 // of two bytes. Only if it needs to advance the address by more
734 // than twice that range will it need to use both DW_LNS_advance_pc
735 // and a special opcode, requiring three or more bytes.
736 {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000737 uint8_t AdjustOpcode = 255 - Prologue.OpcodeBase;
738 uint64_t AddrOffset =
739 (AdjustOpcode / Prologue.LineRange) * Prologue.MinInstLength;
Alexey Lapshin77fc1f62019-02-27 13:17:36 +0000740 State.Row.Address.Address += AddrOffset;
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000741 if (OS)
742 *OS
743 << format(" (0x%16.16" PRIx64 ")", AddrOffset);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000744 }
745 break;
746
747 case DW_LNS_fixed_advance_pc:
748 // Takes a single uhalf operand. Add to the address register of
749 // the state machine the value of the (unencoded) operand. This
750 // is the only extended opcode that takes an argument that is not
751 // a variable length number. The motivation for DW_LNS_fixed_advance_pc
752 // is this: existing assemblers cannot emit DW_LNS_advance_pc or
753 // special opcodes because they cannot encode LEB128 numbers or
754 // judge when the computation of a special opcode overflows and
755 // requires the use of DW_LNS_advance_pc. Such assemblers, however,
756 // can use DW_LNS_fixed_advance_pc instead, sacrificing compression.
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000757 {
Alex Bradbury44deaf72019-07-18 05:22:55 +0000758 uint16_t PCOffset = DebugLineData.getRelocatedValue(2, OffsetPtr);
Alexey Lapshin77fc1f62019-02-27 13:17:36 +0000759 State.Row.Address.Address += PCOffset;
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000760 if (OS)
761 *OS
Igor Kudrin74c350a2019-07-16 06:56:10 +0000762 << format(" (0x%4.4" PRIx16 ")", PCOffset);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000763 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000764 break;
765
766 case DW_LNS_set_prologue_end:
767 // Takes no arguments. Set the prologue_end register of the
768 // state machine to true
Alexey Samsonov110d5952014-04-30 00:09:19 +0000769 State.Row.PrologueEnd = true;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000770 break;
771
772 case DW_LNS_set_epilogue_begin:
773 // Takes no arguments. Set the basic_block register of the
774 // state machine to true
Alexey Samsonov110d5952014-04-30 00:09:19 +0000775 State.Row.EpilogueBegin = true;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000776 break;
777
778 case DW_LNS_set_isa:
779 // Takes a single unsigned LEB128 operand and stores it in the
780 // column register of the state machine.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000781 State.Row.Isa = DebugLineData.getULEB128(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000782 if (OS)
783 *OS << " (" << State.Row.Isa << ")";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000784 break;
785
786 default:
787 // Handle any unknown standard opcodes here. We know the lengths
788 // of such opcodes because they are specified in the prologue
789 // as a multiple of LEB128 operands for each opcode.
790 {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000791 assert(Opcode - 1U < Prologue.StandardOpcodeLengths.size());
792 uint8_t OpcodeLength = Prologue.StandardOpcodeLengths[Opcode - 1];
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000793 for (uint8_t I = 0; I < OpcodeLength; ++I) {
794 uint64_t Value = DebugLineData.getULEB128(OffsetPtr);
795 if (OS)
796 *OS << format("Skipping ULEB128 value: 0x%16.16" PRIx64 ")\n",
797 Value);
798 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000799 }
800 break;
801 }
802 } else {
803 // Special Opcodes
804
805 // A special opcode value is chosen based on the amount that needs
806 // to be added to the line and address registers. The maximum line
807 // increment for a special opcode is the value of the line_base
808 // field in the header, plus the value of the line_range field,
809 // minus 1 (line base + line range - 1). If the desired line
810 // increment is greater than the maximum line increment, a standard
NAKAMURA Takumif9959852011-10-08 11:22:47 +0000811 // opcode must be used instead of a special opcode. The "address
812 // advance" is calculated by dividing the desired address increment
Benjamin Kramer5acab502011-09-15 02:12:05 +0000813 // by the minimum_instruction_length field from the header. The
814 // special opcode is then calculated using the following formula:
815 //
816 // opcode = (desired line increment - line_base) +
817 // (line_range * address advance) + opcode_base
818 //
819 // If the resulting opcode is greater than 255, a standard opcode
820 // must be used instead.
821 //
822 // To decode a special opcode, subtract the opcode_base from the
823 // opcode itself to give the adjusted opcode. The amount to
824 // increment the address register is the result of the adjusted
825 // opcode divided by the line_range multiplied by the
826 // minimum_instruction_length field from the header. That is:
827 //
828 // address increment = (adjusted opcode / line_range) *
829 // minimum_instruction_length
830 //
831 // The amount to increment the line register is the line_base plus
832 // the result of the adjusted opcode modulo the line_range. That is:
833 //
834 // line increment = line_base + (adjusted opcode % line_range)
835
Paul Robinson9d4eb692017-05-01 23:27:55 +0000836 uint8_t AdjustOpcode = Opcode - Prologue.OpcodeBase;
837 uint64_t AddrOffset =
838 (AdjustOpcode / Prologue.LineRange) * Prologue.MinInstLength;
839 int32_t LineOffset =
840 Prologue.LineBase + (AdjustOpcode % Prologue.LineRange);
841 State.Row.Line += LineOffset;
Alexey Lapshin77fc1f62019-02-27 13:17:36 +0000842 State.Row.Address.Address += AddrOffset;
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000843
844 if (OS) {
James Hendersonb6b5b1a2019-02-06 10:31:50 +0000845 *OS << "address += " << AddrOffset << ", line += " << LineOffset
846 << "\n";
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000847 OS->indent(12);
848 State.Row.dump(*OS);
849 }
850
Fangrui Songc4c8bca2019-04-07 13:56:14 +0000851 State.appendRowToMatrix();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000852 }
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000853 if(OS)
854 *OS << "\n";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000855 }
856
Jonas Devlieghere84e99262018-04-14 22:07:23 +0000857 if (!State.Sequence.Empty)
James Henderson004b7292018-05-21 15:30:54 +0000858 RecoverableErrorCallback(
Victor Leschukcba595d2018-08-20 09:59:08 +0000859 createStringError(errc::illegal_byte_sequence,
860 "last sequence in debug line table is not terminated!"));
Alexey Samsonov110d5952014-04-30 00:09:19 +0000861
862 // Sort all sequences so that address lookup will work faster.
863 if (!Sequences.empty()) {
Fangrui Song9b22c462019-04-09 15:08:32 +0000864 llvm::sort(Sequences, Sequence::orderByHighPC);
Alexey Samsonov110d5952014-04-30 00:09:19 +0000865 // Note: actually, instruction address ranges of sequences should not
866 // overlap (in shared objects and executables). If they do, the address
867 // lookup would still work, though, but result would be ambiguous.
868 // We don't report warning in this case. For example,
869 // sometimes .so compiled from multiple object files contains a few
870 // rudimentary sequences for address ranges [0x0, 0xsomething).
871 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000872
James Hendersona3acf992018-05-10 10:51:33 +0000873 return Error::success();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000874}
875
Alexey Lapshin77fc1f62019-02-27 13:17:36 +0000876uint32_t DWARFDebugLine::LineTable::findRowInSeq(
877 const DWARFDebugLine::Sequence &Seq,
878 object::SectionedAddress Address) const {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000879 if (!Seq.containsPC(Address))
Keno Fischerc2c60182015-05-31 23:37:04 +0000880 return UnknownRowIndex;
Alexey Lapshin77fc1f62019-02-27 13:17:36 +0000881 assert(Seq.SectionIndex == Address.SectionIndex);
Fangrui Songb3be23d2019-04-10 07:44:23 +0000882 // In some cases, e.g. first instruction in a function, the compiler generates
883 // two entries, both with the same address. We want the last one.
884 //
885 // In general we want a non-empty range: the last row whose address is less
886 // than or equal to Address. This can be computed as upper_bound - 1.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000887 DWARFDebugLine::Row Row;
888 Row.Address = Address;
889 RowIter FirstRow = Rows.begin() + Seq.FirstRowIndex;
890 RowIter LastRow = Rows.begin() + Seq.LastRowIndex;
Fangrui Songb3be23d2019-04-10 07:44:23 +0000891 assert(FirstRow->Address.Address <= Row.Address.Address &&
892 Row.Address.Address < LastRow[-1].Address.Address);
893 RowIter RowPos = std::upper_bound(FirstRow + 1, LastRow - 1, Row,
894 DWARFDebugLine::Row::orderByAddress) -
895 1;
Alexey Lapshin77fc1f62019-02-27 13:17:36 +0000896 assert(Seq.SectionIndex == RowPos->Address.SectionIndex);
Fangrui Songb3be23d2019-04-10 07:44:23 +0000897 return RowPos - Rows.begin();
Keno Fischerc2c60182015-05-31 23:37:04 +0000898}
899
Alexey Lapshin77fc1f62019-02-27 13:17:36 +0000900uint32_t DWARFDebugLine::LineTable::lookupAddress(
901 object::SectionedAddress Address) const {
902
903 // Search for relocatable addresses
904 uint32_t Result = lookupAddressImpl(Address);
905
906 if (Result != UnknownRowIndex ||
907 Address.SectionIndex == object::SectionedAddress::UndefSection)
908 return Result;
909
910 // Search for absolute addresses
911 Address.SectionIndex = object::SectionedAddress::UndefSection;
912 return lookupAddressImpl(Address);
913}
914
915uint32_t DWARFDebugLine::LineTable::lookupAddressImpl(
916 object::SectionedAddress Address) const {
Alexey Samsonov947228c2012-08-07 11:46:57 +0000917 // First, find an instruction sequence containing the given address.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000918 DWARFDebugLine::Sequence Sequence;
Alexey Lapshin77fc1f62019-02-27 13:17:36 +0000919 Sequence.SectionIndex = Address.SectionIndex;
Fangrui Song9b22c462019-04-09 15:08:32 +0000920 Sequence.HighPC = Address.Address;
921 SequenceIter It = llvm::upper_bound(Sequences, Sequence,
922 DWARFDebugLine::Sequence::orderByHighPC);
923 if (It == Sequences.end() || It->SectionIndex != Address.SectionIndex)
Alexey Lapshin77fc1f62019-02-27 13:17:36 +0000924 return UnknownRowIndex;
Fangrui Song9b22c462019-04-09 15:08:32 +0000925 return findRowInSeq(*It, Address);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000926}
Alexey Samsonov45be7932012-08-30 07:49:50 +0000927
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000928bool DWARFDebugLine::LineTable::lookupAddressRange(
Alexey Lapshin77fc1f62019-02-27 13:17:36 +0000929 object::SectionedAddress Address, uint64_t Size,
930 std::vector<uint32_t> &Result) const {
931
932 // Search for relocatable addresses
933 if (lookupAddressRangeImpl(Address, Size, Result))
934 return true;
935
936 if (Address.SectionIndex == object::SectionedAddress::UndefSection)
937 return false;
938
939 // Search for absolute addresses
940 Address.SectionIndex = object::SectionedAddress::UndefSection;
941 return lookupAddressRangeImpl(Address, Size, Result);
942}
943
944bool DWARFDebugLine::LineTable::lookupAddressRangeImpl(
945 object::SectionedAddress Address, uint64_t Size,
946 std::vector<uint32_t> &Result) const {
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000947 if (Sequences.empty())
948 return false;
Alexey Lapshin77fc1f62019-02-27 13:17:36 +0000949 uint64_t EndAddr = Address.Address + Size;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000950 // First, find an instruction sequence containing the given address.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000951 DWARFDebugLine::Sequence Sequence;
Alexey Lapshin77fc1f62019-02-27 13:17:36 +0000952 Sequence.SectionIndex = Address.SectionIndex;
Fangrui Song9b22c462019-04-09 15:08:32 +0000953 Sequence.HighPC = Address.Address;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000954 SequenceIter LastSeq = Sequences.end();
Fangrui Song9b22c462019-04-09 15:08:32 +0000955 SequenceIter SeqPos = llvm::upper_bound(
956 Sequences, Sequence, DWARFDebugLine::Sequence::orderByHighPC);
957 if (SeqPos == LastSeq || !SeqPos->containsPC(Address))
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000958 return false;
959
Paul Robinson9d4eb692017-05-01 23:27:55 +0000960 SequenceIter StartPos = SeqPos;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000961
962 // Add the rows from the first sequence to the vector, starting with the
963 // index we just calculated
964
Paul Robinson9d4eb692017-05-01 23:27:55 +0000965 while (SeqPos != LastSeq && SeqPos->LowPC < EndAddr) {
966 const DWARFDebugLine::Sequence &CurSeq = *SeqPos;
Keno Fischerc2c60182015-05-31 23:37:04 +0000967 // For the first sequence, we need to find which row in the sequence is the
968 // first in our range.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000969 uint32_t FirstRowIndex = CurSeq.FirstRowIndex;
970 if (SeqPos == StartPos)
971 FirstRowIndex = findRowInSeq(CurSeq, Address);
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000972
Keno Fischerc2c60182015-05-31 23:37:04 +0000973 // Figure out the last row in the range.
Alexey Lapshin77fc1f62019-02-27 13:17:36 +0000974 uint32_t LastRowIndex =
975 findRowInSeq(CurSeq, {EndAddr - 1, Address.SectionIndex});
Paul Robinson9d4eb692017-05-01 23:27:55 +0000976 if (LastRowIndex == UnknownRowIndex)
977 LastRowIndex = CurSeq.LastRowIndex - 1;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000978
Paul Robinson9d4eb692017-05-01 23:27:55 +0000979 assert(FirstRowIndex != UnknownRowIndex);
980 assert(LastRowIndex != UnknownRowIndex);
Keno Fischerc2c60182015-05-31 23:37:04 +0000981
Paul Robinson9d4eb692017-05-01 23:27:55 +0000982 for (uint32_t I = FirstRowIndex; I <= LastRowIndex; ++I) {
983 Result.push_back(I);
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000984 }
985
Paul Robinson9d4eb692017-05-01 23:27:55 +0000986 ++SeqPos;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000987 }
NAKAMURA Takumi4b86cdb2013-01-26 01:45:06 +0000988
989 return true;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000990}
991
Scott Linder16c7bda2018-02-23 23:01:06 +0000992Optional<StringRef> DWARFDebugLine::LineTable::getSourceByIndex(uint64_t FileIndex,
993 FileLineInfoKind Kind) const {
Jonas Devlieghereca16d282019-07-16 01:21:25 +0000994 if (Kind == FileLineInfoKind::None || !Prologue.hasFileAtIndex(FileIndex))
Scott Linder16c7bda2018-02-23 23:01:06 +0000995 return None;
Jonas Devlieghereca16d282019-07-16 01:21:25 +0000996 const FileNameEntry &Entry = Prologue.getFileNameEntry(FileIndex);
Scott Linder16c7bda2018-02-23 23:01:06 +0000997 if (Optional<const char *> source = Entry.Source.getAsCString())
998 return StringRef(*source);
999 return None;
1000}
1001
Eugene Zemtsov82d60d62018-03-13 17:54:29 +00001002static bool isPathAbsoluteOnWindowsOrPosix(const Twine &Path) {
1003 // Debug info can contain paths from any OS, not necessarily
1004 // an OS we're currently running on. Moreover different compilation units can
1005 // be compiled on different operating systems and linked together later.
1006 return sys::path::is_absolute(Path, sys::path::Style::posix) ||
1007 sys::path::is_absolute(Path, sys::path::Style::windows);
1008}
1009
Jonas Devlieghereca16d282019-07-16 01:21:25 +00001010bool DWARFDebugLine::Prologue::getFileNameByIndex(uint64_t FileIndex,
1011 StringRef CompDir,
1012 FileLineInfoKind Kind,
1013 std::string &Result) const {
Pete Cooperb2ba7762016-07-22 01:41:32 +00001014 if (Kind == FileLineInfoKind::None || !hasFileAtIndex(FileIndex))
Alexey Samsonov45be7932012-08-30 07:49:50 +00001015 return false;
Ali Tamur783d84b2019-04-19 02:26:56 +00001016 const FileNameEntry &Entry = getFileNameEntry(FileIndex);
Paul Robinson0a227092018-02-05 20:43:15 +00001017 StringRef FileName = Entry.Name.getAsCString().getValue();
Alexey Samsonovdce67342014-05-15 21:24:32 +00001018 if (Kind != FileLineInfoKind::AbsoluteFilePath ||
Eugene Zemtsov82d60d62018-03-13 17:54:29 +00001019 isPathAbsoluteOnWindowsOrPosix(FileName)) {
Alexey Samsonov45be7932012-08-30 07:49:50 +00001020 Result = FileName;
1021 return true;
1022 }
Frederic Riss101b5e22014-09-19 15:11:51 +00001023
Alexey Samsonov45be7932012-08-30 07:49:50 +00001024 SmallString<16> FilePath;
Paul Robinsonba1c9152017-05-02 17:37:32 +00001025 StringRef IncludeDir;
Alexey Samsonov45be7932012-08-30 07:49:50 +00001026 // Be defensive about the contents of Entry.
Jonas Devlieghereca16d282019-07-16 01:21:25 +00001027 if (getVersion() >= 5) {
1028 if (Entry.DirIdx < IncludeDirectories.size())
1029 IncludeDir = IncludeDirectories[Entry.DirIdx].getAsCString().getValue();
Jonas Devlieghere01ee1722019-07-16 00:59:04 +00001030 } else {
Jonas Devlieghereca16d282019-07-16 01:21:25 +00001031 if (0 < Entry.DirIdx && Entry.DirIdx <= IncludeDirectories.size())
1032 IncludeDir =
1033 IncludeDirectories[Entry.DirIdx - 1].getAsCString().getValue();
Frederic Riss101b5e22014-09-19 15:11:51 +00001034
Fangrui Song7e556722019-05-06 08:03:46 +00001035 // We may still need to append compilation directory of compile unit.
1036 // We know that FileName is not absolute, the only way to have an
1037 // absolute path at this point would be if IncludeDir is absolute.
Jonas Devlieghereca16d282019-07-16 01:21:25 +00001038 if (!CompDir.empty() && !isPathAbsoluteOnWindowsOrPosix(IncludeDir))
Fangrui Song7e556722019-05-06 08:03:46 +00001039 sys::path::append(FilePath, CompDir);
1040 }
Frederic Riss101b5e22014-09-19 15:11:51 +00001041
1042 // sys::path::append skips empty strings.
1043 sys::path::append(FilePath, IncludeDir, FileName);
Alexey Samsonov45be7932012-08-30 07:49:50 +00001044 Result = FilePath.str();
1045 return true;
1046}
Frederic Riss101b5e22014-09-19 15:11:51 +00001047
Dehao Chen1b54fce2016-04-28 22:09:37 +00001048bool DWARFDebugLine::LineTable::getFileLineInfoForAddress(
Alexey Lapshin77fc1f62019-02-27 13:17:36 +00001049 object::SectionedAddress Address, const char *CompDir,
1050 FileLineInfoKind Kind, DILineInfo &Result) const {
Frederic Riss101b5e22014-09-19 15:11:51 +00001051 // Get the index of row we're looking for in the line table.
1052 uint32_t RowIndex = lookupAddress(Address);
1053 if (RowIndex == -1U)
1054 return false;
1055 // Take file number and line/column from the row.
1056 const auto &Row = Rows[RowIndex];
1057 if (!getFileNameByIndex(Row.File, CompDir, Kind, Result.FileName))
1058 return false;
1059 Result.Line = Row.Line;
1060 Result.Column = Row.Column;
Eric Christopherba1024c2016-12-14 18:29:39 +00001061 Result.Discriminator = Row.Discriminator;
Scott Linder16c7bda2018-02-23 23:01:06 +00001062 Result.Source = getSourceByIndex(Row.File, Kind);
Frederic Riss101b5e22014-09-19 15:11:51 +00001063 return true;
1064}
James Hendersona3acf992018-05-10 10:51:33 +00001065
1066// We want to supply the Unit associated with a .debug_line[.dwo] table when
1067// we dump it, if possible, but still dump the table even if there isn't a Unit.
1068// Therefore, collect up handles on all the Units that point into the
1069// line-table section.
1070static DWARFDebugLine::SectionParser::LineToUnitMap
1071buildLineToUnitMap(DWARFDebugLine::SectionParser::cu_range CUs,
Paul Robinson7f330942018-08-01 20:46:46 +00001072 DWARFDebugLine::SectionParser::tu_range TUs) {
James Hendersona3acf992018-05-10 10:51:33 +00001073 DWARFDebugLine::SectionParser::LineToUnitMap LineToUnit;
1074 for (const auto &CU : CUs)
1075 if (auto CUDIE = CU->getUnitDIE())
1076 if (auto StmtOffset = toSectionOffset(CUDIE.find(DW_AT_stmt_list)))
1077 LineToUnit.insert(std::make_pair(*StmtOffset, &*CU));
Paul Robinson7f330942018-08-01 20:46:46 +00001078 for (const auto &TU : TUs)
1079 if (auto TUDIE = TU->getUnitDIE())
1080 if (auto StmtOffset = toSectionOffset(TUDIE.find(DW_AT_stmt_list)))
1081 LineToUnit.insert(std::make_pair(*StmtOffset, &*TU));
James Hendersona3acf992018-05-10 10:51:33 +00001082 return LineToUnit;
1083}
1084
1085DWARFDebugLine::SectionParser::SectionParser(DWARFDataExtractor &Data,
1086 const DWARFContext &C,
1087 cu_range CUs, tu_range TUs)
1088 : DebugLineData(Data), Context(C) {
1089 LineToUnit = buildLineToUnitMap(CUs, TUs);
1090 if (!DebugLineData.isValidOffset(Offset))
1091 Done = true;
1092}
1093
1094bool DWARFDebugLine::Prologue::totalLengthIsValid() const {
Igor Kudrinf48bc012019-07-16 07:01:08 +00001095 return TotalLength == 0xffffffff || TotalLength < 0xfffffff0;
James Hendersona3acf992018-05-10 10:51:33 +00001096}
1097
1098DWARFDebugLine::LineTable DWARFDebugLine::SectionParser::parseNext(
James Henderson004b7292018-05-21 15:30:54 +00001099 function_ref<void(Error)> RecoverableErrorCallback,
1100 function_ref<void(Error)> UnrecoverableErrorCallback, raw_ostream *OS) {
James Hendersona3acf992018-05-10 10:51:33 +00001101 assert(DebugLineData.isValidOffset(Offset) &&
1102 "parsing should have terminated");
1103 DWARFUnit *U = prepareToParse(Offset);
1104 uint32_t OldOffset = Offset;
1105 LineTable LT;
James Henderson004b7292018-05-21 15:30:54 +00001106 if (Error Err = LT.parse(DebugLineData, &Offset, Context, U,
1107 RecoverableErrorCallback, OS))
1108 UnrecoverableErrorCallback(std::move(Err));
James Hendersona3acf992018-05-10 10:51:33 +00001109 moveToNextTable(OldOffset, LT.Prologue);
1110 return LT;
1111}
1112
1113void DWARFDebugLine::SectionParser::skip(
1114 function_ref<void(Error)> ErrorCallback) {
1115 assert(DebugLineData.isValidOffset(Offset) &&
1116 "parsing should have terminated");
1117 DWARFUnit *U = prepareToParse(Offset);
1118 uint32_t OldOffset = Offset;
1119 LineTable LT;
James Henderson004b7292018-05-21 15:30:54 +00001120 if (Error Err = LT.Prologue.parse(DebugLineData, &Offset, Context, U))
1121 ErrorCallback(std::move(Err));
James Hendersona3acf992018-05-10 10:51:33 +00001122 moveToNextTable(OldOffset, LT.Prologue);
1123}
1124
1125DWARFUnit *DWARFDebugLine::SectionParser::prepareToParse(uint32_t Offset) {
1126 DWARFUnit *U = nullptr;
1127 auto It = LineToUnit.find(Offset);
1128 if (It != LineToUnit.end())
1129 U = It->second;
1130 DebugLineData.setAddressSize(U ? U->getAddressByteSize() : 0);
1131 return U;
1132}
1133
1134void DWARFDebugLine::SectionParser::moveToNextTable(uint32_t OldOffset,
1135 const Prologue &P) {
1136 // If the length field is not valid, we don't know where the next table is, so
1137 // cannot continue to parse. Mark the parser as done, and leave the Offset
1138 // value as it currently is. This will be the end of the bad length field.
1139 if (!P.totalLengthIsValid()) {
1140 Done = true;
1141 return;
1142 }
1143
1144 Offset = OldOffset + P.TotalLength + P.sizeofTotalLength();
1145 if (!DebugLineData.isValidOffset(Offset)) {
1146 Done = true;
1147 }
1148}