1diff --git a/nss/cmd/shlibsign/shlibsign.c b/nss/cmd/shlibsign/shlibsign.c
2index 63a4836..a128c1d 100644
3--- a/nss/cmd/shlibsign/shlibsign.c
4+++ b/nss/cmd/shlibsign/shlibsign.c
5@@ -862,6 +862,8 @@ int main(int argc, char **argv)
6 libname = PR_GetLibraryName(NULL, "softokn3");
7 assert(libname != NULL);
8 lib = PR_LoadLibrary(libname);
9+ if (!lib)
10+ lib = PR_LoadLibrary(NIX_NSS_LIBDIR"libsoftokn3.so");
11 assert(lib != NULL);
12 PR_FreeLibraryName(libname);
13
14diff --git a/nss/coreconf/config.mk b/nss/coreconf/config.mk
15index 61d757b..b58a98b 100644
16--- a/nss/coreconf/config.mk
17+++ b/nss/coreconf/config.mk
18@@ -205,3 +205,6 @@ $(error Setting NSS_ENABLE_TLS_1_3 and NSS_DISABLE_ECC isn't a good idea.)
19 endif
20 DEFINES += -DNSS_ENABLE_TLS_1_3
21 endif
22+
23+# Nix specific stuff.
24+DEFINES += -DNIX_NSS_LIBDIR=\"$(out)/lib/\"
25diff --git a/nss/lib/pk11wrap/pk11load.c b/nss/lib/pk11wrap/pk11load.c
26index 5c5d2ca..026e528 100644
27--- a/nss/lib/pk11wrap/pk11load.c
28+++ b/nss/lib/pk11wrap/pk11load.c
29@@ -429,6 +429,13 @@ secmod_LoadPKCS11Module(SECMODModule *mod, SECMODModule **oldModule) {
30 * unload the library if anything goes wrong from here on out...
31 */
32 library = PR_LoadLibrary(mod->dllName);
33+ if ((library == NULL) &&
34+ !rindex(mod->dllName, PR_GetDirectorySeparator())) {
35+ library = PORT_LoadLibraryFromOrigin(my_shlib_name,
36+ (PRFuncPtr) &softoken_LoadDSO,
37+ mod->dllName);
38+ }
39+
40 mod->library = (void *)library;
41
42 if (library == NULL) {
43diff --git a/nss/lib/util/secload.c b/nss/lib/util/secload.c
44index eb8a9ec..f94f67d 100644
45--- a/nss/lib/util/secload.c
46+++ b/nss/lib/util/secload.c
47@@ -69,9 +69,14 @@ loader_LoadLibInReferenceDir(const char *referencePath, const char *name)
48
49 /* Remove the trailing filename from referencePath and add the new one */
50 c = strrchr(referencePath, PR_GetDirectorySeparator());
51+ if (!c) { /* referencePath doesn't contain a / means that dladdr gave us argv[0]
52+ * and program was called from $PATH. Hack to get libs from NIX_NSS_LIBDIR */
53+ referencePath = NIX_NSS_LIBDIR;
54+ c = (char*) &referencePath[sizeof(NIX_NSS_LIBDIR) - 1]; /* last / */
55+ }
56 if (c) {
57 size_t referencePathSize = 1 + c - referencePath;
58- fullName = (char*) PORT_Alloc(strlen(name) + referencePathSize + 1);
59+ fullName = (char*) PORT_Alloc(strlen(name) + referencePathSize + 5);
60 if (fullName) {
61 memcpy(fullName, referencePath, referencePathSize);
62 strcpy(fullName + referencePathSize, name);
63@@ -81,6 +86,11 @@ loader_LoadLibInReferenceDir(const char *referencePath, const char *name)
64 #endif
65 libSpec.type = PR_LibSpec_Pathname;
66 libSpec.value.pathname = fullName;
67+ if ((referencePathSize >= 4) &&
68+ (strncmp(fullName + referencePathSize - 4, "bin", 3) == 0)) {
69+ memcpy(fullName + referencePathSize -4, "lib", 3);
70+ }
71+ strcpy(fullName + referencePathSize, name);
72 dlh = PR_LoadLibraryWithFlags(libSpec, PR_LD_NOW | PR_LD_LOCAL
73 #ifdef PR_LD_ALT_SEARCH_PATH
74 /* allow library's dependencies to be found in the same directory
75@@ -88,6 +98,10 @@ loader_LoadLibInReferenceDir(const char *referencePath, const char *name)
76 | PR_LD_ALT_SEARCH_PATH
77 #endif
78 );
79+ if (! dlh) {
80+ strcpy(fullName + referencePathSize, name);
81+ dlh = PR_LoadLibraryWithFlags(libSpec, PR_LD_NOW | PR_LD_LOCAL);
82+ }
83 PORT_Free(fullName);
84 }
85 }