Always use -fPIC, not -fpic, when building shared libraries with gcc.
authorTom Lane
Thu, 1 Jun 2017 17:32:56 +0000 (13:32 -0400)
committerTom Lane
Thu, 1 Jun 2017 17:32:56 +0000 (13:32 -0400)
On some platforms, -fpic fails for sufficiently large shared libraries.
We've mostly not hit that boundary yet, but there are some extensions
such as Citus and pglogical where it's becoming a problem.  A bit of
research suggests that the penalty for -fPIC is small, in the
single-digit-percentage range --- and there's none at all on popular
platforms such as x86_64.  So let's just default to -fPIC everywhere
and provide one less thing for extension developers to worry about.

Per complaint from Christoph Berg.  Back-patch to all supported branches.
(I did not bother to touch the recently-removed Makefiles for sco and
unixware in the back branches, though.  We'd have no way to test that
it doesn't break anything on those platforms.)

Discussion: https://postgr.es/m/20170529155850[email protected]

doc/src/sgml/dfunc.sgml
src/makefiles/Makefile.linux
src/makefiles/Makefile.netbsd
src/makefiles/Makefile.openbsd

index 8e6c95d15c5933a359a48fcf7e057e51a6196c63..4b90c8b3631427286c08cf00532830ccb9b4eb00 100644 (file)
     
      
       The compiler flag to create PIC is
-      .  To create shared libraries the compiler
+      .  To create shared libraries the compiler
       flag is .
 
-gcc -fpic -c foo.c
+gcc -fPIC -c foo.c
 gcc -shared -o foo.so foo.o
 
       This is applicable as of version 3.0 of
@@ -80,14 +80,14 @@ gcc -shared -o foo.so foo.o
      
       The compiler flag of the system compiler to create
       PIC is .  When using
-      GCC it's . The
+      GCC it's . The
       linker flag for shared libraries is .  So:
 
 cc +z -c foo.c
 
       or:
 
-gcc -fpic -c foo.c
+gcc -fPIC -c foo.c
 
       and then:
 
@@ -122,13 +122,11 @@ ld -shared -o foo.so foo.o
     
      
       The compiler flag to create PIC is
-      .  On some platforms in some situations
-       must be used if 
-      does not work.  Refer to the GCC manual for more information.
+      .
       The compiler flag to create a shared library is
       .  A complete example looks like this:
 
-cc -fpic -c foo.c
+cc -fPIC -c foo.c
 cc -shared -o foo.so foo.o
 
      
@@ -155,12 +153,12 @@ cc -bundle -flat_namespace -undefined suppress -o foo.so foo.o
     
      
       The compiler flag to create PIC is
-      .  For ELF systems, the
+      .  For ELF systems, the
       compiler with the flag  is used to link
       shared libraries.  On the older non-ELF systems, ld
       -Bshareable is used.
 
-gcc -fpic -c foo.c
+gcc -fPIC -c foo.c
 gcc -shared -o foo.so foo.o
 
      
@@ -173,10 +171,10 @@ gcc -shared -o foo.so foo.o
     
      
       The compiler flag to create PIC is
-      .  ld -Bshareable is
+      .  ld -Bshareable is
       used to link shared libraries.
 
-gcc -fpic -c foo.c
+gcc -fPIC -c foo.c
 ld -Bshareable -o foo.so foo.o
 
      
@@ -190,7 +188,7 @@ ld -Bshareable -o foo.so foo.o
      
       The compiler flag to create PIC is
        with the Sun compiler and
-       with GCC.  To
+       with GCC.  To
       link shared libraries, the compiler option is
        with either compiler or alternatively
        with GCC.
@@ -200,7 +198,7 @@ cc -G -o foo.so foo.o
 
       or
 
-gcc -fpic -c foo.c
+gcc -fPIC -c foo.c
 gcc -G -o foo.so foo.o
 
      
index 52bf0b1e2bacacb9a225309fe58b00a9c03e62f6..f4f091caef59362c6edbe797d6cc39e6e8a1e43c 100644 (file)
@@ -1,15 +1,14 @@
 AROPT = crs
+
 export_dynamic = -Wl,-E
 # Use --enable-new-dtags to generate DT_RUNPATH instead of DT_RPATH.
 # This allows LD_LIBRARY_PATH to still work when needed.
 rpath = -Wl,-rpath,'$(rpathdir)',--enable-new-dtags
+
 DLSUFFIX = .so
 
-ifeq "$(findstring sparc,$(host_cpu))" "sparc"
 CFLAGS_SL = -fPIC
-else
-CFLAGS_SL = -fpic
-endif
+
 
 # Rule for building a shared library from a single .o file
 %.so: %.o
index 31a52601aff07cc0cd85e03c40dc4d4ee35603b7..43841c15973b0c0b594fc2333f2bbdea4bcd71ae 100644 (file)
@@ -9,11 +9,7 @@ endif
 
 DLSUFFIX = .so
 
-ifeq ($(findstring sparc,$(host_cpu)), sparc)
 CFLAGS_SL = -fPIC -DPIC
-else
-CFLAGS_SL = -fpic -DPIC
-endif
 
 
 # Rule for building a shared library from a single .o file
index 7bf549330905b5156bfd3f22ee0ce27bbb906a12..d8fde49d5c8d95ee73d4c507d12a50ac94bbb7c9 100644 (file)
@@ -7,11 +7,7 @@ endif
 
 DLSUFFIX = .so
 
-ifeq ($(findstring sparc,$(host_cpu)), sparc)
 CFLAGS_SL = -fPIC -DPIC
-else
-CFLAGS_SL = -fpic -DPIC
-endif
 
 
 # Rule for building a shared library from a single .o file