]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Emit a warning when -z relro is unsupported
authorAlan Modra <amodra@gmail.com>
Thu, 18 Jun 2020 23:47:20 +0000 (09:17 +0930)
committerAlan Modra <amodra@gmail.com>
Fri, 19 Jun 2020 00:20:20 +0000 (09:50 +0930)
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".

15 files changed:
binutils/ChangeLog
binutils/testsuite/config/default.exp
binutils/testsuite/lib/binutils-common.exp
ld/ChangeLog
ld/emultempl/aarch64elf.em
ld/emultempl/armelf.em
ld/emultempl/elf.em
ld/emultempl/linux.em
ld/emultempl/scoreelf.em
ld/testsuite/config/default.exp
ld/testsuite/ld-elf/pr16322.d
ld/testsuite/ld-elf/pr22393-1a.d
ld/testsuite/ld-elf/pr22393-1b.d
ld/testsuite/ld-elf/shared.exp
ld/testsuite/lib/ld-lib.exp

index 30eae9709d4e785703ddb877003d4c238f540cc5..0cac3f4f95515fab24ddc55521f0a15433eb36f1 100644 (file)
@@ -1,3 +1,10 @@
+2020-06-19  Alan Modra  <amodra@gmail.com>
+
+       * 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  <amodra@gmail.com>
 
        * readelf.c (process_mips_specific): Don't alloc memory for
index e3eae97a9e558281549fda17480f228ee8abef4a..c02136c0b693500af9741e5c7be6b31f0292f994 100644 (file)
@@ -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]]
 }
index b9e3c6d8174168226a044d532b760e22ef8f7d8f..491cd8db09ed8cd2f28b89309e377dd4e4028dff 100644 (file)
@@ -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.
index 45cf9485d50223898d4d001db06bb67d89df2b83..7a6caa94af9ce5374b068e8a762760b50d2e0939 100644 (file)
@@ -1,3 +1,21 @@
+2020-06-19  Alan Modra  <amodra@gmail.com>
+
+       * 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  <hongjiu.lu@intel.com>
 
        * testsuite/ld-elf/linux-x86.exp (check_pr25749a): Append "-w"
index d0519b3d7b24542aa7500c1bc706415e48bd4f40..a036da970e7e9bace0c3a3286ab3a39b45bff1ec 100644 (file)
@@ -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 <<EOF
   link_info.relro = DEFAULT_LD_Z_RELRO;
+EOF
+fi
+fragment <<EOF
 }
 
 static void
index fb5bbf85f6f8f308b2fab645074dfff5c156877e..34372ba7b0b64055097325f39cf1959de1e75cb3 100644 (file)
@@ -60,7 +60,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 <<EOF
   link_info.relro = DEFAULT_LD_Z_RELRO;
+EOF
+fi
+fragment <<EOF
 }
 
 static void
index c577e8b2e61b8c128fc309636cc9b443141a354b..356f34538b0d20ba344441feed23f6b00787a48e 100644 (file)
@@ -82,7 +82,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 <<EOF
   link_info.relro = DEFAULT_LD_Z_RELRO;
+EOF
+fi
+fragment <<EOF
   link_info.separate_code = DEFAULT_LD_Z_SEPARATE_CODE;
 }
 
@@ -805,10 +811,16 @@ fragment <<EOF
        link_info.combreloc = FALSE;
       else if (strcmp (optarg, "nocopyreloc") == 0)
        link_info.nocopyreloc = TRUE;
+EOF
+if test -n "$COMMONPAGESIZE"; then
+fragment <<EOF
       else if (strcmp (optarg, "relro") == 0)
        link_info.relro = TRUE;
       else if (strcmp (optarg, "norelro") == 0)
        link_info.relro = FALSE;
+EOF
+fi
+fragment <<EOF
       else if (strcmp (optarg, "separate-code") == 0)
        link_info.separate_code = TRUE;
       else if (strcmp (optarg, "noseparate-code") == 0)
index e9d802c657cdab833c6e7b5287fd7c565e1e27aa..14446109eaef0f60301566c68f760a2e82734967 100644 (file)
@@ -52,7 +52,13 @@ gld${EMULATION_NAME}_before_parse (void)
   input_flags.dynamic = TRUE;
   config.has_shared = TRUE;
   link_info.check_relocs_after_open_input = TRUE;
+EOF
+if test -n "$COMMONPAGESIZE"; then
+fragment <<EOF
   link_info.relro = DEFAULT_LD_Z_RELRO;
+EOF
+fi
+fragment <<EOF
 }
 
 /* Try to open a dynamic archive.  This is where we know that Linux
index 327ec6b6fffae06a6c4d45e1d578f0d5c39a27ab..70b9a705209dab11b2a11170d5d3ff8239111265 100644 (file)
@@ -40,7 +40,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 <<EOF
   link_info.relro = DEFAULT_LD_Z_RELRO;
+EOF
+fi
+fragment <<EOF
 }
 
 static void
index b2097ce7af25a02f29c3a791e3f263ba616982d3..9e89ca1283472c2ab071fb65104007e3246f7a1a 100644 (file)
@@ -76,9 +76,6 @@ if {[file exists tmpdir/libpath.exp]} {
     }
 }
 
-# Many ELF testcases expect that "-z relro" is off.
-set ld_elf_shared_opt "-z norelro"
-
 # The "make check" target in the Makefile passes in
 # "CC=$(CC_FOR_TARGET)".  But, if the user invokes runtest directly
 # (as when testing an installed linker), these flags may not be set.  
index 7bd1a6ff1aeecac62e2e9def2338ab757b7c9a62..1e9175c67de766d1c3b71bacf6b862a28b43f876 100644 (file)
@@ -2,6 +2,7 @@
 #readelf: -l --wide
 #target: *-*-linux-gnu *-*-gnu* *-*-nacl* arm*-*-uclinuxfdpiceabi
 #xfail: ![check_shared_lib_support] 
+#xfail: ![check_relro_support]
 
 #...
   GNU_RELRO .*
index c08ecaa3b0cf7d9f0c7c2671619e649a5fe975e5..2bfa40375671b6ed8ab78245b23cc8f23981a4c2 100644 (file)
@@ -3,6 +3,7 @@
 #readelf: -l --wide
 #target: *-*-linux-gnu *-*-gnu* *-*-nacl* arm*-*-uclinuxfdpiceabi
 #xfail: ![check_shared_lib_support] 
+#xfail: ![check_relro_support]
 
 #failif
 #...
index b0020a17142b97ce3083e56a59cc06c66be76ad8..96d8bc01b97787254958fe2dbc4fb6f39837340f 100644 (file)
@@ -3,6 +3,7 @@
 #readelf: -l --wide
 #target: *-*-linux-gnu *-*-gnu* *-*-nacl* arm*-*-uclinuxfdpiceabi
 #xfail: ![check_shared_lib_support] 
+#xfail: ![check_relro_support]
 
 #failif
 #...
index 9d72cad78d6c326644821192e9f395cce4df02b0..8f4ed71de5576a907fc658ef6eb93fc0a5dbd383 100644 (file)
@@ -454,16 +454,6 @@ run_ld_link_tests [list \
        {pr20995b.s} {} "pr20995.so"] \
 ]
 
-# xfail on arm*-*-eabi*.  The list can be enlarged to those targets that
-# don't support GNU_RELRO.  For more details, please see discussions at:
-#   https://sourceware.org/ml/binutils/2017-01/msg00441.html
-run_ld_link_tests [list \
-    [list "Build pr20995-2.so" \
-       "-shared -z relro" "" "$AFLAGS_PIC" \
-       {pr20995c.s} {{readelf {-l --wide} pr20995-2so.r}} "pr20995-2.so"] \
-] "tic6x-*-*" "arm*-*-eabi*" "hppa*64*-*-hpux*" "aarch64*-*-elf*" \
-  "*-*-lynxos*" "arm*-*-nto*" "i?86-*-nto*" "sh*-*-nto*"
-
 # These targets don't copy dynamic variables into .bss.
 setup_xfail "alpha-*-*" "bfin-*-*" "ia64-*-*" "xtensa-*-*"
 # or don't have .data.rel.ro
@@ -474,16 +464,22 @@ run_ld_link_tests [list \
        "$LFLAGS" "tmpdir/pr20995.so" "$AFLAGS_NONPIC" \
        {pr20995a.s} {{readelf {-S --wide} pr20995.r}} "pr20995"]]
 
-# xfail on arm*-*-eabi* is particularly because of no support of GNU_RELRO.
-# Please see the link above for details.
-setup_xfail "alpha-*-*" "bfin-*-*" "ia64-*-*" "xtensa-*-*" "arm*-*-eabi*"
-setup_xfail "hppa*64*-*-hpux*" "aarch64*-*-elf*" "tic6x-*-*"
+# xfail on targets that don't support GNU_RELRO.
+#   For more details, please see discussions at:
+#   https://sourceware.org/ml/binutils/2017-01/msg00441.html
+run_ld_link_tests [list \
+    [list "Build pr20995-2.so" \
+       "-shared -z relro" "" "$AFLAGS_PIC" \
+       {pr20995c.s} {{readelf {-l --wide} pr20995-2so.r}} "pr20995-2.so"] \
+] {![check_relro_support]}
+
+setup_xfail alpha-*-* xtensa-*-*
 run_ld_link_tests [list \
     [list \
        "pr20995-2" \
        "$LFLAGS" "tmpdir/pr20995-2.so" "$AFLAGS_NONPIC" \
        {pr20995a.s} {{readelf {-S --wide} pr20995.r}} "pr20995-2"]
-] "*-*-lynxos*" "arm*-*-nto*" "i?86-*-nto*" "sh*-*-nto*"
+] {![check_relro_support]}
 
 run_ld_link_tests [list \
     [list "Build pr22374 shared library" \
index a949437cea301b944df559009e5ba0dc21c2b19b..0a256c2395faa1df515e253c8a3b90f48ce2f702 100644 (file)
@@ -463,12 +463,10 @@ proc run_ld_link_tests { ldtests args } {
     global CFLAGS
     global runtests
     global exec_output
-    global ld_elf_shared_opt
 
-    if { [is_elf_format] && [check_shared_lib_support] } {
-       set ld_extra_opt "$ld_elf_shared_opt"
-    } else {
-       set ld_extra_opt ""
+    set ld_extra_opt ""
+    if [check_relro_support] {
+       set ld_extra_opt "-z norelro"
     }
 
     foreach testitem $ldtests {