[PATCH 2/2] gas: Anchor DWARF subprograms to the definitions

Longjun Luo luolongjuna@gmail.com
Tue Nov 24 11:51:36 GMT 2026


When GAS generates a DWARF subprogram, it uses the function symbol for
address reference.  For mechanisms like weak/strong and COMDAT,
a function may lose its original symbol after link.  In these
situations, the DIE may refer to the wrong address.

Generate a local start symbol for DWARF reference.  And copy the
value expression so that offsets in .set definitions can be
evaluated after relaxation.

Signed-off-by: Longjun Luo <luolongjuna@gmail.com>
---
 gas/config/tc-mips.c                          | 13 +++++++
 gas/config/tc-mips.h                          |  3 ++
 gas/doc/internals.texi                        |  5 +++
 gas/dwarf2dbg.c                               | 16 +++++++-
 .../gas/elf/dwarf-3-func-weak-reloc.d         | 10 +++++
 gas/testsuite/gas/elf/elf.exp                 |  1 +
 ld/testsuite/ld-arm/arm-elf.exp               |  1 +
 ld/testsuite/ld-arm/dwarf-func-strong.s       |  7 ++++
 ld/testsuite/ld-arm/dwarf-func-thumb-range.d  | 38 +++++++++++++++++++
 ld/testsuite/ld-arm/dwarf-func-thumb.s        | 16 ++++++++
 ld/testsuite/ld-elf/dwarf-func-comdat-range.d | 26 +++++++++++++
 ld/testsuite/ld-elf/dwarf-func-comdat.s       |  8 ++++
 ld/testsuite/ld-elf/dwarf-func-strong.s       |  7 ++++
 ld/testsuite/ld-elf/dwarf-func-weak-range.d   | 24 ++++++++++++
 ld/testsuite/ld-elf/dwarf-func-weak.s         | 17 +++++++++
 ld/testsuite/ld-mips-elf/dwarf-func-strong.s  | 10 +++++
 .../ld-mips-elf/dwarf-func-weak-range.d       | 28 ++++++++++++++
 ld/testsuite/ld-mips-elf/dwarf-func-weak.s    | 22 +++++++++++
 ld/testsuite/ld-mips-elf/mips-elf.exp         |  3 ++
 19 files changed, 253 insertions(+), 2 deletions(-)
 create mode 100644 gas/testsuite/gas/elf/dwarf-3-func-weak-reloc.d
 create mode 100644 ld/testsuite/ld-arm/dwarf-func-strong.s
 create mode 100644 ld/testsuite/ld-arm/dwarf-func-thumb-range.d
 create mode 100644 ld/testsuite/ld-arm/dwarf-func-thumb.s
 create mode 100644 ld/testsuite/ld-elf/dwarf-func-comdat-range.d
 create mode 100644 ld/testsuite/ld-elf/dwarf-func-comdat.s
 create mode 100644 ld/testsuite/ld-elf/dwarf-func-strong.s
 create mode 100644 ld/testsuite/ld-elf/dwarf-func-weak-range.d
 create mode 100644 ld/testsuite/ld-elf/dwarf-func-weak.s
 create mode 100644 ld/testsuite/ld-mips-elf/dwarf-func-strong.s
 create mode 100644 ld/testsuite/ld-mips-elf/dwarf-func-weak-range.d
 create mode 100644 ld/testsuite/ld-mips-elf/dwarf-func-weak.s

diff --git a/gas/config/tc-mips.c b/gas/config/tc-mips.c
index 6733bd810f1..04d6660f3cd 100644
--- a/gas/config/tc-mips.c
+++ b/gas/config/tc-mips.c
@@ -20881,6 +20881,19 @@ mips_dwarf2_addr_size (void)
     return 4;
 }
 
+/* Save the ISA mode in st_other for MIPS.  */
+
+void
+mips_dwarf2_function_start (symbolS *start, symbolS *func)
+{
+  unsigned int other = S_GET_OTHER (func);
+
+  if (ELF_ST_IS_MIPS16 (other))
+    S_SET_OTHER (start, STO_MIPS16);
+  else if (ELF_ST_IS_MICROMIPS (other))
+    S_SET_OTHER (start, STO_MICROMIPS);
+}
+
 /* Standard calling conventions leave the CFA at SP on entry.  */
 void
 mips_cfi_frame_initial_instructions (void)
diff --git a/gas/config/tc-mips.h b/gas/config/tc-mips.h
index 9e668293416..0f9c17552c7 100644
--- a/gas/config/tc-mips.h
+++ b/gas/config/tc-mips.h
@@ -189,6 +189,9 @@ extern int mips_dwarf2_addr_size (void);
 #define DWARF2_FDE_RELOC_ENCODING(enc) \
   (enc | (compact_eh ? DW_EH_PE_pcrel : 0))
 
+#define tc_dwarf2_function_start mips_dwarf2_function_start
+extern void mips_dwarf2_function_start (symbolS *, symbolS *);
+
 #define TARGET_USE_CFIPOP 1
 
 #define tc_cfi_frame_initial_instructions mips_cfi_frame_initial_instructions
diff --git a/gas/doc/internals.texi b/gas/doc/internals.texi
index d8345dd1283..116b2da2b3b 100644
--- a/gas/doc/internals.texi
+++ b/gas/doc/internals.texi
@@ -1567,6 +1567,11 @@ It should return the size of an address, as it should be represented in
 debugging info.  If you don't define this macro, the default definition uses
 the number of bits per address, as defined in @var{bfd}, divided by 8.
 
+@item tc_dwarf2_function_start (@var{start}, @var{func})
+@cindex tc_dwarf2_function_start
+If defined, GAS calls this macro to copy target-specific attributes from
+@var{func} to the local symbol @var{start} used for DWARF subprogram ranges.
+
 @item   MD_DEBUG_FORMAT_SELECTOR
 @cindex MD_DEBUG_FORMAT_SELECTOR
 If defined this macro is the name of a function to be called when the
diff --git a/gas/dwarf2dbg.c b/gas/dwarf2dbg.c
index 3eb7f3468e4..9dc353fa37a 100644
--- a/gas/dwarf2dbg.c
+++ b/gas/dwarf2dbg.c
@@ -2936,6 +2936,7 @@ out_debug_info (segT info_seg, segT abbrev_seg, segT line_seg, segT str_seg,
 	{
 	  const char *name;
 	  size_t len;
+	  symbolS *start;
 	  expressionS size = { .X_op = O_constant };
 
 	  /* Skip warning constructs (see above).  */
@@ -2990,9 +2991,20 @@ out_debug_info (segT info_seg, segT abbrev_seg, segT line_seg, segT str_seg,
 	      emit_leb128_expr (&exp, 0);
 	    }
 
+	  /* After linking, original symbols for weak or COMDAT functions
+	     may be overridden.  Generate a temp symbol for the following
+	     references and keep the value expression for future calculation.  */
+	  start = symbol_temp_new (S_GET_SEGMENT (symp),
+				   symbol_get_frag (symp), 0);
+	  symbol_set_value_expression (start,
+				       symbol_get_value_expression (symp));
+#ifdef tc_dwarf2_function_start
+	  tc_dwarf2_function_start (start, symp);
+#endif
+
 	  /* DW_AT_low_pc */
 	  exp.X_op = O_symbol;
-	  exp.X_add_symbol = symp;
+	  exp.X_add_symbol = start;
 	  exp.X_add_number = 0;
 	  emit_expr (&exp, sizeof_address);
 
@@ -3001,7 +3013,7 @@ out_debug_info (segT info_seg, segT abbrev_seg, segT line_seg, segT str_seg,
 	    {
 	      if (size.X_op == O_constant)
 		size.X_op = O_symbol;
-	      size.X_add_symbol = symp;
+	      size.X_add_symbol = start;
 	      emit_expr (&size, sizeof_address);
 	    }
 	  else if (size.X_op == O_constant)
diff --git a/gas/testsuite/gas/elf/dwarf-3-func-weak-reloc.d b/gas/testsuite/gas/elf/dwarf-3-func-weak-reloc.d
new file mode 100644
index 00000000000..bf03dd507da
--- /dev/null
+++ b/gas/testsuite/gas/elf/dwarf-3-func-weak-reloc.d
@@ -0,0 +1,10 @@
+#as: --gdwarf-3
+#name: DWARF weak function address relocations
+#source: dwarf-3-func-weak.s
+#objdump: -r -j .debug_info
+#target: i?86-*-* x86_64-*-*
+
+#failif
+#...
+[0-9a-f]+ +R_[^ ]+ +wfunc([+-].*)?
+#...
diff --git a/gas/testsuite/gas/elf/elf.exp b/gas/testsuite/gas/elf/elf.exp
index d39c30cf711..bdd81f06ac2 100644
--- a/gas/testsuite/gas/elf/elf.exp
+++ b/gas/testsuite/gas/elf/elf.exp
@@ -360,6 +360,7 @@ if { [is_elf_format] } then {
     run_dump_test "dwarf-5-func-local" $dump_opts
     run_dump_test "dwarf-3-func-weak" $dump_opts
     run_dump_test "dwarf-5-func-weak" $dump_opts
+    run_dump_test "dwarf-3-func-weak-reloc" $dump_opts
 
     # Exclude targets defining ONLY_STANDARD_ESCAPES. It's not clear how these
     # are supposed to reference macro arguments in double-quoted strings.
diff --git a/ld/testsuite/ld-arm/arm-elf.exp b/ld/testsuite/ld-arm/arm-elf.exp
index f50c0c1795c..f697b753ba5 100644
--- a/ld/testsuite/ld-arm/arm-elf.exp
+++ b/ld/testsuite/ld-arm/arm-elf.exp
@@ -421,6 +421,7 @@ run_dump_test "group-relocs-ldrs-bad"
 run_dump_test "group-relocs-ldrs-bad-2"
 run_dump_test "group-relocs-ldc-bad"
 run_dump_test "group-relocs-ldc-bad-2"
+run_dump_test "dwarf-func-thumb-range"
 run_dump_test "thumb2-bl-undefweak"
 run_dump_test "thumb2-bl-undefweak1"
 run_dump_test "emit-relocs1"
diff --git a/ld/testsuite/ld-arm/dwarf-func-strong.s b/ld/testsuite/ld-arm/dwarf-func-strong.s
new file mode 100644
index 00000000000..c443b62d055
--- /dev/null
+++ b/ld/testsuite/ld-arm/dwarf-func-strong.s
@@ -0,0 +1,7 @@
+	.text
+	.arm
+	.globl f
+	.type f, %function
+f:
+	bx lr
+	.size f, .-f
diff --git a/ld/testsuite/ld-arm/dwarf-func-thumb-range.d b/ld/testsuite/ld-arm/dwarf-func-thumb-range.d
new file mode 100644
index 00000000000..a34261219e3
--- /dev/null
+++ b/ld/testsuite/ld-arm/dwarf-func-thumb-range.d
@@ -0,0 +1,38 @@
+#name: Thumb DWARF ranges and function pointers
+#source: dwarf-func-thumb.s
+#source: dwarf-func-strong.s
+#as: -march=armv4t --gdwarf-3
+#ld: -Ttext=0x1000 -e f
+#readelf: -wi -x .data
+
+# These words point to the selected ARM f and to Thumb weak_only.
+#...
+Hex dump of section '\.data':
+  0x[0-9a-f]+ (08100000 03100000|00001008 00001003) .*
+
+#...
+ .*DW_AT_name +: .*: f
+#...
+ .*DW_AT_low_pc +: 0x1000
+ .*DW_AT_high_pc +: 0x1002
+#...
+ .*DW_AT_name +: .*: weak_only
+#...
+ .*DW_AT_low_pc +: 0x1002
+ .*DW_AT_high_pc +: 0x1004
+#...
+ .*DW_AT_name +: .*: thumb_global
+#...
+ .*DW_AT_low_pc +: 0x1004
+ .*DW_AT_high_pc +: 0x1006
+#...
+ .*DW_AT_name +: .*: thumb_local
+#...
+ .*DW_AT_low_pc +: 0x1006
+ .*DW_AT_high_pc +: 0x1008
+#...
+ .*DW_AT_name +: .*: f
+#...
+ .*DW_AT_low_pc +: 0x1008
+ .*DW_AT_high_pc +: 0x100c
+#pass
diff --git a/ld/testsuite/ld-arm/dwarf-func-thumb.s b/ld/testsuite/ld-arm/dwarf-func-thumb.s
new file mode 100644
index 00000000000..24d43512516
--- /dev/null
+++ b/ld/testsuite/ld-arm/dwarf-func-thumb.s
@@ -0,0 +1,16 @@
+	.text
+	.thumb
+	.weak f, weak_only
+	.globl thumb_global
+	.local thumb_local
+
+	.irp name, f, weak_only, thumb_global, thumb_local
+	.type \name, %function
+	.thumb_func
+\name:
+	bx lr
+	.size \name, .-\name
+	.endr
+
+	.data
+	.word f, weak_only
diff --git a/ld/testsuite/ld-elf/dwarf-func-comdat-range.d b/ld/testsuite/ld-elf/dwarf-func-comdat-range.d
new file mode 100644
index 00000000000..965d26bae4f
--- /dev/null
+++ b/ld/testsuite/ld-elf/dwarf-func-comdat-range.d
@@ -0,0 +1,26 @@
+#name: DWARF ranges for COMDAT functions of different sizes
+#target: i?86-*-linux* x86_64-*-linux*
+#source: dwarf-func-comdat.s --defsym nops=2
+#source: dwarf-func-comdat.s --defsym nops=8
+#as: --gdwarf-5
+#ld: -Ttext=0x1000 -e f
+#readelf: -wi
+
+# The two-byte definition is kept.  The discarded function and its
+# compilation unit both have a zero start and retain their size.
+#...
+ .*\(DW_TAG_subprogram\)
+#...
+ .*DW_AT_low_pc +: 0x1000
+ .*DW_AT_high_pc +: 2
+#...
+ .*\(DW_TAG_compile_unit\)
+#...
+ .*DW_AT_low_pc +: 0
+ .*DW_AT_high_pc +: 8
+#...
+ .*\(DW_TAG_subprogram\)
+#...
+ .*DW_AT_low_pc +: 0
+ .*DW_AT_high_pc +: 8
+#pass
diff --git a/ld/testsuite/ld-elf/dwarf-func-comdat.s b/ld/testsuite/ld-elf/dwarf-func-comdat.s
new file mode 100644
index 00000000000..a48de70ec7a
--- /dev/null
+++ b/ld/testsuite/ld-elf/dwarf-func-comdat.s
@@ -0,0 +1,8 @@
+	.section .text.f,"axG",@progbits,f,comdat
+	.globl f
+	.type f, @function
+f:
+	.rept nops
+	nop
+	.endr
+	.size f, .-f
diff --git a/ld/testsuite/ld-elf/dwarf-func-strong.s b/ld/testsuite/ld-elf/dwarf-func-strong.s
new file mode 100644
index 00000000000..1f10fe7e3bc
--- /dev/null
+++ b/ld/testsuite/ld-elf/dwarf-func-strong.s
@@ -0,0 +1,7 @@
+	.text
+	.globl f
+	.type f, @function
+f:
+	xor %eax, %eax
+	ret
+	.size f, .-f
diff --git a/ld/testsuite/ld-elf/dwarf-func-weak-range.d b/ld/testsuite/ld-elf/dwarf-func-weak-range.d
new file mode 100644
index 00000000000..597fcdb8849
--- /dev/null
+++ b/ld/testsuite/ld-elf/dwarf-func-weak-range.d
@@ -0,0 +1,24 @@
+#name: DWARF ranges for weak and strong function definitions
+#target: i?86-*-linux* x86_64-*-linux*
+#source: dwarf-func-weak.s
+#source: dwarf-func-strong.s
+#as: --gdwarf-3
+#ld: -Ttext=0x1000 -e f
+#readelf: -wi
+
+#...
+ .*DW_AT_name +: .*: f
+#...
+ .*DW_AT_low_pc +: 0x1000
+ .*DW_AT_high_pc +: 0x1006
+#...
+ .*DW_AT_name +: .*: aligned_func
+#...
+ .*DW_AT_low_pc +: 0x1010
+ .*DW_AT_high_pc +: 0x1011
+#...
+ .*DW_AT_name +: .*: f
+#...
+ .*DW_AT_low_pc +: 0x1011
+ .*DW_AT_high_pc +: 0x1014
+#pass
diff --git a/ld/testsuite/ld-elf/dwarf-func-weak.s b/ld/testsuite/ld-elf/dwarf-func-weak.s
new file mode 100644
index 00000000000..1da1491bdc6
--- /dev/null
+++ b/ld/testsuite/ld-elf/dwarf-func-weak.s
@@ -0,0 +1,17 @@
+	.text
+	.weak f
+	.type f, @function
+f:
+.Lstart:
+	mov $11, %eax
+	ret
+	.size f, .-f
+
+	.p2align 4
+.Laligned:
+	ret
+
+	# Before layout the difference is zero.  After alignment it is 16.
+	.type aligned_func, @function
+	.set aligned_func, .Lstart + (.Laligned - .Lstart)
+	.size aligned_func, 1
diff --git a/ld/testsuite/ld-mips-elf/dwarf-func-strong.s b/ld/testsuite/ld-mips-elf/dwarf-func-strong.s
new file mode 100644
index 00000000000..25de0805ee0
--- /dev/null
+++ b/ld/testsuite/ld-mips-elf/dwarf-func-strong.s
@@ -0,0 +1,10 @@
+	.text
+	.set nomips16
+	.set nomicromips
+	.set noreorder
+	.p2align 4
+	.globl f
+	.type f, @function
+f:
+	nop
+	.size f, .-f
diff --git a/ld/testsuite/ld-mips-elf/dwarf-func-weak-range.d b/ld/testsuite/ld-mips-elf/dwarf-func-weak-range.d
new file mode 100644
index 00000000000..0ad6a13dff9
--- /dev/null
+++ b/ld/testsuite/ld-mips-elf/dwarf-func-weak-range.d
@@ -0,0 +1,28 @@
+#name: DWARF ranges for compressed and standard MIPS functions
+#source: dwarf-func-weak.s
+#source: dwarf-func-strong.s
+#as: -mips32r2 --gdwarf-3
+#ld: -Ttext=0x1000 -e f
+#readelf: -wi
+
+#...
+ .*DW_AT_name +: .*: f
+#...
+ .*DW_AT_low_pc +: 0x1001
+ .*DW_AT_high_pc +: 0x1003
+#...
+ .*DW_AT_name +: .*: compressed
+#...
+ .*DW_AT_low_pc +: 0x1003
+ .*DW_AT_high_pc +: 0x1005
+#...
+ .*DW_AT_name +: .*: weak_only
+#...
+ .*DW_AT_low_pc +: 0x1007
+ .*DW_AT_high_pc +: 0x1009
+#...
+ .*DW_AT_name +: .*: f
+#...
+ .*DW_AT_low_pc +: 0x1010
+ .*DW_AT_high_pc +: 0x1014
+#pass
diff --git a/ld/testsuite/ld-mips-elf/dwarf-func-weak.s b/ld/testsuite/ld-mips-elf/dwarf-func-weak.s
new file mode 100644
index 00000000000..d90b2db8dfd
--- /dev/null
+++ b/ld/testsuite/ld-mips-elf/dwarf-func-weak.s
@@ -0,0 +1,22 @@
+	.text
+	.set noreorder
+	.weak f
+	.type f, @function
+f:
+	nop
+	.size f, .-f
+
+	.globl compressed
+	.type compressed, @function
+compressed:
+	nop
+	.size compressed, .-compressed
+
+	# weak_only is halfword-aligned but has an odd offset in its frag.
+	.space 1
+	.byte 0
+	.weak weak_only
+	.type weak_only, @function
+weak_only:
+	nop
+	.size weak_only, .-weak_only
diff --git a/ld/testsuite/ld-mips-elf/mips-elf.exp b/ld/testsuite/ld-mips-elf/mips-elf.exp
index f347302a621..a1e2605b3ef 100644
--- a/ld/testsuite/ld-mips-elf/mips-elf.exp
+++ b/ld/testsuite/ld-mips-elf/mips-elf.exp
@@ -1151,6 +1151,9 @@ if { $has_abi(o32) } {
     run_ld_link_tests $mips16_intermix_test
 }
 
+run_dump_test_o32 "dwarf-func-weak-range" {{name (MIPS16)} {as -mips16}}
+run_dump_test_o32 "dwarf-func-weak-range" {{name (microMIPS)} {as -mmicromips}}
+
 run_dump_test_o32 "mips16-local-stubs-1"
 run_dump_test_n32 "mips16-local-stubs-1" [list [list name (n32)] \
 					       [list xfail *-*-irix6*]]
-- 
2.55.0



More information about the Binutils mailing list