From 4aad97c5ac2aa8e9f12e01b55a2bf3dee3f22827 Mon Sep 17 00:00:00 2001
From: Alberto Bertogli <albertito@blitiri.com.ar>
Date: Sun, 10 Oct 2010 23:09:13 -0300
Subject: [PATCH 07/17] preload: Make detection of libc's soname and -ldl more robust

This patch turns the detection of libc's soname and -ldl requirement from a
make macro into a target, which is slightly more robust.

It also avoids invoking the linker directly, but uses the compiler instead.
This is used to avoid problems when the way we invoke the linker is not the
same way the compiler does, which can cause troubles.

Specifically, on Fedora 14 Beta, running ld by hand did not pass the
requirement of a non-executable stack, which broke ldd's processing of the
resulting shared library (because of selinux).

It's still not the nicest thing to do, but it's better than using autoconf.

Signed-off-by: Alberto Bertogli <albertito@blitiri.com.ar>
---
 .gitignore             |    3 +++
 preload/posix/Makefile |   32 ++++++++++++++++++++++----------
 preload/run/Makefile   |   14 ++++++++++----
 3 files changed, 35 insertions(+), 14 deletions(-)

diff --git a/.gitignore b/.gitignore
index 83b1c17..24e7bb9 100644
--- a/.gitignore
+++ b/.gitignore
@@ -10,6 +10,8 @@ libfiu/build-flags
 preload/posix/*.o
 preload/posix/*.so
 preload/posix/build-flags
+preload/posix/build-libcsoname
+preload/posix/build-needlibdl
 preload/posix/build-env.h
 preload/posix/function_list
 preload/posix/modules/*.o
@@ -19,4 +21,5 @@ preload/run/*.o
 preload/run/*.so
 preload/run/fiu-run
 preload/run/build-flags
+preload/run/build-needlibdl
 
diff --git a/preload/posix/Makefile b/preload/posix/Makefile
index e8d3c1e..20d658f 100644
--- a/preload/posix/Makefile
+++ b/preload/posix/Makefile
@@ -60,23 +60,34 @@ $(OBJS): build-flags
 .c.o:
 	$(NICE_CC) $(ALL_CFLAGS) -c $< -o $@
 
+
 # some platforms do not have libdl, we only use it if available
-NEED_LIBDL := $(shell ld -o dlcheck.so -shared -ldl 2>/dev/null && echo -ldl; \
-	rm -f dlcheck.so)
+build-needlibdl:
+	@$(LD) -ldl -o dlcheck.so 2>/dev/null \
+		&& echo -ldl > $@ || echo > $@
+	@rm dlcheck.so
 
 # 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
+build-libcsoname:
+	@$(CC) -x c /dev/null -lc -shared -o build-libccheck.so
+	@ldd build-libccheck.so | grep libc.so | awk '{ print $$1 }' > $@
+	@rm build-libccheck.so
+	@test "`cat $@`" != "" || \
+		(echo "Error finding soname, please report"; rm $@; exit 1)
+
+build-env.h: build-env.h.in build-libcsoname
 	@echo "  GEN $@"
-	$(Q) sed "s+@@LIBC_SONAME@@+$(LIBC_SONAME)+g" build-env.h.in > build-env.h
+	$(Q) sed "s+@@LIBC_SONAME@@+`cat build-libcsoname`+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) \
+
+fiu_posix_preload.so: build-flags build-env.h build-needlibdl $(OBJS)
+	$(NICE_CC) $(ALL_CFLAGS) -shared -fPIC $(OBJS) \
+		-lfiu `cat build-needlibdl` \
 		-o fiu_posix_preload.so
 
+
 # this should only be needed when building the function list and not the
 # preload library
 %.mod.fl: %.mod
@@ -95,7 +106,8 @@ uninstall:
 	$(RM) $(PREFIX)/lib/fiu_posix_preload.so
 
 clean:
-	rm -f $(OBJS) $(GEN_OBJS:.o=.c) $(GEN_FL) build-flags build-env.h
+	rm -f $(OBJS) $(GEN_OBJS:.o=.c) $(GEN_FL)
+	rm -f build-flags build-env.h build-libcsoname build-needlibdl
 	rm -f function_list fiu_posix_preload.so
 	rm -f *.bb *.bbg *.da *.gcov *.gcda *.gcno gmon.out
 
diff --git a/preload/run/Makefile b/preload/run/Makefile
index bd4fca3..72d3c4c 100644
--- a/preload/run/Makefile
+++ b/preload/run/Makefile
@@ -49,14 +49,20 @@ $(OBJS): build-flags
 .c.o:
 	$(NICE_CC) $(ALL_CFLAGS) -c $< -o $@
 
+
 # some platforms do not have libdl, we only use it if available
-NEED_LIBDL := $(shell ld -o dlcheck.so -shared -ldl 2>/dev/null && echo -ldl; \
-	rm -f dlcheck.so)
+build-needlibdl:
+	@$(LD) -ldl -o dlcheck.so 2>/dev/null \
+		&& echo -ldl > $@ || echo > $@
+	@rm dlcheck.so
+
 
-fiu_run_preload.so: build-flags $(OBJS)
-	$(NICE_CC) $(ALL_CFLAGS) -shared -fPIC $(OBJS) -lfiu $(NEED_LIBDL) \
+fiu_run_preload.so: build-flags build-needlibdl $(OBJS)
+	$(NICE_CC) $(ALL_CFLAGS) -shared -fPIC $(OBJS) \
+		-lfiu `cat build-needlibdl` \
 		-o fiu_run_preload.so
 
+
 fiu-run: build-flags fiu-run.in
 	cat fiu-run.in | sed "s+@@PLIBPATH@@+$(PLIBPATH)+g" > fiu-run
 	chmod +x fiu-run
-- 
1.6.2.2.646.gb214

