x86: Check static link of dynamic objects
authorH.J. Lu <hjl.tools@gmail.com>
Fri, 13 Mar 2020 14:34:56 +0000 (07:34 -0700)
committerH.J. Lu <hjl.tools@gmail.com>
Fri, 13 Mar 2020 14:39:06 +0000 (07:39 -0700)
On Linux/x86, when -static is passed to gcc, gcc passes it to linker
before all input files suitable for creating static executable.  X86
linker will report error for dynamic input objects if -static is passed
at command-line before all input files without --dynamic-linker unless
--no-dynamic-linker is used.

bfd/

PR ld/24920
* elf-linker-x86.h (elf_linker_x86_params): Add
static_before_all_inputs and has_dynamic_linker.
* elfxx-x86.c (_bfd_x86_elf_link_setup_gnu_properties): Report
dynamic input objects if -static is passed at command-line
before all input files without --dynamic-linker unless
--no-dynamic-linker is used.

ld/

PR ld/24920
* emulparams/elf32_x86_64.sh: Use static.sh.
* emulparams/elf_i386.sh: Likewise.
* emulparams/elf_x86_64.sh: Likewise.
* emulparams/static.sh: New file.
* emultempl/elf-x86.em: Include "ldlex.h".
* testsuite/ld-elf/pr24920.err: New file.
* testsuite/ld-elf/linux-x86.exp: Run ld/24920 tests.

bfd/ChangeLog
bfd/elf-linker-x86.h
bfd/elfxx-x86.c
ld/ChangeLog
ld/emulparams/elf32_x86_64.sh
ld/emulparams/elf_i386.sh
ld/emulparams/elf_x86_64.sh
ld/emulparams/static.sh [new file with mode: 0644]
ld/emultempl/elf-x86.em
ld/testsuite/ld-elf/linux-x86.exp
ld/testsuite/ld-elf/pr24920.err [new file with mode: 0644]

index 70774e5cfc888b76f656b00258c816885ad16002..8b0909386242887218c2b5706dec7ed7a478afea 100644 (file)
@@ -1,3 +1,13 @@
+2020-03-13  H.J. Lu  <hongjiu.lu@intel.com>
+
+       PR ld/24920
+       * elf-linker-x86.h (elf_linker_x86_params): Add
+       static_before_all_inputs and has_dynamic_linker.
+       * elfxx-x86.c (_bfd_x86_elf_link_setup_gnu_properties): Report
+       dynamic input objects if -static is passed at command-line
+       before all input files without --dynamic-linker unless
+       --no-dynamic-linker is used.
+
 2020-03-13  Kamil Rytarowski  <n54@gmx.com>
 
        * elf.c (elfcore_grok_netbsd_note): Add support for aarch64.
index 8ede7299dd64f8018ba08cbced5c6ac6ae4206a3..d0cb20dde527944e1268e16828e5e66b31d18181 100644 (file)
@@ -49,6 +49,12 @@ struct elf_linker_x86_params
   /* TRUE if generate a 1-byte NOP as suffix for x86 call instruction.  */
   unsigned int call_nop_as_suffix : 1;
 
+  /* TRUE if -static is passed at command-line before all input files.  */
+  unsigned int static_before_all_inputs : 1;
+
+  /* TRUE if --dynamic-linker is passed at command-line.  */
+  unsigned int has_dynamic_linker : 1;
+
   /* Report missing IBT and SHSTK properties.  */
   enum elf_x86_cet_report cet_report;
 
index 2e0e01e31d0d81bf9b6da15c2e06cdb3418e3cf1..108e04a1588e0f48ed9c5a186b21afbc2756d9d6 100644 (file)
@@ -2998,6 +2998,23 @@ _bfd_x86_elf_link_setup_gnu_properties
                                  : bed->plt_alignment);
     }
 
+  if (bfd_link_executable (info)
+      && !info->nointerp
+      && !htab->params->has_dynamic_linker
+      && htab->params->static_before_all_inputs)
+    {
+      /* Report error for dynamic input objects if -static is passed at
+        command-line before all input files without --dynamic-linker
+        unless --no-dynamic-linker is used.  */
+      bfd *abfd;
+
+      for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
+       if ((abfd->flags & DYNAMIC))
+         info->callbacks->einfo
+           (_("%X%P: attempted static link of dynamic object `%pB'\n"),
+            abfd);
+    }
+
   return pbfd;
 }
 
index 8ac66fc2e58bdca2aea702b7022d97d9a4de74e3..74d2f1b41fe60b2e7861d65a8e830d6f894e6ee8 100644 (file)
@@ -1,3 +1,14 @@
+2020-03-13  H.J. Lu  <hongjiu.lu@intel.com>
+
+       PR ld/24920
+       * emulparams/elf32_x86_64.sh: Use static.sh.
+       * emulparams/elf_i386.sh: Likewise.
+       * emulparams/elf_x86_64.sh: Likewise.
+       * emulparams/static.sh: New file.
+       * emultempl/elf-x86.em: Include "ldlex.h".
+       * testsuite/ld-elf/pr24920.err: New file.
+       * testsuite/ld-elf/linux-x86.exp: Run ld/24920 tests.
+
 2020-03-13  Christian Eggers  <ceggers@gmx.de>
 
        * ldexp.c (fold_name): Return SIZEOF_HEADERS in bytes.
index 249553aaac1c832f3480db3e10f97ea5b91f6f87..1f672c6e42644373ec70030ad1623648cc12d8af 100644 (file)
@@ -4,6 +4,7 @@ source_sh ${srcdir}/emulparams/dynamic_undefined_weak.sh
 source_sh ${srcdir}/emulparams/reloc_overflow.sh
 source_sh ${srcdir}/emulparams/call_nop.sh
 source_sh ${srcdir}/emulparams/cet.sh
+source_sh ${srcdir}/emulparams/static.sh
 SCRIPT_NAME=elf
 ELFSIZE=32
 OUTPUT_FORMAT="elf32-x86-64"
index 645707ddbdb8304e7af731ed79ff8c346d6d2ea3..c98d5e6600e678e62ce08530132e20e98e69c757 100644 (file)
@@ -3,6 +3,7 @@ source_sh ${srcdir}/emulparams/extern_protected_data.sh
 source_sh ${srcdir}/emulparams/dynamic_undefined_weak.sh
 source_sh ${srcdir}/emulparams/call_nop.sh
 source_sh ${srcdir}/emulparams/cet.sh
+source_sh ${srcdir}/emulparams/static.sh
 SCRIPT_NAME=elf
 OUTPUT_FORMAT="elf32-i386"
 NO_RELA_RELOCS=yes
index 2b34941986ec920598c42e86d8fb0981c3f6d85c..be98082982f58076d98d4cef31390e3b310d7231 100644 (file)
@@ -4,6 +4,7 @@ source_sh ${srcdir}/emulparams/dynamic_undefined_weak.sh
 source_sh ${srcdir}/emulparams/reloc_overflow.sh
 source_sh ${srcdir}/emulparams/call_nop.sh
 source_sh ${srcdir}/emulparams/cet.sh
+source_sh ${srcdir}/emulparams/static.sh
 SCRIPT_NAME=elf
 ELFSIZE=64
 OUTPUT_FORMAT="elf64-x86-64"
diff --git a/ld/emulparams/static.sh b/ld/emulparams/static.sh
new file mode 100644 (file)
index 0000000..410839b
--- /dev/null
@@ -0,0 +1,12 @@
+PARSE_AND_LIST_ARGS_CASES="$PARSE_AND_LIST_ARGS_CASES
+    case OPTION_DYNAMIC_LINKER:
+      params.has_dynamic_linker = TRUE;
+      return FALSE;
+
+    case OPTION_NON_SHARED:
+      /* Check if -static is passed at command-line before all input
+        files.  */
+      if (!lang_has_input_file)
+       params.static_before_all_inputs = TRUE;
+      return FALSE;
+"
index 8de449169919959e6d4b3cc2e7e227771a104800..e18caffbb26dadad26104712f714f7b64f171de2 100644 (file)
@@ -22,6 +22,7 @@
 #
 fragment <<EOF
 
+#include "ldlex.h"
 #include "elf-linker-x86.h"
 
 static struct elf_linker_x86_params params;
index d304fb14427f7c8f45d1b9f15bb43fcf5c1ee1c8..63a321b9668469b9dff7e997641c13b338b6299a 100644 (file)
 # MA 02110-1301, USA.
 #
 
+# Linux/x86 tests.
+if { ![istarget "i?86-*-linux*"] \
+       && ![istarget "x86_64-*-linux*"] \
+       && ![istarget "amd64-*-linux*"] } {
+    return
+}
+
+run_ld_link_tests [list \
+    [list \
+       "Build pr24920.so" \
+       "-shared" \
+       "" \
+       "" \
+       {dummy.s} \
+       {} \
+       "pr24920.so" \
+    ] \
+    [list \
+       "Build pr24920" \
+       "-static " \
+       "-Bdynamic tmpdir/pr24920.so" \
+       "" \
+       {start.s} \
+       {{ld pr24920.err}} \
+       "pr24920" \
+    ] \
+]
+
 # Test very simple native Linux/x86 programs with linux-x86.S.
-if { ![isnative] || ![check_compiler_available] \
-     || (![istarget "i?86-*-linux*"] \
-         && ![istarget "x86_64-*-linux*"] \
-         && ![istarget "amd64-*-linux*"]) } {
+if { ![isnative] || ![check_compiler_available] } {
     return
 }
 
diff --git a/ld/testsuite/ld-elf/pr24920.err b/ld/testsuite/ld-elf/pr24920.err
new file mode 100644 (file)
index 0000000..8f5cab9
--- /dev/null
@@ -0,0 +1 @@
+.*: attempted static link of dynamic object `tmpdir/pr24920.so'