Skip to content

Commit 804c2ce

Browse files
topolarityKristofferC
authored andcommitted
Remove usages of weak symbols (#57523)
Inspired by a bug I hit on MinGW recently with weak symbol resolution. I'm not sure why we started using these in 70000ac, since I believe we should be able to lookup these symbols just fine in our `jl_exe_handle` as long as it was linked / compiled with `-rdynamic` Migrating away from weak symbols seems a good idea, given their uneven implementation across platforms. (cherry picked from commit 4a6ada6)
1 parent fdb6a0d commit 804c2ce

File tree

7 files changed

+49
-48
lines changed

7 files changed

+49
-48
lines changed

src/Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ SRCS := \
5656
jltypes gf typemap smallintset ast builtins module interpreter symbol \
5757
dlload sys init task array genericmemory staticdata toplevel jl_uv datatype \
5858
simplevector runtime_intrinsics precompile jloptions mtarraylist \
59-
threading scheduler stackwalk \
59+
threading scheduler stackwalk null_sysimage \
6060
method jlapi signal-handling safepoint timing subtype rtutils \
6161
crc32c APInt-C processor ircode opaque_closure codegen-stubs coverage runtime_ccall engine \
6262
$(GC_SRCS)
@@ -75,7 +75,7 @@ GC_CODEGEN_SRCS += llvm-late-gc-lowering-stock
7575
endif
7676
CODEGEN_SRCS := codegen jitlayers aotcompile debuginfo disasm llvm-simdloop \
7777
llvm-pass-helpers llvm-ptls \
78-
llvm-lower-handlers llvm-propagate-addrspaces \
78+
llvm-lower-handlers llvm-propagate-addrspaces null_sysimage \
7979
llvm-multiversioning llvm-alloc-opt llvm-alloc-helpers cgmemmgr llvm-remove-addrspaces \
8080
llvm-remove-ni llvm-julia-licm llvm-demote-float16 llvm-cpufeatures pipeline llvm_api \
8181
$(GC_CODEGEN_SRCS)
@@ -184,7 +184,7 @@ endif
184184
CLANG_LDFLAGS := $(LLVM_LDFLAGS)
185185
ifeq ($(OS), Darwin)
186186
CLANG_LDFLAGS += -Wl,-undefined,dynamic_lookup
187-
OSLIBS += -Wl,-U,__dyld_atfork_parent -Wl,-U,__dyld_atfork_prepare -Wl,-U,__dyld_dlopen_atfork_parent -Wl,-U,__dyld_dlopen_atfork_prepare -Wl,-U,_jl_image_pointers -Wl,-U,_jl_system_image_data -Wl,-U,_jl_system_image_size
187+
OSLIBS += -Wl,-U,__dyld_atfork_parent -Wl,-U,__dyld_atfork_prepare -Wl,-U,__dyld_dlopen_atfork_parent -Wl,-U,__dyld_dlopen_atfork_prepare
188188
LIBJULIA_PATH_REL := @rpath/libjulia
189189
else
190190
LIBJULIA_PATH_REL := libjulia

src/julia_internal.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2008,17 +2008,6 @@ jl_sym_t *_jl_symbol(const char *str, size_t len) JL_NOTSAFEPOINT;
20082008
#define JL_GC_ASSERT_LIVE(x) (void)(x)
20092009
#endif
20102010

2011-
#ifdef _OS_WINDOWS_
2012-
// On Windows, weak symbols do not default to 0 due to a GCC bug
2013-
// (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90826), use symbol
2014-
// aliases with a known value instead.
2015-
#define JL_WEAK_SYMBOL_OR_ALIAS_DEFAULT(sym) __attribute__((weak,alias(#sym)))
2016-
#define JL_WEAK_SYMBOL_DEFAULT(sym) &sym
2017-
#else
2018-
#define JL_WEAK_SYMBOL_OR_ALIAS_DEFAULT(sym) __attribute__((weak))
2019-
#define JL_WEAK_SYMBOL_DEFAULT(sym) NULL
2020-
#endif
2021-
20222011
JL_DLLEXPORT uint32_t jl_crc32c(uint32_t crc, const char *buf, size_t len);
20232012

20242013
// -- exports from codegen -- //

src/null_sysimage.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// This file is a part of Julia. License is MIT: https://julialang.org/license
2+
3+
#include <stddef.h>
4+
#include "processor.h"
5+
6+
/**
7+
* These symbols support statically linking the sysimage with libjulia-internal.
8+
*
9+
* Here we provide dummy definitions that are used when these are not linked
10+
* together (the default build configuration). The 0 value of jl_system_image_size
11+
* is used as a sentinel to indicate that the sysimage should be loaded externally.
12+
**/
13+
char jl_system_image_data = 0;
14+
size_t jl_system_image_size = 0;
15+
jl_image_pointers_t jl_image_pointers = { 0 };

src/processor.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -619,11 +619,6 @@ static inline llvm::SmallVector<TargetData<n>, 0> &get_cmdline_targets(F &&featu
619619
return targets;
620620
}
621621

622-
extern "C" {
623-
void *image_pointers_unavailable;
624-
extern void * JL_WEAK_SYMBOL_OR_ALIAS_DEFAULT(image_pointers_unavailable) jl_image_pointers;
625-
}
626-
627622
// Load sysimg, use the `callback` for dispatch and perform all relocations
628623
// for the selected target.
629624
template<typename F>
@@ -633,10 +628,10 @@ static inline jl_image_t parse_sysimg(void *hdl, F &&callback)
633628
jl_image_t res{};
634629

635630
const jl_image_pointers_t *pointers;
636-
if (hdl == jl_exe_handle && &jl_image_pointers != JL_WEAK_SYMBOL_DEFAULT(image_pointers_unavailable))
637-
pointers = (const jl_image_pointers_t *)&jl_image_pointers;
638-
else
631+
if (jl_system_image_size == 0)
639632
jl_dlsym(hdl, "jl_image_pointers", (void**)&pointers, 1);
633+
else
634+
pointers = &jl_image_pointers; // libjulia-internal and sysimage statically linked
640635

641636
const void *ids = pointers->target_data;
642637
jl_value_t* rejection_reason = nullptr;

src/processor.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,18 @@ JL_DLLEXPORT int32_t jl_set_zero_subnormals(int8_t isZero);
224224
JL_DLLEXPORT int32_t jl_get_zero_subnormals(void);
225225
JL_DLLEXPORT int32_t jl_set_default_nans(int8_t isDefault);
226226
JL_DLLEXPORT int32_t jl_get_default_nans(void);
227+
228+
/**
229+
* System image contents.
230+
*
231+
* These symbols are typically dummy values, unless statically linking
232+
* libjulia-* and the sysimage together (see null_sysimage.c), in which
233+
* case they allow accessing the local copy of the sysimage.
234+
**/
235+
extern char jl_system_image_data;
236+
extern size_t jl_system_image_size;
237+
extern jl_image_pointers_t jl_image_pointers;
238+
227239
#ifdef __cplusplus
228240
}
229241

src/staticdata.c

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -659,24 +659,25 @@ JL_DLLEXPORT int jl_running_on_valgrind(void)
659659
return RUNNING_ON_VALGRIND;
660660
}
661661

662-
void *system_image_data_unavailable;
663-
extern void * JL_WEAK_SYMBOL_OR_ALIAS_DEFAULT(system_image_data_unavailable) jl_system_image_data;
664-
extern void * JL_WEAK_SYMBOL_OR_ALIAS_DEFAULT(system_image_data_unavailable) jl_system_image_size;
665662
static void jl_load_sysimg_so(void)
666663
{
667-
const char *sysimg_data;
668664
assert(sysimage.fptrs.ptrs); // jl_init_processor_sysimg should already be run
669-
if (jl_sysimg_handle == jl_exe_handle &&
670-
&jl_system_image_data != JL_WEAK_SYMBOL_DEFAULT(system_image_data_unavailable))
671-
sysimg_data = (const char*)&jl_system_image_data;
672-
else
673-
jl_dlsym(jl_sysimg_handle, "jl_system_image_data", (void **)&sysimg_data, 1);
665+
674666
size_t *plen;
675-
if (jl_sysimg_handle == jl_exe_handle &&
676-
&jl_system_image_size != JL_WEAK_SYMBOL_DEFAULT(system_image_data_unavailable))
677-
plen = (size_t *)&jl_system_image_size;
678-
else
667+
const char *sysimg_data;
668+
669+
if (jl_system_image_size == 0) {
670+
// in the usual case, the sysimage was not statically linked to libjulia-internal
671+
// look up the external sysimage symbols via the dynamic linker
679672
jl_dlsym(jl_sysimg_handle, "jl_system_image_size", (void **)&plen, 1);
673+
jl_dlsym(jl_sysimg_handle, "jl_system_image_data", (void **)&sysimg_data, 1);
674+
} else {
675+
// the sysimage was statically linked directly against libjulia-internal
676+
// use the internal symbols
677+
plen = &jl_system_image_size;
678+
sysimg_data = &jl_system_image_data;
679+
}
680+
680681
jl_gc_notify_image_load(sysimg_data, *plen);
681682
jl_restore_system_image_data(sysimg_data, *plen);
682683
}

src/threading.c

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -228,10 +228,6 @@ void jl_set_pgcstack(jl_gcframe_t **pgcstack) JL_NOTSAFEPOINT
228228
{
229229
*jl_pgcstack_key() = pgcstack;
230230
}
231-
# if JL_USE_IFUNC
232-
JL_DLLEXPORT __attribute__((weak))
233-
void jl_register_pgcstack_getter(void);
234-
# endif
235231
static jl_gcframe_t **jl_get_pgcstack_init(void);
236232
static jl_get_pgcstack_func *jl_get_pgcstack_cb = jl_get_pgcstack_init;
237233
static jl_gcframe_t **jl_get_pgcstack_init(void)
@@ -244,15 +240,8 @@ static jl_gcframe_t **jl_get_pgcstack_init(void)
244240
// This is clearly not thread-safe but should be fine since we
245241
// make sure the tls states callback is finalized before adding
246242
// multiple threads
247-
# if JL_USE_IFUNC
248-
if (jl_register_pgcstack_getter)
249-
jl_register_pgcstack_getter();
250-
else
251-
# endif
252-
{
253-
jl_get_pgcstack_cb = jl_get_pgcstack_fallback;
254-
jl_pgcstack_key = &jl_pgcstack_addr_fallback;
255-
}
243+
jl_get_pgcstack_cb = jl_get_pgcstack_fallback;
244+
jl_pgcstack_key = &jl_pgcstack_addr_fallback;
256245
return jl_get_pgcstack_cb();
257246
}
258247

0 commit comments

Comments
 (0)