andre@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ andre@0: /* This Source Code Form is subject to the terms of the Mozilla Public andre@0: * License, v. 2.0. If a copy of the MPL was not distributed with this andre@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ andre@0: andre@0: #ifndef prlink_h___ andre@0: #define prlink_h___ andre@0: andre@0: /* andre@0: ** API to static and dynamic linking. andre@0: */ andre@0: #include "prtypes.h" andre@0: andre@0: PR_BEGIN_EXTERN_C andre@0: andre@0: typedef struct PRLibrary PRLibrary; andre@0: andre@0: typedef struct PRStaticLinkTable { andre@0: const char *name; andre@0: void (*fp)(void); andre@0: } PRStaticLinkTable; andre@0: andre@0: /* andre@0: ** Change the default library path to the given string. The string is andre@0: ** copied. This call will fail if it runs out of memory. andre@0: ** andre@0: ** The string provided as 'path' is copied. The caller can do whatever is andre@0: ** convenient with the argument when the function is complete. andre@0: */ andre@0: NSPR_API(PRStatus) PR_SetLibraryPath(const char *path); andre@0: andre@0: /* andre@0: ** Return a character string which contains the path used to search for andre@0: ** dynamically loadable libraries. andre@0: ** andre@0: ** The returned value is basically a copy of a PR_SetLibraryPath(). andre@0: ** The storage is allocated by the runtime and becomes the responsibilty andre@0: ** of the caller. andre@0: */ andre@0: NSPR_API(char*) PR_GetLibraryPath(void); andre@0: andre@0: /* andre@0: ** Given a directory name "dir" and a library name "lib" construct a full andre@0: ** path name that will refer to the actual dynamically loaded andre@0: ** library. This does not test for existance of said file, it just andre@0: ** constructs the full filename. The name constructed is system dependent andre@0: ** and prepared for PR_LoadLibrary. The result must be free'd when the andre@0: ** caller is done with it. andre@0: ** andre@0: ** The storage for the result is allocated by the runtime and becomes the andre@0: ** responsibility of the caller. andre@0: */ andre@0: NSPR_API(char*) PR_GetLibraryName(const char *dir, const char *lib); andre@0: andre@0: /* andre@0: ** andre@0: ** Free the memory allocated, for the caller, by PR_GetLibraryName andre@0: */ andre@0: NSPR_API(void) PR_FreeLibraryName(char *mem); andre@0: andre@0: /* andre@0: ** Given a library "name" try to load the library. The argument "name" andre@0: ** is a machine-dependent name for the library, such as the full pathname andre@0: ** returned by PR_GetLibraryName. If the library is already loaded, andre@0: ** this function will avoid loading the library twice. andre@0: ** andre@0: ** If the library is loaded successfully, then a pointer to the PRLibrary andre@0: ** structure representing the library is returned. Otherwise, NULL is andre@0: ** returned. andre@0: ** andre@0: ** This increments the reference count of the library. andre@0: */ andre@0: NSPR_API(PRLibrary*) PR_LoadLibrary(const char *name); andre@0: andre@0: /* andre@0: ** Each operating system has its preferred way of specifying andre@0: ** a file in the file system. Most operating systems use andre@0: ** a pathname. Mac OS Classic, on the other hand, uses the FSSpec andre@0: ** structure to specify a file. PRLibSpec allows NSPR clients andre@0: ** to use the type of file specification that is most efficient andre@0: ** for a particular platform. andre@0: ** andre@0: ** On some operating systems such as Mac OS Classic, a shared library andre@0: ** may contain code fragments that can be individually loaded. andre@0: ** PRLibSpec also allows NSPR clients to identify a code fragment andre@0: ** in a library, if code fragments are supported by the OS. andre@0: ** A code fragment can be specified by name or by an integer index. andre@0: ** andre@0: ** Right now PRLibSpec supports four types of library specification: andre@0: ** a pathname in the native character encoding, a Mac code fragment andre@0: ** by name, a Mac code fragment by index, and a UTF-16 pathname. andre@0: */ andre@0: andre@0: typedef enum PRLibSpecType { andre@0: PR_LibSpec_Pathname, andre@0: PR_LibSpec_MacNamedFragment, /* obsolete (for Mac OS Classic) */ andre@0: PR_LibSpec_MacIndexedFragment, /* obsolete (for Mac OS Classic) */ andre@0: PR_LibSpec_PathnameU /* supported only on Win32 */ andre@0: } PRLibSpecType; andre@0: andre@0: struct FSSpec; /* Mac OS Classic FSSpec */ andre@0: andre@0: typedef struct PRLibSpec { andre@0: PRLibSpecType type; andre@0: union { andre@0: /* if type is PR_LibSpec_Pathname */ andre@0: const char *pathname; andre@0: andre@0: /* if type is PR_LibSpec_MacNamedFragment */ andre@0: struct { andre@0: const struct FSSpec *fsspec; andre@0: const char *name; andre@0: } mac_named_fragment; /* obsolete (for Mac OS Classic) */ andre@0: andre@0: /* if type is PR_LibSpec_MacIndexedFragment */ andre@0: struct { andre@0: const struct FSSpec *fsspec; andre@0: PRUint32 index; andre@0: } mac_indexed_fragment; /* obsolete (for Mac OS Classic) */ andre@0: andre@0: /* if type is PR_LibSpec_PathnameU */ andre@0: const PRUnichar *pathname_u; /* supported only on Win32 */ andre@0: } value; andre@0: } PRLibSpec; andre@0: andre@0: /* andre@0: ** The following bit flags may be or'd together and passed andre@0: ** as the 'flags' argument to PR_LoadLibraryWithFlags. andre@0: ** Flags not supported by the underlying OS are ignored. andre@0: */ andre@0: andre@0: #define PR_LD_LAZY 0x1 /* equivalent to RTLD_LAZY on Unix */ andre@0: #define PR_LD_NOW 0x2 /* equivalent to RTLD_NOW on Unix */ andre@0: #define PR_LD_GLOBAL 0x4 /* equivalent to RTLD_GLOBAL on Unix */ andre@0: #define PR_LD_LOCAL 0x8 /* equivalent to RTLD_LOCAL on Unix */ andre@0: /* The following is equivalent to LOAD_WITH_ALTERED_SEARCH_PATH on Windows */ andre@0: #define PR_LD_ALT_SEARCH_PATH 0x10 andre@0: /* 0x8000 reserved for NSPR internal use */ andre@0: andre@0: /* andre@0: ** Load the specified library, in the manner specified by 'flags'. andre@0: */ andre@0: andre@0: NSPR_API(PRLibrary *) andre@0: PR_LoadLibraryWithFlags( andre@0: PRLibSpec libSpec, /* the shared library */ andre@0: PRIntn flags /* flags that affect the loading */ andre@0: ); andre@0: andre@0: /* andre@0: ** Unload a previously loaded library. If the library was a static andre@0: ** library then the static link table will no longer be referenced. The andre@0: ** associated PRLibrary object is freed. andre@0: ** andre@0: ** PR_FAILURE is returned if the library cannot be unloaded. andre@0: ** andre@0: ** This function decrements the reference count of the library. andre@0: */ andre@0: NSPR_API(PRStatus) PR_UnloadLibrary(PRLibrary *lib); andre@0: andre@0: /* andre@0: ** Given the name of a procedure, return the address of the function that andre@0: ** implements the procedure, or NULL if no such function can be andre@0: ** found. This does not find symbols in the main program (the ".exe"); andre@0: ** use PR_LoadStaticLibrary to register symbols in the main program. andre@0: ** andre@0: ** This function does not modify the reference count of the library. andre@0: */ andre@0: NSPR_API(void*) PR_FindSymbol(PRLibrary *lib, const char *name); andre@0: andre@0: /* andre@0: ** Similar to PR_FindSymbol, except that the return value is a pointer to andre@0: ** a function, and not a pointer to void. Casting between a data pointer andre@0: ** and a function pointer is not portable according to the C standard. andre@0: ** Any function pointer can be cast to any other function pointer. andre@0: ** andre@0: ** This function does not modify the reference count of the library. andre@0: */ andre@0: typedef void (*PRFuncPtr)(void); andre@0: NSPR_API(PRFuncPtr) PR_FindFunctionSymbol(PRLibrary *lib, const char *name); andre@0: andre@0: /* andre@0: ** Finds a symbol in one of the currently loaded libraries. Given the andre@0: ** name of a procedure, return the address of the function that andre@0: ** implements the procedure, and return the library that contains that andre@0: ** symbol, or NULL if no such function can be found. This does not find andre@0: ** symbols in the main program (the ".exe"); use PR_AddStaticLibrary to andre@0: ** register symbols in the main program. andre@0: ** andre@0: ** This increments the reference count of the library. andre@0: */ andre@0: NSPR_API(void*) PR_FindSymbolAndLibrary(const char *name, andre@0: PRLibrary* *lib); andre@0: andre@0: /* andre@0: ** Similar to PR_FindSymbolAndLibrary, except that the return value is andre@0: ** a pointer to a function, and not a pointer to void. Casting between a andre@0: ** data pointer and a function pointer is not portable according to the C andre@0: ** standard. Any function pointer can be cast to any other function pointer. andre@0: ** andre@0: ** This increments the reference count of the library. andre@0: */ andre@0: NSPR_API(PRFuncPtr) PR_FindFunctionSymbolAndLibrary(const char *name, andre@0: PRLibrary* *lib); andre@0: andre@0: /* andre@0: ** Register a static link table with the runtime under the name andre@0: ** "name". The symbols present in the static link table will be made andre@0: ** available to PR_FindSymbol. If "name" is null then the symbols will be andre@0: ** made available to the library which represents the executable. The andre@0: ** tables are not copied. andre@0: ** andre@0: ** Returns the library object if successful, null otherwise. andre@0: ** andre@0: ** This increments the reference count of the library. andre@0: */ andre@0: NSPR_API(PRLibrary*) PR_LoadStaticLibrary( andre@0: const char *name, const PRStaticLinkTable *table); andre@0: andre@0: /* andre@0: ** Return the pathname of the file that the library "name" was loaded andre@0: ** from. "addr" is the address of a function defined in the library. andre@0: ** andre@0: ** The caller is responsible for freeing the result with PR_Free. andre@0: */ andre@0: NSPR_API(char *) PR_GetLibraryFilePathname(const char *name, PRFuncPtr addr); andre@0: andre@0: PR_END_EXTERN_C andre@0: andre@0: #endif /* prlink_h___ */