Upload Tizen:Base source
[toolchains/nspr.git] / mozilla / nsprpub / pr / include / prlink.h
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4  *
5  * The contents of this file are subject to the Mozilla Public License Version
6  * 1.1 (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  * http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the
13  * License.
14  *
15  * The Original Code is the Netscape Portable Runtime (NSPR).
16  *
17  * The Initial Developer of the Original Code is
18  * Netscape Communications Corporation.
19  * Portions created by the Initial Developer are Copyright (C) 1998-2000
20  * the Initial Developer. All Rights Reserved.
21  *
22  * Contributor(s):
23  *
24  * Alternatively, the contents of this file may be used under the terms of
25  * either the GNU General Public License Version 2 or later (the "GPL"), or
26  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27  * in which case the provisions of the GPL or the LGPL are applicable instead
28  * of those above. If you wish to allow use of your version of this file only
29  * under the terms of either the GPL or the LGPL, and not to allow others to
30  * use your version of this file under the terms of the MPL, indicate your
31  * decision by deleting the provisions above and replace them with the notice
32  * and other provisions required by the GPL or the LGPL. If you do not delete
33  * the provisions above, a recipient may use your version of this file under
34  * the terms of any one of the MPL, the GPL or the LGPL.
35  *
36  * ***** END LICENSE BLOCK ***** */
37
38 #ifndef prlink_h___
39 #define prlink_h___
40
41 /*
42 ** API to static and dynamic linking.
43 */
44 #include "prtypes.h"
45
46 PR_BEGIN_EXTERN_C
47
48 typedef struct PRLibrary PRLibrary;
49
50 typedef struct PRStaticLinkTable {
51     const char *name;
52     void (*fp)(void);
53 } PRStaticLinkTable;
54
55 /*
56 ** Change the default library path to the given string. The string is
57 ** copied. This call will fail if it runs out of memory.
58 **
59 ** The string provided as 'path' is copied. The caller can do whatever is
60 ** convenient with the argument when the function is complete.
61 */
62 NSPR_API(PRStatus) PR_SetLibraryPath(const char *path);
63
64 /*
65 ** Return a character string which contains the path used to search for
66 ** dynamically loadable libraries.
67 **
68 ** The returned value is basically a copy of a PR_SetLibraryPath().
69 ** The storage is allocated by the runtime and becomes the responsibilty
70 ** of the caller.
71 */
72 NSPR_API(char*) PR_GetLibraryPath(void);
73
74 /*
75 ** Given a directory name "dir" and a library name "lib" construct a full
76 ** path name that will refer to the actual dynamically loaded
77 ** library. This does not test for existance of said file, it just
78 ** constructs the full filename. The name constructed is system dependent
79 ** and prepared for PR_LoadLibrary. The result must be free'd when the
80 ** caller is done with it.
81 **
82 ** The storage for the result is allocated by the runtime and becomes the
83 ** responsibility of the caller.
84 */
85 NSPR_API(char*) PR_GetLibraryName(const char *dir, const char *lib);
86
87 /*
88 **
89 ** Free the memory allocated, for the caller, by PR_GetLibraryName
90 */
91 NSPR_API(void) PR_FreeLibraryName(char *mem);
92
93 /*
94 ** Given a library "name" try to load the library. The argument "name"
95 ** is a machine-dependent name for the library, such as the full pathname
96 ** returned by PR_GetLibraryName.  If the library is already loaded,
97 ** this function will avoid loading the library twice.
98 **
99 ** If the library is loaded successfully, then a pointer to the PRLibrary
100 ** structure representing the library is returned.  Otherwise, NULL is
101 ** returned.
102 **
103 ** This increments the reference count of the library.
104 */
105 NSPR_API(PRLibrary*) PR_LoadLibrary(const char *name);
106
107 /*
108 ** Each operating system has its preferred way of specifying
109 ** a file in the file system.  Most operating systems use
110 ** a pathname.  Mac OS Classic, on the other hand, uses the FSSpec
111 ** structure to specify a file. PRLibSpec allows NSPR clients
112 ** to use the type of file specification that is most efficient
113 ** for a particular platform.
114 **
115 ** On some operating systems such as Mac OS Classic, a shared library
116 ** may contain code fragments that can be individually loaded.
117 ** PRLibSpec also allows NSPR clients to identify a code fragment
118 ** in a library, if code fragments are supported by the OS.
119 ** A code fragment can be specified by name or by an integer index.
120 **
121 ** Right now PRLibSpec supports four types of library specification:
122 ** a pathname in the native character encoding, a Mac code fragment
123 ** by name, a Mac code fragment by index, and a UTF-16 pathname.
124 */
125
126 typedef enum PRLibSpecType {
127     PR_LibSpec_Pathname,
128     PR_LibSpec_MacNamedFragment,   /* obsolete (for Mac OS Classic) */
129     PR_LibSpec_MacIndexedFragment, /* obsolete (for Mac OS Classic) */
130     PR_LibSpec_PathnameU           /* supported only on Win32 */ 
131 } PRLibSpecType;
132
133 struct FSSpec; /* Mac OS Classic FSSpec */
134
135 typedef struct PRLibSpec {
136     PRLibSpecType type;
137     union {
138         /* if type is PR_LibSpec_Pathname */
139         const char *pathname;
140
141         /* if type is PR_LibSpec_MacNamedFragment */
142         struct {
143             const struct FSSpec *fsspec;
144             const char *name;
145         } mac_named_fragment;      /* obsolete (for Mac OS Classic) */
146
147         /* if type is PR_LibSpec_MacIndexedFragment */
148         struct {
149             const struct FSSpec *fsspec;
150             PRUint32 index;
151         } mac_indexed_fragment;    /* obsolete (for Mac OS Classic) */
152
153         /* if type is PR_LibSpec_PathnameU */
154         const PRUnichar *pathname_u; /* supported only on Win32 */
155     } value;
156 } PRLibSpec;
157
158 /*
159 ** The following bit flags may be or'd together and passed
160 ** as the 'flags' argument to PR_LoadLibraryWithFlags.
161 ** Flags not supported by the underlying OS are ignored.
162 */
163
164 #define PR_LD_LAZY   0x1  /* equivalent to RTLD_LAZY on Unix */
165 #define PR_LD_NOW    0x2  /* equivalent to RTLD_NOW on Unix */
166 #define PR_LD_GLOBAL 0x4  /* equivalent to RTLD_GLOBAL on Unix */
167 #define PR_LD_LOCAL  0x8  /* equivalent to RTLD_LOCAL on Unix */
168 /* The following is equivalent to LOAD_WITH_ALTERED_SEARCH_PATH on Windows */
169 #define PR_LD_ALT_SEARCH_PATH  0x10  
170 /*                0x8000     reserved for NSPR internal use */
171
172 /*
173 ** Load the specified library, in the manner specified by 'flags'.
174 */
175
176 NSPR_API(PRLibrary *)
177 PR_LoadLibraryWithFlags(
178     PRLibSpec libSpec,    /* the shared library */
179     PRIntn flags          /* flags that affect the loading */
180 );
181
182 /*
183 ** Unload a previously loaded library. If the library was a static
184 ** library then the static link table will no longer be referenced. The
185 ** associated PRLibrary object is freed.
186 **
187 ** PR_FAILURE is returned if the library cannot be unloaded.
188 **
189 ** This function decrements the reference count of the library.
190 */
191 NSPR_API(PRStatus) PR_UnloadLibrary(PRLibrary *lib);
192
193 /*
194 ** Given the name of a procedure, return the address of the function that
195 ** implements the procedure, or NULL if no such function can be
196 ** found. This does not find symbols in the main program (the ".exe");
197 ** use PR_LoadStaticLibrary to register symbols in the main program.
198 **
199 ** This function does not modify the reference count of the library.
200 */
201 NSPR_API(void*) PR_FindSymbol(PRLibrary *lib, const char *name);
202
203 /*
204 ** Similar to PR_FindSymbol, except that the return value is a pointer to
205 ** a function, and not a pointer to void. Casting between a data pointer
206 ** and a function pointer is not portable according to the C standard.
207 ** Any function pointer can be cast to any other function pointer.
208 **
209 ** This function does not modify the reference count of the library.
210 */
211 typedef void (*PRFuncPtr)(void);
212 NSPR_API(PRFuncPtr) PR_FindFunctionSymbol(PRLibrary *lib, const char *name);
213
214 /*
215 ** Finds a symbol in one of the currently loaded libraries. Given the
216 ** name of a procedure, return the address of the function that
217 ** implements the procedure, and return the library that contains that
218 ** symbol, or NULL if no such function can be found. This does not find
219 ** symbols in the main program (the ".exe"); use PR_AddStaticLibrary to
220 ** register symbols in the main program.  
221 **
222 ** This increments the reference count of the library.
223 */
224 NSPR_API(void*) PR_FindSymbolAndLibrary(const char *name,
225                                                       PRLibrary* *lib);
226
227 /*
228 ** Similar to PR_FindSymbolAndLibrary, except that the return value is
229 ** a pointer to a function, and not a pointer to void. Casting between a
230 ** data pointer and a function pointer is not portable according to the C
231 ** standard. Any function pointer can be cast to any other function pointer.
232 **
233 ** This increments the reference count of the library.
234 */
235 NSPR_API(PRFuncPtr) PR_FindFunctionSymbolAndLibrary(const char *name,
236                                                       PRLibrary* *lib);
237
238 /*
239 ** Register a static link table with the runtime under the name
240 ** "name". The symbols present in the static link table will be made
241 ** available to PR_FindSymbol. If "name" is null then the symbols will be
242 ** made available to the library which represents the executable. The
243 ** tables are not copied.
244 **
245 ** Returns the library object if successful, null otherwise.
246 **
247 ** This increments the reference count of the library.
248 */
249 NSPR_API(PRLibrary*) PR_LoadStaticLibrary(
250     const char *name, const PRStaticLinkTable *table);
251
252 /*
253 ** Return the pathname of the file that the library "name" was loaded
254 ** from. "addr" is the address of a function defined in the library.
255 **
256 ** The caller is responsible for freeing the result with PR_Free.
257 */
258 NSPR_API(char *) PR_GetLibraryFilePathname(const char *name, PRFuncPtr addr);
259
260 PR_END_EXTERN_C
261
262 #endif /* prlink_h___ */