dynelf_xsb_i.h

00001 /* File:      dynelf_xsb_i.h
00002 ** Author(s): Harald Schroepfer, Steve Dawson
00003 ** Contact:   xsb-contact@cs.sunysb.edu
00004 ** 
00005 ** Copyright (C) The Research Foundation of SUNY, 1986, 1993-1998
00006 ** Copyright (C) ECRC, Germany, 1990
00007 ** 
00008 ** XSB is free software; you can redistribute it and/or modify it under the
00009 ** terms of the GNU Library General Public License as published by the Free
00010 ** Software Foundation; either version 2 of the License, or (at your option)
00011 ** any later version.
00012 ** 
00013 ** XSB is distributed in the hope that it will be useful, but WITHOUT ANY
00014 ** WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00015 ** FOR A PARTICULAR PURPOSE.  See the GNU Library General Public License for
00016 ** more details.
00017 ** 
00018 ** You should have received a copy of the GNU Library General Public License
00019 ** along with XSB; if not, write to the Free Software Foundation,
00020 ** Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00021 **
00022 ** $Id: dynelf_xsb_i.h,v 1.23 2006/06/11 21:35:10 tswift Exp $
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 /* wind2unix.h must be included after sys/stat.h */
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 /* Under which Solaris is this needed? Doesn't seem to be needed under
00053    2.6,2.7,2.8 */
00054 /*extern int putenv(const char *);*/
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   char  ldtemp; 
00077   char  *ldp1,*ldp2;
00078   static XSB_StrDefine(ldstring_oldenv);
00079   static XSB_StrDefine(ldstring_newenv);
00080   char  *libpath;
00081   */
00082   
00083   /* (1) create filename.so */
00084   strcpy(sofilename, pofilename);
00085   
00086   file_extension_ptr = xsb_strrstr(sofilename, XSB_OBJ_EXTENSION_STRING);
00087 #if (defined(__APPLE__))
00088   /* replace the OBJ file suffix with the dylib suffix */
00089  strcpy(file_extension_ptr+1, "dylib");
00090 #else
00091   /* replace the OBJ file suffix with the so suffix */
00092  strcpy(file_extension_ptr+1, "so");
00093 #endif
00094 
00095   /* (1.5) include necessary paths into LD_LIBRARY_PATH
00096      so that the right libraries would be consulted at loading time.
00097      NOTE: Some systems (Solaris) ignore runtime changes of LD_LIBRARY_PATH,
00098      so adding these libraries won't help. This works on Linux, though.
00099 
00100      On systems where LD_LIBRARY_PATH can't be changed at runtime, one should
00101      build the runtime library search path into the .so module at compile
00102      time. Usually, this is done with the -R linker flag.
00103      XSB provides a predicate, runtime_loader_flag/2, which gives the
00104      appropriate loader flag, if possible.
00105   */
00106 
00107 #if 0 /* skip setting LD_LIBRARY_PATH -- many systems ignore runtime changes */
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   /* search for -Lpath, -L"paths" or -L'paths' */
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 /* skipping LD_LIBRARY_PATH */
00142   
00143   /* (2) open the needed object */
00144   handle = dlopen(sofilename, RTLD_LAZY);
00145 
00146   /*
00147   if (putenv(ldstring_oldenv.string) != 0)
00148     xsb_error("LOAD_OBJ_DYN: can't restore the value of LD_LIBRARY_PATH");
00149   */
00150   
00151 
00152   if (handle == 0) {
00153     xsb_mesg("%s", dlerror());
00154     return NULL;
00155   }
00156 
00157   
00158   /* (3) find address of function and data objects */
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 

Generated on Wed Jul 26 13:30:40 2006 for XSB by  doxygen 1.4.5