NSPR Reference Previous Contents Next |
Library Linking Types
Library Linking Functions
Platform Notes
PRLibrary
is an opaque structure. A reference to such a structure can be
returned by some of the functions in the runtime and serve to identify a particular
instance of a library.
typedef struct PRLibrary PRLibrary;
typedef struct PRStaticLinkTable {
const char *name;
void (*fp)();
} PRStaticLinkTable;
PR_SetLibraryPath
PR_GetLibraryPath
PR_GetLibraryName
PR_FreeLibraryName
PR_LoadLibrary
PR_UnloadLibrary
PR_FindSymbol
PR_FindSymbolAndLibrary
PRStatus PR_SetLibraryPath(const char *path);
PR_SUCCESS
.
If unsuccessful, PR_FAILURE
. This may indicate that the function cannot
allocate sufficient storage to make a copy of the path
string
char* PR_GetLibraryPath(void);
NULL
.
NULL
. Storage for the result is allocated by the runtime and becomes the
responsibility of the caller. When it is no longer used, free it using
PR_FreeLibraryName
.
char* PR_GetLibraryName (
const char *dir,
const char *lib);
dir
|
A NULL -terminated string representing the path name of the library, as
returned by PR_GetLibraryPath .
|
lib
|
The leaf name of the library of interest.
|
NULL
.
PR_LoadLibrary
call.
This function does not test for existence of the specified file, it just constructs the full filename. The way the name is constructed is system dependent.
If sufficient storage cannot be allocated to contain the constructed path name, the
function returns NULL
. Storage for the result is allocated by the runtime and
becomes the responsibility of the caller. When it is no longer used, free it using
PR_FreeLibraryName
.
void PR_FreeLibraryName(char *mem);
mem
|
A reference to a character array that was previously allocated by the
dynamic library runtime.
|
malloc
in order to isolate the runtime's semantics regarding storage
management.
PRLibrary* PR_LoadLibrary(const char *name);
name
|
A platform-dependent character array that names the library to be
loaded, as returned by PR_GetLibraryName .
|
PRLibrary
object.
If the operation fails, returns NULL
. Use PR_GetError
to find the reason for the
failure.
Each call to PR_LoadLibrary
must be paired with a corresponding call to
PR_UnloadLibrary
in order to return the runtime to its original state.
PR_LoadLibrary
.
PRStatus PR_UnloadLibrary(PRLibrary *lib);
lib
|
A reference previously returned from PR_LoadLibrary .
|
PR_SUCCESS
.
If unsuccessful, PR_FAILURE
. Use PR_GetError
to find the reason for the
failure.
PR_LoadLibrary
. After calling this function,
future references to the library using its identity as returned by PR_LoadLibrary
will be invalid.
PR_FindSymbol()
will return an untyped reference to a symbol in a particular
library given the identity of the library and a textual representation of the symbol
in question.
void* PR_FindSymbol (
PRLibrary*lib,
const char *name);
lib
|
A valid reference to a loaded library, as returned by
PR_LoadLibrary , or NULL .
|
name
|
A textual representation of the symbol to resolve.
|
lib
parameter is NULL
, all libraries known to the runtime
and the main program are searched in an unspecified order.
Use this function to look up functions or data symbols in a shared library. Getting a
pointer to a symbol in a library does indicate that the library is available when the
search was made. The runtime does nothing to ensure the continued validity of the
symbol. If the library is unloaded, for instance, the results of any PR_FindSymbol
calls become invalid as well.
void* PR_FindSymbolAndLibrary (
PRLibrary
const char *name,
**lib);
NULL
pointer to the found symbol, and stores a pointer
to the library in which it was found at the location pointed to by lib
.
If the symbol could not be found, returns NULL
.
lib
contains a pointer to the library that contains that symbol. The
location must be pre-allocated by the caller.
The function returns NULL
if no such function can be found. The order in which the
known libraries are searched in not specified.This function is equivalent to calling
first PR_LoadLibrary
, then PR_FindSymbol
.
The identity returned from this function must be the target of a PR_UnloadLibrary
in order to return the runtime to its original state.
PR_LoadLibrary
cannot open a handle that references the main executable
program. (This is admittedly an omission that should be fixed.) However, it is
possible to look up symbols defined in the main executable program as follows.
PRLibrary *lib;
void *funcPtr;
funcPtr = PR_FindSymbolAndLibrary("FunctionName", &lib);
When PR_FindSymbolAndLibrary
returns, funcPtr
is the value of the function
pointer you want to look up, and the variable lib
references the main executable
program. You can then call PR_FindSymbol
on lib
to look up other symbols
defined in the main program. Remember to call PR_UnloadLibrary
(lib)
to close
the library handle when you are done.
This section summarizes these platform idiosyncrasies. For more information,
consult the man pages for ld
and dlopen
(or shl_load
on HP-UX) for Unix, and
the LoadLibrary
documentation for Win32.
Dynamic Library Search Path
Exporting Symbols from the Main Executable Program
LD_LIBRARY_PATH
. These
systems typically use dlopen
to load a dynamic library.
HP-UX uses shl_load
to load dynamic libraries, and the environment variable
specifying the dynamic library search path is SHLIB_PATH
. Moreover, the
executable program must be linked with the +s
option so that it will search for
shared libraries in the directories specified by SHLIB_PATH
at run time.
Alternatively, you can enable the +s
option as a postprocessing step using the
chatr
tool. For example, link your executable program a.out
without the +s
option, then execute the following:
chatr +s enable a.out
On Rhapsody, the environment variable is DYLD_LIBRARY_PATH
.
On Win32, the environment variable is PATH
. The same search path is used to
search for executable programs and DLLs.
-E
linker option in order to export all symbols in the main program to shared libraries.
If you use the GNU compilers
(on any platform), you must also link the executable
program with the -E
option.
Last Updated May 18, 2001