forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dlload.c
155 lines (142 loc) · 4.1 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#ifdef WIN32
#include <malloc.h>
#include <windows.h>
#undef TRUE
#undef FALSE
#undef VOID
#define GET_FUNCTION_FROM_MODULE GetProcAddress
#define CLOSE_MODULE FreeLibrary
typedef HINSTANCE module_handle_t;
static char *extensions[] = { "", ".dll" };
#define N_EXTENSIONS 2
#elif 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 2
#elif defined(MACOSX) || defined(MACINTEL)
#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 3
#endif
#ifdef BOEHM_GC
#include <gc.h>
#endif
#include "llt.h"
#include "julia.h"
#define PATHBUF 512
#ifdef WIN32
#define STDCALL __stdcall
#else
#define STDCALL
#endif
extern char *julia_home;
void *jl_load_dynamic_library(char *fname)
{
module_handle_t handle;
char *modname, *ext;
char path[PATHBUF];
int i;
#ifdef WIN32
LPVOID lpMsgBuf;
DWORD dw;
#endif
modname = fname;
#ifndef WIN32
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] != '/') {
cwd = getcwd(path, PATHBUF);
if (cwd != NULL) {
/* first, 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;
}
if (julia_home) {
/* now try julia home */
strncpy(path, julia_home, PATHBUF-1);
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);
#else
if (modname == NULL) {
return (void*)GetModuleHandle(NULL);
}
for(i=0; i < N_EXTENSIONS; i++) {
ext = extensions[i];
path[0] = '\0';
handle = NULL;
if (julia_home) {
strncpy(path, julia_home, PATHBUF-1);
strncat(path, "\\", PATHBUF-1-strlen(path));
strncat(path, modname, PATHBUF-1-strlen(path));
strncat(path, ext, PATHBUF-1-strlen(path));
handle = LoadLibrary(path);
if (handle != NULL) return handle;
}
strncpy(path, modname, PATHBUF-1);
strncat(path, ext, PATHBUF-1-strlen(path));
handle = LoadLibrary(path);
if (handle != NULL) return handle;
}
assert(handle == NULL);
dw = GetLastError();
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0, NULL );
jl_errorf("could not load module %s: (%d) %s", fname, dw,
lpMsgBuf);
#endif
return NULL;
}
void *jl_dlsym(void *handle, char *symbol)
{
#ifndef WIN32
(void)dlerror();
void *ptr = dlsym(handle, symbol);
char *msg = dlerror();
if (msg != NULL) {
jl_errorf("dlsym: %s", msg);
}
return ptr;
#else
void *ptr = GET_FUNCTION_FROM_MODULE(handle, symbol);
#endif
return ptr;
}