Skip to content

Commit efc35a6

Browse files
authored
Set storage class of julia globals to dllimport on windows to avoid auto-import weirdness (#54586)
Forward port of #54572
1 parent 92dfdca commit efc35a6

8 files changed

+23
-24
lines changed

Make.inc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1406,7 +1406,7 @@ ifeq ($(OS), WINNT)
14061406
HAVE_SSP := 1
14071407
OSLIBS += -Wl,--export-all-symbols -Wl,--version-script=$(BUILDROOT)/src/julia.expmap \
14081408
$(NO_WHOLE_ARCHIVE) -lpsapi -lkernel32 -lws2_32 -liphlpapi -lwinmm -ldbghelp -luserenv -lsecur32 -latomic -lole32
1409-
JLDFLAGS += -Wl,--stack,8388608
1409+
JLDFLAGS += -Wl,--stack,8388608 --disable-auto-import --disable-runtime-pseudo-reloc
14101410
ifeq ($(ARCH),i686)
14111411
JLDFLAGS += -Wl,--large-address-aware
14121412
endif

base/linking.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ function ld()
110110
# LLD supports mingw style linking
111111
flavor = "gnu"
112112
m = Sys.ARCH == :x86_64 ? "i386pep" : "i386pe"
113-
default_args = `-m $m -Bdynamic --enable-auto-image-base --allow-multiple-definition`
113+
default_args = `-m $m -Bdynamic --enable-auto-image-base --allow-multiple-definition --disable-auto-import --disable-runtime-pseudo-reloc`
114114
elseif Sys.isapple()
115115
flavor = "darwin"
116116
arch = Sys.ARCH == :aarch64 ? :arm64 : Sys.ARCH

cli/Makefile

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ LOADER_CFLAGS += -DGLIBCXX_LEAST_VERSION_SYMBOL=\"$(shell echo "$(CSL_NEXT_GLIBC
1717
endif
1818

1919
ifeq ($(OS),WINNT)
20-
LOADER_LDFLAGS += -municode -mconsole -nostdlib --disable-auto-import \
21-
--disable-runtime-pseudo-reloc -lntdll -lkernel32 -lpsapi
20+
LOADER_LDFLAGS += -municode -mconsole -nostdlib -lntdll -lkernel32 -lpsapi
2221
else ifeq ($(OS),Linux)
2322
# textoff and notext are aliases to the same option which suppress the TEXTREL warning for i686
2423
LOADER_LDFLAGS += -Wl,--no-as-needed -ldl -lpthread -rdynamic -lc -Wl,--as-needed -Wl,-z,notext

src/aotcompile.cpp

+11-9
Original file line numberDiff line numberDiff line change
@@ -1242,6 +1242,8 @@ static void materializePreserved(Module &M, Partition &partition) {
12421242
GV.setInitializer(nullptr);
12431243
GV.setLinkage(GlobalValue::ExternalLinkage);
12441244
GV.setVisibility(GlobalValue::HiddenVisibility);
1245+
if (GV.getDLLStorageClass() != GlobalValue::DLLStorageClassTypes::DefaultStorageClass)
1246+
continue; // Don't mess with exported or imported globals
12451247
GV.setDSOLocal(true);
12461248
}
12471249

@@ -1778,6 +1780,7 @@ void jl_dump_native_impl(void *native_code,
17781780
if (jl_small_typeof_copy) {
17791781
jl_small_typeof_copy->setVisibility(GlobalValue::HiddenVisibility);
17801782
jl_small_typeof_copy->setDSOLocal(true);
1783+
jl_small_typeof_copy->setDLLStorageClass(GlobalValue::DLLStorageClassTypes::DefaultStorageClass);
17811784
}
17821785
}
17831786

@@ -1811,19 +1814,18 @@ void jl_dump_native_impl(void *native_code,
18111814
// reflect the address of the jl_RTLD_DEFAULT_handle variable
18121815
// back to the caller, so that we can check for consistency issues
18131816
GlobalValue *jlRTLD_DEFAULT_var = jl_emit_RTLD_DEFAULT_var(&metadataM);
1814-
if (TheTriple.isOSBinFormatCOFF()) {
1815-
jlRTLD_DEFAULT_var->setDLLStorageClass(GlobalValue::DLLImportStorageClass);
1816-
}
1817-
addComdat(new GlobalVariable(metadataM,
1818-
jlRTLD_DEFAULT_var->getType(),
1819-
true,
1820-
GlobalVariable::ExternalLinkage,
1821-
jlRTLD_DEFAULT_var,
1822-
"jl_RTLD_DEFAULT_handle_pointer"), TheTriple);
18231817

18241818
Type *T_size = DL.getIntPtrType(Context);
18251819
Type *T_psize = T_size->getPointerTo();
18261820

1821+
auto FT = FunctionType::get(Type::getInt8Ty(Context)->getPointerTo()->getPointerTo(), {}, false);
1822+
auto F = Function::Create(FT, Function::ExternalLinkage, "get_jl_RTLD_DEFAULT_handle_addr", metadataM);
1823+
llvm::IRBuilder<> builder(BasicBlock::Create(Context, "top", F));
1824+
builder.CreateRet(jlRTLD_DEFAULT_var);
1825+
F->setLinkage(GlobalValue::ExternalLinkage);
1826+
if (TheTriple.isOSBinFormatCOFF())
1827+
F->setDLLStorageClass(GlobalValue::DLLStorageClassTypes::DLLExportStorageClass);
1828+
18271829
if (TheTriple.isOSWindows()) {
18281830
// Windows expect that the function `_DllMainStartup` is present in an dll.
18291831
// Normal compilers use something like Zig's crtdll.c instead we provide a

src/codegen.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -542,9 +542,12 @@ struct JuliaVariable {
542542
if (GlobalValue *V = m->getNamedValue(name))
543543
return cast<GlobalVariable>(V);
544544
auto T_size = m->getDataLayout().getIntPtrType(m->getContext());
545-
return new GlobalVariable(*m, _type(T_size),
545+
auto var = new GlobalVariable(*m, _type(T_size),
546546
isconst, GlobalVariable::ExternalLinkage,
547547
NULL, name);
548+
if (Triple(m->getTargetTriple()).isOSWindows())
549+
var->setDLLStorageClass(GlobalValue::DLLStorageClassTypes::DLLImportStorageClass); // Cross-library imports must be explicit for COFF (Windows)
550+
return var;
548551
}
549552
GlobalVariable *realize(jl_codectx_t &ctx);
550553
};
@@ -2141,9 +2144,6 @@ static inline GlobalVariable *prepare_global_in(Module *M, GlobalVariable *G)
21412144
proto->setInitializer(G->getInitializer());
21422145
}
21432146
proto->copyAttributesFrom(G);
2144-
// DLLImport only needs to be set for the shadow module
2145-
// it just gets annoying in the JIT
2146-
proto->setDLLStorageClass(GlobalValue::DefaultStorageClass);
21472147
return proto;
21482148
}
21492149
return cast<GlobalVariable>(local);

src/staticdata.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -3070,10 +3070,10 @@ JL_DLLEXPORT void jl_preload_sysimg_so(const char *fname)
30703070
// Allow passing in a module handle directly, rather than a path
30713071
JL_DLLEXPORT void jl_set_sysimg_so(void *handle)
30723072
{
3073-
void* *jl_RTLD_DEFAULT_handle_pointer;
3073+
void** (*get_jl_RTLD_DEFAULT_handle_addr)(void) = NULL;
30743074
if (handle != jl_RTLD_DEFAULT_handle) {
3075-
int symbol_found = jl_dlsym(handle, "jl_RTLD_DEFAULT_handle_pointer", (void **)&jl_RTLD_DEFAULT_handle_pointer, 0);
3076-
if (!symbol_found || (void*)&jl_RTLD_DEFAULT_handle != *jl_RTLD_DEFAULT_handle_pointer)
3075+
int symbol_found = jl_dlsym(handle, "get_jl_RTLD_DEFAULT_handle_addr", (void **)&get_jl_RTLD_DEFAULT_handle_addr, 0);
3076+
if (!symbol_found || (void*)&jl_RTLD_DEFAULT_handle != (get_jl_RTLD_DEFAULT_handle_addr()))
30773077
jl_error("System image file failed consistency check: maybe opened the wrong version?");
30783078
}
30793079
if (jl_options.cpu_target == NULL)

sysimage.mk

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ $(build_private_libdir)/%.$(SHLIB_EXT): $(build_private_libdir)/%-o.a
1717
@$(call PRINT_LINK, $(CXX) $(LDFLAGS) -shared $(fPIC) -L$(build_private_libdir) -L$(build_libdir) -L$(build_shlibdir) -o $@ \
1818
$(WHOLE_ARCHIVE) $< $(NO_WHOLE_ARCHIVE) \
1919
$(if $(findstring -debug,$(notdir $@)),-ljulia-internal-debug -ljulia-debug,-ljulia-internal -ljulia) \
20-
$$([ $(OS) = WINNT ] && echo '' -lssp))
20+
$$([ $(OS) = WINNT ] && echo '' -lssp --disable-auto-import --disable-runtime-pseudo-reloc))
2121
@$(INSTALL_NAME_CMD)$(notdir $@) $@
2222
@$(DSYMUTIL) $@
2323

test/compileall.jl

+1-3
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ mktempdir() do dir
66
@test success(pipeline(`$(Base.julia_cmd()) --compile=all --strip-ir --output-o $(dir)/sys.o.a -e 'exit()'`, stderr=stderr)) broken=(Sys.iswindows() && Sys.WORD_SIZE == 32)
77
if isfile(joinpath(dir, "sys.o.a"))
88
Base.Linking.link_image(joinpath(dir, "sys.o.a"), joinpath(dir, "sys.so"))
9-
# TODO: Broken on Windows due to
10-
# https://github.com/llvm/llvm-project/issues/84424
11-
@test success(`$(Base.julia_cmd()) -J $(dir)/sys.so -e 'Base.scrub_repl_backtrace(nothing); exit()'`) broken=Sys.iswindows()
9+
@test success(`$(Base.julia_cmd()) -J $(dir)/sys.so -e 'Base.scrub_repl_backtrace(nothing); exit()'`)
1210
end
1311
end

0 commit comments

Comments
 (0)