Shared library symbol versioning (PORTNAME=linux)

Even though we pay attention to never add a new shlib symbol by
minor version update, this change makes the life easier for some
PG distributors - for those who aim to support multiple major
versions of PostgreSQL stacks - all built against one set of
shared PG libraries (in other words, unless there's some SO
version bump each PG library exists on such system only in one
instance).

For example (without symbol versioning) it might easily happen
that some system's binary using PQencryptPasswordConn symbol (new
in v10) is run against 'libpq.so.5' coming from v9.6.  This is not
terribly hard to imagine since at build-time it is likely that
the binary is linked against proper (v10) version of the
libpq.so.5.  Such case would end up with linking issues at
runtime.  This problem is very likely to happen on systems with
rather long term support (e.g. 10+ years) where many updates of
the shared library are likely to happen during lifetime.

Shlib symbol versioning then helps the package-tooling to always
guarantee that sufficiently new shlib is installed on such systems
(e.g. that 'libpq.so.5' providing PQencryptPasswordConn symbol is
installed, when required by any other package).

Luckily, the build system is already using the GNU ld option
--version-script on linux port, so adding symbol versions there
shouldn't really cause any new build failures.

diff --git a/config/Makefile b/config/Makefile
index 67e7998f55..86612a42c3 100644
*** a/config/Makefile
--- b/config/Makefile
***************
*** 8,13 **** include $(top_builddir)/src/Makefile.global
--- 8,14 ----
  install: all installdirs
  	$(INSTALL_SCRIPT) $(srcdir)/install-sh '$(DESTDIR)$(pgxsdir)/config/install-sh'
  	$(INSTALL_SCRIPT) $(srcdir)/missing '$(DESTDIR)$(pgxsdir)/config/missing'
+ 	$(INSTALL_SCRIPT) $(srcdir)/build-exports-gnu-ld '$(DESTDIR)$(pgxsdir)/config/build-exports-gnu-ld'
  
  installdirs:
  	$(MKDIR_P) '$(DESTDIR)$(pgxsdir)/config'
diff --git a/config/bunew file mode 100755
index 0000000000..042d9e9342
*** /dev/null
--- b/config/build-exports-gnu-ld
***************
*** 0 ****
--- 1,45 ----
+ #! /bin/sh
+ 
+ # by default use PG_ prefix
+ : "${SYMBOL_VERSION_PREFIX=PG_}"
+ 
+ # we started symbol versioning since v12
+ : "${SYMBOL_VERSION_START=9.6}"
+ 
+ version=$SYMBOL_VERSION_START
+ version_prev=
+ first=:
+ 
+ open_block ()
+ {
+ 	$first || echo
+ 	first=false
+ 	echo "${SYMBOL_VERSION_PREFIX}$version {"
+ 	echo "global:"
+ }
+ 
+ close_block ()
+ {
+ 	if test -n "$version_prev"; then
+ 		echo "} $SYMBOL_VERSION_PREFIX$version_prev;"
+ 	else
+ 		echo "};"
+ 	fi
+ 	version_prev=$version
+ 	version=$1
+ }
+ 
+ open_block
+ while read -r symbol _ new_version
+ do
+ 	case $symbol in '#'*) continue ;; esac
+ 	if test -n "$new_version" && test "$new_version" != "$version"; then
+ 		close_block "$new_version"
+ 		open_block
+ 	fi
+ 	echo "	$symbol;"
+ done
+ 
+ echo "local:"
+ echo "	*;"
+ close_block
diff --git a/src/Makefile.shlib b/index 95b82a6dea..a7065d02a4 100644
*** a/src/Makefile.shlib
--- b/src/Makefile.shlib
***************
*** 221,227 **** ifeq ($(PORTNAME), linux)
    ifdef soname
      LINK.shared		+= -Wl,-soname,$(soname)
    endif
!   BUILD.exports		= ( echo '{ global:'; $(AWK) '/^[^\#]/ {printf "%s;\n",$$1}' $<; echo ' local: *; };' ) >$@
    exports_file		= $(SHLIB_EXPORTS:%.txt=%.list)
    ifneq (,$(exports_file))
      LINK.shared		+= -Wl,--version-script=$(exports_file)
--- 221,227 ----
    ifdef soname
      LINK.shared		+= -Wl,-soname,$(soname)
    endif
!   BUILD.exports		= $(SHELL) $(top_srcdir)/config/build-exports-gnu-ld < $< > $@
    exports_file		= $(SHLIB_EXPORTS:%.txt=%.list)
    ifneq (,$(exports_file))
      LINK.shared		+= -Wl,--version-script=$(exports_file)
diff --git a/src/interfacindex d6a38d0df8..29bebeac7e 100644
*** a/src/interfaces/libpq/exports.txt
--- b/src/interfaces/libpq/exports.txt
***************
*** 171,174 **** PQsslAttributeNames       168
  PQsslAttribute            169
  PQsetErrorContextVisibility 170
  PQresultVerboseErrorMessage 171
! PQencryptPasswordConn     172
--- 171,174 ----
  PQsslAttribute            169
  PQsetErrorContextVisibility 170
  PQresultVerboseErrorMessage 171
! PQencryptPasswordConn     172     10
