use same object files for libc.a and libc.so if compiler produces PIC
authorRich Felker <dalias@aerifal.cx>
Tue, 26 Jan 2016 00:57:38 +0000 (19:57 -0500)
committerRich Felker <dalias@aerifal.cx>
Tue, 26 Jan 2016 00:57:38 +0000 (19:57 -0500)
now that .lo and .o files differ only by whether -fPIC is passed (and
no longer at the source level based on the SHARED macro), it's
possible to use the same object files for both static and shared libc
when the compiler would produce PIC for the static files anyway. this
happens if the user has included -fPIC in their CFLAGS or if the
compiler has been configured to produce PIE by default.

we use the .lo files for both, and still append -fPIC to the CFLAGS,
rather than using the .o files so that libc.so does not break
catastrophically if the user later removes -fPIC from CFLAGS in
config.mak or on the make command line. this also ensures that we get
full -fPIC in case -fpic, -fPIE, or some other lesser-PIC option was
passed in CFLAGS.

Makefile
configure

index 7c2cd77..82ea024 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -25,6 +25,7 @@ REPLACED_OBJS = $(sort $(subst /$(ARCH)/,/,$(ARCH_OBJS)))
 LDSO_SRCS = $(sort $(wildcard $(srcdir)/ldso/*.c))
 LDSO_OBJS = $(patsubst $(srcdir)/%,obj/%.lo,$(basename $(LDSO_SRCS)))
 OBJS = $(addprefix obj/, $(filter-out $(REPLACED_OBJS), $(sort $(BASE_OBJS) $(ARCH_OBJS))))
+AOBJS = $(OBJS)
 LOBJS = $(OBJS:.o=.lo)
 GENH = obj/include/bits/alltypes.h
 GENH_INT = obj/src/internal/version.h
@@ -158,9 +159,9 @@ lib/libc.so: $(LOBJS) $(LDSO_OBJS)
        -Wl,-e,_dlstart -Wl,-Bsymbolic-functions \
        -o $@ $(LOBJS) $(LDSO_OBJS) $(LIBCC)
 
-lib/libc.a: $(OBJS)
+lib/libc.a: $(AOBJS)
        rm -f $@
-       $(AR) rc $@ $(OBJS)
+       $(AR) rc $@ $(AOBJS)
        $(RANLIB) $@
 
 $(EMPTY_LIBS):
index 5b97f71..2d7ea30 100755 (executable)
--- a/configure
+++ b/configure
@@ -532,6 +532,15 @@ CFLAGS_AUTO="$CFLAGS_AUTO -include vis.h"
 CFLAGS_AUTO="${CFLAGS_AUTO# }"
 fi
 
+# Determine if the compiler produces position-independent code (PIC)
+# by default. If so, we don't need to compile separate object files
+# for libc.a and libc.so.
+if trycppif __PIC__ "$CFLAGS_C99FSE $CPPFLAGS $CFLAGS" ; then
+pic_default=yes
+else
+pic_default=no
+fi
+
 # Reduce space lost to padding for alignment purposes by sorting data
 # objects according to their alignment reqirements. This approximates
 # optimal packing.
@@ -692,6 +701,7 @@ test "x$static" = xno && echo "STATIC_LIBS ="
 test "x$shared" = xno && echo "SHARED_LIBS ="
 test "x$cc_family" = xgcc && echo 'WRAPCC_GCC = $(CC)'
 test "x$cc_family" = xclang && echo 'WRAPCC_CLANG = $(CC)'
+test "x$pic_default" = xyes && echo 'AOBJS = $(LOBJS)'
 exec 1>&3 3>&-
 
 test "$srcdir" = "." || ln -sf $srcdir/Makefile .