[RFC PATCH] LoongArch: Add parameters to DWARF2_USE_FIXED_ADVANCE_PC macro

mengqinggang <[email protected]> Wed, 22 Jul 2026 11:23:30 +0800
Newsgroups gmane.comp.gnu.binutils
Message-ID <[email protected]>
If DWARF2_USE_FIXED_ADVANCE_PC is nonzero, use the DW_LNS_fixed_advance_pc
opcode; otherwise, use special opcodes.

Add FROM symbol and TO symbol parameters for finer-grained control.

Use special opcodes when no linker-relaxable instructions between two symbols;
otherwise, use DW_LNS_fixed_advance_pc opcodes.
---
 gas/config/tc-h8300.h                         |  2 +-
 gas/config/tc-loongarch.c                     | 13 +++++++++++
 gas/config/tc-loongarch.h                     |  4 +++-
 gas/config/tc-msp430.h                        |  2 +-
 gas/config/tc-nds32.h                         |  2 +-
 gas/config/tc-riscv.h                         |  2 +-
 gas/config/tc-rl78.h                          |  2 +-
 gas/config/tc-xtensa.h                        |  2 +-
 gas/dwarf2dbg.c                               | 23 +++++++++++++------
 gas/testsuite/gas/elf/dwarf-5-irp.d           |  3 +--
 gas/testsuite/gas/elf/dwarf-5-loc0.d          |  3 +--
 gas/testsuite/gas/elf/dwarf-5-macro-include.d |  2 +-
 gas/testsuite/gas/elf/dwarf-5-macro.d         |  2 +-
 gas/testsuite/gas/elf/dwarf2-11.d             |  3 +--
 gas/testsuite/gas/elf/dwarf2-15.d             |  3 +--
 gas/testsuite/gas/elf/dwarf2-16.d             |  3 +--
 gas/testsuite/gas/elf/dwarf2-17.d             |  3 +--
 gas/testsuite/gas/elf/dwarf2-18.d             |  3 +--
 gas/testsuite/gas/elf/dwarf2-19.d             |  3 +--
 gas/testsuite/gas/elf/dwarf2-5.d              |  3 +--
 gas/testsuite/gas/lns/lns.exp                 |  1 -
 21 files changed, 49 insertions(+), 35 deletions(-)

diff --git a/gas/config/tc-h8300.h b/gas/config/tc-h8300.h
index c4689d37b12..66664553a24 100644
--- a/gas/config/tc-h8300.h
+++ b/gas/config/tc-h8300.h
@@ -46,7 +46,7 @@ struct internal_reloc;
 
 /* Minimum instruction is of 16 bits.  */
 #define DWARF2_LINE_MIN_INSN_LENGTH 2
-#define DWARF2_USE_FIXED_ADVANCE_PC 0
+#define DWARF2_USE_FIXED_ADVANCE_PC(FROM, TO) 0
 
 /* Provide mappings from the original H8 COFF relocation names to
    their corresponding BFD relocation names.  */
diff --git a/gas/config/tc-loongarch.c b/gas/config/tc-loongarch.c
index bd7d8dabb7a..7cb16d5dcf1 100644
--- a/gas/config/tc-loongarch.c
+++ b/gas/config/tc-loongarch.c
@@ -1644,6 +1644,19 @@ _loongarch_force_relocation_sub_same (segT sec,
   return true;
 }
 
+/* Use DW_LNS_fixed_advance_pc opcode in .debug_line if return ture,
+   otherwise use special opcode.  */
+bool
+loongarch_fixed_advance_pc (symbolS *from, symbolS *to)
+{
+  segT fromsec = S_GET_SEGMENT (from);
+  segT tosec = S_GET_SEGMENT (to);
+  if (fromsec != tosec)
+    return false;
+  fragS *fromfrag = symbol_get_frag (from);
+  fragS *tofrag = symbol_get_frag (to);
+  return _loongarch_force_relocation_sub_same (fromsec, fromfrag, tofrag);
+}
 
 /* Postpone text-section label subtraction calculation until linking,
    since linker relaxations might change the deltas.  */
diff --git a/gas/config/tc-loongarch.h b/gas/config/tc-loongarch.h
index 146844e772e..32fbc23d679 100644
--- a/gas/config/tc-loongarch.h
+++ b/gas/config/tc-loongarch.h
@@ -102,7 +102,9 @@ extern bool loongarch_force_relocation_sub_same(struct fix *, asection *);
 
 #define TARGET_USE_CFIPOP 1
 /* Adjust debug_line after relaxation.  */
-#define DWARF2_USE_FIXED_ADVANCE_PC   1
+extern bool loongarch_fixed_advance_pc (symbolS *, symbolS *);
+#define DWARF2_USE_FIXED_ADVANCE_PC(FROM, TO) \
+  loongarch_fixed_advance_pc (FROM, TO)
 
 /* FDE Data Alignment Factor.
    FDE Code Alignment Factor (DWARF2_LINE_MIN_INSN_LENGTH) should be 1
diff --git a/gas/config/tc-msp430.h b/gas/config/tc-msp430.h
index fe202abc713..f9b6c1e588c 100644
--- a/gas/config/tc-msp430.h
+++ b/gas/config/tc-msp430.h
@@ -168,7 +168,7 @@ extern bool msp430_allow_local_subtract (expressionS *, expressionS *, segT);
    so don't report errors at this point.  */
 #define TC_VALIDATE_FIX_SUB(FIX, SEG) 1
 
-#define DWARF2_USE_FIXED_ADVANCE_PC 1
+#define DWARF2_USE_FIXED_ADVANCE_PC(FROM, TO) 1
 
 #define TC_LINKRELAX_FIXUP(seg) ((seg->flags & SEC_CODE) || (seg->flags & SEC_DEBUGGING))
 
diff --git a/gas/config/tc-nds32.h b/gas/config/tc-nds32.h
index 432d46d9fb1..49a1bead095 100644
--- a/gas/config/tc-nds32.h
+++ b/gas/config/tc-nds32.h
@@ -77,7 +77,7 @@ extern bool nds32_allow_local_subtract (expressionS *, expressionS *, segT);
 #define md_allow_local_subtract(lhs,rhs,sect)	nds32_allow_local_subtract (lhs, rhs, sect)
 
 /* dwarf2dbg.c.  */
-#define DWARF2_USE_FIXED_ADVANCE_PC		1
+#define DWARF2_USE_FIXED_ADVANCE_PC(FROM, TO)	1
 
 /* write.c.  */
 extern long nds32_pcrel_from_section (struct fix *, segT);
diff --git a/gas/config/tc-riscv.h b/gas/config/tc-riscv.h
index d695db83477..7518d2a70f7 100644
--- a/gas/config/tc-riscv.h
+++ b/gas/config/tc-riscv.h
@@ -135,7 +135,7 @@ extern void riscv_elf_section_change_hook (void);
 extern void riscv_elf_final_processing (void);
 
 /* Adjust debug_line after relaxation.  */
-#define DWARF2_USE_FIXED_ADVANCE_PC 1
+#define DWARF2_USE_FIXED_ADVANCE_PC(FROM, TO) 1
 
 #define md_parse_name(name, exp, mode, c) \
   riscv_parse_name (name, exp, mode)
diff --git a/gas/config/tc-rl78.h b/gas/config/tc-rl78.h
index 9761d509aa1..fb54a2e8da7 100644
--- a/gas/config/tc-rl78.h
+++ b/gas/config/tc-rl78.h
@@ -94,6 +94,6 @@ extern void rl78_elf_final_processing (void);
    || ((SEC)->flags & SEC_DEBUGGING) != 0	\
    || TC_FORCE_RELOCATION (FIX))
 
-#define DWARF2_USE_FIXED_ADVANCE_PC 1
+#define DWARF2_USE_FIXED_ADVANCE_PC(FROM, TO) 1
 
 #define TC_FORCE_RELOCATION(FIX) (linkrelax)
diff --git a/gas/config/tc-xtensa.h b/gas/config/tc-xtensa.h
index 64d735144d7..d6ddac738fe 100644
--- a/gas/config/tc-xtensa.h
+++ b/gas/config/tc-xtensa.h
@@ -419,7 +419,7 @@ extern void xtensa_init (int, char **);
 #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) 0
 
 /* Use line number format that is amenable to linker relaxation.  */
-#define DWARF2_USE_FIXED_ADVANCE_PC (linkrelax != 0)
+#define DWARF2_USE_FIXED_ADVANCE_PC(FROM, TO) (linkrelax != 0)
 
 
 /* Resource reservation info functions.  */
diff --git a/gas/dwarf2dbg.c b/gas/dwarf2dbg.c
index a1ceab4e428..04a30a606d7 100644
--- a/gas/dwarf2dbg.c
+++ b/gas/dwarf2dbg.c
@@ -112,7 +112,7 @@
    opcodes and variable-length operands cannot be used.  If this macro is
    nonzero, use the DW_LNS_fixed_advance_pc opcode instead.  */
 #ifndef DWARF2_USE_FIXED_ADVANCE_PC
-# define DWARF2_USE_FIXED_ADVANCE_PC	linkrelax
+# define DWARF2_USE_FIXED_ADVANCE_PC(FROM, TO)	linkrelax
 #endif
 
 /* First special line opcode - leave room for the standard opcodes.
@@ -1898,7 +1898,7 @@ relax_inc_line_addr (int line_delta, symbolS *to_sym, symbolS *from_sym)
 
   /* The maximum size of the frag is the line delta with a maximum
      sized address delta.  */
-  if (DWARF2_USE_FIXED_ADVANCE_PC)
+  if (DWARF2_USE_FIXED_ADVANCE_PC (from_sym, to_sym))
     max_chars = size_fixed_inc_line_addr (line_delta,
 					  -DWARF2_LINE_MIN_INSN_LENGTH);
   else
@@ -1919,7 +1919,11 @@ dwarf2dbg_estimate_size_before_relax (fragS *frag)
   int size;
 
   addr_delta = resolve_symbol_value (frag->fr_symbol);
-  if (DWARF2_USE_FIXED_ADVANCE_PC)
+  symbolS *from = symbol_get_value_expression (frag->fr_symbol)->X_op_symbol;
+  symbolS *to = symbol_get_value_expression (frag->fr_symbol)->X_add_symbol;
+  (void)from;
+  (void)to;
+  if (DWARF2_USE_FIXED_ADVANCE_PC (from, to))
     size = size_fixed_inc_line_addr (frag->fr_offset, addr_delta);
   else
     size = size_inc_line_addr (frag->fr_offset, addr_delta);
@@ -1953,7 +1957,11 @@ dwarf2dbg_convert_frag (fragS *frag)
 {
   offsetT addr_diff;
 
-  if (DWARF2_USE_FIXED_ADVANCE_PC)
+  symbolS *from = symbol_get_value_expression (frag->fr_symbol)->X_op_symbol;
+  symbolS *to = symbol_get_value_expression (frag->fr_symbol)->X_add_symbol;
+  (void)from;
+  (void)to;
+  if (DWARF2_USE_FIXED_ADVANCE_PC (from, to))
     {
       /* If linker relaxation is enabled then the distance between the two
 	 symbols in the frag->fr_symbol expression might change.  Hence we
@@ -1977,7 +1985,7 @@ dwarf2dbg_convert_frag (fragS *frag)
      course, have allocated enough memory earlier.  */
   gas_assert (frag->fr_var >= (int) frag->fr_subtype);
 
-  if (DWARF2_USE_FIXED_ADVANCE_PC)
+  if (DWARF2_USE_FIXED_ADVANCE_PC (from, to))
     emit_fixed_inc_line_addr (frag->fr_offset, addr_diff, frag,
 			      frag->fr_literal + frag->fr_fix,
 			      frag->fr_subtype);
@@ -2110,7 +2118,8 @@ process_entries (segT seg, struct line_entry *e)
 	  out_set_addr (lab);
 	  out_inc_line_addr (line_delta, 0);
 	}
-      else if (frag == last_frag && ! DWARF2_USE_FIXED_ADVANCE_PC)
+      else if (frag == last_frag
+	       && ! DWARF2_USE_FIXED_ADVANCE_PC (last_lab, lab))
 	out_inc_line_addr (line_delta, frag_ofs - last_frag_ofs);
       else
 	relax_inc_line_addr (line_delta, lab, last_lab);
@@ -2127,7 +2136,7 @@ process_entries (segT seg, struct line_entry *e)
   /* Emit a DW_LNE_end_sequence for the end of the section.  */
   frag = last_frag_for_seg (seg);
   frag_ofs = get_frag_fix (frag, seg);
-  if (frag == last_frag && ! DWARF2_USE_FIXED_ADVANCE_PC)
+  if (frag == last_frag && ! DWARF2_USE_FIXED_ADVANCE_PC (last_lab, lab))
     out_inc_line_addr (INT_MAX, frag_ofs - last_frag_ofs);
   else
     {
diff --git a/gas/testsuite/gas/elf/dwarf-5-irp.d b/gas/testsuite/gas/elf/dwarf-5-irp.d
index a4f01355db3..781b2a32aaa 100644
--- a/gas/testsuite/gas/elf/dwarf-5-irp.d
+++ b/gas/testsuite/gas/elf/dwarf-5-irp.d
@@ -5,8 +5,7 @@
 # The bfin target does not allow .subsection with an equated symbol as operand.
 # The d30v target emits sufficiently different debug info, apparently also covering padding it inserts.
 # The riscv targets do not support the subtraction of symbols.
-# The loongarch targets do not support the subtraction of symbols.
-#xfail: am33*-* bfin-* cr16-* crx-* d30v-* ft32-* loongarch*-* mn10*-* msp430-* nds32*-* riscv*-* rl78-*
+#xfail: am33*-* bfin-* cr16-* crx-* d30v-* ft32-* mn10*-* msp430-* nds32*-* riscv*-* rl78-*
 
 Raw dump of debug contents .*
 #...
diff --git a/gas/testsuite/gas/elf/dwarf-5-loc0.d b/gas/testsuite/gas/elf/dwarf-5-loc0.d
index 9338271c0eb..9439d103b1f 100644
--- a/gas/testsuite/gas/elf/dwarf-5-loc0.d
+++ b/gas/testsuite/gas/elf/dwarf-5-loc0.d
@@ -3,8 +3,7 @@
 #name: DWARF5 .loc 0
 # The am33 cr16 crx ft32 mn10 msp430 nds32 and rl78 targets do not evaluate the subtraction of symbols at assembly time.
 # The riscv targets do not support the subtraction of symbols.
-# The loongarch targets do not support the subtraction of symbols.
-#xfail: am3*-* cr16-* crx-* ft32*-* loongarch*-* mn10*-* msp430-* nds32*-* riscv*-* rl78-*
+#xfail: am3*-* cr16-* crx-* ft32*-* mn10*-* msp430-* nds32*-* riscv*-* rl78-*
 
 Contents of the \.debug_line section:
 
diff --git a/gas/testsuite/gas/elf/dwarf-5-macro-include.d b/gas/testsuite/gas/elf/dwarf-5-macro-include.d
index c39425aa8c9..32ea6386992 100644
--- a/gas/testsuite/gas/elf/dwarf-5-macro-include.d
+++ b/gas/testsuite/gas/elf/dwarf-5-macro-include.d
@@ -4,7 +4,7 @@
 # The am33 cr16 crx ft32 mn10* msp430 nds32* and rl78 targets do not evaluate the subtraction of symbols at assembly time.
 # The d30v target emits sufficiently different debug info, apparently also covering padding it inserts.
 # The riscv targets do not support the subtraction of symbols.
-#xfail: am33*-* cr16-* crx-* d30v-* ft32-* loongarch*-* mn10*-* msp430-* nds32*-* riscv*-* rl78-*
+#xfail: am33*-* cr16-* crx-* d30v-* ft32-* mn10*-* msp430-* nds32*-* riscv*-* rl78-*
 
 Raw dump of debug contents .*
 #...
diff --git a/gas/testsuite/gas/elf/dwarf-5-macro.d b/gas/testsuite/gas/elf/dwarf-5-macro.d
index b2613bd1c16..794e17a575e 100644
--- a/gas/testsuite/gas/elf/dwarf-5-macro.d
+++ b/gas/testsuite/gas/elf/dwarf-5-macro.d
@@ -4,7 +4,7 @@
 # The am33 cr16 crx ft32 mn10* msp430 nds32* and rl78 targets do not evaluate the subtraction of symbols at assembly time.
 # The d30v target emits sufficiently different debug info, apparently also covering padding it inserts.
 # The riscv targets do not support the subtraction of symbols.
-#xfail: am33*-* cr16-* crx-* d30v-* ft32-* loongarch*-* mn10*-* msp430-* nds32*-* riscv*-* rl78-*
+#xfail: am33*-* cr16-* crx-* d30v-* ft32-* mn10*-* msp430-* nds32*-* riscv*-* rl78-*
 
 Raw dump of debug contents .*
 #...
diff --git a/gas/testsuite/gas/elf/dwarf2-11.d b/gas/testsuite/gas/elf/dwarf2-11.d
index 44f5cd18ec6..2c30e55c695 100644
--- a/gas/testsuite/gas/elf/dwarf2-11.d
+++ b/gas/testsuite/gas/elf/dwarf2-11.d
@@ -3,8 +3,7 @@
 #name: DWARF2 11
 # The am33 cr16 crx ft32 mn10 msp430 nds32 and rl78 targets do not evaluate the subtraction of symbols at assembly time.
 # The riscv targets do not support the subtraction of symbols.
-# The loongarch targets do not support the subtraction of symbols.
-#xfail: am3*-* cr16-* crx-* ft32*-* loongarch*-* mn10*-* msp430-* nds32*-* riscv*-* rl78-*
+#xfail: am3*-* cr16-* crx-* ft32*-* mn10*-* msp430-* nds32*-* riscv*-* rl78-*
 
 Contents of the \.debug_line section:
 
diff --git a/gas/testsuite/gas/elf/dwarf2-15.d b/gas/testsuite/gas/elf/dwarf2-15.d
index 3ec9f2785a7..939c61598cb 100644
--- a/gas/testsuite/gas/elf/dwarf2-15.d
+++ b/gas/testsuite/gas/elf/dwarf2-15.d
@@ -3,8 +3,7 @@
 #name: DWARF2 15
 # The am33 cr16 crx ft32 mn10 msp430 nds32 and rl78 targets do not evaluate the subtraction of symbols at assembly time.
 # The riscv targets do not support the subtraction of symbols.
-# The loongarch targets do not support the subtraction of symbols.
-#xfail: am3*-* cr16-* crx-* ft32*-* loongarch*-* mn10*-* msp430-* nds32*-* riscv*-* rl78-*
+#xfail: am3*-* cr16-* crx-* ft32*-* mn10*-* msp430-* nds32*-* riscv*-* rl78-*
 
 Hex dump of section '\.rodata':
   0x00000000 01 *.*
diff --git a/gas/testsuite/gas/elf/dwarf2-16.d b/gas/testsuite/gas/elf/dwarf2-16.d
index 32b57763137..8e35d493619 100644
--- a/gas/testsuite/gas/elf/dwarf2-16.d
+++ b/gas/testsuite/gas/elf/dwarf2-16.d
@@ -4,8 +4,7 @@
 # The am33 cr16 crx ft32 mn10 msp430 nds32 and rl78 targets do not evaluate the subtraction of symbols at assembly time.
 # The mep target tries to relay code sections which breaks symbolic view computations.
 # The riscv targets do not support the subtraction of symbols.
-# The loongarch targets do not support the subtraction of symbols.
-#xfail: am3*-* cr16-* crx-* ft32*-* mep-* loongarch*-* mn10*-* msp430-* nds32*-* riscv*-* rl78-*
+#xfail: am3*-* cr16-* crx-* ft32*-* mep-* mn10*-* msp430-* nds32*-* riscv*-* rl78-*
 
 Hex dump of section '\.rodata':
   0x00000000 01 *.*
diff --git a/gas/testsuite/gas/elf/dwarf2-17.d b/gas/testsuite/gas/elf/dwarf2-17.d
index 4c2f2e3999e..881477cf95b 100644
--- a/gas/testsuite/gas/elf/dwarf2-17.d
+++ b/gas/testsuite/gas/elf/dwarf2-17.d
@@ -4,8 +4,7 @@
 # The am33 cr16 crx ft32 mn10 msp430 nds32 and rl78 targets do not evaluate the subtraction of symbols at assembly time.
 # The mep target tries to relay code sections which breaks symbolic view computations.
 # The riscv targets do not support the subtraction of symbols.
-# The loongarch targets do not support the subtraction of symbols.
-#xfail: am3*-* cr16-* crx-* ft32*-* loongarch*-* mep-* mn10*-* msp430-* nds32*-* riscv*-* rl78-*
+#xfail: am3*-* cr16-* crx-* ft32*-* mep-* mn10*-* msp430-* nds32*-* riscv*-* rl78-*
 
 Hex dump of section '\.rodata':
   0x00000000 00 *.*
diff --git a/gas/testsuite/gas/elf/dwarf2-18.d b/gas/testsuite/gas/elf/dwarf2-18.d
index 18157c634a5..fbaebaa9019 100644
--- a/gas/testsuite/gas/elf/dwarf2-18.d
+++ b/gas/testsuite/gas/elf/dwarf2-18.d
@@ -3,8 +3,7 @@
 #name: DWARF2 18
 # The am33 cr16 crx ft32 mn10 msp430 nds32 and rl78 targets do not evaluate the subtraction of symbols at assembly time.
 # The riscv targets do not support the subtraction of symbols.
-# The loongarch targets do not support the subtraction of symbols.
-#xfail: am3*-* cr16-* crx-* ft32*-* loongarch*-* mn10*-* msp430-* nds32*-* riscv*-* rl78-*
+#xfail: am3*-* cr16-* crx-* ft32*-* mn10*-* msp430-* nds32*-* riscv*-* rl78-*
 
 Hex dump of section '\.rodata':
   0x00000000 0100 *.*
diff --git a/gas/testsuite/gas/elf/dwarf2-19.d b/gas/testsuite/gas/elf/dwarf2-19.d
index 133294affe7..55d0caf4cb1 100644
--- a/gas/testsuite/gas/elf/dwarf2-19.d
+++ b/gas/testsuite/gas/elf/dwarf2-19.d
@@ -4,8 +4,7 @@
 # The am33 cr16 crx ft32 mn10 msp430 nds32 and rl78 targets do not evaluate the subtraction of symbols at assembly time.
 # The mep targets turns some view computations into complex relocations.
 # The riscv targets do not support the subtraction of symbols.
-# The loongarch targets do not support the subtraction of symbols.
-#xfail: am3*-* cr16-* crx-* ft32*-* mep-* loongarch*-* mn10*-* msp430-* nds32*-* riscv*-* rl78-*
+#xfail: am3*-* cr16-* crx-* ft32*-* mep-* mn10*-* msp430-* nds32*-* riscv*-* rl78-*
 
 Hex dump of section '\.rodata':
   0x00000000 01000102 *.*
diff --git a/gas/testsuite/gas/elf/dwarf2-5.d b/gas/testsuite/gas/elf/dwarf2-5.d
index 1dd65732c50..bf1eeb5bbfd 100644
--- a/gas/testsuite/gas/elf/dwarf2-5.d
+++ b/gas/testsuite/gas/elf/dwarf2-5.d
@@ -4,8 +4,7 @@
 # The am33 cr16 crx ft32 mn10 msp430 nds32 and rl78 targets do not evaluate the subtraction of symbols at assembly time.
 # The mep target tries to relay code sections which breaks symbolic view computations.
 # The riscv targets do not support the subtraction of symbols.
-# The loongarch targets do not support the subtraction of symbols.
-#xfail: am3*-* cr16-* crx-* ft32*-* loongarch*-* mep-* mn10*-* msp430-* nds32*-* riscv*-* rl78-*
+#xfail: am3*-* cr16-* crx-* ft32*-* mep-* mn10*-* msp430-* nds32*-* riscv*-* rl78-*
 
 Hex dump of section '\.rodata':
   0x00000000 01010201 010203 *.*
diff --git a/gas/testsuite/gas/lns/lns.exp b/gas/testsuite/gas/lns/lns.exp
index 7a92c54a260..58c2235b628 100644
--- a/gas/testsuite/gas/lns/lns.exp
+++ b/gas/testsuite/gas/lns/lns.exp
@@ -31,7 +31,6 @@ if { ![istarget s390*-*-*] } {
 	 || [istarget cr16-*-*]
 	 || [istarget crx-*-*]
 	 || [istarget ft32*-*]
-	 || [istarget loongarch*-*-*]
 	 || [istarget mn10*-*-*]
 	 || [istarget msp430-*-*]
 	 || [istarget nds32*-*-*]
-- 
2.34.1