diff --git a/src/Makefile.global.in b/src/Makefile.global.in
index 47b2f5e491ee5a44db89260cc79f9b45a08dc8d7..280578a4c39470daf67f5cc783cdb33f7262879f 100644
--- a/src/Makefile.global.in
+++ b/src/Makefile.global.in
@@ -1,5 +1,5 @@
 # -*-makefile-*-
-# $PostgreSQL: pgsql/src/Makefile.global.in,v 1.263 2010/07/05 18:54:37 tgl Exp $
+# $PostgreSQL: pgsql/src/Makefile.global.in,v 1.264 2010/07/06 22:03:05 tgl Exp $
 
 #------------------------------------------------------------------------------
 # All PostgreSQL makefiles include this file and use the variables it sets,
@@ -235,9 +235,21 @@ OSSP_UUID_LIBS = @OSSP_UUID_LIBS@
 LD = @LD@
 with_gnu_ld = @with_gnu_ld@
 ld_R_works = @ld_R_works@
-LDFLAGS = @LDFLAGS@
+
+# We want -L for libpgport.a to be first in LDFLAGS.  We also need LDFLAGS
+# to be a "recursively expanded" variable, else adjustments to rpathdir
+# don't work right.  So we must NOT do LDFLAGS := something, meaning this has
+# to be done first and elsewhere we must only do LDFLAGS += something.
+ifdef PGXS
+  LDFLAGS = -L$(libdir)
+else
+  LDFLAGS = -L$(top_builddir)/src/port
+endif
+LDFLAGS += @LDFLAGS@
+
 LDFLAGS_EX = @LDFLAGS_EX@
-LDFLAGS_SL = @LDFLAGS_SL@
+# LDFLAGS_SL might have already been assigned by calling makefile
+LDFLAGS_SL += @LDFLAGS_SL@
 LDREL = -r
 LDOUT = -o
 RANLIB = @RANLIB@
@@ -450,16 +462,11 @@ endif
 ##########################################################################
 #
 # substitute implementations of C library routines (see src/port/)
+# note we already included -L.../src/port in LDFLAGS above
 
 LIBOBJS = @LIBOBJS@
 
 LIBS := -lpgport $(LIBS)
-# add location of libpgport.a to LDFLAGS
-ifdef PGXS
-  LDFLAGS += -L$(libdir)
-else
-  LDFLAGS += -L$(top_builddir)/src/port
-endif
 
 # to make ws2_32.lib the last library, and always link with shfolder,
 # so SHGetFolderName isn't picked up from shell32.dll