-
Notifications
You must be signed in to change notification settings - Fork 5
/
dll.cpp
195 lines (173 loc) · 4.73 KB
/
dll.cpp
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
// Copyright (c) 2000 Suneido Software Corp. All rights reserved
// Licensed under GPLv2
#define _CRT_NONSTDC_NO_WARNINGS
#include "dll.h"
#include "interp.h"
#include "globals.h"
#include "symbols.h"
#include "ostreamfile.h"
#include "win.h"
#include "trace.h"
#include <cstring> // for _stricmp
#ifdef __GNUC__
#define _stricmp stricmp
#endif
struct WinLib {
WinLib() {
}
explicit WinLib(char* s) : name(dupstr(s)), lib(LoadLibrary(name)) {
}
~WinLib() {
if (lib)
FreeLibrary(lib);
}
void* GetProcAddress(char* procname) const {
return (void*) ::GetProcAddress(lib, procname);
}
explicit operator bool() const {
return lib;
}
char* name = nullptr;
HMODULE lib = nullptr;
};
static WinLib& loadlib(char* name) {
const int NLIBS = 30;
static WinLib libs[NLIBS]; // TODO change to vector (or hash map)
static int nlibs = 0;
int i = 0;
for (; i < nlibs; ++i)
if (0 == _stricmp(libs[i].name, name))
return libs[i];
if (nlibs >= NLIBS)
except("can't load " << name << " - too many dll's loaded");
new (libs + nlibs++) WinLib(name);
return libs[i];
}
static OstreamFile& log() {
static OstreamFile ofs("dll.log");
return ofs;
}
Dll::Dll(short rt, char* library, char* name, TypeItem* p, short* ns, short n)
: params(p, n), rtype(rt), trace(false) {
nparams = n;
dll = globals(library);
fn = globals(name);
locals = dup(ns, nparams);
WinLib& hlibrary = loadlib(library);
if (!hlibrary)
except("can't LoadLibrary " << library);
if (0 == (pfn = hlibrary.GetProcAddress(name))) {
char uname[64];
verify(strlen(name) < 63);
strcpy(uname, name);
strcat(uname, "A");
if (0 == (pfn = hlibrary.GetProcAddress(uname)))
except("can't get " << library << ":" << name << " or " << uname);
}
// log() << this << endl;
}
void Dll::out(Ostream& os) const {
if (named.num)
os << named.name() << " /* " << named.lib << " dll " << pfn << " */";
else {
os << "dll " << (rtype ? globals(rtype) : "void") << " " << globals(dll)
<< ":" << globals(fn);
os << '(';
for (int i = 0; i < nparams; ++i) {
if (i > 0)
os << ", ";
params.items[i].out(os);
verify(locals);
os << " " << symstr(locals[i]);
}
os << ')';
}
}
const int maxbuf = 1024;
Value Dll::call(Value self, Value member, short nargs, short nargnames,
short* argnames, int each) {
static Value Trace("Trace");
if (member == Trace) {
trace = true;
return Value();
}
if (member != CALL)
return Func::call(self, member, nargs, nargnames, argnames, each);
args(nargs, nargnames, argnames, each);
Framer frame(this, self);
char buf[maxbuf];
char* dst = buf;
const int64_t MARKER = 0xaa55aa55aa55aa55;
char buf2[32000]; // for stuff passed by pointer
char* dst2 = buf2;
char* lim2 = buf2 + sizeof buf2 - sizeof(MARKER);
const int params_size = params.size();
if (params_size > maxbuf)
except("dll arguments too big");
Value* args = GETSP() - nparams + 1;
params.putall(dst, dst2, lim2, args);
verify(dst == buf + params_size);
*((int64_t*) dst2) = MARKER;
if (trace) {
log() << named.name() << endl << " ";
for (void** p = (void**) buf; p < (void**) dst; ++p)
log() << *p << " ";
log() << endl << " ";
for (char* s = buf2; s < dst2; ++s)
log() << hex << (uint32_t)(uint8_t) *s << dec << " ";
log() << endl;
}
int save_trace_level = trace_level;
TRACE_CLEAR();
int result, result2;
int nlongs = (dst - buf) / 4;
#ifdef _MSC_VER
if (nlongs)
__asm {
mov ecx, nlongs
mov ebx, dst
NEXT: sub ebx,4
push dword ptr [ebx]
loop NEXT
}
// ReSharper disable once CppDeclaratorNeverUsed
uint32_t f = (uint32_t) pfn;
__asm
{
mov eax,f
call eax
mov result,eax
mov result2,edx
}
#elif defined(__GNUC__)
int dummy;
if (nlongs)
asm volatile("1:\n\t"
"sub $4, %%ebx\n\t"
"pushl (%%ebx)\n\t"
"loop 1b\n\t"
: "=b"(dummy) // outputs
: "c"(nlongs), "b"(dst) // inputs
);
asm volatile("call *%%eax\n\t"
: "=a"(result), "=d"(result2) // outputs
: "a"(pfn) // inputs
: "memory" // clobbers
);
// WARNING: should be more clobbers but not sure what???
#else
#warning "replacement for inline assembler required"
#endif
verify(*((int64_t*) dst2) == MARKER);
trace_level = save_trace_level;
if (trace) {
log() << " => " << hex << result << dec << endl;
for (char* s = buf2; s < dst2; ++s)
log() << hex << (uint32_t)(uint8_t) *s << dec << " ";
}
// update SuObject args passed by pointer
const char* src = buf;
params.getall(src, args);
return rtype ? force<Type*>(globals[rtype])->result(result2, result)
: Value();
}