]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
LoongArch: Emit alignment relocations after relaxed instructions
authormengqinggang <mengqinggang@loongson.cn>
Tue, 23 Jun 2026 06:03:52 +0000 (14:03 +0800)
committermengqinggang <mengqinggang@loongson.cn>
Fri, 10 Jul 2026 01:31:54 +0000 (09:31 +0800)
Alignment cannot change before relaxed instructions.
Emit alignment relocations only after them.

Similar with https://github.com/llvm/llvm-project/pull/150816.

12 files changed:
gas/config/tc-loongarch.c
gas/testsuite/gas/loongarch/negative_right_shift.d
gas/testsuite/gas/loongarch/negative_right_shift.s
gas/testsuite/gas/loongarch/relax-align.d [new file with mode: 0644]
gas/testsuite/gas/loongarch/relax-align.l [moved from gas/testsuite/gas/loongarch/relax_align.l with 100% similarity]
gas/testsuite/gas/loongarch/relax-align.s [moved from gas/testsuite/gas/loongarch/relax_align.s with 79% similarity]
gas/testsuite/gas/loongarch/relax-align1.d [new file with mode: 0644]
gas/testsuite/gas/loongarch/relax-align1.s [new file with mode: 0644]
gas/testsuite/gas/loongarch/relax-align2.d
gas/testsuite/gas/loongarch/relax-align2.s
gas/testsuite/gas/loongarch/relax_align.d [deleted file]
ld/testsuite/ld-undefined/undefined.exp

index ed5d47a036084395fe60f8cf63926658c084eb04..e67394c460c5d3506eed389170bbf2a02896d919 100644 (file)
@@ -2178,6 +2178,11 @@ loongarch_frag_align_code (int n, int max)
   if (!LARCH_opts.relax)
     return false;
 
+  /* Only create an alignment frag if the current section already
+     has relaxed instructions.  */
+  if (!now_seg->sec_flg1)
+    return false;
+
   bfd_vma align_bytes = (bfd_vma) 1 << n;
   bfd_vma worst_case_bytes = align_bytes - 4;
   bfd_vma addend = worst_case_bytes;
@@ -2192,8 +2197,7 @@ loongarch_frag_align_code (int n, int max)
   frag_wane (frag_now);
   frag_new (0);
 
-  /* Mark the current section and frag as linker relaxable.  */
-  now_seg->sec_flg1 = true;
+  /* Mark the current frag as linker relaxable.  */
   frag_now->tc_frag_data.linker_relax = true;
 
   /* If max <= 0, ignore max.
index 4237710c2b33ae6f6e6df2153b697a66c0fed617..9221e61f3ab1b492e2af475d433c4a1d8086c264 100644 (file)
@@ -1,40 +1,33 @@
-#as:
+#as: -mrelax
 #objdump: -d
-#skip: loongarch32-*-*
 #warning_output: negative_right_shift.l
 
-.*:     file format .*
-
-
-Disassembly of section \.text:
-
-0+ <directives>:
-   0:  03400000        nop
-   4:  00000001        \.word          0x00000001
-   8:  00000001        \.word          0x00000001
-
-0+c <insns>:
-   c:  02bff9ac        addi.w          \$t0, \$t1, -2
-  10:  02fff9ac        addi.d          \$t0, \$t1, -2
-  14:  13fff9ac        addu16i.d       \$t0, \$t1, -2
-  18:  15ffffcc        lu12i.w         \$t0, -2
-  1c:  17ffffcc        lu32i.d         \$t0, -2
-  20:  033ff9ac        lu52i.d         \$t0, \$t1, -2
-  24:  023ff9ac        slti            \$t0, \$t1, -2
-  28:  027ff9ac        sltui           \$t0, \$t1, -2
-  2c:  19ffffcc        pcaddi          \$t0, -2
-  30:  1dffffcc        pcaddu12i       \$t0, -2
-  34:  1fffffcc        pcaddu18i       \$t0, -2
-  38:  1bffffcc        pcalau12i       \$t0, -2
-  3c:  02bffdac        addi.w          \$t0, \$t1, -1
-  40:  02fffdac        addi.d          \$t0, \$t1, -1
-  44:  13fffdac        addu16i.d       \$t0, \$t1, -1
-  48:  15ffffec        lu12i.w         \$t0, -1
-  4c:  17ffffec        lu32i.d         \$t0, -1
-  50:  033ffdac        lu52i.d         \$t0, \$t1, -1
-  54:  023ffdac        slti            \$t0, \$t1, -1
-  58:  027ffdac        sltui           \$t0, \$t1, -1
-  5c:  19ffffec        pcaddi          \$t0, -1
-  60:  1dffffec        pcaddu12i       \$t0, -1
-  64:  1fffffec        pcaddu18i       \$t0, -1
-  68:  1bffffec        pcalau12i       \$t0, -1
+#...
+.*03400000     nop
+.*00000001     \.word          0x00000001
+.*00000001     \.word          0x00000001
+#...
+.*02bff9ac     addi.w          \$t0, \$t1, -2
+.*02fff9ac     addi.d          \$t0, \$t1, -2
+.*13fff9ac     addu16i.d       \$t0, \$t1, -2
+.*15ffffcc     lu12i.w         \$t0, -2
+.*17ffffcc     lu32i.d         \$t0, -2
+.*033ff9ac     lu52i.d         \$t0, \$t1, -2
+.*023ff9ac     slti            \$t0, \$t1, -2
+.*027ff9ac     sltui           \$t0, \$t1, -2
+.*19ffffcc     pcaddi          \$t0, -2
+.*1dffffcc     pcaddu12i       \$t0, -2
+.*1fffffcc     pcaddu18i       \$t0, -2
+.*1bffffcc     pcalau12i       \$t0, -2
+.*02bffdac     addi.w          \$t0, \$t1, -1
+.*02fffdac     addi.d          \$t0, \$t1, -1
+.*13fffdac     addu16i.d       \$t0, \$t1, -1
+.*15ffffec     lu12i.w         \$t0, -1
+.*17ffffec     lu32i.d         \$t0, -1
+.*033ffdac     lu52i.d         \$t0, \$t1, -1
+.*023ffdac     slti            \$t0, \$t1, -1
+.*027ffdac     sltui           \$t0, \$t1, -1
+.*19ffffec     pcaddi          \$t0, -1
+.*1dffffec     pcaddu12i       \$t0, -1
+.*1fffffec     pcaddu18i       \$t0, -1
+.*1bffffec     pcalau12i       \$t0, -1
index c6a8a1e7c6e7452d838910e08e9b6494e511d3c1..302dba1fb8f3106c0eb6369f2223dd1aee58b319 100644 (file)
@@ -1,9 +1,12 @@
-directives:
+.text
+call .text
+
+directives: # unsigned shift
 .align   -1>>62
 .word    -1>>63
 .4byte   -1>>63
 
-insns:
+insns: #signed left shift
 addi.w   $t0, $t1, -1<<1
 addi.d   $t0, $t1, -1<<1
 addu16i.d $t0, $t1, -1<<1
@@ -17,7 +20,7 @@ pcaddu12i $t0, -1<<1
 pcaddu18i $t0, -1<<1
 pcalau12i $t0, -1<<1
 
-# warn
+# warn signed right shift
 addi.w   $t0, $t1, -1>>63
 addi.d   $t0, $t1, -1>>63
 addu16i.d $t0, $t1, -1>>63
diff --git a/gas/testsuite/gas/loongarch/relax-align.d b/gas/testsuite/gas/loongarch/relax-align.d
new file mode 100644 (file)
index 0000000..8e61da5
--- /dev/null
@@ -0,0 +1,46 @@
+#as: -mrelax
+#objdump: -dr
+#warning_output: relax-align.l
+
+#...
+[      ]+8:[   ]+4c000020[     ]+ret
+[      ]+c:[   ]+03400000[     ]+nop
+[      ]+c:[   ]+R_LARCH_ALIGN[        ]+\*ABS\*\+0xc
+[      ]+10:[  ]+03400000[     ]+nop
+[      ]+14:[  ]+03400000[     ]+nop
+[      ]+18:[  ]+4c000020[     ]+ret
+[      ]+1c:[  ]+03400000[     ]+nop
+[      ]+1c:[  ]+R_LARCH_ALIGN[        ]+\*ABS\*\+0xc
+[      ]+20:[  ]+03400000[     ]+nop
+[      ]+24:[  ]+03400000[     ]+nop
+[      ]+28:[  ]+4c000020[     ]+ret
+[      ]+2c:[  ]+03400000[     ]+nop
+[      ]+2c:[  ]+R_LARCH_ALIGN[        ]+.Lla-relax-align\+0x104
+[      ]+30:[  ]+03400000[     ]+nop
+[      ]+34:[  ]+03400000[     ]+nop
+[      ]+38:[  ]+4c000020[     ]+ret
+[      ]+3c:[  ]+03400000[     ]+nop
+[      ]+3c:[  ]+R_LARCH_ALIGN[        ]+.Lla-relax-align\+0xb04
+[      ]+40:[  ]+03400000[     ]+nop
+[      ]+44:[  ]+03400000[     ]+nop
+[      ]+48:[  ]+4c000020[     ]+ret
+[      ]+4c:[  ]+03400000[     ]+nop
+[      ]+4c:[  ]+R_LARCH_ALIGN[        ]+\*ABS\*\+0xc
+[      ]+50:[  ]+03400000[     ]+nop
+[      ]+54:[  ]+03400000[     ]+nop
+[      ]+58:[  ]+4c000020[     ]+ret
+[      ]+5c:[  ]+03400000[     ]+nop
+[      ]+5c:[  ]+R_LARCH_ALIGN[        ]+\*ABS\*\+0xc
+[      ]+60:[  ]+03400000[     ]+nop
+[      ]+64:[  ]+03400000[     ]+nop
+[      ]+68:[  ]+4c000020[     ]+ret
+[      ]+6c:[  ]+03400000[     ]+nop
+[      ]+6c:[  ]+R_LARCH_ALIGN[        ]+\*ABS\*\+0xc
+[      ]+70:[  ]+03400000[     ]+nop
+[      ]+74:[  ]+03400000[     ]+nop
+[      ]+78:[  ]+4c000020[     ]+ret
+[      ]+7c:[  ]+03400000[     ]+nop
+[      ]+7c:[  ]+R_LARCH_ALIGN[        ]+\*ABS\*\+0xc
+[      ]+80:[  ]+03400000[     ]+nop
+[      ]+84:[  ]+03400000[     ]+nop
+[      ]+88:[  ]+4c000020[     ]+ret
similarity index 79%
rename from gas/testsuite/gas/loongarch/relax_align.s
rename to gas/testsuite/gas/loongarch/relax-align.s
index 079f549895ca7d27ebe6d8395d99fc23556411a7..be78f4c3687e74235d9f66e5e867a74318087c23 100644 (file)
@@ -1,6 +1,6 @@
 # Range of max: 0<= max <= 0xffffffff
-  .text
-.L1:
+.text
+  call .text
   ret
   .align 4
   ret
@@ -14,7 +14,7 @@
   ret
   .align 4, , 0xffffffff
   ret
-# ignore out of range alignment maxumum
+  # ignore out of range alignment maxumum
   .align 4, , -1
   ret
   .align 4, , -0x80000000
diff --git a/gas/testsuite/gas/loongarch/relax-align1.d b/gas/testsuite/gas/loongarch/relax-align1.d
new file mode 100644 (file)
index 0000000..13576df
--- /dev/null
@@ -0,0 +1,8 @@
+#as: -mrelax
+#objdump: -dr
+
+#...
+.*10: R_LARCH_ALIGN.*0x4
+#...
+.*10: R_LARCH_ALIGN.*0x4
+#...
diff --git a/gas/testsuite/gas/loongarch/relax-align1.s b/gas/testsuite/gas/loongarch/relax-align1.s
new file mode 100644 (file)
index 0000000..dfb9342
--- /dev/null
@@ -0,0 +1,16 @@
+# Emit align relocation only if the current section
+# already has relaxed instrctions.
+.text
+  addi.d $t0, $t0, 1
+  .align 3 # Do not emit an align relocation
+  call .text
+  .align 3 # Emit an align relocation
+  addi.d $t0, $t0, 1
+
+.section ".text.a", "ax"
+  addi.d $t0, $t0, 1
+  .align 3 # Do not emit an align relocation
+  call .text
+  .align 3 # Emit an align relocation
+  addi.d $t0, $t0, 1
+
index cbef84f87771a4cbc2d0ae0e7af96b0c9ce33952..516091a57353e73817c94afde6dac0653c2f16a3 100644 (file)
@@ -1,24 +1,19 @@
 #as: --no-warn
 #readelf: -rsW
-#skip: loongarch32-*-*
 
-Relocation section '\.rela\.text' at offset .* contains 2 entries:
-.*
-0+04[  ]+0000000000000066[     ]+R_LARCH_ALIGN[        ]+c
-0+14[  ]+0000000500000066[     ]+R_LARCH_ALIGN[        ]+0+[   ]+\.Lla-relax-align \+ 404
-
-Relocation section '\.rela\.text2' at offset .* contains 2 entries:
-.*
-0+04[  ]+0000000000000066[     ]+R_LARCH_ALIGN[        ]+c
-0+14[  ]+0000000600000066[     ]+R_LARCH_ALIGN[        ]+0+[   ]+\.Lla-relax-align \+ 404
-
-Symbol table '\.symtab' contains .* entries:
 #...
-[      ]+.*:[  ]+0+[   ]+0[    ]+SECTION[      ]+LOCAL[        ]+DEFAULT[      ]+1[    ]+\.text
+Relocation section '\.rela\.text'.*
 #...
-[      ]+.*:[  ]+0+[   ]+0[    ]+SECTION[      ]+LOCAL[        ]+DEFAULT[      ]+5[    ]+\.text2
+.*R_LARCH_ALIGN.*c
+.*R_LARCH_ALIGN.*\.Lla-relax-align.*404
 #...
-[      ]+.*:[  ]+0+[   ]+0[    ]+NOTYPE[       ]+LOCAL[        ]+DEFAULT[      ]+1[    ]+\.Lla-relax-align
+Relocation section '\.rela\.text2'.*
+#...
+.*R_LARCH_ALIGN.*c
+.*R_LARCH_ALIGN.*\.Lla-relax-align.*404
+#...
+Symbol table '\.symtab' contains .* entries:
 #...
-[      ]+.*:[  ]+0+[   ]+0[    ]+NOTYPE[       ]+LOCAL[        ]+DEFAULT[      ]+5[    ]+\.Lla-relax-align
+.*1.*\.Lla-relax-align
+.*5.*\.Lla-relax-align
 #pass
index 6cd6bd8731b3b0b63cf7962ace81ee4434651b6b..4e73d47bf7a737ceca5d96fa1969ada76ba74bbc 100644 (file)
@@ -1,10 +1,14 @@
+# Make align symbol .Lla-relax-align in same section with .align
+
 .section ".text", "ax"
+call .text
 nop
 .align 4
 nop
 .align 4, , 4
 
 .section ".text2", "ax"
+call .text
 nop
 .align 4
 nop
diff --git a/gas/testsuite/gas/loongarch/relax_align.d b/gas/testsuite/gas/loongarch/relax_align.d
deleted file mode 100644 (file)
index a92df37..0000000
+++ /dev/null
@@ -1,52 +0,0 @@
-#as:
-#objdump: -dr
-#skip: loongarch32-*-*
-#warning_output: relax_align.l
-
-.*:[    ]+file format .*
-
-
-Disassembly of section .text:
-
-[      ]*0000000000000000 <.Lla-relax-align>:
-[      ]+0:[   ]+4c000020[     ]+ret
-[      ]+4:[   ]+03400000[     ]+nop
-[      ]+4: R_LARCH_ALIGN[     ]+\*ABS\*\+0xc
-[      ]+8:[   ]+03400000[     ]+nop
-[      ]+c:[   ]+03400000[     ]+nop
-[      ]+10:[  ]+4c000020[     ]+ret
-[      ]+14:[  ]+03400000[     ]+nop
-[      ]+14: R_LARCH_ALIGN[    ]+\*ABS\*\+0xc
-[      ]+18:[  ]+03400000[     ]+nop
-[      ]+1c:[  ]+03400000[     ]+nop
-[      ]+20:[  ]+4c000020[     ]+ret
-[      ]+24:[  ]+03400000[     ]+nop
-[      ]+24: R_LARCH_ALIGN[    ]+.Lla-relax-align\+0x104
-[      ]+28:[  ]+03400000[     ]+nop
-[      ]+2c:[  ]+03400000[     ]+nop
-[      ]+30:[  ]+4c000020[     ]+ret
-[      ]+34:[  ]+03400000[     ]+nop
-[      ]+34: R_LARCH_ALIGN[    ]+.Lla-relax-align\+0xb04
-[      ]+38:[  ]+03400000[     ]+nop
-[      ]+3c:[  ]+03400000[     ]+nop
-[      ]+40:[  ]+4c000020[     ]+ret
-[      ]+44:[  ]+03400000[     ]+nop
-[      ]+44: R_LARCH_ALIGN[    ]+\*ABS\*\+0xc
-[      ]+48:[  ]+03400000[     ]+nop
-[      ]+4c:[  ]+03400000[     ]+nop
-[      ]+50:[  ]+4c000020[     ]+ret
-[      ]+54:[  ]+03400000[     ]+nop
-[      ]+54: R_LARCH_ALIGN[    ]+\*ABS\*\+0xc
-[      ]+58:[  ]+03400000[     ]+nop
-[      ]+5c:[  ]+03400000[     ]+nop
-[      ]+60:[  ]+4c000020[     ]+ret
-[      ]+64:[  ]+03400000[     ]+nop
-[      ]+64: R_LARCH_ALIGN[    ]+\*ABS\*\+0xc
-[      ]+68:[  ]+03400000[     ]+nop
-[      ]+6c:[  ]+03400000[     ]+nop
-[      ]+70:[  ]+4c000020[     ]+ret
-[      ]+74:[  ]+03400000[     ]+nop
-[      ]+74: R_LARCH_ALIGN[    ]+\*ABS\*\+0xc
-[      ]+78:[  ]+03400000[     ]+nop
-[      ]+7c:[  ]+03400000[     ]+nop
-[      ]+80:[  ]+4c000020[     ]+ret
index 45c70b4732bff0ed66321bfe9ec18d29d76e142a..d4ea14e105c82416933a27cad424fc984fd4e7ec 100644 (file)
@@ -74,7 +74,6 @@ if { ![check_compiler_available] } {
     # in a literal pool outside the function, so that both the
     # "undefined function" and "undefined line" tests fail.
     setup_xfail xtensa*-*-linux*
-    setup_xfail loongarch*-*-*
 
     set mf "tmpdir/undefined.o* in function `function':"
     checkund $mf $testfn
@@ -155,7 +154,6 @@ if { ![check_compiler_available] } {
 
     # eBPF doesn't support dwarf yet.
     setup_xfail bpf-*-*
-    setup_xfail loongarch*-*-*
 
     checkund $ml $testline
 }