Check PC-relative offset overflow in PLT entry
authorH.J. Lu <hjl.tools@gmail.com>
Tue, 18 Nov 2014 19:03:09 +0000 (11:03 -0800)
committerH.J. Lu <hjl.tools@gmail.com>
Tue, 18 Nov 2014 19:04:46 +0000 (11:04 -0800)
This patch checks PC-relative offset overflow in pushq instruction in
x86-64 PLT entry.

bfd/

PR ld/17618
* elf64-x86-64.c (elf_x86_64_finish_dynamic_symbol): Check
PC-relative offset overflow in PLT entry.

ld/testsuite/

PR ld/17618
* ld-x86-64/x86-64.exp: Run pr17618 for Linux target.

* ld-x86-64/pr17618.d: New file.
* ld-x86-64/pr17618.s: Likewise.

bfd/ChangeLog
bfd/elf64-x86-64.c
ld/testsuite/ChangeLog
ld/testsuite/ld-x86-64/pr17618.d [new file with mode: 0644]
ld/testsuite/ld-x86-64/pr17618.s [new file with mode: 0644]
ld/testsuite/ld-x86-64/x86-64.exp

index f606e1516b8069afd94376b70f8e5ceb337619db..579cca27d58c132b9a554025d5eacb8e334c1579 100644 (file)
@@ -1,3 +1,9 @@
+2014-11-18  H.J. Lu  <hongjiu.lu@intel.com>
+
+       PR ld/17618
+       * elf64-x86-64.c (elf_x86_64_finish_dynamic_symbol): Check
+       PC-relative offset overflow in PLT entry.
+
 2014-11-18  Nick Clifton  <nickc@redhat.com>
 
        PR binutils/17512
index 20f45a917c90700b9b98a9911e0880ef1a5aad3c..432ab588546cef4d9508797c8b08cc2bfc87805b 100644 (file)
@@ -4749,6 +4749,8 @@ elf_x86_64_finish_dynamic_symbol (bfd *output_bfd,
       bfd_byte *loc;
       asection *plt, *gotplt, *relplt, *resolved_plt;
       const struct elf_backend_data *bed;
+      bfd_boolean gotplt_after_plt;
+      int32_t plt_got_pcrel_offset;
 
       /* When building a static executable, use .iplt, .igot.plt and
         .rela.iplt sections for STT_GNU_IFUNC symbols.  */
@@ -4853,14 +4855,23 @@ elf_x86_64_finish_dynamic_symbol (bfd *output_bfd,
 
       /* Put offset the PC-relative instruction referring to the GOT entry,
         subtracting the size of that instruction.  */
-      bfd_put_32 (output_bfd,
-                 (gotplt->output_section->vma
-                  + gotplt->output_offset
-                  + got_offset
-                  - resolved_plt->output_section->vma
-                  - resolved_plt->output_offset
-                  - plt_offset
-                  - plt_got_insn_size),
+      plt_got_pcrel_offset = (gotplt->output_section->vma
+                             + gotplt->output_offset
+                             + got_offset
+                             - resolved_plt->output_section->vma
+                             - resolved_plt->output_offset
+                             - plt_offset
+                             - plt_got_insn_size);
+
+      /* Check PC-relative offset overflow in PLT entry.  */
+      gotplt_after_plt = (gotplt->output_section->vma
+                         > resolved_plt->output_section->vma);
+      if ((gotplt_after_plt && plt_got_pcrel_offset < 0)
+         || (!gotplt_after_plt && plt_got_pcrel_offset > 0))
+       info->callbacks->einfo (_("%F%B: PC-relative offset overflow in PLT entry for `%s'\n"),
+                               output_bfd, h->root.root.string);
+
+      bfd_put_32 (output_bfd, plt_got_pcrel_offset,
                  resolved_plt->contents + plt_offset + plt_got_offset);
 
       /* Fill in the entry in the global offset table, initially this
index a8aa37dc05eb27ae2d941c0276b86e1ae75dea04..72b4bb97038773e6c2d2979e5f6b0f45b4f6919f 100644 (file)
@@ -1,3 +1,11 @@
+2014-11-18  H.J. Lu  <hongjiu.lu@intel.com>
+
+       PR ld/17618
+       * ld-x86-64/x86-64.exp: Run pr17618 for Linux target.
+
+       * ld-x86-64/pr17618.d: New file.
+       * ld-x86-64/pr17618.s: Likewise.
+
 2014-11-18  H.J. Lu  <hongjiu.lu@intel.com>
 
        * ld-x86-64/mpx.exp: Always run mpx3 and mpx4 tests in 64-bit.
diff --git a/ld/testsuite/ld-x86-64/pr17618.d b/ld/testsuite/ld-x86-64/pr17618.d
new file mode 100644 (file)
index 0000000..e640b40
--- /dev/null
@@ -0,0 +1,4 @@
+#name: PLT PC-relative offset overflow check
+#as: --64
+#ld: -shared -melf_x86_64
+#error: .*PC-relative offset overflow in PLT entry for `bar'
diff --git a/ld/testsuite/ld-x86-64/pr17618.s b/ld/testsuite/ld-x86-64/pr17618.s
new file mode 100644 (file)
index 0000000..39102e0
--- /dev/null
@@ -0,0 +1,18 @@
+       .text
+       .globl  foo
+       .type   foo, @function
+foo:
+       call bar@PLT
+       .size   foo, .-foo
+
+       .globl  gap
+       .type   gap, @function
+gap:
+       jmp .L0
+       .space 0x40000000, 0x90
+.L0:
+       jmp .L2
+       .space 0x3fdfff14, 0x90
+.L2:
+       .size   gap, .-gap
+       .section        .note.GNU-stack,"",@progbits
index a9b68ff213382e80c43d6b8c8d0e479a0df97f54..7f09458d01f7984de84916fa6586aedb95db57ac 100644 (file)
@@ -333,3 +333,10 @@ if { [isnative] && [which $CC] != 0 } {
        {dummy.s} {{readelf {-s --wide} x86-64-x32.rd}} "x86-64-x32"}
     }
 }
+
+if { ![istarget "x86_64-*-linux*"]} {
+    return
+}
+
+# Linux only tests
+run_dump_test "pr17618"