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 249E338515D7 for ; Wed, 16 Nov 2022 04:02:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 249E338515D7 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=1668571378; 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=CMElUNGiLyfZ1XFv2ISETy5RILdPllzKDTVd33nfh0aDWE+pcLTsdyWVyUmlFe36f1XW3O 8FRNHvcpGWg0ePxYxqfamiEdgDzuBxrL8aS2TetU1idLTfRfQCyBGgDhhDIEiya4UgwORT GuHa/F2aSxk3R/mtoapUkf8qI4pMW9E= Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-554-WG6LkqzvPNeFcmsT_nJQTQ-1; Tue, 15 Nov 2022 23:02:57 -0500 X-MC-Unique: WG6LkqzvPNeFcmsT_nJQTQ-1 Received: by mail-qt1-f198.google.com with SMTP id n12-20020ac85a0c000000b003a5849497f9so12133227qta.20 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=Y/Dtq9Z6CGPPIsv9jwpS3SQZLCPxURqyPU6DxL8TN70RB3xwe3QmJLU2rkQJYdEtjY 8vIPIBgnRP0IereKJuM3qTu34RCWpXJv5TgE3w9d6E1yQnpJTJO3jll/6UTuhtdAM0Nk neZTrwzWgRTKTsD4RzfG5pjN/XC/Uv/33rLk2bnRCRy7t2wTQwJ49FCfFoQRoQRThkvJ fL1oVMAxXGMeWqP7qr8Mq08tFApn84QWHp2qGqxZ2wTg792Yv8H/zpDPhkiaUdcuisNP Ruhl6icBIPb/9ociVFKPZZjmxYTh/3WwIuD3BdiGyVsfTypEYD96dyJp0Vr2NbaqNKZY gjkw== X-Gm-Message-State: ANoB5pk0LUWoYouw9BwiPAvSJsGrSt912g2KqBdMi5XxsNe2NmoOxbL4 Twfg2+DWKsH0W2OhvwGFyg31cmUmgSm3um41PKPBMZtXrkgBOPfA5WwfstIrQ4WMsVKXj4l11u5 RUesq6QeJERjABTU= X-Received: by 2002:ac8:460d:0:b0:3a5:3623:3714 with SMTP id p13-20020ac8460d000000b003a536233714mr19625355qtn.337.1668571376829; 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=-13.9 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 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