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