public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/108076] New: GCC with -O3 produces code which fails to link
@ 2022-12-12 14:53 martid0311 at gmail dot com
  2022-12-12 14:54 ` [Bug c/108076] " martid0311 at gmail dot com
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: martid0311 at gmail dot com @ 2022-12-12 14:53 UTC (permalink / raw)
  To: gcc-bugs

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)

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2023-07-07 10:06 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-12-12 14:53 [Bug c/108076] New: GCC with -O3 produces code which fails to link martid0311 at gmail dot com
2022-12-12 14:54 ` [Bug c/108076] " martid0311 at gmail dot com
2022-12-12 15:15 ` [Bug tree-optimization/108076] [10/11/12/13 Regression] " amonakov at gcc dot gnu.org
2022-12-12 16:19 ` pinskia at gcc dot gnu.org
2022-12-12 16:30 ` jakub at gcc dot gnu.org
2022-12-12 16:38 ` rguenth at gcc dot gnu.org
2022-12-13 10:41 ` cvs-commit at gcc dot gnu.org
2022-12-13 10:56 ` [Bug tree-optimization/108076] [10/11/12 " rguenth at gcc dot gnu.org
2023-01-24 14:27 ` cvs-commit at gcc dot gnu.org
2023-05-02 12:03 ` [Bug tree-optimization/108076] [10/11 " cvs-commit at gcc dot gnu.org
2023-07-07 10:06 ` [Bug tree-optimization/108076] [10 " rguenth at gcc dot gnu.org

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).