Bug 20513 - GNU ld does not ignore symbols defined in sections marked EXCLUDE
Summary: GNU ld does not ignore symbols defined in sections marked EXCLUDE
Status: RESOLVED FIXED
Alias: None
Product: binutils
Classification: Unclassified
Component: ld (show other bugs)
Version: 2.26
: P2 normal
Target Milestone: 2.28
Assignee: H.J. Lu
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-08-25 07:27 UTC by Richard Biener
Modified: 2016-09-02 08:59 UTC (History)
2 users (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 Richard Biener 2016-08-25 07:27:09 UTC
When linking two object files, one with a definition of symbol FOO and one with
a definition of symbol FOO in a section marked with SHF_EXCLUDE then GNU ld
complains about 'multiple definitions of 'FOO''.  GOLD handles this situation
just fine.

t1.s:
        .text
        .section        .text.startup,"ax",@progbits
        .p2align 4,,15
        .globl  main
        .type   main, @function
main:

t2.s:
        .text
        .section        .text.startup,"axe",@progbits
        .p2align 4,,15
        .globl  main
        .type   main, @function
main:

(ignore assembler warnings about bogus section attributes)

> ld t1.o t2.o -o a.out
t2.o: In function `main':
(.text.startup+0x0): multiple definition of `main'
t1.o:(.text.startup+0x0): first defined here
> gold t1.o t2.o -o a.out
Comment 1 H.J. Lu 2016-08-26 19:59:24 UTC
This works:

diff --git a/ld/ldmain.c b/ld/ldmain.c
index e7a8dff..294c5b9 100644
--- a/ld/ldmain.c
+++ b/ld/ldmain.c
@@ -959,6 +959,11 @@ multiple_definition (struct bfd_link_info *info,
     && bfd_is_abs_section (nsec->output_section)))
     return;
 
+  /* Ignore a redefinition from excluded section.  */
+  if ((osec->flags & SEC_EXCLUDE) != 0
+      || (nsec->flags & SEC_EXCLUDE) != 0)
+    return;
+
   name = h->root.string;
   if (nbfd == NULL)
     {
Comment 2 H.J. Lu 2016-08-26 23:52:22 UTC
This is better:

index e7a8dff..052b276 100644
--- a/ld/ldmain.c
+++ b/ld/ldmain.c
@@ -959,6 +959,19 @@ multiple_definition (struct bfd_link_info *info,
     && bfd_is_abs_section (nsec->output_section)))
     return;
 
+  /* Ignore a redefinition from excluded section.  */
+  if (!bfd_link_relocatable (&link_info)
+      && ((osec->flags & SEC_EXCLUDE) != 0
+    || (nsec->flags & SEC_EXCLUDE) != 0))
+    {
+      if ((osec->flags & SEC_EXCLUDE) != 0)
+  {
+    h->u.def.section = nsec;
+    h->u.def.value = nval;
+  }
+      return;
+    }
+
   name = h->root.string;
   if (nbfd == NULL)
     {
Comment 3 Alan Modra 2016-08-27 01:34:21 UTC
My gut feeling is that it would be better to deal with this as we do with other sections that are discarded early in the link.

--- a/ld/ldlang.c
+++ b/ld/ldlang.c
@@ -2294,6 +2294,12 @@ section_already_linked (bfd *abfd, asection *sec, void *data)
       return;
     }
 
+  /* Deal with SHF_EXCLUDE ELF sections.  */
+  if (!bfd_link_relocatable (&link_info)
+      && (abfd->flags & BFD_PLUGIN) == 0
+      && (sec->flags & (SEC_GROUP | SEC_KEEP | SEC_EXCLUDE)) == SEC_EXCLUDE)
+    sec->output_section = bfd_abs_section_ptr;
+
   if (!(abfd->flags & DYNAMIC))
     bfd_section_already_linked (abfd, sec, &link_info);
 }
Comment 4 Sourceware Commits 2016-08-31 09:57:23 UTC
The master branch has been updated by Alan Modra <amodra@sourceware.org>:

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

commit 2e84f9c1b5770b803b739bcd157d7a457c22b56f
Author: Alan Modra <amodra@gmail.com>
Date:   Fri Aug 26 22:01:15 2016 +0930

    Ignore symbols defined in SHF_EXCLUDE sections
    
    	PR 20513
    	* ldlang.c (section_already_linked): Deal with SHF_EXCLUDE sections.
Comment 5 Alan Modra 2016-08-31 10:14:20 UTC
Fixed
Comment 6 Sourceware Commits 2016-09-01 19:06:09 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=652a8f4f57243c41e71da06b6d0fe27540f3cbba

commit 652a8f4f57243c41e71da06b6d0fe27540f3cbba
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Thu Sep 1 12:01:00 2016 -0700

    Add tests for PR ld/20513
    
    	PR ld/20513
    	* testsuite/ld-elf/pr20513a.d: New file.
    	* testsuite/ld-elf/pr20513a.s: Likewise.
    	* testsuite/ld-elf/pr20513b.d: Likewise.
    	* testsuite/ld-elf/pr20513b.s: Likewise.
    	* testsuite/ld-elf/pr20513c.d: Likewise.
    	* testsuite/ld-elf/pr20513d.d: Likewise.
    	* testsuite/ld-elf/pr20513e.d: Likewise.
    	* testsuite/ld-elf/pr20513f.d: Likewise.
Comment 7 Nick Clifton 2016-09-02 08:59:44 UTC
Hi H.J.

>     Add tests for PR ld/20513
>     
>     	PR ld/20513
>     	* testsuite/ld-elf/pr20513c.d: Likewise.
>     	* testsuite/ld-elf/pr20513d.d: Likewise.

These new tests are producing multiple regressions in the linker testsuite for lots of targets.  For example:

  Checking Binutils in: arceb-elf ... done
    LD REGRESSION: ld-elf/pr20513c    
    LD REGRESSION: ld-elf/pr20513d    

Affected targets are:

 arceb-elf, avr-elf, cr16-elf, cris-elf, crisv32-elf, crx-elf,
 d10v-elf, d30v-elf, dlx-elf, fr30-elf, frv-elf, ft32-elf, h8300-elf,
 h8300-rtems, i860-stardent-elf, ip2k-elf, iq2000-elf, lm32-rtems4.10,
 m32c-elf, m32r-elf, m68hc11-elf, mcore-elf, mep-elf, microblaze-elf,
 mn10200-elf, moxie-elf, msp430-elf, mt-elf, nds32le-elf, or1k-elf,
 pj-elf, rl78-elf, rx-elf, spu-elf, tic6x-elf, v850-elf, visium-elf,
 xgate-elf, xstormy16-elf 

Most of the failures are due to the targets not supporting shared libraries, but the dlx-elf target has extra problems that need to be investigated.

Please could you fix these regressions ?

Cheers
  Nick