[PATCH v4] MIPS/Gas: Disallow branch to absolute address for PIC

YunQiang Su syq@gcc.gnu.org
Tue Feb 6 17:05:38 GMT 2024


The asm code like
	b	(0)
will generate binary like:

00000000 <.text>:
	0: 1000ffff b 0x0
           0: R_MIPS_PC16 *ABS*
	4: 00000000 nop
If this object is linked into an PIC/PIC dynamic executable,
this branch instruction will jump to a wrong address, with low
16bit unset.
Maybe we need a new dynamic relocation, which is not even defined
by MIPS psABI, and I don't think that it is worth at all.

See PR31343.
---
 gas/config/tc-mips.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/gas/config/tc-mips.c b/gas/config/tc-mips.c
index 43c12de2c8a..f3f5c1f469d 100644
--- a/gas/config/tc-mips.c
+++ b/gas/config/tc-mips.c
@@ -7560,6 +7560,9 @@ append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
 		ip->insn_opcode |= ((address_expr->X_add_number >> shift)
 				    & 0xffff);
 	      }
+	    if (mips_pic != NO_PIC && address_expr->X_op == O_constant)
+	      as_bad (_("PIC code branch to absolute address (0x%lx)"),
+		      (unsigned long) address_expr->X_add_number);
 	  }
 	  break;
 
@@ -7577,6 +7580,9 @@ append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
 		      (unsigned long) address_expr->X_add_number);
 	    ip->insn_opcode |= ((address_expr->X_add_number >> shift)
 				& 0x1fffff);
+	    if (mips_pic != NO_PIC && address_expr->X_op == O_constant)
+	      as_bad (_("PIC code branch to absolute address (0x%lx)"),
+		      (unsigned long) address_expr->X_add_number);
 	  }
 	  break;
 
@@ -7594,6 +7600,9 @@ append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
 		      (unsigned long) address_expr->X_add_number);
 	    ip->insn_opcode |= ((address_expr->X_add_number >> shift)
 				& 0x3ffffff);
+	    if (mips_pic != NO_PIC && address_expr->X_op == O_constant)
+	      as_bad (_("PIC code branch to absolute address (0x%lx)"),
+		      (unsigned long) address_expr->X_add_number);
 	  }
 	  break;
 
-- 
2.39.2



More information about the Binutils mailing list