From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id E99153858C2B; Wed, 19 Jul 2023 19:35:03 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E99153858C2B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1689795303; bh=Xcxulo8wq+e230Xng2qB2kJO9b89Z5DJotNHA7Xg/S8=; h=From:To:Subject:Date:From; b=NaKhO0tre9mvjd3R/f6g6OshWBwm5qXYbi9ibOLW+WVZjJn3fLW44AFY2bjWESgSg 25xtssstKOEurmRAYxTTE+qpZgljNRjYp6o8djbCGnhiWLNf+mOyrgCQ/1Djifv4Ic v3PNG/AMOYIY+Fa9XjBvZJifxGGOmiy0LsgE9xzQ= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r13-7591] libstdc++: Fix iostream init for Clang on darwin [PR110432] X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/releases/gcc-13 X-Git-Oldrev: 33a84d438883b5d6616048636a5fc47f6dd4ed28 X-Git-Newrev: 61bf34d17473d611bb2695329808810dbd5af478 Message-Id: <20230719193503.E99153858C2B@sourceware.org> Date: Wed, 19 Jul 2023 19:35:03 +0000 (GMT) List-Id: https://gcc.gnu.org/g:61bf34d17473d611bb2695329808810dbd5af478 commit r13-7591-g61bf34d17473d611bb2695329808810dbd5af478 Author: Jonathan Wakely Date: Fri Jun 30 11:07:35 2023 +0100 libstdc++: Fix iostream init for Clang on darwin [PR110432] The __has_attribute(init_priority) check in is true for Clang on darwin, which means that user code including 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 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 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 , but ensures the initialization happens somewhere if there's any doubt about the attribute working correctly due to missing linker support. libstdc++-v3/ChangeLog: PR libstdc++/110432 * acinclude.m4 (GLIBCXX_CHECK_INIT_PRIORITY): New. * config.h.in: Regenerate. * configure: Regenerate. * configure.ac: Use GLIBCXX_CHECK_INIT_PRIORITY. * include/std/iostream: Use new autoconf macro as well as __has_attribute. * src/c++98/ios_base_init.h: Use new autoconf macro instead of __has_attribute. Reviewed-by: Patrick Palka (cherry picked from commit fe2651affa8c15624188bfd062fb894648743431) Diff: --- libstdc++-v3/acinclude.m4 | 27 ++++++++++++++++++ libstdc++-v3/config.h.in | 3 ++ libstdc++-v3/configure | 51 ++++++++++++++++++++++++++++++++++ libstdc++-v3/configure.ac | 3 ++ libstdc++-v3/include/std/iostream | 3 +- libstdc++-v3/src/c++98/ios_base_init.h | 2 +- 6 files changed, 87 insertions(+), 2 deletions(-) diff --git a/libstdc++-v3/acinclude.m4 b/libstdc++-v3/acinclude.m4 index c2977e4c375..49cb0623e4f 100644 --- a/libstdc++-v3/acinclude.m4 +++ b/libstdc++-v3/acinclude.m4 @@ -5290,6 +5290,33 @@ AC_DEFUN([GLIBCXX_CHECK_ALIGNAS_CACHELINE], [ AC_LANG_RESTORE ]) +dnl +dnl Check whether iostream initialization should be done in the library, +dnl using the init_priority attribute. +dnl +dnl Defines: +dnl _GLIBCXX_USE_INIT_PRIORITY_ATTRIBUTE if GCC supports the init_priority +dnl attribute for the target. +dnl +AC_DEFUN([GLIBCXX_CHECK_INIT_PRIORITY], [ +AC_LANG_SAVE + AC_LANG_CPLUSPLUS + + AC_MSG_CHECKING([whether init_priority attribute is supported]) + AC_TRY_COMPILE(, [ + #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 + AC_DEFINE_UNQUOTED(_GLIBCXX_USE_INIT_PRIORITY_ATTRIBUTE, 1, + [Define if init_priority should be used for iostream initialization.]) + fi + AC_MSG_RESULT($ac_init_priority) + + AC_LANG_RESTORE +]) + # Macros from the top-level gcc directory. m4_include([../config/gc++filt.m4]) m4_include([../config/tls.m4]) diff --git a/libstdc++-v3/config.h.in b/libstdc++-v3/config.h.in index 047dcc4f1dd..7129adfad54 100644 --- a/libstdc++-v3/config.h.in +++ b/libstdc++-v3/config.h.in @@ -958,6 +958,9 @@ /* Define if get_nprocs is available in . */ #undef _GLIBCXX_USE_GET_NPROCS +/* Define if init_priority should be used for iostream initialization. */ +#undef _GLIBCXX_USE_INIT_PRIORITY_ATTRIBUTE + /* Define if LFS support is available. */ #undef _GLIBCXX_USE_LFS diff --git a/libstdc++-v3/configure b/libstdc++-v3/configure index 5339834b875..9b602777545 100755 --- a/libstdc++-v3/configure +++ b/libstdc++-v3/configure @@ -71732,6 +71732,57 @@ ac_compiler_gnu=$ac_cv_c_compiler_gnu +# For using init_priority in ios_init.cc + + + ac_ext=cpp +ac_cpp='$CXXCPP $CPPFLAGS' +ac_compile='$CXX -c $CXXFLAGS $CPPFLAGS conftest.$ac_ext >&5' +ac_link='$CXX -o conftest$ac_exeext $CXXFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5' +ac_compiler_gnu=$ac_cv_cxx_compiler_gnu + + + { $as_echo "$as_me:${as_lineno-$LINENO}: checking whether init_priority attribute is supported" >&5 +$as_echo_n "checking whether init_priority attribute is supported... " >&6; } + cat confdefs.h - <<_ACEOF >conftest.$ac_ext +/* end confdefs.h. */ + +int +main () +{ + + #if ! __has_attribute(init_priority) + #error init_priority not supported + #endif + + ; + return 0; +} +_ACEOF +if ac_fn_cxx_try_compile "$LINENO"; then : + ac_init_priority=yes +else + ac_init_priority=no +fi +rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext + if test "$ac_init_priority" = yes; then + +cat >>confdefs.h <<_ACEOF +#define _GLIBCXX_USE_INIT_PRIORITY_ATTRIBUTE 1 +_ACEOF + + fi + { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_init_priority" >&5 +$as_echo "$ac_init_priority" >&6; } + + ac_ext=c +ac_cpp='$CPP $CPPFLAGS' +ac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5' +ac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5' +ac_compiler_gnu=$ac_cv_c_compiler_gnu + + + # Define documentation rules conditionally. # See if makeinfo has been installed and is modern enough diff --git a/libstdc++-v3/configure.ac b/libstdc++-v3/configure.ac index df01f58bd83..91b3c1f32f2 100644 --- a/libstdc++-v3/configure.ac +++ b/libstdc++-v3/configure.ac @@ -541,6 +541,9 @@ GLIBCXX_ZONEINFO_DIR # For src/c++11/shared_ptr.cc alignment. GLIBCXX_CHECK_ALIGNAS_CACHELINE +# For using init_priority in ios_init.cc +GLIBCXX_CHECK_INIT_PRIORITY + # 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..0abf7445f58 100644 --- a/libstdc++-v3/include/std/iostream +++ b/libstdc++-v3/include/std/iostream @@ -75,7 +75,8 @@ _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/globals_io.cc). -#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) . // This needs to be done in the same TU that defines the stream objects. -#if __has_attribute(init_priority) +#if _GLIBCXX_USE_INIT_PRIORITY_ATTRIBUTE static ios_base::Init __ioinit __attribute__((init_priority(90))); #endif