Bug 32083 - lto1: fatal error: multiple prevailing defs for ‘func’
Summary: lto1: fatal error: multiple prevailing defs for ‘func’
Status: RESOLVED FIXED
Alias: None
Product: binutils
Classification: Unclassified
Component: ld (show other bugs)
Version: 2.43.1
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2024-08-14 11:04 UTC by H.J. Lu
Modified: 2024-09-08 17:25 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description H.J. Lu 2024-08-14 11:04:45 UTC
When linker sees a common symbol, foo, if there is foo in an archive, linker
calls elf_link_is_defined_archive_symbol to check if foo is a definition. Since
elf_link_is_defined_archive_symbol calls plugin_object_p to check if the LTO plugin
can claim the archive member.  When plugin_call_all_symbols_read is called, all
symbols from claimed LTO objects are returned, including the unused archive member.
Since liby.a appears twice on the command line, we get ultiple prevailing defs
error:

[hjl@gnu-cfl-3 lto-2]$ cat exec.c
#include <stdio.h>

int foo;

int main()
{
  if (foo == 1)
    printf ("PASS\n");

  return 0;
}
[hjl@gnu-cfl-3 lto-2]$ cat y.c
int foo;

void
func (void)
{
}
[hjl@gnu-cfl-3 lto-2]$ make y
gcc -B./ -fcommon -flto   -c -o exec.o exec.c
gcc -B./ -fcommon -flto   -c -o y.o y.c
ar -rv liby.a y.o
ar: creating liby.a
a - y.o
gcc -B./ -fcommon -flto -o y exec.o liby.a liby.a
lto1: fatal error: multiple prevailing defs for ‘func’
compilation terminated.
lto-wrapper: fatal error: gcc returned 1 exit status
compilation terminated.
./ld: error: lto-wrapper failed
collect2: error: ld returned 1 exit status
make: *** [Makefile:20: y] Error 1
rm y.o
[hjl@gnu-cfl-3 lto-2]$
Comment 1 H.J. Lu 2024-08-14 12:17:08 UTC
/* Callback used by a linker to check if the plugin will claim FILE. Writes
   the result in CLAIMED.  If KNOWN_USED, the object is known by the linker
   to be used, or an older API version is in use that does not provide that
   information; otherwise, the linker is only determining whether this is
   a plugin object and it should not be registered as having offload data if
   not claimed by the plugin.  */

static enum ld_plugin_status
claim_file_handler_v2 (const struct ld_plugin_input_file *file, int *claimed,
                       int known_used)

...
    if (obj.offload && (known_used || obj.found > 0))

Is known_used ignored when obj.found > 0?
Comment 2 H.J. Lu 2024-08-14 13:37:49 UTC
Linker fix is at

https://sourceware.org/pipermail/binutils/2024-August/136333.html
Comment 3 Sourceware Commits 2024-08-15 10:57:26 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=a6f8fe0a9e9cbe871652e46ba7c22d5e9fb86208

commit a6f8fe0a9e9cbe871652e46ba7c22d5e9fb86208
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Wed Aug 14 20:50:02 2024 -0700

    lto: Don't include unused LTO archive members in output
    
    When plugin_object_p is called by elf_link_is_defined_archive_symbol to
    check if a symbol in archive is a real definition, set archive member
    plugin_format to bfd_plugin_yes_unused to avoid including the unused LTO
    archive members in linker output.  When plugin_object_p is called as
    known used, call plugin claim_file if plugin_format is bfd_plugin_unknown
    or bfd_plugin_yes_unused.
    
    To get the proper support for archives with LTO common symbols with GCC,
    the GCC fix for
    
    https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116361
    
    is needed.
    
    bfd/
    
            PR ld/32083
            * archures.c (bfd_arch_get_compatible): Treat bfd_plugin_yes_unused
            the same as bfd_plugin_yes.
            * elflink.c (elf_link_is_defined_archive_symbol): Likewise.
            * bfd.c (bfd_plugin_format): Add bfd_plugin_yes_unused.
            * plugin.c (try_claim): Try claim_file_v2 first.
            * bfd-in2.h: Regenerated.
    
    ld/
    
            PR ld/32083
            * plugin.c (plugin_call_claim_file): Add an argument to return
            if LDPT_REGISTER_CLAIM_FILE_HOOK_V2 is used.
            (plugin_object_p): When KNOWN_USED is false, we call plugin
            claim_file if plugin_format is bfd_plugin_unknown and set
            plugin_format to bfd_plugin_yes_unused on LTO object.  When
            KNOWN_USED is true, we call plugin claim_file if plugin_format
            is bfd_plugin_unknown or bfd_plugin_yes_unused.
    
    Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
Comment 4 Sourceware Commits 2024-08-15 23:32:14 UTC
The binutils-2_43-branch branch has been updated by H.J. Lu <hjl@sourceware.org>:

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

commit 92b6ecd5589fb7db9f28dba41978f87974312aa5
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Wed Aug 14 20:50:02 2024 -0700

    lto: Don't include unused LTO archive members in output
    
    When plugin_object_p is called by elf_link_is_defined_archive_symbol to
    check if a symbol in archive is a real definition, set archive member
    plugin_format to bfd_plugin_yes_unused to avoid including the unused LTO
    archive members in linker output.  When plugin_object_p is called as
    known used, call plugin claim_file if plugin_format is bfd_plugin_unknown
    or bfd_plugin_yes_unused.
    
    To get the proper support for archives with LTO common symbols with GCC,
    the GCC fix for
    
    https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116361
    
    is needed.
    
    bfd/
    
            PR ld/32083
            * archures.c (bfd_arch_get_compatible): Treat bfd_plugin_yes_unused
            the same as bfd_plugin_yes.
            * elflink.c (elf_link_is_defined_archive_symbol): Likewise.
            * bfd.c (bfd_plugin_format): Add bfd_plugin_yes_unused.
            * plugin.c (try_claim): Try claim_file_v2 first.
            * bfd-in2.h: Regenerated.
    
    ld/
    
            PR ld/32083
            * plugin.c (plugin_call_claim_file): Add an argument to return
            if LDPT_REGISTER_CLAIM_FILE_HOOK_V2 is used.
            (plugin_object_p): When KNOWN_USED is false, we call plugin
            claim_file if plugin_format is bfd_plugin_unknown and set
            plugin_format to bfd_plugin_yes_unused on LTO object.  When
            KNOWN_USED is true, we call plugin claim_file if plugin_format
            is bfd_plugin_unknown or bfd_plugin_yes_unused.
    
    Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
    (cherry picked from commit a6f8fe0a9e9cbe871652e46ba7c22d5e9fb86208)
Comment 5 Sourceware Commits 2024-08-22 13:36:19 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=eb53cbeca8cf456b4a79f33d7a2a586a99b4b2e3

commit eb53cbeca8cf456b4a79f33d7a2a586a99b4b2e3
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Fri Aug 16 03:48:34 2024 -0700

    lto: Add a test for PR ld/32083
    
    Add a test for PR ld/32083 and xfail the test for GCC without the fix:
    
    commit a98dd536b1017c2b814a3465206c6c01b2890998
    Author: H.J. Lu <hjl.tools@gmail.com>
    Date:   Wed Aug 21 07:25:25 2024 -0700
    
        Update LDPT_REGISTER_CLAIM_FILE_HOOK_V2 linker plugin hook
    
            PR ld/32083
            * testsuite/ld-plugin/common-2a.c: New file.
            * testsuite/ld-plugin/common-2b.c: Likewise.
            * testsuite/ld-plugin/lto.exp: Run PR ld/32083 test.
    
    Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
Comment 6 H.J. Lu 2024-08-22 13:39:54 UTC
Fixed for 2.44 and 2.43.1.
Comment 7 Sourceware Commits 2024-08-22 13:56:05 UTC
The binutils-2_43-branch branch has been updated by H.J. Lu <hjl@sourceware.org>:

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

commit 2d37b890e569330a29181ec3bd34b5cc1c354cbc
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Fri Aug 16 03:48:34 2024 -0700

    lto: Add a test for PR ld/32083
    
    Add a test for PR ld/32083 and xfail the test for GCC without the fix:
    
    commit a98dd536b1017c2b814a3465206c6c01b2890998
    Author: H.J. Lu <hjl.tools@gmail.com>
    Date:   Wed Aug 21 07:25:25 2024 -0700
    
        Update LDPT_REGISTER_CLAIM_FILE_HOOK_V2 linker plugin hook
    
            PR ld/32083
            * testsuite/ld-plugin/common-2a.c: New file.
            * testsuite/ld-plugin/common-2b.c: Likewise.
            * testsuite/ld-plugin/lto.exp: Run PR ld/32083 test.
    
    Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
    (cherry picked from commit eb53cbeca8cf456b4a79f33d7a2a586a99b4b2e3)