Restore proper linkage of pg_char_to_encoding() and friends.

Back in the 8.3 era we discovered that it was problematic if
libpq.so had encoding ID assignments different from the backend,
which is possible because on some platforms libpq.so might be
of a different major version from the calling programs.
psql should use libpq's assignments, but initdb has to use the
backend's, else it will put wrong values into pg_database.
The solution devised in commit 8468146b0 relied on giving initdb
its own copy of encnames.c rather than relying on the functions
exported by libpq.  Later, that metamorphosed into ensuring that
libpgcommon got linked before libpq -- which made things OK for
initdb but broke psql.  We didn't notice for lack of any changes
in enum pg_enc since then.  Commit 06843df4a reversed that, fixing
the latent bug in psql but adding one in initdb.  The meson build
infrastructure is also not being sufficiently careful about link
order, and trying to make it so would be equally fragile.

Hence, let's use a new scheme based on giving the libpq-exported
symbols different real names than the same functions exported from
libpgcommon.a or libpgcommon_srv.a.  (We could distinguish those
two cases as well, but there seems no need to.)  libpq gets the
official names to avoid an ABI break for libpq clients, while the
other cases use #define's to make the real names "xxx_private"
rather than "xxx".  By controlling where the #define's are
applied, we can force any particular client program to use one
set or the other of the encnames.c functions.

We cannot back-patch this, since it'd be an ABI break for backend
loadable modules, but there seems little need to.  We're just
trying to ensure that the world is safe for hypothetical future
additions to enum pg_enc.

In passing this should fix "duplicate symbol" linker warnings
that we've been seeing on AIX buildfarm members since commit
06843df4a.  It's not very clear why that linker is complaining
now, when there were strictly *more* duplicates visible before,
but in any case this should remove the reason for complaint.

Patch by me; thanks to Andres Freund for review.

Discussion: https://postgr.es/m/2385119.1696354473@sss.pgh.pa.us
This commit is contained in:
Tom Lane 2023-10-07 12:08:10 -04:00
parent e8c334c47a
commit b6c7cfac88
5 changed files with 41 additions and 9 deletions

View File

@ -16,13 +16,12 @@ subdir = src/bin/initdb
top_builddir = ../../..
include $(top_builddir)/src/Makefile.global
override CPPFLAGS := -I$(libpq_srcdir) -I$(top_srcdir)/src/timezone $(ICU_CFLAGS) $(CPPFLAGS)
# Note: it's important that we link to encnames.o from libpgcommon, not
# from libpq, else we have risks of version skew if we run with a libpq
# shared library from a different PG version. The libpq_pgport macro
# should ensure that that happens.
#
# shared library from a different PG version. Define
# USE_PRIVATE_ENCODING_FUNCS to ensure that that happens.
override CPPFLAGS := -DUSE_PRIVATE_ENCODING_FUNCS -I$(libpq_srcdir) -I$(top_srcdir)/src/timezone $(ICU_CFLAGS) $(CPPFLAGS)
# We need libpq only because fe_utils does.
LDFLAGS_INTERNAL += -L$(top_builddir)/src/fe_utils -lpgfeutils $(libpq_pgport) $(ICU_LIBS)

View File

@ -7,8 +7,6 @@ initdb_sources = files(
initdb_sources += timezone_localtime_source
#fixme: reimplement libpq_pgport logic
if host_system == 'windows'
initdb_sources += rc_bin_gen.process(win32ver_rc, extra_args: [
'--NAME', 'initdb',
@ -18,6 +16,11 @@ endif
initdb = executable('initdb',
initdb_sources,
include_directories: [timezone_inc],
# Note: it's important that we link to encnames.o from libpgcommon, not
# from libpq, else we have risks of version skew if we run with a libpq
# shared library from a different PG version. Define
# USE_PRIVATE_ENCODING_FUNCS to ensure that that happens.
c_args: ['-DUSE_PRIVATE_ENCODING_FUNCS'],
dependencies: [frontend_code, libpq, icu, icu_i18n],
kwargs: default_bin_args,
)

View File

@ -140,6 +140,13 @@ libpgcommon.a: $(OBJS_FRONTEND)
rm -f $@
$(AR) $(AROPT) $@ $^
#
# Files in libpgcommon.a should use/export the "xxx_private" versions
# of pg_char_to_encoding() and friends.
#
$(OBJS_FRONTEND): CPPFLAGS += -DUSE_PRIVATE_ENCODING_FUNCS
#
# Shared library versions of object files
#

View File

@ -112,8 +112,8 @@ common_sources_frontend_static += files(
'logging.c',
)
# Build pgport once for backend, once for use in frontend binaries, and once
# for use in shared libraries
# Build pgcommon once for backend, once for use in frontend binaries, and
# once for use in shared libraries
#
# XXX: in most environments we could probably link_whole pgcommon_shlib
# against pgcommon_static, instead of compiling twice.
@ -131,6 +131,9 @@ pgcommon_variants = {
'': default_lib_args + {
'sources': common_sources_frontend_static,
'dependencies': [frontend_common_code],
# Files in libpgcommon.a should use/export the "xxx_private" versions
# of pg_char_to_encoding() and friends.
'c_args': ['-DUSE_PRIVATE_ENCODING_FUNCS'],
},
'_shlib': default_lib_args + {
'pic': true,

View File

@ -13,6 +13,9 @@
* included by libpq client programs. In particular, a libpq client
* should not assume that the encoding IDs used by the version of libpq
* it's linked to match up with the IDs declared here.
* To help prevent mistakes, relevant functions that are exported by
* libpq have a physically different name when being referenced
* statically.
*
*-------------------------------------------------------------------------
*/
@ -562,6 +565,23 @@ surrogate_pair_to_codepoint(pg_wchar first, pg_wchar second)
}
/*
* The functions in this list are exported by libpq, and we need to be sure
* that we know which calls are satisfied by libpq and which are satisfied
* by static linkage to libpgcommon. (This is because we might be using a
* libpq.so that's of a different major version and has encoding IDs that
* differ from the current version's.) The nominal function names are what
* are actually used in and exported by libpq, while the names exported by
* libpgcommon.a and libpgcommon_srv.a end in "_private".
*/
#if defined(USE_PRIVATE_ENCODING_FUNCS) || !defined(FRONTEND)
#define pg_char_to_encoding pg_char_to_encoding_private
#define pg_encoding_to_char pg_encoding_to_char_private
#define pg_valid_server_encoding pg_valid_server_encoding_private
#define pg_valid_server_encoding_id pg_valid_server_encoding_id_private
#define pg_utf_mblen pg_utf_mblen_private
#endif
/*
* These functions are considered part of libpq's exported API and
* are also declared in libpq-fe.h.