You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
3.9 KiB

  1. /* Support for dynamic loading of extension modules on Mac OS X
  2. ** All references to "NeXT" are for historical reasons.
  3. */
  4. #include "Python.h"
  5. #include "importdl.h"
  6. #include <mach-o/dyld.h>
  7. const struct filedescr _PyImport_DynLoadFiletab[] = {
  8. {".so", "rb", C_EXTENSION},
  9. {0, 0}
  10. };
  11. /*
  12. ** Python modules are Mach-O MH_BUNDLE files. The best way to load these
  13. ** is each in a private namespace, so you can load, say, a module bar and a
  14. ** module foo.bar. If we load everything in the global namespace the two
  15. ** initbar() symbols will conflict.
  16. ** However, it seems some extension packages depend upon being able to access
  17. ** each others' global symbols. There seems to be no way to eat our cake and
  18. ** have it, so the USE_DYLD_GLOBAL_NAMESPACE define determines which behaviour
  19. ** you get.
  20. */
  21. #ifdef USE_DYLD_GLOBAL_NAMESPACE
  22. #define LINKOPTIONS NSLINKMODULE_OPTION_BINDNOW|NSLINKMODULE_OPTION_RETURN_ON_ERROR
  23. #else
  24. #define LINKOPTIONS NSLINKMODULE_OPTION_BINDNOW| \
  25. NSLINKMODULE_OPTION_RETURN_ON_ERROR|NSLINKMODULE_OPTION_PRIVATE
  26. #endif
  27. dl_funcptr _PyImport_GetDynLoadFunc(const char *shortname,
  28. const char *pathname, FILE *fp)
  29. {
  30. dl_funcptr p = NULL;
  31. char funcname[258];
  32. NSObjectFileImageReturnCode rc;
  33. NSObjectFileImage image;
  34. NSModule newModule;
  35. NSSymbol theSym;
  36. const char *errString;
  37. char errBuf[512];
  38. PyOS_snprintf(funcname, sizeof(funcname), "_PyInit_%.200s", shortname);
  39. #ifdef USE_DYLD_GLOBAL_NAMESPACE
  40. if (NSIsSymbolNameDefined(funcname)) {
  41. theSym = NSLookupAndBindSymbol(funcname);
  42. p = (dl_funcptr)NSAddressOfSymbol(theSym);
  43. return p;
  44. }
  45. #endif
  46. rc = NSCreateObjectFileImageFromFile(pathname, &image);
  47. switch(rc) {
  48. default:
  49. case NSObjectFileImageFailure:
  50. case NSObjectFileImageFormat:
  51. /* for these a message is printed on stderr by dyld */
  52. errString = "Can't create object file image";
  53. break;
  54. case NSObjectFileImageSuccess:
  55. errString = NULL;
  56. break;
  57. case NSObjectFileImageInappropriateFile:
  58. errString = "Inappropriate file type for dynamic loading";
  59. break;
  60. case NSObjectFileImageArch:
  61. errString = "Wrong CPU type in object file";
  62. break;
  63. case NSObjectFileImageAccess:
  64. errString = "Can't read object file (no access)";
  65. break;
  66. }
  67. if (errString == NULL) {
  68. newModule = NSLinkModule(image, pathname, LINKOPTIONS);
  69. if (newModule == NULL) {
  70. int errNo;
  71. const char *fileName, *moreErrorStr;
  72. NSLinkEditErrors c;
  73. NSLinkEditError( &c, &errNo, &fileName, &moreErrorStr );
  74. PyOS_snprintf(errBuf, 512, "Failure linking new module: %s: %s",
  75. fileName, moreErrorStr);
  76. errString = errBuf;
  77. }
  78. }
  79. if (errString != NULL) {
  80. PyErr_SetString(PyExc_ImportError, errString);
  81. return NULL;
  82. }
  83. #ifdef USE_DYLD_GLOBAL_NAMESPACE
  84. if (!NSIsSymbolNameDefined(funcname)) {
  85. /* UnlinkModule() isn't implemented in current versions, but calling it does no harm */
  86. /* NSUnLinkModule(newModule, FALSE); removed: causes problems for ObjC code */
  87. PyErr_Format(PyExc_ImportError,
  88. "Loaded module does not contain symbol %.200s",
  89. funcname);
  90. return NULL;
  91. }
  92. theSym = NSLookupAndBindSymbol(funcname);
  93. #else
  94. theSym = NSLookupSymbolInModule(newModule, funcname);
  95. if ( theSym == NULL ) {
  96. /* NSUnLinkModule(newModule, FALSE); removed: causes problems for ObjC code */
  97. PyErr_Format(PyExc_ImportError,
  98. "Loaded module does not contain symbol %.200s",
  99. funcname);
  100. return NULL;
  101. }
  102. #endif
  103. p = (dl_funcptr)NSAddressOfSymbol(theSym);
  104. return p;
  105. }