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.

133 lines
3.4 KiB

13 years ago
  1. /* Support for dynamic loading of extension modules */
  2. #include "Python.h"
  3. #include "pycore_interp.h" // _PyInterpreterState.dlopenflags
  4. #include "pycore_pystate.h" // _PyInterpreterState_GET()
  5. #include "importdl.h"
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #if defined(__NetBSD__)
  9. #include <sys/param.h>
  10. #if (NetBSD < 199712)
  11. #include <nlist.h>
  12. #include <link.h>
  13. #define dlerror() "error in dynamic linking"
  14. #endif
  15. #endif /* NetBSD */
  16. #ifdef HAVE_DLFCN_H
  17. #include <dlfcn.h>
  18. #endif
  19. #if (defined(__OpenBSD__) || defined(__NetBSD__)) && !defined(__ELF__)
  20. #define LEAD_UNDERSCORE "_"
  21. #else
  22. #define LEAD_UNDERSCORE ""
  23. #endif
  24. /* The .so extension module ABI tag, supplied by the Makefile via
  25. Makefile.pre.in and configure. This is used to discriminate between
  26. incompatible .so files so that extensions for different Python builds can
  27. live in the same directory. E.g. foomodule.cpython-32.so
  28. */
  29. const char *_PyImport_DynLoadFiletab[] = {
  30. #ifdef __CYGWIN__
  31. ".dll",
  32. #else /* !__CYGWIN__ */
  33. "." SOABI ".so",
  34. #ifdef ALT_SOABI
  35. "." ALT_SOABI ".so",
  36. #endif
  37. ".abi" PYTHON_ABI_STRING ".so",
  38. ".so",
  39. #endif /* __CYGWIN__ */
  40. NULL,
  41. };
  42. static struct {
  43. dev_t dev;
  44. ino_t ino;
  45. void *handle;
  46. } handles[128];
  47. static int nhandles = 0;
  48. dl_funcptr
  49. _PyImport_FindSharedFuncptr(const char *prefix,
  50. const char *shortname,
  51. const char *pathname, FILE *fp)
  52. {
  53. dl_funcptr p;
  54. void *handle;
  55. char funcname[258];
  56. char pathbuf[260];
  57. int dlopenflags=0;
  58. if (strchr(pathname, '/') == NULL) {
  59. /* Prefix bare filename with "./" */
  60. PyOS_snprintf(pathbuf, sizeof(pathbuf), "./%-.255s", pathname);
  61. pathname = pathbuf;
  62. }
  63. PyOS_snprintf(funcname, sizeof(funcname),
  64. LEAD_UNDERSCORE "%.20s_%.200s", prefix, shortname);
  65. if (fp != NULL) {
  66. int i;
  67. struct _Py_stat_struct status;
  68. if (_Py_fstat(fileno(fp), &status) == -1)
  69. return NULL;
  70. for (i = 0; i < nhandles; i++) {
  71. if (status.st_dev == handles[i].dev &&
  72. status.st_ino == handles[i].ino) {
  73. p = (dl_funcptr) dlsym(handles[i].handle,
  74. funcname);
  75. return p;
  76. }
  77. }
  78. if (nhandles < 128) {
  79. handles[nhandles].dev = status.st_dev;
  80. handles[nhandles].ino = status.st_ino;
  81. }
  82. }
  83. dlopenflags = _PyInterpreterState_GET()->dlopenflags;
  84. handle = dlopen(pathname, dlopenflags);
  85. if (handle == NULL) {
  86. PyObject *mod_name;
  87. PyObject *path;
  88. PyObject *error_ob;
  89. const char *error = dlerror();
  90. if (error == NULL)
  91. error = "unknown dlopen() error";
  92. error_ob = PyUnicode_FromString(error);
  93. if (error_ob == NULL)
  94. return NULL;
  95. mod_name = PyUnicode_FromString(shortname);
  96. if (mod_name == NULL) {
  97. Py_DECREF(error_ob);
  98. return NULL;
  99. }
  100. path = PyUnicode_FromString(pathname);
  101. if (path == NULL) {
  102. Py_DECREF(error_ob);
  103. Py_DECREF(mod_name);
  104. return NULL;
  105. }
  106. PyErr_SetImportError(error_ob, mod_name, path);
  107. Py_DECREF(error_ob);
  108. Py_DECREF(mod_name);
  109. Py_DECREF(path);
  110. return NULL;
  111. }
  112. if (fp != NULL && nhandles < 128)
  113. handles[nhandles++].handle = handle;
  114. p = (dl_funcptr) dlsym(handle, funcname);
  115. return p;
  116. }