diff --git a/gcc/testsuite/gfortran.dg/coarray/caf.exp b/gcc/testsuite/gfortran.dg/coarray/caf.exp
index dae46bd92fa..31c13cd34e5 100644
--- a/gcc/testsuite/gfortran.dg/coarray/caf.exp
+++ b/gcc/testsuite/gfortran.dg/coarray/caf.exp
@@ -70,18 +70,6 @@ proc dg-compile-aux-modules { args } {
     }
 }
 
-# Add -latomic only where supported.  Assume built-in support elsewhere.
-set maybe_atomic_lib ""
-if [check_effective_target_libatomic_available] {
-    if ![is_remote host] {
-	if [info exists TOOL_OPTIONS] {
-	    set maybe_atomic_lib "[atomic_link_flags [get_multilibs ${TOOL_OPTIONS}]]"
-	} else {
-	    set maybe_atomic_lib "[atomic_link_flags [get_multilibs]]"
-	}
-    }
-}
-
 # Main loop.
 foreach test [lsort [glob -nocomplain $srcdir/$subdir/*.\[fF\]{,90,95,03,08} ]] {
     # If we're only testing specific files and this isn't one of them, skip it.
@@ -105,14 +93,14 @@ foreach test [lsort [glob -nocomplain $srcdir/$subdir/*.\[fF\]{,90,95,03,08} ]]
     foreach flags $option_list {
 	verbose "Testing $nshort (single), $flags" 1
         set gfortran_aux_module_flags "-fcoarray=single $flags"
-	dg-test $test "-fcoarray=single $flags" $maybe_atomic_lib
+	dg-test $test "-fcoarray=single $flags" {}
 	cleanup-modules ""
     }
 
     foreach flags $option_list {
 	verbose "Testing $nshort (libcaf_single), $flags" 1
         set gfortran_aux_module_flags "-fcoarray=lib $flags -lcaf_single"
-	dg-test $test "-fcoarray=lib $flags -lcaf_single" $maybe_atomic_lib
+	dg-test $test "-fcoarray=lib $flags -lcaf_single" {}
 	cleanup-modules ""
     }
 }
diff --git a/gcc/testsuite/gfortran.dg/dg.exp b/gcc/testsuite/gfortran.dg/dg.exp
index f936fd38644..7a9cb89c194 100644
--- a/gcc/testsuite/gfortran.dg/dg.exp
+++ b/gcc/testsuite/gfortran.dg/dg.exp
@@ -54,27 +54,7 @@ proc dg-compile-aux-modules { args } {
     }
 }
 
-# coarray tests might need libatomic.  Assume that it is either not needed or
-# provided by builtins if it's not available.
-set maybe_atomic_lib ""
-if [check_effective_target_libatomic_available] {
-    if ![is_remote host] {
-	if [info exists TOOL_OPTIONS] {
-	    set maybe_atomic_lib "[atomic_link_flags [get_multilibs ${TOOL_OPTIONS}]]"
-	} else {
-	    set maybe_atomic_lib "[atomic_link_flags [get_multilibs]]"
-	}
-    } else {
-        set maybe_atomic_lib ""
-    }
-}
-
 set all_flags $DEFAULT_FFLAGS
-if { $maybe_atomic_lib != "" } {
-   foreach f $maybe_atomic_lib {
-     lappend all_flags $f
-   }
-}
 
 # Main loop.
 gfortran-dg-runtest [lsort \
diff --git a/gcc/testsuite/lib/gfortran.exp b/gcc/testsuite/lib/gfortran.exp
index c3e258b410b..1ccb81ccec5 100644
--- a/gcc/testsuite/lib/gfortran.exp
+++ b/gcc/testsuite/lib/gfortran.exp
@@ -79,6 +79,7 @@ proc gfortran_link_flags { paths } {
     global ld_library_path
     global GFORTRAN_UNDER_TEST
     global shlib_ext
+    global ENABLE_DARWIN_AT_RPATH
 
     set gccpath ${paths}
     set libio_dir ""
@@ -87,39 +88,63 @@ proc gfortran_link_flags { paths } {
     set shlib_ext [get_shlib_extension]
     verbose "shared lib extension: $shlib_ext"
 
+    # We need to add options to locate libgfortran and the dependent libs
+    # libquadmath (supporting REAL*16) and libatomic (supporting operations
+    # used by coarrays).  Usually '-L' options are added to point to the
+    # relevant directories for the uninstalled libraries.
+
+    # In cases where libraries are available as both shared and convenience
+    # some additional checks are made.
+
+    # For some targets -static-xxxx options are handled by specs substitution
+    # and need a '-B' option rather than '-L'.  For Darwin, when embedded
+    # runpaths are in use (the default for all versions after macOS 10.11),
+    # '-B' is also needed to provide the runpath.
+    # When '-B' is used, this results in a '-L' for each path that exists (so
+    # that appending a '-L' as well is a needless duplicate).  There are also
+    # cases where tools warn for duplicates, leading to spurious fails.
+    # Therefore the objective of the code below is to add just one '-L' or
+    # '-B' for each of the libraries.
+
+    set target_wants_B_option 0
+    if { [istarget *-*-darwin9* ] || [istarget *-*-darwin\[12\]* ] } {
+      set target_wants_B_option 1
+    }
+
     if { $gccpath != "" } {
-      if [file exists "${gccpath}/libgfortran/.libs/libgfortran.a"] {
-          # Some targets use libgfortran.a%s in their specs, so they need a -B option
-          # for uninstalled testing.
-          append flags "-B${gccpath}/libgfortran/.libs "
-          append flags "-L${gccpath}/libgfortran/.libs "
-          append ld_library_path ":${gccpath}/libgfortran/.libs"
-      }
-      if [file exists "${gccpath}/libgfortran/.libs/libgfortran.${shlib_ext}"] {
-	  append flags "-L${gccpath}/libgfortran/.libs "
-	  append ld_library_path ":${gccpath}/libgfortran/.libs"
-      }
       if [file exists "${gccpath}/libgfortran/libgforbegin.a"] {
           append flags "-L${gccpath}/libgfortran "
       }
-      if [file exists "${gccpath}/libatomic/.libs/libatomic.${shlib_ext}"] {
-	  append flags "-L${gccpath}/libatomic/.libs "
-	  append ld_library_path ":${gccpath}/libatomic/.libs"
+      if { [file exists "${gccpath}/libgfortran/.libs/libgfortran.a"] ||
+	 [file exists "${gccpath}/libgfortran/.libs/libgfortran.${shlib_ext}"] } {
+	if { $target_wants_B_option } {
+          append flags "-B${gccpath}/libgfortran/.libs "
+	} else {
+          append flags "-L${gccpath}/libgfortran/.libs "
+	}
+	append ld_library_path ":${gccpath}/libgfortran/.libs"
       }
-      if [file exists "${gccpath}/libatomic/libatomic.a"] {
-          append flags "-L${gccpath}/libatomic "
+
+      if { [file exists "${gccpath}/libatomic/.libs/libatomic.a"] ||
+	 [file exists "${gccpath}/libatomic/.libs/libatomic.${shlib_ext}"] } {
+	if { $target_wants_B_option } {
+          append flags "-B${gccpath}/libatomic/.libs "
+	} else {
+          append flags "-L${gccpath}/libatomic/.libs "
+	}
+	append ld_library_path ":${gccpath}/libatomic/.libs"
       }
-      if [file exists "${gccpath}/libquadmath/.libs/libquadmath.a"] {
-          # Some targets use libquadmath.a%s in their specs, so they need a -B option
-          # for uninstalled testing.
+
+      if { [file exists "${gccpath}/libquadmath/.libs/libquadmath.a"] ||
+	 [file exists "${gccpath}/libquadmath/.libs/libquadmath.${shlib_ext}"] } {
+	if { $target_wants_B_option } {
           append flags "-B${gccpath}/libquadmath/.libs "
+	} else {
           append flags "-L${gccpath}/libquadmath/.libs "
-          append ld_library_path ":${gccpath}/libquadmath/.libs"
-      }
-      if [file exists "${gccpath}/libquadmath/.libs/libquadmath.${shlib_ext}"] {
-	  append flags "-L${gccpath}/libquadmath/.libs "
-	  append ld_library_path ":${gccpath}/libquadmath/.libs"
+	}
+	append ld_library_path ":${gccpath}/libquadmath/.libs"
       }
+
       if [file exists "${gccpath}/libiberty/libiberty.a"] {
           append flags "-L${gccpath}/libiberty "
       }
