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/Optional.h" 110b57cec5SDimitry Andric #include "llvm/ADT/SmallString.h" 120b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h" 130b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h" 140b57cec5SDimitry Andric #include "llvm/BinaryFormat/Dwarf.h" 150b57cec5SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFFormValue.h" 160b57cec5SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFRelocMap.h" 170b57cec5SDimitry Andric #include "llvm/Support/Errc.h" 180b57cec5SDimitry Andric #include "llvm/Support/Format.h" 190b57cec5SDimitry Andric #include "llvm/Support/WithColor.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 42*480093f4SDimitry Andric } // end anonymous namespace 430b57cec5SDimitry Andric 440b57cec5SDimitry Andric void DWARFDebugLine::ContentTypeTracker::trackContentType( 450b57cec5SDimitry Andric dwarf::LineNumberEntryFormat ContentType) { 460b57cec5SDimitry Andric switch (ContentType) { 470b57cec5SDimitry Andric case dwarf::DW_LNCT_timestamp: 480b57cec5SDimitry Andric HasModTime = true; 490b57cec5SDimitry Andric break; 500b57cec5SDimitry Andric case dwarf::DW_LNCT_size: 510b57cec5SDimitry Andric HasLength = true; 520b57cec5SDimitry Andric break; 530b57cec5SDimitry Andric case dwarf::DW_LNCT_MD5: 540b57cec5SDimitry Andric HasMD5 = true; 550b57cec5SDimitry Andric break; 560b57cec5SDimitry Andric case dwarf::DW_LNCT_LLVM_source: 570b57cec5SDimitry Andric HasSource = true; 580b57cec5SDimitry Andric break; 590b57cec5SDimitry Andric default: 600b57cec5SDimitry Andric // We only care about values we consider optional, and new values may be 610b57cec5SDimitry Andric // added in the vendor extension range, so we do not match exhaustively. 620b57cec5SDimitry Andric break; 630b57cec5SDimitry Andric } 640b57cec5SDimitry Andric } 650b57cec5SDimitry Andric 660b57cec5SDimitry Andric DWARFDebugLine::Prologue::Prologue() { clear(); } 670b57cec5SDimitry Andric 680b57cec5SDimitry Andric bool DWARFDebugLine::Prologue::hasFileAtIndex(uint64_t FileIndex) const { 690b57cec5SDimitry Andric uint16_t DwarfVersion = getVersion(); 700b57cec5SDimitry Andric assert(DwarfVersion != 0 && 710b57cec5SDimitry Andric "line table prologue has no dwarf version information"); 720b57cec5SDimitry Andric if (DwarfVersion >= 5) 730b57cec5SDimitry Andric return FileIndex < FileNames.size(); 740b57cec5SDimitry Andric return FileIndex != 0 && FileIndex <= FileNames.size(); 750b57cec5SDimitry Andric } 760b57cec5SDimitry Andric 770b57cec5SDimitry Andric const llvm::DWARFDebugLine::FileNameEntry & 780b57cec5SDimitry Andric DWARFDebugLine::Prologue::getFileNameEntry(uint64_t Index) const { 790b57cec5SDimitry Andric uint16_t DwarfVersion = getVersion(); 800b57cec5SDimitry Andric assert(DwarfVersion != 0 && 810b57cec5SDimitry Andric "line table prologue has no dwarf version information"); 820b57cec5SDimitry Andric // In DWARF v5 the file names are 0-indexed. 830b57cec5SDimitry Andric if (DwarfVersion >= 5) 840b57cec5SDimitry Andric return FileNames[Index]; 850b57cec5SDimitry Andric return FileNames[Index - 1]; 860b57cec5SDimitry Andric } 870b57cec5SDimitry Andric 880b57cec5SDimitry Andric void DWARFDebugLine::Prologue::clear() { 890b57cec5SDimitry Andric TotalLength = PrologueLength = 0; 900b57cec5SDimitry Andric SegSelectorSize = 0; 910b57cec5SDimitry Andric MinInstLength = MaxOpsPerInst = DefaultIsStmt = LineBase = LineRange = 0; 920b57cec5SDimitry Andric OpcodeBase = 0; 930b57cec5SDimitry Andric FormParams = dwarf::FormParams({0, 0, DWARF32}); 940b57cec5SDimitry Andric ContentTypes = ContentTypeTracker(); 950b57cec5SDimitry Andric StandardOpcodeLengths.clear(); 960b57cec5SDimitry Andric IncludeDirectories.clear(); 970b57cec5SDimitry Andric FileNames.clear(); 980b57cec5SDimitry Andric } 990b57cec5SDimitry Andric 1000b57cec5SDimitry Andric void DWARFDebugLine::Prologue::dump(raw_ostream &OS, 1010b57cec5SDimitry Andric DIDumpOptions DumpOptions) const { 1020b57cec5SDimitry Andric OS << "Line table prologue:\n" 1030b57cec5SDimitry Andric << format(" total_length: 0x%8.8" PRIx64 "\n", TotalLength) 1040b57cec5SDimitry Andric << format(" version: %u\n", getVersion()); 1050b57cec5SDimitry Andric if (getVersion() >= 5) 1060b57cec5SDimitry Andric OS << format(" address_size: %u\n", getAddressSize()) 1070b57cec5SDimitry Andric << format(" seg_select_size: %u\n", SegSelectorSize); 1080b57cec5SDimitry Andric OS << format(" prologue_length: 0x%8.8" PRIx64 "\n", PrologueLength) 1090b57cec5SDimitry Andric << format(" min_inst_length: %u\n", MinInstLength) 1100b57cec5SDimitry Andric << format(getVersion() >= 4 ? "max_ops_per_inst: %u\n" : "", MaxOpsPerInst) 1110b57cec5SDimitry Andric << format(" default_is_stmt: %u\n", DefaultIsStmt) 1120b57cec5SDimitry Andric << format(" line_base: %i\n", LineBase) 1130b57cec5SDimitry Andric << format(" line_range: %u\n", LineRange) 1140b57cec5SDimitry Andric << format(" opcode_base: %u\n", OpcodeBase); 1150b57cec5SDimitry Andric 1160b57cec5SDimitry Andric for (uint32_t I = 0; I != StandardOpcodeLengths.size(); ++I) 1170b57cec5SDimitry Andric OS << format("standard_opcode_lengths[%s] = %u\n", 1180b57cec5SDimitry Andric LNStandardString(I + 1).data(), StandardOpcodeLengths[I]); 1190b57cec5SDimitry Andric 1200b57cec5SDimitry Andric if (!IncludeDirectories.empty()) { 1210b57cec5SDimitry Andric // DWARF v5 starts directory indexes at 0. 1220b57cec5SDimitry Andric uint32_t DirBase = getVersion() >= 5 ? 0 : 1; 1230b57cec5SDimitry Andric for (uint32_t I = 0; I != IncludeDirectories.size(); ++I) { 1240b57cec5SDimitry Andric OS << format("include_directories[%3u] = ", I + DirBase); 1250b57cec5SDimitry Andric IncludeDirectories[I].dump(OS, DumpOptions); 1260b57cec5SDimitry Andric OS << '\n'; 1270b57cec5SDimitry Andric } 1280b57cec5SDimitry Andric } 1290b57cec5SDimitry Andric 1300b57cec5SDimitry Andric if (!FileNames.empty()) { 1310b57cec5SDimitry Andric // DWARF v5 starts file indexes at 0. 1320b57cec5SDimitry Andric uint32_t FileBase = getVersion() >= 5 ? 0 : 1; 1330b57cec5SDimitry Andric for (uint32_t I = 0; I != FileNames.size(); ++I) { 1340b57cec5SDimitry Andric const FileNameEntry &FileEntry = FileNames[I]; 1350b57cec5SDimitry Andric OS << format("file_names[%3u]:\n", I + FileBase); 1360b57cec5SDimitry Andric OS << " name: "; 1370b57cec5SDimitry Andric FileEntry.Name.dump(OS, DumpOptions); 1380b57cec5SDimitry Andric OS << '\n' 1390b57cec5SDimitry Andric << format(" dir_index: %" PRIu64 "\n", FileEntry.DirIdx); 1400b57cec5SDimitry Andric if (ContentTypes.HasMD5) 1410b57cec5SDimitry Andric OS << " md5_checksum: " << FileEntry.Checksum.digest() << '\n'; 1420b57cec5SDimitry Andric if (ContentTypes.HasModTime) 1430b57cec5SDimitry Andric OS << format(" mod_time: 0x%8.8" PRIx64 "\n", FileEntry.ModTime); 1440b57cec5SDimitry Andric if (ContentTypes.HasLength) 1450b57cec5SDimitry Andric OS << format(" length: 0x%8.8" PRIx64 "\n", FileEntry.Length); 1460b57cec5SDimitry Andric if (ContentTypes.HasSource) { 1470b57cec5SDimitry Andric OS << " source: "; 1480b57cec5SDimitry Andric FileEntry.Source.dump(OS, DumpOptions); 1490b57cec5SDimitry Andric OS << '\n'; 1500b57cec5SDimitry Andric } 1510b57cec5SDimitry Andric } 1520b57cec5SDimitry Andric } 1530b57cec5SDimitry Andric } 1540b57cec5SDimitry Andric 1550b57cec5SDimitry Andric // Parse v2-v4 directory and file tables. 1560b57cec5SDimitry Andric static void 1570b57cec5SDimitry Andric parseV2DirFileTables(const DWARFDataExtractor &DebugLineData, 1588bcb0991SDimitry Andric uint64_t *OffsetPtr, uint64_t EndPrologueOffset, 1590b57cec5SDimitry Andric DWARFDebugLine::ContentTypeTracker &ContentTypes, 1600b57cec5SDimitry Andric std::vector<DWARFFormValue> &IncludeDirectories, 1610b57cec5SDimitry Andric std::vector<DWARFDebugLine::FileNameEntry> &FileNames) { 1620b57cec5SDimitry Andric while (*OffsetPtr < EndPrologueOffset) { 1630b57cec5SDimitry Andric StringRef S = DebugLineData.getCStrRef(OffsetPtr); 1640b57cec5SDimitry Andric if (S.empty()) 1650b57cec5SDimitry Andric break; 1660b57cec5SDimitry Andric DWARFFormValue Dir = 1670b57cec5SDimitry Andric DWARFFormValue::createFromPValue(dwarf::DW_FORM_string, S.data()); 1680b57cec5SDimitry Andric IncludeDirectories.push_back(Dir); 1690b57cec5SDimitry Andric } 1700b57cec5SDimitry Andric 1710b57cec5SDimitry Andric while (*OffsetPtr < EndPrologueOffset) { 1720b57cec5SDimitry Andric StringRef Name = DebugLineData.getCStrRef(OffsetPtr); 1730b57cec5SDimitry Andric if (Name.empty()) 1740b57cec5SDimitry Andric break; 1750b57cec5SDimitry Andric DWARFDebugLine::FileNameEntry FileEntry; 1760b57cec5SDimitry Andric FileEntry.Name = 1770b57cec5SDimitry Andric DWARFFormValue::createFromPValue(dwarf::DW_FORM_string, Name.data()); 1780b57cec5SDimitry Andric FileEntry.DirIdx = DebugLineData.getULEB128(OffsetPtr); 1790b57cec5SDimitry Andric FileEntry.ModTime = DebugLineData.getULEB128(OffsetPtr); 1800b57cec5SDimitry Andric FileEntry.Length = DebugLineData.getULEB128(OffsetPtr); 1810b57cec5SDimitry Andric FileNames.push_back(FileEntry); 1820b57cec5SDimitry Andric } 1830b57cec5SDimitry Andric 1840b57cec5SDimitry Andric ContentTypes.HasModTime = true; 1850b57cec5SDimitry Andric ContentTypes.HasLength = true; 1860b57cec5SDimitry Andric } 1870b57cec5SDimitry Andric 1880b57cec5SDimitry Andric // Parse v5 directory/file entry content descriptions. 1898bcb0991SDimitry Andric // Returns the descriptors, or an error if we did not find a path or ran off 1908bcb0991SDimitry Andric // the end of the prologue. 1918bcb0991SDimitry Andric static llvm::Expected<ContentDescriptors> 1928bcb0991SDimitry Andric parseV5EntryFormat(const DWARFDataExtractor &DebugLineData, uint64_t *OffsetPtr, 1938bcb0991SDimitry Andric DWARFDebugLine::ContentTypeTracker *ContentTypes) { 1940b57cec5SDimitry Andric ContentDescriptors Descriptors; 1950b57cec5SDimitry Andric int FormatCount = DebugLineData.getU8(OffsetPtr); 1960b57cec5SDimitry Andric bool HasPath = false; 1970b57cec5SDimitry Andric for (int I = 0; I != FormatCount; ++I) { 1980b57cec5SDimitry Andric ContentDescriptor Descriptor; 1990b57cec5SDimitry Andric Descriptor.Type = 2000b57cec5SDimitry Andric dwarf::LineNumberEntryFormat(DebugLineData.getULEB128(OffsetPtr)); 2010b57cec5SDimitry Andric Descriptor.Form = dwarf::Form(DebugLineData.getULEB128(OffsetPtr)); 2020b57cec5SDimitry Andric if (Descriptor.Type == dwarf::DW_LNCT_path) 2030b57cec5SDimitry Andric HasPath = true; 2040b57cec5SDimitry Andric if (ContentTypes) 2050b57cec5SDimitry Andric ContentTypes->trackContentType(Descriptor.Type); 2060b57cec5SDimitry Andric Descriptors.push_back(Descriptor); 2070b57cec5SDimitry Andric } 2088bcb0991SDimitry Andric 2098bcb0991SDimitry Andric if (!HasPath) 2108bcb0991SDimitry Andric return createStringError(errc::invalid_argument, 2118bcb0991SDimitry Andric "failed to parse entry content descriptions" 2128bcb0991SDimitry Andric " because no path was found"); 2138bcb0991SDimitry Andric return Descriptors; 2140b57cec5SDimitry Andric } 2150b57cec5SDimitry Andric 2168bcb0991SDimitry Andric static Error 2170b57cec5SDimitry Andric parseV5DirFileTables(const DWARFDataExtractor &DebugLineData, 218*480093f4SDimitry Andric uint64_t *OffsetPtr, const dwarf::FormParams &FormParams, 2190b57cec5SDimitry Andric const DWARFContext &Ctx, const DWARFUnit *U, 2200b57cec5SDimitry Andric DWARFDebugLine::ContentTypeTracker &ContentTypes, 2210b57cec5SDimitry Andric std::vector<DWARFFormValue> &IncludeDirectories, 2220b57cec5SDimitry Andric std::vector<DWARFDebugLine::FileNameEntry> &FileNames) { 2230b57cec5SDimitry Andric // Get the directory entry description. 2248bcb0991SDimitry Andric llvm::Expected<ContentDescriptors> DirDescriptors = 225*480093f4SDimitry Andric parseV5EntryFormat(DebugLineData, OffsetPtr, nullptr); 2268bcb0991SDimitry Andric if (!DirDescriptors) 2278bcb0991SDimitry Andric return DirDescriptors.takeError(); 2280b57cec5SDimitry Andric 2290b57cec5SDimitry Andric // Get the directory entries, according to the format described above. 2300b57cec5SDimitry Andric int DirEntryCount = DebugLineData.getU8(OffsetPtr); 2310b57cec5SDimitry Andric for (int I = 0; I != DirEntryCount; ++I) { 2328bcb0991SDimitry Andric for (auto Descriptor : *DirDescriptors) { 2330b57cec5SDimitry Andric DWARFFormValue Value(Descriptor.Form); 2340b57cec5SDimitry Andric switch (Descriptor.Type) { 2350b57cec5SDimitry Andric case DW_LNCT_path: 2360b57cec5SDimitry Andric if (!Value.extractValue(DebugLineData, OffsetPtr, FormParams, &Ctx, U)) 2378bcb0991SDimitry Andric return createStringError(errc::invalid_argument, 2388bcb0991SDimitry Andric "failed to parse directory entry because " 2398bcb0991SDimitry Andric "extracting the form value failed."); 2400b57cec5SDimitry Andric IncludeDirectories.push_back(Value); 2410b57cec5SDimitry Andric break; 2420b57cec5SDimitry Andric default: 2430b57cec5SDimitry Andric if (!Value.skipValue(DebugLineData, OffsetPtr, FormParams)) 2448bcb0991SDimitry Andric return createStringError(errc::invalid_argument, 2458bcb0991SDimitry Andric "failed to parse directory entry because " 2468bcb0991SDimitry Andric "skipping the form value failed."); 2470b57cec5SDimitry Andric } 2480b57cec5SDimitry Andric } 2490b57cec5SDimitry Andric } 2500b57cec5SDimitry Andric 2510b57cec5SDimitry Andric // Get the file entry description. 252*480093f4SDimitry Andric llvm::Expected<ContentDescriptors> FileDescriptors = 253*480093f4SDimitry Andric parseV5EntryFormat(DebugLineData, OffsetPtr, &ContentTypes); 2548bcb0991SDimitry Andric if (!FileDescriptors) 2558bcb0991SDimitry Andric return FileDescriptors.takeError(); 2560b57cec5SDimitry Andric 2570b57cec5SDimitry Andric // Get the file entries, according to the format described above. 2580b57cec5SDimitry Andric int FileEntryCount = DebugLineData.getU8(OffsetPtr); 2590b57cec5SDimitry Andric for (int I = 0; I != FileEntryCount; ++I) { 2600b57cec5SDimitry Andric DWARFDebugLine::FileNameEntry FileEntry; 2618bcb0991SDimitry Andric for (auto Descriptor : *FileDescriptors) { 2620b57cec5SDimitry Andric DWARFFormValue Value(Descriptor.Form); 2630b57cec5SDimitry Andric if (!Value.extractValue(DebugLineData, OffsetPtr, FormParams, &Ctx, U)) 2648bcb0991SDimitry Andric return createStringError(errc::invalid_argument, 2658bcb0991SDimitry Andric "failed to parse file entry because " 2668bcb0991SDimitry Andric "extracting the form value failed."); 2670b57cec5SDimitry Andric switch (Descriptor.Type) { 2680b57cec5SDimitry Andric case DW_LNCT_path: 2690b57cec5SDimitry Andric FileEntry.Name = Value; 2700b57cec5SDimitry Andric break; 2710b57cec5SDimitry Andric case DW_LNCT_LLVM_source: 2720b57cec5SDimitry Andric FileEntry.Source = Value; 2730b57cec5SDimitry Andric break; 2740b57cec5SDimitry Andric case DW_LNCT_directory_index: 2750b57cec5SDimitry Andric FileEntry.DirIdx = Value.getAsUnsignedConstant().getValue(); 2760b57cec5SDimitry Andric break; 2770b57cec5SDimitry Andric case DW_LNCT_timestamp: 2780b57cec5SDimitry Andric FileEntry.ModTime = Value.getAsUnsignedConstant().getValue(); 2790b57cec5SDimitry Andric break; 2800b57cec5SDimitry Andric case DW_LNCT_size: 2810b57cec5SDimitry Andric FileEntry.Length = Value.getAsUnsignedConstant().getValue(); 2820b57cec5SDimitry Andric break; 2830b57cec5SDimitry Andric case DW_LNCT_MD5: 2848bcb0991SDimitry Andric if (!Value.getAsBlock() || Value.getAsBlock().getValue().size() != 16) 2858bcb0991SDimitry Andric return createStringError( 2868bcb0991SDimitry Andric errc::invalid_argument, 2878bcb0991SDimitry Andric "failed to parse file entry because the MD5 hash is invalid"); 2880b57cec5SDimitry Andric std::uninitialized_copy_n(Value.getAsBlock().getValue().begin(), 16, 2890b57cec5SDimitry Andric FileEntry.Checksum.Bytes.begin()); 2900b57cec5SDimitry Andric break; 2910b57cec5SDimitry Andric default: 2920b57cec5SDimitry Andric break; 2930b57cec5SDimitry Andric } 2940b57cec5SDimitry Andric } 2950b57cec5SDimitry Andric FileNames.push_back(FileEntry); 2960b57cec5SDimitry Andric } 2978bcb0991SDimitry Andric return Error::success(); 2980b57cec5SDimitry Andric } 2990b57cec5SDimitry Andric 3000b57cec5SDimitry Andric Error DWARFDebugLine::Prologue::parse(const DWARFDataExtractor &DebugLineData, 3018bcb0991SDimitry Andric uint64_t *OffsetPtr, 3020b57cec5SDimitry Andric const DWARFContext &Ctx, 3030b57cec5SDimitry Andric const DWARFUnit *U) { 3040b57cec5SDimitry Andric const uint64_t PrologueOffset = *OffsetPtr; 3050b57cec5SDimitry Andric 3060b57cec5SDimitry Andric clear(); 3070b57cec5SDimitry Andric TotalLength = DebugLineData.getRelocatedValue(4, OffsetPtr); 3088bcb0991SDimitry Andric if (TotalLength == dwarf::DW_LENGTH_DWARF64) { 3090b57cec5SDimitry Andric FormParams.Format = dwarf::DWARF64; 3100b57cec5SDimitry Andric TotalLength = DebugLineData.getU64(OffsetPtr); 3118bcb0991SDimitry Andric } else if (TotalLength >= dwarf::DW_LENGTH_lo_reserved) { 3120b57cec5SDimitry Andric return createStringError(errc::invalid_argument, 3130b57cec5SDimitry Andric "parsing line table prologue at offset 0x%8.8" PRIx64 3140b57cec5SDimitry Andric " unsupported reserved unit length found of value 0x%8.8" PRIx64, 3150b57cec5SDimitry Andric PrologueOffset, TotalLength); 3160b57cec5SDimitry Andric } 3170b57cec5SDimitry Andric FormParams.Version = DebugLineData.getU16(OffsetPtr); 3180b57cec5SDimitry Andric if (getVersion() < 2) 3190b57cec5SDimitry Andric return createStringError(errc::not_supported, 3200b57cec5SDimitry Andric "parsing line table prologue at offset 0x%8.8" PRIx64 3210b57cec5SDimitry Andric " found unsupported version 0x%2.2" PRIx16, 3220b57cec5SDimitry Andric PrologueOffset, getVersion()); 3230b57cec5SDimitry Andric 3240b57cec5SDimitry Andric if (getVersion() >= 5) { 3250b57cec5SDimitry Andric FormParams.AddrSize = DebugLineData.getU8(OffsetPtr); 3260b57cec5SDimitry Andric assert((DebugLineData.getAddressSize() == 0 || 3270b57cec5SDimitry Andric DebugLineData.getAddressSize() == getAddressSize()) && 3280b57cec5SDimitry Andric "Line table header and data extractor disagree"); 3290b57cec5SDimitry Andric SegSelectorSize = DebugLineData.getU8(OffsetPtr); 3300b57cec5SDimitry Andric } 3310b57cec5SDimitry Andric 3320b57cec5SDimitry Andric PrologueLength = 3330b57cec5SDimitry Andric DebugLineData.getRelocatedValue(sizeofPrologueLength(), OffsetPtr); 3340b57cec5SDimitry Andric const uint64_t EndPrologueOffset = PrologueLength + *OffsetPtr; 3350b57cec5SDimitry Andric MinInstLength = DebugLineData.getU8(OffsetPtr); 3360b57cec5SDimitry Andric if (getVersion() >= 4) 3370b57cec5SDimitry Andric MaxOpsPerInst = DebugLineData.getU8(OffsetPtr); 3380b57cec5SDimitry Andric DefaultIsStmt = DebugLineData.getU8(OffsetPtr); 3390b57cec5SDimitry Andric LineBase = DebugLineData.getU8(OffsetPtr); 3400b57cec5SDimitry Andric LineRange = DebugLineData.getU8(OffsetPtr); 3410b57cec5SDimitry Andric OpcodeBase = DebugLineData.getU8(OffsetPtr); 3420b57cec5SDimitry Andric 3430b57cec5SDimitry Andric StandardOpcodeLengths.reserve(OpcodeBase - 1); 3440b57cec5SDimitry Andric for (uint32_t I = 1; I < OpcodeBase; ++I) { 3450b57cec5SDimitry Andric uint8_t OpLen = DebugLineData.getU8(OffsetPtr); 3460b57cec5SDimitry Andric StandardOpcodeLengths.push_back(OpLen); 3470b57cec5SDimitry Andric } 3480b57cec5SDimitry Andric 3490b57cec5SDimitry Andric if (getVersion() >= 5) { 350*480093f4SDimitry Andric if (Error E = 351*480093f4SDimitry Andric parseV5DirFileTables(DebugLineData, OffsetPtr, FormParams, Ctx, U, 3528bcb0991SDimitry Andric ContentTypes, IncludeDirectories, FileNames)) { 3538bcb0991SDimitry Andric return joinErrors( 3548bcb0991SDimitry Andric createStringError( 3558bcb0991SDimitry Andric errc::invalid_argument, 3560b57cec5SDimitry Andric "parsing line table prologue at 0x%8.8" PRIx64 3570b57cec5SDimitry Andric " found an invalid directory or file table description at" 3580b57cec5SDimitry Andric " 0x%8.8" PRIx64, 3598bcb0991SDimitry Andric PrologueOffset, *OffsetPtr), 360*480093f4SDimitry Andric std::move(E)); 3610b57cec5SDimitry Andric } 3620b57cec5SDimitry Andric } else 3630b57cec5SDimitry Andric parseV2DirFileTables(DebugLineData, OffsetPtr, EndPrologueOffset, 3640b57cec5SDimitry Andric ContentTypes, IncludeDirectories, FileNames); 3650b57cec5SDimitry Andric 3660b57cec5SDimitry Andric if (*OffsetPtr != EndPrologueOffset) 3670b57cec5SDimitry Andric return createStringError(errc::invalid_argument, 3680b57cec5SDimitry Andric "parsing line table prologue at 0x%8.8" PRIx64 3690b57cec5SDimitry Andric " should have ended at 0x%8.8" PRIx64 3700b57cec5SDimitry Andric " but it ended at 0x%8.8" PRIx64, 3718bcb0991SDimitry Andric PrologueOffset, EndPrologueOffset, *OffsetPtr); 3720b57cec5SDimitry Andric return Error::success(); 3730b57cec5SDimitry Andric } 3740b57cec5SDimitry Andric 3750b57cec5SDimitry Andric DWARFDebugLine::Row::Row(bool DefaultIsStmt) { reset(DefaultIsStmt); } 3760b57cec5SDimitry Andric 3770b57cec5SDimitry Andric void DWARFDebugLine::Row::postAppend() { 3780b57cec5SDimitry Andric Discriminator = 0; 3790b57cec5SDimitry Andric BasicBlock = false; 3800b57cec5SDimitry Andric PrologueEnd = false; 3810b57cec5SDimitry Andric EpilogueBegin = false; 3820b57cec5SDimitry Andric } 3830b57cec5SDimitry Andric 3840b57cec5SDimitry Andric void DWARFDebugLine::Row::reset(bool DefaultIsStmt) { 3850b57cec5SDimitry Andric Address.Address = 0; 3860b57cec5SDimitry Andric Address.SectionIndex = object::SectionedAddress::UndefSection; 3870b57cec5SDimitry Andric Line = 1; 3880b57cec5SDimitry Andric Column = 0; 3890b57cec5SDimitry Andric File = 1; 3900b57cec5SDimitry Andric Isa = 0; 3910b57cec5SDimitry Andric Discriminator = 0; 3920b57cec5SDimitry Andric IsStmt = DefaultIsStmt; 3930b57cec5SDimitry Andric BasicBlock = false; 3940b57cec5SDimitry Andric EndSequence = false; 3950b57cec5SDimitry Andric PrologueEnd = false; 3960b57cec5SDimitry Andric EpilogueBegin = false; 3970b57cec5SDimitry Andric } 3980b57cec5SDimitry Andric 3990b57cec5SDimitry Andric void DWARFDebugLine::Row::dumpTableHeader(raw_ostream &OS) { 4000b57cec5SDimitry Andric OS << "Address Line Column File ISA Discriminator Flags\n" 4010b57cec5SDimitry Andric << "------------------ ------ ------ ------ --- ------------- " 4020b57cec5SDimitry Andric "-------------\n"; 4030b57cec5SDimitry Andric } 4040b57cec5SDimitry Andric 4050b57cec5SDimitry Andric void DWARFDebugLine::Row::dump(raw_ostream &OS) const { 4060b57cec5SDimitry Andric OS << format("0x%16.16" PRIx64 " %6u %6u", Address.Address, Line, Column) 4070b57cec5SDimitry Andric << format(" %6u %3u %13u ", File, Isa, Discriminator) 4080b57cec5SDimitry Andric << (IsStmt ? " is_stmt" : "") << (BasicBlock ? " basic_block" : "") 4090b57cec5SDimitry Andric << (PrologueEnd ? " prologue_end" : "") 4100b57cec5SDimitry Andric << (EpilogueBegin ? " epilogue_begin" : "") 4110b57cec5SDimitry Andric << (EndSequence ? " end_sequence" : "") << '\n'; 4120b57cec5SDimitry Andric } 4130b57cec5SDimitry Andric 4140b57cec5SDimitry Andric DWARFDebugLine::Sequence::Sequence() { reset(); } 4150b57cec5SDimitry Andric 4160b57cec5SDimitry Andric void DWARFDebugLine::Sequence::reset() { 4170b57cec5SDimitry Andric LowPC = 0; 4180b57cec5SDimitry Andric HighPC = 0; 4190b57cec5SDimitry Andric SectionIndex = object::SectionedAddress::UndefSection; 4200b57cec5SDimitry Andric FirstRowIndex = 0; 4210b57cec5SDimitry Andric LastRowIndex = 0; 4220b57cec5SDimitry Andric Empty = true; 4230b57cec5SDimitry Andric } 4240b57cec5SDimitry Andric 4250b57cec5SDimitry Andric DWARFDebugLine::LineTable::LineTable() { clear(); } 4260b57cec5SDimitry Andric 4270b57cec5SDimitry Andric void DWARFDebugLine::LineTable::dump(raw_ostream &OS, 4280b57cec5SDimitry Andric DIDumpOptions DumpOptions) const { 4290b57cec5SDimitry Andric Prologue.dump(OS, DumpOptions); 4300b57cec5SDimitry Andric 4310b57cec5SDimitry Andric if (!Rows.empty()) { 432*480093f4SDimitry Andric OS << '\n'; 4330b57cec5SDimitry Andric Row::dumpTableHeader(OS); 4340b57cec5SDimitry Andric for (const Row &R : Rows) { 4350b57cec5SDimitry Andric R.dump(OS); 4360b57cec5SDimitry Andric } 4370b57cec5SDimitry Andric } 438*480093f4SDimitry Andric 439*480093f4SDimitry Andric // Terminate the table with a final blank line to clearly delineate it from 440*480093f4SDimitry Andric // later dumps. 441*480093f4SDimitry Andric OS << '\n'; 4420b57cec5SDimitry Andric } 4430b57cec5SDimitry Andric 4440b57cec5SDimitry Andric void DWARFDebugLine::LineTable::clear() { 4450b57cec5SDimitry Andric Prologue.clear(); 4460b57cec5SDimitry Andric Rows.clear(); 4470b57cec5SDimitry Andric Sequences.clear(); 4480b57cec5SDimitry Andric } 4490b57cec5SDimitry Andric 4500b57cec5SDimitry Andric DWARFDebugLine::ParsingState::ParsingState(struct LineTable *LT) 4510b57cec5SDimitry Andric : LineTable(LT) { 4520b57cec5SDimitry Andric resetRowAndSequence(); 4530b57cec5SDimitry Andric } 4540b57cec5SDimitry Andric 4550b57cec5SDimitry Andric void DWARFDebugLine::ParsingState::resetRowAndSequence() { 4560b57cec5SDimitry Andric Row.reset(LineTable->Prologue.DefaultIsStmt); 4570b57cec5SDimitry Andric Sequence.reset(); 4580b57cec5SDimitry Andric } 4590b57cec5SDimitry Andric 4600b57cec5SDimitry Andric void DWARFDebugLine::ParsingState::appendRowToMatrix() { 4610b57cec5SDimitry Andric unsigned RowNumber = LineTable->Rows.size(); 4620b57cec5SDimitry Andric if (Sequence.Empty) { 4630b57cec5SDimitry Andric // Record the beginning of instruction sequence. 4640b57cec5SDimitry Andric Sequence.Empty = false; 4650b57cec5SDimitry Andric Sequence.LowPC = Row.Address.Address; 4660b57cec5SDimitry Andric Sequence.FirstRowIndex = RowNumber; 4670b57cec5SDimitry Andric } 4680b57cec5SDimitry Andric LineTable->appendRow(Row); 4690b57cec5SDimitry Andric if (Row.EndSequence) { 4700b57cec5SDimitry Andric // Record the end of instruction sequence. 4710b57cec5SDimitry Andric Sequence.HighPC = Row.Address.Address; 4720b57cec5SDimitry Andric Sequence.LastRowIndex = RowNumber + 1; 4730b57cec5SDimitry Andric Sequence.SectionIndex = Row.Address.SectionIndex; 4740b57cec5SDimitry Andric if (Sequence.isValid()) 4750b57cec5SDimitry Andric LineTable->appendSequence(Sequence); 4760b57cec5SDimitry Andric Sequence.reset(); 4770b57cec5SDimitry Andric } 4780b57cec5SDimitry Andric Row.postAppend(); 4790b57cec5SDimitry Andric } 4800b57cec5SDimitry Andric 4810b57cec5SDimitry Andric const DWARFDebugLine::LineTable * 4828bcb0991SDimitry Andric DWARFDebugLine::getLineTable(uint64_t Offset) const { 4830b57cec5SDimitry Andric LineTableConstIter Pos = LineTableMap.find(Offset); 4840b57cec5SDimitry Andric if (Pos != LineTableMap.end()) 4850b57cec5SDimitry Andric return &Pos->second; 4860b57cec5SDimitry Andric return nullptr; 4870b57cec5SDimitry Andric } 4880b57cec5SDimitry Andric 4890b57cec5SDimitry Andric Expected<const DWARFDebugLine::LineTable *> DWARFDebugLine::getOrParseLineTable( 4908bcb0991SDimitry Andric DWARFDataExtractor &DebugLineData, uint64_t Offset, const DWARFContext &Ctx, 491*480093f4SDimitry Andric const DWARFUnit *U, function_ref<void(Error)> RecoverableErrorCallback) { 4920b57cec5SDimitry Andric if (!DebugLineData.isValidOffset(Offset)) 4938bcb0991SDimitry Andric return createStringError(errc::invalid_argument, "offset 0x%8.8" PRIx64 4940b57cec5SDimitry Andric " is not a valid debug line section offset", 4950b57cec5SDimitry Andric Offset); 4960b57cec5SDimitry Andric 4970b57cec5SDimitry Andric std::pair<LineTableIter, bool> Pos = 4980b57cec5SDimitry Andric LineTableMap.insert(LineTableMapTy::value_type(Offset, LineTable())); 4990b57cec5SDimitry Andric LineTable *LT = &Pos.first->second; 5000b57cec5SDimitry Andric if (Pos.second) { 5010b57cec5SDimitry Andric if (Error Err = 5020b57cec5SDimitry Andric LT->parse(DebugLineData, &Offset, Ctx, U, RecoverableErrorCallback)) 5030b57cec5SDimitry Andric return std::move(Err); 5040b57cec5SDimitry Andric return LT; 5050b57cec5SDimitry Andric } 5060b57cec5SDimitry Andric return LT; 5070b57cec5SDimitry Andric } 5080b57cec5SDimitry Andric 5090b57cec5SDimitry Andric Error DWARFDebugLine::LineTable::parse( 5108bcb0991SDimitry Andric DWARFDataExtractor &DebugLineData, uint64_t *OffsetPtr, 5110b57cec5SDimitry Andric const DWARFContext &Ctx, const DWARFUnit *U, 512*480093f4SDimitry Andric function_ref<void(Error)> RecoverableErrorCallback, raw_ostream *OS) { 5138bcb0991SDimitry Andric const uint64_t DebugLineOffset = *OffsetPtr; 5140b57cec5SDimitry Andric 5150b57cec5SDimitry Andric clear(); 5160b57cec5SDimitry Andric 5170b57cec5SDimitry Andric Error PrologueErr = Prologue.parse(DebugLineData, OffsetPtr, Ctx, U); 5180b57cec5SDimitry Andric 5190b57cec5SDimitry Andric if (OS) { 5200b57cec5SDimitry Andric // The presence of OS signals verbose dumping. 5210b57cec5SDimitry Andric DIDumpOptions DumpOptions; 5220b57cec5SDimitry Andric DumpOptions.Verbose = true; 5230b57cec5SDimitry Andric Prologue.dump(*OS, DumpOptions); 5240b57cec5SDimitry Andric } 5250b57cec5SDimitry Andric 5260b57cec5SDimitry Andric if (PrologueErr) 5270b57cec5SDimitry Andric return PrologueErr; 5280b57cec5SDimitry Andric 529*480093f4SDimitry Andric uint64_t ProgramLength = Prologue.TotalLength + Prologue.sizeofTotalLength(); 530*480093f4SDimitry Andric if (!DebugLineData.isValidOffsetForDataOfSize(DebugLineOffset, 531*480093f4SDimitry Andric ProgramLength)) { 532*480093f4SDimitry Andric assert(DebugLineData.size() > DebugLineOffset && 533*480093f4SDimitry Andric "prologue parsing should handle invalid offset"); 534*480093f4SDimitry Andric uint64_t BytesRemaining = DebugLineData.size() - DebugLineOffset; 535*480093f4SDimitry Andric RecoverableErrorCallback( 536*480093f4SDimitry Andric createStringError(errc::invalid_argument, 537*480093f4SDimitry Andric "line table program with offset 0x%8.8" PRIx64 538*480093f4SDimitry Andric " has length 0x%8.8" PRIx64 " but only 0x%8.8" PRIx64 539*480093f4SDimitry Andric " bytes are available", 540*480093f4SDimitry Andric DebugLineOffset, ProgramLength, BytesRemaining)); 541*480093f4SDimitry Andric // Continue by capping the length at the number of remaining bytes. 542*480093f4SDimitry Andric ProgramLength = BytesRemaining; 543*480093f4SDimitry Andric } 544*480093f4SDimitry Andric 545*480093f4SDimitry Andric const uint64_t EndOffset = DebugLineOffset + ProgramLength; 5460b57cec5SDimitry Andric 5470b57cec5SDimitry Andric // See if we should tell the data extractor the address size. 5480b57cec5SDimitry Andric if (DebugLineData.getAddressSize() == 0) 5490b57cec5SDimitry Andric DebugLineData.setAddressSize(Prologue.getAddressSize()); 5500b57cec5SDimitry Andric else 5510b57cec5SDimitry Andric assert(Prologue.getAddressSize() == 0 || 5520b57cec5SDimitry Andric Prologue.getAddressSize() == DebugLineData.getAddressSize()); 5530b57cec5SDimitry Andric 5540b57cec5SDimitry Andric ParsingState State(this); 5550b57cec5SDimitry Andric 5560b57cec5SDimitry Andric while (*OffsetPtr < EndOffset) { 5570b57cec5SDimitry Andric if (OS) 5588bcb0991SDimitry Andric *OS << format("0x%08.08" PRIx64 ": ", *OffsetPtr); 5590b57cec5SDimitry Andric 5600b57cec5SDimitry Andric uint8_t Opcode = DebugLineData.getU8(OffsetPtr); 5610b57cec5SDimitry Andric 5620b57cec5SDimitry Andric if (OS) 5630b57cec5SDimitry Andric *OS << format("%02.02" PRIx8 " ", Opcode); 5640b57cec5SDimitry Andric 5650b57cec5SDimitry Andric if (Opcode == 0) { 5660b57cec5SDimitry Andric // Extended Opcodes always start with a zero opcode followed by 5670b57cec5SDimitry Andric // a uleb128 length so you can skip ones you don't know about 5680b57cec5SDimitry Andric uint64_t Len = DebugLineData.getULEB128(OffsetPtr); 5698bcb0991SDimitry Andric uint64_t ExtOffset = *OffsetPtr; 5700b57cec5SDimitry Andric 5710b57cec5SDimitry Andric // Tolerate zero-length; assume length is correct and soldier on. 5720b57cec5SDimitry Andric if (Len == 0) { 5730b57cec5SDimitry Andric if (OS) 5740b57cec5SDimitry Andric *OS << "Badly formed extended line op (length 0)\n"; 5750b57cec5SDimitry Andric continue; 5760b57cec5SDimitry Andric } 5770b57cec5SDimitry Andric 5780b57cec5SDimitry Andric uint8_t SubOpcode = DebugLineData.getU8(OffsetPtr); 5790b57cec5SDimitry Andric if (OS) 5800b57cec5SDimitry Andric *OS << LNExtendedString(SubOpcode); 5810b57cec5SDimitry Andric switch (SubOpcode) { 5820b57cec5SDimitry Andric case DW_LNE_end_sequence: 5830b57cec5SDimitry Andric // Set the end_sequence register of the state machine to true and 5840b57cec5SDimitry Andric // append a row to the matrix using the current values of the 5850b57cec5SDimitry Andric // state-machine registers. Then reset the registers to the initial 5860b57cec5SDimitry Andric // values specified above. Every statement program sequence must end 5870b57cec5SDimitry Andric // with a DW_LNE_end_sequence instruction which creates a row whose 5880b57cec5SDimitry Andric // address is that of the byte after the last target machine instruction 5890b57cec5SDimitry Andric // of the sequence. 5900b57cec5SDimitry Andric State.Row.EndSequence = true; 5910b57cec5SDimitry Andric if (OS) { 5920b57cec5SDimitry Andric *OS << "\n"; 5930b57cec5SDimitry Andric OS->indent(12); 5940b57cec5SDimitry Andric State.Row.dump(*OS); 5950b57cec5SDimitry Andric } 596*480093f4SDimitry Andric State.appendRowToMatrix(); 5970b57cec5SDimitry Andric State.resetRowAndSequence(); 5980b57cec5SDimitry Andric break; 5990b57cec5SDimitry Andric 6000b57cec5SDimitry Andric case DW_LNE_set_address: 6010b57cec5SDimitry Andric // Takes a single relocatable address as an operand. The size of the 6020b57cec5SDimitry Andric // operand is the size appropriate to hold an address on the target 6030b57cec5SDimitry Andric // machine. Set the address register to the value given by the 6040b57cec5SDimitry Andric // relocatable address. All of the other statement program opcodes 6050b57cec5SDimitry Andric // that affect the address register add a delta to it. This instruction 6060b57cec5SDimitry Andric // stores a relocatable value into it instead. 6070b57cec5SDimitry Andric // 6080b57cec5SDimitry Andric // Make sure the extractor knows the address size. If not, infer it 6090b57cec5SDimitry Andric // from the size of the operand. 610*480093f4SDimitry Andric { 611*480093f4SDimitry Andric uint8_t ExtractorAddressSize = DebugLineData.getAddressSize(); 612*480093f4SDimitry Andric if (ExtractorAddressSize != Len - 1 && ExtractorAddressSize != 0) 613*480093f4SDimitry Andric RecoverableErrorCallback(createStringError( 614*480093f4SDimitry Andric errc::invalid_argument, 6158bcb0991SDimitry Andric "mismatching address size at offset 0x%8.8" PRIx64 6160b57cec5SDimitry Andric " expected 0x%2.2" PRIx8 " found 0x%2.2" PRIx64, 617*480093f4SDimitry Andric ExtOffset, ExtractorAddressSize, Len - 1)); 618*480093f4SDimitry Andric 619*480093f4SDimitry Andric // Assume that the line table is correct and temporarily override the 620*480093f4SDimitry Andric // address size. 621*480093f4SDimitry Andric DebugLineData.setAddressSize(Len - 1); 6220b57cec5SDimitry Andric State.Row.Address.Address = DebugLineData.getRelocatedAddress( 6230b57cec5SDimitry Andric OffsetPtr, &State.Row.Address.SectionIndex); 624*480093f4SDimitry Andric 625*480093f4SDimitry Andric // Restore the address size if the extractor already had it. 626*480093f4SDimitry Andric if (ExtractorAddressSize != 0) 627*480093f4SDimitry Andric DebugLineData.setAddressSize(ExtractorAddressSize); 628*480093f4SDimitry Andric 6290b57cec5SDimitry Andric if (OS) 6300b57cec5SDimitry Andric *OS << format(" (0x%16.16" PRIx64 ")", State.Row.Address.Address); 631*480093f4SDimitry Andric } 6320b57cec5SDimitry Andric break; 6330b57cec5SDimitry Andric 6340b57cec5SDimitry Andric case DW_LNE_define_file: 6350b57cec5SDimitry Andric // Takes 4 arguments. The first is a null terminated string containing 6360b57cec5SDimitry Andric // a source file name. The second is an unsigned LEB128 number 6370b57cec5SDimitry Andric // representing the directory index of the directory in which the file 6380b57cec5SDimitry Andric // was found. The third is an unsigned LEB128 number representing the 6390b57cec5SDimitry Andric // time of last modification of the file. The fourth is an unsigned 6400b57cec5SDimitry Andric // LEB128 number representing the length in bytes of the file. The time 6410b57cec5SDimitry Andric // and length fields may contain LEB128(0) if the information is not 6420b57cec5SDimitry Andric // available. 6430b57cec5SDimitry Andric // 6440b57cec5SDimitry Andric // The directory index represents an entry in the include_directories 6450b57cec5SDimitry Andric // section of the statement program prologue. The index is LEB128(0) 6460b57cec5SDimitry Andric // if the file was found in the current directory of the compilation, 6470b57cec5SDimitry Andric // LEB128(1) if it was found in the first directory in the 6480b57cec5SDimitry Andric // include_directories section, and so on. The directory index is 6490b57cec5SDimitry Andric // ignored for file names that represent full path names. 6500b57cec5SDimitry Andric // 6510b57cec5SDimitry Andric // The files are numbered, starting at 1, in the order in which they 6520b57cec5SDimitry Andric // appear; the names in the prologue come before names defined by 6530b57cec5SDimitry Andric // the DW_LNE_define_file instruction. These numbers are used in the 6540b57cec5SDimitry Andric // the file register of the state machine. 6550b57cec5SDimitry Andric { 6560b57cec5SDimitry Andric FileNameEntry FileEntry; 6570b57cec5SDimitry Andric const char *Name = DebugLineData.getCStr(OffsetPtr); 6580b57cec5SDimitry Andric FileEntry.Name = 6590b57cec5SDimitry Andric DWARFFormValue::createFromPValue(dwarf::DW_FORM_string, Name); 6600b57cec5SDimitry Andric FileEntry.DirIdx = DebugLineData.getULEB128(OffsetPtr); 6610b57cec5SDimitry Andric FileEntry.ModTime = DebugLineData.getULEB128(OffsetPtr); 6620b57cec5SDimitry Andric FileEntry.Length = DebugLineData.getULEB128(OffsetPtr); 6630b57cec5SDimitry Andric Prologue.FileNames.push_back(FileEntry); 6640b57cec5SDimitry Andric if (OS) 6650b57cec5SDimitry Andric *OS << " (" << Name << ", dir=" << FileEntry.DirIdx << ", mod_time=" 6660b57cec5SDimitry Andric << format("(0x%16.16" PRIx64 ")", FileEntry.ModTime) 6670b57cec5SDimitry Andric << ", length=" << FileEntry.Length << ")"; 6680b57cec5SDimitry Andric } 6690b57cec5SDimitry Andric break; 6700b57cec5SDimitry Andric 6710b57cec5SDimitry Andric case DW_LNE_set_discriminator: 6720b57cec5SDimitry Andric State.Row.Discriminator = DebugLineData.getULEB128(OffsetPtr); 6730b57cec5SDimitry Andric if (OS) 6740b57cec5SDimitry Andric *OS << " (" << State.Row.Discriminator << ")"; 6750b57cec5SDimitry Andric break; 6760b57cec5SDimitry Andric 6770b57cec5SDimitry Andric default: 6780b57cec5SDimitry Andric if (OS) 6790b57cec5SDimitry Andric *OS << format("Unrecognized extended op 0x%02.02" PRIx8, SubOpcode) 6800b57cec5SDimitry Andric << format(" length %" PRIx64, Len); 6810b57cec5SDimitry Andric // Len doesn't include the zero opcode byte or the length itself, but 6820b57cec5SDimitry Andric // it does include the sub_opcode, so we have to adjust for that. 6830b57cec5SDimitry Andric (*OffsetPtr) += Len - 1; 6840b57cec5SDimitry Andric break; 6850b57cec5SDimitry Andric } 6860b57cec5SDimitry Andric // Make sure the stated and parsed lengths are the same. 6870b57cec5SDimitry Andric // Otherwise we have an unparseable line-number program. 6880b57cec5SDimitry Andric if (*OffsetPtr - ExtOffset != Len) 6890b57cec5SDimitry Andric return createStringError(errc::illegal_byte_sequence, 6908bcb0991SDimitry Andric "unexpected line op length at offset 0x%8.8" PRIx64 6918bcb0991SDimitry Andric " expected 0x%2.2" PRIx64 " found 0x%2.2" PRIx64, 6920b57cec5SDimitry Andric ExtOffset, Len, *OffsetPtr - ExtOffset); 6930b57cec5SDimitry Andric } else if (Opcode < Prologue.OpcodeBase) { 6940b57cec5SDimitry Andric if (OS) 6950b57cec5SDimitry Andric *OS << LNStandardString(Opcode); 6960b57cec5SDimitry Andric switch (Opcode) { 6970b57cec5SDimitry Andric // Standard Opcodes 6980b57cec5SDimitry Andric case DW_LNS_copy: 6990b57cec5SDimitry Andric // Takes no arguments. Append a row to the matrix using the 7000b57cec5SDimitry Andric // current values of the state-machine registers. 7010b57cec5SDimitry Andric if (OS) { 7020b57cec5SDimitry Andric *OS << "\n"; 7030b57cec5SDimitry Andric OS->indent(12); 7040b57cec5SDimitry Andric State.Row.dump(*OS); 7050b57cec5SDimitry Andric *OS << "\n"; 7060b57cec5SDimitry Andric } 7070b57cec5SDimitry Andric State.appendRowToMatrix(); 7080b57cec5SDimitry Andric break; 7090b57cec5SDimitry Andric 7100b57cec5SDimitry Andric case DW_LNS_advance_pc: 7110b57cec5SDimitry Andric // Takes a single unsigned LEB128 operand, multiplies it by the 7120b57cec5SDimitry Andric // min_inst_length field of the prologue, and adds the 7130b57cec5SDimitry Andric // result to the address register of the state machine. 7140b57cec5SDimitry Andric { 7150b57cec5SDimitry Andric uint64_t AddrOffset = 7160b57cec5SDimitry Andric DebugLineData.getULEB128(OffsetPtr) * Prologue.MinInstLength; 7170b57cec5SDimitry Andric State.Row.Address.Address += AddrOffset; 7180b57cec5SDimitry Andric if (OS) 7190b57cec5SDimitry Andric *OS << " (" << AddrOffset << ")"; 7200b57cec5SDimitry Andric } 7210b57cec5SDimitry Andric break; 7220b57cec5SDimitry Andric 7230b57cec5SDimitry Andric case DW_LNS_advance_line: 7240b57cec5SDimitry Andric // Takes a single signed LEB128 operand and adds that value to 7250b57cec5SDimitry Andric // the line register of the state machine. 7260b57cec5SDimitry Andric State.Row.Line += DebugLineData.getSLEB128(OffsetPtr); 7270b57cec5SDimitry Andric if (OS) 7280b57cec5SDimitry Andric *OS << " (" << State.Row.Line << ")"; 7290b57cec5SDimitry Andric break; 7300b57cec5SDimitry Andric 7310b57cec5SDimitry Andric case DW_LNS_set_file: 7320b57cec5SDimitry Andric // Takes a single unsigned LEB128 operand and stores it in the file 7330b57cec5SDimitry Andric // register of the state machine. 7340b57cec5SDimitry Andric State.Row.File = DebugLineData.getULEB128(OffsetPtr); 7350b57cec5SDimitry Andric if (OS) 7360b57cec5SDimitry Andric *OS << " (" << State.Row.File << ")"; 7370b57cec5SDimitry Andric break; 7380b57cec5SDimitry Andric 7390b57cec5SDimitry Andric case DW_LNS_set_column: 7400b57cec5SDimitry Andric // Takes a single unsigned LEB128 operand and stores it in the 7410b57cec5SDimitry Andric // column register of the state machine. 7420b57cec5SDimitry Andric State.Row.Column = DebugLineData.getULEB128(OffsetPtr); 7430b57cec5SDimitry Andric if (OS) 7440b57cec5SDimitry Andric *OS << " (" << State.Row.Column << ")"; 7450b57cec5SDimitry Andric break; 7460b57cec5SDimitry Andric 7470b57cec5SDimitry Andric case DW_LNS_negate_stmt: 7480b57cec5SDimitry Andric // Takes no arguments. Set the is_stmt register of the state 7490b57cec5SDimitry Andric // machine to the logical negation of its current value. 7500b57cec5SDimitry Andric State.Row.IsStmt = !State.Row.IsStmt; 7510b57cec5SDimitry Andric break; 7520b57cec5SDimitry Andric 7530b57cec5SDimitry Andric case DW_LNS_set_basic_block: 7540b57cec5SDimitry Andric // Takes no arguments. Set the basic_block register of the 7550b57cec5SDimitry Andric // state machine to true 7560b57cec5SDimitry Andric State.Row.BasicBlock = true; 7570b57cec5SDimitry Andric break; 7580b57cec5SDimitry Andric 7590b57cec5SDimitry Andric case DW_LNS_const_add_pc: 7600b57cec5SDimitry Andric // Takes no arguments. Add to the address register of the state 7610b57cec5SDimitry Andric // machine the address increment value corresponding to special 7620b57cec5SDimitry Andric // opcode 255. The motivation for DW_LNS_const_add_pc is this: 7630b57cec5SDimitry Andric // when the statement program needs to advance the address by a 7640b57cec5SDimitry Andric // small amount, it can use a single special opcode, which occupies 7650b57cec5SDimitry Andric // a single byte. When it needs to advance the address by up to 7660b57cec5SDimitry Andric // twice the range of the last special opcode, it can use 7670b57cec5SDimitry Andric // DW_LNS_const_add_pc followed by a special opcode, for a total 7680b57cec5SDimitry Andric // of two bytes. Only if it needs to advance the address by more 7690b57cec5SDimitry Andric // than twice that range will it need to use both DW_LNS_advance_pc 7700b57cec5SDimitry Andric // and a special opcode, requiring three or more bytes. 7710b57cec5SDimitry Andric { 7720b57cec5SDimitry Andric uint8_t AdjustOpcode = 255 - Prologue.OpcodeBase; 7730b57cec5SDimitry Andric uint64_t AddrOffset = 7740b57cec5SDimitry Andric (AdjustOpcode / Prologue.LineRange) * Prologue.MinInstLength; 7750b57cec5SDimitry Andric State.Row.Address.Address += AddrOffset; 7760b57cec5SDimitry Andric if (OS) 7770b57cec5SDimitry Andric *OS 7780b57cec5SDimitry Andric << format(" (0x%16.16" PRIx64 ")", AddrOffset); 7790b57cec5SDimitry Andric } 7800b57cec5SDimitry Andric break; 7810b57cec5SDimitry Andric 7820b57cec5SDimitry Andric case DW_LNS_fixed_advance_pc: 7830b57cec5SDimitry Andric // Takes a single uhalf operand. Add to the address register of 7840b57cec5SDimitry Andric // the state machine the value of the (unencoded) operand. This 7850b57cec5SDimitry Andric // is the only extended opcode that takes an argument that is not 7860b57cec5SDimitry Andric // a variable length number. The motivation for DW_LNS_fixed_advance_pc 7870b57cec5SDimitry Andric // is this: existing assemblers cannot emit DW_LNS_advance_pc or 7880b57cec5SDimitry Andric // special opcodes because they cannot encode LEB128 numbers or 7890b57cec5SDimitry Andric // judge when the computation of a special opcode overflows and 7900b57cec5SDimitry Andric // requires the use of DW_LNS_advance_pc. Such assemblers, however, 7910b57cec5SDimitry Andric // can use DW_LNS_fixed_advance_pc instead, sacrificing compression. 7920b57cec5SDimitry Andric { 7930b57cec5SDimitry Andric uint16_t PCOffset = DebugLineData.getRelocatedValue(2, OffsetPtr); 7940b57cec5SDimitry Andric State.Row.Address.Address += PCOffset; 7950b57cec5SDimitry Andric if (OS) 7960b57cec5SDimitry Andric *OS 7970b57cec5SDimitry Andric << format(" (0x%4.4" PRIx16 ")", PCOffset); 7980b57cec5SDimitry Andric } 7990b57cec5SDimitry Andric break; 8000b57cec5SDimitry Andric 8010b57cec5SDimitry Andric case DW_LNS_set_prologue_end: 8020b57cec5SDimitry Andric // Takes no arguments. Set the prologue_end register of the 8030b57cec5SDimitry Andric // state machine to true 8040b57cec5SDimitry Andric State.Row.PrologueEnd = true; 8050b57cec5SDimitry Andric break; 8060b57cec5SDimitry Andric 8070b57cec5SDimitry Andric case DW_LNS_set_epilogue_begin: 8080b57cec5SDimitry Andric // Takes no arguments. Set the basic_block register of the 8090b57cec5SDimitry Andric // state machine to true 8100b57cec5SDimitry Andric State.Row.EpilogueBegin = true; 8110b57cec5SDimitry Andric break; 8120b57cec5SDimitry Andric 8130b57cec5SDimitry Andric case DW_LNS_set_isa: 8140b57cec5SDimitry Andric // Takes a single unsigned LEB128 operand and stores it in the 8150b57cec5SDimitry Andric // column register of the state machine. 8160b57cec5SDimitry Andric State.Row.Isa = DebugLineData.getULEB128(OffsetPtr); 8170b57cec5SDimitry Andric if (OS) 818*480093f4SDimitry Andric *OS << " (" << (uint64_t)State.Row.Isa << ")"; 8190b57cec5SDimitry Andric break; 8200b57cec5SDimitry Andric 8210b57cec5SDimitry Andric default: 8220b57cec5SDimitry Andric // Handle any unknown standard opcodes here. We know the lengths 8230b57cec5SDimitry Andric // of such opcodes because they are specified in the prologue 8240b57cec5SDimitry Andric // as a multiple of LEB128 operands for each opcode. 8250b57cec5SDimitry Andric { 8260b57cec5SDimitry Andric assert(Opcode - 1U < Prologue.StandardOpcodeLengths.size()); 8270b57cec5SDimitry Andric uint8_t OpcodeLength = Prologue.StandardOpcodeLengths[Opcode - 1]; 8280b57cec5SDimitry Andric for (uint8_t I = 0; I < OpcodeLength; ++I) { 8290b57cec5SDimitry Andric uint64_t Value = DebugLineData.getULEB128(OffsetPtr); 8300b57cec5SDimitry Andric if (OS) 8310b57cec5SDimitry Andric *OS << format("Skipping ULEB128 value: 0x%16.16" PRIx64 ")\n", 8320b57cec5SDimitry Andric Value); 8330b57cec5SDimitry Andric } 8340b57cec5SDimitry Andric } 8350b57cec5SDimitry Andric break; 8360b57cec5SDimitry Andric } 8370b57cec5SDimitry Andric } else { 8380b57cec5SDimitry Andric // Special Opcodes 8390b57cec5SDimitry Andric 8400b57cec5SDimitry Andric // A special opcode value is chosen based on the amount that needs 8410b57cec5SDimitry Andric // to be added to the line and address registers. The maximum line 8420b57cec5SDimitry Andric // increment for a special opcode is the value of the line_base 8430b57cec5SDimitry Andric // field in the header, plus the value of the line_range field, 8440b57cec5SDimitry Andric // minus 1 (line base + line range - 1). If the desired line 8450b57cec5SDimitry Andric // increment is greater than the maximum line increment, a standard 8460b57cec5SDimitry Andric // opcode must be used instead of a special opcode. The "address 8470b57cec5SDimitry Andric // advance" is calculated by dividing the desired address increment 8480b57cec5SDimitry Andric // by the minimum_instruction_length field from the header. The 8490b57cec5SDimitry Andric // special opcode is then calculated using the following formula: 8500b57cec5SDimitry Andric // 8510b57cec5SDimitry Andric // opcode = (desired line increment - line_base) + 8520b57cec5SDimitry Andric // (line_range * address advance) + opcode_base 8530b57cec5SDimitry Andric // 8540b57cec5SDimitry Andric // If the resulting opcode is greater than 255, a standard opcode 8550b57cec5SDimitry Andric // must be used instead. 8560b57cec5SDimitry Andric // 8570b57cec5SDimitry Andric // To decode a special opcode, subtract the opcode_base from the 8580b57cec5SDimitry Andric // opcode itself to give the adjusted opcode. The amount to 8590b57cec5SDimitry Andric // increment the address register is the result of the adjusted 8600b57cec5SDimitry Andric // opcode divided by the line_range multiplied by the 8610b57cec5SDimitry Andric // minimum_instruction_length field from the header. That is: 8620b57cec5SDimitry Andric // 8630b57cec5SDimitry Andric // address increment = (adjusted opcode / line_range) * 8640b57cec5SDimitry Andric // minimum_instruction_length 8650b57cec5SDimitry Andric // 8660b57cec5SDimitry Andric // The amount to increment the line register is the line_base plus 8670b57cec5SDimitry Andric // the result of the adjusted opcode modulo the line_range. That is: 8680b57cec5SDimitry Andric // 8690b57cec5SDimitry Andric // line increment = line_base + (adjusted opcode % line_range) 8700b57cec5SDimitry Andric 8710b57cec5SDimitry Andric uint8_t AdjustOpcode = Opcode - Prologue.OpcodeBase; 8720b57cec5SDimitry Andric uint64_t AddrOffset = 8730b57cec5SDimitry Andric (AdjustOpcode / Prologue.LineRange) * Prologue.MinInstLength; 8740b57cec5SDimitry Andric int32_t LineOffset = 8750b57cec5SDimitry Andric Prologue.LineBase + (AdjustOpcode % Prologue.LineRange); 8760b57cec5SDimitry Andric State.Row.Line += LineOffset; 8770b57cec5SDimitry Andric State.Row.Address.Address += AddrOffset; 8780b57cec5SDimitry Andric 8790b57cec5SDimitry Andric if (OS) { 8800b57cec5SDimitry Andric *OS << "address += " << AddrOffset << ", line += " << LineOffset 8810b57cec5SDimitry Andric << "\n"; 8820b57cec5SDimitry Andric OS->indent(12); 8830b57cec5SDimitry Andric State.Row.dump(*OS); 8840b57cec5SDimitry Andric } 8850b57cec5SDimitry Andric 8860b57cec5SDimitry Andric State.appendRowToMatrix(); 8870b57cec5SDimitry Andric } 8880b57cec5SDimitry Andric if(OS) 8890b57cec5SDimitry Andric *OS << "\n"; 8900b57cec5SDimitry Andric } 8910b57cec5SDimitry Andric 8920b57cec5SDimitry Andric if (!State.Sequence.Empty) 893*480093f4SDimitry Andric RecoverableErrorCallback(createStringError( 894*480093f4SDimitry Andric errc::illegal_byte_sequence, 895*480093f4SDimitry Andric "last sequence in debug line table at offset 0x%8.8" PRIx64 896*480093f4SDimitry Andric " is not terminated", 897*480093f4SDimitry Andric DebugLineOffset)); 8980b57cec5SDimitry Andric 8990b57cec5SDimitry Andric // Sort all sequences so that address lookup will work faster. 9000b57cec5SDimitry Andric if (!Sequences.empty()) { 9010b57cec5SDimitry Andric llvm::sort(Sequences, Sequence::orderByHighPC); 9020b57cec5SDimitry Andric // Note: actually, instruction address ranges of sequences should not 9030b57cec5SDimitry Andric // overlap (in shared objects and executables). If they do, the address 9040b57cec5SDimitry Andric // lookup would still work, though, but result would be ambiguous. 9050b57cec5SDimitry Andric // We don't report warning in this case. For example, 9060b57cec5SDimitry Andric // sometimes .so compiled from multiple object files contains a few 9070b57cec5SDimitry Andric // rudimentary sequences for address ranges [0x0, 0xsomething). 9080b57cec5SDimitry Andric } 9090b57cec5SDimitry Andric 9100b57cec5SDimitry Andric return Error::success(); 9110b57cec5SDimitry Andric } 9120b57cec5SDimitry Andric 9130b57cec5SDimitry Andric uint32_t DWARFDebugLine::LineTable::findRowInSeq( 9140b57cec5SDimitry Andric const DWARFDebugLine::Sequence &Seq, 9150b57cec5SDimitry Andric object::SectionedAddress Address) const { 9160b57cec5SDimitry Andric if (!Seq.containsPC(Address)) 9170b57cec5SDimitry Andric return UnknownRowIndex; 9180b57cec5SDimitry Andric assert(Seq.SectionIndex == Address.SectionIndex); 9190b57cec5SDimitry Andric // In some cases, e.g. first instruction in a function, the compiler generates 9200b57cec5SDimitry Andric // two entries, both with the same address. We want the last one. 9210b57cec5SDimitry Andric // 9220b57cec5SDimitry Andric // In general we want a non-empty range: the last row whose address is less 9230b57cec5SDimitry Andric // than or equal to Address. This can be computed as upper_bound - 1. 9240b57cec5SDimitry Andric DWARFDebugLine::Row Row; 9250b57cec5SDimitry Andric Row.Address = Address; 9260b57cec5SDimitry Andric RowIter FirstRow = Rows.begin() + Seq.FirstRowIndex; 9270b57cec5SDimitry Andric RowIter LastRow = Rows.begin() + Seq.LastRowIndex; 9280b57cec5SDimitry Andric assert(FirstRow->Address.Address <= Row.Address.Address && 9290b57cec5SDimitry Andric Row.Address.Address < LastRow[-1].Address.Address); 9300b57cec5SDimitry Andric RowIter RowPos = std::upper_bound(FirstRow + 1, LastRow - 1, Row, 9310b57cec5SDimitry Andric DWARFDebugLine::Row::orderByAddress) - 9320b57cec5SDimitry Andric 1; 9330b57cec5SDimitry Andric assert(Seq.SectionIndex == RowPos->Address.SectionIndex); 9340b57cec5SDimitry Andric return RowPos - Rows.begin(); 9350b57cec5SDimitry Andric } 9360b57cec5SDimitry Andric 9370b57cec5SDimitry Andric uint32_t DWARFDebugLine::LineTable::lookupAddress( 9380b57cec5SDimitry Andric object::SectionedAddress Address) const { 9390b57cec5SDimitry Andric 9400b57cec5SDimitry Andric // Search for relocatable addresses 9410b57cec5SDimitry Andric uint32_t Result = lookupAddressImpl(Address); 9420b57cec5SDimitry Andric 9430b57cec5SDimitry Andric if (Result != UnknownRowIndex || 9440b57cec5SDimitry Andric Address.SectionIndex == object::SectionedAddress::UndefSection) 9450b57cec5SDimitry Andric return Result; 9460b57cec5SDimitry Andric 9470b57cec5SDimitry Andric // Search for absolute addresses 9480b57cec5SDimitry Andric Address.SectionIndex = object::SectionedAddress::UndefSection; 9490b57cec5SDimitry Andric return lookupAddressImpl(Address); 9500b57cec5SDimitry Andric } 9510b57cec5SDimitry Andric 9520b57cec5SDimitry Andric uint32_t DWARFDebugLine::LineTable::lookupAddressImpl( 9530b57cec5SDimitry Andric object::SectionedAddress Address) const { 9540b57cec5SDimitry Andric // First, find an instruction sequence containing the given address. 9550b57cec5SDimitry Andric DWARFDebugLine::Sequence Sequence; 9560b57cec5SDimitry Andric Sequence.SectionIndex = Address.SectionIndex; 9570b57cec5SDimitry Andric Sequence.HighPC = Address.Address; 9580b57cec5SDimitry Andric SequenceIter It = llvm::upper_bound(Sequences, Sequence, 9590b57cec5SDimitry Andric DWARFDebugLine::Sequence::orderByHighPC); 9600b57cec5SDimitry Andric if (It == Sequences.end() || It->SectionIndex != Address.SectionIndex) 9610b57cec5SDimitry Andric return UnknownRowIndex; 9620b57cec5SDimitry Andric return findRowInSeq(*It, Address); 9630b57cec5SDimitry Andric } 9640b57cec5SDimitry Andric 9650b57cec5SDimitry Andric bool DWARFDebugLine::LineTable::lookupAddressRange( 9660b57cec5SDimitry Andric object::SectionedAddress Address, uint64_t Size, 9670b57cec5SDimitry Andric std::vector<uint32_t> &Result) const { 9680b57cec5SDimitry Andric 9690b57cec5SDimitry Andric // Search for relocatable addresses 9700b57cec5SDimitry Andric if (lookupAddressRangeImpl(Address, Size, Result)) 9710b57cec5SDimitry Andric return true; 9720b57cec5SDimitry Andric 9730b57cec5SDimitry Andric if (Address.SectionIndex == object::SectionedAddress::UndefSection) 9740b57cec5SDimitry Andric return false; 9750b57cec5SDimitry Andric 9760b57cec5SDimitry Andric // Search for absolute addresses 9770b57cec5SDimitry Andric Address.SectionIndex = object::SectionedAddress::UndefSection; 9780b57cec5SDimitry Andric return lookupAddressRangeImpl(Address, Size, Result); 9790b57cec5SDimitry Andric } 9800b57cec5SDimitry Andric 9810b57cec5SDimitry Andric bool DWARFDebugLine::LineTable::lookupAddressRangeImpl( 9820b57cec5SDimitry Andric object::SectionedAddress Address, uint64_t Size, 9830b57cec5SDimitry Andric std::vector<uint32_t> &Result) const { 9840b57cec5SDimitry Andric if (Sequences.empty()) 9850b57cec5SDimitry Andric return false; 9860b57cec5SDimitry Andric uint64_t EndAddr = Address.Address + Size; 9870b57cec5SDimitry Andric // First, find an instruction sequence containing the given address. 9880b57cec5SDimitry Andric DWARFDebugLine::Sequence Sequence; 9890b57cec5SDimitry Andric Sequence.SectionIndex = Address.SectionIndex; 9900b57cec5SDimitry Andric Sequence.HighPC = Address.Address; 9910b57cec5SDimitry Andric SequenceIter LastSeq = Sequences.end(); 9920b57cec5SDimitry Andric SequenceIter SeqPos = llvm::upper_bound( 9930b57cec5SDimitry Andric Sequences, Sequence, DWARFDebugLine::Sequence::orderByHighPC); 9940b57cec5SDimitry Andric if (SeqPos == LastSeq || !SeqPos->containsPC(Address)) 9950b57cec5SDimitry Andric return false; 9960b57cec5SDimitry Andric 9970b57cec5SDimitry Andric SequenceIter StartPos = SeqPos; 9980b57cec5SDimitry Andric 9990b57cec5SDimitry Andric // Add the rows from the first sequence to the vector, starting with the 10000b57cec5SDimitry Andric // index we just calculated 10010b57cec5SDimitry Andric 10020b57cec5SDimitry Andric while (SeqPos != LastSeq && SeqPos->LowPC < EndAddr) { 10030b57cec5SDimitry Andric const DWARFDebugLine::Sequence &CurSeq = *SeqPos; 10040b57cec5SDimitry Andric // For the first sequence, we need to find which row in the sequence is the 10050b57cec5SDimitry Andric // first in our range. 10060b57cec5SDimitry Andric uint32_t FirstRowIndex = CurSeq.FirstRowIndex; 10070b57cec5SDimitry Andric if (SeqPos == StartPos) 10080b57cec5SDimitry Andric FirstRowIndex = findRowInSeq(CurSeq, Address); 10090b57cec5SDimitry Andric 10100b57cec5SDimitry Andric // Figure out the last row in the range. 10110b57cec5SDimitry Andric uint32_t LastRowIndex = 10120b57cec5SDimitry Andric findRowInSeq(CurSeq, {EndAddr - 1, Address.SectionIndex}); 10130b57cec5SDimitry Andric if (LastRowIndex == UnknownRowIndex) 10140b57cec5SDimitry Andric LastRowIndex = CurSeq.LastRowIndex - 1; 10150b57cec5SDimitry Andric 10160b57cec5SDimitry Andric assert(FirstRowIndex != UnknownRowIndex); 10170b57cec5SDimitry Andric assert(LastRowIndex != UnknownRowIndex); 10180b57cec5SDimitry Andric 10190b57cec5SDimitry Andric for (uint32_t I = FirstRowIndex; I <= LastRowIndex; ++I) { 10200b57cec5SDimitry Andric Result.push_back(I); 10210b57cec5SDimitry Andric } 10220b57cec5SDimitry Andric 10230b57cec5SDimitry Andric ++SeqPos; 10240b57cec5SDimitry Andric } 10250b57cec5SDimitry Andric 10260b57cec5SDimitry Andric return true; 10270b57cec5SDimitry Andric } 10280b57cec5SDimitry Andric 10290b57cec5SDimitry Andric Optional<StringRef> DWARFDebugLine::LineTable::getSourceByIndex(uint64_t FileIndex, 10300b57cec5SDimitry Andric FileLineInfoKind Kind) const { 10310b57cec5SDimitry Andric if (Kind == FileLineInfoKind::None || !Prologue.hasFileAtIndex(FileIndex)) 10320b57cec5SDimitry Andric return None; 10330b57cec5SDimitry Andric const FileNameEntry &Entry = Prologue.getFileNameEntry(FileIndex); 10340b57cec5SDimitry Andric if (Optional<const char *> source = Entry.Source.getAsCString()) 10350b57cec5SDimitry Andric return StringRef(*source); 10360b57cec5SDimitry Andric return None; 10370b57cec5SDimitry Andric } 10380b57cec5SDimitry Andric 10390b57cec5SDimitry Andric static bool isPathAbsoluteOnWindowsOrPosix(const Twine &Path) { 10400b57cec5SDimitry Andric // Debug info can contain paths from any OS, not necessarily 10410b57cec5SDimitry Andric // an OS we're currently running on. Moreover different compilation units can 10420b57cec5SDimitry Andric // be compiled on different operating systems and linked together later. 10430b57cec5SDimitry Andric return sys::path::is_absolute(Path, sys::path::Style::posix) || 10440b57cec5SDimitry Andric sys::path::is_absolute(Path, sys::path::Style::windows); 10450b57cec5SDimitry Andric } 10460b57cec5SDimitry Andric 10478bcb0991SDimitry Andric bool DWARFDebugLine::Prologue::getFileNameByIndex( 10488bcb0991SDimitry Andric uint64_t FileIndex, StringRef CompDir, FileLineInfoKind Kind, 10498bcb0991SDimitry Andric std::string &Result, sys::path::Style Style) const { 10500b57cec5SDimitry Andric if (Kind == FileLineInfoKind::None || !hasFileAtIndex(FileIndex)) 10510b57cec5SDimitry Andric return false; 10520b57cec5SDimitry Andric const FileNameEntry &Entry = getFileNameEntry(FileIndex); 1053*480093f4SDimitry Andric Optional<const char *> Name = Entry.Name.getAsCString(); 1054*480093f4SDimitry Andric if (!Name) 1055*480093f4SDimitry Andric return false; 1056*480093f4SDimitry Andric StringRef FileName = *Name; 10570b57cec5SDimitry Andric if (Kind != FileLineInfoKind::AbsoluteFilePath || 10580b57cec5SDimitry Andric isPathAbsoluteOnWindowsOrPosix(FileName)) { 10590b57cec5SDimitry Andric Result = FileName; 10600b57cec5SDimitry Andric return true; 10610b57cec5SDimitry Andric } 10620b57cec5SDimitry Andric 10630b57cec5SDimitry Andric SmallString<16> FilePath; 10640b57cec5SDimitry Andric StringRef IncludeDir; 10650b57cec5SDimitry Andric // Be defensive about the contents of Entry. 10660b57cec5SDimitry Andric if (getVersion() >= 5) { 10670b57cec5SDimitry Andric if (Entry.DirIdx < IncludeDirectories.size()) 10680b57cec5SDimitry Andric IncludeDir = IncludeDirectories[Entry.DirIdx].getAsCString().getValue(); 10690b57cec5SDimitry Andric } else { 10700b57cec5SDimitry Andric if (0 < Entry.DirIdx && Entry.DirIdx <= IncludeDirectories.size()) 10710b57cec5SDimitry Andric IncludeDir = 10720b57cec5SDimitry Andric IncludeDirectories[Entry.DirIdx - 1].getAsCString().getValue(); 10730b57cec5SDimitry Andric 10740b57cec5SDimitry Andric // We may still need to append compilation directory of compile unit. 10750b57cec5SDimitry Andric // We know that FileName is not absolute, the only way to have an 10760b57cec5SDimitry Andric // absolute path at this point would be if IncludeDir is absolute. 10770b57cec5SDimitry Andric if (!CompDir.empty() && !isPathAbsoluteOnWindowsOrPosix(IncludeDir)) 10788bcb0991SDimitry Andric sys::path::append(FilePath, Style, CompDir); 10790b57cec5SDimitry Andric } 10800b57cec5SDimitry Andric 10810b57cec5SDimitry Andric // sys::path::append skips empty strings. 10828bcb0991SDimitry Andric sys::path::append(FilePath, Style, IncludeDir, FileName); 10830b57cec5SDimitry Andric Result = FilePath.str(); 10840b57cec5SDimitry Andric return true; 10850b57cec5SDimitry Andric } 10860b57cec5SDimitry Andric 10870b57cec5SDimitry Andric bool DWARFDebugLine::LineTable::getFileLineInfoForAddress( 10880b57cec5SDimitry Andric object::SectionedAddress Address, const char *CompDir, 10890b57cec5SDimitry Andric FileLineInfoKind Kind, DILineInfo &Result) const { 10900b57cec5SDimitry Andric // Get the index of row we're looking for in the line table. 10910b57cec5SDimitry Andric uint32_t RowIndex = lookupAddress(Address); 10920b57cec5SDimitry Andric if (RowIndex == -1U) 10930b57cec5SDimitry Andric return false; 10940b57cec5SDimitry Andric // Take file number and line/column from the row. 10950b57cec5SDimitry Andric const auto &Row = Rows[RowIndex]; 10960b57cec5SDimitry Andric if (!getFileNameByIndex(Row.File, CompDir, Kind, Result.FileName)) 10970b57cec5SDimitry Andric return false; 10980b57cec5SDimitry Andric Result.Line = Row.Line; 10990b57cec5SDimitry Andric Result.Column = Row.Column; 11000b57cec5SDimitry Andric Result.Discriminator = Row.Discriminator; 11010b57cec5SDimitry Andric Result.Source = getSourceByIndex(Row.File, Kind); 11020b57cec5SDimitry Andric return true; 11030b57cec5SDimitry Andric } 11040b57cec5SDimitry Andric 11050b57cec5SDimitry Andric // We want to supply the Unit associated with a .debug_line[.dwo] table when 11060b57cec5SDimitry Andric // we dump it, if possible, but still dump the table even if there isn't a Unit. 11070b57cec5SDimitry Andric // Therefore, collect up handles on all the Units that point into the 11080b57cec5SDimitry Andric // line-table section. 11090b57cec5SDimitry Andric static DWARFDebugLine::SectionParser::LineToUnitMap 11100b57cec5SDimitry Andric buildLineToUnitMap(DWARFDebugLine::SectionParser::cu_range CUs, 11110b57cec5SDimitry Andric DWARFDebugLine::SectionParser::tu_range TUs) { 11120b57cec5SDimitry Andric DWARFDebugLine::SectionParser::LineToUnitMap LineToUnit; 11130b57cec5SDimitry Andric for (const auto &CU : CUs) 11140b57cec5SDimitry Andric if (auto CUDIE = CU->getUnitDIE()) 11150b57cec5SDimitry Andric if (auto StmtOffset = toSectionOffset(CUDIE.find(DW_AT_stmt_list))) 11160b57cec5SDimitry Andric LineToUnit.insert(std::make_pair(*StmtOffset, &*CU)); 11170b57cec5SDimitry Andric for (const auto &TU : TUs) 11180b57cec5SDimitry Andric if (auto TUDIE = TU->getUnitDIE()) 11190b57cec5SDimitry Andric if (auto StmtOffset = toSectionOffset(TUDIE.find(DW_AT_stmt_list))) 11200b57cec5SDimitry Andric LineToUnit.insert(std::make_pair(*StmtOffset, &*TU)); 11210b57cec5SDimitry Andric return LineToUnit; 11220b57cec5SDimitry Andric } 11230b57cec5SDimitry Andric 11240b57cec5SDimitry Andric DWARFDebugLine::SectionParser::SectionParser(DWARFDataExtractor &Data, 11250b57cec5SDimitry Andric const DWARFContext &C, 11260b57cec5SDimitry Andric cu_range CUs, tu_range TUs) 11270b57cec5SDimitry Andric : DebugLineData(Data), Context(C) { 11280b57cec5SDimitry Andric LineToUnit = buildLineToUnitMap(CUs, TUs); 11290b57cec5SDimitry Andric if (!DebugLineData.isValidOffset(Offset)) 11300b57cec5SDimitry Andric Done = true; 11310b57cec5SDimitry Andric } 11320b57cec5SDimitry Andric 11330b57cec5SDimitry Andric bool DWARFDebugLine::Prologue::totalLengthIsValid() const { 11348bcb0991SDimitry Andric return TotalLength == dwarf::DW_LENGTH_DWARF64 || 11358bcb0991SDimitry Andric TotalLength < dwarf::DW_LENGTH_lo_reserved; 11360b57cec5SDimitry Andric } 11370b57cec5SDimitry Andric 11380b57cec5SDimitry Andric DWARFDebugLine::LineTable DWARFDebugLine::SectionParser::parseNext( 11390b57cec5SDimitry Andric function_ref<void(Error)> RecoverableErrorCallback, 11400b57cec5SDimitry Andric function_ref<void(Error)> UnrecoverableErrorCallback, raw_ostream *OS) { 11410b57cec5SDimitry Andric assert(DebugLineData.isValidOffset(Offset) && 11420b57cec5SDimitry Andric "parsing should have terminated"); 11430b57cec5SDimitry Andric DWARFUnit *U = prepareToParse(Offset); 11448bcb0991SDimitry Andric uint64_t OldOffset = Offset; 11450b57cec5SDimitry Andric LineTable LT; 11460b57cec5SDimitry Andric if (Error Err = LT.parse(DebugLineData, &Offset, Context, U, 11470b57cec5SDimitry Andric RecoverableErrorCallback, OS)) 11480b57cec5SDimitry Andric UnrecoverableErrorCallback(std::move(Err)); 11490b57cec5SDimitry Andric moveToNextTable(OldOffset, LT.Prologue); 11500b57cec5SDimitry Andric return LT; 11510b57cec5SDimitry Andric } 11520b57cec5SDimitry Andric 11530b57cec5SDimitry Andric void DWARFDebugLine::SectionParser::skip( 11540b57cec5SDimitry Andric function_ref<void(Error)> ErrorCallback) { 11550b57cec5SDimitry Andric assert(DebugLineData.isValidOffset(Offset) && 11560b57cec5SDimitry Andric "parsing should have terminated"); 11570b57cec5SDimitry Andric DWARFUnit *U = prepareToParse(Offset); 11588bcb0991SDimitry Andric uint64_t OldOffset = Offset; 11590b57cec5SDimitry Andric LineTable LT; 11600b57cec5SDimitry Andric if (Error Err = LT.Prologue.parse(DebugLineData, &Offset, Context, U)) 11610b57cec5SDimitry Andric ErrorCallback(std::move(Err)); 11620b57cec5SDimitry Andric moveToNextTable(OldOffset, LT.Prologue); 11630b57cec5SDimitry Andric } 11640b57cec5SDimitry Andric 11658bcb0991SDimitry Andric DWARFUnit *DWARFDebugLine::SectionParser::prepareToParse(uint64_t Offset) { 11660b57cec5SDimitry Andric DWARFUnit *U = nullptr; 11670b57cec5SDimitry Andric auto It = LineToUnit.find(Offset); 11680b57cec5SDimitry Andric if (It != LineToUnit.end()) 11690b57cec5SDimitry Andric U = It->second; 11700b57cec5SDimitry Andric DebugLineData.setAddressSize(U ? U->getAddressByteSize() : 0); 11710b57cec5SDimitry Andric return U; 11720b57cec5SDimitry Andric } 11730b57cec5SDimitry Andric 11748bcb0991SDimitry Andric void DWARFDebugLine::SectionParser::moveToNextTable(uint64_t OldOffset, 11750b57cec5SDimitry Andric const Prologue &P) { 11760b57cec5SDimitry Andric // If the length field is not valid, we don't know where the next table is, so 11770b57cec5SDimitry Andric // cannot continue to parse. Mark the parser as done, and leave the Offset 11780b57cec5SDimitry Andric // value as it currently is. This will be the end of the bad length field. 11790b57cec5SDimitry Andric if (!P.totalLengthIsValid()) { 11800b57cec5SDimitry Andric Done = true; 11810b57cec5SDimitry Andric return; 11820b57cec5SDimitry Andric } 11830b57cec5SDimitry Andric 11840b57cec5SDimitry Andric Offset = OldOffset + P.TotalLength + P.sizeofTotalLength(); 11850b57cec5SDimitry Andric if (!DebugLineData.isValidOffset(Offset)) { 11860b57cec5SDimitry Andric Done = true; 11870b57cec5SDimitry Andric } 11880b57cec5SDimitry Andric } 1189