Skip to content

Commit 90acf2a

Browse files
gbaraldiKristofferC
authored and
KristofferC
committed
Set storage class of julia globals to dllimport on windows to avoid auto-import weirdness (#54586)
Forward port of #54572 (cherry picked from commit efc35a6)
1 parent 6b4b5ef commit 90acf2a

File tree

7 files changed

+22
-18
lines changed

7 files changed

+22
-18
lines changed

Make.inc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1393,7 +1393,7 @@ ifeq ($(OS), WINNT)
13931393
HAVE_SSP := 1
13941394
OSLIBS += -Wl,--export-all-symbols -Wl,--version-script=$(BUILDROOT)/src/julia.expmap \
13951395
$(NO_WHOLE_ARCHIVE) -lpsapi -lkernel32 -lws2_32 -liphlpapi -lwinmm -ldbghelp -luserenv -lsecur32 -latomic -lole32
1396-
JLDFLAGS += -Wl,--stack,8388608
1396+
JLDFLAGS += -Wl,--stack,8388608 --disable-auto-import --disable-runtime-pseudo-reloc
13971397
ifeq ($(ARCH),i686)
13981398
JLDFLAGS += -Wl,--large-address-aware
13991399
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-6
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

@@ -1773,6 +1775,7 @@ void jl_dump_native_impl(void *native_code,
17731775
if (jl_small_typeof_copy) {
17741776
jl_small_typeof_copy->setVisibility(GlobalValue::HiddenVisibility);
17751777
jl_small_typeof_copy->setDSOLocal(true);
1778+
jl_small_typeof_copy->setDLLStorageClass(GlobalValue::DLLStorageClassTypes::DefaultStorageClass);
17761779
}
17771780
}
17781781

@@ -1804,16 +1807,18 @@ void jl_dump_native_impl(void *native_code,
18041807
// reflect the address of the jl_RTLD_DEFAULT_handle variable
18051808
// back to the caller, so that we can check for consistency issues
18061809
GlobalValue *jlRTLD_DEFAULT_var = jl_emit_RTLD_DEFAULT_var(&metadataM);
1807-
addComdat(new GlobalVariable(metadataM,
1808-
jlRTLD_DEFAULT_var->getType(),
1809-
true,
1810-
GlobalVariable::ExternalLinkage,
1811-
jlRTLD_DEFAULT_var,
1812-
"jl_RTLD_DEFAULT_handle_pointer"), TheTriple);
18131810

18141811
Type *T_size = DL.getIntPtrType(Context);
18151812
Type *T_psize = T_size->getPointerTo();
18161813

1814+
auto FT = FunctionType::get(Type::getInt8Ty(Context)->getPointerTo()->getPointerTo(), {}, false);
1815+
auto F = Function::Create(FT, Function::ExternalLinkage, "get_jl_RTLD_DEFAULT_handle_addr", metadataM);
1816+
llvm::IRBuilder<> builder(BasicBlock::Create(Context, "top", F));
1817+
builder.CreateRet(jlRTLD_DEFAULT_var);
1818+
F->setLinkage(GlobalValue::ExternalLinkage);
1819+
if (TheTriple.isOSBinFormatCOFF())
1820+
F->setDLLStorageClass(GlobalValue::DLLStorageClassTypes::DLLExportStorageClass);
1821+
18171822
if (TheTriple.isOSWindows()) {
18181823
// Windows expect that the function `_DllMainStartup` is present in an dll.
18191824
// 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
@@ -536,9 +536,12 @@ struct JuliaVariable {
536536
if (GlobalValue *V = m->getNamedValue(name))
537537
return cast<GlobalVariable>(V);
538538
auto T_size = m->getDataLayout().getIntPtrType(m->getContext());
539-
return new GlobalVariable(*m, _type(T_size),
539+
auto var = new GlobalVariable(*m, _type(T_size),
540540
isconst, GlobalVariable::ExternalLinkage,
541541
NULL, name);
542+
if (Triple(m->getTargetTriple()).isOSWindows())
543+
var->setDLLStorageClass(GlobalValue::DLLStorageClassTypes::DLLImportStorageClass); // Cross-library imports must be explicit for COFF (Windows)
544+
return var;
542545
}
543546
GlobalVariable *realize(jl_codectx_t &ctx);
544547
};
@@ -2129,9 +2132,6 @@ static inline GlobalVariable *prepare_global_in(Module *M, GlobalVariable *G)
21292132
proto->setInitializer(G->getInitializer());
21302133
}
21312134
proto->copyAttributesFrom(G);
2132-
// DLLImport only needs to be set for the shadow module
2133-
// it just gets annoying in the JIT
2134-
proto->setDLLStorageClass(GlobalValue::DefaultStorageClass);
21352135
return proto;
21362136
}
21372137
return cast<GlobalVariable>(local);

src/staticdata.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -3042,10 +3042,10 @@ JL_DLLEXPORT void jl_preload_sysimg_so(const char *fname)
30423042
// Allow passing in a module handle directly, rather than a path
30433043
JL_DLLEXPORT void jl_set_sysimg_so(void *handle)
30443044
{
3045-
void* *jl_RTLD_DEFAULT_handle_pointer;
3045+
void** (*get_jl_RTLD_DEFAULT_handle_addr)(void) = NULL;
30463046
if (handle != jl_RTLD_DEFAULT_handle) {
3047-
int symbol_found = jl_dlsym(handle, "jl_RTLD_DEFAULT_handle_pointer", (void **)&jl_RTLD_DEFAULT_handle_pointer, 0);
3048-
if (!symbol_found || (void*)&jl_RTLD_DEFAULT_handle != *jl_RTLD_DEFAULT_handle_pointer)
3047+
int symbol_found = jl_dlsym(handle, "get_jl_RTLD_DEFAULT_handle_addr", (void **)&get_jl_RTLD_DEFAULT_handle_addr, 0);
3048+
if (!symbol_found || (void*)&jl_RTLD_DEFAULT_handle != (get_jl_RTLD_DEFAULT_handle_addr()))
30493049
jl_error("System image file failed consistency check: maybe opened the wrong version?");
30503050
}
30513051
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

0 commit comments

Comments
 (0)