Bug 27122 - ld -r does not complain about LTO/non-LTO mix and silently produces invalid object files.
Summary: ld -r does not complain about LTO/non-LTO mix and silently produces invalid o...
Status: NEW
Alias: None
Product: binutils
Classification: Unclassified
Component: ld (show other bugs)
Version: 2.36
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-12-28 13:40 UTC by Sergei Trofimovich
Modified: 2023-08-07 09:48 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed: 2020-12-28 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
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