From 5fd104addfddb68844fb8df67be832ee98ad9888 Mon Sep 17 00:00:00 2001 From: Alan Modra Date: Fri, 19 Jun 2020 09:17:20 +0930 Subject: [PATCH] Emit a warning when -z relro is unsupported ld silently accepts -z relro and -z norelro for targets that lack the necessary GNU_RELRO support. This patch makes those targets emit a warning instead, and adds testsuite infrastructure to detect when relro is unsupported. binutils/ * testsuite/config/default.exp (ld_elf_shared_opt): Don't set. * testsuite/lib/binutils-common.exp (check_relro_support): New proc. (run_dump_test): Use check_relro_support to decide whether to pass extra ld option "-z norelro". ld/ * emultempl/elf.em (gld${EMULATION_NAME}_handle_option): Omit -z relro and -z norelro when target support for GNU_RELRO is lacking. (gld${EMULATION_NAME}_before_parse): Ignore RELRO default too. * emultempl/aarch64elf.em (gld${EMULATION_NAME}_before_parse): Ignore RELRO default when target support for GNU_RELRO is lacking. * emultempl/armelf.em (gld${EMULATION_NAME}_before_parse): Likewise. * emultempl/linux.em (gld${EMULATION_NAME}_before_parse): Likewise. * emultempl/scoreelf.em (gld${EMULATION_NAME}_before_parse): Likewise. * testsuite/config/default.exp (ld_elf_shared_opt): Don't set. * testsuite/ld-elf/pr16322.d: xfail when no relro support. * testsuite/ld-elf/pr22393-1a.d: Likewise. * testsuite/ld-elf/pr22393-1b.d: Likewise. * testsuite/ld-elf/shared.exp (pr20995-2.so, pr20995-2): Likewise. * testsuite/lib/ld-lib.exp (run_ld_link_tests): Use check_relro_support to decide whether to pass extra ld option "-z norelro". --- binutils/ChangeLog | 7 ++++++ binutils/testsuite/config/default.exp | 2 -- binutils/testsuite/lib/binutils-common.exp | 27 ++++++++++++++++++---- ld/ChangeLog | 18 +++++++++++++++ ld/emultempl/aarch64elf.em | 6 +++++ ld/emultempl/armelf.em | 6 +++++ ld/emultempl/elf.em | 12 ++++++++++ ld/emultempl/linux.em | 6 +++++ ld/emultempl/scoreelf.em | 6 +++++ ld/testsuite/config/default.exp | 3 --- ld/testsuite/ld-elf/pr16322.d | 1 + ld/testsuite/ld-elf/pr22393-1a.d | 1 + ld/testsuite/ld-elf/pr22393-1b.d | 1 + ld/testsuite/ld-elf/shared.exp | 26 +++++++++------------ ld/testsuite/lib/ld-lib.exp | 8 +++---- 15 files changed, 101 insertions(+), 29 deletions(-) diff --git a/binutils/ChangeLog b/binutils/ChangeLog index 30eae9709d4..0cac3f4f955 100644 --- a/binutils/ChangeLog +++ b/binutils/ChangeLog @@ -1,3 +1,10 @@ +2020-06-19 Alan Modra + + * testsuite/config/default.exp (ld_elf_shared_opt): Don't set. + * testsuite/lib/binutils-common.exp (check_relro_support): New proc. + (run_dump_test): Use check_relro_support to decide whether to pass + extra ld option "-z norelro". + 2020-06-11 Alan Modra * readelf.c (process_mips_specific): Don't alloc memory for diff --git a/binutils/testsuite/config/default.exp b/binutils/testsuite/config/default.exp index e3eae97a9e5..c02136c0b69 100644 --- a/binutils/testsuite/config/default.exp +++ b/binutils/testsuite/config/default.exp @@ -34,8 +34,6 @@ if ![info exists LD] then { if ![info exists LDFLAGS] then { set LDFLAGS "" } -set ld_elf_shared_opt "-z norelro" - if ![info exists NM] then { set NM [findfile $base_dir/nm-new $base_dir/nm-new [transform nm]] } diff --git a/binutils/testsuite/lib/binutils-common.exp b/binutils/testsuite/lib/binutils-common.exp index b9e3c6d8174..491cd8db09e 100644 --- a/binutils/testsuite/lib/binutils-common.exp +++ b/binutils/testsuite/lib/binutils-common.exp @@ -308,6 +308,25 @@ proc check_pie_support { } { return $pie_available_saved } +proc check_relro_support { } { + global relro_available_saved + global ld + + if {![info exists relro_available_saved]} { + remote_file host delete norelro + set ld_output [remote_exec host $ld "-z norelro"] + if { [string first "not supported" $ld_output] >= 0 + || [string first "unrecognized option" $ld_output] >= 0 + || [string first "-z norelro ignored" $ld_output] >= 0 + || [string first "cannot find norelro" $ld_output] >= 0 } { + set relro_available_saved 0 + } else { + set relro_available_saved 1 + } + } + return $relro_available_saved +} + # Compare two files line-by-line. FILE_1 is the actual output and FILE_2 # is the expected output. Ignore blank lines in either file. # @@ -729,7 +748,7 @@ proc run_dump_test { name {extra_options {}} } { global ADDR2LINE ADDR2LINEFLAGS AS ASFLAGS ELFEDIT ELFEDITFLAGS LD LDFLAGS global NM NMFLAGS OBJCOPY OBJCOPYFLAGS OBJDUMP OBJDUMPFLAGS global READELF READELFFLAGS STRIP STRIPFLAGS - global copyfile env ld_elf_shared_opt runtests srcdir subdir verbose + global copyfile env runtests srcdir subdir verbose if [string match "*/*" $name] { set file $name @@ -1119,9 +1138,9 @@ proc run_dump_test { name {extra_options {}} } { set ld_extra_opt "" global ld set ld "$LD" - if { [is_elf_format] && [check_shared_lib_support] } { - set ld_extra_opt "$ld_elf_shared_opt" - } + if [check_relro_support] { + set ld_extra_opt "-z norelro" + } # Add -L$srcdir/$subdir so that the linker command can use # linker scripts in the source directory. diff --git a/ld/ChangeLog b/ld/ChangeLog index 45cf9485d50..7a6caa94af9 100644 --- a/ld/ChangeLog +++ b/ld/ChangeLog @@ -1,3 +1,21 @@ +2020-06-19 Alan Modra + + * emultempl/elf.em (gld${EMULATION_NAME}_handle_option): Omit + -z relro and -z norelro when target support for GNU_RELRO is lacking. + (gld${EMULATION_NAME}_before_parse): Ignore RELRO default too. + * emultempl/aarch64elf.em (gld${EMULATION_NAME}_before_parse): Ignore + RELRO default when target support for GNU_RELRO is lacking. + * emultempl/armelf.em (gld${EMULATION_NAME}_before_parse): Likewise. + * emultempl/linux.em (gld${EMULATION_NAME}_before_parse): Likewise. + * emultempl/scoreelf.em (gld${EMULATION_NAME}_before_parse): Likewise. + * testsuite/config/default.exp (ld_elf_shared_opt): Don't set. + * testsuite/ld-elf/pr16322.d: xfail when no relro support. + * testsuite/ld-elf/pr22393-1a.d: Likewise. + * testsuite/ld-elf/pr22393-1b.d: Likewise. + * testsuite/ld-elf/shared.exp (pr20995-2.so, pr20995-2): Likewise. + * testsuite/lib/ld-lib.exp (run_ld_link_tests): Use check_relro_support + to decide whether to pass extra ld option "-z norelro". + 2020-06-17 H.J. Lu * testsuite/ld-elf/linux-x86.exp (check_pr25749a): Append "-w" diff --git a/ld/emultempl/aarch64elf.em b/ld/emultempl/aarch64elf.em index d0519b3d7b2..a036da970e7 100644 --- a/ld/emultempl/aarch64elf.em +++ b/ld/emultempl/aarch64elf.em @@ -47,7 +47,13 @@ gld${EMULATION_NAME}_before_parse (void) config.has_shared = `if test -n "$GENERATE_SHLIB_SCRIPT" ; then echo TRUE ; else echo FALSE ; fi`; config.separate_code = `if test "x${SEPARATE_CODE}" = xyes ; then echo TRUE ; else echo FALSE ; fi`; link_info.check_relocs_after_open_input = TRUE; +EOF +if test -n "$COMMONPAGESIZE"; then +fragment <