From 5d134a4bf21f603c7d810da27a0c62e24c438fb2 Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Thu, 19 Feb 2026 15:37:26 -0500
Subject: [PATCH v7 4/8] 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.

Prevent building static libraries that duplicate shared libraries,
as they would have the same names 'libxxx.a'.

Include /opt/freeware/lib in -Wl,-blibpath, even when it is not
mentioned anywhere in LDFLAGS.

Add some helpful comments.

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

Author: Aditya Kamath <Aditya.Kamath1@ibm.com>
Author: Srirama Kucherlapati <sriram.rk@in.ibm.com>
Co-authored-by: Peter Eisentraut <peter@eisentraut.org>
Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/CY5PR11MB63928CC05906F27FB10D74D0FD322@CY5PR11MB6392.namprd11.prod.outlook.com
---
 meson.build                        | 17 ++++++++++-------
 src/Makefile.shlib                 |  2 +-
 src/backend/meson.build            |  1 +
 src/backend/port/aix/mkldexport.sh | 17 ++++++++++++-----
 src/makefiles/Makefile.aix         |  2 +-
 src/template/aix                   | 10 +++++-----
 src/tools/gen_export.pl            |  6 +++++-
 7 files changed, 35 insertions(+), 20 deletions(-)

diff --git a/meson.build b/meson.build
index 21d9a60e063..2ebf4a189eb 100644
--- a/meson.build
+++ b/meson.build
@@ -229,23 +229,26 @@ 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
+  # On AIX, shared libraries are wrapped in static libraries and can have
+  # the same extension '.a'.  Therefore we must refrain from trying to build
+  # static libraries alongside shared ones, or meson will complain about
+  # duplicate targets.  Note however that we leave dlsuffix with its default
+  # value of '.so'; this results in using '.so' for loadable modules, which
+  # is fine.
+  build_static_lib = false
 
 elif host_system == 'cygwin'
   sema_kind = 'unnamed_posix'
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/makefiles/Makefile.aix b/src/makefiles/Makefile.aix
index dd16a7a0378..828641655aa 100644
--- a/src/makefiles/Makefile.aix
+++ b/src/makefiles/Makefile.aix
@@ -3,7 +3,7 @@
 MAKE_EXPORTS= true
 
 # -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
+libpath := $(shell echo $(subst -L,:,$(filter -L/%,$(LDFLAGS))) | sed -e's/ //g'):/opt/freeware/lib:/usr/lib:/lib
 
 # when building with gcc, need to make sure that libgcc can be found
 ifeq ($(GCC), yes)
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

