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.

132 lines
3.4 KiB

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