29#import <mach-o/dyld.h>
37 switch(LOCAL_dl_errno) {
39 case NSObjectFileImageFailure:
40 case NSObjectFileImageFormat:
42 errString =
"Can't create object file image";
44 case NSObjectFileImageSuccess:
47 case NSObjectFileImageInappropriateFile:
48 errString =
"Inappropriate file type for dynamic loading";
50 case NSObjectFileImageArch:
51 errString =
"Wrong CPU type in object file";
53 case NSObjectFileImageAccess:
54 errString =
"Can't read object file (no access)";
66Yap_FindExecutable(
void)
69 uint32_t size =
sizeof(path);
70 if (_NSGetExecutablePath(path, &size) == 0) {
71 char *rc = malloc(size+1);
72 strncpy(rc, path, size);
75 char *rc = malloc(size+1);
76 if (_NSGetExecutablePath(rc, &size) == 0)
87 NSObjectFileImage ofile;
88 NSModule handle = NULL;
89 dyld_result = NSCreateObjectFileImageFromFile(path, &ofile);
90 if (dyld_result != NSObjectFileImageSuccess) {
91 LOCAL_dl_errno = dyld_result;
95 handle = NSLinkModule(ofile, path, TRUE);
107 snprintf(funcname,
sizeof(funcname),
"_%.200s", symbol);
109 sprintf(funcname,
"_%.200s", symbol);
111 if (NSIsSymbolNameDefined(funcname))
112 addr = NSAddressOfSymbol(NSLookupAndBindSymbol(funcname));
119mydlclose(
void *handle)
121 NSUnLinkModule(handle, NSUNLINKMODULE_OPTION_NONE);
126Yap_LoadForeignFile(
char *file,
int flags)
128 return (
void *)mydlopen(file);
132Yap_CallForeignFile(
void *handle,
char *f)
134 YapInitProc proc = (YapInitProc) mydlsym(f);
142Yap_CloseForeignFile(
void *handle)
144 return mydlclose(handle);
154 char *proc_name, YapInitProc *init_proc)
156 int lvl = push_text_stack();
161 iconst
char *file = AtomName(ofiles->name);
162 if (!(file=Yap_findFile(file,
true) )) {
163 strcpy(LOCAL_ErrorSay,
"%% Trying to open unexisting file in LoadForeign");
164 popen_text_stack(lvl);
167 if((handle=mydlopen(file)) == 0)
169 fprintf(stderr,
"calling dlopen with error %s\n", mydlerror());
171 popen_text_stack(lvl);
175 ofiles->handle = handle;
177 ofiles = ofiles->next;
182 char *s = AtomName(lib->name);
185 strcpy(LOCAL_FileNameBuf,
"lib");
186 strcat(LOCAL_FileNameBuf,s+2);
187 strcat(LOCAL_FileNameBuf,
".so");
189 strcpy(LOCAL_FileNameBuf,s);
192 if((libs->handle=mydlopen(LOCAL_FileNameBuf)) == NULL)
194 strcpy(LOCAL_ErrorSay,mydlerror());
195 popen_text_stack(lvl);
201 *init_proc = (YapInitProc) mydlsym(proc_name);
204 strcpy(LOCAL_ErrorSay,
"Could not locate initialization routine");
205 popen_text_stack(lvl);
209 popen_text_stack(lvl);
210 return LOAD_SUCCEEDED;
215 char *proc_name, YapInitProc *init_proc)
217 return LoadForeign(ofiles, libs, proc_name, init_proc);
221Yap_ShutdownLoadForeign(
void)
225 f_code = ForeignCodeLoaded;
226 while (f_code != NULL) {
230 while (objs != NULL) {
231 if (mydlclose(objs->handle) != 0)
236 while (libs != NULL) {
237 if (mydlclose(libs->handle) != 0)
241 f_code = f_code->next;
247 char *proc_name, YapInitProc *init_proc)
249 return(LoadForeign(ofiles,libs, proc_name, init_proc));
load_foreign_files/3 has works for the following configurations: