ltdl.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /* ltdl.h -- generic dlopen functions
  2. Copyright (C) 1998-2000 Free Software Foundation, Inc.
  3. Originally by Thomas Tanner <tanner@ffii.org>
  4. This file is part of GNU Libtool.
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2 of the License, or (at your option) any later version.
  9. As a special exception to the GNU Lesser General Public License,
  10. if you distribute this file as part of a program or library that
  11. is built using GNU libtool, you may include it under the same
  12. distribution terms that you use for the rest of that program.
  13. This library is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. Lesser General Public License for more details.
  17. You should have received a copy of the GNU Lesser General Public
  18. License along with this library; if not, write to the Free
  19. Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  20. 02110-1301 USA
  21. */
  22. /* Only include this header file once. */
  23. #ifndef LTDL_H
  24. #define LTDL_H 1
  25. #include <sys/types.h> /* for size_t declaration */
  26. /* --- MACROS FOR PORTABILITY --- */
  27. /* Saves on those hard to debug '\0' typos.... */
  28. #define LT_EOS_CHAR '\0'
  29. /* LTDL_BEGIN_C_DECLS should be used at the beginning of your declarations,
  30. so that C++ compilers don't mangle their names. Use LTDL_END_C_DECLS at
  31. the end of C declarations. */
  32. #ifdef __cplusplus
  33. # define LT_BEGIN_C_DECLS extern "C" {
  34. # define LT_END_C_DECLS }
  35. #else
  36. # define LT_BEGIN_C_DECLS /* empty */
  37. # define LT_END_C_DECLS /* empty */
  38. #endif
  39. LT_BEGIN_C_DECLS
  40. /* LT_PARAMS is a macro used to wrap function prototypes, so that compilers
  41. that don't understand ANSI C prototypes still work, and ANSI C
  42. compilers can issue warnings about type mismatches. */
  43. #if defined (__STDC__) || defined (_AIX) || (defined (__mips) && defined (_SYSTYPE_SVR4)) || defined(WIN32) || defined(__cplusplus)
  44. # define LT_PARAMS(protos) protos
  45. # define lt_ptr void*
  46. #else
  47. # define LT_PARAMS(protos) ()
  48. # define lt_ptr char*
  49. #endif
  50. /* LT_STMT_START/END are used to create macros which expand to a
  51. a single compound statement in a portable way. */
  52. #if defined (__GNUC__) && !defined (__STRICT_ANSI__) && !defined (__cplusplus)
  53. # define LT_STMT_START (void)(
  54. # define LT_STMT_END )
  55. #else
  56. # if (defined (sun) || defined (__sun__))
  57. # define LT_STMT_START if (1)
  58. # define LT_STMT_END else (void)0
  59. # else
  60. # define LT_STMT_START do
  61. # define LT_STMT_END while (0)
  62. # endif
  63. #endif
  64. /* LT_CONC creates a new concatenated symbol for the compiler
  65. in a portable way. */
  66. #if defined(__STDC__) || defined(__cplusplus) || defined(_MSC_VER)
  67. # define LT_CONC(s,t) s##t
  68. #else
  69. # define LT_CONC(s,t) s/**/t
  70. #endif
  71. /* LT_STRLEN can be used safely on NULL pointers. */
  72. #define LT_STRLEN(s) (((s) && (s)[0]) ? strlen (s) : 0)
  73. /* --- WINDOWS SUPPORT --- */
  74. /* Canonicalise Windows and Cygwin recognition macros. */
  75. #ifdef __CYGWIN32__
  76. # ifndef __CYGWIN__
  77. # define __CYGWIN__ __CYGWIN32__
  78. # endif
  79. #endif
  80. #if defined(_WIN32) || defined(WIN32)
  81. # ifndef __WINDOWS__
  82. # ifdef _WIN32
  83. # define __WINDOWS__ _WIN32
  84. # else
  85. # ifdef WIN32
  86. # define __WINDOWS__ WIN32
  87. # endif
  88. # endif
  89. # endif
  90. #endif
  91. #ifdef __WINDOWS__
  92. # ifndef __CYGWIN__
  93. /* LT_DIRSEP_CHAR is accepted *in addition* to '/' as a directory
  94. separator when it is set. */
  95. # define LT_DIRSEP_CHAR '\\'
  96. # define LT_PATHSEP_CHAR ';'
  97. # endif
  98. #endif
  99. #ifndef LT_PATHSEP_CHAR
  100. # define LT_PATHSEP_CHAR ':'
  101. #endif
  102. /* DLL building support on win32 hosts; mostly to workaround their
  103. ridiculous implementation of data symbol exporting. */
  104. #ifndef LT_SCOPE
  105. # ifdef __WINDOWS__
  106. # ifdef LIBLTDL_DLL_EXPORT /* don't define or ld disables auto-export. */
  107. # define LT_SCOPE __declspec(dllexport)
  108. # endif
  109. # ifdef LIBLTDL_DLL_IMPORT /* define if linking with this dll */
  110. # define LT_SCOPE extern __declspec(dllimport)
  111. # endif
  112. # endif
  113. # ifndef LT_SCOPE /* static linking or !__WINDOWS__ */
  114. # define LT_SCOPE extern
  115. # endif
  116. #endif
  117. #if defined(_MSC_VER) /* Visual Studio */
  118. # define R_OK 4
  119. #endif
  120. /* --- DYNAMIC MODULE LOADING API --- */
  121. typedef struct lt_dlhandle_struct *lt_dlhandle; /* A loaded module. */
  122. /* Initialisation and finalisation functions for libltdl. */
  123. LT_SCOPE int lt_dlinit LT_PARAMS((void));
  124. LT_SCOPE int lt_dlexit LT_PARAMS((void));
  125. /* Module search path manipulation. */
  126. LT_SCOPE int lt_dladdsearchdir LT_PARAMS((const char *search_dir));
  127. LT_SCOPE int lt_dlinsertsearchdir LT_PARAMS((const char *before,
  128. const char *search_dir));
  129. LT_SCOPE int lt_dlsetsearchpath LT_PARAMS((const char *search_path));
  130. LT_SCOPE const char *lt_dlgetsearchpath LT_PARAMS((void));
  131. LT_SCOPE int lt_dlforeachfile LT_PARAMS((
  132. const char *search_path,
  133. int (*func) (const char *filename, lt_ptr data),
  134. lt_ptr data));
  135. /* Portable libltdl versions of the system dlopen() API. */
  136. LT_SCOPE lt_dlhandle lt_dlopen LT_PARAMS((const char *filename));
  137. LT_SCOPE lt_dlhandle lt_dlopenext LT_PARAMS((const char *filename));
  138. LT_SCOPE lt_ptr lt_dlsym LT_PARAMS((lt_dlhandle handle,
  139. const char *name));
  140. LT_SCOPE const char *lt_dlerror LT_PARAMS((void));
  141. LT_SCOPE int lt_dlclose LT_PARAMS((lt_dlhandle handle));
  142. /* Module residency management. */
  143. LT_SCOPE int lt_dlmakeresident LT_PARAMS((lt_dlhandle handle));
  144. LT_SCOPE int lt_dlisresident LT_PARAMS((lt_dlhandle handle));
  145. /* --- MUTEX LOCKING --- */
  146. typedef void lt_dlmutex_lock LT_PARAMS((void));
  147. typedef void lt_dlmutex_unlock LT_PARAMS((void));
  148. typedef void lt_dlmutex_seterror LT_PARAMS((const char *errmsg));
  149. typedef const char *lt_dlmutex_geterror LT_PARAMS((void));
  150. LT_SCOPE int lt_dlmutex_register LT_PARAMS((lt_dlmutex_lock *lock,
  151. lt_dlmutex_unlock *unlock,
  152. lt_dlmutex_seterror *seterror,
  153. lt_dlmutex_geterror *geterror));
  154. /* --- MEMORY HANDLING --- */
  155. /* By default, the realloc function pointer is set to our internal
  156. realloc implementation which iself uses lt_dlmalloc and lt_dlfree.
  157. libltdl relies on a featureful realloc, but if you are sure yours
  158. has the right semantics then you can assign it directly. Generally,
  159. it is safe to assign just a malloc() and a free() function. */
  160. LT_SCOPE lt_ptr (*lt_dlmalloc) LT_PARAMS((size_t size));
  161. LT_SCOPE lt_ptr (*lt_dlrealloc) LT_PARAMS((lt_ptr ptr, size_t size));
  162. LT_SCOPE void (*lt_dlfree) LT_PARAMS((lt_ptr ptr));
  163. /* --- PRELOADED MODULE SUPPORT --- */
  164. /* A preopened symbol. Arrays of this type comprise the exported
  165. symbols for a dlpreopened module. */
  166. typedef struct {
  167. const char *name;
  168. lt_ptr address;
  169. } lt_dlsymlist;
  170. LT_SCOPE int lt_dlpreload LT_PARAMS((const lt_dlsymlist *preloaded));
  171. LT_SCOPE int lt_dlpreload_default
  172. LT_PARAMS((const lt_dlsymlist *preloaded));
  173. #define LTDL_SET_PRELOADED_SYMBOLS() LT_STMT_START{ \
  174. extern const lt_dlsymlist lt_preloaded_symbols[]; \
  175. lt_dlpreload_default(lt_preloaded_symbols); \
  176. }LT_STMT_END
  177. /* --- MODULE INFORMATION --- */
  178. /* Read only information pertaining to a loaded module. */
  179. typedef struct {
  180. char *filename; /* file name */
  181. char *name; /* module name */
  182. int ref_count; /* number of times lt_dlopened minus
  183. number of times lt_dlclosed. */
  184. } lt_dlinfo;
  185. LT_SCOPE const lt_dlinfo *lt_dlgetinfo LT_PARAMS((lt_dlhandle handle));
  186. LT_SCOPE lt_dlhandle lt_dlhandle_next LT_PARAMS((lt_dlhandle place));
  187. LT_SCOPE int lt_dlforeach LT_PARAMS((
  188. int (*func) (lt_dlhandle handle, lt_ptr data),
  189. lt_ptr data));
  190. /* Associating user data with loaded modules. */
  191. typedef unsigned lt_dlcaller_id;
  192. LT_SCOPE lt_dlcaller_id lt_dlcaller_register LT_PARAMS((void));
  193. LT_SCOPE lt_ptr lt_dlcaller_set_data LT_PARAMS((lt_dlcaller_id key,
  194. lt_dlhandle handle,
  195. lt_ptr data));
  196. LT_SCOPE lt_ptr lt_dlcaller_get_data LT_PARAMS((lt_dlcaller_id key,
  197. lt_dlhandle handle));
  198. /* --- USER MODULE LOADER API --- */
  199. typedef struct lt_dlloader lt_dlloader;
  200. typedef lt_ptr lt_user_data;
  201. typedef lt_ptr lt_module;
  202. /* Function pointer types for creating user defined module loaders. */
  203. typedef lt_module lt_module_open LT_PARAMS((lt_user_data loader_data,
  204. const char *filename));
  205. typedef int lt_module_close LT_PARAMS((lt_user_data loader_data,
  206. lt_module handle));
  207. typedef lt_ptr lt_find_sym LT_PARAMS((lt_user_data loader_data,
  208. lt_module handle,
  209. const char *symbol));
  210. typedef int lt_dlloader_exit LT_PARAMS((lt_user_data loader_data));
  211. struct lt_user_dlloader {
  212. const char *sym_prefix;
  213. lt_module_open *module_open;
  214. lt_module_close *module_close;
  215. lt_find_sym *find_sym;
  216. lt_dlloader_exit *dlloader_exit;
  217. lt_user_data dlloader_data;
  218. };
  219. LT_SCOPE lt_dlloader *lt_dlloader_next LT_PARAMS((lt_dlloader *place));
  220. LT_SCOPE lt_dlloader *lt_dlloader_find LT_PARAMS((
  221. const char *loader_name));
  222. LT_SCOPE const char *lt_dlloader_name LT_PARAMS((lt_dlloader *place));
  223. LT_SCOPE lt_user_data *lt_dlloader_data LT_PARAMS((lt_dlloader *place));
  224. LT_SCOPE int lt_dlloader_add LT_PARAMS((lt_dlloader *place,
  225. const struct lt_user_dlloader *dlloader,
  226. const char *loader_name));
  227. LT_SCOPE int lt_dlloader_remove LT_PARAMS((
  228. const char *loader_name));
  229. /* --- ERROR MESSAGE HANDLING --- */
  230. /* Defining error strings alongside their symbolic names in a macro in
  231. this way allows us to expand the macro in different contexts with
  232. confidence that the enumeration of symbolic names will map correctly
  233. onto the table of error strings. */
  234. #define lt_dlerror_table \
  235. LT_ERROR(UNKNOWN, "unknown error") \
  236. LT_ERROR(DLOPEN_NOT_SUPPORTED, "dlopen support not available") \
  237. LT_ERROR(INVALID_LOADER, "invalid loader") \
  238. LT_ERROR(INIT_LOADER, "loader initialization failed") \
  239. LT_ERROR(REMOVE_LOADER, "loader removal failed") \
  240. LT_ERROR(FILE_NOT_FOUND, "file not found") \
  241. LT_ERROR(DEPLIB_NOT_FOUND, "dependency library not found") \
  242. LT_ERROR(NO_SYMBOLS, "no symbols defined") \
  243. LT_ERROR(CANNOT_OPEN, "can't open the module") \
  244. LT_ERROR(CANNOT_CLOSE, "can't close the module") \
  245. LT_ERROR(SYMBOL_NOT_FOUND, "symbol not found") \
  246. LT_ERROR(NO_MEMORY, "not enough memory") \
  247. LT_ERROR(INVALID_HANDLE, "invalid module handle") \
  248. LT_ERROR(BUFFER_OVERFLOW, "internal buffer overflow") \
  249. LT_ERROR(INVALID_ERRORCODE, "invalid errorcode") \
  250. LT_ERROR(SHUTDOWN, "library already shutdown") \
  251. LT_ERROR(CLOSE_RESIDENT_MODULE, "can't close resident module") \
  252. LT_ERROR(INVALID_MUTEX_ARGS, "invalid mutex handler registration") \
  253. LT_ERROR(INVALID_POSITION, "invalid search path insert position")
  254. /* Enumerate the symbolic error names. */
  255. enum {
  256. #define LT_ERROR(name, diagnostic) LT_CONC(LT_ERROR_, name),
  257. lt_dlerror_table
  258. #undef LT_ERROR
  259. LT_ERROR_MAX
  260. };
  261. /* These functions are only useful from inside custom module loaders. */
  262. LT_SCOPE int lt_dladderror LT_PARAMS((const char *diagnostic));
  263. LT_SCOPE int lt_dlseterror LT_PARAMS((int errorcode));
  264. /* --- SOURCE COMPATIBILITY WITH OLD LIBLTDL --- */
  265. #ifdef LT_NON_POSIX_NAMESPACE
  266. # define lt_ptr_t lt_ptr
  267. # define lt_module_t lt_module
  268. # define lt_module_open_t lt_module_open
  269. # define lt_module_close_t lt_module_close
  270. # define lt_find_sym_t lt_find_sym
  271. # define lt_dlloader_exit_t lt_dlloader_exit
  272. # define lt_dlloader_t lt_dlloader
  273. # define lt_dlloader_data_t lt_user_data
  274. #endif
  275. LT_END_C_DECLS
  276. #endif /* !LTDL_H */