Skip to content

Commit f6f7553

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 56535dc commit f6f7553

File tree

19 files changed

+51
-33
lines changed

19 files changed

+51
-33
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
55
PG_CPPFLAGS = -I$(libpq_srcdir)
6-
SHLIB_LINK = $(libpq)
6+
SHLIB_LINK_INTERNAL = $(libpq)
77
SHLIB_PREREQS = submake-libpq
88

99
EXTENSION = dblink

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
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/pgbench/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ PROGRAM = pgbench
77
OBJS = pgbench.o
88

99
PG_CPPFLAGS = -I$(libpq_srcdir)
10-
PG_LIBS = $(libpq_pgport) $(PTHREAD_LIBS)
10+
PG_LIBS_INTERNAL = $(libpq_pgport)
11+
PG_LIBS = $(PTHREAD_LIBS)
1112

1213
ifdef USE_PGXS
1314
PG_CONFIG = pg_config

contrib/postgres_fdw/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ MODULE_big = postgres_fdw
44
OBJS = postgres_fdw.o option.o deparse.o connection.o
55

66
PG_CPPFLAGS = -I$(libpq_srcdir)
7-
SHLIB_LINK = $(libpq)
7+
SHLIB_LINK_INTERNAL = $(libpq)
88
SHLIB_PREREQS = submake-libpq
99

1010
EXTENSION = postgres_fdw

contrib/spi/Makefile

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

19-
LDFLAGS_SL += -L$(top_builddir)/src/port -lpgport
20-
2119
ifdef USE_PGXS
2220
PG_CONFIG = pg_config
2321
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
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
@@ -245,17 +245,26 @@ LD = @LD@
245245
with_gnu_ld = @with_gnu_ld@
246246
ld_R_works = @ld_R_works@
247247

248-
# We want -L for libpgport.a and libpgcommon.a to be first in LDFLAGS. We
249-
# also need LDFLAGS to be a "recursively expanded" variable, else adjustments
250-
# to rpathdir don't work right. So we must NOT do LDFLAGS := something,
251-
# meaning this has to be done first and elsewhere we must only do LDFLAGS +=
252-
# something.
248+
# It's critical that within LDFLAGS, all -L switches pointing to build-tree
249+
# directories come before any -L switches pointing to external directories.
250+
# Otherwise it's possible for, e.g., a platform-provided copy of libpq.so
251+
# to get linked in place of the one we've built. Therefore we adopt the
252+
# convention that the first component of LDFLAGS is an extra variable
253+
# LDFLAGS_INTERNAL, and -L and -l switches for PG's own libraries must be
254+
# put into LDFLAGS_INTERNAL, so they will appear ahead of those for external
255+
# libraries.
256+
#
257+
# We need LDFLAGS and LDFLAGS_INTERNAL to be "recursively expanded" variables,
258+
# else adjustments to, e.g., rpathdir don't work right. So we must NOT do
259+
# "LDFLAGS := something" anywhere, ditto for LDFLAGS_INTERNAL.
260+
# These initial assignments must be "=" type, and elsewhere we must only do
261+
# "LDFLAGS += something" or "LDFLAGS_INTERNAL += something".
253262
ifdef PGXS
254-
LDFLAGS = -L$(libdir)
263+
LDFLAGS_INTERNAL = -L$(libdir)
255264
else
256-
LDFLAGS = -L$(top_builddir)/src/port -L$(top_builddir)/src/common
265+
LDFLAGS_INTERNAL = -L$(top_builddir)/src/port -L$(top_builddir)/src/common
257266
endif
258-
LDFLAGS += @LDFLAGS@
267+
LDFLAGS = $(LDFLAGS_INTERNAL) @LDFLAGS@
259268

260269
LDFLAGS_EX = @LDFLAGS_EX@
261270
# 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.
@@ -77,6 +81,8 @@
7781
COMPILER = $(CC) $(CFLAGS)
7882
LINK.static = $(AR) $(AROPT)
7983

84+
LDFLAGS_INTERNAL += $(SHLIB_LINK_INTERNAL)
85+
8086

8187

8288
ifdef SO_MAJOR_VERSION

src/backend/replication/libpqwalreceiver/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ include $(top_builddir)/src/Makefile.global
1515
override CPPFLAGS := -I$(srcdir) -I$(libpq_srcdir) $(CPPFLAGS)
1616

1717
OBJS = libpqwalreceiver.o
18-
SHLIB_LINK = $(libpq)
18+
SHLIB_LINK_INTERNAL = $(libpq)
1919
SHLIB_PREREQS = submake-libpq
2020
NAME = libpqwalreceiver
2121

src/bin/pg_config/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ OBJS= pg_config.o $(WIN32RES)
1919

2020
# don't include subdirectory-path-dependent -I and -L switches
2121
STD_CPPFLAGS := $(filter-out -I$(top_srcdir)/src/include -I$(top_builddir)/src/include,$(CPPFLAGS))
22-
STD_LDFLAGS := $(filter-out -L$(top_builddir)/src/port,$(LDFLAGS))
22+
STD_LDFLAGS := $(filter-out -L$(top_builddir)/src/common -L$(top_builddir)/src/port,$(LDFLAGS))
2323

2424
override CPPFLAGS += -DVAL_CONFIGURE="\"$(configure_args)\""
2525
override CPPFLAGS += -DVAL_CC="\"$(CC)\""

0 commit comments

Comments
 (0)