https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97771

            Bug ID: 97771
           Summary: gcc/g++ failed to generate proper .init_array entries
                    for local scope function, should create "axG",
                    .init_array comdat
           Product: gcc
           Version: 10.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: erstrauss at gmail dot com
  Target Milestone: ---

The the program below creates a local function inside a Local structure, and
adds the
local function address into the .init_array section in order to call that local
function at process start.

The localFunc template function is using a source_location const static
variable as template parameter.


g++ fails to place the address of the Local::init() functions inside the
.init_array.


$ cat init_array5.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()
        {
            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};
  //**** not places into .init_array.
}

#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"); }
void okFunc2() { LOCAL_FUNC("X2"); }

int main()
{
    std::cout << "main() started" << std::endl;
    okFunc1();
    okFunc2();
    return 0;
}


==========================
output of the g++ compiled code, without the calls to the Local::init()
functions.

$ g++ -O2 -std=c++20 init_array5.cpp -o init_array5
$ ./init_array5
main() started
post-main-start: X1 init_array5.cpp 38 &count: 0x4041d8 count: 1 void
localFunc(const char*) [with const
std::experimental::fundamentals_v2::source_location& X = s]
post-main-start: X2 init_array5.cpp 39 &count: 0x4041d4 count: 1 void
localFunc(const char*) [with const
std::experimental::fundamentals_v2::source_location& X = s]


In case the same code is compiled using clang++, the pre-main local functions
are being called.

$ clang++ -O2 -std=c++20 init_array5.cpp -o init_array5
$ ./init_array5
pre-main-start init from localFunc: init_array5.cpp 38 &count: 0x4041d8 count:
1 okFunc1 / static void localFunc(const char *)::Local::init()
pre-main-start init from localFunc: init_array5.cpp 39 &count: 0x4041dc count:
1 okFunc2 / static void localFunc(const char *)::Local::init()
main() started
post-main-start: X1 init_array5.cpp 38 &count: 0x4041d8 count: 2 void
localFunc(const char *) [X = s]
post-main-start: X2 init_array5.cpp 39 &count: 0x4041dc count: 2 void
localFunc(const char *) [X = s]


g++ version information:
$ g++ --version -v
Using built-in specs.
COLLECT_AS_OPTIONS='--version'
COLLECT_GCC=/usr/bin/g++
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/10/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
g++ (GCC) 10.2.1 20201016 (Red Hat 10.2.1-6)
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap
--enable-languages=c,c++,fortran,objc,obj-c++,ada,go,d,lto --prefix=/usr
--mandir=/usr/share/man --infodir=/usr/share/info
--with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared
--enable-threads=posix --enable-checking=release --enable-multilib
--with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions
--enable-gnu-unique-object --enable-linker-build-id
--with-gcc-major-version-only --with-linker-hash-style=gnu --enable-plugin
--enable-initfini-array --with-isl --enable-offload-targets=nvptx-none
--without-cuda-driver --enable-gnu-indirect-function --enable-cet
--with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 10.2.1 20201016 (Red Hat 10.2.1-6) (GCC) 
COLLECT_GCC_OPTIONS='--version' '-v' '-shared-libgcc' '-mtune=generic'
'-march=x86-64'
 /usr/libexec/gcc/x86_64-redhat-linux/10/cc1 -quiet -v help-dummy -quiet
-dumpbase help-dummy -mtune=generic -march=x86-64 -auxbase help-dummy -version
--version -o /tmp/ccSrOtZU.s
GNU C17 (GCC) version 10.2.1 20201016 (Red Hat 10.2.1-6) (x86_64-redhat-linux)
        compiled by GNU C version 10.2.1 20201016 (Red Hat 10.2.1-6), GMP
version 6.2.0, MPFR version 4.1.0, MPC version 1.1.0, isl version
isl-0.16.1-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
COLLECT_GCC_OPTIONS='--version' '-v' '-shared-libgcc' '-mtune=generic'
'-march=x86-64'
 as -v --64 --version -o /tmp/ccPXlGcS.o /tmp/ccSrOtZU.s
GNU assembler version 2.35 (x86_64-redhat-linux) using BFD version version
2.35-14.fc33
GNU assembler version 2.35-14.fc33
Copyright (C) 2020 Free Software Foundation, Inc.
This program is free software; you may redistribute it under the terms of
the GNU General Public License version 3 or later.
This program has absolutely no warranty.
This assembler was configured for a target of `x86_64-redhat-linux'.
COMPILER_PATH=/usr/libexec/gcc/x86_64-redhat-linux/10/:/usr/libexec/gcc/x86_64-redhat-linux/10/:/usr/libexec/gcc/x86_64-redhat-linux/:/usr/lib/gcc/x86_64-redhat-linux/10/:/usr/lib/gcc/x86_64-redhat-linux/
LIBRARY_PATH=/usr/lib/gcc/x86_64-redhat-linux/10/:/usr/lib/gcc/x86_64-redhat-linux/10/../../../../lib64/:/lib/../lib64/:/usr/lib/../lib64/:/usr/lib/gcc/x86_64-redhat-linux/10/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='--version' '-v' '-shared-libgcc' '-mtune=generic'
'-march=x86-64'
 /usr/libexec/gcc/x86_64-redhat-linux/10/collect2 -plugin
/usr/libexec/gcc/x86_64-redhat-linux/10/liblto_plugin.so
-plugin-opt=/usr/libexec/gcc/x86_64-redhat-linux/10/lto-wrapper
-plugin-opt=-fresolution=/tmp/cc7OxEZS.res -plugin-opt=-pass-through=-lgcc_s
-plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lc
-plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lgcc --build-id
--no-add-needed --eh-frame-hdr --hash-style=gnu -m elf_x86_64 -dynamic-linker
/lib64/ld-linux-x86-64.so.2 --version
/usr/lib/gcc/x86_64-redhat-linux/10/../../../../lib64/crt1.o
/usr/lib/gcc/x86_64-redhat-linux/10/../../../../lib64/crti.o
/usr/lib/gcc/x86_64-redhat-linux/10/crtbegin.o
-L/usr/lib/gcc/x86_64-redhat-linux/10
-L/usr/lib/gcc/x86_64-redhat-linux/10/../../../../lib64 -L/lib/../lib64
-L/usr/lib/../lib64 -L/usr/lib/gcc/x86_64-redhat-linux/10/../../..
/tmp/ccPXlGcS.o -lgcc_s -lgcc -lc -lgcc_s -lgcc
/usr/lib/gcc/x86_64-redhat-linux/10/crtend.o
/usr/lib/gcc/x86_64-redhat-linux/10/../../../../lib64/crtn.o
collect2 version 10.2.1 20201016 (Red Hat 10.2.1-6)
/usr/bin/ld -plugin /usr/libexec/gcc/x86_64-redhat-linux/10/liblto_plugin.so
-plugin-opt=/usr/libexec/gcc/x86_64-redhat-linux/10/lto-wrapper
-plugin-opt=-fresolution=/tmp/cc7OxEZS.res -plugin-opt=-pass-through=-lgcc_s
-plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lc
-plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lgcc --build-id
--no-add-needed --eh-frame-hdr --hash-style=gnu -m elf_x86_64 -dynamic-linker
/lib64/ld-linux-x86-64.so.2 --version
/usr/lib/gcc/x86_64-redhat-linux/10/../../../../lib64/crt1.o
/usr/lib/gcc/x86_64-redhat-linux/10/../../../../lib64/crti.o
/usr/lib/gcc/x86_64-redhat-linux/10/crtbegin.o
-L/usr/lib/gcc/x86_64-redhat-linux/10
-L/usr/lib/gcc/x86_64-redhat-linux/10/../../../../lib64 -L/lib/../lib64
-L/usr/lib/../lib64 -L/usr/lib/gcc/x86_64-redhat-linux/10/../../..
/tmp/ccPXlGcS.o -lgcc_s -lgcc -lc -lgcc_s -lgcc
/usr/lib/gcc/x86_64-redhat-linux/10/crtend.o
/usr/lib/gcc/x86_64-redhat-linux/10/../../../../lib64/crtn.o
GNU ld version 2.35-14.fc33
Copyright (C) 2020 Free Software Foundation, Inc.
This program is free software; you may redistribute it under the terms of
the GNU General Public License version 3 or (at your option) a later version.
This program has absolutely no warranty.
COLLECT_GCC_OPTIONS='--version' '-v' '-shared-libgcc' '-mtune=generic'
'-march=x86-64'

Reply via email to