Path: blob/main/contrib/llvm-project/llvm/lib/DebugInfo/DWARF/DWARFDebugLine.cpp
35266 views
//===- DWARFDebugLine.cpp -------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "llvm/DebugInfo/DWARF/DWARFDebugLine.h"9#include "llvm/ADT/SmallString.h"10#include "llvm/ADT/SmallVector.h"11#include "llvm/ADT/StringRef.h"12#include "llvm/BinaryFormat/Dwarf.h"13#include "llvm/DebugInfo/DWARF/DWARFDataExtractor.h"14#include "llvm/DebugInfo/DWARF/DWARFDie.h"15#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"16#include "llvm/Support/Errc.h"17#include "llvm/Support/Format.h"18#include "llvm/Support/FormatVariadic.h"19#include "llvm/Support/raw_ostream.h"20#include <algorithm>21#include <cassert>22#include <cinttypes>23#include <cstdint>24#include <cstdio>25#include <utility>2627using namespace llvm;28using namespace dwarf;2930using FileLineInfoKind = DILineInfoSpecifier::FileLineInfoKind;3132namespace {3334struct ContentDescriptor {35dwarf::LineNumberEntryFormat Type;36dwarf::Form Form;37};3839using ContentDescriptors = SmallVector<ContentDescriptor, 4>;4041} // end anonymous namespace4243static bool versionIsSupported(uint16_t Version) {44return Version >= 2 && Version <= 5;45}4647void DWARFDebugLine::ContentTypeTracker::trackContentType(48dwarf::LineNumberEntryFormat ContentType) {49switch (ContentType) {50case dwarf::DW_LNCT_timestamp:51HasModTime = true;52break;53case dwarf::DW_LNCT_size:54HasLength = true;55break;56case dwarf::DW_LNCT_MD5:57HasMD5 = true;58break;59case dwarf::DW_LNCT_LLVM_source:60HasSource = true;61break;62default:63// We only care about values we consider optional, and new values may be64// added in the vendor extension range, so we do not match exhaustively.65break;66}67}6869DWARFDebugLine::Prologue::Prologue() { clear(); }7071bool DWARFDebugLine::Prologue::hasFileAtIndex(uint64_t FileIndex) const {72uint16_t DwarfVersion = getVersion();73assert(DwarfVersion != 0 &&74"line table prologue has no dwarf version information");75if (DwarfVersion >= 5)76return FileIndex < FileNames.size();77return FileIndex != 0 && FileIndex <= FileNames.size();78}7980std::optional<uint64_t>81DWARFDebugLine::Prologue::getLastValidFileIndex() const {82if (FileNames.empty())83return std::nullopt;84uint16_t DwarfVersion = getVersion();85assert(DwarfVersion != 0 &&86"line table prologue has no dwarf version information");87// In DWARF v5 the file names are 0-indexed.88if (DwarfVersion >= 5)89return FileNames.size() - 1;90return FileNames.size();91}9293const llvm::DWARFDebugLine::FileNameEntry &94DWARFDebugLine::Prologue::getFileNameEntry(uint64_t Index) const {95uint16_t DwarfVersion = getVersion();96assert(DwarfVersion != 0 &&97"line table prologue has no dwarf version information");98// In DWARF v5 the file names are 0-indexed.99if (DwarfVersion >= 5)100return FileNames[Index];101return FileNames[Index - 1];102}103104void DWARFDebugLine::Prologue::clear() {105TotalLength = PrologueLength = 0;106SegSelectorSize = 0;107MinInstLength = MaxOpsPerInst = DefaultIsStmt = LineBase = LineRange = 0;108OpcodeBase = 0;109FormParams = dwarf::FormParams({0, 0, DWARF32});110ContentTypes = ContentTypeTracker();111StandardOpcodeLengths.clear();112IncludeDirectories.clear();113FileNames.clear();114}115116void DWARFDebugLine::Prologue::dump(raw_ostream &OS,117DIDumpOptions DumpOptions) const {118if (!totalLengthIsValid())119return;120int OffsetDumpWidth = 2 * dwarf::getDwarfOffsetByteSize(FormParams.Format);121OS << "Line table prologue:\n"122<< format(" total_length: 0x%0*" PRIx64 "\n", OffsetDumpWidth,123TotalLength)124<< " format: " << dwarf::FormatString(FormParams.Format) << "\n"125<< format(" version: %u\n", getVersion());126if (!versionIsSupported(getVersion()))127return;128if (getVersion() >= 5)129OS << format(" address_size: %u\n", getAddressSize())130<< format(" seg_select_size: %u\n", SegSelectorSize);131OS << format(" prologue_length: 0x%0*" PRIx64 "\n", OffsetDumpWidth,132PrologueLength)133<< format(" min_inst_length: %u\n", MinInstLength)134<< format(getVersion() >= 4 ? "max_ops_per_inst: %u\n" : "", MaxOpsPerInst)135<< format(" default_is_stmt: %u\n", DefaultIsStmt)136<< format(" line_base: %i\n", LineBase)137<< format(" line_range: %u\n", LineRange)138<< format(" opcode_base: %u\n", OpcodeBase);139140for (uint32_t I = 0; I != StandardOpcodeLengths.size(); ++I)141OS << formatv("standard_opcode_lengths[{0}] = {1}\n",142static_cast<dwarf::LineNumberOps>(I + 1),143StandardOpcodeLengths[I]);144145if (!IncludeDirectories.empty()) {146// DWARF v5 starts directory indexes at 0.147uint32_t DirBase = getVersion() >= 5 ? 0 : 1;148for (uint32_t I = 0; I != IncludeDirectories.size(); ++I) {149OS << format("include_directories[%3u] = ", I + DirBase);150IncludeDirectories[I].dump(OS, DumpOptions);151OS << '\n';152}153}154155if (!FileNames.empty()) {156// DWARF v5 starts file indexes at 0.157uint32_t FileBase = getVersion() >= 5 ? 0 : 1;158for (uint32_t I = 0; I != FileNames.size(); ++I) {159const FileNameEntry &FileEntry = FileNames[I];160OS << format("file_names[%3u]:\n", I + FileBase);161OS << " name: ";162FileEntry.Name.dump(OS, DumpOptions);163OS << '\n'164<< format(" dir_index: %" PRIu64 "\n", FileEntry.DirIdx);165if (ContentTypes.HasMD5)166OS << " md5_checksum: " << FileEntry.Checksum.digest() << '\n';167if (ContentTypes.HasModTime)168OS << format(" mod_time: 0x%8.8" PRIx64 "\n", FileEntry.ModTime);169if (ContentTypes.HasLength)170OS << format(" length: 0x%8.8" PRIx64 "\n", FileEntry.Length);171if (ContentTypes.HasSource) {172auto Source = FileEntry.Source.getAsCString();173if (!Source)174consumeError(Source.takeError());175else if ((*Source)[0]) {176OS << " source: ";177FileEntry.Source.dump(OS, DumpOptions);178OS << '\n';179}180}181}182}183}184185// Parse v2-v4 directory and file tables.186static Error187parseV2DirFileTables(const DWARFDataExtractor &DebugLineData,188uint64_t *OffsetPtr,189DWARFDebugLine::ContentTypeTracker &ContentTypes,190std::vector<DWARFFormValue> &IncludeDirectories,191std::vector<DWARFDebugLine::FileNameEntry> &FileNames) {192while (true) {193Error Err = Error::success();194StringRef S = DebugLineData.getCStrRef(OffsetPtr, &Err);195if (Err) {196consumeError(std::move(Err));197return createStringError(errc::invalid_argument,198"include directories table was not null "199"terminated before the end of the prologue");200}201if (S.empty())202break;203DWARFFormValue Dir =204DWARFFormValue::createFromPValue(dwarf::DW_FORM_string, S.data());205IncludeDirectories.push_back(Dir);206}207208ContentTypes.HasModTime = true;209ContentTypes.HasLength = true;210211while (true) {212Error Err = Error::success();213StringRef Name = DebugLineData.getCStrRef(OffsetPtr, &Err);214if (!Err && Name.empty())215break;216217DWARFDebugLine::FileNameEntry FileEntry;218FileEntry.Name =219DWARFFormValue::createFromPValue(dwarf::DW_FORM_string, Name.data());220FileEntry.DirIdx = DebugLineData.getULEB128(OffsetPtr, &Err);221FileEntry.ModTime = DebugLineData.getULEB128(OffsetPtr, &Err);222FileEntry.Length = DebugLineData.getULEB128(OffsetPtr, &Err);223224if (Err) {225consumeError(std::move(Err));226return createStringError(227errc::invalid_argument,228"file names table was not null terminated before "229"the end of the prologue");230}231FileNames.push_back(FileEntry);232}233234return Error::success();235}236237// Parse v5 directory/file entry content descriptions.238// Returns the descriptors, or an error if we did not find a path or ran off239// the end of the prologue.240static llvm::Expected<ContentDescriptors>241parseV5EntryFormat(const DWARFDataExtractor &DebugLineData, uint64_t *OffsetPtr,242DWARFDebugLine::ContentTypeTracker *ContentTypes) {243Error Err = Error::success();244ContentDescriptors Descriptors;245int FormatCount = DebugLineData.getU8(OffsetPtr, &Err);246bool HasPath = false;247for (int I = 0; I != FormatCount && !Err; ++I) {248ContentDescriptor Descriptor;249Descriptor.Type =250dwarf::LineNumberEntryFormat(DebugLineData.getULEB128(OffsetPtr, &Err));251Descriptor.Form = dwarf::Form(DebugLineData.getULEB128(OffsetPtr, &Err));252if (Descriptor.Type == dwarf::DW_LNCT_path)253HasPath = true;254if (ContentTypes)255ContentTypes->trackContentType(Descriptor.Type);256Descriptors.push_back(Descriptor);257}258259if (Err)260return createStringError(errc::invalid_argument,261"failed to parse entry content descriptors: %s",262toString(std::move(Err)).c_str());263264if (!HasPath)265return createStringError(errc::invalid_argument,266"failed to parse entry content descriptions"267" because no path was found");268return Descriptors;269}270271static Error272parseV5DirFileTables(const DWARFDataExtractor &DebugLineData,273uint64_t *OffsetPtr, const dwarf::FormParams &FormParams,274const DWARFContext &Ctx, const DWARFUnit *U,275DWARFDebugLine::ContentTypeTracker &ContentTypes,276std::vector<DWARFFormValue> &IncludeDirectories,277std::vector<DWARFDebugLine::FileNameEntry> &FileNames) {278// Get the directory entry description.279llvm::Expected<ContentDescriptors> DirDescriptors =280parseV5EntryFormat(DebugLineData, OffsetPtr, nullptr);281if (!DirDescriptors)282return DirDescriptors.takeError();283284// Get the directory entries, according to the format described above.285uint64_t DirEntryCount = DebugLineData.getULEB128(OffsetPtr);286for (uint64_t I = 0; I != DirEntryCount; ++I) {287for (auto Descriptor : *DirDescriptors) {288DWARFFormValue Value(Descriptor.Form);289switch (Descriptor.Type) {290case DW_LNCT_path:291if (!Value.extractValue(DebugLineData, OffsetPtr, FormParams, &Ctx, U))292return createStringError(errc::invalid_argument,293"failed to parse directory entry because "294"extracting the form value failed");295IncludeDirectories.push_back(Value);296break;297default:298if (!Value.skipValue(DebugLineData, OffsetPtr, FormParams))299return createStringError(errc::invalid_argument,300"failed to parse directory entry because "301"skipping the form value failed");302}303}304}305306// Get the file entry description.307llvm::Expected<ContentDescriptors> FileDescriptors =308parseV5EntryFormat(DebugLineData, OffsetPtr, &ContentTypes);309if (!FileDescriptors)310return FileDescriptors.takeError();311312// Get the file entries, according to the format described above.313uint64_t FileEntryCount = DebugLineData.getULEB128(OffsetPtr);314for (uint64_t I = 0; I != FileEntryCount; ++I) {315DWARFDebugLine::FileNameEntry FileEntry;316for (auto Descriptor : *FileDescriptors) {317DWARFFormValue Value(Descriptor.Form);318if (!Value.extractValue(DebugLineData, OffsetPtr, FormParams, &Ctx, U))319return createStringError(errc::invalid_argument,320"failed to parse file entry because "321"extracting the form value failed");322switch (Descriptor.Type) {323case DW_LNCT_path:324FileEntry.Name = Value;325break;326case DW_LNCT_LLVM_source:327FileEntry.Source = Value;328break;329case DW_LNCT_directory_index:330FileEntry.DirIdx = *Value.getAsUnsignedConstant();331break;332case DW_LNCT_timestamp:333FileEntry.ModTime = *Value.getAsUnsignedConstant();334break;335case DW_LNCT_size:336FileEntry.Length = *Value.getAsUnsignedConstant();337break;338case DW_LNCT_MD5:339if (!Value.getAsBlock() || Value.getAsBlock()->size() != 16)340return createStringError(341errc::invalid_argument,342"failed to parse file entry because the MD5 hash is invalid");343std::uninitialized_copy_n(Value.getAsBlock()->begin(), 16,344FileEntry.Checksum.begin());345break;346default:347break;348}349}350FileNames.push_back(FileEntry);351}352return Error::success();353}354355uint64_t DWARFDebugLine::Prologue::getLength() const {356uint64_t Length = PrologueLength + sizeofTotalLength() +357sizeof(getVersion()) + sizeofPrologueLength();358if (getVersion() >= 5)359Length += 2; // Address + Segment selector sizes.360return Length;361}362363Error DWARFDebugLine::Prologue::parse(364DWARFDataExtractor DebugLineData, uint64_t *OffsetPtr,365function_ref<void(Error)> RecoverableErrorHandler, const DWARFContext &Ctx,366const DWARFUnit *U) {367const uint64_t PrologueOffset = *OffsetPtr;368369clear();370DataExtractor::Cursor Cursor(*OffsetPtr);371std::tie(TotalLength, FormParams.Format) =372DebugLineData.getInitialLength(Cursor);373374DebugLineData =375DWARFDataExtractor(DebugLineData, Cursor.tell() + TotalLength);376FormParams.Version = DebugLineData.getU16(Cursor);377if (Cursor && !versionIsSupported(getVersion())) {378// Treat this error as unrecoverable - we cannot be sure what any of379// the data represents including the length field, so cannot skip it or make380// any reasonable assumptions.381*OffsetPtr = Cursor.tell();382return createStringError(383errc::not_supported,384"parsing line table prologue at offset 0x%8.8" PRIx64385": unsupported version %" PRIu16,386PrologueOffset, getVersion());387}388389if (getVersion() >= 5) {390FormParams.AddrSize = DebugLineData.getU8(Cursor);391const uint8_t DataAddrSize = DebugLineData.getAddressSize();392const uint8_t PrologueAddrSize = getAddressSize();393if (Cursor) {394if (DataAddrSize == 0) {395if (PrologueAddrSize != 4 && PrologueAddrSize != 8) {396RecoverableErrorHandler(createStringError(397errc::not_supported,398"parsing line table prologue at offset 0x%8.8" PRIx64399": invalid address size %" PRIu8,400PrologueOffset, PrologueAddrSize));401}402} else if (DataAddrSize != PrologueAddrSize) {403RecoverableErrorHandler(createStringError(404errc::not_supported,405"parsing line table prologue at offset 0x%8.8" PRIx64 ": address "406"size %" PRIu8 " doesn't match architecture address size %" PRIu8,407PrologueOffset, PrologueAddrSize, DataAddrSize));408}409}410SegSelectorSize = DebugLineData.getU8(Cursor);411}412413PrologueLength =414DebugLineData.getRelocatedValue(Cursor, sizeofPrologueLength());415const uint64_t EndPrologueOffset = PrologueLength + Cursor.tell();416DebugLineData = DWARFDataExtractor(DebugLineData, EndPrologueOffset);417MinInstLength = DebugLineData.getU8(Cursor);418if (getVersion() >= 4)419MaxOpsPerInst = DebugLineData.getU8(Cursor);420DefaultIsStmt = DebugLineData.getU8(Cursor);421LineBase = DebugLineData.getU8(Cursor);422LineRange = DebugLineData.getU8(Cursor);423OpcodeBase = DebugLineData.getU8(Cursor);424425if (Cursor && OpcodeBase == 0) {426// If the opcode base is 0, we cannot read the standard opcode lengths (of427// which there are supposed to be one fewer than the opcode base). Assume428// there are no standard opcodes and continue parsing.429RecoverableErrorHandler(createStringError(430errc::invalid_argument,431"parsing line table prologue at offset 0x%8.8" PRIx64432" found opcode base of 0. Assuming no standard opcodes",433PrologueOffset));434} else if (Cursor) {435StandardOpcodeLengths.reserve(OpcodeBase - 1);436for (uint32_t I = 1; I < OpcodeBase; ++I) {437uint8_t OpLen = DebugLineData.getU8(Cursor);438StandardOpcodeLengths.push_back(OpLen);439}440}441442*OffsetPtr = Cursor.tell();443// A corrupt file name or directory table does not prevent interpretation of444// the main line program, so check the cursor state now so that its errors can445// be handled separately.446if (!Cursor)447return createStringError(448errc::invalid_argument,449"parsing line table prologue at offset 0x%8.8" PRIx64 ": %s",450PrologueOffset, toString(Cursor.takeError()).c_str());451452Error E =453getVersion() >= 5454? parseV5DirFileTables(DebugLineData, OffsetPtr, FormParams, Ctx, U,455ContentTypes, IncludeDirectories, FileNames)456: parseV2DirFileTables(DebugLineData, OffsetPtr, ContentTypes,457IncludeDirectories, FileNames);458if (E) {459RecoverableErrorHandler(joinErrors(460createStringError(461errc::invalid_argument,462"parsing line table prologue at 0x%8.8" PRIx64463" found an invalid directory or file table description at"464" 0x%8.8" PRIx64,465PrologueOffset, *OffsetPtr),466std::move(E)));467return Error::success();468}469470assert(*OffsetPtr <= EndPrologueOffset);471if (*OffsetPtr != EndPrologueOffset) {472RecoverableErrorHandler(createStringError(473errc::invalid_argument,474"unknown data in line table prologue at offset 0x%8.8" PRIx64475": parsing ended (at offset 0x%8.8" PRIx64476") before reaching the prologue end at offset 0x%8.8" PRIx64,477PrologueOffset, *OffsetPtr, EndPrologueOffset));478}479return Error::success();480}481482DWARFDebugLine::Row::Row(bool DefaultIsStmt) { reset(DefaultIsStmt); }483484void DWARFDebugLine::Row::postAppend() {485Discriminator = 0;486BasicBlock = false;487PrologueEnd = false;488EpilogueBegin = false;489}490491void DWARFDebugLine::Row::reset(bool DefaultIsStmt) {492Address.Address = 0;493Address.SectionIndex = object::SectionedAddress::UndefSection;494Line = 1;495Column = 0;496File = 1;497Isa = 0;498Discriminator = 0;499IsStmt = DefaultIsStmt;500OpIndex = 0;501BasicBlock = false;502EndSequence = false;503PrologueEnd = false;504EpilogueBegin = false;505}506507void DWARFDebugLine::Row::dumpTableHeader(raw_ostream &OS, unsigned Indent) {508OS.indent(Indent)509<< "Address Line Column File ISA Discriminator OpIndex "510"Flags\n";511OS.indent(Indent)512<< "------------------ ------ ------ ------ --- ------------- ------- "513"-------------\n";514}515516void DWARFDebugLine::Row::dump(raw_ostream &OS) const {517OS << format("0x%16.16" PRIx64 " %6u %6u", Address.Address, Line, Column)518<< format(" %6u %3u %13u %7u ", File, Isa, Discriminator, OpIndex)519<< (IsStmt ? " is_stmt" : "") << (BasicBlock ? " basic_block" : "")520<< (PrologueEnd ? " prologue_end" : "")521<< (EpilogueBegin ? " epilogue_begin" : "")522<< (EndSequence ? " end_sequence" : "") << '\n';523}524525DWARFDebugLine::Sequence::Sequence() { reset(); }526527void DWARFDebugLine::Sequence::reset() {528LowPC = 0;529HighPC = 0;530SectionIndex = object::SectionedAddress::UndefSection;531FirstRowIndex = 0;532LastRowIndex = 0;533Empty = true;534}535536DWARFDebugLine::LineTable::LineTable() { clear(); }537538void DWARFDebugLine::LineTable::dump(raw_ostream &OS,539DIDumpOptions DumpOptions) const {540Prologue.dump(OS, DumpOptions);541542if (!Rows.empty()) {543OS << '\n';544Row::dumpTableHeader(OS, 0);545for (const Row &R : Rows) {546R.dump(OS);547}548}549550// Terminate the table with a final blank line to clearly delineate it from551// later dumps.552OS << '\n';553}554555void DWARFDebugLine::LineTable::clear() {556Prologue.clear();557Rows.clear();558Sequences.clear();559}560561DWARFDebugLine::ParsingState::ParsingState(562struct LineTable *LT, uint64_t TableOffset,563function_ref<void(Error)> ErrorHandler)564: LineTable(LT), LineTableOffset(TableOffset), ErrorHandler(ErrorHandler) {565resetRowAndSequence();566}567568void DWARFDebugLine::ParsingState::resetRowAndSequence() {569Row.reset(LineTable->Prologue.DefaultIsStmt);570Sequence.reset();571}572573void DWARFDebugLine::ParsingState::appendRowToMatrix() {574unsigned RowNumber = LineTable->Rows.size();575if (Sequence.Empty) {576// Record the beginning of instruction sequence.577Sequence.Empty = false;578Sequence.LowPC = Row.Address.Address;579Sequence.FirstRowIndex = RowNumber;580}581LineTable->appendRow(Row);582if (Row.EndSequence) {583// Record the end of instruction sequence.584Sequence.HighPC = Row.Address.Address;585Sequence.LastRowIndex = RowNumber + 1;586Sequence.SectionIndex = Row.Address.SectionIndex;587if (Sequence.isValid())588LineTable->appendSequence(Sequence);589Sequence.reset();590}591Row.postAppend();592}593594const DWARFDebugLine::LineTable *595DWARFDebugLine::getLineTable(uint64_t Offset) const {596LineTableConstIter Pos = LineTableMap.find(Offset);597if (Pos != LineTableMap.end())598return &Pos->second;599return nullptr;600}601602Expected<const DWARFDebugLine::LineTable *> DWARFDebugLine::getOrParseLineTable(603DWARFDataExtractor &DebugLineData, uint64_t Offset, const DWARFContext &Ctx,604const DWARFUnit *U, function_ref<void(Error)> RecoverableErrorHandler) {605if (!DebugLineData.isValidOffset(Offset))606return createStringError(errc::invalid_argument, "offset 0x%8.8" PRIx64607" is not a valid debug line section offset",608Offset);609610std::pair<LineTableIter, bool> Pos =611LineTableMap.insert(LineTableMapTy::value_type(Offset, LineTable()));612LineTable *LT = &Pos.first->second;613if (Pos.second) {614if (Error Err =615LT->parse(DebugLineData, &Offset, Ctx, U, RecoverableErrorHandler))616return std::move(Err);617return LT;618}619return LT;620}621622void DWARFDebugLine::clearLineTable(uint64_t Offset) {623LineTableMap.erase(Offset);624}625626static StringRef getOpcodeName(uint8_t Opcode, uint8_t OpcodeBase) {627assert(Opcode != 0);628if (Opcode < OpcodeBase)629return LNStandardString(Opcode);630return "special";631}632633DWARFDebugLine::ParsingState::AddrOpIndexDelta634DWARFDebugLine::ParsingState::advanceAddrOpIndex(uint64_t OperationAdvance,635uint8_t Opcode,636uint64_t OpcodeOffset) {637StringRef OpcodeName = getOpcodeName(Opcode, LineTable->Prologue.OpcodeBase);638// For versions less than 4, the MaxOpsPerInst member is set to 0, as the639// maximum_operations_per_instruction field wasn't introduced until DWARFv4.640// Don't warn about bad values in this situation.641if (ReportAdvanceAddrProblem && LineTable->Prologue.getVersion() >= 4 &&642LineTable->Prologue.MaxOpsPerInst == 0)643ErrorHandler(createStringError(644errc::invalid_argument,645"line table program at offset 0x%8.8" PRIx64646" contains a %s opcode at offset 0x%8.8" PRIx64647", but the prologue maximum_operations_per_instruction value is 0"648", which is invalid. Assuming a value of 1 instead",649LineTableOffset, OpcodeName.data(), OpcodeOffset));650// Although we are able to correctly parse line number programs with651// MaxOpsPerInst > 1, the rest of DWARFDebugLine and its652// users have not been updated to handle line information for all operations653// in a multi-operation instruction, so warn about potentially incorrect654// results.655if (ReportAdvanceAddrProblem && LineTable->Prologue.MaxOpsPerInst > 1)656ErrorHandler(createStringError(657errc::not_supported,658"line table program at offset 0x%8.8" PRIx64659" contains a %s opcode at offset 0x%8.8" PRIx64660", but the prologue maximum_operations_per_instruction value is %" PRId8661", which is experimentally supported, so line number information "662"may be incorrect",663LineTableOffset, OpcodeName.data(), OpcodeOffset,664LineTable->Prologue.MaxOpsPerInst));665if (ReportAdvanceAddrProblem && LineTable->Prologue.MinInstLength == 0)666ErrorHandler(667createStringError(errc::invalid_argument,668"line table program at offset 0x%8.8" PRIx64669" contains a %s opcode at offset 0x%8.8" PRIx64670", but the prologue minimum_instruction_length value "671"is 0, which prevents any address advancing",672LineTableOffset, OpcodeName.data(), OpcodeOffset));673ReportAdvanceAddrProblem = false;674675// Advances the address and op_index according to DWARFv5, section 6.2.5.1:676//677// new address = address +678// minimum_instruction_length *679// ((op_index + operation advance) / maximum_operations_per_instruction)680//681// new op_index =682// (op_index + operation advance) % maximum_operations_per_instruction683684// For versions less than 4, the MaxOpsPerInst member is set to 0, as the685// maximum_operations_per_instruction field wasn't introduced until DWARFv4.686uint8_t MaxOpsPerInst =687std::max(LineTable->Prologue.MaxOpsPerInst, uint8_t{1});688689uint64_t AddrOffset = ((Row.OpIndex + OperationAdvance) / MaxOpsPerInst) *690LineTable->Prologue.MinInstLength;691Row.Address.Address += AddrOffset;692693uint8_t PrevOpIndex = Row.OpIndex;694Row.OpIndex = (Row.OpIndex + OperationAdvance) % MaxOpsPerInst;695int16_t OpIndexDelta = static_cast<int16_t>(Row.OpIndex) - PrevOpIndex;696697return {AddrOffset, OpIndexDelta};698}699700DWARFDebugLine::ParsingState::OpcodeAdvanceResults701DWARFDebugLine::ParsingState::advanceForOpcode(uint8_t Opcode,702uint64_t OpcodeOffset) {703assert(Opcode == DW_LNS_const_add_pc ||704Opcode >= LineTable->Prologue.OpcodeBase);705if (ReportBadLineRange && LineTable->Prologue.LineRange == 0) {706StringRef OpcodeName =707getOpcodeName(Opcode, LineTable->Prologue.OpcodeBase);708ErrorHandler(709createStringError(errc::not_supported,710"line table program at offset 0x%8.8" PRIx64711" contains a %s opcode at offset 0x%8.8" PRIx64712", but the prologue line_range value is 0. The "713"address and line will not be adjusted",714LineTableOffset, OpcodeName.data(), OpcodeOffset));715ReportBadLineRange = false;716}717718uint8_t OpcodeValue = Opcode;719if (Opcode == DW_LNS_const_add_pc)720OpcodeValue = 255;721uint8_t AdjustedOpcode = OpcodeValue - LineTable->Prologue.OpcodeBase;722uint64_t OperationAdvance =723LineTable->Prologue.LineRange != 0724? AdjustedOpcode / LineTable->Prologue.LineRange725: 0;726AddrOpIndexDelta Advance =727advanceAddrOpIndex(OperationAdvance, Opcode, OpcodeOffset);728return {Advance.AddrOffset, Advance.OpIndexDelta, AdjustedOpcode};729}730731DWARFDebugLine::ParsingState::SpecialOpcodeDelta732DWARFDebugLine::ParsingState::handleSpecialOpcode(uint8_t Opcode,733uint64_t OpcodeOffset) {734// A special opcode value is chosen based on the amount that needs735// to be added to the line and address registers. The maximum line736// increment for a special opcode is the value of the line_base737// field in the header, plus the value of the line_range field,738// minus 1 (line base + line range - 1). If the desired line739// increment is greater than the maximum line increment, a standard740// opcode must be used instead of a special opcode. The "address741// advance" is calculated by dividing the desired address increment742// by the minimum_instruction_length field from the header. The743// special opcode is then calculated using the following formula:744//745// opcode = (desired line increment - line_base) +746// (line_range * address advance) + opcode_base747//748// If the resulting opcode is greater than 255, a standard opcode749// must be used instead.750//751// To decode a special opcode, subtract the opcode_base from the752// opcode itself to give the adjusted opcode. The amount to753// increment the address register is the result of the adjusted754// opcode divided by the line_range multiplied by the755// minimum_instruction_length field from the header. That is:756//757// address increment = (adjusted opcode / line_range) *758// minimum_instruction_length759//760// The amount to increment the line register is the line_base plus761// the result of the adjusted opcode modulo the line_range. That is:762//763// line increment = line_base + (adjusted opcode % line_range)764765DWARFDebugLine::ParsingState::OpcodeAdvanceResults AddrAdvanceResult =766advanceForOpcode(Opcode, OpcodeOffset);767int32_t LineOffset = 0;768if (LineTable->Prologue.LineRange != 0)769LineOffset =770LineTable->Prologue.LineBase +771(AddrAdvanceResult.AdjustedOpcode % LineTable->Prologue.LineRange);772Row.Line += LineOffset;773return {AddrAdvanceResult.AddrDelta, LineOffset,774AddrAdvanceResult.OpIndexDelta};775}776777/// Parse a ULEB128 using the specified \p Cursor. \returns the parsed value on778/// success, or std::nullopt if \p Cursor is in a failing state.779template <typename T>780static std::optional<T> parseULEB128(DWARFDataExtractor &Data,781DataExtractor::Cursor &Cursor) {782T Value = Data.getULEB128(Cursor);783if (Cursor)784return Value;785return std::nullopt;786}787788Error DWARFDebugLine::LineTable::parse(789DWARFDataExtractor &DebugLineData, uint64_t *OffsetPtr,790const DWARFContext &Ctx, const DWARFUnit *U,791function_ref<void(Error)> RecoverableErrorHandler, raw_ostream *OS,792bool Verbose) {793assert((OS || !Verbose) && "cannot have verbose output without stream");794const uint64_t DebugLineOffset = *OffsetPtr;795796clear();797798Error PrologueErr =799Prologue.parse(DebugLineData, OffsetPtr, RecoverableErrorHandler, Ctx, U);800801if (OS) {802DIDumpOptions DumpOptions;803DumpOptions.Verbose = Verbose;804Prologue.dump(*OS, DumpOptions);805}806807if (PrologueErr) {808// Ensure there is a blank line after the prologue to clearly delineate it809// from later dumps.810if (OS)811*OS << "\n";812return PrologueErr;813}814815uint64_t ProgramLength = Prologue.TotalLength + Prologue.sizeofTotalLength();816if (!DebugLineData.isValidOffsetForDataOfSize(DebugLineOffset,817ProgramLength)) {818assert(DebugLineData.size() > DebugLineOffset &&819"prologue parsing should handle invalid offset");820uint64_t BytesRemaining = DebugLineData.size() - DebugLineOffset;821RecoverableErrorHandler(822createStringError(errc::invalid_argument,823"line table program with offset 0x%8.8" PRIx64824" has length 0x%8.8" PRIx64 " but only 0x%8.8" PRIx64825" bytes are available",826DebugLineOffset, ProgramLength, BytesRemaining));827// Continue by capping the length at the number of remaining bytes.828ProgramLength = BytesRemaining;829}830831// Create a DataExtractor which can only see the data up to the end of the832// table, to prevent reading past the end.833const uint64_t EndOffset = DebugLineOffset + ProgramLength;834DWARFDataExtractor TableData(DebugLineData, EndOffset);835836// See if we should tell the data extractor the address size.837if (TableData.getAddressSize() == 0)838TableData.setAddressSize(Prologue.getAddressSize());839else840assert(Prologue.getAddressSize() == 0 ||841Prologue.getAddressSize() == TableData.getAddressSize());842843ParsingState State(this, DebugLineOffset, RecoverableErrorHandler);844845*OffsetPtr = DebugLineOffset + Prologue.getLength();846if (OS && *OffsetPtr < EndOffset) {847*OS << '\n';848Row::dumpTableHeader(*OS, /*Indent=*/Verbose ? 12 : 0);849}850bool TombstonedAddress = false;851auto EmitRow = [&] {852if (!TombstonedAddress) {853if (Verbose) {854*OS << "\n";855OS->indent(12);856}857if (OS)858State.Row.dump(*OS);859State.appendRowToMatrix();860}861};862while (*OffsetPtr < EndOffset) {863DataExtractor::Cursor Cursor(*OffsetPtr);864865if (Verbose)866*OS << format("0x%08.08" PRIx64 ": ", *OffsetPtr);867868uint64_t OpcodeOffset = *OffsetPtr;869uint8_t Opcode = TableData.getU8(Cursor);870size_t RowCount = Rows.size();871872if (Cursor && Verbose)873*OS << format("%02.02" PRIx8 " ", Opcode);874875if (Opcode == 0) {876// Extended Opcodes always start with a zero opcode followed by877// a uleb128 length so you can skip ones you don't know about878uint64_t Len = TableData.getULEB128(Cursor);879uint64_t ExtOffset = Cursor.tell();880881// Tolerate zero-length; assume length is correct and soldier on.882if (Len == 0) {883if (Cursor && Verbose)884*OS << "Badly formed extended line op (length 0)\n";885if (!Cursor) {886if (Verbose)887*OS << "\n";888RecoverableErrorHandler(Cursor.takeError());889}890*OffsetPtr = Cursor.tell();891continue;892}893894uint8_t SubOpcode = TableData.getU8(Cursor);895// OperandOffset will be the same as ExtOffset, if it was not possible to896// read the SubOpcode.897uint64_t OperandOffset = Cursor.tell();898if (Verbose)899*OS << LNExtendedString(SubOpcode);900switch (SubOpcode) {901case DW_LNE_end_sequence:902// Set the end_sequence register of the state machine to true and903// append a row to the matrix using the current values of the904// state-machine registers. Then reset the registers to the initial905// values specified above. Every statement program sequence must end906// with a DW_LNE_end_sequence instruction which creates a row whose907// address is that of the byte after the last target machine instruction908// of the sequence.909State.Row.EndSequence = true;910// No need to test the Cursor is valid here, since it must be to get911// into this code path - if it were invalid, the default case would be912// followed.913EmitRow();914State.resetRowAndSequence();915break;916917case DW_LNE_set_address:918// Takes a single relocatable address as an operand. The size of the919// operand is the size appropriate to hold an address on the target920// machine. Set the address register to the value given by the921// relocatable address and set the op_index register to 0. All of the922// other statement program opcodes that affect the address register923// add a delta to it. This instruction stores a relocatable value into924// it instead.925//926// Make sure the extractor knows the address size. If not, infer it927// from the size of the operand.928{929uint8_t ExtractorAddressSize = TableData.getAddressSize();930uint64_t OpcodeAddressSize = Len - 1;931if (ExtractorAddressSize != OpcodeAddressSize &&932ExtractorAddressSize != 0)933RecoverableErrorHandler(createStringError(934errc::invalid_argument,935"mismatching address size at offset 0x%8.8" PRIx64936" expected 0x%2.2" PRIx8 " found 0x%2.2" PRIx64,937ExtOffset, ExtractorAddressSize, Len - 1));938939// Assume that the line table is correct and temporarily override the940// address size. If the size is unsupported, give up trying to read941// the address and continue to the next opcode.942if (OpcodeAddressSize != 1 && OpcodeAddressSize != 2 &&943OpcodeAddressSize != 4 && OpcodeAddressSize != 8) {944RecoverableErrorHandler(createStringError(945errc::invalid_argument,946"address size 0x%2.2" PRIx64947" of DW_LNE_set_address opcode at offset 0x%8.8" PRIx64948" is unsupported",949OpcodeAddressSize, ExtOffset));950TableData.skip(Cursor, OpcodeAddressSize);951} else {952TableData.setAddressSize(OpcodeAddressSize);953State.Row.Address.Address = TableData.getRelocatedAddress(954Cursor, &State.Row.Address.SectionIndex);955State.Row.OpIndex = 0;956957uint64_t Tombstone =958dwarf::computeTombstoneAddress(OpcodeAddressSize);959TombstonedAddress = State.Row.Address.Address == Tombstone;960961// Restore the address size if the extractor already had it.962if (ExtractorAddressSize != 0)963TableData.setAddressSize(ExtractorAddressSize);964}965966if (Cursor && Verbose) {967*OS << " (";968DWARFFormValue::dumpAddress(*OS, OpcodeAddressSize, State.Row.Address.Address);969*OS << ')';970}971}972break;973974case DW_LNE_define_file:975// Takes 4 arguments. The first is a null terminated string containing976// a source file name. The second is an unsigned LEB128 number977// representing the directory index of the directory in which the file978// was found. The third is an unsigned LEB128 number representing the979// time of last modification of the file. The fourth is an unsigned980// LEB128 number representing the length in bytes of the file. The time981// and length fields may contain LEB128(0) if the information is not982// available.983//984// The directory index represents an entry in the include_directories985// section of the statement program prologue. The index is LEB128(0)986// if the file was found in the current directory of the compilation,987// LEB128(1) if it was found in the first directory in the988// include_directories section, and so on. The directory index is989// ignored for file names that represent full path names.990//991// The files are numbered, starting at 1, in the order in which they992// appear; the names in the prologue come before names defined by993// the DW_LNE_define_file instruction. These numbers are used in the994// the file register of the state machine.995{996FileNameEntry FileEntry;997const char *Name = TableData.getCStr(Cursor);998FileEntry.Name =999DWARFFormValue::createFromPValue(dwarf::DW_FORM_string, Name);1000FileEntry.DirIdx = TableData.getULEB128(Cursor);1001FileEntry.ModTime = TableData.getULEB128(Cursor);1002FileEntry.Length = TableData.getULEB128(Cursor);1003Prologue.FileNames.push_back(FileEntry);1004if (Cursor && Verbose)1005*OS << " (" << Name << ", dir=" << FileEntry.DirIdx << ", mod_time="1006<< format("(0x%16.16" PRIx64 ")", FileEntry.ModTime)1007<< ", length=" << FileEntry.Length << ")";1008}1009break;10101011case DW_LNE_set_discriminator:1012State.Row.Discriminator = TableData.getULEB128(Cursor);1013if (Cursor && Verbose)1014*OS << " (" << State.Row.Discriminator << ")";1015break;10161017default:1018if (Cursor && Verbose)1019*OS << format("Unrecognized extended op 0x%02.02" PRIx8, SubOpcode)1020<< format(" length %" PRIx64, Len);1021// Len doesn't include the zero opcode byte or the length itself, but1022// it does include the sub_opcode, so we have to adjust for that.1023TableData.skip(Cursor, Len - 1);1024break;1025}1026// Make sure the length as recorded in the table and the standard length1027// for the opcode match. If they don't, continue from the end as claimed1028// by the table. Similarly, continue from the claimed end in the event of1029// a parsing error.1030uint64_t End = ExtOffset + Len;1031if (Cursor && Cursor.tell() != End)1032RecoverableErrorHandler(createStringError(1033errc::illegal_byte_sequence,1034"unexpected line op length at offset 0x%8.8" PRIx641035" expected 0x%2.2" PRIx64 " found 0x%2.2" PRIx64,1036ExtOffset, Len, Cursor.tell() - ExtOffset));1037if (!Cursor && Verbose) {1038DWARFDataExtractor::Cursor ByteCursor(OperandOffset);1039uint8_t Byte = TableData.getU8(ByteCursor);1040if (ByteCursor) {1041*OS << " (<parsing error>";1042do {1043*OS << format(" %2.2" PRIx8, Byte);1044Byte = TableData.getU8(ByteCursor);1045} while (ByteCursor);1046*OS << ")";1047}10481049// The only parse failure in this case should be if the end was reached.1050// In that case, throw away the error, as the main Cursor's error will1051// be sufficient.1052consumeError(ByteCursor.takeError());1053}1054*OffsetPtr = End;1055} else if (Opcode < Prologue.OpcodeBase) {1056if (Verbose)1057*OS << LNStandardString(Opcode);1058switch (Opcode) {1059// Standard Opcodes1060case DW_LNS_copy:1061// Takes no arguments. Append a row to the matrix using the1062// current values of the state-machine registers.1063EmitRow();1064break;10651066case DW_LNS_advance_pc:1067// Takes a single unsigned LEB128 operand as the operation advance1068// and modifies the address and op_index registers of the state machine1069// according to that.1070if (std::optional<uint64_t> Operand =1071parseULEB128<uint64_t>(TableData, Cursor)) {1072ParsingState::AddrOpIndexDelta Advance =1073State.advanceAddrOpIndex(*Operand, Opcode, OpcodeOffset);1074if (Verbose)1075*OS << " (addr += " << Advance.AddrOffset1076<< ", op-index += " << Advance.OpIndexDelta << ")";1077}1078break;10791080case DW_LNS_advance_line:1081// Takes a single signed LEB128 operand and adds that value to1082// the line register of the state machine.1083{1084int64_t LineDelta = TableData.getSLEB128(Cursor);1085if (Cursor) {1086State.Row.Line += LineDelta;1087if (Verbose)1088*OS << " (" << State.Row.Line << ")";1089}1090}1091break;10921093case DW_LNS_set_file:1094// Takes a single unsigned LEB128 operand and stores it in the file1095// register of the state machine.1096if (std::optional<uint16_t> File =1097parseULEB128<uint16_t>(TableData, Cursor)) {1098State.Row.File = *File;1099if (Verbose)1100*OS << " (" << State.Row.File << ")";1101}1102break;11031104case DW_LNS_set_column:1105// Takes a single unsigned LEB128 operand and stores it in the1106// column register of the state machine.1107if (std::optional<uint16_t> Column =1108parseULEB128<uint16_t>(TableData, Cursor)) {1109State.Row.Column = *Column;1110if (Verbose)1111*OS << " (" << State.Row.Column << ")";1112}1113break;11141115case DW_LNS_negate_stmt:1116// Takes no arguments. Set the is_stmt register of the state1117// machine to the logical negation of its current value.1118State.Row.IsStmt = !State.Row.IsStmt;1119break;11201121case DW_LNS_set_basic_block:1122// Takes no arguments. Set the basic_block register of the1123// state machine to true1124State.Row.BasicBlock = true;1125break;11261127case DW_LNS_const_add_pc:1128// Takes no arguments. Advance the address and op_index registers of1129// the state machine by the increments corresponding to special1130// opcode 255. The motivation for DW_LNS_const_add_pc is this:1131// when the statement program needs to advance the address by a1132// small amount, it can use a single special opcode, which occupies1133// a single byte. When it needs to advance the address by up to1134// twice the range of the last special opcode, it can use1135// DW_LNS_const_add_pc followed by a special opcode, for a total1136// of two bytes. Only if it needs to advance the address by more1137// than twice that range will it need to use both DW_LNS_advance_pc1138// and a special opcode, requiring three or more bytes.1139{1140ParsingState::OpcodeAdvanceResults Advance =1141State.advanceForOpcode(Opcode, OpcodeOffset);1142if (Verbose)1143*OS << format(" (addr += 0x%16.16" PRIx64 ", op-index += %" PRIu81144")",1145Advance.AddrDelta, Advance.OpIndexDelta);1146}1147break;11481149case DW_LNS_fixed_advance_pc:1150// Takes a single uhalf operand. Add to the address register of1151// the state machine the value of the (unencoded) operand and set1152// the op_index register to 0. This is the only extended opcode that1153// takes an argument that is not a variable length number.1154// The motivation for DW_LNS_fixed_advance_pc is this: existing1155// assemblers cannot emit DW_LNS_advance_pc or special opcodes because1156// they cannot encode LEB128 numbers or judge when the computation1157// of a special opcode overflows and requires the use of1158// DW_LNS_advance_pc. Such assemblers, however, can use1159// DW_LNS_fixed_advance_pc instead, sacrificing compression.1160{1161uint16_t PCOffset =1162TableData.getRelocatedValue(Cursor, 2);1163if (Cursor) {1164State.Row.Address.Address += PCOffset;1165State.Row.OpIndex = 0;1166if (Verbose)1167*OS << format(" (addr += 0x%4.4" PRIx16 ", op-index = 0)",1168PCOffset);1169}1170}1171break;11721173case DW_LNS_set_prologue_end:1174// Takes no arguments. Set the prologue_end register of the1175// state machine to true1176State.Row.PrologueEnd = true;1177break;11781179case DW_LNS_set_epilogue_begin:1180// Takes no arguments. Set the basic_block register of the1181// state machine to true1182State.Row.EpilogueBegin = true;1183break;11841185case DW_LNS_set_isa:1186// Takes a single unsigned LEB128 operand and stores it in the1187// ISA register of the state machine.1188if (std::optional<uint8_t> Isa =1189parseULEB128<uint8_t>(TableData, Cursor)) {1190State.Row.Isa = *Isa;1191if (Verbose)1192*OS << " (" << (uint64_t)State.Row.Isa << ")";1193}1194break;11951196default:1197// Handle any unknown standard opcodes here. We know the lengths1198// of such opcodes because they are specified in the prologue1199// as a multiple of LEB128 operands for each opcode.1200{1201assert(Opcode - 1U < Prologue.StandardOpcodeLengths.size());1202if (Verbose)1203*OS << "Unrecognized standard opcode";1204uint8_t OpcodeLength = Prologue.StandardOpcodeLengths[Opcode - 1];1205std::vector<uint64_t> Operands;1206for (uint8_t I = 0; I < OpcodeLength; ++I) {1207if (std::optional<uint64_t> Value =1208parseULEB128<uint64_t>(TableData, Cursor))1209Operands.push_back(*Value);1210else1211break;1212}1213if (Verbose && !Operands.empty()) {1214*OS << " (operands: ";1215bool First = true;1216for (uint64_t Value : Operands) {1217if (!First)1218*OS << ", ";1219First = false;1220*OS << format("0x%16.16" PRIx64, Value);1221}1222if (Verbose)1223*OS << ')';1224}1225}1226break;1227}12281229*OffsetPtr = Cursor.tell();1230} else {1231// Special Opcodes.1232ParsingState::SpecialOpcodeDelta Delta =1233State.handleSpecialOpcode(Opcode, OpcodeOffset);12341235if (Verbose)1236*OS << "address += " << Delta.Address << ", line += " << Delta.Line1237<< ", op-index += " << Delta.OpIndex;1238EmitRow();1239*OffsetPtr = Cursor.tell();1240}12411242// When a row is added to the matrix, it is also dumped, which includes a1243// new line already, so don't add an extra one.1244if (Verbose && Rows.size() == RowCount)1245*OS << "\n";12461247// Most parse failures other than when parsing extended opcodes are due to1248// failures to read ULEBs. Bail out of parsing, since we don't know where to1249// continue reading from as there is no stated length for such byte1250// sequences. Print the final trailing new line if needed before doing so.1251if (!Cursor && Opcode != 0) {1252if (Verbose)1253*OS << "\n";1254return Cursor.takeError();1255}12561257if (!Cursor)1258RecoverableErrorHandler(Cursor.takeError());1259}12601261if (!State.Sequence.Empty)1262RecoverableErrorHandler(createStringError(1263errc::illegal_byte_sequence,1264"last sequence in debug line table at offset 0x%8.8" PRIx641265" is not terminated",1266DebugLineOffset));12671268// Sort all sequences so that address lookup will work faster.1269if (!Sequences.empty()) {1270llvm::sort(Sequences, Sequence::orderByHighPC);1271// Note: actually, instruction address ranges of sequences should not1272// overlap (in shared objects and executables). If they do, the address1273// lookup would still work, though, but result would be ambiguous.1274// We don't report warning in this case. For example,1275// sometimes .so compiled from multiple object files contains a few1276// rudimentary sequences for address ranges [0x0, 0xsomething).1277}12781279// Terminate the table with a final blank line to clearly delineate it from1280// later dumps.1281if (OS)1282*OS << "\n";12831284return Error::success();1285}12861287uint32_t DWARFDebugLine::LineTable::findRowInSeq(1288const DWARFDebugLine::Sequence &Seq,1289object::SectionedAddress Address) const {1290if (!Seq.containsPC(Address))1291return UnknownRowIndex;1292assert(Seq.SectionIndex == Address.SectionIndex);1293// In some cases, e.g. first instruction in a function, the compiler generates1294// two entries, both with the same address. We want the last one.1295//1296// In general we want a non-empty range: the last row whose address is less1297// than or equal to Address. This can be computed as upper_bound - 1.1298//1299// TODO: This function, and its users, needs to be update to return multiple1300// rows for bundles with multiple op-indexes.1301DWARFDebugLine::Row Row;1302Row.Address = Address;1303RowIter FirstRow = Rows.begin() + Seq.FirstRowIndex;1304RowIter LastRow = Rows.begin() + Seq.LastRowIndex;1305assert(FirstRow->Address.Address <= Row.Address.Address &&1306Row.Address.Address < LastRow[-1].Address.Address);1307RowIter RowPos = std::upper_bound(FirstRow + 1, LastRow - 1, Row,1308DWARFDebugLine::Row::orderByAddress) -13091;1310assert(Seq.SectionIndex == RowPos->Address.SectionIndex);1311return RowPos - Rows.begin();1312}13131314uint32_t DWARFDebugLine::LineTable::lookupAddress(1315object::SectionedAddress Address) const {13161317// Search for relocatable addresses1318uint32_t Result = lookupAddressImpl(Address);13191320if (Result != UnknownRowIndex ||1321Address.SectionIndex == object::SectionedAddress::UndefSection)1322return Result;13231324// Search for absolute addresses1325Address.SectionIndex = object::SectionedAddress::UndefSection;1326return lookupAddressImpl(Address);1327}13281329uint32_t DWARFDebugLine::LineTable::lookupAddressImpl(1330object::SectionedAddress Address) const {1331// First, find an instruction sequence containing the given address.1332DWARFDebugLine::Sequence Sequence;1333Sequence.SectionIndex = Address.SectionIndex;1334Sequence.HighPC = Address.Address;1335SequenceIter It = llvm::upper_bound(Sequences, Sequence,1336DWARFDebugLine::Sequence::orderByHighPC);1337if (It == Sequences.end() || It->SectionIndex != Address.SectionIndex)1338return UnknownRowIndex;1339return findRowInSeq(*It, Address);1340}13411342bool DWARFDebugLine::LineTable::lookupAddressRange(1343object::SectionedAddress Address, uint64_t Size,1344std::vector<uint32_t> &Result) const {13451346// Search for relocatable addresses1347if (lookupAddressRangeImpl(Address, Size, Result))1348return true;13491350if (Address.SectionIndex == object::SectionedAddress::UndefSection)1351return false;13521353// Search for absolute addresses1354Address.SectionIndex = object::SectionedAddress::UndefSection;1355return lookupAddressRangeImpl(Address, Size, Result);1356}13571358bool DWARFDebugLine::LineTable::lookupAddressRangeImpl(1359object::SectionedAddress Address, uint64_t Size,1360std::vector<uint32_t> &Result) const {1361if (Sequences.empty())1362return false;1363uint64_t EndAddr = Address.Address + Size;1364// First, find an instruction sequence containing the given address.1365DWARFDebugLine::Sequence Sequence;1366Sequence.SectionIndex = Address.SectionIndex;1367Sequence.HighPC = Address.Address;1368SequenceIter LastSeq = Sequences.end();1369SequenceIter SeqPos = llvm::upper_bound(1370Sequences, Sequence, DWARFDebugLine::Sequence::orderByHighPC);1371if (SeqPos == LastSeq || !SeqPos->containsPC(Address))1372return false;13731374SequenceIter StartPos = SeqPos;13751376// Add the rows from the first sequence to the vector, starting with the1377// index we just calculated13781379while (SeqPos != LastSeq && SeqPos->LowPC < EndAddr) {1380const DWARFDebugLine::Sequence &CurSeq = *SeqPos;1381// For the first sequence, we need to find which row in the sequence is the1382// first in our range.1383uint32_t FirstRowIndex = CurSeq.FirstRowIndex;1384if (SeqPos == StartPos)1385FirstRowIndex = findRowInSeq(CurSeq, Address);13861387// Figure out the last row in the range.1388uint32_t LastRowIndex =1389findRowInSeq(CurSeq, {EndAddr - 1, Address.SectionIndex});1390if (LastRowIndex == UnknownRowIndex)1391LastRowIndex = CurSeq.LastRowIndex - 1;13921393assert(FirstRowIndex != UnknownRowIndex);1394assert(LastRowIndex != UnknownRowIndex);13951396for (uint32_t I = FirstRowIndex; I <= LastRowIndex; ++I) {1397Result.push_back(I);1398}13991400++SeqPos;1401}14021403return true;1404}14051406std::optional<StringRef>1407DWARFDebugLine::LineTable::getSourceByIndex(uint64_t FileIndex,1408FileLineInfoKind Kind) const {1409if (Kind == FileLineInfoKind::None || !Prologue.hasFileAtIndex(FileIndex))1410return std::nullopt;1411const FileNameEntry &Entry = Prologue.getFileNameEntry(FileIndex);1412if (auto E = dwarf::toString(Entry.Source))1413return StringRef(*E);1414return std::nullopt;1415}14161417static bool isPathAbsoluteOnWindowsOrPosix(const Twine &Path) {1418// Debug info can contain paths from any OS, not necessarily1419// an OS we're currently running on. Moreover different compilation units can1420// be compiled on different operating systems and linked together later.1421return sys::path::is_absolute(Path, sys::path::Style::posix) ||1422sys::path::is_absolute(Path, sys::path::Style::windows);1423}14241425bool DWARFDebugLine::Prologue::getFileNameByIndex(1426uint64_t FileIndex, StringRef CompDir, FileLineInfoKind Kind,1427std::string &Result, sys::path::Style Style) const {1428if (Kind == FileLineInfoKind::None || !hasFileAtIndex(FileIndex))1429return false;1430const FileNameEntry &Entry = getFileNameEntry(FileIndex);1431auto E = dwarf::toString(Entry.Name);1432if (!E)1433return false;1434StringRef FileName = *E;1435if (Kind == FileLineInfoKind::RawValue ||1436isPathAbsoluteOnWindowsOrPosix(FileName)) {1437Result = std::string(FileName);1438return true;1439}1440if (Kind == FileLineInfoKind::BaseNameOnly) {1441Result = std::string(llvm::sys::path::filename(FileName));1442return true;1443}14441445SmallString<16> FilePath;1446StringRef IncludeDir;1447// Be defensive about the contents of Entry.1448if (getVersion() >= 5) {1449// DirIdx 0 is the compilation directory, so don't include it for1450// relative names.1451if ((Entry.DirIdx != 0 || Kind != FileLineInfoKind::RelativeFilePath) &&1452Entry.DirIdx < IncludeDirectories.size())1453IncludeDir = dwarf::toStringRef(IncludeDirectories[Entry.DirIdx]);1454} else {1455if (0 < Entry.DirIdx && Entry.DirIdx <= IncludeDirectories.size())1456IncludeDir = dwarf::toStringRef(IncludeDirectories[Entry.DirIdx - 1]);1457}14581459// For absolute paths only, include the compilation directory of compile unit,1460// unless v5 DirIdx == 0 (IncludeDir indicates the compilation directory). We1461// know that FileName is not absolute, the only way to have an absolute path1462// at this point would be if IncludeDir is absolute.1463if (Kind == FileLineInfoKind::AbsoluteFilePath &&1464(getVersion() < 5 || Entry.DirIdx != 0) && !CompDir.empty() &&1465!isPathAbsoluteOnWindowsOrPosix(IncludeDir))1466sys::path::append(FilePath, Style, CompDir);14671468assert((Kind == FileLineInfoKind::AbsoluteFilePath ||1469Kind == FileLineInfoKind::RelativeFilePath) &&1470"invalid FileLineInfo Kind");14711472// sys::path::append skips empty strings.1473sys::path::append(FilePath, Style, IncludeDir, FileName);1474Result = std::string(FilePath);1475return true;1476}14771478bool DWARFDebugLine::LineTable::getFileLineInfoForAddress(1479object::SectionedAddress Address, const char *CompDir,1480FileLineInfoKind Kind, DILineInfo &Result) const {1481// Get the index of row we're looking for in the line table.1482uint32_t RowIndex = lookupAddress(Address);1483if (RowIndex == -1U)1484return false;1485// Take file number and line/column from the row.1486const auto &Row = Rows[RowIndex];1487if (!getFileNameByIndex(Row.File, CompDir, Kind, Result.FileName))1488return false;1489Result.Line = Row.Line;1490Result.Column = Row.Column;1491Result.Discriminator = Row.Discriminator;1492Result.Source = getSourceByIndex(Row.File, Kind);1493return true;1494}14951496bool DWARFDebugLine::LineTable::getDirectoryForEntry(1497const FileNameEntry &Entry, std::string &Directory) const {1498if (Prologue.getVersion() >= 5) {1499if (Entry.DirIdx < Prologue.IncludeDirectories.size()) {1500Directory =1501dwarf::toString(Prologue.IncludeDirectories[Entry.DirIdx], "");1502return true;1503}1504return false;1505}1506if (0 < Entry.DirIdx && Entry.DirIdx <= Prologue.IncludeDirectories.size()) {1507Directory =1508dwarf::toString(Prologue.IncludeDirectories[Entry.DirIdx - 1], "");1509return true;1510}1511return false;1512}15131514// We want to supply the Unit associated with a .debug_line[.dwo] table when1515// we dump it, if possible, but still dump the table even if there isn't a Unit.1516// Therefore, collect up handles on all the Units that point into the1517// line-table section.1518static DWARFDebugLine::SectionParser::LineToUnitMap1519buildLineToUnitMap(DWARFUnitVector::iterator_range Units) {1520DWARFDebugLine::SectionParser::LineToUnitMap LineToUnit;1521for (const auto &U : Units)1522if (auto CUDIE = U->getUnitDIE())1523if (auto StmtOffset = toSectionOffset(CUDIE.find(DW_AT_stmt_list)))1524LineToUnit.insert(std::make_pair(*StmtOffset, &*U));1525return LineToUnit;1526}15271528DWARFDebugLine::SectionParser::SectionParser(1529DWARFDataExtractor &Data, const DWARFContext &C,1530DWARFUnitVector::iterator_range Units)1531: DebugLineData(Data), Context(C) {1532LineToUnit = buildLineToUnitMap(Units);1533if (!DebugLineData.isValidOffset(Offset))1534Done = true;1535}15361537bool DWARFDebugLine::Prologue::totalLengthIsValid() const {1538return TotalLength != 0u;1539}15401541DWARFDebugLine::LineTable DWARFDebugLine::SectionParser::parseNext(1542function_ref<void(Error)> RecoverableErrorHandler,1543function_ref<void(Error)> UnrecoverableErrorHandler, raw_ostream *OS,1544bool Verbose) {1545assert(DebugLineData.isValidOffset(Offset) &&1546"parsing should have terminated");1547DWARFUnit *U = prepareToParse(Offset);1548uint64_t OldOffset = Offset;1549LineTable LT;1550if (Error Err = LT.parse(DebugLineData, &Offset, Context, U,1551RecoverableErrorHandler, OS, Verbose))1552UnrecoverableErrorHandler(std::move(Err));1553moveToNextTable(OldOffset, LT.Prologue);1554return LT;1555}15561557void DWARFDebugLine::SectionParser::skip(1558function_ref<void(Error)> RecoverableErrorHandler,1559function_ref<void(Error)> UnrecoverableErrorHandler) {1560assert(DebugLineData.isValidOffset(Offset) &&1561"parsing should have terminated");1562DWARFUnit *U = prepareToParse(Offset);1563uint64_t OldOffset = Offset;1564LineTable LT;1565if (Error Err = LT.Prologue.parse(DebugLineData, &Offset,1566RecoverableErrorHandler, Context, U))1567UnrecoverableErrorHandler(std::move(Err));1568moveToNextTable(OldOffset, LT.Prologue);1569}15701571DWARFUnit *DWARFDebugLine::SectionParser::prepareToParse(uint64_t Offset) {1572DWARFUnit *U = nullptr;1573auto It = LineToUnit.find(Offset);1574if (It != LineToUnit.end())1575U = It->second;1576DebugLineData.setAddressSize(U ? U->getAddressByteSize() : 0);1577return U;1578}15791580bool DWARFDebugLine::SectionParser::hasValidVersion(uint64_t Offset) {1581DataExtractor::Cursor Cursor(Offset);1582auto [TotalLength, _] = DebugLineData.getInitialLength(Cursor);1583DWARFDataExtractor HeaderData(DebugLineData, Cursor.tell() + TotalLength);1584uint16_t Version = HeaderData.getU16(Cursor);1585if (!Cursor) {1586// Ignore any error here.1587// If this is not the end of the section parseNext() will still be1588// attempted, where this error will occur again (and can be handled).1589consumeError(Cursor.takeError());1590return false;1591}1592return versionIsSupported(Version);1593}15941595void DWARFDebugLine::SectionParser::moveToNextTable(uint64_t OldOffset,1596const Prologue &P) {1597// If the length field is not valid, we don't know where the next table is, so1598// cannot continue to parse. Mark the parser as done, and leave the Offset1599// value as it currently is. This will be the end of the bad length field.1600if (!P.totalLengthIsValid()) {1601Done = true;1602return;1603}16041605Offset = OldOffset + P.TotalLength + P.sizeofTotalLength();1606if (!DebugLineData.isValidOffset(Offset)) {1607Done = true;1608return;1609}16101611// Heuristic: If the version is valid, then this is probably a line table.1612// Otherwise, the offset might need alignment (to a 4 or 8 byte boundary).1613if (hasValidVersion(Offset))1614return;16151616// ARM C/C++ Compiler aligns each line table to word boundaries and pads out1617// the .debug_line section to a word multiple. Note that in the specification1618// this does not seem forbidden since each unit has a DW_AT_stmt_list.1619for (unsigned Align : {4, 8}) {1620uint64_t AlignedOffset = alignTo(Offset, Align);1621if (!DebugLineData.isValidOffset(AlignedOffset)) {1622// This is almost certainly not another line table but some alignment1623// padding. This assumes the alignments tested are ordered, and are1624// smaller than the header size (which is true for 4 and 8).1625Done = true;1626return;1627}1628if (hasValidVersion(AlignedOffset)) {1629Offset = AlignedOffset;1630break;1631}1632}1633}163416351636