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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
|
/*
load_opcodes.c:
Copyright (C) 1991 Barry Vercoe, John ffitch
This file is part of Csound.
The Csound Library is free software; you can redistribute it
and/or modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
Csound is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with Csound; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA
*/
#include "cs.h"
#include "csound.h"
#if defined(WIN32)
#define _WINSOCKAPI_
#include <io.h>
#if !defined(__CYGWIN__)
#include <direct.h>
#endif
#include <windows.h>
PUBLIC void *csoundOpenLibrary(const char *libraryPath)
{
void *library = 0;
library = (void *) LoadLibrary(libraryPath);
return library;
}
PUBLIC void *csoundCloseLibrary(void *library)
{
return 0;
}
PUBLIC void *csoundGetLibrarySymbol(void *library,
const char *procedureName)
{
void *procedureAddress = 0;
procedureAddress = GetProcAddress((HINSTANCE) library,
procedureName);
return procedureAddress;
}
#elif defined(LINUX)
#include <dlfcn.h>
#include <dirent.h>
#include <string.h>
void *csoundOpenLibrary(const char *libraryPath)
{
void *library = 0;
library = dlopen(libraryPath, RTLD_NOW | RTLD_GLOBAL );
if(!library)
{
fprintf(stderr, "Error in dlopen(): '%s'\n", dlerror());
}
return library;
}
void *csoundCloseLibrary(void *library)
{
void *returnValue = 0;
returnValue = (void *)dlclose(library);
return returnValue;
}
void *csoundGetLibrarySymbol(void *library,
const char *procedureName)
{
void *procedureAddress = 0;
procedureAddress = dlsym(library, procedureName);
if (!procedureAddress) {
printf("Failed to find %s in library: %s\n", procedureName, dlerror());
}
return procedureAddress;
}
#elif defined(MACOSX)
#define ERR_STR_LEN 255
#include <mach-o/dyld.h>
/* Set and get the error string for use by dlerror */
static const char *error(int setget, const char *str, ...)
{
static char errstr[ERR_STR_LEN];
static int err_filled = 0;
const char *retval;
NSLinkEditErrors ler;
int lerno;
const char *dylderrstr;
const char *file;
va_list arg;
if (setget <= 0)
{
va_start(arg, str);
strncpy(errstr, "dlsimple: ", ERR_STR_LEN);
vsnprintf(errstr + 10, ERR_STR_LEN - 10, str, arg);
va_end(arg);
/* We prefer to use the dyld error string if getset is 1*/
if (setget == 0) {
NSLinkEditError(&ler, &lerno, &file, &dylderrstr);
fprintf(stderr,"dyld: %s\n",dylderrstr);
if (dylderrstr && strlen(dylderrstr))
strncpy(errstr,dylderrstr,ERR_STR_LEN);
}
err_filled = 1;
retval = NULL;
}
else
{
if (!err_filled)
retval = NULL;
else
retval = errstr;
err_filled = 0;
}
return retval;
}
/* dlsymIntern is used by dlsym to find the symbol */
void *dlsymIntern(void *handle, const char *symbol)
{
NSSymbol *nssym = 0;
/* If the handle is -1, if is the app global context */
if (handle == (void *)-1)
{
/* Global context, use NSLookupAndBindSymbol */
if (NSIsSymbolNameDefined(symbol))
{
nssym = NSLookupAndBindSymbol(symbol);
}
}
/* Now see if the handle is a struch mach_header* or not,
use NSLookupSymbol in image for libraries, and
NSLookupSymbolInModule for bundles */
else
{
/* Check for both possible magic numbers depending
on x86/ppc byte order */
if ((((struct mach_header *)handle)->magic == MH_MAGIC) ||
(((struct mach_header *)handle)->magic == MH_CIGAM))
{
if (NSIsSymbolNameDefinedInImage((struct mach_header *)handle,
symbol))
{
nssym = NSLookupSymbolInImage((struct mach_header *)handle,
symbol, NSLOOKUPSYMBOLINIMAGE_OPTION_BIND
| NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR);
}
}
else
{
nssym = NSLookupSymbolInModule(handle, symbol);
}
}
if (!nssym)
{
error(0, "Symbol \"%s\" Not found", symbol);
return NULL;
}
return NSAddressOfSymbol(nssym);
}
void *csoundOpenLibrary(const char *libraryPath)
{
void *module = 0;
NSObjectFileImage ofi = 0;
NSObjectFileImageReturnCode ofirc;
static int (*make_private_module_public) (NSModule module) = 0;
unsigned int flags = NSLINKMODULE_OPTION_RETURN_ON_ERROR |
NSLINKMODULE_OPTION_PRIVATE;
/* If we got no path, the app wants the global namespace,
use -1 as the marker in this case */
if (!libraryPath)
return (void *)-1;
/* Create the object file image, works for things linked
with the -bundle arg to ld */
ofirc = NSCreateObjectFileImageFromFile(libraryPath, &ofi);
switch (ofirc)
{
case NSObjectFileImageSuccess:
/* It was okay, so use NSLinkModule to link in the image */
module = NSLinkModule(ofi, libraryPath,flags);
/* Don't forget to destroy the object file
image, unless you like leaks */
NSDestroyObjectFileImage(ofi);
/* If the mode was global, then change the module, this avoids
multiply defined symbol errors to first load private then
make global. Silly, isn't it. */
if (!make_private_module_public)
{
_dyld_func_lookup("__dyld_NSMakePrivateModulePublic",
(unsigned long *)&make_private_module_public);
}
make_private_module_public(module);
break;
case NSObjectFileImageInappropriateFile:
/* It may have been a dynamic library rather
than a bundle, try to load it */
module = (void *)NSAddImage(libraryPath,
NSADDIMAGE_OPTION_RETURN_ON_ERROR);
break;
case NSObjectFileImageFailure:
error(0,"Object file setup failure : \"%s\"", libraryPath);
return 0;
case NSObjectFileImageArch:
error(0,"No object for this architecture : \"%s\"", libraryPath);
return 0;
case NSObjectFileImageFormat:
error(0,"Bad object file format : \"%s\"", libraryPath);
return 0;
case NSObjectFileImageAccess:
error(0,"Can't read object file : \"%s\"", libraryPath);
return 0;
}
if (!module)
error(0, "Can not open \"%s\"", libraryPath);
return module;
}
void *csoundCloseLibrary(void *library)
{
if ((((struct mach_header *)library)->magic == MH_MAGIC) ||
(((struct mach_header *)library)->magic == MH_CIGAM))
{
error(-1, "Can't remove dynamic libraries on darwin");
return 0;
}
if (!NSUnLinkModule(library, 0))
{
error(0, "unable to unlink module %s", NSNameOfModule(library));
//return 1;
return 0;
}
return 0;
}
void *csoundGetLibrarySymbol(void *library, const char *procedureName)
{
static char undersym[257]; /* Saves calls to malloc(3) */
int sym_len = strlen(procedureName);
void *value = NULL;
char *malloc_sym = NULL;
if (sym_len < 256)
{
snprintf(undersym, 256, "_%s", procedureName);
value = dlsymIntern(library, undersym);
}
else
{
malloc_sym = malloc(sym_len + 2);
if (malloc_sym)
{
sprintf(malloc_sym, "_%s", procedureName);
value = dlsymIntern(library, malloc_sym);
free(malloc_sym);
}
else
{
error(-1, "Unable to allocate memory");
}
}
return value;
}
#else
// not sure what this is for - SYY - 11.5.2003
//#include <extras.h>
void *csoundOpenLibrary(const char *libraryPath)
{
return 0;
}
void *csoundCloseLibrary(void *library)
{
return 0;
}
void *csoundGetLibrarySymbol(void *library, const char *procedureName)
{
return 0;
}
#endif
PUBLIC int csoundLoadExternal(void *csound, const char* libraryPath) {
void *handle;
OENTRY *opcodlst_n;
long length, olength;
OENTRY *(*init)(GLOBALS*);
long (*size)(void);
handle = csoundOpenLibrary(libraryPath);
if(!handle) {
return -1;
}
#ifdef BETA
printf("Found handle\n");
#endif
size = csoundGetLibrarySymbol(handle, "opcode_size");
if(!size)
{
return -1;
}
#ifdef BETA
printf("Found size\n");
#endif
length = (*size)();
#ifdef BETA
printf("Length=%d\n", length);
#endif
init = csoundGetLibrarySymbol(handle, "opcode_init");
if(!init)
{
return -1;
}
#ifdef BETA
printf("Found init\n");
printf("Calling init\n");
#endif
opcodlst_n = (*init)(&cglob);
olength = oplstend-opcodlst;
#ifdef BETA
printf("Got opcodlst %p\noplstend=%p, opcodlst=%p, length=%d\n",
opcodlst_n, oplstend, opcodlst, olength);
printf("Adding %d(%d) -- first opcode is %s\n",
length, length/sizeof(OENTRY), opcodlst_n[0].opname);
#endif
opcodlst = (OENTRY*) mrealloc(opcodlst, olength*sizeof(OENTRY) + length);
memcpy(opcodlst+olength, opcodlst_n, length);
oplstend = opcodlst + olength + length/sizeof(OENTRY);
return 0;
}
PUBLIC int csoundLoadExternals(void *csound)
{
char *libname;
char buffer[256];
if(cglob.oplibs == NULL)
{
return 1;
}
printf("Loading libraries %s\n", cglob.oplibs);
strcpy(buffer, cglob.oplibs);
libname = strtok(buffer, ",");
while(libname != NULL)
{
csoundLoadExternal(csound, libname); // error code not currently checked!!!
libname = strtok(NULL, ",");
}
return 1;
}
|