[llvm] [BOLT][DWARF] Fix unit layout with forward DW_FORM_ref_udata references (PR #226076)
Tim Besard via llvm-commits
llvm-commits at lists.llvm.org
Sun Oct 4 01:22:54 PDT 2026
https://github.com/maleadt updated https://github.com/llvm/llvm-project/pull/226076
>From 2840bd567a9a52ddf086f6394444bafd8f4ba74f Mon Sep 17 00:00:00 2001
From: Tim Besard <tim.besard at gmail.com>
Date: Thu, 24 Sep 2026 10:14:31 +0200
Subject: [PATCH] [BOLT][DWARF] Fix unit layout with forward DW_FORM_ref_udata
references
finalizeDIEs() sizes a DW_FORM_ref_udata reference from the referenced
DIE's current offset. For a forward reference, that is still the input
section offset set by allocDIE(), not the unit-relative offset that is
emitted later. If their ULEB128 sizes differ, the unit length and all
following DIE offsets are wrong.
GNU as emits such references in DWARF 5 units for assembly files, e.g.
libgcc's AArch64 outline-atomics helpers.
Emit these references as DW_FORM_ref4 instead, as BOLT already does for
type references in location expressions.
Assisted-by: Claude Code (Opus 5.5)
---
bolt/lib/Core/DIEBuilder.cpp | 8 ++-
bolt/test/X86/dwarf5-form-ref-udata.s | 70 +++++++++++++++++++++------
2 files changed, 62 insertions(+), 16 deletions(-)
diff --git a/bolt/lib/Core/DIEBuilder.cpp b/bolt/lib/Core/DIEBuilder.cpp
index fd2b872d66eae6..527cbdbcdf787d 100644
--- a/bolt/lib/Core/DIEBuilder.cpp
+++ b/bolt/lib/Core/DIEBuilder.cpp
@@ -701,8 +701,12 @@ void DIEBuilder::cloneDieOffsetReferenceAttribute(
return;
}
- Die.addValue(getState().DIEAlloc, AttrSpec.Attr, AttrSpec.Form,
- DIEEntry(*NewRefDie));
+ // The size of a DW_FORM_ref_udata depends on the referenced DIE's offset,
+ // which is not final yet for forward references. Use a fixed-size form.
+ const dwarf::Form Form = AttrSpec.Form == dwarf::DW_FORM_ref_udata
+ ? dwarf::DW_FORM_ref4
+ : AttrSpec.Form;
+ Die.addValue(getState().DIEAlloc, AttrSpec.Attr, Form, DIEEntry(*NewRefDie));
}
void DIEBuilder::cloneStringAttribute(
diff --git a/bolt/test/X86/dwarf5-form-ref-udata.s b/bolt/test/X86/dwarf5-form-ref-udata.s
index 0b63a1711423db..028c2b974e84af 100644
--- a/bolt/test/X86/dwarf5-form-ref-udata.s
+++ b/bolt/test/X86/dwarf5-form-ref-udata.s
@@ -2,16 +2,30 @@
# RUN: llvm-mc -dwarf-version=5 -filetype=obj -triple x86_64-unknown-linux %s -o %t.o
# RUN: %clang %cflags -dwarf-5 %t.o -o %t.exe -Wl,-q
+# RUN: llvm-dwarfdump --show-form --verbose --debug-info %t.exe | \
+# RUN: FileCheck %s --check-prefix CHECK-INPUT
# RUN: llvm-bolt %t.exe -o %t.bolt --update-debug-sections 2>&1 | \
# RUN: FileCheck %s --check-prefix CHECK-BOLT
-# RUN: llvm-dwarfdump --show-form --verbose --debug-info %t.bolt | FileCheck %s
+# RUN: llvm-dwarfdump --show-form --verbose --debug-info %t.bolt 2>&1 | \
+# RUN: FileCheck %s --implicit-check-not=warning:
+# RUN: llvm-dwarfdump --verify %t.bolt | FileCheck %s --check-prefix CHECK-VERIFY
-## Verify BOLT preserves DW_FORM_ref_udata (CU-relative ULEB128 DIE reference),
-## a form GCC may emit instead of DW_FORM_ref4.
+## Verify BOLT rewrites DW_FORM_ref_udata (CU-relative ULEB128) references as
+## DW_FORM_ref4. The subprogram in the second CU forward-references a DIE whose
+## section offset needs a two-byte ULEB128 but whose CU offset needs one. BOLT
+## used to size the reference by the former, corrupting the unit layout.
+# CHECK-INPUT: DW_AT_type [DW_FORM_ref_udata] (cu + 0x{{[0-7]?[0-9a-f]}} => {0x000000{{[89a-f][0-9a-f]}}} "long")
-# CHECK: DW_TAG_subprogram
-# CHECK: DW_AT_type [DW_FORM_ref_udata]
+# CHECK: DW_TAG_compile_unit
+# CHECK: DW_TAG_variable
+# CHECK: DW_AT_type [DW_FORM_ref4]
# CHECK-SAME: "int"
+# CHECK: DW_TAG_compile_unit
+# CHECK: DW_TAG_subprogram
+# CHECK: DW_AT_type [DW_FORM_ref4]
+# CHECK-SAME: "long"
+
+# CHECK-VERIFY: No errors.
# CHECK-BOLT-NOT: BOLT-WARNING
@@ -31,21 +45,30 @@ main:
.section .debug_abbrev,"", at progbits
.byte 1, 17, 1 # CU, has children
+ .byte 37, 8 # DW_AT_producer, DW_FORM_string
+ .byte 3, 8 # DW_AT_name, DW_FORM_string
+ .byte 0, 0
+ .byte 2, 52, 0 # variable, no children
+ .byte 3, 8 # DW_AT_name, DW_FORM_string
+ .byte 73, 21 # DW_AT_type, DW_FORM_ref_udata
+ .byte 0, 0
+ .byte 3, 36, 0 # base_type, no children
+ .byte 3, 8 # DW_AT_name, DW_FORM_string
+ .byte 0, 0
+ .byte 4, 17, 1 # CU, has children
.byte 17, 1 # DW_AT_low_pc, DW_FORM_addr
.byte 18, 6 # DW_AT_high_pc, DW_FORM_data4
.byte 16, 23 # DW_AT_stmt_list, DW_FORM_sec_offset
.byte 0, 0
- .byte 2, 46, 0 # subprogram, no children
+ .byte 5, 46, 0 # subprogram, no children
.byte 17, 1 # DW_AT_low_pc, DW_FORM_addr
.byte 18, 6 # DW_AT_high_pc, DW_FORM_data4
.byte 73, 21 # DW_AT_type, DW_FORM_ref_udata
.byte 0, 0
- .byte 3, 36, 0 # base_type, no children
- .byte 3, 8 # DW_AT_name, DW_FORM_string
- .byte 0, 0
.byte 0
.section .debug_info,"", at progbits
+## Pushes the second CU past offset 0x80.
.Lcu_begin0:
.long .Ldebug_info_end0-.Ldebug_info_start0
.Ldebug_info_start0:
@@ -54,17 +77,36 @@ main:
.byte 8 # Address size
.long .debug_abbrev # Abbrev offset
.byte 1 # CU
+ .asciz "GNU C17 13.2.0 -mtune=generic -march=x86-64 -g -O2" # DW_AT_producer
+ .asciz "/src/lib/global_data.c" # DW_AT_name
+ .byte 2 # variable
+ .asciz "data" # DW_AT_name
+ .uleb128 .Ltype_int-.Lcu_begin0 # DW_AT_type (DW_FORM_ref_udata)
+.Ltype_int:
+ .byte 3 # base_type
+ .asciz "int" # DW_AT_name
+ .byte 0 # End children of CU
+.Ldebug_info_end0:
+
+.Lcu_begin1:
+ .long .Ldebug_info_end1-.Ldebug_info_start1
+.Ldebug_info_start1:
+ .short 5 # DWARF version
+ .byte 1 # DW_UT_compile
+ .byte 8 # Address size
+ .long .debug_abbrev # Abbrev offset
+ .byte 4 # CU
.quad .Lfunc_begin0 # DW_AT_low_pc
.long .Lfunc_end0-.Lfunc_begin0 # DW_AT_high_pc
.long .Lline_table_start0 # DW_AT_stmt_list
- .byte 2 # subprogram
+ .byte 5 # subprogram
.quad .Lfunc_begin0 # DW_AT_low_pc
.long .Lfunc_end0-.Lfunc_begin0 # DW_AT_high_pc
- .uleb128 .Ltype_int-.Lcu_begin0 # DW_AT_type (DW_FORM_ref_udata)
-.Ltype_int:
+ .uleb128 .Ltype_long-.Lcu_begin1 # DW_AT_type (DW_FORM_ref_udata)
+.Ltype_long:
.byte 3 # base_type
- .asciz "int" # DW_AT_name
+ .asciz "long" # DW_AT_name
.byte 0 # End children of CU
-.Ldebug_info_end0:
+.Ldebug_info_end1:
.section .debug_line,"", at progbits
.Lline_table_start0:
More information about the llvm-commits
mailing list