blob: bc32b742394a0ebb834e1f2c9322d42bd9508d1f [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,
Igor Kudrinf26a70a2019-08-06 10:49:40 +0000159 uint64_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.
Jonas Devlieghere0e7ba062019-07-22 23:23:34 +0000190// Returns the descriptors, or an error if we did not find a path or ran off
191// the end of the prologue.
192static llvm::Expected<ContentDescriptors>
Igor Kudrinf26a70a2019-08-06 10:49:40 +0000193parseV5EntryFormat(const DWARFDataExtractor &DebugLineData, uint64_t *OffsetPtr,
Jonas Devlieghere0e7ba062019-07-22 23:23:34 +0000194 uint64_t EndPrologueOffset,
195 DWARFDebugLine::ContentTypeTracker *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)
Jonas Devlieghere0e7ba062019-07-22 23:23:34 +0000201 return createStringError(
202 errc::invalid_argument,
203 "failed to parse entry content descriptions at offset "
Igor Kudrinf26a70a2019-08-06 10:49:40 +0000204 "0x%8.8" PRIx64
Jonas Devlieghere0e7ba062019-07-22 23:23:34 +0000205 " because offset extends beyond the prologue end at offset "
206 "0x%8.8" PRIx64,
207 *OffsetPtr, EndPrologueOffset);
Paul Robinson2bc38732017-05-02 21:40:47 +0000208 ContentDescriptor Descriptor;
209 Descriptor.Type =
210 dwarf::LineNumberEntryFormat(DebugLineData.getULEB128(OffsetPtr));
211 Descriptor.Form = dwarf::Form(DebugLineData.getULEB128(OffsetPtr));
212 if (Descriptor.Type == dwarf::DW_LNCT_path)
213 HasPath = true;
Scott Linder16c7bda2018-02-23 23:01:06 +0000214 if (ContentTypes)
215 ContentTypes->trackContentType(Descriptor.Type);
Paul Robinson2bc38732017-05-02 21:40:47 +0000216 Descriptors.push_back(Descriptor);
217 }
Jonas Devlieghere0e7ba062019-07-22 23:23:34 +0000218
219 if (!HasPath)
220 return createStringError(errc::invalid_argument,
221 "failed to parse entry content descriptions"
222 " because no path was found");
223 return Descriptors;
Paul Robinson2bc38732017-05-02 21:40:47 +0000224}
225
Jonas Devlieghere0e7ba062019-07-22 23:23:34 +0000226static Error
Paul Robinson17536b92017-06-29 16:52:08 +0000227parseV5DirFileTables(const DWARFDataExtractor &DebugLineData,
Igor Kudrinf26a70a2019-08-06 10:49:40 +0000228 uint64_t *OffsetPtr, uint64_t EndPrologueOffset,
Pavel Labath322711f2018-03-14 09:39:54 +0000229 const dwarf::FormParams &FormParams,
230 const DWARFContext &Ctx, const DWARFUnit *U,
Scott Linder16c7bda2018-02-23 23:01:06 +0000231 DWARFDebugLine::ContentTypeTracker &ContentTypes,
Paul Robinson0a227092018-02-05 20:43:15 +0000232 std::vector<DWARFFormValue> &IncludeDirectories,
Paul Robinson2bc38732017-05-02 21:40:47 +0000233 std::vector<DWARFDebugLine::FileNameEntry> &FileNames) {
234 // Get the directory entry description.
Jonas Devlieghere0e7ba062019-07-22 23:23:34 +0000235 llvm::Expected<ContentDescriptors> DirDescriptors =
Paul Robinsona06f8dc2017-12-18 19:08:35 +0000236 parseV5EntryFormat(DebugLineData, OffsetPtr, EndPrologueOffset, nullptr);
Jonas Devlieghere0e7ba062019-07-22 23:23:34 +0000237 if (!DirDescriptors)
238 return DirDescriptors.takeError();
Paul Robinson2bc38732017-05-02 21:40:47 +0000239
240 // Get the directory entries, according to the format described above.
241 int DirEntryCount = DebugLineData.getU8(OffsetPtr);
242 for (int I = 0; I != DirEntryCount; ++I) {
243 if (*OffsetPtr >= EndPrologueOffset)
Jonas Devlieghere0e7ba062019-07-22 23:23:34 +0000244 return createStringError(
245 errc::invalid_argument,
246 "failed to parse directory entry at offset "
Igor Kudrinf26a70a2019-08-06 10:49:40 +0000247 "0x%8.8" PRIx64
Jonas Devlieghere0e7ba062019-07-22 23:23:34 +0000248 " because offset extends beyond the prologue end at offset "
249 "0x%8.8" PRIx64,
250 *OffsetPtr, EndPrologueOffset);
251 for (auto Descriptor : *DirDescriptors) {
Vlad Tsyrklevich53a9f1d2019-03-02 01:10:00 +0000252 DWARFFormValue Value(Descriptor.Form);
Paul Robinson2bc38732017-05-02 21:40:47 +0000253 switch (Descriptor.Type) {
254 case DW_LNCT_path:
Vlad Tsyrklevich53a9f1d2019-03-02 01:10:00 +0000255 if (!Value.extractValue(DebugLineData, OffsetPtr, FormParams, &Ctx, U))
Jonas Devlieghere0e7ba062019-07-22 23:23:34 +0000256 return createStringError(errc::invalid_argument,
257 "failed to parse directory entry because "
258 "extracting the form value failed.");
Vlad Tsyrklevich53a9f1d2019-03-02 01:10:00 +0000259 IncludeDirectories.push_back(Value);
Paul Robinson2bc38732017-05-02 21:40:47 +0000260 break;
261 default:
Vlad Tsyrklevich53a9f1d2019-03-02 01:10:00 +0000262 if (!Value.skipValue(DebugLineData, OffsetPtr, FormParams))
Jonas Devlieghere0e7ba062019-07-22 23:23:34 +0000263 return createStringError(errc::invalid_argument,
264 "failed to parse directory entry because "
265 "skipping the form value failed.");
Paul Robinson2bc38732017-05-02 21:40:47 +0000266 }
267 }
268 }
269
270 // Get the file entry description.
Jonas Devlieghere0e7ba062019-07-22 23:23:34 +0000271 llvm::Expected<ContentDescriptors> FileDescriptors = parseV5EntryFormat(
272 DebugLineData, OffsetPtr, EndPrologueOffset, &ContentTypes);
273 if (!FileDescriptors)
274 return FileDescriptors.takeError();
Paul Robinson2bc38732017-05-02 21:40:47 +0000275
276 // Get the file entries, according to the format described above.
277 int FileEntryCount = DebugLineData.getU8(OffsetPtr);
278 for (int I = 0; I != FileEntryCount; ++I) {
279 if (*OffsetPtr >= EndPrologueOffset)
Jonas Devlieghere0e7ba062019-07-22 23:23:34 +0000280 return createStringError(
281 errc::invalid_argument,
282 "failed to parse file entry at offset "
Igor Kudrinf26a70a2019-08-06 10:49:40 +0000283 "0x%8.8" PRIx64
Jonas Devlieghere0e7ba062019-07-22 23:23:34 +0000284 " because offset extends beyond the prologue end at offset "
285 "0x%8.8" PRIx64,
286 *OffsetPtr, EndPrologueOffset);
Paul Robinson2bc38732017-05-02 21:40:47 +0000287 DWARFDebugLine::FileNameEntry FileEntry;
Jonas Devlieghere0e7ba062019-07-22 23:23:34 +0000288 for (auto Descriptor : *FileDescriptors) {
Vlad Tsyrklevich53a9f1d2019-03-02 01:10:00 +0000289 DWARFFormValue Value(Descriptor.Form);
290 if (!Value.extractValue(DebugLineData, OffsetPtr, FormParams, &Ctx, U))
Jonas Devlieghere0e7ba062019-07-22 23:23:34 +0000291 return createStringError(errc::invalid_argument,
292 "failed to parse file entry because "
293 "extracting the form value failed.");
Paul Robinson2bc38732017-05-02 21:40:47 +0000294 switch (Descriptor.Type) {
295 case DW_LNCT_path:
Paul Robinson0a227092018-02-05 20:43:15 +0000296 FileEntry.Name = Value;
Paul Robinson2bc38732017-05-02 21:40:47 +0000297 break;
Scott Linder16c7bda2018-02-23 23:01:06 +0000298 case DW_LNCT_LLVM_source:
299 FileEntry.Source = Value;
300 break;
Paul Robinson2bc38732017-05-02 21:40:47 +0000301 case DW_LNCT_directory_index:
302 FileEntry.DirIdx = Value.getAsUnsignedConstant().getValue();
303 break;
304 case DW_LNCT_timestamp:
305 FileEntry.ModTime = Value.getAsUnsignedConstant().getValue();
306 break;
307 case DW_LNCT_size:
308 FileEntry.Length = Value.getAsUnsignedConstant().getValue();
309 break;
Paul Robinsona06f8dc2017-12-18 19:08:35 +0000310 case DW_LNCT_MD5:
Jonas Devlieghere0e7ba062019-07-22 23:23:34 +0000311 if (!Value.getAsBlock() || Value.getAsBlock().getValue().size() != 16)
312 return createStringError(
313 errc::invalid_argument,
314 "failed to parse file entry because the MD5 hash is invalid");
Paul Robinsona06f8dc2017-12-18 19:08:35 +0000315 std::uninitialized_copy_n(Value.getAsBlock().getValue().begin(), 16,
316 FileEntry.Checksum.Bytes.begin());
317 break;
Paul Robinson2bc38732017-05-02 21:40:47 +0000318 default:
319 break;
320 }
321 }
322 FileNames.push_back(FileEntry);
323 }
Jonas Devlieghere0e7ba062019-07-22 23:23:34 +0000324 return Error::success();
Paul Robinson2bc38732017-05-02 21:40:47 +0000325}
326
James Hendersona3acf992018-05-10 10:51:33 +0000327Error DWARFDebugLine::Prologue::parse(const DWARFDataExtractor &DebugLineData,
Igor Kudrinf26a70a2019-08-06 10:49:40 +0000328 uint64_t *OffsetPtr,
James Hendersona3acf992018-05-10 10:51:33 +0000329 const DWARFContext &Ctx,
330 const DWARFUnit *U) {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000331 const uint64_t PrologueOffset = *OffsetPtr;
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000332
333 clear();
Alex Bradbury44deaf72019-07-18 05:22:55 +0000334 TotalLength = DebugLineData.getRelocatedValue(4, OffsetPtr);
Igor Kudrin3daefb02019-07-24 11:34:29 +0000335 if (TotalLength == dwarf::DW_LENGTH_DWARF64) {
Paul Robinson75c068c2017-06-26 18:43:01 +0000336 FormParams.Format = dwarf::DWARF64;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000337 TotalLength = DebugLineData.getU64(OffsetPtr);
Igor Kudrin3daefb02019-07-24 11:34:29 +0000338 } else if (TotalLength >= dwarf::DW_LENGTH_lo_reserved) {
Victor Leschukcba595d2018-08-20 09:59:08 +0000339 return createStringError(errc::invalid_argument,
James Hendersona3acf992018-05-10 10:51:33 +0000340 "parsing line table prologue at offset 0x%8.8" PRIx64
341 " unsupported reserved unit length found of value 0x%8.8" PRIx64,
342 PrologueOffset, TotalLength);
Ed Maste6d0bee52015-05-28 15:38:17 +0000343 }
Paul Robinson75c068c2017-06-26 18:43:01 +0000344 FormParams.Version = DebugLineData.getU16(OffsetPtr);
345 if (getVersion() < 2)
Victor Leschukcba595d2018-08-20 09:59:08 +0000346 return createStringError(errc::not_supported,
347 "parsing line table prologue at offset 0x%8.8" PRIx64
James Hendersona3acf992018-05-10 10:51:33 +0000348 " found unsupported version 0x%2.2" PRIx16,
349 PrologueOffset, getVersion());
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000350
Paul Robinson75c068c2017-06-26 18:43:01 +0000351 if (getVersion() >= 5) {
352 FormParams.AddrSize = DebugLineData.getU8(OffsetPtr);
Paul Robinson63811a42017-11-22 15:33:17 +0000353 assert((DebugLineData.getAddressSize() == 0 ||
354 DebugLineData.getAddressSize() == getAddressSize()) &&
Paul Robinson75c068c2017-06-26 18:43:01 +0000355 "Line table header and data extractor disagree");
Paul Robinson2bc38732017-05-02 21:40:47 +0000356 SegSelectorSize = DebugLineData.getU8(OffsetPtr);
357 }
358
Alex Bradbury44deaf72019-07-18 05:22:55 +0000359 PrologueLength =
360 DebugLineData.getRelocatedValue(sizeofPrologueLength(), OffsetPtr);
Paul Robinson9d4eb692017-05-01 23:27:55 +0000361 const uint64_t EndPrologueOffset = PrologueLength + *OffsetPtr;
362 MinInstLength = DebugLineData.getU8(OffsetPtr);
Paul Robinson75c068c2017-06-26 18:43:01 +0000363 if (getVersion() >= 4)
Paul Robinson9d4eb692017-05-01 23:27:55 +0000364 MaxOpsPerInst = DebugLineData.getU8(OffsetPtr);
365 DefaultIsStmt = DebugLineData.getU8(OffsetPtr);
366 LineBase = DebugLineData.getU8(OffsetPtr);
367 LineRange = DebugLineData.getU8(OffsetPtr);
368 OpcodeBase = DebugLineData.getU8(OffsetPtr);
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000369
370 StandardOpcodeLengths.reserve(OpcodeBase - 1);
Paul Robinson9d4eb692017-05-01 23:27:55 +0000371 for (uint32_t I = 1; I < OpcodeBase; ++I) {
372 uint8_t OpLen = DebugLineData.getU8(OffsetPtr);
373 StandardOpcodeLengths.push_back(OpLen);
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000374 }
375
Paul Robinson75c068c2017-06-26 18:43:01 +0000376 if (getVersion() >= 5) {
Jonas Devlieghere0e7ba062019-07-22 23:23:34 +0000377 if (Error e = parseV5DirFileTables(
378 DebugLineData, OffsetPtr, EndPrologueOffset, FormParams, Ctx, U,
379 ContentTypes, IncludeDirectories, FileNames)) {
380 return joinErrors(
381 createStringError(
382 errc::invalid_argument,
383 "parsing line table prologue at 0x%8.8" PRIx64
384 " found an invalid directory or file table description at"
Igor Kudrinf26a70a2019-08-06 10:49:40 +0000385 " 0x%8.8" PRIx64,
Jonas Devliegheref8552e62019-07-23 22:34:21 +0000386 PrologueOffset, *OffsetPtr),
Jonas Devlieghere0e7ba062019-07-22 23:23:34 +0000387 std::move(e));
Paul Robinson2bc38732017-05-02 21:40:47 +0000388 }
389 } else
390 parseV2DirFileTables(DebugLineData, OffsetPtr, EndPrologueOffset,
Scott Linder16c7bda2018-02-23 23:01:06 +0000391 ContentTypes, IncludeDirectories, FileNames);
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000392
James Hendersona3acf992018-05-10 10:51:33 +0000393 if (*OffsetPtr != EndPrologueOffset)
Victor Leschukcba595d2018-08-20 09:59:08 +0000394 return createStringError(errc::invalid_argument,
395 "parsing line table prologue at 0x%8.8" PRIx64
James Hendersona3acf992018-05-10 10:51:33 +0000396 " should have ended at 0x%8.8" PRIx64
Igor Kudrinf26a70a2019-08-06 10:49:40 +0000397 " but it ended at 0x%8.8" PRIx64,
Jonas Devliegheref8552e62019-07-23 22:34:21 +0000398 PrologueOffset, EndPrologueOffset, *OffsetPtr);
James Hendersona3acf992018-05-10 10:51:33 +0000399 return Error::success();
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000400}
401
Paul Robinson9d4eb692017-05-01 23:27:55 +0000402DWARFDebugLine::Row::Row(bool DefaultIsStmt) { reset(DefaultIsStmt); }
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000403
Benjamin Kramer5acab502011-09-15 02:12:05 +0000404void DWARFDebugLine::Row::postAppend() {
Fangrui Song6a285df2019-04-11 02:02:44 +0000405 Discriminator = 0;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000406 BasicBlock = false;
407 PrologueEnd = false;
408 EpilogueBegin = false;
409}
410
Paul Robinson9d4eb692017-05-01 23:27:55 +0000411void DWARFDebugLine::Row::reset(bool DefaultIsStmt) {
Alexey Lapshin77fc1f62019-02-27 13:17:36 +0000412 Address.Address = 0;
413 Address.SectionIndex = object::SectionedAddress::UndefSection;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000414 Line = 1;
415 Column = 0;
416 File = 1;
417 Isa = 0;
Diego Novillo5b5cf502014-02-14 19:27:53 +0000418 Discriminator = 0;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000419 IsStmt = DefaultIsStmt;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000420 BasicBlock = false;
421 EndSequence = false;
422 PrologueEnd = false;
423 EpilogueBegin = false;
424}
425
Greg Clayton67070462017-05-02 22:48:52 +0000426void DWARFDebugLine::Row::dumpTableHeader(raw_ostream &OS) {
427 OS << "Address Line Column File ISA Discriminator Flags\n"
428 << "------------------ ------ ------ ------ --- ------------- "
429 "-------------\n";
430}
431
Benjamin Kramer5acab502011-09-15 02:12:05 +0000432void DWARFDebugLine::Row::dump(raw_ostream &OS) const {
Alexey Lapshin77fc1f62019-02-27 13:17:36 +0000433 OS << format("0x%16.16" PRIx64 " %6u %6u", Address.Address, Line, Column)
Diego Novillo5b5cf502014-02-14 19:27:53 +0000434 << format(" %6u %3u %13u ", File, Isa, Discriminator)
Dehao Chen1b54fce2016-04-28 22:09:37 +0000435 << (IsStmt ? " is_stmt" : "") << (BasicBlock ? " basic_block" : "")
Benjamin Kramer5acab502011-09-15 02:12:05 +0000436 << (PrologueEnd ? " prologue_end" : "")
437 << (EpilogueBegin ? " epilogue_begin" : "")
Dehao Chen1b54fce2016-04-28 22:09:37 +0000438 << (EndSequence ? " end_sequence" : "") << '\n';
Benjamin Kramer5acab502011-09-15 02:12:05 +0000439}
440
Dehao Chen1b54fce2016-04-28 22:09:37 +0000441DWARFDebugLine::Sequence::Sequence() { reset(); }
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000442
443void DWARFDebugLine::Sequence::reset() {
444 LowPC = 0;
445 HighPC = 0;
Alexey Lapshin77fc1f62019-02-27 13:17:36 +0000446 SectionIndex = object::SectionedAddress::UndefSection;
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000447 FirstRowIndex = 0;
448 LastRowIndex = 0;
449 Empty = true;
450}
451
Dehao Chen1b54fce2016-04-28 22:09:37 +0000452DWARFDebugLine::LineTable::LineTable() { clear(); }
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000453
Paul Robinson0a227092018-02-05 20:43:15 +0000454void DWARFDebugLine::LineTable::dump(raw_ostream &OS,
455 DIDumpOptions DumpOptions) const {
456 Prologue.dump(OS, DumpOptions);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000457 OS << '\n';
458
459 if (!Rows.empty()) {
Greg Clayton67070462017-05-02 22:48:52 +0000460 Row::dumpTableHeader(OS);
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000461 for (const Row &R : Rows) {
462 R.dump(OS);
463 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000464 }
465}
466
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000467void DWARFDebugLine::LineTable::clear() {
468 Prologue.clear();
469 Rows.clear();
470 Sequences.clear();
471}
472
Alexey Samsonov110d5952014-04-30 00:09:19 +0000473DWARFDebugLine::ParsingState::ParsingState(struct LineTable *LT)
Eugene Zelenko2db0cfa2017-06-23 21:57:40 +0000474 : LineTable(LT) {
Alexey Samsonov110d5952014-04-30 00:09:19 +0000475 resetRowAndSequence();
476}
Nick Lewycky4d044922011-09-15 03:41:51 +0000477
Alexey Samsonov110d5952014-04-30 00:09:19 +0000478void DWARFDebugLine::ParsingState::resetRowAndSequence() {
479 Row.reset(LineTable->Prologue.DefaultIsStmt);
480 Sequence.reset();
481}
482
Fangrui Songc4c8bca2019-04-07 13:56:14 +0000483void DWARFDebugLine::ParsingState::appendRowToMatrix() {
Fangrui Song50a09672019-04-15 07:40:30 +0000484 unsigned RowNumber = LineTable->Rows.size();
Alexey Samsonov110d5952014-04-30 00:09:19 +0000485 if (Sequence.Empty) {
Alexey Samsonov947228c2012-08-07 11:46:57 +0000486 // Record the beginning of instruction sequence.
Alexey Samsonov110d5952014-04-30 00:09:19 +0000487 Sequence.Empty = false;
Alexey Lapshin77fc1f62019-02-27 13:17:36 +0000488 Sequence.LowPC = Row.Address.Address;
Alexey Samsonov110d5952014-04-30 00:09:19 +0000489 Sequence.FirstRowIndex = RowNumber;
Alexey Samsonov947228c2012-08-07 11:46:57 +0000490 }
Alexey Samsonov110d5952014-04-30 00:09:19 +0000491 LineTable->appendRow(Row);
492 if (Row.EndSequence) {
Alexey Samsonov947228c2012-08-07 11:46:57 +0000493 // Record the end of instruction sequence.
Alexey Lapshin77fc1f62019-02-27 13:17:36 +0000494 Sequence.HighPC = Row.Address.Address;
Fangrui Song50a09672019-04-15 07:40:30 +0000495 Sequence.LastRowIndex = RowNumber + 1;
Alexey Lapshin77fc1f62019-02-27 13:17:36 +0000496 Sequence.SectionIndex = Row.Address.SectionIndex;
Alexey Samsonov110d5952014-04-30 00:09:19 +0000497 if (Sequence.isValid())
498 LineTable->appendSequence(Sequence);
499 Sequence.reset();
Alexey Samsonov947228c2012-08-07 11:46:57 +0000500 }
Alexey Samsonov110d5952014-04-30 00:09:19 +0000501 Row.postAppend();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000502}
503
Benjamin Kramer5acab502011-09-15 02:12:05 +0000504const DWARFDebugLine::LineTable *
Igor Kudrinf26a70a2019-08-06 10:49:40 +0000505DWARFDebugLine::getLineTable(uint64_t Offset) const {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000506 LineTableConstIter Pos = LineTableMap.find(Offset);
507 if (Pos != LineTableMap.end())
508 return &Pos->second;
Craig Topper2617dcc2014-04-15 06:32:26 +0000509 return nullptr;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000510}
511
James Hendersona3acf992018-05-10 10:51:33 +0000512Expected<const DWARFDebugLine::LineTable *> DWARFDebugLine::getOrParseLineTable(
Igor Kudrinf26a70a2019-08-06 10:49:40 +0000513 DWARFDataExtractor &DebugLineData, uint64_t Offset, const DWARFContext &Ctx,
James Henderson004b7292018-05-21 15:30:54 +0000514 const DWARFUnit *U, std::function<void(Error)> RecoverableErrorCallback) {
James Henderson66702622018-03-08 10:53:34 +0000515 if (!DebugLineData.isValidOffset(Offset))
Igor Kudrinf26a70a2019-08-06 10:49:40 +0000516 return createStringError(errc::invalid_argument, "offset 0x%8.8" PRIx64
James Hendersona3acf992018-05-10 10:51:33 +0000517 " is not a valid debug line section offset",
518 Offset);
James Henderson66702622018-03-08 10:53:34 +0000519
Paul Robinson9d4eb692017-05-01 23:27:55 +0000520 std::pair<LineTableIter, bool> Pos =
521 LineTableMap.insert(LineTableMapTy::value_type(Offset, LineTable()));
522 LineTable *LT = &Pos.first->second;
523 if (Pos.second) {
James Henderson004b7292018-05-21 15:30:54 +0000524 if (Error Err =
525 LT->parse(DebugLineData, &Offset, Ctx, U, RecoverableErrorCallback))
James Hendersona3acf992018-05-10 10:51:33 +0000526 return std::move(Err);
527 return LT;
Benjamin Kramer679e1752011-09-15 20:43:18 +0000528 }
Alexey Samsonov110d5952014-04-30 00:09:19 +0000529 return LT;
Benjamin Kramer679e1752011-09-15 20:43:18 +0000530}
531
James Hendersona3acf992018-05-10 10:51:33 +0000532Error DWARFDebugLine::LineTable::parse(
Igor Kudrinf26a70a2019-08-06 10:49:40 +0000533 DWARFDataExtractor &DebugLineData, uint64_t *OffsetPtr,
James Hendersona3acf992018-05-10 10:51:33 +0000534 const DWARFContext &Ctx, const DWARFUnit *U,
James Henderson004b7292018-05-21 15:30:54 +0000535 std::function<void(Error)> RecoverableErrorCallback, raw_ostream *OS) {
Igor Kudrinf26a70a2019-08-06 10:49:40 +0000536 const uint64_t DebugLineOffset = *OffsetPtr;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000537
Alexey Samsonov110d5952014-04-30 00:09:19 +0000538 clear();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000539
James Hendersona3acf992018-05-10 10:51:33 +0000540 Error PrologueErr = Prologue.parse(DebugLineData, OffsetPtr, Ctx, U);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000541
Paul Robinson0a227092018-02-05 20:43:15 +0000542 if (OS) {
543 // The presence of OS signals verbose dumping.
544 DIDumpOptions DumpOptions;
545 DumpOptions.Verbose = true;
546 Prologue.dump(*OS, DumpOptions);
547 }
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000548
James Hendersona3acf992018-05-10 10:51:33 +0000549 if (PrologueErr)
550 return PrologueErr;
551
Igor Kudrinf26a70a2019-08-06 10:49:40 +0000552 const uint64_t EndOffset =
Paul Robinson9d4eb692017-05-01 23:27:55 +0000553 DebugLineOffset + Prologue.TotalLength + Prologue.sizeofTotalLength();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000554
Paul Robinson511b54c2017-11-22 15:48:30 +0000555 // See if we should tell the data extractor the address size.
556 if (DebugLineData.getAddressSize() == 0)
557 DebugLineData.setAddressSize(Prologue.getAddressSize());
558 else
559 assert(Prologue.getAddressSize() == 0 ||
560 Prologue.getAddressSize() == DebugLineData.getAddressSize());
561
Alexey Samsonov110d5952014-04-30 00:09:19 +0000562 ParsingState State(this);
Benjamin Kramer112ec172011-09-15 21:59:13 +0000563
Paul Robinson9d4eb692017-05-01 23:27:55 +0000564 while (*OffsetPtr < EndOffset) {
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000565 if (OS)
Igor Kudrinf26a70a2019-08-06 10:49:40 +0000566 *OS << format("0x%08.08" PRIx64 ": ", *OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000567
Paul Robinson9d4eb692017-05-01 23:27:55 +0000568 uint8_t Opcode = DebugLineData.getU8(OffsetPtr);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000569
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000570 if (OS)
571 *OS << format("%02.02" PRIx8 " ", Opcode);
572
Paul Robinson9d4eb692017-05-01 23:27:55 +0000573 if (Opcode == 0) {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000574 // Extended Opcodes always start with a zero opcode followed by
575 // a uleb128 length so you can skip ones you don't know about
Paul Robinson9d4eb692017-05-01 23:27:55 +0000576 uint64_t Len = DebugLineData.getULEB128(OffsetPtr);
Igor Kudrinf26a70a2019-08-06 10:49:40 +0000577 uint64_t ExtOffset = *OffsetPtr;
Paul Robinsone0833342017-11-22 15:14:49 +0000578
579 // Tolerate zero-length; assume length is correct and soldier on.
580 if (Len == 0) {
581 if (OS)
582 *OS << "Badly formed extended line op (length 0)\n";
583 continue;
584 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000585
Paul Robinson9d4eb692017-05-01 23:27:55 +0000586 uint8_t SubOpcode = DebugLineData.getU8(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000587 if (OS)
588 *OS << LNExtendedString(SubOpcode);
Paul Robinson9d4eb692017-05-01 23:27:55 +0000589 switch (SubOpcode) {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000590 case DW_LNE_end_sequence:
591 // Set the end_sequence register of the state machine to true and
592 // append a row to the matrix using the current values of the
593 // state-machine registers. Then reset the registers to the initial
594 // values specified above. Every statement program sequence must end
595 // with a DW_LNE_end_sequence instruction which creates a row whose
596 // address is that of the byte after the last target machine instruction
597 // of the sequence.
Alexey Samsonov110d5952014-04-30 00:09:19 +0000598 State.Row.EndSequence = true;
Fangrui Songc4c8bca2019-04-07 13:56:14 +0000599 State.appendRowToMatrix();
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000600 if (OS) {
601 *OS << "\n";
602 OS->indent(12);
603 State.Row.dump(*OS);
604 }
Alexey Samsonov110d5952014-04-30 00:09:19 +0000605 State.resetRowAndSequence();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000606 break;
607
608 case DW_LNE_set_address:
609 // Takes a single relocatable address as an operand. The size of the
610 // operand is the size appropriate to hold an address on the target
611 // machine. Set the address register to the value given by the
612 // relocatable address. All of the other statement program opcodes
613 // that affect the address register add a delta to it. This instruction
614 // stores a relocatable value into it instead.
Paul Robinson511b54c2017-11-22 15:48:30 +0000615 //
616 // Make sure the extractor knows the address size. If not, infer it
617 // from the size of the operand.
618 if (DebugLineData.getAddressSize() == 0)
619 DebugLineData.setAddressSize(Len - 1);
Paul Robinson79474682018-03-22 19:37:56 +0000620 else if (DebugLineData.getAddressSize() != Len - 1) {
Victor Leschukcba595d2018-08-20 09:59:08 +0000621 return createStringError(errc::invalid_argument,
Igor Kudrinf26a70a2019-08-06 10:49:40 +0000622 "mismatching address size at offset 0x%8.8" PRIx64
James Hendersona3acf992018-05-10 10:51:33 +0000623 " expected 0x%2.2" PRIx8 " found 0x%2.2" PRIx64,
624 ExtOffset, DebugLineData.getAddressSize(),
625 Len - 1);
Paul Robinson79474682018-03-22 19:37:56 +0000626 }
Alexey Lapshin77fc1f62019-02-27 13:17:36 +0000627 State.Row.Address.Address = DebugLineData.getRelocatedAddress(
628 OffsetPtr, &State.Row.Address.SectionIndex);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000629 if (OS)
Alexey Lapshin77fc1f62019-02-27 13:17:36 +0000630 *OS << format(" (0x%16.16" PRIx64 ")", State.Row.Address.Address);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000631 break;
632
633 case DW_LNE_define_file:
634 // Takes 4 arguments. The first is a null terminated string containing
635 // a source file name. The second is an unsigned LEB128 number
636 // representing the directory index of the directory in which the file
637 // was found. The third is an unsigned LEB128 number representing the
638 // time of last modification of the file. The fourth is an unsigned
639 // LEB128 number representing the length in bytes of the file. The time
640 // and length fields may contain LEB128(0) if the information is not
641 // available.
642 //
643 // The directory index represents an entry in the include_directories
644 // section of the statement program prologue. The index is LEB128(0)
645 // if the file was found in the current directory of the compilation,
646 // LEB128(1) if it was found in the first directory in the
647 // include_directories section, and so on. The directory index is
648 // ignored for file names that represent full path names.
649 //
650 // The files are numbered, starting at 1, in the order in which they
651 // appear; the names in the prologue come before names defined by
652 // the DW_LNE_define_file instruction. These numbers are used in the
653 // the file register of the state machine.
654 {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000655 FileNameEntry FileEntry;
Paul Robinson0a227092018-02-05 20:43:15 +0000656 const char *Name = DebugLineData.getCStr(OffsetPtr);
Jonas Devliegherebb111152019-02-27 00:58:09 +0000657 FileEntry.Name =
658 DWARFFormValue::createFromPValue(dwarf::DW_FORM_string, Name);
Paul Robinson9d4eb692017-05-01 23:27:55 +0000659 FileEntry.DirIdx = DebugLineData.getULEB128(OffsetPtr);
660 FileEntry.ModTime = DebugLineData.getULEB128(OffsetPtr);
661 FileEntry.Length = DebugLineData.getULEB128(OffsetPtr);
662 Prologue.FileNames.push_back(FileEntry);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000663 if (OS)
Paul Robinson0a227092018-02-05 20:43:15 +0000664 *OS << " (" << Name << ", dir=" << FileEntry.DirIdx << ", mod_time="
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000665 << format("(0x%16.16" PRIx64 ")", FileEntry.ModTime)
666 << ", length=" << FileEntry.Length << ")";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000667 }
668 break;
669
Diego Novillo5b5cf502014-02-14 19:27:53 +0000670 case DW_LNE_set_discriminator:
Paul Robinson9d4eb692017-05-01 23:27:55 +0000671 State.Row.Discriminator = DebugLineData.getULEB128(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000672 if (OS)
673 *OS << " (" << State.Row.Discriminator << ")";
Diego Novillo5b5cf502014-02-14 19:27:53 +0000674 break;
675
Benjamin Kramer5acab502011-09-15 02:12:05 +0000676 default:
Paul Robinsone0833342017-11-22 15:14:49 +0000677 if (OS)
678 *OS << format("Unrecognized extended op 0x%02.02" PRIx8, SubOpcode)
679 << format(" length %" PRIx64, Len);
680 // Len doesn't include the zero opcode byte or the length itself, but
681 // it does include the sub_opcode, so we have to adjust for that.
682 (*OffsetPtr) += Len - 1;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000683 break;
684 }
Paul Robinsone0833342017-11-22 15:14:49 +0000685 // Make sure the stated and parsed lengths are the same.
686 // Otherwise we have an unparseable line-number program.
James Hendersona3acf992018-05-10 10:51:33 +0000687 if (*OffsetPtr - ExtOffset != Len)
Victor Leschukcba595d2018-08-20 09:59:08 +0000688 return createStringError(errc::illegal_byte_sequence,
Igor Kudrinf26a70a2019-08-06 10:49:40 +0000689 "unexpected line op length at offset 0x%8.8" PRIx64
690 " expected 0x%2.2" PRIx64 " found 0x%2.2" PRIx64,
James Hendersona3acf992018-05-10 10:51:33 +0000691 ExtOffset, Len, *OffsetPtr - ExtOffset);
Paul Robinson9d4eb692017-05-01 23:27:55 +0000692 } else if (Opcode < Prologue.OpcodeBase) {
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000693 if (OS)
694 *OS << LNStandardString(Opcode);
Paul Robinson9d4eb692017-05-01 23:27:55 +0000695 switch (Opcode) {
Benjamin Kramer5acab502011-09-15 02:12:05 +0000696 // Standard Opcodes
697 case DW_LNS_copy:
698 // Takes no arguments. Append a row to the matrix using the
Fangrui Song6a285df2019-04-11 02:02:44 +0000699 // current values of the state-machine registers.
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000700 if (OS) {
701 *OS << "\n";
702 OS->indent(12);
703 State.Row.dump(*OS);
704 *OS << "\n";
705 }
Fangrui Song6a285df2019-04-11 02:02:44 +0000706 State.appendRowToMatrix();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000707 break;
708
709 case DW_LNS_advance_pc:
710 // Takes a single unsigned LEB128 operand, multiplies it by the
711 // min_inst_length field of the prologue, and adds the
712 // result to the address register of the state machine.
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000713 {
714 uint64_t AddrOffset =
715 DebugLineData.getULEB128(OffsetPtr) * Prologue.MinInstLength;
Alexey Lapshin77fc1f62019-02-27 13:17:36 +0000716 State.Row.Address.Address += AddrOffset;
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000717 if (OS)
718 *OS << " (" << AddrOffset << ")";
719 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000720 break;
721
722 case DW_LNS_advance_line:
723 // Takes a single signed LEB128 operand and adds that value to
724 // the line register of the state machine.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000725 State.Row.Line += DebugLineData.getSLEB128(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000726 if (OS)
727 *OS << " (" << State.Row.Line << ")";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000728 break;
729
730 case DW_LNS_set_file:
731 // Takes a single unsigned LEB128 operand and stores it in the file
732 // register of the state machine.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000733 State.Row.File = DebugLineData.getULEB128(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000734 if (OS)
735 *OS << " (" << State.Row.File << ")";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000736 break;
737
738 case DW_LNS_set_column:
739 // Takes a single unsigned LEB128 operand and stores it in the
740 // column register of the state machine.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000741 State.Row.Column = DebugLineData.getULEB128(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000742 if (OS)
743 *OS << " (" << State.Row.Column << ")";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000744 break;
745
746 case DW_LNS_negate_stmt:
747 // Takes no arguments. Set the is_stmt register of the state
748 // machine to the logical negation of its current value.
Alexey Samsonov110d5952014-04-30 00:09:19 +0000749 State.Row.IsStmt = !State.Row.IsStmt;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000750 break;
751
752 case DW_LNS_set_basic_block:
753 // Takes no arguments. Set the basic_block register of the
754 // state machine to true
Alexey Samsonov110d5952014-04-30 00:09:19 +0000755 State.Row.BasicBlock = true;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000756 break;
757
758 case DW_LNS_const_add_pc:
759 // Takes no arguments. Add to the address register of the state
760 // machine the address increment value corresponding to special
761 // opcode 255. The motivation for DW_LNS_const_add_pc is this:
762 // when the statement program needs to advance the address by a
763 // small amount, it can use a single special opcode, which occupies
764 // a single byte. When it needs to advance the address by up to
765 // twice the range of the last special opcode, it can use
766 // DW_LNS_const_add_pc followed by a special opcode, for a total
767 // of two bytes. Only if it needs to advance the address by more
768 // than twice that range will it need to use both DW_LNS_advance_pc
769 // and a special opcode, requiring three or more bytes.
770 {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000771 uint8_t AdjustOpcode = 255 - Prologue.OpcodeBase;
772 uint64_t AddrOffset =
773 (AdjustOpcode / Prologue.LineRange) * Prologue.MinInstLength;
Alexey Lapshin77fc1f62019-02-27 13:17:36 +0000774 State.Row.Address.Address += AddrOffset;
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000775 if (OS)
776 *OS
777 << format(" (0x%16.16" PRIx64 ")", AddrOffset);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000778 }
779 break;
780
781 case DW_LNS_fixed_advance_pc:
782 // Takes a single uhalf operand. Add to the address register of
783 // the state machine the value of the (unencoded) operand. This
784 // is the only extended opcode that takes an argument that is not
785 // a variable length number. The motivation for DW_LNS_fixed_advance_pc
786 // is this: existing assemblers cannot emit DW_LNS_advance_pc or
787 // special opcodes because they cannot encode LEB128 numbers or
788 // judge when the computation of a special opcode overflows and
789 // requires the use of DW_LNS_advance_pc. Such assemblers, however,
790 // can use DW_LNS_fixed_advance_pc instead, sacrificing compression.
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000791 {
Alex Bradbury44deaf72019-07-18 05:22:55 +0000792 uint16_t PCOffset = DebugLineData.getRelocatedValue(2, OffsetPtr);
Alexey Lapshin77fc1f62019-02-27 13:17:36 +0000793 State.Row.Address.Address += PCOffset;
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000794 if (OS)
795 *OS
Igor Kudrin74c350a2019-07-16 06:56:10 +0000796 << format(" (0x%4.4" PRIx16 ")", PCOffset);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000797 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000798 break;
799
800 case DW_LNS_set_prologue_end:
801 // Takes no arguments. Set the prologue_end register of the
802 // state machine to true
Alexey Samsonov110d5952014-04-30 00:09:19 +0000803 State.Row.PrologueEnd = true;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000804 break;
805
806 case DW_LNS_set_epilogue_begin:
807 // Takes no arguments. Set the basic_block register of the
808 // state machine to true
Alexey Samsonov110d5952014-04-30 00:09:19 +0000809 State.Row.EpilogueBegin = true;
Benjamin Kramer5acab502011-09-15 02:12:05 +0000810 break;
811
812 case DW_LNS_set_isa:
813 // Takes a single unsigned LEB128 operand and stores it in the
814 // column register of the state machine.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000815 State.Row.Isa = DebugLineData.getULEB128(OffsetPtr);
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000816 if (OS)
817 *OS << " (" << State.Row.Isa << ")";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000818 break;
819
820 default:
821 // Handle any unknown standard opcodes here. We know the lengths
822 // of such opcodes because they are specified in the prologue
823 // as a multiple of LEB128 operands for each opcode.
824 {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000825 assert(Opcode - 1U < Prologue.StandardOpcodeLengths.size());
826 uint8_t OpcodeLength = Prologue.StandardOpcodeLengths[Opcode - 1];
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000827 for (uint8_t I = 0; I < OpcodeLength; ++I) {
828 uint64_t Value = DebugLineData.getULEB128(OffsetPtr);
829 if (OS)
830 *OS << format("Skipping ULEB128 value: 0x%16.16" PRIx64 ")\n",
831 Value);
832 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000833 }
834 break;
835 }
836 } else {
837 // Special Opcodes
838
839 // A special opcode value is chosen based on the amount that needs
840 // to be added to the line and address registers. The maximum line
841 // increment for a special opcode is the value of the line_base
842 // field in the header, plus the value of the line_range field,
843 // minus 1 (line base + line range - 1). If the desired line
844 // increment is greater than the maximum line increment, a standard
NAKAMURA Takumif9959852011-10-08 11:22:47 +0000845 // opcode must be used instead of a special opcode. The "address
846 // advance" is calculated by dividing the desired address increment
Benjamin Kramer5acab502011-09-15 02:12:05 +0000847 // by the minimum_instruction_length field from the header. The
848 // special opcode is then calculated using the following formula:
849 //
850 // opcode = (desired line increment - line_base) +
851 // (line_range * address advance) + opcode_base
852 //
853 // If the resulting opcode is greater than 255, a standard opcode
854 // must be used instead.
855 //
856 // To decode a special opcode, subtract the opcode_base from the
857 // opcode itself to give the adjusted opcode. The amount to
858 // increment the address register is the result of the adjusted
859 // opcode divided by the line_range multiplied by the
860 // minimum_instruction_length field from the header. That is:
861 //
862 // address increment = (adjusted opcode / line_range) *
863 // minimum_instruction_length
864 //
865 // The amount to increment the line register is the line_base plus
866 // the result of the adjusted opcode modulo the line_range. That is:
867 //
868 // line increment = line_base + (adjusted opcode % line_range)
869
Paul Robinson9d4eb692017-05-01 23:27:55 +0000870 uint8_t AdjustOpcode = Opcode - Prologue.OpcodeBase;
871 uint64_t AddrOffset =
872 (AdjustOpcode / Prologue.LineRange) * Prologue.MinInstLength;
873 int32_t LineOffset =
874 Prologue.LineBase + (AdjustOpcode % Prologue.LineRange);
875 State.Row.Line += LineOffset;
Alexey Lapshin77fc1f62019-02-27 13:17:36 +0000876 State.Row.Address.Address += AddrOffset;
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000877
878 if (OS) {
James Hendersonb6b5b1a2019-02-06 10:31:50 +0000879 *OS << "address += " << AddrOffset << ", line += " << LineOffset
880 << "\n";
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000881 OS->indent(12);
882 State.Row.dump(*OS);
883 }
884
Fangrui Songc4c8bca2019-04-07 13:56:14 +0000885 State.appendRowToMatrix();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000886 }
Jonas Devlieghere26f9a0c2017-09-21 20:15:30 +0000887 if(OS)
888 *OS << "\n";
Benjamin Kramer5acab502011-09-15 02:12:05 +0000889 }
890
Jonas Devlieghere84e99262018-04-14 22:07:23 +0000891 if (!State.Sequence.Empty)
James Henderson004b7292018-05-21 15:30:54 +0000892 RecoverableErrorCallback(
Victor Leschukcba595d2018-08-20 09:59:08 +0000893 createStringError(errc::illegal_byte_sequence,
894 "last sequence in debug line table is not terminated!"));
Alexey Samsonov110d5952014-04-30 00:09:19 +0000895
896 // Sort all sequences so that address lookup will work faster.
897 if (!Sequences.empty()) {
Fangrui Song9b22c462019-04-09 15:08:32 +0000898 llvm::sort(Sequences, Sequence::orderByHighPC);
Alexey Samsonov110d5952014-04-30 00:09:19 +0000899 // Note: actually, instruction address ranges of sequences should not
900 // overlap (in shared objects and executables). If they do, the address
901 // lookup would still work, though, but result would be ambiguous.
902 // We don't report warning in this case. For example,
903 // sometimes .so compiled from multiple object files contains a few
904 // rudimentary sequences for address ranges [0x0, 0xsomething).
905 }
Benjamin Kramer5acab502011-09-15 02:12:05 +0000906
James Hendersona3acf992018-05-10 10:51:33 +0000907 return Error::success();
Benjamin Kramer5acab502011-09-15 02:12:05 +0000908}
909
Alexey Lapshin77fc1f62019-02-27 13:17:36 +0000910uint32_t DWARFDebugLine::LineTable::findRowInSeq(
911 const DWARFDebugLine::Sequence &Seq,
912 object::SectionedAddress Address) const {
Paul Robinson9d4eb692017-05-01 23:27:55 +0000913 if (!Seq.containsPC(Address))
Keno Fischerc2c60182015-05-31 23:37:04 +0000914 return UnknownRowIndex;
Alexey Lapshin77fc1f62019-02-27 13:17:36 +0000915 assert(Seq.SectionIndex == Address.SectionIndex);
Fangrui Songb3be23d2019-04-10 07:44:23 +0000916 // In some cases, e.g. first instruction in a function, the compiler generates
917 // two entries, both with the same address. We want the last one.
918 //
919 // In general we want a non-empty range: the last row whose address is less
920 // than or equal to Address. This can be computed as upper_bound - 1.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000921 DWARFDebugLine::Row Row;
922 Row.Address = Address;
923 RowIter FirstRow = Rows.begin() + Seq.FirstRowIndex;
924 RowIter LastRow = Rows.begin() + Seq.LastRowIndex;
Fangrui Songb3be23d2019-04-10 07:44:23 +0000925 assert(FirstRow->Address.Address <= Row.Address.Address &&
926 Row.Address.Address < LastRow[-1].Address.Address);
927 RowIter RowPos = std::upper_bound(FirstRow + 1, LastRow - 1, Row,
928 DWARFDebugLine::Row::orderByAddress) -
929 1;
Alexey Lapshin77fc1f62019-02-27 13:17:36 +0000930 assert(Seq.SectionIndex == RowPos->Address.SectionIndex);
Fangrui Songb3be23d2019-04-10 07:44:23 +0000931 return RowPos - Rows.begin();
Keno Fischerc2c60182015-05-31 23:37:04 +0000932}
933
Alexey Lapshin77fc1f62019-02-27 13:17:36 +0000934uint32_t DWARFDebugLine::LineTable::lookupAddress(
935 object::SectionedAddress Address) const {
936
937 // Search for relocatable addresses
938 uint32_t Result = lookupAddressImpl(Address);
939
940 if (Result != UnknownRowIndex ||
941 Address.SectionIndex == object::SectionedAddress::UndefSection)
942 return Result;
943
944 // Search for absolute addresses
945 Address.SectionIndex = object::SectionedAddress::UndefSection;
946 return lookupAddressImpl(Address);
947}
948
949uint32_t DWARFDebugLine::LineTable::lookupAddressImpl(
950 object::SectionedAddress Address) const {
Alexey Samsonov947228c2012-08-07 11:46:57 +0000951 // First, find an instruction sequence containing the given address.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000952 DWARFDebugLine::Sequence Sequence;
Alexey Lapshin77fc1f62019-02-27 13:17:36 +0000953 Sequence.SectionIndex = Address.SectionIndex;
Fangrui Song9b22c462019-04-09 15:08:32 +0000954 Sequence.HighPC = Address.Address;
955 SequenceIter It = llvm::upper_bound(Sequences, Sequence,
956 DWARFDebugLine::Sequence::orderByHighPC);
957 if (It == Sequences.end() || It->SectionIndex != Address.SectionIndex)
Alexey Lapshin77fc1f62019-02-27 13:17:36 +0000958 return UnknownRowIndex;
Fangrui Song9b22c462019-04-09 15:08:32 +0000959 return findRowInSeq(*It, Address);
Benjamin Kramer5acab502011-09-15 02:12:05 +0000960}
Alexey Samsonov45be7932012-08-30 07:49:50 +0000961
Alexey Samsonov836b1ae2014-04-29 21:28:13 +0000962bool DWARFDebugLine::LineTable::lookupAddressRange(
Alexey Lapshin77fc1f62019-02-27 13:17:36 +0000963 object::SectionedAddress Address, uint64_t Size,
964 std::vector<uint32_t> &Result) const {
965
966 // Search for relocatable addresses
967 if (lookupAddressRangeImpl(Address, Size, Result))
968 return true;
969
970 if (Address.SectionIndex == object::SectionedAddress::UndefSection)
971 return false;
972
973 // Search for absolute addresses
974 Address.SectionIndex = object::SectionedAddress::UndefSection;
975 return lookupAddressRangeImpl(Address, Size, Result);
976}
977
978bool DWARFDebugLine::LineTable::lookupAddressRangeImpl(
979 object::SectionedAddress Address, uint64_t Size,
980 std::vector<uint32_t> &Result) const {
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000981 if (Sequences.empty())
982 return false;
Alexey Lapshin77fc1f62019-02-27 13:17:36 +0000983 uint64_t EndAddr = Address.Address + Size;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000984 // First, find an instruction sequence containing the given address.
Paul Robinson9d4eb692017-05-01 23:27:55 +0000985 DWARFDebugLine::Sequence Sequence;
Alexey Lapshin77fc1f62019-02-27 13:17:36 +0000986 Sequence.SectionIndex = Address.SectionIndex;
Fangrui Song9b22c462019-04-09 15:08:32 +0000987 Sequence.HighPC = Address.Address;
Paul Robinson9d4eb692017-05-01 23:27:55 +0000988 SequenceIter LastSeq = Sequences.end();
Fangrui Song9b22c462019-04-09 15:08:32 +0000989 SequenceIter SeqPos = llvm::upper_bound(
990 Sequences, Sequence, DWARFDebugLine::Sequence::orderByHighPC);
991 if (SeqPos == LastSeq || !SeqPos->containsPC(Address))
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000992 return false;
993
Paul Robinson9d4eb692017-05-01 23:27:55 +0000994 SequenceIter StartPos = SeqPos;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +0000995
996 // Add the rows from the first sequence to the vector, starting with the
997 // index we just calculated
998
Paul Robinson9d4eb692017-05-01 23:27:55 +0000999 while (SeqPos != LastSeq && SeqPos->LowPC < EndAddr) {
1000 const DWARFDebugLine::Sequence &CurSeq = *SeqPos;
Keno Fischerc2c60182015-05-31 23:37:04 +00001001 // For the first sequence, we need to find which row in the sequence is the
1002 // first in our range.
Paul Robinson9d4eb692017-05-01 23:27:55 +00001003 uint32_t FirstRowIndex = CurSeq.FirstRowIndex;
1004 if (SeqPos == StartPos)
1005 FirstRowIndex = findRowInSeq(CurSeq, Address);
Andrew Kaylor9a8ff812013-01-26 00:28:05 +00001006
Keno Fischerc2c60182015-05-31 23:37:04 +00001007 // Figure out the last row in the range.
Alexey Lapshin77fc1f62019-02-27 13:17:36 +00001008 uint32_t LastRowIndex =
1009 findRowInSeq(CurSeq, {EndAddr - 1, Address.SectionIndex});
Paul Robinson9d4eb692017-05-01 23:27:55 +00001010 if (LastRowIndex == UnknownRowIndex)
1011 LastRowIndex = CurSeq.LastRowIndex - 1;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +00001012
Paul Robinson9d4eb692017-05-01 23:27:55 +00001013 assert(FirstRowIndex != UnknownRowIndex);
1014 assert(LastRowIndex != UnknownRowIndex);
Keno Fischerc2c60182015-05-31 23:37:04 +00001015
Paul Robinson9d4eb692017-05-01 23:27:55 +00001016 for (uint32_t I = FirstRowIndex; I <= LastRowIndex; ++I) {
1017 Result.push_back(I);
Andrew Kaylor9a8ff812013-01-26 00:28:05 +00001018 }
1019
Paul Robinson9d4eb692017-05-01 23:27:55 +00001020 ++SeqPos;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +00001021 }
NAKAMURA Takumi4b86cdb2013-01-26 01:45:06 +00001022
1023 return true;
Andrew Kaylor9a8ff812013-01-26 00:28:05 +00001024}
1025
Scott Linder16c7bda2018-02-23 23:01:06 +00001026Optional<StringRef> DWARFDebugLine::LineTable::getSourceByIndex(uint64_t FileIndex,
1027 FileLineInfoKind Kind) const {
Jonas Devlieghereca16d282019-07-16 01:21:25 +00001028 if (Kind == FileLineInfoKind::None || !Prologue.hasFileAtIndex(FileIndex))
Scott Linder16c7bda2018-02-23 23:01:06 +00001029 return None;
Jonas Devlieghereca16d282019-07-16 01:21:25 +00001030 const FileNameEntry &Entry = Prologue.getFileNameEntry(FileIndex);
Scott Linder16c7bda2018-02-23 23:01:06 +00001031 if (Optional<const char *> source = Entry.Source.getAsCString())
1032 return StringRef(*source);
1033 return None;
1034}
1035
Eugene Zemtsov82d60d62018-03-13 17:54:29 +00001036static bool isPathAbsoluteOnWindowsOrPosix(const Twine &Path) {
1037 // Debug info can contain paths from any OS, not necessarily
1038 // an OS we're currently running on. Moreover different compilation units can
1039 // be compiled on different operating systems and linked together later.
1040 return sys::path::is_absolute(Path, sys::path::Style::posix) ||
1041 sys::path::is_absolute(Path, sys::path::Style::windows);
1042}
1043
Jonas Devliegherec0a9b1e2019-08-14 17:00:10 +00001044/// If given an absolute path, guess the path style.
1045static sys::path::Style GuessPathStyle(StringRef Path) {
1046 bool Posix = sys::path::is_absolute(Path, sys::path::Style::posix);
1047 bool Windows = sys::path::is_absolute(Path, sys::path::Style::windows);
1048 // This is a relative path.
1049 if (!Posix && !Windows)
1050 return sys::path::Style::native;
1051 // This is a valid absolute path for both Windows and Posix.
1052 if (Posix && Windows)
1053 return sys::path::Style::native;
1054 if (Posix)
1055 return sys::path::Style::posix;
1056 if (Windows)
1057 return sys::path::Style::windows;
1058
1059 llvm_unreachable("All combinations should have been handled.");
1060}
1061
Jonas Devlieghereca16d282019-07-16 01:21:25 +00001062bool DWARFDebugLine::Prologue::getFileNameByIndex(uint64_t FileIndex,
1063 StringRef CompDir,
1064 FileLineInfoKind Kind,
1065 std::string &Result) const {
Pete Cooperb2ba7762016-07-22 01:41:32 +00001066 if (Kind == FileLineInfoKind::None || !hasFileAtIndex(FileIndex))
Alexey Samsonov45be7932012-08-30 07:49:50 +00001067 return false;
Ali Tamur783d84b2019-04-19 02:26:56 +00001068 const FileNameEntry &Entry = getFileNameEntry(FileIndex);
Paul Robinson0a227092018-02-05 20:43:15 +00001069 StringRef FileName = Entry.Name.getAsCString().getValue();
Alexey Samsonovdce67342014-05-15 21:24:32 +00001070 if (Kind != FileLineInfoKind::AbsoluteFilePath ||
Eugene Zemtsov82d60d62018-03-13 17:54:29 +00001071 isPathAbsoluteOnWindowsOrPosix(FileName)) {
Alexey Samsonov45be7932012-08-30 07:49:50 +00001072 Result = FileName;
1073 return true;
1074 }
Frederic Riss101b5e22014-09-19 15:11:51 +00001075
Alexey Samsonov45be7932012-08-30 07:49:50 +00001076 SmallString<16> FilePath;
Paul Robinsonba1c9152017-05-02 17:37:32 +00001077 StringRef IncludeDir;
Alexey Samsonov45be7932012-08-30 07:49:50 +00001078 // Be defensive about the contents of Entry.
Jonas Devlieghereca16d282019-07-16 01:21:25 +00001079 if (getVersion() >= 5) {
1080 if (Entry.DirIdx < IncludeDirectories.size())
1081 IncludeDir = IncludeDirectories[Entry.DirIdx].getAsCString().getValue();
Jonas Devlieghere01ee1722019-07-16 00:59:04 +00001082 } else {
Jonas Devlieghereca16d282019-07-16 01:21:25 +00001083 if (0 < Entry.DirIdx && Entry.DirIdx <= IncludeDirectories.size())
1084 IncludeDir =
1085 IncludeDirectories[Entry.DirIdx - 1].getAsCString().getValue();
Frederic Riss101b5e22014-09-19 15:11:51 +00001086
Fangrui Song7e556722019-05-06 08:03:46 +00001087 // We may still need to append compilation directory of compile unit.
1088 // We know that FileName is not absolute, the only way to have an
1089 // absolute path at this point would be if IncludeDir is absolute.
Jonas Devlieghereca16d282019-07-16 01:21:25 +00001090 if (!CompDir.empty() && !isPathAbsoluteOnWindowsOrPosix(IncludeDir))
Jonas Devliegherec0a9b1e2019-08-14 17:00:10 +00001091 sys::path::append(FilePath, GuessPathStyle(CompDir), CompDir);
Fangrui Song7e556722019-05-06 08:03:46 +00001092 }
Frederic Riss101b5e22014-09-19 15:11:51 +00001093
1094 // sys::path::append skips empty strings.
Jonas Devliegherec0a9b1e2019-08-14 17:00:10 +00001095 sys::path::append(FilePath, GuessPathStyle(IncludeDir), IncludeDir, FileName);
Alexey Samsonov45be7932012-08-30 07:49:50 +00001096 Result = FilePath.str();
1097 return true;
1098}
Frederic Riss101b5e22014-09-19 15:11:51 +00001099
Dehao Chen1b54fce2016-04-28 22:09:37 +00001100bool DWARFDebugLine::LineTable::getFileLineInfoForAddress(
Alexey Lapshin77fc1f62019-02-27 13:17:36 +00001101 object::SectionedAddress Address, const char *CompDir,
1102 FileLineInfoKind Kind, DILineInfo &Result) const {
Frederic Riss101b5e22014-09-19 15:11:51 +00001103 // Get the index of row we're looking for in the line table.
1104 uint32_t RowIndex = lookupAddress(Address);
1105 if (RowIndex == -1U)
1106 return false;
1107 // Take file number and line/column from the row.
1108 const auto &Row = Rows[RowIndex];
1109 if (!getFileNameByIndex(Row.File, CompDir, Kind, Result.FileName))
1110 return false;
1111 Result.Line = Row.Line;
1112 Result.Column = Row.Column;
Eric Christopherba1024c2016-12-14 18:29:39 +00001113 Result.Discriminator = Row.Discriminator;
Scott Linder16c7bda2018-02-23 23:01:06 +00001114 Result.Source = getSourceByIndex(Row.File, Kind);
Frederic Riss101b5e22014-09-19 15:11:51 +00001115 return true;
1116}
James Hendersona3acf992018-05-10 10:51:33 +00001117
1118// We want to supply the Unit associated with a .debug_line[.dwo] table when
1119// we dump it, if possible, but still dump the table even if there isn't a Unit.
1120// Therefore, collect up handles on all the Units that point into the
1121// line-table section.
1122static DWARFDebugLine::SectionParser::LineToUnitMap
1123buildLineToUnitMap(DWARFDebugLine::SectionParser::cu_range CUs,
Paul Robinson7f330942018-08-01 20:46:46 +00001124 DWARFDebugLine::SectionParser::tu_range TUs) {
James Hendersona3acf992018-05-10 10:51:33 +00001125 DWARFDebugLine::SectionParser::LineToUnitMap LineToUnit;
1126 for (const auto &CU : CUs)
1127 if (auto CUDIE = CU->getUnitDIE())
1128 if (auto StmtOffset = toSectionOffset(CUDIE.find(DW_AT_stmt_list)))
1129 LineToUnit.insert(std::make_pair(*StmtOffset, &*CU));
Paul Robinson7f330942018-08-01 20:46:46 +00001130 for (const auto &TU : TUs)
1131 if (auto TUDIE = TU->getUnitDIE())
1132 if (auto StmtOffset = toSectionOffset(TUDIE.find(DW_AT_stmt_list)))
1133 LineToUnit.insert(std::make_pair(*StmtOffset, &*TU));
James Hendersona3acf992018-05-10 10:51:33 +00001134 return LineToUnit;
1135}
1136
1137DWARFDebugLine::SectionParser::SectionParser(DWARFDataExtractor &Data,
1138 const DWARFContext &C,
1139 cu_range CUs, tu_range TUs)
1140 : DebugLineData(Data), Context(C) {
1141 LineToUnit = buildLineToUnitMap(CUs, TUs);
1142 if (!DebugLineData.isValidOffset(Offset))
1143 Done = true;
1144}
1145
1146bool DWARFDebugLine::Prologue::totalLengthIsValid() const {
Igor Kudrin3daefb02019-07-24 11:34:29 +00001147 return TotalLength == dwarf::DW_LENGTH_DWARF64 ||
1148 TotalLength < dwarf::DW_LENGTH_lo_reserved;
James Hendersona3acf992018-05-10 10:51:33 +00001149}
1150
1151DWARFDebugLine::LineTable DWARFDebugLine::SectionParser::parseNext(
James Henderson004b7292018-05-21 15:30:54 +00001152 function_ref<void(Error)> RecoverableErrorCallback,
1153 function_ref<void(Error)> UnrecoverableErrorCallback, raw_ostream *OS) {
James Hendersona3acf992018-05-10 10:51:33 +00001154 assert(DebugLineData.isValidOffset(Offset) &&
1155 "parsing should have terminated");
1156 DWARFUnit *U = prepareToParse(Offset);
Igor Kudrinf26a70a2019-08-06 10:49:40 +00001157 uint64_t OldOffset = Offset;
James Hendersona3acf992018-05-10 10:51:33 +00001158 LineTable LT;
James Henderson004b7292018-05-21 15:30:54 +00001159 if (Error Err = LT.parse(DebugLineData, &Offset, Context, U,
1160 RecoverableErrorCallback, OS))
1161 UnrecoverableErrorCallback(std::move(Err));
James Hendersona3acf992018-05-10 10:51:33 +00001162 moveToNextTable(OldOffset, LT.Prologue);
1163 return LT;
1164}
1165
1166void DWARFDebugLine::SectionParser::skip(
1167 function_ref<void(Error)> ErrorCallback) {
1168 assert(DebugLineData.isValidOffset(Offset) &&
1169 "parsing should have terminated");
1170 DWARFUnit *U = prepareToParse(Offset);
Igor Kudrinf26a70a2019-08-06 10:49:40 +00001171 uint64_t OldOffset = Offset;
James Hendersona3acf992018-05-10 10:51:33 +00001172 LineTable LT;
James Henderson004b7292018-05-21 15:30:54 +00001173 if (Error Err = LT.Prologue.parse(DebugLineData, &Offset, Context, U))
1174 ErrorCallback(std::move(Err));
James Hendersona3acf992018-05-10 10:51:33 +00001175 moveToNextTable(OldOffset, LT.Prologue);
1176}
1177
Igor Kudrinf26a70a2019-08-06 10:49:40 +00001178DWARFUnit *DWARFDebugLine::SectionParser::prepareToParse(uint64_t Offset) {
James Hendersona3acf992018-05-10 10:51:33 +00001179 DWARFUnit *U = nullptr;
1180 auto It = LineToUnit.find(Offset);
1181 if (It != LineToUnit.end())
1182 U = It->second;
1183 DebugLineData.setAddressSize(U ? U->getAddressByteSize() : 0);
1184 return U;
1185}
1186
Igor Kudrinf26a70a2019-08-06 10:49:40 +00001187void DWARFDebugLine::SectionParser::moveToNextTable(uint64_t OldOffset,
James Hendersona3acf992018-05-10 10:51:33 +00001188 const Prologue &P) {
1189 // If the length field is not valid, we don't know where the next table is, so
1190 // cannot continue to parse. Mark the parser as done, and leave the Offset
1191 // value as it currently is. This will be the end of the bad length field.
1192 if (!P.totalLengthIsValid()) {
1193 Done = true;
1194 return;
1195 }
1196
1197 Offset = OldOffset + P.TotalLength + P.sizeofTotalLength();
1198 if (!DebugLineData.isValidOffset(Offset)) {
1199 Done = true;
1200 }
1201}