From b2701bf5e9421ac6301697c56a45f5248351e063 Mon Sep 17 00:00:00 2001
From: Thomas Munro <thomas.munro@gmail.com>
Date: Tue, 19 Jul 2022 18:07:51 +1200
Subject: [PATCH 01/13] Remove dlopen configure probe.

dlopen() is in SUSv2 and all targeted Unix systems have it.  We still
need replacement functions for Windows, but we don't need a configure
probe for that.
---
 configure                      | 19 ++++++-------------
 configure.ac                   |  2 +-
 src/backend/utils/fmgr/dfmgr.c |  4 ++--
 src/include/pg_config.h.in     |  3 ---
 src/include/port.h             |  3 ++-
 src/port/dlopen.c              |  6 +-----
 src/tools/msvc/Solution.pm     |  1 -
 7 files changed, 12 insertions(+), 26 deletions(-)

diff --git a/configure b/configure
index c5bc382395..a2706684e3 100755
--- a/configure
+++ b/configure
@@ -16687,19 +16687,6 @@ $as_echo "#define HAVE_PS_STRINGS 1" >>confdefs.h
 
 fi
 
-ac_fn_c_check_func "$LINENO" "dlopen" "ac_cv_func_dlopen"
-if test "x$ac_cv_func_dlopen" = xyes; then :
-  $as_echo "#define HAVE_DLOPEN 1" >>confdefs.h
-
-else
-  case " $LIBOBJS " in
-  *" dlopen.$ac_objext "* ) ;;
-  *) LIBOBJS="$LIBOBJS dlopen.$ac_objext"
- ;;
-esac
-
-fi
-
 ac_fn_c_check_func "$LINENO" "explicit_bzero" "ac_cv_func_explicit_bzero"
 if test "x$ac_cv_func_explicit_bzero" = xyes; then :
   $as_echo "#define HAVE_EXPLICIT_BZERO 1" >>confdefs.h
@@ -17048,6 +17035,12 @@ fi
  ;;
 esac
 
+  case " $LIBOBJS " in
+  *" dlopen.$ac_objext "* ) ;;
+  *) LIBOBJS="$LIBOBJS dlopen.$ac_objext"
+ ;;
+esac
+
   case " $LIBOBJS " in
   *" fdatasync.$ac_objext "* ) ;;
   *) LIBOBJS="$LIBOBJS fdatasync.$ac_objext"
diff --git a/configure.ac b/configure.ac
index 61d0dd5d58..0861a10ec3 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1880,7 +1880,6 @@ if test "$pgac_cv_var_PS_STRINGS" = yes ; then
 fi
 
 AC_REPLACE_FUNCS(m4_normalize([
-	dlopen
 	explicit_bzero
 	getopt
 	getpeereid
@@ -1957,6 +1956,7 @@ if test "$PORTNAME" = "win32"; then
   AC_CHECK_FUNCS(_configthreadlocale)
   AC_REPLACE_FUNCS(gettimeofday)
   AC_LIBOBJ(dirmod)
+  AC_LIBOBJ(dlopen)
   AC_LIBOBJ(fdatasync)
   AC_LIBOBJ(getrusage)
   AC_LIBOBJ(kill)
diff --git a/src/backend/utils/fmgr/dfmgr.c b/src/backend/utils/fmgr/dfmgr.c
index 7f9ea97280..08fd7e1264 100644
--- a/src/backend/utils/fmgr/dfmgr.c
+++ b/src/backend/utils/fmgr/dfmgr.c
@@ -16,7 +16,7 @@
 
 #include <sys/stat.h>
 
-#ifdef HAVE_DLOPEN
+#ifndef WIN32
 #include <dlfcn.h>
 
 /*
@@ -28,7 +28,7 @@
 #undef bool
 #endif
 #endif
-#endif							/* HAVE_DLOPEN */
+#endif							/* !WIN32 */
 
 #include "fmgr.h"
 #include "lib/stringinfo.h"
diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in
index f9618e1986..c213f31273 100644
--- a/src/include/pg_config.h.in
+++ b/src/include/pg_config.h.in
@@ -169,9 +169,6 @@
    don't. */
 #undef HAVE_DECL_STRTOULL
 
-/* Define to 1 if you have the `dlopen' function. */
-#undef HAVE_DLOPEN
-
 /* Define to 1 if you have the <editline/history.h> header file. */
 #undef HAVE_EDITLINE_HISTORY_H
 
diff --git a/src/include/port.h b/src/include/port.h
index d39b04141f..4532608bb3 100644
--- a/src/include/port.h
+++ b/src/include/port.h
@@ -455,7 +455,8 @@ extern int	setenv(const char *name, const char *value, int overwrite);
 extern int	unsetenv(const char *name);
 #endif
 
-#ifndef HAVE_DLOPEN
+#ifdef WIN32
+/* src/port/dlopen.c has an implementation of dlopen for Windows */
 extern void *dlopen(const char *file, int mode);
 extern void *dlsym(void *handle, const char *symbol);
 extern int	dlclose(void *handle);
diff --git a/src/port/dlopen.c b/src/port/dlopen.c
index 6ff9f4bf64..48aa37129d 100644
--- a/src/port/dlopen.c
+++ b/src/port/dlopen.c
@@ -1,7 +1,7 @@
 /*-------------------------------------------------------------------------
  *
  * dlopen.c
- *	  dynamic loader for platforms without dlopen()
+ *	  dynamic loader for Windows
  *
  * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
@@ -15,8 +15,6 @@
 
 #include "c.h"
 
-#if defined(WIN32)
-
 static char last_dyn_error[512];
 
 static void
@@ -93,5 +91,3 @@ dlopen(const char *file, int mode)
 	last_dyn_error[0] = 0;
 	return (void *) h;
 }
-
-#endif
diff --git a/src/tools/msvc/Solution.pm b/src/tools/msvc/Solution.pm
index b09872e018..ce56b23bba 100644
--- a/src/tools/msvc/Solution.pm
+++ b/src/tools/msvc/Solution.pm
@@ -251,7 +251,6 @@ sub GenerateFiles
 		HAVE_DECL_STRNLEN                           => 1,
 		HAVE_DECL_STRTOLL                           => 1,
 		HAVE_DECL_STRTOULL                          => 1,
-		HAVE_DLOPEN                                 => undef,
 		HAVE_EDITLINE_HISTORY_H                     => undef,
 		HAVE_EDITLINE_READLINE_H                    => undef,
 		HAVE_EXECINFO_H                             => undef,
-- 
2.30.2

