From patchwork Wed Nov 16 02:46:19 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 20670 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp3060099wru; Tue, 15 Nov 2022 18:47:44 -0800 (PST) X-Google-Smtp-Source: AA0mqf7+OPv18yhPqQg1ivO5hcphhbjHt1WFxcqst22psUHUCmopRgi7GujMUbnN2fHADtkFUhME X-Received: by 2002:a17:906:1515:b0:78e:2ba7:70f6 with SMTP id b21-20020a170906151500b0078e2ba770f6mr16568948ejd.382.1668566864036; Tue, 15 Nov 2022 18:47:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668566864; cv=none; d=google.com; s=arc-20160816; b=kgbztO5SWyoqP7VKswpm/9GvZU9PPD6RmQZkDwPZdzMSJi2Oz/xvPXxcHXmshRGcdV AdhECsDZ6P0zIGmWlwpgSuJ2WeOD2PAMmI1hjsqdlI976Sdo+ZFngfE9zMKLduBSw3lW +YQW7PFKMASOhbgoYROvPk1xGY5xNrLQsiQAyiEnFAS7DwNcmuSQKPs1Rbd2ax1qrAz/ s+9NfGNM1pr8yYa0sicP4Ed5NrMrDTe2xRGQMc+EzNp//Hi1vBH3SvSjkWeJ5VuDD7kL t7YaWN3YSfIUWveZLZOMhD6+Sy/UveupEyscRRf8ZxbYw5Hj40umgVQSA3hVlHdl1Tld KjGg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:cc :to:dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=PGxk0ozAGOFe9JWxb1XcR4O0s4ogU/CfNuINspHazXM=; b=bUZnBWIlqmxN2hq9dNH63BU/oFLE1T/T2vp7i7tuFEwFenQrlABv8XCF09q44d+RTM GKvBzxt1WBy81IRVw7YoNsVSjASbXEPrQDfZWFMYSjfwJS4J1PLT4X42lrZOuxyHOi9i Ft9Fz9zEfYrg+ZiOjt423FjMngwUC03HFQdjQ8CroL1MQpgN6894u9+3YIT2+CPRnOHA heWtx5RxigvWy5R+lUIy6i0v2nrfwjJdhRtm9DRWqA9lGL0RiSp6xR+/V1zp2GnPtSBC ogeJx1LX3qsb0i+gG6xd8ldnThEw3FfTIocGlz9T3ymi2dZ9p8qc4zkPKemf5pskHBp7 wa8g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b="iJz1/CNT"; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id z2-20020a1709063ac200b0079bf804c0c4si9950575ejd.103.2022.11.15.18.47.43 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Nov 2022 18:47:44 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b="iJz1/CNT"; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C5B0E395251E for ; Wed, 16 Nov 2022 02:47:42 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C5B0E395251E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1668566862; bh=PGxk0ozAGOFe9JWxb1XcR4O0s4ogU/CfNuINspHazXM=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=iJz1/CNTh13mw7xJCU97onbCFIoFzl1k6NkD1cIkX1TSdhHaW2Nh3W0lcGoJpA9jo 93npl7egX2zYwL0gmQlDvBdWltOrPiHo6DpqR4Ai4jDKu0ZhvjZsWFc6xqQKUSmbXt X71Uvu/wbsoAs9ea35TsZGnMOrVwwMDwXqZVwZDg= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org 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 CEA1138A8158 for ; Wed, 16 Nov 2022 02:46:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org CEA1138A8158 Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-256-Ou5gCi0DMzCQqHtBlzxQKQ-1; Tue, 15 Nov 2022 21:46:23 -0500 X-MC-Unique: Ou5gCi0DMzCQqHtBlzxQKQ-1 Received: by mail-qk1-f199.google.com with SMTP id x2-20020a05620a448200b006fa7dad5c1cso15833255qkp.10 for ; Tue, 15 Nov 2022 18:46:23 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=PGxk0ozAGOFe9JWxb1XcR4O0s4ogU/CfNuINspHazXM=; b=VAMMPbfkB1r7W8/itRRvTK7zUbNIIPlH6S2qodBxr/ntRwyZJtJvgelqCxAXPoloDL 9mtUgtldso3+PheJo1ORWwI32AeEJjKqyotOclugCS08G7RvM+M3t49XPix3vc/CcbYS ToFfxxtfrl80X3ytCDWWVYXeLprtc0oh0CPSmecw2OSUjDqVkc2qkrCMrWHl/eXel7M1 06LoxRuz1mnXT3t30+VhkrSMAnhbGQY5BjXT6CDiBwisLDbUC2vVEgKTYtFeUTD5MJP+ jt+sdojkjOrmfYhWnhpCi2GAWG5KEzoyc7OMYyaDjBCje5D/0UxsxTekjwGXS+oJVf+d R5tw== X-Gm-Message-State: ANoB5pl0u5EpsTUKDIH7XzCgY6P440bIW/5IaaQ98iMoXsrVNkKxDhw8 25GxfGOM9BCsf+Siomm+iAVv1rNz9Hr3SgB8qKBwHemPI0ojqA/ka6NQLJDYqsb5D3liU4D5H+C x0qnwqeB+lprRInsaCiIz0UdQE5JjlqoKm2qTpcKyFH+iGiOfOoNEL9+iAlR3D/re2zk= X-Received: by 2002:a05:620a:199d:b0:6fa:e099:bc12 with SMTP id bm29-20020a05620a199d00b006fae099bc12mr18228139qkb.308.1668566782285; Tue, 15 Nov 2022 18:46:22 -0800 (PST) X-Received: by 2002:a05:620a:199d:b0:6fa:e099:bc12 with SMTP id bm29-20020a05620a199d00b006fae099bc12mr18228121qkb.308.1668566781987; Tue, 15 Nov 2022 18:46:21 -0800 (PST) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id v14-20020ac873ce000000b003a5689134afsm8091561qtp.36.2022.11.15.18.46.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Nov 2022 18:46:21 -0800 (PST) To: gcc-patches@gcc.gnu.org Cc: libstdc++@gcc.gnu.org, Patrick Palka Subject: [PATCH] libstdc++: Fix stream initialization with static library [PR107701] Date: Tue, 15 Nov 2022 21:46:19 -0500 Message-Id: <20221116024619.1465996-1-ppalka@redhat.com> X-Mailer: git-send-email 2.38.1.436.geea7033409 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Patrick Palka via Gcc-patches From: Patrick Palka Reply-To: Patrick Palka Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749619168145687804?= X-GMAIL-MSGID: =?utf-8?q?1749619168145687804?= 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? 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++" }