Check run-time R_X86_64_32 relocation overflow
authorH.J. Lu <hjl.tools@gmail.com>
Wed, 20 Apr 2016 18:10:21 +0000 (11:10 -0700)
committerH.J. Lu <hjl.tools@gmail.com>
Wed, 20 Apr 2016 18:10:38 +0000 (11:10 -0700)
Since elf_x86_64_check_relocs is called after opening all input files,
we can detect dynamic R_X86_64_32 relocation overflow there.

bfd/

PR ld/19969
* elf64-x86-64.c (check_relocs_failed): New.
(elf_x86_64_need_pic): Moved before elf_x86_64_check_relocs.
Support relocation agaist local symbol.  Set check_relocs_failed.
(elf_x86_64_check_relocs): Use elf_x86_64_need_pic.  Check
R_X86_64_32 relocation overflow.
(elf_x86_64_relocate_section): Skip if check_relocs failed.
Update one elf_x86_64_need_pic and remove one elf_x86_64_need_pic.

ld/

PR ld/19969
* testsuite/ld-x86-64/pr19969.d: New file.
* testsuite/ld-x86-64/pr19969a.S: Likewise.
* testsuite/ld-x86-64/pr19969b.S: Likewise.
* testsuite/ld-x86-64/x86-64.exp: Run pr19969 tests.

bfd/ChangeLog
bfd/elf64-x86-64.c
ld/ChangeLog
ld/testsuite/ld-x86-64/pr19969.d [new file with mode: 0644]
ld/testsuite/ld-x86-64/pr19969a.S [new file with mode: 0644]
ld/testsuite/ld-x86-64/pr19969b.S [new file with mode: 0644]
ld/testsuite/ld-x86-64/x86-64.exp

index 46ee8fd..73b0b4d 100644 (file)
@@ -1,5 +1,16 @@
 2016-04-20  H.J. Lu  <hongjiu.lu@intel.com>
 
+       PR ld/19969
+       * elf64-x86-64.c (check_relocs_failed): New.
+       (elf_x86_64_need_pic): Moved before elf_x86_64_check_relocs.
+       Support relocation agaist local symbol.  Set check_relocs_failed.
+       (elf_x86_64_check_relocs): Use elf_x86_64_need_pic.  Check
+       R_X86_64_32 relocation overflow.
+       (elf_x86_64_relocate_section): Skip if check_relocs failed.
+       Update one elf_x86_64_need_pic and remove one elf_x86_64_need_pic.
+
+2016-04-20  H.J. Lu  <hongjiu.lu@intel.com>
+
        * elf32-i386.c (elf_i386_check_relocs): Call
        _bfd_elf_create_ifunc_sections only for STT_GNU_IFUNC symbol.
        * elf64-x86-64.c (elf_x86_64_check_relocs): Likewise.
index 197a1b8..54fc1c8 100644 (file)
@@ -1587,7 +1587,57 @@ elf_x86_64_tls_transition (struct bfd_link_info *info, bfd *abfd,
 
 /* Rename some of the generic section flags to better document how they
    are used here.  */
-#define need_convert_load sec_flg0
+#define need_convert_load      sec_flg0
+#define check_relocs_failed    sec_flg1
+
+static bfd_boolean
+elf_x86_64_need_pic (bfd *input_bfd, asection *sec,
+                    struct elf_link_hash_entry *h,
+                    Elf_Internal_Shdr *symtab_hdr,
+                    Elf_Internal_Sym *isym,
+                    reloc_howto_type *howto)
+{
+  const char *v = "";
+  const char *und = "";
+  const char *pic = "";
+
+  const char *name;
+  if (h)
+    {
+      name = h->root.root.string;
+      switch (ELF_ST_VISIBILITY (h->other))
+       {
+       case STV_HIDDEN:
+         v = _("hidden symbol ");
+         break;
+       case STV_INTERNAL:
+         v = _("internal symbol ");
+         break;
+       case STV_PROTECTED:
+         v = _("protected symbol ");
+         break;
+       default:
+         v = _("symbol ");
+         pic = _("; recompile with -fPIC");
+         break;
+       }
+
+      if (!h->def_regular && !h->def_dynamic)
+       und = _("undefined ");
+    }
+  else
+    {
+      name = bfd_elf_sym_name (input_bfd, symtab_hdr, isym, NULL);
+      pic = _("; recompile with -fPIC");
+    }
+
+  (*_bfd_error_handler) (_("%B: relocation %s against %s%s`%s' can "
+                          "not be used when making a shared object%s"),
+                        input_bfd, howto->name, und, v, name, pic);
+  bfd_set_error (bfd_error_bad_value);
+  sec->check_relocs_failed = 1;
+  return FALSE;
+}
 
 /* Look through the relocs for a section during the first phase, and
    calculate needed space in the global offset table, procedure
@@ -1802,19 +1852,8 @@ elf_x86_64_check_relocs (bfd *abfd, struct bfd_link_info *info,
 
        case R_X86_64_TPOFF32:
          if (!bfd_link_executable (info) && ABI_64_P (abfd))
-           {
-             if (h)
-               name = h->root.root.string;
-             else
-               name = bfd_elf_sym_name (abfd, symtab_hdr, isym,
-                                        NULL);
-             (*_bfd_error_handler)
-               (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
-                abfd,
-                x86_64_elf_howto_table[r_type].name, name);
-             bfd_set_error (bfd_error_bad_value);
-             return FALSE;
-           }
+           return elf_x86_64_need_pic (abfd, sec, h, symtab_hdr, isym,
+                                       &x86_64_elf_howto_table[r_type]);
          if (eh != NULL)
            eh->has_got_reloc = 1;
          break;
@@ -1973,26 +2012,20 @@ elf_x86_64_check_relocs (bfd *abfd, struct bfd_link_info *info,
        case R_X86_64_8:
        case R_X86_64_16:
        case R_X86_64_32S:
-         /* Let's help debug shared library creation.  These relocs
-            cannot be used in shared libs.  Don't error out for
+         /* Check relocation overflow as these relocs may lead to
+            run-time relocation overflow.  Don't error out for
             sections we don't care about, such as debug sections or
-            non-constant sections, or when relocation overflow check
-            is disabled.  */
+            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)
-           {
-             if (h)
-               name = h->root.root.string;
-             else
-               name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
-             (*_bfd_error_handler)
-               (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
-                abfd, x86_64_elf_howto_table[r_type].name, name);
-             bfd_set_error (bfd_error_bad_value);
-             return FALSE;
-           }
+             && (bfd_link_pic (info)
+                 || (bfd_link_executable (info)
+                     && h != NULL
+                     && !h->def_regular
+                     && h->def_dynamic
+                     && (sec->flags & SEC_READONLY) == 0))
+             && (sec->flags & SEC_ALLOC) != 0)
+           return elf_x86_64_need_pic (abfd, sec, h, symtab_hdr, isym,
+                                       &x86_64_elf_howto_table[r_type]);
          /* Fall through.  */
 
        case R_X86_64_PC8:
@@ -4040,42 +4073,6 @@ is_32bit_relative_branch (bfd_byte *contents, bfd_vma offset)
              && (contents [offset - 1] & 0xf0) == 0x80));
 }
 
-static bfd_boolean
-elf_x86_64_need_pic (bfd *input_bfd, struct elf_link_hash_entry *h,
-                    reloc_howto_type *howto)
-{
-  const char *fmt;
-  const char *v;
-  const char *pic = "";
-
-  switch (ELF_ST_VISIBILITY (h->other))
-    {
-    case STV_HIDDEN:
-      v = _("hidden symbol");
-      break;
-    case STV_INTERNAL:
-      v = _("internal symbol");
-      break;
-    case STV_PROTECTED:
-      v = _("protected symbol");
-      break;
-    default:
-      v = _("symbol");
-      pic = _("; recompile with -fPIC");
-      break;
-    }
-
-  if (h->def_regular)
-    fmt = _("%B: relocation %s against %s `%s' can not be used when making a shared object%s");
-  else
-    fmt = _("%B: relocation %s against undefined %s `%s' can not be used when making a shared object%s");
-
-  (*_bfd_error_handler) (fmt, input_bfd, howto->name,
-                        v,  h->root.root.string, pic);
-  bfd_set_error (bfd_error_bad_value);
-  return FALSE;
-}
-
 /* Relocate an x86_64 ELF section.  */
 
 static bfd_boolean
@@ -4100,6 +4097,10 @@ elf_x86_64_relocate_section (bfd *output_bfd,
 
   BFD_ASSERT (is_x86_64_elf (input_bfd));
 
+  /* Skip if check_relocs failed.  */
+  if (input_section->check_relocs_failed)
+    return FALSE;
+
   htab = elf_x86_64_hash_table (info);
   if (htab == NULL)
     return FALSE;
@@ -4756,7 +4757,8 @@ elf_x86_64_relocate_section (bfd *output_bfd,
                }
 
              if (fail)
-               return elf_x86_64_need_pic (input_bfd, h, howto);
+               return elf_x86_64_need_pic (input_bfd, input_section,
+                                           h, NULL, NULL, howto);
            }
          /* Fall through.  */
 
@@ -4838,11 +4840,6 @@ direct:
                                || SYMBOLIC_BIND (info, h))
                           || ! h->def_regular))
                {
-                 if ((r_type != R_X86_64_PC64 && r_type != R_X86_64_64)
-                     && bfd_link_executable (info)
-                     && h->root.type == bfd_link_hash_undefweak
-                     && !resolved_to_zero)
-                   return elf_x86_64_need_pic (input_bfd, h, howto);
                  outrel.r_info = htab->r_info (h->dynindx, r_type);
                  outrel.r_addend = rel->r_addend;
                }
index ac908e5..efa69f9 100644 (file)
@@ -1,5 +1,13 @@
 2016-04-20  H.J. Lu  <hongjiu.lu@intel.com>
 
+       PR ld/19969
+       * testsuite/ld-x86-64/pr19969.d: New file.
+       * testsuite/ld-x86-64/pr19969a.S: Likewise.
+       * testsuite/ld-x86-64/pr19969b.S: Likewise.
+       * testsuite/ld-x86-64/x86-64.exp: Run pr19969 tests.
+
+2016-04-20  H.J. Lu  <hongjiu.lu@intel.com>
+
        * emulparams/elf32_x86_64.sh (CHECK_RELOCS_AFTER_OPEN_INPUT):
        New.
        * emulparams/elf_i386.sh (CHECK_RELOCS_AFTER_OPEN_INPUT):
diff --git a/ld/testsuite/ld-x86-64/pr19969.d b/ld/testsuite/ld-x86-64/pr19969.d
new file mode 100644 (file)
index 0000000..c56af2f
--- /dev/null
@@ -0,0 +1,4 @@
+#source: pr19969b.S
+#as: --64
+#ld: -melf_x86_64 tmpdir/pr19969.so
+#error: .*relocation R_X86_64_32 against symbol `foo' can not be used when making a shared object; recompile with -fPIC
diff --git a/ld/testsuite/ld-x86-64/pr19969a.S b/ld/testsuite/ld-x86-64/pr19969a.S
new file mode 100644 (file)
index 0000000..f318401
--- /dev/null
@@ -0,0 +1,7 @@
+       .data
+       .align 4
+       .global foo
+       .type   foo, @object
+       .size   foo, 4
+foo:
+       .long   -1
diff --git a/ld/testsuite/ld-x86-64/pr19969b.S b/ld/testsuite/ld-x86-64/pr19969b.S
new file mode 100644 (file)
index 0000000..eabeba2
--- /dev/null
@@ -0,0 +1,14 @@
+       .text
+       .globl  _start
+       .type   _start, @function
+_start:
+       movl    foo_p(%rip), %eax
+       ret
+       .size   _start, .-_start
+       .globl  foo_p
+       .data
+       .align 4
+       .type   foo_p, @object
+       .size   foo_p, 4
+foo_p:
+       .long   foo
index 2dba81d..fbd39af 100644 (file)
@@ -156,6 +156,8 @@ set x86_64tests {
      "--64" { dummy.s } {{readelf {-rW} pr19827.rd}} "pr19827"}
     {"Build pr19827.so" "-melf_x86_64 -shared -Bsymbolic" ""
      "--64" { pr19827a.S }  {{readelf {-rW} pr19827.rd}} "pr19827.so"}
+    {"Build pr19969.so" "-melf_x86_64 -shared" ""
+     "--64" { pr19969a.S } {} "pr19969.so"}
 }
 
 # So as to avoid rewriting every last test case here in a nacl variant,
@@ -262,6 +264,7 @@ run_dump_test "pr19807-2b"
 run_dump_test "pr19807-2c"
 run_dump_test "pr19807-2d"
 run_dump_test "pr19807-2e"
+run_dump_test "pr19969"
 
 if { ![istarget "x86_64-*-linux*"] && ![istarget "x86_64-*-nacl*"]} {
     return