diff --git a/gcc/testsuite/g++.dg/ubsan/ubsan.exp b/gcc/testsuite/g++.dg/ubsan/ubsan.exp
index d7197070a92a1fe47557803b0e4e95331f0560d8..4bab1b83de965751f7d247d6dff220a84508cf44 100644
--- a/gcc/testsuite/g++.dg/ubsan/ubsan.exp
+++ b/gcc/testsuite/g++.dg/ubsan/ubsan.exp
@@ -22,7 +22,8 @@ load_lib ubsan-dg.exp
 
 # Initialize `dg'.
 dg-init
-ubsan_init
+# libubsan uses libstdc++ but we assume that's added by the g++ impl.
+ubsan_init 0
 
 # Main loop.
 if [check_effective_target_fsanitize_undefined] {
diff --git a/gcc/testsuite/gcc.dg/ubsan/ubsan.exp b/gcc/testsuite/gcc.dg/ubsan/ubsan.exp
index 84170495e2806ae30c900ee3727712dc07ae9de2..560e5843be65f968ea676c72566673097a08ef43 100644
--- a/gcc/testsuite/gcc.dg/ubsan/ubsan.exp
+++ b/gcc/testsuite/gcc.dg/ubsan/ubsan.exp
@@ -24,7 +24,8 @@ load_lib ubsan-dg.exp
 
 # Initialize `dg'.
 dg-init
-ubsan_init
+# libubsan uses libstdc++ so make sure we provide paths for it.
+ubsan_init 1
 
 # Main loop.
 if [check_effective_target_fsanitize_undefined] {
diff --git a/gcc/testsuite/gdc.dg/ubsan/ubsan.exp b/gcc/testsuite/gdc.dg/ubsan/ubsan.exp
index 6ad665a1a8d74356628f4b1c1929858d141443f7..7613a3b487c3c344c1035946b2bdfc8e114e5044 100644
--- a/gcc/testsuite/gdc.dg/ubsan/ubsan.exp
+++ b/gcc/testsuite/gdc.dg/ubsan/ubsan.exp
@@ -20,7 +20,8 @@ load_lib ubsan-dg.exp
 
 # Initialize `dg'.
 dg-init
-ubsan_init
+# libubsan uses libstdc++ so make sure we provide paths for it.
+ubsan_init 1
 
 # Main loop.
 if [check_effective_target_fsanitize_undefined] {
diff --git a/gcc/testsuite/gfortran.dg/ubsan/ubsan.exp b/gcc/testsuite/gfortran.dg/ubsan/ubsan.exp
index 0c61153e68b72bc758e1795a73a445745e41527c..b2360785e6c2342f5b91320d7906e30ac85a7958 100644
--- a/gcc/testsuite/gfortran.dg/ubsan/ubsan.exp
+++ b/gcc/testsuite/gfortran.dg/ubsan/ubsan.exp
@@ -22,10 +22,10 @@
 load_lib gfortran-dg.exp
 load_lib ubsan-dg.exp
 
-
 # Initialize `dg'.
 dg-init
-ubsan_init
+# libubsan uses libstdc++ so make sure we provide paths for it.
+ubsan_init 1
 
 # Main loop.
 if [check_effective_target_fsanitize_undefined] {
diff --git a/gcc/testsuite/lib/ubsan-dg.exp b/gcc/testsuite/lib/ubsan-dg.exp
index 108b9980cac7d38a65d8c07af94d1c0b341cafde..860e78f3975342b03cfa4e6e592338ce007f0053 100644
--- a/gcc/testsuite/lib/ubsan-dg.exp
+++ b/gcc/testsuite/lib/ubsan-dg.exp
@@ -31,7 +31,7 @@ proc check_effective_target_fsanitize_undefined {} {
 # (originally from g++.exp)
 #
 
-proc ubsan_link_flags { paths } {
+proc ubsan_link_flags { paths needs_cxx } {
     global srcdir
     global ld_library_path
     global shlib_ext
@@ -43,15 +43,24 @@ proc ubsan_link_flags { paths } {
     set shlib_ext [get_shlib_extension]
     set ubsan_saved_library_path $ld_library_path
 
+    # Providing -B instead of -L means that it works for targets that use
+    # spec substitution for handling -static-xxxxx, it also works for targets
+    # the use the startfile paths to provide a runpath for uninstalled test.
+    # Each -B option will produce a -L on the link line (for paths that exist).
     if { $gccpath != "" } {
       if { [file exists "${gccpath}/libsanitizer/ubsan/.libs/libubsan.a"]
 	   || [file exists "${gccpath}/libsanitizer/ubsan/.libs/libubsan.${shlib_ext}"] } {
 	  append flags " -B${gccpath}/libsanitizer/ "
 	  append flags " -B${gccpath}/libsanitizer/ubsan/ "
-	  append flags " -L${gccpath}/libsanitizer/ubsan/.libs"
+	  append flags " -B${gccpath}/libsanitizer/ubsan/.libs"
 	  append ld_library_path ":${gccpath}/libsanitizer/ubsan/.libs"
-	  append ld_library_path ":${gccpath}/libstdc++-v3/src/.libs"
       }
+      # libasan links to libstdc++, so we must include it for C testcases.
+      if { $needs_cxx && ( [file exists "${gccpath}/libstdc++-v3/src/.libs/libstdc++.a"]
+	   || [file exists "${gccpath}/libstdc++-v3/src/.libs/libstdc++.${shlib_ext}"] ) } {
+	append flags " -B${gccpath}/libstdc++-v3/src/.libs "
+	append ld_library_path ":${gccpath}/libstdc++-v3/src/.libs"
+      }      
     } else {
       global tool_root_dir
 
@@ -79,6 +88,7 @@ proc ubsan_init { args } {
     global ubsan_saved_ALWAYS_CXXFLAGS
     global orig_ubsan_options_saved
     global orig_ubsan_options
+    set needs_cxx [lindex $args 0]
 
     if { $orig_ubsan_options_saved == 0 } {
 	# Save the original environment.
@@ -92,9 +102,9 @@ proc ubsan_init { args } {
     set link_flags ""
     if ![is_remote host] {
 	if [info exists TOOL_OPTIONS] {
-	    set link_flags "[ubsan_link_flags [get_multilibs ${TOOL_OPTIONS}]]"
+	    set link_flags "[ubsan_link_flags [get_multilibs ${TOOL_OPTIONS}] $needs_cxx]"
 	} else {
-	    set link_flags "[ubsan_link_flags [get_multilibs]]"
+	    set link_flags "[ubsan_link_flags [get_multilibs] $needs_cxx]"
 	}
     }