git » libfiu » commit 2bfeeff

preload/posix: Get libc's soname from the build environment

author Alberto Bertogli
2010-03-18 00:10:42 UTC
committer Alberto Bertogli
2010-03-18 00:14:51 UTC
parent 4d8d9cdd157947ce27d8baa9bbb2d274a2760d1f

preload/posix: Get libc's soname from the build environment

ia64 and alpha use a different libc soname, libc.so.6.1, instead of the usual
libc.so.6.

This patch makes the build system find which one is in use, so the constant
used for dlopen() has the right value.

Thanks to Serafeim Zanikolas for the bug report (via Debian's BTS).

Signed-off-by: Alberto Bertogli <albertito@blitiri.com.ar>

.gitignore +1 -0
preload/posix/Makefile +11 -2
preload/posix/build-env.h.in +12 -0
preload/posix/codegen.c +2 -1

diff --git a/.gitignore b/.gitignore
index 7740f5e..83b1c17 100644
--- a/.gitignore
+++ b/.gitignore
@@ -10,6 +10,7 @@ libfiu/build-flags
 preload/posix/*.o
 preload/posix/*.so
 preload/posix/build-flags
+preload/posix/build-env.h
 preload/posix/function_list
 preload/posix/modules/*.o
 preload/posix/modules/*.mod.c
diff --git a/preload/posix/Makefile b/preload/posix/Makefile
index 40ca573..e8d3c1e 100644
--- a/preload/posix/Makefile
+++ b/preload/posix/Makefile
@@ -64,7 +64,16 @@ $(OBJS): build-flags
 NEED_LIBDL := $(shell ld -o dlcheck.so -shared -ldl 2>/dev/null && echo -ldl; \
 	rm -f dlcheck.so)
 
-fiu_posix_preload.so: build-flags $(OBJS)
+# libc's soname depends on the platform (most use libc.so.6, but for example
+# ia64 and alpha use libc.so.6.1), so find which one to use at build-time
+LIBC_SONAME = $(shell ld -o libccheck.so -lc -shared && \
+	ldd libccheck.so | grep libc.so | awk '{ print $$1 }'; rm -f libccheck.so)
+
+build-env.h: build-env.h.in
+	@echo "  GEN $@"
+	$(Q) sed "s+@@LIBC_SONAME@@+$(LIBC_SONAME)+g" build-env.h.in > build-env.h
+
+fiu_posix_preload.so: build-flags build-env.h $(OBJS)
 	$(NICE_CC) $(ALL_CFLAGS) -shared -fPIC $(OBJS) -lfiu $(NEED_LIBDL) \
 		-o fiu_posix_preload.so
 
@@ -86,7 +95,7 @@ uninstall:
 	$(RM) $(PREFIX)/lib/fiu_posix_preload.so
 
 clean:
-	rm -f $(OBJS) $(GEN_OBJS:.o=.c) $(GEN_FL) build-flags
+	rm -f $(OBJS) $(GEN_OBJS:.o=.c) $(GEN_FL) build-flags build-env.h
 	rm -f function_list fiu_posix_preload.so
 	rm -f *.bb *.bbg *.da *.gcov *.gcda *.gcno gmon.out
 
diff --git a/preload/posix/build-env.h.in b/preload/posix/build-env.h.in
new file mode 100644
index 0000000..63c06f6
--- /dev/null
+++ b/preload/posix/build-env.h.in
@@ -0,0 +1,12 @@
+
+#ifndef _BUILD_ENV_H
+#define _BUILD_ENV_H
+
+/*
+ * Constants taken from the build environment
+ */
+
+/* libc's soname, used for dlopen()ing the C library */
+#define LIBC_SONAME "@@LIBC_SONAME@@"
+
+#endif
diff --git a/preload/posix/codegen.c b/preload/posix/codegen.c
index 71644c5..177fdcd 100644
--- a/preload/posix/codegen.c
+++ b/preload/posix/codegen.c
@@ -4,6 +4,7 @@
 #include <sys/time.h>
 #include <stdlib.h>
 #include "codegen.h"
+#include "build-env.h"
 
 /* Dynamically load libc */
 void *_fiu_libc;
@@ -30,7 +31,7 @@ void constructor_attr(200) _fiu_init(void)
 	if (initialized)
 		goto exit;
 
-	_fiu_libc = dlopen("libc.so.6", RTLD_NOW);
+	_fiu_libc = dlopen(LIBC_SONAME, RTLD_NOW);
 	if (_fiu_libc == NULL) {
 		fprintf(stderr, "Error loading libc: %s\n", dlerror());
 		exit(1);