forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runtime_ccall.cpp
142 lines (128 loc) · 3.67 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
// This file is a part of Julia. License is MIT: http:https://julialang.org/license
#include <map>
#include <string>
#include <cstdio>
#include <llvm/Support/Host.h>
#include "julia.h"
#include "julia_internal.h"
using namespace llvm;
// --- library symbol lookup ---
// map from "libX" to full soname "libX.so.ver"
#if defined(__linux__) || defined(__FreeBSD__)
static std::map<std::string, std::string> sonameMap;
static bool got_sonames = false;
static 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
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);
}
extern "C" DLLEXPORT const char *jl_lookup_soname(const char *pfx, size_t n)
{
if (!got_sonames) {
jl_read_sonames();
got_sonames = true;
}
std::string str(pfx, n);
if (sonameMap.find(str) != sonameMap.end()) {
return sonameMap[str].c_str();
}
return NULL;
}
#endif
// map from user-specified lib names to handles
static std::map<std::string, uv_lib_t*> libMap;
extern "C"
uv_lib_t *jl_get_library(char *f_lib)
{
uv_lib_t *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 = (uv_lib_t *) jl_load_dynamic_library(f_lib, JL_RTLD_DEFAULT);
if (hnd != NULL)
libMap[f_lib] = hnd;
return hnd;
}
extern "C" DLLEXPORT
void *jl_load_and_lookup(char *f_lib, char *f_name, uv_lib_t **hnd)
{
uv_lib_t *handle = *hnd;
if (!handle)
*hnd = handle = jl_get_library(f_lib);
void *ptr = jl_dlsym_e(handle, f_name);
if (!ptr)
jl_errorf("symbol \"%s\" could not be found: %s", f_name, uv_dlerror(handle));
return ptr;
}
// miscellany
extern "C" DLLEXPORT
jl_value_t *jl_get_cpu_name(void)
{
#if LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR < 5
std::string HostCPUName = llvm::sys::getHostCPUName();
#else
StringRef HostCPUName = llvm::sys::getHostCPUName();
#endif
return jl_pchar_to_string(HostCPUName.data(), HostCPUName.size());
}