Check 64-bit relocation addend overflow for x32
authorH.J. Lu <hjl.tools@gmail.com>
Thu, 10 May 2012 03:25:17 +0000 (03:25 +0000)
committerH.J. Lu <hjl.tools@gmail.com>
Thu, 10 May 2012 03:25:17 +0000 (03:25 +0000)
bfd/

* elf64-x86-64.c (elf_x86_64_relocate_section): Check addend
overflow for R_X86_64_RELATIVE64.

gas/

* config/tc-i386.c (tc_gen_reloc): Check x32 addend overflow
for BFD_RELOC_64.

gas/testsuite/

* gas/i386/ilp32/ilp32.exp: Run reloc64-inval.

* gas/i386/ilp32/reloc64.s: Add tests for ".quad".
* gas/i386/ilp32/reloc64.d: Updated.

* gas/i386/ilp32/reloc64-inval.l: New file.
* gas/i386/ilp32/reloc64-inval.s: Likewise.

ld/testsuite/

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

* ld-x86-64/x86-64.exp: Run ilp32-11.

14 files changed:
bfd/ChangeLog
bfd/elf64-x86-64.c
gas/ChangeLog
gas/config/tc-i386.c
gas/testsuite/ChangeLog
gas/testsuite/gas/i386/ilp32/ilp32.exp
gas/testsuite/gas/i386/ilp32/reloc64-inval.l [new file with mode: 0644]
gas/testsuite/gas/i386/ilp32/reloc64-inval.s [new file with mode: 0644]
gas/testsuite/gas/i386/ilp32/reloc64.d
gas/testsuite/gas/i386/ilp32/reloc64.s
ld/testsuite/ChangeLog
ld/testsuite/ld-x86-64/ilp32-11.d [new file with mode: 0644]
ld/testsuite/ld-x86-64/ilp32-11.s [new file with mode: 0644]
ld/testsuite/ld-x86-64/x86-64.exp

index 4c49cf9b59d68fe9a4b46e2768037c6687663d42..ee2c264a5bd7044ac38456dc4b11318711226ab1 100644 (file)
@@ -1,3 +1,8 @@
+2012-05-09  H.J. Lu  <hongjiu.lu@intel.com>
+
+       * elf64-x86-64.c (elf_x86_64_relocate_section): Check addend
+       overflow for R_X86_64_RELATIVE64.
+
 2012-05-08  Ben Cheng  <bccheng@google.com>
 
        * bfd/elf.c: Preserve the original p_align and p_flags if they are
index de7fd6f957fac0c172b31aaf7f3321c9d820765e..8eafbf084c2c3bedc72c0155c4ef934187d883a9 100644 (file)
@@ -3681,6 +3681,27 @@ elf_x86_64_relocate_section (bfd *output_bfd,
                      outrel.r_info = htab->r_info (0,
                                                    R_X86_64_RELATIVE64);
                      outrel.r_addend = relocation + rel->r_addend;
+                     /* Check addend overflow.  */
+                     if ((outrel.r_addend & 0x80000000)
+                         != (rel->r_addend & 0x80000000))
+                       {
+                         const char *name;
+                         if (h && h->root.root.string)
+                           name = h->root.root.string;
+                         else
+                           name = bfd_elf_sym_name (input_bfd, symtab_hdr,
+                                                    sym, NULL);
+                         (*_bfd_error_handler)
+                           (_("%B: addend %ld in relocation %s against "
+                              "symbol `%s' at 0x%lx in section `%A' is "
+                              "out of range"),
+                            input_bfd, input_section,
+                            (long) rel->r_addend,
+                            x86_64_elf_howto_table[r_type].name,
+                            name, (unsigned long) rel->r_offset);
+                         bfd_set_error (bfd_error_bad_value);
+                         return FALSE;
+                       }
                    }
                  else
                    {
index 2cc6360525c4363d29147929f3421591b90d8a6d..3b43ecd06e8c74af0ddee7b66f1912b7810066cd 100644 (file)
@@ -1,3 +1,8 @@
+2012-05-09  H.J. Lu  <hongjiu.lu@intel.com>
+
+       * config/tc-i386.c (tc_gen_reloc): Check x32 addend overflow
+       for BFD_RELOC_64.
+
 2012-05-08  Alan Modra  <amodra@gmail.com>
 
        * Makefile.am (check_DEJAGNU): Export LC_ALL=C in place of other
index ccf54bcae5429785559023d4948545f1690c88c2..06f9764e2282314783bd7b1713f472c98ca8a0ac 100644 (file)
@@ -9173,6 +9173,17 @@ tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
       if (disallow_64bit_reloc)
        switch (code)
          {
+         case BFD_RELOC_64:
+           /* Check addend overflow.  */
+           if ((long long) fixp->fx_offset > 0x7fffffffLL
+               || (long long) fixp->fx_offset < -0x80000000LL)
+             {
+               as_bad_where (fixp->fx_file, fixp->fx_line,
+                             _("cannot represent relocation %s with addend %lld in x32 mode"),
+                             bfd_get_reloc_code_name (code),
+                             (long long) fixp->fx_offset);
+             }
+           break;
          case BFD_RELOC_X86_64_DTPOFF64:
          case BFD_RELOC_X86_64_TPOFF64:
          case BFD_RELOC_64_PCREL:
index e9c7bc771ce819d8ccff007c3aa8e9eaca03f31c..7559561993cf0fa6d4b0fbfa106bc4bcc94ce713 100644 (file)
@@ -1,3 +1,13 @@
+2012-05-09  H.J. Lu  <hongjiu.lu@intel.com>
+
+       * gas/i386/ilp32/ilp32.exp: Run reloc64-inval.
+
+       * gas/i386/ilp32/reloc64.s: Add tests for ".quad".
+       * gas/i386/ilp32/reloc64.d: Updated.
+
+       * gas/i386/ilp32/reloc64-inval.l: New file.
+       * gas/i386/ilp32/reloc64-inval.s: Likewise.
+
 2012-05-08  Alan Modra  <amodra@gmail.com>
 
        * lib/gas-defs.exp (run_dump_test): Don't set LC_ALL here.
index de43bf25ed78ceb7fe21f740a6330e9b92a9fea5..95f3a2d69d4e7b1494e661b07adb6fb763bd68f4 100644 (file)
@@ -26,6 +26,7 @@ if [expr ([istarget "i*86-*-*"] || [istarget "x86_64-*-*"]) && [gas_64_check] &&
     }
 
     run_list_test "reloc64" "--defsym _bad_=1"
+    run_list_test "reloc64-inval"
 
     set ASFLAGS "$old_ASFLAGS"
 }
diff --git a/gas/testsuite/gas/i386/ilp32/reloc64-inval.l b/gas/testsuite/gas/i386/ilp32/reloc64-inval.l
new file mode 100644 (file)
index 0000000..1328237
--- /dev/null
@@ -0,0 +1,3 @@
+.*: Assembler messages:
+.*:2: Error: .*
+.*:3: Error: .*
diff --git a/gas/testsuite/gas/i386/ilp32/reloc64-inval.s b/gas/testsuite/gas/i386/ilp32/reloc64-inval.s
new file mode 100644 (file)
index 0000000..14134aa
--- /dev/null
@@ -0,0 +1,3 @@
+       .data
+       .quad   xtrn + 0x80000000
+       .quad   xtrn - 0x80000001
index 08c15e41857e47f09c0da6cab525b834259ac209..140f24d781b32ab97c950e5ab09c340bf668d590 100644 (file)
@@ -90,3 +90,6 @@ Disassembly of section \.data:
 .*[    ]+R_X86_64_PC16[        ]+xtrn
 .*[    ]+R_X86_64_8[   ]+xtrn
 .*[    ]+R_X86_64_PC8[         ]+xtrn
+.*[    ]+R_X86_64_64[  ]+xtrn
+.*[    ]+R_X86_64_64[  ]+xtrn\+0x7fffffff
+.*[    ]+R_X86_64_64[  ]+xtrn\+0x80000000
index 3f18d04b0250619fcbd9d63cdfeeb855a8f28854..3a2dbb87495649967c6acbfbc4ebbca8eb3eaedf 100644 (file)
@@ -178,3 +178,8 @@ bad .byte   xtrn@tpoff
 
        .text
        mov     xtrn@tpoff (%rbx), %eax
+
+       .data
+       .quad   xtrn
+       .quad   xtrn + 0x7fffffff
+       .quad   xtrn - 0x80000000
index ae46fd0c5da60a696e036ed9008319e04a46f72a..34e8d62aacd42ddbaf63bea1a48882bfc8c4c9a6 100644 (file)
@@ -1,3 +1,10 @@
+2012-05-09  H.J. Lu  <hongjiu.lu@intel.com>
+
+       * ld-x86-64/ilp32-11.d: New file.
+       * ld-x86-64/ilp32-11.s: Likewise.
+
+       * ld-x86-64/x86-64.exp: Run ilp32-11.
+
 2012-05-05  H.J. Lu  <hongjiu.lu@intel.com>
 
        PR ld/14052
diff --git a/ld/testsuite/ld-x86-64/ilp32-11.d b/ld/testsuite/ld-x86-64/ilp32-11.d
new file mode 100644 (file)
index 0000000..7240c58
--- /dev/null
@@ -0,0 +1,3 @@
+#as: --x32
+#ld: -shared -melf32_x86_64
+#error: .*addend 2147483647 in relocation R_X86_64_64 against symbol `.text' at 0x0 in section `.data.rel.local' is out of range
diff --git a/ld/testsuite/ld-x86-64/ilp32-11.s b/ld/testsuite/ld-x86-64/ilp32-11.s
new file mode 100644 (file)
index 0000000..2c85385
--- /dev/null
@@ -0,0 +1,9 @@
+       .section        .data.rel.local,"aw",@progbits
+       .align 8
+.Ljmp:
+       .quad   func + 0x7fffffff
+
+       .text
+       .type   func, @function
+func:
+       ret
index 27174d50946703468fe39b5232475c59cab371e6..e8afbc291f427c56e97db832682a43fde5f1ec24 100644 (file)
@@ -253,6 +253,7 @@ run_dump_test "ilp32-7"
 run_dump_test "ilp32-8"
 run_dump_test "ilp32-9"
 run_dump_test "ilp32-10"
+run_dump_test "ilp32-11"
 run_dump_test "ia32-1"
 run_dump_test "ia32-2"
 run_dump_test "ia32-3"