blob: 1f18973f1d48c3c871d87b34f9c6befc05fc43aa [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
69void DWARFDebugLine::Prologue::clear() {
Paul Robinson75c068c2017-06-26 18:43:01 +000070 TotalLength = PrologueLength = 0;
71 SegSelectorSize = 0;
Alexey Samsonov836b1ae2014-04-29 21:28:13 +000072 MinInstLength = MaxOpsPerInst = DefaultIsStmt = LineBase = LineRange = 0;
73 OpcodeBase = 0;
Pavel Labath322711f2018-03-14 09:39:54 +000074 FormParams = dwarf::FormParams({0, 0, DWARF32});
Scott Linder16c7bda2018-02-23 23:01:06 +000075 ContentTypes = ContentTypeTracker();
Alexey Samsonov836b1ae2014-04-29 21:28:13 +000076 StandardOpcodeLengths.clear();
77 IncludeDirectories.clear();
78 FileNames.clear();
79}
80
Paul Robinson0a227092018-02-05 20:43:15 +000081void DWARFDebugLine::Prologue::dump(raw_ostream &OS,
82 DIDumpOptions DumpOptions) const {
Benjamin Kramer5acab502011-09-15 02:12:05 +000083 OS << "Line table prologue:\n"
Ed Maste6d0bee52015-05-28 15:38:17 +000084 << format(" total_length: 0x%8.8" PRIx64 "\n", TotalLength)
Paul Robinson75c068c2017-06-26 18:43:01 +000085 << format(" version: %u\n", getVersion());
86 if (getVersion() >= 5)
87 OS << format(" address_size: %u\n", getAddressSize())
88 << format(" seg_select_size: %u\n", SegSelectorSize);
89 OS << format(" prologue_length: 0x%8.8" PRIx64 "\n", PrologueLength)
David Blaikie1d4736e2014-02-24 23:58:54 +000090 << format(" min_inst_length: %u\n", MinInstLength)
Paul Robinson75c068c2017-06-26 18:43:01 +000091 << format(getVersion() >= 4 ? "max_ops_per_inst: %u\n" : "", MaxOpsPerInst)
David Blaikie1d4736e2014-02-24 23:58:54 +000092 << format(" default_is_stmt: %u\n", DefaultIsStmt)
93 << format(" line_base: %i\n", LineBase)
94 << format(" line_range: %u\n", LineRange)
95 << format(" opcode_base: %u\n", OpcodeBase);
Benjamin Kramer5acab502011-09-15 02:12:05 +000096
Paul Robinson9d4eb692017-05-01 23:27:55 +000097 for (uint32_t I = 0; I != StandardOpcodeLengths.size(); ++I)
Mehdi Amini149f6ea2016-10-05 05:59:29 +000098 OS << format("standard_opcode_lengths[%s] = %u\n",
Paul Robinson9d4eb692017-05-01 23:27:55 +000099 LNStandardString(I + 1).data(), StandardOpcodeLengths[I]);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000100
Paul Robinson8181d232018-01-18 20:33:35 +0000101 if (!IncludeDirectories.empty()) {
102 // DWARF v5 starts directory indexes at 0.
103 uint32_t DirBase = getVersion() >= 5 ? 0 : 1;
Paul Robinson0a227092018-02-05 20:43:15 +0000104 for (uint32_t I = 0; I != IncludeDirectories.size(); ++I) {
105 OS << format("include_directories[%3u] = ", I + DirBase);
106 IncludeDirectories[I].dump(OS, DumpOptions);
107 OS << '\n';
108 }
Paul Robinson8181d232018-01-18 20:33:35 +0000109 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000110
111 if (!FileNames.empty()) {
Paul Robinsonceafcd42018-02-08 23:08:02 +0000112 // DWARF v5 starts file indexes at 0.
113 uint32_t FileBase = getVersion() >= 5 ? 0 : 1;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000114 for (uint32_t I = 0; I != FileNames.size(); ++I) {
115 const FileNameEntry &FileEntry = FileNames[I];
Scott Linder16c7bda2018-02-23 23:01:06 +0000116 OS << format("file_names[%3u]:\n", I + FileBase);
117 OS << " name: ";
Paul Robinson0a227092018-02-05 20:43:15 +0000118 FileEntry.Name.dump(OS, DumpOptions);
Scott Linder16c7bda2018-02-23 23:01:06 +0000119 OS << '\n'
120 << format(" dir_index: %" PRIu64 "\n", FileEntry.DirIdx);
121 if (ContentTypes.HasMD5)
122 OS << " md5_checksum: " << FileEntry.Checksum.digest() << '\n';
123 if (ContentTypes.HasModTime)
124 OS << format(" mod_time: 0x%8.8" PRIx64 "\n", FileEntry.ModTime);
125 if (ContentTypes.HasLength)
126 OS << format(" length: 0x%8.8" PRIx64 "\n", FileEntry.Length);
127 if (ContentTypes.HasSource) {
128 OS << " source: ";
129 FileEntry.Source.dump(OS, DumpOptions);
130 OS << '\n';
131 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000132 }
133 }
134}
135
Paul Robinson2bc38732017-05-02 21:40:47 +0000136// Parse v2-v4 directory and file tables.
137static void
Paul Robinson17536b92017-06-29 16:52:08 +0000138parseV2DirFileTables(const DWARFDataExtractor &DebugLineData,
139 uint32_t *OffsetPtr, uint64_t EndPrologueOffset,
Scott Linder16c7bda2018-02-23 23:01:06 +0000140 DWARFDebugLine::ContentTypeTracker &ContentTypes,
Paul Robinson0a227092018-02-05 20:43:15 +0000141 std::vector<DWARFFormValue> &IncludeDirectories,
Paul Robinson2bc38732017-05-02 21:40:47 +0000142 std::vector<DWARFDebugLine::FileNameEntry> &FileNames) {
143 while (*OffsetPtr < EndPrologueOffset) {
144 StringRef S = DebugLineData.getCStrRef(OffsetPtr);
145 if (S.empty())
146 break;
Jonas Devliegherebb111152019-02-27 00:58:09 +0000147 DWARFFormValue Dir =
148 DWARFFormValue::createFromPValue(dwarf::DW_FORM_string, S.data());
Paul Robinson0a227092018-02-05 20:43:15 +0000149 IncludeDirectories.push_back(Dir);
Paul Robinson2bc38732017-05-02 21:40:47 +0000150 }
151
152 while (*OffsetPtr < EndPrologueOffset) {
153 StringRef Name = DebugLineData.getCStrRef(OffsetPtr);
154 if (Name.empty())
155 break;
156 DWARFDebugLine::FileNameEntry FileEntry;
Jonas Devliegherebb111152019-02-27 00:58:09 +0000157 FileEntry.Name =
158 DWARFFormValue::createFromPValue(dwarf::DW_FORM_string, Name.data());
Paul Robinson2bc38732017-05-02 21:40:47 +0000159 FileEntry.DirIdx = DebugLineData.getULEB128(OffsetPtr);
160 FileEntry.ModTime = DebugLineData.getULEB128(OffsetPtr);
161 FileEntry.Length = DebugLineData.getULEB128(OffsetPtr);
162 FileNames.push_back(FileEntry);
163 }
Scott Linder16c7bda2018-02-23 23:01:06 +0000164
165 ContentTypes.HasModTime = true;
166 ContentTypes.HasLength = true;
Paul Robinson2bc38732017-05-02 21:40:47 +0000167}
168
169// Parse v5 directory/file entry content descriptions.
170// Returns the descriptors, or an empty vector if we did not find a path or
171// ran off the end of the prologue.
172static ContentDescriptors
Scott Linder16c7bda2018-02-23 23:01:06 +0000173parseV5EntryFormat(const DWARFDataExtractor &DebugLineData, uint32_t
174 *OffsetPtr, uint64_t EndPrologueOffset, DWARFDebugLine::ContentTypeTracker
175 *ContentTypes) {
Paul Robinson2bc38732017-05-02 21:40:47 +0000176 ContentDescriptors Descriptors;
177 int FormatCount = DebugLineData.getU8(OffsetPtr);
178 bool HasPath = false;
179 for (int I = 0; I != FormatCount; ++I) {
180 if (*OffsetPtr >= EndPrologueOffset)
181 return ContentDescriptors();
182 ContentDescriptor Descriptor;
183 Descriptor.Type =
184 dwarf::LineNumberEntryFormat(DebugLineData.getULEB128(OffsetPtr));
185 Descriptor.Form = dwarf::Form(DebugLineData.getULEB128(OffsetPtr));
186 if (Descriptor.Type == dwarf::DW_LNCT_path)
187 HasPath = true;
Scott Linder16c7bda2018-02-23 23:01:06 +0000188 if (ContentTypes)
189 ContentTypes->trackContentType(Descriptor.Type);
Paul Robinson2bc38732017-05-02 21:40:47 +0000190 Descriptors.push_back(Descriptor);
191 }
192 return HasPath ? Descriptors : ContentDescriptors();
193}
194
195static bool
Paul Robinson17536b92017-06-29 16:52:08 +0000196parseV5DirFileTables(const DWARFDataExtractor &DebugLineData,
197 uint32_t *OffsetPtr, uint64_t EndPrologueOffset,
Pavel Labath322711f2018-03-14 09:39:54 +0000198 const dwarf::FormParams &FormParams,
199 const DWARFContext &Ctx, const DWARFUnit *U,
Scott Linder16c7bda2018-02-23 23:01:06 +0000200 DWARFDebugLine::ContentTypeTracker &ContentTypes,
Paul Robinson0a227092018-02-05 20:43:15 +0000201 std::vector<DWARFFormValue> &IncludeDirectories,
Paul Robinson2bc38732017-05-02 21:40:47 +0000202 std::vector<DWARFDebugLine::FileNameEntry> &FileNames) {
203 // Get the directory entry description.
204 ContentDescriptors DirDescriptors =
Paul Robinsona06f8dc2017-12-18 19:08:35 +0000205 parseV5EntryFormat(DebugLineData, OffsetPtr, EndPrologueOffset, nullptr);
Paul Robinson2bc38732017-05-02 21:40:47 +0000206 if (DirDescriptors.empty())
207 return false;
208
209 // Get the directory entries, according to the format described above.
210 int DirEntryCount = DebugLineData.getU8(OffsetPtr);
211 for (int I = 0; I != DirEntryCount; ++I) {
212 if (*OffsetPtr >= EndPrologueOffset)
213 return false;
214 for (auto Descriptor : DirDescriptors) {
Vlad Tsyrklevich53a9f1d2019-03-02 01:10:00 +0000215 DWARFFormValue Value(Descriptor.Form);
Paul Robinson2bc38732017-05-02 21:40:47 +0000216 switch (Descriptor.Type) {
217 case DW_LNCT_path:
Vlad Tsyrklevich53a9f1d2019-03-02 01:10:00 +0000218 if (!Value.extractValue(DebugLineData, OffsetPtr, FormParams, &Ctx, U))
219 return false;
220 IncludeDirectories.push_back(Value);
Paul Robinson2bc38732017-05-02 21:40:47 +0000221 break;
222 default:
Vlad Tsyrklevich53a9f1d2019-03-02 01:10:00 +0000223 if (!Value.skipValue(DebugLineData, OffsetPtr, FormParams))
Paul Robinson2bc38732017-05-02 21:40:47 +0000224 return false;
225 }
226 }
227 }
228
229 // Get the file entry description.
230 ContentDescriptors FileDescriptors =
Scott Linder16c7bda2018-02-23 23:01:06 +0000231 parseV5EntryFormat(DebugLineData, OffsetPtr, EndPrologueOffset,
232 &ContentTypes);
Paul Robinson2bc38732017-05-02 21:40:47 +0000233 if (FileDescriptors.empty())
234 return false;
235
236 // Get the file entries, according to the format described above.
237 int FileEntryCount = DebugLineData.getU8(OffsetPtr);
238 for (int I = 0; I != FileEntryCount; ++I) {
239 if (*OffsetPtr >= EndPrologueOffset)
240 return false;
241 DWARFDebugLine::FileNameEntry FileEntry;
242 for (auto Descriptor : FileDescriptors) {
Vlad Tsyrklevich53a9f1d2019-03-02 01:10:00 +0000243 DWARFFormValue Value(Descriptor.Form);
244 if (!Value.extractValue(DebugLineData, OffsetPtr, FormParams, &Ctx, U))
245 return false;
Paul Robinson2bc38732017-05-02 21:40:47 +0000246 switch (Descriptor.Type) {
247 case DW_LNCT_path:
Paul Robinson0a227092018-02-05 20:43:15 +0000248 FileEntry.Name = Value;
Paul Robinson2bc38732017-05-02 21:40:47 +0000249 break;
Scott Linder16c7bda2018-02-23 23:01:06 +0000250 case DW_LNCT_LLVM_source:
251 FileEntry.Source = Value;
252 break;
Paul Robinson2bc38732017-05-02 21:40:47 +0000253 case DW_LNCT_directory_index:
254 FileEntry.DirIdx = Value.getAsUnsignedConstant().getValue();
255 break;
256 case DW_LNCT_timestamp:
257 FileEntry.ModTime = Value.getAsUnsignedConstant().getValue();
258 break;
259 case DW_LNCT_size:
260 FileEntry.Length = Value.getAsUnsignedConstant().getValue();
261 break;
Paul Robinsona06f8dc2017-12-18 19:08:35 +0000262 case DW_LNCT_MD5:
263 assert(Value.getAsBlock().getValue().size() == 16);
264 std::uninitialized_copy_n(Value.getAsBlock().getValue().begin(), 16,
265 FileEntry.Checksum.Bytes.begin());
266 break;
Paul Robinson2bc38732017-05-02 21:40:47 +0000267 default:
268 break;
269 }
270 }
271 FileNames.push_back(FileEntry);
272 }
273 return true;
274}
275
James Hendersona3acf992018-05-10 10:51:33 +0000276Error DWARFDebugLine::Prologue::parse(const DWARFDataExtractor &DebugLineData,
277 uint32_t *OffsetPtr,
278 const DWARFContext &Ctx,
279 const DWARFUnit *U) {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000280 const uint64_t PrologueOffset = *OffsetPtr;
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000281
282 clear();
Paul Robinson9d4eb692017-05-01 23:27:55 +0000283 TotalLength = DebugLineData.getU32(OffsetPtr);
Ed Maste6d0bee52015-05-28 15:38:17 +0000284 if (TotalLength == UINT32_MAX) {
Paul Robinson75c068c2017-06-26 18:43:01 +0000285 FormParams.Format = dwarf::DWARF64;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000286 TotalLength = DebugLineData.getU64(OffsetPtr);
Paul Robinson75c068c2017-06-26 18:43:01 +0000287 } else if (TotalLength >= 0xffffff00) {
Victor Leschukcba595d2018-08-20 09:59:08 +0000288 return createStringError(errc::invalid_argument,
James Hendersona3acf992018-05-10 10:51:33 +0000289 "parsing line table prologue at offset 0x%8.8" PRIx64
290 " unsupported reserved unit length found of value 0x%8.8" PRIx64,
291 PrologueOffset, TotalLength);
Ed Maste6d0bee52015-05-28 15:38:17 +0000292 }
Paul Robinson75c068c2017-06-26 18:43:01 +0000293 FormParams.Version = DebugLineData.getU16(OffsetPtr);
294 if (getVersion() < 2)
Victor Leschukcba595d2018-08-20 09:59:08 +0000295 return createStringError(errc::not_supported,
296 "parsing line table prologue at offset 0x%8.8" PRIx64
James Hendersona3acf992018-05-10 10:51:33 +0000297 " found unsupported version 0x%2.2" PRIx16,
298 PrologueOffset, getVersion());
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000299
Paul Robinson75c068c2017-06-26 18:43:01 +0000300 if (getVersion() >= 5) {
301 FormParams.AddrSize = DebugLineData.getU8(OffsetPtr);
Paul Robinson63811a42017-11-22 15:33:17 +0000302 assert((DebugLineData.getAddressSize() == 0 ||
303 DebugLineData.getAddressSize() == getAddressSize()) &&
Paul Robinson75c068c2017-06-26 18:43:01 +0000304 "Line table header and data extractor disagree");
Paul Robinson2bc38732017-05-02 21:40:47 +0000305 SegSelectorSize = DebugLineData.getU8(OffsetPtr);
306 }
307
Paul Robinson9d4eb692017-05-01 23:27:55 +0000308 PrologueLength = DebugLineData.getUnsigned(OffsetPtr, sizeofPrologueLength());
309 const uint64_t EndPrologueOffset = PrologueLength + *OffsetPtr;
310 MinInstLength = DebugLineData.getU8(OffsetPtr);
Paul Robinson75c068c2017-06-26 18:43:01 +0000311 if (getVersion() >= 4)
Paul Robinson9d4eb692017-05-01 23:27:55 +0000312 MaxOpsPerInst = DebugLineData.getU8(OffsetPtr);
313 DefaultIsStmt = DebugLineData.getU8(OffsetPtr);
314 LineBase = DebugLineData.getU8(OffsetPtr);
315 LineRange = DebugLineData.getU8(OffsetPtr);
316 OpcodeBase = DebugLineData.getU8(OffsetPtr);
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000317
318 StandardOpcodeLengths.reserve(OpcodeBase - 1);
Paul Robinson9d4eb692017-05-01 23:27:55 +0000319 for (uint32_t I = 1; I < OpcodeBase; ++I) {
320 uint8_t OpLen = DebugLineData.getU8(OffsetPtr);
321 StandardOpcodeLengths.push_back(OpLen);
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000322 }
323
Paul Robinson75c068c2017-06-26 18:43:01 +0000324 if (getVersion() >= 5) {
Paul Robinson2bc38732017-05-02 21:40:47 +0000325 if (!parseV5DirFileTables(DebugLineData, OffsetPtr, EndPrologueOffset,
Scott Linder16c7bda2018-02-23 23:01:06 +0000326 FormParams, Ctx, U, ContentTypes,
327 IncludeDirectories, FileNames)) {
Victor Leschukcba595d2018-08-20 09:59:08 +0000328 return createStringError(errc::invalid_argument,
Jonas Devlieghere84e99262018-04-14 22:07:23 +0000329 "parsing line table prologue at 0x%8.8" PRIx64
330 " found an invalid directory or file table description at"
James Hendersona3acf992018-05-10 10:51:33 +0000331 " 0x%8.8" PRIx64,
Jonas Devlieghere84e99262018-04-14 22:07:23 +0000332 PrologueOffset, (uint64_t)*OffsetPtr);
Paul Robinson2bc38732017-05-02 21:40:47 +0000333 }
334 } else
335 parseV2DirFileTables(DebugLineData, OffsetPtr, EndPrologueOffset,
Scott Linder16c7bda2018-02-23 23:01:06 +0000336 ContentTypes, IncludeDirectories, FileNames);
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000337
James Hendersona3acf992018-05-10 10:51:33 +0000338 if (*OffsetPtr != EndPrologueOffset)
Victor Leschukcba595d2018-08-20 09:59:08 +0000339 return createStringError(errc::invalid_argument,
340 "parsing line table prologue at 0x%8.8" PRIx64
James Hendersona3acf992018-05-10 10:51:33 +0000341 " should have ended at 0x%8.8" PRIx64
342 " but it ended at 0x%8.8" PRIx64,
343 PrologueOffset, EndPrologueOffset, (uint64_t)*OffsetPtr);
344 return Error::success();
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000345}
346
Paul Robinson9d4eb692017-05-01 23:27:55 +0000347DWARFDebugLine::Row::Row(bool DefaultIsStmt) { reset(DefaultIsStmt); }
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000348
Benjamin Kramer5acab502011-09-15 02:12:05 +0000349void DWARFDebugLine::Row::postAppend() {
Fangrui Song6a285df2019-04-11 02:02:44 +0000350 Discriminator = 0;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000351 BasicBlock = false;
352 PrologueEnd = false;
353 EpilogueBegin = false;
354}
355
Paul Robinson9d4eb692017-05-01 23:27:55 +0000356void DWARFDebugLine::Row::reset(bool DefaultIsStmt) {
Alexey Lapshin77fc1f62019-02-27 13:17:36 +0000357 Address.Address = 0;
358 Address.SectionIndex = object::SectionedAddress::UndefSection;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000359 Line = 1;
360 Column = 0;
361 File = 1;
362 Isa = 0;
Diego Novillo5b5cf502014-02-14 19:27:53 +0000363 Discriminator = 0;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000364 IsStmt = DefaultIsStmt;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000365 BasicBlock = false;
366 EndSequence = false;
367 PrologueEnd = false;
368 EpilogueBegin = false;
369}
370
Greg Clayton67070462017-05-02 22:48:52 +0000371void DWARFDebugLine::Row::dumpTableHeader(raw_ostream &OS) {
372 OS << "Address Line Column File ISA Discriminator Flags\n"
373 << "------------------ ------ ------ ------ --- ------------- "
374 "-------------\n";
375}
376
Benjamin Kramer5acab502011-09-15 02:12:05 +0000377void DWARFDebugLine::Row::dump(raw_ostream &OS) const {
Alexey Lapshin77fc1f62019-02-27 13:17:36 +0000378 OS << format("0x%16.16" PRIx64 " %6u %6u", Address.Address, Line, Column)
Diego Novillo5b5cf502014-02-14 19:27:53 +0000379 << format(" %6u %3u %13u ", File, Isa, Discriminator)
Dehao Chen1b54fce2016-04-28 22:09:37 +0000380 << (IsStmt ? " is_stmt" : "") << (BasicBlock ? " basic_block" : "")
Benjamin Kramer5acab502011-09-15 02:12:05 +0000381 << (PrologueEnd ? " prologue_end" : "")
382 << (EpilogueBegin ? " epilogue_begin" : "")
Dehao Chen1b54fce2016-04-28 22:09:37 +0000383 << (EndSequence ? " end_sequence" : "") << '\n';
Benjamin Kramer5acab502011-09-15 02:12:05 +0000384}
385
Dehao Chen1b54fce2016-04-28 22:09:37 +0000386DWARFDebugLine::Sequence::Sequence() { reset(); }
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000387
388void DWARFDebugLine::Sequence::reset() {
389 LowPC = 0;
390 HighPC = 0;
Alexey Lapshin77fc1f62019-02-27 13:17:36 +0000391 SectionIndex = object::SectionedAddress::UndefSection;
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000392 FirstRowIndex = 0;
393 LastRowIndex = 0;
394 Empty = true;
395}
396
Dehao Chen1b54fce2016-04-28 22:09:37 +0000397DWARFDebugLine::LineTable::LineTable() { clear(); }
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000398
Paul Robinson0a227092018-02-05 20:43:15 +0000399void DWARFDebugLine::LineTable::dump(raw_ostream &OS,
400 DIDumpOptions DumpOptions) const {
401 Prologue.dump(OS, DumpOptions);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000402 OS << '\n';
403
404 if (!Rows.empty()) {
Greg Clayton67070462017-05-02 22:48:52 +0000405 Row::dumpTableHeader(OS);
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000406 for (const Row &R : Rows) {
407 R.dump(OS);
408 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000409 }
410}
411
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000412void DWARFDebugLine::LineTable::clear() {
413 Prologue.clear();
414 Rows.clear();
415 Sequences.clear();
416}
417
Alexey Samsonov110d5952014-04-30 00:09:19 +0000418DWARFDebugLine::ParsingState::ParsingState(struct LineTable *LT)
Eugene Zelenko2db0cfa2017-06-23 21:57:40 +0000419 : LineTable(LT) {
Alexey Samsonov110d5952014-04-30 00:09:19 +0000420 resetRowAndSequence();
421}
Nick Lewycky4d044922011-09-15 03:41:51 +0000422
Alexey Samsonov110d5952014-04-30 00:09:19 +0000423void DWARFDebugLine::ParsingState::resetRowAndSequence() {
424 Row.reset(LineTable->Prologue.DefaultIsStmt);
425 Sequence.reset();
426}
427
Fangrui Songc4c8bca2019-04-07 13:56:14 +0000428void DWARFDebugLine::ParsingState::appendRowToMatrix() {
Fangrui Song50a09672019-04-15 07:40:30 +0000429 unsigned RowNumber = LineTable->Rows.size();
Alexey Samsonov110d5952014-04-30 00:09:19 +0000430 if (Sequence.Empty) {
Alexey Samsonov947228c2012-08-07 11:46:57 +0000431 // Record the beginning of instruction sequence.
Alexey Samsonov110d5952014-04-30 00:09:19 +0000432 Sequence.Empty = false;
Alexey Lapshin77fc1f62019-02-27 13:17:36 +0000433 Sequence.LowPC = Row.Address.Address;
Alexey Samsonov110d5952014-04-30 00:09:19 +0000434 Sequence.FirstRowIndex = RowNumber;
Alexey Samsonov947228c2012-08-07 11:46:57 +0000435 }
Alexey Samsonov110d5952014-04-30 00:09:19 +0000436 LineTable->appendRow(Row);
437 if (Row.EndSequence) {
Alexey Samsonov947228c2012-08-07 11:46:57 +0000438 // Record the end of instruction sequence.
Alexey Lapshin77fc1f62019-02-27 13:17:36 +0000439 Sequence.HighPC = Row.Address.Address;
Fangrui Song50a09672019-04-15 07:40:30 +0000440 Sequence.LastRowIndex = RowNumber + 1;
Alexey Lapshin77fc1f62019-02-27 13:17:36 +0000441 Sequence.SectionIndex = Row.Address.SectionIndex;
Alexey Samsonov110d5952014-04-30 00:09:19 +0000442 if (Sequence.isValid())
443 LineTable->appendSequence(Sequence);
444 Sequence.reset();
Alexey Samsonov947228c2012-08-07 11:46:57 +0000445 }
Alexey Samsonov110d5952014-04-30 00:09:19 +0000446 Row.postAppend();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000447}
448
Benjamin Kramer5acab502011-09-15 02:12:05 +0000449const DWARFDebugLine::LineTable *
Paul Robinson9d4eb692017-05-01 23:27:55 +0000450DWARFDebugLine::getLineTable(uint32_t Offset) const {
451 LineTableConstIter Pos = LineTableMap.find(Offset);
452 if (Pos != LineTableMap.end())
453 return &Pos->second;
Craig Topper2617dcc2014-04-15 06:32:26 +0000454 return nullptr;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000455}
456
James Hendersona3acf992018-05-10 10:51:33 +0000457Expected<const DWARFDebugLine::LineTable *> DWARFDebugLine::getOrParseLineTable(
458 DWARFDataExtractor &DebugLineData, uint32_t Offset, const DWARFContext &Ctx,
James Henderson004b7292018-05-21 15:30:54 +0000459 const DWARFUnit *U, std::function<void(Error)> RecoverableErrorCallback) {
James Henderson66702622018-03-08 10:53:34 +0000460 if (!DebugLineData.isValidOffset(Offset))
Victor Leschukcba595d2018-08-20 09:59:08 +0000461 return createStringError(errc::invalid_argument, "offset 0x%8.8" PRIx32
James Hendersona3acf992018-05-10 10:51:33 +0000462 " is not a valid debug line section offset",
463 Offset);
James Henderson66702622018-03-08 10:53:34 +0000464
Paul Robinson9d4eb692017-05-01 23:27:55 +0000465 std::pair<LineTableIter, bool> Pos =
466 LineTableMap.insert(LineTableMapTy::value_type(Offset, LineTable()));
467 LineTable *LT = &Pos.first->second;
468 if (Pos.second) {
James Henderson004b7292018-05-21 15:30:54 +0000469 if (Error Err =
470 LT->parse(DebugLineData, &Offset, Ctx, U, RecoverableErrorCallback))
James Hendersona3acf992018-05-10 10:51:33 +0000471 return std::move(Err);
472 return LT;
Benjamin Kramer679e1752011-09-15 20:43:18 +0000473 }
Alexey Samsonov110d5952014-04-30 00:09:19 +0000474 return LT;
Benjamin Kramer679e1752011-09-15 20:43:18 +0000475}
476
James Hendersona3acf992018-05-10 10:51:33 +0000477Error DWARFDebugLine::LineTable::parse(
478 DWARFDataExtractor &DebugLineData, uint32_t *OffsetPtr,
479 const DWARFContext &Ctx, const DWARFUnit *U,
James Henderson004b7292018-05-21 15:30:54 +0000480 std::function<void(Error)> RecoverableErrorCallback, raw_ostream *OS) {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000481 const uint32_t DebugLineOffset = *OffsetPtr;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000482
Alexey Samsonov110d5952014-04-30 00:09:19 +0000483 clear();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000484
James Hendersona3acf992018-05-10 10:51:33 +0000485 Error PrologueErr = Prologue.parse(DebugLineData, OffsetPtr, Ctx, U);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000486
Paul Robinson0a227092018-02-05 20:43:15 +0000487 if (OS) {
488 // The presence of OS signals verbose dumping.
489 DIDumpOptions DumpOptions;
490 DumpOptions.Verbose = true;
491 Prologue.dump(*OS, DumpOptions);
492 }
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000493
James Hendersona3acf992018-05-10 10:51:33 +0000494 if (PrologueErr)
495 return PrologueErr;
496
Paul Robinson9d4eb692017-05-01 23:27:55 +0000497 const uint32_t EndOffset =
498 DebugLineOffset + Prologue.TotalLength + Prologue.sizeofTotalLength();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000499
Paul Robinson511b54c2017-11-22 15:48:30 +0000500 // See if we should tell the data extractor the address size.
501 if (DebugLineData.getAddressSize() == 0)
502 DebugLineData.setAddressSize(Prologue.getAddressSize());
503 else
504 assert(Prologue.getAddressSize() == 0 ||
505 Prologue.getAddressSize() == DebugLineData.getAddressSize());
506
Alexey Samsonov110d5952014-04-30 00:09:19 +0000507 ParsingState State(this);
Benjamin Kramer112ec172011-09-15 21:59:13 +0000508
Paul Robinson9d4eb692017-05-01 23:27:55 +0000509 while (*OffsetPtr < EndOffset) {
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000510 if (OS)
511 *OS << format("0x%08.08" PRIx32 ": ", *OffsetPtr);
512
Paul Robinson9d4eb692017-05-01 23:27:55 +0000513 uint8_t Opcode = DebugLineData.getU8(OffsetPtr);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000514
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000515 if (OS)
516 *OS << format("%02.02" PRIx8 " ", Opcode);
517
Paul Robinson9d4eb692017-05-01 23:27:55 +0000518 if (Opcode == 0) {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000519 // Extended Opcodes always start with a zero opcode followed by
520 // a uleb128 length so you can skip ones you don't know about
Paul Robinson9d4eb692017-05-01 23:27:55 +0000521 uint64_t Len = DebugLineData.getULEB128(OffsetPtr);
Paul Robinsone0833342017-11-22 15:14:49 +0000522 uint32_t ExtOffset = *OffsetPtr;
523
524 // Tolerate zero-length; assume length is correct and soldier on.
525 if (Len == 0) {
526 if (OS)
527 *OS << "Badly formed extended line op (length 0)\n";
528 continue;
529 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000530
Paul Robinson9d4eb692017-05-01 23:27:55 +0000531 uint8_t SubOpcode = DebugLineData.getU8(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000532 if (OS)
533 *OS << LNExtendedString(SubOpcode);
Paul Robinson9d4eb692017-05-01 23:27:55 +0000534 switch (SubOpcode) {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000535 case DW_LNE_end_sequence:
536 // Set the end_sequence register of the state machine to true and
537 // append a row to the matrix using the current values of the
538 // state-machine registers. Then reset the registers to the initial
539 // values specified above. Every statement program sequence must end
540 // with a DW_LNE_end_sequence instruction which creates a row whose
541 // address is that of the byte after the last target machine instruction
542 // of the sequence.
Alexey Samsonov110d5952014-04-30 00:09:19 +0000543 State.Row.EndSequence = true;
Fangrui Songc4c8bca2019-04-07 13:56:14 +0000544 State.appendRowToMatrix();
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000545 if (OS) {
546 *OS << "\n";
547 OS->indent(12);
548 State.Row.dump(*OS);
549 }
Alexey Samsonov110d5952014-04-30 00:09:19 +0000550 State.resetRowAndSequence();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000551 break;
552
553 case DW_LNE_set_address:
554 // Takes a single relocatable address as an operand. The size of the
555 // operand is the size appropriate to hold an address on the target
556 // machine. Set the address register to the value given by the
557 // relocatable address. All of the other statement program opcodes
558 // that affect the address register add a delta to it. This instruction
559 // stores a relocatable value into it instead.
Paul Robinson511b54c2017-11-22 15:48:30 +0000560 //
561 // Make sure the extractor knows the address size. If not, infer it
562 // from the size of the operand.
563 if (DebugLineData.getAddressSize() == 0)
564 DebugLineData.setAddressSize(Len - 1);
Paul Robinson79474682018-03-22 19:37:56 +0000565 else if (DebugLineData.getAddressSize() != Len - 1) {
Victor Leschukcba595d2018-08-20 09:59:08 +0000566 return createStringError(errc::invalid_argument,
567 "mismatching address size at offset 0x%8.8" PRIx32
James Hendersona3acf992018-05-10 10:51:33 +0000568 " expected 0x%2.2" PRIx8 " found 0x%2.2" PRIx64,
569 ExtOffset, DebugLineData.getAddressSize(),
570 Len - 1);
Paul Robinson79474682018-03-22 19:37:56 +0000571 }
Alexey Lapshin77fc1f62019-02-27 13:17:36 +0000572 State.Row.Address.Address = DebugLineData.getRelocatedAddress(
573 OffsetPtr, &State.Row.Address.SectionIndex);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000574 if (OS)
Alexey Lapshin77fc1f62019-02-27 13:17:36 +0000575 *OS << format(" (0x%16.16" PRIx64 ")", State.Row.Address.Address);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000576 break;
577
578 case DW_LNE_define_file:
579 // Takes 4 arguments. The first is a null terminated string containing
580 // a source file name. The second is an unsigned LEB128 number
581 // representing the directory index of the directory in which the file
582 // was found. The third is an unsigned LEB128 number representing the
583 // time of last modification of the file. The fourth is an unsigned
584 // LEB128 number representing the length in bytes of the file. The time
585 // and length fields may contain LEB128(0) if the information is not
586 // available.
587 //
588 // The directory index represents an entry in the include_directories
589 // section of the statement program prologue. The index is LEB128(0)
590 // if the file was found in the current directory of the compilation,
591 // LEB128(1) if it was found in the first directory in the
592 // include_directories section, and so on. The directory index is
593 // ignored for file names that represent full path names.
594 //
595 // The files are numbered, starting at 1, in the order in which they
596 // appear; the names in the prologue come before names defined by
597 // the DW_LNE_define_file instruction. These numbers are used in the
598 // the file register of the state machine.
599 {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000600 FileNameEntry FileEntry;
Paul Robinson0a227092018-02-05 20:43:15 +0000601 const char *Name = DebugLineData.getCStr(OffsetPtr);
Jonas Devliegherebb111152019-02-27 00:58:09 +0000602 FileEntry.Name =
603 DWARFFormValue::createFromPValue(dwarf::DW_FORM_string, Name);
Paul Robinson9d4eb692017-05-01 23:27:55 +0000604 FileEntry.DirIdx = DebugLineData.getULEB128(OffsetPtr);
605 FileEntry.ModTime = DebugLineData.getULEB128(OffsetPtr);
606 FileEntry.Length = DebugLineData.getULEB128(OffsetPtr);
607 Prologue.FileNames.push_back(FileEntry);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000608 if (OS)
Paul Robinson0a227092018-02-05 20:43:15 +0000609 *OS << " (" << Name << ", dir=" << FileEntry.DirIdx << ", mod_time="
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000610 << format("(0x%16.16" PRIx64 ")", FileEntry.ModTime)
611 << ", length=" << FileEntry.Length << ")";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000612 }
613 break;
614
Diego Novillo5b5cf502014-02-14 19:27:53 +0000615 case DW_LNE_set_discriminator:
Paul Robinson9d4eb692017-05-01 23:27:55 +0000616 State.Row.Discriminator = DebugLineData.getULEB128(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000617 if (OS)
618 *OS << " (" << State.Row.Discriminator << ")";
Diego Novillo5b5cf502014-02-14 19:27:53 +0000619 break;
620
Benjamin Kramer5acab502011-09-15 02:12:05 +0000621 default:
Paul Robinsone0833342017-11-22 15:14:49 +0000622 if (OS)
623 *OS << format("Unrecognized extended op 0x%02.02" PRIx8, SubOpcode)
624 << format(" length %" PRIx64, Len);
625 // Len doesn't include the zero opcode byte or the length itself, but
626 // it does include the sub_opcode, so we have to adjust for that.
627 (*OffsetPtr) += Len - 1;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000628 break;
629 }
Paul Robinsone0833342017-11-22 15:14:49 +0000630 // Make sure the stated and parsed lengths are the same.
631 // Otherwise we have an unparseable line-number program.
James Hendersona3acf992018-05-10 10:51:33 +0000632 if (*OffsetPtr - ExtOffset != Len)
Victor Leschukcba595d2018-08-20 09:59:08 +0000633 return createStringError(errc::illegal_byte_sequence,
634 "unexpected line op length at offset 0x%8.8" PRIx32
James Hendersona3acf992018-05-10 10:51:33 +0000635 " expected 0x%2.2" PRIx64 " found 0x%2.2" PRIx32,
636 ExtOffset, Len, *OffsetPtr - ExtOffset);
Paul Robinson9d4eb692017-05-01 23:27:55 +0000637 } else if (Opcode < Prologue.OpcodeBase) {
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000638 if (OS)
639 *OS << LNStandardString(Opcode);
Paul Robinson9d4eb692017-05-01 23:27:55 +0000640 switch (Opcode) {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000641 // Standard Opcodes
642 case DW_LNS_copy:
643 // Takes no arguments. Append a row to the matrix using the
Fangrui Song6a285df2019-04-11 02:02:44 +0000644 // current values of the state-machine registers.
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000645 if (OS) {
646 *OS << "\n";
647 OS->indent(12);
648 State.Row.dump(*OS);
649 *OS << "\n";
650 }
Fangrui Song6a285df2019-04-11 02:02:44 +0000651 State.appendRowToMatrix();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000652 break;
653
654 case DW_LNS_advance_pc:
655 // Takes a single unsigned LEB128 operand, multiplies it by the
656 // min_inst_length field of the prologue, and adds the
657 // result to the address register of the state machine.
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000658 {
659 uint64_t AddrOffset =
660 DebugLineData.getULEB128(OffsetPtr) * Prologue.MinInstLength;
Alexey Lapshin77fc1f62019-02-27 13:17:36 +0000661 State.Row.Address.Address += AddrOffset;
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000662 if (OS)
663 *OS << " (" << AddrOffset << ")";
664 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000665 break;
666
667 case DW_LNS_advance_line:
668 // Takes a single signed LEB128 operand and adds that value to
669 // the line register of the state machine.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000670 State.Row.Line += DebugLineData.getSLEB128(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000671 if (OS)
672 *OS << " (" << State.Row.Line << ")";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000673 break;
674
675 case DW_LNS_set_file:
676 // Takes a single unsigned LEB128 operand and stores it in the file
677 // register of the state machine.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000678 State.Row.File = DebugLineData.getULEB128(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000679 if (OS)
680 *OS << " (" << State.Row.File << ")";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000681 break;
682
683 case DW_LNS_set_column:
684 // Takes a single unsigned LEB128 operand and stores it in the
685 // column register of the state machine.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000686 State.Row.Column = DebugLineData.getULEB128(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000687 if (OS)
688 *OS << " (" << State.Row.Column << ")";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000689 break;
690
691 case DW_LNS_negate_stmt:
692 // Takes no arguments. Set the is_stmt register of the state
693 // machine to the logical negation of its current value.
Alexey Samsonov110d5952014-04-30 00:09:19 +0000694 State.Row.IsStmt = !State.Row.IsStmt;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000695 break;
696
697 case DW_LNS_set_basic_block:
698 // Takes no arguments. Set the basic_block register of the
699 // state machine to true
Alexey Samsonov110d5952014-04-30 00:09:19 +0000700 State.Row.BasicBlock = true;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000701 break;
702
703 case DW_LNS_const_add_pc:
704 // Takes no arguments. Add to the address register of the state
705 // machine the address increment value corresponding to special
706 // opcode 255. The motivation for DW_LNS_const_add_pc is this:
707 // when the statement program needs to advance the address by a
708 // small amount, it can use a single special opcode, which occupies
709 // a single byte. When it needs to advance the address by up to
710 // twice the range of the last special opcode, it can use
711 // DW_LNS_const_add_pc followed by a special opcode, for a total
712 // of two bytes. Only if it needs to advance the address by more
713 // than twice that range will it need to use both DW_LNS_advance_pc
714 // and a special opcode, requiring three or more bytes.
715 {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000716 uint8_t AdjustOpcode = 255 - Prologue.OpcodeBase;
717 uint64_t AddrOffset =
718 (AdjustOpcode / Prologue.LineRange) * Prologue.MinInstLength;
Alexey Lapshin77fc1f62019-02-27 13:17:36 +0000719 State.Row.Address.Address += AddrOffset;
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000720 if (OS)
721 *OS
722 << format(" (0x%16.16" PRIx64 ")", AddrOffset);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000723 }
724 break;
725
726 case DW_LNS_fixed_advance_pc:
727 // Takes a single uhalf operand. Add to the address register of
728 // the state machine the value of the (unencoded) operand. This
729 // is the only extended opcode that takes an argument that is not
730 // a variable length number. The motivation for DW_LNS_fixed_advance_pc
731 // is this: existing assemblers cannot emit DW_LNS_advance_pc or
732 // special opcodes because they cannot encode LEB128 numbers or
733 // judge when the computation of a special opcode overflows and
734 // requires the use of DW_LNS_advance_pc. Such assemblers, however,
735 // can use DW_LNS_fixed_advance_pc instead, sacrificing compression.
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000736 {
737 uint16_t PCOffset = DebugLineData.getU16(OffsetPtr);
Alexey Lapshin77fc1f62019-02-27 13:17:36 +0000738 State.Row.Address.Address += PCOffset;
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000739 if (OS)
740 *OS
741 << format(" (0x%16.16" PRIx64 ")", PCOffset);
742 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000743 break;
744
745 case DW_LNS_set_prologue_end:
746 // Takes no arguments. Set the prologue_end register of the
747 // state machine to true
Alexey Samsonov110d5952014-04-30 00:09:19 +0000748 State.Row.PrologueEnd = true;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000749 break;
750
751 case DW_LNS_set_epilogue_begin:
752 // Takes no arguments. Set the basic_block register of the
753 // state machine to true
Alexey Samsonov110d5952014-04-30 00:09:19 +0000754 State.Row.EpilogueBegin = true;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000755 break;
756
757 case DW_LNS_set_isa:
758 // Takes a single unsigned LEB128 operand and stores it in the
759 // column register of the state machine.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000760 State.Row.Isa = DebugLineData.getULEB128(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000761 if (OS)
762 *OS << " (" << State.Row.Isa << ")";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000763 break;
764
765 default:
766 // Handle any unknown standard opcodes here. We know the lengths
767 // of such opcodes because they are specified in the prologue
768 // as a multiple of LEB128 operands for each opcode.
769 {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000770 assert(Opcode - 1U < Prologue.StandardOpcodeLengths.size());
771 uint8_t OpcodeLength = Prologue.StandardOpcodeLengths[Opcode - 1];
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000772 for (uint8_t I = 0; I < OpcodeLength; ++I) {
773 uint64_t Value = DebugLineData.getULEB128(OffsetPtr);
774 if (OS)
775 *OS << format("Skipping ULEB128 value: 0x%16.16" PRIx64 ")\n",
776 Value);
777 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000778 }
779 break;
780 }
781 } else {
782 // Special Opcodes
783
784 // A special opcode value is chosen based on the amount that needs
785 // to be added to the line and address registers. The maximum line
786 // increment for a special opcode is the value of the line_base
787 // field in the header, plus the value of the line_range field,
788 // minus 1 (line base + line range - 1). If the desired line
789 // increment is greater than the maximum line increment, a standard
NAKAMURA Takumif9959852011-10-08 11:22:47 +0000790 // opcode must be used instead of a special opcode. The "address
791 // advance" is calculated by dividing the desired address increment
Benjamin Kramer5acab502011-09-15 02:12:05 +0000792 // by the minimum_instruction_length field from the header. The
793 // special opcode is then calculated using the following formula:
794 //
795 // opcode = (desired line increment - line_base) +
796 // (line_range * address advance) + opcode_base
797 //
798 // If the resulting opcode is greater than 255, a standard opcode
799 // must be used instead.
800 //
801 // To decode a special opcode, subtract the opcode_base from the
802 // opcode itself to give the adjusted opcode. The amount to
803 // increment the address register is the result of the adjusted
804 // opcode divided by the line_range multiplied by the
805 // minimum_instruction_length field from the header. That is:
806 //
807 // address increment = (adjusted opcode / line_range) *
808 // minimum_instruction_length
809 //
810 // The amount to increment the line register is the line_base plus
811 // the result of the adjusted opcode modulo the line_range. That is:
812 //
813 // line increment = line_base + (adjusted opcode % line_range)
814
Paul Robinson9d4eb692017-05-01 23:27:55 +0000815 uint8_t AdjustOpcode = Opcode - Prologue.OpcodeBase;
816 uint64_t AddrOffset =
817 (AdjustOpcode / Prologue.LineRange) * Prologue.MinInstLength;
818 int32_t LineOffset =
819 Prologue.LineBase + (AdjustOpcode % Prologue.LineRange);
820 State.Row.Line += LineOffset;
Alexey Lapshin77fc1f62019-02-27 13:17:36 +0000821 State.Row.Address.Address += AddrOffset;
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000822
823 if (OS) {
James Hendersonb6b5b1a2019-02-06 10:31:50 +0000824 *OS << "address += " << AddrOffset << ", line += " << LineOffset
825 << "\n";
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000826 OS->indent(12);
827 State.Row.dump(*OS);
828 }
829
Fangrui Songc4c8bca2019-04-07 13:56:14 +0000830 State.appendRowToMatrix();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000831 }
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000832 if(OS)
833 *OS << "\n";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000834 }
835
Jonas Devlieghere84e99262018-04-14 22:07:23 +0000836 if (!State.Sequence.Empty)
James Henderson004b7292018-05-21 15:30:54 +0000837 RecoverableErrorCallback(
Victor Leschukcba595d2018-08-20 09:59:08 +0000838 createStringError(errc::illegal_byte_sequence,
839 "last sequence in debug line table is not terminated!"));
Alexey Samsonov110d5952014-04-30 00:09:19 +0000840
841 // Sort all sequences so that address lookup will work faster.
842 if (!Sequences.empty()) {
Fangrui Song9b22c462019-04-09 15:08:32 +0000843 llvm::sort(Sequences, Sequence::orderByHighPC);
Alexey Samsonov110d5952014-04-30 00:09:19 +0000844 // Note: actually, instruction address ranges of sequences should not
845 // overlap (in shared objects and executables). If they do, the address
846 // lookup would still work, though, but result would be ambiguous.
847 // We don't report warning in this case. For example,
848 // sometimes .so compiled from multiple object files contains a few
849 // rudimentary sequences for address ranges [0x0, 0xsomething).
850 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000851
James Hendersona3acf992018-05-10 10:51:33 +0000852 return Error::success();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000853}
854
Alexey Lapshin77fc1f62019-02-27 13:17:36 +0000855uint32_t DWARFDebugLine::LineTable::findRowInSeq(
856 const DWARFDebugLine::Sequence &Seq,
857 object::SectionedAddress Address) const {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000858 if (!Seq.containsPC(Address))
Keno Fischerc2c60182015-05-31 23:37:04 +0000859 return UnknownRowIndex;
Alexey Lapshin77fc1f62019-02-27 13:17:36 +0000860 assert(Seq.SectionIndex == Address.SectionIndex);
Fangrui Songb3be23d2019-04-10 07:44:23 +0000861 // In some cases, e.g. first instruction in a function, the compiler generates
862 // two entries, both with the same address. We want the last one.
863 //
864 // In general we want a non-empty range: the last row whose address is less
865 // than or equal to Address. This can be computed as upper_bound - 1.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000866 DWARFDebugLine::Row Row;
867 Row.Address = Address;
868 RowIter FirstRow = Rows.begin() + Seq.FirstRowIndex;
869 RowIter LastRow = Rows.begin() + Seq.LastRowIndex;
Fangrui Songb3be23d2019-04-10 07:44:23 +0000870 assert(FirstRow->Address.Address <= Row.Address.Address &&
871 Row.Address.Address < LastRow[-1].Address.Address);
872 RowIter RowPos = std::upper_bound(FirstRow + 1, LastRow - 1, Row,
873 DWARFDebugLine::Row::orderByAddress) -
874 1;
Alexey Lapshin77fc1f62019-02-27 13:17:36 +0000875 assert(Seq.SectionIndex == RowPos->Address.SectionIndex);
Fangrui Songb3be23d2019-04-10 07:44:23 +0000876 return RowPos - Rows.begin();
Keno Fischerc2c60182015-05-31 23:37:04 +0000877}
878
Alexey Lapshin77fc1f62019-02-27 13:17:36 +0000879uint32_t DWARFDebugLine::LineTable::lookupAddress(
880 object::SectionedAddress Address) const {
881
882 // Search for relocatable addresses
883 uint32_t Result = lookupAddressImpl(Address);
884
885 if (Result != UnknownRowIndex ||
886 Address.SectionIndex == object::SectionedAddress::UndefSection)
887 return Result;
888
889 // Search for absolute addresses
890 Address.SectionIndex = object::SectionedAddress::UndefSection;
891 return lookupAddressImpl(Address);
892}
893
894uint32_t DWARFDebugLine::LineTable::lookupAddressImpl(
895 object::SectionedAddress Address) const {
Alexey Samsonov947228c2012-08-07 11:46:57 +0000896 // First, find an instruction sequence containing the given address.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000897 DWARFDebugLine::Sequence Sequence;
Alexey Lapshin77fc1f62019-02-27 13:17:36 +0000898 Sequence.SectionIndex = Address.SectionIndex;
Fangrui Song9b22c462019-04-09 15:08:32 +0000899 Sequence.HighPC = Address.Address;
900 SequenceIter It = llvm::upper_bound(Sequences, Sequence,
901 DWARFDebugLine::Sequence::orderByHighPC);
902 if (It == Sequences.end() || It->SectionIndex != Address.SectionIndex)
Alexey Lapshin77fc1f62019-02-27 13:17:36 +0000903 return UnknownRowIndex;
Fangrui Song9b22c462019-04-09 15:08:32 +0000904 return findRowInSeq(*It, Address);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000905}
Alexey Samsonov45be7932012-08-30 07:49:50 +0000906
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000907bool DWARFDebugLine::LineTable::lookupAddressRange(
Alexey Lapshin77fc1f62019-02-27 13:17:36 +0000908 object::SectionedAddress Address, uint64_t Size,
909 std::vector<uint32_t> &Result) const {
910
911 // Search for relocatable addresses
912 if (lookupAddressRangeImpl(Address, Size, Result))
913 return true;
914
915 if (Address.SectionIndex == object::SectionedAddress::UndefSection)
916 return false;
917
918 // Search for absolute addresses
919 Address.SectionIndex = object::SectionedAddress::UndefSection;
920 return lookupAddressRangeImpl(Address, Size, Result);
921}
922
923bool DWARFDebugLine::LineTable::lookupAddressRangeImpl(
924 object::SectionedAddress Address, uint64_t Size,
925 std::vector<uint32_t> &Result) const {
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000926 if (Sequences.empty())
927 return false;
Alexey Lapshin77fc1f62019-02-27 13:17:36 +0000928 uint64_t EndAddr = Address.Address + Size;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000929 // First, find an instruction sequence containing the given address.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000930 DWARFDebugLine::Sequence Sequence;
Alexey Lapshin77fc1f62019-02-27 13:17:36 +0000931 Sequence.SectionIndex = Address.SectionIndex;
Fangrui Song9b22c462019-04-09 15:08:32 +0000932 Sequence.HighPC = Address.Address;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000933 SequenceIter LastSeq = Sequences.end();
Fangrui Song9b22c462019-04-09 15:08:32 +0000934 SequenceIter SeqPos = llvm::upper_bound(
935 Sequences, Sequence, DWARFDebugLine::Sequence::orderByHighPC);
936 if (SeqPos == LastSeq || !SeqPos->containsPC(Address))
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000937 return false;
938
Paul Robinson9d4eb692017-05-01 23:27:55 +0000939 SequenceIter StartPos = SeqPos;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000940
941 // Add the rows from the first sequence to the vector, starting with the
942 // index we just calculated
943
Paul Robinson9d4eb692017-05-01 23:27:55 +0000944 while (SeqPos != LastSeq && SeqPos->LowPC < EndAddr) {
945 const DWARFDebugLine::Sequence &CurSeq = *SeqPos;
Keno Fischerc2c60182015-05-31 23:37:04 +0000946 // For the first sequence, we need to find which row in the sequence is the
947 // first in our range.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000948 uint32_t FirstRowIndex = CurSeq.FirstRowIndex;
949 if (SeqPos == StartPos)
950 FirstRowIndex = findRowInSeq(CurSeq, Address);
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000951
Keno Fischerc2c60182015-05-31 23:37:04 +0000952 // Figure out the last row in the range.
Alexey Lapshin77fc1f62019-02-27 13:17:36 +0000953 uint32_t LastRowIndex =
954 findRowInSeq(CurSeq, {EndAddr - 1, Address.SectionIndex});
Paul Robinson9d4eb692017-05-01 23:27:55 +0000955 if (LastRowIndex == UnknownRowIndex)
956 LastRowIndex = CurSeq.LastRowIndex - 1;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000957
Paul Robinson9d4eb692017-05-01 23:27:55 +0000958 assert(FirstRowIndex != UnknownRowIndex);
959 assert(LastRowIndex != UnknownRowIndex);
Keno Fischerc2c60182015-05-31 23:37:04 +0000960
Paul Robinson9d4eb692017-05-01 23:27:55 +0000961 for (uint32_t I = FirstRowIndex; I <= LastRowIndex; ++I) {
962 Result.push_back(I);
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000963 }
964
Paul Robinson9d4eb692017-05-01 23:27:55 +0000965 ++SeqPos;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000966 }
NAKAMURA Takumi4b86cdb2013-01-26 01:45:06 +0000967
968 return true;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000969}
970
Paul Robinson9d4eb692017-05-01 23:27:55 +0000971bool DWARFDebugLine::LineTable::hasFileAtIndex(uint64_t FileIndex) const {
Ali Tamur783d84b2019-04-19 02:26:56 +0000972 uint16_t DwarfVersion = Prologue.getVersion();
973 assert(DwarfVersion != 0 && "LineTable has no dwarf version information");
974 if (DwarfVersion >= 5)
975 return FileIndex < Prologue.FileNames.size();
Ali Tamur02e96642019-03-26 20:05:27 +0000976 return FileIndex != 0 && FileIndex <= Prologue.FileNames.size();
Pete Cooperb2ba7762016-07-22 01:41:32 +0000977}
978
Ali Tamur783d84b2019-04-19 02:26:56 +0000979const llvm::DWARFDebugLine::FileNameEntry &
980DWARFDebugLine::LineTable::getFileNameEntry(uint64_t Index) const {
981 uint16_t DwarfVersion = Prologue.getVersion();
982 assert(DwarfVersion != 0 && "LineTable has no dwarf version information");
Fangrui Song31537642019-04-26 13:41:19 +0000983 // In DWARF v5 the file names are 0-indexed.
Ali Tamur783d84b2019-04-19 02:26:56 +0000984 if (DwarfVersion >= 5)
985 return Prologue.FileNames[Index];
986 else
987 return Prologue.FileNames[Index - 1];
988}
989
Scott Linder16c7bda2018-02-23 23:01:06 +0000990Optional<StringRef> DWARFDebugLine::LineTable::getSourceByIndex(uint64_t FileIndex,
991 FileLineInfoKind Kind) const {
992 if (Kind == FileLineInfoKind::None || !hasFileAtIndex(FileIndex))
993 return None;
Ali Tamur783d84b2019-04-19 02:26:56 +0000994 const FileNameEntry &Entry = getFileNameEntry(FileIndex);
Scott Linder16c7bda2018-02-23 23:01:06 +0000995 if (Optional<const char *> source = Entry.Source.getAsCString())
996 return StringRef(*source);
997 return None;
998}
999
Eugene Zemtsov82d60d62018-03-13 17:54:29 +00001000static bool isPathAbsoluteOnWindowsOrPosix(const Twine &Path) {
1001 // Debug info can contain paths from any OS, not necessarily
1002 // an OS we're currently running on. Moreover different compilation units can
1003 // be compiled on different operating systems and linked together later.
1004 return sys::path::is_absolute(Path, sys::path::Style::posix) ||
1005 sys::path::is_absolute(Path, sys::path::Style::windows);
1006}
1007
Paul Robinson9d4eb692017-05-01 23:27:55 +00001008bool DWARFDebugLine::LineTable::getFileNameByIndex(uint64_t FileIndex,
1009 const char *CompDir,
1010 FileLineInfoKind Kind,
1011 std::string &Result) const {
Pete Cooperb2ba7762016-07-22 01:41:32 +00001012 if (Kind == FileLineInfoKind::None || !hasFileAtIndex(FileIndex))
Alexey Samsonov45be7932012-08-30 07:49:50 +00001013 return false;
Ali Tamur783d84b2019-04-19 02:26:56 +00001014 const FileNameEntry &Entry = getFileNameEntry(FileIndex);
Paul Robinson0a227092018-02-05 20:43:15 +00001015 StringRef FileName = Entry.Name.getAsCString().getValue();
Alexey Samsonovdce67342014-05-15 21:24:32 +00001016 if (Kind != FileLineInfoKind::AbsoluteFilePath ||
Eugene Zemtsov82d60d62018-03-13 17:54:29 +00001017 isPathAbsoluteOnWindowsOrPosix(FileName)) {
Alexey Samsonov45be7932012-08-30 07:49:50 +00001018 Result = FileName;
1019 return true;
1020 }
Frederic Riss101b5e22014-09-19 15:11:51 +00001021
Alexey Samsonov45be7932012-08-30 07:49:50 +00001022 SmallString<16> FilePath;
1023 uint64_t IncludeDirIndex = Entry.DirIdx;
Paul Robinsonba1c9152017-05-02 17:37:32 +00001024 StringRef IncludeDir;
Alexey Samsonov45be7932012-08-30 07:49:50 +00001025 // Be defensive about the contents of Entry.
1026 if (IncludeDirIndex > 0 &&
Frederic Riss101b5e22014-09-19 15:11:51 +00001027 IncludeDirIndex <= Prologue.IncludeDirectories.size())
Paul Robinson0a227092018-02-05 20:43:15 +00001028 IncludeDir = Prologue.IncludeDirectories[IncludeDirIndex - 1]
1029 .getAsCString()
1030 .getValue();
Frederic Riss101b5e22014-09-19 15:11:51 +00001031
1032 // We may still need to append compilation directory of compile unit.
1033 // We know that FileName is not absolute, the only way to have an
1034 // absolute path at this point would be if IncludeDir is absolute.
Fangrui Songcc1fec32019-04-29 08:15:13 +00001035 if (CompDir && !isPathAbsoluteOnWindowsOrPosix(IncludeDir))
Frederic Riss101b5e22014-09-19 15:11:51 +00001036 sys::path::append(FilePath, CompDir);
1037
1038 // sys::path::append skips empty strings.
1039 sys::path::append(FilePath, IncludeDir, FileName);
Alexey Samsonov45be7932012-08-30 07:49:50 +00001040 Result = FilePath.str();
1041 return true;
1042}
Frederic Riss101b5e22014-09-19 15:11:51 +00001043
Dehao Chen1b54fce2016-04-28 22:09:37 +00001044bool DWARFDebugLine::LineTable::getFileLineInfoForAddress(
Alexey Lapshin77fc1f62019-02-27 13:17:36 +00001045 object::SectionedAddress Address, const char *CompDir,
1046 FileLineInfoKind Kind, DILineInfo &Result) const {
Frederic Riss101b5e22014-09-19 15:11:51 +00001047 // Get the index of row we're looking for in the line table.
1048 uint32_t RowIndex = lookupAddress(Address);
1049 if (RowIndex == -1U)
1050 return false;
1051 // Take file number and line/column from the row.
1052 const auto &Row = Rows[RowIndex];
1053 if (!getFileNameByIndex(Row.File, CompDir, Kind, Result.FileName))
1054 return false;
1055 Result.Line = Row.Line;
1056 Result.Column = Row.Column;
Eric Christopherba1024c2016-12-14 18:29:39 +00001057 Result.Discriminator = Row.Discriminator;
Scott Linder16c7bda2018-02-23 23:01:06 +00001058 Result.Source = getSourceByIndex(Row.File, Kind);
Frederic Riss101b5e22014-09-19 15:11:51 +00001059 return true;
1060}
James Hendersona3acf992018-05-10 10:51:33 +00001061
1062// We want to supply the Unit associated with a .debug_line[.dwo] table when
1063// we dump it, if possible, but still dump the table even if there isn't a Unit.
1064// Therefore, collect up handles on all the Units that point into the
1065// line-table section.
1066static DWARFDebugLine::SectionParser::LineToUnitMap
1067buildLineToUnitMap(DWARFDebugLine::SectionParser::cu_range CUs,
Paul Robinson7f330942018-08-01 20:46:46 +00001068 DWARFDebugLine::SectionParser::tu_range TUs) {
James Hendersona3acf992018-05-10 10:51:33 +00001069 DWARFDebugLine::SectionParser::LineToUnitMap LineToUnit;
1070 for (const auto &CU : CUs)
1071 if (auto CUDIE = CU->getUnitDIE())
1072 if (auto StmtOffset = toSectionOffset(CUDIE.find(DW_AT_stmt_list)))
1073 LineToUnit.insert(std::make_pair(*StmtOffset, &*CU));
Paul Robinson7f330942018-08-01 20:46:46 +00001074 for (const auto &TU : TUs)
1075 if (auto TUDIE = TU->getUnitDIE())
1076 if (auto StmtOffset = toSectionOffset(TUDIE.find(DW_AT_stmt_list)))
1077 LineToUnit.insert(std::make_pair(*StmtOffset, &*TU));
James Hendersona3acf992018-05-10 10:51:33 +00001078 return LineToUnit;
1079}
1080
1081DWARFDebugLine::SectionParser::SectionParser(DWARFDataExtractor &Data,
1082 const DWARFContext &C,
1083 cu_range CUs, tu_range TUs)
1084 : DebugLineData(Data), Context(C) {
1085 LineToUnit = buildLineToUnitMap(CUs, TUs);
1086 if (!DebugLineData.isValidOffset(Offset))
1087 Done = true;
1088}
1089
1090bool DWARFDebugLine::Prologue::totalLengthIsValid() const {
1091 return TotalLength == 0xffffffff || TotalLength < 0xffffff00;
1092}
1093
1094DWARFDebugLine::LineTable DWARFDebugLine::SectionParser::parseNext(
James Henderson004b7292018-05-21 15:30:54 +00001095 function_ref<void(Error)> RecoverableErrorCallback,
1096 function_ref<void(Error)> UnrecoverableErrorCallback, raw_ostream *OS) {
James Hendersona3acf992018-05-10 10:51:33 +00001097 assert(DebugLineData.isValidOffset(Offset) &&
1098 "parsing should have terminated");
1099 DWARFUnit *U = prepareToParse(Offset);
1100 uint32_t OldOffset = Offset;
1101 LineTable LT;
James Henderson004b7292018-05-21 15:30:54 +00001102 if (Error Err = LT.parse(DebugLineData, &Offset, Context, U,
1103 RecoverableErrorCallback, OS))
1104 UnrecoverableErrorCallback(std::move(Err));
James Hendersona3acf992018-05-10 10:51:33 +00001105 moveToNextTable(OldOffset, LT.Prologue);
1106 return LT;
1107}
1108
1109void DWARFDebugLine::SectionParser::skip(
1110 function_ref<void(Error)> ErrorCallback) {
1111 assert(DebugLineData.isValidOffset(Offset) &&
1112 "parsing should have terminated");
1113 DWARFUnit *U = prepareToParse(Offset);
1114 uint32_t OldOffset = Offset;
1115 LineTable LT;
James Henderson004b7292018-05-21 15:30:54 +00001116 if (Error Err = LT.Prologue.parse(DebugLineData, &Offset, Context, U))
1117 ErrorCallback(std::move(Err));
James Hendersona3acf992018-05-10 10:51:33 +00001118 moveToNextTable(OldOffset, LT.Prologue);
1119}
1120
1121DWARFUnit *DWARFDebugLine::SectionParser::prepareToParse(uint32_t Offset) {
1122 DWARFUnit *U = nullptr;
1123 auto It = LineToUnit.find(Offset);
1124 if (It != LineToUnit.end())
1125 U = It->second;
1126 DebugLineData.setAddressSize(U ? U->getAddressByteSize() : 0);
1127 return U;
1128}
1129
1130void DWARFDebugLine::SectionParser::moveToNextTable(uint32_t OldOffset,
1131 const Prologue &P) {
1132 // If the length field is not valid, we don't know where the next table is, so
1133 // cannot continue to parse. Mark the parser as done, and leave the Offset
1134 // value as it currently is. This will be the end of the bad length field.
1135 if (!P.totalLengthIsValid()) {
1136 Done = true;
1137 return;
1138 }
1139
1140 Offset = OldOffset + P.TotalLength + P.sizeofTotalLength();
1141 if (!DebugLineData.isValidOffset(Offset)) {
1142 Done = true;
1143 }
1144}