Add -z noreloc-overflow option to x86-64 ld
authorH.J. Lu <hjl.tools@gmail.com>
Tue, 15 Mar 2016 18:07:06 +0000 (11:07 -0700)
committerH.J. Lu <hjl.tools@gmail.com>
Tue, 15 Mar 2016 18:07:54 +0000 (11:07 -0700)
Add -z noreloc-overflow command-line option to the x86-64 ELF linker to
disable relocation overflow check.  This can be used to avoid relocation
overflow check if there will be no dynamic relocation overflow at
run-time.

bfd/

PR ld/19807
* elf64-x86-64.c (elf_x86_64_relocate_section): Check
no_reloc_overflow_check to diable R_X86_64_32/R_X86_64_32S
relocation overflow check.

include/

PR ld/19807
* bfdlink.h (bfd_link_info): Add no_reloc_overflow_check.

ld/

PR ld/19807
* Makefile.am (ELF_X86_DEPS): Add
$(srcdir)/emulparams/reloc_overflow.sh.
* Makefile.in: Regenerated.
* NEWS: Mention -z noreloc-overflow.
* ld.texinfo: Document -z noreloc-overflow.
* emulparams/elf32_x86_64.sh: Source
${srcdir}/emulparams/reloc_overflow.sh.
* emulparams/elf_x86_64.sh: Likewise.
* emulparams/reloc_overflow.sh: New file.
* testsuite/ld-x86-64/pr19807-1.s: New file.
* testsuite/ld-x86-64/pr19807-1a.d: Likewise.
* testsuite/ld-x86-64/pr19807-1b.d: Likewise.
* testsuite/ld-x86-64/pr19807-2.s: Likewise.
* testsuite/ld-x86-64/pr19807-2a.d: Likewise.
* testsuite/ld-x86-64/pr19807-2b.d: Likewise.
* testsuite/ld-x86-64/pr19807-2c.d: Likewise.
* testsuite/ld-x86-64/pr19807-2d.d: Likewise.
* testsuite/ld-x86-64/pr19807-2e.d: Likewise.
* testsuite/ld-x86-64/x86-64.exp: Run PR ld/19807 tests.

22 files changed:
bfd/ChangeLog
bfd/elf64-x86-64.c
include/ChangeLog
include/bfdlink.h
ld/ChangeLog
ld/Makefile.am
ld/Makefile.in
ld/NEWS
ld/emulparams/elf32_x86_64.sh
ld/emulparams/elf_x86_64.sh
ld/emulparams/reloc_overflow.sh [new file with mode: 0644]
ld/ld.texinfo
ld/testsuite/ld-x86-64/pr19807-1.s [new file with mode: 0644]
ld/testsuite/ld-x86-64/pr19807-1a.d [new file with mode: 0644]
ld/testsuite/ld-x86-64/pr19807-1b.d [new file with mode: 0644]
ld/testsuite/ld-x86-64/pr19807-2.s [new file with mode: 0644]
ld/testsuite/ld-x86-64/pr19807-2a.d [new file with mode: 0644]
ld/testsuite/ld-x86-64/pr19807-2b.d [new file with mode: 0644]
ld/testsuite/ld-x86-64/pr19807-2c.d [new file with mode: 0644]
ld/testsuite/ld-x86-64/pr19807-2d.d [new file with mode: 0644]
ld/testsuite/ld-x86-64/pr19807-2e.d [new file with mode: 0644]
ld/testsuite/ld-x86-64/x86-64.exp

index 89180cb25620d261d581134c6d0904123beaa75b..2bb0cc1ea6af6facfdc84d7cb282d52a1dd73c7d 100644 (file)
@@ -1,3 +1,10 @@
+2016-03-15  H.J. Lu  <hongjiu.lu@intel.com>
+
+       PR ld/19807
+       * elf64-x86-64.c (elf_x86_64_relocate_section): Check
+       no_reloc_overflow_check to diable R_X86_64_32/R_X86_64_32S
+       relocation overflow check.
+
 2016-03-14  H.J. Lu  <hongjiu.lu@intel.com>
 
        * bfd-in2.h: Regenerated.
index 380376d93c16d464f2ab4bc481ff3cff40074f17..31b1f87d0b7dc86a796b0848048136008c9ed60b 100644 (file)
@@ -1973,8 +1973,10 @@ elf_x86_64_check_relocs (bfd *abfd, struct bfd_link_info *info,
          /* Let's help debug shared library creation.  These relocs
             cannot be used in shared libs.  Don't error out for
             sections we don't care about, such as debug sections or
-            non-constant sections.  */
-         if (bfd_link_pic (info)
+            non-constant sections, or when relocation overflow check
+            is disabled.  */
+         if (!info->no_reloc_overflow_check
+             && bfd_link_pic (info)
              && (sec->flags & SEC_ALLOC) != 0
              && (sec->flags & SEC_READONLY) != 0)
            {
@@ -4842,8 +4844,12 @@ direct:
                }
              else
                {
-                 /* This symbol is local, or marked to become local.  */
-                 if (r_type == htab->pointer_r_type)
+                 /* This symbol is local, or marked to become local.
+                    When relocation overflow check is disabled, we
+                    convert R_X86_64_32 to dynamic R_X86_64_RELATIVE.  */
+                 if (r_type == htab->pointer_r_type
+                     || (r_type == R_X86_64_32
+                         && info->no_reloc_overflow_check))
                    {
                      relocate = TRUE;
                      outrel.r_info = htab->r_info (0, R_X86_64_RELATIVE);
index 7e01b279bd97fd9049584c943f95b57c4578b0ae..3523a27ede686dabe3fb752e3d4bf104136e821b 100644 (file)
@@ -1,3 +1,8 @@
+2016-03-15  H.J. Lu  <hongjiu.lu@intel.com>
+
+       PR ld/19807
+       * bfdlink.h (bfd_link_info): Add no_reloc_overflow_check.
+
 2016-03-08  Cupertino Miranda  <Cupertino.Miranda@synopsys.com>
            Andrew Burgess  <andrew.burgess@embecosm.com>
 
index eeea6ef7a97c7fedd66ce98fac2374c26e5ce396..a285f6dc5e2d9844844e2a877454bdcfbba23201 100644 (file)
@@ -446,6 +446,9 @@ struct bfd_link_info
   /* TRUE if generation of .interp/PT_INTERP should be suppressed.  */
   unsigned int nointerp: 1;
 
+  /* TRUE if we shouldn't check relocation overflow.  */
+  unsigned int no_reloc_overflow_check: 1;
+
   /* TRUE if generate a 1-byte NOP as suffix for x86 call instruction.  */
   unsigned int call_nop_as_suffix : 1;
 
index 1dfe830421ae321fcd1bd5fd0b97845148274505..d3172dcb0a50ccaf15cdeb98a69b18adc9fee077 100644 (file)
@@ -1,3 +1,26 @@
+2016-03-15  H.J. Lu  <hongjiu.lu@intel.com>
+
+       PR ld/19807
+       * Makefile.am (ELF_X86_DEPS): Add
+       $(srcdir)/emulparams/reloc_overflow.sh.
+       * Makefile.in: Regenerated.
+       * NEWS: Mention -z noreloc-overflow.
+       * ld.texinfo: Document -z noreloc-overflow.
+       * emulparams/elf32_x86_64.sh: Source
+       ${srcdir}/emulparams/reloc_overflow.sh.
+       * emulparams/elf_x86_64.sh: Likewise.
+       * emulparams/reloc_overflow.sh: New file.
+       * testsuite/ld-x86-64/pr19807-1.s: New file.
+       * testsuite/ld-x86-64/pr19807-1a.d: Likewise.
+       * testsuite/ld-x86-64/pr19807-1b.d: Likewise.
+       * testsuite/ld-x86-64/pr19807-2.s: Likewise.
+       * testsuite/ld-x86-64/pr19807-2a.d: Likewise.
+       * testsuite/ld-x86-64/pr19807-2b.d: Likewise.
+       * testsuite/ld-x86-64/pr19807-2c.d: Likewise.
+       * testsuite/ld-x86-64/pr19807-2d.d: Likewise.
+       * testsuite/ld-x86-64/pr19807-2e.d: Likewise.
+       * testsuite/ld-x86-64/x86-64.exp: Run PR ld/19807 tests.
+
 2016-03-13  H.J. Lu  <hongjiu.lu@intel.com>
 
        PR ld/19539
index 4a8c0b6e87576bc3cea3592a1e85c6d33833e5ca..d14e769b14ee9df200b1a421a6a6e18ac866c799 100644 (file)
@@ -672,6 +672,7 @@ ELF_GEN_DEPS = $(srcdir)/emultempl/generic.em $(srcdir)/emultempl/elf-generic.em
 ELF_X86_DEPS = $(ELF_DEPS) $(srcdir)/emulparams/plt_unwind.sh \
               $(srcdir)/emulparams/extern_protected_data.sh \
               $(srcdir)/emulparams/dynamic_undefined_weak.sh \
+              $(srcdir)/emulparams/reloc_overflow.sh \
               $(srcdir)/emulparams/call_nop.sh
 
 @TDIRS@
index 45388d7bacfe7d46eeaf1a21f054733e9fbb2df9..8c692282db0e5729cc594211e80d61dbc0b2fe0c 100644 (file)
@@ -888,6 +888,7 @@ ELF_GEN_DEPS = $(srcdir)/emultempl/generic.em $(srcdir)/emultempl/elf-generic.em
 ELF_X86_DEPS = $(ELF_DEPS) $(srcdir)/emulparams/plt_unwind.sh \
               $(srcdir)/emulparams/extern_protected_data.sh \
               $(srcdir)/emulparams/dynamic_undefined_weak.sh \
+              $(srcdir)/emulparams/reloc_overflow.sh \
               $(srcdir)/emulparams/call_nop.sh
 
 
diff --git a/ld/NEWS b/ld/NEWS
index 4bf8091e52b57261cff8d683af648753f23c8770..b88da5cbb095f79841f1eb4aa8e272dacfe656a4 100644 (file)
--- a/ld/NEWS
+++ b/ld/NEWS
@@ -1,5 +1,8 @@
 -*- text -*-
 
+* Support for -z noreloc-overflow in the x86-64 ELF linker to disable
+  relocation overflow check.
+
 * Add -z common/-z nocommon options for ELF targets to control whether to
   convert common symbols to the STT_COMMON type during a relocatable link.
 
index 1b285c559a1f6ccb99027c827c69e9a98d5eca34..967c1b45cd0076f33cedd6fbf2baebfc2da3fd88 100644 (file)
@@ -1,6 +1,7 @@
 . ${srcdir}/emulparams/plt_unwind.sh
 . ${srcdir}/emulparams/extern_protected_data.sh
 . ${srcdir}/emulparams/dynamic_undefined_weak.sh
+. ${srcdir}/emulparams/reloc_overflow.sh
 . ${srcdir}/emulparams/call_nop.sh
 SCRIPT_NAME=elf
 ELFSIZE=32
index 0159a6ca44c980a8aa53384dbc166ffdfcaca255..e935f90d7e630d741dae04bd4820a603558e4021 100644 (file)
@@ -1,6 +1,7 @@
 . ${srcdir}/emulparams/plt_unwind.sh
 . ${srcdir}/emulparams/extern_protected_data.sh
 . ${srcdir}/emulparams/dynamic_undefined_weak.sh
+. ${srcdir}/emulparams/reloc_overflow.sh
 . ${srcdir}/emulparams/call_nop.sh
 SCRIPT_NAME=elf
 ELFSIZE=64
diff --git a/ld/emulparams/reloc_overflow.sh b/ld/emulparams/reloc_overflow.sh
new file mode 100644 (file)
index 0000000..7ba0ee5
--- /dev/null
@@ -0,0 +1,11 @@
+PARSE_AND_LIST_OPTIONS_RELOC_OVERFLOW='
+  fprintf (file, _("\
+  -z noreloc-overflow         Disable relocation overflow check\n"));
+'
+PARSE_AND_LIST_ARGS_CASE_Z_RELOC_OVERFLOW='
+      else if (strcmp (optarg, "noreloc-overflow") == 0)
+       link_info.no_reloc_overflow_check = TRUE;
+'
+
+PARSE_AND_LIST_OPTIONS="$PARSE_AND_LIST_OPTIONS $PARSE_AND_LIST_OPTIONS_RELOC_OVERFLOW"
+PARSE_AND_LIST_ARGS_CASE_Z="$PARSE_AND_LIST_ARGS_CASE_Z $PARSE_AND_LIST_ARGS_CASE_Z_RELOC_OVERFLOW"
index b2b99ab66054ba5bc448908b5754a777bed0404b..d3d8dc608aa6c7ccbdfbe46ebef4c6e31bcfcdcf 100644 (file)
@@ -1213,6 +1213,11 @@ This option overrides linker backend default.  It can be used to avoid
 dynamic relocations against undefined weak symbols in executable.
 Supported for i386 and x86-64.
 
+@item noreloc-overflow
+Disable relocation overflow check.  This can be used to disable
+relocation overflow check if there will be no dynamic relocation
+overflow at run-time.  Supported for x86_64.
+
 @item call-nop=prefix-addr
 @itemx call-nop=prefix-nop
 @itemx call-nop=suffix-nop
diff --git a/ld/testsuite/ld-x86-64/pr19807-1.s b/ld/testsuite/ld-x86-64/pr19807-1.s
new file mode 100644 (file)
index 0000000..0870f6a
--- /dev/null
@@ -0,0 +1,8 @@
+       .globl  _start
+       .type   _start, @function
+_start:
+       movq    $foo, %rax
+       .size   _start, .-_start
+       .data
+foo:
+       .quad   0
diff --git a/ld/testsuite/ld-x86-64/pr19807-1a.d b/ld/testsuite/ld-x86-64/pr19807-1a.d
new file mode 100644 (file)
index 0000000..f3c5390
--- /dev/null
@@ -0,0 +1,13 @@
+#source: pr19807-1.s
+#as: --64
+#ld: -pie -melf_x86_64 -z noreloc-overflow
+#objdump: -dw
+
+.*: +file format .*
+
+
+Disassembly of section .text:
+
+[a-f0-9]+ <_start>:
+[      ]*[a-f0-9]+:    48 c7 c0 ([0-9a-f]{2} ){4}      mov    \$0x[a-f0-9]+,%rax
+#pass
diff --git a/ld/testsuite/ld-x86-64/pr19807-1b.d b/ld/testsuite/ld-x86-64/pr19807-1b.d
new file mode 100644 (file)
index 0000000..6af4985
--- /dev/null
@@ -0,0 +1,13 @@
+#source: pr19807-1.s
+#as: --x32
+#ld: -pie -melf32_x86_64 -z noreloc-overflow
+#objdump: -dw
+
+.*: +file format .*
+
+
+Disassembly of section .text:
+
+[a-f0-9]+ <_start>:
+[      ]*[a-f0-9]+:    48 c7 c0 ([0-9a-f]{2} ){4}      mov    \$0x[a-f0-9]+,%rax
+#pass
diff --git a/ld/testsuite/ld-x86-64/pr19807-2.s b/ld/testsuite/ld-x86-64/pr19807-2.s
new file mode 100644 (file)
index 0000000..2b309f2
--- /dev/null
@@ -0,0 +1,8 @@
+       .globl  _start
+       .type   _start, @function
+_start:
+       movl    $foo, %eax
+       .size   _start, .-_start
+       .data
+foo:
+       .quad   0
diff --git a/ld/testsuite/ld-x86-64/pr19807-2a.d b/ld/testsuite/ld-x86-64/pr19807-2a.d
new file mode 100644 (file)
index 0000000..1357d72
--- /dev/null
@@ -0,0 +1,4 @@
+#source: pr19807-2.s
+#as: --64
+#ld: -pie -melf_x86_64
+#error: .*relocation R_X86_64_32 against `.data' can not be used when making a shared object; recompile with -fPIC
diff --git a/ld/testsuite/ld-x86-64/pr19807-2b.d b/ld/testsuite/ld-x86-64/pr19807-2b.d
new file mode 100644 (file)
index 0000000..a781b30
--- /dev/null
@@ -0,0 +1,8 @@
+#source: pr19807-2.s
+#as: --x32
+#ld: -pie -melf32_x86_64
+#readelf: -r --wide
+
+Relocation section '.rela.dyn' at offset 0x[0-9a-f]+ contains 1 entries:
+ Offset     Info    Type                Sym. Value  Symbol's Name \+ Addend
+[0-9a-f]+ +[0-9a-f]+ +R_X86_64_RELATIVE +[0-9a-f]+
diff --git a/ld/testsuite/ld-x86-64/pr19807-2c.d b/ld/testsuite/ld-x86-64/pr19807-2c.d
new file mode 100644 (file)
index 0000000..83ac641
--- /dev/null
@@ -0,0 +1,13 @@
+#source: pr19807-2.s
+#as: --x32
+#ld: -pie -melf32_x86_64
+#objdump: -dw
+
+.*: +file format .*
+
+
+Disassembly of section .text:
+
+[a-f0-9]+ <_start>:
+[      ]*[a-f0-9]+:    b8 ([0-9a-f]{2} ){4}            mov    \$0x[a-f0-9]+,%eax
+#pass
diff --git a/ld/testsuite/ld-x86-64/pr19807-2d.d b/ld/testsuite/ld-x86-64/pr19807-2d.d
new file mode 100644 (file)
index 0000000..1171283
--- /dev/null
@@ -0,0 +1,8 @@
+#source: pr19807-2.s
+#as: --64
+#ld: -pie -melf_x86_64 -z noreloc-overflow
+#readelf: -r --wide
+
+Relocation section '.rela.dyn' at offset 0x[0-9a-f]+ contains 1 entries:
+    Offset             Info             Type               Symbol's Value  Symbol's Name \+ Addend
+[0-9a-f]+ +[0-9a-f]+ +R_X86_64_RELATIVE +[0-9a-f]+
diff --git a/ld/testsuite/ld-x86-64/pr19807-2e.d b/ld/testsuite/ld-x86-64/pr19807-2e.d
new file mode 100644 (file)
index 0000000..f26616a
--- /dev/null
@@ -0,0 +1,13 @@
+#source: pr19807-2.s
+#as: --64
+#ld: -pie -melf_x86_64 -z noreloc-overflow
+#objdump: -dw
+
+.*: +file format .*
+
+
+Disassembly of section .text:
+
+[a-f0-9]+ <_start>:
+[      ]*[a-f0-9]+:    b8 ([0-9a-f]{2} ){4}            mov    \$0x[a-f0-9]+,%eax
+#pass
index 49f9fa3c2cb8e601c4cc50ffb2164e66edb73af8..8f297610acee7597c2a21cc5da217351685bd477 100644 (file)
@@ -247,6 +247,13 @@ run_dump_test "largecomm-1e"
 run_dump_test "largecomm-1f"
 run_dump_test "pr19539a"
 run_dump_test "pr19539b"
+run_dump_test "pr19807-1a"
+run_dump_test "pr19807-1b"
+run_dump_test "pr19807-2a"
+run_dump_test "pr19807-2b"
+run_dump_test "pr19807-2c"
+run_dump_test "pr19807-2d"
+run_dump_test "pr19807-2e"
 
 if { ![istarget "x86_64-*-linux*"] && ![istarget "x86_64-*-nacl*"]} {
     return