public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: Faraz Shahbazker <fshahbazker@wavecomp.com>
To: "binutils@sourceware.org" <binutils@sourceware.org>
Cc: Faraz Shahbazker <fshahbazker@wavecomp.com>,
	"paul.hua.gm@gmail.com"	<paul.hua.gm@gmail.com>
Subject: [PATCH 1/2] MIPS/LD: Fix memory fault linking non-PIC object in to shared library
Date: Sat, 18 May 2019 08:19:00 -0000	[thread overview]
Message-ID: <20190518081905.223364-2-fshahbazker@wavecomp.com> (raw)
In-Reply-To: <20190518081905.223364-1-fshahbazker@wavecomp.com>

bfd/
	* elfxx-mips.c (_bfd_mips_elf_check_relocs): Add NULL pointer
	checks.  Search the RELA table for n64 relocations.

ld/
	* testsuite/ld-mips-elf/pic-reloc-5.d: New test.
	* testsuite/ld-mips-elf/pic-reloc-6.d: New test.
	* testsuite/ld-mips-elf/pic-reloc-7.d: New test.
	* testsuite/ld-mips-elf/pic-reloc-5.s: New test source.
	* testsuite/ld-mips-elf/pic-reloc-6.s: New test source.
	* testsuite/ld-mips-elf/pic-reloc-7.s: New test source.
	* testsuite/ld-mips-elf/mips-elf.exp: Run the new tests.
---
 bfd/elfxx-mips.c                       | 19 +++++++++++--------
 ld/testsuite/ld-mips-elf/mips-elf.exp  |  7 +++++++
 ld/testsuite/ld-mips-elf/pic-reloc-5.d |  6 ++++++
 ld/testsuite/ld-mips-elf/pic-reloc-5.s | 10 ++++++++++
 ld/testsuite/ld-mips-elf/pic-reloc-6.d |  7 +++++++
 ld/testsuite/ld-mips-elf/pic-reloc-6.s | 10 ++++++++++
 ld/testsuite/ld-mips-elf/pic-reloc-7.d |  8 ++++++++
 ld/testsuite/ld-mips-elf/pic-reloc-7.s | 10 ++++++++++
 8 files changed, 69 insertions(+), 8 deletions(-)
 create mode 100644 ld/testsuite/ld-mips-elf/pic-reloc-5.d
 create mode 100644 ld/testsuite/ld-mips-elf/pic-reloc-5.s
 create mode 100644 ld/testsuite/ld-mips-elf/pic-reloc-6.d
 create mode 100644 ld/testsuite/ld-mips-elf/pic-reloc-6.s
 create mode 100644 ld/testsuite/ld-mips-elf/pic-reloc-7.d
 create mode 100644 ld/testsuite/ld-mips-elf/pic-reloc-7.s

diff --git a/bfd/elfxx-mips.c b/bfd/elfxx-mips.c
index 74dadf4..606c698 100644
--- a/bfd/elfxx-mips.c
+++ b/bfd/elfxx-mips.c
@@ -9060,7 +9060,7 @@ _bfd_mips_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
 		break;
 
 	      /* Likewise an absolute symbol.  */
-	      if (bfd_is_abs_symbol (&h->root))
+	      if (h != NULL && bfd_is_abs_symbol (&h->root))
 		break;
 
 	      /* R_MIPS_HI16 against _gp_disp is used for $gp setup,
@@ -9078,13 +9078,16 @@ _bfd_mips_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
 	    case R_MIPS16_26:
 	    case R_MIPS_26:
 	    case R_MICROMIPS_26_S1:
-	      howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, FALSE);
-	      info->callbacks->einfo
-		/* xgettext:c-format */
-		(_("%X%H: relocation %s against `%s' cannot be used"
-		   " when making a shared object; recompile with -fPIC\n"),
-		 abfd, sec, rel->r_offset, howto->name,
-		 (h) ? h->root.root.string : "a local symbol");
+	      howto = MIPS_ELF_RTYPE_TO_HOWTO (abfd, r_type, NEWABI_P (abfd));
+	      /* An error for unsupported relocations is raised as part
+		 of the above search, so we can skip the following.  */
+	      if (howto != NULL)
+		info->callbacks->einfo
+		  /* xgettext:c-format */
+		  (_("%X%H: relocation %s against `%s' cannot be used"
+		     " when making a shared object; recompile with -fPIC\n"),
+		   abfd, sec, rel->r_offset, howto->name,
+		   (h) ? h->root.root.string : "a local symbol");
 	      break;
 	    default:
 	      break;
diff --git a/ld/testsuite/ld-mips-elf/mips-elf.exp b/ld/testsuite/ld-mips-elf/mips-elf.exp
index e2063e5..99add80 100644
--- a/ld/testsuite/ld-mips-elf/mips-elf.exp
+++ b/ld/testsuite/ld-mips-elf/mips-elf.exp
@@ -1636,3 +1636,10 @@ run_dump_test "pic-reloc-1"
 run_dump_test "pic-reloc-2"
 run_dump_test "pic-reloc-3"
 run_dump_test "pic-reloc-4"
+run_dump_test_o32 "pic-reloc-5"
+run_dump_test_o32 "pic-reloc-5" [list [list name (microMIPS)] \
+				      [list as "-mmicromips"]]
+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"]]
diff --git a/ld/testsuite/ld-mips-elf/pic-reloc-5.d b/ld/testsuite/ld-mips-elf/pic-reloc-5.d
new file mode 100644
index 0000000..1b9d07e
--- /dev/null
+++ b/ld/testsuite/ld-mips-elf/pic-reloc-5.d
@@ -0,0 +1,6 @@
+#name: MIPS PIC relocation 5
+#ld: -shared -T pic-reloc-ordinary.ld
+#target: [check_shared_lib_support]
+#error: \A[^\n]*: in function `foo':\n
+#error:   \(\.text\+0x0\): relocation R_(MICRO|)MIPS_HI16 against `a local symbol' cannot be used when making a shared object; recompile with -fPIC\n
+#error:   \(\.text\+0x8\): relocation R_(MICRO|)MIPS_HI16 against `bar' cannot be used when making a shared object; recompile with -fPIC\Z
diff --git a/ld/testsuite/ld-mips-elf/pic-reloc-5.s b/ld/testsuite/ld-mips-elf/pic-reloc-5.s
new file mode 100644
index 0000000..b8ebd8c
--- /dev/null
+++ b/ld/testsuite/ld-mips-elf/pic-reloc-5.s
@@ -0,0 +1,10 @@
+	.text
+	.globl	foo
+	.ent	foo
+foo:
+$LCL:
+	lui	$2, %hi($LCL)
+	addiu	$2, %lo($LCL)
+	lui	$2, %hi(bar)
+	addiu	$2, %lo(bar)
+	.end	foo
diff --git a/ld/testsuite/ld-mips-elf/pic-reloc-6.d b/ld/testsuite/ld-mips-elf/pic-reloc-6.d
new file mode 100644
index 0000000..68be687
--- /dev/null
+++ b/ld/testsuite/ld-mips-elf/pic-reloc-6.d
@@ -0,0 +1,7 @@
+#name: MIPS PIC relocation 6 (MIPS16)
+#ld: -shared -T pic-reloc-ordinary.ld
+#target: [check_shared_lib_support]
+#as: -mips16
+#error: \A[^\n]*: in function `foo':\n
+#error:   \(\.text\+0x0\): relocation R_MIPS16_HI16 against `a local symbol' cannot be used when making a shared object; recompile with -fPIC\n
+#error:   \(\.text\+0x8\): relocation R_MIPS16_HI16 against `bar' cannot be used when making a shared object; recompile with -fPIC\Z
diff --git a/ld/testsuite/ld-mips-elf/pic-reloc-6.s b/ld/testsuite/ld-mips-elf/pic-reloc-6.s
new file mode 100644
index 0000000..777ea26
--- /dev/null
+++ b/ld/testsuite/ld-mips-elf/pic-reloc-6.s
@@ -0,0 +1,10 @@
+	.text
+	.globl	foo
+	.ent	foo
+foo:
+$LCL:
+	li	$2, %hi($LCL)
+	addiu	$2, %lo($LCL)
+	li	$2, %hi(bar)
+	addiu	$2, %lo(bar)
+	.end	foo
diff --git a/ld/testsuite/ld-mips-elf/pic-reloc-7.d b/ld/testsuite/ld-mips-elf/pic-reloc-7.d
new file mode 100644
index 0000000..e5dfa58
--- /dev/null
+++ b/ld/testsuite/ld-mips-elf/pic-reloc-7.d
@@ -0,0 +1,8 @@
+#name: MIPS PIC relocation 7
+#ld: -shared -T pic-reloc-ordinary.ld
+#target: [check_shared_lib_support]
+#error: \A[^\n]*: in function `foo':\n
+#error:   \(\.text\+0x0\): relocation R_(MICRO|)MIPS_HIGHEST against `a local symbol' cannot be used when making a shared object; recompile with -fPIC\n
+#error:   \(\.text\+0x4\): relocation R_(MICRO|)MIPS_HIGHER against `a local symbol' cannot be used when making a shared object; recompile with -fPIC\n
+#error:   \(\.text\+0x8\): relocation R_(MICRO|)MIPS_HIGHEST against `bar' cannot be used when making a shared object; recompile with -fPIC\n
+#error:   \(\.text\+0xc\): relocation R_(MICRO|)MIPS_HIGHER against `bar' cannot be used when making a shared object; recompile with -fPIC\Z
diff --git a/ld/testsuite/ld-mips-elf/pic-reloc-7.s b/ld/testsuite/ld-mips-elf/pic-reloc-7.s
new file mode 100644
index 0000000..3ad55f0
--- /dev/null
+++ b/ld/testsuite/ld-mips-elf/pic-reloc-7.s
@@ -0,0 +1,10 @@
+	.text
+	.globl	foo
+	.ent	foo
+foo:
+$LCL:
+	lui	$2, %highest($LCL)
+	addiu	$2, %higher($LCL)
+	lui	$2, %highest(bar)
+	addiu	$2, %higher(bar)
+	.end	foo
-- 
2.9.5

  reply	other threads:[~2019-05-18  8:19 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-05-18  8:19 [PATCH 0/2] MIPS/LD: Improve error checking for relocations in shared libraries Faraz Shahbazker
2019-05-18  8:19 ` Faraz Shahbazker [this message]
2019-05-18  8:19 ` [PATCH 2/2] MIPS/LD: Reject tprel_hi and tprel_lo relocations in shared library Faraz Shahbazker
2019-05-21 15:33 ` [PATCH 0/2] MIPS/LD: Improve error checking for relocations in shared libraries Nick Clifton

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=20190518081905.223364-2-fshahbazker@wavecomp.com \
    --to=fshahbazker@wavecomp.com \
    --cc=binutils@sourceware.org \
    --cc=paul.hua.gm@gmail.com \
    /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).