Skip to content

[dsymutil] Fix line table sequence mapping for stmt_sequence attributes #143656

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions llvm/lib/DWARFLinker/Classic/DWARFLinker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2297,8 +2297,37 @@ void DWARFLinker::DIECloner::generateLineTableForUnit(CompileUnit &Unit) {

// Create a map of stmt sequence offsets to original row indices.
DenseMap<uint64_t, unsigned> SeqOffToOrigRow;
for (const DWARFDebugLine::Sequence &Seq : LT->Sequences)
SeqOffToOrigRow[Seq.StmtSeqOffset] = Seq.FirstRowIndex;
// The DWARF parser's discovery of sequences can be incomplete. To
// ensure all DW_AT_LLVM_stmt_sequence attributes can be patched, we
// build a map from both the parser's results and a manual
// reconstruction.
if (!LT->Rows.empty()) {
// First, trust the sequences that the DWARF parser did identify.
for (const DWARFDebugLine::Sequence &Seq : LT->Sequences)
SeqOffToOrigRow.try_emplace(Seq.StmtSeqOffset, Seq.FirstRowIndex);

// Second, manually find sequence boundaries and match them to the
// sorted attributes to handle sequences the parser might have missed.
auto StmtAttrs = Unit.getStmtSeqListAttributes();
llvm::sort(StmtAttrs,
[](const PatchLocation &A, const PatchLocation &B) {
return A.get() < B.get();
});

std::vector<size_t> SeqStartRows;
SeqStartRows.push_back(0);
for (size_t i = 0; i < LT->Rows.size() - 1; ++i)
if (LT->Rows[i].EndSequence)
SeqStartRows.push_back(i + 1);

// Correlate the sorted attributes with the reconstructed sequence
// starts. This provides a partial mapping if counts are mismatched,
// maximizing the number of correctly patched attributes.
size_t NumMappings = std::min(StmtAttrs.size(), SeqStartRows.size());
for (size_t i = 0; i < NumMappings; ++i) {
SeqOffToOrigRow.try_emplace(StmtAttrs[i].get(), SeqStartRows[i]);
}
}

// Create a map of original row indices to new row indices.
DenseMap<size_t, size_t> OrigRowToNewRow;
Expand Down
7 changes: 6 additions & 1 deletion llvm/test/tools/dsymutil/ARM/stmt-seq-macho.test
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
# RUN: yaml2obj %t/stmt_seq_macho.exe.yaml -o %t/stmt_seq_macho.exe
# RUN: yaml2obj %t/stmt_seq_macho.o.yaml -o %t/stmt_seq_macho.o
# RUN: dsymutil --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 | sort | FileCheck %s -check-prefix=CHECK_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 | sort | FileCheck %s -check-prefix=CHECK_DSYM
# 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{{.*}}0xffffffff


# CHECK_DSYM: DW_AT_LLVM_stmt_sequence [DW_FORM_sec_offset] ([[OFFSET1:(0x[0-9a-f]+)]])
# CHECK_DSYM: DW_AT_LLVM_stmt_sequence [DW_FORM_sec_offset] ([[OFFSET2:(0x[0-9a-f]+)]])
Expand Down
Loading