xref: /freebsd-src/contrib/llvm-project/llvm/lib/DebugInfo/DWARF/DWARFDebugLine.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric //===- DWARFDebugLine.cpp -------------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFDebugLine.h"
100b57cec5SDimitry Andric #include "llvm/ADT/SmallString.h"
110b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h"
120b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h"
130b57cec5SDimitry Andric #include "llvm/BinaryFormat/Dwarf.h"
1481ad6265SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h"
1581ad6265SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFDie.h"
160b57cec5SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
170b57cec5SDimitry Andric #include "llvm/Support/Errc.h"
180b57cec5SDimitry Andric #include "llvm/Support/Format.h"
195ffd83dbSDimitry Andric #include "llvm/Support/FormatVariadic.h"
200b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
210b57cec5SDimitry Andric #include <algorithm>
220b57cec5SDimitry Andric #include <cassert>
230b57cec5SDimitry Andric #include <cinttypes>
240b57cec5SDimitry Andric #include <cstdint>
250b57cec5SDimitry Andric #include <cstdio>
260b57cec5SDimitry Andric #include <utility>
270b57cec5SDimitry Andric 
280b57cec5SDimitry Andric using namespace llvm;
290b57cec5SDimitry Andric using namespace dwarf;
300b57cec5SDimitry Andric 
310b57cec5SDimitry Andric using FileLineInfoKind = DILineInfoSpecifier::FileLineInfoKind;
320b57cec5SDimitry Andric 
330b57cec5SDimitry Andric namespace {
340b57cec5SDimitry Andric 
350b57cec5SDimitry Andric struct ContentDescriptor {
360b57cec5SDimitry Andric   dwarf::LineNumberEntryFormat Type;
370b57cec5SDimitry Andric   dwarf::Form Form;
380b57cec5SDimitry Andric };
390b57cec5SDimitry Andric 
400b57cec5SDimitry Andric using ContentDescriptors = SmallVector<ContentDescriptor, 4>;
410b57cec5SDimitry Andric 
42480093f4SDimitry Andric } // end anonymous namespace
430b57cec5SDimitry Andric 
445ffd83dbSDimitry Andric static bool versionIsSupported(uint16_t Version) {
455ffd83dbSDimitry Andric   return Version >= 2 && Version <= 5;
465ffd83dbSDimitry Andric }
475ffd83dbSDimitry Andric 
480b57cec5SDimitry Andric void DWARFDebugLine::ContentTypeTracker::trackContentType(
490b57cec5SDimitry Andric     dwarf::LineNumberEntryFormat ContentType) {
500b57cec5SDimitry Andric   switch (ContentType) {
510b57cec5SDimitry Andric   case dwarf::DW_LNCT_timestamp:
520b57cec5SDimitry Andric     HasModTime = true;
530b57cec5SDimitry Andric     break;
540b57cec5SDimitry Andric   case dwarf::DW_LNCT_size:
550b57cec5SDimitry Andric     HasLength = true;
560b57cec5SDimitry Andric     break;
570b57cec5SDimitry Andric   case dwarf::DW_LNCT_MD5:
580b57cec5SDimitry Andric     HasMD5 = true;
590b57cec5SDimitry Andric     break;
600b57cec5SDimitry Andric   case dwarf::DW_LNCT_LLVM_source:
610b57cec5SDimitry Andric     HasSource = true;
620b57cec5SDimitry Andric     break;
630b57cec5SDimitry Andric   default:
640b57cec5SDimitry Andric     // We only care about values we consider optional, and new values may be
650b57cec5SDimitry Andric     // added in the vendor extension range, so we do not match exhaustively.
660b57cec5SDimitry Andric     break;
670b57cec5SDimitry Andric   }
680b57cec5SDimitry Andric }
690b57cec5SDimitry Andric 
700b57cec5SDimitry Andric DWARFDebugLine::Prologue::Prologue() { clear(); }
710b57cec5SDimitry Andric 
720b57cec5SDimitry Andric bool DWARFDebugLine::Prologue::hasFileAtIndex(uint64_t FileIndex) const {
730b57cec5SDimitry Andric   uint16_t DwarfVersion = getVersion();
740b57cec5SDimitry Andric   assert(DwarfVersion != 0 &&
750b57cec5SDimitry Andric          "line table prologue has no dwarf version information");
760b57cec5SDimitry Andric   if (DwarfVersion >= 5)
770b57cec5SDimitry Andric     return FileIndex < FileNames.size();
780b57cec5SDimitry Andric   return FileIndex != 0 && FileIndex <= FileNames.size();
790b57cec5SDimitry Andric }
800b57cec5SDimitry Andric 
81bdd1243dSDimitry Andric std::optional<uint64_t>
82bdd1243dSDimitry Andric DWARFDebugLine::Prologue::getLastValidFileIndex() const {
83e8d8bef9SDimitry Andric   if (FileNames.empty())
84bdd1243dSDimitry Andric     return std::nullopt;
85e8d8bef9SDimitry Andric   uint16_t DwarfVersion = getVersion();
86e8d8bef9SDimitry Andric   assert(DwarfVersion != 0 &&
87e8d8bef9SDimitry Andric          "line table prologue has no dwarf version information");
88e8d8bef9SDimitry Andric   // In DWARF v5 the file names are 0-indexed.
89e8d8bef9SDimitry Andric   if (DwarfVersion >= 5)
90e8d8bef9SDimitry Andric     return FileNames.size() - 1;
91e8d8bef9SDimitry Andric   return FileNames.size();
92e8d8bef9SDimitry Andric }
93e8d8bef9SDimitry Andric 
940b57cec5SDimitry Andric const llvm::DWARFDebugLine::FileNameEntry &
950b57cec5SDimitry Andric DWARFDebugLine::Prologue::getFileNameEntry(uint64_t Index) const {
960b57cec5SDimitry Andric   uint16_t DwarfVersion = getVersion();
970b57cec5SDimitry Andric   assert(DwarfVersion != 0 &&
980b57cec5SDimitry Andric          "line table prologue has no dwarf version information");
990b57cec5SDimitry Andric   // In DWARF v5 the file names are 0-indexed.
1000b57cec5SDimitry Andric   if (DwarfVersion >= 5)
1010b57cec5SDimitry Andric     return FileNames[Index];
1020b57cec5SDimitry Andric   return FileNames[Index - 1];
1030b57cec5SDimitry Andric }
1040b57cec5SDimitry Andric 
1050b57cec5SDimitry Andric void DWARFDebugLine::Prologue::clear() {
1060b57cec5SDimitry Andric   TotalLength = PrologueLength = 0;
1070b57cec5SDimitry Andric   SegSelectorSize = 0;
1080b57cec5SDimitry Andric   MinInstLength = MaxOpsPerInst = DefaultIsStmt = LineBase = LineRange = 0;
1090b57cec5SDimitry Andric   OpcodeBase = 0;
1100b57cec5SDimitry Andric   FormParams = dwarf::FormParams({0, 0, DWARF32});
1110b57cec5SDimitry Andric   ContentTypes = ContentTypeTracker();
1120b57cec5SDimitry Andric   StandardOpcodeLengths.clear();
1130b57cec5SDimitry Andric   IncludeDirectories.clear();
1140b57cec5SDimitry Andric   FileNames.clear();
1150b57cec5SDimitry Andric }
1160b57cec5SDimitry Andric 
1170b57cec5SDimitry Andric void DWARFDebugLine::Prologue::dump(raw_ostream &OS,
1180b57cec5SDimitry Andric                                     DIDumpOptions DumpOptions) const {
1195ffd83dbSDimitry Andric   if (!totalLengthIsValid())
1205ffd83dbSDimitry Andric     return;
1215ffd83dbSDimitry Andric   int OffsetDumpWidth = 2 * dwarf::getDwarfOffsetByteSize(FormParams.Format);
1220b57cec5SDimitry Andric   OS << "Line table prologue:\n"
1235ffd83dbSDimitry Andric      << format("    total_length: 0x%0*" PRIx64 "\n", OffsetDumpWidth,
1245ffd83dbSDimitry Andric                TotalLength)
1255ffd83dbSDimitry Andric      << "          format: " << dwarf::FormatString(FormParams.Format) << "\n"
1260b57cec5SDimitry Andric      << format("         version: %u\n", getVersion());
1275ffd83dbSDimitry Andric   if (!versionIsSupported(getVersion()))
1285ffd83dbSDimitry Andric     return;
1290b57cec5SDimitry Andric   if (getVersion() >= 5)
1300b57cec5SDimitry Andric     OS << format("    address_size: %u\n", getAddressSize())
1310b57cec5SDimitry Andric        << format(" seg_select_size: %u\n", SegSelectorSize);
1325ffd83dbSDimitry Andric   OS << format(" prologue_length: 0x%0*" PRIx64 "\n", OffsetDumpWidth,
1335ffd83dbSDimitry Andric                PrologueLength)
1340b57cec5SDimitry Andric      << format(" min_inst_length: %u\n", MinInstLength)
1350b57cec5SDimitry Andric      << format(getVersion() >= 4 ? "max_ops_per_inst: %u\n" : "", MaxOpsPerInst)
1360b57cec5SDimitry Andric      << format(" default_is_stmt: %u\n", DefaultIsStmt)
1370b57cec5SDimitry Andric      << format("       line_base: %i\n", LineBase)
1380b57cec5SDimitry Andric      << format("      line_range: %u\n", LineRange)
1390b57cec5SDimitry Andric      << format("     opcode_base: %u\n", OpcodeBase);
1400b57cec5SDimitry Andric 
1410b57cec5SDimitry Andric   for (uint32_t I = 0; I != StandardOpcodeLengths.size(); ++I)
1425ffd83dbSDimitry Andric     OS << formatv("standard_opcode_lengths[{0}] = {1}\n",
1435ffd83dbSDimitry Andric                   static_cast<dwarf::LineNumberOps>(I + 1),
1445ffd83dbSDimitry Andric                   StandardOpcodeLengths[I]);
1450b57cec5SDimitry Andric 
1460b57cec5SDimitry Andric   if (!IncludeDirectories.empty()) {
1470b57cec5SDimitry Andric     // DWARF v5 starts directory indexes at 0.
1480b57cec5SDimitry Andric     uint32_t DirBase = getVersion() >= 5 ? 0 : 1;
1490b57cec5SDimitry Andric     for (uint32_t I = 0; I != IncludeDirectories.size(); ++I) {
1500b57cec5SDimitry Andric       OS << format("include_directories[%3u] = ", I + DirBase);
1510b57cec5SDimitry Andric       IncludeDirectories[I].dump(OS, DumpOptions);
1520b57cec5SDimitry Andric       OS << '\n';
1530b57cec5SDimitry Andric     }
1540b57cec5SDimitry Andric   }
1550b57cec5SDimitry Andric 
1560b57cec5SDimitry Andric   if (!FileNames.empty()) {
1570b57cec5SDimitry Andric     // DWARF v5 starts file indexes at 0.
1580b57cec5SDimitry Andric     uint32_t FileBase = getVersion() >= 5 ? 0 : 1;
1590b57cec5SDimitry Andric     for (uint32_t I = 0; I != FileNames.size(); ++I) {
1600b57cec5SDimitry Andric       const FileNameEntry &FileEntry = FileNames[I];
1610b57cec5SDimitry Andric       OS <<   format("file_names[%3u]:\n", I + FileBase);
1620b57cec5SDimitry Andric       OS <<          "           name: ";
1630b57cec5SDimitry Andric       FileEntry.Name.dump(OS, DumpOptions);
1640b57cec5SDimitry Andric       OS << '\n'
1650b57cec5SDimitry Andric          <<   format("      dir_index: %" PRIu64 "\n", FileEntry.DirIdx);
1660b57cec5SDimitry Andric       if (ContentTypes.HasMD5)
1670b57cec5SDimitry Andric         OS <<        "   md5_checksum: " << FileEntry.Checksum.digest() << '\n';
1680b57cec5SDimitry Andric       if (ContentTypes.HasModTime)
1690b57cec5SDimitry Andric         OS << format("       mod_time: 0x%8.8" PRIx64 "\n", FileEntry.ModTime);
1700b57cec5SDimitry Andric       if (ContentTypes.HasLength)
1710b57cec5SDimitry Andric         OS << format("         length: 0x%8.8" PRIx64 "\n", FileEntry.Length);
1720b57cec5SDimitry Andric       if (ContentTypes.HasSource) {
1735f757f3fSDimitry Andric         auto Source = FileEntry.Source.getAsCString();
1745f757f3fSDimitry Andric         if (!Source)
1755f757f3fSDimitry Andric           consumeError(Source.takeError());
1765f757f3fSDimitry Andric         else if ((*Source)[0]) {
1770b57cec5SDimitry Andric           OS << "         source: ";
1780b57cec5SDimitry Andric           FileEntry.Source.dump(OS, DumpOptions);
1790b57cec5SDimitry Andric           OS << '\n';
1800b57cec5SDimitry Andric         }
1810b57cec5SDimitry Andric       }
1820b57cec5SDimitry Andric     }
1830b57cec5SDimitry Andric   }
1845f757f3fSDimitry Andric }
1850b57cec5SDimitry Andric 
1860b57cec5SDimitry Andric // Parse v2-v4 directory and file tables.
1875ffd83dbSDimitry Andric static Error
1880b57cec5SDimitry Andric parseV2DirFileTables(const DWARFDataExtractor &DebugLineData,
1895ffd83dbSDimitry Andric                      uint64_t *OffsetPtr,
1900b57cec5SDimitry Andric                      DWARFDebugLine::ContentTypeTracker &ContentTypes,
1910b57cec5SDimitry Andric                      std::vector<DWARFFormValue> &IncludeDirectories,
1920b57cec5SDimitry Andric                      std::vector<DWARFDebugLine::FileNameEntry> &FileNames) {
1935ffd83dbSDimitry Andric   while (true) {
1945ffd83dbSDimitry Andric     Error Err = Error::success();
1955ffd83dbSDimitry Andric     StringRef S = DebugLineData.getCStrRef(OffsetPtr, &Err);
1965ffd83dbSDimitry Andric     if (Err) {
1975ffd83dbSDimitry Andric       consumeError(std::move(Err));
1985ffd83dbSDimitry Andric       return createStringError(errc::invalid_argument,
1995ffd83dbSDimitry Andric                                "include directories table was not null "
2005ffd83dbSDimitry Andric                                "terminated before the end of the prologue");
2015ffd83dbSDimitry Andric     }
2020b57cec5SDimitry Andric     if (S.empty())
2030b57cec5SDimitry Andric       break;
2040b57cec5SDimitry Andric     DWARFFormValue Dir =
2050b57cec5SDimitry Andric         DWARFFormValue::createFromPValue(dwarf::DW_FORM_string, S.data());
2060b57cec5SDimitry Andric     IncludeDirectories.push_back(Dir);
2070b57cec5SDimitry Andric   }
2080b57cec5SDimitry Andric 
2095ffd83dbSDimitry Andric   ContentTypes.HasModTime = true;
2105ffd83dbSDimitry Andric   ContentTypes.HasLength = true;
2115ffd83dbSDimitry Andric 
2125ffd83dbSDimitry Andric   while (true) {
2135ffd83dbSDimitry Andric     Error Err = Error::success();
2145ffd83dbSDimitry Andric     StringRef Name = DebugLineData.getCStrRef(OffsetPtr, &Err);
2155ffd83dbSDimitry Andric     if (!Err && Name.empty())
2160b57cec5SDimitry Andric       break;
2175ffd83dbSDimitry Andric 
2180b57cec5SDimitry Andric     DWARFDebugLine::FileNameEntry FileEntry;
2190b57cec5SDimitry Andric     FileEntry.Name =
2200b57cec5SDimitry Andric         DWARFFormValue::createFromPValue(dwarf::DW_FORM_string, Name.data());
2215ffd83dbSDimitry Andric     FileEntry.DirIdx = DebugLineData.getULEB128(OffsetPtr, &Err);
2225ffd83dbSDimitry Andric     FileEntry.ModTime = DebugLineData.getULEB128(OffsetPtr, &Err);
2235ffd83dbSDimitry Andric     FileEntry.Length = DebugLineData.getULEB128(OffsetPtr, &Err);
2245ffd83dbSDimitry Andric 
2255ffd83dbSDimitry Andric     if (Err) {
2265ffd83dbSDimitry Andric       consumeError(std::move(Err));
2275ffd83dbSDimitry Andric       return createStringError(
2285ffd83dbSDimitry Andric           errc::invalid_argument,
2295ffd83dbSDimitry Andric           "file names table was not null terminated before "
2305ffd83dbSDimitry Andric           "the end of the prologue");
2315ffd83dbSDimitry Andric     }
2320b57cec5SDimitry Andric     FileNames.push_back(FileEntry);
2330b57cec5SDimitry Andric   }
2340b57cec5SDimitry Andric 
2355ffd83dbSDimitry Andric   return Error::success();
2360b57cec5SDimitry Andric }
2370b57cec5SDimitry Andric 
2380b57cec5SDimitry Andric // Parse v5 directory/file entry content descriptions.
2398bcb0991SDimitry Andric // Returns the descriptors, or an error if we did not find a path or ran off
2408bcb0991SDimitry Andric // the end of the prologue.
2418bcb0991SDimitry Andric static llvm::Expected<ContentDescriptors>
2428bcb0991SDimitry Andric parseV5EntryFormat(const DWARFDataExtractor &DebugLineData, uint64_t *OffsetPtr,
2438bcb0991SDimitry Andric                    DWARFDebugLine::ContentTypeTracker *ContentTypes) {
2445ffd83dbSDimitry Andric   Error Err = Error::success();
2450b57cec5SDimitry Andric   ContentDescriptors Descriptors;
2465ffd83dbSDimitry Andric   int FormatCount = DebugLineData.getU8(OffsetPtr, &Err);
2470b57cec5SDimitry Andric   bool HasPath = false;
2485ffd83dbSDimitry Andric   for (int I = 0; I != FormatCount && !Err; ++I) {
2490b57cec5SDimitry Andric     ContentDescriptor Descriptor;
2500b57cec5SDimitry Andric     Descriptor.Type =
2515ffd83dbSDimitry Andric         dwarf::LineNumberEntryFormat(DebugLineData.getULEB128(OffsetPtr, &Err));
2525ffd83dbSDimitry Andric     Descriptor.Form = dwarf::Form(DebugLineData.getULEB128(OffsetPtr, &Err));
2530b57cec5SDimitry Andric     if (Descriptor.Type == dwarf::DW_LNCT_path)
2540b57cec5SDimitry Andric       HasPath = true;
2550b57cec5SDimitry Andric     if (ContentTypes)
2560b57cec5SDimitry Andric       ContentTypes->trackContentType(Descriptor.Type);
2570b57cec5SDimitry Andric     Descriptors.push_back(Descriptor);
2580b57cec5SDimitry Andric   }
2598bcb0991SDimitry Andric 
2605ffd83dbSDimitry Andric   if (Err)
2615ffd83dbSDimitry Andric     return createStringError(errc::invalid_argument,
2625ffd83dbSDimitry Andric                              "failed to parse entry content descriptors: %s",
2635ffd83dbSDimitry Andric                              toString(std::move(Err)).c_str());
2645ffd83dbSDimitry Andric 
2658bcb0991SDimitry Andric   if (!HasPath)
2668bcb0991SDimitry Andric     return createStringError(errc::invalid_argument,
2678bcb0991SDimitry Andric                              "failed to parse entry content descriptions"
2688bcb0991SDimitry Andric                              " because no path was found");
2698bcb0991SDimitry Andric   return Descriptors;
2700b57cec5SDimitry Andric }
2710b57cec5SDimitry Andric 
2728bcb0991SDimitry Andric static Error
2730b57cec5SDimitry Andric parseV5DirFileTables(const DWARFDataExtractor &DebugLineData,
274480093f4SDimitry Andric                      uint64_t *OffsetPtr, const dwarf::FormParams &FormParams,
2750b57cec5SDimitry Andric                      const DWARFContext &Ctx, const DWARFUnit *U,
2760b57cec5SDimitry Andric                      DWARFDebugLine::ContentTypeTracker &ContentTypes,
2770b57cec5SDimitry Andric                      std::vector<DWARFFormValue> &IncludeDirectories,
2780b57cec5SDimitry Andric                      std::vector<DWARFDebugLine::FileNameEntry> &FileNames) {
2790b57cec5SDimitry Andric   // Get the directory entry description.
2808bcb0991SDimitry Andric   llvm::Expected<ContentDescriptors> DirDescriptors =
281480093f4SDimitry Andric       parseV5EntryFormat(DebugLineData, OffsetPtr, nullptr);
2828bcb0991SDimitry Andric   if (!DirDescriptors)
2838bcb0991SDimitry Andric     return DirDescriptors.takeError();
2840b57cec5SDimitry Andric 
2850b57cec5SDimitry Andric   // Get the directory entries, according to the format described above.
2865ffd83dbSDimitry Andric   uint64_t DirEntryCount = DebugLineData.getULEB128(OffsetPtr);
2875ffd83dbSDimitry Andric   for (uint64_t I = 0; I != DirEntryCount; ++I) {
2888bcb0991SDimitry Andric     for (auto Descriptor : *DirDescriptors) {
2890b57cec5SDimitry Andric       DWARFFormValue Value(Descriptor.Form);
2900b57cec5SDimitry Andric       switch (Descriptor.Type) {
2910b57cec5SDimitry Andric       case DW_LNCT_path:
2920b57cec5SDimitry Andric         if (!Value.extractValue(DebugLineData, OffsetPtr, FormParams, &Ctx, U))
2938bcb0991SDimitry Andric           return createStringError(errc::invalid_argument,
2948bcb0991SDimitry Andric                                    "failed to parse directory entry because "
2955ffd83dbSDimitry Andric                                    "extracting the form value failed");
2960b57cec5SDimitry Andric         IncludeDirectories.push_back(Value);
2970b57cec5SDimitry Andric         break;
2980b57cec5SDimitry Andric       default:
2990b57cec5SDimitry Andric         if (!Value.skipValue(DebugLineData, OffsetPtr, FormParams))
3008bcb0991SDimitry Andric           return createStringError(errc::invalid_argument,
3018bcb0991SDimitry Andric                                    "failed to parse directory entry because "
3025ffd83dbSDimitry Andric                                    "skipping the form value failed");
3030b57cec5SDimitry Andric       }
3040b57cec5SDimitry Andric     }
3050b57cec5SDimitry Andric   }
3060b57cec5SDimitry Andric 
3070b57cec5SDimitry Andric   // Get the file entry description.
308480093f4SDimitry Andric   llvm::Expected<ContentDescriptors> FileDescriptors =
309480093f4SDimitry Andric       parseV5EntryFormat(DebugLineData, OffsetPtr, &ContentTypes);
3108bcb0991SDimitry Andric   if (!FileDescriptors)
3118bcb0991SDimitry Andric     return FileDescriptors.takeError();
3120b57cec5SDimitry Andric 
3130b57cec5SDimitry Andric   // Get the file entries, according to the format described above.
3145ffd83dbSDimitry Andric   uint64_t FileEntryCount = DebugLineData.getULEB128(OffsetPtr);
3155ffd83dbSDimitry Andric   for (uint64_t I = 0; I != FileEntryCount; ++I) {
3160b57cec5SDimitry Andric     DWARFDebugLine::FileNameEntry FileEntry;
3178bcb0991SDimitry Andric     for (auto Descriptor : *FileDescriptors) {
3180b57cec5SDimitry Andric       DWARFFormValue Value(Descriptor.Form);
3190b57cec5SDimitry Andric       if (!Value.extractValue(DebugLineData, OffsetPtr, FormParams, &Ctx, U))
3208bcb0991SDimitry Andric         return createStringError(errc::invalid_argument,
3218bcb0991SDimitry Andric                                  "failed to parse file entry because "
3225ffd83dbSDimitry Andric                                  "extracting the form value failed");
3230b57cec5SDimitry Andric       switch (Descriptor.Type) {
3240b57cec5SDimitry Andric       case DW_LNCT_path:
3250b57cec5SDimitry Andric         FileEntry.Name = Value;
3260b57cec5SDimitry Andric         break;
3270b57cec5SDimitry Andric       case DW_LNCT_LLVM_source:
3280b57cec5SDimitry Andric         FileEntry.Source = Value;
3290b57cec5SDimitry Andric         break;
3300b57cec5SDimitry Andric       case DW_LNCT_directory_index:
331bdd1243dSDimitry Andric         FileEntry.DirIdx = *Value.getAsUnsignedConstant();
3320b57cec5SDimitry Andric         break;
3330b57cec5SDimitry Andric       case DW_LNCT_timestamp:
334bdd1243dSDimitry Andric         FileEntry.ModTime = *Value.getAsUnsignedConstant();
3350b57cec5SDimitry Andric         break;
3360b57cec5SDimitry Andric       case DW_LNCT_size:
337bdd1243dSDimitry Andric         FileEntry.Length = *Value.getAsUnsignedConstant();
3380b57cec5SDimitry Andric         break;
3390b57cec5SDimitry Andric       case DW_LNCT_MD5:
340bdd1243dSDimitry Andric         if (!Value.getAsBlock() || Value.getAsBlock()->size() != 16)
3418bcb0991SDimitry Andric           return createStringError(
3428bcb0991SDimitry Andric               errc::invalid_argument,
3438bcb0991SDimitry Andric               "failed to parse file entry because the MD5 hash is invalid");
344bdd1243dSDimitry Andric         std::uninitialized_copy_n(Value.getAsBlock()->begin(), 16,
34581ad6265SDimitry Andric                                   FileEntry.Checksum.begin());
3460b57cec5SDimitry Andric         break;
3470b57cec5SDimitry Andric       default:
3480b57cec5SDimitry Andric         break;
3490b57cec5SDimitry Andric       }
3500b57cec5SDimitry Andric     }
3510b57cec5SDimitry Andric     FileNames.push_back(FileEntry);
3520b57cec5SDimitry Andric   }
3538bcb0991SDimitry Andric   return Error::success();
3540b57cec5SDimitry Andric }
3550b57cec5SDimitry Andric 
3565ffd83dbSDimitry Andric uint64_t DWARFDebugLine::Prologue::getLength() const {
3575ffd83dbSDimitry Andric   uint64_t Length = PrologueLength + sizeofTotalLength() +
3585ffd83dbSDimitry Andric                     sizeof(getVersion()) + sizeofPrologueLength();
3595ffd83dbSDimitry Andric   if (getVersion() >= 5)
3605ffd83dbSDimitry Andric     Length += 2; // Address + Segment selector sizes.
3615ffd83dbSDimitry Andric   return Length;
3625ffd83dbSDimitry Andric }
3635ffd83dbSDimitry Andric 
3645ffd83dbSDimitry Andric Error DWARFDebugLine::Prologue::parse(
3655ffd83dbSDimitry Andric     DWARFDataExtractor DebugLineData, uint64_t *OffsetPtr,
3665ffd83dbSDimitry Andric     function_ref<void(Error)> RecoverableErrorHandler, const DWARFContext &Ctx,
3670b57cec5SDimitry Andric     const DWARFUnit *U) {
3680b57cec5SDimitry Andric   const uint64_t PrologueOffset = *OffsetPtr;
3690b57cec5SDimitry Andric 
3700b57cec5SDimitry Andric   clear();
3715ffd83dbSDimitry Andric   DataExtractor::Cursor Cursor(*OffsetPtr);
3725ffd83dbSDimitry Andric   std::tie(TotalLength, FormParams.Format) =
3735ffd83dbSDimitry Andric       DebugLineData.getInitialLength(Cursor);
3745ffd83dbSDimitry Andric 
3755ffd83dbSDimitry Andric   DebugLineData =
3765ffd83dbSDimitry Andric       DWARFDataExtractor(DebugLineData, Cursor.tell() + TotalLength);
3775ffd83dbSDimitry Andric   FormParams.Version = DebugLineData.getU16(Cursor);
3785ffd83dbSDimitry Andric   if (Cursor && !versionIsSupported(getVersion())) {
3795ffd83dbSDimitry Andric     // Treat this error as unrecoverable - we cannot be sure what any of
3805ffd83dbSDimitry Andric     // the data represents including the length field, so cannot skip it or make
3815ffd83dbSDimitry Andric     // any reasonable assumptions.
3825ffd83dbSDimitry Andric     *OffsetPtr = Cursor.tell();
3835ffd83dbSDimitry Andric     return createStringError(
3845ffd83dbSDimitry Andric         errc::not_supported,
3850b57cec5SDimitry Andric         "parsing line table prologue at offset 0x%8.8" PRIx64
3865ffd83dbSDimitry Andric         ": unsupported version %" PRIu16,
3870b57cec5SDimitry Andric         PrologueOffset, getVersion());
3885ffd83dbSDimitry Andric   }
3890b57cec5SDimitry Andric 
3900b57cec5SDimitry Andric   if (getVersion() >= 5) {
3915ffd83dbSDimitry Andric     FormParams.AddrSize = DebugLineData.getU8(Cursor);
392*0fca6ea1SDimitry Andric     const uint8_t DataAddrSize = DebugLineData.getAddressSize();
393*0fca6ea1SDimitry Andric     const uint8_t PrologueAddrSize = getAddressSize();
394*0fca6ea1SDimitry Andric     if (Cursor) {
395*0fca6ea1SDimitry Andric       if (DataAddrSize == 0) {
396*0fca6ea1SDimitry Andric         if (PrologueAddrSize != 4 && PrologueAddrSize != 8) {
397*0fca6ea1SDimitry Andric           RecoverableErrorHandler(createStringError(
398*0fca6ea1SDimitry Andric               errc::not_supported,
399*0fca6ea1SDimitry Andric               "parsing line table prologue at offset 0x%8.8" PRIx64
400*0fca6ea1SDimitry Andric               ": invalid address size %" PRIu8,
401*0fca6ea1SDimitry Andric               PrologueOffset, PrologueAddrSize));
402*0fca6ea1SDimitry Andric         }
403*0fca6ea1SDimitry Andric       } else if (DataAddrSize != PrologueAddrSize) {
404*0fca6ea1SDimitry Andric         RecoverableErrorHandler(createStringError(
405*0fca6ea1SDimitry Andric             errc::not_supported,
406*0fca6ea1SDimitry Andric             "parsing line table prologue at offset 0x%8.8" PRIx64 ": address "
407*0fca6ea1SDimitry Andric             "size %" PRIu8 " doesn't match architecture address size %" PRIu8,
408*0fca6ea1SDimitry Andric             PrologueOffset, PrologueAddrSize, DataAddrSize));
409*0fca6ea1SDimitry Andric       }
410*0fca6ea1SDimitry Andric     }
4115ffd83dbSDimitry Andric     SegSelectorSize = DebugLineData.getU8(Cursor);
4120b57cec5SDimitry Andric   }
4130b57cec5SDimitry Andric 
4140b57cec5SDimitry Andric   PrologueLength =
4155ffd83dbSDimitry Andric       DebugLineData.getRelocatedValue(Cursor, sizeofPrologueLength());
4165ffd83dbSDimitry Andric   const uint64_t EndPrologueOffset = PrologueLength + Cursor.tell();
4175ffd83dbSDimitry Andric   DebugLineData = DWARFDataExtractor(DebugLineData, EndPrologueOffset);
4185ffd83dbSDimitry Andric   MinInstLength = DebugLineData.getU8(Cursor);
4190b57cec5SDimitry Andric   if (getVersion() >= 4)
4205ffd83dbSDimitry Andric     MaxOpsPerInst = DebugLineData.getU8(Cursor);
4215ffd83dbSDimitry Andric   DefaultIsStmt = DebugLineData.getU8(Cursor);
4225ffd83dbSDimitry Andric   LineBase = DebugLineData.getU8(Cursor);
4235ffd83dbSDimitry Andric   LineRange = DebugLineData.getU8(Cursor);
4245ffd83dbSDimitry Andric   OpcodeBase = DebugLineData.getU8(Cursor);
4250b57cec5SDimitry Andric 
4265ffd83dbSDimitry Andric   if (Cursor && OpcodeBase == 0) {
4275ffd83dbSDimitry Andric     // If the opcode base is 0, we cannot read the standard opcode lengths (of
4285ffd83dbSDimitry Andric     // which there are supposed to be one fewer than the opcode base). Assume
4295ffd83dbSDimitry Andric     // there are no standard opcodes and continue parsing.
4305ffd83dbSDimitry Andric     RecoverableErrorHandler(createStringError(
4315ffd83dbSDimitry Andric         errc::invalid_argument,
4325ffd83dbSDimitry Andric         "parsing line table prologue at offset 0x%8.8" PRIx64
4335ffd83dbSDimitry Andric         " found opcode base of 0. Assuming no standard opcodes",
4345ffd83dbSDimitry Andric         PrologueOffset));
4355ffd83dbSDimitry Andric   } else if (Cursor) {
4360b57cec5SDimitry Andric     StandardOpcodeLengths.reserve(OpcodeBase - 1);
4370b57cec5SDimitry Andric     for (uint32_t I = 1; I < OpcodeBase; ++I) {
4385ffd83dbSDimitry Andric       uint8_t OpLen = DebugLineData.getU8(Cursor);
4390b57cec5SDimitry Andric       StandardOpcodeLengths.push_back(OpLen);
4400b57cec5SDimitry Andric     }
4415ffd83dbSDimitry Andric   }
4420b57cec5SDimitry Andric 
4435ffd83dbSDimitry Andric   *OffsetPtr = Cursor.tell();
4445ffd83dbSDimitry Andric   // A corrupt file name or directory table does not prevent interpretation of
4455ffd83dbSDimitry Andric   // the main line program, so check the cursor state now so that its errors can
4465ffd83dbSDimitry Andric   // be handled separately.
4475ffd83dbSDimitry Andric   if (!Cursor)
4485ffd83dbSDimitry Andric     return createStringError(
4495ffd83dbSDimitry Andric         errc::invalid_argument,
4505ffd83dbSDimitry Andric         "parsing line table prologue at offset 0x%8.8" PRIx64 ": %s",
4515ffd83dbSDimitry Andric         PrologueOffset, toString(Cursor.takeError()).c_str());
4525ffd83dbSDimitry Andric 
4535ffd83dbSDimitry Andric   Error E =
4545ffd83dbSDimitry Andric       getVersion() >= 5
4555ffd83dbSDimitry Andric           ? parseV5DirFileTables(DebugLineData, OffsetPtr, FormParams, Ctx, U,
4565ffd83dbSDimitry Andric                                  ContentTypes, IncludeDirectories, FileNames)
4575ffd83dbSDimitry Andric           : parseV2DirFileTables(DebugLineData, OffsetPtr, ContentTypes,
4585ffd83dbSDimitry Andric                                  IncludeDirectories, FileNames);
4595ffd83dbSDimitry Andric   if (E) {
4605ffd83dbSDimitry Andric     RecoverableErrorHandler(joinErrors(
4618bcb0991SDimitry Andric         createStringError(
4628bcb0991SDimitry Andric             errc::invalid_argument,
4630b57cec5SDimitry Andric             "parsing line table prologue at 0x%8.8" PRIx64
4640b57cec5SDimitry Andric             " found an invalid directory or file table description at"
4650b57cec5SDimitry Andric             " 0x%8.8" PRIx64,
4668bcb0991SDimitry Andric             PrologueOffset, *OffsetPtr),
4675ffd83dbSDimitry Andric         std::move(E)));
4685ffd83dbSDimitry Andric     return Error::success();
4690b57cec5SDimitry Andric   }
4700b57cec5SDimitry Andric 
4715ffd83dbSDimitry Andric   assert(*OffsetPtr <= EndPrologueOffset);
4725ffd83dbSDimitry Andric   if (*OffsetPtr != EndPrologueOffset) {
4735ffd83dbSDimitry Andric     RecoverableErrorHandler(createStringError(
4745ffd83dbSDimitry Andric         errc::invalid_argument,
4755ffd83dbSDimitry Andric         "unknown data in line table prologue at offset 0x%8.8" PRIx64
4765ffd83dbSDimitry Andric         ": parsing ended (at offset 0x%8.8" PRIx64
4775ffd83dbSDimitry Andric         ") before reaching the prologue end at offset 0x%8.8" PRIx64,
4785ffd83dbSDimitry Andric         PrologueOffset, *OffsetPtr, EndPrologueOffset));
4795ffd83dbSDimitry Andric   }
4800b57cec5SDimitry Andric   return Error::success();
4810b57cec5SDimitry Andric }
4820b57cec5SDimitry Andric 
4830b57cec5SDimitry Andric DWARFDebugLine::Row::Row(bool DefaultIsStmt) { reset(DefaultIsStmt); }
4840b57cec5SDimitry Andric 
4850b57cec5SDimitry Andric void DWARFDebugLine::Row::postAppend() {
4860b57cec5SDimitry Andric   Discriminator = 0;
4870b57cec5SDimitry Andric   BasicBlock = false;
4880b57cec5SDimitry Andric   PrologueEnd = false;
4890b57cec5SDimitry Andric   EpilogueBegin = false;
4900b57cec5SDimitry Andric }
4910b57cec5SDimitry Andric 
4920b57cec5SDimitry Andric void DWARFDebugLine::Row::reset(bool DefaultIsStmt) {
4930b57cec5SDimitry Andric   Address.Address = 0;
4940b57cec5SDimitry Andric   Address.SectionIndex = object::SectionedAddress::UndefSection;
4950b57cec5SDimitry Andric   Line = 1;
4960b57cec5SDimitry Andric   Column = 0;
4970b57cec5SDimitry Andric   File = 1;
4980b57cec5SDimitry Andric   Isa = 0;
4990b57cec5SDimitry Andric   Discriminator = 0;
5000b57cec5SDimitry Andric   IsStmt = DefaultIsStmt;
50106c3fb27SDimitry Andric   OpIndex = 0;
5020b57cec5SDimitry Andric   BasicBlock = false;
5030b57cec5SDimitry Andric   EndSequence = false;
5040b57cec5SDimitry Andric   PrologueEnd = false;
5050b57cec5SDimitry Andric   EpilogueBegin = false;
5060b57cec5SDimitry Andric }
5070b57cec5SDimitry Andric 
5085ffd83dbSDimitry Andric void DWARFDebugLine::Row::dumpTableHeader(raw_ostream &OS, unsigned Indent) {
5095ffd83dbSDimitry Andric   OS.indent(Indent)
51006c3fb27SDimitry Andric       << "Address            Line   Column File   ISA Discriminator OpIndex "
51106c3fb27SDimitry Andric          "Flags\n";
5125ffd83dbSDimitry Andric   OS.indent(Indent)
51306c3fb27SDimitry Andric       << "------------------ ------ ------ ------ --- ------------- ------- "
5140b57cec5SDimitry Andric          "-------------\n";
5150b57cec5SDimitry Andric }
5160b57cec5SDimitry Andric 
5170b57cec5SDimitry Andric void DWARFDebugLine::Row::dump(raw_ostream &OS) const {
5180b57cec5SDimitry Andric   OS << format("0x%16.16" PRIx64 " %6u %6u", Address.Address, Line, Column)
51906c3fb27SDimitry Andric      << format(" %6u %3u %13u %7u ", File, Isa, Discriminator, OpIndex)
5200b57cec5SDimitry Andric      << (IsStmt ? " is_stmt" : "") << (BasicBlock ? " basic_block" : "")
5210b57cec5SDimitry Andric      << (PrologueEnd ? " prologue_end" : "")
5220b57cec5SDimitry Andric      << (EpilogueBegin ? " epilogue_begin" : "")
5230b57cec5SDimitry Andric      << (EndSequence ? " end_sequence" : "") << '\n';
5240b57cec5SDimitry Andric }
5250b57cec5SDimitry Andric 
5260b57cec5SDimitry Andric DWARFDebugLine::Sequence::Sequence() { reset(); }
5270b57cec5SDimitry Andric 
5280b57cec5SDimitry Andric void DWARFDebugLine::Sequence::reset() {
5290b57cec5SDimitry Andric   LowPC = 0;
5300b57cec5SDimitry Andric   HighPC = 0;
5310b57cec5SDimitry Andric   SectionIndex = object::SectionedAddress::UndefSection;
5320b57cec5SDimitry Andric   FirstRowIndex = 0;
5330b57cec5SDimitry Andric   LastRowIndex = 0;
5340b57cec5SDimitry Andric   Empty = true;
5350b57cec5SDimitry Andric }
5360b57cec5SDimitry Andric 
5370b57cec5SDimitry Andric DWARFDebugLine::LineTable::LineTable() { clear(); }
5380b57cec5SDimitry Andric 
5390b57cec5SDimitry Andric void DWARFDebugLine::LineTable::dump(raw_ostream &OS,
5400b57cec5SDimitry Andric                                      DIDumpOptions DumpOptions) const {
5410b57cec5SDimitry Andric   Prologue.dump(OS, DumpOptions);
5420b57cec5SDimitry Andric 
5430b57cec5SDimitry Andric   if (!Rows.empty()) {
544480093f4SDimitry Andric     OS << '\n';
5455ffd83dbSDimitry Andric     Row::dumpTableHeader(OS, 0);
5460b57cec5SDimitry Andric     for (const Row &R : Rows) {
5470b57cec5SDimitry Andric       R.dump(OS);
5480b57cec5SDimitry Andric     }
5490b57cec5SDimitry Andric   }
550480093f4SDimitry Andric 
551480093f4SDimitry Andric   // Terminate the table with a final blank line to clearly delineate it from
552480093f4SDimitry Andric   // later dumps.
553480093f4SDimitry Andric   OS << '\n';
5540b57cec5SDimitry Andric }
5550b57cec5SDimitry Andric 
5560b57cec5SDimitry Andric void DWARFDebugLine::LineTable::clear() {
5570b57cec5SDimitry Andric   Prologue.clear();
5580b57cec5SDimitry Andric   Rows.clear();
5590b57cec5SDimitry Andric   Sequences.clear();
5600b57cec5SDimitry Andric }
5610b57cec5SDimitry Andric 
5625ffd83dbSDimitry Andric DWARFDebugLine::ParsingState::ParsingState(
5635ffd83dbSDimitry Andric     struct LineTable *LT, uint64_t TableOffset,
5645ffd83dbSDimitry Andric     function_ref<void(Error)> ErrorHandler)
5655ffd83dbSDimitry Andric     : LineTable(LT), LineTableOffset(TableOffset), ErrorHandler(ErrorHandler) {
5660b57cec5SDimitry Andric   resetRowAndSequence();
5670b57cec5SDimitry Andric }
5680b57cec5SDimitry Andric 
5690b57cec5SDimitry Andric void DWARFDebugLine::ParsingState::resetRowAndSequence() {
5700b57cec5SDimitry Andric   Row.reset(LineTable->Prologue.DefaultIsStmt);
5710b57cec5SDimitry Andric   Sequence.reset();
5720b57cec5SDimitry Andric }
5730b57cec5SDimitry Andric 
5740b57cec5SDimitry Andric void DWARFDebugLine::ParsingState::appendRowToMatrix() {
5750b57cec5SDimitry Andric   unsigned RowNumber = LineTable->Rows.size();
5760b57cec5SDimitry Andric   if (Sequence.Empty) {
5770b57cec5SDimitry Andric     // Record the beginning of instruction sequence.
5780b57cec5SDimitry Andric     Sequence.Empty = false;
5790b57cec5SDimitry Andric     Sequence.LowPC = Row.Address.Address;
5800b57cec5SDimitry Andric     Sequence.FirstRowIndex = RowNumber;
5810b57cec5SDimitry Andric   }
5820b57cec5SDimitry Andric   LineTable->appendRow(Row);
5830b57cec5SDimitry Andric   if (Row.EndSequence) {
5840b57cec5SDimitry Andric     // Record the end of instruction sequence.
5850b57cec5SDimitry Andric     Sequence.HighPC = Row.Address.Address;
5860b57cec5SDimitry Andric     Sequence.LastRowIndex = RowNumber + 1;
5870b57cec5SDimitry Andric     Sequence.SectionIndex = Row.Address.SectionIndex;
5880b57cec5SDimitry Andric     if (Sequence.isValid())
5890b57cec5SDimitry Andric       LineTable->appendSequence(Sequence);
5900b57cec5SDimitry Andric     Sequence.reset();
5910b57cec5SDimitry Andric   }
5920b57cec5SDimitry Andric   Row.postAppend();
5930b57cec5SDimitry Andric }
5940b57cec5SDimitry Andric 
5950b57cec5SDimitry Andric const DWARFDebugLine::LineTable *
5968bcb0991SDimitry Andric DWARFDebugLine::getLineTable(uint64_t Offset) const {
5970b57cec5SDimitry Andric   LineTableConstIter Pos = LineTableMap.find(Offset);
5980b57cec5SDimitry Andric   if (Pos != LineTableMap.end())
5990b57cec5SDimitry Andric     return &Pos->second;
6000b57cec5SDimitry Andric   return nullptr;
6010b57cec5SDimitry Andric }
6020b57cec5SDimitry Andric 
6030b57cec5SDimitry Andric Expected<const DWARFDebugLine::LineTable *> DWARFDebugLine::getOrParseLineTable(
6048bcb0991SDimitry Andric     DWARFDataExtractor &DebugLineData, uint64_t Offset, const DWARFContext &Ctx,
6055ffd83dbSDimitry Andric     const DWARFUnit *U, function_ref<void(Error)> RecoverableErrorHandler) {
6060b57cec5SDimitry Andric   if (!DebugLineData.isValidOffset(Offset))
6078bcb0991SDimitry Andric     return createStringError(errc::invalid_argument, "offset 0x%8.8" PRIx64
6080b57cec5SDimitry Andric                        " is not a valid debug line section offset",
6090b57cec5SDimitry Andric                        Offset);
6100b57cec5SDimitry Andric 
6110b57cec5SDimitry Andric   std::pair<LineTableIter, bool> Pos =
6120b57cec5SDimitry Andric       LineTableMap.insert(LineTableMapTy::value_type(Offset, LineTable()));
6130b57cec5SDimitry Andric   LineTable *LT = &Pos.first->second;
6140b57cec5SDimitry Andric   if (Pos.second) {
6150b57cec5SDimitry Andric     if (Error Err =
6165ffd83dbSDimitry Andric             LT->parse(DebugLineData, &Offset, Ctx, U, RecoverableErrorHandler))
6170b57cec5SDimitry Andric       return std::move(Err);
6180b57cec5SDimitry Andric     return LT;
6190b57cec5SDimitry Andric   }
6200b57cec5SDimitry Andric   return LT;
6210b57cec5SDimitry Andric }
6220b57cec5SDimitry Andric 
62381ad6265SDimitry Andric void DWARFDebugLine::clearLineTable(uint64_t Offset) {
62481ad6265SDimitry Andric   LineTableMap.erase(Offset);
62581ad6265SDimitry Andric }
62681ad6265SDimitry Andric 
6275ffd83dbSDimitry Andric static StringRef getOpcodeName(uint8_t Opcode, uint8_t OpcodeBase) {
6285ffd83dbSDimitry Andric   assert(Opcode != 0);
6295ffd83dbSDimitry Andric   if (Opcode < OpcodeBase)
6305ffd83dbSDimitry Andric     return LNStandardString(Opcode);
6315ffd83dbSDimitry Andric   return "special";
6320b57cec5SDimitry Andric }
6330b57cec5SDimitry Andric 
63406c3fb27SDimitry Andric DWARFDebugLine::ParsingState::AddrOpIndexDelta
63506c3fb27SDimitry Andric DWARFDebugLine::ParsingState::advanceAddrOpIndex(uint64_t OperationAdvance,
6365ffd83dbSDimitry Andric                                                  uint8_t Opcode,
6375ffd83dbSDimitry Andric                                                  uint64_t OpcodeOffset) {
6385ffd83dbSDimitry Andric   StringRef OpcodeName = getOpcodeName(Opcode, LineTable->Prologue.OpcodeBase);
6395ffd83dbSDimitry Andric   // For versions less than 4, the MaxOpsPerInst member is set to 0, as the
6405ffd83dbSDimitry Andric   // maximum_operations_per_instruction field wasn't introduced until DWARFv4.
6415ffd83dbSDimitry Andric   // Don't warn about bad values in this situation.
6425ffd83dbSDimitry Andric   if (ReportAdvanceAddrProblem && LineTable->Prologue.getVersion() >= 4 &&
64306c3fb27SDimitry Andric       LineTable->Prologue.MaxOpsPerInst == 0)
64406c3fb27SDimitry Andric     ErrorHandler(createStringError(
64506c3fb27SDimitry Andric         errc::invalid_argument,
64606c3fb27SDimitry Andric         "line table program at offset 0x%8.8" PRIx64
64706c3fb27SDimitry Andric         " contains a %s opcode at offset 0x%8.8" PRIx64
64806c3fb27SDimitry Andric         ", but the prologue maximum_operations_per_instruction value is 0"
64906c3fb27SDimitry Andric         ", which is invalid. Assuming a value of 1 instead",
65006c3fb27SDimitry Andric         LineTableOffset, OpcodeName.data(), OpcodeOffset));
65106c3fb27SDimitry Andric   // Although we are able to correctly parse line number programs with
65206c3fb27SDimitry Andric   // MaxOpsPerInst > 1, the rest of DWARFDebugLine and its
65306c3fb27SDimitry Andric   // users have not been updated to handle line information for all operations
65406c3fb27SDimitry Andric   // in a multi-operation instruction, so warn about potentially incorrect
65506c3fb27SDimitry Andric   // results.
65606c3fb27SDimitry Andric   if (ReportAdvanceAddrProblem && LineTable->Prologue.MaxOpsPerInst > 1)
6575ffd83dbSDimitry Andric     ErrorHandler(createStringError(
6585ffd83dbSDimitry Andric         errc::not_supported,
6595ffd83dbSDimitry Andric         "line table program at offset 0x%8.8" PRIx64
6605ffd83dbSDimitry Andric         " contains a %s opcode at offset 0x%8.8" PRIx64
6615ffd83dbSDimitry Andric         ", but the prologue maximum_operations_per_instruction value is %" PRId8
66206c3fb27SDimitry Andric         ", which is experimentally supported, so line number information "
66306c3fb27SDimitry Andric         "may be incorrect",
6645ffd83dbSDimitry Andric         LineTableOffset, OpcodeName.data(), OpcodeOffset,
6655ffd83dbSDimitry Andric         LineTable->Prologue.MaxOpsPerInst));
6665ffd83dbSDimitry Andric   if (ReportAdvanceAddrProblem && LineTable->Prologue.MinInstLength == 0)
6675ffd83dbSDimitry Andric     ErrorHandler(
668480093f4SDimitry Andric         createStringError(errc::invalid_argument,
6695ffd83dbSDimitry Andric                           "line table program at offset 0x%8.8" PRIx64
6705ffd83dbSDimitry Andric                           " contains a %s opcode at offset 0x%8.8" PRIx64
6715ffd83dbSDimitry Andric                           ", but the prologue minimum_instruction_length value "
6725ffd83dbSDimitry Andric                           "is 0, which prevents any address advancing",
6735ffd83dbSDimitry Andric                           LineTableOffset, OpcodeName.data(), OpcodeOffset));
6745ffd83dbSDimitry Andric   ReportAdvanceAddrProblem = false;
67506c3fb27SDimitry Andric 
67606c3fb27SDimitry Andric   // Advances the address and op_index according to DWARFv5, section 6.2.5.1:
67706c3fb27SDimitry Andric   //
67806c3fb27SDimitry Andric   // new address = address +
67906c3fb27SDimitry Andric   //   minimum_instruction_length *
68006c3fb27SDimitry Andric   //   ((op_index + operation advance) / maximum_operations_per_instruction)
68106c3fb27SDimitry Andric   //
68206c3fb27SDimitry Andric   // new op_index =
68306c3fb27SDimitry Andric   //   (op_index + operation advance) % maximum_operations_per_instruction
68406c3fb27SDimitry Andric 
68506c3fb27SDimitry Andric   // For versions less than 4, the MaxOpsPerInst member is set to 0, as the
68606c3fb27SDimitry Andric   // maximum_operations_per_instruction field wasn't introduced until DWARFv4.
68706c3fb27SDimitry Andric   uint8_t MaxOpsPerInst =
68806c3fb27SDimitry Andric       std::max(LineTable->Prologue.MaxOpsPerInst, uint8_t{1});
68906c3fb27SDimitry Andric 
69006c3fb27SDimitry Andric   uint64_t AddrOffset = ((Row.OpIndex + OperationAdvance) / MaxOpsPerInst) *
69106c3fb27SDimitry Andric                         LineTable->Prologue.MinInstLength;
6925ffd83dbSDimitry Andric   Row.Address.Address += AddrOffset;
69306c3fb27SDimitry Andric 
69406c3fb27SDimitry Andric   uint8_t PrevOpIndex = Row.OpIndex;
69506c3fb27SDimitry Andric   Row.OpIndex = (Row.OpIndex + OperationAdvance) % MaxOpsPerInst;
69606c3fb27SDimitry Andric   int16_t OpIndexDelta = static_cast<int16_t>(Row.OpIndex) - PrevOpIndex;
69706c3fb27SDimitry Andric 
69806c3fb27SDimitry Andric   return {AddrOffset, OpIndexDelta};
699480093f4SDimitry Andric }
700480093f4SDimitry Andric 
70106c3fb27SDimitry Andric DWARFDebugLine::ParsingState::OpcodeAdvanceResults
70206c3fb27SDimitry Andric DWARFDebugLine::ParsingState::advanceForOpcode(uint8_t Opcode,
7035ffd83dbSDimitry Andric                                                uint64_t OpcodeOffset) {
7045ffd83dbSDimitry Andric   assert(Opcode == DW_LNS_const_add_pc ||
7055ffd83dbSDimitry Andric          Opcode >= LineTable->Prologue.OpcodeBase);
7065ffd83dbSDimitry Andric   if (ReportBadLineRange && LineTable->Prologue.LineRange == 0) {
7075ffd83dbSDimitry Andric     StringRef OpcodeName =
7085ffd83dbSDimitry Andric         getOpcodeName(Opcode, LineTable->Prologue.OpcodeBase);
7095ffd83dbSDimitry Andric     ErrorHandler(
7105ffd83dbSDimitry Andric         createStringError(errc::not_supported,
7115ffd83dbSDimitry Andric                           "line table program at offset 0x%8.8" PRIx64
7125ffd83dbSDimitry Andric                           " contains a %s opcode at offset 0x%8.8" PRIx64
7135ffd83dbSDimitry Andric                           ", but the prologue line_range value is 0. The "
7145ffd83dbSDimitry Andric                           "address and line will not be adjusted",
7155ffd83dbSDimitry Andric                           LineTableOffset, OpcodeName.data(), OpcodeOffset));
7165ffd83dbSDimitry Andric     ReportBadLineRange = false;
7170b57cec5SDimitry Andric   }
7180b57cec5SDimitry Andric 
7195ffd83dbSDimitry Andric   uint8_t OpcodeValue = Opcode;
7205ffd83dbSDimitry Andric   if (Opcode == DW_LNS_const_add_pc)
7215ffd83dbSDimitry Andric     OpcodeValue = 255;
7225ffd83dbSDimitry Andric   uint8_t AdjustedOpcode = OpcodeValue - LineTable->Prologue.OpcodeBase;
7235ffd83dbSDimitry Andric   uint64_t OperationAdvance =
7245ffd83dbSDimitry Andric       LineTable->Prologue.LineRange != 0
7255ffd83dbSDimitry Andric           ? AdjustedOpcode / LineTable->Prologue.LineRange
7265ffd83dbSDimitry Andric           : 0;
72706c3fb27SDimitry Andric   AddrOpIndexDelta Advance =
72806c3fb27SDimitry Andric       advanceAddrOpIndex(OperationAdvance, Opcode, OpcodeOffset);
72906c3fb27SDimitry Andric   return {Advance.AddrOffset, Advance.OpIndexDelta, AdjustedOpcode};
7300b57cec5SDimitry Andric }
7310b57cec5SDimitry Andric 
73206c3fb27SDimitry Andric DWARFDebugLine::ParsingState::SpecialOpcodeDelta
7335ffd83dbSDimitry Andric DWARFDebugLine::ParsingState::handleSpecialOpcode(uint8_t Opcode,
7345ffd83dbSDimitry Andric                                                   uint64_t OpcodeOffset) {
7350b57cec5SDimitry Andric   // A special opcode value is chosen based on the amount that needs
7360b57cec5SDimitry Andric   // to be added to the line and address registers. The maximum line
7370b57cec5SDimitry Andric   // increment for a special opcode is the value of the line_base
7380b57cec5SDimitry Andric   // field in the header, plus the value of the line_range field,
7390b57cec5SDimitry Andric   // minus 1 (line base + line range - 1). If the desired line
7400b57cec5SDimitry Andric   // increment is greater than the maximum line increment, a standard
7410b57cec5SDimitry Andric   // opcode must be used instead of a special opcode. The "address
7420b57cec5SDimitry Andric   // advance" is calculated by dividing the desired address increment
7430b57cec5SDimitry Andric   // by the minimum_instruction_length field from the header. The
7440b57cec5SDimitry Andric   // special opcode is then calculated using the following formula:
7450b57cec5SDimitry Andric   //
7460b57cec5SDimitry Andric   //  opcode = (desired line increment - line_base) +
7470b57cec5SDimitry Andric   //           (line_range * address advance) + opcode_base
7480b57cec5SDimitry Andric   //
7490b57cec5SDimitry Andric   // If the resulting opcode is greater than 255, a standard opcode
7500b57cec5SDimitry Andric   // must be used instead.
7510b57cec5SDimitry Andric   //
7520b57cec5SDimitry Andric   // To decode a special opcode, subtract the opcode_base from the
7530b57cec5SDimitry Andric   // opcode itself to give the adjusted opcode. The amount to
7540b57cec5SDimitry Andric   // increment the address register is the result of the adjusted
7550b57cec5SDimitry Andric   // opcode divided by the line_range multiplied by the
7560b57cec5SDimitry Andric   // minimum_instruction_length field from the header. That is:
7570b57cec5SDimitry Andric   //
7580b57cec5SDimitry Andric   //  address increment = (adjusted opcode / line_range) *
7590b57cec5SDimitry Andric   //                      minimum_instruction_length
7600b57cec5SDimitry Andric   //
7610b57cec5SDimitry Andric   // The amount to increment the line register is the line_base plus
7620b57cec5SDimitry Andric   // the result of the adjusted opcode modulo the line_range. That is:
7630b57cec5SDimitry Andric   //
7640b57cec5SDimitry Andric   // line increment = line_base + (adjusted opcode % line_range)
7650b57cec5SDimitry Andric 
76606c3fb27SDimitry Andric   DWARFDebugLine::ParsingState::OpcodeAdvanceResults AddrAdvanceResult =
76706c3fb27SDimitry Andric       advanceForOpcode(Opcode, OpcodeOffset);
7685ffd83dbSDimitry Andric   int32_t LineOffset = 0;
7695ffd83dbSDimitry Andric   if (LineTable->Prologue.LineRange != 0)
7705ffd83dbSDimitry Andric     LineOffset =
7715ffd83dbSDimitry Andric         LineTable->Prologue.LineBase +
7725ffd83dbSDimitry Andric         (AddrAdvanceResult.AdjustedOpcode % LineTable->Prologue.LineRange);
7735ffd83dbSDimitry Andric   Row.Line += LineOffset;
77406c3fb27SDimitry Andric   return {AddrAdvanceResult.AddrDelta, LineOffset,
77506c3fb27SDimitry Andric           AddrAdvanceResult.OpIndexDelta};
7765ffd83dbSDimitry Andric }
7775ffd83dbSDimitry Andric 
7785ffd83dbSDimitry Andric /// Parse a ULEB128 using the specified \p Cursor. \returns the parsed value on
779bdd1243dSDimitry Andric /// success, or std::nullopt if \p Cursor is in a failing state.
7805ffd83dbSDimitry Andric template <typename T>
781bdd1243dSDimitry Andric static std::optional<T> parseULEB128(DWARFDataExtractor &Data,
7825ffd83dbSDimitry Andric                                      DataExtractor::Cursor &Cursor) {
7835ffd83dbSDimitry Andric   T Value = Data.getULEB128(Cursor);
7845ffd83dbSDimitry Andric   if (Cursor)
7855ffd83dbSDimitry Andric     return Value;
786bdd1243dSDimitry Andric   return std::nullopt;
7875ffd83dbSDimitry Andric }
7885ffd83dbSDimitry Andric 
7895ffd83dbSDimitry Andric Error DWARFDebugLine::LineTable::parse(
7905ffd83dbSDimitry Andric     DWARFDataExtractor &DebugLineData, uint64_t *OffsetPtr,
7915ffd83dbSDimitry Andric     const DWARFContext &Ctx, const DWARFUnit *U,
7925ffd83dbSDimitry Andric     function_ref<void(Error)> RecoverableErrorHandler, raw_ostream *OS,
7935ffd83dbSDimitry Andric     bool Verbose) {
7945ffd83dbSDimitry Andric   assert((OS || !Verbose) && "cannot have verbose output without stream");
7955ffd83dbSDimitry Andric   const uint64_t DebugLineOffset = *OffsetPtr;
7965ffd83dbSDimitry Andric 
7975ffd83dbSDimitry Andric   clear();
7985ffd83dbSDimitry Andric 
7995ffd83dbSDimitry Andric   Error PrologueErr =
8005ffd83dbSDimitry Andric       Prologue.parse(DebugLineData, OffsetPtr, RecoverableErrorHandler, Ctx, U);
8010b57cec5SDimitry Andric 
8020b57cec5SDimitry Andric   if (OS) {
8035ffd83dbSDimitry Andric     DIDumpOptions DumpOptions;
8045ffd83dbSDimitry Andric     DumpOptions.Verbose = Verbose;
8055ffd83dbSDimitry Andric     Prologue.dump(*OS, DumpOptions);
8060b57cec5SDimitry Andric   }
8070b57cec5SDimitry Andric 
8085ffd83dbSDimitry Andric   if (PrologueErr) {
8095ffd83dbSDimitry Andric     // Ensure there is a blank line after the prologue to clearly delineate it
8105ffd83dbSDimitry Andric     // from later dumps.
8110b57cec5SDimitry Andric     if (OS)
8120b57cec5SDimitry Andric       *OS << "\n";
8135ffd83dbSDimitry Andric     return PrologueErr;
8145ffd83dbSDimitry Andric   }
8155ffd83dbSDimitry Andric 
8165ffd83dbSDimitry Andric   uint64_t ProgramLength = Prologue.TotalLength + Prologue.sizeofTotalLength();
8175ffd83dbSDimitry Andric   if (!DebugLineData.isValidOffsetForDataOfSize(DebugLineOffset,
8185ffd83dbSDimitry Andric                                                 ProgramLength)) {
8195ffd83dbSDimitry Andric     assert(DebugLineData.size() > DebugLineOffset &&
8205ffd83dbSDimitry Andric            "prologue parsing should handle invalid offset");
8215ffd83dbSDimitry Andric     uint64_t BytesRemaining = DebugLineData.size() - DebugLineOffset;
8225ffd83dbSDimitry Andric     RecoverableErrorHandler(
8235ffd83dbSDimitry Andric         createStringError(errc::invalid_argument,
8245ffd83dbSDimitry Andric                           "line table program with offset 0x%8.8" PRIx64
8255ffd83dbSDimitry Andric                           " has length 0x%8.8" PRIx64 " but only 0x%8.8" PRIx64
8265ffd83dbSDimitry Andric                           " bytes are available",
8275ffd83dbSDimitry Andric                           DebugLineOffset, ProgramLength, BytesRemaining));
8285ffd83dbSDimitry Andric     // Continue by capping the length at the number of remaining bytes.
8295ffd83dbSDimitry Andric     ProgramLength = BytesRemaining;
8305ffd83dbSDimitry Andric   }
8315ffd83dbSDimitry Andric 
8325ffd83dbSDimitry Andric   // Create a DataExtractor which can only see the data up to the end of the
8335ffd83dbSDimitry Andric   // table, to prevent reading past the end.
8345ffd83dbSDimitry Andric   const uint64_t EndOffset = DebugLineOffset + ProgramLength;
8355ffd83dbSDimitry Andric   DWARFDataExtractor TableData(DebugLineData, EndOffset);
8365ffd83dbSDimitry Andric 
8375ffd83dbSDimitry Andric   // See if we should tell the data extractor the address size.
8385ffd83dbSDimitry Andric   if (TableData.getAddressSize() == 0)
8395ffd83dbSDimitry Andric     TableData.setAddressSize(Prologue.getAddressSize());
8405ffd83dbSDimitry Andric   else
8415ffd83dbSDimitry Andric     assert(Prologue.getAddressSize() == 0 ||
8425ffd83dbSDimitry Andric            Prologue.getAddressSize() == TableData.getAddressSize());
8435ffd83dbSDimitry Andric 
8445ffd83dbSDimitry Andric   ParsingState State(this, DebugLineOffset, RecoverableErrorHandler);
8455ffd83dbSDimitry Andric 
8465ffd83dbSDimitry Andric   *OffsetPtr = DebugLineOffset + Prologue.getLength();
8475ffd83dbSDimitry Andric   if (OS && *OffsetPtr < EndOffset) {
8485ffd83dbSDimitry Andric     *OS << '\n';
8495ffd83dbSDimitry Andric     Row::dumpTableHeader(*OS, /*Indent=*/Verbose ? 12 : 0);
8505ffd83dbSDimitry Andric   }
851e8d8bef9SDimitry Andric   bool TombstonedAddress = false;
852e8d8bef9SDimitry Andric   auto EmitRow = [&] {
853e8d8bef9SDimitry Andric     if (!TombstonedAddress) {
854e8d8bef9SDimitry Andric       if (Verbose) {
855e8d8bef9SDimitry Andric         *OS << "\n";
856e8d8bef9SDimitry Andric         OS->indent(12);
857e8d8bef9SDimitry Andric       }
858e8d8bef9SDimitry Andric       if (OS)
859e8d8bef9SDimitry Andric         State.Row.dump(*OS);
860e8d8bef9SDimitry Andric       State.appendRowToMatrix();
861e8d8bef9SDimitry Andric     }
862e8d8bef9SDimitry Andric   };
8635ffd83dbSDimitry Andric   while (*OffsetPtr < EndOffset) {
8645ffd83dbSDimitry Andric     DataExtractor::Cursor Cursor(*OffsetPtr);
8655ffd83dbSDimitry Andric 
8665ffd83dbSDimitry Andric     if (Verbose)
8675ffd83dbSDimitry Andric       *OS << format("0x%08.08" PRIx64 ": ", *OffsetPtr);
8685ffd83dbSDimitry Andric 
8695ffd83dbSDimitry Andric     uint64_t OpcodeOffset = *OffsetPtr;
8705ffd83dbSDimitry Andric     uint8_t Opcode = TableData.getU8(Cursor);
8715ffd83dbSDimitry Andric     size_t RowCount = Rows.size();
8725ffd83dbSDimitry Andric 
8735ffd83dbSDimitry Andric     if (Cursor && Verbose)
8745ffd83dbSDimitry Andric       *OS << format("%02.02" PRIx8 " ", Opcode);
8755ffd83dbSDimitry Andric 
8765ffd83dbSDimitry Andric     if (Opcode == 0) {
8775ffd83dbSDimitry Andric       // Extended Opcodes always start with a zero opcode followed by
8785ffd83dbSDimitry Andric       // a uleb128 length so you can skip ones you don't know about
8795ffd83dbSDimitry Andric       uint64_t Len = TableData.getULEB128(Cursor);
8805ffd83dbSDimitry Andric       uint64_t ExtOffset = Cursor.tell();
8815ffd83dbSDimitry Andric 
8825ffd83dbSDimitry Andric       // Tolerate zero-length; assume length is correct and soldier on.
8835ffd83dbSDimitry Andric       if (Len == 0) {
8845ffd83dbSDimitry Andric         if (Cursor && Verbose)
8855ffd83dbSDimitry Andric           *OS << "Badly formed extended line op (length 0)\n";
8865ffd83dbSDimitry Andric         if (!Cursor) {
8875ffd83dbSDimitry Andric           if (Verbose)
8885ffd83dbSDimitry Andric             *OS << "\n";
8895ffd83dbSDimitry Andric           RecoverableErrorHandler(Cursor.takeError());
8905ffd83dbSDimitry Andric         }
8915ffd83dbSDimitry Andric         *OffsetPtr = Cursor.tell();
8925ffd83dbSDimitry Andric         continue;
8935ffd83dbSDimitry Andric       }
8945ffd83dbSDimitry Andric 
8955ffd83dbSDimitry Andric       uint8_t SubOpcode = TableData.getU8(Cursor);
8965ffd83dbSDimitry Andric       // OperandOffset will be the same as ExtOffset, if it was not possible to
8975ffd83dbSDimitry Andric       // read the SubOpcode.
8985ffd83dbSDimitry Andric       uint64_t OperandOffset = Cursor.tell();
8995ffd83dbSDimitry Andric       if (Verbose)
9005ffd83dbSDimitry Andric         *OS << LNExtendedString(SubOpcode);
9015ffd83dbSDimitry Andric       switch (SubOpcode) {
9025ffd83dbSDimitry Andric       case DW_LNE_end_sequence:
9035ffd83dbSDimitry Andric         // Set the end_sequence register of the state machine to true and
9045ffd83dbSDimitry Andric         // append a row to the matrix using the current values of the
9055ffd83dbSDimitry Andric         // state-machine registers. Then reset the registers to the initial
9065ffd83dbSDimitry Andric         // values specified above. Every statement program sequence must end
9075ffd83dbSDimitry Andric         // with a DW_LNE_end_sequence instruction which creates a row whose
9085ffd83dbSDimitry Andric         // address is that of the byte after the last target machine instruction
9095ffd83dbSDimitry Andric         // of the sequence.
9105ffd83dbSDimitry Andric         State.Row.EndSequence = true;
9115ffd83dbSDimitry Andric         // No need to test the Cursor is valid here, since it must be to get
9125ffd83dbSDimitry Andric         // into this code path - if it were invalid, the default case would be
9135ffd83dbSDimitry Andric         // followed.
914e8d8bef9SDimitry Andric         EmitRow();
9155ffd83dbSDimitry Andric         State.resetRowAndSequence();
9165ffd83dbSDimitry Andric         break;
9175ffd83dbSDimitry Andric 
9185ffd83dbSDimitry Andric       case DW_LNE_set_address:
9195ffd83dbSDimitry Andric         // Takes a single relocatable address as an operand. The size of the
9205ffd83dbSDimitry Andric         // operand is the size appropriate to hold an address on the target
9215ffd83dbSDimitry Andric         // machine. Set the address register to the value given by the
92206c3fb27SDimitry Andric         // relocatable address and set the op_index register to 0. All of the
92306c3fb27SDimitry Andric         // other statement program opcodes that affect the address register
92406c3fb27SDimitry Andric         // add a delta to it. This instruction stores a relocatable value into
92506c3fb27SDimitry Andric         // it instead.
9265ffd83dbSDimitry Andric         //
9275ffd83dbSDimitry Andric         // Make sure the extractor knows the address size.  If not, infer it
9285ffd83dbSDimitry Andric         // from the size of the operand.
9295ffd83dbSDimitry Andric         {
9305ffd83dbSDimitry Andric           uint8_t ExtractorAddressSize = TableData.getAddressSize();
9315ffd83dbSDimitry Andric           uint64_t OpcodeAddressSize = Len - 1;
9325ffd83dbSDimitry Andric           if (ExtractorAddressSize != OpcodeAddressSize &&
9335ffd83dbSDimitry Andric               ExtractorAddressSize != 0)
9345ffd83dbSDimitry Andric             RecoverableErrorHandler(createStringError(
9355ffd83dbSDimitry Andric                 errc::invalid_argument,
9365ffd83dbSDimitry Andric                 "mismatching address size at offset 0x%8.8" PRIx64
9375ffd83dbSDimitry Andric                 " expected 0x%2.2" PRIx8 " found 0x%2.2" PRIx64,
9385ffd83dbSDimitry Andric                 ExtOffset, ExtractorAddressSize, Len - 1));
9395ffd83dbSDimitry Andric 
9405ffd83dbSDimitry Andric           // Assume that the line table is correct and temporarily override the
9415ffd83dbSDimitry Andric           // address size. If the size is unsupported, give up trying to read
9425ffd83dbSDimitry Andric           // the address and continue to the next opcode.
9435ffd83dbSDimitry Andric           if (OpcodeAddressSize != 1 && OpcodeAddressSize != 2 &&
9445ffd83dbSDimitry Andric               OpcodeAddressSize != 4 && OpcodeAddressSize != 8) {
9455ffd83dbSDimitry Andric             RecoverableErrorHandler(createStringError(
9465ffd83dbSDimitry Andric                 errc::invalid_argument,
9475ffd83dbSDimitry Andric                 "address size 0x%2.2" PRIx64
9485ffd83dbSDimitry Andric                 " of DW_LNE_set_address opcode at offset 0x%8.8" PRIx64
9495ffd83dbSDimitry Andric                 " is unsupported",
9505ffd83dbSDimitry Andric                 OpcodeAddressSize, ExtOffset));
9515ffd83dbSDimitry Andric             TableData.skip(Cursor, OpcodeAddressSize);
9525ffd83dbSDimitry Andric           } else {
9535ffd83dbSDimitry Andric             TableData.setAddressSize(OpcodeAddressSize);
9545ffd83dbSDimitry Andric             State.Row.Address.Address = TableData.getRelocatedAddress(
9555ffd83dbSDimitry Andric                 Cursor, &State.Row.Address.SectionIndex);
95606c3fb27SDimitry Andric             State.Row.OpIndex = 0;
9575ffd83dbSDimitry Andric 
958e8d8bef9SDimitry Andric             uint64_t Tombstone =
959e8d8bef9SDimitry Andric                 dwarf::computeTombstoneAddress(OpcodeAddressSize);
960e8d8bef9SDimitry Andric             TombstonedAddress = State.Row.Address.Address == Tombstone;
961e8d8bef9SDimitry Andric 
9625ffd83dbSDimitry Andric             // Restore the address size if the extractor already had it.
9635ffd83dbSDimitry Andric             if (ExtractorAddressSize != 0)
9645ffd83dbSDimitry Andric               TableData.setAddressSize(ExtractorAddressSize);
9655ffd83dbSDimitry Andric           }
9665ffd83dbSDimitry Andric 
967e8d8bef9SDimitry Andric           if (Cursor && Verbose) {
968e8d8bef9SDimitry Andric             *OS << " (";
969e8d8bef9SDimitry Andric             DWARFFormValue::dumpAddress(*OS, OpcodeAddressSize, State.Row.Address.Address);
970e8d8bef9SDimitry Andric             *OS << ')';
971e8d8bef9SDimitry Andric           }
9725ffd83dbSDimitry Andric         }
9735ffd83dbSDimitry Andric         break;
9745ffd83dbSDimitry Andric 
9755ffd83dbSDimitry Andric       case DW_LNE_define_file:
9765ffd83dbSDimitry Andric         // Takes 4 arguments. The first is a null terminated string containing
9775ffd83dbSDimitry Andric         // a source file name. The second is an unsigned LEB128 number
9785ffd83dbSDimitry Andric         // representing the directory index of the directory in which the file
9795ffd83dbSDimitry Andric         // was found. The third is an unsigned LEB128 number representing the
9805ffd83dbSDimitry Andric         // time of last modification of the file. The fourth is an unsigned
9815ffd83dbSDimitry Andric         // LEB128 number representing the length in bytes of the file. The time
9825ffd83dbSDimitry Andric         // and length fields may contain LEB128(0) if the information is not
9835ffd83dbSDimitry Andric         // available.
9845ffd83dbSDimitry Andric         //
9855ffd83dbSDimitry Andric         // The directory index represents an entry in the include_directories
9865ffd83dbSDimitry Andric         // section of the statement program prologue. The index is LEB128(0)
9875ffd83dbSDimitry Andric         // if the file was found in the current directory of the compilation,
9885ffd83dbSDimitry Andric         // LEB128(1) if it was found in the first directory in the
9895ffd83dbSDimitry Andric         // include_directories section, and so on. The directory index is
9905ffd83dbSDimitry Andric         // ignored for file names that represent full path names.
9915ffd83dbSDimitry Andric         //
9925ffd83dbSDimitry Andric         // The files are numbered, starting at 1, in the order in which they
9935ffd83dbSDimitry Andric         // appear; the names in the prologue come before names defined by
9945ffd83dbSDimitry Andric         // the DW_LNE_define_file instruction. These numbers are used in the
9955ffd83dbSDimitry Andric         // the file register of the state machine.
9965ffd83dbSDimitry Andric         {
9975ffd83dbSDimitry Andric           FileNameEntry FileEntry;
9985ffd83dbSDimitry Andric           const char *Name = TableData.getCStr(Cursor);
9995ffd83dbSDimitry Andric           FileEntry.Name =
10005ffd83dbSDimitry Andric               DWARFFormValue::createFromPValue(dwarf::DW_FORM_string, Name);
10015ffd83dbSDimitry Andric           FileEntry.DirIdx = TableData.getULEB128(Cursor);
10025ffd83dbSDimitry Andric           FileEntry.ModTime = TableData.getULEB128(Cursor);
10035ffd83dbSDimitry Andric           FileEntry.Length = TableData.getULEB128(Cursor);
10045ffd83dbSDimitry Andric           Prologue.FileNames.push_back(FileEntry);
10055ffd83dbSDimitry Andric           if (Cursor && Verbose)
10065ffd83dbSDimitry Andric             *OS << " (" << Name << ", dir=" << FileEntry.DirIdx << ", mod_time="
10075ffd83dbSDimitry Andric                 << format("(0x%16.16" PRIx64 ")", FileEntry.ModTime)
10085ffd83dbSDimitry Andric                 << ", length=" << FileEntry.Length << ")";
10095ffd83dbSDimitry Andric         }
10105ffd83dbSDimitry Andric         break;
10115ffd83dbSDimitry Andric 
10125ffd83dbSDimitry Andric       case DW_LNE_set_discriminator:
10135ffd83dbSDimitry Andric         State.Row.Discriminator = TableData.getULEB128(Cursor);
10145ffd83dbSDimitry Andric         if (Cursor && Verbose)
10155ffd83dbSDimitry Andric           *OS << " (" << State.Row.Discriminator << ")";
10165ffd83dbSDimitry Andric         break;
10175ffd83dbSDimitry Andric 
10185ffd83dbSDimitry Andric       default:
10195ffd83dbSDimitry Andric         if (Cursor && Verbose)
10205ffd83dbSDimitry Andric           *OS << format("Unrecognized extended op 0x%02.02" PRIx8, SubOpcode)
10215ffd83dbSDimitry Andric               << format(" length %" PRIx64, Len);
10225ffd83dbSDimitry Andric         // Len doesn't include the zero opcode byte or the length itself, but
10235ffd83dbSDimitry Andric         // it does include the sub_opcode, so we have to adjust for that.
10245ffd83dbSDimitry Andric         TableData.skip(Cursor, Len - 1);
10255ffd83dbSDimitry Andric         break;
10265ffd83dbSDimitry Andric       }
10275ffd83dbSDimitry Andric       // Make sure the length as recorded in the table and the standard length
10285ffd83dbSDimitry Andric       // for the opcode match. If they don't, continue from the end as claimed
10295ffd83dbSDimitry Andric       // by the table. Similarly, continue from the claimed end in the event of
10305ffd83dbSDimitry Andric       // a parsing error.
10315ffd83dbSDimitry Andric       uint64_t End = ExtOffset + Len;
10325ffd83dbSDimitry Andric       if (Cursor && Cursor.tell() != End)
10335ffd83dbSDimitry Andric         RecoverableErrorHandler(createStringError(
10345ffd83dbSDimitry Andric             errc::illegal_byte_sequence,
10355ffd83dbSDimitry Andric             "unexpected line op length at offset 0x%8.8" PRIx64
10365ffd83dbSDimitry Andric             " expected 0x%2.2" PRIx64 " found 0x%2.2" PRIx64,
10375ffd83dbSDimitry Andric             ExtOffset, Len, Cursor.tell() - ExtOffset));
10385ffd83dbSDimitry Andric       if (!Cursor && Verbose) {
10395ffd83dbSDimitry Andric         DWARFDataExtractor::Cursor ByteCursor(OperandOffset);
10405ffd83dbSDimitry Andric         uint8_t Byte = TableData.getU8(ByteCursor);
10415ffd83dbSDimitry Andric         if (ByteCursor) {
10425ffd83dbSDimitry Andric           *OS << " (<parsing error>";
10435ffd83dbSDimitry Andric           do {
10445ffd83dbSDimitry Andric             *OS << format(" %2.2" PRIx8, Byte);
10455ffd83dbSDimitry Andric             Byte = TableData.getU8(ByteCursor);
10465ffd83dbSDimitry Andric           } while (ByteCursor);
10475ffd83dbSDimitry Andric           *OS << ")";
10485ffd83dbSDimitry Andric         }
10495ffd83dbSDimitry Andric 
10505ffd83dbSDimitry Andric         // The only parse failure in this case should be if the end was reached.
10515ffd83dbSDimitry Andric         // In that case, throw away the error, as the main Cursor's error will
10525ffd83dbSDimitry Andric         // be sufficient.
10535ffd83dbSDimitry Andric         consumeError(ByteCursor.takeError());
10545ffd83dbSDimitry Andric       }
10555ffd83dbSDimitry Andric       *OffsetPtr = End;
10565ffd83dbSDimitry Andric     } else if (Opcode < Prologue.OpcodeBase) {
10575ffd83dbSDimitry Andric       if (Verbose)
10585ffd83dbSDimitry Andric         *OS << LNStandardString(Opcode);
10595ffd83dbSDimitry Andric       switch (Opcode) {
10605ffd83dbSDimitry Andric       // Standard Opcodes
10615ffd83dbSDimitry Andric       case DW_LNS_copy:
10625ffd83dbSDimitry Andric         // Takes no arguments. Append a row to the matrix using the
10635ffd83dbSDimitry Andric         // current values of the state-machine registers.
1064e8d8bef9SDimitry Andric         EmitRow();
10655ffd83dbSDimitry Andric         break;
10665ffd83dbSDimitry Andric 
10675ffd83dbSDimitry Andric       case DW_LNS_advance_pc:
106806c3fb27SDimitry Andric         // Takes a single unsigned LEB128 operand as the operation advance
106906c3fb27SDimitry Andric         // and modifies the address and op_index registers of the state machine
107006c3fb27SDimitry Andric         // according to that.
1071bdd1243dSDimitry Andric         if (std::optional<uint64_t> Operand =
10725ffd83dbSDimitry Andric                 parseULEB128<uint64_t>(TableData, Cursor)) {
107306c3fb27SDimitry Andric           ParsingState::AddrOpIndexDelta Advance =
107406c3fb27SDimitry Andric               State.advanceAddrOpIndex(*Operand, Opcode, OpcodeOffset);
10755ffd83dbSDimitry Andric           if (Verbose)
107606c3fb27SDimitry Andric             *OS << " (addr += " << Advance.AddrOffset
107706c3fb27SDimitry Andric                 << ", op-index += " << Advance.OpIndexDelta << ")";
10785ffd83dbSDimitry Andric         }
10795ffd83dbSDimitry Andric         break;
10805ffd83dbSDimitry Andric 
10815ffd83dbSDimitry Andric       case DW_LNS_advance_line:
10825ffd83dbSDimitry Andric         // Takes a single signed LEB128 operand and adds that value to
10835ffd83dbSDimitry Andric         // the line register of the state machine.
10845ffd83dbSDimitry Andric         {
10855ffd83dbSDimitry Andric           int64_t LineDelta = TableData.getSLEB128(Cursor);
10865ffd83dbSDimitry Andric           if (Cursor) {
10875ffd83dbSDimitry Andric             State.Row.Line += LineDelta;
10885ffd83dbSDimitry Andric             if (Verbose)
10895ffd83dbSDimitry Andric               *OS << " (" << State.Row.Line << ")";
10905ffd83dbSDimitry Andric           }
10915ffd83dbSDimitry Andric         }
10925ffd83dbSDimitry Andric         break;
10935ffd83dbSDimitry Andric 
10945ffd83dbSDimitry Andric       case DW_LNS_set_file:
10955ffd83dbSDimitry Andric         // Takes a single unsigned LEB128 operand and stores it in the file
10965ffd83dbSDimitry Andric         // register of the state machine.
1097bdd1243dSDimitry Andric         if (std::optional<uint16_t> File =
10985ffd83dbSDimitry Andric                 parseULEB128<uint16_t>(TableData, Cursor)) {
10995ffd83dbSDimitry Andric           State.Row.File = *File;
11005ffd83dbSDimitry Andric           if (Verbose)
11015ffd83dbSDimitry Andric             *OS << " (" << State.Row.File << ")";
11025ffd83dbSDimitry Andric         }
11035ffd83dbSDimitry Andric         break;
11045ffd83dbSDimitry Andric 
11055ffd83dbSDimitry Andric       case DW_LNS_set_column:
11065ffd83dbSDimitry Andric         // Takes a single unsigned LEB128 operand and stores it in the
11075ffd83dbSDimitry Andric         // column register of the state machine.
1108bdd1243dSDimitry Andric         if (std::optional<uint16_t> Column =
11095ffd83dbSDimitry Andric                 parseULEB128<uint16_t>(TableData, Cursor)) {
11105ffd83dbSDimitry Andric           State.Row.Column = *Column;
11115ffd83dbSDimitry Andric           if (Verbose)
11125ffd83dbSDimitry Andric             *OS << " (" << State.Row.Column << ")";
11135ffd83dbSDimitry Andric         }
11145ffd83dbSDimitry Andric         break;
11155ffd83dbSDimitry Andric 
11165ffd83dbSDimitry Andric       case DW_LNS_negate_stmt:
11175ffd83dbSDimitry Andric         // Takes no arguments. Set the is_stmt register of the state
11185ffd83dbSDimitry Andric         // machine to the logical negation of its current value.
11195ffd83dbSDimitry Andric         State.Row.IsStmt = !State.Row.IsStmt;
11205ffd83dbSDimitry Andric         break;
11215ffd83dbSDimitry Andric 
11225ffd83dbSDimitry Andric       case DW_LNS_set_basic_block:
11235ffd83dbSDimitry Andric         // Takes no arguments. Set the basic_block register of the
11245ffd83dbSDimitry Andric         // state machine to true
11255ffd83dbSDimitry Andric         State.Row.BasicBlock = true;
11265ffd83dbSDimitry Andric         break;
11275ffd83dbSDimitry Andric 
11285ffd83dbSDimitry Andric       case DW_LNS_const_add_pc:
112906c3fb27SDimitry Andric         // Takes no arguments. Advance the address and op_index registers of
113006c3fb27SDimitry Andric         // the state machine by the increments corresponding to special
11315ffd83dbSDimitry Andric         // opcode 255. The motivation for DW_LNS_const_add_pc is this:
11325ffd83dbSDimitry Andric         // when the statement program needs to advance the address by a
11335ffd83dbSDimitry Andric         // small amount, it can use a single special opcode, which occupies
11345ffd83dbSDimitry Andric         // a single byte. When it needs to advance the address by up to
11355ffd83dbSDimitry Andric         // twice the range of the last special opcode, it can use
11365ffd83dbSDimitry Andric         // DW_LNS_const_add_pc followed by a special opcode, for a total
11375ffd83dbSDimitry Andric         // of two bytes. Only if it needs to advance the address by more
11385ffd83dbSDimitry Andric         // than twice that range will it need to use both DW_LNS_advance_pc
11395ffd83dbSDimitry Andric         // and a special opcode, requiring three or more bytes.
11405ffd83dbSDimitry Andric         {
114106c3fb27SDimitry Andric           ParsingState::OpcodeAdvanceResults Advance =
114206c3fb27SDimitry Andric               State.advanceForOpcode(Opcode, OpcodeOffset);
11435ffd83dbSDimitry Andric           if (Verbose)
114406c3fb27SDimitry Andric             *OS << format(" (addr += 0x%16.16" PRIx64 ", op-index += %" PRIu8
114506c3fb27SDimitry Andric                           ")",
114606c3fb27SDimitry Andric                           Advance.AddrDelta, Advance.OpIndexDelta);
11475ffd83dbSDimitry Andric         }
11485ffd83dbSDimitry Andric         break;
11495ffd83dbSDimitry Andric 
11505ffd83dbSDimitry Andric       case DW_LNS_fixed_advance_pc:
11515ffd83dbSDimitry Andric         // Takes a single uhalf operand. Add to the address register of
115206c3fb27SDimitry Andric         // the state machine the value of the (unencoded) operand and set
115306c3fb27SDimitry Andric         // the op_index register to 0. This is the only extended opcode that
115406c3fb27SDimitry Andric         // takes an argument that is not a variable length number.
115506c3fb27SDimitry Andric         // The motivation for DW_LNS_fixed_advance_pc is this: existing
115606c3fb27SDimitry Andric         // assemblers cannot emit DW_LNS_advance_pc or special opcodes because
115706c3fb27SDimitry Andric         // they cannot encode LEB128 numbers or judge when the computation
115806c3fb27SDimitry Andric         // of a special opcode overflows and requires the use of
115906c3fb27SDimitry Andric         // DW_LNS_advance_pc. Such assemblers, however, can use
116006c3fb27SDimitry Andric         // DW_LNS_fixed_advance_pc instead, sacrificing compression.
11615ffd83dbSDimitry Andric         {
11625ffd83dbSDimitry Andric           uint16_t PCOffset =
11635ffd83dbSDimitry Andric               TableData.getRelocatedValue(Cursor, 2);
11645ffd83dbSDimitry Andric           if (Cursor) {
11655ffd83dbSDimitry Andric             State.Row.Address.Address += PCOffset;
116606c3fb27SDimitry Andric             State.Row.OpIndex = 0;
11675ffd83dbSDimitry Andric             if (Verbose)
116806c3fb27SDimitry Andric               *OS << format(" (addr += 0x%4.4" PRIx16 ", op-index = 0)",
116906c3fb27SDimitry Andric                             PCOffset);
11705ffd83dbSDimitry Andric           }
11715ffd83dbSDimitry Andric         }
11725ffd83dbSDimitry Andric         break;
11735ffd83dbSDimitry Andric 
11745ffd83dbSDimitry Andric       case DW_LNS_set_prologue_end:
11755ffd83dbSDimitry Andric         // Takes no arguments. Set the prologue_end register of the
11765ffd83dbSDimitry Andric         // state machine to true
11775ffd83dbSDimitry Andric         State.Row.PrologueEnd = true;
11785ffd83dbSDimitry Andric         break;
11795ffd83dbSDimitry Andric 
11805ffd83dbSDimitry Andric       case DW_LNS_set_epilogue_begin:
11815ffd83dbSDimitry Andric         // Takes no arguments. Set the basic_block register of the
11825ffd83dbSDimitry Andric         // state machine to true
11835ffd83dbSDimitry Andric         State.Row.EpilogueBegin = true;
11845ffd83dbSDimitry Andric         break;
11855ffd83dbSDimitry Andric 
11865ffd83dbSDimitry Andric       case DW_LNS_set_isa:
11875ffd83dbSDimitry Andric         // Takes a single unsigned LEB128 operand and stores it in the
11885ffd83dbSDimitry Andric         // ISA register of the state machine.
1189bdd1243dSDimitry Andric         if (std::optional<uint8_t> Isa =
1190bdd1243dSDimitry Andric                 parseULEB128<uint8_t>(TableData, Cursor)) {
11915ffd83dbSDimitry Andric           State.Row.Isa = *Isa;
11925ffd83dbSDimitry Andric           if (Verbose)
11935ffd83dbSDimitry Andric             *OS << " (" << (uint64_t)State.Row.Isa << ")";
11945ffd83dbSDimitry Andric         }
11955ffd83dbSDimitry Andric         break;
11965ffd83dbSDimitry Andric 
11975ffd83dbSDimitry Andric       default:
11985ffd83dbSDimitry Andric         // Handle any unknown standard opcodes here. We know the lengths
11995ffd83dbSDimitry Andric         // of such opcodes because they are specified in the prologue
12005ffd83dbSDimitry Andric         // as a multiple of LEB128 operands for each opcode.
12015ffd83dbSDimitry Andric         {
12025ffd83dbSDimitry Andric           assert(Opcode - 1U < Prologue.StandardOpcodeLengths.size());
12035ffd83dbSDimitry Andric           if (Verbose)
12045ffd83dbSDimitry Andric             *OS << "Unrecognized standard opcode";
12055ffd83dbSDimitry Andric           uint8_t OpcodeLength = Prologue.StandardOpcodeLengths[Opcode - 1];
12065ffd83dbSDimitry Andric           std::vector<uint64_t> Operands;
12075ffd83dbSDimitry Andric           for (uint8_t I = 0; I < OpcodeLength; ++I) {
1208bdd1243dSDimitry Andric             if (std::optional<uint64_t> Value =
12095ffd83dbSDimitry Andric                     parseULEB128<uint64_t>(TableData, Cursor))
12105ffd83dbSDimitry Andric               Operands.push_back(*Value);
12115ffd83dbSDimitry Andric             else
12125ffd83dbSDimitry Andric               break;
12135ffd83dbSDimitry Andric           }
12145ffd83dbSDimitry Andric           if (Verbose && !Operands.empty()) {
12155ffd83dbSDimitry Andric             *OS << " (operands: ";
12165ffd83dbSDimitry Andric             bool First = true;
12175ffd83dbSDimitry Andric             for (uint64_t Value : Operands) {
12185ffd83dbSDimitry Andric               if (!First)
12195ffd83dbSDimitry Andric                 *OS << ", ";
12205ffd83dbSDimitry Andric               First = false;
12215ffd83dbSDimitry Andric               *OS << format("0x%16.16" PRIx64, Value);
12225ffd83dbSDimitry Andric             }
12235ffd83dbSDimitry Andric             if (Verbose)
12245ffd83dbSDimitry Andric               *OS << ')';
12255ffd83dbSDimitry Andric           }
12265ffd83dbSDimitry Andric         }
12275ffd83dbSDimitry Andric         break;
12285ffd83dbSDimitry Andric       }
12295ffd83dbSDimitry Andric 
12305ffd83dbSDimitry Andric       *OffsetPtr = Cursor.tell();
12315ffd83dbSDimitry Andric     } else {
12325ffd83dbSDimitry Andric       // Special Opcodes.
123306c3fb27SDimitry Andric       ParsingState::SpecialOpcodeDelta Delta =
12345ffd83dbSDimitry Andric           State.handleSpecialOpcode(Opcode, OpcodeOffset);
12355ffd83dbSDimitry Andric 
1236e8d8bef9SDimitry Andric       if (Verbose)
123706c3fb27SDimitry Andric         *OS << "address += " << Delta.Address << ",  line += " << Delta.Line
123806c3fb27SDimitry Andric             << ",  op-index += " << Delta.OpIndex;
1239e8d8bef9SDimitry Andric       EmitRow();
12405ffd83dbSDimitry Andric       *OffsetPtr = Cursor.tell();
12415ffd83dbSDimitry Andric     }
12425ffd83dbSDimitry Andric 
12435ffd83dbSDimitry Andric     // When a row is added to the matrix, it is also dumped, which includes a
12445ffd83dbSDimitry Andric     // new line already, so don't add an extra one.
12455ffd83dbSDimitry Andric     if (Verbose && Rows.size() == RowCount)
12465ffd83dbSDimitry Andric       *OS << "\n";
12475ffd83dbSDimitry Andric 
12485ffd83dbSDimitry Andric     // Most parse failures other than when parsing extended opcodes are due to
12495ffd83dbSDimitry Andric     // failures to read ULEBs. Bail out of parsing, since we don't know where to
12505ffd83dbSDimitry Andric     // continue reading from as there is no stated length for such byte
12515ffd83dbSDimitry Andric     // sequences. Print the final trailing new line if needed before doing so.
12525ffd83dbSDimitry Andric     if (!Cursor && Opcode != 0) {
12535ffd83dbSDimitry Andric       if (Verbose)
12545ffd83dbSDimitry Andric         *OS << "\n";
12555ffd83dbSDimitry Andric       return Cursor.takeError();
12565ffd83dbSDimitry Andric     }
12575ffd83dbSDimitry Andric 
12585ffd83dbSDimitry Andric     if (!Cursor)
12595ffd83dbSDimitry Andric       RecoverableErrorHandler(Cursor.takeError());
12600b57cec5SDimitry Andric   }
12610b57cec5SDimitry Andric 
12620b57cec5SDimitry Andric   if (!State.Sequence.Empty)
12635ffd83dbSDimitry Andric     RecoverableErrorHandler(createStringError(
1264480093f4SDimitry Andric         errc::illegal_byte_sequence,
1265480093f4SDimitry Andric         "last sequence in debug line table at offset 0x%8.8" PRIx64
1266480093f4SDimitry Andric         " is not terminated",
1267480093f4SDimitry Andric         DebugLineOffset));
12680b57cec5SDimitry Andric 
12690b57cec5SDimitry Andric   // Sort all sequences so that address lookup will work faster.
12700b57cec5SDimitry Andric   if (!Sequences.empty()) {
12710b57cec5SDimitry Andric     llvm::sort(Sequences, Sequence::orderByHighPC);
12720b57cec5SDimitry Andric     // Note: actually, instruction address ranges of sequences should not
12730b57cec5SDimitry Andric     // overlap (in shared objects and executables). If they do, the address
12740b57cec5SDimitry Andric     // lookup would still work, though, but result would be ambiguous.
12750b57cec5SDimitry Andric     // We don't report warning in this case. For example,
12760b57cec5SDimitry Andric     // sometimes .so compiled from multiple object files contains a few
12770b57cec5SDimitry Andric     // rudimentary sequences for address ranges [0x0, 0xsomething).
12780b57cec5SDimitry Andric   }
12790b57cec5SDimitry Andric 
12805ffd83dbSDimitry Andric   // Terminate the table with a final blank line to clearly delineate it from
12815ffd83dbSDimitry Andric   // later dumps.
12825ffd83dbSDimitry Andric   if (OS)
12835ffd83dbSDimitry Andric     *OS << "\n";
12845ffd83dbSDimitry Andric 
12850b57cec5SDimitry Andric   return Error::success();
12860b57cec5SDimitry Andric }
12870b57cec5SDimitry Andric 
12880b57cec5SDimitry Andric uint32_t DWARFDebugLine::LineTable::findRowInSeq(
12890b57cec5SDimitry Andric     const DWARFDebugLine::Sequence &Seq,
12900b57cec5SDimitry Andric     object::SectionedAddress Address) const {
12910b57cec5SDimitry Andric   if (!Seq.containsPC(Address))
12920b57cec5SDimitry Andric     return UnknownRowIndex;
12930b57cec5SDimitry Andric   assert(Seq.SectionIndex == Address.SectionIndex);
12940b57cec5SDimitry Andric   // In some cases, e.g. first instruction in a function, the compiler generates
12950b57cec5SDimitry Andric   // two entries, both with the same address. We want the last one.
12960b57cec5SDimitry Andric   //
12970b57cec5SDimitry Andric   // In general we want a non-empty range: the last row whose address is less
12980b57cec5SDimitry Andric   // than or equal to Address. This can be computed as upper_bound - 1.
129906c3fb27SDimitry Andric   //
130006c3fb27SDimitry Andric   // TODO: This function, and its users, needs to be update to return multiple
130106c3fb27SDimitry Andric   // rows for bundles with multiple op-indexes.
13020b57cec5SDimitry Andric   DWARFDebugLine::Row Row;
13030b57cec5SDimitry Andric   Row.Address = Address;
13040b57cec5SDimitry Andric   RowIter FirstRow = Rows.begin() + Seq.FirstRowIndex;
13050b57cec5SDimitry Andric   RowIter LastRow = Rows.begin() + Seq.LastRowIndex;
13060b57cec5SDimitry Andric   assert(FirstRow->Address.Address <= Row.Address.Address &&
13070b57cec5SDimitry Andric          Row.Address.Address < LastRow[-1].Address.Address);
13080b57cec5SDimitry Andric   RowIter RowPos = std::upper_bound(FirstRow + 1, LastRow - 1, Row,
13090b57cec5SDimitry Andric                                     DWARFDebugLine::Row::orderByAddress) -
13100b57cec5SDimitry Andric                    1;
13110b57cec5SDimitry Andric   assert(Seq.SectionIndex == RowPos->Address.SectionIndex);
13120b57cec5SDimitry Andric   return RowPos - Rows.begin();
13130b57cec5SDimitry Andric }
13140b57cec5SDimitry Andric 
13150b57cec5SDimitry Andric uint32_t DWARFDebugLine::LineTable::lookupAddress(
13160b57cec5SDimitry Andric     object::SectionedAddress Address) const {
13170b57cec5SDimitry Andric 
13180b57cec5SDimitry Andric   // Search for relocatable addresses
13190b57cec5SDimitry Andric   uint32_t Result = lookupAddressImpl(Address);
13200b57cec5SDimitry Andric 
13210b57cec5SDimitry Andric   if (Result != UnknownRowIndex ||
13220b57cec5SDimitry Andric       Address.SectionIndex == object::SectionedAddress::UndefSection)
13230b57cec5SDimitry Andric     return Result;
13240b57cec5SDimitry Andric 
13250b57cec5SDimitry Andric   // Search for absolute addresses
13260b57cec5SDimitry Andric   Address.SectionIndex = object::SectionedAddress::UndefSection;
13270b57cec5SDimitry Andric   return lookupAddressImpl(Address);
13280b57cec5SDimitry Andric }
13290b57cec5SDimitry Andric 
13300b57cec5SDimitry Andric uint32_t DWARFDebugLine::LineTable::lookupAddressImpl(
13310b57cec5SDimitry Andric     object::SectionedAddress Address) const {
13320b57cec5SDimitry Andric   // First, find an instruction sequence containing the given address.
13330b57cec5SDimitry Andric   DWARFDebugLine::Sequence Sequence;
13340b57cec5SDimitry Andric   Sequence.SectionIndex = Address.SectionIndex;
13350b57cec5SDimitry Andric   Sequence.HighPC = Address.Address;
13360b57cec5SDimitry Andric   SequenceIter It = llvm::upper_bound(Sequences, Sequence,
13370b57cec5SDimitry Andric                                       DWARFDebugLine::Sequence::orderByHighPC);
13380b57cec5SDimitry Andric   if (It == Sequences.end() || It->SectionIndex != Address.SectionIndex)
13390b57cec5SDimitry Andric     return UnknownRowIndex;
13400b57cec5SDimitry Andric   return findRowInSeq(*It, Address);
13410b57cec5SDimitry Andric }
13420b57cec5SDimitry Andric 
13430b57cec5SDimitry Andric bool DWARFDebugLine::LineTable::lookupAddressRange(
13440b57cec5SDimitry Andric     object::SectionedAddress Address, uint64_t Size,
13450b57cec5SDimitry Andric     std::vector<uint32_t> &Result) const {
13460b57cec5SDimitry Andric 
13470b57cec5SDimitry Andric   // Search for relocatable addresses
13480b57cec5SDimitry Andric   if (lookupAddressRangeImpl(Address, Size, Result))
13490b57cec5SDimitry Andric     return true;
13500b57cec5SDimitry Andric 
13510b57cec5SDimitry Andric   if (Address.SectionIndex == object::SectionedAddress::UndefSection)
13520b57cec5SDimitry Andric     return false;
13530b57cec5SDimitry Andric 
13540b57cec5SDimitry Andric   // Search for absolute addresses
13550b57cec5SDimitry Andric   Address.SectionIndex = object::SectionedAddress::UndefSection;
13560b57cec5SDimitry Andric   return lookupAddressRangeImpl(Address, Size, Result);
13570b57cec5SDimitry Andric }
13580b57cec5SDimitry Andric 
13590b57cec5SDimitry Andric bool DWARFDebugLine::LineTable::lookupAddressRangeImpl(
13600b57cec5SDimitry Andric     object::SectionedAddress Address, uint64_t Size,
13610b57cec5SDimitry Andric     std::vector<uint32_t> &Result) const {
13620b57cec5SDimitry Andric   if (Sequences.empty())
13630b57cec5SDimitry Andric     return false;
13640b57cec5SDimitry Andric   uint64_t EndAddr = Address.Address + Size;
13650b57cec5SDimitry Andric   // First, find an instruction sequence containing the given address.
13660b57cec5SDimitry Andric   DWARFDebugLine::Sequence Sequence;
13670b57cec5SDimitry Andric   Sequence.SectionIndex = Address.SectionIndex;
13680b57cec5SDimitry Andric   Sequence.HighPC = Address.Address;
13690b57cec5SDimitry Andric   SequenceIter LastSeq = Sequences.end();
13700b57cec5SDimitry Andric   SequenceIter SeqPos = llvm::upper_bound(
13710b57cec5SDimitry Andric       Sequences, Sequence, DWARFDebugLine::Sequence::orderByHighPC);
13720b57cec5SDimitry Andric   if (SeqPos == LastSeq || !SeqPos->containsPC(Address))
13730b57cec5SDimitry Andric     return false;
13740b57cec5SDimitry Andric 
13750b57cec5SDimitry Andric   SequenceIter StartPos = SeqPos;
13760b57cec5SDimitry Andric 
13770b57cec5SDimitry Andric   // Add the rows from the first sequence to the vector, starting with the
13780b57cec5SDimitry Andric   // index we just calculated
13790b57cec5SDimitry Andric 
13800b57cec5SDimitry Andric   while (SeqPos != LastSeq && SeqPos->LowPC < EndAddr) {
13810b57cec5SDimitry Andric     const DWARFDebugLine::Sequence &CurSeq = *SeqPos;
13820b57cec5SDimitry Andric     // For the first sequence, we need to find which row in the sequence is the
13830b57cec5SDimitry Andric     // first in our range.
13840b57cec5SDimitry Andric     uint32_t FirstRowIndex = CurSeq.FirstRowIndex;
13850b57cec5SDimitry Andric     if (SeqPos == StartPos)
13860b57cec5SDimitry Andric       FirstRowIndex = findRowInSeq(CurSeq, Address);
13870b57cec5SDimitry Andric 
13880b57cec5SDimitry Andric     // Figure out the last row in the range.
13890b57cec5SDimitry Andric     uint32_t LastRowIndex =
13900b57cec5SDimitry Andric         findRowInSeq(CurSeq, {EndAddr - 1, Address.SectionIndex});
13910b57cec5SDimitry Andric     if (LastRowIndex == UnknownRowIndex)
13920b57cec5SDimitry Andric       LastRowIndex = CurSeq.LastRowIndex - 1;
13930b57cec5SDimitry Andric 
13940b57cec5SDimitry Andric     assert(FirstRowIndex != UnknownRowIndex);
13950b57cec5SDimitry Andric     assert(LastRowIndex != UnknownRowIndex);
13960b57cec5SDimitry Andric 
13970b57cec5SDimitry Andric     for (uint32_t I = FirstRowIndex; I <= LastRowIndex; ++I) {
13980b57cec5SDimitry Andric       Result.push_back(I);
13990b57cec5SDimitry Andric     }
14000b57cec5SDimitry Andric 
14010b57cec5SDimitry Andric     ++SeqPos;
14020b57cec5SDimitry Andric   }
14030b57cec5SDimitry Andric 
14040b57cec5SDimitry Andric   return true;
14050b57cec5SDimitry Andric }
14060b57cec5SDimitry Andric 
1407bdd1243dSDimitry Andric std::optional<StringRef>
1408bdd1243dSDimitry Andric DWARFDebugLine::LineTable::getSourceByIndex(uint64_t FileIndex,
14090b57cec5SDimitry Andric                                             FileLineInfoKind Kind) const {
14100b57cec5SDimitry Andric   if (Kind == FileLineInfoKind::None || !Prologue.hasFileAtIndex(FileIndex))
1411bdd1243dSDimitry Andric     return std::nullopt;
14120b57cec5SDimitry Andric   const FileNameEntry &Entry = Prologue.getFileNameEntry(FileIndex);
14130eae32dcSDimitry Andric   if (auto E = dwarf::toString(Entry.Source))
14140eae32dcSDimitry Andric     return StringRef(*E);
1415bdd1243dSDimitry Andric   return std::nullopt;
14160b57cec5SDimitry Andric }
14170b57cec5SDimitry Andric 
14180b57cec5SDimitry Andric static bool isPathAbsoluteOnWindowsOrPosix(const Twine &Path) {
14190b57cec5SDimitry Andric   // Debug info can contain paths from any OS, not necessarily
14200b57cec5SDimitry Andric   // an OS we're currently running on. Moreover different compilation units can
14210b57cec5SDimitry Andric   // be compiled on different operating systems and linked together later.
14220b57cec5SDimitry Andric   return sys::path::is_absolute(Path, sys::path::Style::posix) ||
14230b57cec5SDimitry Andric          sys::path::is_absolute(Path, sys::path::Style::windows);
14240b57cec5SDimitry Andric }
14250b57cec5SDimitry Andric 
14268bcb0991SDimitry Andric bool DWARFDebugLine::Prologue::getFileNameByIndex(
14278bcb0991SDimitry Andric     uint64_t FileIndex, StringRef CompDir, FileLineInfoKind Kind,
14288bcb0991SDimitry Andric     std::string &Result, sys::path::Style Style) const {
14290b57cec5SDimitry Andric   if (Kind == FileLineInfoKind::None || !hasFileAtIndex(FileIndex))
14300b57cec5SDimitry Andric     return false;
14310b57cec5SDimitry Andric   const FileNameEntry &Entry = getFileNameEntry(FileIndex);
14320eae32dcSDimitry Andric   auto E = dwarf::toString(Entry.Name);
14330eae32dcSDimitry Andric   if (!E)
1434480093f4SDimitry Andric     return false;
14350eae32dcSDimitry Andric   StringRef FileName = *E;
14365ffd83dbSDimitry Andric   if (Kind == FileLineInfoKind::RawValue ||
14370b57cec5SDimitry Andric       isPathAbsoluteOnWindowsOrPosix(FileName)) {
14385ffd83dbSDimitry Andric     Result = std::string(FileName);
14395ffd83dbSDimitry Andric     return true;
14405ffd83dbSDimitry Andric   }
14415ffd83dbSDimitry Andric   if (Kind == FileLineInfoKind::BaseNameOnly) {
14425ffd83dbSDimitry Andric     Result = std::string(llvm::sys::path::filename(FileName));
14430b57cec5SDimitry Andric     return true;
14440b57cec5SDimitry Andric   }
14450b57cec5SDimitry Andric 
14460b57cec5SDimitry Andric   SmallString<16> FilePath;
14470b57cec5SDimitry Andric   StringRef IncludeDir;
14480b57cec5SDimitry Andric   // Be defensive about the contents of Entry.
14490b57cec5SDimitry Andric   if (getVersion() >= 5) {
14505ffd83dbSDimitry Andric     // DirIdx 0 is the compilation directory, so don't include it for
14515ffd83dbSDimitry Andric     // relative names.
14525ffd83dbSDimitry Andric     if ((Entry.DirIdx != 0 || Kind != FileLineInfoKind::RelativeFilePath) &&
14535ffd83dbSDimitry Andric         Entry.DirIdx < IncludeDirectories.size())
14540eae32dcSDimitry Andric       IncludeDir = dwarf::toStringRef(IncludeDirectories[Entry.DirIdx]);
14550b57cec5SDimitry Andric   } else {
14560b57cec5SDimitry Andric     if (0 < Entry.DirIdx && Entry.DirIdx <= IncludeDirectories.size())
14570eae32dcSDimitry Andric       IncludeDir = dwarf::toStringRef(IncludeDirectories[Entry.DirIdx - 1]);
14580b57cec5SDimitry Andric   }
14590b57cec5SDimitry Andric 
1460a4a491e2SDimitry Andric   // For absolute paths only, include the compilation directory of compile unit,
1461a4a491e2SDimitry Andric   // unless v5 DirIdx == 0 (IncludeDir indicates the compilation directory). We
1462a4a491e2SDimitry Andric   // know that FileName is not absolute, the only way to have an absolute path
1463a4a491e2SDimitry Andric   // at this point would be if IncludeDir is absolute.
1464a4a491e2SDimitry Andric   if (Kind == FileLineInfoKind::AbsoluteFilePath &&
1465a4a491e2SDimitry Andric       (getVersion() < 5 || Entry.DirIdx != 0) && !CompDir.empty() &&
14665ffd83dbSDimitry Andric       !isPathAbsoluteOnWindowsOrPosix(IncludeDir))
14675ffd83dbSDimitry Andric     sys::path::append(FilePath, Style, CompDir);
14685ffd83dbSDimitry Andric 
14695ffd83dbSDimitry Andric   assert((Kind == FileLineInfoKind::AbsoluteFilePath ||
14705ffd83dbSDimitry Andric           Kind == FileLineInfoKind::RelativeFilePath) &&
14715ffd83dbSDimitry Andric          "invalid FileLineInfo Kind");
14725ffd83dbSDimitry Andric 
14730b57cec5SDimitry Andric   // sys::path::append skips empty strings.
14748bcb0991SDimitry Andric   sys::path::append(FilePath, Style, IncludeDir, FileName);
14757a6dacacSDimitry Andric   Result = std::string(FilePath);
14760b57cec5SDimitry Andric   return true;
14770b57cec5SDimitry Andric }
14780b57cec5SDimitry Andric 
14790b57cec5SDimitry Andric bool DWARFDebugLine::LineTable::getFileLineInfoForAddress(
14800b57cec5SDimitry Andric     object::SectionedAddress Address, const char *CompDir,
14810b57cec5SDimitry Andric     FileLineInfoKind Kind, DILineInfo &Result) const {
14820b57cec5SDimitry Andric   // Get the index of row we're looking for in the line table.
14830b57cec5SDimitry Andric   uint32_t RowIndex = lookupAddress(Address);
14840b57cec5SDimitry Andric   if (RowIndex == -1U)
14850b57cec5SDimitry Andric     return false;
14860b57cec5SDimitry Andric   // Take file number and line/column from the row.
14870b57cec5SDimitry Andric   const auto &Row = Rows[RowIndex];
14880b57cec5SDimitry Andric   if (!getFileNameByIndex(Row.File, CompDir, Kind, Result.FileName))
14890b57cec5SDimitry Andric     return false;
14900b57cec5SDimitry Andric   Result.Line = Row.Line;
14910b57cec5SDimitry Andric   Result.Column = Row.Column;
14920b57cec5SDimitry Andric   Result.Discriminator = Row.Discriminator;
14930b57cec5SDimitry Andric   Result.Source = getSourceByIndex(Row.File, Kind);
14940b57cec5SDimitry Andric   return true;
14950b57cec5SDimitry Andric }
14960b57cec5SDimitry Andric 
1497bdd1243dSDimitry Andric bool DWARFDebugLine::LineTable::getDirectoryForEntry(
1498bdd1243dSDimitry Andric     const FileNameEntry &Entry, std::string &Directory) const {
1499bdd1243dSDimitry Andric   if (Prologue.getVersion() >= 5) {
1500bdd1243dSDimitry Andric     if (Entry.DirIdx < Prologue.IncludeDirectories.size()) {
1501bdd1243dSDimitry Andric       Directory =
1502bdd1243dSDimitry Andric           dwarf::toString(Prologue.IncludeDirectories[Entry.DirIdx], "");
1503bdd1243dSDimitry Andric       return true;
1504bdd1243dSDimitry Andric     }
1505bdd1243dSDimitry Andric     return false;
1506bdd1243dSDimitry Andric   }
1507bdd1243dSDimitry Andric   if (0 < Entry.DirIdx && Entry.DirIdx <= Prologue.IncludeDirectories.size()) {
1508bdd1243dSDimitry Andric     Directory =
1509bdd1243dSDimitry Andric         dwarf::toString(Prologue.IncludeDirectories[Entry.DirIdx - 1], "");
1510bdd1243dSDimitry Andric     return true;
1511bdd1243dSDimitry Andric   }
1512bdd1243dSDimitry Andric   return false;
1513bdd1243dSDimitry Andric }
1514bdd1243dSDimitry Andric 
15150b57cec5SDimitry Andric // We want to supply the Unit associated with a .debug_line[.dwo] table when
15160b57cec5SDimitry Andric // we dump it, if possible, but still dump the table even if there isn't a Unit.
15170b57cec5SDimitry Andric // Therefore, collect up handles on all the Units that point into the
15180b57cec5SDimitry Andric // line-table section.
15190b57cec5SDimitry Andric static DWARFDebugLine::SectionParser::LineToUnitMap
1520e8d8bef9SDimitry Andric buildLineToUnitMap(DWARFUnitVector::iterator_range Units) {
15210b57cec5SDimitry Andric   DWARFDebugLine::SectionParser::LineToUnitMap LineToUnit;
1522e8d8bef9SDimitry Andric   for (const auto &U : Units)
1523e8d8bef9SDimitry Andric     if (auto CUDIE = U->getUnitDIE())
15240b57cec5SDimitry Andric       if (auto StmtOffset = toSectionOffset(CUDIE.find(DW_AT_stmt_list)))
1525e8d8bef9SDimitry Andric         LineToUnit.insert(std::make_pair(*StmtOffset, &*U));
15260b57cec5SDimitry Andric   return LineToUnit;
15270b57cec5SDimitry Andric }
15280b57cec5SDimitry Andric 
1529e8d8bef9SDimitry Andric DWARFDebugLine::SectionParser::SectionParser(
1530e8d8bef9SDimitry Andric     DWARFDataExtractor &Data, const DWARFContext &C,
1531e8d8bef9SDimitry Andric     DWARFUnitVector::iterator_range Units)
15320b57cec5SDimitry Andric     : DebugLineData(Data), Context(C) {
1533e8d8bef9SDimitry Andric   LineToUnit = buildLineToUnitMap(Units);
15340b57cec5SDimitry Andric   if (!DebugLineData.isValidOffset(Offset))
15350b57cec5SDimitry Andric     Done = true;
15360b57cec5SDimitry Andric }
15370b57cec5SDimitry Andric 
15380b57cec5SDimitry Andric bool DWARFDebugLine::Prologue::totalLengthIsValid() const {
15395ffd83dbSDimitry Andric   return TotalLength != 0u;
15400b57cec5SDimitry Andric }
15410b57cec5SDimitry Andric 
15420b57cec5SDimitry Andric DWARFDebugLine::LineTable DWARFDebugLine::SectionParser::parseNext(
15435ffd83dbSDimitry Andric     function_ref<void(Error)> RecoverableErrorHandler,
15445ffd83dbSDimitry Andric     function_ref<void(Error)> UnrecoverableErrorHandler, raw_ostream *OS,
15455ffd83dbSDimitry Andric     bool Verbose) {
15460b57cec5SDimitry Andric   assert(DebugLineData.isValidOffset(Offset) &&
15470b57cec5SDimitry Andric          "parsing should have terminated");
15480b57cec5SDimitry Andric   DWARFUnit *U = prepareToParse(Offset);
15498bcb0991SDimitry Andric   uint64_t OldOffset = Offset;
15500b57cec5SDimitry Andric   LineTable LT;
15510b57cec5SDimitry Andric   if (Error Err = LT.parse(DebugLineData, &Offset, Context, U,
15525ffd83dbSDimitry Andric                            RecoverableErrorHandler, OS, Verbose))
15535ffd83dbSDimitry Andric     UnrecoverableErrorHandler(std::move(Err));
15540b57cec5SDimitry Andric   moveToNextTable(OldOffset, LT.Prologue);
15550b57cec5SDimitry Andric   return LT;
15560b57cec5SDimitry Andric }
15570b57cec5SDimitry Andric 
15580b57cec5SDimitry Andric void DWARFDebugLine::SectionParser::skip(
15595ffd83dbSDimitry Andric     function_ref<void(Error)> RecoverableErrorHandler,
15605ffd83dbSDimitry Andric     function_ref<void(Error)> UnrecoverableErrorHandler) {
15610b57cec5SDimitry Andric   assert(DebugLineData.isValidOffset(Offset) &&
15620b57cec5SDimitry Andric          "parsing should have terminated");
15630b57cec5SDimitry Andric   DWARFUnit *U = prepareToParse(Offset);
15648bcb0991SDimitry Andric   uint64_t OldOffset = Offset;
15650b57cec5SDimitry Andric   LineTable LT;
15665ffd83dbSDimitry Andric   if (Error Err = LT.Prologue.parse(DebugLineData, &Offset,
15675ffd83dbSDimitry Andric                                     RecoverableErrorHandler, Context, U))
15685ffd83dbSDimitry Andric     UnrecoverableErrorHandler(std::move(Err));
15690b57cec5SDimitry Andric   moveToNextTable(OldOffset, LT.Prologue);
15700b57cec5SDimitry Andric }
15710b57cec5SDimitry Andric 
15728bcb0991SDimitry Andric DWARFUnit *DWARFDebugLine::SectionParser::prepareToParse(uint64_t Offset) {
15730b57cec5SDimitry Andric   DWARFUnit *U = nullptr;
15740b57cec5SDimitry Andric   auto It = LineToUnit.find(Offset);
15750b57cec5SDimitry Andric   if (It != LineToUnit.end())
15760b57cec5SDimitry Andric     U = It->second;
15770b57cec5SDimitry Andric   DebugLineData.setAddressSize(U ? U->getAddressByteSize() : 0);
15780b57cec5SDimitry Andric   return U;
15790b57cec5SDimitry Andric }
15800b57cec5SDimitry Andric 
158106c3fb27SDimitry Andric bool DWARFDebugLine::SectionParser::hasValidVersion(uint64_t Offset) {
158206c3fb27SDimitry Andric   DataExtractor::Cursor Cursor(Offset);
158306c3fb27SDimitry Andric   auto [TotalLength, _] = DebugLineData.getInitialLength(Cursor);
158406c3fb27SDimitry Andric   DWARFDataExtractor HeaderData(DebugLineData, Cursor.tell() + TotalLength);
158506c3fb27SDimitry Andric   uint16_t Version = HeaderData.getU16(Cursor);
158606c3fb27SDimitry Andric   if (!Cursor) {
158706c3fb27SDimitry Andric     // Ignore any error here.
158806c3fb27SDimitry Andric     // If this is not the end of the section parseNext() will still be
158906c3fb27SDimitry Andric     // attempted, where this error will occur again (and can be handled).
159006c3fb27SDimitry Andric     consumeError(Cursor.takeError());
159106c3fb27SDimitry Andric     return false;
159206c3fb27SDimitry Andric   }
159306c3fb27SDimitry Andric   return versionIsSupported(Version);
159406c3fb27SDimitry Andric }
159506c3fb27SDimitry Andric 
15968bcb0991SDimitry Andric void DWARFDebugLine::SectionParser::moveToNextTable(uint64_t OldOffset,
15970b57cec5SDimitry Andric                                                     const Prologue &P) {
15980b57cec5SDimitry Andric   // If the length field is not valid, we don't know where the next table is, so
15990b57cec5SDimitry Andric   // cannot continue to parse. Mark the parser as done, and leave the Offset
16000b57cec5SDimitry Andric   // value as it currently is. This will be the end of the bad length field.
16010b57cec5SDimitry Andric   if (!P.totalLengthIsValid()) {
16020b57cec5SDimitry Andric     Done = true;
16030b57cec5SDimitry Andric     return;
16040b57cec5SDimitry Andric   }
16050b57cec5SDimitry Andric 
16060b57cec5SDimitry Andric   Offset = OldOffset + P.TotalLength + P.sizeofTotalLength();
16070b57cec5SDimitry Andric   if (!DebugLineData.isValidOffset(Offset)) {
16080b57cec5SDimitry Andric     Done = true;
160906c3fb27SDimitry Andric     return;
161006c3fb27SDimitry Andric   }
161106c3fb27SDimitry Andric 
161206c3fb27SDimitry Andric   // Heuristic: If the version is valid, then this is probably a line table.
161306c3fb27SDimitry Andric   // Otherwise, the offset might need alignment (to a 4 or 8 byte boundary).
161406c3fb27SDimitry Andric   if (hasValidVersion(Offset))
161506c3fb27SDimitry Andric     return;
161606c3fb27SDimitry Andric 
161706c3fb27SDimitry Andric   // ARM C/C++ Compiler aligns each line table to word boundaries and pads out
161806c3fb27SDimitry Andric   // the .debug_line section to a word multiple. Note that in the specification
161906c3fb27SDimitry Andric   // this does not seem forbidden since each unit has a DW_AT_stmt_list.
162006c3fb27SDimitry Andric   for (unsigned Align : {4, 8}) {
162106c3fb27SDimitry Andric     uint64_t AlignedOffset = alignTo(Offset, Align);
162206c3fb27SDimitry Andric     if (!DebugLineData.isValidOffset(AlignedOffset)) {
162306c3fb27SDimitry Andric       // This is almost certainly not another line table but some alignment
162406c3fb27SDimitry Andric       // padding. This assumes the alignments tested are ordered, and are
162506c3fb27SDimitry Andric       // smaller than the header size (which is true for 4 and 8).
162606c3fb27SDimitry Andric       Done = true;
162706c3fb27SDimitry Andric       return;
162806c3fb27SDimitry Andric     }
162906c3fb27SDimitry Andric     if (hasValidVersion(AlignedOffset)) {
163006c3fb27SDimitry Andric       Offset = AlignedOffset;
163106c3fb27SDimitry Andric       break;
163206c3fb27SDimitry Andric     }
16330b57cec5SDimitry Andric   }
16340b57cec5SDimitry Andric }
1635