/************************************************************************* * * (c) Copyright 1992 - 1995 by G & A Technical Software, Inc., * 28 Research Drive, Hampton, Virginia, 23666. * * All Rights Reserved. No part of this software or publication may be * reproduced, stored in a retrieval system, or transmitted, in any form * or by any means, electronic, mechanical, photocopying, recording, or * otherwise without the prior written permission of G & A Technical * Software, Inc. * ************************************************************************* * * Filename: s3_dynamic_module_load.c * * Purpose: Provides the ability to dynamically load modules where * O/S provides necessary support. Currently supports two * methods of dynamic loading, GNU dld and dlopen/dlclose/etc * for ELF based systems. * * Author: John Burton * * Date: 5/31/94 * ************************************************************************* * * Modification History: * * $Log$ * ************************************************************************* * * Revision Control Information: */ static char rcsid[] = "$Id$"; static char rcsrev[] = "$Revision$"; #include "s3_machdep.h" #include "s3_defines.h" #include "s3_typedefs.h" #include "s3_externals.h" #include "s3_globals.h" #include typedef struct mlstruct { char *name; char *file; struct mlstruct *next; } Mod_list; static Mod_list *Modules = NULL; static int is_in(char c, char *s) { while(*s) if(*s++ == c) return 1; return 0; } int s3_dml_init (char *path) { } extern File_list *lib_list; void s3_load_dynamic_modules(EMT_struct *emt_info) { int i,result,len; ST_entry *entry; char *tmp,*name; File_list *curr; void *vptr = NULL; extern char *shapplib; extern File_list *s3_GetLibList(); curr = s3_GetLibList(); fprintf(fperr,"Loading Dynamic Modules from %s ",shapplib); for(i=0;inexe;i++) { entry = emt_info->exe_list[i]; name = strsave(entry->en.name); #ifdef _F2C_SUFFIX if((entry->en.type == FFUNC)||(entry->en.type == FSUBR)) { if(is_in('_',entry->en.name)) name = strsave2(name,"__"); else name = strcat(name,"_"); } #endif if((entry->en.vptr = dlopen(entry->en.filename,RTLD_NOW)) == NULL) s3_error(SYS,"s3_load_dynamic_modules - dlopen: %s",dlerror()); if((entry->en.function = (int (*)()) dlsym(entry->en.vptr,name)) == NULL) { fprintf(fperr,"s3_load_dynamic_modules - dlsym returned NULL => %s\n",name); if ((tmp = dlerror()) != NULL) s3_error(SYS,"s3_load_dynamic_modules - dlsym: %s => %s",tmp,name); } xfree(name); } fprintf(fperr," done\n"); } void s3_dml_unload_symbol(char *symbol) { } void s3_unload_dynamic_modules(EMT_struct *emt_info) { int i,result; ST_entry *entry; char tmp[80]; File_list *curr; extern File_list *s3_GetLibList(); curr = s3_GetLibList(); fprintf(fperr,"Unloading Dynamic Modules "); for(i=0;inexe;i++) { fprintf(fperr,"."); entry = emt_info->exe_list[i]; dlclose(entry->en.vptr); } fprintf(fperr," done\n"); } void s3_compile_dynamic_modules(EMT_struct *emt_info) { int len, ulen, result, i; ST_entry *entry; char *tmp,*name; extern char *uliblist; extern char *applib; char *dloadflags = D_LOADFLAGS; char *compiler; uliblist = (uliblist == NULL) ? strdup(" ") : uliblist; ulen = (uliblist != NULL) ? strlen(uliblist) : 0; len = strlen(Cflags) + strlen(applib) + ulen + strlen(dloadflags) + 80; tmp = (char *)xmalloc(len*sizeof(char)); fprintf(fperr,"Compiling Dynamic Modules "); /* for(i=0;inexe;i++) { fprintf(fperr,"."); entry = emt_info->exe_list[i]; name = strsave(entry->en.name); if((entry->en.type == FFUNC)||(entry->en.type == FSUBR)) { compiler = F_COMPILER; #ifdef _F2C_SUFFIX if(is_in('_',name)) name = stringcat(name,"__"); else name = stringcat(name,"_"); #endif } else compiler = C_COMPILER; #ifdef _USE_S3_DL sprintf(tmp,"%s %s -o %s -bM:SRE -e %s %s %s %s >> %s",compiler Cflags,entry->en.filename,entry->en.name,applib,uliblist,D_LOADFLAGS,logfile); #else sprintf(tmp,"%s -shared -rdynamic %s -o %s -u %s -L./shlibs -l%s %s %s & >>%s", compiler,Cflags,entry->en.filename,name,progname,uliblist,dloadflags,logfile); #endif result = system(tmp); if(result < 0) s3_error(SYS,"s3_compile_dynamic_modules: fork or exec system call failed for command %s",tmp); if(result > 0) s3_error(SYS,"s3_compile_dynamic_modules: system call returned %d for command %s",result,tmp); xfree(name); } */ fprintf(fperr," done\n"); xfree(tmp); } void s3_put_file_module(char * name) { char nm_command[80]; char dummy[80]; char symbol[80]; FILE* pipe; int result; Mod_list *tmp; strcpy(nm_command,"nm -p "); strcat(nm_command,name); pipe = popen(nm_command,"r"); while (!feof(pipe)) { fgets(dummy,80,pipe); strcpy(symbol,&dummy[8]); if (!strncmp(symbol," T ",3)) { symbol[strlen(symbol) - 1] = '\0'; tmp = (Mod_list *)xmalloc(sizeof(Mod_list)); tmp->name = strsave(&symbol[4]); tmp->file = strsave(name); tmp->next = Modules; Modules = tmp; } } result = pclose(pipe); } char *s3_get_module_file(char *name) { Mod_list *tmp; char *filename; int result; tmp = Modules; while((tmp != NULL)&&((result = strcmp(name,tmp->name)) != 0)) { tmp = tmp->next; } if(tmp != NULL) return tmp->file; else return NULL; }