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" 19*5ffd83dbSDimitry Andric #include "llvm/Support/FormatVariadic.h" 200b57cec5SDimitry Andric #include "llvm/Support/WithColor.h" 210b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 220b57cec5SDimitry Andric #include <algorithm> 230b57cec5SDimitry Andric #include <cassert> 240b57cec5SDimitry Andric #include <cinttypes> 250b57cec5SDimitry Andric #include <cstdint> 260b57cec5SDimitry Andric #include <cstdio> 270b57cec5SDimitry Andric #include <utility> 280b57cec5SDimitry Andric 290b57cec5SDimitry Andric using namespace llvm; 300b57cec5SDimitry Andric using namespace dwarf; 310b57cec5SDimitry Andric 320b57cec5SDimitry Andric using FileLineInfoKind = DILineInfoSpecifier::FileLineInfoKind; 330b57cec5SDimitry Andric 340b57cec5SDimitry Andric namespace { 350b57cec5SDimitry Andric 360b57cec5SDimitry Andric struct ContentDescriptor { 370b57cec5SDimitry Andric dwarf::LineNumberEntryFormat Type; 380b57cec5SDimitry Andric dwarf::Form Form; 390b57cec5SDimitry Andric }; 400b57cec5SDimitry Andric 410b57cec5SDimitry Andric using ContentDescriptors = SmallVector<ContentDescriptor, 4>; 420b57cec5SDimitry Andric 43480093f4SDimitry Andric } // end anonymous namespace 440b57cec5SDimitry Andric 45*5ffd83dbSDimitry Andric static bool versionIsSupported(uint16_t Version) { 46*5ffd83dbSDimitry Andric return Version >= 2 && Version <= 5; 47*5ffd83dbSDimitry Andric } 48*5ffd83dbSDimitry Andric 490b57cec5SDimitry Andric void DWARFDebugLine::ContentTypeTracker::trackContentType( 500b57cec5SDimitry Andric dwarf::LineNumberEntryFormat ContentType) { 510b57cec5SDimitry Andric switch (ContentType) { 520b57cec5SDimitry Andric case dwarf::DW_LNCT_timestamp: 530b57cec5SDimitry Andric HasModTime = true; 540b57cec5SDimitry Andric break; 550b57cec5SDimitry Andric case dwarf::DW_LNCT_size: 560b57cec5SDimitry Andric HasLength = true; 570b57cec5SDimitry Andric break; 580b57cec5SDimitry Andric case dwarf::DW_LNCT_MD5: 590b57cec5SDimitry Andric HasMD5 = true; 600b57cec5SDimitry Andric break; 610b57cec5SDimitry Andric case dwarf::DW_LNCT_LLVM_source: 620b57cec5SDimitry Andric HasSource = true; 630b57cec5SDimitry Andric break; 640b57cec5SDimitry Andric default: 650b57cec5SDimitry Andric // We only care about values we consider optional, and new values may be 660b57cec5SDimitry Andric // added in the vendor extension range, so we do not match exhaustively. 670b57cec5SDimitry Andric break; 680b57cec5SDimitry Andric } 690b57cec5SDimitry Andric } 700b57cec5SDimitry Andric 710b57cec5SDimitry Andric DWARFDebugLine::Prologue::Prologue() { clear(); } 720b57cec5SDimitry Andric 730b57cec5SDimitry Andric bool DWARFDebugLine::Prologue::hasFileAtIndex(uint64_t FileIndex) const { 740b57cec5SDimitry Andric uint16_t DwarfVersion = getVersion(); 750b57cec5SDimitry Andric assert(DwarfVersion != 0 && 760b57cec5SDimitry Andric "line table prologue has no dwarf version information"); 770b57cec5SDimitry Andric if (DwarfVersion >= 5) 780b57cec5SDimitry Andric return FileIndex < FileNames.size(); 790b57cec5SDimitry Andric return FileIndex != 0 && FileIndex <= FileNames.size(); 800b57cec5SDimitry Andric } 810b57cec5SDimitry Andric 820b57cec5SDimitry Andric const llvm::DWARFDebugLine::FileNameEntry & 830b57cec5SDimitry Andric DWARFDebugLine::Prologue::getFileNameEntry(uint64_t Index) const { 840b57cec5SDimitry Andric uint16_t DwarfVersion = getVersion(); 850b57cec5SDimitry Andric assert(DwarfVersion != 0 && 860b57cec5SDimitry Andric "line table prologue has no dwarf version information"); 870b57cec5SDimitry Andric // In DWARF v5 the file names are 0-indexed. 880b57cec5SDimitry Andric if (DwarfVersion >= 5) 890b57cec5SDimitry Andric return FileNames[Index]; 900b57cec5SDimitry Andric return FileNames[Index - 1]; 910b57cec5SDimitry Andric } 920b57cec5SDimitry Andric 930b57cec5SDimitry Andric void DWARFDebugLine::Prologue::clear() { 940b57cec5SDimitry Andric TotalLength = PrologueLength = 0; 950b57cec5SDimitry Andric SegSelectorSize = 0; 960b57cec5SDimitry Andric MinInstLength = MaxOpsPerInst = DefaultIsStmt = LineBase = LineRange = 0; 970b57cec5SDimitry Andric OpcodeBase = 0; 980b57cec5SDimitry Andric FormParams = dwarf::FormParams({0, 0, DWARF32}); 990b57cec5SDimitry Andric ContentTypes = ContentTypeTracker(); 1000b57cec5SDimitry Andric StandardOpcodeLengths.clear(); 1010b57cec5SDimitry Andric IncludeDirectories.clear(); 1020b57cec5SDimitry Andric FileNames.clear(); 1030b57cec5SDimitry Andric } 1040b57cec5SDimitry Andric 1050b57cec5SDimitry Andric void DWARFDebugLine::Prologue::dump(raw_ostream &OS, 1060b57cec5SDimitry Andric DIDumpOptions DumpOptions) const { 107*5ffd83dbSDimitry Andric if (!totalLengthIsValid()) 108*5ffd83dbSDimitry Andric return; 109*5ffd83dbSDimitry Andric int OffsetDumpWidth = 2 * dwarf::getDwarfOffsetByteSize(FormParams.Format); 1100b57cec5SDimitry Andric OS << "Line table prologue:\n" 111*5ffd83dbSDimitry Andric << format(" total_length: 0x%0*" PRIx64 "\n", OffsetDumpWidth, 112*5ffd83dbSDimitry Andric TotalLength) 113*5ffd83dbSDimitry Andric << " format: " << dwarf::FormatString(FormParams.Format) << "\n" 1140b57cec5SDimitry Andric << format(" version: %u\n", getVersion()); 115*5ffd83dbSDimitry Andric if (!versionIsSupported(getVersion())) 116*5ffd83dbSDimitry Andric return; 1170b57cec5SDimitry Andric if (getVersion() >= 5) 1180b57cec5SDimitry Andric OS << format(" address_size: %u\n", getAddressSize()) 1190b57cec5SDimitry Andric << format(" seg_select_size: %u\n", SegSelectorSize); 120*5ffd83dbSDimitry Andric OS << format(" prologue_length: 0x%0*" PRIx64 "\n", OffsetDumpWidth, 121*5ffd83dbSDimitry Andric PrologueLength) 1220b57cec5SDimitry Andric << format(" min_inst_length: %u\n", MinInstLength) 1230b57cec5SDimitry Andric << format(getVersion() >= 4 ? "max_ops_per_inst: %u\n" : "", MaxOpsPerInst) 1240b57cec5SDimitry Andric << format(" default_is_stmt: %u\n", DefaultIsStmt) 1250b57cec5SDimitry Andric << format(" line_base: %i\n", LineBase) 1260b57cec5SDimitry Andric << format(" line_range: %u\n", LineRange) 1270b57cec5SDimitry Andric << format(" opcode_base: %u\n", OpcodeBase); 1280b57cec5SDimitry Andric 1290b57cec5SDimitry Andric for (uint32_t I = 0; I != StandardOpcodeLengths.size(); ++I) 130*5ffd83dbSDimitry Andric OS << formatv("standard_opcode_lengths[{0}] = {1}\n", 131*5ffd83dbSDimitry Andric static_cast<dwarf::LineNumberOps>(I + 1), 132*5ffd83dbSDimitry Andric StandardOpcodeLengths[I]); 1330b57cec5SDimitry Andric 1340b57cec5SDimitry Andric if (!IncludeDirectories.empty()) { 1350b57cec5SDimitry Andric // DWARF v5 starts directory indexes at 0. 1360b57cec5SDimitry Andric uint32_t DirBase = getVersion() >= 5 ? 0 : 1; 1370b57cec5SDimitry Andric for (uint32_t I = 0; I != IncludeDirectories.size(); ++I) { 1380b57cec5SDimitry Andric OS << format("include_directories[%3u] = ", I + DirBase); 1390b57cec5SDimitry Andric IncludeDirectories[I].dump(OS, DumpOptions); 1400b57cec5SDimitry Andric OS << '\n'; 1410b57cec5SDimitry Andric } 1420b57cec5SDimitry Andric } 1430b57cec5SDimitry Andric 1440b57cec5SDimitry Andric if (!FileNames.empty()) { 1450b57cec5SDimitry Andric // DWARF v5 starts file indexes at 0. 1460b57cec5SDimitry Andric uint32_t FileBase = getVersion() >= 5 ? 0 : 1; 1470b57cec5SDimitry Andric for (uint32_t I = 0; I != FileNames.size(); ++I) { 1480b57cec5SDimitry Andric const FileNameEntry &FileEntry = FileNames[I]; 1490b57cec5SDimitry Andric OS << format("file_names[%3u]:\n", I + FileBase); 1500b57cec5SDimitry Andric OS << " name: "; 1510b57cec5SDimitry Andric FileEntry.Name.dump(OS, DumpOptions); 1520b57cec5SDimitry Andric OS << '\n' 1530b57cec5SDimitry Andric << format(" dir_index: %" PRIu64 "\n", FileEntry.DirIdx); 1540b57cec5SDimitry Andric if (ContentTypes.HasMD5) 1550b57cec5SDimitry Andric OS << " md5_checksum: " << FileEntry.Checksum.digest() << '\n'; 1560b57cec5SDimitry Andric if (ContentTypes.HasModTime) 1570b57cec5SDimitry Andric OS << format(" mod_time: 0x%8.8" PRIx64 "\n", FileEntry.ModTime); 1580b57cec5SDimitry Andric if (ContentTypes.HasLength) 1590b57cec5SDimitry Andric OS << format(" length: 0x%8.8" PRIx64 "\n", FileEntry.Length); 1600b57cec5SDimitry Andric if (ContentTypes.HasSource) { 1610b57cec5SDimitry Andric OS << " source: "; 1620b57cec5SDimitry Andric FileEntry.Source.dump(OS, DumpOptions); 1630b57cec5SDimitry Andric OS << '\n'; 1640b57cec5SDimitry Andric } 1650b57cec5SDimitry Andric } 1660b57cec5SDimitry Andric } 1670b57cec5SDimitry Andric } 1680b57cec5SDimitry Andric 1690b57cec5SDimitry Andric // Parse v2-v4 directory and file tables. 170*5ffd83dbSDimitry Andric static Error 1710b57cec5SDimitry Andric parseV2DirFileTables(const DWARFDataExtractor &DebugLineData, 172*5ffd83dbSDimitry Andric uint64_t *OffsetPtr, 1730b57cec5SDimitry Andric DWARFDebugLine::ContentTypeTracker &ContentTypes, 1740b57cec5SDimitry Andric std::vector<DWARFFormValue> &IncludeDirectories, 1750b57cec5SDimitry Andric std::vector<DWARFDebugLine::FileNameEntry> &FileNames) { 176*5ffd83dbSDimitry Andric while (true) { 177*5ffd83dbSDimitry Andric Error Err = Error::success(); 178*5ffd83dbSDimitry Andric StringRef S = DebugLineData.getCStrRef(OffsetPtr, &Err); 179*5ffd83dbSDimitry Andric if (Err) { 180*5ffd83dbSDimitry Andric consumeError(std::move(Err)); 181*5ffd83dbSDimitry Andric return createStringError(errc::invalid_argument, 182*5ffd83dbSDimitry Andric "include directories table was not null " 183*5ffd83dbSDimitry Andric "terminated before the end of the prologue"); 184*5ffd83dbSDimitry Andric } 1850b57cec5SDimitry Andric if (S.empty()) 1860b57cec5SDimitry Andric break; 1870b57cec5SDimitry Andric DWARFFormValue Dir = 1880b57cec5SDimitry Andric DWARFFormValue::createFromPValue(dwarf::DW_FORM_string, S.data()); 1890b57cec5SDimitry Andric IncludeDirectories.push_back(Dir); 1900b57cec5SDimitry Andric } 1910b57cec5SDimitry Andric 192*5ffd83dbSDimitry Andric ContentTypes.HasModTime = true; 193*5ffd83dbSDimitry Andric ContentTypes.HasLength = true; 194*5ffd83dbSDimitry Andric 195*5ffd83dbSDimitry Andric while (true) { 196*5ffd83dbSDimitry Andric Error Err = Error::success(); 197*5ffd83dbSDimitry Andric StringRef Name = DebugLineData.getCStrRef(OffsetPtr, &Err); 198*5ffd83dbSDimitry Andric if (!Err && Name.empty()) 1990b57cec5SDimitry Andric break; 200*5ffd83dbSDimitry Andric 2010b57cec5SDimitry Andric DWARFDebugLine::FileNameEntry FileEntry; 2020b57cec5SDimitry Andric FileEntry.Name = 2030b57cec5SDimitry Andric DWARFFormValue::createFromPValue(dwarf::DW_FORM_string, Name.data()); 204*5ffd83dbSDimitry Andric FileEntry.DirIdx = DebugLineData.getULEB128(OffsetPtr, &Err); 205*5ffd83dbSDimitry Andric FileEntry.ModTime = DebugLineData.getULEB128(OffsetPtr, &Err); 206*5ffd83dbSDimitry Andric FileEntry.Length = DebugLineData.getULEB128(OffsetPtr, &Err); 207*5ffd83dbSDimitry Andric 208*5ffd83dbSDimitry Andric if (Err) { 209*5ffd83dbSDimitry Andric consumeError(std::move(Err)); 210*5ffd83dbSDimitry Andric return createStringError( 211*5ffd83dbSDimitry Andric errc::invalid_argument, 212*5ffd83dbSDimitry Andric "file names table was not null terminated before " 213*5ffd83dbSDimitry Andric "the end of the prologue"); 214*5ffd83dbSDimitry Andric } 2150b57cec5SDimitry Andric FileNames.push_back(FileEntry); 2160b57cec5SDimitry Andric } 2170b57cec5SDimitry Andric 218*5ffd83dbSDimitry Andric return Error::success(); 2190b57cec5SDimitry Andric } 2200b57cec5SDimitry Andric 2210b57cec5SDimitry Andric // Parse v5 directory/file entry content descriptions. 2228bcb0991SDimitry Andric // Returns the descriptors, or an error if we did not find a path or ran off 2238bcb0991SDimitry Andric // the end of the prologue. 2248bcb0991SDimitry Andric static llvm::Expected<ContentDescriptors> 2258bcb0991SDimitry Andric parseV5EntryFormat(const DWARFDataExtractor &DebugLineData, uint64_t *OffsetPtr, 2268bcb0991SDimitry Andric DWARFDebugLine::ContentTypeTracker *ContentTypes) { 227*5ffd83dbSDimitry Andric Error Err = Error::success(); 2280b57cec5SDimitry Andric ContentDescriptors Descriptors; 229*5ffd83dbSDimitry Andric int FormatCount = DebugLineData.getU8(OffsetPtr, &Err); 2300b57cec5SDimitry Andric bool HasPath = false; 231*5ffd83dbSDimitry Andric for (int I = 0; I != FormatCount && !Err; ++I) { 2320b57cec5SDimitry Andric ContentDescriptor Descriptor; 2330b57cec5SDimitry Andric Descriptor.Type = 234*5ffd83dbSDimitry Andric dwarf::LineNumberEntryFormat(DebugLineData.getULEB128(OffsetPtr, &Err)); 235*5ffd83dbSDimitry Andric Descriptor.Form = dwarf::Form(DebugLineData.getULEB128(OffsetPtr, &Err)); 2360b57cec5SDimitry Andric if (Descriptor.Type == dwarf::DW_LNCT_path) 2370b57cec5SDimitry Andric HasPath = true; 2380b57cec5SDimitry Andric if (ContentTypes) 2390b57cec5SDimitry Andric ContentTypes->trackContentType(Descriptor.Type); 2400b57cec5SDimitry Andric Descriptors.push_back(Descriptor); 2410b57cec5SDimitry Andric } 2428bcb0991SDimitry Andric 243*5ffd83dbSDimitry Andric if (Err) 244*5ffd83dbSDimitry Andric return createStringError(errc::invalid_argument, 245*5ffd83dbSDimitry Andric "failed to parse entry content descriptors: %s", 246*5ffd83dbSDimitry Andric toString(std::move(Err)).c_str()); 247*5ffd83dbSDimitry Andric 2488bcb0991SDimitry Andric if (!HasPath) 2498bcb0991SDimitry Andric return createStringError(errc::invalid_argument, 2508bcb0991SDimitry Andric "failed to parse entry content descriptions" 2518bcb0991SDimitry Andric " because no path was found"); 2528bcb0991SDimitry Andric return Descriptors; 2530b57cec5SDimitry Andric } 2540b57cec5SDimitry Andric 2558bcb0991SDimitry Andric static Error 2560b57cec5SDimitry Andric parseV5DirFileTables(const DWARFDataExtractor &DebugLineData, 257480093f4SDimitry Andric uint64_t *OffsetPtr, const dwarf::FormParams &FormParams, 2580b57cec5SDimitry Andric const DWARFContext &Ctx, const DWARFUnit *U, 2590b57cec5SDimitry Andric DWARFDebugLine::ContentTypeTracker &ContentTypes, 2600b57cec5SDimitry Andric std::vector<DWARFFormValue> &IncludeDirectories, 2610b57cec5SDimitry Andric std::vector<DWARFDebugLine::FileNameEntry> &FileNames) { 2620b57cec5SDimitry Andric // Get the directory entry description. 2638bcb0991SDimitry Andric llvm::Expected<ContentDescriptors> DirDescriptors = 264480093f4SDimitry Andric parseV5EntryFormat(DebugLineData, OffsetPtr, nullptr); 2658bcb0991SDimitry Andric if (!DirDescriptors) 2668bcb0991SDimitry Andric return DirDescriptors.takeError(); 2670b57cec5SDimitry Andric 2680b57cec5SDimitry Andric // Get the directory entries, according to the format described above. 269*5ffd83dbSDimitry Andric uint64_t DirEntryCount = DebugLineData.getULEB128(OffsetPtr); 270*5ffd83dbSDimitry Andric for (uint64_t I = 0; I != DirEntryCount; ++I) { 2718bcb0991SDimitry Andric for (auto Descriptor : *DirDescriptors) { 2720b57cec5SDimitry Andric DWARFFormValue Value(Descriptor.Form); 2730b57cec5SDimitry Andric switch (Descriptor.Type) { 2740b57cec5SDimitry Andric case DW_LNCT_path: 2750b57cec5SDimitry Andric if (!Value.extractValue(DebugLineData, OffsetPtr, FormParams, &Ctx, U)) 2768bcb0991SDimitry Andric return createStringError(errc::invalid_argument, 2778bcb0991SDimitry Andric "failed to parse directory entry because " 278*5ffd83dbSDimitry Andric "extracting the form value failed"); 2790b57cec5SDimitry Andric IncludeDirectories.push_back(Value); 2800b57cec5SDimitry Andric break; 2810b57cec5SDimitry Andric default: 2820b57cec5SDimitry Andric if (!Value.skipValue(DebugLineData, OffsetPtr, FormParams)) 2838bcb0991SDimitry Andric return createStringError(errc::invalid_argument, 2848bcb0991SDimitry Andric "failed to parse directory entry because " 285*5ffd83dbSDimitry Andric "skipping the form value failed"); 2860b57cec5SDimitry Andric } 2870b57cec5SDimitry Andric } 2880b57cec5SDimitry Andric } 2890b57cec5SDimitry Andric 2900b57cec5SDimitry Andric // Get the file entry description. 291480093f4SDimitry Andric llvm::Expected<ContentDescriptors> FileDescriptors = 292480093f4SDimitry Andric parseV5EntryFormat(DebugLineData, OffsetPtr, &ContentTypes); 2938bcb0991SDimitry Andric if (!FileDescriptors) 2948bcb0991SDimitry Andric return FileDescriptors.takeError(); 2950b57cec5SDimitry Andric 2960b57cec5SDimitry Andric // Get the file entries, according to the format described above. 297*5ffd83dbSDimitry Andric uint64_t FileEntryCount = DebugLineData.getULEB128(OffsetPtr); 298*5ffd83dbSDimitry Andric for (uint64_t I = 0; I != FileEntryCount; ++I) { 2990b57cec5SDimitry Andric DWARFDebugLine::FileNameEntry FileEntry; 3008bcb0991SDimitry Andric for (auto Descriptor : *FileDescriptors) { 3010b57cec5SDimitry Andric DWARFFormValue Value(Descriptor.Form); 3020b57cec5SDimitry Andric if (!Value.extractValue(DebugLineData, OffsetPtr, FormParams, &Ctx, U)) 3038bcb0991SDimitry Andric return createStringError(errc::invalid_argument, 3048bcb0991SDimitry Andric "failed to parse file entry because " 305*5ffd83dbSDimitry Andric "extracting the form value failed"); 3060b57cec5SDimitry Andric switch (Descriptor.Type) { 3070b57cec5SDimitry Andric case DW_LNCT_path: 3080b57cec5SDimitry Andric FileEntry.Name = Value; 3090b57cec5SDimitry Andric break; 3100b57cec5SDimitry Andric case DW_LNCT_LLVM_source: 3110b57cec5SDimitry Andric FileEntry.Source = Value; 3120b57cec5SDimitry Andric break; 3130b57cec5SDimitry Andric case DW_LNCT_directory_index: 3140b57cec5SDimitry Andric FileEntry.DirIdx = Value.getAsUnsignedConstant().getValue(); 3150b57cec5SDimitry Andric break; 3160b57cec5SDimitry Andric case DW_LNCT_timestamp: 3170b57cec5SDimitry Andric FileEntry.ModTime = Value.getAsUnsignedConstant().getValue(); 3180b57cec5SDimitry Andric break; 3190b57cec5SDimitry Andric case DW_LNCT_size: 3200b57cec5SDimitry Andric FileEntry.Length = Value.getAsUnsignedConstant().getValue(); 3210b57cec5SDimitry Andric break; 3220b57cec5SDimitry Andric case DW_LNCT_MD5: 3238bcb0991SDimitry Andric if (!Value.getAsBlock() || Value.getAsBlock().getValue().size() != 16) 3248bcb0991SDimitry Andric return createStringError( 3258bcb0991SDimitry Andric errc::invalid_argument, 3268bcb0991SDimitry Andric "failed to parse file entry because the MD5 hash is invalid"); 3270b57cec5SDimitry Andric std::uninitialized_copy_n(Value.getAsBlock().getValue().begin(), 16, 3280b57cec5SDimitry Andric FileEntry.Checksum.Bytes.begin()); 3290b57cec5SDimitry Andric break; 3300b57cec5SDimitry Andric default: 3310b57cec5SDimitry Andric break; 3320b57cec5SDimitry Andric } 3330b57cec5SDimitry Andric } 3340b57cec5SDimitry Andric FileNames.push_back(FileEntry); 3350b57cec5SDimitry Andric } 3368bcb0991SDimitry Andric return Error::success(); 3370b57cec5SDimitry Andric } 3380b57cec5SDimitry Andric 339*5ffd83dbSDimitry Andric uint64_t DWARFDebugLine::Prologue::getLength() const { 340*5ffd83dbSDimitry Andric uint64_t Length = PrologueLength + sizeofTotalLength() + 341*5ffd83dbSDimitry Andric sizeof(getVersion()) + sizeofPrologueLength(); 342*5ffd83dbSDimitry Andric if (getVersion() >= 5) 343*5ffd83dbSDimitry Andric Length += 2; // Address + Segment selector sizes. 344*5ffd83dbSDimitry Andric return Length; 345*5ffd83dbSDimitry Andric } 346*5ffd83dbSDimitry Andric 347*5ffd83dbSDimitry Andric Error DWARFDebugLine::Prologue::parse( 348*5ffd83dbSDimitry Andric DWARFDataExtractor DebugLineData, uint64_t *OffsetPtr, 349*5ffd83dbSDimitry Andric function_ref<void(Error)> RecoverableErrorHandler, const DWARFContext &Ctx, 3500b57cec5SDimitry Andric const DWARFUnit *U) { 3510b57cec5SDimitry Andric const uint64_t PrologueOffset = *OffsetPtr; 3520b57cec5SDimitry Andric 3530b57cec5SDimitry Andric clear(); 354*5ffd83dbSDimitry Andric DataExtractor::Cursor Cursor(*OffsetPtr); 355*5ffd83dbSDimitry Andric std::tie(TotalLength, FormParams.Format) = 356*5ffd83dbSDimitry Andric DebugLineData.getInitialLength(Cursor); 357*5ffd83dbSDimitry Andric 358*5ffd83dbSDimitry Andric DebugLineData = 359*5ffd83dbSDimitry Andric DWARFDataExtractor(DebugLineData, Cursor.tell() + TotalLength); 360*5ffd83dbSDimitry Andric FormParams.Version = DebugLineData.getU16(Cursor); 361*5ffd83dbSDimitry Andric if (Cursor && !versionIsSupported(getVersion())) { 362*5ffd83dbSDimitry Andric // Treat this error as unrecoverable - we cannot be sure what any of 363*5ffd83dbSDimitry Andric // the data represents including the length field, so cannot skip it or make 364*5ffd83dbSDimitry Andric // any reasonable assumptions. 365*5ffd83dbSDimitry Andric *OffsetPtr = Cursor.tell(); 366*5ffd83dbSDimitry Andric return createStringError( 367*5ffd83dbSDimitry Andric errc::not_supported, 3680b57cec5SDimitry Andric "parsing line table prologue at offset 0x%8.8" PRIx64 369*5ffd83dbSDimitry Andric ": unsupported version %" PRIu16, 3700b57cec5SDimitry Andric PrologueOffset, getVersion()); 371*5ffd83dbSDimitry Andric } 3720b57cec5SDimitry Andric 3730b57cec5SDimitry Andric if (getVersion() >= 5) { 374*5ffd83dbSDimitry Andric FormParams.AddrSize = DebugLineData.getU8(Cursor); 375*5ffd83dbSDimitry Andric assert((!Cursor || DebugLineData.getAddressSize() == 0 || 3760b57cec5SDimitry Andric DebugLineData.getAddressSize() == getAddressSize()) && 3770b57cec5SDimitry Andric "Line table header and data extractor disagree"); 378*5ffd83dbSDimitry Andric SegSelectorSize = DebugLineData.getU8(Cursor); 3790b57cec5SDimitry Andric } 3800b57cec5SDimitry Andric 3810b57cec5SDimitry Andric PrologueLength = 382*5ffd83dbSDimitry Andric DebugLineData.getRelocatedValue(Cursor, sizeofPrologueLength()); 383*5ffd83dbSDimitry Andric const uint64_t EndPrologueOffset = PrologueLength + Cursor.tell(); 384*5ffd83dbSDimitry Andric DebugLineData = DWARFDataExtractor(DebugLineData, EndPrologueOffset); 385*5ffd83dbSDimitry Andric MinInstLength = DebugLineData.getU8(Cursor); 3860b57cec5SDimitry Andric if (getVersion() >= 4) 387*5ffd83dbSDimitry Andric MaxOpsPerInst = DebugLineData.getU8(Cursor); 388*5ffd83dbSDimitry Andric DefaultIsStmt = DebugLineData.getU8(Cursor); 389*5ffd83dbSDimitry Andric LineBase = DebugLineData.getU8(Cursor); 390*5ffd83dbSDimitry Andric LineRange = DebugLineData.getU8(Cursor); 391*5ffd83dbSDimitry Andric OpcodeBase = DebugLineData.getU8(Cursor); 3920b57cec5SDimitry Andric 393*5ffd83dbSDimitry Andric if (Cursor && OpcodeBase == 0) { 394*5ffd83dbSDimitry Andric // If the opcode base is 0, we cannot read the standard opcode lengths (of 395*5ffd83dbSDimitry Andric // which there are supposed to be one fewer than the opcode base). Assume 396*5ffd83dbSDimitry Andric // there are no standard opcodes and continue parsing. 397*5ffd83dbSDimitry Andric RecoverableErrorHandler(createStringError( 398*5ffd83dbSDimitry Andric errc::invalid_argument, 399*5ffd83dbSDimitry Andric "parsing line table prologue at offset 0x%8.8" PRIx64 400*5ffd83dbSDimitry Andric " found opcode base of 0. Assuming no standard opcodes", 401*5ffd83dbSDimitry Andric PrologueOffset)); 402*5ffd83dbSDimitry Andric } else if (Cursor) { 4030b57cec5SDimitry Andric StandardOpcodeLengths.reserve(OpcodeBase - 1); 4040b57cec5SDimitry Andric for (uint32_t I = 1; I < OpcodeBase; ++I) { 405*5ffd83dbSDimitry Andric uint8_t OpLen = DebugLineData.getU8(Cursor); 4060b57cec5SDimitry Andric StandardOpcodeLengths.push_back(OpLen); 4070b57cec5SDimitry Andric } 408*5ffd83dbSDimitry Andric } 4090b57cec5SDimitry Andric 410*5ffd83dbSDimitry Andric *OffsetPtr = Cursor.tell(); 411*5ffd83dbSDimitry Andric // A corrupt file name or directory table does not prevent interpretation of 412*5ffd83dbSDimitry Andric // the main line program, so check the cursor state now so that its errors can 413*5ffd83dbSDimitry Andric // be handled separately. 414*5ffd83dbSDimitry Andric if (!Cursor) 415*5ffd83dbSDimitry Andric return createStringError( 416*5ffd83dbSDimitry Andric errc::invalid_argument, 417*5ffd83dbSDimitry Andric "parsing line table prologue at offset 0x%8.8" PRIx64 ": %s", 418*5ffd83dbSDimitry Andric PrologueOffset, toString(Cursor.takeError()).c_str()); 419*5ffd83dbSDimitry Andric 420*5ffd83dbSDimitry Andric Error E = 421*5ffd83dbSDimitry Andric getVersion() >= 5 422*5ffd83dbSDimitry Andric ? parseV5DirFileTables(DebugLineData, OffsetPtr, FormParams, Ctx, U, 423*5ffd83dbSDimitry Andric ContentTypes, IncludeDirectories, FileNames) 424*5ffd83dbSDimitry Andric : parseV2DirFileTables(DebugLineData, OffsetPtr, ContentTypes, 425*5ffd83dbSDimitry Andric IncludeDirectories, FileNames); 426*5ffd83dbSDimitry Andric if (E) { 427*5ffd83dbSDimitry Andric RecoverableErrorHandler(joinErrors( 4288bcb0991SDimitry Andric createStringError( 4298bcb0991SDimitry Andric errc::invalid_argument, 4300b57cec5SDimitry Andric "parsing line table prologue at 0x%8.8" PRIx64 4310b57cec5SDimitry Andric " found an invalid directory or file table description at" 4320b57cec5SDimitry Andric " 0x%8.8" PRIx64, 4338bcb0991SDimitry Andric PrologueOffset, *OffsetPtr), 434*5ffd83dbSDimitry Andric std::move(E))); 435*5ffd83dbSDimitry Andric return Error::success(); 4360b57cec5SDimitry Andric } 4370b57cec5SDimitry Andric 438*5ffd83dbSDimitry Andric assert(*OffsetPtr <= EndPrologueOffset); 439*5ffd83dbSDimitry Andric if (*OffsetPtr != EndPrologueOffset) { 440*5ffd83dbSDimitry Andric RecoverableErrorHandler(createStringError( 441*5ffd83dbSDimitry Andric errc::invalid_argument, 442*5ffd83dbSDimitry Andric "unknown data in line table prologue at offset 0x%8.8" PRIx64 443*5ffd83dbSDimitry Andric ": parsing ended (at offset 0x%8.8" PRIx64 444*5ffd83dbSDimitry Andric ") before reaching the prologue end at offset 0x%8.8" PRIx64, 445*5ffd83dbSDimitry Andric PrologueOffset, *OffsetPtr, EndPrologueOffset)); 446*5ffd83dbSDimitry Andric } 4470b57cec5SDimitry Andric return Error::success(); 4480b57cec5SDimitry Andric } 4490b57cec5SDimitry Andric 4500b57cec5SDimitry Andric DWARFDebugLine::Row::Row(bool DefaultIsStmt) { reset(DefaultIsStmt); } 4510b57cec5SDimitry Andric 4520b57cec5SDimitry Andric void DWARFDebugLine::Row::postAppend() { 4530b57cec5SDimitry Andric Discriminator = 0; 4540b57cec5SDimitry Andric BasicBlock = false; 4550b57cec5SDimitry Andric PrologueEnd = false; 4560b57cec5SDimitry Andric EpilogueBegin = false; 4570b57cec5SDimitry Andric } 4580b57cec5SDimitry Andric 4590b57cec5SDimitry Andric void DWARFDebugLine::Row::reset(bool DefaultIsStmt) { 4600b57cec5SDimitry Andric Address.Address = 0; 4610b57cec5SDimitry Andric Address.SectionIndex = object::SectionedAddress::UndefSection; 4620b57cec5SDimitry Andric Line = 1; 4630b57cec5SDimitry Andric Column = 0; 4640b57cec5SDimitry Andric File = 1; 4650b57cec5SDimitry Andric Isa = 0; 4660b57cec5SDimitry Andric Discriminator = 0; 4670b57cec5SDimitry Andric IsStmt = DefaultIsStmt; 4680b57cec5SDimitry Andric BasicBlock = false; 4690b57cec5SDimitry Andric EndSequence = false; 4700b57cec5SDimitry Andric PrologueEnd = false; 4710b57cec5SDimitry Andric EpilogueBegin = false; 4720b57cec5SDimitry Andric } 4730b57cec5SDimitry Andric 474*5ffd83dbSDimitry Andric void DWARFDebugLine::Row::dumpTableHeader(raw_ostream &OS, unsigned Indent) { 475*5ffd83dbSDimitry Andric OS.indent(Indent) 476*5ffd83dbSDimitry Andric << "Address Line Column File ISA Discriminator Flags\n"; 477*5ffd83dbSDimitry Andric OS.indent(Indent) 4780b57cec5SDimitry Andric << "------------------ ------ ------ ------ --- ------------- " 4790b57cec5SDimitry Andric "-------------\n"; 4800b57cec5SDimitry Andric } 4810b57cec5SDimitry Andric 4820b57cec5SDimitry Andric void DWARFDebugLine::Row::dump(raw_ostream &OS) const { 4830b57cec5SDimitry Andric OS << format("0x%16.16" PRIx64 " %6u %6u", Address.Address, Line, Column) 4840b57cec5SDimitry Andric << format(" %6u %3u %13u ", File, Isa, Discriminator) 4850b57cec5SDimitry Andric << (IsStmt ? " is_stmt" : "") << (BasicBlock ? " basic_block" : "") 4860b57cec5SDimitry Andric << (PrologueEnd ? " prologue_end" : "") 4870b57cec5SDimitry Andric << (EpilogueBegin ? " epilogue_begin" : "") 4880b57cec5SDimitry Andric << (EndSequence ? " end_sequence" : "") << '\n'; 4890b57cec5SDimitry Andric } 4900b57cec5SDimitry Andric 4910b57cec5SDimitry Andric DWARFDebugLine::Sequence::Sequence() { reset(); } 4920b57cec5SDimitry Andric 4930b57cec5SDimitry Andric void DWARFDebugLine::Sequence::reset() { 4940b57cec5SDimitry Andric LowPC = 0; 4950b57cec5SDimitry Andric HighPC = 0; 4960b57cec5SDimitry Andric SectionIndex = object::SectionedAddress::UndefSection; 4970b57cec5SDimitry Andric FirstRowIndex = 0; 4980b57cec5SDimitry Andric LastRowIndex = 0; 4990b57cec5SDimitry Andric Empty = true; 5000b57cec5SDimitry Andric } 5010b57cec5SDimitry Andric 5020b57cec5SDimitry Andric DWARFDebugLine::LineTable::LineTable() { clear(); } 5030b57cec5SDimitry Andric 5040b57cec5SDimitry Andric void DWARFDebugLine::LineTable::dump(raw_ostream &OS, 5050b57cec5SDimitry Andric DIDumpOptions DumpOptions) const { 5060b57cec5SDimitry Andric Prologue.dump(OS, DumpOptions); 5070b57cec5SDimitry Andric 5080b57cec5SDimitry Andric if (!Rows.empty()) { 509480093f4SDimitry Andric OS << '\n'; 510*5ffd83dbSDimitry Andric Row::dumpTableHeader(OS, 0); 5110b57cec5SDimitry Andric for (const Row &R : Rows) { 5120b57cec5SDimitry Andric R.dump(OS); 5130b57cec5SDimitry Andric } 5140b57cec5SDimitry Andric } 515480093f4SDimitry Andric 516480093f4SDimitry Andric // Terminate the table with a final blank line to clearly delineate it from 517480093f4SDimitry Andric // later dumps. 518480093f4SDimitry Andric OS << '\n'; 5190b57cec5SDimitry Andric } 5200b57cec5SDimitry Andric 5210b57cec5SDimitry Andric void DWARFDebugLine::LineTable::clear() { 5220b57cec5SDimitry Andric Prologue.clear(); 5230b57cec5SDimitry Andric Rows.clear(); 5240b57cec5SDimitry Andric Sequences.clear(); 5250b57cec5SDimitry Andric } 5260b57cec5SDimitry Andric 527*5ffd83dbSDimitry Andric DWARFDebugLine::ParsingState::ParsingState( 528*5ffd83dbSDimitry Andric struct LineTable *LT, uint64_t TableOffset, 529*5ffd83dbSDimitry Andric function_ref<void(Error)> ErrorHandler) 530*5ffd83dbSDimitry Andric : LineTable(LT), LineTableOffset(TableOffset), ErrorHandler(ErrorHandler) { 5310b57cec5SDimitry Andric resetRowAndSequence(); 5320b57cec5SDimitry Andric } 5330b57cec5SDimitry Andric 5340b57cec5SDimitry Andric void DWARFDebugLine::ParsingState::resetRowAndSequence() { 5350b57cec5SDimitry Andric Row.reset(LineTable->Prologue.DefaultIsStmt); 5360b57cec5SDimitry Andric Sequence.reset(); 5370b57cec5SDimitry Andric } 5380b57cec5SDimitry Andric 5390b57cec5SDimitry Andric void DWARFDebugLine::ParsingState::appendRowToMatrix() { 5400b57cec5SDimitry Andric unsigned RowNumber = LineTable->Rows.size(); 5410b57cec5SDimitry Andric if (Sequence.Empty) { 5420b57cec5SDimitry Andric // Record the beginning of instruction sequence. 5430b57cec5SDimitry Andric Sequence.Empty = false; 5440b57cec5SDimitry Andric Sequence.LowPC = Row.Address.Address; 5450b57cec5SDimitry Andric Sequence.FirstRowIndex = RowNumber; 5460b57cec5SDimitry Andric } 5470b57cec5SDimitry Andric LineTable->appendRow(Row); 5480b57cec5SDimitry Andric if (Row.EndSequence) { 5490b57cec5SDimitry Andric // Record the end of instruction sequence. 5500b57cec5SDimitry Andric Sequence.HighPC = Row.Address.Address; 5510b57cec5SDimitry Andric Sequence.LastRowIndex = RowNumber + 1; 5520b57cec5SDimitry Andric Sequence.SectionIndex = Row.Address.SectionIndex; 5530b57cec5SDimitry Andric if (Sequence.isValid()) 5540b57cec5SDimitry Andric LineTable->appendSequence(Sequence); 5550b57cec5SDimitry Andric Sequence.reset(); 5560b57cec5SDimitry Andric } 5570b57cec5SDimitry Andric Row.postAppend(); 5580b57cec5SDimitry Andric } 5590b57cec5SDimitry Andric 5600b57cec5SDimitry Andric const DWARFDebugLine::LineTable * 5618bcb0991SDimitry Andric DWARFDebugLine::getLineTable(uint64_t Offset) const { 5620b57cec5SDimitry Andric LineTableConstIter Pos = LineTableMap.find(Offset); 5630b57cec5SDimitry Andric if (Pos != LineTableMap.end()) 5640b57cec5SDimitry Andric return &Pos->second; 5650b57cec5SDimitry Andric return nullptr; 5660b57cec5SDimitry Andric } 5670b57cec5SDimitry Andric 5680b57cec5SDimitry Andric Expected<const DWARFDebugLine::LineTable *> DWARFDebugLine::getOrParseLineTable( 5698bcb0991SDimitry Andric DWARFDataExtractor &DebugLineData, uint64_t Offset, const DWARFContext &Ctx, 570*5ffd83dbSDimitry Andric const DWARFUnit *U, function_ref<void(Error)> RecoverableErrorHandler) { 5710b57cec5SDimitry Andric if (!DebugLineData.isValidOffset(Offset)) 5728bcb0991SDimitry Andric return createStringError(errc::invalid_argument, "offset 0x%8.8" PRIx64 5730b57cec5SDimitry Andric " is not a valid debug line section offset", 5740b57cec5SDimitry Andric Offset); 5750b57cec5SDimitry Andric 5760b57cec5SDimitry Andric std::pair<LineTableIter, bool> Pos = 5770b57cec5SDimitry Andric LineTableMap.insert(LineTableMapTy::value_type(Offset, LineTable())); 5780b57cec5SDimitry Andric LineTable *LT = &Pos.first->second; 5790b57cec5SDimitry Andric if (Pos.second) { 5800b57cec5SDimitry Andric if (Error Err = 581*5ffd83dbSDimitry Andric LT->parse(DebugLineData, &Offset, Ctx, U, RecoverableErrorHandler)) 5820b57cec5SDimitry Andric return std::move(Err); 5830b57cec5SDimitry Andric return LT; 5840b57cec5SDimitry Andric } 5850b57cec5SDimitry Andric return LT; 5860b57cec5SDimitry Andric } 5870b57cec5SDimitry Andric 588*5ffd83dbSDimitry Andric static StringRef getOpcodeName(uint8_t Opcode, uint8_t OpcodeBase) { 589*5ffd83dbSDimitry Andric assert(Opcode != 0); 590*5ffd83dbSDimitry Andric if (Opcode < OpcodeBase) 591*5ffd83dbSDimitry Andric return LNStandardString(Opcode); 592*5ffd83dbSDimitry Andric return "special"; 5930b57cec5SDimitry Andric } 5940b57cec5SDimitry Andric 595*5ffd83dbSDimitry Andric uint64_t DWARFDebugLine::ParsingState::advanceAddr(uint64_t OperationAdvance, 596*5ffd83dbSDimitry Andric uint8_t Opcode, 597*5ffd83dbSDimitry Andric uint64_t OpcodeOffset) { 598*5ffd83dbSDimitry Andric StringRef OpcodeName = getOpcodeName(Opcode, LineTable->Prologue.OpcodeBase); 599*5ffd83dbSDimitry Andric // For versions less than 4, the MaxOpsPerInst member is set to 0, as the 600*5ffd83dbSDimitry Andric // maximum_operations_per_instruction field wasn't introduced until DWARFv4. 601*5ffd83dbSDimitry Andric // Don't warn about bad values in this situation. 602*5ffd83dbSDimitry Andric if (ReportAdvanceAddrProblem && LineTable->Prologue.getVersion() >= 4 && 603*5ffd83dbSDimitry Andric LineTable->Prologue.MaxOpsPerInst != 1) 604*5ffd83dbSDimitry Andric ErrorHandler(createStringError( 605*5ffd83dbSDimitry Andric errc::not_supported, 606*5ffd83dbSDimitry Andric "line table program at offset 0x%8.8" PRIx64 607*5ffd83dbSDimitry Andric " contains a %s opcode at offset 0x%8.8" PRIx64 608*5ffd83dbSDimitry Andric ", but the prologue maximum_operations_per_instruction value is %" PRId8 609*5ffd83dbSDimitry Andric ", which is unsupported. Assuming a value of 1 instead", 610*5ffd83dbSDimitry Andric LineTableOffset, OpcodeName.data(), OpcodeOffset, 611*5ffd83dbSDimitry Andric LineTable->Prologue.MaxOpsPerInst)); 612*5ffd83dbSDimitry Andric if (ReportAdvanceAddrProblem && LineTable->Prologue.MinInstLength == 0) 613*5ffd83dbSDimitry Andric ErrorHandler( 614480093f4SDimitry Andric createStringError(errc::invalid_argument, 615*5ffd83dbSDimitry Andric "line table program at offset 0x%8.8" PRIx64 616*5ffd83dbSDimitry Andric " contains a %s opcode at offset 0x%8.8" PRIx64 617*5ffd83dbSDimitry Andric ", but the prologue minimum_instruction_length value " 618*5ffd83dbSDimitry Andric "is 0, which prevents any address advancing", 619*5ffd83dbSDimitry Andric LineTableOffset, OpcodeName.data(), OpcodeOffset)); 620*5ffd83dbSDimitry Andric ReportAdvanceAddrProblem = false; 621*5ffd83dbSDimitry Andric uint64_t AddrOffset = OperationAdvance * LineTable->Prologue.MinInstLength; 622*5ffd83dbSDimitry Andric Row.Address.Address += AddrOffset; 623*5ffd83dbSDimitry Andric return AddrOffset; 624480093f4SDimitry Andric } 625480093f4SDimitry Andric 626*5ffd83dbSDimitry Andric DWARFDebugLine::ParsingState::AddrAndAdjustedOpcode 627*5ffd83dbSDimitry Andric DWARFDebugLine::ParsingState::advanceAddrForOpcode(uint8_t Opcode, 628*5ffd83dbSDimitry Andric uint64_t OpcodeOffset) { 629*5ffd83dbSDimitry Andric assert(Opcode == DW_LNS_const_add_pc || 630*5ffd83dbSDimitry Andric Opcode >= LineTable->Prologue.OpcodeBase); 631*5ffd83dbSDimitry Andric if (ReportBadLineRange && LineTable->Prologue.LineRange == 0) { 632*5ffd83dbSDimitry Andric StringRef OpcodeName = 633*5ffd83dbSDimitry Andric getOpcodeName(Opcode, LineTable->Prologue.OpcodeBase); 634*5ffd83dbSDimitry Andric ErrorHandler( 635*5ffd83dbSDimitry Andric createStringError(errc::not_supported, 636*5ffd83dbSDimitry Andric "line table program at offset 0x%8.8" PRIx64 637*5ffd83dbSDimitry Andric " contains a %s opcode at offset 0x%8.8" PRIx64 638*5ffd83dbSDimitry Andric ", but the prologue line_range value is 0. The " 639*5ffd83dbSDimitry Andric "address and line will not be adjusted", 640*5ffd83dbSDimitry Andric LineTableOffset, OpcodeName.data(), OpcodeOffset)); 641*5ffd83dbSDimitry Andric ReportBadLineRange = false; 6420b57cec5SDimitry Andric } 6430b57cec5SDimitry Andric 644*5ffd83dbSDimitry Andric uint8_t OpcodeValue = Opcode; 645*5ffd83dbSDimitry Andric if (Opcode == DW_LNS_const_add_pc) 646*5ffd83dbSDimitry Andric OpcodeValue = 255; 647*5ffd83dbSDimitry Andric uint8_t AdjustedOpcode = OpcodeValue - LineTable->Prologue.OpcodeBase; 648*5ffd83dbSDimitry Andric uint64_t OperationAdvance = 649*5ffd83dbSDimitry Andric LineTable->Prologue.LineRange != 0 650*5ffd83dbSDimitry Andric ? AdjustedOpcode / LineTable->Prologue.LineRange 651*5ffd83dbSDimitry Andric : 0; 652*5ffd83dbSDimitry Andric uint64_t AddrOffset = advanceAddr(OperationAdvance, Opcode, OpcodeOffset); 653*5ffd83dbSDimitry Andric return {AddrOffset, AdjustedOpcode}; 6540b57cec5SDimitry Andric } 6550b57cec5SDimitry Andric 656*5ffd83dbSDimitry Andric DWARFDebugLine::ParsingState::AddrAndLineDelta 657*5ffd83dbSDimitry Andric DWARFDebugLine::ParsingState::handleSpecialOpcode(uint8_t Opcode, 658*5ffd83dbSDimitry Andric uint64_t OpcodeOffset) { 6590b57cec5SDimitry Andric // A special opcode value is chosen based on the amount that needs 6600b57cec5SDimitry Andric // to be added to the line and address registers. The maximum line 6610b57cec5SDimitry Andric // increment for a special opcode is the value of the line_base 6620b57cec5SDimitry Andric // field in the header, plus the value of the line_range field, 6630b57cec5SDimitry Andric // minus 1 (line base + line range - 1). If the desired line 6640b57cec5SDimitry Andric // increment is greater than the maximum line increment, a standard 6650b57cec5SDimitry Andric // opcode must be used instead of a special opcode. The "address 6660b57cec5SDimitry Andric // advance" is calculated by dividing the desired address increment 6670b57cec5SDimitry Andric // by the minimum_instruction_length field from the header. The 6680b57cec5SDimitry Andric // special opcode is then calculated using the following formula: 6690b57cec5SDimitry Andric // 6700b57cec5SDimitry Andric // opcode = (desired line increment - line_base) + 6710b57cec5SDimitry Andric // (line_range * address advance) + opcode_base 6720b57cec5SDimitry Andric // 6730b57cec5SDimitry Andric // If the resulting opcode is greater than 255, a standard opcode 6740b57cec5SDimitry Andric // must be used instead. 6750b57cec5SDimitry Andric // 6760b57cec5SDimitry Andric // To decode a special opcode, subtract the opcode_base from the 6770b57cec5SDimitry Andric // opcode itself to give the adjusted opcode. The amount to 6780b57cec5SDimitry Andric // increment the address register is the result of the adjusted 6790b57cec5SDimitry Andric // opcode divided by the line_range multiplied by the 6800b57cec5SDimitry Andric // minimum_instruction_length field from the header. That is: 6810b57cec5SDimitry Andric // 6820b57cec5SDimitry Andric // address increment = (adjusted opcode / line_range) * 6830b57cec5SDimitry Andric // minimum_instruction_length 6840b57cec5SDimitry Andric // 6850b57cec5SDimitry Andric // The amount to increment the line register is the line_base plus 6860b57cec5SDimitry Andric // the result of the adjusted opcode modulo the line_range. That is: 6870b57cec5SDimitry Andric // 6880b57cec5SDimitry Andric // line increment = line_base + (adjusted opcode % line_range) 6890b57cec5SDimitry Andric 690*5ffd83dbSDimitry Andric DWARFDebugLine::ParsingState::AddrAndAdjustedOpcode AddrAdvanceResult = 691*5ffd83dbSDimitry Andric advanceAddrForOpcode(Opcode, OpcodeOffset); 692*5ffd83dbSDimitry Andric int32_t LineOffset = 0; 693*5ffd83dbSDimitry Andric if (LineTable->Prologue.LineRange != 0) 694*5ffd83dbSDimitry Andric LineOffset = 695*5ffd83dbSDimitry Andric LineTable->Prologue.LineBase + 696*5ffd83dbSDimitry Andric (AddrAdvanceResult.AdjustedOpcode % LineTable->Prologue.LineRange); 697*5ffd83dbSDimitry Andric Row.Line += LineOffset; 698*5ffd83dbSDimitry Andric return {AddrAdvanceResult.AddrDelta, LineOffset}; 699*5ffd83dbSDimitry Andric } 700*5ffd83dbSDimitry Andric 701*5ffd83dbSDimitry Andric /// Parse a ULEB128 using the specified \p Cursor. \returns the parsed value on 702*5ffd83dbSDimitry Andric /// success, or None if \p Cursor is in a failing state. 703*5ffd83dbSDimitry Andric template <typename T> 704*5ffd83dbSDimitry Andric static Optional<T> parseULEB128(DWARFDataExtractor &Data, 705*5ffd83dbSDimitry Andric DataExtractor::Cursor &Cursor) { 706*5ffd83dbSDimitry Andric T Value = Data.getULEB128(Cursor); 707*5ffd83dbSDimitry Andric if (Cursor) 708*5ffd83dbSDimitry Andric return Value; 709*5ffd83dbSDimitry Andric return None; 710*5ffd83dbSDimitry Andric } 711*5ffd83dbSDimitry Andric 712*5ffd83dbSDimitry Andric Error DWARFDebugLine::LineTable::parse( 713*5ffd83dbSDimitry Andric DWARFDataExtractor &DebugLineData, uint64_t *OffsetPtr, 714*5ffd83dbSDimitry Andric const DWARFContext &Ctx, const DWARFUnit *U, 715*5ffd83dbSDimitry Andric function_ref<void(Error)> RecoverableErrorHandler, raw_ostream *OS, 716*5ffd83dbSDimitry Andric bool Verbose) { 717*5ffd83dbSDimitry Andric assert((OS || !Verbose) && "cannot have verbose output without stream"); 718*5ffd83dbSDimitry Andric const uint64_t DebugLineOffset = *OffsetPtr; 719*5ffd83dbSDimitry Andric 720*5ffd83dbSDimitry Andric clear(); 721*5ffd83dbSDimitry Andric 722*5ffd83dbSDimitry Andric Error PrologueErr = 723*5ffd83dbSDimitry Andric Prologue.parse(DebugLineData, OffsetPtr, RecoverableErrorHandler, Ctx, U); 7240b57cec5SDimitry Andric 7250b57cec5SDimitry Andric if (OS) { 726*5ffd83dbSDimitry Andric DIDumpOptions DumpOptions; 727*5ffd83dbSDimitry Andric DumpOptions.Verbose = Verbose; 728*5ffd83dbSDimitry Andric Prologue.dump(*OS, DumpOptions); 7290b57cec5SDimitry Andric } 7300b57cec5SDimitry Andric 731*5ffd83dbSDimitry Andric if (PrologueErr) { 732*5ffd83dbSDimitry Andric // Ensure there is a blank line after the prologue to clearly delineate it 733*5ffd83dbSDimitry Andric // from later dumps. 7340b57cec5SDimitry Andric if (OS) 7350b57cec5SDimitry Andric *OS << "\n"; 736*5ffd83dbSDimitry Andric return PrologueErr; 737*5ffd83dbSDimitry Andric } 738*5ffd83dbSDimitry Andric 739*5ffd83dbSDimitry Andric uint64_t ProgramLength = Prologue.TotalLength + Prologue.sizeofTotalLength(); 740*5ffd83dbSDimitry Andric if (!DebugLineData.isValidOffsetForDataOfSize(DebugLineOffset, 741*5ffd83dbSDimitry Andric ProgramLength)) { 742*5ffd83dbSDimitry Andric assert(DebugLineData.size() > DebugLineOffset && 743*5ffd83dbSDimitry Andric "prologue parsing should handle invalid offset"); 744*5ffd83dbSDimitry Andric uint64_t BytesRemaining = DebugLineData.size() - DebugLineOffset; 745*5ffd83dbSDimitry Andric RecoverableErrorHandler( 746*5ffd83dbSDimitry Andric createStringError(errc::invalid_argument, 747*5ffd83dbSDimitry Andric "line table program with offset 0x%8.8" PRIx64 748*5ffd83dbSDimitry Andric " has length 0x%8.8" PRIx64 " but only 0x%8.8" PRIx64 749*5ffd83dbSDimitry Andric " bytes are available", 750*5ffd83dbSDimitry Andric DebugLineOffset, ProgramLength, BytesRemaining)); 751*5ffd83dbSDimitry Andric // Continue by capping the length at the number of remaining bytes. 752*5ffd83dbSDimitry Andric ProgramLength = BytesRemaining; 753*5ffd83dbSDimitry Andric } 754*5ffd83dbSDimitry Andric 755*5ffd83dbSDimitry Andric // Create a DataExtractor which can only see the data up to the end of the 756*5ffd83dbSDimitry Andric // table, to prevent reading past the end. 757*5ffd83dbSDimitry Andric const uint64_t EndOffset = DebugLineOffset + ProgramLength; 758*5ffd83dbSDimitry Andric DWARFDataExtractor TableData(DebugLineData, EndOffset); 759*5ffd83dbSDimitry Andric 760*5ffd83dbSDimitry Andric // See if we should tell the data extractor the address size. 761*5ffd83dbSDimitry Andric if (TableData.getAddressSize() == 0) 762*5ffd83dbSDimitry Andric TableData.setAddressSize(Prologue.getAddressSize()); 763*5ffd83dbSDimitry Andric else 764*5ffd83dbSDimitry Andric assert(Prologue.getAddressSize() == 0 || 765*5ffd83dbSDimitry Andric Prologue.getAddressSize() == TableData.getAddressSize()); 766*5ffd83dbSDimitry Andric 767*5ffd83dbSDimitry Andric ParsingState State(this, DebugLineOffset, RecoverableErrorHandler); 768*5ffd83dbSDimitry Andric 769*5ffd83dbSDimitry Andric *OffsetPtr = DebugLineOffset + Prologue.getLength(); 770*5ffd83dbSDimitry Andric if (OS && *OffsetPtr < EndOffset) { 771*5ffd83dbSDimitry Andric *OS << '\n'; 772*5ffd83dbSDimitry Andric Row::dumpTableHeader(*OS, /*Indent=*/Verbose ? 12 : 0); 773*5ffd83dbSDimitry Andric } 774*5ffd83dbSDimitry Andric while (*OffsetPtr < EndOffset) { 775*5ffd83dbSDimitry Andric DataExtractor::Cursor Cursor(*OffsetPtr); 776*5ffd83dbSDimitry Andric 777*5ffd83dbSDimitry Andric if (Verbose) 778*5ffd83dbSDimitry Andric *OS << format("0x%08.08" PRIx64 ": ", *OffsetPtr); 779*5ffd83dbSDimitry Andric 780*5ffd83dbSDimitry Andric uint64_t OpcodeOffset = *OffsetPtr; 781*5ffd83dbSDimitry Andric uint8_t Opcode = TableData.getU8(Cursor); 782*5ffd83dbSDimitry Andric size_t RowCount = Rows.size(); 783*5ffd83dbSDimitry Andric 784*5ffd83dbSDimitry Andric if (Cursor && Verbose) 785*5ffd83dbSDimitry Andric *OS << format("%02.02" PRIx8 " ", Opcode); 786*5ffd83dbSDimitry Andric 787*5ffd83dbSDimitry Andric if (Opcode == 0) { 788*5ffd83dbSDimitry Andric // Extended Opcodes always start with a zero opcode followed by 789*5ffd83dbSDimitry Andric // a uleb128 length so you can skip ones you don't know about 790*5ffd83dbSDimitry Andric uint64_t Len = TableData.getULEB128(Cursor); 791*5ffd83dbSDimitry Andric uint64_t ExtOffset = Cursor.tell(); 792*5ffd83dbSDimitry Andric 793*5ffd83dbSDimitry Andric // Tolerate zero-length; assume length is correct and soldier on. 794*5ffd83dbSDimitry Andric if (Len == 0) { 795*5ffd83dbSDimitry Andric if (Cursor && Verbose) 796*5ffd83dbSDimitry Andric *OS << "Badly formed extended line op (length 0)\n"; 797*5ffd83dbSDimitry Andric if (!Cursor) { 798*5ffd83dbSDimitry Andric if (Verbose) 799*5ffd83dbSDimitry Andric *OS << "\n"; 800*5ffd83dbSDimitry Andric RecoverableErrorHandler(Cursor.takeError()); 801*5ffd83dbSDimitry Andric } 802*5ffd83dbSDimitry Andric *OffsetPtr = Cursor.tell(); 803*5ffd83dbSDimitry Andric continue; 804*5ffd83dbSDimitry Andric } 805*5ffd83dbSDimitry Andric 806*5ffd83dbSDimitry Andric uint8_t SubOpcode = TableData.getU8(Cursor); 807*5ffd83dbSDimitry Andric // OperandOffset will be the same as ExtOffset, if it was not possible to 808*5ffd83dbSDimitry Andric // read the SubOpcode. 809*5ffd83dbSDimitry Andric uint64_t OperandOffset = Cursor.tell(); 810*5ffd83dbSDimitry Andric if (Verbose) 811*5ffd83dbSDimitry Andric *OS << LNExtendedString(SubOpcode); 812*5ffd83dbSDimitry Andric switch (SubOpcode) { 813*5ffd83dbSDimitry Andric case DW_LNE_end_sequence: 814*5ffd83dbSDimitry Andric // Set the end_sequence register of the state machine to true and 815*5ffd83dbSDimitry Andric // append a row to the matrix using the current values of the 816*5ffd83dbSDimitry Andric // state-machine registers. Then reset the registers to the initial 817*5ffd83dbSDimitry Andric // values specified above. Every statement program sequence must end 818*5ffd83dbSDimitry Andric // with a DW_LNE_end_sequence instruction which creates a row whose 819*5ffd83dbSDimitry Andric // address is that of the byte after the last target machine instruction 820*5ffd83dbSDimitry Andric // of the sequence. 821*5ffd83dbSDimitry Andric State.Row.EndSequence = true; 822*5ffd83dbSDimitry Andric // No need to test the Cursor is valid here, since it must be to get 823*5ffd83dbSDimitry Andric // into this code path - if it were invalid, the default case would be 824*5ffd83dbSDimitry Andric // followed. 825*5ffd83dbSDimitry Andric if (Verbose) { 826*5ffd83dbSDimitry Andric *OS << "\n"; 827*5ffd83dbSDimitry Andric OS->indent(12); 828*5ffd83dbSDimitry Andric } 829*5ffd83dbSDimitry Andric if (OS) 830*5ffd83dbSDimitry Andric State.Row.dump(*OS); 831*5ffd83dbSDimitry Andric State.appendRowToMatrix(); 832*5ffd83dbSDimitry Andric State.resetRowAndSequence(); 833*5ffd83dbSDimitry Andric break; 834*5ffd83dbSDimitry Andric 835*5ffd83dbSDimitry Andric case DW_LNE_set_address: 836*5ffd83dbSDimitry Andric // Takes a single relocatable address as an operand. The size of the 837*5ffd83dbSDimitry Andric // operand is the size appropriate to hold an address on the target 838*5ffd83dbSDimitry Andric // machine. Set the address register to the value given by the 839*5ffd83dbSDimitry Andric // relocatable address. All of the other statement program opcodes 840*5ffd83dbSDimitry Andric // that affect the address register add a delta to it. This instruction 841*5ffd83dbSDimitry Andric // stores a relocatable value into it instead. 842*5ffd83dbSDimitry Andric // 843*5ffd83dbSDimitry Andric // Make sure the extractor knows the address size. If not, infer it 844*5ffd83dbSDimitry Andric // from the size of the operand. 845*5ffd83dbSDimitry Andric { 846*5ffd83dbSDimitry Andric uint8_t ExtractorAddressSize = TableData.getAddressSize(); 847*5ffd83dbSDimitry Andric uint64_t OpcodeAddressSize = Len - 1; 848*5ffd83dbSDimitry Andric if (ExtractorAddressSize != OpcodeAddressSize && 849*5ffd83dbSDimitry Andric ExtractorAddressSize != 0) 850*5ffd83dbSDimitry Andric RecoverableErrorHandler(createStringError( 851*5ffd83dbSDimitry Andric errc::invalid_argument, 852*5ffd83dbSDimitry Andric "mismatching address size at offset 0x%8.8" PRIx64 853*5ffd83dbSDimitry Andric " expected 0x%2.2" PRIx8 " found 0x%2.2" PRIx64, 854*5ffd83dbSDimitry Andric ExtOffset, ExtractorAddressSize, Len - 1)); 855*5ffd83dbSDimitry Andric 856*5ffd83dbSDimitry Andric // Assume that the line table is correct and temporarily override the 857*5ffd83dbSDimitry Andric // address size. If the size is unsupported, give up trying to read 858*5ffd83dbSDimitry Andric // the address and continue to the next opcode. 859*5ffd83dbSDimitry Andric if (OpcodeAddressSize != 1 && OpcodeAddressSize != 2 && 860*5ffd83dbSDimitry Andric OpcodeAddressSize != 4 && OpcodeAddressSize != 8) { 861*5ffd83dbSDimitry Andric RecoverableErrorHandler(createStringError( 862*5ffd83dbSDimitry Andric errc::invalid_argument, 863*5ffd83dbSDimitry Andric "address size 0x%2.2" PRIx64 864*5ffd83dbSDimitry Andric " of DW_LNE_set_address opcode at offset 0x%8.8" PRIx64 865*5ffd83dbSDimitry Andric " is unsupported", 866*5ffd83dbSDimitry Andric OpcodeAddressSize, ExtOffset)); 867*5ffd83dbSDimitry Andric TableData.skip(Cursor, OpcodeAddressSize); 868*5ffd83dbSDimitry Andric } else { 869*5ffd83dbSDimitry Andric TableData.setAddressSize(OpcodeAddressSize); 870*5ffd83dbSDimitry Andric State.Row.Address.Address = TableData.getRelocatedAddress( 871*5ffd83dbSDimitry Andric Cursor, &State.Row.Address.SectionIndex); 872*5ffd83dbSDimitry Andric 873*5ffd83dbSDimitry Andric // Restore the address size if the extractor already had it. 874*5ffd83dbSDimitry Andric if (ExtractorAddressSize != 0) 875*5ffd83dbSDimitry Andric TableData.setAddressSize(ExtractorAddressSize); 876*5ffd83dbSDimitry Andric } 877*5ffd83dbSDimitry Andric 878*5ffd83dbSDimitry Andric if (Cursor && Verbose) 879*5ffd83dbSDimitry Andric *OS << format(" (0x%16.16" PRIx64 ")", State.Row.Address.Address); 880*5ffd83dbSDimitry Andric } 881*5ffd83dbSDimitry Andric break; 882*5ffd83dbSDimitry Andric 883*5ffd83dbSDimitry Andric case DW_LNE_define_file: 884*5ffd83dbSDimitry Andric // Takes 4 arguments. The first is a null terminated string containing 885*5ffd83dbSDimitry Andric // a source file name. The second is an unsigned LEB128 number 886*5ffd83dbSDimitry Andric // representing the directory index of the directory in which the file 887*5ffd83dbSDimitry Andric // was found. The third is an unsigned LEB128 number representing the 888*5ffd83dbSDimitry Andric // time of last modification of the file. The fourth is an unsigned 889*5ffd83dbSDimitry Andric // LEB128 number representing the length in bytes of the file. The time 890*5ffd83dbSDimitry Andric // and length fields may contain LEB128(0) if the information is not 891*5ffd83dbSDimitry Andric // available. 892*5ffd83dbSDimitry Andric // 893*5ffd83dbSDimitry Andric // The directory index represents an entry in the include_directories 894*5ffd83dbSDimitry Andric // section of the statement program prologue. The index is LEB128(0) 895*5ffd83dbSDimitry Andric // if the file was found in the current directory of the compilation, 896*5ffd83dbSDimitry Andric // LEB128(1) if it was found in the first directory in the 897*5ffd83dbSDimitry Andric // include_directories section, and so on. The directory index is 898*5ffd83dbSDimitry Andric // ignored for file names that represent full path names. 899*5ffd83dbSDimitry Andric // 900*5ffd83dbSDimitry Andric // The files are numbered, starting at 1, in the order in which they 901*5ffd83dbSDimitry Andric // appear; the names in the prologue come before names defined by 902*5ffd83dbSDimitry Andric // the DW_LNE_define_file instruction. These numbers are used in the 903*5ffd83dbSDimitry Andric // the file register of the state machine. 904*5ffd83dbSDimitry Andric { 905*5ffd83dbSDimitry Andric FileNameEntry FileEntry; 906*5ffd83dbSDimitry Andric const char *Name = TableData.getCStr(Cursor); 907*5ffd83dbSDimitry Andric FileEntry.Name = 908*5ffd83dbSDimitry Andric DWARFFormValue::createFromPValue(dwarf::DW_FORM_string, Name); 909*5ffd83dbSDimitry Andric FileEntry.DirIdx = TableData.getULEB128(Cursor); 910*5ffd83dbSDimitry Andric FileEntry.ModTime = TableData.getULEB128(Cursor); 911*5ffd83dbSDimitry Andric FileEntry.Length = TableData.getULEB128(Cursor); 912*5ffd83dbSDimitry Andric Prologue.FileNames.push_back(FileEntry); 913*5ffd83dbSDimitry Andric if (Cursor && Verbose) 914*5ffd83dbSDimitry Andric *OS << " (" << Name << ", dir=" << FileEntry.DirIdx << ", mod_time=" 915*5ffd83dbSDimitry Andric << format("(0x%16.16" PRIx64 ")", FileEntry.ModTime) 916*5ffd83dbSDimitry Andric << ", length=" << FileEntry.Length << ")"; 917*5ffd83dbSDimitry Andric } 918*5ffd83dbSDimitry Andric break; 919*5ffd83dbSDimitry Andric 920*5ffd83dbSDimitry Andric case DW_LNE_set_discriminator: 921*5ffd83dbSDimitry Andric State.Row.Discriminator = TableData.getULEB128(Cursor); 922*5ffd83dbSDimitry Andric if (Cursor && Verbose) 923*5ffd83dbSDimitry Andric *OS << " (" << State.Row.Discriminator << ")"; 924*5ffd83dbSDimitry Andric break; 925*5ffd83dbSDimitry Andric 926*5ffd83dbSDimitry Andric default: 927*5ffd83dbSDimitry Andric if (Cursor && Verbose) 928*5ffd83dbSDimitry Andric *OS << format("Unrecognized extended op 0x%02.02" PRIx8, SubOpcode) 929*5ffd83dbSDimitry Andric << format(" length %" PRIx64, Len); 930*5ffd83dbSDimitry Andric // Len doesn't include the zero opcode byte or the length itself, but 931*5ffd83dbSDimitry Andric // it does include the sub_opcode, so we have to adjust for that. 932*5ffd83dbSDimitry Andric TableData.skip(Cursor, Len - 1); 933*5ffd83dbSDimitry Andric break; 934*5ffd83dbSDimitry Andric } 935*5ffd83dbSDimitry Andric // Make sure the length as recorded in the table and the standard length 936*5ffd83dbSDimitry Andric // for the opcode match. If they don't, continue from the end as claimed 937*5ffd83dbSDimitry Andric // by the table. Similarly, continue from the claimed end in the event of 938*5ffd83dbSDimitry Andric // a parsing error. 939*5ffd83dbSDimitry Andric uint64_t End = ExtOffset + Len; 940*5ffd83dbSDimitry Andric if (Cursor && Cursor.tell() != End) 941*5ffd83dbSDimitry Andric RecoverableErrorHandler(createStringError( 942*5ffd83dbSDimitry Andric errc::illegal_byte_sequence, 943*5ffd83dbSDimitry Andric "unexpected line op length at offset 0x%8.8" PRIx64 944*5ffd83dbSDimitry Andric " expected 0x%2.2" PRIx64 " found 0x%2.2" PRIx64, 945*5ffd83dbSDimitry Andric ExtOffset, Len, Cursor.tell() - ExtOffset)); 946*5ffd83dbSDimitry Andric if (!Cursor && Verbose) { 947*5ffd83dbSDimitry Andric DWARFDataExtractor::Cursor ByteCursor(OperandOffset); 948*5ffd83dbSDimitry Andric uint8_t Byte = TableData.getU8(ByteCursor); 949*5ffd83dbSDimitry Andric if (ByteCursor) { 950*5ffd83dbSDimitry Andric *OS << " (<parsing error>"; 951*5ffd83dbSDimitry Andric do { 952*5ffd83dbSDimitry Andric *OS << format(" %2.2" PRIx8, Byte); 953*5ffd83dbSDimitry Andric Byte = TableData.getU8(ByteCursor); 954*5ffd83dbSDimitry Andric } while (ByteCursor); 955*5ffd83dbSDimitry Andric *OS << ")"; 956*5ffd83dbSDimitry Andric } 957*5ffd83dbSDimitry Andric 958*5ffd83dbSDimitry Andric // The only parse failure in this case should be if the end was reached. 959*5ffd83dbSDimitry Andric // In that case, throw away the error, as the main Cursor's error will 960*5ffd83dbSDimitry Andric // be sufficient. 961*5ffd83dbSDimitry Andric consumeError(ByteCursor.takeError()); 962*5ffd83dbSDimitry Andric } 963*5ffd83dbSDimitry Andric *OffsetPtr = End; 964*5ffd83dbSDimitry Andric } else if (Opcode < Prologue.OpcodeBase) { 965*5ffd83dbSDimitry Andric if (Verbose) 966*5ffd83dbSDimitry Andric *OS << LNStandardString(Opcode); 967*5ffd83dbSDimitry Andric switch (Opcode) { 968*5ffd83dbSDimitry Andric // Standard Opcodes 969*5ffd83dbSDimitry Andric case DW_LNS_copy: 970*5ffd83dbSDimitry Andric // Takes no arguments. Append a row to the matrix using the 971*5ffd83dbSDimitry Andric // current values of the state-machine registers. 972*5ffd83dbSDimitry Andric if (Verbose) { 973*5ffd83dbSDimitry Andric *OS << "\n"; 974*5ffd83dbSDimitry Andric OS->indent(12); 975*5ffd83dbSDimitry Andric } 976*5ffd83dbSDimitry Andric if (OS) 977*5ffd83dbSDimitry Andric State.Row.dump(*OS); 978*5ffd83dbSDimitry Andric State.appendRowToMatrix(); 979*5ffd83dbSDimitry Andric break; 980*5ffd83dbSDimitry Andric 981*5ffd83dbSDimitry Andric case DW_LNS_advance_pc: 982*5ffd83dbSDimitry Andric // Takes a single unsigned LEB128 operand, multiplies it by the 983*5ffd83dbSDimitry Andric // min_inst_length field of the prologue, and adds the 984*5ffd83dbSDimitry Andric // result to the address register of the state machine. 985*5ffd83dbSDimitry Andric if (Optional<uint64_t> Operand = 986*5ffd83dbSDimitry Andric parseULEB128<uint64_t>(TableData, Cursor)) { 987*5ffd83dbSDimitry Andric uint64_t AddrOffset = 988*5ffd83dbSDimitry Andric State.advanceAddr(*Operand, Opcode, OpcodeOffset); 989*5ffd83dbSDimitry Andric if (Verbose) 990*5ffd83dbSDimitry Andric *OS << " (" << AddrOffset << ")"; 991*5ffd83dbSDimitry Andric } 992*5ffd83dbSDimitry Andric break; 993*5ffd83dbSDimitry Andric 994*5ffd83dbSDimitry Andric case DW_LNS_advance_line: 995*5ffd83dbSDimitry Andric // Takes a single signed LEB128 operand and adds that value to 996*5ffd83dbSDimitry Andric // the line register of the state machine. 997*5ffd83dbSDimitry Andric { 998*5ffd83dbSDimitry Andric int64_t LineDelta = TableData.getSLEB128(Cursor); 999*5ffd83dbSDimitry Andric if (Cursor) { 1000*5ffd83dbSDimitry Andric State.Row.Line += LineDelta; 1001*5ffd83dbSDimitry Andric if (Verbose) 1002*5ffd83dbSDimitry Andric *OS << " (" << State.Row.Line << ")"; 1003*5ffd83dbSDimitry Andric } 1004*5ffd83dbSDimitry Andric } 1005*5ffd83dbSDimitry Andric break; 1006*5ffd83dbSDimitry Andric 1007*5ffd83dbSDimitry Andric case DW_LNS_set_file: 1008*5ffd83dbSDimitry Andric // Takes a single unsigned LEB128 operand and stores it in the file 1009*5ffd83dbSDimitry Andric // register of the state machine. 1010*5ffd83dbSDimitry Andric if (Optional<uint16_t> File = 1011*5ffd83dbSDimitry Andric parseULEB128<uint16_t>(TableData, Cursor)) { 1012*5ffd83dbSDimitry Andric State.Row.File = *File; 1013*5ffd83dbSDimitry Andric if (Verbose) 1014*5ffd83dbSDimitry Andric *OS << " (" << State.Row.File << ")"; 1015*5ffd83dbSDimitry Andric } 1016*5ffd83dbSDimitry Andric break; 1017*5ffd83dbSDimitry Andric 1018*5ffd83dbSDimitry Andric case DW_LNS_set_column: 1019*5ffd83dbSDimitry Andric // Takes a single unsigned LEB128 operand and stores it in the 1020*5ffd83dbSDimitry Andric // column register of the state machine. 1021*5ffd83dbSDimitry Andric if (Optional<uint16_t> Column = 1022*5ffd83dbSDimitry Andric parseULEB128<uint16_t>(TableData, Cursor)) { 1023*5ffd83dbSDimitry Andric State.Row.Column = *Column; 1024*5ffd83dbSDimitry Andric if (Verbose) 1025*5ffd83dbSDimitry Andric *OS << " (" << State.Row.Column << ")"; 1026*5ffd83dbSDimitry Andric } 1027*5ffd83dbSDimitry Andric break; 1028*5ffd83dbSDimitry Andric 1029*5ffd83dbSDimitry Andric case DW_LNS_negate_stmt: 1030*5ffd83dbSDimitry Andric // Takes no arguments. Set the is_stmt register of the state 1031*5ffd83dbSDimitry Andric // machine to the logical negation of its current value. 1032*5ffd83dbSDimitry Andric State.Row.IsStmt = !State.Row.IsStmt; 1033*5ffd83dbSDimitry Andric break; 1034*5ffd83dbSDimitry Andric 1035*5ffd83dbSDimitry Andric case DW_LNS_set_basic_block: 1036*5ffd83dbSDimitry Andric // Takes no arguments. Set the basic_block register of the 1037*5ffd83dbSDimitry Andric // state machine to true 1038*5ffd83dbSDimitry Andric State.Row.BasicBlock = true; 1039*5ffd83dbSDimitry Andric break; 1040*5ffd83dbSDimitry Andric 1041*5ffd83dbSDimitry Andric case DW_LNS_const_add_pc: 1042*5ffd83dbSDimitry Andric // Takes no arguments. Add to the address register of the state 1043*5ffd83dbSDimitry Andric // machine the address increment value corresponding to special 1044*5ffd83dbSDimitry Andric // opcode 255. The motivation for DW_LNS_const_add_pc is this: 1045*5ffd83dbSDimitry Andric // when the statement program needs to advance the address by a 1046*5ffd83dbSDimitry Andric // small amount, it can use a single special opcode, which occupies 1047*5ffd83dbSDimitry Andric // a single byte. When it needs to advance the address by up to 1048*5ffd83dbSDimitry Andric // twice the range of the last special opcode, it can use 1049*5ffd83dbSDimitry Andric // DW_LNS_const_add_pc followed by a special opcode, for a total 1050*5ffd83dbSDimitry Andric // of two bytes. Only if it needs to advance the address by more 1051*5ffd83dbSDimitry Andric // than twice that range will it need to use both DW_LNS_advance_pc 1052*5ffd83dbSDimitry Andric // and a special opcode, requiring three or more bytes. 1053*5ffd83dbSDimitry Andric { 1054*5ffd83dbSDimitry Andric uint64_t AddrOffset = 1055*5ffd83dbSDimitry Andric State.advanceAddrForOpcode(Opcode, OpcodeOffset).AddrDelta; 1056*5ffd83dbSDimitry Andric if (Verbose) 1057*5ffd83dbSDimitry Andric *OS << format(" (0x%16.16" PRIx64 ")", AddrOffset); 1058*5ffd83dbSDimitry Andric } 1059*5ffd83dbSDimitry Andric break; 1060*5ffd83dbSDimitry Andric 1061*5ffd83dbSDimitry Andric case DW_LNS_fixed_advance_pc: 1062*5ffd83dbSDimitry Andric // Takes a single uhalf operand. Add to the address register of 1063*5ffd83dbSDimitry Andric // the state machine the value of the (unencoded) operand. This 1064*5ffd83dbSDimitry Andric // is the only extended opcode that takes an argument that is not 1065*5ffd83dbSDimitry Andric // a variable length number. The motivation for DW_LNS_fixed_advance_pc 1066*5ffd83dbSDimitry Andric // is this: existing assemblers cannot emit DW_LNS_advance_pc or 1067*5ffd83dbSDimitry Andric // special opcodes because they cannot encode LEB128 numbers or 1068*5ffd83dbSDimitry Andric // judge when the computation of a special opcode overflows and 1069*5ffd83dbSDimitry Andric // requires the use of DW_LNS_advance_pc. Such assemblers, however, 1070*5ffd83dbSDimitry Andric // can use DW_LNS_fixed_advance_pc instead, sacrificing compression. 1071*5ffd83dbSDimitry Andric { 1072*5ffd83dbSDimitry Andric uint16_t PCOffset = 1073*5ffd83dbSDimitry Andric TableData.getRelocatedValue(Cursor, 2); 1074*5ffd83dbSDimitry Andric if (Cursor) { 1075*5ffd83dbSDimitry Andric State.Row.Address.Address += PCOffset; 1076*5ffd83dbSDimitry Andric if (Verbose) 1077*5ffd83dbSDimitry Andric *OS << format(" (0x%4.4" PRIx16 ")", PCOffset); 1078*5ffd83dbSDimitry Andric } 1079*5ffd83dbSDimitry Andric } 1080*5ffd83dbSDimitry Andric break; 1081*5ffd83dbSDimitry Andric 1082*5ffd83dbSDimitry Andric case DW_LNS_set_prologue_end: 1083*5ffd83dbSDimitry Andric // Takes no arguments. Set the prologue_end register of the 1084*5ffd83dbSDimitry Andric // state machine to true 1085*5ffd83dbSDimitry Andric State.Row.PrologueEnd = true; 1086*5ffd83dbSDimitry Andric break; 1087*5ffd83dbSDimitry Andric 1088*5ffd83dbSDimitry Andric case DW_LNS_set_epilogue_begin: 1089*5ffd83dbSDimitry Andric // Takes no arguments. Set the basic_block register of the 1090*5ffd83dbSDimitry Andric // state machine to true 1091*5ffd83dbSDimitry Andric State.Row.EpilogueBegin = true; 1092*5ffd83dbSDimitry Andric break; 1093*5ffd83dbSDimitry Andric 1094*5ffd83dbSDimitry Andric case DW_LNS_set_isa: 1095*5ffd83dbSDimitry Andric // Takes a single unsigned LEB128 operand and stores it in the 1096*5ffd83dbSDimitry Andric // ISA register of the state machine. 1097*5ffd83dbSDimitry Andric if (Optional<uint8_t> Isa = parseULEB128<uint8_t>(TableData, Cursor)) { 1098*5ffd83dbSDimitry Andric State.Row.Isa = *Isa; 1099*5ffd83dbSDimitry Andric if (Verbose) 1100*5ffd83dbSDimitry Andric *OS << " (" << (uint64_t)State.Row.Isa << ")"; 1101*5ffd83dbSDimitry Andric } 1102*5ffd83dbSDimitry Andric break; 1103*5ffd83dbSDimitry Andric 1104*5ffd83dbSDimitry Andric default: 1105*5ffd83dbSDimitry Andric // Handle any unknown standard opcodes here. We know the lengths 1106*5ffd83dbSDimitry Andric // of such opcodes because they are specified in the prologue 1107*5ffd83dbSDimitry Andric // as a multiple of LEB128 operands for each opcode. 1108*5ffd83dbSDimitry Andric { 1109*5ffd83dbSDimitry Andric assert(Opcode - 1U < Prologue.StandardOpcodeLengths.size()); 1110*5ffd83dbSDimitry Andric if (Verbose) 1111*5ffd83dbSDimitry Andric *OS << "Unrecognized standard opcode"; 1112*5ffd83dbSDimitry Andric uint8_t OpcodeLength = Prologue.StandardOpcodeLengths[Opcode - 1]; 1113*5ffd83dbSDimitry Andric std::vector<uint64_t> Operands; 1114*5ffd83dbSDimitry Andric for (uint8_t I = 0; I < OpcodeLength; ++I) { 1115*5ffd83dbSDimitry Andric if (Optional<uint64_t> Value = 1116*5ffd83dbSDimitry Andric parseULEB128<uint64_t>(TableData, Cursor)) 1117*5ffd83dbSDimitry Andric Operands.push_back(*Value); 1118*5ffd83dbSDimitry Andric else 1119*5ffd83dbSDimitry Andric break; 1120*5ffd83dbSDimitry Andric } 1121*5ffd83dbSDimitry Andric if (Verbose && !Operands.empty()) { 1122*5ffd83dbSDimitry Andric *OS << " (operands: "; 1123*5ffd83dbSDimitry Andric bool First = true; 1124*5ffd83dbSDimitry Andric for (uint64_t Value : Operands) { 1125*5ffd83dbSDimitry Andric if (!First) 1126*5ffd83dbSDimitry Andric *OS << ", "; 1127*5ffd83dbSDimitry Andric First = false; 1128*5ffd83dbSDimitry Andric *OS << format("0x%16.16" PRIx64, Value); 1129*5ffd83dbSDimitry Andric } 1130*5ffd83dbSDimitry Andric if (Verbose) 1131*5ffd83dbSDimitry Andric *OS << ')'; 1132*5ffd83dbSDimitry Andric } 1133*5ffd83dbSDimitry Andric } 1134*5ffd83dbSDimitry Andric break; 1135*5ffd83dbSDimitry Andric } 1136*5ffd83dbSDimitry Andric 1137*5ffd83dbSDimitry Andric *OffsetPtr = Cursor.tell(); 1138*5ffd83dbSDimitry Andric } else { 1139*5ffd83dbSDimitry Andric // Special Opcodes. 1140*5ffd83dbSDimitry Andric ParsingState::AddrAndLineDelta Delta = 1141*5ffd83dbSDimitry Andric State.handleSpecialOpcode(Opcode, OpcodeOffset); 1142*5ffd83dbSDimitry Andric 1143*5ffd83dbSDimitry Andric if (Verbose) { 1144*5ffd83dbSDimitry Andric *OS << "address += " << Delta.Address << ", line += " << Delta.Line 1145*5ffd83dbSDimitry Andric << "\n"; 1146*5ffd83dbSDimitry Andric OS->indent(12); 1147*5ffd83dbSDimitry Andric } 1148*5ffd83dbSDimitry Andric if (OS) 1149*5ffd83dbSDimitry Andric State.Row.dump(*OS); 1150*5ffd83dbSDimitry Andric 1151*5ffd83dbSDimitry Andric State.appendRowToMatrix(); 1152*5ffd83dbSDimitry Andric *OffsetPtr = Cursor.tell(); 1153*5ffd83dbSDimitry Andric } 1154*5ffd83dbSDimitry Andric 1155*5ffd83dbSDimitry Andric // When a row is added to the matrix, it is also dumped, which includes a 1156*5ffd83dbSDimitry Andric // new line already, so don't add an extra one. 1157*5ffd83dbSDimitry Andric if (Verbose && Rows.size() == RowCount) 1158*5ffd83dbSDimitry Andric *OS << "\n"; 1159*5ffd83dbSDimitry Andric 1160*5ffd83dbSDimitry Andric // Most parse failures other than when parsing extended opcodes are due to 1161*5ffd83dbSDimitry Andric // failures to read ULEBs. Bail out of parsing, since we don't know where to 1162*5ffd83dbSDimitry Andric // continue reading from as there is no stated length for such byte 1163*5ffd83dbSDimitry Andric // sequences. Print the final trailing new line if needed before doing so. 1164*5ffd83dbSDimitry Andric if (!Cursor && Opcode != 0) { 1165*5ffd83dbSDimitry Andric if (Verbose) 1166*5ffd83dbSDimitry Andric *OS << "\n"; 1167*5ffd83dbSDimitry Andric return Cursor.takeError(); 1168*5ffd83dbSDimitry Andric } 1169*5ffd83dbSDimitry Andric 1170*5ffd83dbSDimitry Andric if (!Cursor) 1171*5ffd83dbSDimitry Andric RecoverableErrorHandler(Cursor.takeError()); 11720b57cec5SDimitry Andric } 11730b57cec5SDimitry Andric 11740b57cec5SDimitry Andric if (!State.Sequence.Empty) 1175*5ffd83dbSDimitry Andric RecoverableErrorHandler(createStringError( 1176480093f4SDimitry Andric errc::illegal_byte_sequence, 1177480093f4SDimitry Andric "last sequence in debug line table at offset 0x%8.8" PRIx64 1178480093f4SDimitry Andric " is not terminated", 1179480093f4SDimitry Andric DebugLineOffset)); 11800b57cec5SDimitry Andric 11810b57cec5SDimitry Andric // Sort all sequences so that address lookup will work faster. 11820b57cec5SDimitry Andric if (!Sequences.empty()) { 11830b57cec5SDimitry Andric llvm::sort(Sequences, Sequence::orderByHighPC); 11840b57cec5SDimitry Andric // Note: actually, instruction address ranges of sequences should not 11850b57cec5SDimitry Andric // overlap (in shared objects and executables). If they do, the address 11860b57cec5SDimitry Andric // lookup would still work, though, but result would be ambiguous. 11870b57cec5SDimitry Andric // We don't report warning in this case. For example, 11880b57cec5SDimitry Andric // sometimes .so compiled from multiple object files contains a few 11890b57cec5SDimitry Andric // rudimentary sequences for address ranges [0x0, 0xsomething). 11900b57cec5SDimitry Andric } 11910b57cec5SDimitry Andric 1192*5ffd83dbSDimitry Andric // Terminate the table with a final blank line to clearly delineate it from 1193*5ffd83dbSDimitry Andric // later dumps. 1194*5ffd83dbSDimitry Andric if (OS) 1195*5ffd83dbSDimitry Andric *OS << "\n"; 1196*5ffd83dbSDimitry Andric 11970b57cec5SDimitry Andric return Error::success(); 11980b57cec5SDimitry Andric } 11990b57cec5SDimitry Andric 12000b57cec5SDimitry Andric uint32_t DWARFDebugLine::LineTable::findRowInSeq( 12010b57cec5SDimitry Andric const DWARFDebugLine::Sequence &Seq, 12020b57cec5SDimitry Andric object::SectionedAddress Address) const { 12030b57cec5SDimitry Andric if (!Seq.containsPC(Address)) 12040b57cec5SDimitry Andric return UnknownRowIndex; 12050b57cec5SDimitry Andric assert(Seq.SectionIndex == Address.SectionIndex); 12060b57cec5SDimitry Andric // In some cases, e.g. first instruction in a function, the compiler generates 12070b57cec5SDimitry Andric // two entries, both with the same address. We want the last one. 12080b57cec5SDimitry Andric // 12090b57cec5SDimitry Andric // In general we want a non-empty range: the last row whose address is less 12100b57cec5SDimitry Andric // than or equal to Address. This can be computed as upper_bound - 1. 12110b57cec5SDimitry Andric DWARFDebugLine::Row Row; 12120b57cec5SDimitry Andric Row.Address = Address; 12130b57cec5SDimitry Andric RowIter FirstRow = Rows.begin() + Seq.FirstRowIndex; 12140b57cec5SDimitry Andric RowIter LastRow = Rows.begin() + Seq.LastRowIndex; 12150b57cec5SDimitry Andric assert(FirstRow->Address.Address <= Row.Address.Address && 12160b57cec5SDimitry Andric Row.Address.Address < LastRow[-1].Address.Address); 12170b57cec5SDimitry Andric RowIter RowPos = std::upper_bound(FirstRow + 1, LastRow - 1, Row, 12180b57cec5SDimitry Andric DWARFDebugLine::Row::orderByAddress) - 12190b57cec5SDimitry Andric 1; 12200b57cec5SDimitry Andric assert(Seq.SectionIndex == RowPos->Address.SectionIndex); 12210b57cec5SDimitry Andric return RowPos - Rows.begin(); 12220b57cec5SDimitry Andric } 12230b57cec5SDimitry Andric 12240b57cec5SDimitry Andric uint32_t DWARFDebugLine::LineTable::lookupAddress( 12250b57cec5SDimitry Andric object::SectionedAddress Address) const { 12260b57cec5SDimitry Andric 12270b57cec5SDimitry Andric // Search for relocatable addresses 12280b57cec5SDimitry Andric uint32_t Result = lookupAddressImpl(Address); 12290b57cec5SDimitry Andric 12300b57cec5SDimitry Andric if (Result != UnknownRowIndex || 12310b57cec5SDimitry Andric Address.SectionIndex == object::SectionedAddress::UndefSection) 12320b57cec5SDimitry Andric return Result; 12330b57cec5SDimitry Andric 12340b57cec5SDimitry Andric // Search for absolute addresses 12350b57cec5SDimitry Andric Address.SectionIndex = object::SectionedAddress::UndefSection; 12360b57cec5SDimitry Andric return lookupAddressImpl(Address); 12370b57cec5SDimitry Andric } 12380b57cec5SDimitry Andric 12390b57cec5SDimitry Andric uint32_t DWARFDebugLine::LineTable::lookupAddressImpl( 12400b57cec5SDimitry Andric object::SectionedAddress Address) const { 12410b57cec5SDimitry Andric // First, find an instruction sequence containing the given address. 12420b57cec5SDimitry Andric DWARFDebugLine::Sequence Sequence; 12430b57cec5SDimitry Andric Sequence.SectionIndex = Address.SectionIndex; 12440b57cec5SDimitry Andric Sequence.HighPC = Address.Address; 12450b57cec5SDimitry Andric SequenceIter It = llvm::upper_bound(Sequences, Sequence, 12460b57cec5SDimitry Andric DWARFDebugLine::Sequence::orderByHighPC); 12470b57cec5SDimitry Andric if (It == Sequences.end() || It->SectionIndex != Address.SectionIndex) 12480b57cec5SDimitry Andric return UnknownRowIndex; 12490b57cec5SDimitry Andric return findRowInSeq(*It, Address); 12500b57cec5SDimitry Andric } 12510b57cec5SDimitry Andric 12520b57cec5SDimitry Andric bool DWARFDebugLine::LineTable::lookupAddressRange( 12530b57cec5SDimitry Andric object::SectionedAddress Address, uint64_t Size, 12540b57cec5SDimitry Andric std::vector<uint32_t> &Result) const { 12550b57cec5SDimitry Andric 12560b57cec5SDimitry Andric // Search for relocatable addresses 12570b57cec5SDimitry Andric if (lookupAddressRangeImpl(Address, Size, Result)) 12580b57cec5SDimitry Andric return true; 12590b57cec5SDimitry Andric 12600b57cec5SDimitry Andric if (Address.SectionIndex == object::SectionedAddress::UndefSection) 12610b57cec5SDimitry Andric return false; 12620b57cec5SDimitry Andric 12630b57cec5SDimitry Andric // Search for absolute addresses 12640b57cec5SDimitry Andric Address.SectionIndex = object::SectionedAddress::UndefSection; 12650b57cec5SDimitry Andric return lookupAddressRangeImpl(Address, Size, Result); 12660b57cec5SDimitry Andric } 12670b57cec5SDimitry Andric 12680b57cec5SDimitry Andric bool DWARFDebugLine::LineTable::lookupAddressRangeImpl( 12690b57cec5SDimitry Andric object::SectionedAddress Address, uint64_t Size, 12700b57cec5SDimitry Andric std::vector<uint32_t> &Result) const { 12710b57cec5SDimitry Andric if (Sequences.empty()) 12720b57cec5SDimitry Andric return false; 12730b57cec5SDimitry Andric uint64_t EndAddr = Address.Address + Size; 12740b57cec5SDimitry Andric // First, find an instruction sequence containing the given address. 12750b57cec5SDimitry Andric DWARFDebugLine::Sequence Sequence; 12760b57cec5SDimitry Andric Sequence.SectionIndex = Address.SectionIndex; 12770b57cec5SDimitry Andric Sequence.HighPC = Address.Address; 12780b57cec5SDimitry Andric SequenceIter LastSeq = Sequences.end(); 12790b57cec5SDimitry Andric SequenceIter SeqPos = llvm::upper_bound( 12800b57cec5SDimitry Andric Sequences, Sequence, DWARFDebugLine::Sequence::orderByHighPC); 12810b57cec5SDimitry Andric if (SeqPos == LastSeq || !SeqPos->containsPC(Address)) 12820b57cec5SDimitry Andric return false; 12830b57cec5SDimitry Andric 12840b57cec5SDimitry Andric SequenceIter StartPos = SeqPos; 12850b57cec5SDimitry Andric 12860b57cec5SDimitry Andric // Add the rows from the first sequence to the vector, starting with the 12870b57cec5SDimitry Andric // index we just calculated 12880b57cec5SDimitry Andric 12890b57cec5SDimitry Andric while (SeqPos != LastSeq && SeqPos->LowPC < EndAddr) { 12900b57cec5SDimitry Andric const DWARFDebugLine::Sequence &CurSeq = *SeqPos; 12910b57cec5SDimitry Andric // For the first sequence, we need to find which row in the sequence is the 12920b57cec5SDimitry Andric // first in our range. 12930b57cec5SDimitry Andric uint32_t FirstRowIndex = CurSeq.FirstRowIndex; 12940b57cec5SDimitry Andric if (SeqPos == StartPos) 12950b57cec5SDimitry Andric FirstRowIndex = findRowInSeq(CurSeq, Address); 12960b57cec5SDimitry Andric 12970b57cec5SDimitry Andric // Figure out the last row in the range. 12980b57cec5SDimitry Andric uint32_t LastRowIndex = 12990b57cec5SDimitry Andric findRowInSeq(CurSeq, {EndAddr - 1, Address.SectionIndex}); 13000b57cec5SDimitry Andric if (LastRowIndex == UnknownRowIndex) 13010b57cec5SDimitry Andric LastRowIndex = CurSeq.LastRowIndex - 1; 13020b57cec5SDimitry Andric 13030b57cec5SDimitry Andric assert(FirstRowIndex != UnknownRowIndex); 13040b57cec5SDimitry Andric assert(LastRowIndex != UnknownRowIndex); 13050b57cec5SDimitry Andric 13060b57cec5SDimitry Andric for (uint32_t I = FirstRowIndex; I <= LastRowIndex; ++I) { 13070b57cec5SDimitry Andric Result.push_back(I); 13080b57cec5SDimitry Andric } 13090b57cec5SDimitry Andric 13100b57cec5SDimitry Andric ++SeqPos; 13110b57cec5SDimitry Andric } 13120b57cec5SDimitry Andric 13130b57cec5SDimitry Andric return true; 13140b57cec5SDimitry Andric } 13150b57cec5SDimitry Andric 13160b57cec5SDimitry Andric Optional<StringRef> DWARFDebugLine::LineTable::getSourceByIndex(uint64_t FileIndex, 13170b57cec5SDimitry Andric FileLineInfoKind Kind) const { 13180b57cec5SDimitry Andric if (Kind == FileLineInfoKind::None || !Prologue.hasFileAtIndex(FileIndex)) 13190b57cec5SDimitry Andric return None; 13200b57cec5SDimitry Andric const FileNameEntry &Entry = Prologue.getFileNameEntry(FileIndex); 13210b57cec5SDimitry Andric if (Optional<const char *> source = Entry.Source.getAsCString()) 13220b57cec5SDimitry Andric return StringRef(*source); 13230b57cec5SDimitry Andric return None; 13240b57cec5SDimitry Andric } 13250b57cec5SDimitry Andric 13260b57cec5SDimitry Andric static bool isPathAbsoluteOnWindowsOrPosix(const Twine &Path) { 13270b57cec5SDimitry Andric // Debug info can contain paths from any OS, not necessarily 13280b57cec5SDimitry Andric // an OS we're currently running on. Moreover different compilation units can 13290b57cec5SDimitry Andric // be compiled on different operating systems and linked together later. 13300b57cec5SDimitry Andric return sys::path::is_absolute(Path, sys::path::Style::posix) || 13310b57cec5SDimitry Andric sys::path::is_absolute(Path, sys::path::Style::windows); 13320b57cec5SDimitry Andric } 13330b57cec5SDimitry Andric 13348bcb0991SDimitry Andric bool DWARFDebugLine::Prologue::getFileNameByIndex( 13358bcb0991SDimitry Andric uint64_t FileIndex, StringRef CompDir, FileLineInfoKind Kind, 13368bcb0991SDimitry Andric std::string &Result, sys::path::Style Style) const { 13370b57cec5SDimitry Andric if (Kind == FileLineInfoKind::None || !hasFileAtIndex(FileIndex)) 13380b57cec5SDimitry Andric return false; 13390b57cec5SDimitry Andric const FileNameEntry &Entry = getFileNameEntry(FileIndex); 1340480093f4SDimitry Andric Optional<const char *> Name = Entry.Name.getAsCString(); 1341480093f4SDimitry Andric if (!Name) 1342480093f4SDimitry Andric return false; 1343480093f4SDimitry Andric StringRef FileName = *Name; 1344*5ffd83dbSDimitry Andric if (Kind == FileLineInfoKind::RawValue || 13450b57cec5SDimitry Andric isPathAbsoluteOnWindowsOrPosix(FileName)) { 1346*5ffd83dbSDimitry Andric Result = std::string(FileName); 1347*5ffd83dbSDimitry Andric return true; 1348*5ffd83dbSDimitry Andric } 1349*5ffd83dbSDimitry Andric if (Kind == FileLineInfoKind::BaseNameOnly) { 1350*5ffd83dbSDimitry Andric Result = std::string(llvm::sys::path::filename(FileName)); 13510b57cec5SDimitry Andric return true; 13520b57cec5SDimitry Andric } 13530b57cec5SDimitry Andric 13540b57cec5SDimitry Andric SmallString<16> FilePath; 13550b57cec5SDimitry Andric StringRef IncludeDir; 13560b57cec5SDimitry Andric // Be defensive about the contents of Entry. 13570b57cec5SDimitry Andric if (getVersion() >= 5) { 1358*5ffd83dbSDimitry Andric // DirIdx 0 is the compilation directory, so don't include it for 1359*5ffd83dbSDimitry Andric // relative names. 1360*5ffd83dbSDimitry Andric if ((Entry.DirIdx != 0 || Kind != FileLineInfoKind::RelativeFilePath) && 1361*5ffd83dbSDimitry Andric Entry.DirIdx < IncludeDirectories.size()) 13620b57cec5SDimitry Andric IncludeDir = IncludeDirectories[Entry.DirIdx].getAsCString().getValue(); 13630b57cec5SDimitry Andric } else { 13640b57cec5SDimitry Andric if (0 < Entry.DirIdx && Entry.DirIdx <= IncludeDirectories.size()) 13650b57cec5SDimitry Andric IncludeDir = 13660b57cec5SDimitry Andric IncludeDirectories[Entry.DirIdx - 1].getAsCString().getValue(); 13670b57cec5SDimitry Andric } 13680b57cec5SDimitry Andric 1369*5ffd83dbSDimitry Andric // For absolute paths only, include the compilation directory of compile unit. 1370*5ffd83dbSDimitry Andric // We know that FileName is not absolute, the only way to have an absolute 1371*5ffd83dbSDimitry Andric // path at this point would be if IncludeDir is absolute. 1372*5ffd83dbSDimitry Andric if (Kind == FileLineInfoKind::AbsoluteFilePath && !CompDir.empty() && 1373*5ffd83dbSDimitry Andric !isPathAbsoluteOnWindowsOrPosix(IncludeDir)) 1374*5ffd83dbSDimitry Andric sys::path::append(FilePath, Style, CompDir); 1375*5ffd83dbSDimitry Andric 1376*5ffd83dbSDimitry Andric assert((Kind == FileLineInfoKind::AbsoluteFilePath || 1377*5ffd83dbSDimitry Andric Kind == FileLineInfoKind::RelativeFilePath) && 1378*5ffd83dbSDimitry Andric "invalid FileLineInfo Kind"); 1379*5ffd83dbSDimitry Andric 13800b57cec5SDimitry Andric // sys::path::append skips empty strings. 13818bcb0991SDimitry Andric sys::path::append(FilePath, Style, IncludeDir, FileName); 1382*5ffd83dbSDimitry Andric Result = std::string(FilePath.str()); 13830b57cec5SDimitry Andric return true; 13840b57cec5SDimitry Andric } 13850b57cec5SDimitry Andric 13860b57cec5SDimitry Andric bool DWARFDebugLine::LineTable::getFileLineInfoForAddress( 13870b57cec5SDimitry Andric object::SectionedAddress Address, const char *CompDir, 13880b57cec5SDimitry Andric FileLineInfoKind Kind, DILineInfo &Result) const { 13890b57cec5SDimitry Andric // Get the index of row we're looking for in the line table. 13900b57cec5SDimitry Andric uint32_t RowIndex = lookupAddress(Address); 13910b57cec5SDimitry Andric if (RowIndex == -1U) 13920b57cec5SDimitry Andric return false; 13930b57cec5SDimitry Andric // Take file number and line/column from the row. 13940b57cec5SDimitry Andric const auto &Row = Rows[RowIndex]; 13950b57cec5SDimitry Andric if (!getFileNameByIndex(Row.File, CompDir, Kind, Result.FileName)) 13960b57cec5SDimitry Andric return false; 13970b57cec5SDimitry Andric Result.Line = Row.Line; 13980b57cec5SDimitry Andric Result.Column = Row.Column; 13990b57cec5SDimitry Andric Result.Discriminator = Row.Discriminator; 14000b57cec5SDimitry Andric Result.Source = getSourceByIndex(Row.File, Kind); 14010b57cec5SDimitry Andric return true; 14020b57cec5SDimitry Andric } 14030b57cec5SDimitry Andric 14040b57cec5SDimitry Andric // We want to supply the Unit associated with a .debug_line[.dwo] table when 14050b57cec5SDimitry Andric // we dump it, if possible, but still dump the table even if there isn't a Unit. 14060b57cec5SDimitry Andric // Therefore, collect up handles on all the Units that point into the 14070b57cec5SDimitry Andric // line-table section. 14080b57cec5SDimitry Andric static DWARFDebugLine::SectionParser::LineToUnitMap 14090b57cec5SDimitry Andric buildLineToUnitMap(DWARFDebugLine::SectionParser::cu_range CUs, 14100b57cec5SDimitry Andric DWARFDebugLine::SectionParser::tu_range TUs) { 14110b57cec5SDimitry Andric DWARFDebugLine::SectionParser::LineToUnitMap LineToUnit; 14120b57cec5SDimitry Andric for (const auto &CU : CUs) 14130b57cec5SDimitry Andric if (auto CUDIE = CU->getUnitDIE()) 14140b57cec5SDimitry Andric if (auto StmtOffset = toSectionOffset(CUDIE.find(DW_AT_stmt_list))) 14150b57cec5SDimitry Andric LineToUnit.insert(std::make_pair(*StmtOffset, &*CU)); 14160b57cec5SDimitry Andric for (const auto &TU : TUs) 14170b57cec5SDimitry Andric if (auto TUDIE = TU->getUnitDIE()) 14180b57cec5SDimitry Andric if (auto StmtOffset = toSectionOffset(TUDIE.find(DW_AT_stmt_list))) 14190b57cec5SDimitry Andric LineToUnit.insert(std::make_pair(*StmtOffset, &*TU)); 14200b57cec5SDimitry Andric return LineToUnit; 14210b57cec5SDimitry Andric } 14220b57cec5SDimitry Andric 14230b57cec5SDimitry Andric DWARFDebugLine::SectionParser::SectionParser(DWARFDataExtractor &Data, 14240b57cec5SDimitry Andric const DWARFContext &C, 14250b57cec5SDimitry Andric cu_range CUs, tu_range TUs) 14260b57cec5SDimitry Andric : DebugLineData(Data), Context(C) { 14270b57cec5SDimitry Andric LineToUnit = buildLineToUnitMap(CUs, TUs); 14280b57cec5SDimitry Andric if (!DebugLineData.isValidOffset(Offset)) 14290b57cec5SDimitry Andric Done = true; 14300b57cec5SDimitry Andric } 14310b57cec5SDimitry Andric 14320b57cec5SDimitry Andric bool DWARFDebugLine::Prologue::totalLengthIsValid() const { 1433*5ffd83dbSDimitry Andric return TotalLength != 0u; 14340b57cec5SDimitry Andric } 14350b57cec5SDimitry Andric 14360b57cec5SDimitry Andric DWARFDebugLine::LineTable DWARFDebugLine::SectionParser::parseNext( 1437*5ffd83dbSDimitry Andric function_ref<void(Error)> RecoverableErrorHandler, 1438*5ffd83dbSDimitry Andric function_ref<void(Error)> UnrecoverableErrorHandler, raw_ostream *OS, 1439*5ffd83dbSDimitry Andric bool Verbose) { 14400b57cec5SDimitry Andric assert(DebugLineData.isValidOffset(Offset) && 14410b57cec5SDimitry Andric "parsing should have terminated"); 14420b57cec5SDimitry Andric DWARFUnit *U = prepareToParse(Offset); 14438bcb0991SDimitry Andric uint64_t OldOffset = Offset; 14440b57cec5SDimitry Andric LineTable LT; 14450b57cec5SDimitry Andric if (Error Err = LT.parse(DebugLineData, &Offset, Context, U, 1446*5ffd83dbSDimitry Andric RecoverableErrorHandler, OS, Verbose)) 1447*5ffd83dbSDimitry Andric UnrecoverableErrorHandler(std::move(Err)); 14480b57cec5SDimitry Andric moveToNextTable(OldOffset, LT.Prologue); 14490b57cec5SDimitry Andric return LT; 14500b57cec5SDimitry Andric } 14510b57cec5SDimitry Andric 14520b57cec5SDimitry Andric void DWARFDebugLine::SectionParser::skip( 1453*5ffd83dbSDimitry Andric function_ref<void(Error)> RecoverableErrorHandler, 1454*5ffd83dbSDimitry Andric function_ref<void(Error)> UnrecoverableErrorHandler) { 14550b57cec5SDimitry Andric assert(DebugLineData.isValidOffset(Offset) && 14560b57cec5SDimitry Andric "parsing should have terminated"); 14570b57cec5SDimitry Andric DWARFUnit *U = prepareToParse(Offset); 14588bcb0991SDimitry Andric uint64_t OldOffset = Offset; 14590b57cec5SDimitry Andric LineTable LT; 1460*5ffd83dbSDimitry Andric if (Error Err = LT.Prologue.parse(DebugLineData, &Offset, 1461*5ffd83dbSDimitry Andric RecoverableErrorHandler, Context, U)) 1462*5ffd83dbSDimitry Andric UnrecoverableErrorHandler(std::move(Err)); 14630b57cec5SDimitry Andric moveToNextTable(OldOffset, LT.Prologue); 14640b57cec5SDimitry Andric } 14650b57cec5SDimitry Andric 14668bcb0991SDimitry Andric DWARFUnit *DWARFDebugLine::SectionParser::prepareToParse(uint64_t Offset) { 14670b57cec5SDimitry Andric DWARFUnit *U = nullptr; 14680b57cec5SDimitry Andric auto It = LineToUnit.find(Offset); 14690b57cec5SDimitry Andric if (It != LineToUnit.end()) 14700b57cec5SDimitry Andric U = It->second; 14710b57cec5SDimitry Andric DebugLineData.setAddressSize(U ? U->getAddressByteSize() : 0); 14720b57cec5SDimitry Andric return U; 14730b57cec5SDimitry Andric } 14740b57cec5SDimitry Andric 14758bcb0991SDimitry Andric void DWARFDebugLine::SectionParser::moveToNextTable(uint64_t OldOffset, 14760b57cec5SDimitry Andric const Prologue &P) { 14770b57cec5SDimitry Andric // If the length field is not valid, we don't know where the next table is, so 14780b57cec5SDimitry Andric // cannot continue to parse. Mark the parser as done, and leave the Offset 14790b57cec5SDimitry Andric // value as it currently is. This will be the end of the bad length field. 14800b57cec5SDimitry Andric if (!P.totalLengthIsValid()) { 14810b57cec5SDimitry Andric Done = true; 14820b57cec5SDimitry Andric return; 14830b57cec5SDimitry Andric } 14840b57cec5SDimitry Andric 14850b57cec5SDimitry Andric Offset = OldOffset + P.TotalLength + P.sizeofTotalLength(); 14860b57cec5SDimitry Andric if (!DebugLineData.isValidOffset(Offset)) { 14870b57cec5SDimitry Andric Done = true; 14880b57cec5SDimitry Andric } 14890b57cec5SDimitry Andric } 1490