From 20debce9941edc07037a09231307b4a6d75d5258 Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Sat, 14 Feb 2026 18:29:48 -0500
Subject: [PATCH v6 3/7] Various minor adjustments for AIX.

Remove platform-specific adjustment of MEMSET_LOOP_LIMIT; maybe
that's still the right thing, but it really ought to be re-tested.
(Indeed we should reconsider MemSet altogether, but that's not
a job for this patch set.)

Prefer unnamed POSIX semaphores, rather than the default choice
of SysV semaphores.

Add some helpful comments.

Improve the output of gen_export.pl by adding a header.

TODO: who to credit for these?
---
 meson.build                        | 11 +++--------
 src/Makefile.shlib                 |  2 +-
 src/backend/meson.build            |  1 +
 src/backend/port/aix/mkldexport.sh | 17 ++++++++++++-----
 src/template/aix                   | 10 +++++-----
 src/tools/gen_export.pl            |  6 +++++-
 6 files changed, 27 insertions(+), 20 deletions(-)

diff --git a/meson.build b/meson.build
index 77a122b43c8..7e382aebab6 100644
--- a/meson.build
+++ b/meson.build
@@ -203,24 +203,19 @@ endif
 portname = host_system
 
 if host_system == 'aix'
+  sema_kind = 'unnamed_posix'
   library_path_var = 'LIBPATH'
-
   export_file_format = 'aix'
   export_fmt = '-Wl,-bE:@0@'
   mod_link_args_fmt = ['-Wl,-bI:@0@']
   mod_link_with_dir = 'libdir'
   mod_link_with_name = '@0@.imp'
 
-  # M:SRE sets a flag indicating that an object is a shared library. Seems to
-  # work in some circumstances without, but required in others.
+  # M:SRE sets a flag indicating that an object is a shared library.
   ldflags_sl += '-Wl,-bM:SRE'
+  # -brtllib indicates binaries should use runtime-loaded shared libraries.
   ldflags_be += '-Wl,-brtllib'
 
-  # Native memset() is faster, tested on:
-  # - AIX 5.1 and 5.2, XLC 6.0 (IBM's cc)
-  # - AIX 5.3 ML3, gcc 4.0.1
-  memset_loop_limit = 0
-
 elif host_system == 'cygwin'
   sema_kind = 'unnamed_posix'
   cppflags += '-D_GNU_SOURCE'
diff --git a/src/Makefile.shlib b/src/Makefile.shlib
index 65870d629c2..cd1543550ca 100644
--- a/src/Makefile.shlib
+++ b/src/Makefile.shlib
@@ -268,7 +268,7 @@ $(stlib): $(OBJS) | $(SHLIB_PREREQS)
 	touch $@
 endif #haslibarule
 
-# AIX wraps shared libraries inside a static library, can be used both
+# AIX wraps shared libraries inside a static library, which can be used both
 # for static and shared linking
 ifeq ($(PORTNAME), aix)
 $(stlib): $(shlib)
diff --git a/src/backend/meson.build b/src/backend/meson.build
index ba273df24b5..130144a7d5b 100644
--- a/src/backend/meson.build
+++ b/src/backend/meson.build
@@ -104,6 +104,7 @@ elif host_system == 'aix'
     install_dir: dir_lib,
     build_by_default: false,
   )
+  # -Wl,-bE:exportfile indicates these symbols are exported by postgres binary
   backend_link_args += '-Wl,-bE:@0@'.format(postgres_imp.full_path())
   backend_link_depends += postgres_imp
 endif
diff --git a/src/backend/port/aix/mkldexport.sh b/src/backend/port/aix/mkldexport.sh
index adf3793e868..85631ce672a 100755
--- a/src/backend/port/aix/mkldexport.sh
+++ b/src/backend/port/aix/mkldexport.sh
@@ -13,14 +13,21 @@
 #		object file (if different from the current
 #		working directory).
 #
-# [This file comes from the Postgres 4.2 distribution. - ay 7/95]
+# On AIX, executables do not automatically expose their symbols to shared
+# modules. Extensions therefore cannot call functions in the main Postgres
+# binary unless those symbols are explicitly exported. Unlike other platforms,
+# AIX executables are not default symbol providers; each shared module must
+# link against an export list that defines which symbols it can use.
 #
-# Header: /usr/local/devel/postgres/src/tools/mkldexport/RCS/mkldexport.sh,v 1.2 1994/03/13 04:59:12 aoki Exp
+# The mkldexport.sh script fixes AIX's symbol export issue by generating an
+# explicit export list. It uses nm to gather all symbols from the Postgres
+# object files, then writes them into the export file. When invoked with ".",
+# it outputs #! ., which tells AIX the list applies to the main executable.
+# This way, extension modules can link against that list and resolve their
+# undefined symbols directly from the Postgres binary.
 #
 
-# setting this to nm -B might be better
-# ... due to changes in AIX 4.x ...
-# ... let us search in different directories - Gerhard Reithofer
+# Search for the nm command binary.
 if [ -x /usr/ucb/nm ]
 then NM=/usr/ucb/nm
 elif [ -x /usr/bin/nm ]
diff --git a/src/template/aix b/src/template/aix
index f39c680a314..c7c8706fea5 100644
--- a/src/template/aix
+++ b/src/template/aix
@@ -1,9 +1,9 @@
 # src/template/aix
 
+# Prefer unnamed POSIX semaphores if available, unless user overrides choice
+if test x"$PREFERRED_SEMAPHORES" = x"" ; then
+  PREFERRED_SEMAPHORES=UNNAMED_POSIX
+fi
+
 # Extra CFLAGS for code that will go into a shared library
 CFLAGS_SL=""
-
-# Native memset() is faster, tested on:
-# 	AIX 5.1 and 5.2, XLC 6.0 (IBM's cc)
-# 	AIX 5.3 ML3, gcc 4.0.1
-MEMSET_LOOP_LIMIT=0
diff --git a/src/tools/gen_export.pl b/src/tools/gen_export.pl
index 36f3b79bea7..c91fa42a675 100644
--- a/src/tools/gen_export.pl
+++ b/src/tools/gen_export.pl
@@ -31,7 +31,11 @@ open(my $output_handle, '>', $output)
   or die "$0: could not open output file '$output': $!\n";
 
 
-if ($format eq 'gnu')
+if ($format eq 'aix')
+{
+	print $output_handle "#!\n";
+}
+elsif ($format eq 'gnu')
 {
 	print $output_handle "{
   global:
-- 
2.43.7

