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: Wed, 11 Nov 2020 02:29:05 +0000	[thread overview]
Message-ID: <bug-97771-4-HtYP1HDcro@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 #5 from Erez Strauss <erstrauss at gmail dot com> ---
Yes, thanks, the asm() works - but if it can be expressed in C++, why add the
dependency on assembly?

1. attribute constructor - fails to compile
2. placing the address into the .init_array fails to generate the proper code.
3. assembly is required for gcc, but not for clang.

I minimized the program by avoiding any include files, having three different
macros for the different options.
I would expect that gcc will handle correctly the first two options, as other
compilers do.

See https://godbolt.org/z/Mr1794  - for the three compilers: gcc clang icc

========================================
$ cat init_array_minimized.cpp

extern "C" int           write(int, const char *, long);
extern "C" unsigned long strlen(const char *);
extern "C" int           snprintf(char *, unsigned long, const char *, ...);

#define SIMPLE_LOCAL_FUNC_INIT_ARRAY()                                         
     \
    do                                                                         
     \
    {                                                                          
     \
        struct Local                                                           
     \
        {                                                                      
     \
            static void init()                                                 
     \
            {                                                                  
     \
                char buffer[256];                                              
     \
                snprintf(buffer, sizeof(buffer), "%p: %s:%d: %s\n",
(void*)&init, __FILE__, __LINE__,  \
                         __PRETTY_FUNCTION__);                                 
     \
                write(1, buffer, strlen(buffer));                              
     \
            }                                                                  
     \
        };                                                                     
     \
        static void *volatile initp __attribute__((__used__,
section(".init_array"))){(void *)&Local::init}; \
    } while (0)

#define SIMPLE_LOCAL_FUNC_CONSTRUCTOR()                                        
     \
    do                                                                         
     \
    {                                                                          
     \
        struct Local                                                           
     \
        {                                                                      
     \
            static void init() __attribute__((constructor))                    
     \
            {                                                                  
     \
                char buffer[256];                                              
     \
                snprintf(buffer, sizeof(buffer), "%p: %s:%d: %s\n",
(void*)&init, __FILE__, __LINE__,  \
                         __PRETTY_FUNCTION__);                                 
     \
                write(1, buffer, strlen(buffer));                              
     \
            }                                                                  
     \
        };                                                                     
     \
    } while (0)

#define SIMPLE_LOCAL_FUNC_INIT_ASM()                                           
     \
    do                                                                         
     \
    {                                                                          
     \
        struct Local                                                           
     \
        {                                                                      
     \
            static void init()                                                 
     \
            {                                                                  
     \
                char buffer[256];                                              
     \
                snprintf(buffer, sizeof(buffer), "%p: %s:%d: %s\n",
(void*)&init, __FILE__, __LINE__,  \
                         __PRETTY_FUNCTION__);                                 
     \
                write(1, buffer, strlen(buffer));                              
     \
            }                                                                  
     \
        };                                                                     
     \
  __asm (".section .init_array, \"aw\",%%init_array; .balign %P0; .%P0byte %P1;
.previous" : : "i" (sizeof (void *)), "g" ((void*)&Local::init)); \
    } while (0)




//#define LOCAL_INIT_FUNC() SIMPLE_LOCAL_FUNC_INIT_ARRAY()
//#define LOCAL_INIT_FUNC() SIMPLE_LOCAL_FUNC_CONSTRUCTOR()
#define LOCAL_INIT_FUNC() SIMPLE_LOCAL_FUNC_INIT_ASM()



void funcA() {
        LOCAL_INIT_FUNC();
        LOCAL_INIT_FUNC();
}

inline void funcB() { LOCAL_INIT_FUNC(); }

template<int> void funcT(int) { LOCAL_INIT_FUNC(); }

int main()
{
    write(1, "main\n", 5);
    funcA();
    funcB();
    funcT<0>(0);
    funcT<1>(0);
    return 0;
}
==================================

$ g++ -std=c++20  -O2 -fverbose-asm --save-temps -o init_array_minimized
init_array_minimized.cpp
$ ./init_array_minimized 
0x4011e0: init_array_minimized.cpp:63: static void funcA()::Local::init()
0x401180: init_array_minimized.cpp:64: static void funcA()::Local::init()
0x401250: init_array_minimized.cpp:67: static void funcB()::Local::init()
0x4012b0: init_array_minimized.cpp:69: static void funcT(int)::Local::init()
[with int <anonymous> = 0]
0x401310: init_array_minimized.cpp:69: static void funcT(int)::Local::init()
[with int <anonymous> = 1]
main

  parent reply	other threads:[~2020-11-11  2:29 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
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 [this message]
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-HtYP1HDcro@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: 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).