prlink.h 9.2 KB

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