From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 109418 invoked by alias); 20 Apr 2016 18:11:33 -0000 Mailing-List: contact binutils-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: binutils-owner@sourceware.org Received: (qmail 109404 invoked by uid 89); 20 Apr 2016 18:11:33 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=1.1 required=5.0 tests=AWL,BAYES_50,FREEMAIL_FROM,SPF_SOFTFAIL autolearn=no version=3.3.2 spammy=*abfd, htab, sk:elf_int, sk:Elf_Int X-HELO: mga01.intel.com Received: from mga01.intel.com (HELO mga01.intel.com) (192.55.52.88) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 20 Apr 2016 18:11:23 +0000 Received: from orsmga003.jf.intel.com ([10.7.209.27]) by fmsmga101.fm.intel.com with ESMTP; 20 Apr 2016 11:11:21 -0700 X-ExtLoop1: 1 Received: from gnu-6.sc.intel.com ([172.25.70.218]) by orsmga003.jf.intel.com with ESMTP; 20 Apr 2016 11:11:22 -0700 From: "H.J. Lu" To: binutils@sourceware.org Subject: [PATCH] Check run-time R_X86_64_32 relocation overflow Date: Wed, 20 Apr 2016 18:11:00 -0000 Message-Id: <1461175881-32076-1-git-send-email-hjl.tools@gmail.com> X-IsSubscribed: yes X-SW-Source: 2016-04/txt/msg00342.txt.bz2 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 | 11 +++ bfd/elf64-x86-64.c | 145 +++++++++++++++++++------------------- ld/ChangeLog | 8 +++ ld/testsuite/ld-x86-64/pr19969.d | 4 ++ ld/testsuite/ld-x86-64/pr19969a.S | 7 ++ ld/testsuite/ld-x86-64/pr19969b.S | 14 ++++ ld/testsuite/ld-x86-64/x86-64.exp | 3 + 7 files changed, 118 insertions(+), 74 deletions(-) create mode 100644 ld/testsuite/ld-x86-64/pr19969.d create mode 100644 ld/testsuite/ld-x86-64/pr19969a.S create mode 100644 ld/testsuite/ld-x86-64/pr19969b.S diff --git a/bfd/ChangeLog b/bfd/ChangeLog index 46ee8fd..73b0b4d 100644 --- a/bfd/ChangeLog +++ b/bfd/ChangeLog @@ -1,5 +1,16 @@ 2016-04-20 H.J. Lu + 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 + * 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. diff --git a/bfd/elf64-x86-64.c b/bfd/elf64-x86-64.c index 197a1b8..54fc1c8 100644 --- a/bfd/elf64-x86-64.c +++ b/bfd/elf64-x86-64.c @@ -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; } diff --git a/ld/ChangeLog b/ld/ChangeLog index ac908e5..efa69f9 100644 --- a/ld/ChangeLog +++ b/ld/ChangeLog @@ -1,5 +1,13 @@ 2016-04-20 H.J. Lu + 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 + * 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 index 0000000..c56af2f --- /dev/null +++ b/ld/testsuite/ld-x86-64/pr19969.d @@ -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 index 0000000..f318401 --- /dev/null +++ b/ld/testsuite/ld-x86-64/pr19969a.S @@ -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 index 0000000..eabeba2 --- /dev/null +++ b/ld/testsuite/ld-x86-64/pr19969b.S @@ -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 diff --git a/ld/testsuite/ld-x86-64/x86-64.exp b/ld/testsuite/ld-x86-64/x86-64.exp index 2dba81d..fbd39af 100644 --- a/ld/testsuite/ld-x86-64/x86-64.exp +++ b/ld/testsuite/ld-x86-64/x86-64.exp @@ -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 -- 2.5.5