Bug 15323 - Unable to redefine weak function in -flto incrementally-linked executable
Summary: Unable to redefine weak function in -flto incrementally-linked executable
Status: RESOLVED FIXED
Alias: None
Product: binutils
Classification: Unclassified
Component: ld (show other bugs)
Version: 2.23
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2013-03-29 21:29 UTC by Joshua Conner
Modified: 2023-06-05 03:02 UTC (History)
1 user (show)

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


Attachments
A patch (349 bytes, patch)
2013-03-29 22:58 UTC, H.J. Lu
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Joshua Conner 2013-03-29 21:29:28 UTC
If I have a weak function defined in an executable created with -flto -r, when I try to redefine the function by re-linking with an object file containing a non-weak definition of the same function, I get an error:

`foobar' referenced in section `.text.startup' of test-intermediate: defined in discarded section `.text' of test.o (symbol from plugin)
collect2: error: ld returned 1 exit status

Here are some steps to reproduce, where gcc is using ld with the linker plugin for lto:

$ cat test-weak.c
volatile int x;

__attribute__((weak))
void foobar (void) { x++; }

int main (void)
{
  foobar ();
  return 0;
}

$ cat test.c
extern volatile int x;

void foobar (void) { x--; }

$ gcc -flto -g -O2 -c -o test-weak.o test-weak.c
$ gcc -flto -g -O2 -c -o test.o test.c
$ gcc -flto -g -O2 -r -o test-intermediate test-weak.o -nostdlib
-nostartfiles
$ gcc -flto -g -O2 -o test test-intermediate test.o
`foobar' referenced in section `.text.startup' of test-intermediate: defined in discarded section `.text' of test.o (symbol from plugin)
collect2: error: ld returned 1 exit status

Let me know if you need an example that is assembly-only, or if you need me to verify with top-of-tree.

(Note that I can work around the error with -fno-use-linker-plugin, but I want to use the linker plugin for whole program optimization purposes).
Comment 1 H.J. Lu 2013-03-29 22:58:54 UTC
Created attachment 6960 [details]
A patch

We didn't set non_ir_ref for reference to the definition
within the same file.  This should fix x86 ELF targets.
Comment 2 Sourceware Commits 2013-03-30 00:38:41 UTC
CVSROOT:	/cvs/src
Module name:	src
Changes by:	hjl@sourceware.org	2013-03-30 00:38:39

Modified files:
	bfd            : ChangeLog elf32-i386.c elf64-x86-64.c 
	ld/testsuite   : ChangeLog 
	ld/testsuite/ld-plugin: lto.exp 
Added files:
	ld/testsuite/ld-plugin: pr15323.out pr15323a.c pr15323b.c 

Log message:
	Set non_ir_ref if a symbol is referenced by a non-shared object
	
	bfd/
	
	PR ld/15323
	* elf32-i386.c (elf_i386_check_relocs): Set non_ir_ref if a
	symbol is referenced by a non-shared object.
	* elf64-x86-64.c (elf_x86_64_check_relocs): Likewise.
	
	ld/testsuite/
	
	PR ld/15323
	* ld-plugin/lto.exp (lto_link_tests): Add pr15323a-r.o.
	(lto_run_tests): Add a test for PR ld/15323.
	
	* ld-plugin/pr15323.out: New file.
	* ld-plugin/pr15323a.c: Likewise.
	* ld-plugin/pr15323b.c: Likewise.

Patches:
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/ChangeLog.diff?cvsroot=src&r1=1.6014&r2=1.6015
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf32-i386.c.diff?cvsroot=src&r1=1.303&r2=1.304
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf64-x86-64.c.diff?cvsroot=src&r1=1.295&r2=1.296
http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ChangeLog.diff?cvsroot=src&r1=1.1702&r2=1.1703
http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-plugin/pr15323.out.diff?cvsroot=src&r1=NONE&r2=1.1
http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-plugin/pr15323a.c.diff?cvsroot=src&r1=NONE&r2=1.1
http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-plugin/pr15323b.c.diff?cvsroot=src&r1=NONE&r2=1.1
http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-plugin/lto.exp.diff?cvsroot=src&r1=1.5&r2=1.6
Comment 3 Sourceware Commits 2013-03-30 10:14:18 UTC
CVSROOT:	/cvs/src
Module name:	src
Changes by:	amodra@sourceware.org	2013-03-30 10:14:15

Modified files:
	bfd            : ChangeLog elf-m10300.c elf32-arm.c elf32-bfin.c 
	                 elf32-cr16.c elf32-cris.c elf32-d10v.c 
	                 elf32-dlx.c elf32-fr30.c elf32-frv.c 
	                 elf32-hppa.c elf32-i370.c elf32-iq2000.c 
	                 elf32-lm32.c elf32-m32c.c elf32-m32r.c 
	                 elf32-m68hc1x.c elf32-m68k.c elf32-mcore.c 
	                 elf32-metag.c elf32-microblaze.c elf32-moxie.c 
	                 elf32-msp430.c elf32-mt.c elf32-nios2.c 
	                 elf32-openrisc.c elf32-ppc.c elf32-rl78.c 
	                 elf32-s390.c elf32-score.c elf32-score7.c 
	                 elf32-sh.c elf32-tic6x.c elf32-tilepro.c 
	                 elf32-v850.c elf32-vax.c elf32-xstormy16.c 
	                 elf32-xtensa.c elf64-aarch64.c elf64-alpha.c 
	                 elf64-hppa.c elf64-ia64-vms.c elf64-mmix.c 
	                 elf64-ppc.c elf64-s390.c elf64-sh64.c 
	                 elfnn-ia64.c elfxx-mips.c elfxx-sparc.c 
	                 elfxx-tilegx.c 
	ld/testsuite   : ChangeLog 
	ld/testsuite/ld-plugin: lto.exp 

Log message:
	PR ld/15323
	bfd/
	* elf-m10300.c (mn10300_elf_check_relocs): Set non_ir_ref for
	global symbols referenced by relocs.
	* elf32-arm.c (elf32_arm_check_relocs): Likewise.
	* elf32-bfin.c (bfin_check_relocs): Likewise.
	* elf32-cr16.c (cr16_elf_check_relocs): Likewise.
	* elf32-cris.c (cris_elf_check_relocs): Likewise.
	* elf32-d10v.c (elf32_d10v_check_relocs): Likewise.
	* elf32-dlx.c (elf32_dlx_check_relocs): Likewise.
	* elf32-fr30.c (fr30_elf_check_relocs): Likewise.
	* elf32-frv.c (elf32_frv_check_relocs): Likewise.
	* elf32-hppa.c (elf32_hppa_check_relocs): Likewise.
	* elf32-i370.c (i370_elf_check_relocs): Likewise.
	* elf32-iq2000.c (iq2000_elf_check_relocs): Likewise.
	* elf32-lm32.c (lm32_elf_check_relocs): Likewise.
	* elf32-m32c.c (m32c_elf_check_relocs): Likewise.
	* elf32-m32r.c (m32r_elf_check_relocs): Likewise.
	* elf32-m68hc1x.c (elf32_m68hc11_check_relocs): Likewise.
	* elf32-m68k.c (elf_m68k_check_relocs): Likewise.
	* elf32-mcore.c (mcore_elf_check_relocs): Likewise.
	* elf32-metag.c (elf_metag_check_relocs): Likewise.
	* elf32-microblaze.c (microblaze_elf_check_relocs): Likewise.
	* elf32-moxie.c (moxie_elf_check_relocs): Likewise.
	* elf32-msp430.c (elf32_msp430_check_relocs): Likewise.
	* elf32-mt.c (mt_elf_check_relocs): Likewise.
	* elf32-nios2.c (nios2_elf32_check_relocs): Likewise.
	* elf32-openrisc.c (openrisc_elf_check_relocs): Likewise.
	* elf32-ppc.c (ppc_elf_check_relocs): Likewise.
	* elf32-rl78.c (rl78_elf_check_relocs): Likewise.
	* elf32-s390.c (elf_s390_check_relocs): Likewise.
	* elf32-score.c (s3_bfd_score_elf_check_relocs): Likewise.
	* elf32-score7.c (s7_bfd_score_elf_check_relocs): Likewise.
	* elf32-sh.c (sh_elf_check_relocs): Likewise.
	* elf32-tic6x.c (elf32_tic6x_check_relocs): Likewise.
	* elf32-tilepro.c (tilepro_elf_check_relocs): Likewise.
	* elf32-v850.c (v850_elf_check_relocs): Likewise.
	* elf32-vax.c (elf_vax_check_relocs): Likewise.
	* elf32-xstormy16.c (xstormy16_elf_check_relocs): Likewise.
	* elf32-xtensa.c (elf_xtensa_check_relocs): Likewise.
	* elf64-aarch64.c (elf64_aarch64_check_relocs): Likewise.
	* elf64-alpha.c (elf64_alpha_check_relocs): Likewise.
	* elf64-hppa.c (elf64_hppa_check_relocs): Likewise.
	* elf64-ia64-vms.c (elf64_ia64_check_relocs): Likewise.
	* elf64-mmix.c (mmix_elf_check_relocs): Likewise.
	* elf64-ppc.c (ppc64_elf_check_relocs): Likewise.
	* elf64-s390.c (elf_s390_check_relocs): Likewise.
	* elf64-sh64.c (sh_elf64_check_relocs): Likewise.
	* elfnn-ia64.c (elfNN_ia64_check_relocs): Likewise.
	* elfxx-sparc.c (_bfd_sparc_elf_check_relocs): Likewise.
	* elfxx-tilegx.c (tilegx_elf_check_relocs): Likewise.
	* elfxx-mips.c (_bfd_mips_elf_check_relocs): Likewise.  Don't
	test indirect/warning links for NULL.
	ld/testsuite/
	* ld-plugin/lto.exp (pr15323a.c): Compile without -flto rather
	than using -r to effectively strip out lto info.

Patches:
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/ChangeLog.diff?cvsroot=src&r1=1.6015&r2=1.6016
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf-m10300.c.diff?cvsroot=src&r1=1.121&r2=1.122
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf32-arm.c.diff?cvsroot=src&r1=1.314&r2=1.315
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf32-bfin.c.diff?cvsroot=src&r1=1.62&r2=1.63
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf32-cr16.c.diff?cvsroot=src&r1=1.26&r2=1.27
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf32-cris.c.diff?cvsroot=src&r1=1.130&r2=1.131
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf32-d10v.c.diff?cvsroot=src&r1=1.41&r2=1.42
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf32-dlx.c.diff?cvsroot=src&r1=1.24&r2=1.25
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf32-fr30.c.diff?cvsroot=src&r1=1.43&r2=1.44
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf32-frv.c.diff?cvsroot=src&r1=1.83&r2=1.84
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf32-hppa.c.diff?cvsroot=src&r1=1.193&r2=1.194
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf32-i370.c.diff?cvsroot=src&r1=1.74&r2=1.75
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf32-iq2000.c.diff?cvsroot=src&r1=1.30&r2=1.31
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf32-lm32.c.diff?cvsroot=src&r1=1.20&r2=1.21
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf32-m32c.c.diff?cvsroot=src&r1=1.30&r2=1.31
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf32-m32r.c.diff?cvsroot=src&r1=1.112&r2=1.113
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf32-m68hc1x.c.diff?cvsroot=src&r1=1.51&r2=1.52
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf32-m68k.c.diff?cvsroot=src&r1=1.141&r2=1.142
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf32-mcore.c.diff?cvsroot=src&r1=1.57&r2=1.58
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf32-metag.c.diff?cvsroot=src&r1=1.7&r2=1.8
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf32-microblaze.c.diff?cvsroot=src&r1=1.19&r2=1.20
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf32-moxie.c.diff?cvsroot=src&r1=1.7&r2=1.8
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf32-msp430.c.diff?cvsroot=src&r1=1.26&r2=1.27
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf32-mt.c.diff?cvsroot=src&r1=1.17&r2=1.18
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf32-nios2.c.diff?cvsroot=src&r1=1.4&r2=1.5
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf32-openrisc.c.diff?cvsroot=src&r1=1.33&r2=1.34
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf32-ppc.c.diff?cvsroot=src&r1=1.330&r2=1.331
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf32-rl78.c.diff?cvsroot=src&r1=1.12&r2=1.13
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf32-s390.c.diff?cvsroot=src&r1=1.129&r2=1.130
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf32-score.c.diff?cvsroot=src&r1=1.28&r2=1.29
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf32-score7.c.diff?cvsroot=src&r1=1.15&r2=1.16
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf32-sh.c.diff?cvsroot=src&r1=1.188&r2=1.189
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf32-tic6x.c.diff?cvsroot=src&r1=1.37&r2=1.38
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf32-tilepro.c.diff?cvsroot=src&r1=1.19&r2=1.20
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf32-v850.c.diff?cvsroot=src&r1=1.87&r2=1.88
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf32-vax.c.diff?cvsroot=src&r1=1.75&r2=1.76
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf32-xstormy16.c.diff?cvsroot=src&r1=1.46&r2=1.47
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf32-xtensa.c.diff?cvsroot=src&r1=1.140&r2=1.141
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf64-aarch64.c.diff?cvsroot=src&r1=1.11&r2=1.12
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf64-alpha.c.diff?cvsroot=src&r1=1.191&r2=1.192
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf64-hppa.c.diff?cvsroot=src&r1=1.115&r2=1.116
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf64-ia64-vms.c.diff?cvsroot=src&r1=1.9&r2=1.10
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf64-mmix.c.diff?cvsroot=src&r1=1.68&r2=1.69
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf64-ppc.c.diff?cvsroot=src&r1=1.407&r2=1.408
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf64-s390.c.diff?cvsroot=src&r1=1.127&r2=1.128
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf64-sh64.c.diff?cvsroot=src&r1=1.95&r2=1.96
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elfnn-ia64.c.diff?cvsroot=src&r1=1.15&r2=1.16
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elfxx-mips.c.diff?cvsroot=src&r1=1.351&r2=1.352
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elfxx-sparc.c.diff?cvsroot=src&r1=1.73&r2=1.74
http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elfxx-tilegx.c.diff?cvsroot=src&r1=1.20&r2=1.21
http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ChangeLog.diff?cvsroot=src&r1=1.1703&r2=1.1704
http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-plugin/lto.exp.diff?cvsroot=src&r1=1.6&r2=1.7
Comment 4 Alan Modra 2013-04-03 23:30:35 UTC
Fixed
Comment 5 Joshua Conner 2013-04-03 23:38:43 UTC
Thanks for the quick help!
Comment 6 Jackie Rosen 2014-02-16 18:24:04 UTC Comment hidden (spam)
Comment 7 Sourceware Commits 2015-09-11 17:09:06 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=6d636d8c779d3be92dd5db021d12c2e3a2c5d419

commit 6d636d8c779d3be92dd5db021d12c2e3a2c5d419
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Fri Sep 11 10:02:57 2015 -0700

    Add "ld -r" tests for PR ld/15323
    
    Weak defined function is turned into non-weak defined function by
    "ld -r -flto" with GCC 5 due to a GCC 5 regression:
    
    https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67548
    
    Add "ld -r" tests for PR ld/15323 to make sure that any linker change
    won't introduce linker regression for PR ld/15323.
    
    	* ld-plugin/lto.exp (lto_link_tests): Add a "ld -r" test for
    	PR ld/15323.
    	(lto_run_tests): Add a "ld -r" test for PR ld/15323.
Comment 8 Sourceware Commits 2023-06-05 03:02:15 UTC
The master branch has been updated by YunQiang Su <syq@sourceware.org>:

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

commit a0631c1501c113c04891c9a24a9ff5276257f28d
Author: YunQiang Su <yunqiang.su@cipunited.com>
Date:   Tue May 16 09:46:46 2023 +0800

    MIPS: fix some ld testcases with compiler
    
    1. config/default.exp:
            use -mabi=32 not for -gnuabi64
            xfail_from_runlist: remove an element and mark it xfail.
    2. ld-elf/indirect.exp: xfail
            indirect5a indirect5b indirect6a indirect6b
            indirect5c indirect5d indirect6c indirect6d
    3. ld-elf/pr23658-2: mips output is not common
    4. ld-elf/shared.exp: non-run on mips: Build libpr16496b.so
    5. ld-elfvers/vers.exp:
            xfail vers4, vers4b
            no-run on mips: vers24a, vers24b, vers24c
    6. ld-gc/gc.exp: add -KPIC into asflags for pr13683, pr14265, pr19161
    7. ld-mips-elf/mips-elf.exp:
            use noarch for mips16-local-stubs-1, since it use -mips4
    8. ld-plugin/lto.exp:
            no-run on mips/linux: PR ld/12982
            add -KPIC into asflags for lto-3r, lto-5r, PR ld/19317 (2)
            xfail PR ld/15323 (4), PR ld/19317 (3)
    9. ld-plugin/plugin.exp: xfail
            plugin claimfile lost symbol
            plugin claimfile replace symbol
            plugin claimfile replace symbol
            plugin claimfile lost symbol with source
            plugin claimfile replace symbol with source
            plugin claimfile resolve symbol with source
            plugin 2 with source lib
            load plugin 2 with source
            plugin 3 with source lib
            load plugin 3 with source
    11. ld-selective/selective.exp: add -fno-PIC, which is needed for -mno-abicalls
    12. ld-shared/shared.exp: xfail shared (non PIC), shared (PIC main, non PIC so)