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.

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