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.

184 lines
5.7 KiB

  1. /* Support for dynamic loading of extension modules */
  2. #include "Python.h"
  3. #include "importdl.h"
  4. #include <ctype.h> /* for isdigit() */
  5. #include <errno.h> /* for global errno */
  6. #include <string.h> /* for strerror() */
  7. #include <stdlib.h> /* for malloc(), free() */
  8. #include <sys/ldr.h>
  9. #ifdef AIX_GENUINE_CPLUSPLUS
  10. #include <load.h>
  11. #define aix_load loadAndInit
  12. #else
  13. #define aix_load load
  14. #endif
  15. extern char *Py_GetProgramName(void);
  16. typedef struct Module {
  17. struct Module *next;
  18. void *entry;
  19. } Module, *ModulePtr;
  20. const char *_PyImport_DynLoadFiletab[] = {".so", NULL};
  21. static int
  22. aix_getoldmodules(void **modlistptr)
  23. {
  24. register ModulePtr modptr, prevmodptr;
  25. register struct ld_info *ldiptr;
  26. register char *ldibuf;
  27. register int errflag, bufsize = 1024;
  28. register unsigned int offset;
  29. char *progname = Py_GetProgramName();
  30. /*
  31. -- Get the list of loaded modules into ld_info structures.
  32. */
  33. if ((ldibuf = malloc(bufsize)) == NULL) {
  34. PyErr_SetString(PyExc_ImportError, strerror(errno));
  35. return -1;
  36. }
  37. while ((errflag = loadquery(L_GETINFO, ldibuf, bufsize)) == -1
  38. && errno == ENOMEM) {
  39. free(ldibuf);
  40. bufsize += 1024;
  41. if ((ldibuf = malloc(bufsize)) == NULL) {
  42. PyErr_SetString(PyExc_ImportError, strerror(errno));
  43. return -1;
  44. }
  45. }
  46. if (errflag == -1) {
  47. PyErr_SetString(PyExc_ImportError, strerror(errno));
  48. return -1;
  49. }
  50. /*
  51. -- Make the modules list from the ld_info structures.
  52. */
  53. ldiptr = (struct ld_info *)ldibuf;
  54. prevmodptr = NULL;
  55. do {
  56. if (strstr(progname, ldiptr->ldinfo_filename) == NULL &&
  57. strstr(ldiptr->ldinfo_filename, "python") == NULL) {
  58. /*
  59. -- Extract only the modules belonging to the main
  60. -- executable + those containing "python" as a
  61. -- substring (like the "python[version]" binary or
  62. -- "libpython[version].a" in case it's a shared lib).
  63. */
  64. offset = (unsigned int)ldiptr->ldinfo_next;
  65. ldiptr = (struct ld_info *)((char*)ldiptr + offset);
  66. continue;
  67. }
  68. if ((modptr = (ModulePtr)malloc(sizeof(Module))) == NULL) {
  69. PyErr_SetString(PyExc_ImportError, strerror(errno));
  70. while (*modlistptr) {
  71. modptr = (ModulePtr)*modlistptr;
  72. *modlistptr = (void *)modptr->next;
  73. free(modptr);
  74. }
  75. return -1;
  76. }
  77. modptr->entry = ldiptr->ldinfo_dataorg;
  78. modptr->next = NULL;
  79. if (prevmodptr == NULL)
  80. *modlistptr = (void *)modptr;
  81. else
  82. prevmodptr->next = modptr;
  83. prevmodptr = modptr;
  84. offset = (unsigned int)ldiptr->ldinfo_next;
  85. ldiptr = (struct ld_info *)((char*)ldiptr + offset);
  86. } while (offset);
  87. free(ldibuf);
  88. return 0;
  89. }
  90. static void
  91. aix_loaderror(const char *pathname)
  92. {
  93. char *message[1024], errbuf[1024];
  94. PyObject *pathname_ob = NULL;
  95. PyObject *errbuf_ob = NULL;
  96. register int i,j;
  97. struct errtab {
  98. int errNo;
  99. char *errstr;
  100. } load_errtab[] = {
  101. {L_ERROR_TOOMANY, "too many errors, rest skipped."},
  102. {L_ERROR_NOLIB, "can't load library:"},
  103. {L_ERROR_UNDEF, "can't find symbol in library:"},
  104. {L_ERROR_RLDBAD,
  105. "RLD index out of range or bad relocation type:"},
  106. {L_ERROR_FORMAT, "not a valid, executable xcoff file:"},
  107. {L_ERROR_MEMBER,
  108. "file not an archive or does not contain requested member:"},
  109. {L_ERROR_TYPE, "symbol table mismatch:"},
  110. {L_ERROR_ALIGN, "text alignment in file is wrong."},
  111. {L_ERROR_SYSTEM, "System error:"},
  112. {L_ERROR_ERRNO, NULL}
  113. };
  114. #define ERRBUF_APPEND(s) strncat(errbuf, s, sizeof(errbuf)-strlen(errbuf)-1)
  115. PyOS_snprintf(errbuf, sizeof(errbuf), "from module %.200s ", pathname);
  116. if (!loadquery(L_GETMESSAGES, &message[0], sizeof(message))) {
  117. ERRBUF_APPEND(strerror(errno));
  118. ERRBUF_APPEND("\n");
  119. }
  120. for(i = 0; message[i] && *message[i]; i++) {
  121. int nerr = atoi(message[i]);
  122. for (j=0; j < Py_ARRAY_LENGTH(load_errtab); j++) {
  123. if (nerr == load_errtab[j].errNo && load_errtab[j].errstr)
  124. ERRBUF_APPEND(load_errtab[j].errstr);
  125. }
  126. while (isdigit(Py_CHARMASK(*message[i]))) message[i]++ ;
  127. ERRBUF_APPEND(message[i]);
  128. ERRBUF_APPEND("\n");
  129. }
  130. errbuf[strlen(errbuf)-1] = '\0'; /* trim off last newline */
  131. pathname_ob = PyUnicode_FromString(pathname);
  132. errbuf_ob = PyUnicode_FromString(errbuf);
  133. PyErr_SetImportError(errbuf_ob, NULL, pathname);
  134. Py_DECREF(pathname_ob);
  135. Py_DECREF(errbuf_ob);
  136. return;
  137. }
  138. dl_funcptr _PyImport_GetDynLoadFunc(const char *shortname,
  139. const char *pathname, FILE *fp)
  140. {
  141. dl_funcptr p;
  142. /*
  143. -- Invoke load() with L_NOAUTODEFER leaving the imported symbols
  144. -- of the shared module unresolved. Thus we have to resolve them
  145. -- explicitly with loadbind. The new module is loaded, then we
  146. -- resolve its symbols using the list of already loaded modules
  147. -- (only those that belong to the python executable). Get these
  148. -- with loadquery(L_GETINFO).
  149. */
  150. static void *staticmodlistptr = NULL;
  151. if (!staticmodlistptr)
  152. if (aix_getoldmodules(&staticmodlistptr) == -1)
  153. return NULL;
  154. p = (dl_funcptr) aix_load((char *)pathname, L_NOAUTODEFER, 0);
  155. if (p == NULL) {
  156. aix_loaderror(pathname);
  157. return NULL;
  158. }
  159. return p;
  160. }