Bug 28264 - [2.37 Regression] ld.bfd crashes on linking efivar with LTO
Summary: [2.37 Regression] ld.bfd crashes on linking efivar with LTO
Status: RESOLVED FIXED
Alias: None
Product: binutils
Classification: Unclassified
Component: ld (show other bugs)
Version: 2.37
: P2 normal
Target Milestone: 2.38
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on: 28879
Blocks:
  Show dependency treegraph
 
Reported: 2021-08-25 11:07 UTC by kloczek
Modified: 2022-02-11 13:49 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed: 2021-08-25 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description kloczek 2021-08-25 11:07:34 UTC
binutils-2.37-9.fc36.x86_64 from rawhide

efivar from master https://github.com/rhboot/efivar/

When LTO is used gcc crashes

make[1]: Entering directory '/home/tkloczko/rpmbuild/BUILD/efivar-37/src'
/usr/bin/gcc -O2 -g -grecord-gcc-switches -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -fstack-protector-strong -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -fdata-sections -ffunction-sections -flto=auto -flto-partition=none -Os -std=gnu11 -funsigned-char -fvisibility=hidden -specs=/home/tkloczko/rpmbuild/BUILD/efivar-37/src/include/gcc.specs -fno-merge-constants  -L. -Wl,-z,relro -Wl,--as-needed -Wl,--gc-sections -Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -flto=auto -flto-partition=none -fuse-linker-plugin  -Wl,--add-needed -Wl,--build-id -Wl,--no-allow-shlib-undefined -Wl,--no-undefined-version -Wl,-z,now -Wl,-z,muldefs -Wl,-z,relro -Wl,--fatal-warnings     -DLIBEFIVAR_VERSION=37 -D_GNU_SOURCE -I/home/tkloczko/rpmbuild/BUILD/efivar-37/src/include/  -shared -Wl,-soname,libefivar.so.1 -Wl,--version-script=libefivar.map  \
  -o libefivar.so crc32.o dp.o dp-acpi.o dp-hw.o dp-media.o dp-message.o efivarfs.o error.o export.o guid.o guids.o guid-symbols.o lib.o vars.o -ldl
collect2: fatal error: ld terminated with signal 11 [Segmentation fault], core dumped
compilation terminated.
make[1]: *** [/home/tkloczko/rpmbuild/BUILD/efivar-37/src/include/rules.mk:32: libefivar.so] Error 1
make[1]: Leaving directory '/home/tkloczko/rpmbuild/BUILD/efivar-37/src'
make: *** [Makefile:14: all] Error 2


[tkloczko@barrel SPECS]$ coredumpctl gdb 2906717
           PID: 2906717 (ld)
           UID: 1000 (tkloczko)
           GID: 1000 (tkloczko)
        Signal: 11 (SEGV)
     Timestamp: Wed 2021-08-25 11:57:46 BST (9min ago)
  Command Line: /usr/bin/ld -plugin /usr/libexec/gcc/x86_64-redhat-linux/11/liblto_plugin.so -plugin-opt=/usr/libexec/gcc/x86_64-redhat-linux/11/lto-wrapper -plugin-opt=-fresolution=/tmp/ccoyzJjy.res -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lc -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s --build-id --no-add-needed --eh-frame-hdr --hash-style=gnu -m elf_x86_64 -shared --fatal-warnings --no-undefined-version --no-allow-shlib-undefined --add-needed -z now --build-id -z relro -o libefivar.so /usr/lib/gcc/x86_64-redhat-linux/11/../../../../lib64/crti.o /usr/lib/gcc/x86_64-redhat-linux/11/crtbeginS.o -L. -L/usr/lib/gcc/x86_64-redhat-linux/11 -L/usr/lib/gcc/x86_64-redhat-linux/11/../../../../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L/usr/lib/gcc/x86_64-redhat-linux/11/../../.. -z relro --as-needed --gc-sections -z now --add-needed --build-id --no-allow-shlib-undefined --no-undefined-version -z now -z muldefs -z relro --fatal-warnings -soname libefivar.so.1 --version-script=libefivar.map crc32.o dp.o dp-acpi.o dp-hw.o dp-media.o dp-message.o efivarfs.o error.o export.o guid.o guids.o guid-symbols.o lib.o vars.o -ldl -lgcc --push-state --as-needed -lgcc_s --pop-state -lc -lgcc --push-state --as-needed -lgcc_s --pop-state /usr/lib/gcc/x86_64-redhat-linux/11/crtendS.o /usr/lib/gcc/x86_64-redhat-linux/11/../../../../lib64/crtn.o
    Executable: /usr/bin/ld.bfd
 Control Group: /user.slice/user-1000.slice/session-2408.scope
          Unit: session-2408.scope
         Slice: user-1000.slice
       Session: 2408
     Owner UID: 1000 (tkloczko)
       Boot ID: dacb7fefc2824304bb9d4576770821a4
    Machine ID: d033f66b249a4ec09acfa81ac205554f
      Hostname: barrel
       Storage: /var/lib/systemd/coredump/core.ld.1000.dacb7fefc2824304bb9d4576770821a4.2906717.1629889066000000.zst
       Message: Process 2906717 (ld) of user 1000 dumped core.

                Stack trace of thread 2906717:
                #0  0x00007fc7a04cdd42 __strcmp_avx2_rtm (libc.so.6 + 0x18bd42)
                #1  0x00007fc7a05ea706 _bfd_generic_link_add_one_symbol (libbfd-2.37-9.fc36.so + 0x52706)
                #2  0x00007fc7a0626f55 bfd_elf_link_add_symbols (libbfd-2.37-9.fc36.so + 0x8ef55)
                #3  0x000000000041aacc load_symbols.part.0 (ld.bfd + 0x1aacc)
                #4  0x00000000004129b9 open_input_bfds.lto_priv.0 (ld.bfd + 0x129b9)
                #5  0x0000000000419f54 lang_process (ld.bfd + 0x19f54)
                #6  0x0000000000408289 main (ld.bfd + 0x8289)
                #7  0x00007fc7a036f560 __libc_start_call_main (libc.so.6 + 0x2d560)
                #8  0x00007fc7a036f60c __libc_start_main@@GLIBC_2.34 (libc.so.6 + 0x2d60c)
                #9  0x0000000000409985 _start (ld.bfd + 0x9985)
Comment 1 H.J. Lu 2021-08-25 14:27:28 UTC
This is triggered by

commit 726d7d1ecfd1fc0966983e1d5e59b527b90cf7c5
Author: Alan Modra <amodra@gmail.com>
Date:   Wed Dec 2 13:03:23 2020 +1030

    PR26978, Inconsistency for strong foo@v1 and weak foo@@v1

Please try users/hjl/pr26978/master branch:

https://gitlab.com/x86-binutils/binutils-gdb/-/tree/users/hjl/pr26978/master
Comment 2 H.J. Lu 2021-08-25 20:43:08 UTC
(gdb) f 1
#1  0x000000000043b9b4 in _bfd_generic_link_add_one_symbol (
    info=0x667620 <link_info>, abfd=0x6d9240, 
    name=0xa756d2 "_efi_set_variable", flags=2, section=0xa661e8, value=29806, 
    string=0x0, copy=false, collect=false, hashp=0x7e0ce0)
    at /export/gnu/import/git/gitlab/x86-binutils/bfd/linker.c:1685
1685		  if (strcmp (h->u.i.link->root.string, string) == 0)
(gdb) p h->u.i.link->root.string
$2 = 0x7da6cb "_efi_set_variable@libefivar.so.0"
(gdb) p string
$3 = 0x0
(gdb)

4985	      if (! (_bfd_generic_link_add_one_symbol
4986		     (info, override ? override : abfd, name, flags, sec, value,
4987		      NULL, false, bed->collect,
                      ^^^^
4988		      (struct bfd_link_hash_entry **) sym_hash)))

calls _bfd_generic_link_add_one_symbol with string == NULL.
Comment 3 H.J. Lu 2021-08-25 21:31:11 UTC
[hjl@gnu-cfl-2 pr28264]$ cat foo.v
libefivar.so.0 {
	global:	
		_efi_set_variable;
	local:	*;
};
[hjl@gnu-cfl-2 pr28264]$ cat foo.c
void
__attribute__ ((symver ("_efi_set_variable@libefivar.so.0")))
_efi_set_variable()
{
}
[hjl@gnu-cfl-2 pr28264]$ make
gcc -B./ -flto -fPIC   -c -o foo.o foo.c
gcc -B./ -shared -o libfoo.so -Wl,--version-script=foo.v foo.o
collect2: fatal error: ld terminated with signal 11 [Segmentation fault], core dumped
compilation terminated.
make: *** [Makefile:16: libfoo.so] Error 1
[hjl@gnu-cfl-2 pr28264]$
Comment 4 Alan Modra 2021-08-26 00:56:10 UTC
(In reply to H.J. Lu from comment #2)
> calls _bfd_generic_link_add_one_symbol with string == NULL.

Yes, it looks like commit 726d7d1ecfd1fc should have checked for that in the MIND case before calling strcmp.
Comment 5 Alan Modra 2021-08-26 01:02:32 UTC
At least, that would get us back to the 2.35 behaviour of compiling your #c3 testcase without complaint if not using -flto, and complaining about multiple definitions of _efi_set_variable if using -flto.
Comment 6 H.J. Lu 2021-08-26 14:54:21 UTC
A patch has been posted at

https://sourceware.org/pipermail/binutils/2021-August/117773.html
Comment 7 Sourceware Commits 2021-08-28 00:50:44 UTC
The master branch has been updated by Alan Modra <amodra@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=b05929a20efa7caa40415d2f069e44f7e76615e4

commit b05929a20efa7caa40415d2f069e44f7e76615e4
Author: Alan Modra <amodra@gmail.com>
Date:   Thu Aug 26 12:19:35 2021 +0930

    PR28264, ld.bfd crash on linking efivar with LTO
    
            PR 28264
            PR 26978
            * linker.c (_bfd_generic_link_add_one_symbol <MIND>): Check
            that string is non-NULL.
Comment 8 Sourceware Commits 2021-08-28 02:47:17 UTC
The master branch has been updated by H.J. Lu <hjl@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=7de7786bb7db5159fc8a7bfa3df72381ff16a38c

commit 7de7786bb7db5159fc8a7bfa3df72381ff16a38c
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Thu Aug 26 07:43:23 2021 -0700

    ld: Change indirect symbol from IR to undefined
    
    bfd/
    
            PR ld/28264
            * elflink.c (_bfd_elf_merge_symbol): Change indirect symbol from
            IR to undefined.
    
    ld/
    
            PR ld/28264
            * testsuite/ld-plugin/lto.exp: Run PR ld/28264 test.
            * testsuite/ld-plugin/pr28264-1.d: New file.
            * testsuite/ld-plugin/pr28264-2.d: Likewise.
            * testsuite/ld-plugin/pr28264-3.d: Likewise.
            * testsuite/ld-plugin/pr28264-4.d: Likewise.
            * testsuite/ld-plugin/pr28264.c: Likewise.
            * testsuite/ld-plugin/pr28264.ver: Likewise.
Comment 9 Alan Modra 2021-09-01 01:29:47 UTC
Fixed