From b442f1a047c2648c609675dd8d292a3d6a50b45f Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Sat, 16 Jul 2022 12:48:16 -0700
Subject: [PATCH v3 1/4] Add central declarations for dlsym()ed symbols

This is in preparation for defaulting to -fvisibility=hidden in extensions,
instead of relying on all symbols in extensions to be exported. For that
exported symbols need to be tagged with PGDLLEXPORT. Most extensions only need
to do so for _PG_init() and functions defined with PG_FUNCTION_INFO_V1. Adding
central declarations with PGDLLEXPORT avoids breaking extensions declaring
_PG_init(), as long as they include fmgr.h before the declaration.

Author: Andres Freund <andres@anarazel.de>
Discussion: https://postgr.es/m/20211101020311.av6hphdl6xbjbuif@alap3.anarazel.de
---
 src/include/fmgr.h                      | 11 +++++++++++
 src/include/jit/jit.h                   |  2 +-
 src/include/postmaster/pgarch.h         |  2 ++
 src/include/replication/output_plugin.h |  2 ++
 4 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/src/include/fmgr.h b/src/include/fmgr.h
index 5314b737052..7f0c37470a6 100644
--- a/src/include/fmgr.h
+++ b/src/include/fmgr.h
@@ -424,6 +424,17 @@ CppConcat(pg_finfo_,funcname) (void) \
 extern int no_such_variable
 
 
+/*
+ * Declare _PG_init/_PG_fini centrally. Historically each shared library had
+ * its own declaration, but now that we want to mark the symbols PGDLLEXPORT,
+ * the central declaration avoids each extension having to add it. The
+ * existing declaration in extensions continue to work as long as fmgr.h is
+ * included before, otherwise compilation targetting windows fails.
+ */
+extern PGDLLEXPORT void _PG_init(void);
+extern PGDLLEXPORT void _PG_fini(void);
+
+
 /*-------------------------------------------------------------------------
  *		Support for verifying backend compatibility of loaded modules
  *
diff --git a/src/include/jit/jit.h b/src/include/jit/jit.h
index d1940332094..600ddfc7539 100644
--- a/src/include/jit/jit.h
+++ b/src/include/jit/jit.h
@@ -63,7 +63,7 @@ typedef struct JitContext
 
 typedef struct JitProviderCallbacks JitProviderCallbacks;
 
-extern void _PG_jit_provider_init(JitProviderCallbacks *cb);
+extern PGDLLEXPORT void _PG_jit_provider_init(JitProviderCallbacks *cb);
 typedef void (*JitProviderInit) (JitProviderCallbacks *cb);
 typedef void (*JitProviderResetAfterErrorCB) (void);
 typedef void (*JitProviderReleaseContextCB) (JitContext *context);
diff --git a/src/include/postmaster/pgarch.h b/src/include/postmaster/pgarch.h
index f366a159a8e..2546e2ae883 100644
--- a/src/include/postmaster/pgarch.h
+++ b/src/include/postmaster/pgarch.h
@@ -63,6 +63,8 @@ typedef struct ArchiveModuleCallbacks
  */
 typedef void (*ArchiveModuleInit) (ArchiveModuleCallbacks *cb);
 
+extern PGDLLEXPORT void _PG_archive_module_init(ArchiveModuleCallbacks *cb);
+
 /*
  * Since the logic for archiving via a shell command is in the core server
  * and does not need to be loaded via a shared library, it has a special
diff --git a/src/include/replication/output_plugin.h b/src/include/replication/output_plugin.h
index 539dc8e6974..b7d28d7045c 100644
--- a/src/include/replication/output_plugin.h
+++ b/src/include/replication/output_plugin.h
@@ -35,6 +35,8 @@ typedef struct OutputPluginOptions
  */
 typedef void (*LogicalOutputPluginInit) (struct OutputPluginCallbacks *cb);
 
+extern PGDLLEXPORT void _PG_output_plugin_init(struct OutputPluginCallbacks *cb);
+
 /*
  * Callback that gets called in a user-defined plugin. ctx->private_data can
  * be set to some private data.
-- 
2.37.0.3.g30cc8d0f14

