Bug 21086 - static linking with --dynamic-list adds dynamic section and interpreter
Summary: static linking with --dynamic-list adds dynamic section and interpreter
Status: RESOLVED FIXED
Alias: None
Product: binutils
Classification: Unclassified
Component: ld (show other bugs)
Version: 2.29
: P2 normal
Target Milestone: 2.29
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2017-01-25 15:02 UTC by Szabolcs Nagy
Modified: 2017-04-21 19:04 UTC (History)
3 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 Szabolcs Nagy 2017-01-25 15:02:06 UTC
i think static linking should work (produce a static
linked executable without any dynamic linker) even
with --dynamic-list, now the produced executable is
broken.

reproducer:

echo 'int main(){}' >a.c
echo '{main;};' >foo.list
gcc -static -Wl,--dynamic-list=foo.list a.c
readelf -l a.out |grep interpreter
      [Requesting program interpreter: /lib/ld64.so.1]

(this is on x86_64, but other targets have the same behaviour)

introduced in

https://sourceware.org/ml/binutils/2016-02/msg00251.html

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=bf89386a862ace008f0152bca8bddf996d3993c8
Comment 1 Szabolcs Nagy 2017-01-25 15:05:35 UTC
also discussed at
https://sourceware.org/ml/binutils/2017-01/msg00429.html
Comment 2 H.J. Lu 2017-01-25 16:14:08 UTC
Since you asked to put "main" in dynamic symbol table, linker does
what you requested.  If you don't want to put "main" in dynamic symbol
table, you shouldn't use -Wl,--dynamic-list=foo.list.
Comment 3 Rich Felker 2017-01-25 17:04:48 UTC
--dynamic-list is not a request to export the symbol in the dynamic symbol table; -E/-rdynamic does that. --dynamic-list is an exclusion list for -Bsymbolic that makes everything not in the list get treated as -Bsymbolic.

As for using --dynamic-list, it's gdb (part of the binutils tree) that's using it in its build process and thus breaking static linking.
Comment 4 H.J. Lu 2017-01-25 17:34:16 UTC
(In reply to Rich Felker from comment #3)
> --dynamic-list is not a request to export the symbol in the dynamic symbol
> table; -E/-rdynamic does that. --dynamic-list is an exclusion list for
> -Bsymbolic that makes everything not in the list get treated as -Bsymbolic.

-E puts all global symbols in dynamic symbol table and --dynamic-list puts
symbols listed in file in dynamic symbol table.  If no symbols should be
put in dynamic symbol table, --dynamic-list shouldn't be used.
Comment 5 ma.jiang 2017-04-12 02:25:06 UTC
Hi guys,
  I encoutered this bug when trying to compile a static-linked perf. In my option, there were two serious problem here. 
  First of all, ld should never create a broken executable silently. If "-static" is conflicted with any other option, ld should abort and print information about the conflict.
  Secondly, "--dynamic-list" should be documented more clearly and directly. 

"Specify the name of a dynamic list file to the linker.  This is typically used when creating shared libraries to specify a list of global symbols whose references shouldn't be bound to the definition within the shared library, or creating dynamically linked executables to specify a list of symbols which should be added to the symbol table in the executable."
  This paragraph(listed above) in man pages is too obscure to tell users what the option really mean. I thinks we should clearly describe what the option do(and some symbol into dynamic symbol table?) first, and then tell users what are the typical scenarios.
Comment 6 Sourceware Commits 2017-04-21 19:01:54 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=3c5fce9bc29b216af7d10f8d6e4d8c3f11a48359

commit 3c5fce9bc29b216af7d10f8d6e4d8c3f11a48359
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Fri Apr 21 12:00:55 2017 -0700

    Require --no-dynamic-linker with -static -E/--dynamic-list
    
    When -static -E/--dynamic-list are passed to linker, linker may create
    executable with dynamic sections which aren't supported by run-time.
    We require --no-dynamic-linker together with -static -E/--dynamic-list
    before adding dynamic symbol table to static executable.
    
    bfd/
    
    	PR ld/19617
    	PR ld/21086
    	* elflink.c (elf_link_add_object_symbols): Require
    	--no-dynamic-linker with -E/--dynamic-list when creating
    	dynamic sections.
    
    ld/
    
    	PR ld/19617
    	PR ld/21086
    	* testsuite/ld-elf/pr19617a.d: Pass --no-dynamic-linker to ld.
    	* testsuite/ld-elf/pr19617b.d: Likewise.
    	* testsuite/ld-elf/pr19617c.d: Likewise.
    	*testsuite/ld-i386/pr19636-4d.d: Likewise.
    	* testsuite/ld-elf/readelf.exp: Pass --no-dynamic-linker to ld
    	with --export-dynamic.
    	* testsuite/ld-elf/shared.exp: Pass --no-dynamic-linker to ld
    	with -E.
Comment 7 H.J. Lu 2017-04-21 19:04:27 UTC
Fixed for 2.29.