From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 5AE993959CA3 for ; Wed, 16 Nov 2022 10:55:30 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 5AE993959CA3 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1668596129; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=jEpjCLGDooUwchicl/OOv/BMgvjeVrqhpjS1IbIX3Y8=; b=V/X5hfFTNVduFTFx27DqpcDxoGtdLFnkaIwcSBFohfAxOptbkQfjYG9lLo5hGGU4WuLS4C BoVuoYdqYzhOKtARAKVIUkPQHNcVyTt88jE2NXi+7EPBldaZd42wbwXSDgdW1Dj75+bje6 z+81Q4OVQdDcqPgKcvDiVmSh2s73TKM= Received: from mail-ej1-f69.google.com (mail-ej1-f69.google.com [209.85.218.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-175-WvlhYwrkOEe9gNSk_IOjJQ-1; Wed, 16 Nov 2022 05:55:28 -0500 X-MC-Unique: WvlhYwrkOEe9gNSk_IOjJQ-1 Received: by mail-ej1-f69.google.com with SMTP id xc12-20020a170907074c00b007416699ea14so9555354ejb.19 for ; Wed, 16 Nov 2022 02:55:28 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=jEpjCLGDooUwchicl/OOv/BMgvjeVrqhpjS1IbIX3Y8=; b=j+oOL25cK6/WgR+B/hz5t0HBA0losRT+20v5PRBH5gLZfv17lkXr1Sm2H/zHBuGcjM 0YYi1hDH/XA5oUzDo35CKSESHQbqD6VafuLubwZi+HnK5tp0Vu+kSZjUybIyTF3P5UvL bcTgye3R+vMlGSYyPUtVJlVMgvgLn53gOy62cFaPW5jph5ny/BbPnPqPxTpqkN5kFrKi 5AkC9rO2xm4ZAbwKXIuIndkVPn9f4B6X+gR6RzGm3qDA1+GBhZaF7Xi6fJAPjO4o6pRb dJPxNqTI8FrO2LpwwGgTf1SZPVubneHMyr4O1cbZ4HrLzpDovfUF/rzOgP4m77Dp9Rws cmZg== X-Gm-Message-State: ANoB5plyzLeKDtbOkAHhEdpZy465ECIbxOCNpmyCX/Qb4auXCoYuPB+1 1aIDvBd+2NjNWZihOPzCxoOJsPV184wPVC3ei70dS+/v6uDjvTU991bp18YMg7+8XT+rjoJX+FO Lrbe+WIG47XUGy25P6S2UNj/k4Iu6p8Ii8Q== X-Received: by 2002:a17:906:5a6f:b0:7ad:8bd5:b7df with SMTP id my47-20020a1709065a6f00b007ad8bd5b7dfmr16736224ejc.57.1668596127400; Wed, 16 Nov 2022 02:55:27 -0800 (PST) X-Google-Smtp-Source: AA0mqf5H0VKVMfs+hNH9CGxpyGmKSP4jIrZeHGWniQKdh10YPbTwQ0vkn6BM9EHGxjtD3CbuJnF3uzXqQy3lcD9XQZk= X-Received: by 2002:a17:906:5a6f:b0:7ad:8bd5:b7df with SMTP id my47-20020a1709065a6f00b007ad8bd5b7dfmr16736216ejc.57.1668596127207; Wed, 16 Nov 2022 02:55:27 -0800 (PST) MIME-Version: 1.0 References: <20221116024619.1465996-1-ppalka@redhat.com> In-Reply-To: <20221116024619.1465996-1-ppalka@redhat.com> From: Jonathan Wakely Date: Wed, 16 Nov 2022 10:55:16 +0000 Message-ID: Subject: Re: [PATCH] libstdc++: Fix stream initialization with static library [PR107701] To: Patrick Palka Cc: gcc-patches@gcc.gnu.org, libstdc++@gcc.gnu.org X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-13.5 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Wed, 16 Nov 2022 at 02:46, Patrick Palka via Libstdc++ wrote: > > When linking with a static library, the linker seems to exclude a > constituent .o object (including its global initializers) if nothing > from it is referenced by the program (unless e.g. --whole-archive is > used). This behavior breaks iostream when linking with static libstdc++.a > (on systems that support init priorities) because we're defining the > global initializer for the standard stream objects in a separate TU > (ios_init.cc) from the stream objects definitions (globals_io.cc). > > Thus in order to ensure that the global initializer for the standard > stream objects doesn't get wrongly dropped when statically linking, > we need to perform the initialization from the same TU that actually > defines the stream objects. > > Tested on x86_64-pc-linux-gnu, does this look OK for trunk? OK, thanks. I'm a little nervous that future improvements to --gc-sections or LTO could cause the same problem, but I think we're OK for now. > > PR libstdc++/107701 > > libstdc++-v3/ChangeLog: > > * src/c++98/globals_io.cc: Include "io_base_init.h" here > instead of ... > * src/c++98/ios_init.cc: ... here. > * testsuite/17_intro/static.cc: Run the test as well. > --- > libstdc++-v3/src/c++98/globals_io.cc | 2 ++ > libstdc++-v3/src/c++98/ios_init.cc | 2 -- > libstdc++-v3/testsuite/17_intro/static.cc | 2 +- > 3 files changed, 3 insertions(+), 3 deletions(-) > > diff --git a/libstdc++-v3/src/c++98/globals_io.cc b/libstdc++-v3/src/c++98/globals_io.cc > index 04fecb22aeb..bfd808b5bbd 100644 > --- a/libstdc++-v3/src/c++98/globals_io.cc > +++ b/libstdc++-v3/src/c++98/globals_io.cc > @@ -69,6 +69,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION > fake_wostream wclog; > #endif > > +#include "ios_base_init.h" > + > _GLIBCXX_END_NAMESPACE_VERSION > } // namespace > > diff --git a/libstdc++-v3/src/c++98/ios_init.cc b/libstdc++-v3/src/c++98/ios_init.cc > index 4016fcab785..1b5132f1c2d 100644 > --- a/libstdc++-v3/src/c++98/ios_init.cc > +++ b/libstdc++-v3/src/c++98/ios_init.cc > @@ -75,8 +75,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION > extern wostream wclog; > #endif > > -#include "ios_base_init.h" > - > ios_base::Init::Init() > { > if (__gnu_cxx::__exchange_and_add_dispatch(&_S_refcount, 1) == 0) > diff --git a/libstdc++-v3/testsuite/17_intro/static.cc b/libstdc++-v3/testsuite/17_intro/static.cc > index ffa7ecb7077..a0d6ed081f8 100644 > --- a/libstdc++-v3/testsuite/17_intro/static.cc > +++ b/libstdc++-v3/testsuite/17_intro/static.cc > @@ -1,4 +1,4 @@ > -// { dg-do link { target c++11 } } > +// { dg-do run { target c++11 } } > // { dg-require-static-libstdcxx } > // { dg-options "-static-libstdc++" } > > -- > 2.38.1.436.geea7033409 >