public inbox for gcc-bugs@sourceware.org help / color / mirror / Atom feed
From: "erstrauss at gmail dot com" <gcc-bugzilla@gcc.gnu.org> To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/97771] gcc/g++ failed to generate proper .init_array entries for local scope function, should create "axG", .init_array comdat Date: Tue, 10 Nov 2020 13:27:46 +0000 [thread overview] Message-ID: <bug-97771-4-pnRanC2RFe@http.gcc.gnu.org/bugzilla/> (raw) In-Reply-To: <bug-97771-4@http.gcc.gnu.org/bugzilla/> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97771 --- Comment #2 from Erez Strauss <erstrauss at gmail dot com> --- Thanks, I tried, it fails with: $ g++ -O2 -std=c++20 init_array5c.cpp -o init_array5c init_array5c.cpp: In function ‘void localFunc(const char*)’: init_array5c.cpp:17:55: warning: ‘constructor’ attribute ignored [-Wattributes] 17 | static void init() __attribute__((constructor)) | ^ =========================== $ ./init_array5c main() started post-main-start: X1 init_array5c.cpp 37 &count: 0x4041d4 count: 1 void localFunc(const char*) [with const std::experimental::fundamentals_v2::source_location& X = s] post-main-start: X2 init_array5c.cpp 38 &count: 0x4041dc count: 1 void localFunc(const char*) [with const std::experimental::fundamentals_v2::source_location& X = s] =========================== clang++ accepts correctly the attribute: $ clang++ -O2 -std=c++20 init_array5c.cpp -o init_array5c =========================== $ ./init_array5c pre-main-start init from localFunc: init_array5c.cpp 37 &count: 0x4041d8 count: 1 okFunc1 / static void localFunc(const char *)::Local::init() pre-main-start init from localFunc: init_array5c.cpp 38 &count: 0x4041dc count: 1 okFunc2 / static void localFunc(const char *)::Local::init() main() started post-main-start: X1 init_array5c.cpp 37 &count: 0x4041d8 count: 2 void localFunc(const char *) [X = s] post-main-start: X2 init_array5c.cpp 38 &count: 0x4041dc count: 2 void localFunc(const char *) [X = s] =========================== $ cat init_array5c.cpp #include <experimental/source_location> #include <iostream> using namespace std::experimental; template<const source_location& X> void localFunc(const char* name) { static int count{0}; volatile ::std::ios_base::Init dummy{}; std::cout << "post-main-start: " << name << " " << X.file_name() << " " << X.line() << " &count: " << (void*)&count << " count: " << (++count) << " " << __PRETTY_FUNCTION__ << std::endl; struct Local { static void init() __attribute__((constructor)) { volatile ::std::ios_base::Init dummy{}; std::cout << "pre-main-start init from localFunc: " << X.file_name() << " " << X.line() << " &count: " << (void*)&count << " count: " << (++count) << " " << X.function_name() << " / " << __PRETTY_FUNCTION__ << std::endl; } }; //static void* volatile initp __attribute__((__used__, section(".init_array"))){(void*)&Local::init}; } #define LOCAL_FUNC(NAME) \ do \ { \ constexpr static auto s = source_location::current(); \ localFunc<s>((const char*)&(NAME)[0]); \ } while (0) void okFunc1() { LOCAL_FUNC("X1"); } inline void okFunc2() { LOCAL_FUNC("X2"); } int main() { std::cout << "main() started" << std::endl; okFunc1(); okFunc2(); return 0; }
next prev parent reply other threads:[~2020-11-10 13:27 UTC|newest] Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top 2020-11-10 4:55 [Bug c++/97771] New: " erstrauss at gmail dot com 2020-11-10 8:16 ` [Bug c++/97771] " rguenth at gcc dot gnu.org 2020-11-10 13:27 ` erstrauss at gmail dot com [this message] 2020-11-10 14:31 ` jakub at gcc dot gnu.org 2020-11-10 14:43 ` jakub at gcc dot gnu.org 2020-11-11 2:29 ` erstrauss at gmail dot com 2021-08-17 7:47 ` pinskia at gcc dot gnu.org 2021-08-29 2:45 ` erstrauss at gmail dot com 2021-08-29 3:12 ` pinskia at gcc dot gnu.org
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: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=bug-97771-4-pnRanC2RFe@http.gcc.gnu.org/bugzilla/ \ --to=gcc-bugzilla@gcc.gnu.org \ --cc=gcc-bugs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe 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).