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