blob: 8346f065e3f4e53a17c6942c57c50eff4f7033f3 [file] [log] [blame]
Guido van Rossum22a1d361999-12-20 21:18:49 +00001
2/* Support for dynamic loading of extension modules */
3
Neal Norwitzdf5126d2003-03-22 16:35:37 +00004#include "Python.h"
5#include "importdl.h"
6
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007#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 Rossum22a1d361999-12-20 21:18:49 +000011#include <sys/ldr.h>
12
Guido van Rossum22a1d361999-12-20 21:18:49 +000013
14#ifdef AIX_GENUINE_CPLUSPLUS
Georg Brandl59e87bd2011-02-15 19:48:59 +000015#include <load.h>
Guido van Rossum22a1d361999-12-20 21:18:49 +000016#define aix_load loadAndInit
17#else
18#define aix_load load
19#endif
20
21
Thomas Woutersb4bd21c2000-07-22 23:38:01 +000022extern char *Py_GetProgramName(void);
Guido van Rossum22a1d361999-12-20 21:18:49 +000023
24typedef struct Module {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000025 struct Module *next;
26 void *entry;
Guido van Rossum22a1d361999-12-20 21:18:49 +000027} Module, *ModulePtr;
28
29const struct filedescr _PyImport_DynLoadFiletab[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000030 {".so", "rb", C_EXTENSION},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000031 {0, 0}
Guido van Rossum22a1d361999-12-20 21:18:49 +000032};
33
34static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000035aix_getoldmodules(void **modlistptr)
Guido van Rossum22a1d361999-12-20 21:18:49 +000036{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000037 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 Rossum22a1d361999-12-20 21:18:49 +0000103}
104
Guido van Rossum22a1d361999-12-20 21:18:49 +0000105
106static void
Vladimir Marangozov547936c2000-09-04 00:54:56 +0000107aix_loaderror(const char *pathname)
Guido van Rossum22a1d361999-12-20 21:18:49 +0000108{
109
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000110 char *message[1024], errbuf[1024];
111 register int i,j;
Guido van Rossum22a1d361999-12-20 21:18:49 +0000112
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 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 Rossum22a1d361999-12-20 21:18:49 +0000130
Guido van Rossum22a1d361999-12-20 21:18:49 +0000131#define ERRBUF_APPEND(s) strncat(errbuf, s, sizeof(errbuf)-strlen(errbuf)-1)
132
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000133 PyOS_snprintf(errbuf, sizeof(errbuf), "from module %.200s ", pathname);
Guido van Rossum22a1d361999-12-20 21:18:49 +0000134
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000135 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 Stinner63941882011-09-29 00:42:28 +0200141 for (j=0; j < Py_ARRAY_LENGTH(load_errtab); j++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000142 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 Rossum22a1d361999-12-20 21:18:49 +0000152}
153
154
Victor Stinner42040fb2011-02-22 23:16:19 +0000155dl_funcptr _PyImport_GetDynLoadFunc(const char *shortname,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000156 const char *pathname, FILE *fp)
Guido van Rossum22a1d361999-12-20 21:18:49 +0000157{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000158 dl_funcptr p;
Guido van Rossum22a1d361999-12-20 21:18:49 +0000159
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000160 /*
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 Rossum22a1d361999-12-20 21:18:49 +0000168
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000169 static void *staticmodlistptr = NULL;
Guido van Rossum22a1d361999-12-20 21:18:49 +0000170
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000171 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 Rossum22a1d361999-12-20 21:18:49 +0000179
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000180 return p;
Guido van Rossum22a1d361999-12-20 21:18:49 +0000181}