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.

152 lines
4.2 KiB

  1. /* Support for dynamic loading of extension modules */
  2. #include "Python.h"
  3. #include "importdl.h"
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #if defined(__NetBSD__)
  7. #include <sys/param.h>
  8. #if (NetBSD < 199712)
  9. #include <nlist.h>
  10. #include <link.h>
  11. #define dlerror() "error in dynamic linking"
  12. #endif
  13. #endif /* NetBSD */
  14. #ifdef HAVE_DLFCN_H
  15. #include <dlfcn.h>
  16. #else
  17. #if defined(PYOS_OS2) && defined(PYCC_GCC)
  18. #include "dlfcn.h"
  19. #endif
  20. #endif
  21. #if (defined(__OpenBSD__) || defined(__NetBSD__)) && !defined(__ELF__)
  22. #define LEAD_UNDERSCORE "_"
  23. #else
  24. #define LEAD_UNDERSCORE ""
  25. #endif
  26. /* The .so extension module ABI tag, supplied by the Makefile via
  27. Makefile.pre.in and configure. This is used to discriminate between
  28. incompatible .so files so that extensions for different Python builds can
  29. live in the same directory. E.g. foomodule.cpython-32.so
  30. */
  31. const struct filedescr _PyImport_DynLoadFiletab[] = {
  32. #ifdef __CYGWIN__
  33. {".dll", "rb", C_EXTENSION},
  34. {"module.dll", "rb", C_EXTENSION},
  35. #else /* !__CYGWIN__ */
  36. #if defined(PYOS_OS2) && defined(PYCC_GCC)
  37. {".pyd", "rb", C_EXTENSION},
  38. {".dll", "rb", C_EXTENSION},
  39. #else /* !(defined(PYOS_OS2) && defined(PYCC_GCC)) */
  40. #ifdef __VMS
  41. {".exe", "rb", C_EXTENSION},
  42. {".EXE", "rb", C_EXTENSION},
  43. {"module.exe", "rb", C_EXTENSION},
  44. {"MODULE.EXE", "rb", C_EXTENSION},
  45. #else /* !__VMS */
  46. {"." SOABI ".so", "rb", C_EXTENSION},
  47. {"module." SOABI ".so", "rb", C_EXTENSION},
  48. {".abi" PYTHON_ABI_STRING ".so", "rb", C_EXTENSION},
  49. {"module.abi" PYTHON_ABI_STRING ".so", "rb", C_EXTENSION},
  50. {".so", "rb", C_EXTENSION},
  51. {"module.so", "rb", C_EXTENSION},
  52. #endif /* __VMS */
  53. #endif /* defined(PYOS_OS2) && defined(PYCC_GCC) */
  54. #endif /* __CYGWIN__ */
  55. {0, 0}
  56. };
  57. static struct {
  58. dev_t dev;
  59. #ifdef __VMS
  60. ino_t ino[3];
  61. #else
  62. ino_t ino;
  63. #endif
  64. void *handle;
  65. } handles[128];
  66. static int nhandles = 0;
  67. dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname,
  68. const char *pathname, FILE *fp)
  69. {
  70. dl_funcptr p;
  71. void *handle;
  72. char funcname[258];
  73. char pathbuf[260];
  74. int dlopenflags=0;
  75. if (strchr(pathname, '/') == NULL) {
  76. /* Prefix bare filename with "./" */
  77. PyOS_snprintf(pathbuf, sizeof(pathbuf), "./%-.255s", pathname);
  78. pathname = pathbuf;
  79. }
  80. PyOS_snprintf(funcname, sizeof(funcname),
  81. LEAD_UNDERSCORE "PyInit_%.200s", shortname);
  82. if (fp != NULL) {
  83. int i;
  84. struct stat statb;
  85. fstat(fileno(fp), &statb);
  86. for (i = 0; i < nhandles; i++) {
  87. if (statb.st_dev == handles[i].dev &&
  88. statb.st_ino == handles[i].ino) {
  89. p = (dl_funcptr) dlsym(handles[i].handle,
  90. funcname);
  91. return p;
  92. }
  93. }
  94. if (nhandles < 128) {
  95. handles[nhandles].dev = statb.st_dev;
  96. #ifdef __VMS
  97. handles[nhandles].ino[0] = statb.st_ino[0];
  98. handles[nhandles].ino[1] = statb.st_ino[1];
  99. handles[nhandles].ino[2] = statb.st_ino[2];
  100. #else
  101. handles[nhandles].ino = statb.st_ino;
  102. #endif
  103. }
  104. }
  105. #if !(defined(PYOS_OS2) && defined(PYCC_GCC))
  106. dlopenflags = PyThreadState_GET()->interp->dlopenflags;
  107. #endif
  108. if (Py_VerboseFlag)
  109. PySys_WriteStderr("dlopen(\"%s\", %x);\n", pathname,
  110. dlopenflags);
  111. #ifdef __VMS
  112. /* VMS currently don't allow a pathname, use a logical name instead */
  113. /* Concatenate 'python_module_' and shortname */
  114. /* so "import vms.bar" will use the logical python_module_bar */
  115. /* As C module use only one name space this is probably not a */
  116. /* important limitation */
  117. PyOS_snprintf(pathbuf, sizeof(pathbuf), "python_module_%-.200s",
  118. shortname);
  119. pathname = pathbuf;
  120. #endif
  121. handle = dlopen(pathname, dlopenflags);
  122. if (handle == NULL) {
  123. const char *error = dlerror();
  124. if (error == NULL)
  125. error = "unknown dlopen() error";
  126. PyErr_SetString(PyExc_ImportError, error);
  127. return NULL;
  128. }
  129. if (fp != NULL && nhandles < 128)
  130. handles[nhandles++].handle = handle;
  131. p = (dl_funcptr) dlsym(handle, funcname);
  132. return p;
  133. }