public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* [PATCH v2 1/2] LoongArch: bfd: Correct the name of R_LARCH_SOP_POP_32_U in howto_table
@ 2023-09-02  6:50 Jinyang He
  2023-09-02  6:50 ` [PATCH v2 2/2] LoongArch: ld: Fix other pop relocs overflow check and add tests Jinyang He
  0 siblings, 1 reply; 4+ messages in thread
From: Jinyang He @ 2023-09-02  6:50 UTC (permalink / raw)
  To: Chenghua Xu, Zhensong Liu, mengqinggang
  Cc: WANG Xuerui, Xi Ruoyao, binutils, Xing Li

---
 bfd/elfxx-loongarch.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/bfd/elfxx-loongarch.c b/bfd/elfxx-loongarch.c
index f27c9fdba..924b11cfa 100644
--- a/bfd/elfxx-loongarch.c
+++ b/bfd/elfxx-loongarch.c
@@ -516,7 +516,7 @@ static loongarch_reloc_howto_type loongarch_howto_table[] =
 	 0,					/* bitpos.  */
 	 complain_overflow_unsigned,		/* complain_on_overflow.  */
 	 bfd_elf_generic_reloc,			/* special_function.  */
-	 "R_LARCH_SOP_POP_32_S_U",		/* name.  */
+	 "R_LARCH_SOP_POP_32_U",		/* name.  */
 	 false,					/* partial_inplace.  */
 	 0xffffffff00000000,			/* src_mask */
 	 0x00000000ffffffff,			/* dst_mask */
-- 
2.33.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2 2/2] LoongArch: ld: Fix other pop relocs overflow check and add tests
  2023-09-02  6:50 [PATCH v2 1/2] LoongArch: bfd: Correct the name of R_LARCH_SOP_POP_32_U in howto_table Jinyang He
@ 2023-09-02  6:50 ` Jinyang He
  2023-09-04  1:26   ` liuzhensong
  0 siblings, 1 reply; 4+ messages in thread
From: Jinyang He @ 2023-09-02  6:50 UTC (permalink / raw)
  To: Chenghua Xu, Zhensong Liu, mengqinggang
  Cc: WANG Xuerui, Xi Ruoyao, binutils, Xing Li

Add reloc_unsign_bits() to fix others sop_pop relocs overflow check.
Then add over/underflow tests for relocs B*, SOP_POP* and PCREL20_S2.

bfd/ChangeLog:

	* bfd/elfxx-loongarch.c: Add reloc_unsign_bits().

ld/ChangeLog:

	* ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp: Add tests.
	* ld/testsuite/ld-loongarch-elf/abi1_max_imm.dd: New test.
	* ld/testsuite/ld-loongarch-elf/abi1_max_imm.s: New test.
	* ld/testsuite/ld-loongarch-elf/abi1_sops.s: New test.
	* ld/testsuite/ld-loongarch-elf/abi2_max_imm.s: New test.
	* ld/testsuite/ld-loongarch-elf/abi2_overflows.s: New test.
	* ld/testsuite/ld-loongarch-elf/max_imm_b16.d: New test.
	* ld/testsuite/ld-loongarch-elf/max_imm_b21.d: New test.
	* ld/testsuite/ld-loongarch-elf/max_imm_b26.d: New test.
	* ld/testsuite/ld-loongarch-elf/max_imm_pcrel20.d: New test.
	* ld/testsuite/ld-loongarch-elf/overflow_b16.d: New test.
	* ld/testsuite/ld-loongarch-elf/overflow_b21.d: New test.
	* ld/testsuite/ld-loongarch-elf/overflow_b26.d: New test.
	* ld/testsuite/ld-loongarch-elf/overflow_pcrel20.d: New test.
	* ld/testsuite/ld-loongarch-elf/overflow_s_0_10_10_16_s2.d: New test.
	* ld/testsuite/ld-loongarch-elf/overflow_s_0_5_10_16_s2.d: New test.
	* ld/testsuite/ld-loongarch-elf/overflow_s_10_12.d: New test.
	* ld/testsuite/ld-loongarch-elf/overflow_s_10_16.d: New test.
	* ld/testsuite/ld-loongarch-elf/overflow_s_10_16_s2.d: New test.
	* ld/testsuite/ld-loongarch-elf/overflow_s_10_5.d: New test.
	* ld/testsuite/ld-loongarch-elf/overflow_s_5_20.d: New test.
	* ld/testsuite/ld-loongarch-elf/overflow_u.d: New test.
	* ld/testsuite/ld-loongarch-elf/overflow_u_10_12.d: New test.
	* ld/testsuite/ld-loongarch-elf/underflow_b16.d: New test.
	* ld/testsuite/ld-loongarch-elf/underflow_b21.d: New test.
	* ld/testsuite/ld-loongarch-elf/underflow_b26.d: New test.
	* ld/testsuite/ld-loongarch-elf/underflow_pcrel20.d: New test.
	* ld/testsuite/ld-loongarch-elf/underflow_s_0_10_10_16_s2.d: New test.
	* ld/testsuite/ld-loongarch-elf/underflow_s_0_5_10_16_s2.d: New test.
	* ld/testsuite/ld-loongarch-elf/underflow_s_10_12.d: New test.
	* ld/testsuite/ld-loongarch-elf/underflow_s_10_16.d: New test.
	* ld/testsuite/ld-loongarch-elf/underflow_s_10_16_s2.d: New test.
	* ld/testsuite/ld-loongarch-elf/underflow_s_10_5.d: New test.
	* ld/testsuite/ld-loongarch-elf/underflow_s_5_20.d: New test.
---

v2:
1. Change filename to max_imm, overflow and underflow.
2. Add -Ttext 0x20000000 when check b16, b21, b26, pcrel20_s2.

 bfd/elfxx-loongarch.c                         | 33 ++++++++++----
 ld/testsuite/ld-loongarch-elf/abi1_max_imm.dd | 18 ++++++++
 ld/testsuite/ld-loongarch-elf/abi1_max_imm.s  | 39 +++++++++++++++++
 ld/testsuite/ld-loongarch-elf/abi1_sops.s     | 43 +++++++++++++++++++
 ld/testsuite/ld-loongarch-elf/abi2_max_imm.s  | 24 +++++++++++
 .../ld-loongarch-elf/abi2_overflows.s         | 24 +++++++++++
 .../ld-loongarch-elf/ld-loongarch-elf.exp     | 39 +++++++++++++++++
 ld/testsuite/ld-loongarch-elf/max_imm_b16.d   |  9 ++++
 ld/testsuite/ld-loongarch-elf/max_imm_b21.d   |  9 ++++
 ld/testsuite/ld-loongarch-elf/max_imm_b26.d   |  9 ++++
 .../ld-loongarch-elf/max_imm_pcrel20.d        |  9 ++++
 ld/testsuite/ld-loongarch-elf/overflow_b16.d  |  4 ++
 ld/testsuite/ld-loongarch-elf/overflow_b21.d  |  4 ++
 ld/testsuite/ld-loongarch-elf/overflow_b26.d  |  4 ++
 .../ld-loongarch-elf/overflow_pcrel20.d       |  4 ++
 .../overflow_s_0_10_10_16_s2.d                |  4 ++
 .../overflow_s_0_5_10_16_s2.d                 |  4 ++
 .../ld-loongarch-elf/overflow_s_10_12.d       |  4 ++
 .../ld-loongarch-elf/overflow_s_10_16.d       |  4 ++
 .../ld-loongarch-elf/overflow_s_10_16_s2.d    |  4 ++
 .../ld-loongarch-elf/overflow_s_10_5.d        |  4 ++
 .../ld-loongarch-elf/overflow_s_5_20.d        |  4 ++
 ld/testsuite/ld-loongarch-elf/overflow_u.d    |  4 ++
 .../ld-loongarch-elf/overflow_u_10_12.d       |  4 ++
 ld/testsuite/ld-loongarch-elf/underflow_b16.d |  4 ++
 ld/testsuite/ld-loongarch-elf/underflow_b21.d |  4 ++
 ld/testsuite/ld-loongarch-elf/underflow_b26.d |  4 ++
 .../ld-loongarch-elf/underflow_pcrel20.d      |  4 ++
 .../underflow_s_0_10_10_16_s2.d               |  4 ++
 .../underflow_s_0_5_10_16_s2.d                |  4 ++
 .../ld-loongarch-elf/underflow_s_10_12.d      |  4 ++
 .../ld-loongarch-elf/underflow_s_10_16.d      |  4 ++
 .../ld-loongarch-elf/underflow_s_10_16_s2.d   |  4 ++
 .../ld-loongarch-elf/underflow_s_10_5.d       |  4 ++
 .../ld-loongarch-elf/underflow_s_5_20.d       |  4 ++
 35 files changed, 343 insertions(+), 9 deletions(-)
 create mode 100644 ld/testsuite/ld-loongarch-elf/abi1_max_imm.dd
 create mode 100644 ld/testsuite/ld-loongarch-elf/abi1_max_imm.s
 create mode 100644 ld/testsuite/ld-loongarch-elf/abi1_sops.s
 create mode 100644 ld/testsuite/ld-loongarch-elf/abi2_max_imm.s
 create mode 100644 ld/testsuite/ld-loongarch-elf/abi2_overflows.s
 create mode 100644 ld/testsuite/ld-loongarch-elf/max_imm_b16.d
 create mode 100644 ld/testsuite/ld-loongarch-elf/max_imm_b21.d
 create mode 100644 ld/testsuite/ld-loongarch-elf/max_imm_b26.d
 create mode 100644 ld/testsuite/ld-loongarch-elf/max_imm_pcrel20.d
 create mode 100644 ld/testsuite/ld-loongarch-elf/overflow_b16.d
 create mode 100644 ld/testsuite/ld-loongarch-elf/overflow_b21.d
 create mode 100644 ld/testsuite/ld-loongarch-elf/overflow_b26.d
 create mode 100644 ld/testsuite/ld-loongarch-elf/overflow_pcrel20.d
 create mode 100644 ld/testsuite/ld-loongarch-elf/overflow_s_0_10_10_16_s2.d
 create mode 100644 ld/testsuite/ld-loongarch-elf/overflow_s_0_5_10_16_s2.d
 create mode 100644 ld/testsuite/ld-loongarch-elf/overflow_s_10_12.d
 create mode 100644 ld/testsuite/ld-loongarch-elf/overflow_s_10_16.d
 create mode 100644 ld/testsuite/ld-loongarch-elf/overflow_s_10_16_s2.d
 create mode 100644 ld/testsuite/ld-loongarch-elf/overflow_s_10_5.d
 create mode 100644 ld/testsuite/ld-loongarch-elf/overflow_s_5_20.d
 create mode 100644 ld/testsuite/ld-loongarch-elf/overflow_u.d
 create mode 100644 ld/testsuite/ld-loongarch-elf/overflow_u_10_12.d
 create mode 100644 ld/testsuite/ld-loongarch-elf/underflow_b16.d
 create mode 100644 ld/testsuite/ld-loongarch-elf/underflow_b21.d
 create mode 100644 ld/testsuite/ld-loongarch-elf/underflow_b26.d
 create mode 100644 ld/testsuite/ld-loongarch-elf/underflow_pcrel20.d
 create mode 100644 ld/testsuite/ld-loongarch-elf/underflow_s_0_10_10_16_s2.d
 create mode 100644 ld/testsuite/ld-loongarch-elf/underflow_s_0_5_10_16_s2.d
 create mode 100644 ld/testsuite/ld-loongarch-elf/underflow_s_10_12.d
 create mode 100644 ld/testsuite/ld-loongarch-elf/underflow_s_10_16.d
 create mode 100644 ld/testsuite/ld-loongarch-elf/underflow_s_10_16_s2.d
 create mode 100644 ld/testsuite/ld-loongarch-elf/underflow_s_10_5.d
 create mode 100644 ld/testsuite/ld-loongarch-elf/underflow_s_5_20.d

diff --git a/bfd/elfxx-loongarch.c b/bfd/elfxx-loongarch.c
index 924b11cfa..9b4fbfe36 100644
--- a/bfd/elfxx-loongarch.c
+++ b/bfd/elfxx-loongarch.c
@@ -55,6 +55,8 @@ static bool
 reloc_bits (bfd *abfd, reloc_howto_type *howto, bfd_vma *val);
 static bool
 reloc_sign_bits (bfd *abfd, reloc_howto_type *howto, bfd_vma *fix_val);
+static bool
+reloc_unsign_bits (bfd *abfd, reloc_howto_type *howto, bfd_vma *fix_val);
 
 static bfd_reloc_status_type
 loongarch_elf_add_sub_reloc (bfd *, arelent *, asymbol *, void *,
@@ -383,7 +385,7 @@ static loongarch_reloc_howto_type loongarch_howto_table[] =
 	 0x7c00,				  /* dst_mask */
 	 false,					  /* pcrel_offset */
 	 BFD_RELOC_LARCH_SOP_POP_32_S_10_5,	  /* bfd_reloc_code_real_type */
-	 reloc_bits,				  /* adjust_reloc_bits */
+	 reloc_sign_bits,			  /* adjust_reloc_bits */
 	 NULL),					  /* larch_reloc_type_name */
 
   LOONGARCH_HOWTO (R_LARCH_SOP_POP_32_U_10_12,	  /* type (39).  */
@@ -400,7 +402,7 @@ static loongarch_reloc_howto_type loongarch_howto_table[] =
 	 0x3ffc00,				  /* dst_mask */
 	 false,					  /* pcrel_offset */
 	 BFD_RELOC_LARCH_SOP_POP_32_U_10_12,	  /* bfd_reloc_code_real_type */
-	 reloc_bits,				  /* adjust_reloc_bits */
+	 reloc_unsign_bits,			  /* adjust_reloc_bits */
 	 NULL),					  /* larch_reloc_type_name */
 
   LOONGARCH_HOWTO (R_LARCH_SOP_POP_32_S_10_12,	  /* type (40).  */
@@ -417,7 +419,7 @@ static loongarch_reloc_howto_type loongarch_howto_table[] =
 	 0x3ffc00,				  /* dst_mask */
 	 false,					  /* pcrel_offset */
 	 BFD_RELOC_LARCH_SOP_POP_32_S_10_12,	  /* bfd_reloc_code_real_type */
-	 reloc_bits,				  /* adjust_reloc_bits */
+	 reloc_sign_bits,			  /* adjust_reloc_bits */
 	 NULL),					  /* larch_reloc_type_name */
 
   LOONGARCH_HOWTO (R_LARCH_SOP_POP_32_S_10_16,	  /* type (41).  */
@@ -434,7 +436,7 @@ static loongarch_reloc_howto_type loongarch_howto_table[] =
 	 0x3fffc00,				  /* dst_mask */
 	 false,					  /* pcrel_offset */
 	 BFD_RELOC_LARCH_SOP_POP_32_S_10_16,	  /* bfd_reloc_code_real_type */
-	 reloc_bits,				  /* adjust_reloc_bits */
+	 reloc_sign_bits,			  /* adjust_reloc_bits */
 	 NULL),					  /* larch_reloc_type_name */
 
   LOONGARCH_HOWTO (R_LARCH_SOP_POP_32_S_10_16_S2, /* type (42).  */
@@ -468,7 +470,7 @@ static loongarch_reloc_howto_type loongarch_howto_table[] =
 	 0x1ffffe0,				  /* dst_mask */
 	 false,					  /* pcrel_offset */
 	 BFD_RELOC_LARCH_SOP_POP_32_S_5_20,	  /* bfd_reloc_code_real_type */
-	 reloc_bits,				  /* adjust_reloc_bits */
+	 reloc_sign_bits,			  /* adjust_reloc_bits */
 	 NULL),					  /* larch_reloc_type_name */
 
   LOONGARCH_HOWTO (R_LARCH_SOP_POP_32_S_0_5_10_16_S2,
@@ -522,7 +524,7 @@ static loongarch_reloc_howto_type loongarch_howto_table[] =
 	 0x00000000ffffffff,			/* dst_mask */
 	 false,					/* pcrel_offset */
 	 BFD_RELOC_LARCH_SOP_POP_32_U,		/* bfd_reloc_code_real_type */
-	 reloc_bits,				/* adjust_reloc_bits */
+	 reloc_unsign_bits,			/* adjust_reloc_bits */
 	 NULL),					/* larch_reloc_type_name */
 
   /* 8-bit in-place addition, for local label subtraction.  */
@@ -1671,9 +1673,10 @@ reloc_bits (bfd *abfd ATTRIBUTE_UNUSED,
 }
 
 static bool
-reloc_sign_bits (bfd *abfd, reloc_howto_type *howto, bfd_vma *fix_val)
+reloc_bits_sanity (bfd *abfd, reloc_howto_type *howto, bfd_vma *fix_val, bool sign)
 {
-  if (howto->complain_on_overflow != complain_overflow_signed)
+  if ((sign && howto->complain_on_overflow != complain_overflow_signed)
+      || (!sign && howto->complain_on_overflow != complain_overflow_unsigned))
     return false;
 
   bfd_signed_vma val = (bfd_signed_vma)(*fix_val);
@@ -1689,7 +1692,7 @@ reloc_sign_bits (bfd *abfd, reloc_howto_type *howto, bfd_vma *fix_val)
     }
 
   bfd_signed_vma mask = ((bfd_signed_vma)0x1 << (howto->bitsize
-			  + howto->rightshift - 1)) - 1;
+			  + howto->rightshift - sign)) - 1;
 
   /* Positive number: high part is all 0;
      Negative number: if high part is not all 0, high part must be all 1.
@@ -1728,6 +1731,18 @@ reloc_sign_bits (bfd *abfd, reloc_howto_type *howto, bfd_vma *fix_val)
   return true;
 }
 
+static bool
+reloc_sign_bits (bfd *abfd, reloc_howto_type *howto, bfd_vma *fix_val)
+{
+  return reloc_bits_sanity (abfd, howto, fix_val, true);
+}
+
+static bool
+reloc_unsign_bits (bfd *abfd, reloc_howto_type *howto, bfd_vma *fix_val)
+{
+  return reloc_bits_sanity (abfd, howto, fix_val, false);
+}
+
 bool
 loongarch_adjust_reloc_bitsfield (bfd *abfd, reloc_howto_type *howto,
 				  bfd_vma *fix_val)
diff --git a/ld/testsuite/ld-loongarch-elf/abi1_max_imm.dd b/ld/testsuite/ld-loongarch-elf/abi1_max_imm.dd
new file mode 100644
index 000000000..c85271408
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/abi1_max_imm.dd
@@ -0,0 +1,18 @@
+#...
+.*00003c00.*
+.*00004000.*
+.*001ffc00.*
+.*00200000.*
+.*01fffc00.*
+.*02000000.*
+.*01fffc00.*
+.*02000000.*
+.*03fffc0f.*
+.*00000010.*
+.*03fffdff.*
+.*00000200.*
+.*00ffffe0.*
+.*01000000.*
+.*003ffc00.*
+.*ffffffff.*
+#pass
diff --git a/ld/testsuite/ld-loongarch-elf/abi1_max_imm.s b/ld/testsuite/ld-loongarch-elf/abi1_max_imm.s
new file mode 100644
index 000000000..02507f19c
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/abi1_max_imm.s
@@ -0,0 +1,39 @@
+.macro sign_check reloc_name reloc_bits
+  .reloc off, R_LARCH_SOP_PUSH_ABSOLUTE, (1 << (\reloc_bits - 1)) - 1
+  .reloc off, \reloc_name
+  .word 0
+  .set off, off + 4
+  .reloc off, R_LARCH_SOP_PUSH_ABSOLUTE, -(1 << (\reloc_bits - 1))
+  .reloc off, \reloc_name
+  .word 0
+  .set off, off + 4
+.endm
+
+.macro sign_check_s2 reloc_name reloc_bits
+  .reloc off, R_LARCH_SOP_PUSH_ABSOLUTE, ((1 << (\reloc_bits - 1)) - 1) << 2
+  .reloc off, \reloc_name
+  .word 0
+  .set off, off + 4
+  .reloc off, R_LARCH_SOP_PUSH_ABSOLUTE, (-(1 << (\reloc_bits - 1))) << 2
+  .reloc off, \reloc_name
+  .word 0
+  .set off, off + 4
+.endm
+
+.macro unsign_check reloc_name reloc_bits
+  .reloc off, R_LARCH_SOP_PUSH_ABSOLUTE, (1 << \reloc_bits) - 1
+  .reloc off, \reloc_name
+  .word 0
+  .set off, off + 4
+.endm
+
+.set off, 0
+sign_check R_LARCH_SOP_POP_32_S_10_5 5
+sign_check R_LARCH_SOP_POP_32_S_10_12 12
+sign_check R_LARCH_SOP_POP_32_S_10_16 16
+sign_check_s2 R_LARCH_SOP_POP_32_S_10_16_S2 16
+sign_check_s2 R_LARCH_SOP_POP_32_S_0_5_10_16_S2 21
+sign_check_s2 R_LARCH_SOP_POP_32_S_0_10_10_16_S2 26
+sign_check R_LARCH_SOP_POP_32_S_5_20 20
+unsign_check R_LARCH_SOP_POP_32_U_10_12 12
+unsign_check R_LARCH_SOP_POP_32_U 32
diff --git a/ld/testsuite/ld-loongarch-elf/abi1_sops.s b/ld/testsuite/ld-loongarch-elf/abi1_sops.s
new file mode 100644
index 000000000..34f2d5920
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/abi1_sops.s
@@ -0,0 +1,43 @@
+.macro sign_check reloc_sym reloc_name reloc_bits
+  .ifdef \reloc_sym
+    .ifdef overflow
+      .reloc 0, R_LARCH_SOP_PUSH_ABSOLUTE, 1 << (\reloc_bits - 1)
+      .reloc 0, \reloc_name
+    .else
+      .reloc 0, R_LARCH_SOP_PUSH_ABSOLUTE, -(1 << (\reloc_bits - 1)) - 1
+      .reloc 0, \reloc_name
+    .endif
+    .word 0
+  .endif
+.endm
+
+.macro sign_check_s2 reloc_sym reloc_name reloc_bits
+  .ifdef \reloc_sym
+    .ifdef overflow
+      .reloc 0, R_LARCH_SOP_PUSH_ABSOLUTE, (1 << (\reloc_bits - 1)) << 2
+      .reloc 0, \reloc_name
+    .else
+      .reloc 0, R_LARCH_SOP_PUSH_ABSOLUTE, (-(1 << (\reloc_bits - 1)) - 1) << 2
+      .reloc 0, \reloc_name
+    .endif
+    .word 0
+  .endif
+.endm
+
+.macro unsign_check reloc_sym reloc_name reloc_bits
+  .ifdef \reloc_sym
+    .reloc 0, R_LARCH_SOP_PUSH_ABSOLUTE, 1 << \reloc_bits
+    .reloc 0, \reloc_name
+    .word 0
+  .endif
+.endm
+
+sign_check	s_10_5		R_LARCH_SOP_POP_32_S_10_5 5
+sign_check	s_10_12		R_LARCH_SOP_POP_32_S_10_12 12
+sign_check	s_10_16		R_LARCH_SOP_POP_32_S_10_16 16
+sign_check_s2	s_10_16_s2	R_LARCH_SOP_POP_32_S_10_16_S2 16
+sign_check_s2	s_0_5_10_16_s2	R_LARCH_SOP_POP_32_S_0_5_10_16_S2 21
+sign_check_s2	s_0_10_10_16_s2	R_LARCH_SOP_POP_32_S_0_10_10_16_S2 26
+sign_check	s_5_20		R_LARCH_SOP_POP_32_S_5_20 20
+unsign_check	u_10_12		R_LARCH_SOP_POP_32_U_10_12 12
+unsign_check	u		R_LARCH_SOP_POP_32_U 32
diff --git a/ld/testsuite/ld-loongarch-elf/abi2_max_imm.s b/ld/testsuite/ld-loongarch-elf/abi2_max_imm.s
new file mode 100644
index 000000000..f1bc3868f
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/abi2_max_imm.s
@@ -0,0 +1,24 @@
+  .text
+.ifdef b16
+  beq $a0, $a1, low
+  beq $a0, $a1, high
+.endif
+.ifdef b21
+  beqz $a0, low
+  beqz $a0, high
+.endif
+.ifdef b26
+  b low
+  b high
+.endif
+.ifdef pcrel20
+  .reloc 0, R_LARCH_PCREL20_S2, low
+  pcaddi $a0, 0
+  .reloc 4, R_LARCH_PCREL20_S2, high
+  pcaddi $a0, 0
+.endif
+
+  .section ".low"
+low: nop
+  .section ".high"
+high: nop
diff --git a/ld/testsuite/ld-loongarch-elf/abi2_overflows.s b/ld/testsuite/ld-loongarch-elf/abi2_overflows.s
new file mode 100644
index 000000000..1c25bd8c5
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/abi2_overflows.s
@@ -0,0 +1,24 @@
+  .text
+
+.set idx, 0
+.ifdef underflow
+  nop
+  .set idx, 4
+.endif
+
+.ifdef b16
+  beq $a0, $a1, local_sym
+.endif
+.ifdef b21
+  beqz $a0, local_sym
+.endif
+.ifdef b26
+  b local_sym
+.endif
+.ifdef pcrel20
+  .reloc idx, R_LARCH_PCREL20_S2, local_sym
+  pcaddi $a0, 0
+.endif
+
+  .section ".local_sym"
+local_sym: nop
diff --git a/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp b/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp
index b95cc53e5..ae90daec2 100644
--- a/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp
+++ b/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp
@@ -32,6 +32,22 @@ if [istarget "loongarch64-*-*"] {
     run_dump_test "syscall"
     run_dump_test "disas-jirl"
     run_dump_test "local-ifunc-reloc"
+    run_dump_test "overflow_s_10_5"
+    run_dump_test "overflow_s_10_12"
+    run_dump_test "overflow_s_10_16"
+    run_dump_test "overflow_s_10_16_s2"
+    run_dump_test "overflow_s_0_5_10_16_s2"
+    run_dump_test "overflow_s_0_10_10_16_s2"
+    run_dump_test "overflow_s_5_20"
+    run_dump_test "overflow_u_10_12"
+    run_dump_test "overflow_u"
+    run_dump_test "underflow_s_10_5"
+    run_dump_test "underflow_s_10_12"
+    run_dump_test "underflow_s_10_16"
+    run_dump_test "underflow_s_10_16_s2"
+    run_dump_test "underflow_s_0_5_10_16_s2"
+    run_dump_test "underflow_s_0_10_10_16_s2"
+    run_dump_test "underflow_s_5_20"
 }
 
 if [istarget "loongarch32-*-*"] {
@@ -54,5 +70,28 @@ if [istarget "loongarch64-*-*"] {
 	      ] \
 	      "64_pcrel" \
 	  ] \
+	  [list \
+	      "abi1_max_imm" \
+	      "-e 0x0" "" \
+	      "" \
+	      {abi1_max_imm.s} \
+	      [list \
+		  [list objdump -D abi1_max_imm.dd] \
+	      ] \
+	      "abi1_max_imm" \
+	  ] \
       ]
 }
+
+run_dump_test "max_imm_b16"
+run_dump_test "max_imm_b21"
+run_dump_test "max_imm_b26"
+run_dump_test "max_imm_pcrel20"
+run_dump_test "overflow_b16"
+run_dump_test "overflow_b21"
+run_dump_test "overflow_b26"
+run_dump_test "overflow_pcrel20"
+run_dump_test "underflow_b16"
+run_dump_test "underflow_b21"
+run_dump_test "underflow_b26"
+run_dump_test "underflow_pcrel20"
diff --git a/ld/testsuite/ld-loongarch-elf/max_imm_b16.d b/ld/testsuite/ld-loongarch-elf/max_imm_b16.d
new file mode 100644
index 000000000..2c3f6cdbd
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/max_imm_b16.d
@@ -0,0 +1,9 @@
+#source: abi2_max_imm.s
+#as: --defsym b16=1
+#ld: -e 0 -Ttext 0x20000000 --section-start=.low=0x1ffe0000 --section-start=.high=0x20020000
+#objdump: -s
+
+#...
+Contents of section .text:
+ 20000000 8500005a 85fcff59.*
+#pass
diff --git a/ld/testsuite/ld-loongarch-elf/max_imm_b21.d b/ld/testsuite/ld-loongarch-elf/max_imm_b21.d
new file mode 100644
index 000000000..5861fb464
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/max_imm_b21.d
@@ -0,0 +1,9 @@
+#source: abi2_max_imm.s
+#as: --defsym b21=1
+#ld: -e 0 -Ttext 0x20000000 --section-start=.low=0x1fc00000 --section-start=.high=0x20400000
+#objdump: -s
+
+#...
+Contents of section .text:
+ 20000000 90000040 8ffcff43.*
+#pass
diff --git a/ld/testsuite/ld-loongarch-elf/max_imm_b26.d b/ld/testsuite/ld-loongarch-elf/max_imm_b26.d
new file mode 100644
index 000000000..f475ccb32
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/max_imm_b26.d
@@ -0,0 +1,9 @@
+#source: abi2_max_imm.s
+#as: --defsym b26=1
+#ld: -e 0 -Ttext 0x20000000 --section-start=.low=0x18000000 --section-start=.high=0x28000000
+#objdump: -s
+
+#...
+Contents of section .text:
+ 20000000 00020050 fffdff53.*
+#pass
diff --git a/ld/testsuite/ld-loongarch-elf/max_imm_pcrel20.d b/ld/testsuite/ld-loongarch-elf/max_imm_pcrel20.d
new file mode 100644
index 000000000..4bd9d2f11
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/max_imm_pcrel20.d
@@ -0,0 +1,9 @@
+#source: abi2_max_imm.s
+#as: --defsym pcrel20=1
+#ld: -e 0 -Ttext 0x20000000 --section-start=.low=0x1fe00000 --section-start=.high=0x20200000
+#objdump: -s
+
+#...
+Contents of section .text:
+ 20000000 04000019 e4ffff18.*
+#pass
diff --git a/ld/testsuite/ld-loongarch-elf/overflow_b16.d b/ld/testsuite/ld-loongarch-elf/overflow_b16.d
new file mode 100644
index 000000000..26cad414b
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/overflow_b16.d
@@ -0,0 +1,4 @@
+#source: abi2_overflows.s
+#as: --defsym overflow=1 --defsym b16=1
+#ld: -e 0 --section-start=.local_sym=0x20020000 -Ttext 0x20000000
+#error: .*relocation truncated to fit: R_LARCH_B16 against `local_sym'
diff --git a/ld/testsuite/ld-loongarch-elf/overflow_b21.d b/ld/testsuite/ld-loongarch-elf/overflow_b21.d
new file mode 100644
index 000000000..826856f20
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/overflow_b21.d
@@ -0,0 +1,4 @@
+#source: abi2_overflows.s
+#as: --defsym overflow=1 --defsym b21=1
+#ld: -e 0 --section-start=.local_sym=0x20400000 -Ttext 0x20000000
+#error: .*relocation truncated to fit: R_LARCH_B21 against `local_sym'
diff --git a/ld/testsuite/ld-loongarch-elf/overflow_b26.d b/ld/testsuite/ld-loongarch-elf/overflow_b26.d
new file mode 100644
index 000000000..474e57510
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/overflow_b26.d
@@ -0,0 +1,4 @@
+#source: abi2_overflows.s
+#as: --defsym overflow=1 --defsym b26=1
+#ld: -e 0 --section-start=.local_sym=0x28000000 -Ttext 0x20000000
+#error: .*relocation truncated to fit: R_LARCH_B26 against `local_sym'
diff --git a/ld/testsuite/ld-loongarch-elf/overflow_pcrel20.d b/ld/testsuite/ld-loongarch-elf/overflow_pcrel20.d
new file mode 100644
index 000000000..420be7290
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/overflow_pcrel20.d
@@ -0,0 +1,4 @@
+#source: abi2_overflows.s
+#as: --defsym overflow=1 --defsym pcrel20=1
+#ld: -e 0 --section-start=.local_sym=0x20200000 -Ttext 0x20000000
+#error: .*relocation truncated to fit: R_LARCH_PCREL20_S2 against `local_sym'
diff --git a/ld/testsuite/ld-loongarch-elf/overflow_s_0_10_10_16_s2.d b/ld/testsuite/ld-loongarch-elf/overflow_s_0_10_10_16_s2.d
new file mode 100644
index 000000000..621054b1d
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/overflow_s_0_10_10_16_s2.d
@@ -0,0 +1,4 @@
+#source: abi1_sops.s
+#as: --defsym overflow=1 --defsym s_0_10_10_16_s2=1
+#ld: -e 0
+#error: .*relocation truncated to fit: R_LARCH_SOP_POP_32_S_0_10_10_16_S2 against `<nameless>'
\ No newline at end of file
diff --git a/ld/testsuite/ld-loongarch-elf/overflow_s_0_5_10_16_s2.d b/ld/testsuite/ld-loongarch-elf/overflow_s_0_5_10_16_s2.d
new file mode 100644
index 000000000..348f5ae93
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/overflow_s_0_5_10_16_s2.d
@@ -0,0 +1,4 @@
+#source: abi1_sops.s
+#as: --defsym overflow=1 --defsym s_0_5_10_16_s2=1
+#ld: -e 0
+#error: .*relocation truncated to fit: R_LARCH_SOP_POP_32_S_0_5_10_16_S2 against `<nameless>'
\ No newline at end of file
diff --git a/ld/testsuite/ld-loongarch-elf/overflow_s_10_12.d b/ld/testsuite/ld-loongarch-elf/overflow_s_10_12.d
new file mode 100644
index 000000000..dcadff7e0
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/overflow_s_10_12.d
@@ -0,0 +1,4 @@
+#source: abi1_sops.s
+#as: --defsym overflow=1 --defsym s_10_12=1
+#ld: -e 0
+#error: .*relocation truncated to fit: R_LARCH_SOP_POP_32_S_10_12 against `<nameless>'
\ No newline at end of file
diff --git a/ld/testsuite/ld-loongarch-elf/overflow_s_10_16.d b/ld/testsuite/ld-loongarch-elf/overflow_s_10_16.d
new file mode 100644
index 000000000..3677415af
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/overflow_s_10_16.d
@@ -0,0 +1,4 @@
+#source: abi1_sops.s
+#as: --defsym overflow=1 --defsym s_10_16=1
+#ld: -e 0
+#error: .*relocation truncated to fit: R_LARCH_SOP_POP_32_S_10_16 against `<nameless>'
\ No newline at end of file
diff --git a/ld/testsuite/ld-loongarch-elf/overflow_s_10_16_s2.d b/ld/testsuite/ld-loongarch-elf/overflow_s_10_16_s2.d
new file mode 100644
index 000000000..24ffa811a
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/overflow_s_10_16_s2.d
@@ -0,0 +1,4 @@
+#source: abi1_sops.s
+#as: --defsym overflow=1 --defsym s_10_16_s2=1
+#ld: -e 0
+#error: .*relocation truncated to fit: R_LARCH_SOP_POP_32_S_10_16_S2 against `<nameless>'
\ No newline at end of file
diff --git a/ld/testsuite/ld-loongarch-elf/overflow_s_10_5.d b/ld/testsuite/ld-loongarch-elf/overflow_s_10_5.d
new file mode 100644
index 000000000..7881457d0
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/overflow_s_10_5.d
@@ -0,0 +1,4 @@
+#source: abi1_sops.s
+#as: --defsym overflow=1 --defsym s_10_5=1
+#ld: -e 0
+#error: .*relocation truncated to fit: R_LARCH_SOP_POP_32_S_10_5 against `<nameless>'
\ No newline at end of file
diff --git a/ld/testsuite/ld-loongarch-elf/overflow_s_5_20.d b/ld/testsuite/ld-loongarch-elf/overflow_s_5_20.d
new file mode 100644
index 000000000..5f06eed8c
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/overflow_s_5_20.d
@@ -0,0 +1,4 @@
+#source: abi1_sops.s
+#as: --defsym overflow=1 --defsym s_5_20=1
+#ld: -e 0
+#error: .*relocation truncated to fit: R_LARCH_SOP_POP_32_S_5_20 against `<nameless>'
\ No newline at end of file
diff --git a/ld/testsuite/ld-loongarch-elf/overflow_u.d b/ld/testsuite/ld-loongarch-elf/overflow_u.d
new file mode 100644
index 000000000..96906f643
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/overflow_u.d
@@ -0,0 +1,4 @@
+#source: abi1_sops.s
+#as: --defsym overflow=1 --defsym u=1
+#ld: -e 0
+#error: .*relocation truncated to fit: R_LARCH_SOP_POP_32_U against `<nameless>'
\ No newline at end of file
diff --git a/ld/testsuite/ld-loongarch-elf/overflow_u_10_12.d b/ld/testsuite/ld-loongarch-elf/overflow_u_10_12.d
new file mode 100644
index 000000000..9d8e09056
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/overflow_u_10_12.d
@@ -0,0 +1,4 @@
+#source: abi1_sops.s
+#as: --defsym overflow=1 --defsym u_10_12=1
+#ld: -e 0
+#error: .*relocation truncated to fit: R_LARCH_SOP_POP_32_U_10_12 against `<nameless>'
\ No newline at end of file
diff --git a/ld/testsuite/ld-loongarch-elf/underflow_b16.d b/ld/testsuite/ld-loongarch-elf/underflow_b16.d
new file mode 100644
index 000000000..e2e21949b
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/underflow_b16.d
@@ -0,0 +1,4 @@
+#source: abi2_overflows.s
+#as: --defsym underflow=1 --defsym b16=1
+#ld: -e 0 --section-start=.local_sym=0x1ffe0000 -Ttext 0x20000000
+#error: .*relocation truncated to fit: R_LARCH_B16 against `local_sym'
diff --git a/ld/testsuite/ld-loongarch-elf/underflow_b21.d b/ld/testsuite/ld-loongarch-elf/underflow_b21.d
new file mode 100644
index 000000000..6f56526b5
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/underflow_b21.d
@@ -0,0 +1,4 @@
+#source: abi2_overflows.s
+#as: --defsym underflow=1 --defsym b21=1
+#ld: -e 0 --section-start=.local_sym=0x1fc00000 -Ttext 0x20000000
+#error: .*relocation truncated to fit: R_LARCH_B21 against `local_sym'
diff --git a/ld/testsuite/ld-loongarch-elf/underflow_b26.d b/ld/testsuite/ld-loongarch-elf/underflow_b26.d
new file mode 100644
index 000000000..5756cc14c
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/underflow_b26.d
@@ -0,0 +1,4 @@
+#source: abi2_overflows.s
+#as: --defsym underflow=1 --defsym b26=1
+#ld: -e 0 --section-start=.local_sym=0x18000000 -Ttext 0x20000000
+#error: .*relocation truncated to fit: R_LARCH_B26 against `local_sym'
diff --git a/ld/testsuite/ld-loongarch-elf/underflow_pcrel20.d b/ld/testsuite/ld-loongarch-elf/underflow_pcrel20.d
new file mode 100644
index 000000000..e65c60ad7
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/underflow_pcrel20.d
@@ -0,0 +1,4 @@
+#source: abi2_overflows.s
+#as: --defsym underflow=1 --defsym pcrel20=1
+#ld: -e 0 --section-start=.local_sym=0x1fe00000 -Ttext 0x20000000
+#error: .*relocation truncated to fit: R_LARCH_PCREL20_S2 against `local_sym'
diff --git a/ld/testsuite/ld-loongarch-elf/underflow_s_0_10_10_16_s2.d b/ld/testsuite/ld-loongarch-elf/underflow_s_0_10_10_16_s2.d
new file mode 100644
index 000000000..3e7715a27
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/underflow_s_0_10_10_16_s2.d
@@ -0,0 +1,4 @@
+#source: abi1_sops.s
+#as: --defsym underflow=1 --defsym s_0_10_10_16_s2=1
+#ld: -e 0
+#error: .*relocation truncated to fit: R_LARCH_SOP_POP_32_S_0_10_10_16_S2 against `<nameless>'
\ No newline at end of file
diff --git a/ld/testsuite/ld-loongarch-elf/underflow_s_0_5_10_16_s2.d b/ld/testsuite/ld-loongarch-elf/underflow_s_0_5_10_16_s2.d
new file mode 100644
index 000000000..14be5a9c6
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/underflow_s_0_5_10_16_s2.d
@@ -0,0 +1,4 @@
+#source: abi1_sops.s
+#as: --defsym underflow=1 --defsym s_0_5_10_16_s2=1
+#ld: -e 0
+#error: .*relocation truncated to fit: R_LARCH_SOP_POP_32_S_0_5_10_16_S2 against `<nameless>'
\ No newline at end of file
diff --git a/ld/testsuite/ld-loongarch-elf/underflow_s_10_12.d b/ld/testsuite/ld-loongarch-elf/underflow_s_10_12.d
new file mode 100644
index 000000000..ed9fe9b70
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/underflow_s_10_12.d
@@ -0,0 +1,4 @@
+#source: abi1_sops.s
+#as: --defsym underflow=1 --defsym s_10_12=1
+#ld: -e 0
+#error: .*relocation truncated to fit: R_LARCH_SOP_POP_32_S_10_12 against `<nameless>'
\ No newline at end of file
diff --git a/ld/testsuite/ld-loongarch-elf/underflow_s_10_16.d b/ld/testsuite/ld-loongarch-elf/underflow_s_10_16.d
new file mode 100644
index 000000000..03b851b57
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/underflow_s_10_16.d
@@ -0,0 +1,4 @@
+#source: abi1_sops.s
+#as: --defsym underflow=1 --defsym s_10_16=1
+#ld: -e 0
+#error: .*relocation truncated to fit: R_LARCH_SOP_POP_32_S_10_16 against `<nameless>'
\ No newline at end of file
diff --git a/ld/testsuite/ld-loongarch-elf/underflow_s_10_16_s2.d b/ld/testsuite/ld-loongarch-elf/underflow_s_10_16_s2.d
new file mode 100644
index 000000000..c0e4ca039
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/underflow_s_10_16_s2.d
@@ -0,0 +1,4 @@
+#source: abi1_sops.s
+#as: --defsym underflow=1 --defsym s_10_16_s2=1
+#ld: -e 0
+#error: .*relocation truncated to fit: R_LARCH_SOP_POP_32_S_10_16_S2 against `<nameless>'
\ No newline at end of file
diff --git a/ld/testsuite/ld-loongarch-elf/underflow_s_10_5.d b/ld/testsuite/ld-loongarch-elf/underflow_s_10_5.d
new file mode 100644
index 000000000..77d0eb04e
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/underflow_s_10_5.d
@@ -0,0 +1,4 @@
+#source: abi1_sops.s
+#as: --defsym underflow=1 --defsym s_10_5=1
+#ld: -e 0
+#error: .*relocation truncated to fit: R_LARCH_SOP_POP_32_S_10_5 against `<nameless>'
\ No newline at end of file
diff --git a/ld/testsuite/ld-loongarch-elf/underflow_s_5_20.d b/ld/testsuite/ld-loongarch-elf/underflow_s_5_20.d
new file mode 100644
index 000000000..c458bb815
--- /dev/null
+++ b/ld/testsuite/ld-loongarch-elf/underflow_s_5_20.d
@@ -0,0 +1,4 @@
+#source: abi1_sops.s
+#as: --defsym underflow=1 --defsym s_5_20=1
+#ld: -e 0
+#error: .*relocation truncated to fit: R_LARCH_SOP_POP_32_S_5_20 against `<nameless>'
\ No newline at end of file
-- 
2.33.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2 2/2] LoongArch: ld: Fix other pop relocs overflow check and add tests
  2023-09-02  6:50 ` [PATCH v2 2/2] LoongArch: ld: Fix other pop relocs overflow check and add tests Jinyang He
@ 2023-09-04  1:26   ` liuzhensong
  2023-09-04  2:24     ` Jinyang He
  0 siblings, 1 reply; 4+ messages in thread
From: liuzhensong @ 2023-09-04  1:26 UTC (permalink / raw)
  To: Jinyang He, Chenghua Xu, mengqinggang
  Cc: WANG Xuerui, Xi Ruoyao, binutils, Xing Li

在 2023/9/2 下午2:50, Jinyang He 写道:
> Add reloc_unsign_bits() to fix others sop_pop relocs overflow check.
> Then add over/underflow tests for relocs B*, SOP_POP* and PCREL20_S2.
>
> bfd/ChangeLog:
>
> 	* bfd/elfxx-loongarch.c: Add reloc_unsign_bits().
>
> ld/ChangeLog:
>
> 	* ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp: Add tests.
> 	* ld/testsuite/ld-loongarch-elf/abi1_max_imm.dd: New test.
> 	* ld/testsuite/ld-loongarch-elf/abi1_max_imm.s: New test.
> 	* ld/testsuite/ld-loongarch-elf/abi1_sops.s: New test.
> 	* ld/testsuite/ld-loongarch-elf/abi2_max_imm.s: New test.
> 	* ld/testsuite/ld-loongarch-elf/abi2_overflows.s: New test.
> 	* ld/testsuite/ld-loongarch-elf/max_imm_b16.d: New test.
> 	* ld/testsuite/ld-loongarch-elf/max_imm_b21.d: New test.
> 	* ld/testsuite/ld-loongarch-elf/max_imm_b26.d: New test.
> 	* ld/testsuite/ld-loongarch-elf/max_imm_pcrel20.d: New test.
> 	* ld/testsuite/ld-loongarch-elf/overflow_b16.d: New test.
> 	* ld/testsuite/ld-loongarch-elf/overflow_b21.d: New test.
> 	* ld/testsuite/ld-loongarch-elf/overflow_b26.d: New test.
> 	* ld/testsuite/ld-loongarch-elf/overflow_pcrel20.d: New test.
> 	* ld/testsuite/ld-loongarch-elf/overflow_s_0_10_10_16_s2.d: New test.
> 	* ld/testsuite/ld-loongarch-elf/overflow_s_0_5_10_16_s2.d: New test.
> 	* ld/testsuite/ld-loongarch-elf/overflow_s_10_12.d: New test.
> 	* ld/testsuite/ld-loongarch-elf/overflow_s_10_16.d: New test.
> 	* ld/testsuite/ld-loongarch-elf/overflow_s_10_16_s2.d: New test.
> 	* ld/testsuite/ld-loongarch-elf/overflow_s_10_5.d: New test.
> 	* ld/testsuite/ld-loongarch-elf/overflow_s_5_20.d: New test.
> 	* ld/testsuite/ld-loongarch-elf/overflow_u.d: New test.
> 	* ld/testsuite/ld-loongarch-elf/overflow_u_10_12.d: New test.
> 	* ld/testsuite/ld-loongarch-elf/underflow_b16.d: New test.
> 	* ld/testsuite/ld-loongarch-elf/underflow_b21.d: New test.
> 	* ld/testsuite/ld-loongarch-elf/underflow_b26.d: New test.
> 	* ld/testsuite/ld-loongarch-elf/underflow_pcrel20.d: New test.
> 	* ld/testsuite/ld-loongarch-elf/underflow_s_0_10_10_16_s2.d: New test.
> 	* ld/testsuite/ld-loongarch-elf/underflow_s_0_5_10_16_s2.d: New test.
> 	* ld/testsuite/ld-loongarch-elf/underflow_s_10_12.d: New test.
> 	* ld/testsuite/ld-loongarch-elf/underflow_s_10_16.d: New test.
> 	* ld/testsuite/ld-loongarch-elf/underflow_s_10_16_s2.d: New test.
> 	* ld/testsuite/ld-loongarch-elf/underflow_s_10_5.d: New test.
> 	* ld/testsuite/ld-loongarch-elf/underflow_s_5_20.d: New test.
> ---
>
> v2:
> 1. Change filename to max_imm, overflow and underflow.
> 2. Add -Ttext 0x20000000 when check b16, b21, b26, pcrel20_s2.
>
>   bfd/elfxx-loongarch.c                         | 33 ++++++++++----
>   ld/testsuite/ld-loongarch-elf/abi1_max_imm.dd | 18 ++++++++
>   ld/testsuite/ld-loongarch-elf/abi1_max_imm.s  | 39 +++++++++++++++++
>   ld/testsuite/ld-loongarch-elf/abi1_sops.s     | 43 +++++++++++++++++++
>   ld/testsuite/ld-loongarch-elf/abi2_max_imm.s  | 24 +++++++++++
>   .../ld-loongarch-elf/abi2_overflows.s         | 24 +++++++++++
>   .../ld-loongarch-elf/ld-loongarch-elf.exp     | 39 +++++++++++++++++
>   ld/testsuite/ld-loongarch-elf/max_imm_b16.d   |  9 ++++
>   ld/testsuite/ld-loongarch-elf/max_imm_b21.d   |  9 ++++
>   ld/testsuite/ld-loongarch-elf/max_imm_b26.d   |  9 ++++
>   .../ld-loongarch-elf/max_imm_pcrel20.d        |  9 ++++
>   ld/testsuite/ld-loongarch-elf/overflow_b16.d  |  4 ++
>   ld/testsuite/ld-loongarch-elf/overflow_b21.d  |  4 ++
>   ld/testsuite/ld-loongarch-elf/overflow_b26.d  |  4 ++
>   .../ld-loongarch-elf/overflow_pcrel20.d       |  4 ++
>   .../overflow_s_0_10_10_16_s2.d                |  4 ++
>   .../overflow_s_0_5_10_16_s2.d                 |  4 ++
>   .../ld-loongarch-elf/overflow_s_10_12.d       |  4 ++
>   .../ld-loongarch-elf/overflow_s_10_16.d       |  4 ++
>   .../ld-loongarch-elf/overflow_s_10_16_s2.d    |  4 ++
>   .../ld-loongarch-elf/overflow_s_10_5.d        |  4 ++
>   .../ld-loongarch-elf/overflow_s_5_20.d        |  4 ++
>   ld/testsuite/ld-loongarch-elf/overflow_u.d    |  4 ++
>   .../ld-loongarch-elf/overflow_u_10_12.d       |  4 ++
>   ld/testsuite/ld-loongarch-elf/underflow_b16.d |  4 ++
>   ld/testsuite/ld-loongarch-elf/underflow_b21.d |  4 ++
>   ld/testsuite/ld-loongarch-elf/underflow_b26.d |  4 ++
>   .../ld-loongarch-elf/underflow_pcrel20.d      |  4 ++
>   .../underflow_s_0_10_10_16_s2.d               |  4 ++
>   .../underflow_s_0_5_10_16_s2.d                |  4 ++
>   .../ld-loongarch-elf/underflow_s_10_12.d      |  4 ++
>   .../ld-loongarch-elf/underflow_s_10_16.d      |  4 ++
>   .../ld-loongarch-elf/underflow_s_10_16_s2.d   |  4 ++
>   .../ld-loongarch-elf/underflow_s_10_5.d       |  4 ++
>   .../ld-loongarch-elf/underflow_s_5_20.d       |  4 ++
>   35 files changed, 343 insertions(+), 9 deletions(-)
>   create mode 100644 ld/testsuite/ld-loongarch-elf/abi1_max_imm.dd
>   create mode 100644 ld/testsuite/ld-loongarch-elf/abi1_max_imm.s
>   create mode 100644 ld/testsuite/ld-loongarch-elf/abi1_sops.s
>   create mode 100644 ld/testsuite/ld-loongarch-elf/abi2_max_imm.s
>   create mode 100644 ld/testsuite/ld-loongarch-elf/abi2_overflows.s
>   create mode 100644 ld/testsuite/ld-loongarch-elf/max_imm_b16.d
>   create mode 100644 ld/testsuite/ld-loongarch-elf/max_imm_b21.d
>   create mode 100644 ld/testsuite/ld-loongarch-elf/max_imm_b26.d
>   create mode 100644 ld/testsuite/ld-loongarch-elf/max_imm_pcrel20.d
>   create mode 100644 ld/testsuite/ld-loongarch-elf/overflow_b16.d
>   create mode 100644 ld/testsuite/ld-loongarch-elf/overflow_b21.d
>   create mode 100644 ld/testsuite/ld-loongarch-elf/overflow_b26.d
>   create mode 100644 ld/testsuite/ld-loongarch-elf/overflow_pcrel20.d
>   create mode 100644 ld/testsuite/ld-loongarch-elf/overflow_s_0_10_10_16_s2.d
>   create mode 100644 ld/testsuite/ld-loongarch-elf/overflow_s_0_5_10_16_s2.d
>   create mode 100644 ld/testsuite/ld-loongarch-elf/overflow_s_10_12.d
>   create mode 100644 ld/testsuite/ld-loongarch-elf/overflow_s_10_16.d
>   create mode 100644 ld/testsuite/ld-loongarch-elf/overflow_s_10_16_s2.d
>   create mode 100644 ld/testsuite/ld-loongarch-elf/overflow_s_10_5.d
>   create mode 100644 ld/testsuite/ld-loongarch-elf/overflow_s_5_20.d
>   create mode 100644 ld/testsuite/ld-loongarch-elf/overflow_u.d
>   create mode 100644 ld/testsuite/ld-loongarch-elf/overflow_u_10_12.d
>   create mode 100644 ld/testsuite/ld-loongarch-elf/underflow_b16.d
>   create mode 100644 ld/testsuite/ld-loongarch-elf/underflow_b21.d
>   create mode 100644 ld/testsuite/ld-loongarch-elf/underflow_b26.d
>   create mode 100644 ld/testsuite/ld-loongarch-elf/underflow_pcrel20.d
>   create mode 100644 ld/testsuite/ld-loongarch-elf/underflow_s_0_10_10_16_s2.d
>   create mode 100644 ld/testsuite/ld-loongarch-elf/underflow_s_0_5_10_16_s2.d
>   create mode 100644 ld/testsuite/ld-loongarch-elf/underflow_s_10_12.d
>   create mode 100644 ld/testsuite/ld-loongarch-elf/underflow_s_10_16.d
>   create mode 100644 ld/testsuite/ld-loongarch-elf/underflow_s_10_16_s2.d
>   create mode 100644 ld/testsuite/ld-loongarch-elf/underflow_s_10_5.d
>   create mode 100644 ld/testsuite/ld-loongarch-elf/underflow_s_5_20.d
>
> diff --git a/bfd/elfxx-loongarch.c b/bfd/elfxx-loongarch.c
> index 924b11cfa..9b4fbfe36 100644
> --- a/bfd/elfxx-loongarch.c
> +++ b/bfd/elfxx-loongarch.c
> @@ -55,6 +55,8 @@ static bool
>   reloc_bits (bfd *abfd, reloc_howto_type *howto, bfd_vma *val);
>   static bool
>   reloc_sign_bits (bfd *abfd, reloc_howto_type *howto, bfd_vma *fix_val);
> +static bool
> +reloc_unsign_bits (bfd *abfd, reloc_howto_type *howto, bfd_vma *fix_val);
>   
>   static bfd_reloc_status_type
>   loongarch_elf_add_sub_reloc (bfd *, arelent *, asymbol *, void *,
> @@ -383,7 +385,7 @@ static loongarch_reloc_howto_type loongarch_howto_table[] =
>   	 0x7c00,				  /* dst_mask */
>   	 false,					  /* pcrel_offset */
>   	 BFD_RELOC_LARCH_SOP_POP_32_S_10_5,	  /* bfd_reloc_code_real_type */
> -	 reloc_bits,				  /* adjust_reloc_bits */
> +	 reloc_sign_bits,			  /* adjust_reloc_bits */
>   	 NULL),					  /* larch_reloc_type_name */
>   
>     LOONGARCH_HOWTO (R_LARCH_SOP_POP_32_U_10_12,	  /* type (39).  */
> @@ -400,7 +402,7 @@ static loongarch_reloc_howto_type loongarch_howto_table[] =
>   	 0x3ffc00,				  /* dst_mask */
>   	 false,					  /* pcrel_offset */
>   	 BFD_RELOC_LARCH_SOP_POP_32_U_10_12,	  /* bfd_reloc_code_real_type */
> -	 reloc_bits,				  /* adjust_reloc_bits */
> +	 reloc_unsign_bits,			  /* adjust_reloc_bits */
>   	 NULL),					  /* larch_reloc_type_name */
>   
>     LOONGARCH_HOWTO (R_LARCH_SOP_POP_32_S_10_12,	  /* type (40).  */
> @@ -417,7 +419,7 @@ static loongarch_reloc_howto_type loongarch_howto_table[] =
>   	 0x3ffc00,				  /* dst_mask */
>   	 false,					  /* pcrel_offset */
>   	 BFD_RELOC_LARCH_SOP_POP_32_S_10_12,	  /* bfd_reloc_code_real_type */
> -	 reloc_bits,				  /* adjust_reloc_bits */
> +	 reloc_sign_bits,			  /* adjust_reloc_bits */
>   	 NULL),					  /* larch_reloc_type_name */
>   
>     LOONGARCH_HOWTO (R_LARCH_SOP_POP_32_S_10_16,	  /* type (41).  */
> @@ -434,7 +436,7 @@ static loongarch_reloc_howto_type loongarch_howto_table[] =
>   	 0x3fffc00,				  /* dst_mask */
>   	 false,					  /* pcrel_offset */
>   	 BFD_RELOC_LARCH_SOP_POP_32_S_10_16,	  /* bfd_reloc_code_real_type */
> -	 reloc_bits,				  /* adjust_reloc_bits */
> +	 reloc_sign_bits,			  /* adjust_reloc_bits */
>   	 NULL),					  /* larch_reloc_type_name */
>   
>     LOONGARCH_HOWTO (R_LARCH_SOP_POP_32_S_10_16_S2, /* type (42).  */
> @@ -468,7 +470,7 @@ static loongarch_reloc_howto_type loongarch_howto_table[] =
>   	 0x1ffffe0,				  /* dst_mask */
>   	 false,					  /* pcrel_offset */
>   	 BFD_RELOC_LARCH_SOP_POP_32_S_5_20,	  /* bfd_reloc_code_real_type */
> -	 reloc_bits,				  /* adjust_reloc_bits */
> +	 reloc_sign_bits,			  /* adjust_reloc_bits */
>   	 NULL),					  /* larch_reloc_type_name */
>   
>     LOONGARCH_HOWTO (R_LARCH_SOP_POP_32_S_0_5_10_16_S2,
> @@ -522,7 +524,7 @@ static loongarch_reloc_howto_type loongarch_howto_table[] =
>   	 0x00000000ffffffff,			/* dst_mask */
>   	 false,					/* pcrel_offset */
>   	 BFD_RELOC_LARCH_SOP_POP_32_U,		/* bfd_reloc_code_real_type */
> -	 reloc_bits,				/* adjust_reloc_bits */
> +	 reloc_unsign_bits,			/* adjust_reloc_bits */
>   	 NULL),					/* larch_reloc_type_name */
>   
>     /* 8-bit in-place addition, for local label subtraction.  */
> @@ -1671,9 +1673,10 @@ reloc_bits (bfd *abfd ATTRIBUTE_UNUSED,
>   }
>   
>   static bool
> -reloc_sign_bits (bfd *abfd, reloc_howto_type *howto, bfd_vma *fix_val)
> +reloc_bits_sanity (bfd *abfd, reloc_howto_type *howto, bfd_vma *fix_val, bool sign)
>   {
> -  if (howto->complain_on_overflow != complain_overflow_signed)
> +  if ((sign && howto->complain_on_overflow != complain_overflow_signed)
> +      || (!sign && howto->complain_on_overflow != complain_overflow_unsigned))
>       return false;
>   
>     bfd_signed_vma val = (bfd_signed_vma)(*fix_val);
> @@ -1689,7 +1692,7 @@ reloc_sign_bits (bfd *abfd, reloc_howto_type *howto, bfd_vma *fix_val)
>       }
>   
>     bfd_signed_vma mask = ((bfd_signed_vma)0x1 << (howto->bitsize
> -			  + howto->rightshift - 1)) - 1;
> +			  + howto->rightshift - sign)) - 1;

   Is safe to compute  howto->rightshift(unsigned int) and sign(bool)?

>   
>     /* Positive number: high part is all 0;
>        Negative number: if high part is not all 0, high part must be all 1.
> @@ -1728,6 +1731,18 @@ reloc_sign_bits (bfd *abfd, reloc_howto_type *howto, bfd_vma *fix_val)
>     return true;
>   }
>   
> +static bool
> +reloc_sign_bits (bfd *abfd, reloc_howto_type *howto, bfd_vma *fix_val)
> +{
> +  return reloc_bits_sanity (abfd, howto, fix_val, true);
> +}
> +
> +static bool
> +reloc_unsign_bits (bfd *abfd, reloc_howto_type *howto, bfd_vma *fix_val)
> +{
> +  return reloc_bits_sanity (abfd, howto, fix_val, false);
> +}
> +
>   bool
>   loongarch_adjust_reloc_bitsfield (bfd *abfd, reloc_howto_type *howto,
>   				  bfd_vma *fix_val)
> diff --git a/ld/testsuite/ld-loongarch-elf/abi1_max_imm.dd b/ld/testsuite/ld-loongarch-elf/abi1_max_imm.dd
> new file mode 100644
> index 000000000..c85271408
> --- /dev/null
> +++ b/ld/testsuite/ld-loongarch-elf/abi1_max_imm.dd
> @@ -0,0 +1,18 @@
> +#...
> +.*00003c00.*
> +.*00004000.*
> +.*001ffc00.*
> +.*00200000.*
> +.*01fffc00.*
> +.*02000000.*
> +.*01fffc00.*
> +.*02000000.*
> +.*03fffc0f.*
> +.*00000010.*
> +.*03fffdff.*
> +.*00000200.*
> +.*00ffffe0.*
> +.*01000000.*
> +.*003ffc00.*
> +.*ffffffff.*
> +#pass
> diff --git a/ld/testsuite/ld-loongarch-elf/abi1_max_imm.s b/ld/testsuite/ld-loongarch-elf/abi1_max_imm.s
> new file mode 100644
> index 000000000..02507f19c
> --- /dev/null
> +++ b/ld/testsuite/ld-loongarch-elf/abi1_max_imm.s
> @@ -0,0 +1,39 @@
> +.macro sign_check reloc_name reloc_bits
> +  .reloc off, R_LARCH_SOP_PUSH_ABSOLUTE, (1 << (\reloc_bits - 1)) - 1
> +  .reloc off, \reloc_name
> +  .word 0
> +  .set off, off + 4
> +  .reloc off, R_LARCH_SOP_PUSH_ABSOLUTE, -(1 << (\reloc_bits - 1))
> +  .reloc off, \reloc_name
> +  .word 0
> +  .set off, off + 4
> +.endm
> +
> +.macro sign_check_s2 reloc_name reloc_bits
> +  .reloc off, R_LARCH_SOP_PUSH_ABSOLUTE, ((1 << (\reloc_bits - 1)) - 1) << 2
> +  .reloc off, \reloc_name
> +  .word 0
> +  .set off, off + 4
> +  .reloc off, R_LARCH_SOP_PUSH_ABSOLUTE, (-(1 << (\reloc_bits - 1))) << 2
> +  .reloc off, \reloc_name
> +  .word 0
> +  .set off, off + 4
> +.endm
> +
> +.macro unsign_check reloc_name reloc_bits
> +  .reloc off, R_LARCH_SOP_PUSH_ABSOLUTE, (1 << \reloc_bits) - 1
> +  .reloc off, \reloc_name
> +  .word 0
> +  .set off, off + 4
> +.endm
> +
> +.set off, 0
> +sign_check R_LARCH_SOP_POP_32_S_10_5 5
> +sign_check R_LARCH_SOP_POP_32_S_10_12 12
> +sign_check R_LARCH_SOP_POP_32_S_10_16 16
> +sign_check_s2 R_LARCH_SOP_POP_32_S_10_16_S2 16
> +sign_check_s2 R_LARCH_SOP_POP_32_S_0_5_10_16_S2 21
> +sign_check_s2 R_LARCH_SOP_POP_32_S_0_10_10_16_S2 26
> +sign_check R_LARCH_SOP_POP_32_S_5_20 20
> +unsign_check R_LARCH_SOP_POP_32_U_10_12 12
> +unsign_check R_LARCH_SOP_POP_32_U 32
> diff --git a/ld/testsuite/ld-loongarch-elf/abi1_sops.s b/ld/testsuite/ld-loongarch-elf/abi1_sops.s
> new file mode 100644
> index 000000000..34f2d5920
> --- /dev/null
> +++ b/ld/testsuite/ld-loongarch-elf/abi1_sops.s
> @@ -0,0 +1,43 @@
> +.macro sign_check reloc_sym reloc_name reloc_bits
> +  .ifdef \reloc_sym
> +    .ifdef overflow
> +      .reloc 0, R_LARCH_SOP_PUSH_ABSOLUTE, 1 << (\reloc_bits - 1)
> +      .reloc 0, \reloc_name
> +    .else
> +      .reloc 0, R_LARCH_SOP_PUSH_ABSOLUTE, -(1 << (\reloc_bits - 1)) - 1
> +      .reloc 0, \reloc_name
> +    .endif
> +    .word 0
> +  .endif
> +.endm
> +
> +.macro sign_check_s2 reloc_sym reloc_name reloc_bits
> +  .ifdef \reloc_sym
> +    .ifdef overflow
> +      .reloc 0, R_LARCH_SOP_PUSH_ABSOLUTE, (1 << (\reloc_bits - 1)) << 2
> +      .reloc 0, \reloc_name
> +    .else
> +      .reloc 0, R_LARCH_SOP_PUSH_ABSOLUTE, (-(1 << (\reloc_bits - 1)) - 1) << 2
> +      .reloc 0, \reloc_name
> +    .endif
> +    .word 0
> +  .endif
> +.endm
> +
> +.macro unsign_check reloc_sym reloc_name reloc_bits
> +  .ifdef \reloc_sym
> +    .reloc 0, R_LARCH_SOP_PUSH_ABSOLUTE, 1 << \reloc_bits
> +    .reloc 0, \reloc_name
> +    .word 0
> +  .endif
> +.endm
> +
> +sign_check	s_10_5		R_LARCH_SOP_POP_32_S_10_5 5
> +sign_check	s_10_12		R_LARCH_SOP_POP_32_S_10_12 12
> +sign_check	s_10_16		R_LARCH_SOP_POP_32_S_10_16 16
> +sign_check_s2	s_10_16_s2	R_LARCH_SOP_POP_32_S_10_16_S2 16
> +sign_check_s2	s_0_5_10_16_s2	R_LARCH_SOP_POP_32_S_0_5_10_16_S2 21
> +sign_check_s2	s_0_10_10_16_s2	R_LARCH_SOP_POP_32_S_0_10_10_16_S2 26
> +sign_check	s_5_20		R_LARCH_SOP_POP_32_S_5_20 20
> +unsign_check	u_10_12		R_LARCH_SOP_POP_32_U_10_12 12
> +unsign_check	u		R_LARCH_SOP_POP_32_U 32
> diff --git a/ld/testsuite/ld-loongarch-elf/abi2_max_imm.s b/ld/testsuite/ld-loongarch-elf/abi2_max_imm.s
> new file mode 100644
> index 000000000..f1bc3868f
> --- /dev/null
> +++ b/ld/testsuite/ld-loongarch-elf/abi2_max_imm.s
> @@ -0,0 +1,24 @@
> +  .text
> +.ifdef b16
> +  beq $a0, $a1, low
> +  beq $a0, $a1, high
> +.endif
> +.ifdef b21
> +  beqz $a0, low
> +  beqz $a0, high
> +.endif
> +.ifdef b26
> +  b low
> +  b high
> +.endif
> +.ifdef pcrel20
> +  .reloc 0, R_LARCH_PCREL20_S2, low
> +  pcaddi $a0, 0
> +  .reloc 4, R_LARCH_PCREL20_S2, high
> +  pcaddi $a0, 0
> +.endif
> +
> +  .section ".low"
> +low: nop
> +  .section ".high"
> +high: nop
> diff --git a/ld/testsuite/ld-loongarch-elf/abi2_overflows.s b/ld/testsuite/ld-loongarch-elf/abi2_overflows.s
> new file mode 100644
> index 000000000..1c25bd8c5
> --- /dev/null
> +++ b/ld/testsuite/ld-loongarch-elf/abi2_overflows.s
> @@ -0,0 +1,24 @@
> +  .text
> +
> +.set idx, 0
> +.ifdef underflow
> +  nop
> +  .set idx, 4
> +.endif
> +
> +.ifdef b16
> +  beq $a0, $a1, local_sym
> +.endif
> +.ifdef b21
> +  beqz $a0, local_sym
> +.endif
> +.ifdef b26
> +  b local_sym
> +.endif
> +.ifdef pcrel20
> +  .reloc idx, R_LARCH_PCREL20_S2, local_sym
> +  pcaddi $a0, 0
> +.endif
> +
> +  .section ".local_sym"
> +local_sym: nop
> diff --git a/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp b/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp
> index b95cc53e5..ae90daec2 100644
> --- a/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp
> +++ b/ld/testsuite/ld-loongarch-elf/ld-loongarch-elf.exp
> @@ -32,6 +32,22 @@ if [istarget "loongarch64-*-*"] {
>       run_dump_test "syscall"
>       run_dump_test "disas-jirl"
>       run_dump_test "local-ifunc-reloc"
> +    run_dump_test "overflow_s_10_5"
> +    run_dump_test "overflow_s_10_12"
> +    run_dump_test "overflow_s_10_16"
> +    run_dump_test "overflow_s_10_16_s2"
> +    run_dump_test "overflow_s_0_5_10_16_s2"
> +    run_dump_test "overflow_s_0_10_10_16_s2"
> +    run_dump_test "overflow_s_5_20"
> +    run_dump_test "overflow_u_10_12"
> +    run_dump_test "overflow_u"
> +    run_dump_test "underflow_s_10_5"
> +    run_dump_test "underflow_s_10_12"
> +    run_dump_test "underflow_s_10_16"
> +    run_dump_test "underflow_s_10_16_s2"
> +    run_dump_test "underflow_s_0_5_10_16_s2"
> +    run_dump_test "underflow_s_0_10_10_16_s2"
> +    run_dump_test "underflow_s_5_20"
>   }
>   
>   if [istarget "loongarch32-*-*"] {
> @@ -54,5 +70,28 @@ if [istarget "loongarch64-*-*"] {
>   	      ] \
>   	      "64_pcrel" \
>   	  ] \
> +	  [list \
> +	      "abi1_max_imm" \
> +	      "-e 0x0" "" \
> +	      "" \
> +	      {abi1_max_imm.s} \
> +	      [list \
> +		  [list objdump -D abi1_max_imm.dd] \
> +	      ] \
> +	      "abi1_max_imm" \
> +	  ] \
>         ]
>   }
> +
> +run_dump_test "max_imm_b16"
> +run_dump_test "max_imm_b21"
> +run_dump_test "max_imm_b26"
> +run_dump_test "max_imm_pcrel20"
> +run_dump_test "overflow_b16"
> +run_dump_test "overflow_b21"
> +run_dump_test "overflow_b26"
> +run_dump_test "overflow_pcrel20"
> +run_dump_test "underflow_b16"
> +run_dump_test "underflow_b21"
> +run_dump_test "underflow_b26"
> +run_dump_test "underflow_pcrel20"
> diff --git a/ld/testsuite/ld-loongarch-elf/max_imm_b16.d b/ld/testsuite/ld-loongarch-elf/max_imm_b16.d
> new file mode 100644
> index 000000000..2c3f6cdbd
> --- /dev/null
> +++ b/ld/testsuite/ld-loongarch-elf/max_imm_b16.d
> @@ -0,0 +1,9 @@
> +#source: abi2_max_imm.s
> +#as: --defsym b16=1
> +#ld: -e 0 -Ttext 0x20000000 --section-start=.low=0x1ffe0000 --section-start=.high=0x20020000
> +#objdump: -s
> +
> +#...
> +Contents of section .text:
> + 20000000 8500005a 85fcff59.*
> +#pass
> diff --git a/ld/testsuite/ld-loongarch-elf/max_imm_b21.d b/ld/testsuite/ld-loongarch-elf/max_imm_b21.d
> new file mode 100644
> index 000000000..5861fb464
> --- /dev/null
> +++ b/ld/testsuite/ld-loongarch-elf/max_imm_b21.d
> @@ -0,0 +1,9 @@
> +#source: abi2_max_imm.s
> +#as: --defsym b21=1
> +#ld: -e 0 -Ttext 0x20000000 --section-start=.low=0x1fc00000 --section-start=.high=0x20400000
> +#objdump: -s
> +
> +#...
> +Contents of section .text:
> + 20000000 90000040 8ffcff43.*
> +#pass
> diff --git a/ld/testsuite/ld-loongarch-elf/max_imm_b26.d b/ld/testsuite/ld-loongarch-elf/max_imm_b26.d
> new file mode 100644
> index 000000000..f475ccb32
> --- /dev/null
> +++ b/ld/testsuite/ld-loongarch-elf/max_imm_b26.d
> @@ -0,0 +1,9 @@
> +#source: abi2_max_imm.s
> +#as: --defsym b26=1
> +#ld: -e 0 -Ttext 0x20000000 --section-start=.low=0x18000000 --section-start=.high=0x28000000
> +#objdump: -s
> +
> +#...
> +Contents of section .text:
> + 20000000 00020050 fffdff53.*
> +#pass
> diff --git a/ld/testsuite/ld-loongarch-elf/max_imm_pcrel20.d b/ld/testsuite/ld-loongarch-elf/max_imm_pcrel20.d
> new file mode 100644
> index 000000000..4bd9d2f11
> --- /dev/null
> +++ b/ld/testsuite/ld-loongarch-elf/max_imm_pcrel20.d
> @@ -0,0 +1,9 @@
> +#source: abi2_max_imm.s
> +#as: --defsym pcrel20=1
> +#ld: -e 0 -Ttext 0x20000000 --section-start=.low=0x1fe00000 --section-start=.high=0x20200000
> +#objdump: -s
> +
> +#...
> +Contents of section .text:
> + 20000000 04000019 e4ffff18.*
> +#pass
> diff --git a/ld/testsuite/ld-loongarch-elf/overflow_b16.d b/ld/testsuite/ld-loongarch-elf/overflow_b16.d
> new file mode 100644
> index 000000000..26cad414b
> --- /dev/null
> +++ b/ld/testsuite/ld-loongarch-elf/overflow_b16.d
> @@ -0,0 +1,4 @@
> +#source: abi2_overflows.s
> +#as: --defsym overflow=1 --defsym b16=1
> +#ld: -e 0 --section-start=.local_sym=0x20020000 -Ttext 0x20000000
> +#error: .*relocation truncated to fit: R_LARCH_B16 against `local_sym'
> diff --git a/ld/testsuite/ld-loongarch-elf/overflow_b21.d b/ld/testsuite/ld-loongarch-elf/overflow_b21.d
> new file mode 100644
> index 000000000..826856f20
> --- /dev/null
> +++ b/ld/testsuite/ld-loongarch-elf/overflow_b21.d
> @@ -0,0 +1,4 @@
> +#source: abi2_overflows.s
> +#as: --defsym overflow=1 --defsym b21=1
> +#ld: -e 0 --section-start=.local_sym=0x20400000 -Ttext 0x20000000
> +#error: .*relocation truncated to fit: R_LARCH_B21 against `local_sym'
> diff --git a/ld/testsuite/ld-loongarch-elf/overflow_b26.d b/ld/testsuite/ld-loongarch-elf/overflow_b26.d
> new file mode 100644
> index 000000000..474e57510
> --- /dev/null
> +++ b/ld/testsuite/ld-loongarch-elf/overflow_b26.d
> @@ -0,0 +1,4 @@
> +#source: abi2_overflows.s
> +#as: --defsym overflow=1 --defsym b26=1
> +#ld: -e 0 --section-start=.local_sym=0x28000000 -Ttext 0x20000000
> +#error: .*relocation truncated to fit: R_LARCH_B26 against `local_sym'
> diff --git a/ld/testsuite/ld-loongarch-elf/overflow_pcrel20.d b/ld/testsuite/ld-loongarch-elf/overflow_pcrel20.d
> new file mode 100644
> index 000000000..420be7290
> --- /dev/null
> +++ b/ld/testsuite/ld-loongarch-elf/overflow_pcrel20.d
> @@ -0,0 +1,4 @@
> +#source: abi2_overflows.s
> +#as: --defsym overflow=1 --defsym pcrel20=1
> +#ld: -e 0 --section-start=.local_sym=0x20200000 -Ttext 0x20000000
> +#error: .*relocation truncated to fit: R_LARCH_PCREL20_S2 against `local_sym'
> diff --git a/ld/testsuite/ld-loongarch-elf/overflow_s_0_10_10_16_s2.d b/ld/testsuite/ld-loongarch-elf/overflow_s_0_10_10_16_s2.d
> new file mode 100644
> index 000000000..621054b1d
> --- /dev/null
> +++ b/ld/testsuite/ld-loongarch-elf/overflow_s_0_10_10_16_s2.d
> @@ -0,0 +1,4 @@
> +#source: abi1_sops.s
> +#as: --defsym overflow=1 --defsym s_0_10_10_16_s2=1
> +#ld: -e 0
> +#error: .*relocation truncated to fit: R_LARCH_SOP_POP_32_S_0_10_10_16_S2 against `<nameless>'
> \ No newline at end of file
> diff --git a/ld/testsuite/ld-loongarch-elf/overflow_s_0_5_10_16_s2.d b/ld/testsuite/ld-loongarch-elf/overflow_s_0_5_10_16_s2.d
> new file mode 100644
> index 000000000..348f5ae93
> --- /dev/null
> +++ b/ld/testsuite/ld-loongarch-elf/overflow_s_0_5_10_16_s2.d
> @@ -0,0 +1,4 @@
> +#source: abi1_sops.s
> +#as: --defsym overflow=1 --defsym s_0_5_10_16_s2=1
> +#ld: -e 0
> +#error: .*relocation truncated to fit: R_LARCH_SOP_POP_32_S_0_5_10_16_S2 against `<nameless>'
> \ No newline at end of file
> diff --git a/ld/testsuite/ld-loongarch-elf/overflow_s_10_12.d b/ld/testsuite/ld-loongarch-elf/overflow_s_10_12.d
> new file mode 100644
> index 000000000..dcadff7e0
> --- /dev/null
> +++ b/ld/testsuite/ld-loongarch-elf/overflow_s_10_12.d
> @@ -0,0 +1,4 @@
> +#source: abi1_sops.s
> +#as: --defsym overflow=1 --defsym s_10_12=1
> +#ld: -e 0
> +#error: .*relocation truncated to fit: R_LARCH_SOP_POP_32_S_10_12 against `<nameless>'
> \ No newline at end of file
> diff --git a/ld/testsuite/ld-loongarch-elf/overflow_s_10_16.d b/ld/testsuite/ld-loongarch-elf/overflow_s_10_16.d
> new file mode 100644
> index 000000000..3677415af
> --- /dev/null
> +++ b/ld/testsuite/ld-loongarch-elf/overflow_s_10_16.d
> @@ -0,0 +1,4 @@
> +#source: abi1_sops.s
> +#as: --defsym overflow=1 --defsym s_10_16=1
> +#ld: -e 0
> +#error: .*relocation truncated to fit: R_LARCH_SOP_POP_32_S_10_16 against `<nameless>'
> \ No newline at end of file
> diff --git a/ld/testsuite/ld-loongarch-elf/overflow_s_10_16_s2.d b/ld/testsuite/ld-loongarch-elf/overflow_s_10_16_s2.d
> new file mode 100644
> index 000000000..24ffa811a
> --- /dev/null
> +++ b/ld/testsuite/ld-loongarch-elf/overflow_s_10_16_s2.d
> @@ -0,0 +1,4 @@
> +#source: abi1_sops.s
> +#as: --defsym overflow=1 --defsym s_10_16_s2=1
> +#ld: -e 0
> +#error: .*relocation truncated to fit: R_LARCH_SOP_POP_32_S_10_16_S2 against `<nameless>'
> \ No newline at end of file
> diff --git a/ld/testsuite/ld-loongarch-elf/overflow_s_10_5.d b/ld/testsuite/ld-loongarch-elf/overflow_s_10_5.d
> new file mode 100644
> index 000000000..7881457d0
> --- /dev/null
> +++ b/ld/testsuite/ld-loongarch-elf/overflow_s_10_5.d
> @@ -0,0 +1,4 @@
> +#source: abi1_sops.s
> +#as: --defsym overflow=1 --defsym s_10_5=1
> +#ld: -e 0
> +#error: .*relocation truncated to fit: R_LARCH_SOP_POP_32_S_10_5 against `<nameless>'
> \ No newline at end of file
> diff --git a/ld/testsuite/ld-loongarch-elf/overflow_s_5_20.d b/ld/testsuite/ld-loongarch-elf/overflow_s_5_20.d
> new file mode 100644
> index 000000000..5f06eed8c
> --- /dev/null
> +++ b/ld/testsuite/ld-loongarch-elf/overflow_s_5_20.d
> @@ -0,0 +1,4 @@
> +#source: abi1_sops.s
> +#as: --defsym overflow=1 --defsym s_5_20=1
> +#ld: -e 0
> +#error: .*relocation truncated to fit: R_LARCH_SOP_POP_32_S_5_20 against `<nameless>'
> \ No newline at end of file
> diff --git a/ld/testsuite/ld-loongarch-elf/overflow_u.d b/ld/testsuite/ld-loongarch-elf/overflow_u.d
> new file mode 100644
> index 000000000..96906f643
> --- /dev/null
> +++ b/ld/testsuite/ld-loongarch-elf/overflow_u.d
> @@ -0,0 +1,4 @@
> +#source: abi1_sops.s
> +#as: --defsym overflow=1 --defsym u=1
> +#ld: -e 0
> +#error: .*relocation truncated to fit: R_LARCH_SOP_POP_32_U against `<nameless>'
> \ No newline at end of file
> diff --git a/ld/testsuite/ld-loongarch-elf/overflow_u_10_12.d b/ld/testsuite/ld-loongarch-elf/overflow_u_10_12.d
> new file mode 100644
> index 000000000..9d8e09056
> --- /dev/null
> +++ b/ld/testsuite/ld-loongarch-elf/overflow_u_10_12.d
> @@ -0,0 +1,4 @@
> +#source: abi1_sops.s
> +#as: --defsym overflow=1 --defsym u_10_12=1
> +#ld: -e 0
> +#error: .*relocation truncated to fit: R_LARCH_SOP_POP_32_U_10_12 against `<nameless>'
> \ No newline at end of file
> diff --git a/ld/testsuite/ld-loongarch-elf/underflow_b16.d b/ld/testsuite/ld-loongarch-elf/underflow_b16.d
> new file mode 100644
> index 000000000..e2e21949b
> --- /dev/null
> +++ b/ld/testsuite/ld-loongarch-elf/underflow_b16.d
> @@ -0,0 +1,4 @@
> +#source: abi2_overflows.s
> +#as: --defsym underflow=1 --defsym b16=1
> +#ld: -e 0 --section-start=.local_sym=0x1ffe0000 -Ttext 0x20000000
> +#error: .*relocation truncated to fit: R_LARCH_B16 against `local_sym'
> diff --git a/ld/testsuite/ld-loongarch-elf/underflow_b21.d b/ld/testsuite/ld-loongarch-elf/underflow_b21.d
> new file mode 100644
> index 000000000..6f56526b5
> --- /dev/null
> +++ b/ld/testsuite/ld-loongarch-elf/underflow_b21.d
> @@ -0,0 +1,4 @@
> +#source: abi2_overflows.s
> +#as: --defsym underflow=1 --defsym b21=1
> +#ld: -e 0 --section-start=.local_sym=0x1fc00000 -Ttext 0x20000000
> +#error: .*relocation truncated to fit: R_LARCH_B21 against `local_sym'
> diff --git a/ld/testsuite/ld-loongarch-elf/underflow_b26.d b/ld/testsuite/ld-loongarch-elf/underflow_b26.d
> new file mode 100644
> index 000000000..5756cc14c
> --- /dev/null
> +++ b/ld/testsuite/ld-loongarch-elf/underflow_b26.d
> @@ -0,0 +1,4 @@
> +#source: abi2_overflows.s
> +#as: --defsym underflow=1 --defsym b26=1
> +#ld: -e 0 --section-start=.local_sym=0x18000000 -Ttext 0x20000000
> +#error: .*relocation truncated to fit: R_LARCH_B26 against `local_sym'
> diff --git a/ld/testsuite/ld-loongarch-elf/underflow_pcrel20.d b/ld/testsuite/ld-loongarch-elf/underflow_pcrel20.d
> new file mode 100644
> index 000000000..e65c60ad7
> --- /dev/null
> +++ b/ld/testsuite/ld-loongarch-elf/underflow_pcrel20.d
> @@ -0,0 +1,4 @@
> +#source: abi2_overflows.s
> +#as: --defsym underflow=1 --defsym pcrel20=1
> +#ld: -e 0 --section-start=.local_sym=0x1fe00000 -Ttext 0x20000000
> +#error: .*relocation truncated to fit: R_LARCH_PCREL20_S2 against `local_sym'
> diff --git a/ld/testsuite/ld-loongarch-elf/underflow_s_0_10_10_16_s2.d b/ld/testsuite/ld-loongarch-elf/underflow_s_0_10_10_16_s2.d
> new file mode 100644
> index 000000000..3e7715a27
> --- /dev/null
> +++ b/ld/testsuite/ld-loongarch-elf/underflow_s_0_10_10_16_s2.d
> @@ -0,0 +1,4 @@
> +#source: abi1_sops.s
> +#as: --defsym underflow=1 --defsym s_0_10_10_16_s2=1
> +#ld: -e 0
> +#error: .*relocation truncated to fit: R_LARCH_SOP_POP_32_S_0_10_10_16_S2 against `<nameless>'
> \ No newline at end of file
> diff --git a/ld/testsuite/ld-loongarch-elf/underflow_s_0_5_10_16_s2.d b/ld/testsuite/ld-loongarch-elf/underflow_s_0_5_10_16_s2.d
> new file mode 100644
> index 000000000..14be5a9c6
> --- /dev/null
> +++ b/ld/testsuite/ld-loongarch-elf/underflow_s_0_5_10_16_s2.d
> @@ -0,0 +1,4 @@
> +#source: abi1_sops.s
> +#as: --defsym underflow=1 --defsym s_0_5_10_16_s2=1
> +#ld: -e 0
> +#error: .*relocation truncated to fit: R_LARCH_SOP_POP_32_S_0_5_10_16_S2 against `<nameless>'
> \ No newline at end of file
> diff --git a/ld/testsuite/ld-loongarch-elf/underflow_s_10_12.d b/ld/testsuite/ld-loongarch-elf/underflow_s_10_12.d
> new file mode 100644
> index 000000000..ed9fe9b70
> --- /dev/null
> +++ b/ld/testsuite/ld-loongarch-elf/underflow_s_10_12.d
> @@ -0,0 +1,4 @@
> +#source: abi1_sops.s
> +#as: --defsym underflow=1 --defsym s_10_12=1
> +#ld: -e 0
> +#error: .*relocation truncated to fit: R_LARCH_SOP_POP_32_S_10_12 against `<nameless>'
> \ No newline at end of file
> diff --git a/ld/testsuite/ld-loongarch-elf/underflow_s_10_16.d b/ld/testsuite/ld-loongarch-elf/underflow_s_10_16.d
> new file mode 100644
> index 000000000..03b851b57
> --- /dev/null
> +++ b/ld/testsuite/ld-loongarch-elf/underflow_s_10_16.d
> @@ -0,0 +1,4 @@
> +#source: abi1_sops.s
> +#as: --defsym underflow=1 --defsym s_10_16=1
> +#ld: -e 0
> +#error: .*relocation truncated to fit: R_LARCH_SOP_POP_32_S_10_16 against `<nameless>'
> \ No newline at end of file
> diff --git a/ld/testsuite/ld-loongarch-elf/underflow_s_10_16_s2.d b/ld/testsuite/ld-loongarch-elf/underflow_s_10_16_s2.d
> new file mode 100644
> index 000000000..c0e4ca039
> --- /dev/null
> +++ b/ld/testsuite/ld-loongarch-elf/underflow_s_10_16_s2.d
> @@ -0,0 +1,4 @@
> +#source: abi1_sops.s
> +#as: --defsym underflow=1 --defsym s_10_16_s2=1
> +#ld: -e 0
> +#error: .*relocation truncated to fit: R_LARCH_SOP_POP_32_S_10_16_S2 against `<nameless>'
> \ No newline at end of file
> diff --git a/ld/testsuite/ld-loongarch-elf/underflow_s_10_5.d b/ld/testsuite/ld-loongarch-elf/underflow_s_10_5.d
> new file mode 100644
> index 000000000..77d0eb04e
> --- /dev/null
> +++ b/ld/testsuite/ld-loongarch-elf/underflow_s_10_5.d
> @@ -0,0 +1,4 @@
> +#source: abi1_sops.s
> +#as: --defsym underflow=1 --defsym s_10_5=1
> +#ld: -e 0
> +#error: .*relocation truncated to fit: R_LARCH_SOP_POP_32_S_10_5 against `<nameless>'
> \ No newline at end of file
> diff --git a/ld/testsuite/ld-loongarch-elf/underflow_s_5_20.d b/ld/testsuite/ld-loongarch-elf/underflow_s_5_20.d
> new file mode 100644
> index 000000000..c458bb815
> --- /dev/null
> +++ b/ld/testsuite/ld-loongarch-elf/underflow_s_5_20.d
> @@ -0,0 +1,4 @@
> +#source: abi1_sops.s
> +#as: --defsym underflow=1 --defsym s_5_20=1
> +#ld: -e 0
> +#error: .*relocation truncated to fit: R_LARCH_SOP_POP_32_S_5_20 against `<nameless>'
> \ No newline at end of file



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2 2/2] LoongArch: ld: Fix other pop relocs overflow check and add tests
  2023-09-04  1:26   ` liuzhensong
@ 2023-09-04  2:24     ` Jinyang He
  0 siblings, 0 replies; 4+ messages in thread
From: Jinyang He @ 2023-09-04  2:24 UTC (permalink / raw)
  To: liuzhensong, Chenghua Xu, mengqinggang
  Cc: WANG Xuerui, Xi Ruoyao, binutils, Xing Li

On 2023-09-04 09:26, liuzhensong wrote:

[...]
>>       bfd_signed_vma mask = ((bfd_signed_vma)0x1 << (howto->bitsize
>> -              + howto->rightshift - 1)) - 1;
>> +              + howto->rightshift - sign)) - 1;
>
>   Is safe to compute  howto->rightshift(unsigned int) and sign(bool)?

That looks confused when I see from stdbool.h that the definition
of bool value is always 0 or 1. Just to be safe, I'll pass "sign" in an
unsigned int type, in the next version.

[...]


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-09-04  2:24 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-09-02  6:50 [PATCH v2 1/2] LoongArch: bfd: Correct the name of R_LARCH_SOP_POP_32_U in howto_table Jinyang He
2023-09-02  6:50 ` [PATCH v2 2/2] LoongArch: ld: Fix other pop relocs overflow check and add tests Jinyang He
2023-09-04  1:26   ` liuzhensong
2023-09-04  2:24     ` Jinyang He

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).