[PATCH] gas: Mark weak functions as external in DWARF

Longjun Luo luolongjuna@gmail.com
Thu Nov 5 03:43:33 GMT 2026


S_IS_EXTERNAL only tests BSF_GLOBAL, which is cleared for weak symbols.
Subprogram DIEs generated for weak functions therefore either omit
DW_AT_external or set it to false, causing GDB to classify them as
static.

Check S_IS_WEAK alongside S_IS_EXTERNAL when emitting subprogram DIEs.

Signed-off-by: Longjun Luo <luolongjuna@gmail.com>
---
Regtested GAS on 121 target configurations, including x86_64-linux-gnu,
with no regressions.

This patch is independent of my pending "gas: Name the CU after the
primary source file" patch; either can be applied first.

 gas/dwarf2dbg.c                           |  4 +--
 gas/testsuite/gas/elf/dwarf-3-func-weak.d | 37 +++++++++++++++++++++
 gas/testsuite/gas/elf/dwarf-3-func-weak.s |  7 ++++
 gas/testsuite/gas/elf/dwarf-3-func.d      |  6 ++++
 gas/testsuite/gas/elf/dwarf-3-func.s      |  8 +++++
 gas/testsuite/gas/elf/dwarf-5-func-weak.d | 39 +++++++++++++++++++++++
 gas/testsuite/gas/elf/dwarf-5-func.d      |  6 ++++
 gas/testsuite/gas/elf/elf.exp             |  2 ++
 8 files changed, 107 insertions(+), 2 deletions(-)
 create mode 100644 gas/testsuite/gas/elf/dwarf-3-func-weak.d
 create mode 100644 gas/testsuite/gas/elf/dwarf-3-func-weak.s
 create mode 100644 gas/testsuite/gas/elf/dwarf-5-func-weak.d

diff --git a/gas/dwarf2dbg.c b/gas/dwarf2dbg.c
index c899eecd26b..81c7a30019b 100644
--- a/gas/dwarf2dbg.c
+++ b/gas/dwarf2dbg.c
@@ -2753,7 +2753,7 @@ out_debug_abbrev (segT abbrev_seg,
 	  continue;
 #endif
 
-	  if (S_IS_EXTERNAL (symp))
+	  if (S_IS_EXTERNAL (symp) || S_IS_WEAK (symp))
 	    have_efunc = true;
 	  else
 	    have_lfunc = true;
@@ -2978,7 +2978,7 @@ out_debug_info (segT info_seg, segT abbrev_seg, segT line_seg, segT str_seg,
 
 	  /* DW_AT_external.  */
 	  if (func_form == DW_FORM_flag)
-	    out_byte (S_IS_EXTERNAL (symp));
+	    out_byte (S_IS_EXTERNAL (symp) || S_IS_WEAK (symp));
 
 	  /* PR 29517: Let consumers know that we do not have
 	     return type information for this function.  */
diff --git a/gas/testsuite/gas/elf/dwarf-3-func-weak.d b/gas/testsuite/gas/elf/dwarf-3-func-weak.d
new file mode 100644
index 00000000000..47dc40fd802
--- /dev/null
+++ b/gas/testsuite/gas/elf/dwarf-3-func-weak.d
@@ -0,0 +1,37 @@
+#as: --gdwarf-3
+#name: Dwarf3 weak function debug info
+#readelf: -W -wai
+#target: i?86-*-* x86_64-*-*
+
+Contents of the .debug_info section:
+
+ +Compilation Unit @ offset (0x)?0:
+ +Length: .*
+ +Version: +3
+ +Abbrev Offset: +(0x)?0
+ +Pointer Size: .*
+#...
+ <0><[0-9a-f]+>: Abbrev Number: 1 \(DW_TAG_compile_unit\)
+#...
+ <1><[0-9a-f]+>: Abbrev Number: 2 \(DW_TAG_subprogram\)
+ +<[0-9a-f]+> +DW_AT_name +: \(strp\) \(offset: (0x)?[0-9a-f]+\): wfunc
+ +<[0-9a-f]+> +DW_AT_external +: \(flag\) 1
+ +<[0-9a-f]+> +DW_AT_type +: \(ref_udata\) <0x[0-9a-f]+>
+ +<[0-9a-f]+> +DW_AT_low_pc +: \(addr\) (0x)?0
+ +<[0-9a-f]+> +DW_AT_high_pc +: \(addr\) (0x)?1
+ <1><[0-9a-f]+>: Abbrev Number: 3 \(DW_TAG_unspecified_type\)
+ <1><[0-9a-f]+>: Abbrev Number: 0
+
+Contents of the .debug_abbrev section:
+
+ +Number TAG \((0x)?0\)
+ +1 +DW_TAG_compile_unit +\[has children\]
+#...
+ +2 +DW_TAG_subprogram +\[no children\]
+ +DW_AT_name +DW_FORM_strp
+ +DW_AT_external +DW_FORM_flag
+ +DW_AT_type +DW_FORM_ref_udata
+ +DW_AT_low_pc +DW_FORM_addr
+ +DW_AT_high_pc +DW_FORM_addr
+ +DW_AT value: 0 +DW_FORM value: 0
+#pass
diff --git a/gas/testsuite/gas/elf/dwarf-3-func-weak.s b/gas/testsuite/gas/elf/dwarf-3-func-weak.s
new file mode 100644
index 00000000000..f5318c3fdbf
--- /dev/null
+++ b/gas/testsuite/gas/elf/dwarf-3-func-weak.s
@@ -0,0 +1,7 @@
+	.text
+
+wfunc:
+	.nop
+	.weak wfunc
+	.type wfunc, %function
+	.size wfunc, .-wfunc
diff --git a/gas/testsuite/gas/elf/dwarf-3-func.d b/gas/testsuite/gas/elf/dwarf-3-func.d
index c92b0d311c0..4a269f814dc 100644
--- a/gas/testsuite/gas/elf/dwarf-3-func.d
+++ b/gas/testsuite/gas/elf/dwarf-3-func.d
@@ -37,6 +37,12 @@ Contents of the .debug_info section:
  +<[0-9a-f]+> +DW_AT_type +: \(ref_udata\) <0x[0-9a-f]+>
  +<[0-9a-f]+> +DW_AT_low_pc +: \(addr\) (0x)?35
  +<[0-9a-f]+> +DW_AT_high_pc +: \(addr\) (0x)?38
+ <1><[0-9a-f]+>: Abbrev Number: 2 \(DW_TAG_subprogram\)
+ +<[0-9a-f]+> +DW_AT_name +: \(strp\) \(offset: (0x)?[0-9a-f]+\): wfunc
+ +<[0-9a-f]+> +DW_AT_external +: \(flag\) 1
+ +<[0-9a-f]+> +DW_AT_type +: \(ref_udata\) <0x[0-9a-f]+>
+ +<[0-9a-f]+> +DW_AT_low_pc +: \(addr\) (0x)?38
+ +<[0-9a-f]+> +DW_AT_high_pc +: \(addr\) (0x)?39
  <1><[0-9a-f]+>: Abbrev Number: 3 \(DW_TAG_unspecified_type\)
  <1><[0-9a-f]+>: Abbrev Number: 0
 
diff --git a/gas/testsuite/gas/elf/dwarf-3-func.s b/gas/testsuite/gas/elf/dwarf-3-func.s
index 4610686afd2..84ec3af8d38 100644
--- a/gas/testsuite/gas/elf/dwarf-3-func.s
+++ b/gas/testsuite/gas/elf/dwarf-3-func.s
@@ -37,4 +37,12 @@ lfunc2:
 	.nop
 	.type lfunc2, %function
 	.size lfunc2, .-lfunc2
+
+	.ifndef LOCAL
+wfunc:
+	.nop
+	.weak wfunc
+	.type wfunc, %function
+	.size wfunc, .-wfunc
+	.endif
 	.endif
diff --git a/gas/testsuite/gas/elf/dwarf-5-func-weak.d b/gas/testsuite/gas/elf/dwarf-5-func-weak.d
new file mode 100644
index 00000000000..9ca833a9262
--- /dev/null
+++ b/gas/testsuite/gas/elf/dwarf-5-func-weak.d
@@ -0,0 +1,39 @@
+#as: --gdwarf-5
+#name: Dwarf5 weak function debug info
+#readelf: -W -wai
+#source: dwarf-3-func-weak.s
+#target: i?86-*-* x86_64-*-*
+
+Contents of the .debug_info section:
+
+ +Compilation Unit @ offset (0x)?0:
+ +Length: .*
+ +Version: +5
+ +Unit Type: +DW_UT_compile \(1\)
+ +Abbrev Offset: +(0x)?0
+ +Pointer Size: .*
+#...
+ <0><[0-9a-f]+>: Abbrev Number: 1 \(DW_TAG_compile_unit\)
+#...
+ <1><[0-9a-f]+>: Abbrev Number: 2 \(DW_TAG_subprogram\)
+ +<[0-9a-f]+> +DW_AT_name +: \(strp\) \(offset: (0x)?[0-9a-f]+\): wfunc
+ +<[0-9a-f]+> +DW_AT_external +: \(flag_present\) 1
+ +<[0-9a-f]+> +DW_AT_type +: \(ref_udata\) <0x[0-9a-f]+>
+ +<[0-9a-f]+> +DW_AT_low_pc +: \(addr\) (0x)?0
+ +<[0-9a-f]+> +DW_AT_high_pc +: \(udata\) 1
+ <1><[0-9a-f]+>: Abbrev Number: 3 \(DW_TAG_unspecified_type\)
+ <1><[0-9a-f]+>: Abbrev Number: 0
+
+Contents of the .debug_abbrev section:
+
+ +Number TAG \((0x)?0\)
+ +1 +DW_TAG_compile_unit +\[has children\]
+#...
+ +2 +DW_TAG_subprogram +\[no children\]
+ +DW_AT_name +DW_FORM_strp
+ +DW_AT_external +DW_FORM_flag_present
+ +DW_AT_type +DW_FORM_ref_udata
+ +DW_AT_low_pc +DW_FORM_addr
+ +DW_AT_high_pc +DW_FORM_udata
+ +DW_AT value: 0 +DW_FORM value: 0
+#pass
diff --git a/gas/testsuite/gas/elf/dwarf-5-func.d b/gas/testsuite/gas/elf/dwarf-5-func.d
index df5cfc7882d..e6d2a08409d 100644
--- a/gas/testsuite/gas/elf/dwarf-5-func.d
+++ b/gas/testsuite/gas/elf/dwarf-5-func.d
@@ -39,6 +39,12 @@ Contents of the .debug_info section:
  +<[0-9a-f]+> +DW_AT_type +: \(ref_udata\) <0x[0-9a-f]+>
  +<[0-9a-f]+> +DW_AT_low_pc +: \(addr\) (0x)?35
  +<[0-9a-f]+> +DW_AT_high_pc +: \(udata\) 3
+ <1><[0-9a-f]+>: Abbrev Number: 2 \(DW_TAG_subprogram\)
+ +<[0-9a-f]+> +DW_AT_name +: \(strp\) \(offset: (0x)?[0-9a-f]+\): wfunc
+ +<[0-9a-f]+> +DW_AT_external +: \(flag\) 1
+ +<[0-9a-f]+> +DW_AT_type +: \(ref_udata\) <0x[0-9a-f]+>
+ +<[0-9a-f]+> +DW_AT_low_pc +: \(addr\) (0x)?38
+ +<[0-9a-f]+> +DW_AT_high_pc +: \(udata\) 1
  <1><[0-9a-f]+>: Abbrev Number: 3 \(DW_TAG_unspecified_type\)
  <1><[0-9a-f]+>: Abbrev Number: 0
 
diff --git a/gas/testsuite/gas/elf/elf.exp b/gas/testsuite/gas/elf/elf.exp
index 5580c494c54..14263097cd6 100644
--- a/gas/testsuite/gas/elf/elf.exp
+++ b/gas/testsuite/gas/elf/elf.exp
@@ -356,6 +356,8 @@ if { [is_elf_format] } then {
     run_dump_test "dwarf-5-func" $dump_opts
     run_dump_test "dwarf-5-func-global" $dump_opts
     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
 
     # Exclude targets defining ONLY_STANDARD_ESCAPES. It's not clear how these
     # are supposed to reference macro arguments in double-quoted strings.
-- 
2.53.0



More information about the Binutils mailing list