CC_FOR_TARGET et al

Message ID YTHIRZrRYehHOpxp@squeak.grove.modra.org
State New
Headers show
Series
  • CC_FOR_TARGET et al
Related show

Commit Message

Alan Modra via Binutils Sept. 3, 2021, 7:01 a.m.
The top level Makefile, the ld Makefile and others, define
CC_FOR_TARGET to be a compiler for the binutils target machine.  This
is the compiler that should be used for almost all tests with C
source.  There are _FOR_TARGET versions of CFLAGS, CXX, and CXXFLAGS
too.  This was all supposed to work with the testsuite .exp files
using CC for the target compiler, and CC_FOR_HOST for the host
compiler, with the makefiles passing CC=$CC_FOR_TARGET and
CC_FOR_HOST=$CC to the runtest invocation.

One exception to the rule of using CC_FOR_TARGET is the native-only ld
bootstrap test, which uses the newly built ld to link a copy of
itself.  Since the files being linked were created with the host
compiler, the boostrap test should use CC and CFLAGS, in case some
host compiler option provides needed libraries automatically.
However, bootstrap.exp used CC where it should have used CC_FOR_HOST.
I set about fixing that problem, then decided that playing games in
the makefiles with CC was a bad idea.  Not only is it confusing, but
other dejagnu code knows about CC_FOR_TARGET.  See dejagnu/target.exp.

So this patch gets rid of the makefile variable renaming and changes
all the .exp files to use the correct _FOR_TARGET variables.
CC_FOR_HOST and CFLAGS_FOR_HOST disappear.  A followup patch will
correct bootstrap.exp to use CFLAGS, and a number of other things I
noticed.

binutils/
	* testsuite/lib/binutils-common.exp (run_dump_test): Use
	CC_FOR_TARGET and CFLAGS_FOR_TARGET rather than CC and CFLAGS.
ld/
	* Makefile.am (check-DEJAGNU): Don't set CC to CC_FOR_TARGET
	and similar.  Pass variables with unchanged names.  Don't set
	CC_FOR_HOST or CFLAGS_FOR_HOST.
	* Makefile.in: Regenerate.
	* testsuite/config/default.exp: Update default CC and similar.
	(compiler_supports, plug_opt): Use CC_FOR_TARGET.
	* testsuite/ld-cdtest/cdtest.exp: Replace all uses of CC with
	CC_FOR_TARGET, and similarly for CFLAGS, CXX and CXXFLAGS.
	* testsuite/ld-auto-import/auto-import.exp: Likewise.
	* testsuite/ld-cygwin/exe-export.exp: Likewise.
	* testsuite/ld-elf/dwarf.exp: Likewise.
	* testsuite/ld-elf/indirect.exp: Likewise.
	* testsuite/ld-elf/shared.exp: Likewise.
	* testsuite/ld-elfcomm/elfcomm.exp: Likewise.
	* testsuite/ld-elfvers/vers.exp: Likewise.
	* testsuite/ld-elfvsb/elfvsb.exp: Likewise.
	* testsuite/ld-elfweak/elfweak.exp: Likewise.
	* testsuite/ld-gc/gc.exp: Likewise.
	* testsuite/ld-ifunc/ifunc.exp: Likewise.
	* testsuite/ld-mn10300/mn10300.exp: Likewise.
	* testsuite/ld-pe/pe-compile.exp: Likewise.
	* testsuite/ld-pe/pe-run.exp: Likewise.
	* testsuite/ld-pe/pe-run2.exp: Likewise.
	* testsuite/ld-pie/pie.exp: Likewise.
	* testsuite/ld-plugin/lto.exp: Likewise.
	* testsuite/ld-plugin/plugin.exp: Likewise.
	* testsuite/ld-scripts/crossref.exp: Likewise.
	* testsuite/ld-selective/selective.exp: Likewise.
	* testsuite/ld-sh/sh.exp: Likewise.
	* testsuite/ld-shared/shared.exp: Likewise.
	* testsuite/ld-srec/srec.exp: Likewise.
	* testsuite/ld-undefined/undefined.exp: Likewise.
	* testsuite/ld-unique/unique.exp: Likewise.
	* testsuite/ld-x86-64/tls.exp: Likewise.
	* testsuite/lib/ld-lib.exp: Likewise.
libctf/
	* Makefile.am (check-DEJAGNU): Don't set CC to CC_FOR_TARGET.
	Pass CC and CC_FOR_TARGET.  Don't set CC_FOR_HOST.
	* Makefile.in: Regenerate.
	* testsuite/config/default.exp: Update default CC and similar.
	* testsuite/lib/ctf-lib.exp (run_native_host_cmd): Use CC rather
	than CC_FOR_HOST.
	(run_lookup_test): Use CC_FOR_TARGET and CFLAGS_FOR_TARGET.


-- 
Alan Modra
Australia Development Lab, IBM

Patch

diff --git a/binutils/testsuite/lib/binutils-common.exp b/binutils/testsuite/lib/binutils-common.exp
index 0419b3b0b93..759f7c798c0 100644
--- a/binutils/testsuite/lib/binutils-common.exp
+++ b/binutils/testsuite/lib/binutils-common.exp
@@ -851,9 +851,9 @@  if ![string length [info proc prune_warnings]] {
 # regexps in FILE.d.
 #
 proc run_dump_test { name {extra_options {}} } {
-    global ADDR2LINE ADDR2LINEFLAGS AS ASFLAGS CC CFLAGS ELFEDIT ELFEDITFLAGS
-    global LD LDFLAGS NM NMFLAGS OBJCOPY OBJCOPYFLAGS OBJDUMP OBJDUMPFLAGS
-    global READELF READELFFLAGS STRIP STRIPFLAGS
+    global ADDR2LINE ADDR2LINEFLAGS AS ASFLAGS CC_FOR_TARGET CFLAGS_FOR_TARGET
+    global ELFEDIT ELFEDITFLAGS LD LDFLAGS NM NMFLAGS OBJCOPY OBJCOPYFLAGS
+    global OBJDUMP OBJDUMPFLAGS READELF READELFFLAGS STRIP STRIPFLAGS
     global copyfile env runtests srcdir subdir verbose
 
     if [string match "*/*" $name] {
@@ -1159,7 +1159,7 @@  proc run_dump_test { name {extra_options {}} } {
 	    # ofile is never absolute, so this always works to protect sfile
 	    # from later absolutization.
 	    set sfile "./[file rootname $ofile].s"
-	    set cmd "$CC $CFLAGS -S $opts(cc) -o $sfile $cfile"
+	    set cmd "$CC_FOR_TARGET $CFLAGS_FOR_TARGET -S $opts(cc) -o $sfile $cfile"
 	    send_log "$cmd\n"
 	    set cmdret [remote_exec host [concat sh -c [list "$cmd 2>&1"]] "" "/dev/null" "dump.tmp"]
 	    remote_upload host "dump.tmp"
diff --git a/ld/Makefile.am b/ld/Makefile.am
index 97f336dee40..0345f846ca0 100644
--- a/ld/Makefile.am
+++ b/ld/Makefile.am
@@ -980,9 +980,12 @@  check-DEJAGNU: site.exp
 	runtest=$(RUNTEST); \
 	if $(SHELL) -c "$$runtest --version" > /dev/null 2>&1; then \
 	  $$runtest --tool $(DEJATOOL) --srcdir $${srcroot}/testsuite \
-		CC="$(CC_FOR_TARGET)" CFLAGS="$(CFLAGS_FOR_TARGET)" \
-		CXX="$(CXX_FOR_TARGET)" CXXFLAGS="$(CXXFLAGS_FOR_TARGET)" \
-		CC_FOR_HOST="$(CC)" CFLAGS_FOR_HOST="$(CFLAGS)" \
+		CC="$(CC)" CFLAGS="$(CFLAGS)" \
+		CXX="$(CXX)" CXXFLAGS="$(CXXFLAGS)" \
+		CC_FOR_TARGET="$(CC_FOR_TARGET)" \
+		CFLAGS_FOR_TARGET="$(CFLAGS_FOR_TARGET)" \
+		CXX_FOR_TARGET="$(CXX_FOR_TARGET)" \
+		CXXFLAGS_FOR_TARGET="$(CXXFLAGS_FOR_TARGET)" \
 		OFILES="$(OFILES)" BFDLIB="$(TESTBFDLIB)" CTFLIB="$(TESTCTFLIB) $(ZLIB)" \
 		LIBIBERTY="$(LIBIBERTY) $(LIBINTL)" LIBS="$(LIBS)" \
 		DO_COMPARE="`echo '$(do_compare)' | sed -e 's,\\$$,,g'`" \
diff --git a/ld/Makefile.in b/ld/Makefile.in
index b8ceca3938a..366d03489df 100644
--- a/ld/Makefile.in
+++ b/ld/Makefile.in
@@ -2625,9 +2625,12 @@  check-DEJAGNU: site.exp
 	runtest=$(RUNTEST); \
 	if $(SHELL) -c "$$runtest --version" > /dev/null 2>&1; then \
 	  $$runtest --tool $(DEJATOOL) --srcdir $${srcroot}/testsuite \
-		CC="$(CC_FOR_TARGET)" CFLAGS="$(CFLAGS_FOR_TARGET)" \
-		CXX="$(CXX_FOR_TARGET)" CXXFLAGS="$(CXXFLAGS_FOR_TARGET)" \
-		CC_FOR_HOST="$(CC)" CFLAGS_FOR_HOST="$(CFLAGS)" \
+		CC="$(CC)" CFLAGS="$(CFLAGS)" \
+		CXX="$(CXX)" CXXFLAGS="$(CXXFLAGS)" \
+		CC_FOR_TARGET="$(CC_FOR_TARGET)" \
+		CFLAGS_FOR_TARGET="$(CFLAGS_FOR_TARGET)" \
+		CXX_FOR_TARGET="$(CXX_FOR_TARGET)" \
+		CXXFLAGS_FOR_TARGET="$(CXXFLAGS_FOR_TARGET)" \
 		OFILES="$(OFILES)" BFDLIB="$(TESTBFDLIB)" CTFLIB="$(TESTCTFLIB) $(ZLIB)" \
 		LIBIBERTY="$(LIBIBERTY) $(LIBINTL)" LIBS="$(LIBS)" \
 		DO_COMPARE="`echo '$(do_compare)' | sed -e 's,\\$$,,g'`" \
diff --git a/ld/testsuite/config/default.exp b/ld/testsuite/config/default.exp
index 0795ea81bfb..8507a1f0208 100644
--- a/ld/testsuite/config/default.exp
+++ b/ld/testsuite/config/default.exp
@@ -76,20 +76,23 @@  if {[file exists tmpdir/libpath.exp]} {
     }
 }
 
-# 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.  
 if {![info exists CC]} {
-    set CC [find_gcc]
+    set CC gcc
 }
 if {![info exists CFLAGS]} {
     set CFLAGS "-g -O2"
 }
-if {![info exists CXX]} {
-    set CXX [find_g++]
+if {![info exists CC_FOR_TARGET]} {
+    set CC_FOR_TARGET [find_gcc]
 }
-if {![info exists CXXFLAGS]} {
-    set CXXFLAGS ""
+if {![info exists CFLAGS_FOR_TARGET]} {
+    set CFLAGS_FOR_TARGET "-g -O2"
+}
+if {![info exists CXX_FOR_TARGET]} {
+    set CXX_FOR_TARGET [find_g++]
+}
+if {![info exists CXXFLAGS_FOR_TARGET]} {
+    set CXXFLAGS_FOR_TARGET ""
 }
 
 # This allows us to run the linker testsuite with clang as the compilation
@@ -324,7 +327,7 @@  if { ![info exists LD_CLASS] } then {
 # Compiler tests
 proc compiler_supports { flag args } {
     if { [check_compiler_available] } {
-	global CC
+	global CC_FOR_TARGET
 	set flags ""
 	if [board_info [target_info name] exists cflags] {
 	    append flags " [board_info [target_info name] cflags]"
@@ -341,7 +344,7 @@  proc compiler_supports { flag args } {
 	}
 	close $f
 	set rfn [remote_download host $fn]
-	set avail [run_host_cmd_yesno "$CC" "$flags $flag $rfn -o /dev/null"]
+	set avail [run_host_cmd_yesno "$CC_FOR_TARGET" "$flags $flag $rfn -o /dev/null"]
 	remote_file host delete $rfn
 	file delete $fn
 	return $avail
@@ -442,9 +445,9 @@  if { [check_compiler_available] } {
 	cyglto_plugin-0.dll
     }
     foreach plug $plugin_names {
-	set plug_so [run_host_cmd $CC "--print-prog-name $plug"]
+	set plug_so [run_host_cmd $CC_FOR_TARGET "--print-prog-name $plug"]
 	if { $plug_so eq $plug } then {
-	    set plug_so [run_host_cmd $CC "--print-file-name $plug"]
+	    set plug_so [run_host_cmd $CC_FOR_TARGET "--print-file-name $plug"]
 	}
 	if { $plug_so ne $plug } then {
 	    set plug_opt "--plugin $plug_so"
diff --git a/ld/testsuite/ld-auto-import/auto-import.exp b/ld/testsuite/ld-auto-import/auto-import.exp
index 39a0e376849..74e8db293ae 100644
--- a/ld/testsuite/ld-auto-import/auto-import.exp
+++ b/ld/testsuite/ld-auto-import/auto-import.exp
@@ -118,7 +118,7 @@  if [istarget *-pc-cygwin] {
     set MYLIBS "-L/usr/lib -lcygwin -L/usr/lib/w32api -lkernel32"
     
     # Compile the dll.
-    if ![ld_compile "$CC $CFLAGS $SHCFLAG" $srcdir/$subdir/dll.c $tmpdir/dll.o] {
+    if ![ld_compile "$CC_FOR_TARGET $CFLAGS_FOR_TARGET $SHCFLAG" $srcdir/$subdir/dll.c $tmpdir/dll.o] {
 	fail "compiling shared lib"
     }
     if ![ld_special_link "$ld -shared --enable-auto-import -e __cygwin_dll_entry@12 --out-implib=$tmpdir/libstandard.dll.a" $tmpdir/dll.dll "$tmpdir/dll.o $MYLIBS"] {
@@ -129,7 +129,7 @@  if [istarget *-pc-cygwin] {
     catch "exec ln -fs dll.dll $tmpdir/libsymlinked_dll.dll.a" ln_catch
 
     # Compile and link the client program.
-    if ![ld_compile "$CC $CFLAGS $SHCFLAG" $srcdir/$subdir/client.c $tmpdir/client.o] {
+    if ![ld_compile "$CC_FOR_TARGET $CFLAGS_FOR_TARGET $SHCFLAG" $srcdir/$subdir/client.c $tmpdir/client.o] {
         fail "compiling client"
     }
 
diff --git a/ld/testsuite/ld-cdtest/cdtest.exp b/ld/testsuite/ld-cdtest/cdtest.exp
index 80119802d8d..50f6ff1e40b 100644
--- a/ld/testsuite/ld-cdtest/cdtest.exp
+++ b/ld/testsuite/ld-cdtest/cdtest.exp
@@ -32,15 +32,15 @@  if ![isnative] {
     return
 }
 
-if { [which $CXX] == 0 } {
+if { [which $CXX_FOR_TARGET] == 0 } {
     untested $test1
     untested $test2
     return
 }
 
-if {   ![ld_compile "$CXX $CXXFLAGS -fno-exceptions" $srcdir/$subdir/cdtest-foo.cc tmpdir/cdtest-foo.o]
-    || ![ld_compile "$CXX $CXXFLAGS -fno-exceptions" $srcdir/$subdir/cdtest-bar.cc tmpdir/cdtest-bar.o]
-    || ![ld_compile "$CXX $CXXFLAGS -fno-exceptions" $srcdir/$subdir/cdtest-main.cc tmpdir/cdtest-main.o] } {
+if {   ![ld_compile "$CXX_FOR_TARGET $CXXFLAGS_FOR_TARGET -fno-exceptions" $srcdir/$subdir/cdtest-foo.cc tmpdir/cdtest-foo.o]
+    || ![ld_compile "$CXX_FOR_TARGET $CXXFLAGS_FOR_TARGET -fno-exceptions" $srcdir/$subdir/cdtest-bar.cc tmpdir/cdtest-bar.o]
+    || ![ld_compile "$CXX_FOR_TARGET $CXXFLAGS_FOR_TARGET -fno-exceptions" $srcdir/$subdir/cdtest-main.cc tmpdir/cdtest-main.o] } {
     unsupported $test1
     unsupported $test2
     return
@@ -48,7 +48,7 @@  if {   ![ld_compile "$CXX $CXXFLAGS -fno-exceptions" $srcdir/$subdir/cdtest-foo.
 
 set expected_output "$srcdir/$subdir/cdtest.dat"
 
-if ![ld_link $CC tmpdir/cdtest {tmpdir/cdtest-foo.o tmpdir/cdtest-bar.o tmpdir/cdtest-main.o}] {
+if ![ld_link $CC_FOR_TARGET tmpdir/cdtest {tmpdir/cdtest-foo.o tmpdir/cdtest-bar.o tmpdir/cdtest-main.o}] {
     fail $test1
 } else {
     send_log   "tmpdir/cdtest >tmpdir/cdtest.out\n"
@@ -100,7 +100,7 @@  if [is_pecoff_format] {
 if ![ld_relocate $ld tmpdir/cdtest.o {-Ur tmpdir/cdtest-foo.o tmpdir/cdtest-bar.o tmpdir/cdtest-main.o}] {
     fail $test2
 } else {
-    if ![ld_link $CC tmpdir/cdtest tmpdir/cdtest.o] {
+    if ![ld_link $CC_FOR_TARGET tmpdir/cdtest tmpdir/cdtest.o] {
 	fail $test2
     } else {
 	send_log   "tmpdir/cdtest >tmpdir/cdtest.out\n"
diff --git a/ld/testsuite/ld-cygwin/exe-export.exp b/ld/testsuite/ld-cygwin/exe-export.exp
index b9ad7598b8e..1e5b93c51ce 100644
--- a/ld/testsuite/ld-cygwin/exe-export.exp
+++ b/ld/testsuite/ld-cygwin/exe-export.exp
@@ -113,23 +113,23 @@  if ![run_dlltool $tmpdir/testdll.lib $srcdir/$subdir/testdll.def] {
 } 
 
 # Compile the executable.
-if ![ld_compile "$CC $CFLAGS" $srcdir/$subdir/testexe.c $tmpdir/testexe.o] {
+if ![ld_compile "$CC_FOR_TARGET $CFLAGS_FOR_TARGET" $srcdir/$subdir/testexe.c $tmpdir/testexe.o] {
     fail "compiling executable"
     return
 }
 
-if ![ld_special_link "$CC $LDFLAGS $MYLDFLAGS -e _testexe_main@16" $tmpdir/testexe.exe "$tmpdir/testexe.o $srcdir/$subdir/testexe.def $tmpdir/testdll.lib -lkernel32"] {
+if ![ld_special_link "$CC_FOR_TARGET $LDFLAGS $MYLDFLAGS -e _testexe_main@16" $tmpdir/testexe.exe "$tmpdir/testexe.o $srcdir/$subdir/testexe.def $tmpdir/testdll.lib -lkernel32"] {
     fail "linking executable"
     return
 } 
 
 # Compile the dll.
-if ![ld_compile "$CC $CFLAGS" $srcdir/$subdir/testdll.c $tmpdir/testdll.o] {
+if ![ld_compile "$CC_FOR_TARGET $CFLAGS_FOR_TARGET" $srcdir/$subdir/testdll.c $tmpdir/testdll.o] {
     fail "compiling shared lib"
     return
 }
 
-if ![ld_special_link "$CC $LDFLAGS -nostartfiles -nostdlib -e _testdll_main@12" $tmpdir/testdll.dll "$tmpdir/testdll.o $srcdir/$subdir/testdll.def $tmpdir/testexe.lib"] {
+if ![ld_special_link "$CC_FOR_TARGET $LDFLAGS -nostartfiles -nostdlib -e _testdll_main@12" $tmpdir/testdll.dll "$tmpdir/testdll.o $srcdir/$subdir/testdll.def $tmpdir/testexe.lib"] {
     fail "linking shared lib"
     return
 } 
diff --git a/ld/testsuite/ld-elf/dwarf.exp b/ld/testsuite/ld-elf/dwarf.exp
index e620651efb0..61973ac82f7 100644
--- a/ld/testsuite/ld-elf/dwarf.exp
+++ b/ld/testsuite/ld-elf/dwarf.exp
@@ -41,7 +41,7 @@  if { ![check_compiler_available] } {
 }
 
 # Skip if -feliminate-dwarf2-dups isn't supported.
-if ![ld_compile "$CC -g -feliminate-dwarf2-dups" $srcdir/$subdir/dummy.c tmpdir/dummy.o] {
+if ![ld_compile "$CC_FOR_TARGET -g -feliminate-dwarf2-dups" $srcdir/$subdir/dummy.c tmpdir/dummy.o] {
     return
 }
 
@@ -69,11 +69,11 @@  set run_tests {
 }
 
 # Disable all sanitizers.
-set old_CFLAGS "$CFLAGS"
-append CFLAGS " $NOSANITIZE_CFLAGS"
+set old_CFLAGS "$CFLAGS_FOR_TARGET"
+append CFLAGS_FOR_TARGET " $NOSANITIZE_CFLAGS"
 run_cc_link_tests $build_tests
 run_ld_link_exec_tests $run_tests
-set CFLAGS "$old_CFLAGS"
+set CFLAGS_FOR_TARGET "$old_CFLAGS"
 
 proc strip_test {} {
     global ld
diff --git a/ld/testsuite/ld-elf/indirect.exp b/ld/testsuite/ld-elf/indirect.exp
index bd4dab809db..c1bf23a575b 100644
--- a/ld/testsuite/ld-elf/indirect.exp
+++ b/ld/testsuite/ld-elf/indirect.exp
@@ -38,7 +38,7 @@  if { ![check_compiler_available] } {
 }
 
 # Some bare-metal targets don't support shared libs or PIC.
-if { ![run_host_cmd_yesno $CC "-shared -fPIC $srcdir/$subdir/dummy.c -o tmpdir/t.so"] } {
+if { ![run_host_cmd_yesno $CC_FOR_TARGET "-shared -fPIC $srcdir/$subdir/dummy.c -o tmpdir/t.so"] } {
     return
 }
 
@@ -59,7 +59,7 @@  proc check_link_message { cmd string testname } {
 }
 
 # Disable LTO for these tests.
-set cc_cmd "$CC"
+set cc_cmd "$CC_FOR_TARGET"
 if {[check_lto_available]} {
    append cc_cmd " -fno-lto"
 }
@@ -226,8 +226,8 @@  foreach t [list indirect5a indirect5b indirect6a indirect6b] {
     }
 }
 
-send_log "$CC -fPIE -pie $srcdir/$subdir/main.c -o tmpdir/pie"
-catch "exec $CC -fPIE -pie $srcdir/$subdir/main.c -o tmpdir/pie" exec_output
+send_log "$CC_FOR_TARGET -fPIE -pie $srcdir/$subdir/main.c -o tmpdir/pie"
+catch "exec $CC_FOR_TARGET -fPIE -pie $srcdir/$subdir/main.c -o tmpdir/pie" exec_output
 send_log "$exec_output"
 if { ! [string match "" $exec_output] } {
     return
diff --git a/ld/testsuite/ld-elf/shared.exp b/ld/testsuite/ld-elf/shared.exp
index 4eb5fba94f8..9224782d234 100644
--- a/ld/testsuite/ld-elf/shared.exp
+++ b/ld/testsuite/ld-elf/shared.exp
@@ -1180,36 +1180,36 @@  if [check_libdl_available] {
   # XFAIL on NetBSD ELF systems as they do not currently support the .*_array
   # sections.
   # Disable all sanitizers.
-  set old_CFLAGS "$CFLAGS"
-  append CFLAGS " $NOSANITIZE_CFLAGS"
+  set old_CFLAGS "$CFLAGS_FOR_TARGET"
+  append CFLAGS_FOR_TARGET " $NOSANITIZE_CFLAGS"
   run_ld_link_exec_tests $dlopen_run_tests "*-*-netbsd*"
-  set CFLAGS "$old_CFLAGS"
+  set CFLAGS_FOR_TARGET "$old_CFLAGS"
 }
 
 # Check --no-add-needed and --no-copy-dt-needed-entries
 set testname "--no-add-needed"
-set exec_output [run_host_cmd "$CC" "tmpdir/libneeded1c.o -Wl,--no-add-needed,-rpath-link=tmpdir -Ltmpdir -lneeded1a"]
+set exec_output [run_host_cmd "$CC_FOR_TARGET" "tmpdir/libneeded1c.o -Wl,--no-add-needed,-rpath-link=tmpdir -Ltmpdir -lneeded1a"]
 if { [ regexp "tmpdir/libneeded1b.so: .*: DSO missing" $exec_output ] } {
     pass $testname
 } {
     fail $testname
 }
 set testname "--no-copy-dt-needed-entries"
-set exec_output [run_host_cmd "$CC" "tmpdir/libneeded1c.o -Wl,--no-copy-dt-needed-entries,-rpath-link=tmpdir -Ltmpdir -lneeded1a"]
+set exec_output [run_host_cmd "$CC_FOR_TARGET" "tmpdir/libneeded1c.o -Wl,--no-copy-dt-needed-entries,-rpath-link=tmpdir -Ltmpdir -lneeded1a"]
 if { [ regexp "tmpdir/libneeded1b.so: .*: DSO missing" $exec_output ] } {
     pass $testname
 } {
     fail $testname
 }
 set testname "--no-add-needed -shared"
-set exec_output [run_host_cmd "$CC" "-shared tmpdir/libneeded1pic.o -Wl,--no-add-needed,-z,defs -Ltmpdir -lneeded1a"]
+set exec_output [run_host_cmd "$CC_FOR_TARGET" "-shared tmpdir/libneeded1pic.o -Wl,--no-add-needed,-z,defs -Ltmpdir -lneeded1a"]
 if { [ regexp "undefined reference to `\.?bar'" $exec_output ] } {
     pass $testname
 } {
     fail $testname
 }
 set testname "--no-copy-dt-needed-entries -shared"
-set exec_output [run_host_cmd "$CC" "-shared tmpdir/libneeded1pic.o -Wl,--no-copy-dt-needed-entries,-z,defs -Ltmpdir -lneeded1a"]
+set exec_output [run_host_cmd "$CC_FOR_TARGET" "-shared tmpdir/libneeded1pic.o -Wl,--no-copy-dt-needed-entries,-z,defs -Ltmpdir -lneeded1a"]
 if { [ regexp "undefined reference to `\.?bar'" $exec_output ] } {
     pass $testname
 } {
@@ -1217,7 +1217,7 @@  if { [ regexp "undefined reference to `\.?bar'" $exec_output ] } {
 }
 
 # Check to see if the C++ compiler works
-if { [which $CXX] == 0 } {
+if { [which $CXX_FOR_TARGET] == 0 } {
     return
 }
 
diff --git a/ld/testsuite/ld-elfcomm/elfcomm.exp b/ld/testsuite/ld-elfcomm/elfcomm.exp
index 4e4c92d24b3..b74910440ae 100644
--- a/ld/testsuite/ld-elfcomm/elfcomm.exp
+++ b/ld/testsuite/ld-elfcomm/elfcomm.exp
@@ -164,14 +164,14 @@  proc stt_common_test { options testname } {
 }
 
 if [istarget nios2*-*-*] {
-    set CFLAGS "$CFLAGS -G0"
+    append CFLAGS_FOR_TARGET " -G0"
 }
 
 # Explicitly use "-fcommon" so that even if $CFLAGS includes
 # "-fno-common", these tests are compiled as expected.
-if {   ![ld_compile "$CC $CFLAGS -fcommon $NOSANITIZE_CFLAGS $NOLTO_CFLAGS" $srcdir/$subdir/common1a.c tmpdir/common1a.o]
-    || ![ld_compile "$CC $CFLAGS -fcommon $NOSANITIZE_CFLAGS $NOLTO_CFLAGS" $srcdir/$subdir/common1b.c tmpdir/common1b.o]
-    || ![ld_compile "$CC $CFLAGS $NOSANITIZE_CFLAGS $NOLTO_CFLAGS -Wa,--elf-stt-common=yes -fcommon" $srcdir/$subdir/common1b.c tmpdir/common1c.o] } {
+if {   ![ld_compile "$CC_FOR_TARGET $CFLAGS_FOR_TARGET -fcommon $NOSANITIZE_CFLAGS $NOLTO_CFLAGS" $srcdir/$subdir/common1a.c tmpdir/common1a.o]
+    || ![ld_compile "$CC_FOR_TARGET $CFLAGS_FOR_TARGET -fcommon $NOSANITIZE_CFLAGS $NOLTO_CFLAGS" $srcdir/$subdir/common1b.c tmpdir/common1b.o]
+    || ![ld_compile "$CC_FOR_TARGET $CFLAGS_FOR_TARGET $NOSANITIZE_CFLAGS $NOLTO_CFLAGS -Wa,--elf-stt-common=yes -fcommon" $srcdir/$subdir/common1b.c tmpdir/common1c.o] } {
     unsupported $test1
     return
 }
diff --git a/ld/testsuite/ld-elfvers/vers.exp b/ld/testsuite/ld-elfvers/vers.exp
index 8a1383fe840..b97da8014e9 100644
--- a/ld/testsuite/ld-elfvers/vers.exp
+++ b/ld/testsuite/ld-elfvers/vers.exp
@@ -73,9 +73,9 @@  if { [istarget mips*-*-*] && ! [at_least_gcc_version 4 3] } then {
     # Unfortunately, the gcc argument is -fpic and the cc argument is
     # -KPIC.  We have to try both.
     set picflag "-fpic"
-    send_log "$CC $picflag\n"
-    verbose "$CC $picflag"
-    catch "exec $CC $picflag" exec_output
+    send_log "$CC_FOR_TARGET $picflag\n"
+    verbose "$CC_FOR_TARGET $picflag"
+    catch "exec $CC_FOR_TARGET $picflag" exec_output
     send_log "$exec_output\n"
     verbose "--" "$exec_output"
     if { [string match "*illegal option*" $exec_output]
@@ -486,13 +486,13 @@  proc build_binary { shared pic test source libname other mapfile verexp versymex
     global as
     global as_options
     global objdump
-    global CC
-    global CFLAGS
+    global CC_FOR_TARGET
+    global CFLAGS_FOR_TARGET
     global script
     global NOSANITIZE_CFLAGS
     global NOLTO_CFLAGS
 
-    if ![ld_compile "$CC -S $pic $CFLAGS $NOSANITIZE_CFLAGS $NOLTO_CFLAGS" $srcdir/$subdir/$source $tmpdir/$libname.s]     {
+    if ![ld_compile "$CC_FOR_TARGET -S $pic $CFLAGS_FOR_TARGET $NOSANITIZE_CFLAGS $NOLTO_CFLAGS" $srcdir/$subdir/$source $tmpdir/$libname.s]     {
 	unsupported "$test"
 	return
     }
@@ -578,8 +578,8 @@  proc test_ldfail { test flag source execname other mapfile whyfail } {
     global as
     global as_options
     global objdump
-    global CC
-    global CFLAGS
+    global CC_FOR_TARGET
+    global CFLAGS_FOR_TARGET
     global script
 
     if [string match "" $other] then {
@@ -588,7 +588,7 @@  proc test_ldfail { test flag source execname other mapfile whyfail } {
 	set other_lib $tmpdir/$other
     }
 
-    if ![ld_compile "$CC -S $flag $CFLAGS" $srcdir/$subdir/$source $tmpdir/$execname.s]     {
+    if ![ld_compile "$CC_FOR_TARGET -S $flag $CFLAGS_FOR_TARGET" $srcdir/$subdir/$source $tmpdir/$execname.s]     {
 	unsupported "$test"
 	return
     }
@@ -606,7 +606,7 @@  proc test_ldfail { test flag source execname other mapfile whyfail } {
 	set script_arg "-Wl,$script $srcdir/$subdir/$mapfile"
     }
 
-    if {![ld_link $CC $tmpdir/$execname "$tmpdir/$execname.o $other_lib $script_arg"]}     {
+    if {![ld_link $CC_FOR_TARGET $tmpdir/$execname "$tmpdir/$execname.o $other_lib $script_arg"]}     {
 	pass "$test"
 	return
     }
@@ -618,11 +618,11 @@  proc test_asfail { test flag source execname whyfail } {
     global subdir
     global tmpdir
     global as
-    global CC
-    global CFLAGS
+    global CC_FOR_TARGET
+    global CFLAGS_FOR_TARGET
     global NOLTO_CFLAGS
 
-    if ![ld_compile "$CC -S $flag $CFLAGS $NOLTO_CFLAGS" $srcdir/$subdir/$source $tmpdir/$execname.s]     {
+    if ![ld_compile "$CC_FOR_TARGET -S $flag $CFLAGS_FOR_TARGET $NOLTO_CFLAGS" $srcdir/$subdir/$source $tmpdir/$execname.s]     {
 	unsupported "$test"
 	return
     }
@@ -688,8 +688,8 @@  proc build_exec { test source execname flags solibname verexp versymexp symexp }
     global as
     global as_options
     global objdump
-    global CC
-    global CFLAGS
+    global CC_FOR_TARGET
+    global CFLAGS_FOR_TARGET
     global NOSANITIZE_CFLAGS
     global NOLTO_CFLAGS
 
@@ -697,7 +697,7 @@  proc build_exec { test source execname flags solibname verexp versymexp symexp }
     set script --version-script
 
     # Disable LTO for these tests.
-    set cc_cmd "$CC -S $CFLAGS"
+    set cc_cmd "$CC_FOR_TARGET -S $CFLAGS_FOR_TARGET"
     if {[check_lto_available]} {
 	append cc_cmd " -fno-lto"
     }
@@ -721,7 +721,7 @@  proc build_exec { test source execname flags solibname verexp versymexp symexp }
 	set solibname_lib $tmpdir/$solibname
     }
 
-    if {![ld_link $CC $tmpdir/$execname "$flags $tmpdir/$execname.o $solibname_lib"]}     {
+    if {![ld_link $CC_FOR_TARGET $tmpdir/$execname "$flags $tmpdir/$execname.o $solibname_lib"]}     {
 	fail "$test"
 	return
     }
diff --git a/ld/testsuite/ld-elfvsb/elfvsb.exp b/ld/testsuite/ld-elfvsb/elfvsb.exp
index ee73cfe8f65..3c0826161cb 100644
--- a/ld/testsuite/ld-elfvsb/elfvsb.exp
+++ b/ld/testsuite/ld-elfvsb/elfvsb.exp
@@ -76,7 +76,7 @@  if [istarget arm*-*-linux*] {
 	set file [open $tmpdir/movw-detect.c w]
 	puts $file "void foo(void) { __asm (\"movw r0, #0\"); }"
 	close $file
-	if [run_host_cmd_yesno "$CC" "$CFLAGS -c $tmpdir/movw-detect.c -o $tmpdir/movw-detect.o"] {
+	if [run_host_cmd_yesno "$CC_FOR_TARGET" "$CFLAGS_FOR_TARGET -c $tmpdir/movw-detect.c -o $tmpdir/movw-detect.o"] {
 	    set shared_needs_pic "yes"
 	}
     }
@@ -87,8 +87,8 @@  set support_protected "no"
 if { [istarget *-*-linux*]
      || [istarget *-*-nacl*]
      || [istarget *-*-gnu*] } {
-    if [ld_compile "$CC -g $CFLAGS -DPROTECTED_CHECK" $srcdir/$subdir/main.c $tmpdir/main.o] {
-      if [ld_link $CC $tmpdir/main "$tmpdir/main.o"] {
+    if [ld_compile "$CC_FOR_TARGET -g $CFLAGS_FOR_TARGET -DPROTECTED_CHECK" $srcdir/$subdir/main.c $tmpdir/main.o] {
+      if [ld_link $CC_FOR_TARGET $tmpdir/main "$tmpdir/main.o"] {
 	catch "exec $tmpdir/main" support_protected
       }
     }
@@ -96,7 +96,7 @@  if { [istarget *-*-linux*]
 
 # The test procedure.
 proc visibility_test { visibility progname testname main sh1 sh2 dat args } {
-    global CC
+    global CC_FOR_TARGET
     global srcdir
     global subdir
     global exec_output
@@ -111,7 +111,7 @@  proc visibility_test { visibility progname testname main sh1 sh2 dat args } {
     if { [is_elf_format] && [check_shared_lib_support] } {
 	append shared " -Wl,-z,notext"
     }
-    if {![ld_link $CC $tmpdir/$progname.so "$shared $shldflags $tmpdir/$sh1 $tmpdir/$sh2"]} {
+    if {![ld_link $CC_FOR_TARGET $tmpdir/$progname.so "$shared $shldflags $tmpdir/$sh1 $tmpdir/$sh2"]} {
 	if { [ string match $visibility "hidden_undef" ]
 	     && [regexp "undefined reference to \`\.?visibility\'" $link_output]
 	     && [regexp "undefined reference to \`visibility_var\'" $link_output] } {
@@ -129,7 +129,7 @@  proc visibility_test { visibility progname testname main sh1 sh2 dat args } {
     # Link against the shared library.  Use -rpath so that the
     # dynamic linker can locate the shared library at runtime.
     set rpath $tmpdir
-    if ![ld_link $CC $tmpdir/$progname "-Wl,-rpath,$rpath $tmpdir/$main $tmpdir/$progname.so"] {
+    if ![ld_link $CC_FOR_TARGET $tmpdir/$progname "-Wl,-rpath,$rpath $tmpdir/$main $tmpdir/$progname.so"] {
 	if { [ string match $visibility "hidden" ]
 	     && [regexp "undefined reference to \`\.?visibility\'" $link_output]
 	     && [regexp "undefined reference to \`visibility_var\'" $link_output] } {
@@ -184,8 +184,8 @@  proc visibility_test { visibility progname testname main sh1 sh2 dat args } {
 }
 
 proc visibility_run {visibility} {
-    global CC
-    global CFLAGS
+    global CC_FOR_TARGET
+    global CFLAGS_FOR_TARGET
     global SHCFLAG
     global srcdir
     global subdir
@@ -237,7 +237,7 @@  proc visibility_run {visibility} {
     } else {
     # Compile the main program.  Make sure that PLT is used since PLT
     # is expected.
-    if ![ld_compile "$CC -g $PLT_CFLAGS $CFLAGS $SHCFLAG $VSBCFLAG $NOSANITIZE_CFLAGS $NOLTO_CFLAGS" $srcdir/$subdir/main.c $tmpdir/mainnp.o] {
+    if ![ld_compile "$CC_FOR_TARGET -g $PLT_CFLAGS $CFLAGS_FOR_TARGET $SHCFLAG $VSBCFLAG $NOSANITIZE_CFLAGS $NOLTO_CFLAGS" $srcdir/$subdir/main.c $tmpdir/mainnp.o] {
 	unsupported "visibility ($visibility) (non PIC)"
 	unsupported "visibility ($visibility)"
     } else {
@@ -247,8 +247,8 @@  proc visibility_run {visibility} {
 	# 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 -g $NOPIE_CFLAGS $PLT_CFLAGS $CFLAGS $SHCFLAG $VSBCFLAG $NOSANITIZE_CFLAGS $NOLTO_CFLAGS" $srcdir/$subdir/sh1.c $tmpdir/sh1np.o]
-	     || ![ld_compile "$CC -g $NOPIE_CFLAGS $PLT_CFLAGS $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 $CFLAGS_FOR_TARGET $SHCFLAG $VSBCFLAG $NOSANITIZE_CFLAGS $NOLTO_CFLAGS" $srcdir/$subdir/sh1.c $tmpdir/sh1np.o]
+	     || ![ld_compile "$CC_FOR_TARGET -g $NOPIE_CFLAGS $PLT_CFLAGS $CFLAGS_FOR_TARGET $SHCFLAG $VSBCFLAG $NOSANITIZE_CFLAGS $NOLTO_CFLAGS" $srcdir/$subdir/sh2.c $tmpdir/sh2np.o] } {
 	    unsupported "visibility ($visibility) (non PIC)"
 	} else {
 	    # SunOS non PIC shared libraries don't permit some cases of
@@ -336,8 +336,8 @@  proc visibility_run {visibility} {
 
 	# Now compile the code using -fpic.
 
-	if { ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG $NOSANITIZE_CFLAGS $NOLTO_CFLAGS -DSHARED $picflag" $srcdir/$subdir/sh1.c $tmpdir/sh1p.o]
-	    || ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG $NOSANITIZE_CFLAGS $NOLTO_CFLAGS -DSHARED $picflag" $srcdir/$subdir/sh2.c $tmpdir/sh2p.o] } {
+	if { ![ld_compile "$CC_FOR_TARGET -g $CFLAGS_FOR_TARGET $SHCFLAG $VSBCFLAG $NOSANITIZE_CFLAGS $NOLTO_CFLAGS -DSHARED $picflag" $srcdir/$subdir/sh1.c $tmpdir/sh1p.o]
+	    || ![ld_compile "$CC_FOR_TARGET -g $CFLAGS_FOR_TARGET $SHCFLAG $VSBCFLAG $NOSANITIZE_CFLAGS $NOLTO_CFLAGS -DSHARED $picflag" $srcdir/$subdir/sh2.c $tmpdir/sh2p.o] } {
 	    unsupported "visibility ($visibility)"
 	} else {
 	    if { [ string match $visibility "protected" ]
@@ -354,7 +354,7 @@  proc visibility_run {visibility} {
 	# Don't bother.
     } else {
     # Now do the same tests again, but this time compile main.c PIC.
-    if ![ld_compile "$CC -g $CFLAGS $SHCFLAG $VSBCFLAG $NOSANITIZE_CFLAGS $NOLTO_CFLAGS -DSHARED $picflag" $srcdir/$subdir/main.c $tmpdir/mainp.o] {
+    if ![ld_compile "$CC_FOR_TARGET -g $CFLAGS_FOR_TARGET $SHCFLAG $VSBCFLAG $NOSANITIZE_CFLAGS $NOLTO_CFLAGS -DSHARED $picflag" $srcdir/$subdir/main.c $tmpdir/mainp.o] {
 	unsupported "visibility ($visibility) (PIC main, non PIC so)"
 	unsupported "visibility ($visibility) (PIC main)"
     } else {
@@ -421,9 +421,9 @@  if { [istarget mips*-*-*] && ! [at_least_gcc_version 4 3] } then {
     # Unfortunately, the gcc argument is -fpic and the cc argument is
     # -KPIC.  We have to try both.
     set picflag "-fpic"
-    send_log "$CC $picflag\n"
-    verbose "$CC $picflag"
-    catch "exec $CC $picflag" exec_output
+    send_log "$CC_FOR_TARGET $picflag\n"
+    verbose "$CC_FOR_TARGET $picflag"
+    catch "exec $CC_FOR_TARGET $picflag" exec_output
     send_log "$exec_output\n"
     verbose "--" "$exec_output"
     if { [string match "*illegal option*" $exec_output] \
@@ -446,7 +446,7 @@  visibility_run protected_undef_def
 visibility_run protected_weak
 visibility_run normal
 
-if { ![ld_compile "$CC -g $CFLAGS $NOSANITIZE_CFLAGS $NOLTO_CFLAGS" $srcdir/$subdir/common.c tmpdir/common.o] } {
+if { ![ld_compile "$CC_FOR_TARGET -g $CFLAGS_FOR_TARGET $NOSANITIZE_CFLAGS $NOLTO_CFLAGS" $srcdir/$subdir/common.c tmpdir/common.o] } {
     unsupported "common hidden symbol"
 } else {
     if ![ld_link $ld tmpdir/common "tmpdir/common.o"] {
@@ -456,10 +456,10 @@  if { ![ld_compile "$CC -g $CFLAGS $NOSANITIZE_CFLAGS $NOLTO_CFLAGS" $srcdir/$sub
     }
 }
 
-if { ![ld_compile "$CC -g $CFLAGS $NOSANITIZE_CFLAGS $NOLTO_CFLAGS" $srcdir/$subdir/test.c tmpdir/test.o] } {
+if { ![ld_compile "$CC_FOR_TARGET -g $CFLAGS_FOR_TARGET $NOSANITIZE_CFLAGS $NOLTO_CFLAGS" $srcdir/$subdir/test.c tmpdir/test.o] } {
     unsupported "weak hidden symbol"
 } else {
-   if { ![ld_compile "$CC -g $CFLAGS $NOSANITIZE_CFLAGS $NOLTO_CFLAGS -DSHARED $picflag" $srcdir/$subdir/sh3.c tmpdir/sh3.o] } {
+   if { ![ld_compile "$CC_FOR_TARGET -g $CFLAGS_FOR_TARGET $NOSANITIZE_CFLAGS $NOLTO_CFLAGS -DSHARED $picflag" $srcdir/$subdir/sh3.c tmpdir/sh3.o] } {
 	unsupported "weak hidden symbol"
     } else {
 	if ![ld_link $ld tmpdir/sh3.so "-shared tmpdir/sh3.o"] {
diff --git a/ld/testsuite/ld-elfweak/elfweak.exp b/ld/testsuite/ld-elfweak/elfweak.exp
index 56949d7431d..762b214ab68 100644
--- a/ld/testsuite/ld-elfweak/elfweak.exp
+++ b/ld/testsuite/ld-elfweak/elfweak.exp
@@ -261,7 +261,7 @@  proc objdump_dynsymstuff { objdump object expectfile } {
 }
 
 proc build_lib {test libname objs dynsymexp} {
-    global CC
+    global CC_FOR_TARGET
     global objdump
     global tmpdir
     global shared
@@ -275,7 +275,7 @@  proc build_lib {test libname objs dynsymexp} {
       set files "$files $tmpdir/$obj"
     }
 
-    if {![ld_link "$CC $NOSANITIZE_CFLAGS $NOLTO_CFLAGS" $tmpdir/$libname.so "$shared $files"]} {
+    if {![ld_link "$CC_FOR_TARGET $NOSANITIZE_CFLAGS $NOLTO_CFLAGS" $tmpdir/$libname.so "$shared $files"]} {
 	fail $test
 	return
     }
@@ -289,7 +289,7 @@  proc build_lib {test libname objs dynsymexp} {
 }
 
 proc build_exec { test execname objs flags dat dynsymexp symexp} {
-    global CC
+    global CC_FOR_TARGET
     global objdump
     global tmpdir
     global srcdir
@@ -303,7 +303,7 @@  proc build_exec { test execname objs flags dat dynsymexp symexp} {
       set files "$files $tmpdir/$obj"
     }
 
-    if {![ld_link $CC $tmpdir/$execname "$flags $NOSANITIZE_CFLAGS $NOLTO_CFLAGS $files"]} {
+    if {![ld_link $CC_FOR_TARGET $tmpdir/$execname "$flags $NOSANITIZE_CFLAGS $NOLTO_CFLAGS $files"]} {
 	fail "$test"
 	return
     }
@@ -353,14 +353,14 @@  proc build_exec { test execname objs flags dat dynsymexp symexp} {
 }
 
 # Disable LTO for these tests.
-set cc_cmd "$CC"
+set cc_cmd "$CC_FOR_TARGET"
 if {[check_lto_available]} {
    append cc_cmd " -fno-lto"
 }
 
 # Disable all sanitizers and LTO.
-set saved_CFLAGS "$CFLAGS"
-set CFLAGS "$CFLAGS $NOSANITIZE_CFLAGS $NOLTO_CFLAGS"
+set saved_CFLAGS "$CFLAGS_FOR_TARGET"
+append CFLAGS_FOR_TARGET " $NOSANITIZE_CFLAGS $NOLTO_CFLAGS"
 
 # Old version of GCC for MIPS default to enabling -fpic
 # and get confused if it is used on the command line.
@@ -370,9 +370,9 @@  if { [istarget mips*-*-*] && ! [at_least_gcc_version 4 3] } then {
     # Unfortunately, the gcc argument is -fpic and the cc argument is
     # -KPIC.  We have to try both.
     set picflag "-fpic"
-    send_log "$CC $picflag\n"
-    verbose "$CC $picflag"
-    catch "exec $CC $picflag" exec_output
+    send_log "$CC_FOR_TARGET $picflag\n"
+    verbose "$CC_FOR_TARGET $picflag"
+    catch "exec $CC_FOR_TARGET $picflag" exec_output
     send_log "$exec_output\n"
     verbose "--" "$exec_output"
     if { [string match "*illegal option*" $exec_output]
@@ -384,16 +384,16 @@  if { [istarget mips*-*-*] && ! [at_least_gcc_version 4 3] } then {
 }
 verbose "Using $picflag to compile PIC code"
 
-if {![ld_compile "$cc_cmd $CFLAGS $picflag" $srcdir/$subdir/foo.c $tmpdir/foo.o]
-    || ![ld_compile "$cc_cmd $CFLAGS $picflag" $srcdir/$subdir/bar.c $tmpdir/bar.o]
-    || ![ld_compile "$cc_cmd $CFLAGS" $srcdir/$subdir/main.c $tmpdir/main.o]
+if {![ld_compile "$cc_cmd $CFLAGS_FOR_TARGET $picflag" $srcdir/$subdir/foo.c $tmpdir/foo.o]
+    || ![ld_compile "$cc_cmd $CFLAGS_FOR_TARGET $picflag" $srcdir/$subdir/bar.c $tmpdir/bar.o]
+    || ![ld_compile "$cc_cmd $CFLAGS_FOR_TARGET" $srcdir/$subdir/main.c $tmpdir/main.o]
     || ![ld_link $cc_cmd $tmpdir/libbar.so "$shared $tmpdir/bar.o"]
-    || ![ld_compile "$cc_cmd $CFLAGS $picflag" $srcdir/$subdir/foo1a.c $tmpdir/foo1a.o]
-    || ![ld_compile "$cc_cmd $CFLAGS $picflag" $srcdir/$subdir/foo1b.c $tmpdir/foo1b.o]
-    || ![ld_compile "$cc_cmd $CFLAGS $picflag" $srcdir/$subdir/bar1a.c $tmpdir/bar1a.o]
-    || ![ld_compile "$cc_cmd $CFLAGS $picflag" $srcdir/$subdir/bar1b.c $tmpdir/bar1b.o]
-    || ![ld_compile "$cc_cmd $CFLAGS $picflag" $srcdir/$subdir/bar1c.c $tmpdir/bar1c.o]
-    || ![ld_compile "$cc_cmd $CFLAGS $picflag" $srcdir/$subdir/main1.c $tmpdir/main1.o]} then {
+    || ![ld_compile "$cc_cmd $CFLAGS_FOR_TARGET $picflag" $srcdir/$subdir/foo1a.c $tmpdir/foo1a.o]
+    || ![ld_compile "$cc_cmd $CFLAGS_FOR_TARGET $picflag" $srcdir/$subdir/foo1b.c $tmpdir/foo1b.o]
+    || ![ld_compile "$cc_cmd $CFLAGS_FOR_TARGET $picflag" $srcdir/$subdir/bar1a.c $tmpdir/bar1a.o]
+    || ![ld_compile "$cc_cmd $CFLAGS_FOR_TARGET $picflag" $srcdir/$subdir/bar1b.c $tmpdir/bar1b.o]
+    || ![ld_compile "$cc_cmd $CFLAGS_FOR_TARGET $picflag" $srcdir/$subdir/bar1c.c $tmpdir/bar1c.o]
+    || ![ld_compile "$cc_cmd $CFLAGS_FOR_TARGET $picflag" $srcdir/$subdir/main1.c $tmpdir/main1.o]} then {
     unsupported "ELF weak"
 } elseif {![ld_link $cc_cmd $tmpdir/libfoo1a.so "$shared $tmpdir/foo1a.o"]
 	  || ![ld_link $cc_cmd $tmpdir/libfoo1b.so "$shared $tmpdir/foo1b.o"]
@@ -424,10 +424,10 @@  if {![ld_compile "$cc_cmd $CFLAGS $picflag" $srcdir/$subdir/foo.c $tmpdir/foo.o]
     build_exec "ELF weak data last DSO common" foo "libfoo1b.so main1.o libbar1a.so" "-Wl,--no-as-needed,-rpath,.,-rpath-link,." weakdata weakdata.dsym ""
 }
 
-if {![ld_compile "$cc_cmd $CFLAGS $picflag" $srcdir/$subdir/size_foo.c $tmpdir/size_foo.o]
-    || ![ld_compile "$cc_cmd $CFLAGS $picflag" $srcdir/$subdir/size_bar.c $tmpdir/size_bar_s.o]
-    || ![ld_compile "$cc_cmd $CFLAGS $picflag -DSIZE_BIG" $srcdir/$subdir/size_bar.c $tmpdir/size_bar.o]
-    || ![ld_compile "$cc_cmd $CFLAGS" $srcdir/$subdir/size_main.c $tmpdir/size_main.o]} then {
+if {![ld_compile "$cc_cmd $CFLAGS_FOR_TARGET $picflag" $srcdir/$subdir/size_foo.c $tmpdir/size_foo.o]
+    || ![ld_compile "$cc_cmd $CFLAGS_FOR_TARGET $picflag" $srcdir/$subdir/size_bar.c $tmpdir/size_bar_s.o]
+    || ![ld_compile "$cc_cmd $CFLAGS_FOR_TARGET $picflag -DSIZE_BIG" $srcdir/$subdir/size_bar.c $tmpdir/size_bar.o]
+    || ![ld_compile "$cc_cmd $CFLAGS_FOR_TARGET" $srcdir/$subdir/size_main.c $tmpdir/size_main.o]} then {
     unsupported "ELF weak (size)"
 } else {
     build_lib "ELF DSO small bar (size)" libsize_bar_s "size_bar_s.o" ""
@@ -439,14 +439,14 @@  if {![ld_compile "$cc_cmd $CFLAGS $picflag" $srcdir/$subdir/size_foo.c $tmpdir/s
 verbose "size2"
 run_dump_test $srcdir/$subdir/size2
 
-if {![ld_compile "$cc_cmd $CFLAGS $picflag" $srcdir/$subdir/alias.c $tmpdir/alias.o]
+if {![ld_compile "$cc_cmd $CFLAGS_FOR_TARGET $picflag" $srcdir/$subdir/alias.c $tmpdir/alias.o]
     || ![ld_link $cc_cmd $tmpdir/alias.so "$shared $tmpdir/alias.o"]
-    || ![ld_compile "$cc_cmd $CFLAGS" $srcdir/$subdir/aliasmain.c $tmpdir/aliasmain.o]
-    || ![ld_compile "$cc_cmd $CFLAGS" $srcdir/$subdir/weakref1.c $tmpdir/weakref1.o]
-    || ![ld_compile "$cc_cmd $CFLAGS" $srcdir/$subdir/weakref2.c $tmpdir/weakref2.o]} then {
+    || ![ld_compile "$cc_cmd $CFLAGS_FOR_TARGET" $srcdir/$subdir/aliasmain.c $tmpdir/aliasmain.o]
+    || ![ld_compile "$cc_cmd $CFLAGS_FOR_TARGET" $srcdir/$subdir/weakref1.c $tmpdir/weakref1.o]
+    || ![ld_compile "$cc_cmd $CFLAGS_FOR_TARGET" $srcdir/$subdir/weakref2.c $tmpdir/weakref2.o]} then {
     unsupported "ELF weak (alias)"
 } else {
     build_exec "ELF weak (alias)" alias "aliasmain.o weakref1.o weakref2.o alias.so" "-Wl,-rpath=.,--no-as-needed" alias "" ""
 }
 
-set CFLAGS "$saved_CFLAGS"
+set CFLAGS_FOR_TARGET "$saved_CFLAGS"
diff --git a/ld/testsuite/ld-gc/gc.exp b/ld/testsuite/ld-gc/gc.exp
index 298f2d48406..60491fe0f80 100644
--- a/ld/testsuite/ld-gc/gc.exp
+++ b/ld/testsuite/ld-gc/gc.exp
@@ -37,7 +37,7 @@  if { [istarget m681*-*-*] || [istarget m68hc1*-*-*] } {
 }
 
 if { [check_compiler_available] } {
-    ld_compile "$CC -c $CFLAGS $cflags" $srcdir/$subdir/gc.c $objfile
+    ld_compile "$CC_FOR_TARGET -c $CFLAGS_FOR_TARGET $cflags" $srcdir/$subdir/gc.c $objfile
 }
 
 proc test_gc { testname filename linker ldflags} {
@@ -144,9 +144,9 @@  if { [check_compiler_available] } {
     if { [istarget "*-*-linux*"]
 	 || [istarget "*-*-nacl*"]
 	 || [istarget "*-*-gnu*"] } {
-	ld_compile "$CC -fPIC $CFLAGS $cflags" $srcdir/$subdir/pr11218-1.c tmpdir/pr11218-1.o
+	ld_compile "$CC_FOR_TARGET -fPIC $CFLAGS_FOR_TARGET $cflags" $srcdir/$subdir/pr11218-1.c tmpdir/pr11218-1.o
 	ld_link $ld tmpdir/pr11218-1.so "-shared tmpdir/pr11218-1.o"
- 	ld_compile "$CC -c $CFLAGS $cflags" $srcdir/$subdir/pr11218-2.c tmpdir/pr11218-2.o
+	ld_compile "$CC_FOR_TARGET -c $CFLAGS_FOR_TARGET $cflags" $srcdir/$subdir/pr11218-2.c tmpdir/pr11218-2.o
 	run_dump_test "pr11218"
     }
 }
@@ -157,18 +157,18 @@  if [is_pecoff_format] {
 }
 
 if { [check_compiler_available] } {
-    ld_compile "$CC $CFLAGS $cflags" $srcdir/$subdir/pr13683.c tmpdir/pr13683.o
+    ld_compile "$CC_FOR_TARGET $CFLAGS_FOR_TARGET $cflags" $srcdir/$subdir/pr13683.c tmpdir/pr13683.o
     run_dump_test "pr13683" $symdefs
 }
 
 if { [check_compiler_available] } {
-    ld_compile "$CC $CFLAGS $cflags $NOPIE_CFLAGS" $srcdir/$subdir/pr14265.c tmpdir/pr14265.o
+    ld_compile "$CC_FOR_TARGET $CFLAGS_FOR_TARGET $cflags $NOPIE_CFLAGS" $srcdir/$subdir/pr14265.c tmpdir/pr14265.o
     run_dump_test "pr14265" $symdefs
 }
 
 if { [check_compiler_available] } {
-    ld_compile "$CC $CFLAGS $cflags -O0" $srcdir/$subdir/pr19161-1.c tmpdir/pr19161-1.o
-    ld_compile "$CC $CFLAGS $cflags -O0" $srcdir/$subdir/pr19161-2.c tmpdir/pr19161-2.o
+    ld_compile "$CC_FOR_TARGET $CFLAGS_FOR_TARGET $cflags -O0" $srcdir/$subdir/pr19161-1.c tmpdir/pr19161-1.o
+    ld_compile "$CC_FOR_TARGET $CFLAGS_FOR_TARGET $cflags -O0" $srcdir/$subdir/pr19161-2.c tmpdir/pr19161-2.o
     if [istarget hppa*-*-linux*] {
 	set symdefs {{ld {--defsym '\$\$dyncall=0'}}}
     }
diff --git a/ld/testsuite/ld-ifunc/ifunc.exp b/ld/testsuite/ld-ifunc/ifunc.exp
index 45b47d7d6a8..e559884fd0e 100644
--- a/ld/testsuite/ld-ifunc/ifunc.exp
+++ b/ld/testsuite/ld-ifunc/ifunc.exp
@@ -182,7 +182,7 @@  proc contains_ifunc_reloc { binary_file } {
 set fails 0
 
 # Disable LTO for these tests.
-set cc_cmd "$CC"
+set cc_cmd "$CC_FOR_TARGET"
 if {[check_lto_available]} {
    append cc_cmd " -fno-lto"
 }
@@ -238,16 +238,16 @@  if { $fails != 0 } {
     return
 }
 
-if ![ld_link $CC "tmpdir/dynamic_prog" "-Wl,--no-as-needed,-rpath=./tmpdir,-Bdynamic -Ltmpdir tmpdir/shared_prog.o -lshared_ifunc"] {
+if ![ld_link $CC_FOR_TARGET "tmpdir/dynamic_prog" "-Wl,--no-as-needed,-rpath=./tmpdir,-Bdynamic -Ltmpdir tmpdir/shared_prog.o -lshared_ifunc"] {
     fail "Could not link a dynamic executable"
     set fails [expr $fails + 1]
 }
-if ![ld_link $CC "tmpdir/local_prog" "$NOPIE_LDFLAGS -Wl,--no-as-needed,-rpath=./tmpdir -Ltmpdir tmpdir/static_prog.o -lifunc"] {
+if ![ld_link $CC_FOR_TARGET "tmpdir/local_prog" "$NOPIE_LDFLAGS -Wl,--no-as-needed,-rpath=./tmpdir -Ltmpdir tmpdir/static_prog.o -lifunc"] {
     fail "Could not link a dynamic executable using local ifunc"
     set fails [expr $fails + 1]
 }
 if ![string match "" $STATIC_LDFLAGS] {
-    if ![ld_link $CC "tmpdir/static_prog" "-static -Ltmpdir tmpdir/static_prog.o -lifunc"] {
+    if ![ld_link $CC_FOR_TARGET "tmpdir/static_prog" "-static -Ltmpdir tmpdir/static_prog.o -lifunc"] {
 	fail "Could not link a static executable"
 	set fails [expr $fails + 1]
     }
@@ -256,7 +256,7 @@  if ![ld_link $ld "tmpdir/static_nonifunc_prog" "-static tmpdir/empty.o"] {
     fail "Could not link a non-ifunc using static executable"
     set fails [expr $fails + 1]
 }
-if ![ld_link $CC "tmpdir/test-1" "-Wl,--no-as-needed,-rpath=./tmpdir tmpdir/test-1.o tmpdir/libshared_ifunc.so"] {
+if ![ld_link $CC_FOR_TARGET "tmpdir/test-1" "-Wl,--no-as-needed,-rpath=./tmpdir tmpdir/test-1.o tmpdir/libshared_ifunc.so"] {
     fail "Could not link test-1"
     set fails [expr $fails + 1]
 }
diff --git a/ld/testsuite/ld-mn10300/mn10300.exp b/ld/testsuite/ld-mn10300/mn10300.exp
index 7a44954a482..97f3fc7de9e 100644
--- a/ld/testsuite/ld-mn10300/mn10300.exp
+++ b/ld/testsuite/ld-mn10300/mn10300.exp
@@ -126,7 +126,7 @@  set am33_tests {
 run_ld_link_tests $am33_tests
 
 proc i126256-test { } {
-    global CC
+    global CC_FOR_TARGET
     global ld
     global srcdir
     global subdir
@@ -138,12 +138,12 @@  proc i126256-test { } {
 	return
     }
 
-    if { ![ld_compile "$CC -mrelax -fPIC" $srcdir/$subdir/i126256-1.c $tmpdir/i126256-1.o] } {
+    if { ![ld_compile "$CC_FOR_TARGET -mrelax -fPIC" $srcdir/$subdir/i126256-1.c $tmpdir/i126256-1.o] } {
 	unsupported $testname
 	return
     }
 
-    if { ![ld_compile "$CC -mrelax -fPIC" $srcdir/$subdir/i126256-2.c $tmpdir/i126256-2.o] } {
+    if { ![ld_compile "$CC_FOR_TARGET -mrelax -fPIC" $srcdir/$subdir/i126256-2.c $tmpdir/i126256-2.o] } {
 	unsupported $testname
 	return
     }
diff --git a/ld/testsuite/ld-pe/pe-compile.exp b/ld/testsuite/ld-pe/pe-compile.exp
index dad2bfdce79..5282cfdb0a1 100644
--- a/ld/testsuite/ld-pe/pe-compile.exp
+++ b/ld/testsuite/ld-pe/pe-compile.exp
@@ -34,13 +34,13 @@  if { ![check_compiler_available] } {
 }
 
 proc build_basefile1_o {} {
-    global CC
-    global CFLAGS
+    global CC_FOR_TARGET
+    global CFLAGS_FOR_TARGET
     global srcdir
     global subdir
 
     # Compile the object file.
-    if ![ld_compile "$CC $CFLAGS" $srcdir/$subdir/basefile1.s tmpdir/basefile1.o] {
+    if ![ld_compile "$CC_FOR_TARGET $CFLAGS_FOR_TARGET" $srcdir/$subdir/basefile1.s tmpdir/basefile1.o] {
 	fail "compiling basefile1 object"
     }
 }
@@ -75,24 +75,24 @@  proc run_basefile_test { testname } {
 }
 
 proc build_vers_script_dll_o {} {
-    global CC
-    global CFLAGS
+    global CC_FOR_TARGET
+    global CFLAGS_FOR_TARGET
     global srcdir
     global subdir
 
     # Compile the object file.
-    if ![ld_compile "$CC $CFLAGS -shared" $srcdir/$subdir/vers-script-dll.c tmpdir/vers-script-dll.o] {
+    if ![ld_compile "$CC_FOR_TARGET $CFLAGS_FOR_TARGET -shared" $srcdir/$subdir/vers-script-dll.c tmpdir/vers-script-dll.o] {
 	fail "compiling shared lib object"
     }
 }
 
 proc run_ver_script_test { testname } {
-    global CC
+    global CC_FOR_TARGET
     global srcdir
     global subdir
     global verbose
 
-    if ![ld_link "$CC -shared \
+    if ![ld_link "$CC_FOR_TARGET -shared \
 	    -Wl,--version-script,$srcdir/$subdir/$testname.ver \
 	    -Wl,--output-def,tmpdir/$testname.def" tmpdir/$testname.dll \
 	    "tmpdir/vers-script-dll.o"] {
diff --git a/ld/testsuite/ld-pe/pe-run.exp b/ld/testsuite/ld-pe/pe-run.exp
index 1efbf892601..a26644cc367 100644
--- a/ld/testsuite/ld-pe/pe-run.exp
+++ b/ld/testsuite/ld-pe/pe-run.exp
@@ -62,27 +62,27 @@  if { ![check_compiler_available] } {
 set tmpdir tmpdir
 
 proc test_direct_link_dll {} {
-    global CC
-    global CFLAGS
+    global CC_FOR_TARGET
+    global CFLAGS_FOR_TARGET
     global srcdir
     global subdir
     global tmpdir
 
     # Compile the dll.
-    if ![ld_compile "$CC $CFLAGS" $srcdir/$subdir/direct_dll.c $tmpdir/direct_dll.o ] {
+    if ![ld_compile "$CC_FOR_TARGET $CFLAGS_FOR_TARGET" $srcdir/$subdir/direct_dll.c $tmpdir/direct_dll.o ] {
 	fail "compiling shared lib"
-    } elseif ![ld_link "$CC -shared" $tmpdir/direct_dll.dll "$tmpdir/direct_dll.o" ] {
+    } elseif ![ld_link "$CC_FOR_TARGET -shared" $tmpdir/direct_dll.dll "$tmpdir/direct_dll.o" ] {
 	fail "linking shared lib (.dll)"
-    } elseif ![ld_link "$CC -shared" $tmpdir/direct_dll.sl "$tmpdir/direct_dll.o" ] {
+    } elseif ![ld_link "$CC_FOR_TARGET -shared" $tmpdir/direct_dll.sl "$tmpdir/direct_dll.o" ] {
 	fail "linking shared lib (.sl)"
     } else {
 	# Compile and link the client program.
-	if ![ld_compile "$CC $CFLAGS" $srcdir/$subdir/direct_client.c $tmpdir/direct_client.o ] {
+	if ![ld_compile "$CC_FOR_TARGET $CFLAGS_FOR_TARGET" $srcdir/$subdir/direct_client.c $tmpdir/direct_client.o ] {
 	    fail "compiling client"
 	} else {
 	    # Check linking directly to direct_dll.dll.
 	    set msg "linking client (.dll)"
-	    if [ld_link "$CC -Wl,--enable-auto-import" $tmpdir/direct_client_dll.exe \
+	    if [ld_link "$CC_FOR_TARGET -Wl,--enable-auto-import" $tmpdir/direct_client_dll.exe \
 	      "$tmpdir/direct_client.o $tmpdir/direct_dll.dll" ] {
 		pass $msg
 	    } else {
@@ -91,7 +91,7 @@  proc test_direct_link_dll {} {
 
 	    # Check linking directly to direct_dll.sl.
 	    set msg "linking client (.sl)"
-	    if [ld_link "$CC -Wl,--enable-auto-import" $tmpdir/direct_client_sl.exe \
+	    if [ld_link "$CC_FOR_TARGET -Wl,--enable-auto-import" $tmpdir/direct_client_sl.exe \
 	      "$tmpdir/direct_client.o $tmpdir/direct_dll.sl" ] {
 		pass $msg
 	    } else {
@@ -102,7 +102,7 @@  proc test_direct_link_dll {} {
 	    # Create symbolic link.
 	    catch "exec ln -fs direct_dll.dll $tmpdir/libdirect_dll.dll.a" ln_catch
 	    set msg "linking client (symlink -> .dll)"
-	    if [ld_link "$CC -Wl,--enable-auto-import" $tmpdir/direct_client_symlink_dll.exe \
+	    if [ld_link "$CC_FOR_TARGET -Wl,--enable-auto-import" $tmpdir/direct_client_symlink_dll.exe \
 	      "$tmpdir/direct_client.o $tmpdir/libdirect_dll.dll.a" ] {
 	        pass $msg
 	    } else {
@@ -113,7 +113,7 @@  proc test_direct_link_dll {} {
 	    # Create symbolic link.
 	    catch "exec ln -fs direct_dll.sl $tmpdir/libdirect_sl.dll.a" ln_catch
 	    set msg "linking client (symlink -> .sl)"
-	    if [ld_link "$CC -Wl,--enable-auto-import" $tmpdir/direct_client_symlink_sl.exe \
+	    if [ld_link "$CC_FOR_TARGET -Wl,--enable-auto-import" $tmpdir/direct_client_symlink_sl.exe \
 	      "$tmpdir/direct_client.o $tmpdir/libdirect_sl.dll.a" ] {
 		pass $msg
 	    } else {
diff --git a/ld/testsuite/ld-pe/pe-run2.exp b/ld/testsuite/ld-pe/pe-run2.exp
index 1bf2e737828..b4b4e785bb8 100644
--- a/ld/testsuite/ld-pe/pe-run2.exp
+++ b/ld/testsuite/ld-pe/pe-run2.exp
@@ -64,27 +64,27 @@  if { ![check_compiler_available] } {
 set tmpdir tmpdir
 
 proc test_direct2_link_dll {} {
-    global CC
-    global CFLAGS
+    global CC_FOR_TARGET
+    global CFLAGS_FOR_TARGET
     global srcdir
     global subdir
     global tmpdir
 
     # Compile the dll.
-    if ![ld_compile "$CC $CFLAGS" $srcdir/$subdir/direct2_dll.c $tmpdir/direct2_dll.o ] {
+    if ![ld_compile "$CC_FOR_TARGET $CFLAGS_FOR_TARGET" $srcdir/$subdir/direct2_dll.c $tmpdir/direct2_dll.o ] {
 	fail "compiling shared lib fastcall/stdcall"
-    } elseif ![ld_link "$CC -shared -Wl,--enable-stdcall-fixup -Wl,--kill-at " $tmpdir/direct2_dll.dll "$tmpdir/direct2_dll.o" ] {
+    } elseif ![ld_link "$CC_FOR_TARGET -shared -Wl,--enable-stdcall-fixup -Wl,--kill-at " $tmpdir/direct2_dll.dll "$tmpdir/direct2_dll.o" ] {
 	fail "linking shared lib (.dll) fastcall/stdcall"
-    } elseif ![ld_link "$CC -shared -Wl,--enable-stdcall-fixup -Wl,--kill-at " $tmpdir/direct2_dll.sl "$tmpdir/direct2_dll.o" ] {
+    } elseif ![ld_link "$CC_FOR_TARGET -shared -Wl,--enable-stdcall-fixup -Wl,--kill-at " $tmpdir/direct2_dll.sl "$tmpdir/direct2_dll.o" ] {
 	fail "linking shared lib (.sl) fastcall/stdcall"
     } else {
 	# Compile and link the client program.
-	if ![ld_compile "$CC $CFLAGS" $srcdir/$subdir/direct2_client.c $tmpdir/direct2_client.o ] {
+	if ![ld_compile "$CC_FOR_TARGET $CFLAGS_FOR_TARGET" $srcdir/$subdir/direct2_client.c $tmpdir/direct2_client.o ] {
 	    fail "compiling client fastcall/stdcall"
 	} else {
 	    # Check linking directly to direct2_dll.dll.
 	    set msg "linking client (.dll) fastcall/stdcall"
-	    if [ld_link "$CC $CFLAGS -Wl,--enable-stdcall-fixup -Wl,--enable-auto-import" $tmpdir/direct2_client_dll.exe \
+	    if [ld_link "$CC_FOR_TARGET $CFLAGS_FOR_TARGET -Wl,--enable-stdcall-fixup -Wl,--enable-auto-import" $tmpdir/direct2_client_dll.exe \
 	      "$tmpdir/direct2_client.o $tmpdir/direct2_dll.dll" ] {
 		pass $msg
 	    } else {
@@ -93,7 +93,7 @@  proc test_direct2_link_dll {} {
 
 	    # Check linking directly to direct2_dll.sl.
 	    set msg "linking client (.sl) fastcall/stdcall"
-	    if [ld_link "$CC $CFLAGS -Wl,--enable-stdcall-fixup -Wl,--enable-auto-import" $tmpdir/direct2_client_sl.exe \
+	    if [ld_link "$CC_FOR_TARGET $CFLAGS_FOR_TARGET -Wl,--enable-stdcall-fixup -Wl,--enable-auto-import" $tmpdir/direct2_client_sl.exe \
 	      "$tmpdir/direct2_client.o $tmpdir/direct2_dll.sl" ] {
 		pass $msg
 	    } else {
@@ -104,7 +104,7 @@  proc test_direct2_link_dll {} {
 	    # Create symbolic link.
 	    catch "exec ln -fs direct2_dll.dll $tmpdir/libdirect2_dll.dll.a" ln_catch
 	    set msg "linking client (symlink -> .dll) fastcall/stdcall"
-	    if [ld_link "$CC $CFLAGS -Wl,--enable-stdcall-fixup -Wl,--enable-auto-import" $tmpdir/direct2_client_symlink_dll.exe \
+	    if [ld_link "$CC_FOR_TARGET $CFLAGS_FOR_TARGET -Wl,--enable-stdcall-fixup -Wl,--enable-auto-import" $tmpdir/direct2_client_symlink_dll.exe \
 	      "$tmpdir/direct2_client.o $tmpdir/libdirect2_dll.dll.a" ] {
 	        pass $msg
 	    } else {
@@ -115,7 +115,7 @@  proc test_direct2_link_dll {} {
 	    # Create symbolic link.
 	    catch "exec ln -fs direct2_dll.sl $tmpdir/libdirect2_sl.dll.a" ln_catch
 	    set msg "linking client (symlink -> .sl) fastcall/stdcall"
-	    if [ld_link "$CC $CFLAGS -Wl,--enable-stdcall-fixup -Wl,--enable-auto-import" $tmpdir/direct2_client_symlink_sl.exe \
+	    if [ld_link "$CC_FOR_TARGET $CFLAGS_FOR_TARGET -Wl,--enable-stdcall-fixup -Wl,--enable-auto-import" $tmpdir/direct2_client_symlink_sl.exe \
 	      "$tmpdir/direct2_client.o $tmpdir/libdirect2_sl.dll.a" ] {
 		pass $msg
 	    } else {
diff --git a/ld/testsuite/ld-pie/pie.exp b/ld/testsuite/ld-pie/pie.exp
index eaf761ac56e..282742cca5b 100644
--- a/ld/testsuite/ld-pie/pie.exp
+++ b/ld/testsuite/ld-pie/pie.exp
@@ -31,8 +31,8 @@  if { ![check_compiler_available] } {
 }
 
 # Check if -pie is supported or not.
-send_log "$CC -fpie -pie $srcdir/$subdir/pie.c -o tmpdir/pie"
-catch "exec $CC -fpie -pie $srcdir/$subdir/pie.c -o tmpdir/pie" exec_output
+send_log "$CC_FOR_TARGET -fpie -pie $srcdir/$subdir/pie.c -o tmpdir/pie"
+catch "exec $CC_FOR_TARGET -fpie -pie $srcdir/$subdir/pie.c -o tmpdir/pie" exec_output
 send_log "$exec_output\n"
 if { ![string match "" $exec_output] } {
     return
diff --git a/ld/testsuite/ld-plugin/lto.exp b/ld/testsuite/ld-plugin/lto.exp
index c343132c460..51e01046a12 100644
--- a/ld/testsuite/ld-plugin/lto.exp
+++ b/ld/testsuite/ld-plugin/lto.exp
@@ -19,7 +19,7 @@ 
 # MA 02110-1301, USA.
 
 # Check to see if the C and C++ compilers work
-if { ![check_compiler_available] || [which $CXX] == 0 } {
+if { ![check_compiler_available] || [which $CXX_FOR_TARGET] == 0 } {
     return
 }
 
@@ -29,16 +29,18 @@  if { ![check_plugin_api_available]
     return
 }
 
-set saved_CFLAGS "$CFLAGS"
-set saved_CXXFLAGS "$CXXFLAGS"
-regsub -all "(\\-Wp,)?-D_FORTIFY_SOURCE=\[0-9\]+" $CFLAGS "" CFLAGS
-regsub -all "(\\-Wp,)?-D_FORTIFY_SOURCE=\[0-9\]+" $CXXFLAGS "" CXXFLAGS
+set saved_CFLAGS "$CFLAGS_FOR_TARGET"
+set saved_CXXFLAGS "$CXXFLAGS_FOR_TARGET"
+regsub -all "(\\-Wp,)?-D_FORTIFY_SOURCE=\[0-9\]+" $CFLAGS_FOR_TARGET "" CFLAGS_FOR_TARGET
+regsub -all "(\\-Wp,)?-D_FORTIFY_SOURCE=\[0-9\]+" $CXXFLAGS_FOR_TARGET "" CXXFLAGS_FOR_TARGET
 
 proc restore_notify { } {
   global saved_CFLAGS
   global saved_CXXFLAGS
-  set CFLAGS "$saved_CFLAGS"
-  set CXXFLAGS "$saved_CXXFLAGS"
+  global CFLAGS_FOR_TARGET
+  global CXXFLAGS_FOR_TARGET
+  set CFLAGS_FOR_TARGET "$saved_CFLAGS"
+  set CXXFLAGS_FOR_TARGET "$saved_CXXFLAGS"
 }
 
 set lto_fat ""
@@ -674,21 +676,21 @@  run_cc_link_tests $lto_compile_elf_tests
 if { [is_elf_format] && [check_lto_shared_available] } {
     run_cc_link_tests $lto_link_elf_tests
     set testname "PR ld/15146 (2)"
-    set exec_output [run_host_cmd "$CC" "-O2 -flto -fuse-linker-plugin -Wl,-rpath-link,. -Wl,--no-copy-dt-needed-entries -Wl,--no-as-needed tmpdir/pr15146d.o tmpdir/pr15146c.so"]
+    set exec_output [run_host_cmd "$CC_FOR_TARGET" "-O2 -flto -fuse-linker-plugin -Wl,-rpath-link,. -Wl,--no-copy-dt-needed-entries -Wl,--no-as-needed tmpdir/pr15146d.o tmpdir/pr15146c.so"]
     if { [ regexp "undefined reference to symbol '\\.?xxx'" $exec_output ] } {
 	pass $testname
     } {
 	fail $testname
     }
     set testname "PR ld/16746 (3)"
-    set exec_output [run_host_cmd "$CC" "-O2 -flto -fuse-linker-plugin tmpdir/pr16746b.o tmpdir/pr16746d.o"]
+    set exec_output [run_host_cmd "$CC_FOR_TARGET" "-O2 -flto -fuse-linker-plugin tmpdir/pr16746b.o tmpdir/pr16746d.o"]
     if { [ regexp "warning: \\.?foobar" $exec_output ] && ![ regexp "symbol from plugin" $exec_output ] } {
 	pass $testname
     } {
 	fail $testname
     }
     set testname "PR ld/16746 (4)"
-    set exec_output [run_host_cmd "$CC" "-O2 -flto -fuse-linker-plugin tmpdir/pr16746d.o tmpdir/pr16746b.o"]
+    set exec_output [run_host_cmd "$CC_FOR_TARGET" "-O2 -flto -fuse-linker-plugin tmpdir/pr16746d.o tmpdir/pr16746b.o"]
     if { [ regexp "warning: \\.?foobar" $exec_output ] && ![ regexp "symbol from plugin" $exec_output ] } {
 	pass $testname
     } {
@@ -712,7 +714,7 @@  run_cc_link_tests [list \
 
 set exec_output [run_host_cmd "sh" \
 			      "-c \"ulimit -n 20; \
-			      $CC -Btmpdir/ld -o tmpdir/pr28138 \
+			      $CC_FOR_TARGET -Btmpdir/ld -o tmpdir/pr28138 \
 			      tmpdir/pr28138.o tmpdir/pr28138.a\""]
 set exec_output [prune_warnings $exec_output]
 if [string match "" $exec_output] then {
@@ -749,7 +751,7 @@  if { [at_least_gcc_version 4 7] } {
 	   || [istarget "x86_64-*-linux*"]
 	   || [istarget "amd64-*-linux*"]) } {
 	set testname "PR ld/12365"
-	set exec_output [run_host_cmd "$CC" "-O2 -flto -flto-partition=none -fuse-linker-plugin -o tmpdir/pr12365 tmpdir/pr12365a.o tmpdir/pr12365b.o tmpdir/pr12365c.o"]
+	set exec_output [run_host_cmd "$CC_FOR_TARGET" "-O2 -flto -flto-partition=none -fuse-linker-plugin -o tmpdir/pr12365 tmpdir/pr12365a.o tmpdir/pr12365b.o tmpdir/pr12365c.o"]
 	set exec_output [prune_warnings $exec_output]
 	if { [ regexp "undefined reference to `my_bcopy'" $exec_output ] } {
 	    # Linker should catch the reference to undefined `my_bcopy'
@@ -797,7 +799,7 @@  if { [at_least_gcc_version 4 7] } {
 	]
     }
     set testname "PR ld/12942 (3)"
-    set exec_output [run_host_cmd "$CXX" "-O2 -flto -fuse-linker-plugin tmpdir/pr12942b.o tmpdir/pr12942a.o"]
+    set exec_output [run_host_cmd "$CXX_FOR_TARGET" "-O2 -flto -fuse-linker-plugin tmpdir/pr12942b.o tmpdir/pr12942a.o"]
     if { [ regexp "undefined reference to `\\.?link_error\\(\\)'" $exec_output ] } {
         pass $testname
     } {
@@ -889,10 +891,10 @@  if { [is_elf_format] && [check_lto_shared_available] } {
 }
 
 proc pr20103 {cflags libs} {
-    global CC
+    global CC_FOR_TARGET
 
     set testname "PR ld/20103 ($cflags $libs)"
-    set exec_output [run_host_cmd "$CC" "$cflags $libs"]
+    set exec_output [run_host_cmd "$CC_FOR_TARGET" "$cflags $libs"]
     if { [ regexp "undefined reference to `\\.?dead'" $exec_output ] } {
         pass "$testname (1)"
     } {
diff --git a/ld/testsuite/ld-plugin/plugin.exp b/ld/testsuite/ld-plugin/plugin.exp
index b8bf29e7ca0..2652a8d3ed1 100644
--- a/ld/testsuite/ld-plugin/plugin.exp
+++ b/ld/testsuite/ld-plugin/plugin.exp
@@ -77,10 +77,6 @@  set regas "-plugin-opt registerallsymbolsread"
 set regassilent "-plugin-opt registerallsymbolsreadsilent"
 set regcln "-plugin-opt registercleanup"
 
-if { [istarget m681*-*-*] || [istarget m68hc1*-*-*] || [istarget m9s12x*-*-*] } {
-    # otherwise get FAILS due to _.frame
-    set CFLAGS "$CFLAGS -fomit-frame-pointer"
-}
 # In order to define symbols in plugin options in the list of tests below,
 # we need to know if the platform prepends an underscore to C symbols,
 # which we find out by compiling the test objects now.  If there is any
@@ -91,16 +87,20 @@  if { [istarget m681*-*-*] || [istarget m68hc1*-*-*] || [istarget m9s12x*-*-*] }
 set failed_compile 0
 set _ ""
 set plugin_nm_output ""
-set old_CFLAGS "$CFLAGS"
-append CFLAGS " $NOSANITIZE_CFLAGS $NOLTO_CFLAGS"
+set old_CFLAGS "$CFLAGS_FOR_TARGET"
+append CFLAGS_FOR_TARGET " $NOSANITIZE_CFLAGS $NOLTO_CFLAGS"
+if { [istarget m681*-*-*] || [istarget m68hc1*-*-*] || [istarget m9s12x*-*-*] } {
+    # otherwise get FAILS due to _.frame
+    append CFLAGS_FOR_TARGET " -fomit-frame-pointer"
+}
 
 if { $can_compile && \
-	(![ld_compile "$CC $CFLAGS" $srcdir/$subdir/main.c tmpdir/main.o] \
-	|| ![ld_compile "$CC $CFLAGS" $srcdir/$subdir/func.c tmpdir/func.o] \
-	|| ![ld_compile "$CC $CFLAGS" $srcdir/$subdir/text.c tmpdir/text.o] \
-	|| ![ld_compile "$CC $CFLAGS" $srcdir/$subdir/pr20070a.c tmpdir/pr20070a.o] \
-	|| ![ld_compile "$CC $CFLAGS" $srcdir/$subdir/dummy.s tmpdir/dummy.o] \
-	|| ![ld_compile "$CC $CFLAGS" $srcdir/$subdir/pr17973.s tmpdir/pr17973.o]) } {
+	(![ld_compile "$CC_FOR_TARGET $CFLAGS_FOR_TARGET" $srcdir/$subdir/main.c tmpdir/main.o] \
+	|| ![ld_compile "$CC_FOR_TARGET $CFLAGS_FOR_TARGET" $srcdir/$subdir/func.c tmpdir/func.o] \
+	|| ![ld_compile "$CC_FOR_TARGET $CFLAGS_FOR_TARGET" $srcdir/$subdir/text.c tmpdir/text.o] \
+	|| ![ld_compile "$CC_FOR_TARGET $CFLAGS_FOR_TARGET" $srcdir/$subdir/pr20070a.c tmpdir/pr20070a.o] \
+	|| ![ld_compile "$CC_FOR_TARGET $CFLAGS_FOR_TARGET" $srcdir/$subdir/dummy.s tmpdir/dummy.o] \
+	|| ![ld_compile "$CC_FOR_TARGET $CFLAGS_FOR_TARGET" $srcdir/$subdir/pr17973.s tmpdir/pr17973.o]) } {
     # Defer fail until we have list of tests set.
     set failed_compile 1
 }
@@ -291,16 +291,16 @@  if { !$can_compile || $failed_compile } {
 	    unsupported [lindex $testitem 0]
 	}
     }
-    set CFLAGS "$old_CFLAGS"
+    set CFLAGS_FOR_TARGET "$old_CFLAGS"
     return
 }
 
 run_ld_link_tests $plugin_tests
 
 if { [is_elf_format] \
-     && [ld_compile "$CC $CFLAGS" $srcdir/$subdir/func1p.c tmpdir/func1p.o] \
-     && [ld_compile "$CC $CFLAGS" $srcdir/$subdir/func2i.c tmpdir/func2i.o] \
-     && [ld_compile "$CC $CFLAGS" $srcdir/$subdir/func3h.c tmpdir/func3h.o] } {
+     && [ld_compile "$CC_FOR_TARGET $CFLAGS_FOR_TARGET" $srcdir/$subdir/func1p.c tmpdir/func1p.o] \
+     && [ld_compile "$CC_FOR_TARGET $CFLAGS_FOR_TARGET" $srcdir/$subdir/func2i.c tmpdir/func2i.o] \
+     && [ld_compile "$CC_FOR_TARGET $CFLAGS_FOR_TARGET" $srcdir/$subdir/func3h.c tmpdir/func3h.o] } {
     run_ld_link_tests $plugin_extra_elf_tests
 }
 
@@ -394,4 +394,4 @@  if [ar_simple_create $ar "--plugin $plugin4_path" "tmpdir/libpr20070.a" \
     unsupported "PR ld/20070"
 }
 
-set CFLAGS "$old_CFLAGS"
+set CFLAGS_FOR_TARGET "$old_CFLAGS"
diff --git a/ld/testsuite/ld-scripts/crossref.exp b/ld/testsuite/ld-scripts/crossref.exp
index 4eaafcc0f25..42ce87ffaba 100644
--- a/ld/testsuite/ld-scripts/crossref.exp
+++ b/ld/testsuite/ld-scripts/crossref.exp
@@ -38,37 +38,37 @@  if { ![check_compiler_available] } {
     return
 }
 
-set old_CFLAGS "$CFLAGS"
+set old_CFLAGS "$CFLAGS_FOR_TARGET"
 
 # Pass -fplt to CC since -fno-plt doesn't work with NOCROSSREFS tests.
 # Also add $NOPIE_CFLAGS since PIE doesn't work NOCROSSREFS tests.
-set old_CC "$CC"
-set CC "$CC $PLT_CFLAGS $NOPIE_CFLAGS"
+set old_CC "$CC_FOR_TARGET"
+append CC_FOR_TARGET " $PLT_CFLAGS $NOPIE_CFLAGS"
 
 # Xtensa targets currently default to putting literal values in a separate
 # section and that requires linker script support, so put literals in text.
 if [istarget xtensa*-*-*] {
-    set CFLAGS "$CFLAGS -mtext-section-literals"
+    append CFLAGS_FOR_TARGET " -mtext-section-literals"
 }
 
 # Prevent the use of the MeP's small data area which references a symbol
 # called __sdabase which will not be defined by our test linker scripts.
 if [istarget mep*-*-elf] {
-    set CFLAGS "-mtiny=0"
+    append CFLAGS_FOR_TARGET " -mtiny=0"
 }
 
 # The .dsbt section and __c6xabi_DSBT_BASE are not defined in our test
 # linker scripts.
 if [istarget tic6x*-*-*] {
-    set CFLAGS "-mno-dsbt -msdata=none"
+    append CFLAGS_FOR_TARGET " -mno-dsbt -msdata=none"
 }
 
-if { ![ld_compile "$CC $NOSANITIZE_CFLAGS $NOLTO_CFLAGS" "$srcdir/$subdir/cross1.c" tmpdir/cross1.o] \
-     || ![ld_compile "$CC $NOSANITIZE_CFLAGS $NOLTO_CFLAGS" "$srcdir/$subdir/cross2.c" tmpdir/cross2.o] } {
+if { ![ld_compile "$CC_FOR_TARGET $NOSANITIZE_CFLAGS $NOLTO_CFLAGS" "$srcdir/$subdir/cross1.c" tmpdir/cross1.o] \
+     || ![ld_compile "$CC_FOR_TARGET $NOSANITIZE_CFLAGS $NOLTO_CFLAGS" "$srcdir/$subdir/cross2.c" tmpdir/cross2.o] } {
     unsupported $test1
     unsupported $test2
-    set CFLAGS "$old_CFLAGS"
-    set CC "$old_CC"
+    set CFLAGS_FOR_TARGET "$old_CFLAGS"
+    set CC_FOR_TARGET "$old_CC"
     return
 }
 
@@ -102,10 +102,10 @@  if [string match "" $exec_output] then {
 
 # Check cross references within a single object.
 
-if { ![ld_compile "$CC $NOSANITIZE_CFLAGS $NOLTO_CFLAGS" "$srcdir/$subdir/cross3.c" tmpdir/cross3.o] } {
+if { ![ld_compile "$CC_FOR_TARGET $NOSANITIZE_CFLAGS $NOLTO_CFLAGS" "$srcdir/$subdir/cross3.c" tmpdir/cross3.o] } {
     unsupported $test2
-    set CFLAGS "$old_CFLAGS"
-    set CC "$old_CC"
+    set CFLAGS_FOR_TARGET "$old_CFLAGS"
+    set CC_FOR_TARGET "$old_CC"
     return
 }
 
@@ -127,17 +127,17 @@  if [string match "" $exec_output] then {
 
 # Check cross references for ld -r
 
-if { ![ld_compile "$CC $NOSANITIZE_CFLAGS $NOLTO_CFLAGS" "$srcdir/$subdir/cross4.c" tmpdir/cross4.o] } {
+if { ![ld_compile "$CC_FOR_TARGET $NOSANITIZE_CFLAGS $NOLTO_CFLAGS" "$srcdir/$subdir/cross4.c" tmpdir/cross4.o] } {
     unsupported $test3
-    set CFLAGS "$old_CFLAGS"
-    set CC "$old_CC"
+    set CFLAGS_FOR_TARGET "$old_CFLAGS"
+    set CC_FOR_TARGET "$old_CC"
     return
 }
 
 if ![ld_relocate $ld tmpdir/cross3-partial.o "tmpdir/cross1.o tmpdir/cross4.o"] {
     fail $test3
-    set CFLAGS "$old_CFLAGS"
-    set CC "$old_CC"
+    set CFLAGS_FOR_TARGET "$old_CFLAGS"
+    set CC_FOR_TARGET "$old_CC"
     return
 }
 
@@ -210,5 +210,5 @@  if [string match "" $exec_output] then {
     }
 }
 
-set CFLAGS "$old_CFLAGS"
-set CC "$old_CC"
+set CFLAGS_FOR_TARGET "$old_CFLAGS"
+set CC_FOR_TARGET "$old_CC"
diff --git a/ld/testsuite/ld-selective/selective.exp b/ld/testsuite/ld-selective/selective.exp
index 7b115adb781..1c42bbc8ad5 100644
--- a/ld/testsuite/ld-selective/selective.exp
+++ b/ld/testsuite/ld-selective/selective.exp
@@ -64,7 +64,7 @@  if [istarget avr-*-*] {
 }
 
 # If we don't have g++ for the target, mark all tests as untested.
-if { ![is_remote host] && [which $CXX] == 0 } {
+if { ![is_remote host] && [which $CXX_FOR_TARGET] == 0 } {
     foreach testitem $seltests {
 	untested "[lindex $testitem 0]"
     }
@@ -90,10 +90,10 @@  foreach testitem $seltests {
 
     # It's either C or C++ at the moment.
     if { $testtype == "C++" } {
-	set compiler "$CXX"
+	set compiler "$CXX_FOR_TARGET"
 	# Starting with 3.4.0, -fvtable-gc is no longer supported and thus
 	# the functionality we try to test for cannot be expected to work.
-	set version [remote_exec host "$CXX -dumpversion"]
+	set version [remote_exec host "$CXX_FOR_TARGET -dumpversion"]
 	set version [lindex $version 1]
 	if { [regexp "^(\[1-9\]\[0-9\]+|\[4-9\]|3.(\[1-9\]\[0-9\]+))\\." $version] \
 	     || [regexp "^(\[1-9\]\[0-9\]+|\[4-9\])" $version] } {
@@ -104,7 +104,7 @@  foreach testitem $seltests {
 	}
     } else {
 	set testflags "$cflags"
-	set compiler "$CC"
+	set compiler "$CC_FOR_TARGET"
     }
 
     # Note that we do not actually *use* CXX; we just add cxxflags for C++
diff --git a/ld/testsuite/ld-sh/sh.exp b/ld/testsuite/ld-sh/sh.exp
index 78905f0d89b..2611fb809b8 100644
--- a/ld/testsuite/ld-sh/sh.exp
+++ b/ld/testsuite/ld-sh/sh.exp
@@ -144,7 +144,7 @@  if [istarget sh*-*linux*] {
 }
 
 if {![ld_assemble $as "-relax tmpdir/start.s" tmpdir/start.o] \
-    || ![ld_compile $CC "-O -mrelax -foptimize-sibling-calls $srcdir/$subdir/sh2.c" tmpdir/sh2.o]} {
+    || ![ld_compile $CC_FOR_TARGET "-O -mrelax -foptimize-sibling-calls $srcdir/$subdir/sh2.c" tmpdir/sh2.o]} {
     unsupported $testlink
     unsupported $testjsr
     unsupported $testrun
diff --git a/ld/testsuite/ld-shared/shared.exp b/ld/testsuite/ld-shared/shared.exp
index 0e45f5c0423..d191e316e46 100644
--- a/ld/testsuite/ld-shared/shared.exp
+++ b/ld/testsuite/ld-shared/shared.exp
@@ -66,8 +66,8 @@  set SHCFLAG ""
 set shared_needs_pic "no"
 
 # Disable all sanitizers.
-set old_CFLAGS "$CFLAGS"
-append CFLAGS " $NOSANITIZE_CFLAGS"
+set old_CFLAGS "$CFLAGS_FOR_TARGET"
+append CFLAGS_FOR_TARGET " $NOSANITIZE_CFLAGS"
 
 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
 
@@ -114,7 +114,7 @@  if [istarget arm*-*-linux*] {
 	set file [open $tmpdir/movw-detect.c w]
 	puts $file "void foo(void) { __asm (\"movw r0, #0\"); }"
 	close $file
-	if [run_host_cmd_yesno "$CC" "$CFLAGS -c $tmpdir/movw-detect.c -o $tmpdir/movw-detect.o"] {
+	if [run_host_cmd_yesno "$CC_FOR_TARGET" "$CFLAGS_FOR_TARGET -c $tmpdir/movw-detect.c -o $tmpdir/movw-detect.o"] {
 	    set shared_needs_pic "yes"
 	}
     }
@@ -122,7 +122,7 @@  if [istarget arm*-*-linux*] {
 
 # The test procedure.
 proc shared_test { progname testname main sh1 sh2 dat args } {
-    global CC
+    global CC_FOR_TARGET
     global srcdir
     global subdir
     global exec_output
@@ -140,7 +140,7 @@  proc shared_test { progname testname main sh1 sh2 dat args } {
     if { [is_elf_format] && [check_shared_lib_support] } {
 	append shared " -Wl,-z,notext"
     }
-    if {![ld_link $CC $tmpdir/$progname.so "$shared $shldflags $tmpdir/$sh1 $tmpdir/$sh2"]} {
+    if {![ld_link $CC_FOR_TARGET $tmpdir/$progname.so "$shared $shldflags $tmpdir/$sh1 $tmpdir/$sh2"]} {
         fail "$testname"
     	return
     }
@@ -153,7 +153,7 @@  proc shared_test { progname testname main sh1 sh2 dat args } {
     if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
 	set rpath /lib:$tmpdir
     }
-    if ![ld_link $CC $tmpdir/$progname "-Wl,-rpath,$rpath $tmpdir/$main $tmpdir/$progname.so"] {
+    if ![ld_link $CC_FOR_TARGET $tmpdir/$progname "-Wl,-rpath,$rpath $tmpdir/$main $tmpdir/$progname.so"] {
     	fail "$testname"
     	return
     }
@@ -197,9 +197,9 @@  if { [istarget mips*-*-*] && ! [at_least_gcc_version 4 3] } then {
     # Unfortunately, the gcc argument is -fpic and the cc argument is
     # -KPIC.  We have to try both.
     set picflag "-fpic"
-    send_log "$CC $picflag\n"
-    verbose "$CC $picflag"
-    catch "exec $CC $picflag" exec_output
+    send_log "$CC_FOR_TARGET $picflag\n"
+    verbose "$CC_FOR_TARGET $picflag"
+    catch "exec $CC_FOR_TARGET $picflag" exec_output
     send_log "$exec_output\n"
     verbose "--" "$exec_output"
     if { [string match "*illegal option*" $exec_output] \
@@ -212,7 +212,7 @@  if { [istarget mips*-*-*] && ! [at_least_gcc_version 4 3] } then {
 verbose "Using $picflag to compile PIC code"
 
 # Compile the main program.
-if ![ld_compile "$CC $CFLAGS $SHCFLAG" $srcdir/$subdir/main.c $tmpdir/mainnp.o] {
+if ![ld_compile "$CC_FOR_TARGET $CFLAGS_FOR_TARGET $SHCFLAG" $srcdir/$subdir/main.c $tmpdir/mainnp.o] {
     unsupported "shared (non PIC)"
     unsupported "shared"
 } else {
@@ -223,8 +223,8 @@  if ![ld_compile "$CC $CFLAGS $SHCFLAG" $srcdir/$subdir/main.c $tmpdir/mainnp.o]
     # using -fpic will cause some of the tests to return different
     # results.  Make sure that PLT is used since PLT is expected.
     global PLT_CFLAGS NOPIE_CFLAGS
-    if { ![ld_compile "$CC $PLT_CFLAGS $NOPIE_CFLAGS $CFLAGS $SHCFLAG" $srcdir/$subdir/sh1.c $tmpdir/sh1np.o]
-	 || ![ld_compile "$CC $PLT_CFLAGS $CFLAGS $SHCFLAG" $srcdir/$subdir/sh2.c $tmpdir/sh2np.o] } {
+    if { ![ld_compile "$CC_FOR_TARGET $PLT_CFLAGS $NOPIE_CFLAGS $CFLAGS_FOR_TARGET $SHCFLAG" $srcdir/$subdir/sh1.c $tmpdir/sh1np.o]
+	 || ![ld_compile "$CC_FOR_TARGET $PLT_CFLAGS $CFLAGS_FOR_TARGET $SHCFLAG" $srcdir/$subdir/sh2.c $tmpdir/sh2np.o] } {
 	unsupported "shared (non PIC)"
     } else { if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
 	shared_test shnp "shared (nonPIC)" mainnp.o sh1np.o sh2np.o xcoff
@@ -284,25 +284,25 @@  if ![ld_compile "$CC $CFLAGS $SHCFLAG" $srcdir/$subdir/main.c $tmpdir/mainnp.o]
 
     # Now compile the code using -fpic.
 
-    if { ![ld_compile "$CC $CFLAGS $SHCFLAG $picflag" $srcdir/$subdir/sh1.c $tmpdir/sh1p.o]
-	 || ![ld_compile "$CC $CFLAGS $SHCFLAG $picflag" $srcdir/$subdir/sh2.c $tmpdir/sh2p.o] } {
+    if { ![ld_compile "$CC_FOR_TARGET $CFLAGS_FOR_TARGET $SHCFLAG $picflag" $srcdir/$subdir/sh1.c $tmpdir/sh1p.o]
+	 || ![ld_compile "$CC_FOR_TARGET $CFLAGS_FOR_TARGET $SHCFLAG $picflag" $srcdir/$subdir/sh2.c $tmpdir/sh2p.o] } {
 	unsupported "shared"
     } else {
 	if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
 	    shared_test shp "shared" mainnp.o sh1p.o sh2p.o xcoff
 	} else {
 	    shared_test shp "shared" mainnp.o sh1p.o sh2p.o shared
-	    ld_compile "$CC $CFLAGS -DSYMBOLIC_TEST -DXCOFF_TEST $SHCFLAG" $srcdir/$subdir/main.c $tmpdir/mainnp.o
-	    ld_compile "$CC $CFLAGS -DSYMBOLIC_TEST -DXCOFF_TEST $SHCFLAG $picflag" $srcdir/$subdir/sh1.c $tmpdir/sh1p.o
+	    ld_compile "$CC_FOR_TARGET $CFLAGS_FOR_TARGET -DSYMBOLIC_TEST -DXCOFF_TEST $SHCFLAG" $srcdir/$subdir/main.c $tmpdir/mainnp.o
+	    ld_compile "$CC_FOR_TARGET $CFLAGS_FOR_TARGET -DSYMBOLIC_TEST -DXCOFF_TEST $SHCFLAG $picflag" $srcdir/$subdir/sh1.c $tmpdir/sh1p.o
 	    shared_test shp "shared -Bsymbolic" mainnp.o sh1p.o sh2p.o symbolic "-Bsymbolic"
-	    ld_compile "$CC $CFLAGS $SHCFLAG" $srcdir/$subdir/main.c $tmpdir/mainnp.o
-	    ld_compile "$CC $CFLAGS $SHCFLAG $picflag" $srcdir/$subdir/sh1.c $tmpdir/sh1p.o
+	    ld_compile "$CC_FOR_TARGET $CFLAGS_FOR_TARGET $SHCFLAG" $srcdir/$subdir/main.c $tmpdir/mainnp.o
+	    ld_compile "$CC_FOR_TARGET $CFLAGS_FOR_TARGET $SHCFLAG $picflag" $srcdir/$subdir/sh1.c $tmpdir/sh1p.o
 	}
     }
 }
 
 # Now do the same tests again, but this time compile main.c PIC.
-if ![ld_compile "$CC $CFLAGS $SHCFLAG $picflag" $srcdir/$subdir/main.c $tmpdir/mainp.o] {
+if ![ld_compile "$CC_FOR_TARGET $CFLAGS_FOR_TARGET $SHCFLAG $picflag" $srcdir/$subdir/main.c $tmpdir/mainp.o] {
     unsupported "shared (PIC main, non PIC so)"
     unsupported "shared (PIC main)"
 } else {
@@ -347,7 +347,7 @@  if ![ld_compile "$CC $CFLAGS $SHCFLAG $picflag" $srcdir/$subdir/main.c $tmpdir/m
     }
 }
 
-set CFLAGS "$old_CFLAGS"
+set CFLAGS_FOR_TARGET "$old_CFLAGS"
 
 if { [istarget rs6000*-*-aix*] || [istarget powerpc*-*-aix*] } {
     # Remove the temporary directory.
diff --git a/ld/testsuite/ld-srec/srec.exp b/ld/testsuite/ld-srec/srec.exp
index c8d561b87a3..20472afcfc7 100644
--- a/ld/testsuite/ld-srec/srec.exp
+++ b/ld/testsuite/ld-srec/srec.exp
@@ -355,26 +355,26 @@  if { ![check_compiler_available] } {
 # 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.
-set old_CC "$CC"
-set CC "$CC $PLT_CFLAGS $NOPIE_CFLAGS $NOPIE_LDFLAGS $NOCF_PROTECTION_CFLAGS $NOSANITIZE_CFLAGS $NOLTO_CFLAGS"
-set old_CXX "$CXX"
-set old_CXXFLAGS "$CXXFLAGS"
-set CXXFLAGS "$CXXFLAGS $PLT_CFLAGS $NOPIE_CFLAGS $NOPIE_LDFLAGS $NOCF_PROTECTION_CFLAGS $NOSANITIZE_CFLAGS $NOLTO_CFLAGS"
+set old_CC "$CC_FOR_TARGET"
+append CC_FOR_TARGET " $PLT_CFLAGS $NOPIE_CFLAGS $NOPIE_LDFLAGS $NOCF_PROTECTION_CFLAGS $NOSANITIZE_CFLAGS $NOLTO_CFLAGS"
+set old_CXX "$CXX_FOR_TARGET"
+set old_CXXFLAGS "$CXXFLAGS_FOR_TARGET"
+append CXXFLAGS_FOR_TARGET " $PLT_CFLAGS $NOPIE_CFLAGS $NOPIE_LDFLAGS $NOCF_PROTECTION_CFLAGS $NOSANITIZE_CFLAGS $NOLTO_CFLAGS"
 
 # S-records can't handle .note.gnu.property sections.
 if { [is_elf_format] \
      && ([istarget "i?86-*-*"] || [istarget "x86_64-*-*"]) } {
-    set CC "$CC -Wa,-mx86-used-note=no"
-    set CXX "$CXX -Wa,-mx86-used-note=no"
+    append CC_FOR_TARGET " -Wa,-mx86-used-note=no"
+    append CXX_FOR_TARGET " -Wa,-mx86-used-note=no"
 }
 
-if { ![ld_compile $CC $srcdir/$subdir/sr1.c tmpdir/sr1.o] \
-     || ![ld_compile $CC $srcdir/$subdir/sr2.c tmpdir/sr2.o] } {
+if { ![ld_compile $CC_FOR_TARGET $srcdir/$subdir/sr1.c tmpdir/sr1.o] \
+     || ![ld_compile $CC_FOR_TARGET $srcdir/$subdir/sr2.c tmpdir/sr2.o] } {
     unsupported $test1
     unsupported $test2
-    set CC "$old_CC"
-    set CXX "$old_CXX"
-    set CXXFLAGS "$old_CXXFLAGS"
+    set CC_FOR_TARGET "$old_CC"
+    set CXX_FOR_TARGET "$old_CXX"
+    set CXXFLAGS_FOR_TARGET "$old_CXXFLAGS"
     return
 }
 
@@ -444,19 +444,19 @@  run_srec_test $test1 "tmpdir/sr1.o tmpdir/sr2.o"
 # destructors.  Note that since we are not linking against any
 # libraries, this program won't actually work or anything.
 
-if { ![is_remote host] && [which $CXX] == 0 } {
+if { ![is_remote host] && [which $CXX_FOR_TARGET] == 0 } {
     untested $test2
-    set CC "$old_CC"
-    set CXX "$old_CXX"
-    set CXXFLAGS "$old_CXXFLAGS"
+    set CC_FOR_TARGET "$old_CC"
+    set CXX_FOR_TARGET "$old_CXX"
+    set CXXFLAGS_FOR_TARGET "$old_CXXFLAGS"
     return
 }
 
-if ![ld_compile "$CXX $CXXFLAGS -fno-exceptions" $srcdir/$subdir/sr3.cc tmpdir/sr3.o] {
+if ![ld_compile "$CXX_FOR_TARGET $CXXFLAGS_FOR_TARGET -fno-exceptions" $srcdir/$subdir/sr3.cc tmpdir/sr3.o] {
     unsupported $test2
-    set CC "$old_CC"
-    set CXX "$old_CXX"
-    set CXXFLAGS "$old_CXXFLAGS"
+    set CC_FOR_TARGET "$old_CC"
+    set CXX_FOR_TARGET "$old_CXX"
+    set CXXFLAGS_FOR_TARGET "$old_CXXFLAGS"
     return
 }
 
@@ -482,6 +482,6 @@  setup_xfail "bpf-*-*"
 
 run_srec_test $test2 "tmpdir/sr3.o"
 
-set CC "$old_CC"
-set CXX "$old_CXX"
-set CXXFLAGS "$old_CXXFLAGS"
+set CC_FOR_TARGET "$old_CC"
+set CXX_FOR_TARGET "$old_CXX"
+set CXXFLAGS_FOR_TARGET "$old_CXXFLAGS"
diff --git a/ld/testsuite/ld-undefined/undefined.exp b/ld/testsuite/ld-undefined/undefined.exp
index 9e2b073881b..017d72dc610 100644
--- a/ld/testsuite/ld-undefined/undefined.exp
+++ b/ld/testsuite/ld-undefined/undefined.exp
@@ -29,7 +29,7 @@  if { ![check_compiler_available] } {
     untested $testund
     untested $testfn
     untested $testline
-} elseif { ![ld_compile "$CC -g $NOLTO_CFLAGS" $srcdir/$subdir/undefined.c tmpdir/undefined.o] } {
+} elseif { ![ld_compile "$CC_FOR_TARGET -g $NOLTO_CFLAGS" $srcdir/$subdir/undefined.c tmpdir/undefined.o] } {
     verbose "Unable to compile test file!" 1
     unsupported $testund
     unsupported $testfn
diff --git a/ld/testsuite/ld-unique/unique.exp b/ld/testsuite/ld-unique/unique.exp
index 22209995e94..88a6a2ff3d6 100644
--- a/ld/testsuite/ld-unique/unique.exp
+++ b/ld/testsuite/ld-unique/unique.exp
@@ -102,13 +102,13 @@  proc contains_unique_symbol { binary_file } {
 set fails 0
 
 # Create object file containing unique symbol.
-if ![ld_compile "$CC -c" "$srcdir/$subdir/unique.s" "tmpdir/unique.o"] {
+if ![ld_compile "$CC_FOR_TARGET -c" "$srcdir/$subdir/unique.s" "tmpdir/unique.o"] {
     fail "Could not create a unique object"
     set fails [expr $fails + 1]
 }
 
 # Create object file NOT containing unique symbol.
-if ![ld_compile "$CC -c" "$srcdir/$subdir/unique_empty.s" "tmpdir/unique_empty.o"] {
+if ![ld_compile "$CC_FOR_TARGET -c" "$srcdir/$subdir/unique_empty.s" "tmpdir/unique_empty.o"] {
     fail "Could not create a non-unique object"
     set fails [expr $fails + 1]
 }
@@ -123,7 +123,7 @@  if [board_info [target_info name] exists cflags] {
 }
 
 # Create executable containing unique symbol.
-if ![ld_link "$CC $NOPIE_LDFLAGS $board_cflags" "tmpdir/unique_prog" "tmpdir/unique.o"] {
+if ![ld_link "$CC_FOR_TARGET $NOPIE_LDFLAGS $board_cflags" "tmpdir/unique_prog" "tmpdir/unique.o"] {
     fail "Could not link a unique executable"
     set fails [expr $fails + 1]
 }
@@ -190,7 +190,7 @@  if { ![check_shared_lib_support] } {
 }
 
 # Create pic object file containing unique symbol.
-if {![ld_compile "$CC -c -fPIC" "$srcdir/$subdir/unique_shared.s" "tmpdir/unique_shared.o"] } {
+if {![ld_compile "$CC_FOR_TARGET -c -fPIC" "$srcdir/$subdir/unique_shared.s" "tmpdir/unique_shared.o"] } {
     fail "Could not create a pic unique object"
     set fails [expr $fails + 1]
 }
@@ -202,7 +202,7 @@  if {![ld_link $ld "tmpdir/libunique_shared.so" "-shared tmpdir/unique_shared.o"]
 }
 
 # Create executable NOT containing unique symbol linked against library.
-if {![ld_link "$CC $NOPIE_LDFLAGS $board_cflags" "tmpdir/unique_shared_prog" "-Ltmpdir tmpdir/unique_empty.o -Wl,-Bdynamic,-rpath=./tmpdir -lunique_shared"] } {
+if {![ld_link "$CC_FOR_TARGET $NOPIE_LDFLAGS $board_cflags" "tmpdir/unique_shared_prog" "-Ltmpdir tmpdir/unique_empty.o -Wl,-Bdynamic,-rpath=./tmpdir -lunique_shared"] } {
     fail "Could not link a dynamic executable"
     set fails [expr $fails + 1]
 }
diff --git a/ld/testsuite/ld-x86-64/tls.exp b/ld/testsuite/ld-x86-64/tls.exp
index 8f6c21c2ac9..9947b51e296 100644
--- a/ld/testsuite/ld-x86-64/tls.exp
+++ b/ld/testsuite/ld-x86-64/tls.exp
@@ -178,7 +178,7 @@  run_ld_link_exec_tests [list \
 ]
 
 # Skip if -mtls-dialect=gnu2 doesn't work.
-if { ![run_host_cmd_yesno $CC "-c -O2 -fPIC $GNU2_CFLAGS $srcdir/$subdir/tlsdesc1b.c -o tmpdir/tlsdesc1b.o"] } {
+if { ![run_host_cmd_yesno $CC_FOR_TARGET "-c -O2 -fPIC $GNU2_CFLAGS $srcdir/$subdir/tlsdesc1b.c -o tmpdir/tlsdesc1b.o"] } {
     return
 }
 
diff --git a/ld/testsuite/lib/ld-lib.exp b/ld/testsuite/lib/ld-lib.exp
index fec31fa8e19..a42e433d6d3 100644
--- a/ld/testsuite/lib/ld-lib.exp
+++ b/ld/testsuite/lib/ld-lib.exp
@@ -24,16 +24,16 @@  load_file $srcdir/../../binutils/testsuite/lib/binutils-common.exp
 # Returns 0 otherwise.
 #
 proc at_least_gcc_version { major minor } {
-    global CC
+    global CC_FOR_TARGET
 
-    if {![info exists CC]} {
-	set CC [find_gcc]
+    if {![info exists CC_FOR_TARGET]} {
+	set CC_FOR_TARGET [find_gcc]
     }
-    if { $CC == "" } {
+    if { $CC_FOR_TARGET == "" } {
 	return 0
     }
     # Filter out -Wl, options.
-    regsub -all -- "-Wl,\[^ ^\t\]+" $CC "" cc_cmd
+    regsub -all -- "-Wl,\[^ ^\t\]+" $CC_FOR_TARGET "" cc_cmd
     set state [remote_exec host $cc_cmd --version]
     if { [lindex $state 0] != 0 } {
 	return 0;
@@ -216,8 +216,8 @@  proc default_ld_link { ld target objects } {
 # Compile an object using cc.
 #
 proc default_ld_compile { cc source object } {
-    global CFLAGS
-    global CXXFLAGS
+    global CFLAGS_FOR_TARGET
+    global CXXFLAGS_FOR_TARGET
     global srcdir
     global subdir
     global host_triplet
@@ -252,9 +252,9 @@  proc default_ld_compile { cc source object } {
 
     set ccexe [string replace $ccexe 0 [string last "/" $ccexe] ""]
     if {[string match "*++*" $ccexe]} {
-	append flags " $CXXFLAGS"
+	append flags " $CXXFLAGS_FOR_TARGET"
     } else {
-	append flags " $CFLAGS"
+	append flags " $CFLAGS_FOR_TARGET"
     }
 
     if [board_info [target_info name] exists cflags] {
@@ -466,8 +466,8 @@  proc run_ld_link_tests { ldtests args } {
     global srcdir
     global subdir
     global env
-    global CC
-    global CFLAGS
+    global CC_FOR_TARGET
+    global CFLAGS_FOR_TARGET
     global runtests
     global exec_output
 
@@ -534,7 +534,7 @@  proc run_ld_link_tests { ldtests args } {
 		set objfile "tmpdir/$fileroot.o"
 		if { [file extension $src_file] == ".c" } {
 		    set as_file "tmpdir/$fileroot.s"
-		    if ![ld_compile "$CC -S $CFLAGS $cflags" $srcdir/$subdir/$src_file $as_file] {
+		    if ![ld_compile "$CC_FOR_TARGET -S $CFLAGS_FOR_TARGET $cflags" $srcdir/$subdir/$src_file $as_file] {
 			set is_unsupported 1
 			break
 		    }
@@ -683,10 +683,10 @@  proc run_ld_link_exec_tests { ldtests args } {
     global srcdir
     global subdir
     global env
-    global CC
-    global CXX
-    global CFLAGS
-    global CXXFLAGS
+    global CC_FOR_TARGET
+    global CXX_FOR_TARGET
+    global CFLAGS_FOR_TARGET
+    global CXXFLAGS_FOR_TARGET
     global errcnt
     global exec_output
     global board_cflags
@@ -746,9 +746,9 @@  proc run_ld_link_exec_tests { ldtests args } {
 	    lappend objfiles $objfile
 
 	    if { [ string match "c++" $lang ] } {
-		set cmd "$CXX -c $CXXFLAGS $cflags"
+		set cmd "$CXX_FOR_TARGET -c $CXXFLAGS_FOR_TARGET $cflags"
 	    } else {
-		set cmd "$CC -c $CFLAGS $cflags"
+		set cmd "$CC_FOR_TARGET -c $CFLAGS_FOR_TARGET $cflags"
 	    }
 	    if ![ld_compile $cmd $srcdir/$subdir/$src_file $objfile] {
 		set failed 1
@@ -765,10 +765,10 @@  proc run_ld_link_exec_tests { ldtests args } {
 	    set link_cmd $ld
 	} elseif { [ string match "c++" $lang ] } {
 	    set link_proc ld_link
-	    set link_cmd $CXX
+	    set link_cmd $CXX_FOR_TARGET
 	} else {
 	    set link_proc ld_link
-	    set link_cmd $CC
+	    set link_cmd $CC_FOR_TARGET
 	}
 
 	if { $binfile eq "tmpdir/" } {
@@ -851,10 +851,10 @@  proc run_cc_link_tests { ldtests } {
     global srcdir
     global subdir
     global env
-    global CC
-    global CXX
-    global CFLAGS
-    global CXXFLAGS
+    global CC_FOR_TARGET
+    global CXX_FOR_TARGET
+    global CFLAGS_FOR_TARGET
+    global CXXFLAGS_FOR_TARGET
     global ar
     global exec_output
     global board_cflags
@@ -936,9 +936,9 @@  proc run_cc_link_tests { ldtests } {
 	    lappend objfiles $objfile
 
 	    if { [ string match "c++" $lang ] } {
-		set cmd "$CXX -c $CXXFLAGS $cflags"
+		set cmd "$CXX_FOR_TARGET -c $CXXFLAGS_FOR_TARGET $cflags"
 	    } else {
-		set cmd "$CC -c $CFLAGS $cflags"
+		set cmd "$CC_FOR_TARGET -c $CFLAGS_FOR_TARGET $cflags"
 	    }
 	    if ![ld_compile $cmd $srcdir/$subdir/$src_file $objfile] {
 		set failed 1
@@ -954,9 +954,9 @@  proc run_cc_link_tests { ldtests } {
 	reset_vars
 
 	if { [ string match "c++" $lang ] } {
-	    set cc_cmd $CXX
+	    set cc_cmd $CXX_FOR_TARGET
 	} else {
-	    set cc_cmd $CC
+	    set cc_cmd $CC_FOR_TARGET
 	}
 
 	if { $binfile eq "tmpdir/" } {
@@ -1200,10 +1200,10 @@  proc check_sysroot_available { } {
 
 proc check_compiler_available { } {
     global compiler_available_saved
-    global CC
+    global CC_FOR_TARGET
 
     if {![info exists compiler_available_saved]} {
-	if { [which $CC] == 0 } {
+	if { [which $CC_FOR_TARGET] == 0 } {
 	    set compiler_available_saved 0
 	    return 0
 	}
@@ -1228,7 +1228,7 @@  proc check_compiler_available { } {
 	if [is_remote host] {
 	    set src [remote_download host $src]
 	}
-	set compiler_available_saved [run_host_cmd_yesno "$CC" "$flags $src -o $output"]
+	set compiler_available_saved [run_host_cmd_yesno "$CC_FOR_TARGET" "$flags $src -o $output"]
 	remote_file host delete $src
 	remote_file host delete $output
 	file delete $src
@@ -1238,16 +1238,16 @@  proc check_compiler_available { } {
 
 # Returns 1 if plugin is enabled in gcc.  Returns 0 otherwise.
 proc check_gcc_plugin_enabled { } {
-    global CC
+    global CC_FOR_TARGET
 
-    if {![info exists CC]} {
-	set CC [find_gcc]
+    if {![info exists CC_FOR_TARGET]} {
+	set CC_FOR_TARGET [find_gcc]
     }
-    if { $CC == ""} {
+    if { $CC_FOR_TARGET == ""} {
 	return 0
     }
     # Filter out -Wl, options.
-    regsub -all -- "-Wl,\[^ ^\t\]+" $CC "" cc_cmd
+    regsub -all -- "-Wl,\[^ ^\t\]+" $CC_FOR_TARGET "" cc_cmd
     set state [remote_exec host $cc_cmd -v]
     if { [lindex $state 0] != 0 } {
 	return 0;
@@ -1266,7 +1266,7 @@  proc check_gcc_plugin_enabled { } {
 # Returns true if the target compiler supports LTO
 proc check_lto_available { } {
     global lto_available_saved
-    global CC
+    global CC_FOR_TARGET
 
     if {![info exists lto_available_saved]} {
 	if { ![check_gcc_plugin_enabled] } {
@@ -1298,7 +1298,7 @@  proc check_lto_available { } {
 	if [is_remote host] {
 	    set src [remote_download host $src]
 	}
-	set lto_available_saved [run_host_cmd_yesno "$CC" "$flags -flto -fuse-linker-plugin $src -o $output"]
+	set lto_available_saved [run_host_cmd_yesno "$CC_FOR_TARGET" "$flags -flto -fuse-linker-plugin $src -o $output"]
 	remote_file host delete $src
 	remote_file host delete $output
 	file delete $src
@@ -1309,7 +1309,7 @@  proc check_lto_available { } {
 # Returns true if the target compiler supports LTO  -ffat-lto-objects
 proc check_lto_fat_available { } {
     global lto_fat_available_saved
-    global CC
+    global CC_FOR_TARGET
 
     if {![info exists lto_fat_available_saved]} {
 	if { ![check_gcc_plugin_enabled] } {
@@ -1341,7 +1341,7 @@  proc check_lto_fat_available { } {
 	if [is_remote host] {
 	    set src [remote_download host $src]
 	}
-	set lto_fat_available_saved [run_host_cmd_yesno "$CC" "$flags -flto -ffat-lto-objects -fuse-linker-plugin $src -o $output"]
+	set lto_fat_available_saved [run_host_cmd_yesno "$CC_FOR_TARGET" "$flags -flto -ffat-lto-objects -fuse-linker-plugin $src -o $output"]
 	remote_file host delete $src
 	remote_file host delete $output
 	file delete $src
@@ -1352,7 +1352,7 @@  proc check_lto_fat_available { } {
 # Returns true if the target compiler supports LTO and -shared
 proc check_lto_shared_available { } {
     global lto_shared_available_saved
-    global CC
+    global CC_FOR_TARGET
 
     if {![info exists lto_shared_available_saved]} {
 	if { ![check_gcc_plugin_enabled] } {
@@ -1384,7 +1384,7 @@  proc check_lto_shared_available { } {
 	if [is_remote host] {
 	    set src [remote_download host $src]
 	}
-	set lto_shared_available_saved [run_host_cmd_yesno "$CC" "$flags -shared -fPIC -flto -fuse-linker-plugin $src -o $output"]
+	set lto_shared_available_saved [run_host_cmd_yesno "$CC_FOR_TARGET" "$flags -shared -fPIC -flto -fuse-linker-plugin $src -o $output"]
 	remote_file host delete $src
 	remote_file host delete $output
 	file delete $src
@@ -1418,7 +1418,7 @@  proc check_as_cfi { } {
 
 proc check_ifunc_available { } {
     global ifunc_available_saved
-    global CC
+    global CC_FOR_TARGET
 
     if {![info exists ifunc_available_saved]} {
 	if { ![check_compiler_available] } {
@@ -1452,7 +1452,7 @@  proc check_ifunc_available { } {
 	if [is_remote host] {
 	    set src [remote_download host $src]
 	}
-	set ifunc_available_saved [run_host_cmd_yesno "$CC" "$flags $src -o $output"]
+	set ifunc_available_saved [run_host_cmd_yesno "$CC_FOR_TARGET" "$flags $src -o $output"]
 	if { [isnative] && $ifunc_available_saved == 1 } {
 	  set ifunc_available_saved [run_host_cmd_yesno "$output" ""]
 	}
@@ -1467,7 +1467,7 @@  proc check_ifunc_available { } {
 
 proc check_ifunc_attribute_available { } {
     global ifunc_attribute_available_saved
-    global CC
+    global CC_FOR_TARGET
 
     if {![info exists ifunc_attribute_available_saved]} {
 	if { ![check_compiler_available] } {
@@ -1499,7 +1499,7 @@  proc check_ifunc_attribute_available { } {
 	if [is_remote host] {
 	    set src [remote_download host $src]
 	}
-	set ifunc_attribute_available_saved [run_host_cmd_yesno "$CC" "$flags $src -o $output"]
+	set ifunc_attribute_available_saved [run_host_cmd_yesno "$CC_FOR_TARGET" "$flags $src -o $output"]
 	if { [isnative] && $ifunc_attribute_available_saved == 1 } {
 	  set ifunc_attribute_available_saved [run_host_cmd_yesno "$output" ""]
 	}
@@ -1514,7 +1514,7 @@  proc check_ifunc_attribute_available { } {
 
 proc check_libdl_available { } {
     global libdl_available_saved
-    global CC
+    global CC_FOR_TARGET
 
     if {![info exists libdl_available_saved]} {
 	if { ![check_compiler_available] } {
@@ -1537,7 +1537,7 @@  proc check_libdl_available { } {
 	if [is_remote host] {
 	    set src [remote_download host $src]
 	}
-	set libdl_available_saved [run_host_cmd_yesno "$CC" "$src -o $output -ldl"]
+	set libdl_available_saved [run_host_cmd_yesno "$CC_FOR_TARGET" "$src -o $output -ldl"]
 	remote_file host delete $src
 	remote_file host delete $output
 	file delete $src
@@ -1549,7 +1549,7 @@  proc check_libdl_available { } {
 
 proc check_gnu2_tls_available { } {
     global gnu2_tls_available_saved
-    global CC
+    global CC_FOR_TARGET
     global GNU2_CFLAGS
 
     if {![info exists gnu2_tls_available_saved]} {
@@ -1593,9 +1593,9 @@  proc check_gnu2_tls_available { } {
 	if [is_remote host] {
 	    set src2 [remote_download host $src2]
 	}
-	set gnu2_tls_available_saved [run_host_cmd_yesno "$CC" "-fPIC -shared $flags $src1 -o $output1"]
+	set gnu2_tls_available_saved [run_host_cmd_yesno "$CC_FOR_TARGET" "-fPIC -shared $flags $src1 -o $output1"]
 	if { $gnu2_tls_available_saved == 1 } {
-	    set gnu2_tls_available_saved [run_host_cmd_yesno "$CC" "$flags $src2 $output1 -o $output2"]
+	    set gnu2_tls_available_saved [run_host_cmd_yesno "$CC_FOR_TARGET" "$flags $src2 $output1 -o $output2"]
 	    if { $gnu2_tls_available_saved == 1 } {
 		set gnu2_tls_available_saved [run_host_cmd_yesno "$output2" ""]
 	    }
@@ -1611,8 +1611,8 @@  proc check_gnu2_tls_available { } {
 
 # Compile a C source file, with the specified additional_flags.
 proc compile_one_cc { src output additional_flags } {
-    global CC
-    global CFLAGS
+    global CC_FOR_TARGET
+    global CFLAGS_FOR_TARGET
 
     set flags ""
     if [board_info [target_info name] exists cflags] {
@@ -1625,7 +1625,7 @@  proc compile_one_cc { src output additional_flags } {
     if [is_remote host] {
 	set src [remote_download host $src]
     }
-    return [run_host_cmd_yesno "$CC" "$flags $CFLAGS $additional_flags $src -o $output"]
+    return [run_host_cmd_yesno "$CC_FOR_TARGET" "$flags $CFLAGS_FOR_TARGET $additional_flags $src -o $output"]
 }
 
 # Returns true if the target compiler supports -gctf
diff --git a/libctf/Makefile.am b/libctf/Makefile.am
index e586d25fb37..76881844798 100644
--- a/libctf/Makefile.am
+++ b/libctf/Makefile.am
@@ -86,8 +86,9 @@  check-DEJAGNU: site.exp development.exp
 	runtest=$(RUNTEST); \
 	if $(SHELL) -c "$$runtest --version" > /dev/null 2>&1; then \
 	  $$runtest --tool $(DEJATOOL) --srcdir $${srcroot}/testsuite \
-		CC="$(CC_FOR_TARGET)" CFLAGS="$(CFLAGS) -I$(INCDIR) -I$(srcdir) -I$(builddir) -I$(builddir)/../bfd $(ZLIBINC)" \
-		CC_FOR_HOST="$(CC)" LIBS="$(libctf_nobfd_la_LIBADD) $(LIBS)" $(RUNTESTFLAGS); \
+		CC="$(CC)" CC_FOR_TARGET="$(CC_FOR_TARGET)" \
+		CFLAGS="$(CFLAGS) -I$(INCDIR) -I$(srcdir) -I$(builddir) -I$(builddir)/../bfd $(ZLIBINC)" \
+		LIBS="$(libctf_nobfd_la_LIBADD) $(LIBS)" $(RUNTESTFLAGS); \
 	else echo "WARNING: could not find \`runtest'" 1>&2; :;\
 	fi
 
diff --git a/libctf/Makefile.in b/libctf/Makefile.in
index 5cfa100f9cc..bb3b88fc109 100644
--- a/libctf/Makefile.in
+++ b/libctf/Makefile.in
@@ -1374,8 +1374,9 @@  uninstall-am: uninstall-includeHEADERS uninstall-libLTLIBRARIES
 @TCL_TRY_TRUE@	runtest=$(RUNTEST); \
 @TCL_TRY_TRUE@	if $(SHELL) -c "$$runtest --version" > /dev/null 2>&1; then \
 @TCL_TRY_TRUE@	  $$runtest --tool $(DEJATOOL) --srcdir $${srcroot}/testsuite \
-@TCL_TRY_TRUE@		CC="$(CC_FOR_TARGET)" CFLAGS="$(CFLAGS) -I$(INCDIR) -I$(srcdir) -I$(builddir) -I$(builddir)/../bfd $(ZLIBINC)" \
-@TCL_TRY_TRUE@		CC_FOR_HOST="$(CC)" LIBS="$(libctf_nobfd_la_LIBADD) $(LIBS)" $(RUNTESTFLAGS); \
+@TCL_TRY_TRUE@		CC="$(CC)" CC_FOR_TARGET="$(CC_FOR_TARGET)" \
+@TCL_TRY_TRUE@		CFLAGS="$(CFLAGS) -I$(INCDIR) -I$(srcdir) -I$(builddir) -I$(builddir)/../bfd $(ZLIBINC)" \
+@TCL_TRY_TRUE@		LIBS="$(libctf_nobfd_la_LIBADD) $(LIBS)" $(RUNTESTFLAGS); \
 @TCL_TRY_TRUE@	else echo "WARNING: could not find \`runtest'" 1>&2; :;\
 @TCL_TRY_TRUE@	fi
 
diff --git a/libctf/testsuite/config/default.exp b/libctf/testsuite/config/default.exp
index 32e882d0fef..f2aba561fe2 100644
--- a/libctf/testsuite/config/default.exp
+++ b/libctf/testsuite/config/default.exp
@@ -43,18 +43,18 @@  if {![file isdirectory tmpdir/libctf]} then {
 set gcc_B_opt "-B[pwd]/tmpdir/libctf/"
 set ld_L_opt ""
 
-# The "make check" target in the Makefile passes in
-# "CC=$(CC_FOR_TARGET)".  But, if the user invokes runtest directly,
-# these flags may not be set.
 if {![info exists CC]} {
-    set CC [find_gcc]
-}
-if {![info exists CC_FOR_HOST]} {
-    set CC_FOR_HOST $CC
+    set CC gcc
 }
 if {![info exists CFLAGS]} {
     set CFLAGS "-g -O2"
 }
+if {![info exists CC_FOR_TARGET]} {
+    set CC_FOR_TARGET [find_gcc]
+}
+if {![info exists CFLAGS_FOR_TARGET]} {
+    set CFLAGS_FOR_TARGET $CFLAGS
+}
 
 # load the utility procedures
 load_lib ctf-lib.exp
diff --git a/libctf/testsuite/lib/ctf-lib.exp b/libctf/testsuite/lib/ctf-lib.exp
index 0fb095829c6..f2908f78c7c 100644
--- a/libctf/testsuite/lib/ctf-lib.exp
+++ b/libctf/testsuite/lib/ctf-lib.exp
@@ -48,10 +48,10 @@  proc run_native_host_cmd { command } {
 
 # Compile and link a C source file for execution on the host.
 proc compile_link_one_host_cc { src output additional_args } {
-    global CC_FOR_HOST
+    global CC
     global CFLAGS
 
-    return [run_native_host_cmd "./libtool --quiet --tag=CC --mode=link $CC_FOR_HOST $CFLAGS $src -o $output $additional_args" ]
+    return [run_native_host_cmd "./libtool --quiet --tag=CC --mode=link $CC $CFLAGS $src -o $output $additional_args" ]
 }
 
 # run_lookup_test FILE
@@ -110,7 +110,7 @@  proc compile_link_one_host_cc { src output additional_args } {
 # regexps in FILE.d.
 #
 proc run_lookup_test { name } {
-    global CC CFLAGS LIBS
+    global CC_FOR_TARGET CFLAGS_FOR_TARGET LIBS
     global copyfile env runtests srcdir subdir verbose
 
     if ![runtest_file_p $runtests $name] then {
@@ -217,7 +217,7 @@  proc run_lookup_test { name } {
 	    }
 	}
 
-	set comp_output [run_host_cmd "$CC" "$CFLAGS $lookup_flags [concat $src] -o $lookup_output"]
+	set comp_output [run_host_cmd "$CC_FOR_TARGET" "$CFLAGS_FOR_TARGET $lookup_flags [concat $src] -o $lookup_output"]
 
 	if { $comp_output != ""} {
 	    send_log "compilation of CTF program [concat $src] failed with <$comp_output>"