Skip to content

Commit e3d77ea

Browse files
committed
Prevent mis-linking of src/port and src/common functions on *BSD.
On ELF-based platforms (and maybe others?) it's possible for a shared library, when dynamically loaded into the backend, to call the backend versions of src/port and src/common functions rather than the frontend versions that are actually linked into the shlib. This is the cause of bug #15367 from Jeremy Evans, and is likely to lead to more problems in future; it's accidental that we've failed to notice any bad effects up to now. The recommended way to fix this on ELF-based platforms is to use a linker "version script" that makes the shlib's versions of the functions local. (Apparently, -Bsymbolic would fix it as well, but with other side effects that we don't want.) Doing so has the additional benefit that we can make sure the shlib only exposes the symbols that are meant to be part of its API, and not ones that are just for cross-file references within the shlib. So we'd already been using a version script for libpq on popular platforms, but it's now apparent that it's necessary for correctness on every ELF-based platform. Hence, add appropriate logic to the openbsd, freebsd, and netbsd stanzas of Makefile.shlib; this is just a copy-and-paste from the linux stanza. There may be additional work to do if commit ed0cdf0 reveals that the problem exists elsewhere, but this is all that is known to be needed right now. Back-patch to v10 where SCRAM support came in. The problem is ancient, but analysis suggests that there were no really severe consequences in older branches. Hence, I won't take the risk of such a large change in the build process for older branches. In passing, remove a rather opaque comment about -Bsymbolic; I don't think it's very on-point about why we don't use that, if indeed that's what it's talking about at all. Patch by me; thanks to Andrew Gierth for helping to diagnose the problem, and for additional testing. Discussion: https://postgr.es/m/153626613985.23143.4743626885618266803@wrigleys.postgresql.org
1 parent cf98467 commit e3d77ea

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

src/Makefile.shlib

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,6 @@
6464
#
6565
# Got that? Look at src/interfaces/libpq/Makefile for an example.
6666
#
67-
# While the linker allows creation of most shared libraries,
68-
# -Bsymbolic requires resolution of all symbols, making the
69-
# compiler a better choice for shared library creation on ELF platforms.
70-
# With the linker, -Bsymbolic requires the crt1.o startup object file.
71-
# bjm 2001-02-10
7267

7368

7469
COMPILER = $(CC) $(CFLAGS)
@@ -149,6 +144,11 @@ ifeq ($(PORTNAME), openbsd)
149144
ifdef soname
150145
LINK.shared += -Wl,-x,-soname,$(soname)
151146
endif
147+
BUILD.exports = ( echo '{ global:'; $(AWK) '/^[^\#]/ {printf "%s;\n",$$1}' $<; echo ' local: *; };' ) >$@
148+
exports_file = $(SHLIB_EXPORTS:%.txt=%.list)
149+
ifneq (,$(exports_file))
150+
LINK.shared += -Wl,--version-script=$(exports_file)
151+
endif
152152
SHLIB_LINK += -lc
153153
else
154154
LINK.shared = $(LD) -x -Bshareable -Bforcearchive
@@ -164,6 +164,11 @@ ifeq ($(PORTNAME), freebsd)
164164
ifdef soname
165165
LINK.shared += -Wl,-x,-soname,$(soname)
166166
endif
167+
BUILD.exports = ( echo '{ global:'; $(AWK) '/^[^\#]/ {printf "%s;\n",$$1}' $<; echo ' local: *; };' ) >$@
168+
exports_file = $(SHLIB_EXPORTS:%.txt=%.list)
169+
ifneq (,$(exports_file))
170+
LINK.shared += -Wl,--version-script=$(exports_file)
171+
endif
167172
else
168173
ifdef SO_MAJOR_VERSION
169174
shlib = lib$(NAME)$(DLSUFFIX).$(SO_MAJOR_VERSION).$(SO_MINOR_VERSION)
@@ -178,6 +183,11 @@ ifeq ($(PORTNAME), netbsd)
178183
ifdef soname
179184
LINK.shared += -Wl,-x,-soname,$(soname)
180185
endif
186+
BUILD.exports = ( echo '{ global:'; $(AWK) '/^[^\#]/ {printf "%s;\n",$$1}' $<; echo ' local: *; };' ) >$@
187+
exports_file = $(SHLIB_EXPORTS:%.txt=%.list)
188+
ifneq (,$(exports_file))
189+
LINK.shared += -Wl,--version-script=$(exports_file)
190+
endif
181191
else
182192
LINK.shared = $(LD) -x -Bshareable -Bforcearchive
183193
endif

0 commit comments

Comments
 (0)