forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdlload.c
96 lines (86 loc) · 2.72 KB
/
dlload.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <sys/stat.h>
#if defined(__linux)
#include <unistd.h>
#include <dlfcn.h>
#define GET_FUNCTION_FROM_MODULE dlsym
#define CLOSE_MODULE dlclose
typedef void * module_handle_t;
static char *extensions[] = { ".so" };
#define N_EXTENSIONS 1
#elif defined(__APPLE__)
#include <unistd.h>
#include <dlfcn.h>
#define GET_FUNCTION_FROM_MODULE dlsym
#define CLOSE_MODULE dlclose
typedef void * module_handle_t;
static char *extensions[] = { ".dylib", ".bundle" };
#define N_EXTENSIONS 2
#endif
#include "julia.h"
#define PATHBUF 512
extern char *julia_home;
void *jl_load_dynamic_library(char *fname)
{
module_handle_t handle;
char *modname, *ext;
char path[PATHBUF];
int i;
modname = fname;
if (modname == NULL) {
return (void*)dlopen(NULL, RTLD_NOW);
}
char *cwd;
for(i=0; i < N_EXTENSIONS; i++) {
ext = extensions[i];
path[0] = '\0';
handle = NULL;
if (modname[0] != '/') {
if (julia_home) {
/* try julia_home/lib */
strncpy(path, julia_home, PATHBUF-1);
strncat(path, "/lib/", PATHBUF-1-strlen(path));
strncat(path, modname, PATHBUF-1-strlen(path));
strncat(path, ext, PATHBUF-1-strlen(path));
handle = dlopen(path, RTLD_NOW);
if (handle != NULL) return handle;
// if file exists but didn't load, show error details
struct stat sbuf;
if (stat(path, &sbuf) != -1) {
ios_printf(ios_stderr, "%s\n", dlerror());
jl_errorf("could not load module %s", fname);
}
}
cwd = getcwd(path, PATHBUF);
if (cwd != NULL) {
/* next try load from current directory */
strncat(path, "/", PATHBUF-1-strlen(path));
strncat(path, modname, PATHBUF-1-strlen(path));
strncat(path, ext, PATHBUF-1-strlen(path));
handle = dlopen(path, RTLD_NOW);
if (handle != NULL) return handle;
}
}
/* try loading from standard library path */
strncpy(path, modname, PATHBUF-1);
strncat(path, ext, PATHBUF-1-strlen(path));
handle = dlopen(path, RTLD_NOW);
if (handle != NULL) return handle;
}
assert(handle == NULL);
ios_printf(ios_stderr, "%s\n", dlerror());
jl_errorf("could not load module %s", fname);
return NULL;
}
void *jl_dlsym(void *handle, char *symbol)
{
(void)dlerror();
void *ptr = dlsym(handle, symbol);
char *msg = dlerror();
if (msg != NULL) {
jl_errorf("dlsym: %s", msg);
}
return ptr;
}