/* * Thu Mar 6 09:17:56 CET 2003 * * * Minimal plug-in sample code * */ #include #include #ifdef __DARWIN__ # include # define SHLT NSModule #else # include # define SHLT void * #endif int mint=0; SHLT load(char *name,float (**fun)(float)); int unload(SHLT module); main( int argc, char **argv) { SHLT mod; float x; float (*f)(float); if (argc<2) { fprintf(stderr,"Usage: %s object_file\n",argv[0]); exit(1); } /* Carga del objeto. Devuelve 1 si lo consigue */ if(NULL!=(mod=load(argv[1],&f))) { for (x=0; x<10; x++) { printf("f(%f) = %f\n",x,f(x)); mint++; } } unload(mod); } void init( void ) { static int a=0; if (a == 0) { puts("Init"); a++ ; } } #ifndef __DARWIN__ SHLT load(char *name,float (**fun)(float)) { void *coderef; char *errstr; int res; char ename[PATH_MAX]; char **ident; *fun = NULL; printf("Loading %s\n",name); /* sprintf(ename, "./%s", name);*/ sprintf(ename, "%s", name); res = 0; /* * Intenta abrir el fichero objeto, Devuelve NULL si no lo encuentra, * si lo encuentra devuelve una referencia al trozo de codigo y * variables * * RTLD_NOW es para que todas las referencias a variables que haya * en el fichero se resuelvan en el momento de la carga */ coderef = dlopen(ename,RTLD_NOW); if (coderef != NULL) { /* Acceso a una funcion */ *fun = dlsym(coderef,"fun"); if ( res = (*fun != NULL)) { if ( NULL == ( ident = (char **)dlsym(coderef, "_name"))) { fprintf(stderr,"can't get name\n"); } else { fprintf(stderr,"name: (%p) %s\n", *ident, *ident); } } } if ((errstr=dlerror()) != NULL) fprintf(stderr,"%s\n",errstr); return coderef; } int unload(SHLT module) { int res = dlclose(module); if (res) { perror("dlclose"); return 1; } return 0; } #else SHLT load(char *name,float (**fun)(float)) { NSObjectFileImage image; NSObjectFileImageReturnCode result; NSModule handle = NULL; NSSymbol symbol; void *sym; char **ident; const char* err = NULL; printf("Loading %s\n",name); result = NSCreateObjectFileImageFromFile(name, &image); if (result == NSObjectFileImageSuccess) { handle = NSLinkModule(image,name,NSLINKMODULE_OPTION_RETURN_ON_ERROR); /* If something went wrong, get the errors... */ if (!handle) { NSLinkEditErrors c; int errornumber; const char *filename; NSLinkEditError(&c, &errornumber, &filename, &err); } NSDestroyObjectFileImage(image); } else if (( result == NSObjectFileImageFormat || result == NSObjectFileImageInappropriateFile)) { err = "invalid MH_BUNDLE file"; } else { err = "unable to create object file image"; } if (err==NULL) { if (NULL==(symbol = NSLookupSymbolInModule(handle,"__name"))) { fprintf(stderr,"can't get name\n"); } else if (NULL!=(ident = (char **)NSAddressOfSymbol(symbol))) { fprintf(stderr,"name: (%p) %s\n", *ident, *ident); } fflush(stderr); if (NULL==(symbol = NSLookupSymbolInModule(handle,"_fun"))) { err = "undefined"; } else if (NULL==(*fun = NSAddressOfSymbol(symbol))) { err = "can't resolve address"; *fun=0; } } if ( NULL != err ) { fprintf(stderr,"Err: %s\n",err); } return handle; } int unload(SHLT module) { NSUnLinkModule(module, FALSE); return 0; } #endif