ld: Compile some tests with -mdirect-extern-access

When GCC enables -mno-direct-extern-access by default, some tests fail
without -mdirect-extern-access.  Define DIRECT_EXTERN_ACCESS_CFLAGS
to compile these tests with -mdirect-extern-access.  Also pass
"-z noindirect-extern-access" to linker to support the C library
compiled with -mno-direct-extern-access.

	PR ld/33267
	* testsuite/config/default.exp (DIRECT_EXTERN_ACCESS_CFLAGS): New.
	* testsuite/ld-elf/linux-x86.exp: Compile some tests with
	$DIRECT_EXTERN_ACCESS_CFLAGS.
	* testsuite/ld-elfvers/vers.exp (need_direct_extern_access): New
	for i?86.
	Compile tests with $need_direct_extern_access.
	* testsuite/ld-i386/i386.exp: Compile some tests with
	$DIRECT_EXTERN_ACCESS_CFLAGS.
	* testsuite/ld-ifunc/ifunc.exp (need_direct_extern_access): New
	for i?86.
	Compile tests with $need_direct_extern_access.
	* testsuite/ld-shared/shared.exp (need_direct_extern_access): New
	for i?86.
	Compile tests with $need_direct_extern_access.
	* testsuite/ld-srec/srec.exp (CFLAGS_FOR_TARGET_TEST): Add
	$DIRECT_EXTERN_ACCESS_CFLAGS.
	(CXXFLAGS_FOR_TARGET_TEST): Likewise.
	* testsuite/ld-vsb/vsb.exp (need_direct_extern_access): New
	for i?86.
	Compile tests with $need_direct_extern_access.
	* testsuite/ld-x86-64/x86-64.exp: Compile some tests with
	$DIRECT_EXTERN_ACCESS_CFLAGS and link some tests with
	"-Wl,-z,noindirect-extern-access".

Co-Authored-By: Sam James <sam@gentoo.org>
Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
Signed-off-by: Sam James <sam@gentoo.org>
diff --git a/ld/testsuite/config/default.exp b/ld/testsuite/config/default.exp
index 9f28683..3619f52 100644
--- a/ld/testsuite/config/default.exp
+++ b/ld/testsuite/config/default.exp
@@ -456,6 +456,15 @@
     }
 }
 
+if { ![info exists DIRECT_EXTERN_ACCESS_CFLAGS] } then {
+    set DIRECT_EXTERN_ACCESS_CFLAGS ""
+    if [compiler_supports "-mdirect-extern-access"] {
+	set DIRECT_EXTERN_ACCESS_CFLAGS "-mdirect-extern-access"
+    } elseif [compiler_supports "-fdirect-access-external-data"] {
+	set DIRECT_EXTERN_ACCESS_CFLAGS "-fdirect-access-external-data"
+    }
+}
+
 if { ![info exists NOCF_PROTECTION_CFLAGS] } then {
     set NOCF_PROTECTION_CFLAGS ""
     if [compiler_supports "-fcf-protection=none"] {
diff --git a/ld/testsuite/ld-elf/linux-x86.exp b/ld/testsuite/ld-elf/linux-x86.exp
index 27173b9..a4dfc8a 100644
--- a/ld/testsuite/ld-elf/linux-x86.exp
+++ b/ld/testsuite/ld-elf/linux-x86.exp
@@ -84,9 +84,9 @@
     ] \
     [list \
 	"Build indirect-extern-access-1a without PIE" \
-	"$NOPIE_LDFLAGS -Wl,--no-as-needed -z noexecstack \
+	"$NOPIE_LDFLAGS $DIRECT_EXTERN_ACCESS_CFLAGS -Wl,--no-as-needed -z noexecstack \
 	 tmpdir/indirect-extern-access-1.so" \
-	"$NOPIE_CFLAGS" \
+	"$NOPIE_CFLAGS $DIRECT_EXTERN_ACCESS_CFLAGS" \
 	{ indirect-extern-access.S indirect-extern-access-1b.c } \
 	{{readelf -rn indirect-extern-access-1.rd}} \
 	"indirect-extern-access-1a" \
@@ -101,9 +101,9 @@
     ] \
     [list \
 	"Build indirect-extern-access-2a without PIE" \
-	"$NOPIE_LDFLAGS -Wl,--no-as-needed -z noexecstack \
+	"$NOPIE_LDFLAGS $DIRECT_EXTERN_ACCESS_CFLAGS -Wl,--no-as-needed -z noexecstack \
 	 tmpdir/indirect-extern-access-2.so" \
-	"$NOPIE_CFLAGS" \
+	"$NOPIE_CFLAGS $DIRECT_EXTERN_ACCESS_CFLAGS" \
 	{ indirect-extern-access.S indirect-extern-access-1b.c } \
 	{{readelf -rn indirect-extern-access-2.rd}} \
 	"indirect-extern-access-2a" \
@@ -375,11 +375,11 @@
 
 check_pr25749a "pr25749-1a" "pr25749-1.c" "pr25749-1a.c" "$NOPIE_CFLAGS" "$NOPIE_LDFLAGS" ""
 check_pr25749a "pr25749-1a" "pr25749-1.c" "pr25749-1a.c" "-fPIE" "-pie" ""
-check_pr25749a "pr25749-1b" "pr25749-1.c" "pr25749-1b.c" "$NOPIE_CFLAGS" "$NOPIE_LDFLAGS" ""
+check_pr25749a "pr25749-1b" "pr25749-1.c" "pr25749-1b.c" "$NOPIE_CFLAGS $DIRECT_EXTERN_ACCESS_CFLAGS" "$NOPIE_LDFLAGS" ""
 if { [istarget "i?86-*-linux*"] || ![at_least_gcc_version 5 1] } {
-    check_pr25749a "pr25749-1b" "pr25749-1.c" "pr25749-1b.c" "-fPIE" "-pie" ""
+    check_pr25749a "pr25749-1b" "pr25749-1.c" "pr25749-1b.c" "-fPIE $DIRECT_EXTERN_ACCESS_CFLAGS" "-pie" ""
 } else {
-    check_pr25749a "pr25749-1b" "pr25749-1.c" "pr25749-1b.c" "-fPIE" "-pie" "pr25749-1b.err"
+    check_pr25749a "pr25749-1b" "pr25749-1.c" "pr25749-1b.c" "-fPIE $DIRECT_EXTERN_ACCESS_CFLAGS" "-pie" "pr25749-1b.err"
 }
 check_pr25749a "pr25749-1c" "pr25749-1.c" "pr25749-1c.c" "-fPIC" "-shared" "pr25749-1b.err"
 check_pr25749a "pr25749-2a" "pr25749-2.c" "pr25749-2a.s" "$NOPIE_CFLAGS" "$NOPIE_LDFLAGS" ""
diff --git a/ld/testsuite/ld-elfvers/vers.exp b/ld/testsuite/ld-elfvers/vers.exp
index 4f59e0c..80faaa0 100644
--- a/ld/testsuite/ld-elfvers/vers.exp
+++ b/ld/testsuite/ld-elfvers/vers.exp
@@ -60,6 +60,13 @@
     return
 }
 
+set need_direct_extern_access ""
+if { [istarget i?86-*-linux*]
+     || [istarget i?86-*-gnu*] } then {
+    # These targets need direct extern access in some tests.
+    set need_direct_extern_access "$DIRECT_EXTERN_ACCESS_CFLAGS"
+}
+
 set diff diff
 set tmpdir tmpdir
 set VOBJDUMP_FLAGS --private-headers
@@ -558,7 +565,8 @@
   global shared
   # Make sure that PLT is used since PLT is expected.
   global PLT_CFLAGS
-  build_binary "$shared -z notext" $PLT_CFLAGS $test $source $libname $other $mapfile $verexp $versymexp $symexp ""
+  global need_direct_extern_access
+  build_binary "$shared -z notext" "$PLT_CFLAGS $need_direct_extern_access" $test $source $libname $other $mapfile $verexp $versymexp $symexp ""
 }
 
 proc build_vers_lib_pic { test source libname other mapfile verexp versymexp symexp } {
diff --git a/ld/testsuite/ld-i386/i386.exp b/ld/testsuite/ld-i386/i386.exp
index 86748b1..8633a66 100644
--- a/ld/testsuite/ld-i386/i386.exp
+++ b/ld/testsuite/ld-i386/i386.exp
@@ -712,7 +712,7 @@
 	[list \
 	    "Build pr17689.so" \
 	    "-shared" \
-	    "-fPIC -Wa,-mrelax-relocations=yes" \
+	    "-fPIC $DIRECT_EXTERN_ACCESS_CFLAGS -Wa,-mrelax-relocations=yes" \
 	    { pr17689a.c } \
 	    {} \
 	    "pr17689.so" \
@@ -736,7 +736,7 @@
 	[list \
 	    "Build pr17689.a" \
 	    "" \
-	    "-Wa,-mrelax-relocations=yes" \
+	    "$DIRECT_EXTERN_ACCESS_CFLAGS -Wa,-mrelax-relocations=yes" \
 	    { pr17689b.S } \
 	    {} \
 	    "pr17689.a" \
@@ -744,7 +744,7 @@
 	[list \
 	    "Build pr17689 with PIE and GOTOFF" \
 	    "tmpdir/pr17689b.o tmpdir/pr17689.so -pie" \
-	    "-Wa,-mrelax-relocations=yes" \
+	    "$DIRECT_EXTERN_ACCESS_CFLAGS -Wa,-mrelax-relocations=yes" \
 	    { dummy.c } \
 	    {{readelf {-Wr} pr17689.rd}} \
 	    "pr17689" \
@@ -752,7 +752,7 @@
 	[list \
 	    "Build pr17689 with PIE, -z now and GOTOFF" \
 	    "tmpdir/pr17689b.o tmpdir/pr17689.so -pie -Wl,-z,now" \
-	    "-Wa,-mrelax-relocations=yes" \
+	    "$DIRECT_EXTERN_ACCESS_CFLAGS -Wa,-mrelax-relocations=yes" \
 	    { dummy.c } \
 	    {{readelf {-Wr} pr17689now.rd}} \
 	    "pr17689now" \
@@ -760,7 +760,7 @@
 	[list \
 	    "Build pr17689ver with PIE and GOTOFF" \
 	    "tmpdir/pr17689b.o tmpdir/pr17689ver.so -pie" \
-	    "-Wa,-mrelax-relocations=yes" \
+	    "$DIRECT_EXTERN_ACCESS_CFLAGS -Wa,-mrelax-relocations=yes" \
 	    { dummy.c } \
 	    {{readelf {-Wr} pr17689ver.rd}} \
 	    "pr17689ver" \
@@ -768,7 +768,7 @@
 	[list \
 	    "Build pr17827 with PIE and GOTOFF" \
 	    "tmpdir/pr17689b.o tmpdir/pr17689.so -pie" \
-	    "-Wa,-mrelax-relocations=yes" \
+	    "$DIRECT_EXTERN_ACCESS_CFLAGS -Wa,-mrelax-relocations=yes" \
 	    { dummy.c } \
 	    {{readelf {-Wr} pr17827.rd}} \
 	    "pr17827" \
@@ -874,7 +874,7 @@
 	[list \
 	    "Build property 1 (.o)" \
 	    "-r -nostdlib" \
-	    "-Wa,-mx86-used-note=yes" \
+	    "$DIRECT_EXTERN_ACCESS_CFLAGS -Wa,-mx86-used-note=yes" \
 	    {pass.c property-no-copy.S} \
 	    {{readelf {-n} property-1a.r}} \
 	    "property-1.o" \
@@ -898,7 +898,7 @@
 	[list \
 	    "Build property 2 (.o)" \
 	    "-r -nostdlib" \
-	    "-Wa,-mx86-used-note=yes" \
+	    "$DIRECT_EXTERN_ACCESS_CFLAGS -Wa,-mx86-used-note=yes" \
 	    {pass.c property-stack.S} \
 	    {{readelf {-n} property-2a.r}} \
 	    "property-2.o" \
@@ -922,7 +922,7 @@
 	[list \
 	    "Build property 3 (.o)" \
 	    "-r -nostdlib" \
-	    "-Wa,-mx86-used-note=yes" \
+	    "$DIRECT_EXTERN_ACCESS_CFLAGS -Wa,-mx86-used-note=yes" \
 	    {pass.c property-x86-1.S property-stack.S} \
 	    {{readelf {-n} property-3a.r}} \
 	    "property-3.o" \
@@ -946,7 +946,7 @@
 	[list \
 	    "Build property 4 (.o)" \
 	    "-r -nostdlib" \
-	    "-Wa,-mx86-used-note=yes" \
+	    "$DIRECT_EXTERN_ACCESS_CFLAGS -Wa,-mx86-used-note=yes" \
 	    {pass.c property-x86-2.S property-x86-1.S property-stack.S} \
 	    {{readelf {-n} property-4a.r}} \
 	    "property-4.o" \
@@ -978,7 +978,7 @@
 	[list \
 	    "Build property 5 (.o)" \
 	    "-r -nostdlib -Wl,-z,stack-size=0x900000" \
-	    "-Wa,-mx86-used-note=yes" \
+	    "$DIRECT_EXTERN_ACCESS_CFLAGS -Wa,-mx86-used-note=yes" \
 	    {pass.c property-x86-2.S property-x86-1.S property-stack.S} \
 	    {{readelf {-n} property-5a.r}} \
 	    "property-5.o" \
@@ -1002,7 +1002,7 @@
 	[list \
 	    "Build property-6.o" \
 	    "-r -nostdlib" \
-	    "-Wa,-mx86-used-note=yes" \
+	    "$DIRECT_EXTERN_ACCESS_CFLAGS -Wa,-mx86-used-note=yes" \
 	    {property-6b.c property-stack.S} \
 	    {{readelf {-n} property-2a.r}} \
 	    "property-6.o" \
@@ -1069,6 +1069,7 @@
 	    { dummy.c } \
 	    "pr17689" \
 	    "pr17689.out" \
+	    "$DIRECT_EXTERN_ACCESS_CFLAGS" \
 	] \
 	[list \
 	    "Run pr17689 with PIE, -z now and GOTOFF" \
@@ -1077,6 +1078,7 @@
 	    { dummy.c } \
 	    "pr17689now" \
 	    "pr17689.out" \
+	    "$DIRECT_EXTERN_ACCESS_CFLAGS" \
 	] \
 	[list \
 	    "Run pr17689ver with PIE and GOTOFF" \
@@ -1085,6 +1087,7 @@
 	    { dummy.c } \
 	    "pr17689ver" \
 	    "pr17689.out" \
+	    "$DIRECT_EXTERN_ACCESS_CFLAGS" \
 	] \
 	[list \
 	    "Run pr18900" \
@@ -1320,7 +1323,7 @@
 	[list \
 	    "Build pr28875" \
 	    "$NOPIE_LDFLAGS -Wl,--no-as-needed tmpdir/pr28875.so" \
-	    "$NOPIE_CFLAGS" \
+	    "$NOPIE_CFLAGS $DIRECT_EXTERN_ACCESS_CFLAGS" \
 	    { pr28875b.c } \
 	    {{error_output "pr28875.err"}} \
 	    "pr28875" \
@@ -1328,7 +1331,7 @@
 	[list \
 	    "Build pr21997-1" \
 	    "$NOPIE_LDFLAGS -Wl,--no-as-needed,-z,notext tmpdir/pr21997-1.so" \
-	    "$NOPIE_CFLAGS -Wa,-mx86-used-note=yes" \
+	    "$NOPIE_CFLAGS $DIRECT_EXTERN_ACCESS_CFLAGS -Wa,-mx86-used-note=yes" \
 	    { pr21997-1b.c } \
 	    {{error_output "pr21997-1.err"}} \
 	    "pr21997-1" \
diff --git a/ld/testsuite/ld-ifunc/ifunc.exp b/ld/testsuite/ld-ifunc/ifunc.exp
index e8b93b3..07a8a2f 100644
--- a/ld/testsuite/ld-ifunc/ifunc.exp
+++ b/ld/testsuite/ld-ifunc/ifunc.exp
@@ -54,6 +54,13 @@
     return
 }
 
+set need_direct_extern_access ""
+if { [istarget i?86-*-linux*]
+     || [istarget i?86-*-gnu*] } then {
+    # These targets need direct extern access in some tests.
+    set need_direct_extern_access "$DIRECT_EXTERN_ACCESS_CFLAGS"
+}
+
 set saved_ASFLAGS "$ASFLAGS"
 if { [istarget "i?86-*-*"] || [istarget "x86_64-*-*"] } {
     set ASFLAGS "$ASFLAGS -mx86-used-note=no"
@@ -497,6 +504,7 @@
 	    { ifunc-main.c } \
 	    "ifunc-main" \
 	    "ifunc-main.out" \
+	    "$need_direct_extern_access" \
 	] \
 	[list \
 	    "Run ifunc-main with -fpic" \
@@ -514,6 +522,7 @@
 	    { ifunc-main.c } \
 	    "ifunc-mainn" \
 	    "ifunc-main.out" \
+	    "$need_direct_extern_access" \
 	] \
 	[list \
 	    "Run ifunc-main with PIE (-z now)" \
diff --git a/ld/testsuite/ld-shared/shared.exp b/ld/testsuite/ld-shared/shared.exp
index e7f77fa..daafee0 100644
--- a/ld/testsuite/ld-shared/shared.exp
+++ b/ld/testsuite/ld-shared/shared.exp
@@ -62,6 +62,13 @@
     return
 }
 
+set need_direct_extern_access ""
+if { [istarget i?86-*-linux*]
+     || [istarget i?86-*-gnu*] } then {
+    # These targets need direct extern access in some tests.
+    set need_direct_extern_access "$DIRECT_EXTERN_ACCESS_CFLAGS"
+}
+
 set tmpdir tmpdir
 set SHCFLAG ""
 set shared_needs_pic "no"
@@ -217,8 +224,8 @@
     # will need to do more relocation work.  However, note that not
     # using -fpic will cause some of the tests to return different
     # results.  Make sure that PLT is used since PLT is expected.
-    if { ![ld_compile "$CC_FOR_TARGET $PLT_CFLAGS $NOPIE_CFLAGS $SHCFLAG" $srcdir/$subdir/sh1.c $tmpdir/sh1np.o]
-	 || ![ld_compile "$CC_FOR_TARGET $PLT_CFLAGS $NOPIE_CFLAGS $SHCFLAG" $srcdir/$subdir/sh2.c $tmpdir/sh2np.o] } {
+    if { ![ld_compile "$CC_FOR_TARGET $PLT_CFLAGS $NOPIE_CFLAGS $SHCFLAG $need_direct_extern_access" $srcdir/$subdir/sh1.c $tmpdir/sh1np.o]
+	 || ![ld_compile "$CC_FOR_TARGET $PLT_CFLAGS $NOPIE_CFLAGS $SHCFLAG $need_direct_extern_access" $srcdir/$subdir/sh2.c $tmpdir/sh2np.o] } {
 	unsupported "shared (non PIC)"
     } else { if { [is_xcoff_format] } {
 	shared_test shnp "shared (non PIC)" mainnp.o sh1np.o sh2np.o xcoff "" $NOPIE_LDFLAGS
diff --git a/ld/testsuite/ld-srec/srec.exp b/ld/testsuite/ld-srec/srec.exp
index 5afc84c..227411a 100644
--- a/ld/testsuite/ld-srec/srec.exp
+++ b/ld/testsuite/ld-srec/srec.exp
@@ -364,11 +364,12 @@
 # Pass -fplt to CC and CXX since -fno-plt doesn't work with S-records
 # tests. Also add $NOPIE_CFLAGS and $NOPIE_LDFLAGS if PIE doesn't work
 # with S-records.  Also add $NOCF_PROTECTION_CFLAGS for S-records.
-# Also add $NOSANITIZE_CFLAGS for S-records.
+# Also add $NOSANITIZE_CFLAGS for S-records.  Also add
+# $DIRECT_EXTERN_ACCESS_CFLAGS.
 set old_CFLAGS "$CFLAGS_FOR_TARGET_TEST"
-append CFLAGS_FOR_TARGET_TEST " $PLT_CFLAGS $NOPIE_CFLAGS $NOPIE_LDFLAGS $NOCF_PROTECTION_CFLAGS $NOSANITIZE_CFLAGS $NOLTO_CFLAGS"
+append CFLAGS_FOR_TARGET_TEST " $PLT_CFLAGS $NOPIE_CFLAGS $NOPIE_LDFLAGS $NOCF_PROTECTION_CFLAGS $NOSANITIZE_CFLAGS $NOLTO_CFLAGS $DIRECT_EXTERN_ACCESS_CFLAGS"
 set old_CXXFLAGS "$CXXFLAGS_FOR_TARGET_TEST"
-append CXXFLAGS_FOR_TARGET_TEST " $PLT_CFLAGS $NOPIE_CFLAGS $NOPIE_LDFLAGS $NOCF_PROTECTION_CFLAGS $NOSANITIZE_CFLAGS $NOLTO_CFLAGS"
+append CXXFLAGS_FOR_TARGET_TEST " $PLT_CFLAGS $NOPIE_CFLAGS $NOPIE_LDFLAGS $NOCF_PROTECTION_CFLAGS $NOSANITIZE_CFLAGS $NOLTO_CFLAGS $DIRECT_EXTERN_ACCESS_CFLAGS"
 
 # S-records can't handle .note.gnu.property sections.
 if { [is_elf_format] \
diff --git a/ld/testsuite/ld-vsb/vsb.exp b/ld/testsuite/ld-vsb/vsb.exp
index 84ae1ed..8eff759 100644
--- a/ld/testsuite/ld-vsb/vsb.exp
+++ b/ld/testsuite/ld-vsb/vsb.exp
@@ -50,6 +50,13 @@
     return
 }
 
+set need_direct_extern_access ""
+if { [istarget i?86-*-linux*]
+     || [istarget i?86-*-gnu*] } then {
+    # These targets need direct extern access in some tests.
+    set need_direct_extern_access "$DIRECT_EXTERN_ACCESS_CFLAGS"
+}
+
 if [is_xcoff_format] {
     set test_list [lsort [glob -nocomplain $srcdir/$subdir/*-xcoff*.d]]
 } else {
@@ -238,6 +245,7 @@
     global COMPRESS_LDFLAG
     global NOSANITIZE_CFLAGS
     global NOLTO_CFLAGS
+    global need_direct_extern_access
 
     if [ string match $visibility "hidden" ] {
 	set VSBCFLAG "-DHIDDEN_TEST"
@@ -284,7 +292,7 @@
     } else {
     # Compile the main program.  Make sure that PLT is used since PLT
     # is expected.
-    if ![ld_compile "$CC_FOR_TARGET -g $NOPIE_CFLAGS $PLT_CFLAGS $SHCFLAG $VSBCFLAG $NOSANITIZE_CFLAGS $NOLTO_CFLAGS" $srcdir/$subdir/main.c $tmpdir/mainnp.o] {
+    if ![ld_compile "$CC_FOR_TARGET -g $NOPIE_CFLAGS $PLT_CFLAGS $SHCFLAG $VSBCFLAG $NOSANITIZE_CFLAGS $NOLTO_CFLAGS $need_direct_extern_access" $srcdir/$subdir/main.c $tmpdir/mainnp.o] {
 	unsupported "visibility ($visibility) (non PIC)"
 	unsupported "visibility ($visibility)"
     } else {
@@ -294,8 +302,8 @@
 	# will need to do more relocation work.  However, note that not
 	# using -fpic will cause some of the tests to return different
 	# results.  Make sure that PLT is used since PLT is expected.
-	if { ![ld_compile "$CC_FOR_TARGET -g $NOPIE_CFLAGS $PLT_CFLAGS $SHCFLAG $VSBCFLAG $NOSANITIZE_CFLAGS $NOLTO_CFLAGS" $srcdir/$subdir/sh1.c $tmpdir/sh1np.o]
-	     || ![ld_compile "$CC_FOR_TARGET -g $NOPIE_CFLAGS $PLT_CFLAGS $SHCFLAG $VSBCFLAG $NOSANITIZE_CFLAGS $NOLTO_CFLAGS" $srcdir/$subdir/sh2.c $tmpdir/sh2np.o] } {
+	if { ![ld_compile "$CC_FOR_TARGET -g $NOPIE_CFLAGS $PLT_CFLAGS $SHCFLAG $VSBCFLAG $NOSANITIZE_CFLAGS $NOLTO_CFLAGS $need_direct_extern_access" $srcdir/$subdir/sh1.c $tmpdir/sh1np.o]
+	     || ![ld_compile "$CC_FOR_TARGET -g $NOPIE_CFLAGS $PLT_CFLAGS $SHCFLAG $VSBCFLAG $NOSANITIZE_CFLAGS $NOLTO_CFLAGS $need_direct_extern_access" $srcdir/$subdir/sh2.c $tmpdir/sh2np.o] } {
 	    unsupported "visibility ($visibility) (non PIC)"
 	} else {
 	    # SunOS non PIC shared libraries don't permit some cases of
diff --git a/ld/testsuite/ld-x86-64/x86-64.exp b/ld/testsuite/ld-x86-64/x86-64.exp
index a72a7da..9d97531 100644
--- a/ld/testsuite/ld-x86-64/x86-64.exp
+++ b/ld/testsuite/ld-x86-64/x86-64.exp
@@ -1007,7 +1007,7 @@
 	[list \
 	    "Build pr17689.so" \
 	    "-shared" \
-	    "-fPIC -Wa,-mx86-used-note=yes" \
+	    "$DIRECT_EXTERN_ACCESS_CFLAGS -fPIC -Wa,-mx86-used-note=yes" \
 	    { pr17689a.c } \
 	    {} \
 	    "pr17689.so" \
@@ -1015,7 +1015,7 @@
 	[list \
 	    "Build pr17689now.so with -z now" \
 	    "-shared -Wl,-z,now" \
-	    "-fPIC -Wa,-mx86-used-note=yes" \
+	    "$DIRECT_EXTERN_ACCESS_CFLAGS -fPIC -Wa,-mx86-used-note=yes" \
 	    { pr17689a.c } \
 	    {{readelf {-Wr} pr17689now.rd}} \
 	    "pr17689now.so" \
@@ -1023,30 +1023,30 @@
 	[list \
 	    "Build pr17689b.o" \
 	    "" \
-	    "-Wa,-mx86-used-note=yes" \
+	    "$DIRECT_EXTERN_ACCESS_CFLAGS -Wa,-mx86-used-note=yes" \
 	    { pr17689b.S } \
 	    {} \
 	] \
 	[list \
 	    "Build pr17689 with PIE without -fPIE" \
-	    "tmpdir/pr17689b.o tmpdir/pr17689.so -pie" \
-	    "-Wa,-mx86-used-note=yes" \
+	    "tmpdir/pr17689b.o tmpdir/pr17689.so -pie -Wl,-z,noindirect-extern-access" \
+	    "$DIRECT_EXTERN_ACCESS_CFLAGS -Wa,-mx86-used-note=yes" \
 	    { dummy.s } \
 	    {{readelf {-Wr} pr17689.rd}} \
 	    "pr17689" \
 	] \
 	[list \
 	    "Build pr17689 with PIE -z now without -fPIE" \
-	    "tmpdir/pr17689b.o tmpdir/pr17689.so -pie -Wl,-z,now" \
-	    "-Wa,-mx86-used-note=yes" \
+	    "tmpdir/pr17689b.o tmpdir/pr17689.so -pie -Wl,-z,now -Wl,-z,noindirect-extern-access" \
+	    "$DIRECT_EXTERN_ACCESS_CFLAGS -Wa,-mx86-used-note=yes" \
 	    { dummy.s } \
 	    {{readelf {-Wr} pr17689now.rd}} \
 	    "pr17689now" \
 	] \
 	[list \
 	    "Build pr17827 with PIE without -fPIE" \
-	    "-Wl,--as-needed tmpdir/pr17689b.o tmpdir/pr17689.so -pie" \
-	    "-Wa,-mx86-used-note=yes" \
+	    "-Wl,--as-needed tmpdir/pr17689b.o tmpdir/pr17689.so -pie -Wl,-z,noindirect-extern-access" \
+	    "$DIRECT_EXTERN_ACCESS_CFLAGS -Wa,-mx86-used-note=yes" \
 	    { dummy.s } \
 	    {{readelf {-Wr} pr17827.rd}} \
 	    "pr17827" \
@@ -1151,7 +1151,7 @@
 	[list \
 	    "Build property 1 (.o)" \
 	    "-r -nostdlib" \
-	    "-Wa,-mx86-used-note=yes" \
+	    "$DIRECT_EXTERN_ACCESS_CFLAGS -Wa,-mx86-used-note=yes" \
 	    {pass.c property-no-copy.S} \
 	    {{readelf {-n} property-1a.r}} \
 	    "property-1.o" \
@@ -1175,7 +1175,7 @@
 	[list \
 	    "Build property 2 (.o)" \
 	    "-r -nostdlib" \
-	    "-Wa,-mx86-used-note=yes" \
+	    "$DIRECT_EXTERN_ACCESS_CFLAGS -Wa,-mx86-used-note=yes" \
 	    {pass.c property-stack.S} \
 	    {{readelf {-n} property-2a.r}} \
 	    "property-2.o" \
@@ -1199,7 +1199,7 @@
 	[list \
 	    "Build property-6.o" \
 	    "-r -nostdlib" \
-	    "-Wa,-mx86-used-note=yes" \
+	    "$DIRECT_EXTERN_ACCESS_CFLAGS -Wa,-mx86-used-note=yes" \
 	    {property-6b.c property-stack.S} \
 	    {{readelf {-n} property-2a.r}} \
 	    "property-6.o" \
@@ -1230,16 +1230,16 @@
 	] \
 	[list \
 	    "Build pr22001-1.so" \
-	    "-shared" \
-	    "-fPIC -Wa,-mx86-used-note=yes" \
+	    "-shared -Wl,-z,noindirect-extern-access" \
+	    "$DIRECT_EXTERN_ACCESS_CFLAGS -fPIC -Wa,-mx86-used-note=yes" \
 	    { pr22001-1a.c } \
 	    {} \
 	    "pr22001-1.so" \
 	] \
 	[list \
 	    "Build pr22001-1a" \
-	    "$NOPIE_LDFLAGS -Wl,-z,nocopyreloc,--no-as-needed,-z,notext tmpdir/pr22001-1.so" \
-	    "$NOPIE_CFLAGS -Wa,-mx86-used-note=yes" \
+	    "$NOPIE_LDFLAGS -Wl,-z,nocopyreloc,--no-as-needed,-z,notext,-z,noindirect-extern-access tmpdir/pr22001-1.so" \
+	    "$DIRECT_EXTERN_ACCESS_CFLAGS $NOPIE_CFLAGS -Wa,-mx86-used-note=yes" \
 	    { pr22001-1b.c } \
 	    {{error_output "pr22001-1a.err"}} \
 	    "pr22001-1a" \
@@ -1247,7 +1247,7 @@
 	[list \
 	    "Build pr21997-1.so" \
 	    "-shared" \
-	    "-Wa,-mx86-used-note=yes" \
+	    "$DIRECT_EXTERN_ACCESS_CFLAGS -Wa,-mx86-used-note=yes" \
 	    { property-stack.S property-no-copy.S pr21997-1a.S } \
 	    {} \
 	    "pr21997-1.so" \
@@ -1255,7 +1255,7 @@
 	[list \
 	    "Build pr21997-1a" \
 	    "$NOPIE_LDFLAGS -Wl,--no-as-needed,-z,notext tmpdir/pr21997-1.so" \
-	    "$NOPIE_CFLAGS -Wa,-mx86-used-note=yes" \
+	    "$DIRECT_EXTERN_ACCESS_CFLAGS $NOPIE_CFLAGS -Wa,-mx86-used-note=yes" \
 	    { pr21997-1b.c } \
 	    {{error_output "pr21997-1a.err"}} \
 	    "pr21997-1a" \
@@ -1401,7 +1401,7 @@
 	[list \
 	    "Build protected-func-2 without PIE" \
 	    "$NOPIE_LDFLAGS -Wl,--no-as-needed tmpdir/libprotected-func-2b.so" \
-	    "$NOPIE_CFLAGS -Wa,-mx86-used-note=yes" \
+	    "$DIRECT_EXTERN_ACCESS_CFLAGS $NOPIE_CFLAGS -Wa,-mx86-used-note=yes" \
 	    { protected-func-1b.c } \
 	    {{error_output "pr28875-func.err"}} \
 	    "protected-func-2" \
@@ -1417,7 +1417,7 @@
 	[list \
 	    "Build protected-func-2a without PIE" \
 	    "$NOPIE_LDFLAGS -Wl,--no-as-needed tmpdir/libprotected-func-2c.so" \
-	    "$NOPIE_CFLAGS -Wa,-mx86-used-note=yes" \
+	    "$DIRECT_EXTERN_ACCESS_CFLAGS $NOPIE_CFLAGS -Wa,-mx86-used-note=yes" \
 	    { protected-func-1b.c } \
 	    {{error_output "pr28875-func.err"}} \
 	    "protected-func-2a" \
@@ -1441,7 +1441,7 @@
 	[list \
 	    "Build protected-data-1 without PIE" \
 	    "$NOPIE_LDFLAGS -Wl,--no-as-needed tmpdir/libprotected-data-1b.so" \
-	    "$NOPIE_CFLAGS -Wa,-mx86-used-note=yes" \
+	    "$DIRECT_EXTERN_ACCESS_CFLAGS $NOPIE_CFLAGS -Wa,-mx86-used-note=yes" \
 	    { protected-data-1b.c } \
 	    {{error_output "pr28875-data.err"}} \
 	    "protected-data-1" \
@@ -1535,7 +1535,7 @@
 		{ pr22001-1c.c } \
 		"pr22001-1b" \
 		"pass.out" \
-		"$NOPIE_CFLAGS" \
+		"$DIRECT_EXTERN_ACCESS_CFLAGS $NOPIE_CFLAGS" \
 	    ] \
 	    [list \
 		"Run pr25416-5a (GDesc -> IE -maddress-mode=short)" \
@@ -1603,8 +1603,8 @@
 	run_cc_link_tests [list \
 	    [list \
 		"Build pr22001-1b" \
-		"$NOPIE_LDFLAGS -Wl,-z,nocopyreloc,--no-as-needed,-z,notext tmpdir/pr22001-1.so" \
-		"$NOPIE_CFLAGS -Wa,-mx86-used-note=yes" \
+		"$NOPIE_LDFLAGS -Wl,-z,nocopyreloc,--no-as-needed,-z,notext,-z,noindirect-extern-access tmpdir/pr22001-1.so" \
+		"$DIRECT_EXTERN_ACCESS_CFLAGS $NOPIE_CFLAGS -Wa,-mx86-used-note=yes" \
 		{ pr22001-1c.c } \
 		{{error_output "pr22001-1b.err"}} \
 		"pr22001-1b" \
@@ -1612,14 +1612,14 @@
 	    [list \
 		"Build pr21997-1b" \
 		"$NOPIE_LDFLAGS -Wl,--no-as-needed,-z,notext tmpdir/pr21997-1.so" \
-		"$NOPIE_CFLAGS -Wa,-mx86-used-note=yes" \
+		"$DIRECT_EXTERN_ACCESS_CFLAGS $NOPIE_CFLAGS -Wa,-mx86-used-note=yes" \
 		{ pr21997-1c.c } \
 		{{error_output "pr21997-1b.err"}} \
 		"pr21997-1b" \
 	    ] \
 	    [list \
 		"Build lam-u48.so" \
-		"-shared -Wl,-z,lam-u48" \
+		"-shared -Wl,-z,lam-u48,-z,noindirect-extern-access" \
 		"" \
 		{dummy.s} \
 		{{readelf -n lam-u48.rd}}  \
@@ -1627,7 +1627,7 @@
 	    ] \
 	    [list \
 		"Build lam-u57.so" \
-		"-shared -Wl,-z,lam-u57" \
+		"-shared -Wl,-z,lam-u57,-z,noindirect-extern-access" \
 		"" \
 		{dummy.s} \
 		{{readelf -n lam-u57.rd}}  \
@@ -1659,23 +1659,23 @@
 	[list \
 	    "Run copyreloc-main with PIE without -fPIE" \
 	    "-Wl,--as-needed -pie tmpdir/copyreloc-main.o tmpdir/copyreloc-lib.so" \
-	    "-Wa,-mx86-used-note=yes" \
+	    "$DIRECT_EXTERN_ACCESS_CFLAGS -Wa,-mx86-used-note=yes" \
 	    { dummy.s } \
 	    "copyreloc-main" \
 	    "copyreloc-main.out" \
 	] \
 	[list \
 	    "Run pr17689 with PIE without -fPIE" \
-	    "-Wl,--no-as-needed -pie tmpdir/pr17689b.o tmpdir/pr17689.so" \
-	    "-Wa,-mx86-used-note=yes" \
+	    "-Wl,--no-as-needed,-z,noindirect-extern-access -pie tmpdir/pr17689b.o tmpdir/pr17689.so" \
+	    "$DIRECT_EXTERN_ACCESS_CFLAGS -Wa,-mx86-used-note=yes" \
 	    { dummy.s } \
 	    "pr17689" \
 	    "pr17689.out" \
 	] \
 	[list \
 	    "Run pr17689 with PIE -z now without -fPIE" \
-	    "-Wl,--as-needed,-z,now -pie tmpdir/pr17689b.o tmpdir/pr17689.so" \
-	    "-Wa,-mx86-used-note=yes" \
+	    "-Wl,--as-needed,-z,now,-z,noindirect-extern-access -pie tmpdir/pr17689b.o tmpdir/pr17689.so" \
+	    "$DIRECT_EXTERN_ACCESS_CFLAGS -Wa,-mx86-used-note=yes" \
 	    { dummy.s } \
 	    "pr17689now" \
 	    "pr17689.out" \
@@ -1750,25 +1750,25 @@
 	[list \
 	    "Run pr22001-1a (PIC 1)" \
 	    "$NOPIE_LDFLAGS -Wl,-z,nocopyreloc,--no-as-needed tmpdir/pr22001-1.so" \
-	    "-Wa,-mx86-used-note=yes" \
+	    "$DIRECT_EXTERN_ACCESS_CFLAGS -Wa,-mx86-used-note=yes" \
 	    { pr22001-1b.c } \
 	    "pr22001-1a-pic-1" \
 	    "pass.out" \
-	    "-fPIC" \
+	    "$DIRECT_EXTERN_ACCESS_CFLAGS -fPIC" \
 	] \
 	[list \
 	    "Run pr22001-1a (PIC 2)" \
 	    "-pie -Wl,-z,nocopyreloc,--no-as-needed tmpdir/pr22001-1.so" \
-	    "-Wa,-mx86-used-note=yes" \
+	    "$DIRECT_EXTERN_ACCESS_CFLAGS -Wa,-mx86-used-note=yes" \
 	    { pr22001-1b.c } \
 	    "pr22001-1a-pic-2" \
 	    "pass.out" \
-	    "-fPIC" \
+	    "$DIRECT_EXTERN_ACCESS_CFLAGS -fPIC" \
 	] \
 	[list \
 	    "Run pr22001-1b (PIC 1)" \
 	    "$NOPIE_LDFLAGS -Wl,-z,nocopyreloc,--no-as-needed tmpdir/pr22001-1.so" \
-	    "-Wa,-mx86-used-note=yes" \
+	    "$DIRECT_EXTERN_ACCESS_CFLAGS -Wa,-mx86-used-note=yes" \
 	    { pr22001-1c.c } \
 	    "pr22001-1b-pic-1" \
 	    "pass.out" \
@@ -1777,7 +1777,7 @@
 	[list \
 	    "Run pr22001-1b (PIC 2)" \
 	    "-pie -Wl,-z,nocopyreloc,--no-as-needed tmpdir/pr22001-1.so" \
-	    "-Wa,-mx86-used-note=yes" \
+	    "$DIRECT_EXTERN_ACCESS_CFLAGS -Wa,-mx86-used-note=yes" \
 	    { pr22001-1c.c } \
 	    "pr22001-1b-pic-2" \
 	    "pass.out" \
@@ -2196,7 +2196,7 @@
 	}
     }
 
-    undefined_weak "$NOPIE_CFLAGS" "$NOPIE_LDFLAGS"
+    undefined_weak "$DIRECT_EXTERN_ACCESS_CFLAGS $NOPIE_CFLAGS" "$NOPIE_LDFLAGS"
     undefined_weak "-fPIE" "$NOPIE_LDFLAGS"
     undefined_weak "-fPIE" "-pie"
     undefined_weak "-fPIE" "$NOPIE_LDFLAGS -Wl,-z,nodynamic-undefined-weak"