--- old/usr/src/cmd/make/bin/main.cc	2015-05-20 12:11:52.612594228 -0400
+++ new/usr/src/cmd/make/bin/main.cc	2015-05-20 12:11:52.690610321 -0400
@@ -40,6 +40,7 @@
 
 
 #include <locale.h>		/* setlocale() */
+#include <libgen.h>
 #include <mk/defs.h>
 #include <mksdmsi18n/mksdmsi18n.h>	/* libmksdmsi18n_init() */
 #include <mksh/macro.h>		/* getvar() */
@@ -82,8 +83,13 @@
 #define	LD_SUPPORT_ENV_VAR_32	NOCATGETS("SGS_SUPPORT_32")
 #define	LD_SUPPORT_ENV_VAR_64	NOCATGETS("SGS_SUPPORT_64")
 #define	LD_SUPPORT_MAKE_LIB	NOCATGETS("libmakestate.so.1")
-#define	LD_SUPPORT_MAKE_LIB_DIR	NOCATGETS("/lib")
-#define	LD_SUPPORT_MAKE_LIB_DIR_64	NOCATGETS("/64")
+#ifdef __i386
+#define	LD_SUPPORT_MAKE_ARCH	NOCATGETS("i386")
+#elif __sparc
+#define	LD_SUPPORT_MAKE_ARCH	NOCATGETS("sparc")
+#else
+#error "Unsupported architecture"
+#endif
 
 /*
  * typedefs & structs
@@ -413,13 +419,27 @@
 	 */
     if ((!pmake_cap_r_specified) &&
         (!pmake_machinesfile_specified)) {
+	char *s = strdup(argv[0]);    
+	    
 	MBSTOWCS(wcs_buffer, NOCATGETS("DMAKE_MODE"));
 	dmake_name2 = GETNAME(wcs_buffer, FIND_LENGTH);
 	prop2 = get_prop(dmake_name2->prop, macro_prop);
-	if (prop2 == NULL) {
-		/* DMAKE_MODE not defined, default to parallel mode */
-		dmake_mode_type = parallel_mode;
-		no_parallel = false;
+	// If we're invoked as 'make' run serially, regardless of DMAKE_MODE
+	// If we're invoked as 'make' but passed -j, run parallel
+	// If we're invoked as 'dmake', without DMAKE_MODE, default parallel
+	// If we're invoked as 'dmake' and DMAKE_MODE is set, honour it.
+	if ((strcmp(basename(s), NOCATGETS("make")) == 0) &&
+	    !dmake_max_jobs_specified) {
+		dmake_mode_type = serial_mode;
+		no_parallel = true;
+	} else if (prop2 == NULL) {
+		/* DMAKE_MODE not defined, default based on our name */
+		char *s = strdup(argv[0]);
+
+		if (strcmp(basename(s), NOCATGETS("dmake")) == 0) {
+			dmake_mode_type = parallel_mode;
+			no_parallel = false;
+		}
 	} else {
 		dmake_value2 = prop2->body.macro.value;
 		if (IS_EQUAL(dmake_value2->string_mb, NOCATGETS("parallel"))) {
@@ -432,7 +452,7 @@
 			fatal(catgets(catd, 1, 307, "Unknown dmake mode argument `%s' after -m flag"), dmake_value2->string_mb);
 		}
 	}
-
+	free(s);
     }
 
 	parallel_flag = true;
@@ -1363,6 +1383,8 @@
 		if (invert_this) {
 			dmake_max_jobs_specified = false;
 		} else {
+			dmake_mode_type = parallel_mode;
+			no_parallel = false;
 			dmake_max_jobs_specified = true;
 		}
 		return 8;
@@ -1599,6 +1621,50 @@
 	}
 }
 
+char *
+make_install_prefix(void)
+{
+	int ret;
+	char origin[PATH_MAX];
+	char *dir;
+
+	if ((ret = readlink("/proc/self/path/a.out", origin,
+	    PATH_MAX - 1)) < 0)
+		fatal("failed to read origin from /proc\n");
+
+	
+	origin[ret] = '\0';
+	return strdup(dirname(origin));
+}
+
+static char *
+add_to_env(const char *var, const char *value, const char *fallback)
+{
+	const char *oldpath;
+	char *newpath;
+
+	oldpath = getenv(var);
+	if (oldpath == NULL) {
+		if (value != NULL) {
+			asprintf(&newpath, "%s=%s",
+			    var, value);
+		} else {
+			asprintf(&newpath, "%s=%s",
+			    var, fallback);
+		}
+	} else {
+		if (value != NULL) {
+			asprintf(&newpath, "%s=%s:%s",
+			    var, oldpath, value);
+		} else {
+			asprintf(&newpath, "%s=%s:%s",
+			    var, oldpath, fallback);			
+		}
+	}
+
+	return (newpath);
+}
+
 /*
  *	set_sgs_support()
  *
@@ -1618,57 +1684,44 @@
 {
 	int		len;
 	char		*newpath, *newpath64;
-	char		*oldpath, *oldpath64;
+	char 		*lib32, *lib64;
 	static char	*prev_path, *prev_path64;
+	char		*origin = make_install_prefix();
+	struct stat st;
 
-	oldpath = getenv(LD_SUPPORT_ENV_VAR_32);
-	if (oldpath == NULL) {
-		len = snprintf(NULL, 0, "%s=%s/%s/%s:%s",
-		    LD_SUPPORT_ENV_VAR_32,
-		    MAKE_PREFIX,
-		    LD_SUPPORT_MAKE_LIB_DIR,
-		    LD_SUPPORT_MAKE_LIB, LD_SUPPORT_MAKE_LIB) + 1;
-		newpath = (char *) malloc(len);
-		sprintf(newpath, "%s=%s/%s/%s:%s",
-		    LD_SUPPORT_ENV_VAR_32,
-		    MAKE_PREFIX,
-		    LD_SUPPORT_MAKE_LIB_DIR,
-		    LD_SUPPORT_MAKE_LIB, LD_SUPPORT_MAKE_LIB);
-	} else {
-		len = snprintf(NULL, 0, "%s=%s:%s/%s/%s:%s",
-		    LD_SUPPORT_ENV_VAR_32, oldpath, MAKE_PREFIX,
-		    LD_SUPPORT_MAKE_LIB_DIR, LD_SUPPORT_MAKE_LIB,
-		    LD_SUPPORT_MAKE_LIB) + 1;
-		newpath = (char *) malloc(len);
-		sprintf(newpath, "%s=%s:%s/%s/%s:%s",
-		    LD_SUPPORT_ENV_VAR_32, oldpath, MAKE_PREFIX,
-		    LD_SUPPORT_MAKE_LIB_DIR, LD_SUPPORT_MAKE_LIB,
-		    LD_SUPPORT_MAKE_LIB);
-	}
-
-	oldpath64 = getenv(LD_SUPPORT_ENV_VAR_64);
-	if (oldpath64 == NULL) {
-		len = snprintf(NULL, 0, "%s=%s/%s/%s/%s:%s",
-		    LD_SUPPORT_ENV_VAR_64, MAKE_PREFIX, LD_SUPPORT_MAKE_LIB_DIR,
-		    LD_SUPPORT_MAKE_LIB_DIR_64, LD_SUPPORT_MAKE_LIB,
-		    LD_SUPPORT_MAKE_LIB) + 1;
-		newpath64 = (char *) malloc(len);
-		sprintf(newpath64, "%s=%s/%s/%s/%s:%s",
-		    LD_SUPPORT_ENV_VAR_64, MAKE_PREFIX, LD_SUPPORT_MAKE_LIB_DIR,
-		    LD_SUPPORT_MAKE_LIB_DIR_64, LD_SUPPORT_MAKE_LIB,
-		    LD_SUPPORT_MAKE_LIB);
-	} else {
-		len = snprintf(NULL, 0, "%s=%s:%s/%s/%s/%s:%s",
-		    LD_SUPPORT_ENV_VAR_64, oldpath64, MAKE_PREFIX,
-		    LD_SUPPORT_MAKE_LIB_DIR, LD_SUPPORT_MAKE_LIB_DIR_64,
-		    LD_SUPPORT_MAKE_LIB, LD_SUPPORT_MAKE_LIB) + 1;
-		newpath64 = (char *) malloc(len);
-		sprintf(newpath64, "%s=%s:%s/%s/%s/%s:%s",
-		    LD_SUPPORT_ENV_VAR_64, oldpath64, MAKE_PREFIX,
-		    LD_SUPPORT_MAKE_LIB_DIR, LD_SUPPORT_MAKE_LIB_DIR_64,
-		    LD_SUPPORT_MAKE_LIB, LD_SUPPORT_MAKE_LIB);
+	asprintf(&lib32, "%s/%s/%s", origin, "../lib",
+	    LD_SUPPORT_MAKE_LIB);
+
+	if (stat(lib32, &st) != 0) {
+		free(lib32);
+		// Try the tools path
+		asprintf(&lib32, "%s/%s/%s/%s", origin, "../../lib/",
+		    LD_SUPPORT_MAKE_ARCH, LD_SUPPORT_MAKE_LIB);
+
+		if (stat(lib32, &st) != 0) {
+			free(lib32);
+			lib32 = NULL;
+		}
+	}
+
+	asprintf(&lib64, "%s/%s/64/%s", origin, "../lib",
+	    LD_SUPPORT_MAKE_LIB);
+
+	if (stat(lib64, &st) != 0) {
+		free(lib64);
+		// Try the tools path
+		asprintf(&lib64, "%s/%s/%s/64/%s", origin, "../../lib/",
+		    LD_SUPPORT_MAKE_ARCH, LD_SUPPORT_MAKE_LIB);
+
+		if (stat(lib64, &st) != 0) {
+			free(lib64);
+			lib64 = NULL;
+		}
 	}
 
+	newpath = add_to_env(LD_SUPPORT_ENV_VAR_32, lib32, LD_SUPPORT_MAKE_LIB);
+	newpath64 = add_to_env(LD_SUPPORT_ENV_VAR_64, lib64, LD_SUPPORT_MAKE_LIB);
+
 	putenv(newpath);
 	if (prev_path) {
 		free(prev_path);
@@ -1680,6 +1733,9 @@
 		free(prev_path64);
 	}
 	prev_path64 = newpath64;
+	free(lib32);
+	free(lib64);
+	free(origin);
 }
 
 /*
