Skip to content

Commit 7a0990c

Browse files
DataCorruptedLukacma
authored andcommitted
[gsymutil] Fix how invalid LLVM_stmt_seq is set and used (llvm#164015)
Previously, invalid offset is set to UINT64_MAX, this is not right when DWARF32, which leads to incorrect debug into in GSYM, the branch: ``` if (StmtSeqVal != UNIT64_MAX) StmtSeqOffset = StmtSeqVal; ``` will always be true. In this PR, [commit 1](llvm@b1983d6) sets up a test that demonstrates the problem, [commit 2](llvm@0d58ce4) fixes it. [Diffing commit 1 and 2](llvm@0d58ce4#diff-019bdbc9922ad34fdfbcb524a9805f5af26c432540e76b87a6a5f73d9e0e853aL44) in this PR shows how after the PR the symbolicated line number changed from function definition to function body
1 parent d7809b0 commit 7a0990c

File tree

4 files changed

+220
-114
lines changed

4 files changed

+220
-114
lines changed

llvm/include/llvm/BinaryFormat/Dwarf.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,6 +1128,9 @@ struct FormParams {
11281128
uint8_t getDwarfOffsetByteSize() const {
11291129
return dwarf::getDwarfOffsetByteSize(Format);
11301130
}
1131+
inline uint64_t getDwarfMaxOffset() const {
1132+
return (getDwarfOffsetByteSize() == 4) ? UINT32_MAX : UINT64_MAX;
1133+
}
11311134

11321135
explicit operator bool() const { return Version && AddrSize; }
11331136
};

llvm/lib/DWARFLinker/Classic/DWARFLinker.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2427,11 +2427,13 @@ void DWARFLinker::DIECloner::generateLineTableForUnit(CompileUnit &Unit) {
24272427
uint64_t OrigStmtSeq = StmtSeq.get();
24282428
// 1. Get the original row index from the stmt list offset.
24292429
auto OrigRowIter = SeqOffToOrigRow.find(OrigStmtSeq);
2430+
const uint64_t InvalidOffset =
2431+
Unit.getOrigUnit().getFormParams().getDwarfMaxOffset();
24302432
// Check whether we have an output sequence for the StmtSeq offset.
24312433
// Some sequences are discarded by the DWARFLinker if they are invalid
24322434
// (empty).
24332435
if (OrigRowIter == SeqOffToOrigRow.end()) {
2434-
StmtSeq.set(UINT64_MAX);
2436+
StmtSeq.set(InvalidOffset);
24352437
continue;
24362438
}
24372439
size_t OrigRowIndex = OrigRowIter->second;
@@ -2441,7 +2443,7 @@ void DWARFLinker::DIECloner::generateLineTableForUnit(CompileUnit &Unit) {
24412443
if (NewRowIter == OrigRowToNewRow.end()) {
24422444
// If the original row index is not found in the map, update the
24432445
// stmt_sequence attribute to the 'invalid offset' magic value.
2444-
StmtSeq.set(UINT64_MAX);
2446+
StmtSeq.set(InvalidOffset);
24452447
continue;
24462448
}
24472449

llvm/lib/DebugInfo/GSYM/DwarfTransformer.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -320,12 +320,16 @@ static void convertFunctionLineTable(OutputAggregator &Out, CUInfo &CUI,
320320
// Attempt to retrieve DW_AT_LLVM_stmt_sequence if present.
321321
std::optional<uint64_t> StmtSeqOffset;
322322
if (auto StmtSeqAttr = Die.find(llvm::dwarf::DW_AT_LLVM_stmt_sequence)) {
323-
// The `DW_AT_LLVM_stmt_sequence` attribute might be set to `UINT64_MAX`
324-
// when it refers to an empty line sequence. In such cases, the DWARF linker
325-
// will exclude the empty sequence from the final output and assign
326-
// `UINT64_MAX` to the `DW_AT_LLVM_stmt_sequence` attribute.
327-
uint64_t StmtSeqVal = dwarf::toSectionOffset(StmtSeqAttr, UINT64_MAX);
328-
if (StmtSeqVal != UINT64_MAX)
323+
// The `DW_AT_LLVM_stmt_sequence` attribute might be set to an invalid
324+
// sentinel value when it refers to an empty line sequence. In such cases,
325+
// the DWARF linker will exclude the empty sequence from the final output
326+
// and assign the sentinel value to the `DW_AT_LLVM_stmt_sequence`
327+
// attribute. The sentinel value is UINT32_MAX for DWARF32 and UINT64_MAX
328+
// for DWARF64.
329+
const uint64_t InvalidOffset =
330+
Die.getDwarfUnit()->getFormParams().getDwarfMaxOffset();
331+
uint64_t StmtSeqVal = dwarf::toSectionOffset(StmtSeqAttr, InvalidOffset);
332+
if (StmtSeqVal != InvalidOffset)
329333
StmtSeqOffset = StmtSeqVal;
330334
}
331335

0 commit comments

Comments
 (0)