]> granicus.if.org Git - postgresql/commitdiff
Ensure link commands list *.o files before LDFLAGS.
authorTom Lane <tgl@sss.pgh.pa.us>
Wed, 2 Jan 2019 18:57:42 +0000 (13:57 -0500)
committerTom Lane <tgl@sss.pgh.pa.us>
Wed, 2 Jan 2019 18:57:54 +0000 (13:57 -0500)
It's important for link commands to list *.o input files before -l
switches for libraries, as library code may not get pulled into the link
unless referenced by an earlier command-line entry.  This is certainly
necessary for static libraries (.a style).  Apparently on some platforms
it is also necessary for shared libraries, as reported by Donald Dong.

We often put -l switches for within-tree libraries into LDFLAGS, meaning
that link commands that list *.o files after LDFLAGS are hazardous.
Most of our link commands got this right, but a few did not.  In
particular, places that relied on gmake's default implicit link rule
failed, because that puts LDFLAGS first.  Fix that by overriding the
built-in rule with our own.  The implicit link rules in
src/makefiles/Makefile.* for single-.o-file shared libraries mostly
got this wrong too, so fix them.  I also changed the link rules for the
backend and a couple of other places for consistency, even though they
are not (currently) at risk because they aren't adding any -l switches
to LDFLAGS.

Arguably, the real problem here is that we're abusing LDFLAGS by
putting -l switches in it and we should stop doing that.  But changing
that would be quite invasive, so I'm not eager to do so.

Perhaps this is a candidate for back-patching, but so far it seems
that problems can only be exhibited in test code we don't normally
build, and at least some of the problems are new in HEAD anyway.
So I'll refrain for now.

Donald Dong and Tom Lane

Discussion: https://postgr.es/m/CAKABAquXn-BF-vBeRZxhzvPyfMqgGuc74p8BmQZyCFDpyROBJQ@mail.gmail.com

14 files changed:
src/Makefile.global.in
src/backend/Makefile
src/interfaces/ecpg/preproc/Makefile
src/interfaces/ecpg/test/Makefile
src/interfaces/ecpg/test/Makefile.regress
src/makefiles/Makefile.aix
src/makefiles/Makefile.darwin
src/makefiles/Makefile.freebsd
src/makefiles/Makefile.hpux
src/makefiles/Makefile.linux
src/makefiles/Makefile.netbsd
src/makefiles/Makefile.openbsd
src/makefiles/Makefile.solaris
src/test/thread/Makefile

index 4b6bab37dc9cdc5fccf2158a4036bde5d12d901b..758ea4357a7219c12081e7c02d5dd90e2e42b662 100644 (file)
@@ -742,6 +742,13 @@ endif
 # tracking (see below) is used.
 %: %.c
 
+# Replace gmake's default rule for linking a single .o file to produce an
+# executable.  The main point here is to put LDFLAGS after the .o file,
+# since we put -l switches into LDFLAGS and those are order-sensitive.
+# In addition, include CFLAGS and LDFLAGS_EX per project conventions.
+%: %.o
+       $(CC) $(CFLAGS) $< $(LDFLAGS) $(LDFLAGS_EX) $(LIBS) -o $@$(X)
+
 ifndef PGXS
 
 # Remake Makefile.global from Makefile.global.in if the latter
index e05f55cad49b4fcea232f3868df6788cf7948761..478a96db9bcbbe019373c2eae17a68bd324b7880 100644 (file)
@@ -60,7 +60,7 @@ ifneq ($(PORTNAME), win32)
 ifneq ($(PORTNAME), aix)
 
 postgres: $(OBJS)
-       $(CC) $(CFLAGS) $(LDFLAGS) $(LDFLAGS_EX) $(export_dynamic) $(call expand_subsys,$^) $(LIBS) -o $@
+       $(CC) $(CFLAGS) $(call expand_subsys,$^) $(LDFLAGS) $(LDFLAGS_EX) $(export_dynamic) $(LIBS) -o $@
 
 endif
 endif
@@ -69,7 +69,7 @@ endif
 ifeq ($(PORTNAME), cygwin)
 
 postgres: $(OBJS)
-       $(CC) $(CFLAGS) $(LDFLAGS) $(LDFLAGS_EX) $(export_dynamic) -Wl,--stack,$(WIN32_STACK_RLIMIT) -Wl,--export-all-symbols -Wl,--out-implib=libpostgres.a $(call expand_subsys,$^) $(LIBS) -o $@
+       $(CC) $(CFLAGS) $(call expand_subsys,$^) $(LDFLAGS) $(LDFLAGS_EX) $(export_dynamic) -Wl,--stack,$(WIN32_STACK_RLIMIT) -Wl,--export-all-symbols -Wl,--out-implib=libpostgres.a $(LIBS) -o $@
 
 # libpostgres.a is actually built in the preceding rule, but we need this to
 # ensure it's newer than postgres; see notes in src/backend/parser/Makefile
@@ -82,7 +82,7 @@ ifeq ($(PORTNAME), win32)
 LIBS += -lsecur32
 
 postgres: $(OBJS) $(WIN32RES)
-       $(CC) $(CFLAGS) $(LDFLAGS) $(LDFLAGS_EX) -Wl,--stack=$(WIN32_STACK_RLIMIT) -Wl,--export-all-symbols -Wl,--out-implib=libpostgres.a $(call expand_subsys,$(OBJS)) $(WIN32RES) $(LIBS) -o $@$(X)
+       $(CC) $(CFLAGS) $(call expand_subsys,$(OBJS)) $(WIN32RES) $(LDFLAGS) $(LDFLAGS_EX) -Wl,--stack=$(WIN32_STACK_RLIMIT) -Wl,--export-all-symbols -Wl,--out-implib=libpostgres.a $(LIBS) -o $@$(X)
 
 # libpostgres.a is actually built in the preceding rule, but we need this to
 # ensure it's newer than postgres; see notes in src/backend/parser/Makefile
@@ -94,7 +94,7 @@ endif # win32
 ifeq ($(PORTNAME), aix)
 
 postgres: $(POSTGRES_IMP)
-       $(CC) $(CFLAGS) $(LDFLAGS) $(LDFLAGS_EX) $(call expand_subsys,$(OBJS)) -Wl,-bE:$(top_builddir)/src/backend/$(POSTGRES_IMP) $(LIBS) -Wl,-brtllib -o $@
+       $(CC) $(CFLAGS) $(call expand_subsys,$(OBJS)) $(LDFLAGS) $(LDFLAGS_EX) -Wl,-bE:$(top_builddir)/src/backend/$(POSTGRES_IMP) $(LIBS) -Wl,-brtllib -o $@
 
 $(POSTGRES_IMP): $(OBJS)
        $(LD) $(LDREL) $(LDOUT) SUBSYS.o $(call expand_subsys,$^)
@@ -117,7 +117,7 @@ $(top_builddir)/src/port/libpgport_srv.a: | submake-libpgport
 # The postgres.o target is needed by the rule in Makefile.global that
 # creates the exports file when MAKE_EXPORTS = true.
 postgres.o: $(OBJS)
-       $(CC) $(LDREL) $(LDFLAGS) $(LDFLAGS_EX) $(call expand_subsys,$^) $(LIBS) -o $@
+       $(CC) $(LDREL) $(call expand_subsys,$^) $(LDFLAGS) $(LDFLAGS_EX) $(LIBS) -o $@
 
 
 # The following targets are specified in make commands that appear in
@@ -319,4 +319,4 @@ maintainer-clean: distclean
 # are up to date.  It saves the time of doing all the submakes.
 .PHONY: quick
 quick: $(OBJS)
-       $(CC) $(CFLAGS) $(LDFLAGS) $(LDFLAGS_EX) $(export_dynamic) $(call expand_subsys,$^) $(LIBS) -o postgres
+       $(CC) $(CFLAGS) $(call expand_subsys,$^) $(LDFLAGS) $(LDFLAGS_EX) $(export_dynamic) $(LIBS) -o postgres
index 911d03dbd148a8d6f5b1201474ed808252115968..69ddd8e9f71e5779b7ff8a91310012d3e8780c14 100644 (file)
@@ -37,7 +37,7 @@ endif
 all: ecpg
 
 ecpg: $(OBJS) | submake-libpgport
-       $(CC) $(CFLAGS) $(LDFLAGS) $(LDFLAGS_EX) $^ $(LIBS) $(PTHREAD_LIBS) -o $@$(X)
+       $(CC) $(CFLAGS) $^ $(LDFLAGS) $(LDFLAGS_EX) $(LIBS) $(PTHREAD_LIBS) -o $@$(X)
 
 # We symlink typename.c from ecpglib and recompile it here
 typename.c: % : $(top_srcdir)/src/interfaces/ecpg/ecpglib/%
index c761a4dcb0d0a6cdd29a51e31cf21d610d7782dc..be53b7b94dc610255b0e4a7a5e256219113663a8 100644 (file)
@@ -49,7 +49,7 @@ clean distclean maintainer-clean:
 all: pg_regress$(X)
 
 pg_regress$(X): pg_regress_ecpg.o $(WIN32RES) $(top_builddir)/src/test/regress/pg_regress.o
-       $(CC) $(CFLAGS) $(LDFLAGS) $(LDFLAGS_EX) $^ $(LIBS) -o $@
+       $(CC) $(CFLAGS) $^ $(LDFLAGS) $(LDFLAGS_EX) $(LIBS) -o $@
 
 $(top_builddir)/src/test/regress/pg_regress.o:
        $(MAKE) -C $(dir $@) $(notdir $@)
index 4da1bb8a03692f6a22dd75bffb5fa9fd62e09c86..b0647cd2c5fdfd0c5565fc27b3436ddb0845b8d1 100644 (file)
@@ -21,9 +21,6 @@ ECPG_TEST_DEPENDENCIES = ../../preproc/ecpg$(X) \
        $(srcdir)/../../include/sqltypes.h \
        $(srcdir)/../../include/sql3types.h
 
-%: %.o
-       $(CC) $(CFLAGS) $< $(LDFLAGS) $(LDFLAGS_EX) $(LIBS) -o $@
-
 # Caution: this build rule is overridden in some child Makefiles
 # where it's necessary to use nondefault switches to ecpg;
 # make sure those rules match except for the extra switches.
index e5ad89d147007d4f6811e2c7f86e1a61be7dae62..0f6c0289384d579ef68d50aac7325d91abb7bcfc 100644 (file)
@@ -40,4 +40,4 @@ MKLDEXPORT=$(top_srcdir)/$(MKLDEXPORT_DIR)/mkldexport.sh
 
 # Rule for building a shared library from a single .o file
 %$(DLSUFFIX): %.o %.exp
-       $(CC) $(CFLAGS) $(LDFLAGS) $(LDFLAGS_SL) -o $@ $*.o -Wl,-bE:$*.exp $(BE_DLLLIBS)
+       $(CC) $(CFLAGS) $*.o $(LDFLAGS) $(LDFLAGS_SL) -o $@ -Wl,-bE:$*.exp $(BE_DLLLIBS)
index 7a8ba3e52748320c396972112d51fb3f8a965a09..e2b1d44959f90ec7693f71c22d5f6412dbbc7c4c 100644 (file)
@@ -10,4 +10,4 @@ endif
 
 # Rule for building a shared library from a single .o file
 %.so: %.o
-       $(CC) $(CFLAGS) $(LDFLAGS) $(LDFLAGS_SL) -bundle $(BE_DLLLIBS) -o $@ $<
+       $(CC) $(CFLAGS) $< $(LDFLAGS) $(LDFLAGS_SL) -bundle $(BE_DLLLIBS) -o $@
index 5a98e5a2b0bb0962f3bd38b07c22fa503d5fc9fc..ce03c8dcd26b1da0108220d7df3c243812659a34 100644 (file)
@@ -13,7 +13,7 @@ CFLAGS_SL = -fPIC -DPIC
 # Rule for building a shared library from a single .o file
 %.so: %.o
 ifdef ELF_SYSTEM
-       $(CC) $(CFLAGS) $(LDFLAGS) $(LDFLAGS_SL) -shared -o $@ $<
+       $(CC) $(CFLAGS) $< $(LDFLAGS) $(LDFLAGS_SL) -shared -o $@
 else
        $(LD) $(LDREL) $(LDOUT) $<.obj -x $<
        @echo building shared object $@
index 97bd0ba6d9e27e2c9694a0d587bfbc36b8b4b052..30dd3eb77e673b8cc728e43c98b724f980512eab 100644 (file)
@@ -40,13 +40,13 @@ endif
 %$(DLSUFFIX): %.o
 ifeq ($(GCC), yes)
   ifeq ($(with_gnu_ld), yes)
-       $(CC) $(CFLAGS) $(LDFLAGS) $(LDFLAGS_SL) -shared -o $@ $< `$(CC) $(LDFLAGS) -print-libgcc-file-name`
+       $(CC) $(CFLAGS) $< $(LDFLAGS) $(LDFLAGS_SL) -shared -o $@ `$(CC) $(LDFLAGS) -print-libgcc-file-name`
   else
        $(LD) -b -o $@ $< `$(CC) $(LDFLAGS) -print-libgcc-file-name`
   endif
 else
   ifeq ($(with_gnu_ld), yes)
-       $(CC) $(CFLAGS) $(LDFLAGS) $(LDFLAGS_SL) -shared -o $@ $<
+       $(CC) $(CFLAGS) $< $(LDFLAGS) $(LDFLAGS_SL) -shared -o $@
   else
        $(LD) -b -o $@ $<
   endif
index f4f091caef59362c6edbe797d6cc39e6e8a1e43c..ac58fe45de0c07b9fe814f09b836792696bb6d45 100644 (file)
@@ -12,4 +12,4 @@ CFLAGS_SL = -fPIC
 
 # Rule for building a shared library from a single .o file
 %.so: %.o
-       $(CC) $(CFLAGS) $(LDFLAGS) $(LDFLAGS_SL) -shared -o $@ $<
+       $(CC) $(CFLAGS) $< $(LDFLAGS) $(LDFLAGS_SL) -shared -o $@
index 43841c15973b0c0b594fc2333f2bbdea4bcd71ae..7bb9721fa5b86c5ad67cf2299d2062cfe8c94816 100644 (file)
@@ -15,7 +15,7 @@ CFLAGS_SL = -fPIC -DPIC
 # Rule for building a shared library from a single .o file
 %.so: %.o
 ifdef ELF_SYSTEM
-       $(CC) $(CFLAGS) $(LDFLAGS) $(LDFLAGS_SL) -shared -o $@ $<
+       $(CC) $(CFLAGS) $< $(LDFLAGS) $(LDFLAGS_SL) -shared -o $@
 else
        $(LD) $(LDREL) $(LDOUT) $<.obj -x $<
        @echo building shared object $@
index d8fde49d5c8d95ee73d4c507d12a50ac94bbb7c9..eda311087cb4c7a49b81e146d5653f1bffffe441 100644 (file)
@@ -13,7 +13,7 @@ CFLAGS_SL = -fPIC -DPIC
 # Rule for building a shared library from a single .o file
 %.so: %.o
 ifdef ELF_SYSTEM
-       $(CC) $(CFLAGS) $(LDFLAGS) $(LDFLAGS_SL) -shared -o $@ $<
+       $(CC) $(CFLAGS) $< $(LDFLAGS) $(LDFLAGS_SL) -shared -o $@
 else
        $(LD) $(LDREL) $(LDOUT) $<.obj -x $<
        @echo building shared object $@
index e459de30cf4933869dadeac4a443ae9c165082ed..a7f5652f0ca9fcc6b9f284bdcf17bf1c5ecc80f2 100644 (file)
@@ -19,9 +19,9 @@ endif
 # Rule for building a shared library from a single .o file
 %.so: %.o
 ifeq ($(GCC), yes)
-       $(CC) $(CFLAGS) $(LDFLAGS) $(LDFLAGS_SL) -shared -o $@ $<
+       $(CC) $(CFLAGS) $< $(LDFLAGS) $(LDFLAGS_SL) -shared -o $@
 else
-       $(CC) $(CFLAGS) $(LDFLAGS) $(LDFLAGS_SL) -G -o $@ $<
+       $(CC) $(CFLAGS) $< $(LDFLAGS) $(LDFLAGS_SL) -G -o $@
 endif
 
 sqlmansect = 5sql
index f45bbda9fd94fbabfefe28a087a05729f477b3cf..dab1a4803fe3bd22629187a7569b2199a1de8a90 100644 (file)
@@ -18,7 +18,7 @@ all: thread_test
 
 thread_test: thread_test.o
 # no need for $LIBS, might not be compiled yet
-       $(CC) $(CFLAGS) $(LDFLAGS) $(LDFLAGS_EX) $^ $(PTHREAD_LIBS) -o $@
+       $(CC) $(CFLAGS) $^ $(LDFLAGS) $(LDFLAGS_EX) $(PTHREAD_LIBS) -o $@$(X)
 
 clean distclean maintainer-clean:
        rm -f thread_test$(X) thread_test.o