/Modules/_ctypes/darwin/dlfcn_simple.c

http://unladen-swallow.googlecode.com/ · C · 272 lines · 200 code · 28 blank · 44 comment · 23 complexity · f8535d5eef40c9fca989432e2d94bd26 MD5 · raw file

  1. /*
  2. Copyright (c) 2002 Peter O'Gorman <ogorman@users.sourceforge.net>
  3. Permission is hereby granted, free of charge, to any person obtaining
  4. a copy of this software and associated documentation files (the
  5. "Software"), to deal in the Software without restriction, including
  6. without limitation the rights to use, copy, modify, merge, publish,
  7. distribute, sublicense, and/or sell copies of the Software, and to
  8. permit persons to whom the Software is furnished to do so, subject to
  9. the following conditions:
  10. The above copyright notice and this permission notice shall be
  11. included in all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  16. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  17. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  18. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. */
  20. /* Just to prove that it isn't that hard to add Mac calls to your code :)
  21. This works with pretty much everything, including kde3 xemacs and the gimp,
  22. I'd guess that it'd work in at least 95% of cases, use this as your starting
  23. point, rather than the mess that is dlfcn.c, assuming that your code does not
  24. require ref counting or symbol lookups in dependent libraries
  25. */
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <sys/types.h>
  30. #include <sys/stat.h>
  31. #include <stdarg.h>
  32. #include <limits.h>
  33. #include <mach-o/dyld.h>
  34. #include <AvailabilityMacros.h>
  35. #include "dlfcn.h"
  36. #ifdef CTYPES_DARWIN_DLFCN
  37. #define ERR_STR_LEN 256
  38. #ifndef MAC_OS_X_VERSION_10_3
  39. #define MAC_OS_X_VERSION_10_3 1030
  40. #endif
  41. #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
  42. #define DARWIN_HAS_DLOPEN
  43. extern void * dlopen(const char *path, int mode) __attribute__((weak_import));
  44. extern void * dlsym(void * handle, const char *symbol) __attribute__((weak_import));
  45. extern const char * dlerror(void) __attribute__((weak_import));
  46. extern int dlclose(void * handle) __attribute__((weak_import));
  47. extern int dladdr(const void *, Dl_info *) __attribute__((weak_import));
  48. #endif /* MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3 */
  49. #ifndef DARWIN_HAS_DLOPEN
  50. #define dlopen darwin_dlopen
  51. #define dlsym darwin_dlsym
  52. #define dlerror darwin_dlerror
  53. #define dlclose darwin_dlclose
  54. #define dladdr darwin_dladdr
  55. #endif
  56. void * (*ctypes_dlopen)(const char *path, int mode);
  57. void * (*ctypes_dlsym)(void * handle, const char *symbol);
  58. const char * (*ctypes_dlerror)(void);
  59. int (*ctypes_dlclose)(void * handle);
  60. int (*ctypes_dladdr)(const void *, Dl_info *);
  61. #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_3
  62. /* Mac OS X 10.3+ has dlopen, so strip all this dead code to avoid warnings */
  63. static void *dlsymIntern(void *handle, const char *symbol);
  64. static const char *error(int setget, const char *str, ...);
  65. /* Set and get the error string for use by dlerror */
  66. static const char *error(int setget, const char *str, ...)
  67. {
  68. static char errstr[ERR_STR_LEN];
  69. static int err_filled = 0;
  70. const char *retval;
  71. va_list arg;
  72. if (setget == 0)
  73. {
  74. va_start(arg, str);
  75. strncpy(errstr, "dlcompat: ", ERR_STR_LEN);
  76. vsnprintf(errstr + 10, ERR_STR_LEN - 10, str, arg);
  77. va_end(arg);
  78. err_filled = 1;
  79. retval = NULL;
  80. }
  81. else
  82. {
  83. if (!err_filled)
  84. retval = NULL;
  85. else
  86. retval = errstr;
  87. err_filled = 0;
  88. }
  89. return retval;
  90. }
  91. /* darwin_dlopen */
  92. static void *darwin_dlopen(const char *path, int mode)
  93. {
  94. void *module = 0;
  95. NSObjectFileImage ofi = 0;
  96. NSObjectFileImageReturnCode ofirc;
  97. /* If we got no path, the app wants the global namespace, use -1 as the marker
  98. in this case */
  99. if (!path)
  100. return (void *)-1;
  101. /* Create the object file image, works for things linked with the -bundle arg to ld */
  102. ofirc = NSCreateObjectFileImageFromFile(path, &ofi);
  103. switch (ofirc)
  104. {
  105. case NSObjectFileImageSuccess:
  106. /* It was okay, so use NSLinkModule to link in the image */
  107. module = NSLinkModule(ofi, path,
  108. NSLINKMODULE_OPTION_RETURN_ON_ERROR
  109. | (mode & RTLD_GLOBAL) ? 0 : NSLINKMODULE_OPTION_PRIVATE
  110. | (mode & RTLD_LAZY) ? 0 : NSLINKMODULE_OPTION_BINDNOW);
  111. NSDestroyObjectFileImage(ofi);
  112. break;
  113. case NSObjectFileImageInappropriateFile:
  114. /* It may have been a dynamic library rather than a bundle, try to load it */
  115. module = (void *)NSAddImage(path, NSADDIMAGE_OPTION_RETURN_ON_ERROR);
  116. break;
  117. default:
  118. /* God knows what we got */
  119. error(0, "Can not open \"%s\"", path);
  120. return 0;
  121. }
  122. if (!module)
  123. error(0, "Can not open \"%s\"", path);
  124. return module;
  125. }
  126. /* dlsymIntern is used by dlsym to find the symbol */
  127. static void *dlsymIntern(void *handle, const char *symbol)
  128. {
  129. NSSymbol nssym = 0;
  130. /* If the handle is -1, if is the app global context */
  131. if (handle == (void *)-1)
  132. {
  133. /* Global context, use NSLookupAndBindSymbol */
  134. if (NSIsSymbolNameDefined(symbol))
  135. {
  136. nssym = NSLookupAndBindSymbol(symbol);
  137. }
  138. }
  139. /* Now see if the handle is a struch mach_header* or not, use NSLookupSymbol in image
  140. for libraries, and NSLookupSymbolInModule for bundles */
  141. else
  142. {
  143. /* Check for both possible magic numbers depending on x86/ppc byte order */
  144. if ((((struct mach_header *)handle)->magic == MH_MAGIC) ||
  145. (((struct mach_header *)handle)->magic == MH_CIGAM))
  146. {
  147. if (NSIsSymbolNameDefinedInImage((struct mach_header *)handle, symbol))
  148. {
  149. nssym = NSLookupSymbolInImage((struct mach_header *)handle,
  150. symbol,
  151. NSLOOKUPSYMBOLINIMAGE_OPTION_BIND
  152. | NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR);
  153. }
  154. }
  155. else
  156. {
  157. nssym = NSLookupSymbolInModule(handle, symbol);
  158. }
  159. }
  160. if (!nssym)
  161. {
  162. error(0, "Symbol \"%s\" Not found", symbol);
  163. return NULL;
  164. }
  165. return NSAddressOfSymbol(nssym);
  166. }
  167. static const char *darwin_dlerror(void)
  168. {
  169. return error(1, (char *)NULL);
  170. }
  171. static int darwin_dlclose(void *handle)
  172. {
  173. if ((((struct mach_header *)handle)->magic == MH_MAGIC) ||
  174. (((struct mach_header *)handle)->magic == MH_CIGAM))
  175. {
  176. error(0, "Can't remove dynamic libraries on darwin");
  177. return 0;
  178. }
  179. if (!NSUnLinkModule(handle, 0))
  180. {
  181. error(0, "unable to unlink module %s", NSNameOfModule(handle));
  182. return 1;
  183. }
  184. return 0;
  185. }
  186. /* dlsym, prepend the underscore and call dlsymIntern */
  187. static void *darwin_dlsym(void *handle, const char *symbol)
  188. {
  189. static char undersym[257]; /* Saves calls to malloc(3) */
  190. int sym_len = strlen(symbol);
  191. void *value = NULL;
  192. char *malloc_sym = NULL;
  193. if (sym_len < 256)
  194. {
  195. snprintf(undersym, 256, "_%s", symbol);
  196. value = dlsymIntern(handle, undersym);
  197. }
  198. else
  199. {
  200. malloc_sym = malloc(sym_len + 2);
  201. if (malloc_sym)
  202. {
  203. sprintf(malloc_sym, "_%s", symbol);
  204. value = dlsymIntern(handle, malloc_sym);
  205. free(malloc_sym);
  206. }
  207. else
  208. {
  209. error(0, "Unable to allocate memory");
  210. }
  211. }
  212. return value;
  213. }
  214. static int darwin_dladdr(const void *handle, Dl_info *info) {
  215. return 0;
  216. }
  217. #endif /* MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_3 */
  218. #if __GNUC__ < 4
  219. #pragma CALL_ON_LOAD ctypes_dlfcn_init
  220. #else
  221. static void __attribute__ ((constructor)) ctypes_dlfcn_init(void);
  222. static
  223. #endif
  224. void ctypes_dlfcn_init(void) {
  225. if (dlopen != NULL) {
  226. ctypes_dlsym = dlsym;
  227. ctypes_dlopen = dlopen;
  228. ctypes_dlerror = dlerror;
  229. ctypes_dlclose = dlclose;
  230. ctypes_dladdr = dladdr;
  231. } else {
  232. #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_3
  233. ctypes_dlsym = darwin_dlsym;
  234. ctypes_dlopen = darwin_dlopen;
  235. ctypes_dlerror = darwin_dlerror;
  236. ctypes_dlclose = darwin_dlclose;
  237. ctypes_dladdr = darwin_dladdr;
  238. #endif /* MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_3 */
  239. }
  240. }
  241. #endif /* CTYPES_DARWIN_DLFCN */