Bug 19719 - Mixing PIC and non-PIC input files generates unexpected result
Summary: Mixing PIC and non-PIC input files generates unexpected result
Status: RESOLVED FIXED
Alias: None
Product: binutils
Classification: Unclassified
Component: ld (show other bugs)
Version: 2.27
: P2 normal
Target Milestone: 2.27
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-02-24 14:44 UTC by H.J. Lu
Modified: 2016-04-20 03:14 UTC (History)
0 users

See Also:
Host:
Target: x86
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 2016-02-24 14:44:56 UTC
[hjl@gnu-6 weak-4]$ cat foo.c 
extern int __attribute__ ((weak)) fun (void);
int
foo (void)
{
  if (&fun != 0)
    return fun ();
  return 0;
}
[hjl@gnu-6 weak-4]$ cat bar.c 
extern int __attribute__ ((weak)) fun (void);
int
bar (void)
{
  if (&fun != 0)
    return fun ();
  return 0;
}
[hjl@gnu-6 weak-4]$ cat main.c 
#include <stdlib.h>

extern int foo (void);
extern int bar (void);

int
main (void)
{
  if (foo () != bar ())
    abort ();
  return 0;
}
[hjl@gnu-6 weak-4]$ cat fun1.c 
/* Dummy */
[hjl@gnu-6 weak-4]$ cat fun2.c 
int
fun (void)
{
  return 20;
}
[hjl@gnu-6 weak-4]$ make
gcc -B./ -O2   -c -o foo.o foo.c
gcc -B./ -O2 -fPIC   -c -o bar.o bar.c
gcc -B./ -O2   -c -o main.o main.c
gcc -B./ -shared -Wl,-soname,libfun.so -fPIC -O2 -o libfun1.so fun1.c
gcc -B./ -shared -Wl,-soname,libfun.so -fPIC -O2 -o libfun2.so fun2.c
ln -sf libfun1.so libfun.so
gcc -B./ -O2 -o x foo.o bar.o main.o libfun.so -Wl,-R,.
ln -sf libfun1.so libfun.so
./x
ln -sf libfun2.so libfun.so
./x
Makefile:20: recipe for target 'all' failed
make: *** [all] Aborted
[hjl@gnu-6 weak-4]$
Comment 1 cvs-commit@gcc.gnu.org 2016-02-26 12:56:50 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=aec6b87e0b66d707ead62ca40d220ee78b4cf5a5

commit aec6b87e0b66d707ead62ca40d220ee78b4cf5a5
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Fri Feb 26 04:16:15 2016 -0800

    [x86] Resolve non-PIC undefweak symbols in executable
    
    For i386 and x86-64, non-PIC references to undefined weak symbols are
    resolved without dynamic relocation when creating executable.  Resolved
    undefined weak symbols are removed from the dynamic symbol table in
    executable.  One exception is on i386, we need resolved undefined weak
    symbols in the dynamic symbol table in PIE if input relocatable files
    contain branchs without PLT so that we can branch to 0 with dynamic
    relocation in text section.
    
    This makes behaviors of dynamic executable and position independent
    executable predictable with mixed PIC and non-PIC references to undefined
    weak symbols.  If all references to undefined weak symbols are PIC,
    dynamic relocations against undefined weak symbols will be generated
    in executable unless -z nodynamic-undefined-weak is passed to linker.
    
    bfd/
    
    	PR ld/19636
    	PR ld/19704
    	PR ld/19719
    	* elf32-i386.c (UNDEFINED_WEAK_RESOLVED_TO_ZERO): New.
    	(elf_i386_link_hash_entry): Add has_got_reloc and
    	has_non_got_reloc.
    	(elf_i386_link_hash_table): Add interp.
    	(elf_i386_link_hash_newfunc): Initialize has_got_reloc and
    	has_non_got_reloc.
    	(elf_i386_copy_indirect_symbol): Copy has_got_reloc and
    	has_non_got_reloc.
    	(elf_i386_check_relocs): Set has_got_reloc and has_non_got_reloc.
    	(elf_i386_fixup_symbol): New function.
    	(elf_i386_pie_finish_undefweak_symbol): Likewise.
    	(elf_i386_allocate_dynrelocs): Don't allocate space for dynamic
    	relocations and discard relocations against resolved undefined
    	weak symbols in executable.  Don't make resolved undefined weak
    	symbols in executable dynamic.  Keep dynamic non-GOT/non-PLT
    	relocation against undefined weak symbols in PIE.
    	(elf_i386_size_dynamic_sections): Set interp to .interp section.
    	(elf_i386_relocate_section): Don't generate dynamic relocations
    	against resolved undefined weak symbols in PIE, except for
    	R_386_PC32.
    	(elf_i386_finish_dynamic_symbol): Keep PLT/GOT entries without
    	dynamic PLT/GOT relocations for resolved undefined weak symbols.
    	Don't generate dynamic relocation against resolved undefined weak
    	symbol in executable.
    	(elf_i386_finish_dynamic_sections): Call
    	elf_i386_pie_finish_undefweak_symbol on all symbols in PIE.
    	(elf_backend_fixup_symbol): New.
    	* elf64-x86-64.c (UNDEFINED_WEAK_RESOLVED_TO_ZERO): New.
    	(elf_x86_64_link_hash_entry): Add has_got_reloc and
    	has_non_got_reloc.
    	(elf_x86_64_link_hash_table): Add interp.
    	(elf_x86_64_link_hash_newfunc): Initialize has_got_reloc and
    	has_non_got_reloc.
    	(elf_x86_64_copy_indirect_symbol): Copy has_got_reloc and
    	has_non_got_reloc.
    	(elf_x86_64_check_relocs): Set has_got_reloc and
    	has_non_got_reloc.
    	(elf_x86_64_fixup_symbol): New function.
    	(elf_x86_64_pie_finish_undefweak_symbol): Likewise.
    	(elf_x86_64_allocate_dynrelocs): Don't allocate space for dynamic
    	relocations and discard relocations against resolved undefined
    	weak symbols in executable.  Don't make resolved undefined weak
    	symbols in executable dynamic.
    	(elf_x86_64_size_dynamic_sections): Set interp to .interp section.
    	(elf_x86_64_relocate_section): Check relocation overflow for
    	dynamic relocations against unresolved weak undefined symbols.
    	Don't generate dynamic relocations against resolved weak
    	undefined symbols in PIE.
    	(elf_x86_64_finish_dynamic_symbol): Keep PLT/GOT entries without
    	dynamic PLT/GOT relocations for resolved undefined weak symbols.
    	Don't generate dynamic relocation against resolved undefined weak
    	symbol in executable.
    	(elf_x86_64_finish_dynamic_sections): Call
    	elf_x86_64_pie_finish_undefweak_symbol on all symbols in PIE.
    	(elf_backend_fixup_symbol): New.
    
    include/
    
    	PR ld/19636
    	PR ld/19704
    	PR ld/19719
    	* bfdlink.h (bfd_link_info): Add dynamic_undefined_weak.
    
    ld/
    
    	PR ld/19636
    	PR ld/19704
    	PR ld/19719
    	* Makefile.am (ELF_X86_DEPS): Add dynamic_undefined_weak.sh.
    	* Makefile.in: Regenerated.
    	* NEWS: Mention -z nodynamic-undefined-weak.
    	* ld.texinfo: Document -z nodynamic-undefined-weak.
    	* ldmain.c (main): Initialize dynamic_undefined_weak to -1.
    	* emulparams/dynamic_undefined_weak.sh: New file.
    	* emulparams/elf32_x86_64.sh: Source dynamic_undefined_weak.sh.
    	* emulparams/elf_i386.sh: Likewise.
    	* emulparams/elf_i386_be.sh: Likewise.
    	* emulparams/elf_i386_chaos.sh: Likewise.
    	* emulparams/elf_i386_ldso.sh: Likewise.
    	* emulparams/elf_i386_vxworks.sh: Likewise.
    	* emulparams/elf_iamcu.sh: Likewise.
    	* emulparams/elf_k1om.sh: Likewise.
    	* emulparams/elf_l1om.sh: Likewise.
    	* emulparams/elf_x86_64.sh: Likewise.
    	* emulparams/extern_protected_data.sh (PARSE_AND_LIST_OPTIONS):
    	Append.
    	(PARSE_AND_LIST_ARGS_CASE_Z): Likewise.
    	* testsuite/ld-elf/pr19719a.c: New file.
    	* testsuite/ld-elf/pr19719b.c: Likewise.
    	* testsuite/ld-elf/pr19719c.c: Likewise.
    	* testsuite/ld-elf/pr19719d.c: Likewise.
    	* testsuite/ld-i386/pr19636-1.s: Likewise.
    	* testsuite/ld-i386/pr19636-1a.d: Likewise.
    	* testsuite/ld-i386/pr19636-1b.d: Likewise.
    	* testsuite/ld-i386/pr19636-1c.d: Likewise.
    	* testsuite/ld-i386/pr19636-1d-nacl.d: Likewise.
    	* testsuite/ld-i386/pr19636-1d.d: Likewise.
    	* testsuite/ld-i386/pr19636-1e.d: Likewise.
    	* testsuite/ld-i386/pr19636-1f.d: Likewise.
    	* testsuite/ld-i386/pr19636-1g.d: Likewise.
    	* testsuite/ld-i386/pr19636-1h.d: Likewise.
    	* testsuite/ld-i386/pr19636-1i.d: Likewise.
    	* testsuite/ld-i386/pr19636-2.s: Likewise.
    	* testsuite/ld-i386/pr19636-2a.d: Likewise.
    	* testsuite/ld-i386/pr19636-2b.d: Likewise.
    	* testsuite/ld-i386/pr19636-2c-nacl.d: Likewise.
    	* testsuite/ld-i386/pr19636-2c.d: Likewise.
    	* testsuite/ld-i386/pr19636-2d-nacl.d: Likewise.
    	* testsuite/ld-i386/pr19636-2d.d: Likewise.
    	* testsuite/ld-i386/pr19636-2e-nacl.d: Likewise.
    	* testsuite/ld-i386/pr19636-2e.d: Likewise.
    	* testsuite/ld-i386/pr19636-3.s: Likewise.
    	* testsuite/ld-i386/pr19636-3a.d: Likewise.
    	* testsuite/ld-i386/pr19636-3b.d: Likewise.
    	* testsuite/ld-i386/pr19636-3c.d: Likewise.
    	* testsuite/ld-i386/pr19636-3d.d: Likewise.
    	* testsuite/ld-i386/pr19636-3e.d: Likewise.
    	* testsuite/ld-i386/pr19636-3f.d: Likewise.
    	* testsuite/ld-i386/pr19636-3g.d: Likewise.
    	* testsuite/ld-i386/pr19636-4.s: Likewise.
    	* testsuite/ld-i386/pr19636-4a.d: Likewise.
    	* testsuite/ld-i386/pr19636-4b.d: Likewise.
    	* testsuite/ld-i386/pr19636-4c.d: Likewise.
    	* testsuite/ld-i386/pr19636-4d.d: Likewise.
    	* testsuite/ld-i386/pr19704.out: Likewise.
    	* testsuite/ld-i386/pr19704a.c: Likewise.
    	* testsuite/ld-i386/pr19704b.c: Likewise.
    	* testsuite/ld-x86-64/pr19636-1.s: Likewise.
    	* testsuite/ld-x86-64/pr19636-1a.d: Likewise.
    	* testsuite/ld-x86-64/pr19636-1b.d: Likewise.
    	* testsuite/ld-x86-64/pr19636-1c.d: Likewise.
    	* testsuite/ld-x86-64/pr19636-1d.d: Likewise.
    	* testsuite/ld-x86-64/pr19636-1e.d: Likewise.
    	* testsuite/ld-x86-64/pr19636-1f.d: Likewise.
    	* testsuite/ld-x86-64/pr19636-1g.d: Likewise.
    	* testsuite/ld-x86-64/pr19636-2.s: Likewise.
    	* testsuite/ld-x86-64/pr19636-2a.d: Likewise.
    	* testsuite/ld-x86-64/pr19636-2b.d: Likewise.
    	* testsuite/ld-x86-64/pr19636-2c.d: Likewise.
    	* testsuite/ld-x86-64/pr19636-2d-nacl.d: Likewise.
    	* testsuite/ld-x86-64/pr19636-2d.d: Likewise.
    	* testsuite/ld-x86-64/pr19636-2e.d: Likewise.
    	* testsuite/ld-x86-64/pr19636-2f.d: Likewise.
    	* testsuite/ld-x86-64/pr19636-2g.d: Likewise.
    	* testsuite/ld-x86-64/pr19636-2h.d: Likewise.
    	* testsuite/ld-x86-64/pr19636-2i.d: Likewise.
    	* testsuite/ld-x86-64/pr19636-3.s: Likewise.
    	* testsuite/ld-x86-64/pr19636-3a.d: Likewise.
    	* testsuite/ld-x86-64/pr19636-3b.d: Likewise.
    	* testsuite/ld-x86-64/pr19636-3c.d: Likewise.
    	* testsuite/ld-x86-64/pr19636-3d.d: Likewise.
    	* testsuite/ld-x86-64/pr19704.out: Likewise.
    	* testsuite/ld-x86-64/pr19704a.c: Likewise.
    	* testsuite/ld-x86-64/pr19704b.c: Likewise.
    	* testsuite/ld-elf/shared.exp (mix_pic_and_non_pic): New.
    	Run mix_pic_and_non_pic.
    	* testsuite/ld-i386/i386.exp (undefined_weak): New.
    	Run undefined_weak and PR ld/19636 tests.
    	* testsuite/ld-x86-64/x86-64.exp: Likewise.
    	* testsuite/ld-x86-64/pr13082-3b.d: Updated.
    	* testsuite/ld-x86-64/pr13082-4b.d: Likewise.
Comment 2 H.J. Lu 2016-02-26 13:16:21 UTC
Fixed.
Comment 3 cvs-commit@gcc.gnu.org 2016-04-20 03:14:32 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=87d455c042d2236b2afb1d6e13f337df36256bb0

commit 87d455c042d2236b2afb1d6e13f337df36256bb0
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Tue Apr 19 20:11:55 2016 -0700

    Add another testcase for PR ld/19719
    
    This test checks run-time relocation overflow with GOT relocation and
    32-bit relocation against the same symbol.
    
    	PR ld/19719
    	* testsuite/ld-x86-64/pr19719.d: New file.
    	* testsuite/ld-x86-64/pr19719.s: Likewise.
    	* testsuite/ld-x86-64/x86-64.exp: Run pr19719.