diff --git a/llvm/include/llvm/DebugInfo/DWARF/DWARFContext.h b/llvm/include/llvm/DebugInfo/DWARF/DWARFContext.h --- a/llvm/include/llvm/DebugInfo/DWARF/DWARFContext.h +++ b/llvm/include/llvm/DebugInfo/DWARF/DWARFContext.h @@ -75,7 +75,6 @@ DWARFUnitVector DWOUnits; std::unique_ptr AbbrevDWO; - std::unique_ptr LocDWO; /// The maximum DWARF version of all units. unsigned MaxVersion = 0; @@ -260,9 +259,6 @@ /// Get a pointer to the parsed dwo abbreviations object. const DWARFDebugAbbrev *getDebugAbbrevDWO(); - /// Get a pointer to the parsed DebugLoc object. - const DWARFDebugLoclists *getDebugLocDWO(); - /// Get a pointer to the parsed DebugAranges object. const DWARFDebugAranges *getDebugAranges(); diff --git a/llvm/include/llvm/DebugInfo/DWARF/DWARFDebugLoc.h b/llvm/include/llvm/DebugInfo/DWARF/DWARFDebugLoc.h --- a/llvm/include/llvm/DebugInfo/DWARF/DWARFDebugLoc.h +++ b/llvm/include/llvm/DebugInfo/DWARF/DWARFDebugLoc.h @@ -76,6 +76,9 @@ class DWARFDebugLoclists { public: + // Unconstructible. + DWARFDebugLoclists() = delete; + struct Entry { uint8_t Kind; uint64_t Offset; @@ -87,35 +90,30 @@ DIDumpOptions DumpOpts, unsigned Indent, size_t MaxEncodingStringLength) const; }; - struct LocationList { - uint64_t Offset; - SmallVector Entries; - void dump(raw_ostream &OS, uint64_t BaseAddr, bool IsLittleEndian, - unsigned AddressSize, const MCRegisterInfo *RegInfo, - DWARFUnit *U, DIDumpOptions DumpOpts, unsigned Indent) const; - }; - -private: - using LocationLists = SmallVector; - - LocationLists Locations; - - unsigned AddressSize; - - bool IsLittleEndian; - -public: - void parse(const DWARFDataExtractor &data, uint64_t Offset, - uint64_t EndOffset, uint16_t Version); - void dump(raw_ostream &OS, uint64_t BaseAddr, const MCRegisterInfo *RegInfo, - DIDumpOptions DumpOpts, Optional Offset) const; - - /// Return the location list at the given offset or nullptr. - LocationList const *getLocationListAtOffset(uint64_t Offset) const; - - static Expected - parseOneLocationList(const DWARFDataExtractor &Data, uint64_t *Offset, - unsigned Version); + /// Call the user-provided callback for each entry (including the end-of-list + /// entry) in the location list starting at \p Offset. The callback can return + /// false to terminate the iteration early. Returns an error if it was unable + /// to parse the entire location list correctly. Upon successful termination + /// \p Offset will be updated point past the end of the list. + static Error visitLocationList(const DWARFDataExtractor &Data, + uint64_t *Offset, uint16_t Version, + llvm::function_ref F); + + /// Dump the location list at the given \p Offset. The function returns true + /// iff it has successfully reched the end of the list. This means that one + /// can attempt to parse another list after the current one (\p Offset will be + /// updated to point past the end of the current list). + static bool dumpLocationList(const DWARFDataExtractor &Data, uint64_t *Offset, + uint16_t Version, raw_ostream &OS, + uint64_t BaseAddr, const MCRegisterInfo *MRI, + DWARFUnit *U, DIDumpOptions DumpOpts, + unsigned Indent); + + /// Dump all location lists within the given offset range. + static void dumpRange(const DWARFDataExtractor &Data, uint64_t StartOffset, + uint64_t EndOffset, uint16_t Version, raw_ostream &OS, + uint64_t BaseAddr, const MCRegisterInfo *MRI, + DIDumpOptions DumpOpts); }; } // end namespace llvm diff --git a/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp b/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp --- a/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp +++ b/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp @@ -300,11 +300,21 @@ Header.dump(OS, DumpOpts); - DWARFDebugLoclists Loclists; uint64_t EndOffset = Header.length() + Header.getHeaderOffset(); Data.setAddressSize(Header.getAddrSize()); - Loclists.parse(Data, Offset, EndOffset, Header.getVersion()); - Loclists.dump(OS, 0, MRI, DumpOpts, DumpOffset); + if (DumpOffset) { + if (DumpOffset >= Offset && DumpOffset < EndOffset) { + Offset = *DumpOffset; + DWARFDebugLoclists::dumpLocationList(Data, &Offset, Header.getVersion(), + OS, /*BaseAddr=*/0, MRI, nullptr, + DumpOpts, /*Indent=*/0); + OS << "\n"; + return; + } + } else { + DWARFDebugLoclists::dumpRange(Data, Offset, EndOffset, + Header.getVersion(), OS, 0, MRI, DumpOpts); + } Offset = EndOffset; } } @@ -393,7 +403,19 @@ if (const auto *Off = shouldDump(ExplicitDWO, ".debug_loc.dwo", DIDT_ID_DebugLoc, DObj->getLocDWOSection().Data)) { - getDebugLocDWO()->dump(OS, 0, getRegisterInfo(), DumpOpts, *Off); + DWARFDataExtractor Data(*DObj, DObj->getLocDWOSection(), isLittleEndian(), + 4); + if (*Off) { + uint64_t Offset = **Off; + DWARFDebugLoclists::dumpLocationList(Data, &Offset, /*Version=*/4, OS, + /*BaseAddr=*/0, getRegisterInfo(), + nullptr, DumpOpts, /*Indent=*/0); + OS << "\n"; + } else { + DWARFDebugLoclists::dumpRange(Data, 0, Data.getData().size(), + /*Version=*/4, OS, /*BaseAddr=*/0, + getRegisterInfo(), DumpOpts); + } } if (const auto *Off = shouldDump(Explicit, ".debug_frame", DIDT_ID_DebugFrame, @@ -724,23 +746,6 @@ return Loc.get(); } -const DWARFDebugLoclists *DWARFContext::getDebugLocDWO() { - if (LocDWO) - return LocDWO.get(); - - LocDWO.reset(new DWARFDebugLoclists()); - // Assume all compile units have the same address byte size. - // FIXME: We don't need AddressSize for split DWARF since relocatable - // addresses cannot appear there. At the moment DWARFExpression requires it. - DWARFDataExtractor LocData(*DObj, DObj->getLocDWOSection(), isLittleEndian(), - 4); - // Use version 4. DWO does not support the DWARF v5 .debug_loclists yet and - // that means we are parsing the new style .debug_loc (pre-standatized version - // of the .debug_loclists). - LocDWO->parse(LocData, 0, LocData.getData().size(), 4 /* Version */); - return LocDWO.get(); -} - const DWARFDebugAranges *DWARFContext::getDebugAranges() { if (Aranges) return Aranges.get(); diff --git a/llvm/lib/DebugInfo/DWARF/DWARFDebugLoc.cpp b/llvm/lib/DebugInfo/DWARF/DWARFDebugLoc.cpp --- a/llvm/lib/DebugInfo/DWARF/DWARFDebugLoc.cpp +++ b/llvm/lib/DebugInfo/DWARF/DWARFDebugLoc.cpp @@ -139,19 +139,19 @@ } } -Expected -DWARFDebugLoclists::parseOneLocationList(const DWARFDataExtractor &Data, - uint64_t *Offset, unsigned Version) { - LocationList LL; - LL.Offset = *Offset; - DataExtractor::Cursor C(*Offset); +Error DWARFDebugLoclists::visitLocationList( + const DWARFDataExtractor &Data, uint64_t *Offset, uint16_t Version, + llvm::function_ref F) { - // dwarf::DW_LLE_end_of_list_entry is 0 and indicates the end of the list. - while (auto Kind = Data.getU8(C)) { + DataExtractor::Cursor C(*Offset); + bool Continue = true; + while (Continue) { Entry E; - E.Kind = Kind; - E.Offset = C.tell() - 1; - switch (Kind) { + E.Offset = C.tell(); + E.Kind = Data.getU8(C); + switch (E.Kind) { + case dwarf::DW_LLE_end_of_list: + break; case dwarf::DW_LLE_base_addressx: E.Value0 = Data.getULEB128(C); break; @@ -164,10 +164,6 @@ else E.Value1 = Data.getULEB128(C); break; - case dwarf::DW_LLE_start_length: - E.Value0 = Data.getRelocatedAddress(C); - E.Value1 = Data.getULEB128(C); - break; case dwarf::DW_LLE_offset_pair: E.Value0 = Data.getULEB128(C); E.Value1 = Data.getULEB128(C); @@ -175,53 +171,66 @@ case dwarf::DW_LLE_base_address: E.Value0 = Data.getRelocatedAddress(C); break; + case dwarf::DW_LLE_start_length: + E.Value0 = Data.getRelocatedAddress(C); + E.Value1 = Data.getULEB128(C); + break; + case dwarf::DW_LLE_startx_endx: + case dwarf::DW_LLE_default_location: + case dwarf::DW_LLE_start_end: default: cantFail(C.takeError()); return createStringError(errc::illegal_byte_sequence, - "LLE of kind %x not supported", (int)Kind); + "LLE of kind %x not supported", (int)E.Kind); } - if (Kind != dwarf::DW_LLE_base_address && - Kind != dwarf::DW_LLE_base_addressx) { + if (E.Kind != dwarf::DW_LLE_base_address && + E.Kind != dwarf::DW_LLE_base_addressx && + E.Kind != dwarf::DW_LLE_end_of_list) { unsigned Bytes = Version >= 5 ? Data.getULEB128(C) : Data.getU16(C); // A single location description describing the location of the object... Data.getU8(C, E.Loc, Bytes); } - LL.Entries.push_back(std::move(E)); + if (!C) + return C.takeError(); + Continue = F(E) && E.Kind != dwarf::DW_LLE_end_of_list; } - if (Error Err = C.takeError()) - return std::move(Err); - Entry E; - E.Kind = dwarf::DW_LLE_end_of_list; - E.Offset = C.tell() - 1; - LL.Entries.push_back(E); *Offset = C.tell(); - return LL; + return Error::success(); } -void DWARFDebugLoclists::parse(const DWARFDataExtractor &data, uint64_t Offset, - uint64_t EndOffset, uint16_t Version) { - IsLittleEndian = data.isLittleEndian(); - AddressSize = data.getAddressSize(); - - while (Offset < EndOffset) { - if (auto LL = parseOneLocationList(data, &Offset, Version)) - Locations.push_back(std::move(*LL)); - else { - logAllUnhandledErrors(LL.takeError(), WithColor::error()); - return; - } +bool DWARFDebugLoclists::dumpLocationList(const DWARFDataExtractor &Data, + uint64_t *Offset, uint16_t Version, + raw_ostream &OS, uint64_t BaseAddr, + const MCRegisterInfo *MRI, + DWARFUnit *U, DIDumpOptions DumpOpts, + unsigned Indent) { + size_t MaxEncodingStringLength = 0; + if (DumpOpts.Verbose) { + uint64_t OffsetCopy = *Offset; + consumeError( + visitLocationList(Data, &OffsetCopy, Version, [&](const Entry &E) { + MaxEncodingStringLength = + std::max(MaxEncodingStringLength, + dwarf::LocListEncodingString(E.Kind).size()); + return true; + })); } -} -DWARFDebugLoclists::LocationList const * -DWARFDebugLoclists::getLocationListAtOffset(uint64_t Offset) const { - auto It = partition_point( - Locations, [=](const LocationList &L) { return L.Offset < Offset; }); - if (It != Locations.end() && It->Offset == Offset) - return &(*It); - return nullptr; + OS << format("0x%8.8" PRIx64 ": ", *Offset); + Error E = visitLocationList(Data, Offset, Version, [&](const Entry &E) { + E.dump(OS, BaseAddr, Data.isLittleEndian(), Data.getAddressSize(), MRI, U, + DumpOpts, Indent, MaxEncodingStringLength); + return true; + }); + if (E) { + OS << "\n"; + OS.indent(Indent); + OS << "error: " << toString(std::move(E)); + return false; + } + return true; } void DWARFDebugLoclists::Entry::dump(raw_ostream &OS, uint64_t &BaseAddr, @@ -297,44 +306,21 @@ dumpExpression(OS, Loc, IsLittleEndian, AddressSize, MRI, U); } -void DWARFDebugLoclists::LocationList::dump(raw_ostream &OS, uint64_t BaseAddr, - bool IsLittleEndian, - unsigned AddressSize, - const MCRegisterInfo *MRI, - DWARFUnit *U, - DIDumpOptions DumpOpts, - unsigned Indent) const { - size_t MaxEncodingStringLength = 0; - if (DumpOpts.Verbose) - for (const auto &Entry : Entries) - MaxEncodingStringLength = - std::max(MaxEncodingStringLength, - dwarf::LocListEncodingString(Entry.Kind).size()); - - for (const Entry &E : Entries) - E.dump(OS, BaseAddr, IsLittleEndian, AddressSize, MRI, U, DumpOpts, Indent, - MaxEncodingStringLength); -} -void DWARFDebugLoclists::dump(raw_ostream &OS, uint64_t BaseAddr, - const MCRegisterInfo *MRI, DIDumpOptions DumpOpts, - Optional Offset) const { - auto DumpLocationList = [&](const LocationList &L) { - OS << format("0x%8.8" PRIx64 ": ", L.Offset); - L.dump(OS, BaseAddr, IsLittleEndian, AddressSize, MRI, nullptr, DumpOpts, - /*Indent=*/12); - OS << "\n"; - }; - - if (Offset) { - if (auto *L = getLocationListAtOffset(*Offset)) - DumpLocationList(*L); - return; - } - - for (const LocationList &L : Locations) { - DumpLocationList(L); - if (&L != &Locations.back()) - OS << '\n'; +void DWARFDebugLoclists::dumpRange(const DWARFDataExtractor &Data, + uint64_t StartOffset, uint64_t EndOffset, + uint16_t Version, raw_ostream &OS, + uint64_t BaseAddr, const MCRegisterInfo *MRI, + DIDumpOptions DumpOpts) { + uint64_t Offset = StartOffset; + StringRef Separator; + bool CanContinue = true; + while (CanContinue && Data.isValidOffset(Offset) && Offset < EndOffset) { + OS << Separator; + Separator = "\n"; + + CanContinue = dumpLocationList(Data, &Offset, Version, OS, BaseAddr, MRI, + nullptr, DumpOpts, /*Indent=*/12); + OS << '\n'; } } diff --git a/llvm/lib/DebugInfo/DWARF/DWARFDie.cpp b/llvm/lib/DebugInfo/DWARF/DWARFDie.cpp --- a/llvm/lib/DebugInfo/DWARF/DWARFDie.cpp +++ b/llvm/lib/DebugInfo/DWARF/DWARFDie.cpp @@ -91,30 +91,32 @@ return; } - FormValue.dump(OS, DumpOpts); - const auto &DumpLL = [&](auto ExpectedLL) { - if (ExpectedLL) { - uint64_t BaseAddr = 0; - if (Optional BA = U->getBaseAddress()) - BaseAddr = BA->Address; - auto LLDumpOpts = DumpOpts; - LLDumpOpts.Verbose = false; - ExpectedLL->dump(OS, BaseAddr, Ctx.isLittleEndian(), Obj.getAddressSize(), - MRI, U, LLDumpOpts, Indent); - } else { - OS << '\n'; - OS.indent(Indent); - OS << formatv("error extracting location list: {0}", - fmt_consume(ExpectedLL.takeError())); - } - }; if (FormValue.isFormClass(DWARFFormValue::FC_SectionOffset)) { uint64_t Offset = *FormValue.getAsSectionOffset(); + uint64_t BaseAddr = 0; + if (Optional BA = U->getBaseAddress()) + BaseAddr = BA->Address; + auto LLDumpOpts = DumpOpts; + LLDumpOpts.Verbose = false; + if (!U->isDWOUnit() && !U->getLocSection()->Data.empty()) { DWARFDebugLoc DebugLoc; DWARFDataExtractor Data(Obj, *U->getLocSection(), Ctx.isLittleEndian(), Obj.getAddressSize()); - DumpLL(DebugLoc.parseOneLocationList(Data, &Offset)); + + FormValue.dump(OS, DumpOpts); + OS << ": "; + + if (Expected LL = + DebugLoc.parseOneLocationList(Data, &Offset)) { + LL->dump(OS, BaseAddr, Ctx.isLittleEndian(), Obj.getAddressSize(), MRI, + U, LLDumpOpts, Indent); + } else { + OS << '\n'; + OS.indent(Indent); + OS << formatv("error extracting location list: {0}", + fmt_consume(LL.takeError())); + } return; } @@ -132,10 +134,14 @@ // Modern locations list (.debug_loclists) are used starting from v5. // Ideally we should take the version from the .debug_loclists section // header, but using CU's version for simplicity. - DumpLL(DWARFDebugLoclists::parseOneLocationList( - Data, &Offset, UseLocLists ? U->getVersion() : 4)); + DWARFDebugLoclists::dumpLocationList( + Data, &Offset, UseLocLists ? U->getVersion() : 4, OS, BaseAddr, MRI, + U, LLDumpOpts, Indent); } + return; } + + FormValue.dump(OS, DumpOpts); } /// Dump the name encoded in the type tag. diff --git a/llvm/test/DebugInfo/X86/dwarfdump-debug-loclists-error-cases2.s b/llvm/test/DebugInfo/X86/dwarfdump-debug-loclists-error-cases2.s --- a/llvm/test/DebugInfo/X86/dwarfdump-debug-loclists-error-cases2.s +++ b/llvm/test/DebugInfo/X86/dwarfdump-debug-loclists-error-cases2.s @@ -8,11 +8,11 @@ # CHECK: DW_AT_name ("x1") # CHECK-NEXT: DW_AT_location (0xdeadbeef -# CHECK-NEXT: error extracting location list: unexpected end of data) +# CHECK-NEXT: error: unexpected end of data) # CHECK: DW_AT_name ("x2") # CHECK-NEXT: DW_AT_location (0x00000025 -# CHECK-NEXT: error extracting location list: unexpected end of data) +# CHECK-NEXT: error: unexpected end of data) .type f,@function diff --git a/llvm/test/DebugInfo/X86/fission-ranges.ll b/llvm/test/DebugInfo/X86/fission-ranges.ll --- a/llvm/test/DebugInfo/X86/fission-ranges.ll +++ b/llvm/test/DebugInfo/X86/fission-ranges.ll @@ -32,10 +32,10 @@ ; CHECK: DW_TAG_formal_parameter ; CHECK-NEXT: DW_AT_const_value [DW_FORM_sdata] (1) ; CHECK-NEXT: DW_AT_name {{.*}} "p") -; CHECK: DW_AT_location [DW_FORM_sec_offset] ([[A:0x[0-9a-z]*]] -; CHECK: DW_AT_location [DW_FORM_sec_offset] ([[E:0x[0-9a-z]*]] -; CHECK: DW_AT_location [DW_FORM_sec_offset] ([[B:0x[0-9a-z]*]] -; CHECK: DW_AT_location [DW_FORM_sec_offset] ([[D:0x[0-9a-z]*]] +; CHECK: DW_AT_location [DW_FORM_sec_offset] ([[A:0x[0-9a-z]*]]: +; CHECK: DW_AT_location [DW_FORM_sec_offset] ([[E:0x[0-9a-z]*]]: +; CHECK: DW_AT_location [DW_FORM_sec_offset] ([[B:0x[0-9a-z]*]]: +; CHECK: DW_AT_location [DW_FORM_sec_offset] ([[D:0x[0-9a-z]*]]: ; CHECK: DW_AT_ranges [DW_FORM_sec_offset] (0x00000000 ; CHECK-NOT: .debug_loc contents: ; CHECK-NOT: Beginning address offset diff --git a/llvm/test/DebugInfo/X86/stack-value-piece.ll b/llvm/test/DebugInfo/X86/stack-value-piece.ll --- a/llvm/test/DebugInfo/X86/stack-value-piece.ll +++ b/llvm/test/DebugInfo/X86/stack-value-piece.ll @@ -18,14 +18,14 @@ ; CHECK: DW_TAG_subprogram ; CHECK: DW_AT_name {{.*}} "i" ; CHECK: DW_TAG_variable -; CHECK-NEXT: DW_AT_location {{.*}} ([[I:.*]] +; CHECK-NEXT: DW_AT_location {{.*}} ([[I:0x[0-9a-f]+]] ; CHECK-NEXT: [{{.*}}, {{.*}}): DW_OP_reg5 RDI, DW_OP_piece 0x4, DW_OP_lit0, DW_OP_stack_value, DW_OP_piece 0x4) ; CHECK-NEXT: DW_AT_name {{.*}} "r" ; ; CHECK: DW_TAG_subprogram ; CHECK: DW_AT_name {{.*}} "f" ; CHECK: DW_TAG_variable -; CHECK-NEXT: DW_AT_location {{.*}} ([[F:.*]] +; CHECK-NEXT: DW_AT_location {{.*}} ([[F:0x[0-9a-f]+]] ; CHECK-NEXT: [{{.*}}, {{.*}}): DW_OP_reg17 XMM0, DW_OP_piece 0x4, DW_OP_lit0, DW_OP_stack_value, DW_OP_piece 0x4 ; CHECK-NEXT: [{{.*}}, {{.*}}): DW_OP_piece 0x4, DW_OP_lit0, DW_OP_stack_value, DW_OP_piece 0x4) ; CHECK-NEXT: DW_AT_name {{.*}} "r"