-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
runtime_ccall.cpp
190 lines (173 loc) · 5.14 KB
/
runtime_ccall.cpp
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// This file is a part of Julia. License is MIT: https://julialang.org/license
#include <map>
#include <string>
#include <cstdio>
#include <llvm/Support/Host.h>
#include "julia.h"
#include "julia_internal.h"
#include "llvm-version.h"
using namespace llvm;
// --- library symbol lookup ---
// map from "libX" to full soname "libX.so.ver"
#if defined(__linux__) || defined(__FreeBSD__)
static uv_rwlock_t soname_lock;
static std::map<std::string, std::string> sonameMap;
static bool got_sonames = false;
extern "C" void jl_init_runtime_ccall(void)
{
uv_rwlock_init(&soname_lock);
}
// This reloads the sonames, necessary after system upgrade.
// Keep this DLLEXPORTed, this is used by `BinDeps.jl` to make sure
// newly installed libraries can be found.
extern "C" JL_DLLEXPORT void jl_read_sonames(void)
{
char *line=NULL;
size_t sz=0;
#if defined(__linux__)
FILE *ldc = popen("/sbin/ldconfig -p", "r");
#else
FILE *ldc = popen("/sbin/ldconfig -r", "r");
#endif
if (ldc == NULL) return; // ignore errors in running ldconfig (other than whatever might have been printed to stderr)
// This loop is not allowed to call julia GC while holding the lock
uv_rwlock_wrlock(&soname_lock);
sonameMap.clear();
while (!feof(ldc)) {
ssize_t n = getline(&line, &sz, ldc);
if (n == -1)
break;
if (n > 2 && isspace((unsigned char)line[0])) {
#ifdef __linux__
int i = 0;
while (isspace((unsigned char)line[++i])) ;
char *name = &line[i];
char *dot = strstr(name, ".so");
i = 0;
#else
char *name = strstr(line, ":-l");
if (name == NULL) continue;
strncpy(name, "lib", 3);
char *dot = strchr(name, '.');
#endif
if (NULL == dot)
continue;
#ifdef __linux__
// Detect if this entry is for the current architecture
while (!isspace((unsigned char)dot[++i])) ;
while (isspace((unsigned char)dot[++i])) ;
int j = i;
while (!isspace((unsigned char)dot[++j])) ;
char *arch = strstr(dot+i,"x86-64");
if (arch != NULL && arch < dot + j) {
#ifdef _P32
continue;
#endif
}
else {
#ifdef _P64
continue;
#endif
}
#endif // __linux__
char *abslibpath = strrchr(line, ' ');
if (dot != NULL && abslibpath != NULL) {
std::string pfx(name, dot - name);
// Do not include ' ' in front and '\n' at the end
std::string soname(abslibpath+1, line+n-(abslibpath+1)-1);
sonameMap[pfx] = soname;
}
}
}
free(line);
pclose(ldc);
uv_rwlock_wrunlock(&soname_lock);
}
// This API is not thread safe. The return value can be free'd if
// `jl_read_sonames()` is called on another thread.
extern "C" JL_DLLEXPORT const char *jl_lookup_soname(const char *pfx, size_t n)
{
if (!got_sonames) {
jl_read_sonames();
got_sonames = true;
}
const char *res = nullptr;
uv_rwlock_rdlock(&soname_lock);
auto search = sonameMap.find(std::string(pfx, n));
if (search != sonameMap.end())
res = search->second.c_str();
uv_rwlock_rdunlock(&soname_lock);
return res;
}
extern "C" void *jl_dlopen_soname(const char *pfx, size_t n, unsigned flags)
{
if (!got_sonames) {
jl_read_sonames();
got_sonames = true;
}
void *res = nullptr;
uv_rwlock_rdlock(&soname_lock);
auto search = sonameMap.find(std::string(pfx, n));
if (search != sonameMap.end())
res = jl_dlopen(search->second.c_str(), flags);
uv_rwlock_rdunlock(&soname_lock);
return res;
}
#else
extern "C" void jl_init_runtime_ccall(void)
{
}
#endif
// map from user-specified lib names to handles
static std::map<std::string, void*> libMap;
extern "C"
void *jl_get_library(const char *f_lib)
{
void *hnd;
#ifdef _OS_WINDOWS_
if ((intptr_t)f_lib == 1)
return jl_exe_handle;
if ((intptr_t)f_lib == 2)
return jl_dl_handle;
#endif
if (f_lib == NULL)
return jl_RTLD_DEFAULT_handle;
hnd = libMap[f_lib];
if (hnd != NULL)
return hnd;
hnd = jl_load_dynamic_library(f_lib, JL_RTLD_DEFAULT);
if (hnd != NULL)
libMap[f_lib] = hnd;
return hnd;
}
extern "C" JL_DLLEXPORT
void *jl_load_and_lookup(const char *f_lib, const char *f_name, void **hnd)
{
void *handle = *hnd;
if (!handle)
*hnd = handle = jl_get_library(f_lib);
return jl_dlsym(handle, f_name);
}
// miscellany
extern "C" JL_DLLEXPORT
jl_value_t *jl_get_cpu_name(void)
{
#ifdef LLVM35
StringRef HostCPUName = llvm::sys::getHostCPUName();
#else
const std::string& HostCPUName = llvm::sys::getHostCPUName();
#endif
return jl_pchar_to_string(HostCPUName.data(), HostCPUName.size());
}
extern "C" JL_DLLEXPORT
jl_value_t *jl_get_JIT(void)
{
#if defined(USE_ORCJIT)
const std::string& HostJITName = "ORCJIT";
#elif defined(USE_MCJIT)
const std::string& HostJITName = "MCJIT";
#else
const std::string& HostJITName = "Unknown";
#endif
return jl_pchar_to_string(HostJITName.data(), HostJITName.size());
}