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.133.124]) by sourceware.org (Postfix) with ESMTPS id 21ADD3945C14 for ; Wed, 16 Nov 2022 10:55:30 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 21ADD3945C14 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-ed1-f71.google.com (mail-ed1-f71.google.com [209.85.208.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-8-ZQJnCU1APGetgRJBZWsQHA-1; Wed, 16 Nov 2022 05:55:28 -0500 X-MC-Unique: ZQJnCU1APGetgRJBZWsQHA-1 Received: by mail-ed1-f71.google.com with SMTP id f20-20020a0564021e9400b00461ea0ce17cso12057637edf.16 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=okiQEPS0B6gH/1B35KukXt/LIlTmCOp3ZtXIOL8MKtiQEuK3mffyNTDKioDggVNyx9 ZXZfwih+fLCfiSwH3B1imqwN3VJYpndEOigHL1XzlyG0P8rdAyUgnlhZLanVNJJXwsR4 7dNBU2LDdu455Nm3PimemQousGIc7SQ+Da3SZhTTodmvZSNpeKzHR9DFzDOdmFoKng6i n7y2rEKIGl89x+H6O5G41U2qQza4hO26gNWyDaJ6zLAB+4Kj0DpRhamW8u3XH59FXxdN EfUgkOhW4ojnXFX+9GylmIOCzXV0FDAr4dg31dMrs3FCa3Ln1MnF6OMfkbMpXUMVp1A1 bB7w== X-Gm-Message-State: ANoB5pmxQZHYmEcdv2jgZwfHuRHOW2O3vWbp49B0d/rcrvnrufUtbZaQ +18x8fmtMoJr7vXTiCnCky/G5LNOZkB0aX0dhdG+/qXYqKgYZD1J8LK1tXVaGBry7WkQki8bgA0 v3VdM+CXeVxHLbOJ8Fye2SebtzyQHB6A= X-Received: by 2002:a17:906:5a6f:b0:7ad:8bd5:b7df with SMTP id my47-20020a1709065a6f00b007ad8bd5b7dfmr16736225ejc.57.1668596127416; 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=-12.4 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=ham 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 >