2002-09-24 15:20:56 +01:00
|
|
|
/*************************************************************************
|
|
|
|
* *
|
|
|
|
* YAP Prolog *
|
|
|
|
* *
|
|
|
|
* Yap Prolog was developed at NCCUP - Universidade do Porto *
|
|
|
|
* *
|
|
|
|
* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
|
|
|
|
* *
|
|
|
|
**************************************************************************
|
|
|
|
* *
|
2002-09-24 16:38:17 +01:00
|
|
|
* File: load_dyld.c *
|
|
|
|
* comments: dyld based dynamic loaderr of external routines *
|
|
|
|
* tested on MacOS *
|
2002-09-24 15:20:56 +01:00
|
|
|
*************************************************************************/
|
|
|
|
|
|
|
|
#include "Yap.h"
|
|
|
|
#include "Yatom.h"
|
2009-10-23 14:22:17 +01:00
|
|
|
#include "YapHeap.h"
|
2002-09-24 15:20:56 +01:00
|
|
|
#include "yapio.h"
|
|
|
|
#include "Foreign.h"
|
|
|
|
|
2002-09-24 16:38:17 +01:00
|
|
|
#if LOAD_DYLD
|
2002-09-24 15:20:56 +01:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2002-09-24 16:38:17 +01:00
|
|
|
/* Code originally from Rex A. Dieter's posting in comp.sys.next.programmer
|
|
|
|
and from dynload_next.c in the Python sources
|
|
|
|
*/
|
2002-09-24 15:20:56 +01:00
|
|
|
#import <mach-o/dyld.h>
|
|
|
|
|
2011-05-25 16:40:36 +01:00
|
|
|
|
2002-09-24 15:20:56 +01:00
|
|
|
|
|
|
|
static char *
|
2002-09-24 16:38:17 +01:00
|
|
|
mydlerror(void)
|
2002-09-24 15:20:56 +01:00
|
|
|
{
|
|
|
|
char *errString;
|
2011-05-25 16:40:36 +01:00
|
|
|
switch(LOCAL_dl_errno) {
|
2002-09-24 15:20:56 +01:00
|
|
|
default:
|
|
|
|
case NSObjectFileImageFailure:
|
|
|
|
case NSObjectFileImageFormat:
|
|
|
|
/* for these a message is printed on stderr by dyld */
|
|
|
|
errString = "Can't create object file image";
|
|
|
|
break;
|
|
|
|
case NSObjectFileImageSuccess:
|
|
|
|
errString = NULL;
|
|
|
|
break;
|
|
|
|
case NSObjectFileImageInappropriateFile:
|
|
|
|
errString = "Inappropriate file type for dynamic loading";
|
|
|
|
break;
|
|
|
|
case NSObjectFileImageArch:
|
|
|
|
errString = "Wrong CPU type in object file";
|
|
|
|
break;
|
|
|
|
case NSObjectFileImageAccess:
|
|
|
|
errString = "Can't read object file (no access)";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return(errString);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-24 16:38:17 +01:00
|
|
|
/*
|
|
|
|
* YAP_FindExecutable(argv[0]) should be called on yap initialization to
|
|
|
|
* locate the executable of Yap
|
|
|
|
*/
|
2014-03-04 12:00:13 +00:00
|
|
|
char *
|
|
|
|
Yap_FindExecutable(void)
|
2002-09-24 16:38:17 +01:00
|
|
|
{
|
2013-01-13 17:55:13 +00:00
|
|
|
char path[1024];
|
|
|
|
uint32_t size = sizeof(path);
|
|
|
|
if (_NSGetExecutablePath(path, &size) == 0) {
|
|
|
|
char *rc = malloc(size+1);
|
|
|
|
strncpy(rc, path, size);
|
|
|
|
return rc;
|
|
|
|
} else {
|
|
|
|
char *rc = malloc(size+1);
|
|
|
|
if (_NSGetExecutablePath(rc, &size) == 0)
|
|
|
|
return "yap";
|
|
|
|
return rc;
|
|
|
|
}
|
2002-09-24 16:38:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-24 15:20:56 +01:00
|
|
|
static void *
|
|
|
|
mydlopen(char *path)
|
|
|
|
{
|
|
|
|
int dyld_result;
|
|
|
|
NSObjectFileImage ofile;
|
|
|
|
NSModule handle = NULL;
|
|
|
|
dyld_result = NSCreateObjectFileImageFromFile(path, &ofile);
|
|
|
|
if (dyld_result != NSObjectFileImageSuccess) {
|
2011-05-25 16:40:36 +01:00
|
|
|
LOCAL_dl_errno = dyld_result;
|
2002-09-24 15:20:56 +01:00
|
|
|
} else {
|
|
|
|
/* NSLinkModule will cause the run to abort on any link error's */
|
|
|
|
/* not very friendly but the error recovery functionality is limited */
|
|
|
|
handle = NSLinkModule(ofile, path, TRUE);
|
2014-03-04 12:00:13 +00:00
|
|
|
}
|
|
|
|
return handle;
|
2002-09-24 15:20:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void *
|
|
|
|
mydlsym(char *symbol)
|
|
|
|
{
|
|
|
|
void *addr;
|
2002-09-24 16:38:17 +01:00
|
|
|
char funcname[256];
|
|
|
|
|
|
|
|
#if HAVE_SNPRINTF
|
2002-09-27 21:04:49 +01:00
|
|
|
snprintf(funcname, sizeof(funcname), "_%.200s", symbol);
|
2002-09-24 16:38:17 +01:00
|
|
|
#else
|
|
|
|
sprintf(funcname, "_%.200s", symbol);
|
|
|
|
#endif
|
|
|
|
if (NSIsSymbolNameDefined(funcname))
|
|
|
|
addr = NSAddressOfSymbol(NSLookupAndBindSymbol(funcname));
|
2002-09-24 15:20:56 +01:00
|
|
|
else
|
|
|
|
addr = NULL;
|
|
|
|
return addr;
|
|
|
|
}
|
|
|
|
|
2002-09-24 16:38:17 +01:00
|
|
|
static int
|
2002-09-24 15:20:56 +01:00
|
|
|
mydlclose(void *handle)
|
|
|
|
{
|
|
|
|
NSUnLinkModule(handle, NSUNLINKMODULE_OPTION_NONE);
|
2002-09-24 16:38:17 +01:00
|
|
|
return TRUE;
|
2002-09-24 15:20:56 +01:00
|
|
|
}
|
|
|
|
|
2010-06-17 00:29:01 +01:00
|
|
|
void *
|
|
|
|
Yap_LoadForeignFile(char *file, int flags)
|
|
|
|
{
|
|
|
|
return (void *)mydlopen(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
Yap_CallForeignFile(void *handle, char *f)
|
|
|
|
{
|
|
|
|
YapInitProc proc = (YapInitProc) mydlsym(f);
|
|
|
|
if (!proc)
|
|
|
|
return FALSE;
|
|
|
|
(*proc)();
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
Yap_CloseForeignFile(void *handle)
|
|
|
|
{
|
|
|
|
return mydlclose(handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-24 15:20:56 +01:00
|
|
|
/*
|
|
|
|
* LoadForeign(ofiles,libs,proc_name,init_proc) dynamically loads foreign
|
|
|
|
* code files and libraries and locates an initialization routine
|
|
|
|
*/
|
2002-11-11 17:38:10 +00:00
|
|
|
static Int
|
2002-09-24 15:20:56 +01:00
|
|
|
LoadForeign(StringList ofiles, StringList libs,
|
|
|
|
char *proc_name, YapInitProc *init_proc)
|
|
|
|
{
|
|
|
|
|
|
|
|
while (ofiles) {
|
|
|
|
void *handle;
|
|
|
|
|
|
|
|
/* mydlopen wants to follow the LD_CONFIG_PATH */
|
2016-02-13 02:56:19 +00:00
|
|
|
if (!Yap_locateFile(AtomName(ofiles->name), LOCAL_FileNameBuf, TRUE)) {
|
2011-05-23 16:19:47 +01:00
|
|
|
strcpy(LOCAL_ErrorSay, "%% Trying to open unexisting file in LoadForeign");
|
2002-09-24 15:20:56 +01:00
|
|
|
return LOAD_FAILLED;
|
|
|
|
}
|
2011-05-23 16:19:47 +01:00
|
|
|
if((handle=mydlopen(LOCAL_FileNameBuf)) == 0)
|
2002-09-24 15:20:56 +01:00
|
|
|
{
|
|
|
|
fprintf(stderr,"calling dlopen with error %s\n", mydlerror());
|
2011-05-23 16:19:47 +01:00
|
|
|
/* strcpy(LOCAL_ErrorSay,dlerror());*/
|
2002-09-24 15:20:56 +01:00
|
|
|
return LOAD_FAILLED;
|
|
|
|
}
|
|
|
|
|
|
|
|
ofiles->handle = handle;
|
|
|
|
|
|
|
|
ofiles = ofiles->next;
|
|
|
|
}
|
|
|
|
/* load libraries first so that their symbols are available to
|
|
|
|
other routines */
|
|
|
|
while (libs) {
|
2010-12-12 18:28:55 +00:00
|
|
|
char *s = AtomName(lib->name);
|
|
|
|
|
|
|
|
if (ls[0] == '-') {
|
2011-05-23 16:19:47 +01:00
|
|
|
strcpy(LOCAL_FileNameBuf,"lib");
|
|
|
|
strcat(LOCAL_FileNameBuf,s+2);
|
|
|
|
strcat(LOCAL_FileNameBuf,".so");
|
2002-09-24 15:20:56 +01:00
|
|
|
} else {
|
2011-05-23 16:19:47 +01:00
|
|
|
strcpy(LOCAL_FileNameBuf,s);
|
2002-09-24 15:20:56 +01:00
|
|
|
}
|
|
|
|
|
2011-05-23 16:19:47 +01:00
|
|
|
if((libs->handle=mydlopen(LOCAL_FileNameBuf)) == NULL)
|
2002-09-24 15:20:56 +01:00
|
|
|
{
|
2011-05-23 16:19:47 +01:00
|
|
|
strcpy(LOCAL_ErrorSay,mydlerror());
|
2002-09-24 15:20:56 +01:00
|
|
|
return LOAD_FAILLED;
|
|
|
|
}
|
|
|
|
libs = libs->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
*init_proc = (YapInitProc) mydlsym(proc_name);
|
|
|
|
|
|
|
|
if(! *init_proc) {
|
2011-05-23 16:19:47 +01:00
|
|
|
strcpy(LOCAL_ErrorSay,"Could not locate initialization routine");
|
2002-09-24 15:20:56 +01:00
|
|
|
return LOAD_FAILLED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return LOAD_SUCCEEDED;
|
|
|
|
}
|
|
|
|
|
2002-11-11 17:38:10 +00:00
|
|
|
Int
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_LoadForeign(StringList ofiles, StringList libs,
|
2002-11-11 17:38:10 +00:00
|
|
|
char *proc_name, YapInitProc *init_proc)
|
|
|
|
{
|
|
|
|
return LoadForeign(ofiles, libs, proc_name, init_proc);
|
|
|
|
}
|
|
|
|
|
2002-09-24 15:20:56 +01:00
|
|
|
void
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_ShutdownLoadForeign(void)
|
2002-09-24 15:20:56 +01:00
|
|
|
{
|
|
|
|
ForeignObj *f_code;
|
|
|
|
|
|
|
|
f_code = ForeignCodeLoaded;
|
|
|
|
while (f_code != NULL) {
|
|
|
|
StringList objs, libs;
|
|
|
|
|
|
|
|
objs = f_code->objs;
|
|
|
|
while (objs != NULL) {
|
|
|
|
if (mydlclose(objs->handle) != 0)
|
|
|
|
return; /* ERROR */
|
|
|
|
objs = objs->next;
|
|
|
|
}
|
|
|
|
libs = f_code->libs;
|
|
|
|
while (libs != NULL) {
|
|
|
|
if (mydlclose(libs->handle) != 0)
|
|
|
|
return; /* ERROR */
|
|
|
|
objs = libs->next;
|
|
|
|
}
|
|
|
|
f_code = f_code->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Int
|
2002-11-18 18:18:05 +00:00
|
|
|
Yap_ReLoadForeign(StringList ofiles, StringList libs,
|
2002-09-24 15:20:56 +01:00
|
|
|
char *proc_name, YapInitProc *init_proc)
|
|
|
|
{
|
|
|
|
return(LoadForeign(ofiles,libs, proc_name, init_proc));
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|