public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: YunQiang Su <syq@gcc.gnu.org>
To: nickc@redhat.com
Cc: binutils@sourceware.org, macro@orcam.me.uk, xry111@xry111.site
Subject: [PATCH v6] MIPS: Reject branch absolute relocs for PIC for linking
Date: Wed, 21 Feb 2024 22:52:56 +0800	[thread overview]
Message-ID: <20240221145256.3118097-1-syq@gcc.gnu.org> (raw)

The asm code like:
	b	8
will emit absolute relocs like:
	R_MIPS_PC16	*ABS*

If they are included into PIC shared objects or PIE executables,
the branch target will be like 0x12340000, which will make the
programs crash.
---
 bfd/elfxx-mips.c                                | 9 +++++++++
 ld/testsuite/ld-mips-elf/mips-elf.exp           | 1 +
 ld/testsuite/ld-mips-elf/pic-reject-abs-reloc.d | 5 +++++
 ld/testsuite/ld-mips-elf/pic-reject-abs-reloc.s | 2 ++
 4 files changed, 17 insertions(+)
 create mode 100644 ld/testsuite/ld-mips-elf/pic-reject-abs-reloc.d
 create mode 100644 ld/testsuite/ld-mips-elf/pic-reject-abs-reloc.s

diff --git a/bfd/elfxx-mips.c b/bfd/elfxx-mips.c
index 69dd71419ff..9542250dec4 100644
--- a/bfd/elfxx-mips.c
+++ b/bfd/elfxx-mips.c
@@ -9258,6 +9258,15 @@ _bfd_mips_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
 		   (h) ? h->root.root.string : "a local symbol");
 	      break;
 	    default:
+	      if (branch_reloc_p (r_type) && r_symndx == STN_UNDEF)
+		{
+		  howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, NEWABI_P (abfd));
+		  info->callbacks->einfo
+		    /* xgettext:c-format */
+		    (_("%X%H: relocation %s against `*ABS*' cannot be used"
+		       " when making a PIC/PIE object\n"),
+		     abfd, sec, rel->r_offset, howto->name);
+		}
 	      break;
 	    }
 	}
diff --git a/ld/testsuite/ld-mips-elf/mips-elf.exp b/ld/testsuite/ld-mips-elf/mips-elf.exp
index 50af78d1430..a8e1b91b3a1 100644
--- a/ld/testsuite/ld-mips-elf/mips-elf.exp
+++ b/ld/testsuite/ld-mips-elf/mips-elf.exp
@@ -1678,6 +1678,7 @@ run_dump_test_o32 "pic-reloc-6"
 run_dump_test_n64 "pic-reloc-7"
 run_dump_test_n64 "pic-reloc-7" [list [list name (microMIPS)] \
 				      [list as "-mmicromips"]]
+run_dump_test "pic-reject-abs-reloc"
 
 run_dump_test_o32 "reloc-pcrel-r6"
 
diff --git a/ld/testsuite/ld-mips-elf/pic-reject-abs-reloc.d b/ld/testsuite/ld-mips-elf/pic-reject-abs-reloc.d
new file mode 100644
index 00000000000..16d9f4e8553
--- /dev/null
+++ b/ld/testsuite/ld-mips-elf/pic-reject-abs-reloc.d
@@ -0,0 +1,5 @@
+#name: MIPS PIC rejects branch absolute
+#ld: -shared -T pic-reloc-absolute-lo.ld
+#target: [check_shared_lib_support]
+#error: \A[^\n]*: in function `foo':\n
+#error:   \(\.text\+0x0\): relocation R_MIPS_PC16 against `\*ABS\*' cannot be used when making a PIC/PIE object
diff --git a/ld/testsuite/ld-mips-elf/pic-reject-abs-reloc.s b/ld/testsuite/ld-mips-elf/pic-reject-abs-reloc.s
new file mode 100644
index 00000000000..a845fd50a77
--- /dev/null
+++ b/ld/testsuite/ld-mips-elf/pic-reject-abs-reloc.s
@@ -0,0 +1,2 @@
+foo:
+	b	8
-- 
2.39.2


             reply	other threads:[~2024-02-21 14:53 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-21 14:52 YunQiang Su [this message]
2024-02-22  3:13 ` Hans-Peter Nilsson
2024-02-22  6:29   ` YunQiang Su
2024-02-22 13:13     ` Maciej W. Rozycki
2024-02-25 14:08       ` YunQiang Su
2024-03-01 17:23         ` Maciej W. Rozycki

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240221145256.3118097-1-syq@gcc.gnu.org \
    --to=syq@gcc.gnu.org \
    --cc=binutils@sourceware.org \
    --cc=macro@orcam.me.uk \
    --cc=nickc@redhat.com \
    --cc=xry111@xry111.site \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).