Skip to content

Commit

Permalink
Makefile: Add support for symbol versioning (ON by default) (JuliaLan…
Browse files Browse the repository at this point in the history
…g#49012)

This enables symbol versioning by default, with the version string
`JL_LIBJULIA_1.10` (the version suffix is the SOMAJOR in general). This
will configure the linker to assign the specified version string to all
Julia-exported symbols.

As part of this change, the SOMAJOR has also been changed to be
MAJOR.MINOR instead of just MAJOR. This is required to allow multiple
minor releases of libjulia to live side-by-side in the same process.

`SYMBOL_VERSION_SUFFIX` is provided to allow appending an additional
unique "salt" to SOMAJOR, which can be helpful for creating template
binaries that will be "version-stamped" after distribution.

Correctly compute `LLVM_SHLIB_SYMBOL_VERSION` using
`llvm-config`, so USE_SYSTEM_LLVM should continue to work.
  • Loading branch information
topolarity authored Jun 29, 2023
1 parent 6174056 commit 6d400e4
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 25 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
/source-dist.tmp
/source-dist.tmp1

*.expmap
*.exe
*.dll
*.dwo
Expand Down
24 changes: 13 additions & 11 deletions Make.inc
Original file line number Diff line number Diff line change
Expand Up @@ -188,15 +188,17 @@ JULIA_MINOR_VERSION := $(shell echo $(JULIA_VERSION) | cut -d'-' -f 1 | cut -d'.
JULIA_PATCH_VERSION := $(shell echo $(JULIA_VERSION) | cut -d'-' -f 1 | cut -d'+' -f 1 | cut -d'.' -f 3)

# libjulia's SONAME will follow the format libjulia.so.$(SOMAJOR). Before v1.0.0,
# SOMAJOR will be a two-decimal value, e.g. libjulia.so.0.5, whereas at and beyond
# v1.0.0, SOMAJOR will be simply the major version number, e.g. libjulia.so.1
# somajor was a two-decimal value (e.g. libjulia.so.0.5). During v1.0.x - v1.9.x,
# somajor was simply the major version number (e.g. libjulia.so.1). Starting in
# v1.10.0, somajor is major.minor again (e.g. libjulia.so.1.10)
# The file itself will ultimately symlink to libjulia.so.$(SOMAJOR).$(SOMINOR)
ifeq ($(JULIA_MAJOR_VERSION),0)
SOMAJOR := $(JULIA_MAJOR_VERSION).$(JULIA_MINOR_VERSION)
SOMINOR := $(JULIA_PATCH_VERSION)
else
SOMAJOR := $(JULIA_MAJOR_VERSION)
SOMINOR := $(JULIA_MINOR_VERSION)

# This suffix affects libjulia's SONAME and the symbol version associated with
# all of its exported symbols.
ifdef SYMBOL_VERSION_SUFFIX
SOMAJOR := $(SOMAJOR)_$(SYMBOL_VERSION_SUFFIX)
endif

ifneq ($(NO_GIT), 1)
Expand Down Expand Up @@ -488,7 +490,7 @@ endif

JCFLAGS_COMMON := -std=gnu11 -pipe $(fPIC) -fno-strict-aliasing -D_FILE_OFFSET_BITS=64
JCFLAGS_CLANG := $(JCFLAGS_COMMON)
JCFLAGS_GCC := $(JCFLAGS_COMMON)
JCFLAGS_GCC := $(JCFLAGS_COMMON) -fno-gnu-unique

# AArch64 needs this flag to generate the .eh_frame used by libunwind
JCPPFLAGS_COMMON := -fasynchronous-unwind-tables
Expand All @@ -497,7 +499,7 @@ JCPPFLAGS_GCC := $(JCPPFLAGS_COMMON)

JCXXFLAGS_COMMON := -pipe $(fPIC) -fno-rtti -std=c++14
JCXXFLAGS_CLANG := $(JCXXFLAGS_COMMON) -pedantic
JCXXFLAGS_GCC := $(JCXXFLAGS_COMMON)
JCXXFLAGS_GCC := $(JCXXFLAGS_COMMON) -fno-gnu-unique

DEBUGFLAGS_COMMON := -O0 -DJL_DEBUG_BUILD -fstack-protector
DEBUGFLAGS_CLANG := $(DEBUGFLAGS_COMMON) -g
Expand Down Expand Up @@ -1317,7 +1319,7 @@ ifeq (supported, $(shell echo $(IFUNC_DETECT_SRC) | $(CC) -Werror -x c - -S -o /
JCPPFLAGS += -DJULIA_HAS_IFUNC_SUPPORT=1
endif
JLDFLAGS += -Wl,-Bdynamic
OSLIBS += -Wl,--version-script=$(JULIAHOME)/src/julia.expmap
OSLIBS += -Wl,--version-script=$(BUILDROOT)/src/julia.expmap
ifneq ($(SANITIZE),1)
JLDFLAGS += -Wl,-no-undefined
endif
Expand All @@ -1342,7 +1344,7 @@ OSLIBS += -lelf -lkvm -lrt -lpthread -latomic
# See #21788
OSLIBS += -lgcc_s

OSLIBS += -Wl,--export-dynamic -Wl,--version-script=$(JULIAHOME)/src/julia.expmap \
OSLIBS += -Wl,--export-dynamic -Wl,--version-script=$(BUILDROOT)/src/julia.expmap \
$(NO_WHOLE_ARCHIVE)
endif

Expand All @@ -1357,7 +1359,7 @@ endif

ifeq ($(OS), WINNT)
HAVE_SSP := 1
OSLIBS += -Wl,--export-all-symbols -Wl,--version-script=$(JULIAHOME)/src/julia.expmap \
OSLIBS += -Wl,--export-all-symbols -Wl,--version-script=$(BUILDROOT)/src/julia.expmap \
$(NO_WHOLE_ARCHIVE) -lpsapi -lkernel32 -lws2_32 -liphlpapi -lwinmm -ldbghelp -luserenv -lsecur32 -latomic
JLDFLAGS += -Wl,--stack,8388608
ifeq ($(ARCH),i686)
Expand Down
16 changes: 12 additions & 4 deletions cli/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,18 @@ julia-debug: $(build_bindir)/julia-debug$(EXE)
libjulia-release: $(build_shlibdir)/libjulia.$(SHLIB_EXT)
libjulia-debug: $(build_shlibdir)/libjulia-debug.$(SHLIB_EXT)

ifneq (,$(filter $(OS), Linux FreeBSD))
VERSIONSCRIPT := -Wl,--version-script=$(BUILDDIR)/julia.expmap
endif

ifeq ($(OS),WINNT)
# On Windows we need to strip out exported functions from the generated import library.
STRIP_EXPORTED_FUNCS := $(shell $(CPP_STDOUT) -I$(JULIAHOME)/src $(SRCDIR)/list_strip_symbols.h)
endif

$(build_shlibdir)/libjulia.$(JL_MAJOR_MINOR_SHLIB_EXT): $(LIB_OBJS) $(SRCDIR)/list_strip_symbols.h | $(build_shlibdir) $(build_libdir)
$(build_shlibdir)/libjulia.$(JL_MAJOR_MINOR_SHLIB_EXT): $(LIB_OBJS) $(SRCDIR)/list_strip_symbols.h $(BUILDDIR)/julia.expmap | $(build_shlibdir) $(build_libdir)
@$(call PRINT_LINK, $(CC) $(call IMPLIB_FLAGS,$@.tmp) $(LOADER_CFLAGS) -shared $(SHIPFLAGS) $(LIB_OBJS) -o $@ \
$(JLIBLDFLAGS) $(LOADER_LDFLAGS) $(call SONAME_FLAGS,libjulia.$(JL_MAJOR_SHLIB_EXT)))
$(JLIBLDFLAGS) $(LOADER_LDFLAGS) $(VERSIONSCRIPT) $(call SONAME_FLAGS,libjulia.$(JL_MAJOR_SHLIB_EXT)))
@$(INSTALL_NAME_CMD)libjulia.$(JL_MAJOR_SHLIB_EXT) $@
@$(DSYMUTIL) $@
ifeq ($(OS), WINNT)
Expand All @@ -120,9 +124,9 @@ ifeq ($(OS), WINNT)
@$(call PRINT_ANALYZE, $(OBJCOPY) $(build_libdir)/$(notdir $@).tmp.a $(STRIP_EXPORTED_FUNCS) $(build_libdir)/$(notdir $@).a && rm $(build_libdir)/$(notdir $@).tmp.a)
endif

$(build_shlibdir)/libjulia-debug.$(JL_MAJOR_MINOR_SHLIB_EXT): $(LIB_DOBJS) $(SRCDIR)/list_strip_symbols.h | $(build_shlibdir) $(build_libdir)
$(build_shlibdir)/libjulia-debug.$(JL_MAJOR_MINOR_SHLIB_EXT): $(LIB_DOBJS) $(SRCDIR)/list_strip_symbols.h $(BUILDDIR)/julia.expmap | $(build_shlibdir) $(build_libdir)
@$(call PRINT_LINK, $(CC) $(call IMPLIB_FLAGS,$@.tmp) $(LOADER_CFLAGS) -shared $(DEBUGFLAGS) $(LIB_DOBJS) -o $@ \
$(JLIBLDFLAGS) $(LOADER_LDFLAGS) $(call SONAME_FLAGS,libjulia-debug.$(JL_MAJOR_SHLIB_EXT)))
$(JLIBLDFLAGS) $(LOADER_LDFLAGS) $(VERSIONSCRIPT) $(call SONAME_FLAGS,libjulia-debug.$(JL_MAJOR_SHLIB_EXT)))
@$(INSTALL_NAME_CMD)libjulia-debug.$(JL_MAJOR_SHLIB_EXT) $@
@$(DSYMUTIL) $@
ifeq ($(OS), WINNT)
Expand All @@ -144,8 +148,12 @@ $(build_bindir)/julia$(EXE): $(EXE_OBJS) $(build_shlibdir)/libjulia.$(SHLIB_EXT)
$(build_bindir)/julia-debug$(EXE): $(EXE_DOBJS) $(build_shlibdir)/libjulia-debug.$(SHLIB_EXT) | $(build_bindir)
@$(call PRINT_LINK, $(CC) $(LOADER_CFLAGS) $(DEBUGFLAGS) $(EXE_DOBJS) -o $@ $(LOADER_LDFLAGS) $(RPATH) -ljulia-debug)

$(BUILDDIR)/julia.expmap: $(SRCDIR)/julia.expmap.in
sed <'$<' >'$@' -e 's/@JULIA_SHLIB_SYMBOL_VERSION@/JL_LIBJULIA_$(SOMAJOR)/'

clean: | $(CLEAN_TARGETS)
rm -f $(BUILDDIR)/*.o $(BUILDDIR)/*.dbg.obj
rm -f $(build_bindir)/julia*
rm -f $(BUILDDIR)/julia.expmap

.PHONY: clean release debug julia-release julia-debug
1 change: 1 addition & 0 deletions cli/julia.expmap.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@JULIA_SHLIB_SYMBOL_VERSION@ { global: *; };
1 change: 1 addition & 0 deletions deps/llvm-ver.make
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ LLVM_SHARED_LIB_VER_SUFFIX := $(LLVM_VER_MAJ)jl
# e.g.: "libLLVM-14jl"
LLVM_SHARED_LIB_NAME := libLLVM-$(LLVM_SHARED_LIB_VER_SUFFIX)
LLVM_SHARED_LINK_FLAG := -lLLVM-$(LLVM_SHARED_LIB_VER_SUFFIX)
LLVM_SHLIB_SYMBOL_VERSION := JL_LLVM_$(LLVM_VER_MAJ).$(LLVM_VER_MIN)
20 changes: 14 additions & 6 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ ifeq ($(JULIACODEGEN),LLVM)
ifneq ($(USE_SYSTEM_LLVM),0)
# USE_SYSTEM_LLVM != 0
CG_LLVMLINK += $(LLVM_LDFLAGS) $(shell $(LLVM_CONFIG_HOST) --libs --system-libs)
LLVM_SHLIB_SYMBOL_VERSION := $(shell nm -D --with-symbol-versions $(shell $(LLVM_CONFIG_HOST) --libfiles --link-shared | awk '{print $1; exit}') | \
grep _ZN4llvm3Any6TypeId | head -n 1 | sed -e 's/.*@//')

# HACK: llvm-config doesn't correctly point to shared libs on all platforms
# https://github.com/JuliaLang/julia/issues/29981
else
Expand Down Expand Up @@ -344,10 +347,10 @@ $(FLISP_EXECUTABLE_release): $(BUILDDIR)/flisp/libflisp.a
$(FLISP_EXECUTABLE_debug): $(BUILDDIR)/flisp/libflisp-debug.a
$(MAKE) -C $(BUILDDIR)/flisp $(subst $(abspath $(BUILDDIR)/flisp)/,,$(abspath $(FLISP_EXECUTABLE_debug)))

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

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

$(BUILDDIR)/julia_version.h: $(JULIAHOME)/VERSION
Expand All @@ -365,13 +368,17 @@ $(BUILDDIR)/julia_version.h: $(JULIAHOME)/VERSION

CXXLD = $(CXX) -shared

$(build_shlibdir)/libjulia-internal.$(JL_MAJOR_MINOR_SHLIB_EXT): $(SRCDIR)/julia.expmap $(OBJS) $(BUILDDIR)/flisp/libflisp.a $(BUILDDIR)/support/libsupport.a $(LIBUV)
$(BUILDDIR)/julia.expmap: $(SRCDIR)/julia.expmap.in
sed <'$<' >'$@' -e "s/@JULIA_SHLIB_SYMBOL_VERSION@/JL_LIBJULIA_$(SOMAJOR)/" \
-e "s/@LLVM_SHLIB_SYMBOL_VERSION@/$(LLVM_SHLIB_SYMBOL_VERSION)/"

$(build_shlibdir)/libjulia-internal.$(JL_MAJOR_MINOR_SHLIB_EXT): $(BUILDDIR)/julia.expmap $(OBJS) $(BUILDDIR)/flisp/libflisp.a $(BUILDDIR)/support/libsupport.a $(LIBUV)
@$(call PRINT_LINK, $(CXXLD) $(call IMPLIB_FLAGS,$@) $(JCXXFLAGS) $(JL_CXXFLAGS) $(CXXLDFLAGS) $(SHIPFLAGS) $(OBJS) $(RPATH_LIB) -o $@ \
$(JLDFLAGS) $(JLIBLDFLAGS) $(RT_RELEASE_LIBS) $(call SONAME_FLAGS,libjulia-internal.$(JL_MAJOR_SHLIB_EXT)))
@$(INSTALL_NAME_CMD)libjulia-internal.$(SHLIB_EXT) $@
$(DSYMUTIL) $@

$(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)
$(build_shlibdir)/libjulia-internal-debug.$(JL_MAJOR_MINOR_SHLIB_EXT): $(BUILDDIR)/julia.expmap $(DOBJS) $(BUILDDIR)/flisp/libflisp-debug.a $(BUILDDIR)/support/libsupport-debug.a $(LIBUV)
@$(call PRINT_LINK, $(CXXLD) $(call IMPLIB_FLAGS,$@) $(JCXXFLAGS) $(JL_CXXFLAGS) $(CXXLDFLAGS) $(DEBUGFLAGS) $(DOBJS) $(RPATH_LIB) -o $@ \
$(JLDFLAGS) $(JLIBLDFLAGS) $(RT_DEBUG_LIBS) $(call SONAME_FLAGS,libjulia-internal-debug.$(JL_MAJOR_SHLIB_EXT)))
@$(INSTALL_NAME_CMD)libjulia-internal-debug.$(SHLIB_EXT) $@
Expand All @@ -393,13 +400,13 @@ libjulia-internal-release: $(build_shlibdir)/libjulia-internal.$(JL_MAJOR_MINOR_
libjulia-internal-debug: $(build_shlibdir)/libjulia-internal-debug.$(JL_MAJOR_MINOR_SHLIB_EXT)
libjulia-internal-debug libjulia-internal-release: $(PUBLIC_HEADER_TARGETS)

$(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)
$(build_shlibdir)/libjulia-codegen.$(JL_MAJOR_MINOR_SHLIB_EXT): $(BUILDDIR)/julia.expmap $(CODEGEN_OBJS) $(BUILDDIR)/support/libsupport.a $(build_shlibdir)/libjulia-internal.$(JL_MAJOR_MINOR_SHLIB_EXT)
@$(call PRINT_LINK, $(CXXLD) $(call IMPLIB_FLAGS,$@) $(JCXXFLAGS) $(JL_CXXFLAGS) $(CXXLDFLAGS) $(SHIPFLAGS) $(CODEGEN_OBJS) $(RPATH_LIB) -o $@ \
$(JLDFLAGS) $(JLIBLDFLAGS) $(CG_RELEASE_LIBS) $(call SONAME_FLAGS,libjulia-codegen.$(JL_MAJOR_SHLIB_EXT)))
@$(INSTALL_NAME_CMD)libjulia-codegen.$(SHLIB_EXT) $@
$(DSYMUTIL) $@

$(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)
$(build_shlibdir)/libjulia-codegen-debug.$(JL_MAJOR_MINOR_SHLIB_EXT): $(BUILDDIR)/julia.expmap $(CODEGEN_DOBJS) $(BUILDDIR)/support/libsupport-debug.a $(build_shlibdir)/libjulia-internal-debug.$(JL_MAJOR_MINOR_SHLIB_EXT)
@$(call PRINT_LINK, $(CXXLD) $(call IMPLIB_FLAGS,$@) $(JCXXFLAGS) $(JL_CXXFLAGS) $(CXXLDFLAGS) $(DEBUGFLAGS) $(CODEGEN_DOBJS) $(RPATH_LIB) -o $@ \
$(JLDFLAGS) $(JLIBLDFLAGS) $(CG_DEBUG_LIBS) $(call SONAME_FLAGS,libjulia-codegen-debug.$(JL_MAJOR_SHLIB_EXT)))
@$(INSTALL_NAME_CMD)libjulia-codegen-debug.$(SHLIB_EXT) $@
Expand Down Expand Up @@ -429,6 +436,7 @@ clean:
-rm -fr $(build_shlibdir)/libjulia-internal* $(build_shlibdir)/libjulia-codegen* $(build_shlibdir)/libccalltest* $(build_shlibdir)/libllvmcalltest*
-rm -f $(BUILDDIR)/julia_flisp.boot $(BUILDDIR)/julia_flisp.boot.inc $(BUILDDIR)/jl_internal_funcs.inc
-rm -f $(BUILDDIR)/*.dbg.obj $(BUILDDIR)/*.o $(BUILDDIR)/*.dwo $(BUILDDIR)/*.$(SHLIB_EXT) $(BUILDDIR)/*.a $(BUILDDIR)/*.h.gen
-rm -f $(BUILDDIR)/julia.expmap
-rm -f $(BUILDDIR)/julia_version.h

clean-flisp:
Expand Down
11 changes: 7 additions & 4 deletions src/julia.expmap → src/julia.expmap.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
@JULIA_SHLIB_SYMBOL_VERSION@ {
global:
pthread*;
__stack_chk_guard;
Expand Down Expand Up @@ -35,13 +35,16 @@
JLJIT*;
llvmGetPassPluginInfo;

/* Make visible so that linker will merge duplicate definitions across DSO boundaries */
_ZN4llvm3Any6TypeId*;

/* freebsd */
environ;
__progname;

local:
*;
};

@LLVM_SHLIB_SYMBOL_VERSION@ {
global:
/* Make visible so that linker will merge duplicate definitions across DSO boundaries */
_ZN4llvm3Any6TypeId*;
};

0 comments on commit 6d400e4

Please sign in to comment.