Skip to content

Commit

Permalink
Remove usages of weak symbols (#57523)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
topolarity authored Feb 27, 2025
1 parent 406e704 commit 4a6ada6
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 49 deletions.
7 changes: 3 additions & 4 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ SRCS := \
jltypes gf typemap smallintset ast builtins module interpreter symbol \
dlload sys init task array genericmemory staticdata toplevel jl_uv datatype \
simplevector runtime_intrinsics precompile jloptions mtarraylist \
threading scheduler stackwalk \
threading scheduler stackwalk null_sysimage \
method jlapi signal-handling safepoint timing subtype rtutils \
crc32c APInt-C processor ircode opaque_closure codegen-stubs coverage runtime_ccall engine \
$(GC_SRCS)
Expand All @@ -77,8 +77,7 @@ else
GC_CODEGEN_SRCS += llvm-late-gc-lowering-stock
endif
CODEGEN_SRCS := codegen jitlayers aotcompile debuginfo disasm llvm-simdloop \
llvm-pass-helpers llvm-ptls \
llvm-propagate-addrspaces \
llvm-pass-helpers llvm-ptls llvm-propagate-addrspaces null_sysimage \
llvm-multiversioning llvm-alloc-opt llvm-alloc-helpers cgmemmgr llvm-remove-addrspaces \
llvm-remove-ni llvm-julia-licm llvm-demote-float16 llvm-cpufeatures pipeline llvm_api \
$(GC_CODEGEN_SRCS)
Expand Down Expand Up @@ -187,7 +186,7 @@ endif
CLANG_LDFLAGS := $(LLVM_LDFLAGS)
ifeq ($(OS), Darwin)
CLANG_LDFLAGS += -Wl,-undefined,dynamic_lookup
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
OSLIBS += -Wl,-U,__dyld_atfork_parent -Wl,-U,__dyld_atfork_prepare -Wl,-U,__dyld_dlopen_atfork_parent -Wl,-U,__dyld_dlopen_atfork_prepare
LIBJULIA_PATH_REL := @rpath/libjulia
else
LIBJULIA_PATH_REL := libjulia
Expand Down
11 changes: 0 additions & 11 deletions src/julia_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -1912,17 +1912,6 @@ jl_sym_t *_jl_symbol(const char *str, size_t len) JL_NOTSAFEPOINT;
#define JL_GC_ASSERT_LIVE(x) (void)(x)
#endif

#ifdef _OS_WINDOWS_
// On Windows, weak symbols do not default to 0 due to a GCC bug
// (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90826), use symbol
// aliases with a known value instead.
#define JL_WEAK_SYMBOL_OR_ALIAS_DEFAULT(sym) __attribute__((weak,alias(#sym)))
#define JL_WEAK_SYMBOL_DEFAULT(sym) &sym
#else
#define JL_WEAK_SYMBOL_OR_ALIAS_DEFAULT(sym) __attribute__((weak))
#define JL_WEAK_SYMBOL_DEFAULT(sym) NULL
#endif

//JL_DLLEXPORT float julia__gnu_h2f_ieee(half param) JL_NOTSAFEPOINT;
//JL_DLLEXPORT half julia__gnu_f2h_ieee(float param) JL_NOTSAFEPOINT;
//JL_DLLEXPORT half julia__truncdfhf2(double param) JL_NOTSAFEPOINT;
Expand Down
15 changes: 15 additions & 0 deletions src/null_sysimage.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// This file is a part of Julia. License is MIT: https://julialang.org/license

#include <stddef.h>
#include "processor.h"

/**
* These symbols support statically linking the sysimage with libjulia-internal.
*
* Here we provide dummy definitions that are used when these are not linked
* together (the default build configuration). The 0 value of jl_system_image_size
* is used as a sentinel to indicate that the sysimage should be loaded externally.
**/
char jl_system_image_data = 0;
size_t jl_system_image_size = 0;
jl_image_pointers_t jl_image_pointers = { 0 };
11 changes: 3 additions & 8 deletions src/processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -619,11 +619,6 @@ static inline llvm::SmallVector<TargetData<n>, 0> &get_cmdline_targets(F &&featu
return targets;
}

extern "C" {
void *image_pointers_unavailable;
extern void * JL_WEAK_SYMBOL_OR_ALIAS_DEFAULT(image_pointers_unavailable) jl_image_pointers;
}

// Load sysimg, use the `callback` for dispatch and perform all relocations
// for the selected target.
template<typename F>
Expand All @@ -633,10 +628,10 @@ static inline jl_image_t parse_sysimg(void *hdl, F &&callback)
jl_image_t res{};

const jl_image_pointers_t *pointers;
if (hdl == jl_exe_handle && &jl_image_pointers != JL_WEAK_SYMBOL_DEFAULT(image_pointers_unavailable))
pointers = (const jl_image_pointers_t *)&jl_image_pointers;
else
if (jl_system_image_size == 0)
jl_dlsym(hdl, "jl_image_pointers", (void**)&pointers, 1);
else
pointers = &jl_image_pointers; // libjulia-internal and sysimage statically linked

const void *ids = pointers->target_data;
jl_value_t* rejection_reason = nullptr;
Expand Down
12 changes: 12 additions & 0 deletions src/processor.h
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,18 @@ JL_DLLEXPORT int32_t jl_set_zero_subnormals(int8_t isZero);
JL_DLLEXPORT int32_t jl_get_zero_subnormals(void);
JL_DLLEXPORT int32_t jl_set_default_nans(int8_t isDefault);
JL_DLLEXPORT int32_t jl_get_default_nans(void);

/**
* System image contents.
*
* These symbols are typically dummy values, unless statically linking
* libjulia-* and the sysimage together (see null_sysimage.c), in which
* case they allow accessing the local copy of the sysimage.
**/
extern char jl_system_image_data;
extern size_t jl_system_image_size;
extern jl_image_pointers_t jl_image_pointers;

#ifdef __cplusplus
}

Expand Down
27 changes: 14 additions & 13 deletions src/staticdata.c
Original file line number Diff line number Diff line change
Expand Up @@ -645,24 +645,25 @@ JL_DLLEXPORT int jl_running_on_valgrind(void)
return RUNNING_ON_VALGRIND;
}

void *system_image_data_unavailable;
extern void * JL_WEAK_SYMBOL_OR_ALIAS_DEFAULT(system_image_data_unavailable) jl_system_image_data;
extern void * JL_WEAK_SYMBOL_OR_ALIAS_DEFAULT(system_image_data_unavailable) jl_system_image_size;
static void jl_load_sysimg_so(void)
{
const char *sysimg_data;
assert(sysimage.fptrs.ptrs); // jl_init_processor_sysimg should already be run
if (jl_sysimg_handle == jl_exe_handle &&
&jl_system_image_data != JL_WEAK_SYMBOL_DEFAULT(system_image_data_unavailable))
sysimg_data = (const char*)&jl_system_image_data;
else
jl_dlsym(jl_sysimg_handle, "jl_system_image_data", (void **)&sysimg_data, 1);

size_t *plen;
if (jl_sysimg_handle == jl_exe_handle &&
&jl_system_image_size != JL_WEAK_SYMBOL_DEFAULT(system_image_data_unavailable))
plen = (size_t *)&jl_system_image_size;
else
const char *sysimg_data;

if (jl_system_image_size == 0) {
// in the usual case, the sysimage was not statically linked to libjulia-internal
// look up the external sysimage symbols via the dynamic linker
jl_dlsym(jl_sysimg_handle, "jl_system_image_size", (void **)&plen, 1);
jl_dlsym(jl_sysimg_handle, "jl_system_image_data", (void **)&sysimg_data, 1);
} else {
// the sysimage was statically linked directly against libjulia-internal
// use the internal symbols
plen = &jl_system_image_size;
sysimg_data = &jl_system_image_data;
}

jl_gc_notify_image_load(sysimg_data, *plen);
jl_restore_system_image_data(sysimg_data, *plen);
}
Expand Down
15 changes: 2 additions & 13 deletions src/threading.c
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,6 @@ void jl_set_pgcstack(jl_gcframe_t **pgcstack) JL_NOTSAFEPOINT
{
*jl_pgcstack_key() = pgcstack;
}
# if JL_USE_IFUNC
JL_DLLEXPORT __attribute__((weak))
void jl_register_pgcstack_getter(void);
# endif
static jl_gcframe_t **jl_get_pgcstack_init(void);
static jl_get_pgcstack_func *jl_get_pgcstack_cb = jl_get_pgcstack_init;
static jl_gcframe_t **jl_get_pgcstack_init(void)
Expand All @@ -244,15 +240,8 @@ static jl_gcframe_t **jl_get_pgcstack_init(void)
// This is clearly not thread-safe but should be fine since we
// make sure the tls states callback is finalized before adding
// multiple threads
# if JL_USE_IFUNC
if (jl_register_pgcstack_getter)
jl_register_pgcstack_getter();
else
# endif
{
jl_get_pgcstack_cb = jl_get_pgcstack_fallback;
jl_pgcstack_key = &jl_pgcstack_addr_fallback;
}
jl_get_pgcstack_cb = jl_get_pgcstack_fallback;
jl_pgcstack_key = &jl_pgcstack_addr_fallback;
return jl_get_pgcstack_cb();
}

Expand Down

0 comments on commit 4a6ada6

Please sign in to comment.