Bug 21306

Summary: Different behavior when linking common symbol statically or to shared object
Product: binutils Reporter: Eric Schweitz <eric.schweitz>
Component: ldAssignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED FIXED    
Severity: normal CC: amodra, hjl.tools
Priority: P2    
Version: 2.26   
Target Milestone: ---   
Host: Target:
Build: Last reconfirmed: 2017-04-04 00:00:00
Bug Depends on: 19579    
Bug Blocks:    
Attachments: reproduces behavior shown in report

Description Eric Schweitz 2017-03-24 19:33:10 UTC
Created attachment 9942 [details]
reproduces behavior shown in report

We discovered a change in behavior from Ubuntu 15.10 to Ubuntu 16.04. The difference appears to be the former has

  GNU ld (GNU Binutils for Ubuntu) 2.25.1

and the latter

  GNU ld (GNU Binutils for Ubuntu) 2.26.1

Attached is a stripped down example. With ld 2.25.1, we observe that the tentative definition of globalInt is resolved to the definition appearing in con.o (static) or libcon.so (shared). 

% ./app.static 
globalInt is 7
% LD_LIBRARY_PATH=. ./app.shared
globalInt is 7

In ld 2.26.1, we observe that the tentative definition gets resolved the same way with the static link, but does not get resolved with the shared link the same way. Instead, the tentative definition is resolved to a new, secondary instance (in .bss rather than .data).

% ./app.static 
globalInt is 7
% LD_LIBRARY_PATH=. ./app.shared
globalInt is 0
Comment 1 Alan Modra 2017-04-04 22:45:26 UTC
This behaviour was introduced with git commit 07492f668d.
https://sourceware.org/ml/binutils/2016-03/msg00082.html
Comment 2 H.J. Lu 2017-04-05 16:39:28 UTC
Dup.

*** This bug has been marked as a duplicate of bug 19579 ***
Comment 3 Alan Modra 2017-04-05 23:07:29 UTC
This isn't a dup.  This bug is reporting a regression introduced by the fix for bug 19579.
Comment 4 Sourceware Commits 2017-04-07 14:41:21 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=8170f7693bc0a9442c0aa280197925db92d48ca6

commit 8170f7693bc0a9442c0aa280197925db92d48ca6
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Fri Apr 7 07:40:14 2017 -0700

    ELF: Check ELF_COMMON_DEF_P for common symbols
    
    Since common symbols that are turned into definitions don't have the
    DEF_REGULAR flag set, we need to check ELF_COMMON_DEF_P for common
    symbols.
    
    bfd/
    
    	PR ld/19579
    	PR ld/21306
    	* elf32-s390.c (elf_s390_finish_dynamic_symbol): Check
    	ELF_COMMON_DEF_P for common symbols.
    	* elf64-s390.c (elf_s390_finish_dynamic_symbol): Likewise.
    	* elf64-x86-64.c (elf_x86_64_relocate_section): Likewise.
    	* elflink.c (_bfd_elf_merge_symbol): Revert commits
    	202ac193bbbecc96a4978d1ac3d17148253f9b01 and
    	07492f668d2173da7a2bda3707ff0985e0f460b6.
    
    ld/
    
    	PR ld/19579
    	PR ld/21306
    	* testsuite/ld-elf/pr19579a.c (main): Updated.
Comment 5 Sourceware Commits 2017-04-10 16:04:28 UTC
The binutils-2_28-branch branch has been updated by H.J. Lu <hjl@sourceware.org>:

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

commit 5a0ac7ba3c5277b3ab04f55de23bccb7614a9e59
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Fri Apr 7 07:40:14 2017 -0700

    ELF: Check ELF_COMMON_DEF_P for common symbols
    
    Since common symbols that are turned into definitions don't have the
    DEF_REGULAR flag set, we need to check ELF_COMMON_DEF_P for common
    symbols.
    
    bfd/
    
    	PR ld/19579
    	PR ld/21306
    	* elf32-s390.c (elf_s390_finish_dynamic_symbol): Check
    	ELF_COMMON_DEF_P for common symbols.
    	* elf64-s390.c (elf_s390_finish_dynamic_symbol): Likewise.
    	* elf64-x86-64.c (elf_x86_64_relocate_section): Likewise.
    	* elflink.c (_bfd_elf_merge_symbol): Revert commits
    	202ac193bbbecc96a4978d1ac3d17148253f9b01 and
    	07492f668d2173da7a2bda3707ff0985e0f460b6.
    
    ld/
    
    	PR ld/19579
    	PR ld/21306
    	* testsuite/ld-elf/pr19579a.c (main): Updated.
    
    (cherry picked from commit 8170f7693bc0a9442c0aa280197925db92d48ca6)
Comment 6 H.J. Lu 2017-04-10 16:06:58 UTC
Fixed for master and 2.28 branch.