+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
 
 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]]
 }
 
     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.
 #
     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
             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.
 
+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"
 
   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
 
   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
 
   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;
 }
 
        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)
 
   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
 
   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
 
     }
 }
 
-# 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.  
 
 #readelf: -l --wide
 #target: *-*-linux-gnu *-*-gnu* *-*-nacl* arm*-*-uclinuxfdpiceabi
 #xfail: ![check_shared_lib_support] 
+#xfail: ![check_relro_support]
 
 #...
   GNU_RELRO .*
 
 #readelf: -l --wide
 #target: *-*-linux-gnu *-*-gnu* *-*-nacl* arm*-*-uclinuxfdpiceabi
 #xfail: ![check_shared_lib_support] 
+#xfail: ![check_relro_support]
 
 #failif
 #...
 
 #readelf: -l --wide
 #target: *-*-linux-gnu *-*-gnu* *-*-nacl* arm*-*-uclinuxfdpiceabi
 #xfail: ![check_shared_lib_support] 
+#xfail: ![check_relro_support]
 
 #failif
 #...
 
        {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
        "$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" \
 
     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 {