00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include <dlfcn.h>
00028 #include <sys/file.h>
00029 #include <sys/types.h>
00030 #include <sys/stat.h>
00031 #include <errno.h>
00032 #include <stdio.h>
00033 #include <stdlib.h>
00034 #include <string.h>
00035
00036
00037 #include "wind2unix.h"
00038 #include "auxlry.h"
00039 #include "cell_xsb.h"
00040 #include "memory_xsb.h"
00041 #include "inst_xsb.h"
00042 #include "psc_xsb.h"
00043 #include "error_xsb.h"
00044 #include "io_builtins_xsb.h"
00045 #include "varstring_xsb.h"
00046 #include "string_xsb.h"
00047 #include "extensions_xsb.h"
00048
00049 #define BUFFEXTRA 1024
00050
00051 #if (defined(SOLARIS) && defined(__GNUC__))
00052
00053
00054
00055 #endif
00056
00057
00058
00059 static xsbBool dummy(void)
00060 {
00061 xsb_error("LOADER: Trying to use an undefined foreign procedure");
00062 return FALSE;
00063 }
00064
00065
00066
00067 static byte *load_obj_dyn(char *pofilename, Psc cur_mod, char *ld_option)
00068 {
00069 char *name;
00070 Pair search_ptr;
00071 char sofilename[MAXPATHLEN];
00072 void *handle;
00073 void *funcep;
00074 char *file_extension_ptr;
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084 strcpy(sofilename, pofilename);
00085
00086 file_extension_ptr = xsb_strrstr(sofilename, XSB_OBJ_EXTENSION_STRING);
00087 #if (defined(__APPLE__))
00088
00089 strcpy(file_extension_ptr+1, "dylib");
00090 #else
00091
00092 strcpy(file_extension_ptr+1, "so");
00093 #endif
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107 #if 0
00108 libpath = getenv("LD_LIBRARY_PATH");
00109 if (libpath == NULL)
00110 libpath = "";
00111 XSB_StrSet(&ldstring_oldenv,"LD_LIBRARY_PATH=");
00112 XSB_StrAppend(&ldstring_oldenv, libpath);
00113 XSB_StrSetV(&ldstring_newenv,&ldstring_oldenv);
00114
00115
00116 for (ldp1=ld_option; (*ldp1); ldp1++) {
00117 if (*ldp1 == '-' && *(ldp1+1) == 'L') {
00118 ldp1 += 2;
00119 ldp2 = ldp1;
00120 while (*ldp1 != ' ' && *ldp1 != '\0')
00121 ldp1++;
00122 *ldp1 = '\0';
00123 ldtemp = *(ldp2-1);
00124 *(ldp2-1) = ':';
00125 XSB_StrAppend(&ldstring_newenv, ldp2-1);
00126 *ldp1 = ' ';
00127 *(ldp2-1) = ldtemp;
00128 } else if (*ldp1 == '\'') {
00129 ldp1++;
00130 while (*ldp1 != '\'')
00131 ldp1++;
00132 } else if (*ldp1 == '\"') {
00133 ldp1++;
00134 while (*ldp1 != '\"')
00135 ldp1++;
00136 }
00137 }
00138
00139 if (putenv(ldstring_newenv.string) != 0)
00140 xsb_error("LOAD_OBJ_DYN: can't adjust LD_LIBRARY_PATH");
00141 #endif
00142
00143
00144 handle = dlopen(sofilename, RTLD_LAZY);
00145
00146
00147
00148
00149
00150
00151
00152 if (handle == 0) {
00153 xsb_mesg("%s", dlerror());
00154 return NULL;
00155 }
00156
00157
00158
00159 search_ptr = (Pair)get_data(cur_mod);
00160 while (search_ptr) {
00161 name = get_name(search_ptr->psc_ptr);
00162
00163 if (get_type(search_ptr->psc_ptr) == T_FORN) {
00164 if ((funcep = (int *) dlsym(handle, name)) == NULL) {
00165 fprintf(stdwarn, "%s\n", dlerror());
00166 xsb_warn("LOADER: Cannot find foreign procedure %s", name);
00167 set_forn(search_ptr->psc_ptr, (byte *)(dummy));
00168 } else {
00169 set_forn(search_ptr->psc_ptr, (byte *)(funcep));
00170 }
00171
00172 }
00173 search_ptr = search_ptr->next;
00174 }
00175 return (byte *)4;
00176 }
00177