From 7e916f4ce6d481025007732d7ff59d398380912c Mon Sep 17 00:00:00 2001
From: Alexandre Janniaux <ajanni@videolabs.io>
Date: Fri, 28 May 2021 11:29:23 +0200
Subject: [PATCH] configure.ac: add linker test for dynamic_lookup

Check that the linker features from Darwin ld64 for marking a symbol as
undefined (in the context of two-level namespace, it means that it can
be looked up in other namespaces) are usable. If they are not, the
static module bank is disabled since it depends on either static linkage
(which is obviously not affected by this option) or more generally the
ability to look up the static module bank symbol in a different linkage
unit/namespace.

In particular, they are currently not usable when enabling bitcode
through LDFLAGS+=" -fembed-bitcode" and compiling dynamically. On tvOS,
bitcode is mandatory, so supporting compiling with bitcode but without
the static module bank seems a rather good compromise given that it was
disabled anyway previously.

In static builds (the current shipping target for iOS and tvOS), the
static module bank stays enabled by the !HAVE_DYNAMIC_PLUGINS check.
---
 configure.ac       | 19 ++++++++++++++++++-
 src/Makefile.am    |  2 +-
 src/modules/bank.c |  4 +++-
 3 files changed, 22 insertions(+), 3 deletions(-)

diff --git a/configure.ac b/configure.ac
index e518150c5a06..f3c1eb1be1d4 100644
--- a/configure.ac
+++ b/configure.ac
@@ -205,9 +205,26 @@ case "${host_os}" in
     AX_APPEND_FLAG([-Werror=partial-availability], [OBJCFLAGS])
     AX_APPEND_FLAG([-Wl,-headerpad_max_install_names], [LDFLAGS])
 
-    VLC_ADD_LIBS([libvlc vlc],[-Wl,-undefined,dynamic_lookup])
     VLC_ADD_LIBS([libvlccore],[-Wl,-framework,CoreFoundation])
 
+    dnl This is not supported when bitcode is enabled. In that case, we need
+    dnl to disable the static bank loader.
+    AC_MSG_CHECKING([if -Wl,-U is allowed])
+    VLC_SAVE_FLAGS
+    AX_APPEND_FLAG([LDFLAGS],[-Wl,-U,_my_array -no-undefined])
+    AC_LINK_IFELSE(
+       [AC_LANG_PROGRAM([], [dnl
+__attribute__((visibility("default"))) extern int my_array[];
+__attribute__((visibility("default"))) int foo() { return my_array[0]; }
+])],
+       [
+        VLC_ADD_LDFLAGS([libvlccore],[-Wl,-U,_vlc_static_modules])
+        VLC_ADD_LDFLAGS([libvlc vlc],[-Wl,-undefined,dynamic_lookup])
+        VLC_ADD_CPPFLAGS([libvlccore],[-DHAVE_DYLIB_DYNAMIC_LOOKUP=1])
+        AC_MSG_RESULT([yes])
+       ],[AC_MSG_RESULT([no])])
+    VLC_RESTORE_FLAGS
+
     AC_EGREP_CPP(yes,
             [#import <TargetConditionals.h>
              #if TARGET_OS_IPHONE
diff --git a/src/Makefile.am b/src/Makefile.am
index eea01fbf4e4a..6c438122baf5 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -566,7 +566,7 @@ $(libvlccore_la_OBJECTS): libvlccore_objc.la
 libvlccore_objc_la_OBJCFLAGS = $(AM_OBJCFLAGS) -fobjc-arc
 libvlccore_objc_la_LDFLAGS = -static
 libvlccore_la_LIBADD += libvlccore_objc.la
-libvlccore_la_LDFLAGS +=  -Wl,-framework,Foundation -Xlinker -install_name -Xlinker @rpath/libvlccore.dylib -Wl,-U,_vlc_static_modules
+libvlccore_la_LDFLAGS +=  -Wl,-framework,Foundation -Xlinker -install_name -Xlinker @rpath/libvlccore.dylib
 endif
 
 # iOS and tvOS applications cannot install global shared libraries and
diff --git a/src/modules/bank.c b/src/modules/bank.c
index 76bf56be3636..7f8b0fd8fada 100644
--- a/src/modules/bank.c
+++ b/src/modules/bank.c
@@ -179,7 +179,9 @@ static vlc_plugin_t *module_InitStatic(vlc_plugin_cb entry)
  * not provided at runtime. However, although __MACH__ implies the same runtime
  * consequences for weak linking, it will still require the definition to exist
  * at build time. To workaround this, we add -Wl,-U,vlc_static_modules. */
-#if defined(__ELF__) || defined(__MACH__) || !HAVE_DYNAMIC_PLUGINS
+#if defined(__ELF__) \
+    || (defined(__MACH__) && defined(HAVE_DYLIB_DYNAMIC_LOOKUP)) \
+    || !HAVE_DYNAMIC_PLUGINS
 VLC_WEAK
 extern vlc_plugin_cb vlc_static_modules[];
 
-- 
GitLab