diff --git a/gcc/c-family/c-opts.c b/gcc/c-family/c-opts.c
index aa6801fa811..b8269548bf3 100644
--- a/gcc/c-family/c-opts.c
+++ b/gcc/c-family/c-opts.c
@@ -41,6 +41,11 @@ along with GCC; see the file COPYING3.  If not see
 #include "mkdeps.h"
 #include "dumpfile.h"
 #include "file-prefix-map.h"    /* add_*_prefix_map()  */
+#include "filenames.h"
+
+#if defined (_WIN32)
+#include <windows.h>
+#endif
 
 #ifndef DOLLARS_IN_IDENTIFIERS
 # define DOLLARS_IN_IDENTIFIERS true
@@ -1291,10 +1296,53 @@ c_common_finish (void)
 	deps_stream = stdout;
       else
 	{
-	  deps_stream = fopen (deps_file, deps_append ? "a": "w");
-	  if (!deps_stream)
-	    fatal_error (input_location, "opening dependency file %s: %m",
-			 deps_file);
+	  #if defined (_WIN32)
+          size_t filelen;
+	  
+	  /* PR 25713: Handle extra long path names.
+	   * * For relative paths, convert them to absolute, in case that version is too long.  */
+	  
+	  if (! IS_ABSOLUTE_PATH (deps_file))
+	  {
+                char cwd[1024];
+
+                getcwd (cwd, sizeof (cwd));
+                filelen = strlen (cwd) + 1;
+                strncat (cwd, "\\", sizeof (cwd) - filelen);
+                ++ filelen;
+                strncat (cwd, deps_file, sizeof (cwd) - filelen);
+                deps_file = cwd;
+	  }
+	  
+	  filelen = strlen (deps_file) + 1;
+	  
+	  if (filelen > MAX_PATH - 1)
+	  {
+                char * fullpath;
+	       
+		fullpath = (char *) xmalloc (filelen + 8);
+
+                /* Add a Microsoft recommended prefix that will allow the extra-long path to work. */
+                strcpy (fullpath, "\\\\?\\");
+                strcat (fullpath, deps_file);
+
+                for (int i = 0; fullpath[i]; i++)
+                {
+                        if (IS_UNIX_DIR_SEPARATOR (fullpath[i]))
+				fullpath[i] = '\\';
+                }
+
+                deps_stream = fopen (fullpath, deps_append ? "a": "w");
+                free (fullpath);
+	  }
+	  else
+		  deps_stream = fopen (deps_file, deps_append ? "a": "w");
+	#else
+        deps_stream = fopen (deps_file, deps_append ? "a": "w");
+        #endif
+        if (!deps_stream)
+                fatal_error (input_location, "opening dependency file %s: %m",
+                                deps_file);
 	}
     }
 
