--- gcc-4.8.5/libiberty/make-relative-prefix.c.orig	2016-06-20 10:08:24.002962840 +0100
+++ gcc-4.8.5/libiberty/make-relative-prefix.c	2016-06-20 10:53:47.359700416 +0100
@@ -198,6 +198,49 @@
   return dirs;
 }
 
+/* Replace dir, ../ or dir, ..\ with nothing.  */
+
+static char **
+normalize_split_directories (char **dirs, int *ptr_num_dirs)
+{
+  int r = 0;
+  int w = 0;
+  
+  if (dirs != NULL)
+    {
+      while (dirs[r] != NULL)
+	{
+	  if (w > 0 && ((strcmp (dirs[r], "../") == 0)
+		     || (strcmp (dirs[r], "..\\") == 0)))
+	    {
+	      --w;
+	      free (dirs[w]);
+	      dirs[w] = NULL;
+	    }
+	  else
+	    {
+	      dirs[w] = dirs[r];
+	      ++w;
+	    }
+	  r++;
+	}
+      if (w == 0)
+	{
+	  free ((char *) dirs);
+	  dirs = NULL;
+	  if (ptr_num_dirs)
+	    *ptr_num_dirs = 0;
+	}
+      else
+	{
+	  if (ptr_num_dirs)
+	    *ptr_num_dirs = w;
+	  dirs[w] = NULL;
+	}
+    }
+  return dirs;
+}
+
 /* Release storage held by split directories.  */
 
 static void
@@ -315,11 +358,13 @@
     return NULL;
 
   prog_dirs = split_directories (full_progname, &prog_num);
+  prog_dirs = normalize_split_directories (prog_dirs, &prog_num);
   free (full_progname);
   if (prog_dirs == NULL)
     return NULL;
 
   bin_dirs = split_directories (bin_prefix, &bin_num);
+  bin_dirs = normalize_split_directories (bin_dirs, &bin_num);
   if (bin_dirs == NULL)
     goto bailout;
 
@@ -343,6 +388,7 @@
     }
 
   prefix_dirs = split_directories (prefix, &prefix_num);
+  prefix_dirs = normalize_split_directories (prefix_dirs, &prefix_num);
   if (prefix_dirs == NULL)
     goto bailout;
 
