diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c
index b5a7c57e53a..641e7de21a0 100644
--- a/src/backend/utils/adt/float.c
+++ b/src/backend/utils/adt/float.c
@@ -2852,6 +2852,12 @@ dlgamma(PG_FUNCTION_ARGS)
 	float8		arg1 = PG_GETARG_FLOAT8(0);
 	float8		result;
 
+	/* On some versions of AIX, lgamma(NaN) fails with ERANGE */
+#if defined(_AIX)
+	if (isnan(arg1))
+		PG_RETURN_FLOAT8(arg1);
+#endif
+
 	/*
 	 * Note: lgamma may not be thread-safe because it may write to a global
 	 * variable signgam, which may not be thread-local. However, this doesn't
diff --git a/src/makefiles/Makefile.aix b/src/makefiles/Makefile.aix
index e91b250fd19..d8d8a4d68b0 100644
--- a/src/makefiles/Makefile.aix
+++ b/src/makefiles/Makefile.aix
@@ -6,8 +6,11 @@
 # -blibpath must contain ALL directories where we should look for libraries
 libpath := $(shell echo $(subst -L,:,$(filter -L/%,$(LDFLAGS))) | sed -e's/ //g'):/usr/lib:/lib
 
-# AIX uses a stricter, more explicit approach. The runtime linker expects
-# to tell it exactly where to look using -blibpath.
+# when building with gcc, need to make sure that libgcc can be found
+ifeq ($(GCC), yes)
+libpath := $(libpath):$(dir $(shell gcc -print-libgcc-file-name))
+endif
+
 rpath = -Wl,-blibpath:'$(rpathdir)$(libpath)'
 
 # gcc needs to know it's building a shared lib, otherwise it'll not emit
