Commit e7cf8c5785b14fc8fbd37bb665a5f9a4f28c7888

Guillem Jover 2021-11-28T22:44:08

Switch md5 compatibility logic back to direct linking When using the recent dlsym() based wrapper, we are not requiring any symbol from libmd, as we resolve those dynamically at run-time. We were ending up linking against libmd because in another part of the code we require (depending on the architecture) the SHA512 functions for the getentropy() local implementation. But that function might be provided by the system libc on some systems, which means we end up not linking against libmd at all. To solve this we go back to the previous simpler solution of linking directly, which had the main drawback of then making programs fail to link when not specifying -lmd (on platforms that need it). And then switch the .so link point from a symlink to a linker script, so that we can inject the -lmd library as-needed. This is similar to what glibc is doing. Fixes: commit 31f034e3862debda8615a449b1c11c4d6920dcc7

diff --git a/.gitignore b/.gitignore
index 460667b..4018a03 100644
--- a/.gitignore
+++ b/.gitignore
@@ -20,5 +20,6 @@ autom4te.cache/
 build-aux/
 configure
 config.*
+format.ld
 libtool
 stamp-h1
diff --git a/configure.ac b/configure.ac
index f40b1af..f0ea291 100644
--- a/configure.ac
+++ b/configure.ac
@@ -101,16 +101,10 @@ AM_CONDITIONAL([HAVE_LIBTESTU01],
                [test "x$ac_cv_lib_testu01_unif01_CreateExternGenBits" = "xyes"])
 
 saved_LIBS="$LIBS"
-AC_SEARCH_LIBS([dlsym], [dl], [
-  AS_IF([test "x$ac_cv_search_dlsym" != "xnone required"], [
-    LIBBSD_LIBS="$LIBBSD_LIBS $ac_cv_search_dlsym"
-  ])
-], [
-  AC_MSG_ERROR([cannot find required dlsym function])
-])
 AC_SEARCH_LIBS([MD5Update], [md], [
   AS_IF([test "x$ac_cv_search_MD5Update" != "xnone required"], [
     MD5_LIBS="$MD5_LIBS $ac_cv_search_MD5Update"
+    need_transparent_libmd=yes
   ])
 ], [
   AC_MSG_ERROR([cannot find required MD5 functions in libc or libmd])
@@ -124,6 +118,9 @@ AC_SEARCH_LIBS([SHA512Update], [md], [
 ])
 LIBS="$saved_LIBS"
 
+AM_CONDITIONAL([NEED_TRANSPARENT_LIBMD],
+               [test "x$need_transparent_libmd" = "xyes"])
+
 is_windows=no
 AS_CASE([$host_os],
   [*-gnu*], [
diff --git a/src/Makefile.am b/src/Makefile.am
index d3f0162..2aa5f5b 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -25,6 +25,7 @@ libbsd_la_included_sources = \
 	getentropy_win.c \
 	$(nil)
 
+CLEANFILES =
 EXTRA_DIST = \
 	libbsd.map \
 	libbsd.pc.in \
@@ -125,22 +126,51 @@ libbsd_la_SOURCES += \
 	$(nil)
 endif
 
+if NEED_TRANSPARENT_LIBMD
+CLEANFILES += \
+	format.ld \
+	# EOL
+endif
+
 libbsd_ctor_a_SOURCES = \
 	setproctitle_ctor.c \
 	$(nil)
 
+if NEED_TRANSPARENT_LIBMD
+TRANSPARENT_LIBMD_DEPENDS = format.ld
+
+format.ld:
+	$(CC) -shared -nostdlib -nostartfiles -x assembler /dev/null -o $@.so
+	objdump -f $@.so | sed -n 's/.*file format \(.*\)/OUTPUT_FORMAT(\1)/;T;p' > $@
+	rm -f $@.so
+endif
+
 runtimelibdir = $(libdir)
 
-install-exec-hook:
+install-exec-hook: $(TRANSPARENT_LIBMD_DEPENDS)
 	if [ "$(libdir)" != "$(runtimelibdir)" ]; then \
 		$(MKDIR_P) $(DESTDIR)$(runtimelibdir); \
 		mv $(DESTDIR)$(libdir)/libbsd*.so.* \
 		   $(DESTDIR)$(runtimelibdir)/; \
+	fi
+if NEED_TRANSPARENT_LIBMD
+# The "GNU ld script" magic is required so that GNU ldconfig does not complain
+# about an unknown format file.
+	soname=`readlink $(DESTDIR)$(libdir)/libbsd.so`; \
+	$(RM) $(DESTDIR)$(libdir)/libbsd.so; \
+	(echo '/* GNU ld script'; \
+	 echo ' * The MD5 functions are provided by the libmd library. */'; \
+	 cat format.ld; \
+	 echo "GROUP($(runtimelibdir)/$$soname AS_NEEDED($(MD5_LIBS)))"; \
+	)>$(DESTDIR)$(libdir)/libbsd.so
+else
+	if [ "$(libdir)" != "$(runtimelibdir)" ]; then \
 		soname=`readlink $(DESTDIR)$(libdir)/libbsd.so`; \
 		sorelprefix=`echo $(libdir) | $(SED) -r -e 's:(^/)?[^/]+:..:g'`; \
 		ln -sf $$sorelprefix$(runtimelibdir)/$$soname \
 		       $(DESTDIR)$(libdir)/libbsd.so; \
 	fi
+endif
 
 uninstall-hook:
 	rm -f $(DESTDIR)$(runtimelibdir)/libbsd*.so*
diff --git a/src/md5.c b/src/md5.c
index 00769aa..1053fcd 100644
--- a/src/md5.c
+++ b/src/md5.c
@@ -24,119 +24,88 @@
  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-#include <stddef.h>
-#include <stdlib.h>
-#include <dlfcn.h>
 #include <md5.h>
 #include "local-link.h"
 
-static void (*libmd_MD5Init)(MD5_CTX *);
-static void (*libmd_MD5Update)(MD5_CTX *, const uint8_t *, size_t);
-static void (*libmd_MD5Pad)(MD5_CTX *);
-static void (*libmd_MD5Final)(uint8_t [MD5_DIGEST_LENGTH], MD5_CTX *);
-static void (*libmd_MD5Transform)(uint32_t [4], const uint8_t [MD5_BLOCK_LENGTH]);
-static char *(*libmd_MD5End)(MD5_CTX *, char *);
-static char *(*libmd_MD5File)(const char *, char *);
-static char *(*libmd_MD5FileChunk)(const char *, char *, off_t, off_t);
-static char *(*libmd_MD5Data)(const uint8_t *, size_t, char *);
-
-static void *
-libmd_loader(const char *symbol)
-{
-	void *func;
-
-	func = dlsym(RTLD_NEXT, symbol);
-	if (func == NULL) {
-		fprintf(stderr,
-		        "libbsd: cannot find wrapped symbol %s in libc or libmd\n",
-		        symbol);
-		abort();
-	}
-
-	return func;
-}
-
-#define libmd_wrapper(symbol) \
-	if (libmd_ ## symbol == NULL) \
-		libmd_ ## symbol = libmd_loader(#symbol)
+#pragma GCC diagnostic ignored "-Wmissing-prototypes"
 
 void
-MD5Init(MD5_CTX *context)
+libbsd_MD5Init(MD5_CTX *context)
 {
-	libmd_wrapper(MD5Init);
-	libmd_MD5Init(context);
+	MD5Init(context);
 }
 libbsd_link_warning(MD5Init,
                     "This function is a deprecated wrapper, use libmd instead.");
+libbsd_symver_weak(MD5Init, libbsd_MD5Init, LIBBSD_0.0);
 
 void
-MD5Update(MD5_CTX *context, const uint8_t *data, size_t len)
+libbsd_MD5Update(MD5_CTX *context, const uint8_t *data, size_t len)
 {
-	libmd_wrapper(MD5Update);
-	libmd_MD5Update(context, data, len);
+	MD5Update(context, data, len);
 }
 libbsd_link_warning(MD5Update,
                     "This function is a deprecated wrapper, use libmd instead.");
+libbsd_symver_weak(MD5Update, libbsd_MD5Update, LIBBSD_0.0);
 
 void
-MD5Pad(MD5_CTX *context)
+libbsd_MD5Pad(MD5_CTX *context)
 {
-	libmd_wrapper(MD5Pad);
-	libmd_MD5Pad(context);
+	MD5Pad(context);
 }
 libbsd_link_warning(MD5Pad,
                     "This function is a deprecated wrapper, use libmd instead.");
+libbsd_symver_weak(MD5Pad, libbsd_MD5Pad, LIBBSD_0.0);
 
 void
-MD5Final(uint8_t digest[MD5_DIGEST_LENGTH], MD5_CTX *context)
+libbsd_MD5Final(uint8_t digest[MD5_DIGEST_LENGTH], MD5_CTX *context)
 {
-	libmd_wrapper(MD5Final);
-	libmd_MD5Final(digest, context);
+	MD5Final(digest, context);
 }
 libbsd_link_warning(MD5Final,
                     "This function is a deprecated wrapper, use libmd instead.");
+libbsd_symver_weak(MD5Final, libbsd_MD5Final, LIBBSD_0.0);
 
 void
-MD5Transform(uint32_t state[4], const uint8_t block[MD5_BLOCK_LENGTH])
+libbsd_MD5Transform(uint32_t state[4], const uint8_t block[MD5_BLOCK_LENGTH])
 {
-	libmd_wrapper(MD5Transform);
-	libmd_MD5Transform(state, block);
+	MD5Transform(state, block);
 }
 libbsd_link_warning(MD5Transform,
                     "This function is a deprecated wrapper, use libmd instead.");
+libbsd_symver_weak(MD5Transform, libbsd_MD5Transform, LIBBSD_0.0);
 
 char *
-MD5End(MD5_CTX *context, char *buf)
+libbsd_MD5End(MD5_CTX *context, char *buf)
 {
-	libmd_wrapper(MD5End);
-	return libmd_MD5End(context, buf);
+	return MD5End(context, buf);
 }
 libbsd_link_warning(MD5End,
                     "This function is a deprecated wrapper, use libmd instead.");
+libbsd_symver_weak(MD5End, libbsd_MD5End, LIBBSD_0.0);
 
 char *
-MD5File(const char *filename, char *buf)
+libbsd_MD5File(const char *filename, char *buf)
 {
-	libmd_wrapper(MD5File);
 	return MD5File(filename, buf);
 }
 libbsd_link_warning(MD5File,
                     "This function is a deprecated wrapper, use libmd instead.");
+libbsd_symver_weak(MD5File, libbsd_MD5File, LIBBSD_0.0);
 
 char *
-MD5FileChunk(const char *filename, char *buf, off_t offset, off_t length)
+libbsd_MD5FileChunk(const char *filename, char *buf, off_t offset, off_t length)
 {
-	libmd_wrapper(MD5FileChunk);
-	return libmd_MD5FileChunk(filename, buf, offset, length);
+	return MD5FileChunk(filename, buf, offset, length);
 }
 libbsd_link_warning(MD5FileChunk,
                     "This function is a deprecated wrapper, use libmd instead.");
+libbsd_symver_weak(MD5FileChunk, libbsd_MD5FileChunk, LIBBSD_0.0);
 
 char *
-MD5Data(const uint8_t *data, size_t len, char *buf)
+libbsd_MD5Data(const uint8_t *data, size_t len, char *buf)
 {
-	libmd_wrapper(MD5Data);
-	return libmd_MD5Data(data, len, buf);
+	return MD5Data(data, len, buf);
 }
 libbsd_link_warning(MD5Data,
                     "This function is a deprecated wrapper, use libmd instead.");
+libbsd_symver_weak(MD5Data, libbsd_MD5Data, LIBBSD_0.0);
diff --git a/test/Makefile.am b/test/Makefile.am
index de80b12..33d8aea 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -69,6 +69,11 @@ proctitle_LDFLAGS = \
 check_PROGRAMS += proctitle
 endif
 
+if NEED_TRANSPARENT_LIBMD
+# On the installed system this is handled via the ld script.
+md5_LDADD = $(LDADD) $(MD5_LIBS)
+endif
+
 fgetln_SOURCES = test-stream.c test-stream.h fgetln.c
 fgetln_CFLAGS = -Wno-deprecated-declarations
 fparseln_SOURCES = test-stream.c test-stream.h fparseln.c