1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
#include "dlfcn.h"
#include "string.h"
#include "stdio.h"
#define INCL_BASE
#include <os2.h>
#include <float.h>
#include <stdlib.h>
static ULONG retcode;
static char fail[300];
static ULONG dllHandle;
static int handle_found;
static int handle_loaded;
#ifdef PERL_CORE
#include "EXTERN.h"
#include "perl.h"
#else
char *os2error(int rc);
#endif
#ifdef DLOPEN_INITTERM
unsigned long _DLL_InitTerm(unsigned long modHandle, unsigned long flag)
{
switch (flag) {
case 0: /* INIT */
/* Save handle */
dllHandle = modHandle;
handle_found = 1;
return TRUE;
case 1: /* TERM */
handle_found = 0;
dllHandle = (unsigned long)NULLHANDLE;
return TRUE;
}
return FALSE;
}
#endif
HMODULE
find_myself(void)
{
static APIRET APIENTRY (*pDosQueryModFromEIP) (HMODULE * hmod, ULONG * obj, ULONG BufLen, PCHAR Buf,
ULONG * Offset, ULONG Address);
HMODULE doscalls_h, mod;
static int failed;
ULONG obj, offset, rc;
char buf[260];
if (failed)
return 0;
failed = 1;
doscalls_h = (HMODULE)dlopen("DOSCALLS",0);
if (!doscalls_h)
return 0;
/* {&doscalls_handle, NULL, 360}, */ /* DosQueryModFromEIP */
rc = DosQueryProcAddr(doscalls_h, 360, 0, (PFN*)&pDosQueryModFromEIP);
if (rc)
return 0;
rc = pDosQueryModFromEIP(&mod, &obj, sizeof(buf), buf, &offset, (ULONG)dlopen);
if (rc)
return 0;
failed = 0;
handle_found = 1;
dllHandle = mod;
return mod;
}
void *
dlopen(const char *path, int mode)
{
HMODULE handle;
char tmp[260];
const char *beg, *dot;
ULONG rc;
unsigned fpflag = _control87(0,0);
fail[0] = 0;
if (!path) { /* Our own handle. */
if (handle_found || find_myself()) {
char dllname[260];
if (handle_loaded)
return (void*)dllHandle;
rc = DosQueryModuleName(dllHandle, sizeof(dllname), dllname);
if (rc) {
strcpy(fail, "can't find my DLL name by the handle");
retcode = rc;
return 0;
}
rc = DosLoadModule(fail, sizeof fail, dllname, &handle);
if (rc) {
strcpy(fail, "can't load my own DLL");
retcode = rc;
return 0;
}
handle_loaded = 1;
goto ret;
}
retcode = ERROR_MOD_NOT_FOUND;
strcpy(fail, "can't load from myself: compiled without -DDLOPEN_INITTERM");
return 0;
}
if ((rc = DosLoadModule(fail, sizeof fail, (char*)path, &handle)) == 0)
goto ret;
retcode = rc;
if (strlen(path) >= sizeof(tmp))
return NULL;
/* Not found. Check for non-FAT name and try truncated name. */
/* Don't know if this helps though... */
for (beg = dot = path + strlen(path);
beg > path && !strchr(":/\\", *(beg-1));
beg--)
if (*beg == '.')
dot = beg;
if (dot - beg > 8) {
int n = beg+8-path;
memmove(tmp, path, n);
memmove(tmp+n, dot, strlen(dot)+1);
if (DosLoadModule(fail, sizeof fail, tmp, &handle) == 0)
goto ret;
}
handle = 0;
ret:
_control87(fpflag, MCW_EM); /* Some modules reset FP flags on load */
return (void *)handle;
}
#define ERROR_WRONG_PROCTYPE 0xffffffff
void *
dlsym(void *handle, const char *symbol)
{
ULONG rc, type;
PFN addr;
fail[0] = 0;
rc = DosQueryProcAddr((HMODULE)handle, 0, symbol, &addr);
if (rc == 0) {
rc = DosQueryProcType((HMODULE)handle, 0, symbol, &type);
if (rc == 0 && type == PT_32BIT)
return (void *)addr;
rc = ERROR_WRONG_PROCTYPE;
}
retcode = rc;
return NULL;
}
char *
dlerror(void)
{
static char buf[700];
ULONG len;
char *err;
if (retcode == 0)
return NULL;
if (retcode == ERROR_WRONG_PROCTYPE)
err = "Wrong procedure type";
else
err = os2error(retcode);
len = strlen(err);
if (len > sizeof(buf) - 1)
len = sizeof(buf) - 1;
strncpy(buf, err, len+1);
if (fail[0] && len + strlen(fail) < sizeof(buf) - 100)
sprintf(buf + len, ", possible problematic module: '%s'", fail);
retcode = 0;
return buf;
}
int
dlclose(void *handle)
{
ULONG rc;
if ((rc = DosFreeModule((HMODULE)handle)) == 0) return 0;
retcode = rc;
return 2;
}
|