--- old/usr/src/cmd/mdb/common/mdb/mdb_demangle.c	Tue Jun  5 20:45:28 2018
+++ new/usr/src/cmd/mdb/common/mdb/mdb_demangle.c	Tue Jun  5 20:45:28 2018
@@ -229,7 +229,9 @@
 	}
 
 	if (prefixlen > 0)
-		(void) strlcpy(dmp->dm_buf, name, prefixlen);
+		(void) strlcpy(dmp->dm_buf, name, prefixlen + 1);
+	else
+		*dmp->dm_buf = '\0';
 
 	if (res != NULL) {
 		(void) strlcat(dmp->dm_buf, res, dmp->dm_len);
@@ -327,6 +329,12 @@
 	if ((flags & DCMD_ADDRSPEC) || argc == 0)
 		return (DCMD_USAGE);
 
+	if (mdb.m_demangler == NULL && (mdb.m_demangler =
+	    mdb_dem_load()) == NULL) {
+		mdb_warn("failed to load demangler");
+		return (DCMD_ERR);
+	}
+
 	for (; argc != 0; argc--, argv++) {
 		mdb_printf("%s == %s\n", argv->a_un.a_str,
 		    mdb_dem_convert(mdb.m_demangler, argv->a_un.a_str));
--- old/usr/src/cmd/sgs/dump/common/dump.c	Tue Jun  5 20:45:28 2018
+++ new/usr/src/cmd/sgs/dump/common/dump.c	Tue Jun  5 20:45:28 2018
@@ -478,10 +478,10 @@
 {
 	static char	*buf = NULL;
 	size_t		buflen = 0;
-	const char	*dn;
+	char		*dn;
 	size_t		len;
 
-	dn = conv_demangle_name(s);
+	dn = (char *)conv_demangle_name(s);
 
 	/*
 	 * If not demangled, just return the symbol name
--- old/usr/src/cmd/sgs/gprof/common/printgprof.c	Tue Jun  5 20:45:28 2018
+++ new/usr/src/cmd/sgs/gprof/common/printgprof.c	Tue Jun  5 20:45:28 2018
@@ -528,7 +528,7 @@
 void
 print_demangled_name(int n, nltype *selfp)
 {
-	char *c = demangled_name(selfp);
+	char *c = (char *)demangled_name(selfp);
 	int i;
 
 	if (c == selfp->name)
@@ -539,7 +539,7 @@
 		(void) printf(" ");
 	(void) printf("[%s]", selfp->name);
 
-	free((void *)c);
+	free(c);
 }
 
 void
@@ -1040,13 +1040,16 @@
 				if (does_clash(namesortnlp, i, nnames)) {
 					(void) printf("%6.6s %d:%s\n",
 					    peterbuffer, nlp->module->id, d);
-				} else
+				} else {
 					(void) printf("%6.6s %s\n", peterbuffer,
 					    d);
+				}
 
-				if (d != nlp->name)
+				if (d != nlp->name) {
 					(void) printf("%6.6s   [%s]", "",
 					    nlp->name);
+					free((void *)d);
+				}
 			} else {
 				(void) printf("%6.6s ", peterbuffer);
 				(void) sprintf(peterbuffer, "<cycle %d>",
--- old/usr/src/cmd/sgs/libconv/Makefile.com	Tue Jun  5 20:45:28 2018
+++ new/usr/src/cmd/sgs/libconv/Makefile.com	Tue Jun  5 20:45:28 2018
@@ -21,6 +21,7 @@
 
 #
 # Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
+# Copyright 2018, Joyent, Inc.
 #
 
 LIBRARY =	libconv.a
@@ -113,6 +114,14 @@
 LINTSRCS32 =	$(COMOBJS32:%32.o=../common/%.c)
 LINTSRCS64 =	$(COMOBJS64:%64.o=../common/%.c)
 
+# Since libconv uses dlopen(3C) to load libdemangle-sys.so (much like it did
+# for the old Sun Studio libdemangle.so) in order to avoid messy bootstrapping
+# problems, but it also needs the definitions from demangle-sys.h for
+# SYSDEM_LANG_AUTO, lint will complain about sysdemangle() being defined but not
+# used unless it is explicitly included during the lint pass
+$(LINTOUT32)	:= LDLIBS += -ldemangle-sys
+$(LINTOUT64)	:= LDLIBS += -ldemangle-sys
+
 SGSMSGTARG=	$(BLTOBJS:%_msg.o=../common/%.msg)
 
 LINTFLAGS +=	-u
--- old/usr/src/cmd/sgs/libconv/common/lintsup.c	Tue Jun  5 20:45:28 2018
+++ new/usr/src/cmd/sgs/libconv/common/lintsup.c	Tue Jun  5 20:45:28 2018
@@ -21,6 +21,7 @@
 
 /*
  * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
+ * Copyright 2018, Joyent, Inc.
  */
 /* LINTLIBRARY */
 /* PROTOLIB1 */
@@ -39,7 +40,6 @@
 #include <sys/auxv.h>
 #include <libelf.h>
 #include <link.h>
-#include <demangle.h>
 #include <elfcap.h>
 #include <dwarf.h>
 #include "sgs.h"
--- old/usr/src/pkg/manifests/system-library-demangle.mf	Tue Jun  5 20:45:29 2018
+++ new/usr/src/pkg/manifests/system-library-demangle.mf	Tue Jun  5 20:45:29 2018
@@ -34,8 +34,8 @@
 file path=usr/lib/$(ARCH64)/libdemangle-sys.so.1
 file path=usr/lib/$(ARCH64)/llib-ldemangle-sys.ln
 file path=usr/lib/libdemangle-sys.so.1
-file path=usr/lib/llib-ldemangle_sys
-file path=usr/lib/llib-ldemangle_sys.ln
+file path=usr/lib/llib-ldemangle-sys
+file path=usr/lib/llib-ldemangle-sys.ln
 license lic_CDDL license=lic_CDDL
 license usr/src/lib/libdemangle/THIRDPARTYLICENSE \
     license=usr/src/lib/libdemangle/THIRDPARTYLICENSE
--- old/usr/src/pkg/manifests/system-test-utiltest.mf	Tue Jun  5 20:45:29 2018
+++ new/usr/src/pkg/manifests/system-test-utiltest.mf	Tue Jun  5 20:45:29 2018
@@ -283,7 +283,7 @@
 file path=opt/util-tests/tests/set-linkprop mode=0555
 file path=opt/util-tests/tests/xargs_test mode=0555
 license lic_CDDL license=lic_CDDL
-license usr/src/lib/libsysdemangle/THIRDPARTYLICENSE \
-    license=usr/src/lib/libsysdemangle/THIRDPARTYLICENSE
+license usr/src/lib/libdemangle/THIRDPARTYLICENSE \
+    license=usr/src/lib/libdemangle/THIRDPARTYLICENSE
 depend fmri=system/library/iconv/utf-8 type=require
 depend fmri=system/test/testrunner type=require
