$OpenBSD: patch-gcc_cp_g++spec_c,v 1.2 2008/07/25 20:50:26 martynas Exp $
--- gcc/cp/g++spec.c.orig	Tue Mar 11 02:59:25 2003
+++ gcc/cp/g++spec.c	Fri Jul 25 12:34:58 2008
@@ -37,11 +37,17 @@ Boston, MA 02111-1307, USA.  */
 #endif
 
 #ifndef LIBSTDCXX
-#define LIBSTDCXX "-lstdc++"
+#define LIBSTDCXX "-lestdc++"
 #endif
 #ifndef LIBSTDCXX_PROFILE
-#define LIBSTDCXX_PROFILE "-lstdc++"
+#define LIBSTDCXX_PROFILE "-lestdc++"
 #endif
+#ifndef LIBSUPCXX
+#define LIBSUPCXX "-lsupc++"
+#endif
+#ifndef LIBSUPCXX_PROFILE
+#define LIBSUPCXX_PROFILE "-lsupc++"
+#endif
 
 void
 lang_specific_driver (in_argc, in_argv, in_added_libraries)
@@ -61,6 +67,10 @@ lang_specific_driver (in_argc, in_argv, in_added_libra
      link in libstdc++.  */
   int library = 1;
 
+  /* This will be 1 if we encounter a situation where we should link
+     libsupc++. */
+  int libsupcxx = 0;
+
   /* The number of arguments being added to what's in argv, other than
      libraries.  We use this to track the number of times we've inserted
      -xc++/-xnone.  */
@@ -128,11 +138,18 @@ lang_specific_driver (in_argc, in_argv, in_added_libra
 
       if (argv[i][0] == '-')
 	{
-	  if (library != 0 && (strcmp (argv[i], "-nostdlib") == 0
+	  if ((strcmp (argv[i], "-nostdlib") == 0
 			       || strcmp (argv[i], "-nodefaultlibs") == 0))
 	    {
 	      library = 0;
+	      libsupcxx = -1;
 	    }
+	  else if (strcmp (argv[i], "-shared") == 0)
+	    {
+	      library = 0;
+	      if (libsupcxx == 0)
+	        libsupcxx = 1;
+	    }
 	  else if (strcmp (argv[i], "-lm") == 0
 		   || strcmp (argv[i], "-lmath") == 0
 		   || strcmp (argv[i], MATH_LIBRARY) == 0
@@ -146,6 +163,8 @@ lang_specific_driver (in_argc, in_argv, in_added_libra
 	    }
 	  else if (strcmp (argv[i], "-lc") == 0)
 	    args[i] |= WITHLIBC;
+	  else if (strcmp (argv[i], "-lestdc++") == 0)
+	    libsupcxx = -1;
 	  else if (strcmp (argv[i], "-pg") == 0 || strcmp (argv[i], "-p") == 0)
 	    saw_profile_flag++;
 	  else if (strcmp (argv[i], "-v") == 0)
@@ -269,6 +288,12 @@ lang_specific_driver (in_argc, in_argv, in_added_libra
       j++;
     }
 
+  /* Add -lsupc++ for shared. */
+  if (libsupcxx == 1)
+    {
+      arglist[j++] = saw_profile_flag ? LIBSUPCXX_PROFILE : LIBSUPCXX;
+      added_libraries++;
+    }
   /* Add `-lstdc++' if we haven't already done so.  */
   if (library)
     {
