Commit e9a3c047 authored by Tom Lane's avatar Tom Lane

Always use -fPIC, not -fpic, when building shared libraries with gcc.

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.qojdfrwkkqnjb3ap@msg.df7cb.de
parent 2712da8b
...@@ -63,10 +63,10 @@ ...@@ -63,10 +63,10 @@
<listitem> <listitem>
<para> <para>
The compiler flag to create <acronym>PIC</acronym> is The compiler flag to create <acronym>PIC</acronym> is
<option>-fpic</option>. To create shared libraries the compiler <option>-fPIC</option>. To create shared libraries the compiler
flag is <option>-shared</option>. flag is <option>-shared</option>.
<programlisting> <programlisting>
gcc -fpic -c foo.c gcc -fPIC -c foo.c
gcc -shared -o foo.so foo.o gcc -shared -o foo.so foo.o
</programlisting> </programlisting>
This is applicable as of version 3.0 of This is applicable as of version 3.0 of
...@@ -84,14 +84,14 @@ gcc -shared -o foo.so foo.o ...@@ -84,14 +84,14 @@ gcc -shared -o foo.so foo.o
<para> <para>
The compiler flag of the system compiler to create The compiler flag of the system compiler to create
<acronym>PIC</acronym> is <option>+z</option>. When using <acronym>PIC</acronym> is <option>+z</option>. When using
<application>GCC</application> it's <option>-fpic</option>. The <application>GCC</application> it's <option>-fPIC</option>. The
linker flag for shared libraries is <option>-b</option>. So: linker flag for shared libraries is <option>-b</option>. So:
<programlisting> <programlisting>
cc +z -c foo.c cc +z -c foo.c
</programlisting> </programlisting>
or: or:
<programlisting> <programlisting>
gcc -fpic -c foo.c gcc -fPIC -c foo.c
</programlisting> </programlisting>
and then: and then:
<programlisting> <programlisting>
...@@ -112,13 +112,11 @@ ld -b -o foo.sl foo.o ...@@ -112,13 +112,11 @@ ld -b -o foo.sl foo.o
<listitem> <listitem>
<para> <para>
The compiler flag to create <acronym>PIC</acronym> is The compiler flag to create <acronym>PIC</acronym> is
<option>-fpic</option>. On some platforms in some situations <option>-fPIC</option>.
<option>-fPIC</option> must be used if <option>-fpic</option>
does not work. Refer to the GCC manual for more information.
The compiler flag to create a shared library is The compiler flag to create a shared library is
<option>-shared</option>. A complete example looks like this: <option>-shared</option>. A complete example looks like this:
<programlisting> <programlisting>
cc -fpic -c foo.c cc -fPIC -c foo.c
cc -shared -o foo.so foo.o cc -shared -o foo.so foo.o
</programlisting> </programlisting>
</para> </para>
...@@ -149,12 +147,12 @@ cc -bundle -flat_namespace -undefined suppress -o foo.so foo.o ...@@ -149,12 +147,12 @@ cc -bundle -flat_namespace -undefined suppress -o foo.so foo.o
<listitem> <listitem>
<para> <para>
The compiler flag to create <acronym>PIC</acronym> is The compiler flag to create <acronym>PIC</acronym> is
<option>-fpic</option>. For <acronym>ELF</acronym> systems, the <option>-fPIC</option>. For <acronym>ELF</acronym> systems, the
compiler with the flag <option>-shared</option> is used to link compiler with the flag <option>-shared</option> is used to link
shared libraries. On the older non-ELF systems, <literal>ld shared libraries. On the older non-ELF systems, <literal>ld
-Bshareable</literal> is used. -Bshareable</literal> is used.
<programlisting> <programlisting>
gcc -fpic -c foo.c gcc -fPIC -c foo.c
gcc -shared -o foo.so foo.o gcc -shared -o foo.so foo.o
</programlisting> </programlisting>
</para> </para>
...@@ -169,10 +167,10 @@ gcc -shared -o foo.so foo.o ...@@ -169,10 +167,10 @@ gcc -shared -o foo.so foo.o
<listitem> <listitem>
<para> <para>
The compiler flag to create <acronym>PIC</acronym> is The compiler flag to create <acronym>PIC</acronym> is
<option>-fpic</option>. <literal>ld -Bshareable</literal> is <option>-fPIC</option>. <literal>ld -Bshareable</literal> is
used to link shared libraries. used to link shared libraries.
<programlisting> <programlisting>
gcc -fpic -c foo.c gcc -fPIC -c foo.c
ld -Bshareable -o foo.so foo.o ld -Bshareable -o foo.so foo.o
</programlisting> </programlisting>
</para> </para>
...@@ -188,7 +186,7 @@ ld -Bshareable -o foo.so foo.o ...@@ -188,7 +186,7 @@ ld -Bshareable -o foo.so foo.o
<para> <para>
The compiler flag to create <acronym>PIC</acronym> is The compiler flag to create <acronym>PIC</acronym> is
<option>-KPIC</option> with the Sun compiler and <option>-KPIC</option> with the Sun compiler and
<option>-fpic</option> with <application>GCC</>. To <option>-fPIC</option> with <application>GCC</>. To
link shared libraries, the compiler option is link shared libraries, the compiler option is
<option>-G</option> with either compiler or alternatively <option>-G</option> with either compiler or alternatively
<option>-shared</option> with <application>GCC</>. <option>-shared</option> with <application>GCC</>.
...@@ -198,7 +196,7 @@ cc -G -o foo.so foo.o ...@@ -198,7 +196,7 @@ cc -G -o foo.so foo.o
</programlisting> </programlisting>
or or
<programlisting> <programlisting>
gcc -fpic -c foo.c gcc -fPIC -c foo.c
gcc -G -o foo.so foo.o gcc -G -o foo.so foo.o
</programlisting> </programlisting>
</para> </para>
......
AROPT = crs AROPT = crs
export_dynamic = -Wl,-E export_dynamic = -Wl,-E
# Use --enable-new-dtags to generate DT_RUNPATH instead of DT_RPATH. # Use --enable-new-dtags to generate DT_RUNPATH instead of DT_RPATH.
# This allows LD_LIBRARY_PATH to still work when needed. # This allows LD_LIBRARY_PATH to still work when needed.
rpath = -Wl,-rpath,'$(rpathdir)',--enable-new-dtags rpath = -Wl,-rpath,'$(rpathdir)',--enable-new-dtags
DLSUFFIX = .so DLSUFFIX = .so
ifeq "$(findstring sparc,$(host_cpu))" "sparc"
CFLAGS_SL = -fPIC CFLAGS_SL = -fPIC
else
CFLAGS_SL = -fpic
endif
# Rule for building a shared library from a single .o file # Rule for building a shared library from a single .o file
%.so: %.o %.so: %.o
......
...@@ -9,11 +9,7 @@ endif ...@@ -9,11 +9,7 @@ endif
DLSUFFIX = .so DLSUFFIX = .so
ifeq ($(findstring sparc,$(host_cpu)), sparc)
CFLAGS_SL = -fPIC -DPIC CFLAGS_SL = -fPIC -DPIC
else
CFLAGS_SL = -fpic -DPIC
endif
# Rule for building a shared library from a single .o file # Rule for building a shared library from a single .o file
......
...@@ -7,11 +7,7 @@ endif ...@@ -7,11 +7,7 @@ endif
DLSUFFIX = .so DLSUFFIX = .so
ifeq ($(findstring sparc,$(host_cpu)), sparc)
CFLAGS_SL = -fPIC -DPIC CFLAGS_SL = -fPIC -DPIC
else
CFLAGS_SL = -fpic -DPIC
endif
# Rule for building a shared library from a single .o file # Rule for building a shared library from a single .o file
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment