LCOV - code coverage report
Current view: top level - lib/DebugInfo/DWARF - DWARFDebugLine.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 451 553 81.6 %
Date: 2018-09-23 13:06:45 Functions: 37 39 94.9 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- DWARFDebugLine.cpp -------------------------------------------------===//
       2             : //
       3             : //                     The LLVM Compiler Infrastructure
       4             : //
       5             : // This file is distributed under the University of Illinois Open Source
       6             : // License. See LICENSE.TXT for details.
       7             : //
       8             : //===----------------------------------------------------------------------===//
       9             : 
      10             : #include "llvm/DebugInfo/DWARF/DWARFDebugLine.h"
      11             : #include "llvm/ADT/Optional.h"
      12             : #include "llvm/ADT/SmallString.h"
      13             : #include "llvm/ADT/SmallVector.h"
      14             : #include "llvm/ADT/StringRef.h"
      15             : #include "llvm/BinaryFormat/Dwarf.h"
      16             : #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
      17             : #include "llvm/DebugInfo/DWARF/DWARFRelocMap.h"
      18             : #include "llvm/Support/Errc.h"
      19             : #include "llvm/Support/Format.h"
      20             : #include "llvm/Support/Path.h"
      21             : #include "llvm/Support/WithColor.h"
      22             : #include "llvm/Support/raw_ostream.h"
      23             : #include <algorithm>
      24             : #include <cassert>
      25             : #include <cinttypes>
      26             : #include <cstdint>
      27             : #include <cstdio>
      28             : #include <utility>
      29             : 
      30             : using namespace llvm;
      31             : using namespace dwarf;
      32             : 
      33             : using FileLineInfoKind = DILineInfoSpecifier::FileLineInfoKind;
      34             : 
      35             : namespace {
      36             : 
      37             : struct ContentDescriptor {
      38             :   dwarf::LineNumberEntryFormat Type;
      39             :   dwarf::Form Form;
      40             : };
      41             : 
      42             : using ContentDescriptors = SmallVector<ContentDescriptor, 4>;
      43             : 
      44             : } // end anonmyous namespace
      45             : 
      46         182 : void DWARFDebugLine::ContentTypeTracker::trackContentType(
      47             :     dwarf::LineNumberEntryFormat ContentType) {
      48         182 :   switch (ContentType) {
      49           4 :   case dwarf::DW_LNCT_timestamp:
      50           4 :     HasModTime = true;
      51           4 :     break;
      52           4 :   case dwarf::DW_LNCT_size:
      53           4 :     HasLength = true;
      54           4 :     break;
      55          33 :   case dwarf::DW_LNCT_MD5:
      56          33 :     HasMD5 = true;
      57          33 :     break;
      58           5 :   case dwarf::DW_LNCT_LLVM_source:
      59           5 :     HasSource = true;
      60           5 :     break;
      61             :   default:
      62             :     // We only care about values we consider optional, and new values may be
      63             :     // added in the vendor extension range, so we do not match exhaustively.
      64             :     break;
      65             :   }
      66         182 : }
      67             : 
      68        2926 : DWARFDebugLine::Prologue::Prologue() { clear(); }
      69             : 
      70        5706 : void DWARFDebugLine::Prologue::clear() {
      71        5706 :   TotalLength = PrologueLength = 0;
      72        5706 :   SegSelectorSize = 0;
      73        5706 :   MinInstLength = MaxOpsPerInst = DefaultIsStmt = LineBase = LineRange = 0;
      74        5706 :   OpcodeBase = 0;
      75        5706 :   FormParams = dwarf::FormParams({0, 0, DWARF32});
      76        5706 :   ContentTypes = ContentTypeTracker();
      77             :   StandardOpcodeLengths.clear();
      78             :   IncludeDirectories.clear();
      79             :   FileNames.clear();
      80        5706 : }
      81             : 
      82         347 : void DWARFDebugLine::Prologue::dump(raw_ostream &OS,
      83             :                                     DIDumpOptions DumpOptions) const {
      84         347 :   OS << "Line table prologue:\n"
      85         694 :      << format("    total_length: 0x%8.8" PRIx64 "\n", TotalLength)
      86         694 :      << format("         version: %u\n", getVersion());
      87         347 :   if (getVersion() >= 5)
      88          82 :     OS << format("    address_size: %u\n", getAddressSize())
      89          41 :        << format(" seg_select_size: %u\n", SegSelectorSize);
      90         694 :   OS << format(" prologue_length: 0x%8.8" PRIx64 "\n", PrologueLength)
      91         347 :      << format(" min_inst_length: %u\n", MinInstLength)
      92         506 :      << format(getVersion() >= 4 ? "max_ops_per_inst: %u\n" : "", MaxOpsPerInst)
      93         347 :      << format(" default_is_stmt: %u\n", DefaultIsStmt)
      94         347 :      << format("       line_base: %i\n", LineBase)
      95         347 :      << format("      line_range: %u\n", LineRange)
      96         347 :      << format("     opcode_base: %u\n", OpcodeBase);
      97             : 
      98        8656 :   for (uint32_t I = 0; I != StandardOpcodeLengths.size(); ++I)
      99        3981 :     OS << format("standard_opcode_lengths[%s] = %u\n",
     100        3981 :                  LNStandardString(I + 1).data(), StandardOpcodeLengths[I]);
     101             : 
     102         347 :   if (!IncludeDirectories.empty()) {
     103             :     // DWARF v5 starts directory indexes at 0.
     104          79 :     uint32_t DirBase = getVersion() >= 5 ? 0 : 1;
     105         275 :     for (uint32_t I = 0; I != IncludeDirectories.size(); ++I) {
     106         234 :       OS << format("include_directories[%3u] = ", I + DirBase);
     107         234 :       IncludeDirectories[I].dump(OS, DumpOptions);
     108             :       OS << '\n';
     109             :     }
     110             :   }
     111             : 
     112         347 :   if (!FileNames.empty()) {
     113             :     // DWARF v5 starts file indexes at 0.
     114         314 :     uint32_t FileBase = getVersion() >= 5 ? 0 : 1;
     115        1045 :     for (uint32_t I = 0; I != FileNames.size(); ++I) {
     116             :       const FileNameEntry &FileEntry = FileNames[I];
     117         834 :       OS <<   format("file_names[%3u]:\n", I + FileBase);
     118         417 :       OS <<          "           name: ";
     119         417 :       FileEntry.Name.dump(OS, DumpOptions);
     120             :       OS << '\n'
     121         834 :          <<   format("      dir_index: %" PRIu64 "\n", FileEntry.DirIdx);
     122         417 :       if (ContentTypes.HasMD5)
     123          78 :         OS <<        "   md5_checksum: " << FileEntry.Checksum.digest() << '\n';
     124         417 :       if (ContentTypes.HasModTime)
     125         686 :         OS << format("       mod_time: 0x%8.8" PRIx64 "\n", FileEntry.ModTime);
     126         417 :       if (ContentTypes.HasLength)
     127         686 :         OS << format("         length: 0x%8.8" PRIx64 "\n", FileEntry.Length);
     128         417 :       if (ContentTypes.HasSource) {
     129           9 :         OS <<        "         source: ";
     130           9 :         FileEntry.Source.dump(OS, DumpOptions);
     131             :         OS << '\n';
     132             :       }
     133             :     }
     134             :   }
     135         347 : }
     136             : 
     137             : // Parse v2-v4 directory and file tables.
     138             : static void
     139           0 : parseV2DirFileTables(const DWARFDataExtractor &DebugLineData,
     140             :                      uint32_t *OffsetPtr, uint64_t EndPrologueOffset,
     141             :                      DWARFDebugLine::ContentTypeTracker &ContentTypes,
     142             :                      std::vector<DWARFFormValue> &IncludeDirectories,
     143             :                      std::vector<DWARFDebugLine::FileNameEntry> &FileNames) {
     144           0 :   while (*OffsetPtr < EndPrologueOffset) {
     145           0 :     StringRef S = DebugLineData.getCStrRef(OffsetPtr);
     146           0 :     if (S.empty())
     147             :       break;
     148             :     DWARFFormValue Dir(dwarf::DW_FORM_string);
     149             :     Dir.setPValue(S.data());
     150           0 :     IncludeDirectories.push_back(Dir);
     151             :   }
     152             : 
     153           0 :   while (*OffsetPtr < EndPrologueOffset) {
     154           0 :     StringRef Name = DebugLineData.getCStrRef(OffsetPtr);
     155           0 :     if (Name.empty())
     156             :       break;
     157             :     DWARFDebugLine::FileNameEntry FileEntry;
     158             :     FileEntry.Name.setForm(dwarf::DW_FORM_string);
     159             :     FileEntry.Name.setPValue(Name.data());
     160           0 :     FileEntry.DirIdx = DebugLineData.getULEB128(OffsetPtr);
     161           0 :     FileEntry.ModTime = DebugLineData.getULEB128(OffsetPtr);
     162           0 :     FileEntry.Length = DebugLineData.getULEB128(OffsetPtr);
     163           0 :     FileNames.push_back(FileEntry);
     164             :   }
     165             : 
     166           0 :   ContentTypes.HasModTime = true;
     167           0 :   ContentTypes.HasLength = true;
     168           0 : }
     169             : 
     170             : // Parse v5 directory/file entry content descriptions.
     171             : // Returns the descriptors, or an empty vector if we did not find a path or
     172             : // ran off the end of the prologue.
     173             : static ContentDescriptors
     174         150 : parseV5EntryFormat(const DWARFDataExtractor &DebugLineData, uint32_t
     175             :     *OffsetPtr, uint64_t EndPrologueOffset, DWARFDebugLine::ContentTypeTracker
     176             :     *ContentTypes) {
     177         150 :   ContentDescriptors Descriptors;
     178         150 :   int FormatCount = DebugLineData.getU8(OffsetPtr);
     179             :   bool HasPath = false;
     180         408 :   for (int I = 0; I != FormatCount; ++I) {
     181         258 :     if (*OffsetPtr >= EndPrologueOffset)
     182           0 :       return ContentDescriptors();
     183             :     ContentDescriptor Descriptor;
     184         258 :     Descriptor.Type =
     185         258 :       dwarf::LineNumberEntryFormat(DebugLineData.getULEB128(OffsetPtr));
     186         258 :     Descriptor.Form = dwarf::Form(DebugLineData.getULEB128(OffsetPtr));
     187         258 :     if (Descriptor.Type == dwarf::DW_LNCT_path)
     188             :       HasPath = true;
     189         258 :     if (ContentTypes)
     190         182 :       ContentTypes->trackContentType(Descriptor.Type);
     191         258 :     Descriptors.push_back(Descriptor);
     192             :   }
     193         150 :   return HasPath ? Descriptors : ContentDescriptors();
     194             : }
     195             : 
     196             : static bool
     197           0 : parseV5DirFileTables(const DWARFDataExtractor &DebugLineData,
     198             :                      uint32_t *OffsetPtr, uint64_t EndPrologueOffset,
     199             :                      const dwarf::FormParams &FormParams,
     200             :                      const DWARFContext &Ctx, const DWARFUnit *U,
     201             :                      DWARFDebugLine::ContentTypeTracker &ContentTypes,
     202             :                      std::vector<DWARFFormValue> &IncludeDirectories,
     203             :                      std::vector<DWARFDebugLine::FileNameEntry> &FileNames) {
     204             :   // Get the directory entry description.
     205             :   ContentDescriptors DirDescriptors =
     206           0 :       parseV5EntryFormat(DebugLineData, OffsetPtr, EndPrologueOffset, nullptr);
     207           0 :   if (DirDescriptors.empty())
     208           0 :     return false;
     209             : 
     210             :   // Get the directory entries, according to the format described above.
     211           0 :   int DirEntryCount = DebugLineData.getU8(OffsetPtr);
     212           0 :   for (int I = 0; I != DirEntryCount; ++I) {
     213           0 :     if (*OffsetPtr >= EndPrologueOffset)
     214           0 :       return false;
     215           0 :     for (auto Descriptor : DirDescriptors) {
     216           0 :       DWARFFormValue Value(Descriptor.Form);
     217           0 :       switch (Descriptor.Type) {
     218           0 :       case DW_LNCT_path:
     219           0 :         if (!Value.extractValue(DebugLineData, OffsetPtr, FormParams, &Ctx, U))
     220           0 :           return false;
     221           0 :         IncludeDirectories.push_back(Value);
     222           0 :         break;
     223           0 :       default:
     224           0 :         if (!Value.skipValue(DebugLineData, OffsetPtr, FormParams))
     225           0 :           return false;
     226             :       }
     227             :     }
     228             :   }
     229             : 
     230             :   // Get the file entry description.
     231             :   ContentDescriptors FileDescriptors =
     232             :       parseV5EntryFormat(DebugLineData, OffsetPtr, EndPrologueOffset,
     233           0 :           &ContentTypes);
     234           0 :   if (FileDescriptors.empty())
     235           0 :     return false;
     236             : 
     237             :   // Get the file entries, according to the format described above.
     238           0 :   int FileEntryCount = DebugLineData.getU8(OffsetPtr);
     239           0 :   for (int I = 0; I != FileEntryCount; ++I) {
     240           0 :     if (*OffsetPtr >= EndPrologueOffset)
     241           0 :       return false;
     242             :     DWARFDebugLine::FileNameEntry FileEntry;
     243           0 :     for (auto Descriptor : FileDescriptors) {
     244             :       DWARFFormValue Value(Descriptor.Form);
     245           0 :       if (!Value.extractValue(DebugLineData, OffsetPtr, FormParams, &Ctx, U))
     246           0 :         return false;
     247           0 :       switch (Descriptor.Type) {
     248           0 :       case DW_LNCT_path:
     249           0 :         FileEntry.Name = Value;
     250           0 :         break;
     251           0 :       case DW_LNCT_LLVM_source:
     252           0 :         FileEntry.Source = Value;
     253           0 :         break;
     254           0 :       case DW_LNCT_directory_index:
     255           0 :         FileEntry.DirIdx = Value.getAsUnsignedConstant().getValue();
     256           0 :         break;
     257           0 :       case DW_LNCT_timestamp:
     258           0 :         FileEntry.ModTime = Value.getAsUnsignedConstant().getValue();
     259           0 :         break;
     260           0 :       case DW_LNCT_size:
     261           0 :         FileEntry.Length = Value.getAsUnsignedConstant().getValue();
     262           0 :         break;
     263             :       case DW_LNCT_MD5:
     264             :         assert(Value.getAsBlock().getValue().size() == 16);
     265           0 :         std::uninitialized_copy_n(Value.getAsBlock().getValue().begin(), 16,
     266             :                                   FileEntry.Checksum.Bytes.begin());
     267           0 :         break;
     268             :       default:
     269             :         break;
     270             :       }
     271             :     }
     272           0 :     FileNames.push_back(FileEntry);
     273             :   }
     274             :   return true;
     275             : }
     276             : 
     277        1421 : Error DWARFDebugLine::Prologue::parse(const DWARFDataExtractor &DebugLineData,
     278             :                                       uint32_t *OffsetPtr,
     279             :                                       const DWARFContext &Ctx,
     280             :                                       const DWARFUnit *U) {
     281        1421 :   const uint64_t PrologueOffset = *OffsetPtr;
     282             : 
     283        1421 :   clear();
     284        1421 :   TotalLength = DebugLineData.getU32(OffsetPtr);
     285        1421 :   if (TotalLength == UINT32_MAX) {
     286          16 :     FormParams.Format = dwarf::DWARF64;
     287          16 :     TotalLength = DebugLineData.getU64(OffsetPtr);
     288        1405 :   } else if (TotalLength >= 0xffffff00) {
     289             :     return createStringError(errc::invalid_argument,
     290             :         "parsing line table prologue at offset 0x%8.8" PRIx64
     291             :         " unsupported reserved unit length found of value 0x%8.8" PRIx64,
     292          14 :         PrologueOffset, TotalLength);
     293             :   }
     294        1414 :   FormParams.Version = DebugLineData.getU16(OffsetPtr);
     295        1414 :   if (getVersion() < 2)
     296             :     return createStringError(errc::not_supported,
     297             :                        "parsing line table prologue at offset 0x%8.8" PRIx64
     298             :                        " found unsupported version 0x%2.2" PRIx16,
     299          28 :                        PrologueOffset, getVersion());
     300             : 
     301        1400 :   if (getVersion() >= 5) {
     302          78 :     FormParams.AddrSize = DebugLineData.getU8(OffsetPtr);
     303             :     assert((DebugLineData.getAddressSize() == 0 ||
     304             :             DebugLineData.getAddressSize() == getAddressSize()) &&
     305             :            "Line table header and data extractor disagree");
     306          78 :     SegSelectorSize = DebugLineData.getU8(OffsetPtr);
     307             :   }
     308             : 
     309        1400 :   PrologueLength = DebugLineData.getUnsigned(OffsetPtr, sizeofPrologueLength());
     310        1400 :   const uint64_t EndPrologueOffset = PrologueLength + *OffsetPtr;
     311        1400 :   MinInstLength = DebugLineData.getU8(OffsetPtr);
     312        1400 :   if (getVersion() >= 4)
     313         590 :     MaxOpsPerInst = DebugLineData.getU8(OffsetPtr);
     314        1400 :   DefaultIsStmt = DebugLineData.getU8(OffsetPtr);
     315        1400 :   LineBase = DebugLineData.getU8(OffsetPtr);
     316        1400 :   LineRange = DebugLineData.getU8(OffsetPtr);
     317        1400 :   OpcodeBase = DebugLineData.getU8(OffsetPtr);
     318             : 
     319        1400 :   StandardOpcodeLengths.reserve(OpcodeBase - 1);
     320       17934 :   for (uint32_t I = 1; I < OpcodeBase; ++I) {
     321       16534 :     uint8_t OpLen = DebugLineData.getU8(OffsetPtr);
     322       16534 :     StandardOpcodeLengths.push_back(OpLen);
     323             :   }
     324             : 
     325        1400 :   if (getVersion() >= 5) {
     326          78 :     if (!parseV5DirFileTables(DebugLineData, OffsetPtr, EndPrologueOffset,
     327          78 :                               FormParams, Ctx, U, ContentTypes,
     328          78 :                               IncludeDirectories, FileNames)) {
     329             :       return createStringError(errc::invalid_argument,
     330             :           "parsing line table prologue at 0x%8.8" PRIx64
     331             :           " found an invalid directory or file table description at"
     332             :           " 0x%8.8" PRIx64,
     333          12 :           PrologueOffset, (uint64_t)*OffsetPtr);
     334             :     }
     335             :   } else
     336        1322 :     parseV2DirFileTables(DebugLineData, OffsetPtr, EndPrologueOffset,
     337        1322 :                          ContentTypes, IncludeDirectories, FileNames);
     338             : 
     339        1394 :   if (*OffsetPtr != EndPrologueOffset)
     340             :     return createStringError(errc::invalid_argument,
     341             :                        "parsing line table prologue at 0x%8.8" PRIx64
     342             :                        " should have ended at 0x%8.8" PRIx64
     343             :                        " but it ended at 0x%8.8" PRIx64,
     344          40 :                        PrologueOffset, EndPrologueOffset, (uint64_t)*OffsetPtr);
     345             :   return Error::success();
     346             : }
     347             : 
     348        2129 : DWARFDebugLine::Row::Row(bool DefaultIsStmt) { reset(DefaultIsStmt); }
     349             : 
     350        7958 : void DWARFDebugLine::Row::postAppend() {
     351        7958 :   BasicBlock = false;
     352        7958 :   PrologueEnd = false;
     353        7958 :   EpilogueBegin = false;
     354        7958 : }
     355             : 
     356        4816 : void DWARFDebugLine::Row::reset(bool DefaultIsStmt) {
     357        4816 :   Address = 0;
     358        4816 :   Line = 1;
     359        4816 :   Column = 0;
     360        4816 :   File = 1;
     361        4816 :   Isa = 0;
     362        4816 :   Discriminator = 0;
     363        4816 :   IsStmt = DefaultIsStmt;
     364        4816 :   BasicBlock = false;
     365        4816 :   EndSequence = false;
     366        4816 :   PrologueEnd = false;
     367        4816 :   EpilogueBegin = false;
     368        4816 : }
     369             : 
     370         133 : void DWARFDebugLine::Row::dumpTableHeader(raw_ostream &OS) {
     371         133 :   OS << "Address            Line   Column File   ISA Discriminator Flags\n"
     372             :      << "------------------ ------ ------ ------ --- ------------- "
     373         133 :         "-------------\n";
     374         133 : }
     375             : 
     376        1668 : void DWARFDebugLine::Row::dump(raw_ostream &OS) const {
     377        3336 :   OS << format("0x%16.16" PRIx64 " %6u %6u", Address, Line, Column)
     378        3336 :      << format(" %6u %3u %13u ", File, Isa, Discriminator)
     379        3563 :      << (IsStmt ? " is_stmt" : "") << (BasicBlock ? " basic_block" : "")
     380        2981 :      << (PrologueEnd ? " prologue_end" : "")
     381        3336 :      << (EpilogueBegin ? " epilogue_begin" : "")
     382        3024 :      << (EndSequence ? " end_sequence" : "") << '\n';
     383        1668 : }
     384             : 
     385        2835 : DWARFDebugLine::Sequence::Sequence() { reset(); }
     386             : 
     387        6850 : void DWARFDebugLine::Sequence::reset() {
     388        6850 :   LowPC = 0;
     389        6850 :   HighPC = 0;
     390        6850 :   FirstRowIndex = 0;
     391        6850 :   LastRowIndex = 0;
     392        6850 :   Empty = true;
     393        6850 : }
     394             : 
     395        1431 : DWARFDebugLine::LineTable::LineTable() { clear(); }
     396             : 
     397         150 : void DWARFDebugLine::LineTable::dump(raw_ostream &OS,
     398             :                                      DIDumpOptions DumpOptions) const {
     399         150 :   Prologue.dump(OS, DumpOptions);
     400             :   OS << '\n';
     401             : 
     402         150 :   if (!Rows.empty()) {
     403         131 :     Row::dumpTableHeader(OS);
     404         925 :     for (const Row &R : Rows) {
     405         794 :       R.dump(OS);
     406             :     }
     407             :   }
     408         150 : }
     409             : 
     410        2822 : void DWARFDebugLine::LineTable::clear() {
     411        2822 :   Prologue.clear();
     412             :   Rows.clear();
     413             :   Sequences.clear();
     414        2822 : }
     415             : 
     416        1359 : DWARFDebugLine::ParsingState::ParsingState(struct LineTable *LT)
     417        1359 :     : LineTable(LT) {
     418        1359 :   resetRowAndSequence();
     419        1359 : }
     420             : 
     421        2687 : void DWARFDebugLine::ParsingState::resetRowAndSequence() {
     422        2687 :   Row.reset(LineTable->Prologue.DefaultIsStmt);
     423        2687 :   Sequence.reset();
     424        2687 : }
     425             : 
     426        7958 : void DWARFDebugLine::ParsingState::appendRowToMatrix(uint32_t Offset) {
     427        7958 :   if (Sequence.Empty) {
     428             :     // Record the beginning of instruction sequence.
     429        1334 :     Sequence.Empty = false;
     430        1334 :     Sequence.LowPC = Row.Address;
     431        1334 :     Sequence.FirstRowIndex = RowNumber;
     432             :   }
     433        7958 :   ++RowNumber;
     434        7958 :   LineTable->appendRow(Row);
     435        7958 :   if (Row.EndSequence) {
     436             :     // Record the end of instruction sequence.
     437        1328 :     Sequence.HighPC = Row.Address;
     438        1328 :     Sequence.LastRowIndex = RowNumber;
     439             :     if (Sequence.isValid())
     440        1297 :       LineTable->appendSequence(Sequence);
     441        1328 :     Sequence.reset();
     442             :   }
     443        7958 :   Row.postAppend();
     444        7958 : }
     445             : 
     446             : const DWARFDebugLine::LineTable *
     447        3577 : DWARFDebugLine::getLineTable(uint32_t Offset) const {
     448             :   LineTableConstIter Pos = LineTableMap.find(Offset);
     449        3577 :   if (Pos != LineTableMap.end())
     450        2724 :     return &Pos->second;
     451             :   return nullptr;
     452             : }
     453             : 
     454         872 : Expected<const DWARFDebugLine::LineTable *> DWARFDebugLine::getOrParseLineTable(
     455             :     DWARFDataExtractor &DebugLineData, uint32_t Offset, const DWARFContext &Ctx,
     456             :     const DWARFUnit *U, std::function<void(Error)> RecoverableErrorCallback) {
     457        1744 :   if (!DebugLineData.isValidOffset(Offset))
     458           4 :     return createStringError(errc::invalid_argument, "offset 0x%8.8" PRIx32
     459             :                        " is not a valid debug line section offset",
     460             :                        Offset);
     461             : 
     462             :   std::pair<LineTableIter, bool> Pos =
     463        1736 :       LineTableMap.insert(LineTableMapTy::value_type(Offset, LineTable()));
     464         868 :   LineTable *LT = &Pos.first->second;
     465         868 :   if (Pos.second) {
     466         858 :     if (Error Err =
     467        1716 :             LT->parse(DebugLineData, &Offset, Ctx, U, RecoverableErrorCallback))
     468             :       return std::move(Err);
     469             :     return LT;
     470             :   }
     471             :   return LT;
     472             : }
     473             : 
     474        1391 : Error DWARFDebugLine::LineTable::parse(
     475             :     DWARFDataExtractor &DebugLineData, uint32_t *OffsetPtr,
     476             :     const DWARFContext &Ctx, const DWARFUnit *U,
     477             :     std::function<void(Error)> RecoverableErrorCallback, raw_ostream *OS) {
     478        1391 :   const uint32_t DebugLineOffset = *OffsetPtr;
     479             : 
     480        1391 :   clear();
     481             : 
     482        1391 :   Error PrologueErr = Prologue.parse(DebugLineData, OffsetPtr, Ctx, U);
     483             : 
     484        1391 :   if (OS) {
     485             :     // The presence of OS signals verbose dumping.
     486         197 :     DIDumpOptions DumpOptions;
     487         197 :     DumpOptions.Verbose = true;
     488         197 :     Prologue.dump(*OS, DumpOptions);
     489             :   }
     490             : 
     491        1391 :   if (PrologueErr)
     492             :     return PrologueErr;
     493             : 
     494             :   const uint32_t EndOffset =
     495        1359 :       DebugLineOffset + Prologue.TotalLength + Prologue.sizeofTotalLength();
     496             : 
     497             :   // See if we should tell the data extractor the address size.
     498        1359 :   if (DebugLineData.getAddressSize() == 0)
     499          48 :     DebugLineData.setAddressSize(Prologue.getAddressSize());
     500             :   else
     501             :     assert(Prologue.getAddressSize() == 0 ||
     502             :            Prologue.getAddressSize() == DebugLineData.getAddressSize());
     503             : 
     504        1359 :   ParsingState State(this);
     505             : 
     506       20078 :   while (*OffsetPtr < EndOffset) {
     507       18727 :     if (OS)
     508        2033 :       *OS << format("0x%08.08" PRIx32 ": ", *OffsetPtr);
     509             : 
     510       18727 :     uint8_t Opcode = DebugLineData.getU8(OffsetPtr);
     511             : 
     512       18727 :     if (OS)
     513        2033 :       *OS << format("%02.02" PRIx8 " ", Opcode);
     514             : 
     515       18727 :     if (Opcode == 0) {
     516             :       // Extended Opcodes always start with a zero opcode followed by
     517             :       // a uleb128 length so you can skip ones you don't know about
     518        3046 :       uint64_t Len = DebugLineData.getULEB128(OffsetPtr);
     519        3046 :       uint32_t ExtOffset = *OffsetPtr;
     520             : 
     521             :       // Tolerate zero-length; assume length is correct and soldier on.
     522        3046 :       if (Len == 0) {
     523           2 :         if (OS)
     524           2 :           *OS << "Badly formed extended line op (length 0)\n";
     525           2 :         continue;
     526             :       }
     527             : 
     528        3044 :       uint8_t SubOpcode = DebugLineData.getU8(OffsetPtr);
     529        3044 :       if (OS)
     530         260 :         *OS << LNExtendedString(SubOpcode);
     531        3044 :       switch (SubOpcode) {
     532        1328 :       case DW_LNE_end_sequence:
     533             :         // Set the end_sequence register of the state machine to true and
     534             :         // append a row to the matrix using the current values of the
     535             :         // state-machine registers. Then reset the registers to the initial
     536             :         // values specified above. Every statement program sequence must end
     537             :         // with a DW_LNE_end_sequence instruction which creates a row whose
     538             :         // address is that of the byte after the last target machine instruction
     539             :         // of the sequence.
     540        1328 :         State.Row.EndSequence = true;
     541        1328 :         State.appendRowToMatrix(*OffsetPtr);
     542        1328 :         if (OS) {
     543         129 :           *OS << "\n";
     544         129 :           OS->indent(12);
     545         129 :           State.Row.dump(*OS);
     546             :         }
     547        1328 :         State.resetRowAndSequence();
     548        1328 :         break;
     549             : 
     550        1322 :       case DW_LNE_set_address:
     551             :         // Takes a single relocatable address as an operand. The size of the
     552             :         // operand is the size appropriate to hold an address on the target
     553             :         // machine. Set the address register to the value given by the
     554             :         // relocatable address. All of the other statement program opcodes
     555             :         // that affect the address register add a delta to it. This instruction
     556             :         // stores a relocatable value into it instead.
     557             :         //
     558             :         // Make sure the extractor knows the address size.  If not, infer it
     559             :         // from the size of the operand.
     560        1322 :         if (DebugLineData.getAddressSize() == 0)
     561          19 :           DebugLineData.setAddressSize(Len - 1);
     562        1303 :         else if (DebugLineData.getAddressSize() != Len - 1) {
     563             :           return createStringError(errc::invalid_argument,
     564             :                              "mismatching address size at offset 0x%8.8" PRIx32
     565             :                              " expected 0x%2.2" PRIx8 " found 0x%2.2" PRIx64,
     566           4 :                              ExtOffset, DebugLineData.getAddressSize(),
     567           4 :                              Len - 1);
     568             :         }
     569        1320 :         State.Row.Address = DebugLineData.getRelocatedAddress(OffsetPtr);
     570        1320 :         if (OS)
     571         129 :           *OS << format(" (0x%16.16" PRIx64 ")", State.Row.Address);
     572             :         break;
     573             : 
     574             :       case DW_LNE_define_file:
     575             :         // Takes 4 arguments. The first is a null terminated string containing
     576             :         // a source file name. The second is an unsigned LEB128 number
     577             :         // representing the directory index of the directory in which the file
     578             :         // was found. The third is an unsigned LEB128 number representing the
     579             :         // time of last modification of the file. The fourth is an unsigned
     580             :         // LEB128 number representing the length in bytes of the file. The time
     581             :         // and length fields may contain LEB128(0) if the information is not
     582             :         // available.
     583             :         //
     584             :         // The directory index represents an entry in the include_directories
     585             :         // section of the statement program prologue. The index is LEB128(0)
     586             :         // if the file was found in the current directory of the compilation,
     587             :         // LEB128(1) if it was found in the first directory in the
     588             :         // include_directories section, and so on. The directory index is
     589             :         // ignored for file names that represent full path names.
     590             :         //
     591             :         // The files are numbered, starting at 1, in the order in which they
     592             :         // appear; the names in the prologue come before names defined by
     593             :         // the DW_LNE_define_file instruction. These numbers are used in the
     594             :         // the file register of the state machine.
     595             :         {
     596             :           FileNameEntry FileEntry;
     597           0 :           const char *Name = DebugLineData.getCStr(OffsetPtr);
     598             :           FileEntry.Name.setForm(dwarf::DW_FORM_string);
     599             :           FileEntry.Name.setPValue(Name);
     600           0 :           FileEntry.DirIdx = DebugLineData.getULEB128(OffsetPtr);
     601           0 :           FileEntry.ModTime = DebugLineData.getULEB128(OffsetPtr);
     602           0 :           FileEntry.Length = DebugLineData.getULEB128(OffsetPtr);
     603           0 :           Prologue.FileNames.push_back(FileEntry);
     604           0 :           if (OS)
     605           0 :             *OS << " (" << Name << ", dir=" << FileEntry.DirIdx << ", mod_time="
     606           0 :                 << format("(0x%16.16" PRIx64 ")", FileEntry.ModTime)
     607           0 :                 << ", length=" << FileEntry.Length << ")";
     608             :         }
     609           0 :         break;
     610             : 
     611         392 :       case DW_LNE_set_discriminator:
     612         392 :         State.Row.Discriminator = DebugLineData.getULEB128(OffsetPtr);
     613         392 :         if (OS)
     614           0 :           *OS << " (" << State.Row.Discriminator << ")";
     615             :         break;
     616             : 
     617           2 :       default:
     618           2 :         if (OS)
     619           2 :           *OS << format("Unrecognized extended op 0x%02.02" PRIx8, SubOpcode)
     620           4 :               << format(" length %" PRIx64, Len);
     621             :         // Len doesn't include the zero opcode byte or the length itself, but
     622             :         // it does include the sub_opcode, so we have to adjust for that.
     623           2 :         (*OffsetPtr) += Len - 1;
     624           2 :         break;
     625             :       }
     626             :       // Make sure the stated and parsed lengths are the same.
     627             :       // Otherwise we have an unparseable line-number program.
     628        3042 :       if (*OffsetPtr - ExtOffset != Len)
     629             :         return createStringError(errc::illegal_byte_sequence,
     630             :                            "unexpected line op length at offset 0x%8.8" PRIx32
     631             :                            " expected 0x%2.2" PRIx64 " found 0x%2.2" PRIx32,
     632          12 :                            ExtOffset, Len, *OffsetPtr - ExtOffset);
     633       15681 :     } else if (Opcode < Prologue.OpcodeBase) {
     634       10010 :       if (OS)
     635        1111 :         *OS << LNStandardString(Opcode);
     636       10010 :       switch (Opcode) {
     637             :       // Standard Opcodes
     638         959 :       case DW_LNS_copy:
     639             :         // Takes no arguments. Append a row to the matrix using the
     640             :         // current values of the state-machine registers. Then set
     641             :         // the basic_block register to false.
     642         959 :         State.appendRowToMatrix(*OffsetPtr);
     643         959 :         if (OS) {
     644          82 :           *OS << "\n";
     645          82 :           OS->indent(12);
     646          82 :           State.Row.dump(*OS);
     647          82 :           *OS << "\n";
     648             :         }
     649             :         break;
     650             : 
     651        1679 :       case DW_LNS_advance_pc:
     652             :         // Takes a single unsigned LEB128 operand, multiplies it by the
     653             :         // min_inst_length field of the prologue, and adds the
     654             :         // result to the address register of the state machine.
     655             :         {
     656             :           uint64_t AddrOffset =
     657        1679 :               DebugLineData.getULEB128(OffsetPtr) * Prologue.MinInstLength;
     658        1679 :           State.Row.Address += AddrOffset;
     659        1679 :           if (OS)
     660         161 :             *OS << " (" << AddrOffset << ")";
     661             :         }
     662             :         break;
     663             : 
     664        1073 :       case DW_LNS_advance_line:
     665             :         // Takes a single signed LEB128 operand and adds that value to
     666             :         // the line register of the state machine.
     667        1073 :         State.Row.Line += DebugLineData.getSLEB128(OffsetPtr);
     668        1073 :         if (OS)
     669         108 :           *OS << " (" << State.Row.Line << ")";
     670             :         break;
     671             : 
     672         145 :       case DW_LNS_set_file:
     673             :         // Takes a single unsigned LEB128 operand and stores it in the file
     674             :         // register of the state machine.
     675         145 :         State.Row.File = DebugLineData.getULEB128(OffsetPtr);
     676         145 :         if (OS)
     677          12 :           *OS << " (" << State.Row.File << ")";
     678             :         break;
     679             : 
     680        2447 :       case DW_LNS_set_column:
     681             :         // Takes a single unsigned LEB128 operand and stores it in the
     682             :         // column register of the state machine.
     683        2447 :         State.Row.Column = DebugLineData.getULEB128(OffsetPtr);
     684        2447 :         if (OS)
     685         300 :           *OS << " (" << State.Row.Column << ")";
     686             :         break;
     687             : 
     688        1083 :       case DW_LNS_negate_stmt:
     689             :         // Takes no arguments. Set the is_stmt register of the state
     690             :         // machine to the logical negation of its current value.
     691        1083 :         State.Row.IsStmt = !State.Row.IsStmt;
     692        1083 :         break;
     693             : 
     694           0 :       case DW_LNS_set_basic_block:
     695             :         // Takes no arguments. Set the basic_block register of the
     696             :         // state machine to true
     697           0 :         State.Row.BasicBlock = true;
     698           0 :         break;
     699             : 
     700         892 :       case DW_LNS_const_add_pc:
     701             :         // Takes no arguments. Add to the address register of the state
     702             :         // machine the address increment value corresponding to special
     703             :         // opcode 255. The motivation for DW_LNS_const_add_pc is this:
     704             :         // when the statement program needs to advance the address by a
     705             :         // small amount, it can use a single special opcode, which occupies
     706             :         // a single byte. When it needs to advance the address by up to
     707             :         // twice the range of the last special opcode, it can use
     708             :         // DW_LNS_const_add_pc followed by a special opcode, for a total
     709             :         // of two bytes. Only if it needs to advance the address by more
     710             :         // than twice that range will it need to use both DW_LNS_advance_pc
     711             :         // and a special opcode, requiring three or more bytes.
     712             :         {
     713         892 :           uint8_t AdjustOpcode = 255 - Prologue.OpcodeBase;
     714             :           uint64_t AddrOffset =
     715         892 :               (AdjustOpcode / Prologue.LineRange) * Prologue.MinInstLength;
     716         892 :           State.Row.Address += AddrOffset;
     717         892 :           if (OS)
     718             :             *OS
     719         112 :                 << format(" (0x%16.16" PRIx64 ")", AddrOffset);
     720             :         }
     721             :         break;
     722             : 
     723           0 :       case DW_LNS_fixed_advance_pc:
     724             :         // Takes a single uhalf operand. Add to the address register of
     725             :         // the state machine the value of the (unencoded) operand. This
     726             :         // is the only extended opcode that takes an argument that is not
     727             :         // a variable length number. The motivation for DW_LNS_fixed_advance_pc
     728             :         // is this: existing assemblers cannot emit DW_LNS_advance_pc or
     729             :         // special opcodes because they cannot encode LEB128 numbers or
     730             :         // judge when the computation of a special opcode overflows and
     731             :         // requires the use of DW_LNS_advance_pc. Such assemblers, however,
     732             :         // can use DW_LNS_fixed_advance_pc instead, sacrificing compression.
     733             :         {
     734           0 :           uint16_t PCOffset = DebugLineData.getU16(OffsetPtr);
     735           0 :           State.Row.Address += PCOffset;
     736           0 :           if (OS)
     737             :             *OS
     738           0 :                 << format(" (0x%16.16" PRIx64 ")", PCOffset);
     739             :         }
     740             :         break;
     741             : 
     742        1732 :       case DW_LNS_set_prologue_end:
     743             :         // Takes no arguments. Set the prologue_end register of the
     744             :         // state machine to true
     745        1732 :         State.Row.PrologueEnd = true;
     746        1732 :         break;
     747             : 
     748           0 :       case DW_LNS_set_epilogue_begin:
     749             :         // Takes no arguments. Set the basic_block register of the
     750             :         // state machine to true
     751           0 :         State.Row.EpilogueBegin = true;
     752           0 :         break;
     753             : 
     754           0 :       case DW_LNS_set_isa:
     755             :         // Takes a single unsigned LEB128 operand and stores it in the
     756             :         // column register of the state machine.
     757           0 :         State.Row.Isa = DebugLineData.getULEB128(OffsetPtr);
     758           0 :         if (OS)
     759           0 :           *OS << " (" << State.Row.Isa << ")";
     760             :         break;
     761             : 
     762           0 :       default:
     763             :         // Handle any unknown standard opcodes here. We know the lengths
     764             :         // of such opcodes because they are specified in the prologue
     765             :         // as a multiple of LEB128 operands for each opcode.
     766             :         {
     767             :           assert(Opcode - 1U < Prologue.StandardOpcodeLengths.size());
     768           0 :           uint8_t OpcodeLength = Prologue.StandardOpcodeLengths[Opcode - 1];
     769           0 :           for (uint8_t I = 0; I < OpcodeLength; ++I) {
     770           0 :             uint64_t Value = DebugLineData.getULEB128(OffsetPtr);
     771           0 :             if (OS)
     772           0 :               *OS << format("Skipping ULEB128 value: 0x%16.16" PRIx64 ")\n",
     773           0 :                             Value);
     774           0 :           }
     775             :         }
     776             :         break;
     777             :       }
     778             :     } else {
     779             :       // Special Opcodes
     780             : 
     781             :       // A special opcode value is chosen based on the amount that needs
     782             :       // to be added to the line and address registers. The maximum line
     783             :       // increment for a special opcode is the value of the line_base
     784             :       // field in the header, plus the value of the line_range field,
     785             :       // minus 1 (line base + line range - 1). If the desired line
     786             :       // increment is greater than the maximum line increment, a standard
     787             :       // opcode must be used instead of a special opcode. The "address
     788             :       // advance" is calculated by dividing the desired address increment
     789             :       // by the minimum_instruction_length field from the header. The
     790             :       // special opcode is then calculated using the following formula:
     791             :       //
     792             :       //  opcode = (desired line increment - line_base) +
     793             :       //           (line_range * address advance) + opcode_base
     794             :       //
     795             :       // If the resulting opcode is greater than 255, a standard opcode
     796             :       // must be used instead.
     797             :       //
     798             :       // To decode a special opcode, subtract the opcode_base from the
     799             :       // opcode itself to give the adjusted opcode. The amount to
     800             :       // increment the address register is the result of the adjusted
     801             :       // opcode divided by the line_range multiplied by the
     802             :       // minimum_instruction_length field from the header. That is:
     803             :       //
     804             :       //  address increment = (adjusted opcode / line_range) *
     805             :       //                      minimum_instruction_length
     806             :       //
     807             :       // The amount to increment the line register is the line_base plus
     808             :       // the result of the adjusted opcode modulo the line_range. That is:
     809             :       //
     810             :       // line increment = line_base + (adjusted opcode % line_range)
     811             : 
     812        5671 :       uint8_t AdjustOpcode = Opcode - Prologue.OpcodeBase;
     813        5671 :       uint64_t AddrOffset =
     814        5671 :           (AdjustOpcode / Prologue.LineRange) * Prologue.MinInstLength;
     815        5671 :       int32_t LineOffset =
     816        5671 :           Prologue.LineBase + (AdjustOpcode % Prologue.LineRange);
     817        5671 :       State.Row.Line += LineOffset;
     818        5671 :       State.Row.Address += AddrOffset;
     819             : 
     820        5671 :       if (OS) {
     821         660 :         *OS << "address += " << ((uint32_t)AdjustOpcode)
     822        1320 :             << ",  line += " << LineOffset << "\n";
     823         660 :         OS->indent(12);
     824         660 :         State.Row.dump(*OS);
     825             :       }
     826             : 
     827        5671 :       State.appendRowToMatrix(*OffsetPtr);
     828             :       // Reset discriminator to 0.
     829        5671 :       State.Row.Discriminator = 0;
     830             :     }
     831       18717 :     if(OS)
     832        2028 :       *OS << "\n";
     833             :   }
     834             : 
     835        1351 :   if (!State.Sequence.Empty)
     836             :     RecoverableErrorCallback(
     837          12 :         createStringError(errc::illegal_byte_sequence,
     838             :                     "last sequence in debug line table is not terminated!"));
     839             : 
     840             :   // Sort all sequences so that address lookup will work faster.
     841        1351 :   if (!Sequences.empty()) {
     842             :     llvm::sort(Sequences.begin(), Sequences.end(), Sequence::orderByLowPC);
     843             :     // Note: actually, instruction address ranges of sequences should not
     844             :     // overlap (in shared objects and executables). If they do, the address
     845             :     // lookup would still work, though, but result would be ambiguous.
     846             :     // We don't report warning in this case. For example,
     847             :     // sometimes .so compiled from multiple object files contains a few
     848             :     // rudimentary sequences for address ranges [0x0, 0xsomething).
     849             :   }
     850             : 
     851             :   return Error::success();
     852             : }
     853             : 
     854             : uint32_t
     855         772 : DWARFDebugLine::LineTable::findRowInSeq(const DWARFDebugLine::Sequence &Seq,
     856             :                                         uint64_t Address) const {
     857         772 :   if (!Seq.containsPC(Address))
     858           2 :     return UnknownRowIndex;
     859             :   // Search for instruction address in the rows describing the sequence.
     860             :   // Rows are stored in a vector, so we may use arithmetical operations with
     861             :   // iterators.
     862         770 :   DWARFDebugLine::Row Row;
     863         770 :   Row.Address = Address;
     864         770 :   RowIter FirstRow = Rows.begin() + Seq.FirstRowIndex;
     865         770 :   RowIter LastRow = Rows.begin() + Seq.LastRowIndex;
     866             :   LineTable::RowIter RowPos = std::lower_bound(
     867         770 :       FirstRow, LastRow, Row, DWARFDebugLine::Row::orderByAddress);
     868         770 :   if (RowPos == LastRow) {
     869           0 :     return Seq.LastRowIndex - 1;
     870             :   }
     871         770 :   uint32_t Index = Seq.FirstRowIndex + (RowPos - FirstRow);
     872         770 :   if (RowPos->Address > Address) {
     873         560 :     if (RowPos == FirstRow)
     874           0 :       return UnknownRowIndex;
     875             :     else
     876         560 :       Index--;
     877             :   }
     878             :   return Index;
     879             : }
     880             : 
     881         743 : uint32_t DWARFDebugLine::LineTable::lookupAddress(uint64_t Address) const {
     882         743 :   if (Sequences.empty())
     883          18 :     return UnknownRowIndex;
     884             :   // First, find an instruction sequence containing the given address.
     885         725 :   DWARFDebugLine::Sequence Sequence;
     886         725 :   Sequence.LowPC = Address;
     887         725 :   SequenceIter FirstSeq = Sequences.begin();
     888         725 :   SequenceIter LastSeq = Sequences.end();
     889             :   SequenceIter SeqPos = std::lower_bound(
     890         725 :       FirstSeq, LastSeq, Sequence, DWARFDebugLine::Sequence::orderByLowPC);
     891         725 :   DWARFDebugLine::Sequence FoundSeq;
     892         725 :   if (SeqPos == LastSeq) {
     893         660 :     FoundSeq = Sequences.back();
     894          65 :   } else if (SeqPos->LowPC == Address) {
     895          45 :     FoundSeq = *SeqPos;
     896             :   } else {
     897          20 :     if (SeqPos == FirstSeq)
     898           5 :       return UnknownRowIndex;
     899          15 :     FoundSeq = *(SeqPos - 1);
     900             :   }
     901         720 :   return findRowInSeq(FoundSeq, Address);
     902             : }
     903             : 
     904          26 : bool DWARFDebugLine::LineTable::lookupAddressRange(
     905             :     uint64_t Address, uint64_t Size, std::vector<uint32_t> &Result) const {
     906          26 :   if (Sequences.empty())
     907             :     return false;
     908          26 :   uint64_t EndAddr = Address + Size;
     909             :   // First, find an instruction sequence containing the given address.
     910          26 :   DWARFDebugLine::Sequence Sequence;
     911          26 :   Sequence.LowPC = Address;
     912          26 :   SequenceIter FirstSeq = Sequences.begin();
     913          26 :   SequenceIter LastSeq = Sequences.end();
     914             :   SequenceIter SeqPos = std::lower_bound(
     915          26 :       FirstSeq, LastSeq, Sequence, DWARFDebugLine::Sequence::orderByLowPC);
     916          26 :   if (SeqPos == LastSeq || SeqPos->LowPC != Address) {
     917          14 :     if (SeqPos == FirstSeq)
     918             :       return false;
     919             :     SeqPos--;
     920             :   }
     921          26 :   if (!SeqPos->containsPC(Address))
     922             :     return false;
     923             : 
     924             :   SequenceIter StartPos = SeqPos;
     925             : 
     926             :   // Add the rows from the first sequence to the vector, starting with the
     927             :   // index we just calculated
     928             : 
     929          52 :   while (SeqPos != LastSeq && SeqPos->LowPC < EndAddr) {
     930             :     const DWARFDebugLine::Sequence &CurSeq = *SeqPos;
     931             :     // For the first sequence, we need to find which row in the sequence is the
     932             :     // first in our range.
     933          26 :     uint32_t FirstRowIndex = CurSeq.FirstRowIndex;
     934          26 :     if (SeqPos == StartPos)
     935          26 :       FirstRowIndex = findRowInSeq(CurSeq, Address);
     936             : 
     937             :     // Figure out the last row in the range.
     938          26 :     uint32_t LastRowIndex = findRowInSeq(CurSeq, EndAddr - 1);
     939          26 :     if (LastRowIndex == UnknownRowIndex)
     940           0 :       LastRowIndex = CurSeq.LastRowIndex - 1;
     941             : 
     942             :     assert(FirstRowIndex != UnknownRowIndex);
     943             :     assert(LastRowIndex != UnknownRowIndex);
     944             : 
     945         142 :     for (uint32_t I = FirstRowIndex; I <= LastRowIndex; ++I) {
     946         116 :       Result.push_back(I);
     947             :     }
     948             : 
     949             :     ++SeqPos;
     950             :   }
     951             : 
     952             :   return true;
     953             : }
     954             : 
     955        4421 : bool DWARFDebugLine::LineTable::hasFileAtIndex(uint64_t FileIndex) const {
     956        4421 :   return FileIndex != 0 && FileIndex <= Prologue.FileNames.size();
     957             : }
     958             : 
     959         718 : Optional<StringRef> DWARFDebugLine::LineTable::getSourceByIndex(uint64_t FileIndex,
     960             :                                                                 FileLineInfoKind Kind) const {
     961         718 :   if (Kind == FileLineInfoKind::None || !hasFileAtIndex(FileIndex))
     962             :     return None;
     963         718 :   const FileNameEntry &Entry = Prologue.FileNames[FileIndex - 1];
     964         718 :   if (Optional<const char *> source = Entry.Source.getAsCString())
     965          48 :     return StringRef(*source);
     966             :   return None;
     967             : }
     968             : 
     969        6648 : static bool isPathAbsoluteOnWindowsOrPosix(const Twine &Path) {
     970             :   // Debug info can contain paths from any OS, not necessarily
     971             :   // an OS we're currently running on. Moreover different compilation units can
     972             :   // be compiled on different operating systems and linked together later.
     973       13073 :   return sys::path::is_absolute(Path, sys::path::Style::posix) ||
     974        6425 :          sys::path::is_absolute(Path, sys::path::Style::windows);
     975             : }
     976             : 
     977        3603 : bool DWARFDebugLine::LineTable::getFileNameByIndex(uint64_t FileIndex,
     978             :                                                    const char *CompDir,
     979             :                                                    FileLineInfoKind Kind,
     980             :                                                    std::string &Result) const {
     981        3603 :   if (Kind == FileLineInfoKind::None || !hasFileAtIndex(FileIndex))
     982           0 :     return false;
     983        3603 :   const FileNameEntry &Entry = Prologue.FileNames[FileIndex - 1];
     984        3603 :   StringRef FileName = Entry.Name.getAsCString().getValue();
     985        7087 :   if (Kind != FileLineInfoKind::AbsoluteFilePath ||
     986        7087 :       isPathAbsoluteOnWindowsOrPosix(FileName)) {
     987         122 :     Result = FileName;
     988         122 :     return true;
     989             :   }
     990             : 
     991             :   SmallString<16> FilePath;
     992        3481 :   uint64_t IncludeDirIndex = Entry.DirIdx;
     993        3481 :   StringRef IncludeDir;
     994             :   // Be defensive about the contents of Entry.
     995        3481 :   if (IncludeDirIndex > 0 &&
     996         428 :       IncludeDirIndex <= Prologue.IncludeDirectories.size())
     997         427 :     IncludeDir = Prologue.IncludeDirectories[IncludeDirIndex - 1]
     998         854 :                      .getAsCString()
     999             :                      .getValue();
    1000             : 
    1001             :   // We may still need to append compilation directory of compile unit.
    1002             :   // We know that FileName is not absolute, the only way to have an
    1003             :   // absolute path at this point would be if IncludeDir is absolute.
    1004        6645 :   if (CompDir && Kind == FileLineInfoKind::AbsoluteFilePath &&
    1005        6645 :       !isPathAbsoluteOnWindowsOrPosix(IncludeDir))
    1006        2937 :     sys::path::append(FilePath, CompDir);
    1007             : 
    1008             :   // sys::path::append skips empty strings.
    1009        3481 :   sys::path::append(FilePath, IncludeDir, FileName);
    1010        6962 :   Result = FilePath.str();
    1011             :   return true;
    1012             : }
    1013             : 
    1014         743 : bool DWARFDebugLine::LineTable::getFileLineInfoForAddress(
    1015             :     uint64_t Address, const char *CompDir, FileLineInfoKind Kind,
    1016             :     DILineInfo &Result) const {
    1017             :   // Get the index of row we're looking for in the line table.
    1018         743 :   uint32_t RowIndex = lookupAddress(Address);
    1019         743 :   if (RowIndex == -1U)
    1020             :     return false;
    1021             :   // Take file number and line/column from the row.
    1022         718 :   const auto &Row = Rows[RowIndex];
    1023         718 :   if (!getFileNameByIndex(Row.File, CompDir, Kind, Result.FileName))
    1024             :     return false;
    1025         718 :   Result.Line = Row.Line;
    1026         718 :   Result.Column = Row.Column;
    1027         718 :   Result.Discriminator = Row.Discriminator;
    1028         718 :   Result.Source = getSourceByIndex(Row.File, Kind);
    1029         718 :   return true;
    1030             : }
    1031             : 
    1032             : // We want to supply the Unit associated with a .debug_line[.dwo] table when
    1033             : // we dump it, if possible, but still dump the table even if there isn't a Unit.
    1034             : // Therefore, collect up handles on all the Units that point into the
    1035             : // line-table section.
    1036             : static DWARFDebugLine::SectionParser::LineToUnitMap
    1037         248 : buildLineToUnitMap(DWARFDebugLine::SectionParser::cu_range CUs,
    1038             :                    DWARFDebugLine::SectionParser::tu_range TUs) {
    1039             :   DWARFDebugLine::SectionParser::LineToUnitMap LineToUnit;
    1040         557 :   for (const auto &CU : CUs)
    1041         309 :     if (auto CUDIE = CU->getUnitDIE())
    1042         616 :       if (auto StmtOffset = toSectionOffset(CUDIE.find(DW_AT_stmt_list)))
    1043         283 :         LineToUnit.insert(std::make_pair(*StmtOffset, &*CU));
    1044         296 :   for (const auto &TU : TUs)
    1045          48 :     if (auto TUDIE = TU->getUnitDIE())
    1046          96 :       if (auto StmtOffset = toSectionOffset(TUDIE.find(DW_AT_stmt_list)))
    1047          45 :         LineToUnit.insert(std::make_pair(*StmtOffset, &*TU));
    1048         248 :   return LineToUnit;
    1049             : }
    1050             : 
    1051         248 : DWARFDebugLine::SectionParser::SectionParser(DWARFDataExtractor &Data,
    1052             :                                              const DWARFContext &C,
    1053         248 :                                              cu_range CUs, tu_range TUs)
    1054         248 :     : DebugLineData(Data), Context(C) {
    1055         248 :   LineToUnit = buildLineToUnitMap(CUs, TUs);
    1056         496 :   if (!DebugLineData.isValidOffset(Offset))
    1057           3 :     Done = true;
    1058         248 : }
    1059             : 
    1060         384 : bool DWARFDebugLine::Prologue::totalLengthIsValid() const {
    1061         384 :   return TotalLength == 0xffffffff || TotalLength < 0xffffff00;
    1062             : }
    1063             : 
    1064         354 : DWARFDebugLine::LineTable DWARFDebugLine::SectionParser::parseNext(
    1065             :     function_ref<void(Error)> RecoverableErrorCallback,
    1066             :     function_ref<void(Error)> UnrecoverableErrorCallback, raw_ostream *OS) {
    1067             :   assert(DebugLineData.isValidOffset(Offset) &&
    1068             :          "parsing should have terminated");
    1069         354 :   DWARFUnit *U = prepareToParse(Offset);
    1070         354 :   uint32_t OldOffset = Offset;
    1071         354 :   LineTable LT;
    1072         354 :   if (Error Err = LT.parse(DebugLineData, &Offset, Context, U,
    1073         708 :                            RecoverableErrorCallback, OS))
    1074          46 :     UnrecoverableErrorCallback(std::move(Err));
    1075         354 :   moveToNextTable(OldOffset, LT.Prologue);
    1076         354 :   return LT;
    1077             : }
    1078             : 
    1079          30 : void DWARFDebugLine::SectionParser::skip(
    1080             :     function_ref<void(Error)> ErrorCallback) {
    1081             :   assert(DebugLineData.isValidOffset(Offset) &&
    1082             :          "parsing should have terminated");
    1083          30 :   DWARFUnit *U = prepareToParse(Offset);
    1084          30 :   uint32_t OldOffset = Offset;
    1085          60 :   LineTable LT;
    1086          60 :   if (Error Err = LT.Prologue.parse(DebugLineData, &Offset, Context, U))
    1087          30 :     ErrorCallback(std::move(Err));
    1088          30 :   moveToNextTable(OldOffset, LT.Prologue);
    1089          30 : }
    1090             : 
    1091         384 : DWARFUnit *DWARFDebugLine::SectionParser::prepareToParse(uint32_t Offset) {
    1092             :   DWARFUnit *U = nullptr;
    1093         384 :   auto It = LineToUnit.find(Offset);
    1094         384 :   if (It != LineToUnit.end())
    1095         293 :     U = It->second;
    1096         384 :   DebugLineData.setAddressSize(U ? U->getAddressByteSize() : 0);
    1097         384 :   return U;
    1098             : }
    1099             : 
    1100         384 : void DWARFDebugLine::SectionParser::moveToNextTable(uint32_t OldOffset,
    1101             :                                                     const Prologue &P) {
    1102             :   // If the length field is not valid, we don't know where the next table is, so
    1103             :   // cannot continue to parse. Mark the parser as done, and leave the Offset
    1104             :   // value as it currently is. This will be the end of the bad length field.
    1105         384 :   if (!P.totalLengthIsValid()) {
    1106           6 :     Done = true;
    1107           6 :     return;
    1108             :   }
    1109             : 
    1110         378 :   Offset = OldOffset + P.TotalLength + P.sizeofTotalLength();
    1111         756 :   if (!DebugLineData.isValidOffset(Offset)) {
    1112         239 :     Done = true;
    1113             :   }
    1114             : }

Generated by: LCOV version 1.13