diff -Nur postgresql-7.4-7.4.13/build-tree/postgresql-7.4.13/contrib/contrib-global.mk postgresql-7.4-7.4.13.new/build-tree/postgresql-7.4.13/contrib/contrib-global.mk
--- postgresql-7.4.13/contrib/contrib-global.mk	2003-08-23 06:23:38.000000000 +0200
+++ postgresql-7.4.13/contrib/contrib-global.mk	2006-10-06 19:04:07.000000000 +0200
@@ -228,6 +228,9 @@
 	sed 's,MODULE_PATHNAME,$$libdir/$*,g' $< >$@
 endif
 
+# avoid unnecessary linked libs
+LIBS := $(filter-out -lpam -lssl -lcrypto -lkrb5 -lz -lreadline -lcrypt -lresolv -lnsl -ldl -lm, $(LIBS))
+
 ifdef PROGRAM
 $(PROGRAM): $(OBJS)
 	$(CC) $(CFLAGS) $(OBJS) $(PG_LIBS) $(LDFLAGS) $(LIBS) -o $@
diff -Nur postgresql-7.4-7.4.13/build-tree/postgresql-7.4.13/src/backend/Makefile postgresql-7.4-7.4.13.new/build-tree/postgresql-7.4.13/src/backend/Makefile
--- postgresql-7.4.13/src/backend/Makefile	2003-03-21 18:18:34.000000000 +0100
+++ postgresql-7.4.13/src/backend/Makefile	2006-10-06 19:04:07.000000000 +0200
@@ -17,6 +17,7 @@
 	storage tcop utils
 
 OBJS := $(DIRS:%=%/SUBSYS.o)
+LIBS := $(filter-out -lz -lreadline -lresolv -lnsl, $(LIBS))
 
 ifeq ($(PORTNAME), qnx4)
 # This file crashes qnx4's wlink and is therefore not in
diff -Nur postgresql-7.4-7.4.13/build-tree/postgresql-7.4.13/src/bin/pg_controldata/Makefile postgresql-7.4-7.4.13.new/build-tree/postgresql-7.4.13/src/bin/pg_controldata/Makefile
--- postgresql-7.4.13/src/bin/pg_controldata/Makefile	2002-08-17 06:03:35.000000000 +0200
+++ postgresql-7.4.13/src/bin/pg_controldata/Makefile	2006-10-06 19:04:07.000000000 +0200
@@ -14,6 +14,8 @@
 
 OBJS= pg_controldata.o pg_crc.o
 
+LIBS := $(filter-out -lpam -lssl -lcrypto -lkrb5 -lz -lreadline -lcrypt -lresolv -lnsl -ldl -lm, $(LIBS))
+
 all: submake-libpgport pg_controldata
 
 pg_controldata: $(OBJS)
diff -Nur postgresql-7.4-7.4.13/build-tree/postgresql-7.4.13/src/bin/pg_dump/Makefile postgresql-7.4-7.4.13.new/build-tree/postgresql-7.4.13/src/bin/pg_dump/Makefile
--- postgresql-7.4.13/src/bin/pg_dump/Makefile	2003-08-08 06:52:21.000000000 +0200
+++ postgresql-7.4.13/src/bin/pg_dump/Makefile	2006-10-06 19:04:07.000000000 +0200
@@ -19,6 +19,8 @@
 
 EXTRA_OBJS = $(top_builddir)/src/backend/parser/keywords.o
 
+LIBS := $(filter-out -lpam -lssl -lcrypto -lkrb5 -lz -lreadline -lcrypt -lresolv -lnsl -ldl -lm, $(LIBS))
+
 override CPPFLAGS := -I$(libpq_srcdir) $(CPPFLAGS) -DBINDIR=\"$(bindir)\"
 
 
diff -Nur postgresql-7.4-7.4.13/build-tree/postgresql-7.4.13/src/bin/pg_encoding/Makefile postgresql-7.4-7.4.13.new/build-tree/postgresql-7.4.13/src/bin/pg_encoding/Makefile
--- postgresql-7.4.13/src/bin/pg_encoding/Makefile	2002-07-27 22:10:05.000000000 +0200
+++ postgresql-7.4.13/src/bin/pg_encoding/Makefile	2006-10-06 19:04:07.000000000 +0200
@@ -13,6 +13,7 @@
 include $(top_builddir)/src/Makefile.global
 
 OBJS= pg_encoding.o
+LIBS := $(filter-out -lpam -lssl -lcrypto -lkrb5 -lz -lreadline -lcrypt -lresolv -lnsl -ldl -lm, $(LIBS))
 
 all: submake-libpq submake-libpgport pg_encoding
 
diff -Nur postgresql-7.4-7.4.13/build-tree/postgresql-7.4.13/src/bin/pg_id/Makefile postgresql-7.4-7.4.13.new/build-tree/postgresql-7.4.13/src/bin/pg_id/Makefile
--- postgresql-7.4.13/src/bin/pg_id/Makefile	2000-11-30 21:36:11.000000000 +0100
+++ postgresql-7.4.13/src/bin/pg_id/Makefile	2006-10-06 19:04:07.000000000 +0200
@@ -12,6 +12,8 @@
 top_builddir = ../../..
 include $(top_builddir)/src/Makefile.global
 
+LIBS := $(filter-out -lpam -lssl -lcrypto -lkrb5 -lz -lreadline -lcrypt -lresolv -lnsl -ldl -lm, $(LIBS))
+
 all: pg_id
 
 pg_id: pg_id.o
diff -Nur postgresql-7.4-7.4.13/build-tree/postgresql-7.4.13/src/bin/pg_resetxlog/Makefile postgresql-7.4-7.4.13.new/build-tree/postgresql-7.4.13/src/bin/pg_resetxlog/Makefile
--- postgresql-7.4.13/src/bin/pg_resetxlog/Makefile	2002-08-17 06:03:36.000000000 +0200
+++ postgresql-7.4.13/src/bin/pg_resetxlog/Makefile	2006-10-06 19:04:07.000000000 +0200
@@ -13,6 +13,7 @@
 include $(top_builddir)/src/Makefile.global
 
 OBJS= pg_resetxlog.o pg_crc.o
+LIBS := $(filter-out -lpam -lssl -lcrypto -lkrb5 -lz -lreadline -lcrypt -lresolv -lnsl -ldl -lm, $(LIBS))
 
 all: submake-libpgport pg_resetxlog
 
diff -Nur postgresql-7.4-7.4.13/build-tree/postgresql-7.4.13/src/bin/psql/Makefile postgresql-7.4-7.4.13.new/build-tree/postgresql-7.4.13/src/bin/psql/Makefile
--- postgresql-7.4.13/src/bin/psql/Makefile	2003-08-08 06:52:21.000000000 +0200
+++ postgresql-7.4.13/src/bin/psql/Makefile	2006-10-06 19:04:07.000000000 +0200
@@ -21,6 +21,8 @@
 	startup.o prompt.o variables.o large_obj.o print.o describe.o \
 	tab-complete.o mbprint.o
 
+LIBS := $(filter-out -lpam -lcrypto -lkrb5 -lz -lcrypt -lresolv -lnsl -ldl -lm, $(LIBS))
+
 all: submake-libpq submake-libpgport psql
 
 psql: $(OBJS) $(libpq_builddir)/libpq.a
diff -Nur postgresql-7.4-7.4.13/build-tree/postgresql-7.4.13/src/bin/scripts/Makefile postgresql-7.4-7.4.13.new/build-tree/postgresql-7.4.13/src/bin/scripts/Makefile
--- postgresql-7.4.13/src/bin/scripts/Makefile	2003-09-12 04:40:10.000000000 +0200
+++ postgresql-7.4.13/src/bin/scripts/Makefile	2006-10-06 19:04:07.000000000 +0200
@@ -17,6 +17,8 @@
 
 override CPPFLAGS := -I$(top_srcdir)/src/bin/pg_dump -I$(top_srcdir)/src/bin/psql -I$(libpq_srcdir) $(CPPFLAGS)
 
+LIBS := $(filter-out -lpam -lssl -lcrypto -lkrb5 -lz -lreadline -lcrypt -lresolv -lnsl -ldl -lm, $(LIBS))
+
 all: submake-libpq submake-backend $(PROGRAMS)
 
 %: %.o
diff -Nur postgresql-7.4-7.4.13/build-tree/postgresql-7.4.13/src/interfaces/libpq/Makefile postgresql-7.4-7.4.13.new/build-tree/postgresql-7.4.13/src/interfaces/libpq/Makefile
--- postgresql-7.4.13/src/interfaces/libpq/Makefile	2003-09-27 17:32:48.000000000 +0200
+++ postgresql-7.4.13/src/interfaces/libpq/Makefile	2006-10-06 19:04:07.000000000 +0200
@@ -32,7 +32,7 @@
 # Add libraries that libpq depends (or might depend) on into the
 # shared library link.  (The order in which you list them here doesn't
 # matter.)
-SHLIB_LINK += $(filter -lcrypt -ldes -lkrb -lcom_err -lcrypto -lk5crypto -lkrb5 -lssl -lsocket -lnsl -lresolv -lintl, $(LIBS)) $(THREAD_LIBS)
+SHLIB_LINK += $(filter -lcrypt -ldes -lkrb -lcom_err -lcrypto -lk5crypto -lkrb5 -lssl -lsocket -lintl, $(LIBS)) $(THREAD_LIBS)
 
 
 all: all-lib
diff -Nur postgresql-7.4-7.4.13/build-tree/postgresql-7.4.13/src/pl/plperl/GNUmakefile postgresql-7.4-7.4.13.new/build-tree/postgresql-7.4.13/src/pl/plperl/GNUmakefile
--- postgresql-7.4.13/src/pl/plperl/GNUmakefile	2006-10-06 19:04:02.000000000 +0200
+++ postgresql-7.4.13/src/pl/plperl/GNUmakefile	2006-10-06 19:06:35.000000000 +0200
@@ -29,7 +29,7 @@
 SO_MINOR_VERSION = 0
 
 OBJS = plperl.o eloglvl.o SPI.o
-SHLIB_LINK = $(perl_embed_ldflags) $(BE_DLLLIBS)
+SHLIB_LINK = $(filter-out -lm -lcrypt, $(perl_embed_ldflags) $(BE_DLLLIBS))
 
 include $(top_srcdir)/src/Makefile.shlib
 
diff -Nur postgresql-7.4-7.4.13/build-tree/postgresql-7.4.13/src/pl/plpython/Makefile postgresql-7.4-7.4.13.new/build-tree/postgresql-7.4.13/src/pl/plpython/Makefile
--- postgresql-7.4.13/src/pl/plpython/Makefile	2004-01-21 20:25:11.000000000 +0100
+++ postgresql-7.4.13/src/pl/plpython/Makefile	2006-10-06 19:06:42.000000000 +0200
@@ -24,7 +24,7 @@
 SO_MINOR_VERSION = 0
 OBJS = plpython.o
 
-SHLIB_LINK = $(BE_DLLLIBS) $(python_libspec)
+SHLIB_LINK = $(filter-out -ldl -lutil -lm, $(BE_DLLLIBS) $(python_libspec))
 
 include $(top_srcdir)/src/Makefile.shlib
 
diff -Nur postgresql-7.4-7.4.13/build-tree/postgresql-7.4.13/src/pl/tcl/Makefile postgresql-7.4-7.4.13.new/build-tree/postgresql-7.4.13/src/pl/tcl/Makefile
--- postgresql-7.4.13/src/pl/tcl/Makefile	2004-01-21 20:25:11.000000000 +0100
+++ postgresql-7.4.13/src/pl/tcl/Makefile	2006-10-06 19:06:50.000000000 +0200
@@ -29,7 +29,7 @@
 endif
 
 
-SHLIB_LINK = $(BE_DLLLIBS) $(TCL_LIB_SPEC) $(TCL_LIBS) -lc
+SHLIB_LINK = $(filter-out -ldl -lm, $(BE_DLLLIBS) $(TCL_LIB_SPEC) $(TCL_LIBS) -lc)
 
 NAME = pltcl
 SO_MAJOR_VERSION = 2
