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 E89843830B3B for ; Wed, 16 Nov 2022 04:02:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org E89843830B3B 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=1668571379; 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=qbTIK5YdqgM0lQr4f9CY1+a0L/VwT6WLDe8AwFF2FUs=; b=afsQ0EnLX/Ar6ysvcFETm46/CTB7hluMg+CRxEanG4kAR809A9ioJtKqtilbvtWG/xEAr3 GxhmnWb8qPOqHDNKotpdvU74wWXEc3OpOTkaBjF9AZO1Vu7cb3aLinoCrFzTCJs4bbaXxV 1jruhTgBEgYSRy/5WJrEGiV1HbEe3b0= Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-399-KoJCsBdXNv2JuK1qfRHWmA-1; Tue, 15 Nov 2022 23:02:57 -0500 X-MC-Unique: KoJCsBdXNv2JuK1qfRHWmA-1 Received: by mail-qk1-f198.google.com with SMTP id w13-20020a05620a424d00b006e833c4fb0dso15817748qko.2 for ; Tue, 15 Nov 2022 20:02:57 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=mime-version:references:message-id:in-reply-to:subject:cc:to:date :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=qbTIK5YdqgM0lQr4f9CY1+a0L/VwT6WLDe8AwFF2FUs=; b=l5B/9XgHFcPKUGzJR5Bt8fiNAyGiBKXwnLHOs4kfZpqbYfK4bhZhAkwkwSUEMbcb82 k9OA7XKK2dAwTyZyOYgW5hBVga9iqIaCHWwFNSfmsm4/sEgvfJNfwteexuKLWIRnLpMa PsmL4h9ceiIS75HdshdTrEJJinHURqf3xGuNgbMjuk5i5lcsFidw/j2Dh089TVYsiXP/ ivfIOYXhyAeGbXRTg2yUQ8Xk3qPoYeqiJ5C/q2oQ3BvV2Ia6glOmYt6K3pt8SacYfuuL 7KxY90ASNTCjGEuXkwHmjYbjoiqCcBAdxF+cqi7Ci3pfFyYNPZxTP4mRKFxkMvr1opcb sOZQ== X-Gm-Message-State: ANoB5plgAqSr9vD6/tazUEgERwEnAflIuWa1C1wxjbo1P2euRNdAgHfP u2AcyP12Tuuq6dioTmOfQzSYA9Xnt5Ew+o2HdMUNmiQM3UVgCPgzGHAAAjdAhE2wJO3AlD3X5S/ 1HS3JJ7Lqtvw4TyM83g== X-Received: by 2002:ac8:460d:0:b0:3a5:3623:3714 with SMTP id p13-20020ac8460d000000b003a536233714mr19625356qtn.337.1668571376831; Tue, 15 Nov 2022 20:02:56 -0800 (PST) X-Google-Smtp-Source: AA0mqf7xTWN0GtGaK1X67dh9XDUiQRoaJKRRBPAleTeY4tbkjqXl+EYXqCU09rFJW1QWyP30nJ13fQ== X-Received: by 2002:ac8:460d:0:b0:3a5:3623:3714 with SMTP id p13-20020ac8460d000000b003a536233714mr19625349qtn.337.1668571376520; Tue, 15 Nov 2022 20:02:56 -0800 (PST) Received: from [192.168.1.130] (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id c8-20020ac86608000000b0039d02911555sm8100398qtp.78.2022.11.15.20.02.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Nov 2022 20:02:55 -0800 (PST) From: Patrick Palka X-Google-Original-From: Patrick Palka Date: Tue, 15 Nov 2022 23:02:55 -0500 (EST) To: Patrick Palka cc: gcc-patches@gcc.gnu.org, libstdc++@gcc.gnu.org Subject: Re: [PATCH] libstdc++: Fix stream initialization with static library [PR107701] In-Reply-To: <20221116024619.1465996-1-ppalka@redhat.com> Message-ID: <2aabe052-761c-15d6-e623-4bff2ad0c2db@idea> References: <20221116024619.1465996-1-ppalka@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=US-ASCII X-Spam-Status: No, score=-14.0 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 Tue, 15 Nov 2022, Patrick Palka 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? v2 with more comment fixes/additions: -- >8 -- Subject: [PATCH] libstdc++: Fix stream initialization with static library [PR107701] When linking with a static library, the linker seems to discard 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 define 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? PR libstdc++/107701 libstdc++-v3/ChangeLog: * include/std/iostream (__ioinit): Adjust comment. * src/c++98/globals_io.cc: Include "io_base_init.h" here instead of ... * src/c++98/ios_init.cc: ... here. * src/c++98/ios_base_init.h (__ioinit): Extend comment. * testsuite/17_intro/static.cc: Run the test as well. --- libstdc++-v3/include/std/iostream | 2 +- libstdc++-v3/src/c++98/globals_io.cc | 2 ++ libstdc++-v3/src/c++98/ios_base_init.h | 1 + libstdc++-v3/src/c++98/ios_init.cc | 2 -- libstdc++-v3/testsuite/17_intro/static.cc | 2 +- 5 files changed, 5 insertions(+), 4 deletions(-) diff --git a/libstdc++-v3/include/std/iostream b/libstdc++-v3/include/std/iostream index ff78e1cfb87..0c62e9aeb7f 100644 --- a/libstdc++-v3/include/std/iostream +++ b/libstdc++-v3/include/std/iostream @@ -74,7 +74,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION // For construction of filebuffers for cout, cin, cerr, clog et. al. // When the init_priority attribute is usable, we do this initialization - // in the compiled library instead (src/c++98/ios_init.cc). + // in the compiled library instead (src/c++98/globals_io.cc). #if !__has_attribute(__init_priority__) static ios_base::Init __ioinit; #endif 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_base_init.h b/libstdc++-v3/src/c++98/ios_base_init.h index 1c71038f4a1..b600ec3298e 100644 --- a/libstdc++-v3/src/c++98/ios_base_init.h +++ b/libstdc++-v3/src/c++98/ios_base_init.h @@ -7,6 +7,7 @@ // sufficiently early (so that it happens before any other global // constructor when statically linking with libstdc++.a), instead of // doing so in (each TU that includes) . +// This needs to be done in the same TU that defines the stream objects. #if __has_attribute(init_priority) static ios_base::Init __ioinit __attribute__((init_priority(90))); #endif 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