https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71384
Bug ID: 71384 Summary: Global constructors (init_array) emitted for trivial initialisation depending on source code ordering Product: gcc Version: 5.3.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: niels at penneman dot org Target Milestone: --- GCC versions 4.7.2/5.3.0/5.3.1 are emitting a global constructor (to the .init_array section) for the following piece of trivial code: === struct X { constexpr X(int a); const int m_a; }; X x(999); inline constexpr X::X(int a):m_a(a) {} === Swapping the last two lines, i.e. placing the constructor implementation before the declaration of 'x', causes 'x' to be emitted to the '.data' section with value 999 instead. The latter is desirable as the generate binary is far smaller and does not require global constructors. How to reproduce: g++ -std=c++11 -c -save-temps=obj global-constructor.cxx Occurs regardless of whether you specify -O0, -O1, -O2, -O3. In a complete program, -flto cannot resolve this. The issue occurs on both x86_64 and ARM, so not in the backend. Also occurs with -std=c++14 and -std=c++17 on GCC 5.3.x. Compiler versions used: $ g++ -### Using built-in specs. COLLECT_GCC=g++ COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/5.3.1/lto-wrapper Target: x86_64-redhat-linux Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,objc,obj-c++,fortran,ada,go,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-linker-hash-style=gnu --enable-plugin --enable-initfini-array --disable-libgcj --with-isl --enable-libmpx --enable-gnu-indirect-function --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux Thread model: posix gcc version 5.3.1 20160406 (Red Hat 5.3.1-6) (GCC) $ ${CROSS_COMPILE}g++ -### Using built-in specs. COLLECT_GCC=/home/niels/workspace/toolchains/build/arm-none-eabi/output/bin/arm-none-eabi-g++ COLLECT_LTO_WRAPPER=/home/niels/workspace/toolchains/build/arm-none-eabi/output/libexec/gcc/arm-none-eabi/5.3.0/lto-wrapper Target: arm-none-eabi Configured with: /home/niels/workspace/toolchains/build/arm-none-eabi/work/src/gcc-5.3.0/configure --build=x86_64-build_pc-linux-gnu --host=x86_64-build_pc-linux-gnu --target=arm-none-eabi --prefix=/home/niels/workspace/toolchains/build/arm-none-eabi/output --with-local-prefix=/home/niels/workspace/toolchains/build/arm-none-eabi/output/arm-none-eabi/sysroot --with-sysroot=/home/niels/workspace/toolchains/build/arm-none-eabi/output/arm-none-eabi/sysroot --with-newlib --enable-threads=no --disable-shared --with-float=soft --with-pkgversion='crosstool-NG crosstool-ng-1.22.0-134-ge1d494a' --enable-__cxa_atexit --disable-libgomp --disable-libmudflap --disable-libssp --disable-libquadmath --disable-libquadmath-support --with-gmp=/home/niels/workspace/toolchains/build/arm-none-eabi/work/arm-none-eabi/buildtools --with-mpfr=/home/niels/workspace/toolchains/build/arm-none-eabi/work/arm-none-eabi/buildtools --with-mpc=/home/niels/workspace/toolchains/build/arm-none-eabi/work/arm-none-eabi/buildtools --with-isl=/home/niels/workspace/toolchains/build/arm-none-eabi/work/arm-none-eabi/buildtools --enable-lto --with-host-libstdcxx='-static-libgcc -Wl,-Bstatic,-lstdc++ -lm' --enable-target-optspace --disable-nls --disable-multilib --enable-languages=c,c++ Thread model: single gcc version 5.3.0 (crosstool-NG crosstool-ng-1.22.0-134-ge1d494a) $ ${CROSS_COMPILE}g++ -### Using built-in specs. COLLECT_GCC=/opt/OSELAS.Toolchain-2012.12.1/arm-v5te-linux-gnueabi/gcc-4.7.2-glibc-2.16.0-binutils-2.22-kernel-3.6-sanitized/bin/arm-v5te-linux-gnueabi-g++ COLLECT_LTO_WRAPPER=/opt/OSELAS.Toolchain-2012.12.1/arm-v5te-linux-gnueabi/gcc-4.7.2-glibc-2.16.0-binutils-2.22-kernel-3.6-sanitized/bin/../libexec/gcc/arm-v5te-linux-gnueabi/4.7.2/lto-wrapper Target: arm-v5te-linux-gnueabi Configured with: /home/mol/dude/tmp/OSELAS.Toolchain-2012.12.1/platform-arm-v5te-linux-gnueabi-gcc-4.7.2-glibc-2.16.0-binutils-2.22-kernel-3.6-sanitized/build-cross/gcc-4.7.2/configure --build=x86_64-host-linux-gnu --host=x86_64-host-linux-gnu --target=arm-v5te-linux-gnueabi --with-sysroot=/home/mol/dude/tmp/OSELAS.Toolchain-2012.12.1/inst/opt/OSELAS.Toolchain-2012.12.1/arm-v5te-linux-gnueabi/gcc-4.7.2-glibc-2.16.0-binutils-2.22-kernel-3.6-sanitized/sysroot-arm-v5te-linux-gnueabi --disable-multilib --with-float=soft --with-fpu=vfp --with-cpu=arm926ej-s --enable-__cxa_atexit --disable-sjlj-exceptions --disable-nls --disable-decimal-float --disable-fixed-point --disable-win32-registry --enable-symvers=gnu --with-pkgversion=OSELAS.Toolchain-2012.12.1 --enable-threads=posix --with-system-zlib --with-gmp=/home/mol/dude/tmp/OSELAS.Toolchain-2012.12.1/platform-arm-v5te-linux-gnueabi-gcc-4.7.2-glibc-2.16.0-binutils-2.22-kernel-3.6-sanitized/sysroot-host --with-mpfr=/home/mol/dude/tmp/OSELAS.Toolchain-2012.12.1/platform-arm-v5te-linux-gnueabi-gcc-4.7.2-glibc-2.16.0-binutils-2.22-kernel-3.6-sanitized/sysroot-host --prefix=/home/mol/dude/tmp/OSELAS.Toolchain-2012.12.1/inst/opt/OSELAS.Toolchain-2012.12.1/arm-v5te-linux-gnueabi/gcc-4.7.2-glibc-2.16.0-binutils-2.22-kernel-3.6-sanitized --enable-languages=c,c++ --enable-c99 --enable-long-long --enable-libstdcxx-debug --enable-profile --enable-shared --disable-libssp --enable-checking=release Thread model: posix gcc version 4.7.2 (OSELAS.Toolchain-2012.12.1)