diff --git a/contrib/cube/Makefile b/contrib/cube/Makefile
index f4b0d0b50177b5b1f292d0d36fa639f1f09417dd..dd953aae53df58375a358526d48f59476ac94d64 100644
--- a/contrib/cube/Makefile
+++ b/contrib/cube/Makefile
@@ -1,4 +1,4 @@
-# $PostgreSQL: pgsql/contrib/cube/Makefile,v 1.12 2004/08/20 20:13:02 momjian Exp $
+# $PostgreSQL: pgsql/contrib/cube/Makefile,v 1.13 2005/07/24 23:30:09 tgl Exp $
 
 MODULE_big = cube
 OBJS= cube.o cubeparse.o
@@ -9,6 +9,7 @@ REGRESS = cube
 
 EXTRA_CLEAN = cubeparse.c cubeparse.h cubescan.c y.tab.c y.tab.h
 
+SHLIB_LINK += $(filter -lm, $(LIBS))
 
 ifdef USE_PGXS
 PGXS = $(shell pg_config --pgxs)
diff --git a/contrib/earthdistance/Makefile b/contrib/earthdistance/Makefile
index 35ad6d610465b739cc7910e0410279b8aabe9969..8cb804ee19b9a0e9ed18da96418e8fbdb2fff5a2 100644
--- a/contrib/earthdistance/Makefile
+++ b/contrib/earthdistance/Makefile
@@ -1,10 +1,12 @@
-# $PostgreSQL: pgsql/contrib/earthdistance/Makefile,v 1.14 2004/08/20 20:13:03 momjian Exp $
+# $PostgreSQL: pgsql/contrib/earthdistance/Makefile,v 1.15 2005/07/24 23:30:09 tgl Exp $
 
 MODULES = earthdistance
 DATA_built = earthdistance.sql
 DOCS = README.earthdistance
 REGRESS = earthdistance
 
+SHLIB_LINK += $(filter -lm, $(LIBS))
+
 ifdef USE_PGXS
 PGXS = $(shell pg_config --pgxs)
 include $(PGXS)
diff --git a/contrib/tablefunc/Makefile b/contrib/tablefunc/Makefile
index 4b87fd95a5877a7b907cc77031ae1f1c101905f6..b2b8cb2e003434b2b91bcdcbf163a339471a2018 100644
--- a/contrib/tablefunc/Makefile
+++ b/contrib/tablefunc/Makefile
@@ -3,6 +3,8 @@ DATA_built = tablefunc.sql
 DOCS = README.tablefunc
 REGRESS = tablefunc
 
+SHLIB_LINK += $(filter -lm, $(LIBS))
+
 ifdef USE_PGXS
 PGXS = $(shell pg_config --pgxs)
 include $(PGXS)
diff --git a/contrib/tsearch2/Makefile b/contrib/tsearch2/Makefile
index 15983db428108f4b1c4b4c6c90388d45135debdc..1691dd578d262dab08094ed69bf97988f7608b8c 100644
--- a/contrib/tsearch2/Makefile
+++ b/contrib/tsearch2/Makefile
@@ -1,4 +1,4 @@
-# $PostgreSQL: pgsql/contrib/tsearch2/Makefile,v 1.8 2004/10/17 23:09:31 tgl Exp $
+# $PostgreSQL: pgsql/contrib/tsearch2/Makefile,v 1.9 2005/07/24 23:30:10 tgl Exp $
 
 MODULE_big = tsearch2
 OBJS = dict_ex.o dict.o snmap.o stopword.o common.o prs_dcfg.o \
@@ -19,7 +19,7 @@ DATA_built = tsearch2.sql untsearch2.sql
 DOCS = README.tsearch2
 REGRESS = tsearch2
 
-SHLIB_LINK := -lm
+SHLIB_LINK += $(filter -lm, $(LIBS))
 
 
 ifdef USE_PGXS
diff --git a/src/makefiles/Makefile.aix b/src/makefiles/Makefile.aix
index bb0c2fd58b81ce9ef03820215cfb74cebbf24fc2..99099272eb2d71508115c66e64c0a9bc95fedc9d 100644
--- a/src/makefiles/Makefile.aix
+++ b/src/makefiles/Makefile.aix
@@ -28,6 +28,6 @@ MKLDEXPORT=$(top_srcdir)/src/backend/port/aix/mkldexport.sh
 
 %$(DLSUFFIX): %.o %$(EXPSUFF)
 	@echo Making shared library $@ from $*.o, $*$(EXPSUFF) and postgres.imp
-	$(CC) $(LDFLAGS) $(LDFLAGS_SL) -o $@ $*.o -Wl,-bI:$(top_builddir)/src/backend/$(POSTGRES_IMP) -Wl,-bE:$*$(EXPSUFF) $(LIBS)
+	$(CC) $(LDFLAGS) $(LDFLAGS_SL) -o $@ $*.o -Wl,-bI:$(top_builddir)/src/backend/$(POSTGRES_IMP) -Wl,-bE:$*$(EXPSUFF) $(SHLIB_LINK)
 
 sqlmansect = 7