Bug 28139 - ld.gold fails while ld.bfd succeeds: error: relocation refers to local symbol "" [2], which is defined in a discarded section
Summary: ld.gold fails while ld.bfd succeeds: error: relocation refers to local symbol...
Status: UNCONFIRMED
Alias: None
Product: binutils
Classification: Unclassified
Component: gold (show other bugs)
Version: 2.37
: P2 normal
Target Milestone: ---
Assignee: Cary Coutant
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2021-07-25 21:35 UTC by Sergei Trofimovich
Modified: 2021-07-26 14:28 UTC (History)
2 users (show)

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


Attachments
b.o (619 bytes, application/x-object)
2021-07-25 21:35 UTC, Sergei Trofimovich
Details
preprocessed.c (522 bytes, text/x-csrc)
2021-07-25 21:40 UTC, Sergei Trofimovich
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Sergei Trofimovich 2021-07-25 21:35:09 UTC
Initially reported by Dennis Schridde in https://bugs.gentoo.org/803173 as a systemd-249.1 build failure when linked with ld.gold.

Here is ane xtracted example (needs systemtap header) that illustrates the failure on x86_64:

$ cat bug.c
    #include <sys/sdt.h>

    void f(void *dev) {
        STAP_PROBE("foo", "foo");
    }

    int main(){}

ld.gold fails:

$ x86_64-pc-linux-gnu-gcc -O2 -fno-PIE -fno-stack-protector -o b.o -c bug.c
$ x86_64-pc-linux-gnu-gcc -O2 -fno-PIE -fno-stack-protector -Wl,--gc-sections -Wl,-O1 -fuse-ld=gold b.o -o bug
b.o(.note.stapsdt+0x14): error: relocation refers to local symbol "" [2], which is defined in a discarded section
collect2: error: ld returned 1 exit status


ld.bfd succeeds:

$ x86_64-pc-linux-gnu-gcc -O2 -fno-PIE -fno-stack-protector -Wl,--gc-sections -Wl,-O1 -fuse-ld=bfd b.o -o bug

Looks like -Wl,--gc-sections does too much for ld.gols. Which one is wrong here?
Comment 1 Sergei Trofimovich 2021-07-25 21:35:44 UTC
Created attachment 13567 [details]
b.o

b.o is a binary generated on my system.
Comment 2 Sergei Trofimovich 2021-07-25 21:40:31 UTC
Created attachment 13568 [details]
preprocessed.c

Attached preprocessed file extracted as:
    $ x86_64-pc-linux-gnu-gcc -O2 -fno-PIE -fno-stack-protector -o preprocessed.c -E bug.c
Comment 3 H.J. Lu 2021-07-26 14:28:00 UTC
BFD linker has

      /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
         Also treat note sections as a root, if the section is not part
         of a group.  We must keep all PREINIT_ARRAY, INIT_ARRAY as
         well as FINI_ARRAY sections for ld -r.  */