Back to index

lightning-sunbird  0.9+nobinonly
prlink.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Netscape Portable Runtime (NSPR).
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #ifndef prlink_h___
00039 #define prlink_h___
00040 
00041 /*
00042 ** API to static and dynamic linking.
00043 */
00044 #include "prtypes.h"
00045 
00046 PR_BEGIN_EXTERN_C
00047 
00048 typedef struct PRLibrary PRLibrary;
00049 
00050 typedef struct PRStaticLinkTable {
00051     const char *name;
00052     void (*fp)();
00053 } PRStaticLinkTable;
00054 
00055 /*
00056 ** Change the default library path to the given string. The string is
00057 ** copied. This call will fail if it runs out of memory.
00058 **
00059 ** The string provided as 'path' is copied. The caller can do whatever is
00060 ** convenient with the argument when the function is complete.
00061 */
00062 NSPR_API(PRStatus) PR_SetLibraryPath(const char *path);
00063 
00064 /*
00065 ** Return a character string which contains the path used to search for
00066 ** dynamically loadable libraries.
00067 **
00068 ** The returned value is basically a copy of a PR_SetLibraryPath().
00069 ** The storage is allocated by the runtime and becomes the responsibilty
00070 ** of the caller.
00071 */
00072 NSPR_API(char*) PR_GetLibraryPath(void);
00073 
00074 /*
00075 ** Given a directory name "dir" and a library name "lib" construct a full
00076 ** path name that will refer to the actual dynamically loaded
00077 ** library. This does not test for existance of said file, it just
00078 ** constructs the full filename. The name constructed is system dependent
00079 ** and prepared for PR_LoadLibrary. The result must be free'd when the
00080 ** caller is done with it.
00081 **
00082 ** The storage for the result is allocated by the runtime and becomes the
00083 ** responsibility of the caller.
00084 */
00085 NSPR_API(char*) PR_GetLibraryName(const char *dir, const char *lib);
00086 
00087 /*
00088 **
00089 ** Free the memory allocated, for the caller, by PR_GetLibraryName
00090 */
00091 NSPR_API(void) PR_FreeLibraryName(char *mem);
00092 
00093 /*
00094 ** Given a library "name" try to load the library. The argument "name"
00095 ** is a machine-dependent name for the library, such as the full pathname
00096 ** returned by PR_GetLibraryName.  If the library is already loaded,
00097 ** this function will avoid loading the library twice.
00098 **
00099 ** If the library is loaded successfully, then a pointer to the PRLibrary
00100 ** structure representing the library is returned.  Otherwise, NULL is
00101 ** returned.
00102 **
00103 ** This increments the reference count of the library.
00104 */
00105 NSPR_API(PRLibrary*) PR_LoadLibrary(const char *name);
00106 
00107 /*
00108 ** Each operating system has its preferred way of specifying
00109 ** a file in the file system.  Most operating systems use
00110 ** a pathname.  Mac OS, on the other hand, uses the FSSpec
00111 ** structure to specify a file. PRLibSpec allows NSPR clients
00112 ** to use the type of file specification that is most efficient
00113 ** for a particular platform.
00114 **
00115 ** On some operating systems such as Mac OS, a shared library may
00116 ** contain code fragments that can be individually loaded.
00117 ** PRLibSpec also allows NSPR clients to identify a code fragment
00118 ** in a library, if code fragments are supported by the OS.
00119 ** A code fragment can be specified by name or by an integer index.
00120 **
00121 ** Right now PRLibSpec supports four types of library specification:
00122 ** a pathname in the native character encoding, a Mac code fragment
00123 ** by name, a Mac code fragment by index, and a UTF-16 pathname.
00124 */
00125 
00126 typedef enum PRLibSpecType {
00127     PR_LibSpec_Pathname,
00128     PR_LibSpec_MacNamedFragment,   /* obsolete (for Mac OS Classic) */
00129     PR_LibSpec_MacIndexedFragment, /* obsolete (for Mac OS Classic) */
00130     PR_LibSpec_PathnameU           /* supported only on Win32 */ 
00131 } PRLibSpecType;
00132 
00133 struct FSSpec; /* Mac OS FSSpec */
00134 
00135 typedef struct PRLibSpec {
00136     PRLibSpecType type;
00137     union {
00138         /* if type is PR_LibSpec_Pathname */
00139         const char *pathname;
00140 
00141         /* if type is PR_LibSpec_MacNamedFragment */
00142         struct {
00143             const struct FSSpec *fsspec;
00144             const char *name;
00145         } mac_named_fragment;      /* obsolete (for Mac OS Classic) */
00146 
00147         /* if type is PR_LibSpec_MacIndexedFragment */
00148         struct {
00149             const struct FSSpec *fsspec;
00150             PRUint32 index;
00151         } mac_indexed_fragment;    /* obsolete (for Mac OS Classic) */
00152 
00153         /* if type is PR_LibSpec_PathnameU */
00154         const PRUnichar *pathname_u; /* supported only on Win32 */
00155     } value;
00156 } PRLibSpec;
00157 
00158 /*
00159 ** The following bit flags may be or'd together and passed
00160 ** as the 'flags' argument to PR_LoadLibraryWithFlags.
00161 ** Flags not supported by the underlying OS are ignored.
00162 */
00163 
00164 #define PR_LD_LAZY   0x1  /* equivalent to RTLD_LAZY on Unix */
00165 #define PR_LD_NOW    0x2  /* equivalent to RTLD_NOW on Unix */
00166 #define PR_LD_GLOBAL 0x4  /* equivalent to RTLD_GLOBAL on Unix */
00167 #define PR_LD_LOCAL  0x8  /* equivalent to RTLD_LOCAL on Unix */
00168 /*                0x8000     reserved for NSPR internal use */
00169 
00170 /*
00171 ** Load the specified library, in the manner specified by 'flags'.
00172 */
00173 
00174 NSPR_API(PRLibrary *)
00175 PR_LoadLibraryWithFlags(
00176     PRLibSpec libSpec,    /* the shared library */
00177     PRIntn flags          /* flags that affect the loading */
00178 );
00179 
00180 /*
00181 ** Unload a previously loaded library. If the library was a static
00182 ** library then the static link table will no longer be referenced. The
00183 ** associated PRLibrary object is freed.
00184 **
00185 ** PR_FAILURE is returned if the library cannot be unloaded.
00186 **
00187 ** This function decrements the reference count of the library.
00188 */
00189 NSPR_API(PRStatus) PR_UnloadLibrary(PRLibrary *lib);
00190 
00191 /*
00192 ** Given the name of a procedure, return the address of the function that
00193 ** implements the procedure, or NULL if no such function can be
00194 ** found. This does not find symbols in the main program (the ".exe");
00195 ** use PR_LoadStaticLibrary to register symbols in the main program.
00196 **
00197 ** This function does not modify the reference count of the library.
00198 */
00199 NSPR_API(void*) PR_FindSymbol(PRLibrary *lib, const char *name);
00200 
00201 /*
00202 ** Similar to PR_FindSymbol, except that the return value is a pointer to
00203 ** a function, and not a pointer to void. Casting between a data pointer
00204 ** and a function pointer is not portable according to the C standard.
00205 ** Any function pointer can be cast to any other function pointer.
00206 **
00207 ** This function does not modify the reference count of the library.
00208 */
00209 typedef void (*PRFuncPtr)();
00210 NSPR_API(PRFuncPtr) PR_FindFunctionSymbol(PRLibrary *lib, const char *name);
00211 
00212 /*
00213 ** Finds a symbol in one of the currently loaded libraries. Given the
00214 ** name of a procedure, return the address of the function that
00215 ** implements the procedure, and return the library that contains that
00216 ** symbol, or NULL if no such function can be found. This does not find
00217 ** symbols in the main program (the ".exe"); use PR_AddStaticLibrary to
00218 ** register symbols in the main program.  
00219 **
00220 ** This increments the reference count of the library.
00221 */
00222 NSPR_API(void*) PR_FindSymbolAndLibrary(const char *name,
00223                                                 PRLibrary* *lib);
00224 
00225 /*
00226 ** Similar to PR_FindSymbolAndLibrary, except that the return value is
00227 ** a pointer to a function, and not a pointer to void. Casting between a
00228 ** data pointer and a function pointer is not portable according to the C
00229 ** standard. Any function pointer can be cast to any other function pointer.
00230 **
00231 ** This increments the reference count of the library.
00232 */
00233 NSPR_API(PRFuncPtr) PR_FindFunctionSymbolAndLibrary(const char *name,
00234                                                 PRLibrary* *lib);
00235 
00236 /*
00237 ** Register a static link table with the runtime under the name
00238 ** "name". The symbols present in the static link table will be made
00239 ** available to PR_FindSymbol. If "name" is null then the symbols will be
00240 ** made available to the library which represents the executable. The
00241 ** tables are not copied.
00242 **
00243 ** Returns the library object if successful, null otherwise.
00244 **
00245 ** This increments the reference count of the library.
00246 */
00247 NSPR_API(PRLibrary*) PR_LoadStaticLibrary(
00248     const char *name, const PRStaticLinkTable *table);
00249 
00250 /*
00251 ** Return the pathname of the file that the library "name" was loaded
00252 ** from. "addr" is the address of a function defined in the library.
00253 **
00254 ** The caller is responsible for freeing the result with PR_Free.
00255 */
00256 NSPR_API(char *) PR_GetLibraryFilePathname(const char *name, PRFuncPtr addr);
00257 
00258 PR_END_EXTERN_C
00259 
00260 #endif /* prlink_h___ */