Bug 25022 - Make /DISCARD/ discard SHF_LINK_ORDER sections
Summary: Make /DISCARD/ discard SHF_LINK_ORDER sections
Status: RESOLVED FIXED
Alias: None
Product: binutils
Classification: Unclassified
Component: ld (show other bugs)
Version: 2.35
: P2 normal
Target Milestone: 2.35
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2019-09-20 12:06 UTC by Fangrui Song
Modified: 2020-02-07 13:13 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed: 2020-02-07 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Fangrui Song 2019-09-20 12:06:12 UTC
This depends on https://sourceware.org/bugzilla/show_bug.cgi?id=24526 (Generalize GC support for SHF_LINK_ORDER)

# a.script
SECTIONS { /DISCARD/ : { *(.foo) } }

# a.s
.section .foo,"a"
.quad 0

.section .bar,"ao",@progbits,.foo  # "o" is llvm-mc specific syntax that sets SHF_LINK_ORDER and sh_link
.quad 0

.section .zed,"ao",@progbits,.foo
.quad 0

.section .moo,"ao",@progbits,.zed
.quad 0


GNU ld issues an error

% llvm-mc -filetype=obj -triple=x86_64 a.s -o a.o
% ld.bfd -T a.script a.o
ld.bfd: a.out: sh_link of section `.bar' points to discarded section `.foo' of `a.o'
ld.bfd: final link failed: bad value

In lld, .bar is considered a dependent section of .foo
When /DISCARD/ discards .foo, it discards .bar as well. Transitively, .zed and .moo are discarded. This rule can be seen as a generalization of SHT_RELA or SHT_ARM_EXIDX.

% ld.lld -T a.script a.o
ld.lld: warning: cannot find entry symbol _start; defaulting to 0x0
Comment 1 Sourceware Commits 2020-02-07 03:47:34 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=9b538ba71fc7dbab65371e3e4e5f7e093ff25023

commit 9b538ba71fc7dbab65371e3e4e5f7e093ff25023
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Thu Feb 6 19:42:52 2020 -0800

    ELF: Discard a section if any of its linked-to sections has been discarded
    
    Add ldelf_before_place_orphans to call before lang_place_orphans to
    discard a section if any of its linked-to sections has been discarded.
    
    	PR ld/25022
    	* emultempl/aix.em (ld_${EMULATION_NAME}_emulation): Add
    	before_place_orphans_default.
    	* emultempl/armcoff.em (ld_${EMULATION_NAME}_emulation): Likewise.
    	* emultempl/beos.em (ld_${EMULATION_NAME}_emulation): Likewise.
    	* emultempl/generic.em (ld_${EMULATION_NAME}_emulation): Likewise.
    	* emultempl/linux.em (ld_${EMULATION_NAME}_emulation): Likewise.
    	* emultempl/msp430.em (ld_${EMULATION_NAME}_emulation): Likewise.
    	* emultempl/pe.em (ld_${EMULATION_NAME}_emulation): Likewise.
    	* emultempl/pep.em (ld_${EMULATION_NAME}_emulation): Likewise.
    	* emultempl/ticoff.em (ld_${EMULATION_NAME}_emulation): Likewise.
    	* emultempl/vanilla.em (ld_${EMULATION_NAME}_emulation): Likewise.
    	* emultempl/elf.em (ld_${EMULATION_NAME}_emulation): Use
    	ldelf_before_place_orphans.
    	* ldelf.c (ldelf_before_place_orphans): New.
    	* ldelf.h (ldelf_before_place_orphans): Likewise.
    	* ldemul.c (ldemul_before_place_orphans): Likewise.
    	(before_place_orphans_default): Likewise.
    	* ldemul.h (ldemul_before_place_orphans): Likewise.
    	(before_place_orphans_default): Likewise.
    	(ld_emulation_xfer_struct): Add before_place_orphans.
    	* ldlang.c (lang_process): Call ldemul_before_place_orphans
    	before lang_place_orphans.
    	* testsuite/ld-elf/pr25022.d: New file.
    	* testsuite/ld-elf/pr25022.s: Likewise.
    	* testsuite/ld-elf/pr25022.t: Likewise.
Comment 2 H.J. Lu 2020-02-07 03:49:12 UTC
Fixed for 2.35.
Comment 3 Nick Clifton 2020-02-07 10:57:35 UTC
(In reply to H.J. Lu from comment #2)
> Fixed for 2.35.

Not quite:

Checking Binutils in: dlx-elf ...  LD REGRESSION: ld-elf/pr25022    
Checking Binutils in: frv-elf ...  LD REGRESSION: ld-elf/pr25022    
Checking Binutils in: d30v-elf ... LD REGRESSION: ld-elf/pr25022    
Checking Binutils in: mn10200-elf ...  LD REGRESSION: ld-elf/pr25022    
Checking Binutils in: ms1-elf ... LD REGRESSION: ld-elf/pr25022    
Checking Binutils in: mt-elf ...  LD REGRESSION: ld-elf/pr25022    
Checking Binutils in: xgate-elf ... LD REGRESSION: ld-elf/pr25022    
Checking Binutils in: fr30-elf ...  LD REGRESSION: ld-elf/pr25022    
Checking Binutils in: ft32-elf ... LD REGRESSION: ld-elf/pr25022    
Checking Binutils in: iq2000-elf ...  LD REGRESSION: ld-elf/pr25022    
Checking Binutils in: pj-elf ...  LD REGRESSION: ld-elf/pr25022    
Checking Binutils in: s12z-elf ... LD REGRESSION: ld-elf/pr25022    

Could you have a look please ?

Cheers
  Nick
Comment 4 Sourceware Commits 2020-02-07 13:12:31 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=adb8754e48d53b219ddaa9e8368e4b1acb9db53a

commit adb8754e48d53b219ddaa9e8368e4b1acb9db53a
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Fri Feb 7 05:08:16 2020 -0800

    pr25022.d: Xfail more targets without ldelf_before_place_orphans
    
    	PR ld/25022
    	* testsuite/ld-elf/pr25022.d: Xfail more targets which don't use
    	ldelf_before_place_orphans.
Comment 5 H.J. Lu 2020-02-07 13:13:23 UTC
(In reply to Nick Clifton from comment #3)
> (In reply to H.J. Lu from comment #2)
> > Fixed for 2.35.
> 
> Not quite:
> 
> Checking Binutils in: dlx-elf ...  LD REGRESSION: ld-elf/pr25022    
> Checking Binutils in: frv-elf ...  LD REGRESSION: ld-elf/pr25022    
> Checking Binutils in: d30v-elf ... LD REGRESSION: ld-elf/pr25022    
> Checking Binutils in: mn10200-elf ...  LD REGRESSION: ld-elf/pr25022    
> Checking Binutils in: ms1-elf ... LD REGRESSION: ld-elf/pr25022    
> Checking Binutils in: mt-elf ...  LD REGRESSION: ld-elf/pr25022    
> Checking Binutils in: xgate-elf ... LD REGRESSION: ld-elf/pr25022    
> Checking Binutils in: fr30-elf ...  LD REGRESSION: ld-elf/pr25022    
> Checking Binutils in: ft32-elf ... LD REGRESSION: ld-elf/pr25022    
> Checking Binutils in: iq2000-elf ...  LD REGRESSION: ld-elf/pr25022    
> Checking Binutils in: pj-elf ...  LD REGRESSION: ld-elf/pr25022    
> Checking Binutils in: s12z-elf ... LD REGRESSION: ld-elf/pr25022    
> 
> Could you have a look please ?
> 

Should be fixed now.