From: H.J. Lu Date: Fri, 13 Mar 2020 14:34:56 +0000 (-0700) Subject: x86: Check static link of dynamic objects X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=74e10d1742f1b8312359c59a2af06c9e661252b3;p=binutils-gdb.git x86: Check static link of dynamic objects 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. --- diff --git a/bfd/ChangeLog b/bfd/ChangeLog index 70774e5cfc8..8b090938624 100644 --- a/bfd/ChangeLog +++ b/bfd/ChangeLog @@ -1,3 +1,13 @@ +2020-03-13 H.J. Lu + + 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 * elf.c (elfcore_grok_netbsd_note): Add support for aarch64. diff --git a/bfd/elf-linker-x86.h b/bfd/elf-linker-x86.h index 8ede7299dd6..d0cb20dde52 100644 --- a/bfd/elf-linker-x86.h +++ b/bfd/elf-linker-x86.h @@ -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; diff --git a/bfd/elfxx-x86.c b/bfd/elfxx-x86.c index 2e0e01e31d0..108e04a1588 100644 --- a/bfd/elfxx-x86.c +++ b/bfd/elfxx-x86.c @@ -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; } diff --git a/ld/ChangeLog b/ld/ChangeLog index 8ac66fc2e58..74d2f1b41fe 100644 --- a/ld/ChangeLog +++ b/ld/ChangeLog @@ -1,3 +1,14 @@ +2020-03-13 H.J. Lu + + 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 * ldexp.c (fold_name): Return SIZEOF_HEADERS in bytes. diff --git a/ld/emulparams/elf32_x86_64.sh b/ld/emulparams/elf32_x86_64.sh index 249553aaac1..1f672c6e426 100644 --- a/ld/emulparams/elf32_x86_64.sh +++ b/ld/emulparams/elf32_x86_64.sh @@ -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" diff --git a/ld/emulparams/elf_i386.sh b/ld/emulparams/elf_i386.sh index 645707ddbdb..c98d5e6600e 100644 --- a/ld/emulparams/elf_i386.sh +++ b/ld/emulparams/elf_i386.sh @@ -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 diff --git a/ld/emulparams/elf_x86_64.sh b/ld/emulparams/elf_x86_64.sh index 2b34941986e..be98082982f 100644 --- a/ld/emulparams/elf_x86_64.sh +++ b/ld/emulparams/elf_x86_64.sh @@ -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 index 00000000000..410839b8a56 --- /dev/null +++ b/ld/emulparams/static.sh @@ -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; +" diff --git a/ld/emultempl/elf-x86.em b/ld/emultempl/elf-x86.em index 8de44916991..e18caffbb26 100644 --- a/ld/emultempl/elf-x86.em +++ b/ld/emultempl/elf-x86.em @@ -22,6 +22,7 @@ # fragment <