[Bug c++/97771] gcc/g++ failed to generate proper .init_array entries for local scope function, should create "axG", .init_array comdat
erstrauss at gmail dot com
gcc-bugzilla@gcc.gnu.org
Wed Nov 11 02:29:05 GMT 2020
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
More information about the Gcc-bugs
mailing list