Bug 2119380

Summary: binutils not working correctly with "clang -flto"
Product: Red Hat Enterprise Linux 8 Reporter: Jesus Checa <jchecahi>
Component: binutilsAssignee: Nick Clifton <nickc>
binutils sub component: system-version QA Contact: Miloš Prchlík <mprchlik>
Status: CLOSED ERRATA Docs Contact: Jacob Taylor Valdez <jvaldez>
Severity: unspecified    
Priority: unspecified CC: fweimer, mprchlik, nickc, ohudlick, sipoyare
Version: 8.7Keywords: Bugfix, Triaged
Target Milestone: rc   
Target Release: ---   
Hardware: Unspecified   
OS: Unspecified   
Whiteboard:
Fixed In Version: binutils-2.30-119.el8 Doc Type: No Doc Update
Doc Text:
Story Points: ---
Clone Of:
: 2119383 2120959 (view as bug list) Environment:
Last Closed: 2023-05-16 08:45:00 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:
Bug Depends On:    
Bug Blocks: 2119383, 2120959    

Description Jesus Checa 2022-08-18 13:43:53 UTC
Description of problem:
binutils are not able to correctly read objects and binaries compiled with "clang -flto".

I tested this in RHEL-9.0 and Fedora 34+. In those environments the output is as expected.

This is reproducible in every supported arch.

How reproducible:
100%

Steps to Reproduce:
1. echo "void lto_function(){}" | clang -flto -O2 -c -x c -o foo.o -
2. nm foo.o
3. ar crs foo.a foo.o
4. readelf -c foo.a

Actual results:
[root@hpe-apollo-cn99xx-14-vm-25 ~]# echo "void lto_function(){}" | clang -flto -O2 -c -x c -o foo.o -
[root@hpe-apollo-cn99xx-14-vm-25 ~]# nm foo.o
nm: foo.o: file format not recognized
[root@hpe-apollo-cn99xx-14-vm-25 ~]# ar crs foo.a foo.o
[root@hpe-apollo-cn99xx-14-vm-25 ~]# readelf -c foo.a
foo.a has no archive index
readelf: foo.a: Error: foo.a: unable to dump the index as none was found

Expected results:
$  echo "void lto_function(){}" | clang -flto -O2 -c -x c -o foo.o -
$ nm foo.o
00000000 T lto_function
$ ranlib foo.a foo.o
ranlib: foo.o: file format not recognized
$ ar crs foo.a foo.o
$ readelf -c foo.a
Index of archive foo.a: (1 entries, 0xe bytes in the symbol table)
Contents of binary foo.a(foo.o) at offset 0x5a
        lto_function

Comment 1 Nick Clifton 2022-08-19 12:31:04 UTC
Right - I have found the problem.  There is a bug in the BFD library.  It does automatically load a plugin when needed, but it looks in the wrong place for it:

  # strace nm foo.o |& grep plugin
  openat(AT_FDCWD, "/usr/bin/../bin/../lib/bfd-plugins", O_RDONLY|O_NONBLOCK|O_CLOEXEC|O_DIRECTORY) = -1 ENOENT (No such file or directory)

The LLVMgold.so plugin is in the /usr/lib64/bfd-plugins directory not the /usr/lib/bfd-plugins directory.

Comment 6 Nick Clifton 2022-09-05 11:14:25 UTC
Fixed in binutils-2.30-119.el8

Comment 7 Miloš Prchlík 2022-09-19 06:50:42 UTC
[root@ci-vm-10-0-139-112 ~]# rpm -q binutils
binutils-2.30-119.el8.x86_64
[root@ci-vm-10-0-139-112 ~]# echo "void lto_function(){}" | clang -flto -O2 -c -x c -o foo.o -
[root@ci-vm-10-0-139-112 ~]# nm foo.o
00000000 T lto_function
[root@ci-vm-10-0-139-112 ~]# ar crs foo.a foo.o
[root@ci-vm-10-0-139-112 ~]# readelf -c foo.a
Index of archive foo.a: (1 entries, 0xe bytes in the symbol table)
Contents of binary foo.a(foo.o) at offset 0x5a
        lto_function
[root@ci-vm-10-0-139-112 ~]#

Comment 11 Miloš Prchlík 2022-09-19 06:53:20 UTC
Verified with binutils-2.30-119.el8

Comment 13 errata-xmlrpc 2023-05-16 08:45:00 UTC
Since the problem described in this bug report should be
resolved in a recent advisory, it has been closed with a
resolution of ERRATA.

For information on the advisory (binutils bug fix and enhancement update), and where to find the updated
files, follow the link below.

If the solution does not work for you, open a new bug report.

https://access.redhat.com/errata/RHBA-2023:2949