Bug 27122

Summary: ld -r does not complain about LTO/non-LTO mix and silently produces invalid object files.
Product: binutils Reporter: Sergei Trofimovich <slyich>
Component: ldAssignee: Not yet assigned to anyone <unassigned>
Status: NEW ---    
Severity: normal CC: hjl.tools, sam
Priority: P2    
Version: 2.36   
Target Milestone: ---   
See Also: https://github.com/gentoo-haskell/gentoo-haskell/issues/1110
https://sourceware.org/bugzilla/show_bug.cgi?id=12291
Host: Target:
Build: Last reconfirmed: 2020-12-28 00:00:00

Description Sergei Trofimovich 2020-12-28 13:40:20 UTC
Re-discovered in https://github.com/gentoo-haskell/gentoo-haskell/issues/1110.

PR12291 tracks support for mixed LTO/non-LTO objects. While it's not implemented should ld complain about data loss when partial linking is used? gcc's man page suggest it should but I'm not sure if it's ld's problem or gcc's linker plugin problem.

gcc's manpage says:

```
       -flinker-output=type

           During the incremental link (by -r) the linker plugin defaults to rel. With current interfaces to GNU Binutils it is however not possible to incrementally link LTO objects and
           non-LTO objects into a single mixed object file.  If any of object files in incremental link cannot be used for link-time optimization, the linker plugin issues a warning and
           uses nolto-rel. To maintain whole program optimization, it is recommended to link such objects into static library instead. Alternatively it is possible to use H.J. Lu's
           binutils with support for mixed objects.
```

Example session (models upstream bug):

$ cat a.c
  void a(void) {}

$ cat b.c
  void a(void); /* from non-lto */
  int main(void) { a(); }

$ LANG=C bash -x ./mk.bash
+ x86_64-pc-linux-gnu-gcc-11.0.0 -c a.c
+ x86_64-pc-linux-gnu-gcc-11.0.0 -flto -c b.c
+ x86_64-pc-linux-gnu-gcc-11.0.0 a.o b.o -o o1
+ x86_64-pc-linux-gnu-ld -r a.o b.o -o o.o
+ x86_64-pc-linux-gnu-gcc-11.0.0 -flto o.o -o o2
/usr/lib/gcc/x86_64-pc-linux-gnu/11.0.0/../../../../x86_64-pc-linux-gnu/bin/ld: /tmp/ccHmkp4T.ltrans0.ltrans.o: in function `main':
<artificial>:(.text+0x5): undefined reference to `a'
collect2: error: ld returned 1 exit status

Note: `x86_64-pc-linux-gnu-ld -r a.o b.o -o o.o` issued no warnings.

Thanks!
Comment 1 H.J. Lu 2020-12-28 13:56:00 UTC
Please provide feedbacks for

https://sourceware.org/pipermail/binutils/2020-October/113795.html