dynload_shlib.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. dl_funcptr
  43. _PyImport_FindSharedFuncptr(const char *prefix,
  44. const char *shortname,
  45. const char *pathname, FILE *fp)
  46. {
  47. dl_funcptr p;
  48. void *handle;
  49. char funcname[258];
  50. char pathbuf[260];
  51. int dlopenflags=0;
  52. if (strchr(pathname, '/') == NULL) {
  53. /* Prefix bare filename with "./" */
  54. PyOS_snprintf(pathbuf, sizeof(pathbuf), "./%-.255s", pathname);
  55. pathname = pathbuf;
  56. }
  57. PyOS_snprintf(funcname, sizeof(funcname),
  58. LEAD_UNDERSCORE "%.20s_%.200s", prefix, shortname);
  59. if (fp != NULL) {
  60. struct _Py_stat_struct status;
  61. if (_Py_fstat(fileno(fp), &status) == -1)
  62. return NULL;
  63. }
  64. dlopenflags = _PyImport_GetDLOpenFlags(_PyInterpreterState_GET());
  65. handle = dlopen(pathname, dlopenflags);
  66. if (handle == NULL) {
  67. PyObject *mod_name;
  68. PyObject *path;
  69. PyObject *error_ob;
  70. const char *error = dlerror();
  71. if (error == NULL)
  72. error = "unknown dlopen() error";
  73. error_ob = PyUnicode_DecodeLocale(error, "surrogateescape");
  74. if (error_ob == NULL)
  75. return NULL;
  76. mod_name = PyUnicode_FromString(shortname);
  77. if (mod_name == NULL) {
  78. Py_DECREF(error_ob);
  79. return NULL;
  80. }
  81. path = PyUnicode_DecodeFSDefault(pathname);
  82. if (path == NULL) {
  83. Py_DECREF(error_ob);
  84. Py_DECREF(mod_name);
  85. return NULL;
  86. }
  87. PyErr_SetImportError(error_ob, mod_name, path);
  88. Py_DECREF(error_ob);
  89. Py_DECREF(mod_name);
  90. Py_DECREF(path);
  91. return NULL;
  92. }
  93. p = (dl_funcptr) dlsym(handle, funcname);
  94. return p;
  95. }