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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
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