26char *findExecutable(
const char *av0,
char *buffer);
36#include <mach-o/dyld.h>
39typedef void (*prismf)(void);
42int Yap_CallFunctionByName(
const char *thing_string);
44int Yap_CallFunctionByName(
const char *thing_string) {
45 void *handle = dlopen(NULL, RTLD_LAZY
55 Yap_Error(SYSTEM_ERROR_INTERNAL, ARG1,
56 "Dynamic linking on main module : %s\n", dlerror());
58 prismf *addr = (prismf *)dlsym(handle, thing_string);
69char *Yap_FindExecutable(
void) {
74 char *buf = malloc(MAX_PATH);
77 if (!_NSGetExecutablePath(buf, &size)) {
78 buf = realloc(buf, size + 1);
82#elif defined(__linux__)
83 char *buf = malloc(MAX_PATH);
84 ssize_t len = readlink(
"/proc/self/exe", buf, MAX_PATH - 1);
91#elif defined(__FreeBSD__) || defined(__DragonFly__)
92 enum { BUFFERSIZE = 1024 };
93 char *buf = malloc(BUFFERSIZE);
94 ssize_t len = readlink(
"/proc/curproc/file", buf,
sizeof(buf) - 1);
103 mib[2] = KERN_PROC_PATHNAME;
105 size_t cb = BUFFERSIZE;
106 sysctl(mib, 4, buf, &cb, NULL, 0);
112void *Yap_LoadForeignFile(
char *file,
int flags) {
117 if (flags & EAGER_LOADING)
121 if (flags & GLOBAL_LOADING)
122 dlflag |= RTLD_GLOBAL;
125 dlflag |= RTLD_LOCAL;
127 out = (
void *)dlopen(file, dlflag);
129 const char *m_os = dlerror();
131 LOCAL_ErrorMessage = malloc(MAX_ERROR_MSG_SIZE);
132 strncpy(LOCAL_ErrorMessage, m_os, MAX_ERROR_MSG_SIZE - 1);
134 LOCAL_ErrorMessage =
"dlopen failed";
140int Yap_CallForeignFile(
void *handle,
char *f) {
141 YapInitProc proc = (YapInitProc)dlsym(handle, f);
150int Yap_CloseForeignFile(
void *handle) {
151 if (dlclose(handle) < 0) {
153 Yap_Error(SYSTEM_ERROR_INTERNAL, ARG1,
"dlclose error %s\n", dlerror());
160error_found(
char *omsg,
char *text, ...)
165 omsg =
Malloc(MAX_PATH+1);
166 strcpy(omsg,
"Got the following OS errors:\n");
170 omsg = Realloc(omsg,strlen(omsg)+MAX_PATH);
173 vsnprintf(omsg+strlen(omsg),MAX_PATH-1,text, ap);
184 LOCAL_ErrorMessage = NULL;
187 int lvl = push_text_stack();
189 while (libs || ofiles) {
194 file = AtomName(path->name);
196 if ((path->handle = dlopen(file, RTLD_LAZY | RTLD_GLOBAL)) ==
199 omsg = error_found(omsg,
"Tried to load %s, failed: %s\n", file, dlerror());
201 Yap_ThrowError(SYSTEM_ERROR_DYNAMIC_LOADER,MkAtomTerm(path->name),omsg);
206 omsg = error_found(omsg,
"loaded %s\n", file );
208 if (!libs) ofiles = ofiles->next;
209 else libs = libs->next;
216 YapInitProc o = dlsym(ofiles
220 file = AtomName(ofiles->name);
221 omsg = error_found(omsg,
"%s no symbol in %s, failed: %s\n", proc_name, file, dlerror());
222 ofiles = ofiles->next;
224 Yap_ThrowError(SYSTEM_ERROR_DYNAMIC_LOADER, MkAtomTerm(o0->name),omsg);
230 YapInitProc *init_proc) {
231 *init_proc = LoadForeign(ofiles, libs, proc_name);
232 return *init_proc != NULL? LOAD_SUCCEEDED :LOAD_FAILLED;
235void Yap_ShutdownLoadForeign(
void) {
238 f_code = ForeignCodeLoaded;
239 while (f_code != NULL) {
244 while (objs != NULL) {
246 if (dlclose(objs->handle) != 0)
249 Yap_FreeCodeSpace((ADDR)old);
252 while (libs != NULL) {
254 if (dlclose(libs->handle) != 0)
257 Yap_FreeCodeSpace((ADDR)old);
259 f_code = f_code->next;
260 Yap_FreeCodeSpace((ADDR)of_code);
267 ForeignCodeLoaded = NULL;
271 YapInitProc *init_proc) {
272 return (*init_proc = LoadForeign(ofiles, libs, proc_name)) != NULL;
load_foreign_files/3 has works for the following configurations:
void * Malloc(size_t sz USES_REGS)
allocate a temporary text block