public inbox for
 help / color / mirror / Atom feed
From: Jonathan Wakely <>
Cc: Patrick Palka <>
Subject: [PATCH] libstdc++: Fix iostream init for Clang on darwin [PR110432]
Date: Fri, 30 Jun 2023 15:10:56 +0100	[thread overview]
Message-ID: <> (raw)

Tested x86_64-linux. Patrick, PTAL.

-- >8 --

The __has_attribute(init_priority) check in <iostream> is true for Clang
on darwin, which means that user code including <iostream> thinks the
library will initialize the global streams. However, when libstdc++ is
built by GCC on darwin, the __has_attribute(init_priority) check is
false, which means that the library thinks that user code will do the
initialization when <iostream> is included. This means that the
initialization is never done.

Add an autoconf check so that the header and the library both make their
decision based on the static properties of GCC at build time, with a
consistent outcome.

As a belt and braces check, also do the initialization in <iostream> if
the compiler including that header doesn't support the attribute (even
if the library also containers the initialization). This might result in
redundant initialization done in <iostream>, but ensures the
initialization happens somewhere if there's any doubt about the
attribute working correctly due to missing linker support.


	PR libstdc++/110432
	* acinclude.m4 (GLIBCXX_CHECK_INIT_PRIORITY): New.
	* Regenerate.
	* configure: Regenerate.
	* include/std/iostream:
	* src/c++98/ios_base_init.h: Use new autoconf macro instead of
 libstdc++-v3/acinclude.m4              | 27 ++++++++++++++
 libstdc++-v3/               |  3 ++
 libstdc++-v3/configure                 | 51 ++++++++++++++++++++++++++
 libstdc++-v3/              |  3 ++
 libstdc++-v3/include/std/iostream      |  2 +-
 libstdc++-v3/src/c++98/ios_base_init.h |  2 +-
 6 files changed, 86 insertions(+), 2 deletions(-)

diff --git a/libstdc++-v3/acinclude.m4 b/libstdc++-v3/acinclude.m4
index 277ae10e031..823832f97d4 100644
--- a/libstdc++-v3/acinclude.m4
+++ b/libstdc++-v3/acinclude.m4
+dnl Check whether iostream initialization should be done in the library,
+dnl using the init_priority attribute.
+dnl Defines:
+dnl  _GLIBCXX_USE_INIT_PRIORITY_ATTRIBUTE if GCC supports the init_priority
+dnl    attribute for the target.
+  AC_MSG_CHECKING([whether init_priority attribute is supported])
+  #if ! __has_attribute(init_priority)
+  #error init_priority not supported
+  #endif
+		 ], [ac_init_priority=yes], [ac_init_priority=no])
+  if test "$ac_init_priority" = yes; then
+      [Define if init_priority should be used for iostream initialization.])
+  fi
+  AC_MSG_RESULT($ac_init_priority)
 # Macros from the top-level gcc directory.
diff --git a/libstdc++-v3/ b/libstdc++-v3/
index 9770c178767..fc0f2522027 100644
--- a/libstdc++-v3/
+++ b/libstdc++-v3/
@@ -550,6 +550,9 @@ GLIBCXX_ZONEINFO_DIR
 # For src/c++11/ alignment.
+# For using init_priority in
 # Define documentation rules conditionally.
 # See if makeinfo has been installed and is modern enough
diff --git a/libstdc++-v3/include/std/iostream b/libstdc++-v3/include/std/iostream
index cfd124dcf43..ec337cf89dd 100644
--- a/libstdc++-v3/include/std/iostream
+++ b/libstdc++-v3/include/std/iostream
   // 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/
-#if !__has_attribute(__init_priority__)
+#if !(_GLIBCXX_USE_INIT_PRIORITY_ATTRIBUTE && __has_attribute(init_priority))
   static ios_base::Init __ioinit;
 #elif defined(_GLIBCXX_SYMVER_GNU)
   __extension__ __asm (".globl _ZSt21ios_base_library_initv");
diff --git a/libstdc++-v3/src/c++98/ios_base_init.h b/libstdc++-v3/src/c++98/ios_base_init.h
index b600ec3298e..f7edfc84625 100644
--- a/libstdc++-v3/src/c++98/ios_base_init.h
+++ b/libstdc++-v3/src/c++98/ios_base_init.h
@@ -8,6 +8,6 @@
 // constructor when statically linking with libstdc++.a), instead of
 // doing so in (each TU that includes) <iostream>.
 // 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)));

             reply	other threads:[~2023-06-30 14:11 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-30 14:10 Jonathan Wakely [this message]
2023-06-30 14:28 ` Patrick Palka
2023-06-30 14:37   ` Jonathan Wakely

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \ \ \ \ \ \

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).