diff options
Diffstat (limited to 'pr/src/linking/prlink.c')
-rw-r--r-- | pr/src/linking/prlink.c | 368 |
1 files changed, 202 insertions, 166 deletions
diff --git a/pr/src/linking/prlink.c b/pr/src/linking/prlink.c index 56eddf41..1f343071 100644 --- a/pr/src/linking/prlink.c +++ b/pr/src/linking/prlink.c @@ -120,8 +120,9 @@ static void DLLErrorInternal(PRIntn oserr) PR_snprintf(errStrBuf, sizeof(errStrBuf), "error %d", oserr); error = errStrBuf; #endif - if (NULL != error) + if (NULL != error) { PR_SetErrorText(strlen(error), error); + } } /* DLLErrorInternal */ void _PR_InitLinker(void) @@ -162,7 +163,7 @@ void _PR_InitLinker(void) error = (char*)PR_MALLOC(PR_GetErrorTextLength()); (void) PR_GetErrorText(error); fprintf(stderr, "failed to initialize shared libraries [%s]\n", - error); + error); PR_DELETE(error); abort();/* XXX */ } @@ -189,7 +190,7 @@ void _PR_InitLinker(void) if (lm) { PR_LOG(_pr_linker_lm, PR_LOG_MIN, - ("Loaded library %s (init)", lm->name)); + ("Loaded library %s (init)", lm->name)); } PR_ExitMonitor(pr_linker_lock); @@ -221,7 +222,9 @@ PR_IMPLEMENT(PRStatus) PR_SetLibraryPath(const char *path) { PRStatus rv = PR_SUCCESS; - if (!_pr_initialized) _PR_ImplicitInitialization(); + if (!_pr_initialized) { + _PR_ImplicitInitialization(); + } PR_EnterMonitor(pr_linker_lock); if (_pr_currentLibPath) { free(_pr_currentLibPath); @@ -230,7 +233,7 @@ PR_IMPLEMENT(PRStatus) PR_SetLibraryPath(const char *path) _pr_currentLibPath = strdup(path); if (!_pr_currentLibPath) { PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); - rv = PR_FAILURE; + rv = PR_FAILURE; } } else { _pr_currentLibPath = 0; @@ -248,7 +251,9 @@ PR_GetLibraryPath(void) char *ev; char *copy = NULL; /* a copy of _pr_currentLibPath */ - if (!_pr_initialized) _PR_ImplicitInitialization(); + if (!_pr_initialized) { + _PR_ImplicitInitialization(); + } PR_EnterMonitor(pr_linker_lock); if (_pr_currentLibPath != NULL) { goto exit; @@ -259,7 +264,7 @@ PR_GetLibraryPath(void) #ifdef XP_PC ev = getenv("LD_LIBRARY_PATH"); if (!ev) { - ev = ".;\\lib"; + ev = ".;\\lib"; } ev = strdup(ev); #endif @@ -267,21 +272,21 @@ PR_GetLibraryPath(void) #if defined(XP_UNIX) #if defined(USE_DLFCN) || defined(USE_MACH_DYLD) { - char *p=NULL; - int len; + char *p=NULL; + int len; - ev = getenv("LD_LIBRARY_PATH"); - if (!ev) { - ev = "/usr/lib:/lib"; - } - len = strlen(ev) + 1; /* +1 for the null */ + ev = getenv("LD_LIBRARY_PATH"); + if (!ev) { + ev = "/usr/lib:/lib"; + } + len = strlen(ev) + 1; /* +1 for the null */ - p = (char*) malloc(len); - if (p) { - strcpy(p, ev); - } /* if (p) */ - ev = p; - PR_LOG(_pr_io_lm, PR_LOG_NOTICE, ("linker path '%s'", ev)); + p = (char*) malloc(len); + if (p) { + strcpy(p, ev); + } /* if (p) */ + ev = p; + PR_LOG(_pr_io_lm, PR_LOG_NOTICE, ("linker path '%s'", ev)); } #else @@ -295,7 +300,7 @@ PR_GetLibraryPath(void) */ _pr_currentLibPath = ev; - exit: +exit: if (_pr_currentLibPath) { copy = strdup(_pr_currentLibPath); } @@ -365,25 +370,25 @@ pr_UnlockedFindLibrary(const char *name) const char* np = strrchr(name, PR_DIRECTORY_SEPARATOR); np = np ? np + 1 : name; while (lm) { - const char* cp = strrchr(lm->name, PR_DIRECTORY_SEPARATOR); - cp = cp ? cp + 1 : lm->name; + const char* cp = strrchr(lm->name, PR_DIRECTORY_SEPARATOR); + cp = cp ? cp + 1 : lm->name; #ifdef WIN32 /* Windows DLL names are case insensitive... */ - if (strcmpi(np, cp) == 0) + if (strcmpi(np, cp) == 0) #elif defined(XP_OS2) - if (stricmp(np, cp) == 0) + if (stricmp(np, cp) == 0) #else - if (strcmp(np, cp) == 0) + if (strcmp(np, cp) == 0) #endif - { - /* found */ - lm->refCount++; - PR_LOG(_pr_linker_lm, PR_LOG_MIN, - ("%s incr => %d (find lib)", - lm->name, lm->refCount)); - return lm; - } - lm = lm->next; + { + /* found */ + lm->refCount++; + PR_LOG(_pr_linker_lm, PR_LOG_MIN, + ("%s incr => %d (find lib)", + lm->name, lm->refCount)); + return lm; + } + lm = lm->next; } return NULL; } @@ -430,7 +435,7 @@ pr_LoadMachDyldModule(const char *name) NSObjectFileImage ofi; NSModule h = NULL; if (NSCreateObjectFileImageFromFile(name, &ofi) - == NSObjectFileImageSuccess) { + == NSObjectFileImageSuccess) { h = NSLinkModule(ofi, name, NSLINKMODULE_OPTION_PRIVATE | NSLINKMODULE_OPTION_RETURN_ON_ERROR); if (h == NULL) { @@ -522,18 +527,21 @@ pr_LoadViaCFM(const char *name, PRLibrary *lm) /* Use direct conversion of POSIX path to FSRef to FSSpec. */ err = FSPathMakeRef((const UInt8*)name, &ref, NULL); - if (err != noErr) + if (err != noErr) { return PR_FAILURE; + } err = FSGetCatalogInfo(&ref, kFSCatInfoNone, NULL, NULL, &fileSpec, NULL); - if (err != noErr) + if (err != noErr) { return PR_FAILURE; + } /* Resolve an alias if this was one */ err = ResolveAliasFile(&fileSpec, true, &tempUnusedBool, &tempUnusedBool); - if (err != noErr) + if (err != noErr) { return PR_FAILURE; + } /* Finally, try to load the library */ err = GetDiskFragment(&fileSpec, 0, kCFragGoesToEOF, fileSpec.name, @@ -550,8 +558,9 @@ pr_LoadViaCFM(const char *name, PRLibrary *lm) &kCFTypeDictionaryValueCallBacks); if (lm->wrappers) { lm->main = TV2FP(lm->wrappers, "main", lm->main); - } else + } else { err = memFullErr; + } } return (err == noErr) ? PR_SUCCESS : PR_FAILURE; } @@ -574,13 +583,14 @@ pr_LoadCFBundle(const char *name, PRLibrary *lm) /* Takes care of relative paths and symlinks */ resolvedPath = realpath(name, pathBuf); - if (!resolvedPath) + if (!resolvedPath) { return PR_FAILURE; + } pathRef = CFStringCreateWithCString(NULL, pathBuf, kCFStringEncodingUTF8); if (pathRef) { bundleURL = CFURLCreateWithFileSystemPath(NULL, pathRef, - kCFURLPOSIXPathStyle, true); + kCFURLPOSIXPathStyle, true); if (bundleURL) { bundle = CFBundleCreate(NULL, bundleURL); CFRelease(bundleURL); @@ -635,7 +645,9 @@ pr_LoadLibraryByPathname(const char *name, PRIntn flags) int len; #endif - if (!_pr_initialized) _PR_ImplicitInitialization(); + if (!_pr_initialized) { + _PR_ImplicitInitialization(); + } /* See if library is already loaded */ PR_EnterMonitor(pr_linker_lock); @@ -646,8 +658,9 @@ pr_LoadLibraryByPathname(const char *name, PRIntn flags) wname = (LPWSTR) name; } else { int wlen = MultiByteToWideChar(CP_ACP, 0, name, -1, NULL, 0); - if (wlen > MAX_PATH) + if (wlen > MAX_PATH) { wname = wname_malloc = PR_Malloc(wlen * sizeof(PRUnichar)); + } if (wname == NULL || !MultiByteToWideChar(CP_ACP, 0, name, -1, wname, wlen)) { oserr = _MD_ERRNO(); @@ -655,8 +668,9 @@ pr_LoadLibraryByPathname(const char *name, PRIntn flags) } } len = WideCharToMultiByte(CP_UTF8, 0, wname, -1, NULL, 0, NULL, NULL); - if (len > MAX_PATH) + if (len > MAX_PATH) { utf8name = utf8name_malloc = PR_Malloc(len); + } if (utf8name == NULL || !WideCharToMultiByte(CP_UTF8, 0, wname, -1, utf8name, len, NULL, NULL)) { @@ -670,7 +684,9 @@ pr_LoadLibraryByPathname(const char *name, PRIntn flags) result = pr_UnlockedFindLibrary(name); #endif - if (result != NULL) goto unlock; + if (result != NULL) { + goto unlock; + } lm = PR_NEWZAP(PRLibrary); if (lm == NULL) { @@ -685,63 +701,64 @@ pr_LoadLibraryByPathname(const char *name, PRIntn flags) UCHAR pszError[_MAX_PATH]; ULONG ulRc = NO_ERROR; - ulRc = DosLoadModule(pszError, _MAX_PATH, (PSZ) name, &h); - if (ulRc != NO_ERROR) { - oserr = ulRc; - PR_DELETE(lm); - goto unlock; - } - lm->name = strdup(name); - lm->dlh = h; - lm->next = pr_loadmap; - pr_loadmap = lm; + ulRc = DosLoadModule(pszError, _MAX_PATH, (PSZ) name, &h); + if (ulRc != NO_ERROR) { + oserr = ulRc; + PR_DELETE(lm); + goto unlock; + } + lm->name = strdup(name); + lm->dlh = h; + lm->next = pr_loadmap; + pr_loadmap = lm; } #endif /* XP_OS2 */ #ifdef WIN32 { - HINSTANCE h; + HINSTANCE h; - h = LoadLibraryExW(wname, NULL, - (flags & PR_LD_ALT_SEARCH_PATH) ? - LOAD_WITH_ALTERED_SEARCH_PATH : 0); - if (h == NULL) { - oserr = _MD_ERRNO(); - PR_DELETE(lm); - goto unlock; - } - lm->name = strdup(utf8name); - lm->dlh = h; - lm->next = pr_loadmap; - pr_loadmap = lm; + h = LoadLibraryExW(wname, NULL, + (flags & PR_LD_ALT_SEARCH_PATH) ? + LOAD_WITH_ALTERED_SEARCH_PATH : 0); + if (h == NULL) { + oserr = _MD_ERRNO(); + PR_DELETE(lm); + goto unlock; + } + lm->name = strdup(utf8name); + lm->dlh = h; + lm->next = pr_loadmap; + pr_loadmap = lm; } #endif /* WIN32 */ #if defined(XP_MACOSX) && defined(USE_MACH_DYLD) { - int i; - PRStatus status; + int i; + PRStatus status; - static const macLibraryLoadProc loadProcs[] = { + static const macLibraryLoadProc loadProcs[] = { #ifdef __ppc__ - pr_LoadViaDyld, pr_LoadCFBundle, pr_LoadViaCFM + pr_LoadViaDyld, pr_LoadCFBundle, pr_LoadViaCFM #else /* __ppc__ */ - pr_LoadViaDyld, pr_LoadCFBundle + pr_LoadViaDyld, pr_LoadCFBundle #endif /* __ppc__ */ - }; + }; - for (i = 0; i < sizeof(loadProcs) / sizeof(loadProcs[0]); i++) { - if ((status = loadProcs[i](name, lm)) == PR_SUCCESS) - break; - } - if (status != PR_SUCCESS) { - oserr = cfragNoLibraryErr; - PR_DELETE(lm); - goto unlock; - } - lm->name = strdup(name); - lm->next = pr_loadmap; - pr_loadmap = lm; + for (i = 0; i < sizeof(loadProcs) / sizeof(loadProcs[0]); i++) { + if ((status = loadProcs[i](name, lm)) == PR_SUCCESS) { + break; + } + } + if (status != PR_SUCCESS) { + oserr = cfragNoLibraryErr; + PR_DELETE(lm); + goto unlock; + } + lm->name = strdup(name); + lm->next = pr_loadmap; + pr_loadmap = lm; } #endif @@ -750,73 +767,73 @@ pr_LoadLibraryByPathname(const char *name, PRIntn flags) { #if defined(USE_DLFCN) #ifdef NTO - /* Neutrino needs RTLD_GROUP to load Netscape plugins. (bug 71179) */ - int dl_flags = RTLD_GROUP; + /* Neutrino needs RTLD_GROUP to load Netscape plugins. (bug 71179) */ + int dl_flags = RTLD_GROUP; #elif defined(AIX) - /* AIX needs RTLD_MEMBER to load an archive member. (bug 228899) */ - int dl_flags = RTLD_MEMBER; + /* AIX needs RTLD_MEMBER to load an archive member. (bug 228899) */ + int dl_flags = RTLD_MEMBER; #else - int dl_flags = 0; + int dl_flags = 0; #endif - void *h = NULL; + void *h = NULL; - if (flags & PR_LD_LAZY) { - dl_flags |= RTLD_LAZY; - } - if (flags & PR_LD_NOW) { - dl_flags |= RTLD_NOW; - } - if (flags & PR_LD_GLOBAL) { - dl_flags |= RTLD_GLOBAL; - } - if (flags & PR_LD_LOCAL) { - dl_flags |= RTLD_LOCAL; - } + if (flags & PR_LD_LAZY) { + dl_flags |= RTLD_LAZY; + } + if (flags & PR_LD_NOW) { + dl_flags |= RTLD_NOW; + } + if (flags & PR_LD_GLOBAL) { + dl_flags |= RTLD_GLOBAL; + } + if (flags & PR_LD_LOCAL) { + dl_flags |= RTLD_LOCAL; + } #if defined(DARWIN) - /* ensure the file exists if it contains a slash character i.e. path */ - /* DARWIN's dlopen ignores the provided path and checks for the */ - /* plain filename in DYLD_LIBRARY_PATH */ - if (strchr(name, PR_DIRECTORY_SEPARATOR) == NULL || - PR_Access(name, PR_ACCESS_EXISTS) == PR_SUCCESS) { + /* ensure the file exists if it contains a slash character i.e. path */ + /* DARWIN's dlopen ignores the provided path and checks for the */ + /* plain filename in DYLD_LIBRARY_PATH */ + if (strchr(name, PR_DIRECTORY_SEPARATOR) == NULL || + PR_Access(name, PR_ACCESS_EXISTS) == PR_SUCCESS) { h = dlopen(name, dl_flags); } #else - h = dlopen(name, dl_flags); + h = dlopen(name, dl_flags); #endif #elif defined(USE_HPSHL) - int shl_flags = 0; - shl_t h; + int shl_flags = 0; + shl_t h; - /* - * Use the DYNAMIC_PATH flag only if 'name' is a plain file - * name (containing no directory) to match the behavior of - * dlopen(). - */ - if (strchr(name, PR_DIRECTORY_SEPARATOR) == NULL) { - shl_flags |= DYNAMIC_PATH; - } - if (flags & PR_LD_LAZY) { - shl_flags |= BIND_DEFERRED; - } - if (flags & PR_LD_NOW) { - shl_flags |= BIND_IMMEDIATE; - } - /* No equivalent of PR_LD_GLOBAL and PR_LD_LOCAL. */ - h = shl_load(name, shl_flags, 0L); + /* + * Use the DYNAMIC_PATH flag only if 'name' is a plain file + * name (containing no directory) to match the behavior of + * dlopen(). + */ + if (strchr(name, PR_DIRECTORY_SEPARATOR) == NULL) { + shl_flags |= DYNAMIC_PATH; + } + if (flags & PR_LD_LAZY) { + shl_flags |= BIND_DEFERRED; + } + if (flags & PR_LD_NOW) { + shl_flags |= BIND_IMMEDIATE; + } + /* No equivalent of PR_LD_GLOBAL and PR_LD_LOCAL. */ + h = shl_load(name, shl_flags, 0L); #elif defined(USE_MACH_DYLD) - NSModule h = pr_LoadMachDyldModule(name); + NSModule h = pr_LoadMachDyldModule(name); #else #error Configuration error #endif - if (!h) { - oserr = _MD_ERRNO(); - PR_DELETE(lm); - goto unlock; - } - lm->name = strdup(name); - lm->dlh = h; - lm->next = pr_loadmap; - pr_loadmap = lm; + if (!h) { + oserr = _MD_ERRNO(); + PR_DELETE(lm); + goto unlock; + } + lm->name = strdup(name); + lm->dlh = h; + lm->next = pr_loadmap; + pr_loadmap = lm; } #endif /* HAVE_DLL */ #endif /* XP_UNIX && !(XP_MACOSX && USE_MACH_DYLD) */ @@ -826,16 +843,18 @@ pr_LoadLibraryByPathname(const char *name, PRIntn flags) result = lm; /* success */ PR_LOG(_pr_linker_lm, PR_LOG_MIN, ("Loaded library %s (load lib)", lm->name)); - unlock: +unlock: if (result == NULL) { PR_SetError(PR_LOAD_LIBRARY_ERROR, oserr); DLLErrorInternal(oserr); /* sets error text */ } #ifdef WIN32 - if (utf8name_malloc) + if (utf8name_malloc) { PR_Free(utf8name_malloc); - if (wname_malloc) + } + if (wname_malloc) { PR_Free(wname_malloc); + } #endif PR_ExitMonitor(pr_linker_lock); return result; @@ -864,10 +883,10 @@ PR_UnloadLibrary(PRLibrary *lib) } if (--lib->refCount > 0) { - PR_LOG(_pr_linker_lm, PR_LOG_MIN, - ("%s decr => %d", - lib->name, lib->refCount)); - goto done; + PR_LOG(_pr_linker_lm, PR_LOG_MIN, + ("%s decr => %d", + lib->name, lib->refCount)); + goto done; } #ifdef XP_UNIX @@ -877,8 +896,9 @@ PR_UnloadLibrary(PRLibrary *lib) #elif defined(USE_HPSHL) result = shl_unload(lib->dlh); #elif defined(USE_MACH_DYLD) - if (lib->dlh) + if (lib->dlh) { result = NSUnLinkModule(lib->dlh, NSUNLINKMODULE_OPTION_NONE) ? 0 : -1; + } #else #error Configuration error #endif @@ -893,18 +913,22 @@ PR_UnloadLibrary(PRLibrary *lib) #if defined(XP_MACOSX) && defined(USE_MACH_DYLD) /* Close the connection */ - if (lib->connection) + if (lib->connection) { CloseConnection(&(lib->connection)); - if (lib->bundle) + } + if (lib->bundle) { CFRelease(lib->bundle); - if (lib->wrappers) + } + if (lib->wrappers) { CFRelease(lib->wrappers); + } /* No way to unload an image (lib->image) */ #endif /* unlink from library search list */ - if (pr_loadmap == lib) + if (pr_loadmap == lib) { pr_loadmap = pr_loadmap->next; + } else if (pr_loadmap != NULL) { PRLibrary* prev = pr_loadmap; PRLibrary* next = pr_loadmap->next; @@ -931,7 +955,7 @@ PR_UnloadLibrary(PRLibrary *lib) * succeeds or not. */ - freeLib: +freeLib: PR_LOG(_pr_linker_lm, PR_LOG_MIN, ("Unloaded library %s", lib->name)); free(lib->name); lib->name = NULL; @@ -991,7 +1015,7 @@ pr_FindSymbolInLib(PRLibrary *lm, const char *name) #endif /* WIN32 */ #if defined(XP_MACOSX) && defined(USE_MACH_DYLD) -/* add this offset to skip the leading underscore in name */ + /* add this offset to skip the leading underscore in name */ #define SYM_OFFSET 1 if (lm->bundle) { CFStringRef nameRef = CFStringCreateWithCString(NULL, name + SYM_OFFSET, kCFStringEncodingASCII); @@ -1018,17 +1042,21 @@ pr_FindSymbolInLib(PRLibrary *lm, const char *name) } #endif /* __ppc__ */ - if (f == NULL && strcmp(name + SYM_OFFSET, "main") == 0) f = lm->main; + if (f == NULL && strcmp(name + SYM_OFFSET, "main") == 0) { + f = lm->main; + } } if (lm->image) { NSSymbol symbol; symbol = NSLookupSymbolInImage(lm->image, name, - NSLOOKUPSYMBOLINIMAGE_OPTION_BIND - | NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR); - if (symbol != NULL) + NSLOOKUPSYMBOLINIMAGE_OPTION_BIND + | NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR); + if (symbol != NULL) { f = NSAddressOfSymbol(symbol); - else + } + else { f = NULL; + } } #undef SYM_OFFSET #endif /* XP_MACOSX && USE_MACH_DYLD */ @@ -1045,10 +1073,12 @@ pr_FindSymbolInLib(PRLibrary *lm, const char *name) if (lm->dlh) { NSSymbol symbol; symbol = NSLookupSymbolInModule(lm->dlh, name); - if (symbol != NULL) + if (symbol != NULL) { f = NSAddressOfSymbol(symbol); - else + } + else { f = NULL; + } } #endif #endif /* HAVE_DLL */ @@ -1121,7 +1151,9 @@ PR_FindSymbolAndLibrary(const char *raw_name, PRLibrary* *lib) #endif PRLibrary* lm; - if (!_pr_initialized) _PR_ImplicitInitialization(); + if (!_pr_initialized) { + _PR_ImplicitInitialization(); + } /* ** Mangle the raw symbol name in any way that is platform specific. */ @@ -1148,7 +1180,7 @@ PR_FindSymbolAndLibrary(const char *raw_name, PRLibrary* *lib) *lib = lm; lm->refCount++; PR_LOG(_pr_linker_lm, PR_LOG_MIN, - ("%s incr => %d (for %s)", + ("%s incr => %d (for %s)", lm->name, lm->refCount, name)); break; } @@ -1177,7 +1209,9 @@ PR_LoadStaticLibrary(const char *name, const PRStaticLinkTable *slt) PRLibrary *lm=NULL; PRLibrary* result = NULL; - if (!_pr_initialized) _PR_ImplicitInitialization(); + if (!_pr_initialized) { + _PR_ImplicitInitialization(); + } /* See if library is already loaded */ PR_EnterMonitor(pr_linker_lock); @@ -1192,7 +1226,9 @@ PR_LoadStaticLibrary(const char *name, const PRStaticLinkTable *slt) /* Add library to list...Mark it static */ lm = PR_NEWZAP(PRLibrary); - if (lm == NULL) goto unlock; + if (lm == NULL) { + goto unlock; + } lm->name = strdup(name); lm->refCount = 1; @@ -1204,7 +1240,7 @@ PR_LoadStaticLibrary(const char *name, const PRStaticLinkTable *slt) result = lm; /* success */ PR_ASSERT(lm->refCount == 1); PR_LOG(_pr_linker_lm, PR_LOG_MIN, ("Loaded library %s (static lib)", lm->name)); - unlock: +unlock: PR_ExitMonitor(pr_linker_lock); return result; } @@ -1326,7 +1362,7 @@ PR_GetLibraryFilePathname(const char *name, PRFuncPtr addr) */ for (index--; index >= 0; index--) { if ((shl_get_r(index, &desc) == 0) - && (strstr(desc.filename, name) != NULL)) { + && (strstr(desc.filename, name) != NULL)) { result = PR_Malloc(strlen(desc.filename)+1); if (result != NULL) { strcpy(result, desc.filename); |