Skip to content

Commit ccc286d

Browse files
committed
Prevent accidental linking of system-supplied copies of libpq.so etc.
Back-patch commit dddfc4c, which broke LDFLAGS and related Makefile variables into two parts, one for within-build-tree library references and one for external libraries, to ensure that the order of -L flags has all of the former before all of the latter. This turns out to fix a problem recently noted on buildfarm member peripatus, that we attempted to incorporate code from libpgport.a into a shared library. That will fail on platforms that are sticky about putting non-PIC code into shared libraries. (It's quite surprising we hadn't seen such failures before, since the code in question has been like that for a long time.) I think that peripatus' problem could have been fixed with just a subset of this patch; but since the previous issue of accidentally linking to the wrong copy of a Postgres shlib seems likely to bite people in the field, let's just back-patch the whole change. Now that commit dddfc4c has survived some beta testing, I'm less afraid to back-patch it than I was at the time. This also fixes undesired inclusion of "-DFRONTEND" in pg_config's CPPFLAGS output (in 9.6 and up) and undesired inclusion of "-L../../src/common" in its LDFLAGS output (in all supported branches). Back-patch to v10 and older branches; this is already in v11. Discussion: https://postgr.es/m/20180704234304.bq2dxispefl65odz@ler-imac.local
1 parent 619dea4 commit ccc286d

File tree

29 files changed

+81
-56
lines changed

29 files changed

+81
-56
lines changed

contrib/dblink/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
MODULE_big = dblink
44
OBJS = dblink.o $(WIN32RES)
55
PG_CPPFLAGS = -I$(libpq_srcdir)
6-
SHLIB_LINK = $(libpq)
6+
SHLIB_LINK_INTERNAL = $(libpq)
77

88
EXTENSION = dblink
99
DATA = dblink--1.2.sql dblink--1.1--1.2.sql dblink--1.0--1.1.sql \

contrib/hstore_plperl/Makefile

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,18 @@ endif
2727
# dependency. This does preclude pgxs builds.
2828
ifeq ($(PORTNAME), aix)
2929
rpathdir = $(pkglibdir):$(perl_archlibexp)/CORE
30-
SHLIB_LINK += ../hstore/libhstore.exp $(perl_embed_ldflags)
30+
SHLIB_LINK_INTERNAL += ../hstore/libhstore.exp
31+
SHLIB_LINK += $(perl_embed_ldflags)
3132
endif
3233
ifeq ($(PORTNAME), win32)
3334
# these settings are the same as for plperl
3435
override CPPFLAGS += -DPLPERL_HAVE_UID_GID -Wno-comment
35-
SHLIB_LINK += ../hstore/libhstore.a $(sort $(wildcard ../../src/pl/plperl/libperl*.a))
36+
SHLIB_LINK_INTERNAL += ../hstore/libhstore.a $(sort $(wildcard ../../src/pl/plperl/libperl*.a))
3637
endif
3738

3839
ifeq ($(PORTNAME), cygwin)
39-
SHLIB_LINK += -L../hstore -l hstore $(perl_embed_ldflags)
40+
SHLIB_LINK_INTERNAL += -L../hstore -l hstore
41+
SHLIB_LINK += $(perl_embed_ldflags)
4042
endif
4143

4244
# As with plperl we need to make sure that the CORE directory is included

contrib/hstore_plpython/Makefile

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,17 @@ endif
2727
# dependency. This does preclude pgxs builds.
2828
ifeq ($(PORTNAME), aix)
2929
rpathdir = $(pkglibdir):$(python_libdir)
30-
SHLIB_LINK += ../hstore/libhstore.exp $(python_libspec) $(python_additional_libs) $(sort $(wildcard ../../src/pl/plpython/libplpython*.exp))
30+
SHLIB_LINK_INTERNAL += ../hstore/libhstore.exp $(sort $(wildcard ../../src/pl/plpython/libplpython*.exp))
31+
SHLIB_LINK += $(python_libspec) $(python_additional_libs)
3132
endif
3233
ifeq ($(PORTNAME), win32)
33-
SHLIB_LINK += ../hstore/libhstore.a $(sort $(wildcard ../../src/pl/plpython/libpython*.a)) $(sort $(wildcard ../../src/pl/plpython/libplpython*.a))
34+
SHLIB_LINK_INTERNAL += ../hstore/libhstore.a $(sort $(wildcard ../../src/pl/plpython/libplpython*.a)) $(sort $(wildcard ../../src/pl/plpython/libpython*.a))
3435
endif
3536

3637
ifeq ($(PORTNAME), cygwin)
37-
SHLIB_LINK += -L../hstore -lhstore -L../../src/pl/plpython \
38-
-lplpython$(python_majorversion) $(python_libspec)
38+
SHLIB_LINK_INTERNAL += -L../hstore -lhstore -L../../src/pl/plpython \
39+
-lplpython$(python_majorversion)
40+
SHLIB_LINK += $(python_libspec)
3941
endif
4042

4143
REGRESS_OPTS += --load-extension=hstore

contrib/ltree_plpython/Makefile

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,17 @@ endif
2727
# dependency. This does preclude pgxs builds.
2828
ifeq ($(PORTNAME), aix)
2929
rpathdir = $(pkglibdir):$(python_libdir)
30-
SHLIB_LINK += $(python_libspec) $(python_additional_libs) $(sort $(wildcard ../../src/pl/plpython/libplpython*.exp))
30+
SHLIB_LINK_INTERNAL += $(sort $(wildcard ../../src/pl/plpython/libplpython*.exp))
31+
SHLIB_LINK += $(python_libspec) $(python_additional_libs)
3132
endif
3233
ifeq ($(PORTNAME), win32)
33-
SHLIB_LINK += $(sort $(wildcard ../../src/pl/plpython/libpython*.a)) $(sort $(wildcard ../../src/pl/plpython/libplpython*.a))
34+
SHLIB_LINK_INTERNAL += $(sort $(wildcard ../../src/pl/plpython/libplpython*.a)) $(sort $(wildcard ../../src/pl/plpython/libpython*.a))
3435
endif
3536

3637
ifeq ($(PORTNAME), cygwin)
37-
SHLIB_LINK += -L../ltree -lltree -L../../src/pl/plpython \
38-
-lplpython$(python_majorversion) $(python_libspec)
38+
SHLIB_LINK_INTERNAL += -L../ltree -lltree -L../../src/pl/plpython \
39+
-lplpython$(python_majorversion)
40+
SHLIB_LINK += $(python_libspec)
3941
endif
4042

4143
REGRESS_OPTS += --load-extension=ltree

contrib/oid2name/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ PROGRAM = oid2name
77
OBJS = oid2name.o $(WIN32RES)
88

99
PG_CPPFLAGS = -I$(libpq_srcdir)
10-
PG_LIBS = $(libpq_pgport)
10+
PG_LIBS_INTERNAL = $(libpq_pgport)
1111

1212
ifdef USE_PGXS
1313
PG_CONFIG = pg_config

contrib/postgres_fdw/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ OBJS = postgres_fdw.o option.o deparse.o connection.o shippable.o $(WIN32RES)
55
PGFILEDESC = "postgres_fdw - foreign data wrapper for PostgreSQL"
66

77
PG_CPPFLAGS = -I$(libpq_srcdir)
8-
SHLIB_LINK = $(libpq)
8+
SHLIB_LINK_INTERNAL = $(libpq)
99

1010
EXTENSION = postgres_fdw
1111
DATA = postgres_fdw--1.0.sql

contrib/spi/Makefile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ DOCS = $(addsuffix .example, $(MODULES))
1717
# comment out if you want a quieter refint package for other uses
1818
PG_CPPFLAGS = -DREFINT_VERBOSE
1919

20-
LDFLAGS_SL += -L$(top_builddir)/src/port -lpgport
21-
2220
ifdef USE_PGXS
2321
PG_CONFIG = pg_config
2422
PGXS := $(shell $(PG_CONFIG) --pgxs)

contrib/vacuumlo/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ PROGRAM = vacuumlo
77
OBJS = vacuumlo.o $(WIN32RES)
88

99
PG_CPPFLAGS = -I$(libpq_srcdir)
10-
PG_LIBS = $(libpq_pgport)
10+
PG_LIBS_INTERNAL = $(libpq_pgport)
1111

1212
ifdef USE_PGXS
1313
PG_CONFIG = pg_config

src/Makefile.global.in

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -267,17 +267,26 @@ LD = @LD@
267267
with_gnu_ld = @with_gnu_ld@
268268
ld_R_works = @ld_R_works@
269269

270-
# We want -L for libpgport.a and libpgcommon.a to be first in LDFLAGS. We
271-
# also need LDFLAGS to be a "recursively expanded" variable, else adjustments
272-
# to rpathdir don't work right. So we must NOT do LDFLAGS := something,
273-
# meaning this has to be done first and elsewhere we must only do LDFLAGS +=
274-
# something.
270+
# It's critical that within LDFLAGS, all -L switches pointing to build-tree
271+
# directories come before any -L switches pointing to external directories.
272+
# Otherwise it's possible for, e.g., a platform-provided copy of libpq.so
273+
# to get linked in place of the one we've built. Therefore we adopt the
274+
# convention that the first component of LDFLAGS is an extra variable
275+
# LDFLAGS_INTERNAL, and -L and -l switches for PG's own libraries must be
276+
# put into LDFLAGS_INTERNAL, so they will appear ahead of those for external
277+
# libraries.
278+
#
279+
# We need LDFLAGS and LDFLAGS_INTERNAL to be "recursively expanded" variables,
280+
# else adjustments to, e.g., rpathdir don't work right. So we must NOT do
281+
# "LDFLAGS := something" anywhere, ditto for LDFLAGS_INTERNAL.
282+
# These initial assignments must be "=" type, and elsewhere we must only do
283+
# "LDFLAGS += something" or "LDFLAGS_INTERNAL += something".
275284
ifdef PGXS
276-
LDFLAGS = -L$(libdir)
285+
LDFLAGS_INTERNAL = -L$(libdir)
277286
else
278-
LDFLAGS = -L$(top_builddir)/src/port -L$(top_builddir)/src/common
287+
LDFLAGS_INTERNAL = -L$(top_builddir)/src/port -L$(top_builddir)/src/common
279288
endif
280-
LDFLAGS += @LDFLAGS@
289+
LDFLAGS = $(LDFLAGS_INTERNAL) @LDFLAGS@
281290

282291
LDFLAGS_EX = @LDFLAGS_EX@
283292
# LDFLAGS_SL might have already been assigned by calling makefile

src/Makefile.shlib

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,16 @@
2020
#
2121
# NAME Name of library to build (no suffix nor "lib" prefix)
2222
# OBJS List of object files to include in library
23-
# SHLIB_LINK If shared library relies on other libraries,
24-
# additional stuff to put in its link command
23+
# SHLIB_LINK Stuff to append to library's link command
24+
# (typically, -L and -l switches for external libraries)
25+
# SHLIB_LINK_INTERNAL -L and -l switches for Postgres-supplied libraries
2526
# SHLIB_PREREQS Order-only prerequisites for library build target
2627
# SHLIB_EXPORTS (optional) Name of file containing list of symbols to
2728
# export, in the format "function_name number"
2829
#
30+
# Don't use SHLIB_LINK for references to files in the build tree, or the
31+
# wrong things will happen --- use SHLIB_LINK_INTERNAL for those!
32+
#
2933
# When building a shared library, the following version information
3034
# must also be set. It should be omitted when building a dynamically
3135
# loadable module.
@@ -70,6 +74,8 @@
7074
COMPILER = $(CC) $(CFLAGS)
7175
LINK.static = $(AR) $(AROPT)
7276

77+
LDFLAGS_INTERNAL += $(SHLIB_LINK_INTERNAL)
78+
7379

7480

7581
ifdef SO_MAJOR_VERSION

0 commit comments

Comments
 (0)