[llvm] [DWARFLinker] Patch DW_AT_LLVM_stmt_sequence in the parallel linker (PR #195388)
Jonas Devlieghere via llvm-commits
llvm-commits at lists.llvm.org
Fri May 1 17:03:34 PDT 2026
https://github.com/JDevlieghere created https://github.com/llvm/llvm-project/pull/195388
Mirror dsymutil's stmt-sequence rewriting in the parallel linker so each attribute ends up pointing at the DW_LNE_set_address that opens its containing output sequence, with the correct offset in the combined .debug_line.
At DIE cloning time we resolve each attribute's input offset to the address of its first row and record the pair (DIEValue, address) on the CompileUnit, alongside a DebugOffsetPatch on the .debug_info section so combination adds the CU's .debug_line start offset. The line-table emitter then fills a map from row address to the byte offset of the sequence-opening DW_LNE_set_address.
After emission, each recorded attribute is rewritten by relocating its input address through the CU's function ranges and looking the result up in the map. When resolution fails the DWARF max-offset sentinel is written instead, and the patch applier preserves it unchanged.
First-row lookups share a lazy per-CU cache to keep resolution O(1) per attribute.
>From 8186fd07edcf369bd8613c548b38637ec1544410 Mon Sep 17 00:00:00 2001
From: Jonas Devlieghere <jonas at devlieghere.com>
Date: Fri, 1 May 2026 16:59:57 -0700
Subject: [PATCH] [DWARFLinker] Patch DW_AT_LLVM_stmt_sequence in the parallel
linker
Mirror dsymutil's stmt-sequence rewriting in the parallel linker so each
attribute ends up pointing at the DW_LNE_set_address that opens its
containing output sequence, with the correct offset in the combined
.debug_line.
At DIE cloning time we resolve each attribute's input offset to the
address of its first row and record the pair (DIEValue, address) on the
CompileUnit, alongside a DebugOffsetPatch on the .debug_info section so
combination adds the CU's .debug_line start offset. The line-table
emitter then fills a map from row address to the byte offset of the
sequence-opening DW_LNE_set_address.
After emission, each recorded attribute is rewritten by relocating its
input address through the CU's function ranges and looking the result up
in the map. When resolution fails the DWARF max-offset sentinel is
written instead, and the patch applier preserves it unchanged.
First-row lookups share a lazy per-CU cache to keep resolution O(1) per
attribute.
---
.../Parallel/DIEAttributeCloner.cpp | 30 ++-
.../Parallel/DWARFLinkerCompileUnit.cpp | 186 ++++++++++++------
.../Parallel/DWARFLinkerCompileUnit.h | 49 +++++
.../DWARFLinker/Parallel/DWARFLinkerUnit.cpp | 7 +-
.../DWARFLinker/Parallel/DWARFLinkerUnit.h | 6 +-
.../Parallel/DebugLineSectionEmitter.h | 17 +-
.../DWARFLinker/Parallel/OutputSections.cpp | 16 +-
.../dsymutil/AArch64/stmt-seq-macho.test | 25 ++-
8 files changed, 256 insertions(+), 80 deletions(-)
diff --git a/llvm/lib/DWARFLinker/Parallel/DIEAttributeCloner.cpp b/llvm/lib/DWARFLinker/Parallel/DIEAttributeCloner.cpp
index 07ebd55e2c46f..6a6f77d8406e2 100644
--- a/llvm/lib/DWARFLinker/Parallel/DIEAttributeCloner.cpp
+++ b/llvm/lib/DWARFLinker/Parallel/DIEAttributeCloner.cpp
@@ -514,8 +514,34 @@ size_t DIEAttributeCloner::cloneScalarAttr(
} else if (AttrSpec.Attr == dwarf::DW_AT_declaration && Value)
AttrInfo.IsDeclaration = true;
- return Generator.addScalarAttribute(AttrSpec.Attr, ResultingForm, Value)
- .second;
+ auto Result =
+ Generator.addScalarAttribute(AttrSpec.Attr, ResultingForm, Value);
+ // Record DW_AT_LLVM_stmt_sequence so the attribute value can be
+ // rewritten with the correct .debug_line offset after the line table
+ // for this CU has been emitted. We also register a DebugOffsetPatch so
+ // that the final-section offset of .debug_line gets added when the
+ // section is placed in the combined output. The assert encodes
+ // dsymutil's placement policy: subprograms (which carry
+ // DW_AT_LLVM_stmt_sequence) are placed in compile units, not type
+ // units, so this attribute should never appear on a type-unit DIE.
+ if (AttrSpec.Attr == dwarf::DW_AT_LLVM_stmt_sequence) {
+ assert(OutUnit.isCompileUnit() &&
+ "DW_AT_LLVM_stmt_sequence on a non-compile-unit DIE");
+ // Resolve the attribute's stmt-sequence offset to the address of the
+ // referenced sequence's first row, so that after line-table emission
+ // the attribute can be matched to the output sequence by address.
+ std::optional<uint64_t> InputFirstAddr =
+ InUnit.getStmtSeqFirstAddress(Value);
+ OutUnit.getAsCompileUnit()->noteStmtSeqListAttribute(&Result.first,
+ InputFirstAddr);
+ DebugInfoOutputSection.notePatchWithOffsetUpdate(
+ DebugOffsetPatch{
+ AttrOutOffset,
+ &OutUnit->getOrCreateSectionDescriptor(DebugSectionKind::DebugLine),
+ /*AddLocalValue=*/true},
+ PatchesOffsets);
+ }
+ return Result.second;
}
size_t DIEAttributeCloner::cloneBlockAttr(
diff --git a/llvm/lib/DWARFLinker/Parallel/DWARFLinkerCompileUnit.cpp b/llvm/lib/DWARFLinker/Parallel/DWARFLinkerCompileUnit.cpp
index 1e16716b29082..1ef4d39ed1314 100644
--- a/llvm/lib/DWARFLinker/Parallel/DWARFLinkerCompileUnit.cpp
+++ b/llvm/lib/DWARFLinker/Parallel/DWARFLinkerCompileUnit.cpp
@@ -1553,74 +1553,142 @@ Error CompileUnit::cloneAndEmitLineTable(const Triple &TargetTriple) {
OutLineTable.Rows.clear();
OutLineTable.Sequences = InputLineTable->Sequences;
- } else {
- // This vector is the output line table.
- std::vector<DWARFDebugLine::Row> NewRows;
- NewRows.reserve(InputLineTable->Rows.size());
-
- // Current sequence of rows being extracted, before being inserted
- // in NewRows.
- std::vector<DWARFDebugLine::Row> Seq;
-
- const auto &FunctionRanges = getFunctionRanges();
- std::optional<AddressRangeValuePair> CurrRange;
-
- // FIXME: This logic is meant to generate exactly the same output as
- // Darwin's classic dsymutil. There is a nicer way to implement this
- // by simply putting all the relocated line info in NewRows and simply
- // sorting NewRows before passing it to emitLineTableForUnit. This
- // should be correct as sequences for a function should stay
- // together in the sorted output. There are a few corner cases that
- // look suspicious though, and that required to implement the logic
- // this way. Revisit that once initial validation is finished.
-
- // Iterate over the object file line info and extract the sequences
- // that correspond to linked functions.
- for (DWARFDebugLine::Row Row : InputLineTable->Rows) {
- // Check whether we stepped out of the range. The range is
- // half-open, but consider accept the end address of the range if
- // it is marked as end_sequence in the input (because in that
- // case, the relocation offset is accurate and that entry won't
- // serve as the start of another function).
- if (!CurrRange || !CurrRange->Range.contains(Row.Address.Address)) {
- // We just stepped out of a known range. Insert a end_sequence
- // corresponding to the end of the range.
- uint64_t StopAddress =
- CurrRange ? CurrRange->Range.end() + CurrRange->Value : -1ULL;
- CurrRange = FunctionRanges.getRangeThatContains(Row.Address.Address);
- if (StopAddress != -1ULL && !Seq.empty()) {
- // Insert end sequence row with the computed end address, but
- // the same line as the previous one.
- auto NextLine = Seq.back();
- NextLine.Address.Address = StopAddress;
- NextLine.EndSequence = 1;
- NextLine.PrologueEnd = 0;
- NextLine.BasicBlock = 0;
- NextLine.EpilogueBegin = 0;
- Seq.push_back(NextLine);
- insertLineSequence(Seq, NewRows);
- }
+ return emitDebugLine(TargetTriple, OutLineTable);
+ }
- if (!CurrRange)
- continue;
+ filterLineTableRows(*InputLineTable, OutLineTable.Rows);
+
+ if (StmtSeqListAttributes.empty())
+ return emitDebugLine(TargetTriple, OutLineTable);
+
+ // When DW_AT_LLVM_stmt_sequence attributes on this CU need their values
+ // rewritten to point at the correct output sequence, have the emitter
+ // record, for every row, the byte offset of the DW_LNE_set_address that
+ // opens the sequence containing that row.
+ //
+ // The patching below MUST run before emitDebugInfo() serializes the
+ // DIE bytes and before OutputSections::applyPatches() runs for this
+ // unit's .debug_info — it writes a local offset into the DIEValue that
+ // the serializer then emits, and a DebugOffsetPatch (registered at DIE
+ // cloning time) later adds the CU's .debug_line start offset to reach
+ // the final absolute value.
+ DenseMap<uint64_t, uint64_t> AddrToSeqStartOffset;
+ if (Error Err =
+ emitDebugLine(TargetTriple, OutLineTable, &AddrToSeqStartOffset))
+ return Err;
+
+ patchStmtSeqAttributes(AddrToSeqStartOffset);
+ return Error::success();
+}
+
+void CompileUnit::filterLineTableRows(
+ const DWARFDebugLine::LineTable &InputLineTable,
+ std::vector<DWARFDebugLine::Row> &NewRows) {
+ NewRows.reserve(InputLineTable.Rows.size());
+
+ // Current sequence of rows being extracted, before being inserted
+ // in NewRows.
+ std::vector<DWARFDebugLine::Row> Seq;
+
+ const auto &FunctionRanges = getFunctionRanges();
+ std::optional<AddressRangeValuePair> CurrRange;
+
+ // FIXME: This logic is meant to generate exactly the same output as
+ // Darwin's classic dsymutil. There is a nicer way to implement this
+ // by simply putting all the relocated line info in NewRows and simply
+ // sorting NewRows before passing it to emitLineTableForUnit. This
+ // should be correct as sequences for a function should stay
+ // together in the sorted output. There are a few corner cases that
+ // look suspicious though, and that required to implement the logic
+ // this way. Revisit that once initial validation is finished.
+
+ // Iterate over the object file line info and extract the sequences
+ // that correspond to linked functions.
+ for (DWARFDebugLine::Row Row : InputLineTable.Rows) {
+ // Check whether we stepped out of the range. The range is
+ // half-open, but consider accept the end address of the range if
+ // it is marked as end_sequence in the input (because in that
+ // case, the relocation offset is accurate and that entry won't
+ // serve as the start of another function).
+ if (!CurrRange || !CurrRange->Range.contains(Row.Address.Address)) {
+ // We just stepped out of a known range. Insert a end_sequence
+ // corresponding to the end of the range.
+ uint64_t StopAddress =
+ CurrRange ? CurrRange->Range.end() + CurrRange->Value : -1ULL;
+ CurrRange = FunctionRanges.getRangeThatContains(Row.Address.Address);
+ if (StopAddress != -1ULL && !Seq.empty()) {
+ // Insert end sequence row with the computed end address, but
+ // the same line as the previous one.
+ auto NextLine = Seq.back();
+ NextLine.Address.Address = StopAddress;
+ NextLine.EndSequence = 1;
+ NextLine.PrologueEnd = 0;
+ NextLine.BasicBlock = 0;
+ NextLine.EpilogueBegin = 0;
+ Seq.push_back(NextLine);
+ insertLineSequence(Seq, NewRows);
}
- // Ignore empty sequences.
- if (Row.EndSequence && Seq.empty())
+ if (!CurrRange)
continue;
+ }
+
+ // Ignore empty sequences.
+ if (Row.EndSequence && Seq.empty())
+ continue;
- // Relocate row address and add it to the current sequence.
- Row.Address.Address += CurrRange->Value;
- Seq.emplace_back(Row);
+ // Relocate row address and add it to the current sequence.
+ Row.Address.Address += CurrRange->Value;
+ Seq.emplace_back(Row);
- if (Row.EndSequence)
- insertLineSequence(Seq, NewRows);
- }
+ if (Row.EndSequence)
+ insertLineSequence(Seq, NewRows);
+ }
+}
- OutLineTable.Rows = std::move(NewRows);
+void CompileUnit::patchStmtSeqAttributes(
+ const DenseMap<uint64_t, uint64_t> &AddrToSeqStartOffset) {
+ const uint64_t InvalidOffset = getFormParams().getDwarfMaxOffset();
+ const auto &FunctionRanges = getFunctionRanges();
+
+ for (const CompileUnit::StmtSeqPatch &Patch : StmtSeqListAttributes) {
+ uint64_t NewStmtSeq = InvalidOffset;
+ if (Patch.InputFirstAddr) {
+ if (auto Range =
+ FunctionRanges.getRangeThatContains(*Patch.InputFirstAddr)) {
+ uint64_t OutAddr = *Patch.InputFirstAddr + Range->Value;
+ auto It = AddrToSeqStartOffset.find(OutAddr);
+ if (It != AddrToSeqStartOffset.end())
+ NewStmtSeq = It->second;
+ }
+ }
+ // When resolution fails, the InvalidOffset sentinel must survive the
+ // combination-time section-offset fixup. The patch applier preserves
+ // InvalidOffset as-is so consumers see a clean invalid marker rather
+ // than StartOffset - 1.
+ *Patch.Value = DIEValue(Patch.Value->getAttribute(), Patch.Value->getForm(),
+ DIEInteger(NewStmtSeq));
}
+}
- return emitDebugLine(TargetTriple, OutLineTable);
+std::optional<uint64_t>
+CompileUnit::getStmtSeqFirstAddress(uint64_t StmtSeqOffset) {
+ if (!StmtSeqOffsetToFirstAddr) {
+ StmtSeqOffsetToFirstAddr.emplace();
+ if (const DWARFDebugLine::LineTable *InputLT =
+ getContaingFile().Dwarf->getLineTableForUnit(&getOrigUnit())) {
+ for (const DWARFDebugLine::Sequence &Seq : InputLT->Sequences) {
+ assert(Seq.FirstRowIndex < InputLT->Rows.size() &&
+ "sequence's first-row index out of range");
+ (*StmtSeqOffsetToFirstAddr)[Seq.StmtSeqOffset] =
+ InputLT->Rows[Seq.FirstRowIndex].Address.Address;
+ }
+ }
+ }
+ auto It = StmtSeqOffsetToFirstAddr->find(StmtSeqOffset);
+ if (It == StmtSeqOffsetToFirstAddr->end())
+ return std::nullopt;
+ return It->second;
}
void CompileUnit::insertLineSequence(std::vector<DWARFDebugLine::Row> &Seq,
diff --git a/llvm/lib/DWARFLinker/Parallel/DWARFLinkerCompileUnit.h b/llvm/lib/DWARFLinker/Parallel/DWARFLinkerCompileUnit.h
index 1c4a62c06934d..a0c8f36f8463a 100644
--- a/llvm/lib/DWARFLinker/Parallel/DWARFLinkerCompileUnit.h
+++ b/llvm/lib/DWARFLinker/Parallel/DWARFLinkerCompileUnit.h
@@ -407,6 +407,25 @@ class alignas(8) CompileUnit : public DwarfUnit {
/// Returns function ranges of this unit.
const RangesTy &getFunctionRanges() const { return Ranges; }
+ /// Record that a DW_AT_LLVM_stmt_sequence attribute on this unit
+ /// references the input line-table sequence starting at \p InputFirstAddr
+ /// (std::nullopt if the input offset couldn't be resolved to an input
+ /// row). After the line table for this unit has been emitted, \p V is
+ /// rewritten to the byte offset of the output sequence that contains
+ /// the relocated address (or to an invalid sentinel if the sequence
+ /// wasn't kept).
+ void noteStmtSeqListAttribute(DIEValue *V,
+ std::optional<uint64_t> InputFirstAddr) {
+ StmtSeqListAttributes.push_back({V, InputFirstAddr});
+ }
+
+ /// Return the input-side first-row address of the line-table sequence
+ /// whose header sits at \p StmtSeqOffset in this unit's input
+ /// .debug_line contribution, or std::nullopt if no such sequence was
+ /// recognised by the DWARF parser. Builds and caches a lookup table on
+ /// first call to keep stmt_sequence attribute resolution O(1).
+ std::optional<uint64_t> getStmtSeqFirstAddress(uint64_t StmtSeqOffset);
+
/// Clone and emit this compilation unit.
Error
cloneAndEmit(std::optional<std::reference_wrapper<const Triple>> TargetTriple,
@@ -651,6 +670,19 @@ class alignas(8) CompileUnit : public DwarfUnit {
void insertLineSequence(std::vector<DWARFDebugLine::Row> &Seq,
std::vector<DWARFDebugLine::Row> &Rows);
+ /// Filter \p InputLineTable's rows to those covered by this unit's
+ /// function ranges, relocating addresses in the process, and store the
+ /// result in \p NewRows.
+ void filterLineTableRows(const DWARFDebugLine::LineTable &InputLineTable,
+ std::vector<DWARFDebugLine::Row> &NewRows);
+
+ /// Rewrite every DW_AT_LLVM_stmt_sequence DIEValue recorded on this
+ /// unit with the local .debug_line offset of the output sequence
+ /// corresponding to its input-sequence first-row address. The map is
+ /// built during line-table emission.
+ void patchStmtSeqAttributes(
+ const DenseMap<uint64_t, uint64_t> &AddrToSeqStartOffset);
+
/// Emits body for both macro sections.
void emitMacroTableImpl(const DWARFDebugMacro *MacroTable,
uint64_t OffsetToMacroTable, bool hasDWARFv5Header);
@@ -721,6 +753,23 @@ class alignas(8) CompileUnit : public DwarfUnit {
/// The DW_AT_low_pc of each DW_TAG_label.
using LabelMapTy = SmallDenseMap<uint64_t, uint64_t, 1>;
LabelMapTy Labels;
+
+ /// Recorded DW_AT_LLVM_stmt_sequence attributes for this unit. Each
+ /// entry pairs the DIEValue holding the attribute with the input-side
+ /// first-row address of the referenced line-table sequence (empty if
+ /// the parser couldn't resolve the input offset); the value is
+ /// rewritten with the matching output offset after the line table has
+ /// been emitted.
+ struct StmtSeqPatch {
+ DIEValue *Value = nullptr;
+ std::optional<uint64_t> InputFirstAddr;
+ };
+ SmallVector<StmtSeqPatch, 4> StmtSeqListAttributes;
+
+ /// Lazily-built lookup table from an input DW_AT_LLVM_stmt_sequence
+ /// byte offset to the address of the corresponding sequence's first
+ /// row. Populated on the first call to getStmtSeqFirstAddress().
+ std::optional<DenseMap<uint64_t, uint64_t>> StmtSeqOffsetToFirstAddr;
std::mutex LabelsMutex;
/// This field keeps current stage of overall compile unit processing.
diff --git a/llvm/lib/DWARFLinker/Parallel/DWARFLinkerUnit.cpp b/llvm/lib/DWARFLinker/Parallel/DWARFLinkerUnit.cpp
index c992a2321c14b..9c9d62365e8b1 100644
--- a/llvm/lib/DWARFLinker/Parallel/DWARFLinkerUnit.cpp
+++ b/llvm/lib/DWARFLinker/Parallel/DWARFLinkerUnit.cpp
@@ -118,11 +118,12 @@ Error DwarfUnit::emitDebugInfo(const Triple &TargetTriple) {
return Error::success();
}
-Error DwarfUnit::emitDebugLine(const Triple &TargetTriple,
- const DWARFDebugLine::LineTable &OutLineTable) {
+Error DwarfUnit::emitDebugLine(
+ const Triple &TargetTriple, const DWARFDebugLine::LineTable &OutLineTable,
+ DenseMap<uint64_t, uint64_t> *AddrToSeqStartOffset) {
DebugLineSectionEmitter DebugLineEmitter(TargetTriple, *this);
- return DebugLineEmitter.emit(OutLineTable);
+ return DebugLineEmitter.emit(OutLineTable, AddrToSeqStartOffset);
}
Error DwarfUnit::emitDebugStringOffsetSection() {
diff --git a/llvm/lib/DWARFLinker/Parallel/DWARFLinkerUnit.h b/llvm/lib/DWARFLinker/Parallel/DWARFLinkerUnit.h
index 970abdc38f417..2871ef0c35962 100644
--- a/llvm/lib/DWARFLinker/Parallel/DWARFLinkerUnit.h
+++ b/llvm/lib/DWARFLinker/Parallel/DWARFLinkerUnit.h
@@ -96,8 +96,10 @@ class DwarfUnit : public OutputSections {
Error emitDebugInfo(const Triple &TargetTriple);
/// Emit .debug_line section.
- Error emitDebugLine(const Triple &TargetTriple,
- const DWARFDebugLine::LineTable &OutLineTable);
+ Error
+ emitDebugLine(const Triple &TargetTriple,
+ const DWARFDebugLine::LineTable &OutLineTable,
+ DenseMap<uint64_t, uint64_t> *AddrToSeqStartOffset = nullptr);
/// Emit the .debug_str_offsets section for current unit.
Error emitDebugStringOffsetSection();
diff --git a/llvm/lib/DWARFLinker/Parallel/DebugLineSectionEmitter.h b/llvm/lib/DWARFLinker/Parallel/DebugLineSectionEmitter.h
index 21cdf5887813f..d96e1b9301752 100644
--- a/llvm/lib/DWARFLinker/Parallel/DebugLineSectionEmitter.h
+++ b/llvm/lib/DWARFLinker/Parallel/DebugLineSectionEmitter.h
@@ -26,7 +26,8 @@ class DebugLineSectionEmitter {
DebugLineSectionEmitter(const Triple &TheTriple, DwarfUnit &U)
: TheTriple(TheTriple), U(U) {}
- Error emit(const DWARFDebugLine::LineTable &LineTable) {
+ Error emit(const DWARFDebugLine::LineTable &LineTable,
+ DenseMap<uint64_t, uint64_t> *AddrToSeqStartOffset = nullptr) {
// FIXME: remove dependence on MCDwarfLineAddr::encode.
// As we reuse MCDwarfLineAddr::encode, we need to create/initialize
// some MC* classes.
@@ -45,7 +46,7 @@ class DebugLineSectionEmitter {
emitLineTablePrologue(LineTable.Prologue, OutSection);
// Emit rows.
- emitLineTableRows(LineTable, OutSection);
+ emitLineTableRows(LineTable, OutSection, AddrToSeqStartOffset);
uint64_t OffsetAfterEnd = OutSection.OS.tell();
// Update unit length field with actual length value.
@@ -292,8 +293,9 @@ class DebugLineSectionEmitter {
emitLineTablePrologueV5IncludeAndFileTable(P, Section);
}
- void emitLineTableRows(const DWARFDebugLine::LineTable &LineTable,
- SectionDescriptor &Section) {
+ void emitLineTableRows(
+ const DWARFDebugLine::LineTable &LineTable, SectionDescriptor &Section,
+ DenseMap<uint64_t, uint64_t> *AddrToSeqStartOffset = nullptr) {
MCDwarfLineTableParams Params;
Params.DWARF2LineOpcodeBase = LineTable.Prologue.OpcodeBase;
@@ -321,10 +323,15 @@ class DebugLineSectionEmitter {
uint64_t Address = -1ULL;
unsigned RowsSinceLastSequence = 0;
+ // Offset of the DW_LNE_set_address opcode that opens the sequence
+ // currently being emitted. Recorded for each row's address so that
+ // DW_AT_LLVM_stmt_sequence attributes can be resolved by address.
+ uint64_t CurrentSeqStartOffset = 0;
for (const DWARFDebugLine::Row &Row : LineTable.Rows) {
int64_t AddressDelta;
if (Address == -1ULL) {
+ CurrentSeqStartOffset = Section.OS.tell();
Section.emitIntVal(dwarf::DW_LNS_extended_op, 1);
encodeULEB128(Section.getFormParams().AddrSize + 1, Section.OS);
Section.emitIntVal(dwarf::DW_LNE_set_address, 1);
@@ -335,6 +342,8 @@ class DebugLineSectionEmitter {
AddressDelta =
(Row.Address.Address - Address) / LineTable.Prologue.MinInstLength;
}
+ if (AddrToSeqStartOffset)
+ (*AddrToSeqStartOffset)[Row.Address.Address] = CurrentSeqStartOffset;
// FIXME: code copied and transformed from
// MCDwarf.cpp::EmitDwarfLineTable. We should find a way to share this
diff --git a/llvm/lib/DWARFLinker/Parallel/OutputSections.cpp b/llvm/lib/DWARFLinker/Parallel/OutputSections.cpp
index 9bcd2b895be88..079be51ace24a 100644
--- a/llvm/lib/DWARFLinker/Parallel/OutputSections.cpp
+++ b/llvm/lib/DWARFLinker/Parallel/OutputSections.cpp
@@ -460,9 +460,21 @@ void OutputSections::applyPatches(
uint64_t FinalValue = Patch.SectionPtr.getPointer()->StartOffset;
// Check whether we need to read value from the original location.
- if (Patch.SectionPtr.getInt())
- FinalValue +=
+ if (Patch.SectionPtr.getInt()) {
+ uint64_t LocalValue =
Section.getIntVal(Patch.PatchOffset, Format.getDwarfOffsetByteSize());
+ // DebugOffsetPatch treats the DWARF "invalid offset" sentinel
+ // (0xffffffff for DWARF32) as pass-through: callers that can't
+ // resolve the target write that value and expect it to survive
+ // section combination unchanged. Adding StartOffset would turn it
+ // into a plausible-looking but meaningless offset. Callers that
+ // genuinely want `StartOffset + MaxOffset` don't exist today and
+ // would need a different patch type.
+ if (LocalValue == Format.getDwarfMaxOffset())
+ FinalValue = LocalValue;
+ else
+ FinalValue += LocalValue;
+ }
Section.apply(Patch.PatchOffset, dwarf::DW_FORM_sec_offset, FinalValue);
});
diff --git a/llvm/test/tools/dsymutil/AArch64/stmt-seq-macho.test b/llvm/test/tools/dsymutil/AArch64/stmt-seq-macho.test
index 5cb3557b5becd..b3ecb3968125c 100644
--- a/llvm/test/tools/dsymutil/AArch64/stmt-seq-macho.test
+++ b/llvm/test/tools/dsymutil/AArch64/stmt-seq-macho.test
@@ -1,10 +1,11 @@
-## Test that verifies DW_AT_LLVM_stmt_sequence attributes are correctly patched in the dSYM
-## even when line table sequences need to be reordered due to address ordering.
-##
-## This test uses two object files (a.o and b.o) linked with b.o first so that
-## b's functions get lower addresses than a's functions. When dsymutil processes
-## the debug info, it must correctly handle the case where line table sequences
-## from different compilation units need to be interleaved by address.
+## Test that verifies DW_AT_LLVM_stmt_sequence attributes are correctly
+## patched in the dSYM. Each input object (a.o and b.o) carries one
+## compile unit whose line table has two sequences (one per function).
+## After linking, dsymutil merges each CU's two function sequences into a
+## single output sequence (adjacent addresses). We verify that every
+## DW_AT_LLVM_stmt_sequence attribute in the output points at a real
+## DW_LNE_set_address opcode in the emitted .debug_line, for both the
+## classic and parallel linker backends.
# RUN: rm -rf %t && split-file %s %t && cd %t
# RUN: yaml2obj %t/stmt_seq_macho.exe.yaml -o %t/stmt_seq_macho.exe
@@ -26,7 +27,15 @@
# RUN: cat %t/stmt_seq_macho.dSYM.txt | FileCheck %s -check-prefix=CHECK_NO_INVALID_OFFSET
# CHECK_NO_INVALID_OFFSET-NOT: DW_AT_LLVM_stmt_sequence{{.*}}0xfffffff
-## FIXME: Support --linker parallel
+# RUN: dsymutil --linker parallel --flat --verify-dwarf=none -oso-prepend-path %t %t/stmt_seq_macho.exe -o %t/stmt_seq_macho.dSYM
+# RUN: llvm-dwarfdump --debug-info --debug-line -v %t/stmt_seq_macho.dSYM > %t/stmt_seq_macho.dSYM.txt
+# RUN: cat %t/stmt_seq_macho.dSYM.txt | grep "DW_AT_LLVM_stmt_sequence" | \
+# RUN: sed 's/.*(\(0x[0-9a-f]*\)).*/\1/' | sort -u > %t/stmt_offsets.txt
+# RUN: cat %t/stmt_seq_macho.dSYM.txt | grep "DW_LNE_set_address" | \
+# RUN: sed 's/^\(0x[0-9a-f]*\):.*/\1/' | sort -u > %t/setaddr_offsets.txt
+# RUN: comm -23 %t/stmt_offsets.txt %t/setaddr_offsets.txt > %t/bad_offsets.txt
+# RUN: test ! -s %t/bad_offsets.txt
+# RUN: cat %t/stmt_seq_macho.dSYM.txt | FileCheck %s -check-prefix=CHECK_NO_INVALID_OFFSET
#--- stmt_seq_macho.cpp
// This file is split into a.cpp and b.cpp by the gen script.
More information about the llvm-commits
mailing list