Issue an error for read-only segment with dynamic IFUNC relocations
authorH.J. Lu <hjl.tools@gmail.com>
Thu, 13 Aug 2015 11:31:38 +0000 (04:31 -0700)
committerH.J. Lu <hjl.tools@gmail.com>
Thu, 13 Aug 2015 11:31:38 +0000 (04:31 -0700)
To load an ELF binary with DT_TEXTREL tag, the dynamic linker calls
__mprotect on the read-only segment with PROT_READ|PROT_WRITE before
applying dynamic relocation.  It leads to segfault when performing
IFUNC relocations since the read-only segment has no execute permission.
This patch changes x86 linker to issue an error for read-only segment
with dynamic IFUNC relocations.  Other backends with IFUNC support
may need a similar change.

bfd/

PR ld/18801
* elf32-i386.c (elf_i386_size_dynamic_sections): Issue an error
for read-only segment with dynamic IFUNC relocations.
* elf64-x86-64.c (elf_x86_64_size_dynamic_sections): Likewise.

ld/testsuite/

PR ld/18801
* ld-i386/i386.exp: Run pr18801.
* ld-x86-64/x86-64.exp: Likewise.
* ld-i386/pr18801.d: New file.
* ld-i386/pr18801.s: Likewise.
* ld-x86-64/pr18801.d: Likewise.
* ld-x86-64/pr18801.s: Likewise.

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

index e56176444660ac5ba4b78e7fd559786b543905ec..9ba59027395639b67d051bc402c2f6ca9a60e7c8 100644 (file)
@@ -1,3 +1,10 @@
+2015-08-13  H.J. Lu  <hongjiu.lu@intel.com>
+
+       PR ld/18801
+       * elf32-i386.c (elf_i386_size_dynamic_sections): Issue an error
+       for read-only segment with dynamic IFUNC relocations.
+       * elf64-x86-64.c (elf_x86_64_size_dynamic_sections): Likewise.
+
 2015-08-12  Simon Dardis  <simon.dardis@imgtec.com>
 
        * elfxx-mips.c (STUB_MOVE): Change to use 'or' only.
index 3063bed7d1d468932e8a375c3aebb88bc4a996ba..1e4c3f47184eaf339681bcb1545173094c3fe10f 100644 (file)
@@ -3152,6 +3152,15 @@ elf_i386_size_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
 
          if ((info->flags & DF_TEXTREL) != 0)
            {
+             if ((elf_tdata (output_bfd)->has_gnu_symbols
+                  & elf_gnu_symbol_ifunc) == elf_gnu_symbol_ifunc)
+               {
+                 info->callbacks->einfo
+                   (_("%P%X: read-only segment has dynamic IFUNC relocations; recompile with -fPIC\n"));
+                 bfd_set_error (bfd_error_bad_value);
+                 return FALSE;
+               }
+
              if (!add_dynamic_entry (DT_TEXTREL, 0))
                return FALSE;
            }
index 2d3c55ea81efeb45c15e5e5bcc5b5318087f4dbe..348b297d8f4d74fb0ef74ed6565f023d95e7a313 100644 (file)
@@ -3513,6 +3513,15 @@ elf_x86_64_size_dynamic_sections (bfd *output_bfd,
 
          if ((info->flags & DF_TEXTREL) != 0)
            {
+             if ((elf_tdata (output_bfd)->has_gnu_symbols
+                  & elf_gnu_symbol_ifunc) == elf_gnu_symbol_ifunc)
+               {
+                 info->callbacks->einfo
+                   (_("%P%X: read-only segment has dynamic IFUNC relocations; recompile with -fPIC\n"));
+                 bfd_set_error (bfd_error_bad_value);
+                 return FALSE;
+               }
+
              if (!add_dynamic_entry (DT_TEXTREL, 0))
                return FALSE;
            }
index a38dc8bb3f2db9df15110aaaceb1aa0a7b905a58..5ae5cd46fe6e6c24601a96ebdfd2bc6e5dbd9e9c 100644 (file)
@@ -1,3 +1,13 @@
+2015-08-13  H.J. Lu  <hongjiu.lu@intel.com>
+
+       PR ld/18801
+       * ld-i386/i386.exp: Run pr18801.
+       * ld-x86-64/x86-64.exp: Likewise.
+       * ld-i386/pr18801.d: New file.
+       * ld-i386/pr18801.s: Likewise.
+       * ld-x86-64/pr18801.d: Likewise.
+       * ld-x86-64/pr18801.s: Likewise.
+
 2015-08-12  Simon Dardis  <simon.dardis@imgtec.com>
 
        * ld-mips-elf/compressed-plt-1-n32-mips16.od: Update test.
index 0a718cfb888cc7db9e152f99f227326f7689842c..47911a68c10ff87a52b416a5b6fd9f7746bdc113 100644 (file)
@@ -309,6 +309,7 @@ run_dump_test "pr14215"
 run_dump_test "pr17057"
 run_dump_test "pr17935-1"
 run_dump_test "pr17935-2"
+run_dump_test "pr18801"
 run_dump_test "pr18815"
 
 # Add $PLT_CFLAGS if PLT is expected.
diff --git a/ld/testsuite/ld-i386/pr18801.d b/ld/testsuite/ld-i386/pr18801.d
new file mode 100644 (file)
index 0000000..4bb2efb
--- /dev/null
@@ -0,0 +1,3 @@
+#as: --32
+#ld: -m elf_i386 -pie
+#error: read-only segment has dynamic IFUNC relocations; recompile with -fPIC
diff --git a/ld/testsuite/ld-i386/pr18801.s b/ld/testsuite/ld-i386/pr18801.s
new file mode 100644 (file)
index 0000000..ac3377d
--- /dev/null
@@ -0,0 +1,15 @@
+       .text
+       .type   selector, %function
+foo:
+       movl    $0, %eax
+       ret
+selector:
+       mov     $foo, %eax
+       ret
+       .type   selector, %gnu_indirect_function
+       .globl  _start
+_start:
+       mov     $selector, %eax
+       call    *%eax
+       ret
+       .section        .note.GNU-stack,"",@progbits
diff --git a/ld/testsuite/ld-x86-64/pr18801.d b/ld/testsuite/ld-x86-64/pr18801.d
new file mode 100644 (file)
index 0000000..459db6b
--- /dev/null
@@ -0,0 +1,3 @@
+#as: --64
+#ld: -melf_x86_64 -pie
+#error: read-only segment has dynamic IFUNC relocations; recompile with -fPIC
diff --git a/ld/testsuite/ld-x86-64/pr18801.s b/ld/testsuite/ld-x86-64/pr18801.s
new file mode 100644 (file)
index 0000000..16d36da
--- /dev/null
@@ -0,0 +1,15 @@
+       .text
+       .type   selector, %function
+foo:
+       movl    $0, %eax
+       ret
+selector:
+       movabs  $foo, %rax
+       ret
+       .type   selector, %gnu_indirect_function
+       .globl  _start
+_start:
+       movabs  $selector, %rax
+       call    *%rax
+       ret
+       .section        .note.GNU-stack,"",@progbits
index bd48cd3c6e702165ecbc978cdcdafb66b882fd5c..5409dff41e25e84651aef1741b56f14ecbd1aa9b 100644 (file)
@@ -325,6 +325,7 @@ run_dump_test "pr17935-1"
 run_dump_test "pr17935-2"
 run_dump_test "pr18160"
 run_dump_test "pr18176"
+run_dump_test "pr18801"
 run_dump_test "pr18815"
 
 # Add $PLT_CFLAGS if PLT is expected.