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