michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "primpl.h" michael@0: michael@0: #include michael@0: michael@0: #ifdef XP_BEOS michael@0: #include michael@0: #endif michael@0: michael@0: #if defined(XP_MACOSX) && defined(USE_MACH_DYLD) michael@0: #include michael@0: #include michael@0: #endif michael@0: michael@0: #ifdef XP_UNIX michael@0: #ifdef USE_DLFCN michael@0: #include michael@0: /* Define these on systems that don't have them. */ michael@0: #ifndef RTLD_NOW michael@0: #define RTLD_NOW 0 michael@0: #endif michael@0: #ifndef RTLD_LAZY michael@0: #define RTLD_LAZY RTLD_NOW michael@0: #endif michael@0: #ifndef RTLD_GLOBAL michael@0: #define RTLD_GLOBAL 0 michael@0: #endif michael@0: #ifndef RTLD_LOCAL michael@0: #define RTLD_LOCAL 0 michael@0: #endif michael@0: #ifdef AIX michael@0: #include michael@0: #ifndef L_IGNOREUNLOAD /* AIX 4.3.3 does not have L_IGNOREUNLOAD. */ michael@0: #define L_IGNOREUNLOAD 0x10000000 michael@0: #endif michael@0: #endif michael@0: #ifdef OSF1 michael@0: #include michael@0: #include michael@0: #endif michael@0: #elif defined(USE_HPSHL) michael@0: #include michael@0: #elif defined(USE_MACH_DYLD) michael@0: #include michael@0: #endif michael@0: #endif /* XP_UNIX */ michael@0: michael@0: #define _PR_DEFAULT_LD_FLAGS PR_LD_LAZY michael@0: michael@0: /* michael@0: * On these platforms, symbols have a leading '_'. michael@0: */ michael@0: #if (defined(DARWIN) && defined(USE_MACH_DYLD)) \ michael@0: || defined(XP_OS2) \ michael@0: || ((defined(OPENBSD) || defined(NETBSD)) && !defined(__ELF__)) michael@0: #define NEED_LEADING_UNDERSCORE michael@0: #endif michael@0: michael@0: #define PR_LD_PATHW 0x8000 /* for PR_LibSpec_PathnameU */ michael@0: michael@0: /************************************************************************/ michael@0: michael@0: struct PRLibrary { michael@0: char* name; /* Our own copy of the name string */ michael@0: PRLibrary* next; michael@0: int refCount; michael@0: const PRStaticLinkTable* staticTable; michael@0: michael@0: #ifdef XP_PC michael@0: #ifdef XP_OS2 michael@0: HMODULE dlh; michael@0: #else michael@0: HINSTANCE dlh; michael@0: #endif michael@0: #endif michael@0: michael@0: #if defined(XP_MACOSX) && defined(USE_MACH_DYLD) michael@0: CFragConnectionID connection; michael@0: CFBundleRef bundle; michael@0: Ptr main; michael@0: CFMutableDictionaryRef wrappers; michael@0: const struct mach_header* image; michael@0: #endif michael@0: michael@0: #ifdef XP_UNIX michael@0: #if defined(USE_HPSHL) michael@0: shl_t dlh; michael@0: #elif defined(USE_MACH_DYLD) michael@0: NSModule dlh; michael@0: #else michael@0: void* dlh; michael@0: #endif michael@0: #endif michael@0: michael@0: #ifdef XP_BEOS michael@0: void* dlh; michael@0: void* stub_dlh; michael@0: #endif michael@0: }; michael@0: michael@0: static PRLibrary *pr_loadmap; michael@0: static PRLibrary *pr_exe_loadmap; michael@0: static PRMonitor *pr_linker_lock; michael@0: static char* _pr_currentLibPath = NULL; michael@0: michael@0: static PRLibrary *pr_LoadLibraryByPathname(const char *name, PRIntn flags); michael@0: michael@0: /************************************************************************/ michael@0: michael@0: #if !defined(USE_DLFCN) && !defined(HAVE_STRERROR) michael@0: #define ERR_STR_BUF_LENGTH 20 michael@0: #endif michael@0: michael@0: static void DLLErrorInternal(PRIntn oserr) michael@0: /* michael@0: ** This whole function, and most of the code in this file, are run michael@0: ** with a big hairy lock wrapped around it. Not the best of situations, michael@0: ** but will eventually come up with the right answer. michael@0: */ michael@0: { michael@0: const char *error = NULL; michael@0: #ifdef USE_DLFCN michael@0: error = dlerror(); /* $$$ That'll be wrong some of the time - AOF */ michael@0: #elif defined(HAVE_STRERROR) michael@0: error = strerror(oserr); /* this should be okay */ michael@0: #else michael@0: char errStrBuf[ERR_STR_BUF_LENGTH]; michael@0: PR_snprintf(errStrBuf, sizeof(errStrBuf), "error %d", oserr); michael@0: error = errStrBuf; michael@0: #endif michael@0: if (NULL != error) michael@0: PR_SetErrorText(strlen(error), error); michael@0: } /* DLLErrorInternal */ michael@0: michael@0: void _PR_InitLinker(void) michael@0: { michael@0: PRLibrary *lm = NULL; michael@0: #if defined(XP_UNIX) michael@0: void *h; michael@0: #endif michael@0: michael@0: if (!pr_linker_lock) { michael@0: pr_linker_lock = PR_NewNamedMonitor("linker-lock"); michael@0: } michael@0: PR_EnterMonitor(pr_linker_lock); michael@0: michael@0: #if defined(XP_PC) michael@0: lm = PR_NEWZAP(PRLibrary); michael@0: lm->name = strdup("Executable"); michael@0: #if defined(XP_OS2) michael@0: lm->dlh = NULLHANDLE; michael@0: #else michael@0: /* A module handle for the executable. */ michael@0: lm->dlh = GetModuleHandle(NULL); michael@0: #endif /* ! XP_OS2 */ michael@0: michael@0: lm->refCount = 1; michael@0: lm->staticTable = NULL; michael@0: pr_exe_loadmap = lm; michael@0: pr_loadmap = lm; michael@0: michael@0: #elif defined(XP_UNIX) michael@0: #ifdef HAVE_DLL michael@0: #if defined(USE_DLFCN) && !defined(NO_DLOPEN_NULL) michael@0: h = dlopen(0, RTLD_LAZY); michael@0: if (!h) { michael@0: char *error; michael@0: michael@0: DLLErrorInternal(_MD_ERRNO()); michael@0: error = (char*)PR_MALLOC(PR_GetErrorTextLength()); michael@0: (void) PR_GetErrorText(error); michael@0: fprintf(stderr, "failed to initialize shared libraries [%s]\n", michael@0: error); michael@0: PR_DELETE(error); michael@0: abort();/* XXX */ michael@0: } michael@0: #elif defined(USE_HPSHL) michael@0: h = NULL; michael@0: /* don't abort with this NULL */ michael@0: #elif defined(USE_MACH_DYLD) || defined(NO_DLOPEN_NULL) michael@0: h = NULL; /* XXXX toshok */ /* XXXX vlad */ michael@0: #else michael@0: #error no dll strategy michael@0: #endif /* USE_DLFCN */ michael@0: michael@0: lm = PR_NEWZAP(PRLibrary); michael@0: if (lm) { michael@0: lm->name = strdup("a.out"); michael@0: lm->refCount = 1; michael@0: lm->dlh = h; michael@0: lm->staticTable = NULL; michael@0: } michael@0: pr_exe_loadmap = lm; michael@0: pr_loadmap = lm; michael@0: #endif /* HAVE_DLL */ michael@0: #endif /* XP_UNIX */ michael@0: michael@0: if (lm) { michael@0: PR_LOG(_pr_linker_lm, PR_LOG_MIN, michael@0: ("Loaded library %s (init)", lm->name)); michael@0: } michael@0: michael@0: PR_ExitMonitor(pr_linker_lock); michael@0: } michael@0: michael@0: /* michael@0: * _PR_ShutdownLinker does not unload the dlls loaded by the application michael@0: * via calls to PR_LoadLibrary. Any dlls that still remain on the michael@0: * pr_loadmap list when NSPR shuts down are application programming errors. michael@0: * The only exception is pr_exe_loadmap, which was added to the list by michael@0: * NSPR and hence should be cleaned up by NSPR. michael@0: */ michael@0: void _PR_ShutdownLinker(void) michael@0: { michael@0: /* FIXME: pr_exe_loadmap should be destroyed. */ michael@0: michael@0: PR_DestroyMonitor(pr_linker_lock); michael@0: pr_linker_lock = NULL; michael@0: michael@0: if (_pr_currentLibPath) { michael@0: free(_pr_currentLibPath); michael@0: _pr_currentLibPath = NULL; michael@0: } michael@0: } michael@0: michael@0: /******************************************************************************/ michael@0: michael@0: PR_IMPLEMENT(PRStatus) PR_SetLibraryPath(const char *path) michael@0: { michael@0: PRStatus rv = PR_SUCCESS; michael@0: michael@0: if (!_pr_initialized) _PR_ImplicitInitialization(); michael@0: PR_EnterMonitor(pr_linker_lock); michael@0: if (_pr_currentLibPath) { michael@0: free(_pr_currentLibPath); michael@0: } michael@0: if (path) { michael@0: _pr_currentLibPath = strdup(path); michael@0: if (!_pr_currentLibPath) { michael@0: PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); michael@0: rv = PR_FAILURE; michael@0: } michael@0: } else { michael@0: _pr_currentLibPath = 0; michael@0: } michael@0: PR_ExitMonitor(pr_linker_lock); michael@0: return rv; michael@0: } michael@0: michael@0: /* michael@0: ** Return the library path for finding shared libraries. michael@0: */ michael@0: PR_IMPLEMENT(char *) michael@0: PR_GetLibraryPath(void) michael@0: { michael@0: char *ev; michael@0: char *copy = NULL; /* a copy of _pr_currentLibPath */ michael@0: michael@0: if (!_pr_initialized) _PR_ImplicitInitialization(); michael@0: PR_EnterMonitor(pr_linker_lock); michael@0: if (_pr_currentLibPath != NULL) { michael@0: goto exit; michael@0: } michael@0: michael@0: /* initialize pr_currentLibPath */ michael@0: michael@0: #ifdef XP_PC michael@0: ev = getenv("LD_LIBRARY_PATH"); michael@0: if (!ev) { michael@0: ev = ".;\\lib"; michael@0: } michael@0: ev = strdup(ev); michael@0: #endif michael@0: michael@0: #if defined(XP_UNIX) || defined(XP_BEOS) michael@0: #if defined(USE_DLFCN) || defined(USE_MACH_DYLD) || defined(XP_BEOS) michael@0: { michael@0: char *p=NULL; michael@0: int len; michael@0: michael@0: #ifdef XP_BEOS michael@0: ev = getenv("LIBRARY_PATH"); michael@0: if (!ev) { michael@0: ev = "%A/lib:/boot/home/config/lib:/boot/beos/system/lib"; michael@0: } michael@0: #else michael@0: ev = getenv("LD_LIBRARY_PATH"); michael@0: if (!ev) { michael@0: ev = "/usr/lib:/lib"; michael@0: } michael@0: #endif michael@0: len = strlen(ev) + 1; /* +1 for the null */ michael@0: michael@0: p = (char*) malloc(len); michael@0: if (p) { michael@0: strcpy(p, ev); michael@0: } /* if (p) */ michael@0: ev = p; michael@0: PR_LOG(_pr_io_lm, PR_LOG_NOTICE, ("linker path '%s'", ev)); michael@0: michael@0: } michael@0: #else michael@0: /* AFAIK there isn't a library path with the HP SHL interface --Rob */ michael@0: ev = strdup(""); michael@0: #endif michael@0: #endif michael@0: michael@0: /* michael@0: * If ev is NULL, we have run out of memory michael@0: */ michael@0: _pr_currentLibPath = ev; michael@0: michael@0: exit: michael@0: if (_pr_currentLibPath) { michael@0: copy = strdup(_pr_currentLibPath); michael@0: } michael@0: PR_ExitMonitor(pr_linker_lock); michael@0: if (!copy) { michael@0: PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); michael@0: } michael@0: return copy; michael@0: } michael@0: michael@0: /* michael@0: ** Build library name from path, lib and extensions michael@0: */ michael@0: PR_IMPLEMENT(char*) michael@0: PR_GetLibraryName(const char *path, const char *lib) michael@0: { michael@0: char *fullname; michael@0: michael@0: #ifdef XP_PC michael@0: if (strstr(lib, PR_DLL_SUFFIX) == NULL) michael@0: { michael@0: if (path) { michael@0: fullname = PR_smprintf("%s\\%s%s", path, lib, PR_DLL_SUFFIX); michael@0: } else { michael@0: fullname = PR_smprintf("%s%s", lib, PR_DLL_SUFFIX); michael@0: } michael@0: } else { michael@0: if (path) { michael@0: fullname = PR_smprintf("%s\\%s", path, lib); michael@0: } else { michael@0: fullname = PR_smprintf("%s", lib); michael@0: } michael@0: } michael@0: #endif /* XP_PC */ michael@0: #if defined(XP_UNIX) || defined(XP_BEOS) michael@0: if (strstr(lib, PR_DLL_SUFFIX) == NULL) michael@0: { michael@0: if (path) { michael@0: fullname = PR_smprintf("%s/lib%s%s", path, lib, PR_DLL_SUFFIX); michael@0: } else { michael@0: fullname = PR_smprintf("lib%s%s", lib, PR_DLL_SUFFIX); michael@0: } michael@0: } else { michael@0: if (path) { michael@0: fullname = PR_smprintf("%s/%s", path, lib); michael@0: } else { michael@0: fullname = PR_smprintf("%s", lib); michael@0: } michael@0: } michael@0: #endif /* XP_UNIX || XP_BEOS */ michael@0: return fullname; michael@0: } michael@0: michael@0: /* michael@0: ** Free the memory allocated, for the caller, by PR_GetLibraryName michael@0: */ michael@0: PR_IMPLEMENT(void) michael@0: PR_FreeLibraryName(char *mem) michael@0: { michael@0: PR_smprintf_free(mem); michael@0: } michael@0: michael@0: static PRLibrary* michael@0: pr_UnlockedFindLibrary(const char *name) michael@0: { michael@0: PRLibrary* lm = pr_loadmap; michael@0: const char* np = strrchr(name, PR_DIRECTORY_SEPARATOR); michael@0: np = np ? np + 1 : name; michael@0: while (lm) { michael@0: const char* cp = strrchr(lm->name, PR_DIRECTORY_SEPARATOR); michael@0: cp = cp ? cp + 1 : lm->name; michael@0: #ifdef WIN32 michael@0: /* Windows DLL names are case insensitive... */ michael@0: if (strcmpi(np, cp) == 0) michael@0: #elif defined(XP_OS2) michael@0: if (stricmp(np, cp) == 0) michael@0: #else michael@0: if (strcmp(np, cp) == 0) michael@0: #endif michael@0: { michael@0: /* found */ michael@0: lm->refCount++; michael@0: PR_LOG(_pr_linker_lm, PR_LOG_MIN, michael@0: ("%s incr => %d (find lib)", michael@0: lm->name, lm->refCount)); michael@0: return lm; michael@0: } michael@0: lm = lm->next; michael@0: } michael@0: return NULL; michael@0: } michael@0: michael@0: PR_IMPLEMENT(PRLibrary*) michael@0: PR_LoadLibraryWithFlags(PRLibSpec libSpec, PRIntn flags) michael@0: { michael@0: if (flags == 0) { michael@0: flags = _PR_DEFAULT_LD_FLAGS; michael@0: } michael@0: switch (libSpec.type) { michael@0: case PR_LibSpec_Pathname: michael@0: return pr_LoadLibraryByPathname(libSpec.value.pathname, flags); michael@0: #ifdef WIN32 michael@0: case PR_LibSpec_PathnameU: michael@0: /* michael@0: * cast to |char *| and set PR_LD_PATHW flag so that michael@0: * it can be cast back to PRUnichar* in the callee. michael@0: */ michael@0: return pr_LoadLibraryByPathname((const char*) michael@0: libSpec.value.pathname_u, michael@0: flags | PR_LD_PATHW); michael@0: #endif michael@0: default: michael@0: PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); michael@0: return NULL; michael@0: } michael@0: } michael@0: michael@0: PR_IMPLEMENT(PRLibrary*) michael@0: PR_LoadLibrary(const char *name) michael@0: { michael@0: PRLibSpec libSpec; michael@0: michael@0: libSpec.type = PR_LibSpec_Pathname; michael@0: libSpec.value.pathname = name; michael@0: return PR_LoadLibraryWithFlags(libSpec, 0); michael@0: } michael@0: michael@0: #if defined(USE_MACH_DYLD) michael@0: static NSModule michael@0: pr_LoadMachDyldModule(const char *name) michael@0: { michael@0: NSObjectFileImage ofi; michael@0: NSModule h = NULL; michael@0: if (NSCreateObjectFileImageFromFile(name, &ofi) michael@0: == NSObjectFileImageSuccess) { michael@0: h = NSLinkModule(ofi, name, NSLINKMODULE_OPTION_PRIVATE michael@0: | NSLINKMODULE_OPTION_RETURN_ON_ERROR); michael@0: if (h == NULL) { michael@0: NSLinkEditErrors linkEditError; michael@0: int errorNum; michael@0: const char *fileName; michael@0: const char *errorString; michael@0: NSLinkEditError(&linkEditError, &errorNum, &fileName, &errorString); michael@0: PR_LOG(_pr_linker_lm, PR_LOG_MIN, michael@0: ("LoadMachDyldModule error %d:%d for file %s:\n%s", michael@0: linkEditError, errorNum, fileName, errorString)); michael@0: } michael@0: if (NSDestroyObjectFileImage(ofi) == FALSE) { michael@0: if (h) { michael@0: (void)NSUnLinkModule(h, NSUNLINKMODULE_OPTION_NONE); michael@0: h = NULL; michael@0: } michael@0: } michael@0: } michael@0: return h; michael@0: } michael@0: #endif michael@0: michael@0: #if defined(XP_MACOSX) && defined(USE_MACH_DYLD) michael@0: michael@0: /* michael@0: ** macLibraryLoadProc is a function definition for a Mac shared library michael@0: ** loading method. The "name" param is the same full or partial pathname michael@0: ** that was passed to pr_LoadLibraryByPathName. The function must fill michael@0: ** in the fields of "lm" which apply to its library type. Returns michael@0: ** PR_SUCCESS if successful. michael@0: */ michael@0: michael@0: typedef PRStatus (*macLibraryLoadProc)(const char *name, PRLibrary *lm); michael@0: michael@0: #ifdef __ppc__ michael@0: michael@0: /* michael@0: ** CFM and its TVectors only exist on PowerPC. Other OS X architectures michael@0: ** only use Mach-O as a native binary format. michael@0: */ michael@0: michael@0: static void* TV2FP(CFMutableDictionaryRef dict, const char* name, void *tvp) michael@0: { michael@0: static uint32 glue[6] = { 0x3D800000, 0x618C0000, 0x800C0000, 0x804C0004, 0x7C0903A6, 0x4E800420 }; michael@0: uint32* newGlue = NULL; michael@0: michael@0: if (tvp != NULL) { michael@0: CFStringRef nameRef = CFStringCreateWithCString(NULL, name, kCFStringEncodingASCII); michael@0: if (nameRef) { michael@0: CFMutableDataRef glueData = (CFMutableDataRef) CFDictionaryGetValue(dict, nameRef); michael@0: if (glueData == NULL) { michael@0: glueData = CFDataCreateMutable(NULL, sizeof(glue)); michael@0: if (glueData != NULL) { michael@0: newGlue = (uint32*) CFDataGetMutableBytePtr(glueData); michael@0: memcpy(newGlue, glue, sizeof(glue)); michael@0: newGlue[0] |= ((UInt32)tvp >> 16); michael@0: newGlue[1] |= ((UInt32)tvp & 0xFFFF); michael@0: MakeDataExecutable(newGlue, sizeof(glue)); michael@0: CFDictionaryAddValue(dict, nameRef, glueData); michael@0: CFRelease(glueData); michael@0: michael@0: PR_LOG(_pr_linker_lm, PR_LOG_MIN, ("TV2FP: created wrapper for CFM function %s().", name)); michael@0: } michael@0: } else { michael@0: PR_LOG(_pr_linker_lm, PR_LOG_MIN, ("TV2FP: found wrapper for CFM function %s().", name)); michael@0: michael@0: newGlue = (uint32*) CFDataGetMutableBytePtr(glueData); michael@0: } michael@0: CFRelease(nameRef); michael@0: } michael@0: } michael@0: michael@0: return newGlue; michael@0: } michael@0: michael@0: static PRStatus michael@0: pr_LoadViaCFM(const char *name, PRLibrary *lm) michael@0: { michael@0: OSErr err; michael@0: Str255 errName; michael@0: FSRef ref; michael@0: FSSpec fileSpec; michael@0: Boolean tempUnusedBool; michael@0: michael@0: /* michael@0: * Make an FSSpec from the path name and call GetDiskFragment. michael@0: */ michael@0: michael@0: /* Use direct conversion of POSIX path to FSRef to FSSpec. */ michael@0: err = FSPathMakeRef((const UInt8*)name, &ref, NULL); michael@0: if (err != noErr) michael@0: return PR_FAILURE; michael@0: err = FSGetCatalogInfo(&ref, kFSCatInfoNone, NULL, NULL, michael@0: &fileSpec, NULL); michael@0: if (err != noErr) michael@0: return PR_FAILURE; michael@0: michael@0: /* Resolve an alias if this was one */ michael@0: err = ResolveAliasFile(&fileSpec, true, &tempUnusedBool, michael@0: &tempUnusedBool); michael@0: if (err != noErr) michael@0: return PR_FAILURE; michael@0: michael@0: /* Finally, try to load the library */ michael@0: err = GetDiskFragment(&fileSpec, 0, kCFragGoesToEOF, fileSpec.name, michael@0: kLoadCFrag, &lm->connection, &lm->main, errName); michael@0: michael@0: if (err == noErr && lm->connection) { michael@0: /* michael@0: * if we're a mach-o binary, need to wrap all CFM function michael@0: * pointers. need a hash-table of already seen function michael@0: * pointers, etc. michael@0: */ michael@0: lm->wrappers = CFDictionaryCreateMutable(NULL, 16, michael@0: &kCFTypeDictionaryKeyCallBacks, michael@0: &kCFTypeDictionaryValueCallBacks); michael@0: if (lm->wrappers) { michael@0: lm->main = TV2FP(lm->wrappers, "main", lm->main); michael@0: } else michael@0: err = memFullErr; michael@0: } michael@0: return (err == noErr) ? PR_SUCCESS : PR_FAILURE; michael@0: } michael@0: #endif /* __ppc__ */ michael@0: michael@0: /* michael@0: ** Creates a CFBundleRef if the pathname refers to a Mac OS X bundle michael@0: ** directory. The caller is responsible for calling CFRelease() to michael@0: ** deallocate. michael@0: */ michael@0: michael@0: static PRStatus michael@0: pr_LoadCFBundle(const char *name, PRLibrary *lm) michael@0: { michael@0: CFURLRef bundleURL; michael@0: CFBundleRef bundle = NULL; michael@0: char pathBuf[PATH_MAX]; michael@0: const char *resolvedPath; michael@0: CFStringRef pathRef; michael@0: michael@0: /* Takes care of relative paths and symlinks */ michael@0: resolvedPath = realpath(name, pathBuf); michael@0: if (!resolvedPath) michael@0: return PR_FAILURE; michael@0: michael@0: pathRef = CFStringCreateWithCString(NULL, pathBuf, kCFStringEncodingUTF8); michael@0: if (pathRef) { michael@0: bundleURL = CFURLCreateWithFileSystemPath(NULL, pathRef, michael@0: kCFURLPOSIXPathStyle, true); michael@0: if (bundleURL) { michael@0: bundle = CFBundleCreate(NULL, bundleURL); michael@0: CFRelease(bundleURL); michael@0: } michael@0: CFRelease(pathRef); michael@0: } michael@0: michael@0: lm->bundle = bundle; michael@0: return (bundle != NULL) ? PR_SUCCESS : PR_FAILURE; michael@0: } michael@0: michael@0: static PRStatus michael@0: pr_LoadViaDyld(const char *name, PRLibrary *lm) michael@0: { michael@0: lm->dlh = pr_LoadMachDyldModule(name); michael@0: if (lm->dlh == NULL) { michael@0: lm->image = NSAddImage(name, NSADDIMAGE_OPTION_RETURN_ON_ERROR michael@0: | NSADDIMAGE_OPTION_WITH_SEARCHING); michael@0: if (lm->image == NULL) { michael@0: NSLinkEditErrors linkEditError; michael@0: int errorNum; michael@0: const char *fileName; michael@0: const char *errorString; michael@0: NSLinkEditError(&linkEditError, &errorNum, &fileName, &errorString); michael@0: PR_LOG(_pr_linker_lm, PR_LOG_MIN, michael@0: ("LoadMachDyldModule error %d:%d for file %s:\n%s", michael@0: linkEditError, errorNum, fileName, errorString)); michael@0: } michael@0: } michael@0: return (lm->dlh != NULL || lm->image != NULL) ? PR_SUCCESS : PR_FAILURE; michael@0: } michael@0: michael@0: #endif /* XP_MACOSX && USE_MACH_DYLD */ michael@0: michael@0: /* michael@0: ** Dynamically load a library. Only load libraries once, so scan the load michael@0: ** map first. michael@0: */ michael@0: static PRLibrary* michael@0: pr_LoadLibraryByPathname(const char *name, PRIntn flags) michael@0: { michael@0: PRLibrary *lm; michael@0: PRLibrary* result = NULL; michael@0: PRInt32 oserr; michael@0: #ifdef WIN32 michael@0: char utf8name_stack[MAX_PATH]; michael@0: char *utf8name_malloc = NULL; michael@0: char *utf8name = utf8name_stack; michael@0: PRUnichar wname_stack[MAX_PATH]; michael@0: PRUnichar *wname_malloc = NULL; michael@0: PRUnichar *wname = wname_stack; michael@0: int len; michael@0: #endif michael@0: michael@0: if (!_pr_initialized) _PR_ImplicitInitialization(); michael@0: michael@0: /* See if library is already loaded */ michael@0: PR_EnterMonitor(pr_linker_lock); michael@0: michael@0: #ifdef WIN32 michael@0: if (flags & PR_LD_PATHW) { michael@0: /* cast back what's cast to |char *| for the argument passing. */ michael@0: wname = (LPWSTR) name; michael@0: } else { michael@0: int wlen = MultiByteToWideChar(CP_ACP, 0, name, -1, NULL, 0); michael@0: if (wlen > MAX_PATH) michael@0: wname = wname_malloc = PR_Malloc(wlen * sizeof(PRUnichar)); michael@0: if (wname == NULL || michael@0: !MultiByteToWideChar(CP_ACP, 0, name, -1, wname, wlen)) { michael@0: oserr = _MD_ERRNO(); michael@0: goto unlock; michael@0: } michael@0: } michael@0: len = WideCharToMultiByte(CP_UTF8, 0, wname, -1, NULL, 0, NULL, NULL); michael@0: if (len > MAX_PATH) michael@0: utf8name = utf8name_malloc = PR_Malloc(len); michael@0: if (utf8name == NULL || michael@0: !WideCharToMultiByte(CP_UTF8, 0, wname, -1, michael@0: utf8name, len, NULL, NULL)) { michael@0: oserr = _MD_ERRNO(); michael@0: goto unlock; michael@0: } michael@0: /* the list of loaded library names are always kept in UTF-8 michael@0: * on Win32 platforms */ michael@0: result = pr_UnlockedFindLibrary(utf8name); michael@0: #else michael@0: result = pr_UnlockedFindLibrary(name); michael@0: #endif michael@0: michael@0: if (result != NULL) goto unlock; michael@0: michael@0: lm = PR_NEWZAP(PRLibrary); michael@0: if (lm == NULL) { michael@0: oserr = _MD_ERRNO(); michael@0: goto unlock; michael@0: } michael@0: lm->staticTable = NULL; michael@0: michael@0: #ifdef XP_OS2 /* Why isn't all this stuff in MD code?! */ michael@0: { michael@0: HMODULE h; michael@0: UCHAR pszError[_MAX_PATH]; michael@0: ULONG ulRc = NO_ERROR; michael@0: michael@0: ulRc = DosLoadModule(pszError, _MAX_PATH, (PSZ) name, &h); michael@0: if (ulRc != NO_ERROR) { michael@0: oserr = ulRc; michael@0: PR_DELETE(lm); michael@0: goto unlock; michael@0: } michael@0: lm->name = strdup(name); michael@0: lm->dlh = h; michael@0: lm->next = pr_loadmap; michael@0: pr_loadmap = lm; michael@0: } michael@0: #endif /* XP_OS2 */ michael@0: michael@0: #ifdef WIN32 michael@0: { michael@0: HINSTANCE h; michael@0: michael@0: h = LoadLibraryExW(wname, NULL, michael@0: (flags & PR_LD_ALT_SEARCH_PATH) ? michael@0: LOAD_WITH_ALTERED_SEARCH_PATH : 0); michael@0: if (h == NULL) { michael@0: oserr = _MD_ERRNO(); michael@0: PR_DELETE(lm); michael@0: goto unlock; michael@0: } michael@0: lm->name = strdup(utf8name); michael@0: lm->dlh = h; michael@0: lm->next = pr_loadmap; michael@0: pr_loadmap = lm; michael@0: } michael@0: #endif /* WIN32 */ michael@0: michael@0: #if defined(XP_MACOSX) && defined(USE_MACH_DYLD) michael@0: { michael@0: int i; michael@0: PRStatus status; michael@0: michael@0: static const macLibraryLoadProc loadProcs[] = { michael@0: #ifdef __ppc__ michael@0: pr_LoadViaDyld, pr_LoadCFBundle, pr_LoadViaCFM michael@0: #else /* __ppc__ */ michael@0: pr_LoadViaDyld, pr_LoadCFBundle michael@0: #endif /* __ppc__ */ michael@0: }; michael@0: michael@0: for (i = 0; i < sizeof(loadProcs) / sizeof(loadProcs[0]); i++) { michael@0: if ((status = loadProcs[i](name, lm)) == PR_SUCCESS) michael@0: break; michael@0: } michael@0: if (status != PR_SUCCESS) { michael@0: oserr = cfragNoLibraryErr; michael@0: PR_DELETE(lm); michael@0: goto unlock; michael@0: } michael@0: lm->name = strdup(name); michael@0: lm->next = pr_loadmap; michael@0: pr_loadmap = lm; michael@0: } michael@0: #endif michael@0: michael@0: #if defined(XP_UNIX) && !(defined(XP_MACOSX) && defined(USE_MACH_DYLD)) michael@0: #ifdef HAVE_DLL michael@0: { michael@0: #if defined(USE_DLFCN) michael@0: #ifdef NTO michael@0: /* Neutrino needs RTLD_GROUP to load Netscape plugins. (bug 71179) */ michael@0: int dl_flags = RTLD_GROUP; michael@0: #elif defined(AIX) michael@0: /* AIX needs RTLD_MEMBER to load an archive member. (bug 228899) */ michael@0: int dl_flags = RTLD_MEMBER; michael@0: #else michael@0: int dl_flags = 0; michael@0: #endif michael@0: void *h = NULL; michael@0: michael@0: if (flags & PR_LD_LAZY) { michael@0: dl_flags |= RTLD_LAZY; michael@0: } michael@0: if (flags & PR_LD_NOW) { michael@0: dl_flags |= RTLD_NOW; michael@0: } michael@0: if (flags & PR_LD_GLOBAL) { michael@0: dl_flags |= RTLD_GLOBAL; michael@0: } michael@0: if (flags & PR_LD_LOCAL) { michael@0: dl_flags |= RTLD_LOCAL; michael@0: } michael@0: #if defined(DARWIN) michael@0: /* ensure the file exists if it contains a slash character i.e. path */ michael@0: /* DARWIN's dlopen ignores the provided path and checks for the */ michael@0: /* plain filename in DYLD_LIBRARY_PATH */ michael@0: if (strchr(name, PR_DIRECTORY_SEPARATOR) == NULL || michael@0: PR_Access(name, PR_ACCESS_EXISTS) == PR_SUCCESS) { michael@0: h = dlopen(name, dl_flags); michael@0: } michael@0: #else michael@0: h = dlopen(name, dl_flags); michael@0: #endif michael@0: #elif defined(USE_HPSHL) michael@0: int shl_flags = 0; michael@0: shl_t h; michael@0: michael@0: /* michael@0: * Use the DYNAMIC_PATH flag only if 'name' is a plain file michael@0: * name (containing no directory) to match the behavior of michael@0: * dlopen(). michael@0: */ michael@0: if (strchr(name, PR_DIRECTORY_SEPARATOR) == NULL) { michael@0: shl_flags |= DYNAMIC_PATH; michael@0: } michael@0: if (flags & PR_LD_LAZY) { michael@0: shl_flags |= BIND_DEFERRED; michael@0: } michael@0: if (flags & PR_LD_NOW) { michael@0: shl_flags |= BIND_IMMEDIATE; michael@0: } michael@0: /* No equivalent of PR_LD_GLOBAL and PR_LD_LOCAL. */ michael@0: h = shl_load(name, shl_flags, 0L); michael@0: #elif defined(USE_MACH_DYLD) michael@0: NSModule h = pr_LoadMachDyldModule(name); michael@0: #else michael@0: #error Configuration error michael@0: #endif michael@0: if (!h) { michael@0: oserr = _MD_ERRNO(); michael@0: PR_DELETE(lm); michael@0: goto unlock; michael@0: } michael@0: lm->name = strdup(name); michael@0: lm->dlh = h; michael@0: lm->next = pr_loadmap; michael@0: pr_loadmap = lm; michael@0: } michael@0: #endif /* HAVE_DLL */ michael@0: #endif /* XP_UNIX && !(XP_MACOSX && USE_MACH_DYLD) */ michael@0: michael@0: lm->refCount = 1; michael@0: michael@0: #ifdef XP_BEOS michael@0: { michael@0: image_info info; michael@0: int32 cookie = 0; michael@0: image_id imageid = B_ERROR; michael@0: image_id stubid = B_ERROR; michael@0: PRLibrary *p; michael@0: michael@0: for (p = pr_loadmap; p != NULL; p = p->next) { michael@0: /* hopefully, our caller will always use the same string michael@0: to refer to the same library */ michael@0: if (strcmp(name, p->name) == 0) { michael@0: /* we've already loaded this library */ michael@0: imageid = info.id; michael@0: lm->refCount++; michael@0: break; michael@0: } michael@0: } michael@0: michael@0: if(imageid == B_ERROR) { michael@0: /* it appears the library isn't yet loaded - load it now */ michael@0: char stubName [B_PATH_NAME_LENGTH + 1]; michael@0: michael@0: /* the following is a work-around to a "bug" in the beos - michael@0: the beos system loader allows only 32M (system-wide) michael@0: to be used by code loaded as "add-ons" (code loaded michael@0: through the 'load_add_on()' system call, which includes michael@0: mozilla components), but allows 256M to be used by michael@0: shared libraries. michael@0: michael@0: unfortunately, mozilla is too large to fit into the michael@0: "add-on" space, so we must trick the loader into michael@0: loading some of the components as shared libraries. this michael@0: is accomplished by creating a "stub" add-on (an empty michael@0: shared object), and linking it with the component michael@0: (the actual .so file generated by the build process, michael@0: without any modifications). when this stub is loaded michael@0: by load_add_on(), the loader will automatically load the michael@0: component into the shared library space. michael@0: */ michael@0: michael@0: strcpy(stubName, name); michael@0: strcat(stubName, ".stub"); michael@0: michael@0: /* first, attempt to load the stub (thereby loading the michael@0: component as a shared library */ michael@0: if ((stubid = load_add_on(stubName)) > B_ERROR) { michael@0: /* the stub was loaded successfully. */ michael@0: imageid = B_FILE_NOT_FOUND; michael@0: michael@0: cookie = 0; michael@0: while (get_next_image_info(0, &cookie, &info) == B_OK) { michael@0: const char *endOfSystemName = strrchr(info.name, '/'); michael@0: const char *endOfPassedName = strrchr(name, '/'); michael@0: if( 0 == endOfSystemName ) michael@0: endOfSystemName = info.name; michael@0: else michael@0: endOfSystemName++; michael@0: if( 0 == endOfPassedName ) michael@0: endOfPassedName = name; michael@0: else michael@0: endOfPassedName++; michael@0: if (strcmp(endOfSystemName, endOfPassedName) == 0) { michael@0: /* this is the actual component - remember it */ michael@0: imageid = info.id; michael@0: break; michael@0: } michael@0: } michael@0: michael@0: } else { michael@0: /* we failed to load the "stub" - try to load the michael@0: component directly as an add-on */ michael@0: stubid = B_ERROR; michael@0: imageid = load_add_on(name); michael@0: } michael@0: } michael@0: michael@0: if (imageid <= B_ERROR) { michael@0: oserr = imageid; michael@0: PR_DELETE( lm ); michael@0: goto unlock; michael@0: } michael@0: lm->name = strdup(name); michael@0: lm->dlh = (void*)imageid; michael@0: lm->stub_dlh = (void*)stubid; michael@0: lm->next = pr_loadmap; michael@0: pr_loadmap = lm; michael@0: } michael@0: #endif michael@0: michael@0: result = lm; /* success */ michael@0: PR_LOG(_pr_linker_lm, PR_LOG_MIN, ("Loaded library %s (load lib)", lm->name)); michael@0: michael@0: unlock: michael@0: if (result == NULL) { michael@0: PR_SetError(PR_LOAD_LIBRARY_ERROR, oserr); michael@0: DLLErrorInternal(oserr); /* sets error text */ michael@0: } michael@0: #ifdef WIN32 michael@0: if (utf8name_malloc) michael@0: PR_Free(utf8name_malloc); michael@0: if (wname_malloc) michael@0: PR_Free(wname_malloc); michael@0: #endif michael@0: PR_ExitMonitor(pr_linker_lock); michael@0: return result; michael@0: } michael@0: michael@0: /* michael@0: ** Unload a shared library which was loaded via PR_LoadLibrary michael@0: */ michael@0: PR_IMPLEMENT(PRStatus) michael@0: PR_UnloadLibrary(PRLibrary *lib) michael@0: { michael@0: int result = 0; michael@0: PRStatus status = PR_SUCCESS; michael@0: michael@0: if (lib == 0) { michael@0: PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); michael@0: return PR_FAILURE; michael@0: } michael@0: michael@0: PR_EnterMonitor(pr_linker_lock); michael@0: michael@0: if (lib->refCount <= 0) { michael@0: PR_ExitMonitor(pr_linker_lock); michael@0: PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); michael@0: return PR_FAILURE; michael@0: } michael@0: michael@0: if (--lib->refCount > 0) { michael@0: PR_LOG(_pr_linker_lm, PR_LOG_MIN, michael@0: ("%s decr => %d", michael@0: lib->name, lib->refCount)); michael@0: goto done; michael@0: } michael@0: michael@0: #ifdef XP_BEOS michael@0: if(((image_id)lib->stub_dlh) == B_ERROR) michael@0: unload_add_on( (image_id) lib->dlh ); michael@0: else michael@0: unload_add_on( (image_id) lib->stub_dlh); michael@0: #endif michael@0: michael@0: #ifdef XP_UNIX michael@0: #ifdef HAVE_DLL michael@0: #ifdef USE_DLFCN michael@0: result = dlclose(lib->dlh); michael@0: #elif defined(USE_HPSHL) michael@0: result = shl_unload(lib->dlh); michael@0: #elif defined(USE_MACH_DYLD) michael@0: if (lib->dlh) michael@0: result = NSUnLinkModule(lib->dlh, NSUNLINKMODULE_OPTION_NONE) ? 0 : -1; michael@0: #else michael@0: #error Configuration error michael@0: #endif michael@0: #endif /* HAVE_DLL */ michael@0: #endif /* XP_UNIX */ michael@0: #ifdef XP_PC michael@0: if (lib->dlh) { michael@0: FreeLibrary((HINSTANCE)(lib->dlh)); michael@0: lib->dlh = (HINSTANCE)NULL; michael@0: } michael@0: #endif /* XP_PC */ michael@0: michael@0: #if defined(XP_MACOSX) && defined(USE_MACH_DYLD) michael@0: /* Close the connection */ michael@0: if (lib->connection) michael@0: CloseConnection(&(lib->connection)); michael@0: if (lib->bundle) michael@0: CFRelease(lib->bundle); michael@0: if (lib->wrappers) michael@0: CFRelease(lib->wrappers); michael@0: /* No way to unload an image (lib->image) */ michael@0: #endif michael@0: michael@0: /* unlink from library search list */ michael@0: if (pr_loadmap == lib) michael@0: pr_loadmap = pr_loadmap->next; michael@0: else if (pr_loadmap != NULL) { michael@0: PRLibrary* prev = pr_loadmap; michael@0: PRLibrary* next = pr_loadmap->next; michael@0: while (next != NULL) { michael@0: if (next == lib) { michael@0: prev->next = next->next; michael@0: goto freeLib; michael@0: } michael@0: prev = next; michael@0: next = next->next; michael@0: } michael@0: /* michael@0: * fail (the library is not on the _pr_loadmap list), michael@0: * but don't wipe out an error from dlclose/shl_unload. michael@0: */ michael@0: PR_ASSERT(!"_pr_loadmap and lib->refCount inconsistent"); michael@0: if (result == 0) { michael@0: PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); michael@0: status = PR_FAILURE; michael@0: } michael@0: } michael@0: /* michael@0: * We free the PRLibrary structure whether dlclose/shl_unload michael@0: * succeeds or not. michael@0: */ michael@0: michael@0: freeLib: michael@0: PR_LOG(_pr_linker_lm, PR_LOG_MIN, ("Unloaded library %s", lib->name)); michael@0: free(lib->name); michael@0: lib->name = NULL; michael@0: PR_DELETE(lib); michael@0: if (result != 0) { michael@0: PR_SetError(PR_UNLOAD_LIBRARY_ERROR, _MD_ERRNO()); michael@0: DLLErrorInternal(_MD_ERRNO()); michael@0: status = PR_FAILURE; michael@0: } michael@0: michael@0: done: michael@0: PR_ExitMonitor(pr_linker_lock); michael@0: return status; michael@0: } michael@0: michael@0: static void* michael@0: pr_FindSymbolInLib(PRLibrary *lm, const char *name) michael@0: { michael@0: void *f = NULL; michael@0: #ifdef XP_OS2 michael@0: int rc; michael@0: #endif michael@0: michael@0: if (lm->staticTable != NULL) { michael@0: const PRStaticLinkTable* tp; michael@0: for (tp = lm->staticTable; tp->name; tp++) { michael@0: if (strcmp(name, tp->name) == 0) { michael@0: return (void*) tp->fp; michael@0: } michael@0: } michael@0: /* michael@0: ** If the symbol was not found in the static table then check if michael@0: ** the symbol was exported in the DLL... Win16 only!! michael@0: */ michael@0: #if !defined(WIN16) && !defined(XP_BEOS) michael@0: PR_SetError(PR_FIND_SYMBOL_ERROR, 0); michael@0: return (void*)NULL; michael@0: #endif michael@0: } michael@0: michael@0: #ifdef XP_OS2 michael@0: rc = DosQueryProcAddr(lm->dlh, 0, (PSZ) name, (PFN *) &f); michael@0: #if defined(NEED_LEADING_UNDERSCORE) michael@0: /* michael@0: * Older plugins (not built using GCC) will have symbols that are not michael@0: * underscore prefixed. We check for that here. michael@0: */ michael@0: if (rc != NO_ERROR) { michael@0: name++; michael@0: DosQueryProcAddr(lm->dlh, 0, (PSZ) name, (PFN *) &f); michael@0: } michael@0: #endif michael@0: #endif /* XP_OS2 */ michael@0: michael@0: #ifdef WIN32 michael@0: f = GetProcAddress(lm->dlh, name); michael@0: #endif /* WIN32 */ michael@0: michael@0: #if defined(XP_MACOSX) && defined(USE_MACH_DYLD) michael@0: /* add this offset to skip the leading underscore in name */ michael@0: #define SYM_OFFSET 1 michael@0: if (lm->bundle) { michael@0: CFStringRef nameRef = CFStringCreateWithCString(NULL, name + SYM_OFFSET, kCFStringEncodingASCII); michael@0: if (nameRef) { michael@0: f = CFBundleGetFunctionPointerForName(lm->bundle, nameRef); michael@0: CFRelease(nameRef); michael@0: } michael@0: } michael@0: if (lm->connection) { michael@0: Ptr symAddr; michael@0: CFragSymbolClass symClass; michael@0: Str255 pName; michael@0: michael@0: PR_LOG(_pr_linker_lm, PR_LOG_MIN, ("Looking up symbol: %s", name + SYM_OFFSET)); michael@0: michael@0: c2pstrcpy(pName, name + SYM_OFFSET); michael@0: michael@0: f = (FindSymbol(lm->connection, pName, &symAddr, &symClass) == noErr) ? symAddr : NULL; michael@0: michael@0: #ifdef __ppc__ michael@0: /* callers expect mach-o function pointers, so must wrap tvectors with glue. */ michael@0: if (f && symClass == kTVectorCFragSymbol) { michael@0: f = TV2FP(lm->wrappers, name + SYM_OFFSET, f); michael@0: } michael@0: #endif /* __ppc__ */ michael@0: michael@0: if (f == NULL && strcmp(name + SYM_OFFSET, "main") == 0) f = lm->main; michael@0: } michael@0: if (lm->image) { michael@0: NSSymbol symbol; michael@0: symbol = NSLookupSymbolInImage(lm->image, name, michael@0: NSLOOKUPSYMBOLINIMAGE_OPTION_BIND michael@0: | NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR); michael@0: if (symbol != NULL) michael@0: f = NSAddressOfSymbol(symbol); michael@0: else michael@0: f = NULL; michael@0: } michael@0: #undef SYM_OFFSET michael@0: #endif /* XP_MACOSX && USE_MACH_DYLD */ michael@0: michael@0: #ifdef XP_BEOS michael@0: if( B_NO_ERROR != get_image_symbol( (image_id)lm->dlh, name, B_SYMBOL_TYPE_TEXT, &f ) ) { michael@0: f = NULL; michael@0: } michael@0: #endif michael@0: michael@0: #ifdef XP_UNIX michael@0: #ifdef HAVE_DLL michael@0: #ifdef USE_DLFCN michael@0: f = dlsym(lm->dlh, name); michael@0: #elif defined(USE_HPSHL) michael@0: if (shl_findsym(&lm->dlh, name, TYPE_PROCEDURE, &f) == -1) { michael@0: f = NULL; michael@0: } michael@0: #elif defined(USE_MACH_DYLD) michael@0: if (lm->dlh) { michael@0: NSSymbol symbol; michael@0: symbol = NSLookupSymbolInModule(lm->dlh, name); michael@0: if (symbol != NULL) michael@0: f = NSAddressOfSymbol(symbol); michael@0: else michael@0: f = NULL; michael@0: } michael@0: #endif michael@0: #endif /* HAVE_DLL */ michael@0: #endif /* XP_UNIX */ michael@0: if (f == NULL) { michael@0: PR_SetError(PR_FIND_SYMBOL_ERROR, _MD_ERRNO()); michael@0: DLLErrorInternal(_MD_ERRNO()); michael@0: } michael@0: return f; michael@0: } michael@0: michael@0: /* michael@0: ** Called by class loader to resolve missing native's michael@0: */ michael@0: PR_IMPLEMENT(void*) michael@0: PR_FindSymbol(PRLibrary *lib, const char *raw_name) michael@0: { michael@0: void *f = NULL; michael@0: #if defined(NEED_LEADING_UNDERSCORE) michael@0: char *name; michael@0: #else michael@0: const char *name; michael@0: #endif michael@0: /* michael@0: ** Mangle the raw symbol name in any way that is platform specific. michael@0: */ michael@0: #if defined(NEED_LEADING_UNDERSCORE) michael@0: /* Need a leading _ */ michael@0: name = PR_smprintf("_%s", raw_name); michael@0: #elif defined(AIX) michael@0: /* michael@0: ** AIX with the normal linker put's a "." in front of the symbol michael@0: ** name. When use "svcc" and "svld" then the "." disappears. Go michael@0: ** figure. michael@0: */ michael@0: name = raw_name; michael@0: #else michael@0: name = raw_name; michael@0: #endif michael@0: michael@0: PR_EnterMonitor(pr_linker_lock); michael@0: PR_ASSERT(lib != NULL); michael@0: f = pr_FindSymbolInLib(lib, name); michael@0: michael@0: #if defined(NEED_LEADING_UNDERSCORE) michael@0: PR_smprintf_free(name); michael@0: #endif michael@0: michael@0: PR_ExitMonitor(pr_linker_lock); michael@0: return f; michael@0: } michael@0: michael@0: /* michael@0: ** Return the address of the function 'raw_name' in the library 'lib' michael@0: */ michael@0: PR_IMPLEMENT(PRFuncPtr) michael@0: PR_FindFunctionSymbol(PRLibrary *lib, const char *raw_name) michael@0: { michael@0: return ((PRFuncPtr) PR_FindSymbol(lib, raw_name)); michael@0: } michael@0: michael@0: PR_IMPLEMENT(void*) michael@0: PR_FindSymbolAndLibrary(const char *raw_name, PRLibrary* *lib) michael@0: { michael@0: void *f = NULL; michael@0: #if defined(NEED_LEADING_UNDERSCORE) michael@0: char *name; michael@0: #else michael@0: const char *name; michael@0: #endif michael@0: PRLibrary* lm; michael@0: michael@0: if (!_pr_initialized) _PR_ImplicitInitialization(); michael@0: /* michael@0: ** Mangle the raw symbol name in any way that is platform specific. michael@0: */ michael@0: #if defined(NEED_LEADING_UNDERSCORE) michael@0: /* Need a leading _ */ michael@0: name = PR_smprintf("_%s", raw_name); michael@0: #elif defined(AIX) michael@0: /* michael@0: ** AIX with the normal linker put's a "." in front of the symbol michael@0: ** name. When use "svcc" and "svld" then the "." disappears. Go michael@0: ** figure. michael@0: */ michael@0: name = raw_name; michael@0: #else michael@0: name = raw_name; michael@0: #endif michael@0: michael@0: PR_EnterMonitor(pr_linker_lock); michael@0: michael@0: /* search all libraries */ michael@0: for (lm = pr_loadmap; lm != NULL; lm = lm->next) { michael@0: f = pr_FindSymbolInLib(lm, name); michael@0: if (f != NULL) { michael@0: *lib = lm; michael@0: lm->refCount++; michael@0: PR_LOG(_pr_linker_lm, PR_LOG_MIN, michael@0: ("%s incr => %d (for %s)", michael@0: lm->name, lm->refCount, name)); michael@0: break; michael@0: } michael@0: } michael@0: #if defined(NEED_LEADING_UNDERSCORE) michael@0: PR_smprintf_free(name); michael@0: #endif michael@0: michael@0: PR_ExitMonitor(pr_linker_lock); michael@0: return f; michael@0: } michael@0: michael@0: PR_IMPLEMENT(PRFuncPtr) michael@0: PR_FindFunctionSymbolAndLibrary(const char *raw_name, PRLibrary* *lib) michael@0: { michael@0: return ((PRFuncPtr) PR_FindSymbolAndLibrary(raw_name, lib)); michael@0: } michael@0: michael@0: /* michael@0: ** Add a static library to the list of loaded libraries. If LoadLibrary michael@0: ** is called with the name then we will pretend it was already loaded michael@0: */ michael@0: PR_IMPLEMENT(PRLibrary*) michael@0: PR_LoadStaticLibrary(const char *name, const PRStaticLinkTable *slt) michael@0: { michael@0: PRLibrary *lm=NULL; michael@0: PRLibrary* result = NULL; michael@0: michael@0: if (!_pr_initialized) _PR_ImplicitInitialization(); michael@0: michael@0: /* See if library is already loaded */ michael@0: PR_EnterMonitor(pr_linker_lock); michael@0: michael@0: /* If the lbrary is already loaded, then add the static table information... */ michael@0: result = pr_UnlockedFindLibrary(name); michael@0: if (result != NULL) { michael@0: PR_ASSERT( (result->staticTable == NULL) || (result->staticTable == slt) ); michael@0: result->staticTable = slt; michael@0: goto unlock; michael@0: } michael@0: michael@0: /* Add library to list...Mark it static */ michael@0: lm = PR_NEWZAP(PRLibrary); michael@0: if (lm == NULL) goto unlock; michael@0: michael@0: lm->name = strdup(name); michael@0: lm->refCount = 1; michael@0: lm->dlh = pr_exe_loadmap ? pr_exe_loadmap->dlh : 0; michael@0: lm->staticTable = slt; michael@0: lm->next = pr_loadmap; michael@0: pr_loadmap = lm; michael@0: michael@0: result = lm; /* success */ michael@0: PR_ASSERT(lm->refCount == 1); michael@0: PR_LOG(_pr_linker_lm, PR_LOG_MIN, ("Loaded library %s (static lib)", lm->name)); michael@0: unlock: michael@0: PR_ExitMonitor(pr_linker_lock); michael@0: return result; michael@0: } michael@0: michael@0: PR_IMPLEMENT(char *) michael@0: PR_GetLibraryFilePathname(const char *name, PRFuncPtr addr) michael@0: { michael@0: #if defined(USE_DLFCN) && defined(HAVE_DLADDR) michael@0: Dl_info dli; michael@0: char *result; michael@0: michael@0: if (dladdr((void *)addr, &dli) == 0) { michael@0: PR_SetError(PR_LIBRARY_NOT_LOADED_ERROR, _MD_ERRNO()); michael@0: DLLErrorInternal(_MD_ERRNO()); michael@0: return NULL; michael@0: } michael@0: result = PR_Malloc(strlen(dli.dli_fname)+1); michael@0: if (result != NULL) { michael@0: strcpy(result, dli.dli_fname); michael@0: } michael@0: return result; michael@0: #elif defined(USE_MACH_DYLD) michael@0: char *result; michael@0: const char *image_name; michael@0: int i, count = _dyld_image_count(); michael@0: michael@0: for (i = 0; i < count; i++) { michael@0: image_name = _dyld_get_image_name(i); michael@0: if (strstr(image_name, name) != NULL) { michael@0: result = PR_Malloc(strlen(image_name)+1); michael@0: if (result != NULL) { michael@0: strcpy(result, image_name); michael@0: } michael@0: return result; michael@0: } michael@0: } michael@0: PR_SetError(PR_LIBRARY_NOT_LOADED_ERROR, 0); michael@0: return NULL; michael@0: #elif defined(AIX) michael@0: char *result; michael@0: #define LD_INFO_INCREMENT 64 michael@0: struct ld_info *info; michael@0: unsigned int info_length = LD_INFO_INCREMENT * sizeof(struct ld_info); michael@0: struct ld_info *infop; michael@0: int loadflags = L_GETINFO | L_IGNOREUNLOAD; michael@0: michael@0: for (;;) { michael@0: info = PR_Malloc(info_length); michael@0: if (info == NULL) { michael@0: return NULL; michael@0: } michael@0: /* If buffer is too small, loadquery fails with ENOMEM. */ michael@0: if (loadquery(loadflags, info, info_length) != -1) { michael@0: break; michael@0: } michael@0: /* michael@0: * Calling loadquery when compiled for 64-bit with the michael@0: * L_IGNOREUNLOAD flag can cause an invalid argument error michael@0: * on AIX 5.1. Detect this error the first time that michael@0: * loadquery is called, and try calling it again without michael@0: * this flag set. michael@0: */ michael@0: if (errno == EINVAL && (loadflags & L_IGNOREUNLOAD)) { michael@0: loadflags &= ~L_IGNOREUNLOAD; michael@0: if (loadquery(loadflags, info, info_length) != -1) { michael@0: break; michael@0: } michael@0: } michael@0: PR_Free(info); michael@0: if (errno != ENOMEM) { michael@0: /* should not happen */ michael@0: _PR_MD_MAP_DEFAULT_ERROR(_MD_ERRNO()); michael@0: return NULL; michael@0: } michael@0: /* retry with a larger buffer */ michael@0: info_length += LD_INFO_INCREMENT * sizeof(struct ld_info); michael@0: } michael@0: michael@0: for (infop = info; michael@0: ; michael@0: infop = (struct ld_info *)((char *)infop + infop->ldinfo_next)) { michael@0: unsigned long start = (unsigned long)infop->ldinfo_dataorg; michael@0: unsigned long end = start + infop->ldinfo_datasize; michael@0: if (start <= (unsigned long)addr && end > (unsigned long)addr) { michael@0: result = PR_Malloc(strlen(infop->ldinfo_filename)+1); michael@0: if (result != NULL) { michael@0: strcpy(result, infop->ldinfo_filename); michael@0: } michael@0: break; michael@0: } michael@0: if (!infop->ldinfo_next) { michael@0: PR_SetError(PR_LIBRARY_NOT_LOADED_ERROR, 0); michael@0: result = NULL; michael@0: break; michael@0: } michael@0: } michael@0: PR_Free(info); michael@0: return result; michael@0: #elif defined(OSF1) michael@0: /* Contributed by Steve Streeter of HP */ michael@0: ldr_process_t process, ldr_my_process(); michael@0: ldr_module_t mod_id; michael@0: ldr_module_info_t info; michael@0: ldr_region_t regno; michael@0: ldr_region_info_t reginfo; michael@0: size_t retsize; michael@0: int rv; michael@0: char *result; michael@0: michael@0: /* Get process for which dynamic modules will be listed */ michael@0: michael@0: process = ldr_my_process(); michael@0: michael@0: /* Attach to process */ michael@0: michael@0: rv = ldr_xattach(process); michael@0: if (rv) { michael@0: /* should not happen */ michael@0: _PR_MD_MAP_DEFAULT_ERROR(_MD_ERRNO()); michael@0: return NULL; michael@0: } michael@0: michael@0: /* Print information for list of modules */ michael@0: michael@0: mod_id = LDR_NULL_MODULE; michael@0: michael@0: for (;;) { michael@0: michael@0: /* Get information for the next module in the module list. */ michael@0: michael@0: ldr_next_module(process, &mod_id); michael@0: if (ldr_inq_module(process, mod_id, &info, sizeof(info), michael@0: &retsize) != 0) { michael@0: /* No more modules */ michael@0: break; michael@0: } michael@0: if (retsize < sizeof(info)) { michael@0: continue; michael@0: } michael@0: michael@0: /* michael@0: * Get information for each region in the module and check if any michael@0: * contain the address of this function. michael@0: */ michael@0: michael@0: for (regno = 0; ; regno++) { michael@0: if (ldr_inq_region(process, mod_id, regno, ®info, michael@0: sizeof(reginfo), &retsize) != 0) { michael@0: /* No more regions */ michael@0: break; michael@0: } michael@0: if (((unsigned long)reginfo.lri_mapaddr <= michael@0: (unsigned long)addr) && michael@0: (((unsigned long)reginfo.lri_mapaddr + reginfo.lri_size) > michael@0: (unsigned long)addr)) { michael@0: /* Found it. */ michael@0: result = PR_Malloc(strlen(info.lmi_name)+1); michael@0: if (result != NULL) { michael@0: strcpy(result, info.lmi_name); michael@0: } michael@0: return result; michael@0: } michael@0: } michael@0: } michael@0: PR_SetError(PR_LIBRARY_NOT_LOADED_ERROR, 0); michael@0: return NULL; michael@0: #elif defined(HPUX) && defined(USE_HPSHL) michael@0: int index; michael@0: struct shl_descriptor desc; michael@0: char *result; michael@0: michael@0: for (index = 0; shl_get_r(index, &desc) == 0; index++) { michael@0: if (strstr(desc.filename, name) != NULL) { michael@0: result = PR_Malloc(strlen(desc.filename)+1); michael@0: if (result != NULL) { michael@0: strcpy(result, desc.filename); michael@0: } michael@0: return result; michael@0: } michael@0: } michael@0: /* michael@0: * Since the index value of a library is decremented if michael@0: * a library preceding it in the shared library search michael@0: * list was unloaded, it is possible that we missed some michael@0: * libraries as we went up the list. So we should go michael@0: * down the list to be sure that we not miss anything. michael@0: */ michael@0: for (index--; index >= 0; index--) { michael@0: if ((shl_get_r(index, &desc) == 0) michael@0: && (strstr(desc.filename, name) != NULL)) { michael@0: result = PR_Malloc(strlen(desc.filename)+1); michael@0: if (result != NULL) { michael@0: strcpy(result, desc.filename); michael@0: } michael@0: return result; michael@0: } michael@0: } michael@0: PR_SetError(PR_LIBRARY_NOT_LOADED_ERROR, 0); michael@0: return NULL; michael@0: #elif defined(HPUX) && defined(USE_DLFCN) michael@0: struct load_module_desc desc; michael@0: char *result; michael@0: const char *module_name; michael@0: michael@0: if (dlmodinfo((unsigned long)addr, &desc, sizeof desc, NULL, 0, 0) == 0) { michael@0: PR_SetError(PR_LIBRARY_NOT_LOADED_ERROR, _MD_ERRNO()); michael@0: DLLErrorInternal(_MD_ERRNO()); michael@0: return NULL; michael@0: } michael@0: module_name = dlgetname(&desc, sizeof desc, NULL, 0, 0); michael@0: if (module_name == NULL) { michael@0: /* should not happen */ michael@0: _PR_MD_MAP_DEFAULT_ERROR(_MD_ERRNO()); michael@0: DLLErrorInternal(_MD_ERRNO()); michael@0: return NULL; michael@0: } michael@0: result = PR_Malloc(strlen(module_name)+1); michael@0: if (result != NULL) { michael@0: strcpy(result, module_name); michael@0: } michael@0: return result; michael@0: #elif defined(WIN32) michael@0: PRUnichar wname[MAX_PATH]; michael@0: HMODULE handle = NULL; michael@0: PRUnichar module_name[MAX_PATH]; michael@0: int len; michael@0: char *result; michael@0: michael@0: if (MultiByteToWideChar(CP_ACP, 0, name, -1, wname, MAX_PATH)) { michael@0: handle = GetModuleHandleW(wname); michael@0: } michael@0: if (handle == NULL) { michael@0: PR_SetError(PR_LIBRARY_NOT_LOADED_ERROR, _MD_ERRNO()); michael@0: DLLErrorInternal(_MD_ERRNO()); michael@0: return NULL; michael@0: } michael@0: if (GetModuleFileNameW(handle, module_name, MAX_PATH) == 0) { michael@0: /* should not happen */ michael@0: _PR_MD_MAP_DEFAULT_ERROR(_MD_ERRNO()); michael@0: return NULL; michael@0: } michael@0: len = WideCharToMultiByte(CP_ACP, 0, module_name, -1, michael@0: NULL, 0, NULL, NULL); michael@0: if (len == 0) { michael@0: _PR_MD_MAP_DEFAULT_ERROR(_MD_ERRNO()); michael@0: return NULL; michael@0: } michael@0: result = PR_Malloc(len * sizeof(PRUnichar)); michael@0: if (result != NULL) { michael@0: WideCharToMultiByte(CP_ACP, 0, module_name, -1, michael@0: result, len, NULL, NULL); michael@0: } michael@0: return result; michael@0: #elif defined(XP_OS2) michael@0: HMODULE module = NULL; michael@0: char module_name[_MAX_PATH]; michael@0: char *result; michael@0: APIRET ulrc = DosQueryModFromEIP(&module, NULL, 0, NULL, NULL, (ULONG) addr); michael@0: if ((NO_ERROR != ulrc) || (NULL == module) ) { michael@0: PR_SetError(PR_LIBRARY_NOT_LOADED_ERROR, _MD_ERRNO()); michael@0: DLLErrorInternal(_MD_ERRNO()); michael@0: return NULL; michael@0: } michael@0: ulrc = DosQueryModuleName(module, sizeof module_name, module_name); michael@0: if (NO_ERROR != ulrc) { michael@0: /* should not happen */ michael@0: _PR_MD_MAP_DEFAULT_ERROR(_MD_ERRNO()); michael@0: return NULL; michael@0: } michael@0: result = PR_Malloc(strlen(module_name)+1); michael@0: if (result != NULL) { michael@0: strcpy(result, module_name); michael@0: } michael@0: return result; michael@0: #else michael@0: PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0); michael@0: return NULL; michael@0: #endif michael@0: }