Skip to content

Commit 5673534

Browse files
committed
Makefile: Add support for symbol versioning (off by default)
This change adds `DEFAULT_SYMBOL_VERSION` as a Makefile option. If specified, this will configure the linker to assign the specified version string to all Julia-exported symbols. The other half of the mechanism is `#define JL_SYMBOL_VERSION`, which is used by the ccall machinery to look up symbols by their version + name.
1 parent 02a7ec1 commit 5673534

File tree

6 files changed

+56
-13
lines changed

6 files changed

+56
-13
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
/source-dist.tmp
1313
/source-dist.tmp1
1414

15+
*.generated.expmap
1516
*.exe
1617
*.dll
1718
*.dwo

Make.inc

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,12 @@ JULIA_MAJOR_VERSION := $(shell echo $(JULIA_VERSION) | cut -d'-' -f 1 | cut -d'.
180180
JULIA_MINOR_VERSION := $(shell echo $(JULIA_VERSION) | cut -d'-' -f 1 | cut -d'.' -f 2)
181181
JULIA_PATCH_VERSION := $(shell echo $(JULIA_VERSION) | cut -d'-' -f 1 | cut -d'.' -f 3)
182182

183+
# In order to load multiple libjulia's with different DEFAULT_SYMBOL_VERSION
184+
# each must have a unique SONAME
185+
ifdef DEFAULT_SYMBOL_VERSION
186+
SOMAJOR := "$(SOMAJOR)-$(DEFAULT_SYMBOL_VERSION)"
187+
endif
188+
183189
# libjulia's SONAME will follow the format libjulia.so.$(SOMAJOR). Before v1.0.0,
184190
# SOMAJOR will be a two-decimal value, e.g. libjulia.so.0.5, whereas at and beyond
185191
# v1.0.0, SOMAJOR will be simply the major version number, e.g. libjulia.so.1
@@ -586,6 +592,11 @@ define SONAME_FLAGS
586592
endef
587593
endif
588594

595+
ifdef DEFAULT_SYMBOL_VERSION
596+
JCXXFLAGS += -DJL_SYMBOL_VERSION=\"$(DEFAULT_SYMBOL_VERSION)\"
597+
JCFLAGS += -DJL_SYMBOL_VERSION=\"$(DEFAULT_SYMBOL_VERSION)\"
598+
endif
599+
589600
ifeq ($(OS),WINNT)
590601
define IMPLIB_FLAGS
591602
-Wl,--out-implib,$(build_libdir)/$(notdir $1).a
@@ -1264,7 +1275,7 @@ ifeq (supported, $(shell echo $(IFUNC_DETECT_SRC) | $(CC) -Werror -x c - -S -o /
12641275
JCPPFLAGS += -DJULIA_HAS_IFUNC_SUPPORT=1
12651276
endif
12661277
JLDFLAGS += -Wl,-Bdynamic
1267-
OSLIBS += -Wl,--version-script=$(JULIAHOME)/src/julia.expmap
1278+
OSLIBS += -Wl,--version-script=$(BUILDROOT)/src/julia.generated.expmap
12681279
ifneq ($(SANITIZE),1)
12691280
JLDFLAGS += -Wl,-no-undefined
12701281
endif
@@ -1289,7 +1300,7 @@ OSLIBS += -lelf -lkvm -lrt -lpthread -latomic
12891300
# See #21788
12901301
OSLIBS += -lgcc_s
12911302

1292-
OSLIBS += -Wl,--export-dynamic -Wl,--version-script=$(JULIAHOME)/src/julia.expmap \
1303+
OSLIBS += -Wl,--export-dynamic -Wl,--version-script=$(BUILDROOT)/src/julia.generated.expmap \
12931304
$(NO_WHOLE_ARCHIVE)
12941305
endif
12951306

@@ -1304,7 +1315,7 @@ endif
13041315

13051316
ifeq ($(OS), WINNT)
13061317
HAVE_SSP := 1
1307-
OSLIBS += -Wl,--export-all-symbols -Wl,--version-script=$(JULIAHOME)/src/julia.expmap \
1318+
OSLIBS += -Wl,--export-all-symbols -Wl,--version-script=$(BUILDROOT)/src/julia.generated.expmap \
13081319
$(NO_WHOLE_ARCHIVE) -lpsapi -lkernel32 -lws2_32 -liphlpapi -lwinmm -ldbghelp -luserenv -lsecur32 -latomic
13091320
JLDFLAGS += -Wl,--stack,8388608
13101321
ifeq ($(ARCH),i686)

cli/Makefile

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,20 @@ julia-debug: $(build_bindir)/julia-debug$(EXE)
104104
libjulia-release: $(build_shlibdir)/libjulia.$(SHLIB_EXT)
105105
libjulia-debug: $(build_shlibdir)/libjulia-debug.$(SHLIB_EXT)
106106

107+
ifeq ($(OS), Linux)
108+
VERSIONSCRIPT := -Wl,--version-script=$(BUILDDIR)/julia.generated.expmap
109+
else ifeq ($(OS), FreeBSD)
110+
VERSIONSCRIPT := -Wl,--version-script=$(BUILDDIR)/julia.generated.expmap
111+
endif
112+
107113
ifeq ($(OS),WINNT)
108114
# On Windows we need to strip out exported functions from the generated import library.
109115
STRIP_EXPORTED_FUNCS := $(shell $(CPP_STDOUT) -I$(JULIAHOME)/src $(SRCDIR)/list_strip_symbols.h)
110116
endif
111117

112-
$(build_shlibdir)/libjulia.$(JL_MAJOR_MINOR_SHLIB_EXT): $(LIB_OBJS) $(SRCDIR)/list_strip_symbols.h | $(build_shlibdir) $(build_libdir)
118+
$(build_shlibdir)/libjulia.$(JL_MAJOR_MINOR_SHLIB_EXT): $(LIB_OBJS) $(SRCDIR)/list_strip_symbols.h $(BUILDDIR)/julia.generated.expmap | $(build_shlibdir) $(build_libdir)
113119
@$(call PRINT_LINK, $(CC) $(call IMPLIB_FLAGS,$@.tmp) $(LOADER_CFLAGS) -DLIBRARY_EXPORTS -shared $(SHIPFLAGS) $(LIB_OBJS) -o $@ \
114-
$(JLIBLDFLAGS) $(LOADER_LDFLAGS) $(call SONAME_FLAGS,libjulia.$(JL_MAJOR_SHLIB_EXT)))
120+
$(JLIBLDFLAGS) $(LOADER_LDFLAGS) $(VERSIONSCRIPT) $(call SONAME_FLAGS,libjulia.$(JL_MAJOR_SHLIB_EXT)))
115121
@$(INSTALL_NAME_CMD)libjulia.$(JL_MAJOR_SHLIB_EXT) $@
116122
@$(DSYMUTIL) $@
117123
ifeq ($(OS), WINNT)
@@ -120,9 +126,9 @@ ifeq ($(OS), WINNT)
120126
@$(call PRINT_ANALYZE, $(OBJCOPY) $(build_libdir)/$(notdir $@).tmp.a $(STRIP_EXPORTED_FUNCS) $(build_libdir)/$(notdir $@).a && rm $(build_libdir)/$(notdir $@).tmp.a)
121127
endif
122128

123-
$(build_shlibdir)/libjulia-debug.$(JL_MAJOR_MINOR_SHLIB_EXT): $(LIB_DOBJS) $(SRCDIR)/list_strip_symbols.h | $(build_shlibdir) $(build_libdir)
129+
$(build_shlibdir)/libjulia-debug.$(JL_MAJOR_MINOR_SHLIB_EXT): $(LIB_DOBJS) $(SRCDIR)/list_strip_symbols.h $(SRCDIR)/julia.expmap | $(build_shlibdir) $(build_libdir)
124130
@$(call PRINT_LINK, $(CC) $(call IMPLIB_FLAGS,$@.tmp) $(LOADER_CFLAGS) -DLIBRARY_EXPORTS -shared $(DEBUGFLAGS) $(LIB_DOBJS) -o $@ \
125-
$(JLIBLDFLAGS) $(LOADER_LDFLAGS) $(call SONAME_FLAGS,libjulia-debug.$(JL_MAJOR_SHLIB_EXT)))
131+
$(JLIBLDFLAGS) $(LOADER_LDFLAGS) $(VERSIONSCRIPT) $(call SONAME_FLAGS,libjulia-debug.$(JL_MAJOR_SHLIB_EXT)))
126132
@$(INSTALL_NAME_CMD)libjulia-debug.$(JL_MAJOR_SHLIB_EXT) $@
127133
@$(DSYMUTIL) $@
128134
ifeq ($(OS), WINNT)
@@ -144,8 +150,14 @@ $(build_bindir)/julia$(EXE): $(EXE_OBJS) $(build_shlibdir)/libjulia.$(SHLIB_EXT)
144150
$(build_bindir)/julia-debug$(EXE): $(EXE_DOBJS) $(build_shlibdir)/libjulia-debug.$(SHLIB_EXT) | $(build_bindir)
145151
@$(call PRINT_LINK, $(CC) $(LOADER_CFLAGS) $(DEBUGFLAGS) $(EXE_DOBJS) -o $@ $(LOADER_LDFLAGS) $(RPATH) -ljulia-debug)
146152

153+
$(BUILDDIR)/julia.generated.expmap:
154+
@echo "$(DEFAULT_SYMBOL_VERSION)" > $@.tmp
155+
@echo "{ global: *; };" >> $@.tmp
156+
mv $@.tmp $@
157+
147158
clean: | $(CLEAN_TARGETS)
148159
rm -f $(BUILDDIR)/*.o $(BUILDDIR)/*.dbg.obj
149160
rm -f $(build_bindir)/julia*
161+
rm -f $(BUILDDIR)/julia.generated.expmap
150162

151163
.PHONY: clean release debug julia-release julia-debug

cli/loader_lib.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,13 @@ static void * load_library(const char * rel_path, const char * src_dir, int err)
120120
static void * lookup_symbol(const void * lib_handle, const char * symbol_name) {
121121
#ifdef _OS_WINDOWS_
122122
return GetProcAddress((HMODULE) lib_handle, symbol_name);
123+
#else
124+
#ifdef JL_SYMBOL_VERSION
125+
return dlvsym((void *)lib_handle, symbol_name, JL_SYMBOL_VERSION);
123126
#else
124127
return dlsym((void *)lib_handle, symbol_name);
125128
#endif
129+
#endif
126130
}
127131

128132
// Find the location of libjulia.

src/Makefile

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -342,10 +342,10 @@ $(FLISP_EXECUTABLE_release): $(BUILDDIR)/flisp/libflisp.a
342342
$(FLISP_EXECUTABLE_debug): $(BUILDDIR)/flisp/libflisp-debug.a
343343
$(MAKE) -C $(BUILDDIR)/flisp $(subst $(abspath $(BUILDDIR)/flisp)/,,$(abspath $(FLISP_EXECUTABLE_debug)))
344344

345-
$(BUILDDIR)/flisp/libflisp.a: $(addprefix $(SRCDIR)/flisp/,*.h *.c) $(BUILDDIR)/support/libsupport.a
345+
$(BUILDDIR)/flisp/libflisp.a: $(addprefix $(SRCDIR)/flisp/,*.h *.c) $(BUILDDIR)/support/libsupport.a $(BUILDDIR)/julia.generated.expmap
346346
$(MAKE) -C $(SRCDIR)/flisp BUILDDIR='$(abspath $(BUILDDIR)/flisp)'
347347

348-
$(BUILDDIR)/flisp/libflisp-debug.a: $(addprefix $(SRCDIR)/,flisp/*.h flisp/*.c) $(BUILDDIR)/support/libsupport-debug.a
348+
$(BUILDDIR)/flisp/libflisp-debug.a: $(addprefix $(SRCDIR)/,flisp/*.h flisp/*.c) $(BUILDDIR)/support/libsupport-debug.a $(BUILDDIR)/julia.generated.expmap
349349
$(MAKE) -C $(SRCDIR)/flisp debug BUILDDIR='$(abspath $(BUILDDIR)/flisp)'
350350

351351
$(BUILDDIR)/julia_version.h: $(JULIAHOME)/VERSION
@@ -363,13 +363,18 @@ $(BUILDDIR)/julia_version.h: $(JULIAHOME)/VERSION
363363

364364
CXXLD = $(CXX) -shared
365365

366-
$(build_shlibdir)/libjulia-internal.$(JL_MAJOR_MINOR_SHLIB_EXT): $(SRCDIR)/julia.expmap $(OBJS) $(BUILDDIR)/flisp/libflisp.a $(BUILDDIR)/support/libsupport.a $(LIBUV)
366+
$(BUILDDIR)/julia.generated.expmap: $(SRCDIR)/julia.expmap
367+
@echo "$(DEFAULT_SYMBOL_VERSION)" > $@.tmp
368+
@cat $(SRCDIR)/julia.expmap >> $@.tmp
369+
mv $@.tmp $@
370+
371+
$(build_shlibdir)/libjulia-internal.$(JL_MAJOR_MINOR_SHLIB_EXT): $(BUILDDIR)/julia.generated.expmap $(OBJS) $(BUILDDIR)/flisp/libflisp.a $(BUILDDIR)/support/libsupport.a $(LIBUV)
367372
@$(call PRINT_LINK, $(CXXLD) $(call IMPLIB_FLAGS,$@) $(JCXXFLAGS) $(JL_CXXFLAGS) $(CXXLDFLAGS) $(SHIPFLAGS) $(OBJS) $(RPATH_LIB) -o $@ \
368373
$(JLDFLAGS) $(JLIBLDFLAGS) $(RT_RELEASE_LIBS) $(call SONAME_FLAGS,libjulia-internal.$(JL_MAJOR_SHLIB_EXT)))
369374
@$(INSTALL_NAME_CMD)libjulia-internal.$(SHLIB_EXT) $@
370375
$(DSYMUTIL) $@
371376

372-
$(build_shlibdir)/libjulia-internal-debug.$(JL_MAJOR_MINOR_SHLIB_EXT): $(SRCDIR)/julia.expmap $(DOBJS) $(BUILDDIR)/flisp/libflisp-debug.a $(BUILDDIR)/support/libsupport-debug.a $(LIBUV)
377+
$(build_shlibdir)/libjulia-internal-debug.$(JL_MAJOR_MINOR_SHLIB_EXT): $(BUILDDIR)/julia.generated.expmap $(DOBJS) $(BUILDDIR)/flisp/libflisp-debug.a $(BUILDDIR)/support/libsupport-debug.a $(LIBUV)
373378
@$(call PRINT_LINK, $(CXXLD) $(call IMPLIB_FLAGS,$@) $(JCXXFLAGS) $(JL_CXXFLAGS) $(CXXLDFLAGS) $(DEBUGFLAGS) $(DOBJS) $(RPATH_LIB) -o $@ \
374379
$(JLDFLAGS) $(JLIBLDFLAGS) $(RT_DEBUG_LIBS) $(call SONAME_FLAGS,libjulia-internal-debug.$(JL_MAJOR_SHLIB_EXT)))
375380
@$(INSTALL_NAME_CMD)libjulia-internal-debug.$(SHLIB_EXT) $@
@@ -391,13 +396,13 @@ libjulia-internal-release: $(build_shlibdir)/libjulia-internal.$(JL_MAJOR_MINOR_
391396
libjulia-internal-debug: $(build_shlibdir)/libjulia-internal-debug.$(JL_MAJOR_MINOR_SHLIB_EXT)
392397
libjulia-internal-debug libjulia-internal-release: $(PUBLIC_HEADER_TARGETS)
393398

394-
$(build_shlibdir)/libjulia-codegen.$(JL_MAJOR_MINOR_SHLIB_EXT): $(SRCDIR)/julia.expmap $(CODEGEN_OBJS) $(BUILDDIR)/support/libsupport.a $(build_shlibdir)/libjulia-internal.$(JL_MAJOR_MINOR_SHLIB_EXT)
399+
$(build_shlibdir)/libjulia-codegen.$(JL_MAJOR_MINOR_SHLIB_EXT): $(BUILDDIR)/julia.generated.expmap $(CODEGEN_OBJS) $(BUILDDIR)/support/libsupport.a $(build_shlibdir)/libjulia-internal.$(JL_MAJOR_MINOR_SHLIB_EXT)
395400
@$(call PRINT_LINK, $(CXXLD) $(call IMPLIB_FLAGS,$@) $(JCXXFLAGS) $(JL_CXXFLAGS) $(CXXLDFLAGS) $(SHIPFLAGS) $(CODEGEN_OBJS) $(RPATH_LIB) -o $@ \
396401
$(JLDFLAGS) $(JLIBLDFLAGS) $(CG_RELEASE_LIBS) $(call SONAME_FLAGS,libjulia-codegen.$(JL_MAJOR_SHLIB_EXT)))
397402
@$(INSTALL_NAME_CMD)libjulia-codegen.$(SHLIB_EXT) $@
398403
$(DSYMUTIL) $@
399404

400-
$(build_shlibdir)/libjulia-codegen-debug.$(JL_MAJOR_MINOR_SHLIB_EXT): $(SRCDIR)/julia.expmap $(CODEGEN_DOBJS) $(BUILDDIR)/support/libsupport-debug.a $(build_shlibdir)/libjulia-internal-debug.$(JL_MAJOR_MINOR_SHLIB_EXT)
405+
$(build_shlibdir)/libjulia-codegen-debug.$(JL_MAJOR_MINOR_SHLIB_EXT): $(BUILDDIR)/julia.generated.expmap $(CODEGEN_DOBJS) $(BUILDDIR)/support/libsupport-debug.a $(build_shlibdir)/libjulia-internal-debug.$(JL_MAJOR_MINOR_SHLIB_EXT)
401406
@$(call PRINT_LINK, $(CXXLD) $(call IMPLIB_FLAGS,$@) $(JCXXFLAGS) $(JL_CXXFLAGS) $(CXXLDFLAGS) $(DEBUGFLAGS) $(CODEGEN_DOBJS) $(RPATH_LIB) -o $@ \
402407
$(JLDFLAGS) $(JLIBLDFLAGS) $(CG_DEBUG_LIBS) $(call SONAME_FLAGS,libjulia-codegen-debug.$(JL_MAJOR_SHLIB_EXT)))
403408
@$(INSTALL_NAME_CMD)libjulia-codegen-debug.$(SHLIB_EXT) $@
@@ -421,6 +426,7 @@ clean:
421426
-rm -fr $(build_shlibdir)/libjulia-internal* $(build_shlibdir)/libjulia-codegen* $(build_shlibdir)/libccalltest* $(build_shlibdir)/libllvmcalltest*
422427
-rm -f $(BUILDDIR)/julia_flisp.boot $(BUILDDIR)/julia_flisp.boot.inc $(BUILDDIR)/jl_internal_funcs.inc
423428
-rm -f $(BUILDDIR)/*.dbg.obj $(BUILDDIR)/*.o $(BUILDDIR)/*.dwo $(BUILDDIR)/*.$(SHLIB_EXT) $(BUILDDIR)/*.a $(BUILDDIR)/*.h.gen
429+
-rm -f $(BUILDDIR)/julia.generated.expmap
424430
-rm -f $(BUILDDIR)/julia_version.h
425431

426432
clean-flisp:

src/ccall.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,14 @@ static void interpret_symbol_arg(jl_codectx_t &ctx, native_sym_arg_t &out, jl_va
643643
void *symaddr;
644644
std::string iname("i");
645645
iname += f_name;
646+
#ifdef JL_SYMBOL_VERSION
647+
if (jl_dlvsym(jl_libjulia_internal_handle, iname.c_str(), JL_SYMBOL_VERSION, &symaddr, 0)) {
648+
f_name = jl_symbol_name(jl_symbol(iname.c_str()));
649+
f_version = JL_SYMBOL_VERSION;
650+
} else if (jl_dlvsym(jl_libjulia_internal_handle, f_name, JL_SYMBOL_VERSION, &symaddr, 0)) {
651+
f_version = JL_SYMBOL_VERSION;
652+
}
653+
#else
646654
if (jl_dlsym(jl_libjulia_internal_handle, iname.c_str(), &symaddr, 0)) {
647655
#ifdef _OS_WINDOWS_
648656
f_lib = JL_LIBJULIA_INTERNAL_DL_LIBNAME;
@@ -653,6 +661,7 @@ static void interpret_symbol_arg(jl_codectx_t &ctx, native_sym_arg_t &out, jl_va
653661
else {
654662
f_lib = jl_dlfind_win32(f_name);
655663
}
664+
#endif
656665
#endif
657666
}
658667
}

0 commit comments

Comments
 (0)