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