Bug 89896 - -flto=4 is confused by presence of 'all.c' in a local directory. -flto=1 is not.
Summary: -flto=4 is confused by presence of 'all.c' in a local directory. -flto=1 is not.
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: lto (show other bugs)
Version: 8.3.0
: P3 normal
Target Milestone: ---
Assignee: Richard Biener
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2019-03-31 09:43 UTC by Sergei Trofimovich
Modified: 2019-04-03 08:47 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work: 7.4.1, 8.3.1, 9.0
Known to fail: 7.4.0, 8.3.0
Last reconfirmed: 2019-04-01 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Sergei Trofimovich 2019-03-31 09:43:52 UTC
The bug is originally discovered by Fabio Coatti in https://bugs.gentoo.org/682018.

There isl-0.21 fails to link when built with  CFLAGS="-O2 -flto=4 -pipe" LDFLAGS="${CFLAGS}". The trigger is all.c file name in isl source three. It gets translated to all.o.

Here is the minimal reproducer:

  $ cat foo.bash
  #!/bin/bash

  export LANG=C

  cc=gcc

  mk() {
    local flag=$1

    rm -rf -- *.o *.so*

    ${cc} ${flag} -fPIC -c all.c -o all.o
    ${cc} ${flag} -fPIC -c isl.c -o isl.o
    # Note: all.o is not used here and yet in interferes with -flto=4
    ${cc} ${flag} -fPIC -shared isl.o -o libisl.so
  }

  # empty files are enough to trigger a bug
  touch all.c isl.c

  echo "=== -flto=1 (works) ==="
  mk "-flto=1"

  echo "=== -flto=4 (fails) ==="
  mk "-flto=4"

Running:

  $ ./foo.bash
  === -flto=1 (works) ===
  === -flto=4 (fails) ===
  /usr/lib/gcc/x86_64-pc-linux-gnu/8.3.0/../../../../x86_64-pc-linux-gnu/bin/ld: /usr/lib/gcc/x86_64-pc-linux-gnu/8.3.0/../../../../lib64/../lib64/Scrt1.o: in function `_start':
/usr/src/debug/sys-libs/glibc-2.29-r1/glibc-2.29/csu/../sysdeps/x86_64/start.S:104: undefined reference to `main'
  collect2: error: ld returned 1 exit status
  make: *** [<builtin>: all] Error 1
  lto-wrapper: fatal error: make returned 2 exit status
compilation terminated.
  /usr/lib/gcc/x86_64-pc-linux-gnu/8.3.0/../../../../x86_64-pc-linux-gnu/bin/ld: error: lto-wrapper failed
  collect2: error: ld returned 1 exit status

Note: all.o is never passed to gcc as an input. And yet it's presence interferes with the driver.
Comment 1 Sergei Trofimovich 2019-03-31 10:36:32 UTC
It seems that 'all.c' is actual trigger, not 'all.o'.
Comment 2 Martin Liška 2019-04-01 07:07:12 UTC
Confirmed, minimal reproducer:

$ echo "" | gcc  -shared -fPIC -flto=2 -x c -
/usr/lib64/gcc/x86_64-suse-linux/8/../../../../x86_64-suse-linux/bin/ld: /usr/lib64/gcc/x86_64-suse-linux/8/../../../../lib64/../lib64/crt1.o: in function `_start':
/home/abuild/rpmbuild/BUILD/glibc-2.29/csu/../sysdeps/x86_64/start.S:104: undefined reference to `main'
collect2: error: ld returned 1 exit status
make: *** [<builtin>: all] Error 1
lto-wrapper: fatal error: make returned 2 exit status
compilation terminated.
/usr/lib64/gcc/x86_64-suse-linux/8/../../../../x86_64-suse-linux/bin/ld: error: lto-wrapper failed
collect2: error: ld returned 1 exit status

Looks to me a LD issue.
@H.J. can you please take a look?
Comment 3 Richard Biener 2019-04-01 12:09:08 UTC
It seems to be make looking for all.c via the LTRANS makefile and us
invoking make -f /tmp/ccfGbAMc.mk -j2 all

/tmp/cczwBPXD.ltrans0.ltrans.o:
        @gcc-8  '-xlto' '-c' '-fno-openmp' '-fno-openacc' '-mtune=generic' '-march=x86-64' '-fPIC' '-shared' '-fPIC' '-v' '-save-temps' '-mtune=generic' '-march=x86-64' '-fltrans-output-list=/tmp/cczwBPXD.ltrans.out' '-fltrans' '-o' '/tmp/cczwBPXD.ltrans0.ltrans.o' '/tmp/cczwBPXD.ltrans0.o'
all: \
        /tmp/cczwBPXD.ltrans0.ltrans.o

which eventually leads to

4924  execve("/usr/bin/cc", ["cc", "all.c", "/tmp/cczwBPXD.ltrans0.ltrans.o", "-o", "all"], [/* 107 vars */]) = 0

if you make all.c invalid C then you see

> gcc-8  -shared -fPIC -flto=2 -x c t.c 
all.c:1:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ at end of input
 invalid
 ^~~~~~~
make: *** [all] Error 1
lto-wrapper: fatal error: make returned 2 exit status
compilation terminated.
Comment 4 Richard Biener 2019-04-01 12:14:05 UTC
Index: gcc/lto-wrapper.c
===================================================================
--- gcc/lto-wrapper.c   (revision 270053)
+++ gcc/lto-wrapper.c   (working copy)
@@ -1671,7 +1671,7 @@ cont:
              int j = ltrans_priorities[i*2 + 1];
              fprintf (mstream, " \\\n\t%s", output_names[j]);
            }
-         fprintf (mstream, "\n");
+         fprintf (mstream, "\n\t@:\n");
          fclose (mstream);
          if (!jobserver)
            {

fixes this, adding an empty rule to the all target:

/tmp/ccZYd6M0.ltrans0.ltrans.o:
        @gcc-8  '-xlto' '-c' '-fno-openmp' '-fno-openacc' '-mtune=generic' '-march=x86-64' '-fPIC' '-shared' '-fPIC' '-B' '/abuild/rguenther/trunk2-g/gcc' '-v' '-save-temps' '-mtune=generic' '-march=x86-64' '-fltrans-output-list=/tmp/ccZYd6M0.ltrans.out' '-fltrans' '-o' '/tmp/ccZYd6M0.ltrans0.ltrans.o' '/tmp/ccZYd6M0.ltrans0.o'
all: \
        /tmp/ccZYd6M0.ltrans0.ltrans.o
        @:
Comment 5 H.J. Lu 2019-04-01 12:28:49 UTC
$ gcc -shared -fPIC -flto=4 -o x.so isl.o -v
...
make -f /tmp/ccj2DsQs.mk -j4 all
                             ^^^
...
/usr/local/bin/ld: /usr/lib/gcc/x86_64-redhat-linux/8/../../../../lib64/../lib64/crt1.o: in function `_start':
(.text+0x24): undefined reference to `main'
collect2: error: ld returned 1 exit status
make: *** [<builtin>: all] Error 1
lto-wrapper: fatal error: make returned 2 exit status

Since all.c matches the target "all", make tries to create "all".
Comment 6 Andreas Schwab 2019-04-01 12:42:51 UTC
Just make the target .PHONY.
Comment 7 Richard Biener 2019-04-03 08:46:31 UTC
Author: rguenth
Date: Wed Apr  3 08:46:00 2019
New Revision: 270112

URL: https://gcc.gnu.org/viewcvs?rev=270112&root=gcc&view=rev
Log:
2019-04-03  Richard Biener  <rguenther@suse.de>

	PR lto/89896
	* lto-wrapper.c (run_gcc): Avoid implicit rules making
	the all target phony.

Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/lto-wrapper.c
Comment 8 Richard Biener 2019-04-03 08:47:07 UTC
Author: rguenth
Date: Wed Apr  3 08:46:35 2019
New Revision: 270113

URL: https://gcc.gnu.org/viewcvs?rev=270113&root=gcc&view=rev
Log:
2019-04-03  Richard Biener  <rguenther@suse.de>

	PR lto/89896
	* lto-wrapper.c (run_gcc): Avoid implicit rules making
	the all target phony.

Modified:
    branches/gcc-8-branch/gcc/ChangeLog
    branches/gcc-8-branch/gcc/lto-wrapper.c
Comment 9 Richard Biener 2019-04-03 08:47:46 UTC
Fixed.
Comment 10 Richard Biener 2019-04-03 08:47:51 UTC
Author: rguenth
Date: Wed Apr  3 08:47:19 2019
New Revision: 270114

URL: https://gcc.gnu.org/viewcvs?rev=270114&root=gcc&view=rev
Log:
2019-04-03  Richard Biener  <rguenther@suse.de>

	PR lto/89896
	* lto-wrapper.c (run_gcc): Avoid implicit rules making
	the all target phony.

Modified:
    branches/gcc-7-branch/gcc/ChangeLog
    branches/gcc-7-branch/gcc/lto-wrapper.c