Skip to content

Commit 036add1

Browse files
gbaraldiKenotopolarity
committed
Set storage class of julia globals to dllimport on windows to avoid auto-import weirdness.
Also make the compiler error instead of emitting pseudo-relocations when encountering a reference to a non-exported symbol in a dll. Co-authored-by: Keno Fischer <keno@juliacomputing.com> Co-authored-by: Cody Tapscott <84105208+topolarity@users.noreply.github.com>
1 parent 7eeeb56 commit 036add1

File tree

7 files changed

+22
-19
lines changed

7 files changed

+22
-19
lines changed

Make.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1365,7 +1365,7 @@ ifeq ($(OS), WINNT)
13651365
HAVE_SSP := 1
13661366
OSLIBS += -Wl,--export-all-symbols -Wl,--version-script=$(BUILDROOT)/src/julia.expmap \
13671367
$(NO_WHOLE_ARCHIVE) -lpsapi -lkernel32 -lws2_32 -liphlpapi -lwinmm -ldbghelp -luserenv -lsecur32 -latomic
1368-
JLDFLAGS += -Wl,--stack,8388608
1368+
JLDFLAGS += -Wl,--stack,8388608 --disable-auto-import --disable-runtime-pseudo-reloc
13691369
ifeq ($(ARCH),i686)
13701370
JLDFLAGS += -Wl,--large-address-aware
13711371
endif

base/linking.jl

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 2 deletions
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
LOADER_LDFLAGS += -Wl,--no-as-needed -ldl -lpthread -rdynamic -lc -Wl,--as-needed
2423
else ifeq ($(OS),FreeBSD)

src/aotcompile.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,6 +1166,8 @@ static void materializePreserved(Module &M, Partition &partition) {
11661166
GV.setInitializer(nullptr);
11671167
GV.setLinkage(GlobalValue::ExternalLinkage);
11681168
GV.setVisibility(GlobalValue::HiddenVisibility);
1169+
if (GV.getDLLStorageClass() != GlobalValue::DLLStorageClassTypes::DefaultStorageClass)
1170+
continue; // Don't mess with exported or imported globals
11691171
GV.setDSOLocal(true);
11701172
}
11711173

@@ -1645,6 +1647,7 @@ void jl_dump_native_impl(void *native_code,
16451647
if (jl_small_typeof_copy) {
16461648
jl_small_typeof_copy->setVisibility(GlobalValue::HiddenVisibility);
16471649
jl_small_typeof_copy->setDSOLocal(true);
1650+
jl_small_typeof_copy->setDLLStorageClass(GlobalValue::DLLStorageClassTypes::DefaultStorageClass);
16481651
}
16491652
}
16501653

@@ -1673,16 +1676,17 @@ void jl_dump_native_impl(void *native_code,
16731676
// reflect the address of the jl_RTLD_DEFAULT_handle variable
16741677
// back to the caller, so that we can check for consistency issues
16751678
GlobalValue *jlRTLD_DEFAULT_var = jl_emit_RTLD_DEFAULT_var(&metadataM);
1676-
addComdat(new GlobalVariable(metadataM,
1677-
jlRTLD_DEFAULT_var->getType(),
1678-
true,
1679-
GlobalVariable::ExternalLinkage,
1680-
jlRTLD_DEFAULT_var,
1681-
"jl_RTLD_DEFAULT_handle_pointer"), TheTriple);
1682-
16831679
Type *T_size = DL.getIntPtrType(Context);
16841680
Type *T_psize = T_size->getPointerTo();
16851681

1682+
auto FT = FunctionType::get(Type::getInt8Ty(Context)->getPointerTo()->getPointerTo(), {}, false);
1683+
auto F = Function::Create(FT, Function::ExternalLinkage, "get_jl_RTLD_DEFAULT_handle_addr", metadataM);
1684+
llvm::IRBuilder<> builder(BasicBlock::Create(Context, "top", F));
1685+
builder.CreateRet(jlRTLD_DEFAULT_var);
1686+
F->setLinkage(GlobalValue::ExternalLinkage);
1687+
if (TheTriple.isOSBinFormatCOFF())
1688+
F->setDLLStorageClass(GlobalValue::DLLStorageClassTypes::DLLExportStorageClass);
1689+
16861690
if (TheTriple.isOSWindows()) {
16871691
// Windows expect that the function `_DllMainStartup` is present in an dll.
16881692
// Normal compilers use something like Zig's crtdll.c instead we provide a

src/codegen.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -493,9 +493,12 @@ struct JuliaVariable {
493493
if (GlobalValue *V = m->getNamedValue(name))
494494
return cast<GlobalVariable>(V);
495495
auto T_size = m->getDataLayout().getIntPtrType(m->getContext());
496-
return new GlobalVariable(*m, _type(T_size),
496+
auto var = new GlobalVariable(*m, _type(T_size),
497497
isconst, GlobalVariable::ExternalLinkage,
498498
NULL, name);
499+
if (Triple(m->getTargetTriple()).isOSWindows())
500+
var->setDLLStorageClass(GlobalValue::DLLStorageClassTypes::DLLImportStorageClass); // Cross-library imports must be explicit for COFF (Windows)
501+
return var;
499502
}
500503
GlobalVariable *realize(jl_codectx_t &ctx);
501504
};
@@ -1786,9 +1789,6 @@ static inline GlobalVariable *prepare_global_in(Module *M, GlobalVariable *G)
17861789
G->isConstant(), GlobalVariable::ExternalLinkage,
17871790
nullptr, G->getName(), nullptr, G->getThreadLocalMode());
17881791
proto->copyAttributesFrom(G);
1789-
// DLLImport only needs to be set for the shadow module
1790-
// it just gets annoying in the JIT
1791-
proto->setDLLStorageClass(GlobalValue::DefaultStorageClass);
17921792
return proto;
17931793
}
17941794
return cast<GlobalVariable>(local);

src/staticdata.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2838,9 +2838,9 @@ JL_DLLEXPORT void jl_preload_sysimg_so(const char *fname)
28382838
// Allow passing in a module handle directly, rather than a path
28392839
JL_DLLEXPORT void jl_set_sysimg_so(void *handle)
28402840
{
2841-
void* *jl_RTLD_DEFAULT_handle_pointer;
2842-
int symbol_found = jl_dlsym(handle, "jl_RTLD_DEFAULT_handle_pointer", (void **)&jl_RTLD_DEFAULT_handle_pointer, 0);
2843-
if (!symbol_found || (void*)&jl_RTLD_DEFAULT_handle != *jl_RTLD_DEFAULT_handle_pointer)
2841+
void** (*get_jl_RTLD_DEFAULT_handle_addr)(void) = NULL;
2842+
int symbol_found = jl_dlsym(handle, "get_jl_RTLD_DEFAULT_handle_addr", (void **)&get_jl_RTLD_DEFAULT_handle_addr, 0);
2843+
if (!symbol_found || (void*)&jl_RTLD_DEFAULT_handle != (get_jl_RTLD_DEFAULT_handle_addr()))
28442844
jl_error("System image file failed consistency check: maybe opened the wrong version?");
28452845
if (jl_options.cpu_target == NULL)
28462846
jl_options.cpu_target = "native";

sysimage.mk

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

0 commit comments

Comments
 (0)