/
dlload.c
146 lines (132 loc) · 3.64 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
#include <stdlib.h>
#include <string.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;
#define EXTENSION ".dll"
#elif defined(LINUX)
#include <unistd.h>
#include <dlfcn.h>
#define GET_FUNCTION_FROM_MODULE dlsym
#define CLOSE_MODULE dlclose
typedef void * module_handle_t;
#define EXTENSION ".so"
#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;
#define EXTENSION ".bundle"
#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;
char path[PATHBUF];
path[0] = '\0';
#ifdef WIN32
LPVOID lpMsgBuf;
DWORD dw;
#endif
modname = fname;
#ifndef WIN32
if (modname == NULL) {
return (void*)dlopen(NULL, RTLD_NOW);
}
char *cwd;
/* first, try load from current directory */
if (modname[0] != '/') {
cwd = getcwd(path, PATHBUF);
if (cwd == NULL)
return NULL;
strncat(path, "/", PATHBUF-1-strlen(path));
strncat(path, modname, PATHBUF-1-strlen(path));
handle = dlopen(path, RTLD_NOW);
if (handle == NULL) {
/* try julia home */
if (julia_home) {
strncpy(path, julia_home, PATHBUF-1);
strncat(path, "/", PATHBUF-1-strlen(path));
strncat(path, modname, PATHBUF-1-strlen(path));
handle = dlopen(path, RTLD_NOW);
}
if (handle == NULL) {
//ios_printf(ios_stderr, "%s\n", dlerror());
/* try loading from standard library path */
handle = dlopen(modname, RTLD_NOW);
if (handle == NULL) {
ios_printf(ios_stderr, "%s\n", dlerror());
jl_errorf("could not load module %s", fname);
}
}
}
}
else {
handle = dlopen(modname, RTLD_NOW);
if (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);
}
handle = LoadLibrary(modname);
if (handle == NULL) {
if (julia_home) {
strncpy(path, julia_home, PATHBUF-1);
strncat(path, "\\", PATHBUF-1-strlen(path));
strncat(path, modname, PATHBUF-1-strlen(path));
handle = LoadLibrary(path);
}
if (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 (void*)handle;
}
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;
}