https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108076
Bug ID: 108076 Summary: GCC with -O3 produces code which fails to link Product: gcc Version: 11.3.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: martid0311 at gmail dot com Target Milestone: --- Created attachment 54074 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=54074&action=edit The source code which fails to compile The attached source file ("compiler-output.c") fails to link with GCC with -O3 (compiling for x86_64). I've tested it using "gcc (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0", but someone on IRC has confirmed that it's also an issue on "gcc (Debian 12.2.0-9.1) 12.2.0". Running `gcc -O3 compiler-output.c` fails with this set of errors: /usr/bin/ld: /tmp/ccgWgyyV.o: warning: relocation against `.L8' in read-only section `.text.startup' /usr/bin/ld: /tmp/ccgWgyyV.o: in function `main': gnuc-compiler-output.c:(.text.startup+0x2f): undefined reference to `.L7' /usr/bin/ld: gnuc-compiler-output.c:(.text.startup+0x36): undefined reference to `.L15' /usr/bin/ld: gnuc-compiler-output.c:(.text.startup+0x48): undefined reference to `.L5' /usr/bin/ld: gnuc-compiler-output.c:(.text.startup+0x5a): undefined reference to `.L6' /usr/bin/ld: gnuc-compiler-output.c:(.text.startup+0x6a): undefined reference to `.L8' /usr/bin/ld: gnuc-compiler-output.c:(.text.startup+0x76): undefined reference to `.L9' /usr/bin/ld: gnuc-compiler-output.c:(.text.startup+0x90): undefined reference to `.L10' /usr/bin/ld: gnuc-compiler-output.c:(.text.startup+0x9b): undefined reference to `.L11' /usr/bin/ld: gnuc-compiler-output.c:(.text.startup+0xa9): undefined reference to `.L12' /usr/bin/ld: gnuc-compiler-output.c:(.text.startup+0xb5): undefined reference to `.L13' /usr/bin/ld: gnuc-compiler-output.c:(.text.startup+0xcf): undefined reference to `.L14' /usr/bin/ld: warning: creating DT_TEXTREL in a PIE collect2: error: ld returned 1 exit status I've looked at the generated assembly code (using `gcc -S`), and sure enough, the main function contains code like "leaq .L15(%rip), %rdx" and "leaq .L5(%rip), %rax" even though there's no label called .L15 or .L5. The source code contains a label jump table which gets initialized like this: static void *jmp_targets[62]; int main() { jmp_targets[0] = &&instr_0; jmp_targets[1] = &&instr_1; jmp_targets[2] = &&instr_2; jmp_targets[7] = &&instr_7; ... If we change the code to use an initializer list it works: int main() { void *jmp_targets[] = { [0] = &&instr_0, [1] = &&instr_1, [2] = &&instr_2, [7] = &&instr_7, ... Interestingly, it also works if we compile with `-O3 -flto`. Note: the code will segfault unless you provide it command line arguments. If you want to try to run it, run something like `./a.out 5 10`. It will print the sum of the two numbers. Output of `gcc -v`: Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/11/lto-wrapper OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa OFFLOAD_TARGET_DEFAULT=1 Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Ubuntu 11.3.0-1ubuntu1~22.04' --with-bugurl=file:///usr/share/doc/gcc-11/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++,m2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-11 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib --enable-libphobos-checking=release --with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch --disable-werror --enable-cet --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none=/build/gcc-11-xKiWfi/gcc-11-11.3.0/debian/tmp-nvptx/usr,amdgcn-amdhsa=/build/gcc-11-xKiWfi/gcc-11-11.3.0/debian/tmp-gcn/usr --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu --with-build-config=bootstrap-lto-lean --enable-link-serialization=2 Thread model: posix Supported LTO compression algorithms: zlib zstd gcc version 11.3.0 (Ubuntu 11.3.0-1ubuntu1~22.04)