From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1888) id AD8FA385DC3B; Sun, 6 Nov 2022 16:16:17 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org AD8FA385DC3B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1667751377; bh=B1f4J/WGryWlNWSuRvJ4/LcWaDVpO/DVOhu+CETGFSw=; h=From:To:Subject:Date:From; b=Ea0yLk9omlVvTsMez0GLtjQCE1Brlxm7RSokL17FPv1YXe5Sl/cV+WksV6hf6lk42 0JkkM8SlfUWBMXs7NR7qcJaYSKP0Tyqtc2rl/EeQDoBsGgg7lk8CkjlHlBqiCO1JBL hVeC5WmqUeNvD7DUICllaOGvhXlIU2pPcmwmXdQg= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Patrick Palka To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r13-3707] libstdc++: Move stream initialization into compiled library [PR44952] X-Act-Checkin: gcc X-Git-Author: Patrick Palka X-Git-Refname: refs/heads/master X-Git-Oldrev: d0a492faa6478c99d325fa4a7ed2e5633cef7529 X-Git-Newrev: 4e4e3ffd10f53ef71696bc728ab40258751a2df4 Message-Id: <20221106161617.AD8FA385DC3B@sourceware.org> Date: Sun, 6 Nov 2022 16:16:17 +0000 (GMT) List-Id: https://gcc.gnu.org/g:4e4e3ffd10f53ef71696bc728ab40258751a2df4 commit r13-3707-g4e4e3ffd10f53ef71696bc728ab40258751a2df4 Author: Patrick Palka Date: Sun Nov 6 11:16:00 2022 -0500 libstdc++: Move stream initialization into compiled library [PR44952] This patch moves the static object for constructing the standard streams out from and into the compiled library on systems that support init priorities. This'll mean no longer introduces a separate global constructor in each TU that includes it. We can do this only if the init_priority attribute is supported because we need a way to ensure the stream initialization runs first before any user global initializer, particularly when linking with a static libstdc++.a. PR libstdc++/44952 PR libstdc++/39796 PR libstdc++/98108 libstdc++-v3/ChangeLog: * include/std/iostream (__ioinit): No longer define here if the init_priority attribute is usable. * src/c++98/ios_init.cc (__ioinit): Define here instead if init_priority is usable, via ... * src/c++98/ios_base_init.h: ... this new file. Diff: --- libstdc++-v3/include/std/iostream | 4 ++++ libstdc++-v3/src/c++98/ios_base_init.h | 12 ++++++++++++ libstdc++-v3/src/c++98/ios_init.cc | 2 ++ 3 files changed, 18 insertions(+) diff --git a/libstdc++-v3/include/std/iostream b/libstdc++-v3/include/std/iostream index 70318a45891..ff78e1cfb87 100644 --- a/libstdc++-v3/include/std/iostream +++ b/libstdc++-v3/include/std/iostream @@ -73,7 +73,11 @@ _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). +#if !__has_attribute(__init_priority__) static ios_base::Init __ioinit; +#endif _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 new file mode 100644 index 00000000000..1c71038f4a1 --- /dev/null +++ b/libstdc++-v3/src/c++98/ios_base_init.h @@ -0,0 +1,12 @@ +// This is only in a header so we can use the system_header pragma, +// to suppress the warning caused by using a reserved init_priority. +#pragma GCC system_header + +// If the target supports init priorities, set up a static object in the +// compiled library to perform the initialization once and +// 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) . +#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 1b5132f1c2d..4016fcab785 100644 --- a/libstdc++-v3/src/c++98/ios_init.cc +++ b/libstdc++-v3/src/c++98/ios_init.cc @@ -75,6 +75,8 @@ _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)