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