prlink.h 8.2 KB

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