1e94042caSEugene Zelenko //===- DWARFDebugLine.cpp -------------------------------------------------===//
282af9438SZachary Turner //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
682af9438SZachary Turner //
782af9438SZachary Turner //===----------------------------------------------------------------------===//
882af9438SZachary Turner
99d4eb692SPaul Robinson #include "llvm/DebugInfo/DWARF/DWARFDebugLine.h"
1016c7bdafSScott Linder #include "llvm/ADT/Optional.h"
11e94042caSEugene Zelenko #include "llvm/ADT/SmallString.h"
122db0cfa6SEugene Zelenko #include "llvm/ADT/SmallVector.h"
132db0cfa6SEugene Zelenko #include "llvm/ADT/StringRef.h"
14264b5d9eSZachary Turner #include "llvm/BinaryFormat/Dwarf.h"
15290e4823Sserge-sans-paille #include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h"
16290e4823Sserge-sans-paille #include "llvm/DebugInfo/DWARF/DWARFDie.h"
172bc3873fSPaul Robinson #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
18cba595daSVictor Leschuk #include "llvm/Support/Errc.h"
1982af9438SZachary Turner #include "llvm/Support/Format.h"
2063b428e3SHubert Tong #include "llvm/Support/FormatVariadic.h"
2182af9438SZachary Turner #include "llvm/Support/raw_ostream.h"
2282af9438SZachary Turner #include <algorithm>
23e94042caSEugene Zelenko #include <cassert>
24e94042caSEugene Zelenko #include <cinttypes>
25e94042caSEugene Zelenko #include <cstdint>
26e94042caSEugene Zelenko #include <cstdio>
27e94042caSEugene Zelenko #include <utility>
28e94042caSEugene Zelenko
2982af9438SZachary Turner using namespace llvm;
3082af9438SZachary Turner using namespace dwarf;
31e94042caSEugene Zelenko
322db0cfa6SEugene Zelenko using FileLineInfoKind = DILineInfoSpecifier::FileLineInfoKind;
332db0cfa6SEugene Zelenko
342bc3873fSPaul Robinson namespace {
352db0cfa6SEugene Zelenko
362bc3873fSPaul Robinson struct ContentDescriptor {
372bc3873fSPaul Robinson dwarf::LineNumberEntryFormat Type;
382bc3873fSPaul Robinson dwarf::Form Form;
392bc3873fSPaul Robinson };
402db0cfa6SEugene Zelenko
412db0cfa6SEugene Zelenko using ContentDescriptors = SmallVector<ContentDescriptor, 4>;
422db0cfa6SEugene Zelenko
43d68904f9SJames Henderson } // end anonymous namespace
4482af9438SZachary Turner
versionIsSupported(uint16_t Version)45ced45978SPavel Labath static bool versionIsSupported(uint16_t Version) {
46ced45978SPavel Labath return Version >= 2 && Version <= 5;
47ced45978SPavel Labath }
48ced45978SPavel Labath
trackContentType(dwarf::LineNumberEntryFormat ContentType)4916c7bdafSScott Linder void DWARFDebugLine::ContentTypeTracker::trackContentType(
5016c7bdafSScott Linder dwarf::LineNumberEntryFormat ContentType) {
5116c7bdafSScott Linder switch (ContentType) {
5216c7bdafSScott Linder case dwarf::DW_LNCT_timestamp:
5316c7bdafSScott Linder HasModTime = true;
5416c7bdafSScott Linder break;
5516c7bdafSScott Linder case dwarf::DW_LNCT_size:
5616c7bdafSScott Linder HasLength = true;
5716c7bdafSScott Linder break;
5816c7bdafSScott Linder case dwarf::DW_LNCT_MD5:
5916c7bdafSScott Linder HasMD5 = true;
6016c7bdafSScott Linder break;
6116c7bdafSScott Linder case dwarf::DW_LNCT_LLVM_source:
6216c7bdafSScott Linder HasSource = true;
6316c7bdafSScott Linder break;
6416c7bdafSScott Linder default:
6516c7bdafSScott Linder // We only care about values we consider optional, and new values may be
6616c7bdafSScott Linder // added in the vendor extension range, so we do not match exhaustively.
6716c7bdafSScott Linder break;
6816c7bdafSScott Linder }
6916c7bdafSScott Linder }
7016c7bdafSScott Linder
Prologue()711b54fce3SDehao Chen DWARFDebugLine::Prologue::Prologue() { clear(); }
7282af9438SZachary Turner
hasFileAtIndex(uint64_t FileIndex) const73ca16d280SJonas Devlieghere bool DWARFDebugLine::Prologue::hasFileAtIndex(uint64_t FileIndex) const {
74ca16d280SJonas Devlieghere uint16_t DwarfVersion = getVersion();
75ca16d280SJonas Devlieghere assert(DwarfVersion != 0 &&
76ca16d280SJonas Devlieghere "line table prologue has no dwarf version information");
77ca16d280SJonas Devlieghere if (DwarfVersion >= 5)
78ca16d280SJonas Devlieghere return FileIndex < FileNames.size();
79ca16d280SJonas Devlieghere return FileIndex != 0 && FileIndex <= FileNames.size();
80ca16d280SJonas Devlieghere }
81ca16d280SJonas Devlieghere
getLastValidFileIndex() const82e1de85f9SGreg Clayton Optional<uint64_t> DWARFDebugLine::Prologue::getLastValidFileIndex() const {
83e1de85f9SGreg Clayton if (FileNames.empty())
84e1de85f9SGreg Clayton return None;
85e1de85f9SGreg Clayton uint16_t DwarfVersion = getVersion();
86e1de85f9SGreg Clayton assert(DwarfVersion != 0 &&
87e1de85f9SGreg Clayton "line table prologue has no dwarf version information");
88e1de85f9SGreg Clayton // In DWARF v5 the file names are 0-indexed.
89e1de85f9SGreg Clayton if (DwarfVersion >= 5)
90e1de85f9SGreg Clayton return FileNames.size() - 1;
91e1de85f9SGreg Clayton return FileNames.size();
92e1de85f9SGreg Clayton }
93e1de85f9SGreg Clayton
94ca16d280SJonas Devlieghere const llvm::DWARFDebugLine::FileNameEntry &
getFileNameEntry(uint64_t Index) const95ca16d280SJonas Devlieghere DWARFDebugLine::Prologue::getFileNameEntry(uint64_t Index) const {
96ca16d280SJonas Devlieghere uint16_t DwarfVersion = getVersion();
97ca16d280SJonas Devlieghere assert(DwarfVersion != 0 &&
98ca16d280SJonas Devlieghere "line table prologue has no dwarf version information");
99ca16d280SJonas Devlieghere // In DWARF v5 the file names are 0-indexed.
100ca16d280SJonas Devlieghere if (DwarfVersion >= 5)
101ca16d280SJonas Devlieghere return FileNames[Index];
102ca16d280SJonas Devlieghere return FileNames[Index - 1];
103ca16d280SJonas Devlieghere }
104ca16d280SJonas Devlieghere
clear()10582af9438SZachary Turner void DWARFDebugLine::Prologue::clear() {
10675c068c5SPaul Robinson TotalLength = PrologueLength = 0;
10775c068c5SPaul Robinson SegSelectorSize = 0;
10882af9438SZachary Turner MinInstLength = MaxOpsPerInst = DefaultIsStmt = LineBase = LineRange = 0;
10982af9438SZachary Turner OpcodeBase = 0;
110322711f5SPavel Labath FormParams = dwarf::FormParams({0, 0, DWARF32});
11116c7bdafSScott Linder ContentTypes = ContentTypeTracker();
11282af9438SZachary Turner StandardOpcodeLengths.clear();
11382af9438SZachary Turner IncludeDirectories.clear();
11482af9438SZachary Turner FileNames.clear();
11582af9438SZachary Turner }
11682af9438SZachary Turner
dump(raw_ostream & OS,DIDumpOptions DumpOptions) const1170a22709fSPaul Robinson void DWARFDebugLine::Prologue::dump(raw_ostream &OS,
1180a22709fSPaul Robinson DIDumpOptions DumpOptions) const {
119ced45978SPavel Labath if (!totalLengthIsValid())
120ced45978SPavel Labath return;
121c9122b8fSIgor Kudrin int OffsetDumpWidth = 2 * dwarf::getDwarfOffsetByteSize(FormParams.Format);
12282af9438SZachary Turner OS << "Line table prologue:\n"
123c9122b8fSIgor Kudrin << format(" total_length: 0x%0*" PRIx64 "\n", OffsetDumpWidth,
124c9122b8fSIgor Kudrin TotalLength)
125da913259SIgor Kudrin << " format: " << dwarf::FormatString(FormParams.Format) << "\n"
12675c068c5SPaul Robinson << format(" version: %u\n", getVersion());
127ced45978SPavel Labath if (!versionIsSupported(getVersion()))
128ced45978SPavel Labath return;
12975c068c5SPaul Robinson if (getVersion() >= 5)
13075c068c5SPaul Robinson OS << format(" address_size: %u\n", getAddressSize())
13175c068c5SPaul Robinson << format(" seg_select_size: %u\n", SegSelectorSize);
132c9122b8fSIgor Kudrin OS << format(" prologue_length: 0x%0*" PRIx64 "\n", OffsetDumpWidth,
133c9122b8fSIgor Kudrin PrologueLength)
13482af9438SZachary Turner << format(" min_inst_length: %u\n", MinInstLength)
13575c068c5SPaul Robinson << format(getVersion() >= 4 ? "max_ops_per_inst: %u\n" : "", MaxOpsPerInst)
13682af9438SZachary Turner << format(" default_is_stmt: %u\n", DefaultIsStmt)
13782af9438SZachary Turner << format(" line_base: %i\n", LineBase)
13882af9438SZachary Turner << format(" line_range: %u\n", LineRange)
13982af9438SZachary Turner << format(" opcode_base: %u\n", OpcodeBase);
14082af9438SZachary Turner
1419d4eb692SPaul Robinson for (uint32_t I = 0; I != StandardOpcodeLengths.size(); ++I)
14263b428e3SHubert Tong OS << formatv("standard_opcode_lengths[{0}] = {1}\n",
14363b428e3SHubert Tong static_cast<dwarf::LineNumberOps>(I + 1),
14463b428e3SHubert Tong StandardOpcodeLengths[I]);
14582af9438SZachary Turner
1468181d23bSPaul Robinson if (!IncludeDirectories.empty()) {
1478181d23bSPaul Robinson // DWARF v5 starts directory indexes at 0.
1488181d23bSPaul Robinson uint32_t DirBase = getVersion() >= 5 ? 0 : 1;
1490a22709fSPaul Robinson for (uint32_t I = 0; I != IncludeDirectories.size(); ++I) {
1500a22709fSPaul Robinson OS << format("include_directories[%3u] = ", I + DirBase);
1510a22709fSPaul Robinson IncludeDirectories[I].dump(OS, DumpOptions);
1520a22709fSPaul Robinson OS << '\n';
1530a22709fSPaul Robinson }
1548181d23bSPaul Robinson }
15582af9438SZachary Turner
15682af9438SZachary Turner if (!FileNames.empty()) {
157ceafcd41SPaul Robinson // DWARF v5 starts file indexes at 0.
158ceafcd41SPaul Robinson uint32_t FileBase = getVersion() >= 5 ? 0 : 1;
1599d4eb692SPaul Robinson for (uint32_t I = 0; I != FileNames.size(); ++I) {
1609d4eb692SPaul Robinson const FileNameEntry &FileEntry = FileNames[I];
16116c7bdafSScott Linder OS << format("file_names[%3u]:\n", I + FileBase);
16216c7bdafSScott Linder OS << " name: ";
1630a22709fSPaul Robinson FileEntry.Name.dump(OS, DumpOptions);
16416c7bdafSScott Linder OS << '\n'
16516c7bdafSScott Linder << format(" dir_index: %" PRIu64 "\n", FileEntry.DirIdx);
16616c7bdafSScott Linder if (ContentTypes.HasMD5)
16716c7bdafSScott Linder OS << " md5_checksum: " << FileEntry.Checksum.digest() << '\n';
16816c7bdafSScott Linder if (ContentTypes.HasModTime)
16916c7bdafSScott Linder OS << format(" mod_time: 0x%8.8" PRIx64 "\n", FileEntry.ModTime);
17016c7bdafSScott Linder if (ContentTypes.HasLength)
17116c7bdafSScott Linder OS << format(" length: 0x%8.8" PRIx64 "\n", FileEntry.Length);
17216c7bdafSScott Linder if (ContentTypes.HasSource) {
17316c7bdafSScott Linder OS << " source: ";
17416c7bdafSScott Linder FileEntry.Source.dump(OS, DumpOptions);
1750a22709fSPaul Robinson OS << '\n';
17682af9438SZachary Turner }
17782af9438SZachary Turner }
17882af9438SZachary Turner }
17916c7bdafSScott Linder }
18082af9438SZachary Turner
1812bc3873fSPaul Robinson // Parse v2-v4 directory and file tables.
182bf4d8f29SJames Henderson static Error
parseV2DirFileTables(const DWARFDataExtractor & DebugLineData,uint64_t * OffsetPtr,DWARFDebugLine::ContentTypeTracker & ContentTypes,std::vector<DWARFFormValue> & IncludeDirectories,std::vector<DWARFDebugLine::FileNameEntry> & FileNames)18317536b93SPaul Robinson parseV2DirFileTables(const DWARFDataExtractor &DebugLineData,
1846f55b5a1SPavel Labath uint64_t *OffsetPtr,
18516c7bdafSScott Linder DWARFDebugLine::ContentTypeTracker &ContentTypes,
1860a22709fSPaul Robinson std::vector<DWARFFormValue> &IncludeDirectories,
1872bc3873fSPaul Robinson std::vector<DWARFDebugLine::FileNameEntry> &FileNames) {
188c475856dSPavel Labath while (true) {
189c475856dSPavel Labath Error Err = Error::success();
190c475856dSPavel Labath StringRef S = DebugLineData.getCStrRef(OffsetPtr, &Err);
1916f55b5a1SPavel Labath if (Err) {
192c475856dSPavel Labath consumeError(std::move(Err));
193c475856dSPavel Labath return createStringError(errc::invalid_argument,
194c475856dSPavel Labath "include directories table was not null "
195c475856dSPavel Labath "terminated before the end of the prologue");
196bf4d8f29SJames Henderson }
197c475856dSPavel Labath if (S.empty())
198c475856dSPavel Labath break;
199bb111152SJonas Devlieghere DWARFFormValue Dir =
200bb111152SJonas Devlieghere DWARFFormValue::createFromPValue(dwarf::DW_FORM_string, S.data());
2010a22709fSPaul Robinson IncludeDirectories.push_back(Dir);
2022bc3873fSPaul Robinson }
2032bc3873fSPaul Robinson
20416c7bdafSScott Linder ContentTypes.HasModTime = true;
20516c7bdafSScott Linder ContentTypes.HasLength = true;
206bf4d8f29SJames Henderson
207c475856dSPavel Labath while (true) {
208c475856dSPavel Labath Error Err = Error::success();
209c475856dSPavel Labath StringRef Name = DebugLineData.getCStrRef(OffsetPtr, &Err);
2106f55b5a1SPavel Labath if (!Err && Name.empty())
211c475856dSPavel Labath break;
212c475856dSPavel Labath
213c475856dSPavel Labath DWARFDebugLine::FileNameEntry FileEntry;
214c475856dSPavel Labath FileEntry.Name =
215c475856dSPavel Labath DWARFFormValue::createFromPValue(dwarf::DW_FORM_string, Name.data());
216c475856dSPavel Labath FileEntry.DirIdx = DebugLineData.getULEB128(OffsetPtr, &Err);
217c475856dSPavel Labath FileEntry.ModTime = DebugLineData.getULEB128(OffsetPtr, &Err);
218c475856dSPavel Labath FileEntry.Length = DebugLineData.getULEB128(OffsetPtr, &Err);
219c475856dSPavel Labath
2206f55b5a1SPavel Labath if (Err) {
221c475856dSPavel Labath consumeError(std::move(Err));
222c475856dSPavel Labath return createStringError(
223c475856dSPavel Labath errc::invalid_argument,
224bf4d8f29SJames Henderson "file names table was not null terminated before "
225bf4d8f29SJames Henderson "the end of the prologue");
226c475856dSPavel Labath }
227c475856dSPavel Labath FileNames.push_back(FileEntry);
228c475856dSPavel Labath }
229bf4d8f29SJames Henderson
230bf4d8f29SJames Henderson return Error::success();
2312bc3873fSPaul Robinson }
2322bc3873fSPaul Robinson
2332bc3873fSPaul Robinson // Parse v5 directory/file entry content descriptions.
2340e7ba06eSJonas Devlieghere // Returns the descriptors, or an error if we did not find a path or ran off
2350e7ba06eSJonas Devlieghere // the end of the prologue.
2360e7ba06eSJonas Devlieghere static llvm::Expected<ContentDescriptors>
parseV5EntryFormat(const DWARFDataExtractor & DebugLineData,uint64_t * OffsetPtr,DWARFDebugLine::ContentTypeTracker * ContentTypes)237f26a70a5SIgor Kudrin parseV5EntryFormat(const DWARFDataExtractor &DebugLineData, uint64_t *OffsetPtr,
2380e7ba06eSJonas Devlieghere DWARFDebugLine::ContentTypeTracker *ContentTypes) {
239100483b9SPavel Labath Error Err = Error::success();
2402bc3873fSPaul Robinson ContentDescriptors Descriptors;
241100483b9SPavel Labath int FormatCount = DebugLineData.getU8(OffsetPtr, &Err);
2422bc3873fSPaul Robinson bool HasPath = false;
243100483b9SPavel Labath for (int I = 0; I != FormatCount && !Err; ++I) {
2442bc3873fSPaul Robinson ContentDescriptor Descriptor;
2452bc3873fSPaul Robinson Descriptor.Type =
246100483b9SPavel Labath dwarf::LineNumberEntryFormat(DebugLineData.getULEB128(OffsetPtr, &Err));
247100483b9SPavel Labath Descriptor.Form = dwarf::Form(DebugLineData.getULEB128(OffsetPtr, &Err));
2482bc3873fSPaul Robinson if (Descriptor.Type == dwarf::DW_LNCT_path)
2492bc3873fSPaul Robinson HasPath = true;
25016c7bdafSScott Linder if (ContentTypes)
25116c7bdafSScott Linder ContentTypes->trackContentType(Descriptor.Type);
2522bc3873fSPaul Robinson Descriptors.push_back(Descriptor);
2532bc3873fSPaul Robinson }
2540e7ba06eSJonas Devlieghere
255100483b9SPavel Labath if (Err)
256100483b9SPavel Labath return createStringError(errc::invalid_argument,
257100483b9SPavel Labath "failed to parse entry content descriptors: %s",
258100483b9SPavel Labath toString(std::move(Err)).c_str());
259100483b9SPavel Labath
2600e7ba06eSJonas Devlieghere if (!HasPath)
2610e7ba06eSJonas Devlieghere return createStringError(errc::invalid_argument,
2620e7ba06eSJonas Devlieghere "failed to parse entry content descriptions"
2630e7ba06eSJonas Devlieghere " because no path was found");
2640e7ba06eSJonas Devlieghere return Descriptors;
2652bc3873fSPaul Robinson }
2662bc3873fSPaul Robinson
2670e7ba06eSJonas Devlieghere static Error
parseV5DirFileTables(const DWARFDataExtractor & DebugLineData,uint64_t * OffsetPtr,const dwarf::FormParams & FormParams,const DWARFContext & Ctx,const DWARFUnit * U,DWARFDebugLine::ContentTypeTracker & ContentTypes,std::vector<DWARFFormValue> & IncludeDirectories,std::vector<DWARFDebugLine::FileNameEntry> & FileNames)26817536b93SPaul Robinson parseV5DirFileTables(const DWARFDataExtractor &DebugLineData,
2697e02406fSJames Henderson uint64_t *OffsetPtr, const dwarf::FormParams &FormParams,
270322711f5SPavel Labath const DWARFContext &Ctx, const DWARFUnit *U,
27116c7bdafSScott Linder DWARFDebugLine::ContentTypeTracker &ContentTypes,
2720a22709fSPaul Robinson std::vector<DWARFFormValue> &IncludeDirectories,
2732bc3873fSPaul Robinson std::vector<DWARFDebugLine::FileNameEntry> &FileNames) {
2742bc3873fSPaul Robinson // Get the directory entry description.
2750e7ba06eSJonas Devlieghere llvm::Expected<ContentDescriptors> DirDescriptors =
2767e02406fSJames Henderson parseV5EntryFormat(DebugLineData, OffsetPtr, nullptr);
2770e7ba06eSJonas Devlieghere if (!DirDescriptors)
2780e7ba06eSJonas Devlieghere return DirDescriptors.takeError();
2792bc3873fSPaul Robinson
2802bc3873fSPaul Robinson // Get the directory entries, according to the format described above.
281d381b6a8SPavel Labath uint64_t DirEntryCount = DebugLineData.getULEB128(OffsetPtr);
282d381b6a8SPavel Labath for (uint64_t I = 0; I != DirEntryCount; ++I) {
2830e7ba06eSJonas Devlieghere for (auto Descriptor : *DirDescriptors) {
28453a9f1d3SVlad Tsyrklevich DWARFFormValue Value(Descriptor.Form);
2852bc3873fSPaul Robinson switch (Descriptor.Type) {
2862bc3873fSPaul Robinson case DW_LNCT_path:
28753a9f1d3SVlad Tsyrklevich if (!Value.extractValue(DebugLineData, OffsetPtr, FormParams, &Ctx, U))
2880e7ba06eSJonas Devlieghere return createStringError(errc::invalid_argument,
2890e7ba06eSJonas Devlieghere "failed to parse directory entry because "
2909ed452f3SPavel Labath "extracting the form value failed");
29153a9f1d3SVlad Tsyrklevich IncludeDirectories.push_back(Value);
2922bc3873fSPaul Robinson break;
2932bc3873fSPaul Robinson default:
29453a9f1d3SVlad Tsyrklevich if (!Value.skipValue(DebugLineData, OffsetPtr, FormParams))
2950e7ba06eSJonas Devlieghere return createStringError(errc::invalid_argument,
2960e7ba06eSJonas Devlieghere "failed to parse directory entry because "
2979ed452f3SPavel Labath "skipping the form value failed");
2982bc3873fSPaul Robinson }
2992bc3873fSPaul Robinson }
3002bc3873fSPaul Robinson }
3012bc3873fSPaul Robinson
3022bc3873fSPaul Robinson // Get the file entry description.
3037e02406fSJames Henderson llvm::Expected<ContentDescriptors> FileDescriptors =
3047e02406fSJames Henderson parseV5EntryFormat(DebugLineData, OffsetPtr, &ContentTypes);
3050e7ba06eSJonas Devlieghere if (!FileDescriptors)
3060e7ba06eSJonas Devlieghere return FileDescriptors.takeError();
3072bc3873fSPaul Robinson
3082bc3873fSPaul Robinson // Get the file entries, according to the format described above.
309d381b6a8SPavel Labath uint64_t FileEntryCount = DebugLineData.getULEB128(OffsetPtr);
310d381b6a8SPavel Labath for (uint64_t I = 0; I != FileEntryCount; ++I) {
3112bc3873fSPaul Robinson DWARFDebugLine::FileNameEntry FileEntry;
3120e7ba06eSJonas Devlieghere for (auto Descriptor : *FileDescriptors) {
31353a9f1d3SVlad Tsyrklevich DWARFFormValue Value(Descriptor.Form);
31453a9f1d3SVlad Tsyrklevich if (!Value.extractValue(DebugLineData, OffsetPtr, FormParams, &Ctx, U))
3150e7ba06eSJonas Devlieghere return createStringError(errc::invalid_argument,
3160e7ba06eSJonas Devlieghere "failed to parse file entry because "
3179ed452f3SPavel Labath "extracting the form value failed");
3182bc3873fSPaul Robinson switch (Descriptor.Type) {
3192bc3873fSPaul Robinson case DW_LNCT_path:
3200a22709fSPaul Robinson FileEntry.Name = Value;
3212bc3873fSPaul Robinson break;
32216c7bdafSScott Linder case DW_LNCT_LLVM_source:
32316c7bdafSScott Linder FileEntry.Source = Value;
32416c7bdafSScott Linder break;
3252bc3873fSPaul Robinson case DW_LNCT_directory_index:
326611ffcf4SKazu Hirata FileEntry.DirIdx = Value.getAsUnsignedConstant().value();
3272bc3873fSPaul Robinson break;
3282bc3873fSPaul Robinson case DW_LNCT_timestamp:
329611ffcf4SKazu Hirata FileEntry.ModTime = Value.getAsUnsignedConstant().value();
3302bc3873fSPaul Robinson break;
3312bc3873fSPaul Robinson case DW_LNCT_size:
332611ffcf4SKazu Hirata FileEntry.Length = Value.getAsUnsignedConstant().value();
3332bc3873fSPaul Robinson break;
334a06f8dccSPaul Robinson case DW_LNCT_MD5:
335611ffcf4SKazu Hirata if (!Value.getAsBlock() || Value.getAsBlock().value().size() != 16)
3360e7ba06eSJonas Devlieghere return createStringError(
3370e7ba06eSJonas Devlieghere errc::invalid_argument,
3380e7ba06eSJonas Devlieghere "failed to parse file entry because the MD5 hash is invalid");
339611ffcf4SKazu Hirata std::uninitialized_copy_n(Value.getAsBlock().value().begin(), 16,
340330268baSArgyrios Kyrtzidis FileEntry.Checksum.begin());
341a06f8dccSPaul Robinson break;
3422bc3873fSPaul Robinson default:
3432bc3873fSPaul Robinson break;
3442bc3873fSPaul Robinson }
3452bc3873fSPaul Robinson }
3462bc3873fSPaul Robinson FileNames.push_back(FileEntry);
3472bc3873fSPaul Robinson }
3480e7ba06eSJonas Devlieghere return Error::success();
3492bc3873fSPaul Robinson }
3502bc3873fSPaul Robinson
getLength() const351021f5317SJames Henderson uint64_t DWARFDebugLine::Prologue::getLength() const {
352021f5317SJames Henderson uint64_t Length = PrologueLength + sizeofTotalLength() +
353021f5317SJames Henderson sizeof(getVersion()) + sizeofPrologueLength();
354021f5317SJames Henderson if (getVersion() >= 5)
355021f5317SJames Henderson Length += 2; // Address + Segment selector sizes.
356021f5317SJames Henderson return Length;
357021f5317SJames Henderson }
358021f5317SJames Henderson
parse(DWARFDataExtractor DebugLineData,uint64_t * OffsetPtr,function_ref<void (Error)> RecoverableErrorHandler,const DWARFContext & Ctx,const DWARFUnit * U)3597116e431SJames Henderson Error DWARFDebugLine::Prologue::parse(
3606f55b5a1SPavel Labath DWARFDataExtractor DebugLineData, uint64_t *OffsetPtr,
361cc9b4fb6SAlexey Lapshin function_ref<void(Error)> RecoverableErrorHandler, const DWARFContext &Ctx,
362bf750c80SPaul Robinson const DWARFUnit *U) {
3639d4eb692SPaul Robinson const uint64_t PrologueOffset = *OffsetPtr;
36482af9438SZachary Turner
36582af9438SZachary Turner clear();
3669e09a54cSJames Henderson DataExtractor::Cursor Cursor(*OffsetPtr);
367d978656fSPavel Labath std::tie(TotalLength, FormParams.Format) =
3689e09a54cSJames Henderson DebugLineData.getInitialLength(Cursor);
369d978656fSPavel Labath
3709e09a54cSJames Henderson DebugLineData =
3719e09a54cSJames Henderson DWARFDataExtractor(DebugLineData, Cursor.tell() + TotalLength);
3729e09a54cSJames Henderson FormParams.Version = DebugLineData.getU16(Cursor);
3739e09a54cSJames Henderson if (Cursor && !versionIsSupported(getVersion())) {
3747116e431SJames Henderson // Treat this error as unrecoverable - we cannot be sure what any of
3757116e431SJames Henderson // the data represents including the length field, so cannot skip it or make
3767116e431SJames Henderson // any reasonable assumptions.
3779e09a54cSJames Henderson *OffsetPtr = Cursor.tell();
3781da62b51SJames Henderson return createStringError(
3791da62b51SJames Henderson errc::not_supported,
380cba595daSVictor Leschuk "parsing line table prologue at offset 0x%8.8" PRIx64
381d978656fSPavel Labath ": unsupported version %" PRIu16,
382a3acf99eSJames Henderson PrologueOffset, getVersion());
3839e09a54cSJames Henderson }
38482af9438SZachary Turner
38575c068c5SPaul Robinson if (getVersion() >= 5) {
3869e09a54cSJames Henderson FormParams.AddrSize = DebugLineData.getU8(Cursor);
3879e09a54cSJames Henderson assert((!Cursor || DebugLineData.getAddressSize() == 0 ||
38863811a47SPaul Robinson DebugLineData.getAddressSize() == getAddressSize()) &&
38975c068c5SPaul Robinson "Line table header and data extractor disagree");
3909e09a54cSJames Henderson SegSelectorSize = DebugLineData.getU8(Cursor);
3912bc3873fSPaul Robinson }
3922bc3873fSPaul Robinson
39344deaf7eSAlex Bradbury PrologueLength =
3949e09a54cSJames Henderson DebugLineData.getRelocatedValue(Cursor, sizeofPrologueLength());
3959e09a54cSJames Henderson const uint64_t EndPrologueOffset = PrologueLength + Cursor.tell();
3966f55b5a1SPavel Labath DebugLineData = DWARFDataExtractor(DebugLineData, EndPrologueOffset);
3979e09a54cSJames Henderson MinInstLength = DebugLineData.getU8(Cursor);
39875c068c5SPaul Robinson if (getVersion() >= 4)
3999e09a54cSJames Henderson MaxOpsPerInst = DebugLineData.getU8(Cursor);
4009e09a54cSJames Henderson DefaultIsStmt = DebugLineData.getU8(Cursor);
4019e09a54cSJames Henderson LineBase = DebugLineData.getU8(Cursor);
4029e09a54cSJames Henderson LineRange = DebugLineData.getU8(Cursor);
4039e09a54cSJames Henderson OpcodeBase = DebugLineData.getU8(Cursor);
40482af9438SZachary Turner
4059e09a54cSJames Henderson if (Cursor && OpcodeBase == 0) {
40623cf0a30SJames Henderson // If the opcode base is 0, we cannot read the standard opcode lengths (of
40723cf0a30SJames Henderson // which there are supposed to be one fewer than the opcode base). Assume
40823cf0a30SJames Henderson // there are no standard opcodes and continue parsing.
40923cf0a30SJames Henderson RecoverableErrorHandler(createStringError(
41023cf0a30SJames Henderson errc::invalid_argument,
41123cf0a30SJames Henderson "parsing line table prologue at offset 0x%8.8" PRIx64
41223cf0a30SJames Henderson " found opcode base of 0. Assuming no standard opcodes",
41323cf0a30SJames Henderson PrologueOffset));
4149e09a54cSJames Henderson } else if (Cursor) {
41582af9438SZachary Turner StandardOpcodeLengths.reserve(OpcodeBase - 1);
4169d4eb692SPaul Robinson for (uint32_t I = 1; I < OpcodeBase; ++I) {
4179e09a54cSJames Henderson uint8_t OpLen = DebugLineData.getU8(Cursor);
4189d4eb692SPaul Robinson StandardOpcodeLengths.push_back(OpLen);
41982af9438SZachary Turner }
42023cf0a30SJames Henderson }
42182af9438SZachary Turner
4229e09a54cSJames Henderson *OffsetPtr = Cursor.tell();
4239e09a54cSJames Henderson // A corrupt file name or directory table does not prevent interpretation of
4249e09a54cSJames Henderson // the main line program, so check the cursor state now so that its errors can
4259e09a54cSJames Henderson // be handled separately.
4269e09a54cSJames Henderson if (!Cursor)
4279e09a54cSJames Henderson return createStringError(
4289e09a54cSJames Henderson errc::invalid_argument,
4299e09a54cSJames Henderson "parsing line table prologue at offset 0x%8.8" PRIx64 ": %s",
4309e09a54cSJames Henderson PrologueOffset, toString(Cursor.takeError()).c_str());
4319e09a54cSJames Henderson
432c475856dSPavel Labath Error E =
433c475856dSPavel Labath getVersion() >= 5
434c475856dSPavel Labath ? parseV5DirFileTables(DebugLineData, OffsetPtr, FormParams, Ctx, U,
435c475856dSPavel Labath ContentTypes, IncludeDirectories, FileNames)
4366f55b5a1SPavel Labath : parseV2DirFileTables(DebugLineData, OffsetPtr, ContentTypes,
4376f55b5a1SPavel Labath IncludeDirectories, FileNames);
438c475856dSPavel Labath if (E) {
439cc9b4fb6SAlexey Lapshin RecoverableErrorHandler(joinErrors(
4400e7ba06eSJonas Devlieghere createStringError(
4410e7ba06eSJonas Devlieghere errc::invalid_argument,
44284e99265SJonas Devlieghere "parsing line table prologue at 0x%8.8" PRIx64
4432bc3873fSPaul Robinson " found an invalid directory or file table description at"
444f26a70a5SIgor Kudrin " 0x%8.8" PRIx64,
445f8552e67SJonas Devlieghere PrologueOffset, *OffsetPtr),
4467116e431SJames Henderson std::move(E)));
447c475856dSPavel Labath return Error::success();
448c475856dSPavel Labath }
44982af9438SZachary Turner
4506f55b5a1SPavel Labath assert(*OffsetPtr <= EndPrologueOffset);
4517116e431SJames Henderson if (*OffsetPtr != EndPrologueOffset) {
452cc9b4fb6SAlexey Lapshin RecoverableErrorHandler(createStringError(
4537116e431SJames Henderson errc::invalid_argument,
4546f55b5a1SPavel Labath "unknown data in line table prologue at offset 0x%8.8" PRIx64
4556f55b5a1SPavel Labath ": parsing ended (at offset 0x%8.8" PRIx64
456fccaa89eSPavel Labath ") before reaching the prologue end at offset 0x%8.8" PRIx64,
4576f55b5a1SPavel Labath PrologueOffset, *OffsetPtr, EndPrologueOffset));
4587116e431SJames Henderson }
459a3acf99eSJames Henderson return Error::success();
46082af9438SZachary Turner }
46182af9438SZachary Turner
Row(bool DefaultIsStmt)4629d4eb692SPaul Robinson DWARFDebugLine::Row::Row(bool DefaultIsStmt) { reset(DefaultIsStmt); }
46382af9438SZachary Turner
postAppend()46482af9438SZachary Turner void DWARFDebugLine::Row::postAppend() {
4656a285dfeSFangrui Song Discriminator = 0;
46682af9438SZachary Turner BasicBlock = false;
46782af9438SZachary Turner PrologueEnd = false;
46882af9438SZachary Turner EpilogueBegin = false;
46982af9438SZachary Turner }
47082af9438SZachary Turner
reset(bool DefaultIsStmt)4719d4eb692SPaul Robinson void DWARFDebugLine::Row::reset(bool DefaultIsStmt) {
47277fc1f60SAlexey Lapshin Address.Address = 0;
47377fc1f60SAlexey Lapshin Address.SectionIndex = object::SectionedAddress::UndefSection;
47482af9438SZachary Turner Line = 1;
47582af9438SZachary Turner Column = 0;
47682af9438SZachary Turner File = 1;
47782af9438SZachary Turner Isa = 0;
47882af9438SZachary Turner Discriminator = 0;
4799d4eb692SPaul Robinson IsStmt = DefaultIsStmt;
48082af9438SZachary Turner BasicBlock = false;
48182af9438SZachary Turner EndSequence = false;
48282af9438SZachary Turner PrologueEnd = false;
48382af9438SZachary Turner EpilogueBegin = false;
48482af9438SZachary Turner }
48582af9438SZachary Turner
dumpTableHeader(raw_ostream & OS,unsigned Indent)4869be3567dSFangrui Song void DWARFDebugLine::Row::dumpTableHeader(raw_ostream &OS, unsigned Indent) {
4879be3567dSFangrui Song OS.indent(Indent)
4889be3567dSFangrui Song << "Address Line Column File ISA Discriminator Flags\n";
4899be3567dSFangrui Song OS.indent(Indent)
4906707046fSGreg Clayton << "------------------ ------ ------ ------ --- ------------- "
4916707046fSGreg Clayton "-------------\n";
4926707046fSGreg Clayton }
4936707046fSGreg Clayton
dump(raw_ostream & OS) const49482af9438SZachary Turner void DWARFDebugLine::Row::dump(raw_ostream &OS) const {
49577fc1f60SAlexey Lapshin OS << format("0x%16.16" PRIx64 " %6u %6u", Address.Address, Line, Column)
49682af9438SZachary Turner << format(" %6u %3u %13u ", File, Isa, Discriminator)
4971b54fce3SDehao Chen << (IsStmt ? " is_stmt" : "") << (BasicBlock ? " basic_block" : "")
49882af9438SZachary Turner << (PrologueEnd ? " prologue_end" : "")
49982af9438SZachary Turner << (EpilogueBegin ? " epilogue_begin" : "")
5001b54fce3SDehao Chen << (EndSequence ? " end_sequence" : "") << '\n';
50182af9438SZachary Turner }
50282af9438SZachary Turner
Sequence()5031b54fce3SDehao Chen DWARFDebugLine::Sequence::Sequence() { reset(); }
50482af9438SZachary Turner
reset()50582af9438SZachary Turner void DWARFDebugLine::Sequence::reset() {
50682af9438SZachary Turner LowPC = 0;
50782af9438SZachary Turner HighPC = 0;
50877fc1f60SAlexey Lapshin SectionIndex = object::SectionedAddress::UndefSection;
50982af9438SZachary Turner FirstRowIndex = 0;
51082af9438SZachary Turner LastRowIndex = 0;
51182af9438SZachary Turner Empty = true;
51282af9438SZachary Turner }
51382af9438SZachary Turner
LineTable()5141b54fce3SDehao Chen DWARFDebugLine::LineTable::LineTable() { clear(); }
51582af9438SZachary Turner
dump(raw_ostream & OS,DIDumpOptions DumpOptions) const5160a22709fSPaul Robinson void DWARFDebugLine::LineTable::dump(raw_ostream &OS,
5170a22709fSPaul Robinson DIDumpOptions DumpOptions) const {
5180a22709fSPaul Robinson Prologue.dump(OS, DumpOptions);
51982af9438SZachary Turner
52082af9438SZachary Turner if (!Rows.empty()) {
5215666b70fSJames Henderson OS << '\n';
5229be3567dSFangrui Song Row::dumpTableHeader(OS, 0);
52382af9438SZachary Turner for (const Row &R : Rows) {
52482af9438SZachary Turner R.dump(OS);
52582af9438SZachary Turner }
52682af9438SZachary Turner }
52784a9756aSJames Henderson
52884a9756aSJames Henderson // Terminate the table with a final blank line to clearly delineate it from
52984a9756aSJames Henderson // later dumps.
53084a9756aSJames Henderson OS << '\n';
53182af9438SZachary Turner }
53282af9438SZachary Turner
clear()53382af9438SZachary Turner void DWARFDebugLine::LineTable::clear() {
53482af9438SZachary Turner Prologue.clear();
53582af9438SZachary Turner Rows.clear();
53682af9438SZachary Turner Sequences.clear();
53782af9438SZachary Turner }
53882af9438SZachary Turner
ParsingState(struct LineTable * LT,uint64_t TableOffset,function_ref<void (Error)> ErrorHandler)5398732192bSJames Henderson DWARFDebugLine::ParsingState::ParsingState(
5408732192bSJames Henderson struct LineTable *LT, uint64_t TableOffset,
5418732192bSJames Henderson function_ref<void(Error)> ErrorHandler)
5428732192bSJames Henderson : LineTable(LT), LineTableOffset(TableOffset), ErrorHandler(ErrorHandler) {
54382af9438SZachary Turner resetRowAndSequence();
54482af9438SZachary Turner }
54582af9438SZachary Turner
resetRowAndSequence()54682af9438SZachary Turner void DWARFDebugLine::ParsingState::resetRowAndSequence() {
54782af9438SZachary Turner Row.reset(LineTable->Prologue.DefaultIsStmt);
54882af9438SZachary Turner Sequence.reset();
54982af9438SZachary Turner }
55082af9438SZachary Turner
appendRowToMatrix()551c4c8bcaeSFangrui Song void DWARFDebugLine::ParsingState::appendRowToMatrix() {
55250a09670SFangrui Song unsigned RowNumber = LineTable->Rows.size();
55382af9438SZachary Turner if (Sequence.Empty) {
55482af9438SZachary Turner // Record the beginning of instruction sequence.
55582af9438SZachary Turner Sequence.Empty = false;
55677fc1f60SAlexey Lapshin Sequence.LowPC = Row.Address.Address;
55782af9438SZachary Turner Sequence.FirstRowIndex = RowNumber;
55882af9438SZachary Turner }
55982af9438SZachary Turner LineTable->appendRow(Row);
56082af9438SZachary Turner if (Row.EndSequence) {
56182af9438SZachary Turner // Record the end of instruction sequence.
56277fc1f60SAlexey Lapshin Sequence.HighPC = Row.Address.Address;
56350a09670SFangrui Song Sequence.LastRowIndex = RowNumber + 1;
56477fc1f60SAlexey Lapshin Sequence.SectionIndex = Row.Address.SectionIndex;
56582af9438SZachary Turner if (Sequence.isValid())
56682af9438SZachary Turner LineTable->appendSequence(Sequence);
56782af9438SZachary Turner Sequence.reset();
56882af9438SZachary Turner }
56982af9438SZachary Turner Row.postAppend();
57082af9438SZachary Turner }
57182af9438SZachary Turner
57282af9438SZachary Turner const DWARFDebugLine::LineTable *
getLineTable(uint64_t Offset) const573f26a70a5SIgor Kudrin DWARFDebugLine::getLineTable(uint64_t Offset) const {
5749d4eb692SPaul Robinson LineTableConstIter Pos = LineTableMap.find(Offset);
5759d4eb692SPaul Robinson if (Pos != LineTableMap.end())
5769d4eb692SPaul Robinson return &Pos->second;
57782af9438SZachary Turner return nullptr;
57882af9438SZachary Turner }
57982af9438SZachary Turner
getOrParseLineTable(DWARFDataExtractor & DebugLineData,uint64_t Offset,const DWARFContext & Ctx,const DWARFUnit * U,function_ref<void (Error)> RecoverableErrorHandler)580a3acf99eSJames Henderson Expected<const DWARFDebugLine::LineTable *> DWARFDebugLine::getOrParseLineTable(
581f26a70a5SIgor Kudrin DWARFDataExtractor &DebugLineData, uint64_t Offset, const DWARFContext &Ctx,
582cc9b4fb6SAlexey Lapshin const DWARFUnit *U, function_ref<void(Error)> RecoverableErrorHandler) {
58366702629SJames Henderson if (!DebugLineData.isValidOffset(Offset))
584f26a70a5SIgor Kudrin return createStringError(errc::invalid_argument, "offset 0x%8.8" PRIx64
585a3acf99eSJames Henderson " is not a valid debug line section offset",
586a3acf99eSJames Henderson Offset);
58766702629SJames Henderson
5889d4eb692SPaul Robinson std::pair<LineTableIter, bool> Pos =
5899d4eb692SPaul Robinson LineTableMap.insert(LineTableMapTy::value_type(Offset, LineTable()));
5909d4eb692SPaul Robinson LineTable *LT = &Pos.first->second;
5919d4eb692SPaul Robinson if (Pos.second) {
592004b729eSJames Henderson if (Error Err =
593cc9b4fb6SAlexey Lapshin LT->parse(DebugLineData, &Offset, Ctx, U, RecoverableErrorHandler))
594c55cf4afSBill Wendling return std::move(Err);
595a3acf99eSJames Henderson return LT;
59682af9438SZachary Turner }
59782af9438SZachary Turner return LT;
59882af9438SZachary Turner }
59982af9438SZachary Turner
clearLineTable(uint64_t Offset)6006c12ae81SHyoun Kyu Cho void DWARFDebugLine::clearLineTable(uint64_t Offset) {
6016c12ae81SHyoun Kyu Cho LineTableMap.erase(Offset);
6026c12ae81SHyoun Kyu Cho }
6036c12ae81SHyoun Kyu Cho
getOpcodeName(uint8_t Opcode,uint8_t OpcodeBase)6048732192bSJames Henderson static StringRef getOpcodeName(uint8_t Opcode, uint8_t OpcodeBase) {
6058732192bSJames Henderson assert(Opcode != 0);
6068732192bSJames Henderson if (Opcode < OpcodeBase)
6078732192bSJames Henderson return LNStandardString(Opcode);
6088732192bSJames Henderson return "special";
6098732192bSJames Henderson }
6108732192bSJames Henderson
advanceAddr(uint64_t OperationAdvance,uint8_t Opcode,uint64_t OpcodeOffset)6118732192bSJames Henderson uint64_t DWARFDebugLine::ParsingState::advanceAddr(uint64_t OperationAdvance,
6128732192bSJames Henderson uint8_t Opcode,
6138732192bSJames Henderson uint64_t OpcodeOffset) {
6148732192bSJames Henderson StringRef OpcodeName = getOpcodeName(Opcode, LineTable->Prologue.OpcodeBase);
6158732192bSJames Henderson // For versions less than 4, the MaxOpsPerInst member is set to 0, as the
6168732192bSJames Henderson // maximum_operations_per_instruction field wasn't introduced until DWARFv4.
6178732192bSJames Henderson // Don't warn about bad values in this situation.
6188732192bSJames Henderson if (ReportAdvanceAddrProblem && LineTable->Prologue.getVersion() >= 4 &&
6198732192bSJames Henderson LineTable->Prologue.MaxOpsPerInst != 1)
6208732192bSJames Henderson ErrorHandler(createStringError(
6218732192bSJames Henderson errc::not_supported,
6228732192bSJames Henderson "line table program at offset 0x%8.8" PRIx64
6238732192bSJames Henderson " contains a %s opcode at offset 0x%8.8" PRIx64
6248732192bSJames Henderson ", but the prologue maximum_operations_per_instruction value is %" PRId8
6258732192bSJames Henderson ", which is unsupported. Assuming a value of 1 instead",
6268732192bSJames Henderson LineTableOffset, OpcodeName.data(), OpcodeOffset,
6278732192bSJames Henderson LineTable->Prologue.MaxOpsPerInst));
628684d6fdeSJames Henderson if (ReportAdvanceAddrProblem && LineTable->Prologue.MinInstLength == 0)
629684d6fdeSJames Henderson ErrorHandler(
630684d6fdeSJames Henderson createStringError(errc::invalid_argument,
631684d6fdeSJames Henderson "line table program at offset 0x%8.8" PRIx64
632684d6fdeSJames Henderson " contains a %s opcode at offset 0x%8.8" PRIx64
633684d6fdeSJames Henderson ", but the prologue minimum_instruction_length value "
634684d6fdeSJames Henderson "is 0, which prevents any address advancing",
635684d6fdeSJames Henderson LineTableOffset, OpcodeName.data(), OpcodeOffset));
6368732192bSJames Henderson ReportAdvanceAddrProblem = false;
6370cd7a325SJames Henderson uint64_t AddrOffset = OperationAdvance * LineTable->Prologue.MinInstLength;
6380cd7a325SJames Henderson Row.Address.Address += AddrOffset;
6390cd7a325SJames Henderson return AddrOffset;
6400cd7a325SJames Henderson }
6410cd7a325SJames Henderson
6420cd7a325SJames Henderson DWARFDebugLine::ParsingState::AddrAndAdjustedOpcode
advanceAddrForOpcode(uint8_t Opcode,uint64_t OpcodeOffset)6438732192bSJames Henderson DWARFDebugLine::ParsingState::advanceAddrForOpcode(uint8_t Opcode,
6448732192bSJames Henderson uint64_t OpcodeOffset) {
6450cd7a325SJames Henderson assert(Opcode == DW_LNS_const_add_pc ||
6460cd7a325SJames Henderson Opcode >= LineTable->Prologue.OpcodeBase);
6476e0c9e46SJames Henderson if (ReportBadLineRange && LineTable->Prologue.LineRange == 0) {
6486e0c9e46SJames Henderson StringRef OpcodeName =
6496e0c9e46SJames Henderson getOpcodeName(Opcode, LineTable->Prologue.OpcodeBase);
6506e0c9e46SJames Henderson ErrorHandler(
6516e0c9e46SJames Henderson createStringError(errc::not_supported,
6526e0c9e46SJames Henderson "line table program at offset 0x%8.8" PRIx64
6536e0c9e46SJames Henderson " contains a %s opcode at offset 0x%8.8" PRIx64
6546e0c9e46SJames Henderson ", but the prologue line_range value is 0. The "
6556e0c9e46SJames Henderson "address and line will not be adjusted",
6566e0c9e46SJames Henderson LineTableOffset, OpcodeName.data(), OpcodeOffset));
6576e0c9e46SJames Henderson ReportBadLineRange = false;
6586e0c9e46SJames Henderson }
6596e0c9e46SJames Henderson
6600cd7a325SJames Henderson uint8_t OpcodeValue = Opcode;
6610cd7a325SJames Henderson if (Opcode == DW_LNS_const_add_pc)
6620cd7a325SJames Henderson OpcodeValue = 255;
6630cd7a325SJames Henderson uint8_t AdjustedOpcode = OpcodeValue - LineTable->Prologue.OpcodeBase;
6646e0c9e46SJames Henderson uint64_t OperationAdvance =
6656e0c9e46SJames Henderson LineTable->Prologue.LineRange != 0
6666e0c9e46SJames Henderson ? AdjustedOpcode / LineTable->Prologue.LineRange
6676e0c9e46SJames Henderson : 0;
6688732192bSJames Henderson uint64_t AddrOffset = advanceAddr(OperationAdvance, Opcode, OpcodeOffset);
6690cd7a325SJames Henderson return {AddrOffset, AdjustedOpcode};
6700cd7a325SJames Henderson }
6710cd7a325SJames Henderson
6720cd7a325SJames Henderson DWARFDebugLine::ParsingState::AddrAndLineDelta
handleSpecialOpcode(uint8_t Opcode,uint64_t OpcodeOffset)6738732192bSJames Henderson DWARFDebugLine::ParsingState::handleSpecialOpcode(uint8_t Opcode,
6748732192bSJames Henderson uint64_t OpcodeOffset) {
6750cd7a325SJames Henderson // A special opcode value is chosen based on the amount that needs
6760cd7a325SJames Henderson // to be added to the line and address registers. The maximum line
6770cd7a325SJames Henderson // increment for a special opcode is the value of the line_base
6780cd7a325SJames Henderson // field in the header, plus the value of the line_range field,
6790cd7a325SJames Henderson // minus 1 (line base + line range - 1). If the desired line
6800cd7a325SJames Henderson // increment is greater than the maximum line increment, a standard
6810cd7a325SJames Henderson // opcode must be used instead of a special opcode. The "address
6820cd7a325SJames Henderson // advance" is calculated by dividing the desired address increment
6830cd7a325SJames Henderson // by the minimum_instruction_length field from the header. The
6840cd7a325SJames Henderson // special opcode is then calculated using the following formula:
6850cd7a325SJames Henderson //
6860cd7a325SJames Henderson // opcode = (desired line increment - line_base) +
6870cd7a325SJames Henderson // (line_range * address advance) + opcode_base
6880cd7a325SJames Henderson //
6890cd7a325SJames Henderson // If the resulting opcode is greater than 255, a standard opcode
6900cd7a325SJames Henderson // must be used instead.
6910cd7a325SJames Henderson //
6920cd7a325SJames Henderson // To decode a special opcode, subtract the opcode_base from the
6930cd7a325SJames Henderson // opcode itself to give the adjusted opcode. The amount to
6940cd7a325SJames Henderson // increment the address register is the result of the adjusted
6950cd7a325SJames Henderson // opcode divided by the line_range multiplied by the
6960cd7a325SJames Henderson // minimum_instruction_length field from the header. That is:
6970cd7a325SJames Henderson //
6980cd7a325SJames Henderson // address increment = (adjusted opcode / line_range) *
6990cd7a325SJames Henderson // minimum_instruction_length
7000cd7a325SJames Henderson //
7010cd7a325SJames Henderson // The amount to increment the line register is the line_base plus
7020cd7a325SJames Henderson // the result of the adjusted opcode modulo the line_range. That is:
7030cd7a325SJames Henderson //
7040cd7a325SJames Henderson // line increment = line_base + (adjusted opcode % line_range)
7050cd7a325SJames Henderson
7060cd7a325SJames Henderson DWARFDebugLine::ParsingState::AddrAndAdjustedOpcode AddrAdvanceResult =
7078732192bSJames Henderson advanceAddrForOpcode(Opcode, OpcodeOffset);
7086e0c9e46SJames Henderson int32_t LineOffset = 0;
7096e0c9e46SJames Henderson if (LineTable->Prologue.LineRange != 0)
7106e0c9e46SJames Henderson LineOffset =
7110cd7a325SJames Henderson LineTable->Prologue.LineBase +
7120cd7a325SJames Henderson (AddrAdvanceResult.AdjustedOpcode % LineTable->Prologue.LineRange);
7130cd7a325SJames Henderson Row.Line += LineOffset;
7140cd7a325SJames Henderson return {AddrAdvanceResult.AddrDelta, LineOffset};
7150cd7a325SJames Henderson }
7160cd7a325SJames Henderson
7171a789047SJames Henderson /// Parse a ULEB128 using the specified \p Cursor. \returns the parsed value on
7181a789047SJames Henderson /// success, or None if \p Cursor is in a failing state.
7191a789047SJames Henderson template <typename T>
parseULEB128(DWARFDataExtractor & Data,DataExtractor::Cursor & Cursor)7201a789047SJames Henderson static Optional<T> parseULEB128(DWARFDataExtractor &Data,
7211a789047SJames Henderson DataExtractor::Cursor &Cursor) {
7221a789047SJames Henderson T Value = Data.getULEB128(Cursor);
7231a789047SJames Henderson if (Cursor)
7241a789047SJames Henderson return Value;
7251a789047SJames Henderson return None;
7261a789047SJames Henderson }
7271a789047SJames Henderson
parse(DWARFDataExtractor & DebugLineData,uint64_t * OffsetPtr,const DWARFContext & Ctx,const DWARFUnit * U,function_ref<void (Error)> RecoverableErrorHandler,raw_ostream * OS,bool Verbose)728a3acf99eSJames Henderson Error DWARFDebugLine::LineTable::parse(
729f26a70a5SIgor Kudrin DWARFDataExtractor &DebugLineData, uint64_t *OffsetPtr,
730a3acf99eSJames Henderson const DWARFContext &Ctx, const DWARFUnit *U,
731dbd26fe0SJames Henderson function_ref<void(Error)> RecoverableErrorHandler, raw_ostream *OS,
732dbd26fe0SJames Henderson bool Verbose) {
733dbd26fe0SJames Henderson assert((OS || !Verbose) && "cannot have verbose output without stream");
734f26a70a5SIgor Kudrin const uint64_t DebugLineOffset = *OffsetPtr;
73582af9438SZachary Turner
73682af9438SZachary Turner clear();
73782af9438SZachary Turner
738cc9b4fb6SAlexey Lapshin Error PrologueErr =
739cc9b4fb6SAlexey Lapshin Prologue.parse(DebugLineData, OffsetPtr, RecoverableErrorHandler, Ctx, U);
74082af9438SZachary Turner
7410a22709fSPaul Robinson if (OS) {
7420a22709fSPaul Robinson DIDumpOptions DumpOptions;
743dbd26fe0SJames Henderson DumpOptions.Verbose = Verbose;
7440a22709fSPaul Robinson Prologue.dump(*OS, DumpOptions);
7450a22709fSPaul Robinson }
74626f9a0c5SJonas Devlieghere
747e3547adeSJames Henderson if (PrologueErr) {
748e3547adeSJames Henderson // Ensure there is a blank line after the prologue to clearly delineate it
749e3547adeSJames Henderson // from later dumps.
750e3547adeSJames Henderson if (OS)
751e3547adeSJames Henderson *OS << "\n";
752a3acf99eSJames Henderson return PrologueErr;
753e3547adeSJames Henderson }
754a3acf99eSJames Henderson
755216796f2SJames Henderson uint64_t ProgramLength = Prologue.TotalLength + Prologue.sizeofTotalLength();
756216796f2SJames Henderson if (!DebugLineData.isValidOffsetForDataOfSize(DebugLineOffset,
757216796f2SJames Henderson ProgramLength)) {
758216796f2SJames Henderson assert(DebugLineData.size() > DebugLineOffset &&
759216796f2SJames Henderson "prologue parsing should handle invalid offset");
760216796f2SJames Henderson uint64_t BytesRemaining = DebugLineData.size() - DebugLineOffset;
761cc9b4fb6SAlexey Lapshin RecoverableErrorHandler(
762216796f2SJames Henderson createStringError(errc::invalid_argument,
763216796f2SJames Henderson "line table program with offset 0x%8.8" PRIx64
764216796f2SJames Henderson " has length 0x%8.8" PRIx64 " but only 0x%8.8" PRIx64
765216796f2SJames Henderson " bytes are available",
766216796f2SJames Henderson DebugLineOffset, ProgramLength, BytesRemaining));
767216796f2SJames Henderson // Continue by capping the length at the number of remaining bytes.
768216796f2SJames Henderson ProgramLength = BytesRemaining;
769216796f2SJames Henderson }
770216796f2SJames Henderson
771e8bcf4efSJames Henderson // Create a DataExtractor which can only see the data up to the end of the
772e8bcf4efSJames Henderson // table, to prevent reading past the end.
773216796f2SJames Henderson const uint64_t EndOffset = DebugLineOffset + ProgramLength;
774e8bcf4efSJames Henderson DWARFDataExtractor TableData(DebugLineData, EndOffset);
77582af9438SZachary Turner
776511b54caSPaul Robinson // See if we should tell the data extractor the address size.
777e8bcf4efSJames Henderson if (TableData.getAddressSize() == 0)
778e8bcf4efSJames Henderson TableData.setAddressSize(Prologue.getAddressSize());
779511b54caSPaul Robinson else
780511b54caSPaul Robinson assert(Prologue.getAddressSize() == 0 ||
781e8bcf4efSJames Henderson Prologue.getAddressSize() == TableData.getAddressSize());
782511b54caSPaul Robinson
7838732192bSJames Henderson ParsingState State(this, DebugLineOffset, RecoverableErrorHandler);
78482af9438SZachary Turner
785c475856dSPavel Labath *OffsetPtr = DebugLineOffset + Prologue.getLength();
7869be3567dSFangrui Song if (OS && *OffsetPtr < EndOffset) {
7879be3567dSFangrui Song *OS << '\n';
788dbd26fe0SJames Henderson Row::dumpTableHeader(*OS, /*Indent=*/Verbose ? 12 : 0);
7899be3567dSFangrui Song }
7908036cf7fSDavid Blaikie bool TombstonedAddress = false;
7918036cf7fSDavid Blaikie auto EmitRow = [&] {
7928036cf7fSDavid Blaikie if (!TombstonedAddress) {
7938036cf7fSDavid Blaikie if (Verbose) {
7948036cf7fSDavid Blaikie *OS << "\n";
7958036cf7fSDavid Blaikie OS->indent(12);
7968036cf7fSDavid Blaikie }
7978036cf7fSDavid Blaikie if (OS)
7988036cf7fSDavid Blaikie State.Row.dump(*OS);
7998036cf7fSDavid Blaikie State.appendRowToMatrix();
8008036cf7fSDavid Blaikie }
8018036cf7fSDavid Blaikie };
8029d4eb692SPaul Robinson while (*OffsetPtr < EndOffset) {
803b21794a9SJames Henderson DataExtractor::Cursor Cursor(*OffsetPtr);
804b21794a9SJames Henderson
805dbd26fe0SJames Henderson if (Verbose)
806f26a70a5SIgor Kudrin *OS << format("0x%08.08" PRIx64 ": ", *OffsetPtr);
80726f9a0c5SJonas Devlieghere
8088732192bSJames Henderson uint64_t OpcodeOffset = *OffsetPtr;
809b21794a9SJames Henderson uint8_t Opcode = TableData.getU8(Cursor);
810e3547adeSJames Henderson size_t RowCount = Rows.size();
81182af9438SZachary Turner
812b21794a9SJames Henderson if (Cursor && Verbose)
81326f9a0c5SJonas Devlieghere *OS << format("%02.02" PRIx8 " ", Opcode);
81426f9a0c5SJonas Devlieghere
8159d4eb692SPaul Robinson if (Opcode == 0) {
81682af9438SZachary Turner // Extended Opcodes always start with a zero opcode followed by
81782af9438SZachary Turner // a uleb128 length so you can skip ones you don't know about
8185777570dSJames Henderson uint64_t Len = TableData.getULEB128(Cursor);
8195777570dSJames Henderson uint64_t ExtOffset = Cursor.tell();
820e0833349SPaul Robinson
821e0833349SPaul Robinson // Tolerate zero-length; assume length is correct and soldier on.
822e0833349SPaul Robinson if (Len == 0) {
8239782c922SJames Henderson if (Cursor && Verbose)
824e0833349SPaul Robinson *OS << "Badly formed extended line op (length 0)\n";
8259782c922SJames Henderson if (!Cursor) {
8269782c922SJames Henderson if (Verbose)
8279782c922SJames Henderson *OS << "\n";
8285777570dSJames Henderson RecoverableErrorHandler(Cursor.takeError());
8299782c922SJames Henderson }
8305777570dSJames Henderson *OffsetPtr = Cursor.tell();
831e0833349SPaul Robinson continue;
832e0833349SPaul Robinson }
83382af9438SZachary Turner
8345777570dSJames Henderson uint8_t SubOpcode = TableData.getU8(Cursor);
8359782c922SJames Henderson // OperandOffset will be the same as ExtOffset, if it was not possible to
8369782c922SJames Henderson // read the SubOpcode.
8379782c922SJames Henderson uint64_t OperandOffset = Cursor.tell();
838dbd26fe0SJames Henderson if (Verbose)
83926f9a0c5SJonas Devlieghere *OS << LNExtendedString(SubOpcode);
8409d4eb692SPaul Robinson switch (SubOpcode) {
84182af9438SZachary Turner case DW_LNE_end_sequence:
84282af9438SZachary Turner // Set the end_sequence register of the state machine to true and
84382af9438SZachary Turner // append a row to the matrix using the current values of the
84482af9438SZachary Turner // state-machine registers. Then reset the registers to the initial
84582af9438SZachary Turner // values specified above. Every statement program sequence must end
84682af9438SZachary Turner // with a DW_LNE_end_sequence instruction which creates a row whose
84782af9438SZachary Turner // address is that of the byte after the last target machine instruction
84882af9438SZachary Turner // of the sequence.
84982af9438SZachary Turner State.Row.EndSequence = true;
8509782c922SJames Henderson // No need to test the Cursor is valid here, since it must be to get
8519782c922SJames Henderson // into this code path - if it were invalid, the default case would be
8529782c922SJames Henderson // followed.
8538036cf7fSDavid Blaikie EmitRow();
85482af9438SZachary Turner State.resetRowAndSequence();
85582af9438SZachary Turner break;
85682af9438SZachary Turner
85782af9438SZachary Turner case DW_LNE_set_address:
85882af9438SZachary Turner // Takes a single relocatable address as an operand. The size of the
85982af9438SZachary Turner // operand is the size appropriate to hold an address on the target
86082af9438SZachary Turner // machine. Set the address register to the value given by the
86182af9438SZachary Turner // relocatable address. All of the other statement program opcodes
86282af9438SZachary Turner // that affect the address register add a delta to it. This instruction
86382af9438SZachary Turner // stores a relocatable value into it instead.
864511b54caSPaul Robinson //
865511b54caSPaul Robinson // Make sure the extractor knows the address size. If not, infer it
866511b54caSPaul Robinson // from the size of the operand.
86707804f75SJames Henderson {
868e8bcf4efSJames Henderson uint8_t ExtractorAddressSize = TableData.getAddressSize();
869fe6983a7SJames Henderson uint64_t OpcodeAddressSize = Len - 1;
870fe6983a7SJames Henderson if (ExtractorAddressSize != OpcodeAddressSize &&
871fe6983a7SJames Henderson ExtractorAddressSize != 0)
872cc9b4fb6SAlexey Lapshin RecoverableErrorHandler(createStringError(
87307804f75SJames Henderson errc::invalid_argument,
874f26a70a5SIgor Kudrin "mismatching address size at offset 0x%8.8" PRIx64
875a3acf99eSJames Henderson " expected 0x%2.2" PRIx8 " found 0x%2.2" PRIx64,
87607804f75SJames Henderson ExtOffset, ExtractorAddressSize, Len - 1));
87707804f75SJames Henderson
87807804f75SJames Henderson // Assume that the line table is correct and temporarily override the
879fe6983a7SJames Henderson // address size. If the size is unsupported, give up trying to read
880fe6983a7SJames Henderson // the address and continue to the next opcode.
881fe6983a7SJames Henderson if (OpcodeAddressSize != 1 && OpcodeAddressSize != 2 &&
882fe6983a7SJames Henderson OpcodeAddressSize != 4 && OpcodeAddressSize != 8) {
883fe6983a7SJames Henderson RecoverableErrorHandler(createStringError(
884fe6983a7SJames Henderson errc::invalid_argument,
885fe6983a7SJames Henderson "address size 0x%2.2" PRIx64
886fe6983a7SJames Henderson " of DW_LNE_set_address opcode at offset 0x%8.8" PRIx64
887fe6983a7SJames Henderson " is unsupported",
888fe6983a7SJames Henderson OpcodeAddressSize, ExtOffset));
8895777570dSJames Henderson TableData.skip(Cursor, OpcodeAddressSize);
890fe6983a7SJames Henderson } else {
891e8bcf4efSJames Henderson TableData.setAddressSize(OpcodeAddressSize);
892e8bcf4efSJames Henderson State.Row.Address.Address = TableData.getRelocatedAddress(
8935777570dSJames Henderson Cursor, &State.Row.Address.SectionIndex);
89407804f75SJames Henderson
8958036cf7fSDavid Blaikie uint64_t Tombstone =
8968036cf7fSDavid Blaikie dwarf::computeTombstoneAddress(OpcodeAddressSize);
8978036cf7fSDavid Blaikie TombstonedAddress = State.Row.Address.Address == Tombstone;
8988036cf7fSDavid Blaikie
89907804f75SJames Henderson // Restore the address size if the extractor already had it.
90007804f75SJames Henderson if (ExtractorAddressSize != 0)
901e8bcf4efSJames Henderson TableData.setAddressSize(ExtractorAddressSize);
902fe6983a7SJames Henderson }
90307804f75SJames Henderson
904628a3194SDavid Blaikie if (Cursor && Verbose) {
905628a3194SDavid Blaikie *OS << " (";
906628a3194SDavid Blaikie DWARFFormValue::dumpAddress(*OS, OpcodeAddressSize, State.Row.Address.Address);
907628a3194SDavid Blaikie *OS << ')';
908628a3194SDavid Blaikie }
90907804f75SJames Henderson }
91082af9438SZachary Turner break;
91182af9438SZachary Turner
91282af9438SZachary Turner case DW_LNE_define_file:
91382af9438SZachary Turner // Takes 4 arguments. The first is a null terminated string containing
91482af9438SZachary Turner // a source file name. The second is an unsigned LEB128 number
91582af9438SZachary Turner // representing the directory index of the directory in which the file
91682af9438SZachary Turner // was found. The third is an unsigned LEB128 number representing the
91782af9438SZachary Turner // time of last modification of the file. The fourth is an unsigned
91882af9438SZachary Turner // LEB128 number representing the length in bytes of the file. The time
91982af9438SZachary Turner // and length fields may contain LEB128(0) if the information is not
92082af9438SZachary Turner // available.
92182af9438SZachary Turner //
92282af9438SZachary Turner // The directory index represents an entry in the include_directories
92382af9438SZachary Turner // section of the statement program prologue. The index is LEB128(0)
92482af9438SZachary Turner // if the file was found in the current directory of the compilation,
92582af9438SZachary Turner // LEB128(1) if it was found in the first directory in the
92682af9438SZachary Turner // include_directories section, and so on. The directory index is
92782af9438SZachary Turner // ignored for file names that represent full path names.
92882af9438SZachary Turner //
92982af9438SZachary Turner // The files are numbered, starting at 1, in the order in which they
93082af9438SZachary Turner // appear; the names in the prologue come before names defined by
93182af9438SZachary Turner // the DW_LNE_define_file instruction. These numbers are used in the
93282af9438SZachary Turner // the file register of the state machine.
93382af9438SZachary Turner {
9349d4eb692SPaul Robinson FileNameEntry FileEntry;
9355777570dSJames Henderson const char *Name = TableData.getCStr(Cursor);
936bb111152SJonas Devlieghere FileEntry.Name =
937bb111152SJonas Devlieghere DWARFFormValue::createFromPValue(dwarf::DW_FORM_string, Name);
9385777570dSJames Henderson FileEntry.DirIdx = TableData.getULEB128(Cursor);
9395777570dSJames Henderson FileEntry.ModTime = TableData.getULEB128(Cursor);
9405777570dSJames Henderson FileEntry.Length = TableData.getULEB128(Cursor);
9419d4eb692SPaul Robinson Prologue.FileNames.push_back(FileEntry);
9429782c922SJames Henderson if (Cursor && Verbose)
9430a22709fSPaul Robinson *OS << " (" << Name << ", dir=" << FileEntry.DirIdx << ", mod_time="
94426f9a0c5SJonas Devlieghere << format("(0x%16.16" PRIx64 ")", FileEntry.ModTime)
94526f9a0c5SJonas Devlieghere << ", length=" << FileEntry.Length << ")";
94682af9438SZachary Turner }
94782af9438SZachary Turner break;
94882af9438SZachary Turner
94982af9438SZachary Turner case DW_LNE_set_discriminator:
9505777570dSJames Henderson State.Row.Discriminator = TableData.getULEB128(Cursor);
9519782c922SJames Henderson if (Cursor && Verbose)
95226f9a0c5SJonas Devlieghere *OS << " (" << State.Row.Discriminator << ")";
95382af9438SZachary Turner break;
95482af9438SZachary Turner
95582af9438SZachary Turner default:
9569782c922SJames Henderson if (Cursor && Verbose)
957e0833349SPaul Robinson *OS << format("Unrecognized extended op 0x%02.02" PRIx8, SubOpcode)
958e0833349SPaul Robinson << format(" length %" PRIx64, Len);
959e0833349SPaul Robinson // Len doesn't include the zero opcode byte or the length itself, but
960e0833349SPaul Robinson // it does include the sub_opcode, so we have to adjust for that.
9615777570dSJames Henderson TableData.skip(Cursor, Len - 1);
96282af9438SZachary Turner break;
96382af9438SZachary Turner }
964f1be770fSJames Henderson // Make sure the length as recorded in the table and the standard length
965f1be770fSJames Henderson // for the opcode match. If they don't, continue from the end as claimed
9665777570dSJames Henderson // by the table. Similarly, continue from the claimed end in the event of
9675777570dSJames Henderson // a parsing error.
968f1be770fSJames Henderson uint64_t End = ExtOffset + Len;
969b21794a9SJames Henderson if (Cursor && Cursor.tell() != End)
970cc9b4fb6SAlexey Lapshin RecoverableErrorHandler(createStringError(
971f1be770fSJames Henderson errc::illegal_byte_sequence,
972f26a70a5SIgor Kudrin "unexpected line op length at offset 0x%8.8" PRIx64
973f26a70a5SIgor Kudrin " expected 0x%2.2" PRIx64 " found 0x%2.2" PRIx64,
9745777570dSJames Henderson ExtOffset, Len, Cursor.tell() - ExtOffset));
9759782c922SJames Henderson if (!Cursor && Verbose) {
9769782c922SJames Henderson DWARFDataExtractor::Cursor ByteCursor(OperandOffset);
9779782c922SJames Henderson uint8_t Byte = TableData.getU8(ByteCursor);
9789782c922SJames Henderson if (ByteCursor) {
9799782c922SJames Henderson *OS << " (<parsing error>";
9809782c922SJames Henderson do {
9819782c922SJames Henderson *OS << format(" %2.2" PRIx8, Byte);
9829782c922SJames Henderson Byte = TableData.getU8(ByteCursor);
9839782c922SJames Henderson } while (ByteCursor);
9849782c922SJames Henderson *OS << ")";
9859782c922SJames Henderson }
9869782c922SJames Henderson
9879782c922SJames Henderson // The only parse failure in this case should be if the end was reached.
9889782c922SJames Henderson // In that case, throw away the error, as the main Cursor's error will
9899782c922SJames Henderson // be sufficient.
9909782c922SJames Henderson consumeError(ByteCursor.takeError());
9919782c922SJames Henderson }
992f1be770fSJames Henderson *OffsetPtr = End;
9939d4eb692SPaul Robinson } else if (Opcode < Prologue.OpcodeBase) {
994dbd26fe0SJames Henderson if (Verbose)
99526f9a0c5SJonas Devlieghere *OS << LNStandardString(Opcode);
9969d4eb692SPaul Robinson switch (Opcode) {
99782af9438SZachary Turner // Standard Opcodes
99882af9438SZachary Turner case DW_LNS_copy:
99982af9438SZachary Turner // Takes no arguments. Append a row to the matrix using the
10006a285dfeSFangrui Song // current values of the state-machine registers.
10018036cf7fSDavid Blaikie EmitRow();
100282af9438SZachary Turner break;
100382af9438SZachary Turner
100482af9438SZachary Turner case DW_LNS_advance_pc:
100582af9438SZachary Turner // Takes a single unsigned LEB128 operand, multiplies it by the
100682af9438SZachary Turner // min_inst_length field of the prologue, and adds the
100782af9438SZachary Turner // result to the address register of the state machine.
10081a789047SJames Henderson if (Optional<uint64_t> Operand =
10091a789047SJames Henderson parseULEB128<uint64_t>(TableData, Cursor)) {
10101a789047SJames Henderson uint64_t AddrOffset =
10111a789047SJames Henderson State.advanceAddr(*Operand, Opcode, OpcodeOffset);
1012dbd26fe0SJames Henderson if (Verbose)
101326f9a0c5SJonas Devlieghere *OS << " (" << AddrOffset << ")";
101426f9a0c5SJonas Devlieghere }
101582af9438SZachary Turner break;
101682af9438SZachary Turner
101782af9438SZachary Turner case DW_LNS_advance_line:
101882af9438SZachary Turner // Takes a single signed LEB128 operand and adds that value to
101982af9438SZachary Turner // the line register of the state machine.
10201a789047SJames Henderson {
10211a789047SJames Henderson int64_t LineDelta = TableData.getSLEB128(Cursor);
10221a789047SJames Henderson if (Cursor) {
10231a789047SJames Henderson State.Row.Line += LineDelta;
1024dbd26fe0SJames Henderson if (Verbose)
102526f9a0c5SJonas Devlieghere *OS << " (" << State.Row.Line << ")";
10261a789047SJames Henderson }
10271a789047SJames Henderson }
102882af9438SZachary Turner break;
102982af9438SZachary Turner
103082af9438SZachary Turner case DW_LNS_set_file:
103182af9438SZachary Turner // Takes a single unsigned LEB128 operand and stores it in the file
103282af9438SZachary Turner // register of the state machine.
10331a789047SJames Henderson if (Optional<uint16_t> File =
10341a789047SJames Henderson parseULEB128<uint16_t>(TableData, Cursor)) {
10351a789047SJames Henderson State.Row.File = *File;
1036dbd26fe0SJames Henderson if (Verbose)
103726f9a0c5SJonas Devlieghere *OS << " (" << State.Row.File << ")";
10381a789047SJames Henderson }
103982af9438SZachary Turner break;
104082af9438SZachary Turner
104182af9438SZachary Turner case DW_LNS_set_column:
104282af9438SZachary Turner // Takes a single unsigned LEB128 operand and stores it in the
104382af9438SZachary Turner // column register of the state machine.
10441a789047SJames Henderson if (Optional<uint16_t> Column =
10451a789047SJames Henderson parseULEB128<uint16_t>(TableData, Cursor)) {
10461a789047SJames Henderson State.Row.Column = *Column;
1047dbd26fe0SJames Henderson if (Verbose)
104826f9a0c5SJonas Devlieghere *OS << " (" << State.Row.Column << ")";
10491a789047SJames Henderson }
105082af9438SZachary Turner break;
105182af9438SZachary Turner
105282af9438SZachary Turner case DW_LNS_negate_stmt:
105382af9438SZachary Turner // Takes no arguments. Set the is_stmt register of the state
105482af9438SZachary Turner // machine to the logical negation of its current value.
105582af9438SZachary Turner State.Row.IsStmt = !State.Row.IsStmt;
105682af9438SZachary Turner break;
105782af9438SZachary Turner
105882af9438SZachary Turner case DW_LNS_set_basic_block:
105982af9438SZachary Turner // Takes no arguments. Set the basic_block register of the
106082af9438SZachary Turner // state machine to true
106182af9438SZachary Turner State.Row.BasicBlock = true;
106282af9438SZachary Turner break;
106382af9438SZachary Turner
106482af9438SZachary Turner case DW_LNS_const_add_pc:
106582af9438SZachary Turner // Takes no arguments. Add to the address register of the state
106682af9438SZachary Turner // machine the address increment value corresponding to special
106782af9438SZachary Turner // opcode 255. The motivation for DW_LNS_const_add_pc is this:
106882af9438SZachary Turner // when the statement program needs to advance the address by a
106982af9438SZachary Turner // small amount, it can use a single special opcode, which occupies
107082af9438SZachary Turner // a single byte. When it needs to advance the address by up to
107182af9438SZachary Turner // twice the range of the last special opcode, it can use
107282af9438SZachary Turner // DW_LNS_const_add_pc followed by a special opcode, for a total
107382af9438SZachary Turner // of two bytes. Only if it needs to advance the address by more
107482af9438SZachary Turner // than twice that range will it need to use both DW_LNS_advance_pc
107582af9438SZachary Turner // and a special opcode, requiring three or more bytes.
107682af9438SZachary Turner {
10778732192bSJames Henderson uint64_t AddrOffset =
10788732192bSJames Henderson State.advanceAddrForOpcode(Opcode, OpcodeOffset).AddrDelta;
1079dbd26fe0SJames Henderson if (Verbose)
10800cd7a325SJames Henderson *OS << format(" (0x%16.16" PRIx64 ")", AddrOffset);
108182af9438SZachary Turner }
108282af9438SZachary Turner break;
108382af9438SZachary Turner
108482af9438SZachary Turner case DW_LNS_fixed_advance_pc:
108582af9438SZachary Turner // Takes a single uhalf operand. Add to the address register of
108682af9438SZachary Turner // the state machine the value of the (unencoded) operand. This
108782af9438SZachary Turner // is the only extended opcode that takes an argument that is not
108882af9438SZachary Turner // a variable length number. The motivation for DW_LNS_fixed_advance_pc
108982af9438SZachary Turner // is this: existing assemblers cannot emit DW_LNS_advance_pc or
109082af9438SZachary Turner // special opcodes because they cannot encode LEB128 numbers or
109182af9438SZachary Turner // judge when the computation of a special opcode overflows and
109282af9438SZachary Turner // requires the use of DW_LNS_advance_pc. Such assemblers, however,
109382af9438SZachary Turner // can use DW_LNS_fixed_advance_pc instead, sacrificing compression.
109426f9a0c5SJonas Devlieghere {
10951a789047SJames Henderson uint16_t PCOffset =
10961a789047SJames Henderson TableData.getRelocatedValue(Cursor, 2);
10971a789047SJames Henderson if (Cursor) {
109877fc1f60SAlexey Lapshin State.Row.Address.Address += PCOffset;
1099dbd26fe0SJames Henderson if (Verbose)
1100dbd26fe0SJames Henderson *OS << format(" (0x%4.4" PRIx16 ")", PCOffset);
110126f9a0c5SJonas Devlieghere }
11021a789047SJames Henderson }
110382af9438SZachary Turner break;
110482af9438SZachary Turner
110582af9438SZachary Turner case DW_LNS_set_prologue_end:
110682af9438SZachary Turner // Takes no arguments. Set the prologue_end register of the
110782af9438SZachary Turner // state machine to true
110882af9438SZachary Turner State.Row.PrologueEnd = true;
110982af9438SZachary Turner break;
111082af9438SZachary Turner
111182af9438SZachary Turner case DW_LNS_set_epilogue_begin:
111282af9438SZachary Turner // Takes no arguments. Set the basic_block register of the
111382af9438SZachary Turner // state machine to true
111482af9438SZachary Turner State.Row.EpilogueBegin = true;
111582af9438SZachary Turner break;
111682af9438SZachary Turner
111782af9438SZachary Turner case DW_LNS_set_isa:
111882af9438SZachary Turner // Takes a single unsigned LEB128 operand and stores it in the
11191a789047SJames Henderson // ISA register of the state machine.
11201a789047SJames Henderson if (Optional<uint8_t> Isa = parseULEB128<uint8_t>(TableData, Cursor)) {
11211a789047SJames Henderson State.Row.Isa = *Isa;
1122dbd26fe0SJames Henderson if (Verbose)
11232f815502SJames Henderson *OS << " (" << (uint64_t)State.Row.Isa << ")";
11241a789047SJames Henderson }
112582af9438SZachary Turner break;
112682af9438SZachary Turner
112782af9438SZachary Turner default:
112882af9438SZachary Turner // Handle any unknown standard opcodes here. We know the lengths
112982af9438SZachary Turner // of such opcodes because they are specified in the prologue
113082af9438SZachary Turner // as a multiple of LEB128 operands for each opcode.
113182af9438SZachary Turner {
11329d4eb692SPaul Robinson assert(Opcode - 1U < Prologue.StandardOpcodeLengths.size());
11333b7ec64dSJames Henderson if (Verbose)
11343b7ec64dSJames Henderson *OS << "Unrecognized standard opcode";
11359d4eb692SPaul Robinson uint8_t OpcodeLength = Prologue.StandardOpcodeLengths[Opcode - 1];
11361a789047SJames Henderson std::vector<uint64_t> Operands;
113726f9a0c5SJonas Devlieghere for (uint8_t I = 0; I < OpcodeLength; ++I) {
11381a789047SJames Henderson if (Optional<uint64_t> Value =
11391a789047SJames Henderson parseULEB128<uint64_t>(TableData, Cursor))
11401a789047SJames Henderson Operands.push_back(*Value);
11411a789047SJames Henderson else
11421a789047SJames Henderson break;
11433b7ec64dSJames Henderson }
11441a789047SJames Henderson if (Verbose && !Operands.empty()) {
11451a789047SJames Henderson *OS << " (operands: ";
11461a789047SJames Henderson bool First = true;
11471a789047SJames Henderson for (uint64_t Value : Operands) {
11481a789047SJames Henderson if (!First)
11491a789047SJames Henderson *OS << ", ";
11501a789047SJames Henderson First = false;
11511a789047SJames Henderson *OS << format("0x%16.16" PRIx64, Value);
11523b7ec64dSJames Henderson }
1153dbd26fe0SJames Henderson if (Verbose)
115481cca987SFangrui Song *OS << ')';
115526f9a0c5SJonas Devlieghere }
115682af9438SZachary Turner }
115782af9438SZachary Turner break;
115882af9438SZachary Turner }
11591a789047SJames Henderson
11601a789047SJames Henderson *OffsetPtr = Cursor.tell();
116182af9438SZachary Turner } else {
11620cd7a325SJames Henderson // Special Opcodes.
11638732192bSJames Henderson ParsingState::AddrAndLineDelta Delta =
11648732192bSJames Henderson State.handleSpecialOpcode(Opcode, OpcodeOffset);
116526f9a0c5SJonas Devlieghere
11668036cf7fSDavid Blaikie if (Verbose)
11678036cf7fSDavid Blaikie *OS << "address += " << Delta.Address << ", line += " << Delta.Line;
11688036cf7fSDavid Blaikie EmitRow();
1169b21794a9SJames Henderson *OffsetPtr = Cursor.tell();
117082af9438SZachary Turner }
1171e3547adeSJames Henderson
1172e3547adeSJames Henderson // When a row is added to the matrix, it is also dumped, which includes a
1173e3547adeSJames Henderson // new line already, so don't add an extra one.
1174e3547adeSJames Henderson if (Verbose && Rows.size() == RowCount)
117526f9a0c5SJonas Devlieghere *OS << "\n";
1176b21794a9SJames Henderson
1177b21794a9SJames Henderson // Most parse failures other than when parsing extended opcodes are due to
1178b21794a9SJames Henderson // failures to read ULEBs. Bail out of parsing, since we don't know where to
1179b21794a9SJames Henderson // continue reading from as there is no stated length for such byte
1180b21794a9SJames Henderson // sequences. Print the final trailing new line if needed before doing so.
1181b21794a9SJames Henderson if (!Cursor && Opcode != 0) {
1182b21794a9SJames Henderson if (Verbose)
1183b21794a9SJames Henderson *OS << "\n";
1184b21794a9SJames Henderson return Cursor.takeError();
1185b21794a9SJames Henderson }
1186b21794a9SJames Henderson
1187b21794a9SJames Henderson if (!Cursor)
1188b21794a9SJames Henderson RecoverableErrorHandler(Cursor.takeError());
118982af9438SZachary Turner }
119082af9438SZachary Turner
119184e99265SJonas Devlieghere if (!State.Sequence.Empty)
1192cc9b4fb6SAlexey Lapshin RecoverableErrorHandler(createStringError(
11936e3ca962SJames Henderson errc::illegal_byte_sequence,
11946e3ca962SJames Henderson "last sequence in debug line table at offset 0x%8.8" PRIx64
11956e3ca962SJames Henderson " is not terminated",
11966e3ca962SJames Henderson DebugLineOffset));
119782af9438SZachary Turner
119882af9438SZachary Turner // Sort all sequences so that address lookup will work faster.
119982af9438SZachary Turner if (!Sequences.empty()) {
12009b22c469SFangrui Song llvm::sort(Sequences, Sequence::orderByHighPC);
120182af9438SZachary Turner // Note: actually, instruction address ranges of sequences should not
120282af9438SZachary Turner // overlap (in shared objects and executables). If they do, the address
120382af9438SZachary Turner // lookup would still work, though, but result would be ambiguous.
120482af9438SZachary Turner // We don't report warning in this case. For example,
120582af9438SZachary Turner // sometimes .so compiled from multiple object files contains a few
120682af9438SZachary Turner // rudimentary sequences for address ranges [0x0, 0xsomething).
120782af9438SZachary Turner }
120882af9438SZachary Turner
1209dbd26fe0SJames Henderson // Terminate the table with a final blank line to clearly delineate it from
1210dbd26fe0SJames Henderson // later dumps.
1211dbd26fe0SJames Henderson if (OS)
1212dbd26fe0SJames Henderson *OS << "\n";
1213dbd26fe0SJames Henderson
1214a3acf99eSJames Henderson return Error::success();
121582af9438SZachary Turner }
121682af9438SZachary Turner
findRowInSeq(const DWARFDebugLine::Sequence & Seq,object::SectionedAddress Address) const121777fc1f60SAlexey Lapshin uint32_t DWARFDebugLine::LineTable::findRowInSeq(
121877fc1f60SAlexey Lapshin const DWARFDebugLine::Sequence &Seq,
121977fc1f60SAlexey Lapshin object::SectionedAddress Address) const {
12209d4eb692SPaul Robinson if (!Seq.containsPC(Address))
1221c2c6018cSKeno Fischer return UnknownRowIndex;
122277fc1f60SAlexey Lapshin assert(Seq.SectionIndex == Address.SectionIndex);
1223b3be23d3SFangrui Song // In some cases, e.g. first instruction in a function, the compiler generates
1224b3be23d3SFangrui Song // two entries, both with the same address. We want the last one.
1225b3be23d3SFangrui Song //
1226b3be23d3SFangrui Song // In general we want a non-empty range: the last row whose address is less
1227b3be23d3SFangrui Song // than or equal to Address. This can be computed as upper_bound - 1.
12289d4eb692SPaul Robinson DWARFDebugLine::Row Row;
12299d4eb692SPaul Robinson Row.Address = Address;
12309d4eb692SPaul Robinson RowIter FirstRow = Rows.begin() + Seq.FirstRowIndex;
12319d4eb692SPaul Robinson RowIter LastRow = Rows.begin() + Seq.LastRowIndex;
1232b3be23d3SFangrui Song assert(FirstRow->Address.Address <= Row.Address.Address &&
1233b3be23d3SFangrui Song Row.Address.Address < LastRow[-1].Address.Address);
1234b3be23d3SFangrui Song RowIter RowPos = std::upper_bound(FirstRow + 1, LastRow - 1, Row,
1235b3be23d3SFangrui Song DWARFDebugLine::Row::orderByAddress) -
1236b3be23d3SFangrui Song 1;
123777fc1f60SAlexey Lapshin assert(Seq.SectionIndex == RowPos->Address.SectionIndex);
1238b3be23d3SFangrui Song return RowPos - Rows.begin();
1239c2c6018cSKeno Fischer }
1240c2c6018cSKeno Fischer
lookupAddress(object::SectionedAddress Address) const124177fc1f60SAlexey Lapshin uint32_t DWARFDebugLine::LineTable::lookupAddress(
124277fc1f60SAlexey Lapshin object::SectionedAddress Address) const {
124377fc1f60SAlexey Lapshin
124477fc1f60SAlexey Lapshin // Search for relocatable addresses
124577fc1f60SAlexey Lapshin uint32_t Result = lookupAddressImpl(Address);
124677fc1f60SAlexey Lapshin
124777fc1f60SAlexey Lapshin if (Result != UnknownRowIndex ||
124877fc1f60SAlexey Lapshin Address.SectionIndex == object::SectionedAddress::UndefSection)
124977fc1f60SAlexey Lapshin return Result;
125077fc1f60SAlexey Lapshin
125177fc1f60SAlexey Lapshin // Search for absolute addresses
125277fc1f60SAlexey Lapshin Address.SectionIndex = object::SectionedAddress::UndefSection;
125377fc1f60SAlexey Lapshin return lookupAddressImpl(Address);
125477fc1f60SAlexey Lapshin }
125577fc1f60SAlexey Lapshin
lookupAddressImpl(object::SectionedAddress Address) const125677fc1f60SAlexey Lapshin uint32_t DWARFDebugLine::LineTable::lookupAddressImpl(
125777fc1f60SAlexey Lapshin object::SectionedAddress Address) const {
125882af9438SZachary Turner // First, find an instruction sequence containing the given address.
12599d4eb692SPaul Robinson DWARFDebugLine::Sequence Sequence;
126077fc1f60SAlexey Lapshin Sequence.SectionIndex = Address.SectionIndex;
12619b22c469SFangrui Song Sequence.HighPC = Address.Address;
12629b22c469SFangrui Song SequenceIter It = llvm::upper_bound(Sequences, Sequence,
12639b22c469SFangrui Song DWARFDebugLine::Sequence::orderByHighPC);
12649b22c469SFangrui Song if (It == Sequences.end() || It->SectionIndex != Address.SectionIndex)
1265c2c6018cSKeno Fischer return UnknownRowIndex;
12669b22c469SFangrui Song return findRowInSeq(*It, Address);
126782af9438SZachary Turner }
126882af9438SZachary Turner
lookupAddressRange(object::SectionedAddress Address,uint64_t Size,std::vector<uint32_t> & Result) const126982af9438SZachary Turner bool DWARFDebugLine::LineTable::lookupAddressRange(
127077fc1f60SAlexey Lapshin object::SectionedAddress Address, uint64_t Size,
127177fc1f60SAlexey Lapshin std::vector<uint32_t> &Result) const {
127277fc1f60SAlexey Lapshin
127377fc1f60SAlexey Lapshin // Search for relocatable addresses
127477fc1f60SAlexey Lapshin if (lookupAddressRangeImpl(Address, Size, Result))
127577fc1f60SAlexey Lapshin return true;
127677fc1f60SAlexey Lapshin
127777fc1f60SAlexey Lapshin if (Address.SectionIndex == object::SectionedAddress::UndefSection)
127877fc1f60SAlexey Lapshin return false;
127977fc1f60SAlexey Lapshin
128077fc1f60SAlexey Lapshin // Search for absolute addresses
128177fc1f60SAlexey Lapshin Address.SectionIndex = object::SectionedAddress::UndefSection;
128277fc1f60SAlexey Lapshin return lookupAddressRangeImpl(Address, Size, Result);
128377fc1f60SAlexey Lapshin }
128477fc1f60SAlexey Lapshin
lookupAddressRangeImpl(object::SectionedAddress Address,uint64_t Size,std::vector<uint32_t> & Result) const128577fc1f60SAlexey Lapshin bool DWARFDebugLine::LineTable::lookupAddressRangeImpl(
128677fc1f60SAlexey Lapshin object::SectionedAddress Address, uint64_t Size,
128777fc1f60SAlexey Lapshin std::vector<uint32_t> &Result) const {
128882af9438SZachary Turner if (Sequences.empty())
128982af9438SZachary Turner return false;
129077fc1f60SAlexey Lapshin uint64_t EndAddr = Address.Address + Size;
129182af9438SZachary Turner // First, find an instruction sequence containing the given address.
12929d4eb692SPaul Robinson DWARFDebugLine::Sequence Sequence;
129377fc1f60SAlexey Lapshin Sequence.SectionIndex = Address.SectionIndex;
12949b22c469SFangrui Song Sequence.HighPC = Address.Address;
12959d4eb692SPaul Robinson SequenceIter LastSeq = Sequences.end();
12969b22c469SFangrui Song SequenceIter SeqPos = llvm::upper_bound(
12979b22c469SFangrui Song Sequences, Sequence, DWARFDebugLine::Sequence::orderByHighPC);
12989b22c469SFangrui Song if (SeqPos == LastSeq || !SeqPos->containsPC(Address))
129982af9438SZachary Turner return false;
130082af9438SZachary Turner
13019d4eb692SPaul Robinson SequenceIter StartPos = SeqPos;
130282af9438SZachary Turner
130382af9438SZachary Turner // Add the rows from the first sequence to the vector, starting with the
130482af9438SZachary Turner // index we just calculated
130582af9438SZachary Turner
13069d4eb692SPaul Robinson while (SeqPos != LastSeq && SeqPos->LowPC < EndAddr) {
13079d4eb692SPaul Robinson const DWARFDebugLine::Sequence &CurSeq = *SeqPos;
130882af9438SZachary Turner // For the first sequence, we need to find which row in the sequence is the
1309c2c6018cSKeno Fischer // first in our range.
13109d4eb692SPaul Robinson uint32_t FirstRowIndex = CurSeq.FirstRowIndex;
13119d4eb692SPaul Robinson if (SeqPos == StartPos)
13129d4eb692SPaul Robinson FirstRowIndex = findRowInSeq(CurSeq, Address);
131382af9438SZachary Turner
1314c2c6018cSKeno Fischer // Figure out the last row in the range.
131577fc1f60SAlexey Lapshin uint32_t LastRowIndex =
131677fc1f60SAlexey Lapshin findRowInSeq(CurSeq, {EndAddr - 1, Address.SectionIndex});
13179d4eb692SPaul Robinson if (LastRowIndex == UnknownRowIndex)
13189d4eb692SPaul Robinson LastRowIndex = CurSeq.LastRowIndex - 1;
131982af9438SZachary Turner
13209d4eb692SPaul Robinson assert(FirstRowIndex != UnknownRowIndex);
13219d4eb692SPaul Robinson assert(LastRowIndex != UnknownRowIndex);
1322c2c6018cSKeno Fischer
13239d4eb692SPaul Robinson for (uint32_t I = FirstRowIndex; I <= LastRowIndex; ++I) {
13249d4eb692SPaul Robinson Result.push_back(I);
132582af9438SZachary Turner }
132682af9438SZachary Turner
13279d4eb692SPaul Robinson ++SeqPos;
132882af9438SZachary Turner }
132982af9438SZachary Turner
133082af9438SZachary Turner return true;
133182af9438SZachary Turner }
133282af9438SZachary Turner
getSourceByIndex(uint64_t FileIndex,FileLineInfoKind Kind) const133316c7bdafSScott Linder Optional<StringRef> DWARFDebugLine::LineTable::getSourceByIndex(uint64_t FileIndex,
133416c7bdafSScott Linder FileLineInfoKind Kind) const {
1335ca16d280SJonas Devlieghere if (Kind == FileLineInfoKind::None || !Prologue.hasFileAtIndex(FileIndex))
133616c7bdafSScott Linder return None;
1337ca16d280SJonas Devlieghere const FileNameEntry &Entry = Prologue.getFileNameEntry(FileIndex);
133871e5488aSDavid Blaikie if (auto E = dwarf::toString(Entry.Source))
133971e5488aSDavid Blaikie return StringRef(*E);
134016c7bdafSScott Linder return None;
134116c7bdafSScott Linder }
134216c7bdafSScott Linder
isPathAbsoluteOnWindowsOrPosix(const Twine & Path)134382d60d6bSEugene Zemtsov static bool isPathAbsoluteOnWindowsOrPosix(const Twine &Path) {
134482d60d6bSEugene Zemtsov // Debug info can contain paths from any OS, not necessarily
134582d60d6bSEugene Zemtsov // an OS we're currently running on. Moreover different compilation units can
134682d60d6bSEugene Zemtsov // be compiled on different operating systems and linked together later.
134782d60d6bSEugene Zemtsov return sys::path::is_absolute(Path, sys::path::Style::posix) ||
134882d60d6bSEugene Zemtsov sys::path::is_absolute(Path, sys::path::Style::windows);
134982d60d6bSEugene Zemtsov }
135082d60d6bSEugene Zemtsov
getFileNameByIndex(uint64_t FileIndex,StringRef CompDir,FileLineInfoKind Kind,std::string & Result,sys::path::Style Style) const1351de0ce98aSJonas Devlieghere bool DWARFDebugLine::Prologue::getFileNameByIndex(
1352de0ce98aSJonas Devlieghere uint64_t FileIndex, StringRef CompDir, FileLineInfoKind Kind,
1353de0ce98aSJonas Devlieghere std::string &Result, sys::path::Style Style) const {
1354b2ba776aSPete Cooper if (Kind == FileLineInfoKind::None || !hasFileAtIndex(FileIndex))
135582af9438SZachary Turner return false;
1356783d84bbSAli Tamur const FileNameEntry &Entry = getFileNameEntry(FileIndex);
135771e5488aSDavid Blaikie auto E = dwarf::toString(Entry.Name);
135871e5488aSDavid Blaikie if (!E)
1359c75aac42SJonas Devlieghere return false;
136071e5488aSDavid Blaikie StringRef FileName = *E;
13615de4ba17SSterling Augustine if (Kind == FileLineInfoKind::RawValue ||
136282d60d6bSEugene Zemtsov isPathAbsoluteOnWindowsOrPosix(FileName)) {
1363adcd0268SBenjamin Kramer Result = std::string(FileName);
136482af9438SZachary Turner return true;
136582af9438SZachary Turner }
13665de4ba17SSterling Augustine if (Kind == FileLineInfoKind::BaseNameOnly) {
13675de4ba17SSterling Augustine Result = std::string(llvm::sys::path::filename(FileName));
13685de4ba17SSterling Augustine return true;
13695de4ba17SSterling Augustine }
137082af9438SZachary Turner
137182af9438SZachary Turner SmallString<16> FilePath;
1372ba1c9156SPaul Robinson StringRef IncludeDir;
137382af9438SZachary Turner // Be defensive about the contents of Entry.
1374ca16d280SJonas Devlieghere if (getVersion() >= 5) {
1375f027cfa3SSterling Augustine // DirIdx 0 is the compilation directory, so don't include it for
1376f027cfa3SSterling Augustine // relative names.
1377f027cfa3SSterling Augustine if ((Entry.DirIdx != 0 || Kind != FileLineInfoKind::RelativeFilePath) &&
1378f027cfa3SSterling Augustine Entry.DirIdx < IncludeDirectories.size())
137971e5488aSDavid Blaikie IncludeDir = dwarf::toStringRef(IncludeDirectories[Entry.DirIdx]);
138001ee172eSJonas Devlieghere } else {
1381ca16d280SJonas Devlieghere if (0 < Entry.DirIdx && Entry.DirIdx <= IncludeDirectories.size())
138271e5488aSDavid Blaikie IncludeDir = dwarf::toStringRef(IncludeDirectories[Entry.DirIdx - 1]);
13830758ac4eSSterling Augustine }
1384f027cfa3SSterling Augustine
1385*655a1cbdSFangrui Song // For absolute paths only, include the compilation directory of compile unit,
1386*655a1cbdSFangrui Song // unless v5 DirIdx == 0 (IncludeDir indicates the compilation directory). We
1387*655a1cbdSFangrui Song // know that FileName is not absolute, the only way to have an absolute path
1388*655a1cbdSFangrui Song // at this point would be if IncludeDir is absolute.
1389*655a1cbdSFangrui Song if (Kind == FileLineInfoKind::AbsoluteFilePath &&
1390*655a1cbdSFangrui Song (getVersion() < 5 || Entry.DirIdx != 0) && !CompDir.empty() &&
1391417375d7SSterling Augustine !isPathAbsoluteOnWindowsOrPosix(IncludeDir))
1392de0ce98aSJonas Devlieghere sys::path::append(FilePath, Style, CompDir);
139382af9438SZachary Turner
1394417375d7SSterling Augustine assert((Kind == FileLineInfoKind::AbsoluteFilePath ||
1395417375d7SSterling Augustine Kind == FileLineInfoKind::RelativeFilePath) &&
1396417375d7SSterling Augustine "invalid FileLineInfo Kind");
1397417375d7SSterling Augustine
139882af9438SZachary Turner // sys::path::append skips empty strings.
1399de0ce98aSJonas Devlieghere sys::path::append(FilePath, Style, IncludeDir, FileName);
1400adcd0268SBenjamin Kramer Result = std::string(FilePath.str());
140182af9438SZachary Turner return true;
140282af9438SZachary Turner }
140382af9438SZachary Turner
getFileLineInfoForAddress(object::SectionedAddress Address,const char * CompDir,FileLineInfoKind Kind,DILineInfo & Result) const14041b54fce3SDehao Chen bool DWARFDebugLine::LineTable::getFileLineInfoForAddress(
140577fc1f60SAlexey Lapshin object::SectionedAddress Address, const char *CompDir,
140677fc1f60SAlexey Lapshin FileLineInfoKind Kind, DILineInfo &Result) const {
140782af9438SZachary Turner // Get the index of row we're looking for in the line table.
140882af9438SZachary Turner uint32_t RowIndex = lookupAddress(Address);
140982af9438SZachary Turner if (RowIndex == -1U)
141082af9438SZachary Turner return false;
141182af9438SZachary Turner // Take file number and line/column from the row.
141282af9438SZachary Turner const auto &Row = Rows[RowIndex];
141382af9438SZachary Turner if (!getFileNameByIndex(Row.File, CompDir, Kind, Result.FileName))
141482af9438SZachary Turner return false;
141582af9438SZachary Turner Result.Line = Row.Line;
141682af9438SZachary Turner Result.Column = Row.Column;
1417ba1024cfSEric Christopher Result.Discriminator = Row.Discriminator;
141816c7bdafSScott Linder Result.Source = getSourceByIndex(Row.File, Kind);
141982af9438SZachary Turner return true;
142082af9438SZachary Turner }
1421a3acf99eSJames Henderson
1422a3acf99eSJames Henderson // We want to supply the Unit associated with a .debug_line[.dwo] table when
1423a3acf99eSJames Henderson // we dump it, if possible, but still dump the table even if there isn't a Unit.
1424a3acf99eSJames Henderson // Therefore, collect up handles on all the Units that point into the
1425a3acf99eSJames Henderson // line-table section.
1426a3acf99eSJames Henderson static DWARFDebugLine::SectionParser::LineToUnitMap
buildLineToUnitMap(DWARFUnitVector::iterator_range Units)142751a50534SDavid Blaikie buildLineToUnitMap(DWARFUnitVector::iterator_range Units) {
1428a3acf99eSJames Henderson DWARFDebugLine::SectionParser::LineToUnitMap LineToUnit;
142951a50534SDavid Blaikie for (const auto &U : Units)
143051a50534SDavid Blaikie if (auto CUDIE = U->getUnitDIE())
1431a3acf99eSJames Henderson if (auto StmtOffset = toSectionOffset(CUDIE.find(DW_AT_stmt_list)))
143251a50534SDavid Blaikie LineToUnit.insert(std::make_pair(*StmtOffset, &*U));
1433a3acf99eSJames Henderson return LineToUnit;
1434a3acf99eSJames Henderson }
1435a3acf99eSJames Henderson
SectionParser(DWARFDataExtractor & Data,const DWARFContext & C,DWARFUnitVector::iterator_range Units)143651a50534SDavid Blaikie DWARFDebugLine::SectionParser::SectionParser(
143751a50534SDavid Blaikie DWARFDataExtractor &Data, const DWARFContext &C,
143851a50534SDavid Blaikie DWARFUnitVector::iterator_range Units)
1439a3acf99eSJames Henderson : DebugLineData(Data), Context(C) {
144051a50534SDavid Blaikie LineToUnit = buildLineToUnitMap(Units);
1441a3acf99eSJames Henderson if (!DebugLineData.isValidOffset(Offset))
1442a3acf99eSJames Henderson Done = true;
1443a3acf99eSJames Henderson }
1444a3acf99eSJames Henderson
totalLengthIsValid() const1445a3acf99eSJames Henderson bool DWARFDebugLine::Prologue::totalLengthIsValid() const {
1446d978656fSPavel Labath return TotalLength != 0u;
1447a3acf99eSJames Henderson }
1448a3acf99eSJames Henderson
parseNext(function_ref<void (Error)> RecoverableErrorHandler,function_ref<void (Error)> UnrecoverableErrorHandler,raw_ostream * OS,bool Verbose)1449a3acf99eSJames Henderson DWARFDebugLine::LineTable DWARFDebugLine::SectionParser::parseNext(
1450cc9b4fb6SAlexey Lapshin function_ref<void(Error)> RecoverableErrorHandler,
1451dbd26fe0SJames Henderson function_ref<void(Error)> UnrecoverableErrorHandler, raw_ostream *OS,
1452dbd26fe0SJames Henderson bool Verbose) {
1453a3acf99eSJames Henderson assert(DebugLineData.isValidOffset(Offset) &&
1454a3acf99eSJames Henderson "parsing should have terminated");
1455a3acf99eSJames Henderson DWARFUnit *U = prepareToParse(Offset);
1456f26a70a5SIgor Kudrin uint64_t OldOffset = Offset;
1457a3acf99eSJames Henderson LineTable LT;
1458004b729eSJames Henderson if (Error Err = LT.parse(DebugLineData, &Offset, Context, U,
1459dbd26fe0SJames Henderson RecoverableErrorHandler, OS, Verbose))
1460cc9b4fb6SAlexey Lapshin UnrecoverableErrorHandler(std::move(Err));
1461a3acf99eSJames Henderson moveToNextTable(OldOffset, LT.Prologue);
1462a3acf99eSJames Henderson return LT;
1463a3acf99eSJames Henderson }
1464a3acf99eSJames Henderson
skip(function_ref<void (Error)> RecoverableErrorHandler,function_ref<void (Error)> UnrecoverableErrorHandler)1465a3acf99eSJames Henderson void DWARFDebugLine::SectionParser::skip(
1466cc9b4fb6SAlexey Lapshin function_ref<void(Error)> RecoverableErrorHandler,
1467cc9b4fb6SAlexey Lapshin function_ref<void(Error)> UnrecoverableErrorHandler) {
1468a3acf99eSJames Henderson assert(DebugLineData.isValidOffset(Offset) &&
1469a3acf99eSJames Henderson "parsing should have terminated");
1470a3acf99eSJames Henderson DWARFUnit *U = prepareToParse(Offset);
1471f26a70a5SIgor Kudrin uint64_t OldOffset = Offset;
1472a3acf99eSJames Henderson LineTable LT;
14737116e431SJames Henderson if (Error Err = LT.Prologue.parse(DebugLineData, &Offset,
1474cc9b4fb6SAlexey Lapshin RecoverableErrorHandler, Context, U))
1475cc9b4fb6SAlexey Lapshin UnrecoverableErrorHandler(std::move(Err));
1476a3acf99eSJames Henderson moveToNextTable(OldOffset, LT.Prologue);
1477a3acf99eSJames Henderson }
1478a3acf99eSJames Henderson
prepareToParse(uint64_t Offset)1479f26a70a5SIgor Kudrin DWARFUnit *DWARFDebugLine::SectionParser::prepareToParse(uint64_t Offset) {
1480a3acf99eSJames Henderson DWARFUnit *U = nullptr;
1481a3acf99eSJames Henderson auto It = LineToUnit.find(Offset);
1482a3acf99eSJames Henderson if (It != LineToUnit.end())
1483a3acf99eSJames Henderson U = It->second;
1484a3acf99eSJames Henderson DebugLineData.setAddressSize(U ? U->getAddressByteSize() : 0);
1485a3acf99eSJames Henderson return U;
1486a3acf99eSJames Henderson }
1487a3acf99eSJames Henderson
moveToNextTable(uint64_t OldOffset,const Prologue & P)1488f26a70a5SIgor Kudrin void DWARFDebugLine::SectionParser::moveToNextTable(uint64_t OldOffset,
1489a3acf99eSJames Henderson const Prologue &P) {
1490a3acf99eSJames Henderson // If the length field is not valid, we don't know where the next table is, so
1491a3acf99eSJames Henderson // cannot continue to parse. Mark the parser as done, and leave the Offset
1492a3acf99eSJames Henderson // value as it currently is. This will be the end of the bad length field.
1493a3acf99eSJames Henderson if (!P.totalLengthIsValid()) {
1494a3acf99eSJames Henderson Done = true;
1495a3acf99eSJames Henderson return;
1496a3acf99eSJames Henderson }
1497a3acf99eSJames Henderson
1498a3acf99eSJames Henderson Offset = OldOffset + P.TotalLength + P.sizeofTotalLength();
1499a3acf99eSJames Henderson if (!DebugLineData.isValidOffset(Offset)) {
1500a3acf99eSJames Henderson Done = true;
1501a3acf99eSJames Henderson }
1502a3acf99eSJames Henderson }
1503