From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pg1-f196.google.com (mail-pg1-f196.google.com [209.85.215.196]) by sourceware.org (Postfix) with ESMTPS id 62F15387700C for ; Thu, 12 Mar 2020 04:09:22 +0000 (GMT) Received: by mail-pg1-f196.google.com with SMTP id t24so2362762pgj.7 for ; Wed, 11 Mar 2020 21:09:22 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition:in-reply-to:user-agent; bh=BDiFsmdSlKA8EUoZQ2EhgWv5/MCSmPzoTlyny9Y6Do4=; b=CKZDhEEVP8jQormmUqIbuWE/xSATTqJ7C0mwZrukgUpTbPG06hx8PFiBIsShIAC5iE EU/qi0oLSfJNJ8T5AedLVgU+wuP9tfRtBaASHXMf1ew5v81VPfBxSkRACpzjx5y5KLdA YPURf5rULbEPWVwhjrEum/x+B7WMFJ8etDvVhGXqGjHpOr+FAOBTYeaYpV9th340APNP UCHhz48+Dd/31U5C94jaiVPScoJKiBGx7QE1jZyZuPTxjDngHclz7162Ln7xu0CP1d6s jbNktsyrG5d1zHBaEWL0tU1zRmD8jAz3RQtkJyMFZzyEcE3R+Ta99y6PsOJwtRfS+f0B SSUw== X-Gm-Message-State: ANhLgQ3KFKM3G/PkRN6zrsQNmqCD7/031eAiJKvN2wL/Dfdx9AvUTkIl FICq7GIyPO9uBD5gwrRbECswu3v3 X-Google-Smtp-Source: ADFU+vsgbnSsYK3f8p7KD+nK4NOLdqiL8d3VbLEu9XeEX6LH9ZRrNvm9AaQTzEpQuiBxOfnMFUSBHw== X-Received: by 2002:aa7:82d5:: with SMTP id f21mr6122663pfn.245.1583986161333; Wed, 11 Mar 2020 21:09:21 -0700 (PDT) Received: from localhost ([2601:647:4b01:ae80::51fb]) by smtp.gmail.com with ESMTPSA id mp5sm6830892pjb.48.2020.03.11.21.09.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Mar 2020 21:09:21 -0700 (PDT) Date: Wed, 11 Mar 2020 21:09:20 -0700 From: Fangrui Song To: hjl.tools@gmail.com Cc: binutils@sourceware.org Subject: Re: [PATCH] ld: Properly check static link of dynamic object Message-ID: <20200312040920.GA43870@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: <20200311235752.406955-1-hjl.tools@gmail.com> User-Agent: Mutt/1.12.2 (2019-09-21) X-Spam-Status: No, score=-25.2 required=5.0 tests=BAYES_00, FAKE_REPLY_C, FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: binutils@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 12 Mar 2020 04:09:24 -0000 > When -static is passed to gcc, gcc passes it to linker before any input > files to create static executable. If -Bdynamic is also passed to linker > from command-line, linker should issue an error if dynamic object is used. > > include/ > > PR ld/24920 > * bfdlink.h (bfd_link_info): Add static_exec. > > ld/ > > PR ld/24920 > * ldfile.c (ldfile_try_open_bfd): Also check link_info.static_exec > for static link of dynamic object. > * lexsup.c (parse_args): Set link_info.static_exec to true if we > see -static before any input files. > * testsuite/ld-elf/pr24920.err: New file. > * testsuite/ld-elf/shared.exp: Run ld/24920 tests. > --- > include/bfdlink.h | 3 +++ > ld/ldfile.c | 6 ++++-- > ld/lexsup.c | 4 ++++ > ld/testsuite/ld-elf/pr24920.err | 1 + > ld/testsuite/ld-elf/shared.exp | 18 ++++++++++++++++++ > 5 files changed, 30 insertions(+), 2 deletions(-) > create mode 100644 ld/testsuite/ld-elf/pr24920.err > > diff --git a/include/bfdlink.h b/include/bfdlink.h > index 8d85530e39..a850c6102c 100644 > --- a/include/bfdlink.h > +++ b/include/bfdlink.h > @@ -322,6 +322,9 @@ struct bfd_link_info > /* Output type. */ > ENUM_BITFIELD (output_type) type : 2; > > + /* TRUE if building a static executable. */ > + unsigned int static_exec : 1; > + > /* TRUE if BFD should pre-bind symbols in a shared object. */ > unsigned int symbolic: 1; > > diff --git a/ld/ldfile.c b/ld/ldfile.c > index aa84906992..7b57bd9fb7 100644 > --- a/ld/ldfile.c > +++ b/ld/ldfile.c > @@ -164,7 +164,8 @@ ldfile_try_open_bfd (const char *attempt, > checks out compatible, do not exit early returning TRUE, or > the plugins will not get a chance to claim the file. */ > > - if (entry->flags.search_dirs || !entry->flags.dynamic) > + if (entry->flags.search_dirs > + || (link_info.static_exec || !entry->flags.dynamic)) > { > bfd *check; > > @@ -274,7 +275,8 @@ ldfile_try_open_bfd (const char *attempt, > goto success; > } > > - if (!entry->flags.dynamic && (entry->the_bfd->flags & DYNAMIC) != 0) > + if ((link_info.static_exec || !entry->flags.dynamic) > + && (entry->the_bfd->flags & DYNAMIC) != 0) > { > einfo (_("%F%P: attempted static link of dynamic object `%s'\n"), > attempt); > diff --git a/ld/lexsup.c b/ld/lexsup.c > index 3d15cc491d..aeddc52466 100644 > --- a/ld/lexsup.c > +++ b/ld/lexsup.c > @@ -786,6 +786,10 @@ parse_args (unsigned argc, char **argv) > break; > case OPTION_NON_SHARED: > input_flags.dynamic = FALSE; > + /* If we see -static before any input files, we are building > + a static executable. */ > + if (!lang_has_input_file) > + link_info.static_exec = TRUE; > break; > case OPTION_CREF: > command_line.cref = TRUE; > diff --git a/ld/testsuite/ld-elf/pr24920.err b/ld/testsuite/ld-elf/pr24920.err > new file mode 100644 > index 0000000000..8f5cab9167 > --- /dev/null > +++ b/ld/testsuite/ld-elf/pr24920.err > @@ -0,0 +1 @@ > +.*: attempted static link of dynamic object `tmpdir/pr24920.so' > diff --git a/ld/testsuite/ld-elf/shared.exp b/ld/testsuite/ld-elf/shared.exp > index b1762aff9b..0b8024dd1a 100644 > --- a/ld/testsuite/ld-elf/shared.exp > +++ b/ld/testsuite/ld-elf/shared.exp > @@ -114,6 +114,24 @@ run_ld_link_tests [list \ > {} \ > "pr22649-1.so" \ > ] \ > + [list \ > + "Build pr24920.so" \ > + "$LFLAGS -shared" \ > + "" \ > + "$AFLAGS_PIC" \ > + {dummy.s} \ > + {} \ > + "pr24920.so" \ > + ] \ > + [list \ > + "Build pr24920" \ > + "$LFLAGS -static " \ > + "-Bdynamic tmpdir/pr24920.so" \ > + "" \ > + {start.s} \ > + {{ld pr24920.err}} \ > + "pr24920" \ > + ] \ > ] > > if { [check_gc_sections_available] } { > -- > 2.24.1 Does the patch reject valid use case like: ld -Bstatic a.o -Bdynamic b.so If the gcc driver detects the input file type, I think such an error should be emitted on its side.? -static = -Bstatic. There is no need to teach it to know that the first -static/-Bstatic is different.