From 514b4e191d5f46de8e142fe216e677a35fa9c4bb Mon Sep 17 00:00:00 2001 From: Jeremy Drake Date: Thu, 27 Aug 2020 12:58:27 +0100 Subject: [PATCH] Change the default characteristics of DLLs built by the linker to more secure settings. PR 19011 * emultempl/pe.em (DEFAULT_DLL_CHARACTERISTICS): Define. (pe_dll_characteristics): Initialise to DEFAULT_DLL_CHARACTERISTICS. (add_options): Add options to disable DLL characteristics. (list_options): List the new options. (handle_options): Handle the new options. * emultempl/pep.em: Similar changes to above. (NT_EXE_IMAGE_BASE): Default to an address above 4G. (NT_DLL_IMAGE_BASE, NT_DLL_AUTO_IMAGE_BASE, (NT_DLL_AUTO_IMAGE_MASK): Likewise. * ld.texi: Document the new options. * pe-dll.c (pe_dll_enable_reloc_section): Change to default to true. (generate_reloc): Do nothing if there is no reloc section. (pe_exe_fill_sections): Only assign the reloc section contents if the section exists. * testsuite/ld-pe/pe.exp: Add the --disable-reloc-section flag to the .secrel32 tests. * testsuite/ld-scripts/provide-8.d: Expect for fail on PE targets. * NEWS: Mention the change in DLL generation. --- ld/ChangeLog | 23 ++++++++ ld/NEWS | 3 + ld/emultempl/pe.em | 75 ++++++++++++++++++++---- ld/emultempl/pep.em | 91 +++++++++++++++++++++++------ ld/ld.texi | 34 ++++++++--- ld/pe-dll.c | 10 +++- ld/testsuite/ld-pe/pe.exp | 6 +- ld/testsuite/ld-scripts/provide-8.d | 2 +- 8 files changed, 201 insertions(+), 43 deletions(-) diff --git a/ld/ChangeLog b/ld/ChangeLog index 8bdc015e6d..7a00278508 100644 --- a/ld/ChangeLog +++ b/ld/ChangeLog @@ -1,3 +1,26 @@ +2020-08-27 Jeremy Drake + + PR 19011 + * emultempl/pe.em (DEFAULT_DLL_CHARACTERISTICS): Define. + (pe_dll_characteristics): Initialise to DEFAULT_DLL_CHARACTERISTICS. + (add_options): Add options to disable DLL characteristics. + (list_options): List the new options. + (handle_options): Handle the new options. + * emultempl/pep.em: Similar changes to above. + (NT_EXE_IMAGE_BASE): Default to an address above 4G. + (NT_DLL_IMAGE_BASE, NT_DLL_AUTO_IMAGE_BASE, + (NT_DLL_AUTO_IMAGE_MASK): Likewise. + * ld.texi: Document the new options. + * pe-dll.c (pe_dll_enable_reloc_section): Change to default to + true. + (generate_reloc): Do nothing if there is no reloc section. + (pe_exe_fill_sections): Only assign the reloc section contents if + the section exists. + * testsuite/ld-pe/pe.exp: Add the --disable-reloc-section flag to + the .secrel32 tests. + * testsuite/ld-scripts/provide-8.d: Expect for fail on PE targets. + * NEWS: Mention the change in DLL generation. + 2020-08-26 H.J. Lu * testsuite/ld-elf/indirect.exp: Append $NOSANTIZE_CFLAGS to CC. diff --git a/ld/NEWS b/ld/NEWS index 0fea911fea..695348141b 100644 --- a/ld/NEWS +++ b/ld/NEWS @@ -1,5 +1,8 @@ -*- text -*- +* The creation of PE format DLLs now defaults to using a more secure set of DLL + characteristics. + * The linker now deduplicates the types in .ctf sections. The new command-line option --ctf-share-types describes how to do this: its default value, share-unconflicted, produces the most compact diff --git a/ld/emultempl/pe.em b/ld/emultempl/pe.em index eb7c812470..9d2097f486 100644 --- a/ld/emultempl/pe.em +++ b/ld/emultempl/pe.em @@ -104,6 +104,9 @@ fragment <input_bfds; b; b = b->link.next) for (s = b->sections; s; s = s->next) @@ -1548,9 +1549,11 @@ generate_reloc (bfd *abfd, struct bfd_link_info *info) if (s->output_section->vma == 0) { /* Huh? Shouldn't happen, but punt if it does. */ +#if 0 /* This happens when linking with --just-symbols=, so do not generate an error. */ einfo (_("%P: zero vma section reloc detected: `%s' #%d f=%d\n"), s->output_section->name, s->output_section->index, s->output_section->flags); +#endif continue; } @@ -3631,7 +3634,8 @@ pe_exe_fill_sections (bfd *abfd, struct bfd_link_info *info) /* Do the assignments again. */ lang_do_assignments (lang_final_phase_enum); } - reloc_s->contents = reloc_d; + if (reloc_s) + reloc_s->contents = reloc_d; } bfd_boolean diff --git a/ld/testsuite/ld-pe/pe.exp b/ld/testsuite/ld-pe/pe.exp index 7bc933fc5a..17dafbb02b 100644 --- a/ld/testsuite/ld-pe/pe.exp +++ b/ld/testsuite/ld-pe/pe.exp @@ -33,7 +33,7 @@ if {[istarget i*86-*-cygwin*] if {[istarget x86_64-*-mingw*] } { set pe_tests { - {".secrel32" "" "" "" {secrel1.s secrel2.s} + {".secrel32" "--disable-reloc-section" "" "" {secrel1.s secrel2.s} {{objdump -s secrel_64.d}} "secrel.x"} {"Empty export table" "" "" "" "exports.s" {{objdump -p exports64.d}} "exports.dll"} @@ -42,7 +42,7 @@ if {[istarget i*86-*-cygwin*] } } elseif {[istarget i*86-*-cygwin*] } { set pe_tests { - {".secrel32" "--disable-auto-import" "" "" {secrel1.s secrel2.s} + {".secrel32" "--disable-auto-import --disable-reloc-section" "" "" {secrel1.s secrel2.s} {{objdump -s secrel.d}} "secrel.x"} {"Empty export table" "" "" "" "exports.s" {{objdump -p exports.d}} "exports.dll"} @@ -51,7 +51,7 @@ if {[istarget i*86-*-cygwin*] } } else { set pe_tests { - {".secrel32" "" "" "" {secrel1.s secrel2.s} + {".secrel32" "--disable-reloc-section" "" "" {secrel1.s secrel2.s} {{objdump -s secrel.d}} "secrel.x"} {"Empty export table" "" "" "" "exports.s" {{objdump -p exports.d}} "exports.dll"} diff --git a/ld/testsuite/ld-scripts/provide-8.d b/ld/testsuite/ld-scripts/provide-8.d index 16510be9e9..1dd5553ed3 100644 --- a/ld/testsuite/ld-scripts/provide-8.d +++ b/ld/testsuite/ld-scripts/provide-8.d @@ -1,7 +1,7 @@ #source: provide-5.s #ld: -T provide-8.t #nm: -B -#xfail: mmix-*-* sh-*-pe spu-*-* +#xfail: mmix-*-* *-*-pe spu-*-* *-*-mingw* *-*-cygwin #... 0+4000 D __FOO -- 2.34.1