From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 12680 invoked by alias); 31 Mar 2017 13:36: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 12594 invoked by uid 89); 31 Mar 2017 13:36:30 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,RCVD_IN_DNSWL_NONE,SPF_PASS autolearn=ham version=3.3.2 spammy=Hx-languages-length:3520 X-HELO: smtp.eu.adacore.com Received: from mel.act-europe.fr (HELO smtp.eu.adacore.com) (194.98.77.210) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 31 Mar 2017 13:36:29 +0000 Received: from localhost (localhost [127.0.0.1]) by filtered-smtp.eu.adacore.com (Postfix) with ESMTP id 4AF598135D for ; Fri, 31 Mar 2017 15:36:28 +0200 (CEST) Received: from smtp.eu.adacore.com ([127.0.0.1]) by localhost (smtp.eu.adacore.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id Qretz88sIuNG for ; Fri, 31 Mar 2017 15:36:28 +0200 (CEST) Received: from [IPv6:2a02:2ab8:224:1:d681:d7ff:fe8f:b69d] (unknown [IPv6:2a02:2ab8:224:1:d681:d7ff:fe8f:b69d]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.eu.adacore.com (Postfix) with ESMTPSA id 3824C81342 for ; Fri, 31 Mar 2017 15:36:28 +0200 (CEST) To: binutils@sourceware.org From: Tristan Gingold Subject: [PATCH]: coff/pe: fix handling of weak defined symbols for --gc-sections Message-ID: Date: Fri, 31 Mar 2017 13:36:00 -0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-IsSubscribed: yes X-SW-Source: 2017-03/txt/msg00382.txt.bz2 Hello, this patch handles the coff/pe specific way to represent a potential definition for a weak symbol in gc-section. There is a link in the undefined weak symbol to the definition. So the section for the definition must be keep if there is a reference to the undefined symbol. No regression on i686-pc-mingw32 Ok to commit ? Regards, Tristan. diff --git a/bfd/ChangeLog b/bfd/ChangeLog index bb851c7..e2c0103 100644 --- a/bfd/ChangeLog +++ b/bfd/ChangeLog @@ -1,3 +1,9 @@ +2017-03-31 gingold + + * coffgen.c (_bfd_coff_gc_mark_hook): Handle PE weak + external symbols with a definition. + (_bfd_coff_gc_mark_extra_sections): Fix typo. + 2017-03-30 Pip Cet * elf32-wasm32.c: Add relocation code, two relocs. diff --git a/bfd/coffgen.c b/bfd/coffgen.c index 3c60ed4..18f423b 100644 --- a/bfd/coffgen.c +++ b/bfd/coffgen.c @@ -2785,8 +2785,22 @@ _bfd_coff_gc_mark_hook (asection *sec, case bfd_link_hash_common: return h->root.u.c.p->section; - case bfd_link_hash_undefined: case bfd_link_hash_undefweak: + if (h->symbol_class == C_NT_WEAK && h->numaux == 1) + { + /* PE weak externals. A weak symbol may include an auxiliary + record indicating that if the weak symbol is not resolved, + another external symbol is used instead. */ + struct coff_link_hash_entry *h2 = + h->auxbfd->tdata.coff_obj_data->sym_hashes[ + h->aux->x_sym.x_tagndx.l]; + + if (h2 && h2->root.type != bfd_link_hash_undefined) + return h2->root.u.def.section; + } + break; + + case bfd_link_hash_undefined: default: break; } @@ -2897,7 +2911,7 @@ _bfd_coff_gc_mark_extra_sections (struct bfd_link_info *info, asection *isec; bfd_boolean some_kept; - if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) + if (bfd_get_flavour (ibfd) != bfd_target_coff_flavour) continue; /* Ensure all linker created sections are kept, and see whether diff --git a/ld/ChangeLog b/ld/ChangeLog index 84ef78e..e0fd169 100644 --- a/ld/ChangeLog +++ b/ld/ChangeLog @@ -1,3 +1,9 @@ +2017-03-31 gingold + + * testsuite/ld-pe/pe.exp: New test. + * testsuite/ld-pe/weakdef-1.s: New test source. + * testsuite/ld-pe/weakdef-1.d: New test. + 2017-03-28 Hans-Peter Nilsson PR ld/16044 diff --git a/ld/testsuite/ld-pe/pe.exp b/ld/testsuite/ld-pe/pe.exp index 8d20056..b69f36a 100644 --- a/ld/testsuite/ld-pe/pe.exp +++ b/ld/testsuite/ld-pe/pe.exp @@ -76,6 +76,8 @@ run_dump_test "longsecn-5" run_dump_test "orphan" run_dump_test "orphan_nu" +run_dump_test "weakdef-1" + run_dump_test "pr19803" set pr19803_dll { { "PR 19803: not exporting swept symbols" diff --git a/ld/testsuite/ld-pe/weakdef-1.d b/ld/testsuite/ld-pe/weakdef-1.d new file mode 100644 index 0000000..57a8565 --- /dev/null +++ b/ld/testsuite/ld-pe/weakdef-1.d @@ -0,0 +1,8 @@ +#source: weakdef-1.s +#target: i*86-*-cygwin* i*86-*-pe i*86-*-mingw* +#ld: -e _start --gc-sections +#objdump: -d + +#... + 401003: a1 00 20 40 00 mov 0x402000,%eax +#pass diff --git a/ld/testsuite/ld-pe/weakdef-1.s b/ld/testsuite/ld-pe/weakdef-1.s new file mode 100644 index 0000000..ddce4fb --- /dev/null +++ b/ld/testsuite/ld-pe/weakdef-1.s @@ -0,0 +1,19 @@ + .weak _wsym + .section .data$wsym,"w" + .align 4 +_wsym: + .long 1 + + .section .text$start,"x" + .globl _start + .def _start; .scl 2; .type 32; .endef +_start: + pushl %ebp + movl %esp, %ebp + movl _wsym, %eax + testl %eax, %eax + sete %al + movzbl %al, %eax + nop + popl %ebp + ret