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