public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: "H.J. Lu" <hjl.tools@gmail.com>
To: binutils@sourceware.org
Cc: Peter Edwards <peadar@arista.com>
Subject: [PATCH] x86: Fix DT_JMPREL/DT_PLTRELSZ when relocs share a section
Date: Wed, 23 Aug 2023 08:47:33 -0700	[thread overview]
Message-ID: <20230823154733.276739-1-hjl.tools@gmail.com> (raw)

From: Peter Edwards <peadar@arista.com>

If a linker script does not place the PLT relocations and "normal"
relocations in separate ELF sections, `ld` will currently output incorrect
values for DT_JMPREL and DT_PLTRELSZ - they cover the entire ELF section,
rather than just the PLT relocations

Don't ignore the extent of the BFD section - use the size of the srelplt
BFD section and its offset from the output_secttion

bfd/

	PR ld/30787
	* elfxx-x86.c (_bfd_x86_elf_finish_dynamic_sections): Use input
	section for DT_JMPREL and DT_PLTRELSZ.

ld/

	PR ld/30787
	* testsuite/ld-i386/i386.exp: Run pr30787.
	* testsuite/ld-x86-64/x86-64.exp: Likewise.
	* testsuite/ld-i386/pr30787.d: New file.
	* testsuite/ld-i386/pr30787.s: Likewise.
	* testsuite/ld-i386/pr30787.t: Likewise.
	* testsuite/ld-x86-64/pr30787.d: Likewise.
	* testsuite/ld-x86-64/pr30787.s: Likewise.
	* testsuite/ld-x86-64/pr30787.t: Likewise.
---
 bfd/elfxx-x86.c                   | 5 +++--
 ld/testsuite/ld-i386/i386.exp     | 1 +
 ld/testsuite/ld-i386/pr30787.d    | 7 +++++++
 ld/testsuite/ld-i386/pr30787.s    | 6 ++++++
 ld/testsuite/ld-i386/pr30787.t    | 4 ++++
 ld/testsuite/ld-x86-64/pr30787.d  | 7 +++++++
 ld/testsuite/ld-x86-64/pr30787.s  | 6 ++++++
 ld/testsuite/ld-x86-64/pr30787.t  | 4 ++++
 ld/testsuite/ld-x86-64/x86-64.exp | 1 +
 9 files changed, 39 insertions(+), 2 deletions(-)
 create mode 100644 ld/testsuite/ld-i386/pr30787.d
 create mode 100644 ld/testsuite/ld-i386/pr30787.s
 create mode 100644 ld/testsuite/ld-i386/pr30787.t
 create mode 100644 ld/testsuite/ld-x86-64/pr30787.d
 create mode 100644 ld/testsuite/ld-x86-64/pr30787.s
 create mode 100644 ld/testsuite/ld-x86-64/pr30787.t

diff --git a/bfd/elfxx-x86.c b/bfd/elfxx-x86.c
index f224e8f1354..103559d77ec 100644
--- a/bfd/elfxx-x86.c
+++ b/bfd/elfxx-x86.c
@@ -2771,11 +2771,12 @@ _bfd_x86_elf_finish_dynamic_sections (bfd *output_bfd,
 	  break;
 
 	case DT_JMPREL:
-	  dyn.d_un.d_ptr = htab->elf.srelplt->output_section->vma;
+	  s = htab->elf.srelplt;
+	  dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
 	  break;
 
 	case DT_PLTRELSZ:
-	  s = htab->elf.srelplt->output_section;
+	  s = htab->elf.srelplt;
 	  dyn.d_un.d_val = s->size;
 	  break;
 
diff --git a/ld/testsuite/ld-i386/i386.exp b/ld/testsuite/ld-i386/i386.exp
index 5f53bcf29bc..e8ad973eb36 100644
--- a/ld/testsuite/ld-i386/i386.exp
+++ b/ld/testsuite/ld-i386/i386.exp
@@ -511,6 +511,7 @@ run_dump_test "dt-relr-1a"
 run_dump_test "dt-relr-1b"
 run_dump_test "pr28870"
 run_dump_test "pr28894"
+run_dump_test "pr30787"
 
 if { !([istarget "i?86-*-linux*"]
        || [istarget "i?86-*-gnu*"]
diff --git a/ld/testsuite/ld-i386/pr30787.d b/ld/testsuite/ld-i386/pr30787.d
new file mode 100644
index 00000000000..f82411da8b7
--- /dev/null
+++ b/ld/testsuite/ld-i386/pr30787.d
@@ -0,0 +1,7 @@
+#as: --32
+#ld: -melf_i386 -shared --no-warn-rwx-segments -T pr30787.t
+#readelf: -d --wide
+
+#...
+ 0x0+2 \(PLTRELSZ\) +8 \(bytes\)
+#pass
diff --git a/ld/testsuite/ld-i386/pr30787.s b/ld/testsuite/ld-i386/pr30787.s
new file mode 100644
index 00000000000..71594d2327e
--- /dev/null
+++ b/ld/testsuite/ld-i386/pr30787.s
@@ -0,0 +1,6 @@
+	.text
+	.globl foo
+foo:
+	jmp	bar@PLT
+	leal	func@GOT(%ebx), %eax
+	.section .note.GNU-stack,"",@progbits
diff --git a/ld/testsuite/ld-i386/pr30787.t b/ld/testsuite/ld-i386/pr30787.t
new file mode 100644
index 00000000000..8c476209cba
--- /dev/null
+++ b/ld/testsuite/ld-i386/pr30787.t
@@ -0,0 +1,4 @@
+SECTIONS
+{
+  .rel.dyn : { *(.rel.*) }
+}
diff --git a/ld/testsuite/ld-x86-64/pr30787.d b/ld/testsuite/ld-x86-64/pr30787.d
new file mode 100644
index 00000000000..29102d4cc2c
--- /dev/null
+++ b/ld/testsuite/ld-x86-64/pr30787.d
@@ -0,0 +1,7 @@
+#as: --64
+#ld: -melf_x86_64 -shared --no-warn-rwx-segments -T pr30787.t
+#readelf: -d --wide
+
+#...
+ 0x0+2 \(PLTRELSZ\) +24 \(bytes\)
+#pass
diff --git a/ld/testsuite/ld-x86-64/pr30787.s b/ld/testsuite/ld-x86-64/pr30787.s
new file mode 100644
index 00000000000..8bc774f1312
--- /dev/null
+++ b/ld/testsuite/ld-x86-64/pr30787.s
@@ -0,0 +1,6 @@
+	.text
+	.globl foo
+foo:
+	jmp	bar@PLT
+	movq	func@GOTPCREL(%rip), %rax
+	.section .note.GNU-stack,"",@progbits
diff --git a/ld/testsuite/ld-x86-64/pr30787.t b/ld/testsuite/ld-x86-64/pr30787.t
new file mode 100644
index 00000000000..66759cbb6a6
--- /dev/null
+++ b/ld/testsuite/ld-x86-64/pr30787.t
@@ -0,0 +1,4 @@
+SECTIONS
+{
+  .rela.dyn : { *(.rela.*) }
+}
diff --git a/ld/testsuite/ld-x86-64/x86-64.exp b/ld/testsuite/ld-x86-64/x86-64.exp
index 1a10c395b5c..f94284b079c 100644
--- a/ld/testsuite/ld-x86-64/x86-64.exp
+++ b/ld/testsuite/ld-x86-64/x86-64.exp
@@ -505,6 +505,7 @@ run_dump_test "dt-relr-1a"
 run_dump_test "dt-relr-1a-x32"
 run_dump_test "dt-relr-1b"
 run_dump_test "dt-relr-1b-x32"
+run_dump_test "pr30787"
 
 if { ![skip_sframe_tests] } {
     run_dump_test "sframe-simple-1"
-- 
2.41.0


                 reply	other threads:[~2023-08-23 15:47 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20230823154733.276739-1-hjl.tools@gmail.com \
    --to=hjl.tools@gmail.com \
    --cc=binutils@sourceware.org \
    --cc=peadar@arista.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).