Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[std module] Building std module fails with _FORTIFY_SOURCE #121709

Open
nmlgc opened this issue Jan 5, 2025 · 4 comments
Open

[std module] Building std module fails with _FORTIFY_SOURCE #121709

nmlgc opened this issue Jan 5, 2025 · 4 comments
Labels
clang:modules C++20 modules and Clang Header Modules libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.

Comments

@nmlgc
Copy link

nmlgc commented Jan 5, 2025

Tested on a relatively current trunk, but exists since at least Clang 18:

clang version 20.0.0 (/home/nmlgc/aur/llvm-git/llvm-project 743c84bb9b79ed70d9bed926c2a173db3b30f587)
Target: x86_64-pc-linux-gnu
Thread model: posix
$ clang++ -O -D_FORTIFY_SOURCE -stdlib=libc++ -std=c++2c --precompile -Wno-reserved-module-identifier /usr/share/libc++/v1/std.cppm
In file included from /usr/share/libc++/v1/std.cppm:229:
/usr/share/libc++/v1/std/cstdio.inc:47:14: error: using declaration referring to 'snprintf' with internal linkage cannot be exported
   47 |   using std::snprintf _LIBCPP_USING_IF_EXISTS;
      |              ^
/usr/include/bits/stdio2.h:75:8: note: target of using declaration
   75 | __NTH (snprintf (__fortify_clang_overload_arg (char *, __restrict, __s),
      |        ^
In file included from /usr/share/libc++/v1/std.cppm:229:
/usr/share/libc++/v1/std/cstdio.inc:48:14: error: using declaration referring to 'sprintf' with internal linkage cannot be exported
   48 |   using std::sprintf _LIBCPP_USING_IF_EXISTS;
      |              ^
/usr/include/bits/stdio2.h:37:8: note: target of using declaration
   37 | __NTH (sprintf (__fortify_clang_overload_arg (char *, __restrict, __s),
      |        ^
In file included from /usr/share/libc++/v1/std.cppm:234:
/usr/share/libc++/v1/std/cwchar.inc:30:14: error: using declaration referring to 'swprintf' with internal linkage cannot be exported
   30 |   using std::swprintf _LIBCPP_USING_IF_EXISTS;
      |              ^
/usr/include/bits/wchar2.h:181:8: note: target of using declaration
  181 | __NTH (swprintf (__fortify_clang_overload_arg (wchar_t *, __restrict, __s),
      |        ^
3 errors generated.

_FORTIFY_SOURCE is part of the default CFLAGS of (at least) Arch Linux during package builds via makepkg, and would need to be manually removed to work with modules.

@llvmbot llvmbot added the clang Clang issues not falling into any other category label Jan 5, 2025
@EugeneZelenko EugeneZelenko added clang:modules C++20 modules and Clang Header Modules and removed clang Clang issues not falling into any other category labels Jan 6, 2025
@llvmbot
Copy link
Member

llvmbot commented Jan 6, 2025

@llvm/issue-subscribers-clang-modules

Author: Nmlgc (nmlgc)

Tested on a relatively current trunk, but exists since at least Clang 18:
clang version 20.0.0 (/home/nmlgc/aur/llvm-git/llvm-project 743c84bb9b79ed70d9bed926c2a173db3b30f587)
Target: x86_64-pc-linux-gnu
Thread model: posix
$ clang++ -O -D_FORTIFY_SOURCE -stdlib=libc++ -std=c++2c --precompile -Wno-reserved-module-identifier /usr/share/libc++/v1/std.cppm
In file included from /usr/share/libc++/v1/std.cppm:229:
/usr/share/libc++/v1/std/cstdio.inc:47:14: error: using declaration referring to 'snprintf' with internal linkage cannot be exported
   47 |   using std::snprintf _LIBCPP_USING_IF_EXISTS;
      |              ^
/usr/include/bits/stdio2.h:75:8: note: target of using declaration
   75 | __NTH (snprintf (__fortify_clang_overload_arg (char *, __restrict, __s),
      |        ^
In file included from /usr/share/libc++/v1/std.cppm:229:
/usr/share/libc++/v1/std/cstdio.inc:48:14: error: using declaration referring to 'sprintf' with internal linkage cannot be exported
   48 |   using std::sprintf _LIBCPP_USING_IF_EXISTS;
      |              ^
/usr/include/bits/stdio2.h:37:8: note: target of using declaration
   37 | __NTH (sprintf (__fortify_clang_overload_arg (char *, __restrict, __s),
      |        ^
In file included from /usr/share/libc++/v1/std.cppm:234:
/usr/share/libc++/v1/std/cwchar.inc:30:14: error: using declaration referring to 'swprintf' with internal linkage cannot be exported
   30 |   using std::swprintf _LIBCPP_USING_IF_EXISTS;
      |              ^
/usr/include/bits/wchar2.h:181:8: note: target of using declaration
  181 | __NTH (swprintf (__fortify_clang_overload_arg (wchar_t *, __restrict, __s),
      |        ^
3 errors generated.

_FORTIFY_SOURCE is part of the default CFLAGS of (at least) Arch Linux during package builds via makepkg, and would need to be manually removed to work with modules.

@ChuanqiXu9
Copy link
Member

From the error message, it looks this is a library issue. We indeed can't export declaration with internal linkage. Previously when we met such problems, we will refactor the source to declare functions from static inline to inline.

I think we need to contact with the providers of these functions to change the linkage of these functions.

Although this is not directly related to libc++, I add the libc++ tag to CC libc++ folks.

@ChuanqiXu9 ChuanqiXu9 added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Jan 6, 2025
@frederick-vs-ja
Copy link
Contributor

Seems because of that glibc incorrectly (?) marks some C standard libary functions static when compiling with Clang with _FORTIFY_SOURCE defined.

# define __fortify_function_error_function static __attribute__((__unused__))

Not sure whether we want workaround like that used in MSVC STL's <ctime>.

@ChuanqiXu9
Copy link
Member

I feel, on the one hand, we need to submit an issue report to glibc. On the other hand, giving glibc is rarely updated for most users, it is better to do some workarounds.

@ChuanqiXu9 ChuanqiXu9 changed the title [clang] Precompilation of std module fails with _FORTIFY_SOURCE [std module] Building std module fails with _FORTIFY_SOURCE Jan 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:modules C++20 modules and Clang Header Modules libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.
Projects
None yet
Development

No branches or pull requests

5 participants