If neither -pic, -pie, or -shared is specified, *and* there are no undefined symbols in the executable, ld produces no dynamic sections. However, if -E is specified, the executable is supposed to export its symbols to modules loaded in the future. This configuration is common in embedded environments, including boot loaders. The current workaround is to use -pie, but that generates a manifest .rel.dyn section which is pure waste if -pie isn't actually required.
Created attachment 8979 [details] A patch Try this with master branch.
You also can use --dynamic-list FILE Read dynamic list to specify which symbols to export.
The patch works.
The master branch has been updated by H.J. Lu <hjl@sourceware.org>: https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=bf89386a862ace008f0152bca8bddf996d3993c8 commit bf89386a862ace008f0152bca8bddf996d3993c8 Author: H.J. Lu <hjl.tools@gmail.com> Date: Thu Feb 18 03:13:19 2016 -0800 Always create dynamic sections for -E/--dynamic-list In embedded environments, including boot loaders, the non-PIC executable needs to export its symbols to modules loaded in the future. We should always create dynamic sections for -E/--dynamic-list. bfd/ PR ld/19617 * elflink.c (elf_link_add_object_symbols): Always create dynamic sections for -E/--dynamic-list. ld/ PR ld/19617 * testsuite/ld-elf/pr19617.s: New file. * testsuite/ld-elf/pr19617a.d: Likewise. * testsuite/ld-elf/pr19617b.d: Likewise. * testsuite/ld-elf/pr19617c.d: Likewise.
Fixed for 2.27.
The master branch has been updated by H.J. Lu <hjl@sourceware.org>: https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=c304e18e5ca825f57963bd0c5f022fa8f5797b29 commit c304e18e5ca825f57963bd0c5f022fa8f5797b29 Author: H.J. Lu <hjl.tools@gmail.com> Date: Thu Feb 18 07:48:57 2016 -0800 Enable PR ld/19617 tests only for Linux/GNU/Solaris Since PR ld/19617 tests require share library support, enable them only for Linux/GNU/Solaris targets. * testsuite/ld-elf/pr19617a.d: Enable only for *-*-linux*, *-*-gnu* and *-*-solaris*. * testsuite/ld-elf/pr19617b.d: Likewise. * testsuite/ld-elf/pr19617c.d: Likewise.
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.