blob: b3ff8e288c1a6ae53cc6cd392f79b884ae2b1e9c [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 <errno.h> /* for global errno */
8#include <string.h> /* for strerror() */
9#include <stdlib.h> /* for malloc(), free() */
Guido van Rossum22a1d361999-12-20 21:18:49 +000010#include <sys/ldr.h>
11
Guido van Rossum22a1d361999-12-20 21:18:49 +000012
13#ifdef AIX_GENUINE_CPLUSPLUS
Georg Brandl59e87bd2011-02-15 19:48:59 +000014#include <load.h>
Guido van Rossum22a1d361999-12-20 21:18:49 +000015#define aix_load loadAndInit
16#else
17#define aix_load load
18#endif
19
20
Thomas Woutersb4bd21c2000-07-22 23:38:01 +000021extern char *Py_GetProgramName(void);
Guido van Rossum22a1d361999-12-20 21:18:49 +000022
23typedef struct Module {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000024 struct Module *next;
25 void *entry;
Guido van Rossum22a1d361999-12-20 21:18:49 +000026} Module, *ModulePtr;
27
Brett Cannon2657df42012-05-04 15:20:40 -040028const char *_PyImport_DynLoadFiletab[] = {".so", NULL};
Guido van Rossum22a1d361999-12-20 21:18:49 +000029
30static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000031aix_getoldmodules(void **modlistptr)
Guido van Rossum22a1d361999-12-20 21:18:49 +000032{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020033 ModulePtr modptr, prevmodptr;
34 struct ld_info *ldiptr;
35 char *ldibuf;
36 int errflag, bufsize = 1024;
37 unsigned int offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000038 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 Rossum22a1d361999-12-20 21:18:49 +000099}
100
Guido van Rossum22a1d361999-12-20 21:18:49 +0000101
102static void
Vladimir Marangozov547936c2000-09-04 00:54:56 +0000103aix_loaderror(const char *pathname)
Guido van Rossum22a1d361999-12-20 21:18:49 +0000104{
105
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000106 char *message[1024], errbuf[1024];
Brett Cannon3dfc22c2012-04-20 15:31:11 -0400107 PyObject *pathname_ob = NULL;
108 PyObject *errbuf_ob = NULL;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200109 int i,j;
Guido van Rossum22a1d361999-12-20 21:18:49 +0000110
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000111 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 Rossum22a1d361999-12-20 21:18:49 +0000128
Guido van Rossum22a1d361999-12-20 21:18:49 +0000129#define ERRBUF_APPEND(s) strncat(errbuf, s, sizeof(errbuf)-strlen(errbuf)-1)
130
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000131 PyOS_snprintf(errbuf, sizeof(errbuf), "from module %.200s ", pathname);
Guido van Rossum22a1d361999-12-20 21:18:49 +0000132
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000133 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 Stinner63941882011-09-29 00:42:28 +0200139 for (j=0; j < Py_ARRAY_LENGTH(load_errtab); j++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000140 if (nerr == load_errtab[j].errNo && load_errtab[j].errstr)
141 ERRBUF_APPEND(load_errtab[j].errstr);
142 }
Antoine Pitrou4de74572013-02-09 23:11:27 +0100143 while (Py_ISDIGIT(Py_CHARMASK(*message[i]))) message[i]++ ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000144 ERRBUF_APPEND(message[i]);
145 ERRBUF_APPEND("\n");
146 }
147 errbuf[strlen(errbuf)-1] = '\0'; /* trim off last newline */
Brett Cannon3dfc22c2012-04-20 15:31:11 -0400148 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 Pitrouf95a1b32010-05-09 15:52:27 +0000153 return;
Guido van Rossum22a1d361999-12-20 21:18:49 +0000154}
155
156
Nick Coghland5cacbb2015-05-23 22:24:10 +1000157dl_funcptr _PyImport_FindSharedFuncptr(const char *prefix,
158 const char *shortname,
159 const char *pathname, FILE *fp)
Guido van Rossum22a1d361999-12-20 21:18:49 +0000160{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000161 dl_funcptr p;
Guido van Rossum22a1d361999-12-20 21:18:49 +0000162
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000163 /*
164 -- Invoke load() with L_NOAUTODEFER leaving the imported symbols
165 -- of the shared module unresolved. Thus we have to resolve them
166 -- explicitly with loadbind. The new module is loaded, then we
167 -- resolve its symbols using the list of already loaded modules
168 -- (only those that belong to the python executable). Get these
169 -- with loadquery(L_GETINFO).
170 */
Guido van Rossum22a1d361999-12-20 21:18:49 +0000171
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000172 static void *staticmodlistptr = NULL;
Guido van Rossum22a1d361999-12-20 21:18:49 +0000173
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000174 if (!staticmodlistptr)
175 if (aix_getoldmodules(&staticmodlistptr) == -1)
176 return NULL;
177 p = (dl_funcptr) aix_load((char *)pathname, L_NOAUTODEFER, 0);
178 if (p == NULL) {
179 aix_loaderror(pathname);
180 return NULL;
181 }
Guido van Rossum22a1d361999-12-20 21:18:49 +0000182
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000183 return p;
Guido van Rossum22a1d361999-12-20 21:18:49 +0000184}