Skip to content

Commit a02c731

Browse files
authored
Merge pull request #21465 from JuliaLang/yyc/codegen/libname
Give symbolic name to special libname on windows.
2 parents e79423a + d1686b9 commit a02c731

File tree

4 files changed

+37
-48
lines changed

4 files changed

+37
-48
lines changed

src/ccall.cpp

Lines changed: 30 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -815,7 +815,7 @@ static jl_cgval_t emit_cglobal(jl_value_t **args, size_t nargs, jl_codectx_t *ct
815815
msg << sym.f_name;
816816
if (sym.f_lib != NULL) {
817817
#ifdef _OS_WINDOWS_
818-
assert((intptr_t)sym.f_lib != 1 && (intptr_t)sym.f_lib != 2);
818+
assert(sym.f_lib != JL_EXE_LIBNAME && sym.f_lib != JL_DL_LIBNAME);
819819
#endif
820820
msg << " in library ";
821821
msg << sym.f_lib;
@@ -1584,10 +1584,24 @@ static jl_cgval_t emit_ccall(jl_value_t **args, size_t nargs, jl_codectx_t *ctx)
15841584
if (rt != args[2] && rt != (jl_value_t*)jl_any_type)
15851585
jl_add_method_root(ctx, rt);
15861586

1587+
auto _is_libjulia_func = [&] (uintptr_t ptr, const char *name) {
1588+
if ((uintptr_t)fptr == ptr)
1589+
return true;
1590+
return (!f_lib || f_lib == JL_DL_LIBNAME) && f_name && !strcmp(f_name, name);
1591+
};
1592+
#define is_libjulia_func(name) _is_libjulia_func((uintptr_t)&(name), #name)
1593+
1594+
#ifdef _OS_LINUX_
1595+
// directly accessing the address of an ifunc can cause linker issue on
1596+
// some configurations (e.g. AArch64 + -Bsymbolic-functions).
1597+
static const auto ptls_getter = jl_dlsym_e(jl_dlopen(nullptr, 0),
1598+
"jl_get_ptls_states");
1599+
#else
1600+
static const auto ptls_getter = &jl_get_ptls_states;
1601+
#endif
1602+
15871603
// some special functions
1588-
if (fptr == (void(*)(void))&jl_array_ptr ||
1589-
((f_lib==NULL || (intptr_t)f_lib==2)
1590-
&& f_name && !strcmp(f_name,"jl_array_ptr"))) {
1604+
if (is_libjulia_func(jl_array_ptr)) {
15911605
assert(lrt->isPointerTy());
15921606
assert(!isVa && !llvmcall);
15931607
assert(nargt==1);
@@ -1598,9 +1612,7 @@ static jl_cgval_t emit_ccall(jl_value_t **args, size_t nargs, jl_codectx_t *ctx)
15981612
return mark_or_box_ccall_result(emit_bitcast(emit_arrayptr(ary, ctx), lrt),
15991613
retboxed, rt, unionall, static_rt, ctx);
16001614
}
1601-
if (fptr == (void(*)(void))&jl_value_ptr ||
1602-
((f_lib==NULL || (intptr_t)f_lib==2)
1603-
&& f_name && !strcmp(f_name,"jl_value_ptr"))) {
1615+
else if (is_libjulia_func(jl_value_ptr)) {
16041616
assert(lrt->isPointerTy());
16051617
assert(!isVa && !llvmcall);
16061618
assert(nargt==1);
@@ -1635,18 +1647,14 @@ static jl_cgval_t emit_ccall(jl_value_t **args, size_t nargs, jl_codectx_t *ctx)
16351647
return mark_or_box_ccall_result(emit_bitcast(ary, lrt),
16361648
retboxed, rt, unionall, static_rt, ctx);
16371649
}
1638-
if (JL_CPU_WAKE_NOOP &&
1639-
(fptr == &jl_cpu_wake || ((!f_lib || (intptr_t)f_lib == 2) &&
1640-
f_name && !strcmp(f_name, "jl_cpu_wake")))) {
1650+
else if (JL_CPU_WAKE_NOOP && is_libjulia_func(jl_cpu_wake)) {
16411651
assert(lrt == T_void);
16421652
assert(!isVa && !llvmcall);
16431653
assert(nargt == 0);
16441654
JL_GC_POP();
16451655
return ghostValue(jl_void_type);
16461656
}
1647-
if (fptr == &jl_gc_safepoint ||
1648-
((!f_lib || (intptr_t)f_lib == 2) && f_name &&
1649-
strcmp(f_name, "jl_gc_safepoint") == 0)) {
1657+
else if (is_libjulia_func(jl_gc_safepoint)) {
16501658
assert(lrt == T_void);
16511659
assert(!isVa && !llvmcall);
16521660
assert(nargt == 0);
@@ -1657,17 +1665,7 @@ static jl_cgval_t emit_ccall(jl_value_t **args, size_t nargs, jl_codectx_t *ctx)
16571665
emit_signal_fence();
16581666
return ghostValue(jl_void_type);
16591667
}
1660-
#ifdef _OS_LINUX_
1661-
// directly access the address of a ifunc can cause linker issue on
1662-
// some configurations (e.g. AArch64 + -Bsymbolic-functions).
1663-
static const auto ptls_getter = jl_dlsym_e(jl_dlopen(nullptr, 0),
1664-
"jl_get_ptls_states");
1665-
#else
1666-
static const auto ptls_getter = &jl_get_ptls_states;
1667-
#endif
1668-
if (fptr == (void(*)(void))(uintptr_t)ptls_getter ||
1669-
((!f_lib || (intptr_t)f_lib == 2) && f_name &&
1670-
strcmp(f_name, "jl_get_ptls_states") == 0)) {
1668+
else if (_is_libjulia_func((uintptr_t)ptls_getter, "jl_get_ptls_states")) {
16711669
assert(lrt == T_pint8);
16721670
assert(!isVa && !llvmcall);
16731671
assert(nargt == 0);
@@ -1676,9 +1674,7 @@ static jl_cgval_t emit_ccall(jl_value_t **args, size_t nargs, jl_codectx_t *ctx)
16761674
emit_bitcast(ctx->ptlsStates, lrt),
16771675
retboxed, rt, unionall, static_rt, ctx);
16781676
}
1679-
if (fptr == (void(*)(void))&jl_threadid ||
1680-
((!f_lib || (intptr_t)f_lib == 2) && f_name &&
1681-
strcmp(f_name, "jl_threadid") == 0)) {
1677+
else if (is_libjulia_func(jl_threadid)) {
16821678
assert(lrt == T_int16);
16831679
assert(!isVa && !llvmcall);
16841680
assert(nargt == 0);
@@ -1690,9 +1686,7 @@ static jl_cgval_t emit_ccall(jl_value_t **args, size_t nargs, jl_codectx_t *ctx)
16901686
tbaa_decorate(tbaa_const, builder.CreateLoad(ptid)),
16911687
retboxed, rt, unionall, static_rt, ctx);
16921688
}
1693-
if (fptr == &jl_sigatomic_begin ||
1694-
((!f_lib || (intptr_t)f_lib == 2) && f_name &&
1695-
strcmp(f_name, "jl_sigatomic_begin") == 0)) {
1689+
else if (is_libjulia_func(jl_sigatomic_begin)) {
16961690
assert(lrt == T_void);
16971691
assert(!isVa && !llvmcall);
16981692
assert(nargt == 0);
@@ -1706,9 +1700,7 @@ static jl_cgval_t emit_ccall(jl_value_t **args, size_t nargs, jl_codectx_t *ctx)
17061700
emit_signal_fence();
17071701
return ghostValue(jl_void_type);
17081702
}
1709-
if (fptr == &jl_sigatomic_end ||
1710-
((!f_lib || (intptr_t)f_lib == 2) && f_name &&
1711-
strcmp(f_name, "jl_sigatomic_end") == 0)) {
1703+
else if (is_libjulia_func(jl_sigatomic_end)) {
17121704
assert(lrt == T_void);
17131705
assert(!isVa && !llvmcall);
17141706
assert(nargt == 0);
@@ -1737,9 +1729,7 @@ static jl_cgval_t emit_ccall(jl_value_t **args, size_t nargs, jl_codectx_t *ctx)
17371729
builder.SetInsertPoint(contBB);
17381730
return ghostValue(jl_void_type);
17391731
}
1740-
if (fptr == (void(*)(void))&jl_is_leaf_type ||
1741-
((f_lib==NULL || (intptr_t)f_lib==2)
1742-
&& f_name && !strcmp(f_name, "jl_is_leaf_type"))) {
1732+
else if (is_libjulia_func(jl_is_leaf_type)) {
17431733
assert(nargt == 1);
17441734
assert(!isVa && !llvmcall);
17451735
jl_value_t *arg = args[4];
@@ -1751,9 +1741,7 @@ static jl_cgval_t emit_ccall(jl_value_t **args, size_t nargs, jl_codectx_t *ctx)
17511741
false, rt, unionall, static_rt, ctx);
17521742
}
17531743
}
1754-
if (fptr == (void(*)(void))&jl_function_ptr ||
1755-
((f_lib==NULL || (intptr_t)f_lib==2)
1756-
&& f_name && !strcmp(f_name, "jl_function_ptr"))) {
1744+
else if (is_libjulia_func(jl_function_ptr)) {
17571745
assert(nargt == 3);
17581746
assert(!isVa && !llvmcall);
17591747
jl_value_t *f = static_eval(args[4], ctx, false, false);
@@ -1797,10 +1785,8 @@ static jl_cgval_t emit_ccall(jl_value_t **args, size_t nargs, jl_codectx_t *ctx)
17971785
}
17981786
JL_GC_POP();
17991787
}
1800-
if ((fptr == (void(*)(void))&jl_array_isassigned ||
1801-
((f_lib==NULL || (intptr_t)f_lib==2)
1802-
&& f_name && !strcmp(f_name, "jl_array_isassigned"))) &&
1803-
expr_type(args[6], ctx) == (jl_value_t*)jl_ulong_type) {
1788+
else if (is_libjulia_func(jl_array_isassigned) &&
1789+
expr_type(args[6], ctx) == (jl_value_t*)jl_ulong_type) {
18041790
assert(nargt == 2);
18051791
jl_value_t *aryex = args[4];
18061792
jl_value_t *idxex = args[6];
@@ -2053,7 +2039,7 @@ jl_cgval_t function_sig_t::emit_a_ccall(
20532039
msg << symarg.f_name;
20542040
if (symarg.f_lib != NULL) {
20552041
#ifdef _OS_WINDOWS_
2056-
assert((intptr_t)symarg.f_lib != 1 && (intptr_t)symarg.f_lib != 2);
2042+
assert(symarg.f_lib != JL_EXE_LIBNAME && symarg.f_lib != JL_DL_LIBNAME);
20572043
#endif
20582044
msg << " in library ";
20592045
msg << symarg.f_lib;

src/dlload.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,9 @@ JL_DLLEXPORT void *jl_dlsym(void *handle, const char *symbol)
243243
const char *jl_dlfind_win32(const char *f_name)
244244
{
245245
if (jl_dlsym_e(jl_exe_handle, f_name))
246-
return (const char*)1;
246+
return JL_EXE_LIBNAME;
247247
if (jl_dlsym_e(jl_dl_handle, f_name))
248-
return (const char*)2;
248+
return JL_DL_LIBNAME;
249249
if (jl_dlsym_e(jl_kernel32_handle, f_name))
250250
return "kernel32";
251251
if (jl_dlsym_e(jl_ntdll_handle, f_name))

src/julia_internal.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,9 @@ extern void *jl_winsock_handle;
684684
void *jl_get_library(const char *f_lib);
685685
JL_DLLEXPORT void *jl_load_and_lookup(const char *f_lib, const char *f_name,
686686
void **hnd);
687+
// Windows only
688+
#define JL_EXE_LIBNAME ((const char*)1)
689+
#define JL_DL_LIBNAME ((const char*)2)
687690
const char *jl_dlfind_win32(const char *name);
688691
void *jl_dlopen_soname(const char *pfx, size_t n, unsigned flags);
689692

src/runtime_ccall.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,9 @@ void *jl_get_library(const char *f_lib)
140140
{
141141
void *hnd;
142142
#ifdef _OS_WINDOWS_
143-
if ((intptr_t)f_lib == 1)
143+
if (f_lib == JL_EXE_LIBNAME)
144144
return jl_exe_handle;
145-
if ((intptr_t)f_lib == 2)
145+
if (f_lib == JL_DL_LIBNAME)
146146
return jl_dl_handle;
147147
#endif
148148
if (f_lib == NULL)

0 commit comments

Comments
 (0)