x86: Fix DT_JMPREL/DT_PLTRELSZ when relocs share a section
authorPeter Edwards <peadar@arista.com>
Tue, 22 Aug 2023 18:57:28 +0000 (19:57 +0100)
committerH.J. Lu <hjl.tools@gmail.com>
Wed, 23 Aug 2023 16:10:16 +0000 (09:10 -0700)
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
ld/testsuite/ld-i386/i386.exp
ld/testsuite/ld-i386/pr30787.d [new file with mode: 0644]
ld/testsuite/ld-i386/pr30787.s [new file with mode: 0644]
ld/testsuite/ld-i386/pr30787.t [new file with mode: 0644]
ld/testsuite/ld-x86-64/pr30787.d [new file with mode: 0644]
ld/testsuite/ld-x86-64/pr30787.s [new file with mode: 0644]
ld/testsuite/ld-x86-64/pr30787.t [new file with mode: 0644]
ld/testsuite/ld-x86-64/x86-64.exp

index f224e8f1354dfa76e1a0b3d6bd3944930f9ae597..103559d77ecf9e897fd3f0e27b52e6e0ad82010f 100644 (file)
@@ -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;
 
index 5f53bcf29bc85231f489c4fe4236c3497f13e00c..e8ad973eb36027a30b0a7c4ed3c486b16bd8f252 100644 (file)
@@ -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 (file)
index 0000000..f82411d
--- /dev/null
@@ -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 (file)
index 0000000..71594d2
--- /dev/null
@@ -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 (file)
index 0000000..8c47620
--- /dev/null
@@ -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 (file)
index 0000000..29102d4
--- /dev/null
@@ -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 (file)
index 0000000..8bc774f
--- /dev/null
@@ -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 (file)
index 0000000..66759cb
--- /dev/null
@@ -0,0 +1,4 @@
+SECTIONS
+{
+  .rela.dyn : { *(.rela.*) }
+}
index 1a10c395b5ce0555381d4fab9248a2dad960bf64..f94284b079c66f3da7db33a4ead34d4e7b17bfef 100644 (file)
@@ -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"