debug: Normalize address-sized unsigned conversions [PR127676, PR127680]

Message ID 4a77aa4a-8df3-4a6f-8082-d35811c85120@gmail.com
State New
Headers
Series debug: Normalize address-sized unsigned conversions [PR127676, PR127680] |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gcc_build--master-aarch64 fail Patch failed to apply
linaro-tcwg-bot/tcwg_gcc_build--master-arm fail Patch failed to apply
linaro-tcwg-bot/tcwg_simplebootstrap_build--master-aarch64-bootstrap fail Patch failed to apply

Commit Message

Jielun Wu Nov. 3, 2026, 1:47 p.m. UTC
  Address-sized unsigned conversions can mix typed and generic DWARF
operands, producing invalid location expressions.  Change the size check
in mem_loc_descriptor from < to <= to normalize the result, fixing
PR debug/127676 and PR debug/127680.

gcc/ChangeLog:

	PR debug/127676
	PR debug/127680
	* dwarf2out.cc (mem_loc_descriptor): Convert address-sized
	UNSIGNED_FIX results to the generic DWARF type.

gcc/testsuite/ChangeLog:

	PR debug/127676
	PR debug/127680
	* gcc.dg/debug/dwarf2/pr127676.c: New test.
	* gcc.dg/debug/dwarf2/pr127680.c: Likewise.

Signed-off-by: Jielun Wu <firmiana402@gmail.com>
---
Bootstrapped and regression tested on x86_64-pc-linux-gnu with no new
regressions.

OK for trunk?

  gcc/dwarf2out.cc                             |  2 +-
  gcc/testsuite/gcc.dg/debug/dwarf2/pr127676.c | 18 ++++++++++++++++++
  gcc/testsuite/gcc.dg/debug/dwarf2/pr127680.c | 18 ++++++++++++++++++
  3 files changed, 37 insertions(+), 1 deletion(-)
  create mode 100644 gcc/testsuite/gcc.dg/debug/dwarf2/pr127676.c
  create mode 100644 gcc/testsuite/gcc.dg/debug/dwarf2/pr127680.c

+/* { dg-final { scan-assembler-times {DW_OP_convert[\r\n]+[ 
\t]*\.uleb128[ \t]+0[\r\n]} 1 } } */
  

Patch

diff --git a/gcc/dwarf2out.cc b/gcc/dwarf2out.cc
index 9964fdc8da8..dd0ee53888c 100644
--- a/gcc/dwarf2out.cc
+++ b/gcc/dwarf2out.cc
@@ -17212,7 +17212,7 @@  mem_loc_descriptor (rtx rtl, machine_mode mode,
  	  add_loc_descr (&op0, cvt);
  	  if (is_a <scalar_int_mode> (mode, &int_mode)
  	      && (GET_CODE (rtl) == FIX
-		  || GET_MODE_SIZE (int_mode) < DWARF2_ADDR_SIZE))
+		  || GET_MODE_SIZE (int_mode) <= DWARF2_ADDR_SIZE))
  	    {
  	      op0 = convert_descriptor_to_mode (int_mode, op0);
  	      if (op0 == NULL)
diff --git a/gcc/testsuite/gcc.dg/debug/dwarf2/pr127676.c 
b/gcc/testsuite/gcc.dg/debug/dwarf2/pr127676.c
new file mode 100644
index 00000000000..7b801aa190d
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/debug/dwarf2/pr127676.c
@@ -0,0 +1,18 @@ 
+/* PR debug/127676 */
+/* { dg-do compile { target { { i?86-*-* x86_64-*-* } && lp64 } } } */
+/* { dg-require-effective-target gas } */
+/* { dg-options "-O2 -gdwarf-5 -dA -fvar-tracking-assignments -fno-pie" 
} */
+
+typedef unsigned long long u64;
+
+__attribute__((noinline, noclone)) u64
+bad_large_addend (double input)
+{
+  u64 value = (u64) input + 0x100000000ULL;
+  __asm__ volatile ("nop" ::: "memory");
+  return 0;
+}
+
+/* The address-sized unsigned conversion must return to the generic DWARF
+   type before adding a generic constant.  */
+/* { dg-final { scan-assembler-times {DW_OP_convert[\r\n]+[ 
\t]*\.uleb128[ \t]+0[\r\n]} 1 } } */
diff --git a/gcc/testsuite/gcc.dg/debug/dwarf2/pr127680.c 
b/gcc/testsuite/gcc.dg/debug/dwarf2/pr127680.c
new file mode 100644
index 00000000000..b0f6dd45ad8
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/debug/dwarf2/pr127680.c
@@ -0,0 +1,18 @@ 
+/* PR debug/127680 */
+/* { dg-do compile { target { { i?86-*-* x86_64-*-* } && lp64 } } } */
+/* { dg-require-effective-target gas } */
+/* { dg-options "-O2 -gdwarf-5 -dA -fvar-tracking-assignments -fno-pie" 
} */
+
+typedef unsigned long long u64;
+
+__attribute__((noinline, noclone)) int
+less_than (double input)
+{
+  int result = (u64) input < 0x100000000ULL;
+  __asm__ volatile ("nop" ::: "memory");
+  return 0;
+}
+
+/* The address-sized unsigned conversion must return to the generic DWARF
+   type before comparing with a generic constant.  */