Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

Testing latest pari + WASM + node.js... and it works?! Wow.

28495 views
License: GPL3
ubuntu2004
1
/* Copyright (c) 2002 Peter O'Gorman <[email protected]>
2
3
Permission is hereby granted, free of charge, to any person obtaining
4
a copy of this software and attached documentation files (the
5
"Software"), to deal in the Software without restriction, including
6
without limitation the rights to use, copy, modify, merge, publish,
7
distribute, sublicense, and/or sell copies of the Software, and to
8
permit persons to whom the Software is furnished to do so, subject to
9
the following conditions:
10
11
The above copyright notice and this permission notice shall be
12
included in all copies or substantial portions of the Software.
13
14
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
*/
22
23
/* Modified for PARI/GP by the PARI group */
24
#include "pari.h"
25
#ifndef HAS_DLOPEN
26
27
#include <stdio.h>
28
#include <stdlib.h>
29
#include <string.h>
30
#include <sys/types.h>
31
#include <sys/stat.h>
32
#include <stdarg.h>
33
#include <limits.h>
34
#include <mach-o/dyld.h>
35
#include "dlfcn.h"
36
37
#define ERR_STR_LEN 256
38
39
static void *dlsymIntern(void *handle, const char *symbol);
40
static const char *error(int setget, const char *str, ...);
41
42
/* Set and get the error string for use by dlerror */
43
static const char *error(int setget, const char *str, ...)
44
{
45
static char errstr[ERR_STR_LEN];
46
static int err_filled = 0;
47
const char *retval;
48
va_list arg;
49
if (setget == 0)
50
{
51
va_start(arg, str);
52
strncpy(errstr, "dlsimple: ", ERR_STR_LEN);
53
vsnprintf(errstr + 10, ERR_STR_LEN - 10, str, arg);
54
va_end(arg);
55
err_filled = 1;
56
retval = NULL;
57
}
58
else
59
{
60
retval = err_filled? errstr: NULL;
61
err_filled = 0;
62
}
63
return retval;
64
}
65
66
/* dlopen */
67
void *dlopen(const char *path, int mode)
68
{
69
void *module = 0;
70
NSObjectFileImage ofi = 0;
71
NSObjectFileImageReturnCode ofirc;
72
static int (*make_private_module_public) (NSModule module) = 0;
73
unsigned int flags = NSLINKMODULE_OPTION_RETURN_ON_ERROR | NSLINKMODULE_OPTION_PRIVATE;
74
75
/* If we got no path, the app wants the global namespace, use -1 as the marker
76
in this case */
77
if (!path)
78
return (void *)-1;
79
80
/* Create the object file image, works for things linked with the -bundle arg to ld */
81
ofirc = NSCreateObjectFileImageFromFile(path, &ofi);
82
switch (ofirc)
83
{
84
case NSObjectFileImageSuccess:
85
/* It was okay, so use NSLinkModule to link in the image */
86
if (!(mode & RTLD_LAZY)) flags += NSLINKMODULE_OPTION_BINDNOW;
87
module = NSLinkModule(ofi, path,flags);
88
/* Don't forget to destroy the object file image, unless you like leaks */
89
NSDestroyObjectFileImage(ofi);
90
/* If the mode was global, then change the module, this avoids
91
multiply defined symbol errors to first load private then make
92
global. Silly, isn't it. */
93
if ((mode & RTLD_GLOBAL))
94
{
95
if (!make_private_module_public)
96
{
97
_dyld_func_lookup("__dyld_NSMakePrivateModulePublic",
98
(void**)&make_private_module_public);
99
}
100
make_private_module_public((NSModule)module);
101
}
102
break;
103
case NSObjectFileImageInappropriateFile:
104
/* It may have been a dynamic library rather than a bundle, try to load it */
105
module = (void *)NSAddImage(path, NSADDIMAGE_OPTION_RETURN_ON_ERROR);
106
break;
107
case NSObjectFileImageFailure:
108
error(0,"Object file setup failure : \"%s\"", path);
109
return 0;
110
case NSObjectFileImageArch:
111
error(0,"No object for this architecture : \"%s\"", path);
112
return 0;
113
case NSObjectFileImageFormat:
114
error(0,"Bad object file format : \"%s\"", path);
115
return 0;
116
case NSObjectFileImageAccess:
117
error(0,"Can't read object file : \"%s\"", path);
118
return 0;
119
}
120
if (!module)
121
error(0, "Can not open \"%s\"", path);
122
return module;
123
}
124
125
static int
126
is_mach_header(void *handle)
127
{ /* Check for both possible magic numbers depending on x86/ppc byte order */
128
return ((((struct mach_header *)handle)->magic == MH_MAGIC) ||
129
(((struct mach_header *)handle)->magic == MH_CIGAM));
130
}
131
132
/* used by dlsym to find the symbol */
133
void *dlsymIntern(void *handle, const char *symbol)
134
{
135
NSSymbol nssym = NULL;
136
if (handle == (void *)-1)
137
{ /* Global context */
138
if (NSIsSymbolNameDefined(symbol))
139
nssym = NSLookupAndBindSymbol(symbol);
140
}
141
else
142
{
143
if (is_mach_header(handle))
144
{ /* library */
145
if (NSIsSymbolNameDefinedInImage((struct mach_header *)handle, symbol))
146
nssym = NSLookupSymbolInImage((struct mach_header *)handle, symbol,
147
NSLOOKUPSYMBOLINIMAGE_OPTION_BIND
148
| NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR);
149
}
150
else /* bundle */
151
nssym = NSLookupSymbolInModule((NSModule)handle, symbol);
152
}
153
if (!nssym)
154
{
155
error(0, "Symbol \"%s\" Not found", symbol);
156
return NULL;
157
}
158
return NSAddressOfSymbol(nssym);
159
}
160
161
const char *dlerror(void)
162
{
163
return error(1, (char *)NULL);
164
}
165
166
int dlclose(void *handle)
167
{
168
if (is_mach_header(handle))
169
{
170
error(0, "Can't remove dynamic libraries on darwin");
171
return 0;
172
}
173
if (!NSUnLinkModule((NSModule)handle, 0))
174
{
175
error(0, "unable to unlink module %s", NSNameOfModule((NSModule)handle));
176
return 1;
177
}
178
return 0;
179
}
180
181
/* dlsym, prepend the underscore and call dlsymIntern */
182
void *dlsym(void *handle, const char *symbol)
183
{
184
static char undersym[257]; /* Saves calls to malloc(3) */
185
int sym_len = strlen(symbol);
186
void *value = NULL;
187
char *malloc_sym = NULL;
188
189
if (sym_len < 256)
190
{
191
snprintf(undersym, 256, "_%s", symbol);
192
value = dlsymIntern(handle, undersym);
193
}
194
else
195
{
196
malloc_sym = (char*)malloc(sym_len + 2);
197
if (malloc_sym)
198
{
199
sprintf(malloc_sym, "_%s", symbol);
200
value = dlsymIntern(handle, malloc_sym);
201
pari_free(malloc_sym);
202
}
203
else
204
error(0, "Unable to allocate memory");
205
}
206
return value;
207
}
208
209
#endif
210
211