Skip to content

Commit 48c4283

Browse files
authored
Merge pull request #38938 from imciner2/im/symfix
Fix libjulia symbol trampolines, and export `jl_n_threads`
2 parents 46487ae + 9489e64 commit 48c4283

15 files changed

+76
-33
lines changed

cli/Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ $(BUILDDIR)/loader_exe.o : $(SRCDIR)/loader_exe.c $(HEADERS)
5454
$(BUILDDIR)/loader_exe.dbg.obj : $(SRCDIR)/loader_exe.c $(HEADERS)
5555
@$(call PRINT_CC, $(CC) $(DEBUGFLAGS) $(LOADER_CFLAGS) -c $< -o $@)
5656
$(BUILDDIR)/loader_trampolines.o : $(SRCDIR)/trampolines/trampolines_$(ARCH).S
57-
@$(call PRINT_CC, $(CC) $(DEBUGFLAGS) $(LOADER_CFLAGS) $< -c -o $@)
57+
@$(call PRINT_CC, $(CC) $(SHIPFLAGS) $(LOADER_CFLAGS) $< -c -o $@)
58+
59+
# Debugging target to help us see what kind of code is being generated for our trampolines
60+
dump-trampolines: $(SRCDIR)/trampolines/trampolines_$(ARCH).S
61+
$(CC) $(SHIPFLAGS) $(LOADER_CFLAGS) $< -S | sed -E 's/ ((%%)|;) /\n/g' | sed -E 's/.global/\n.global/g'
5862

5963
DIRS = $(build_bindir) $(build_libdir)
6064
$(DIRS):

cli/jl_exports.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@
55
#include "../src/jl_exported_data.inc"
66
#include "../src/jl_exported_funcs.inc"
77

8-
// Define data symbols as `const void * $(name);`
8+
// Define pointer data as `const void * $(name);`
99
#define XX(name) JL_DLLEXPORT const void * name;
10-
JL_EXPORTED_DATA(XX)
10+
JL_EXPORTED_DATA_POINTERS(XX)
11+
#undef XX
12+
13+
// Define symbol data as `$type) $(name);`
14+
#define XX(name, type) JL_DLLEXPORT type name;
15+
JL_EXPORTED_DATA_SYMBOLS(XX)
1116
#undef XX
1217

1318
// Define holder locations for function addresses as `const void * $(name)_addr`

cli/trampolines/trampolines_aarch64.S

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
11
#include "../../src/jl_exported_funcs.inc"
22

3+
// On macOS, we need to prepend underscores on symbols
4+
#if defined(__APPLE__) && defined(__MACH__)
5+
#define CNAME(x) _##x
6+
#define PAGE(x) x##@PAGE
7+
#define PAGEOFF(x) x##@PAGEOFF
8+
#define SEP %%
9+
#else
10+
#define CNAME(x) x
11+
#define PAGE(x) x
12+
#define PAGEOFF(x) #:lo12:##x
13+
#define SEP ;
14+
#endif
15+
316
#define XX(name) \
4-
.global name; \
5-
.cfi_startproc; \
6-
name##:; \
7-
adrp x0, name##_addr; \
8-
ldr x0, [x0, #:lo12:name##_addr]; \
9-
br x0; \
10-
.cfi_endproc; \
17+
.global CNAME(name) SEP \
18+
.cfi_startproc SEP \
19+
.p2align 2 SEP \
20+
CNAME(name)##: SEP \
21+
adrp x16, PAGE(CNAME(name##_addr)) SEP \
22+
ldr x16, [x16, PAGEOFF(CNAME(name##_addr))] SEP \
23+
br x16 SEP \
24+
.cfi_endproc SEP \
25+
1126
JL_EXPORTED_FUNCS(XX)
1227
#undef XX

cli/trampolines/trampolines_i686.S

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020

2121
#define XX(name) \
2222
DEBUGINFO(name); \
23-
.global name; \
23+
.global CNAME(name); \
2424
.cfi_startproc; \
25-
name##:; \
25+
CNAME(name)##:; \
2626
CET_START(); \
2727
jmpl *(CNAME(name##_addr)); \
2828
ud2; \

cli/trampolines/trampolines_x86_64.S

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@
3434

3535
#define XX(name) \
3636
DEBUGINFO(name); \
37-
.global name; \
37+
.global CNAME(name); \
3838
.cfi_startproc; \
3939
SEH_START1(name); \
40-
name##:; \
40+
CNAME(name)##:; \
4141
SEH_START2(); \
4242
CET_START(); \
4343
mov CNAME(name##_addr)(%rip),%r11; \

src/ccall.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ static bool runtime_sym_gvs(jl_codegen_params_t &emission_context, const char *f
2222
GlobalVariable *libptrgv;
2323
jl_codegen_params_t::SymMapGV *symMap;
2424
#ifdef _OS_WINDOWS_
25-
if ((intptr_t)f_lib == 1) {
25+
if ((intptr_t)f_lib == (intptr_t)JL_EXE_LIBNAME) {
2626
libptrgv = prepare_global_in(M, jlexe_var);
2727
symMap = &emission_context.symMapExe;
2828
}
29-
else if ((intptr_t)f_lib == 2) {
29+
else if ((intptr_t)f_lib == (intptr_t)JL_LIBJULIA_INTERNAL_DL_LIBNAME) {
3030
libptrgv = prepare_global_in(M, jldll_var);
3131
symMap = &emission_context.symMapDl;
3232
}
@@ -1266,7 +1266,7 @@ static jl_cgval_t emit_ccall(jl_codectx_t &ctx, jl_value_t **args, size_t nargs)
12661266
auto _is_libjulia_func = [&] (uintptr_t ptr, const char *name) {
12671267
if ((uintptr_t)fptr == ptr)
12681268
return true;
1269-
return (!f_lib || f_lib == JL_DL_LIBNAME) && f_name && !strcmp(f_name, name);
1269+
return (!f_lib || f_lib == JL_LIBJULIA_INTERNAL_DL_LIBNAME) && f_name && !strcmp(f_name, name);
12701270
};
12711271
#define is_libjulia_func(name) _is_libjulia_func((uintptr_t)&(name), #name)
12721272

src/codegen.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ static const auto jlexe_var = new JuliaVariable{
397397
[](LLVMContext &C) { return T_pint8; },
398398
};
399399
static const auto jldll_var = new JuliaVariable{
400-
"jl_dl_handle",
400+
"jl_libjulia_internal_handle",
401401
true,
402402
[](LLVMContext &C) { return T_pint8; },
403403
};
@@ -7531,7 +7531,7 @@ static void init_jit_functions(void)
75317531
add_named_global(jlRTLD_DEFAULT_var, &jl_RTLD_DEFAULT_handle);
75327532
#ifdef _OS_WINDOWS_
75337533
add_named_global(jlexe_var, &jl_exe_handle);
7534-
add_named_global(jldll_var, &jl_dl_handle);
7534+
add_named_global(jldll_var, &jl_libjulia_internal_handle);
75357535
#endif
75367536
global_jlvalue_to_llvm(new JuliaVariable{"jl_true", true, get_pjlvalue}, &jl_true);
75377537
global_jlvalue_to_llvm(new JuliaVariable{"jl_false", true, get_pjlvalue}, &jl_false);

src/dlload.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -308,8 +308,10 @@ const char *jl_dlfind_win32(const char *f_name)
308308
void * dummy;
309309
if (jl_dlsym(jl_exe_handle, f_name, &dummy, 0))
310310
return JL_EXE_LIBNAME;
311-
if (jl_dlsym(jl_dl_handle, f_name, &dummy, 0))
312-
return JL_DL_LIBNAME;
311+
if (jl_dlsym(jl_libjulia_internal_handle, f_name, &dummy, 0))
312+
return JL_LIBJULIA_INTERNAL_DL_LIBNAME;
313+
if (jl_dlsym(jl_libjulia_handle, f_name, &dummy, 0))
314+
return JL_LIBJULIA_DL_LIBNAME;
313315
if (jl_dlsym(jl_kernel32_handle, f_name, &dummy, 0))
314316
return "kernel32";
315317
if (jl_dlsym(jl_ntdll_handle, f_name, &dummy, 0))
@@ -334,8 +336,8 @@ const char *jl_dlfind_win32(const char *f_name)
334336
// explicit is preferred over implicit
335337
return NULL;
336338
// oops, we didn't find it. NULL defaults to searching jl_RTLD_DEFAULT_handle,
337-
// which defaults to jl_dl_handle, where we won't find it, and will throw the
338-
// appropriate error.
339+
// which defaults to jl_libjulia_internal_handle, where we won't find it, and
340+
// will throw the appropriate error.
339341
}
340342
#endif
341343

src/init.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,8 @@ JL_DLLEXPORT void jl_atexit_hook(int exitcode)
293293

294294
static void post_boot_hooks(void);
295295

296-
JL_DLLEXPORT void *jl_dl_handle;
296+
JL_DLLEXPORT void *jl_libjulia_internal_handle;
297+
JL_DLLEXPORT void *jl_libjulia_handle;
297298
void *jl_RTLD_DEFAULT_handle;
298299
JL_DLLEXPORT void *jl_exe_handle;
299300
#ifdef _OS_WINDOWS_
@@ -657,7 +658,10 @@ void _julia_init(JL_IMAGE_SEARCH rel)
657658
jl_prep_sanitizers();
658659
void *stack_lo, *stack_hi;
659660
jl_init_stack_limits(1, &stack_lo, &stack_hi);
660-
jl_dl_handle = jl_load_dynamic_library(NULL, JL_RTLD_DEFAULT, 1);
661+
662+
// Load libjulia-internal (which contains this function), and libjulia, explicitly.
663+
jl_libjulia_internal_handle = jl_load_dynamic_library(NULL, JL_RTLD_DEFAULT, 1);
664+
jl_libjulia_handle = jl_load_dynamic_library(JL_LIBJULIA_DL_LIBNAME, JL_RTLD_DEFAULT, 1);
661665
#ifdef _OS_WINDOWS_
662666
jl_ntdll_handle = jl_dlopen("ntdll.dll", 0); // bypass julia's pathchecking for system dlls
663667
jl_kernel32_handle = jl_dlopen("kernel32.dll", 0);

src/jl_exported_data.inc

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
#define JL_EXPORTED_DATA(XX) \
1+
// Pointers that are exposed through the public libjulia
2+
#define JL_EXPORTED_DATA_POINTERS(XX) \
23
XX(jl_abstractarray_type) \
34
XX(jl_abstractslot_type) \
45
XX(jl_abstractstring_type) \
@@ -115,3 +116,8 @@
115116
XX(jl_void_type) \
116117
XX(jl_voidpointer_type) \
117118
XX(jl_weakref_type)
119+
120+
121+
// Data symbols that are defined inside the public libjulia
122+
#define JL_EXPORTED_DATA_SYMBOLS(XX) \
123+
XX(jl_n_threads, int)

0 commit comments

Comments
 (0)