automake: ask the linker to do garbage collection
authorEmil Velikov <emil.l.velikov@gmail.com>
Tue, 11 Mar 2014 17:58:08 +0000 (17:58 +0000)
committerEmil Velikov <emil.l.velikov@gmail.com>
Mon, 31 Mar 2014 13:56:14 +0000 (14:56 +0100)
By doing GC the linker removes all the symbols that are not referenced
and/or used by the final library. This results in a saving of ~100K
up-to ~600K per (stripped) binary (classic vs gallium drivers).

If interested one can ask the compiler to print the sections that are
removed using -Wl,--print-gc-sections.

v2: Check if ld supports the flag before using it.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Acked-by: Matt Turner <mattst88@gmail.com> (v1)
20 files changed:
configure.ac
src/egl/main/Makefile.am
src/egl/wayland/wayland-egl/Makefile.am
src/gallium/Automake.inc
src/gallium/targets/egl-static/Makefile.am
src/gallium/targets/gbm/Makefile.am
src/gallium/targets/libgl-xlib/Makefile.am
src/gallium/targets/opencl/Makefile.am
src/gallium/targets/osmesa/Makefile.am
src/gallium/targets/pipe-loader/Makefile.am
src/gallium/targets/xa/Makefile.am
src/gbm/Makefile.am
src/glx/Makefile.am
src/mapi/es1api/Makefile.am
src/mapi/es2api/Makefile.am
src/mapi/shared-glapi/Makefile.am
src/mapi/vgapi/Makefile.am
src/mesa/drivers/dri/Makefile.am
src/mesa/drivers/osmesa/Makefile.am
src/mesa/drivers/x11/Makefile.am

index 3055de6..af1b0d1 100644 (file)
@@ -316,6 +316,22 @@ if test "x$enable_debug" = xyes; then
 fi
 
 dnl
+dnl Check if linker supports garbage collection
+dnl
+save_LDFLAGS=$LDFLAGS
+LDFLAGS="$LDFLAGS -Wl,--gc-sections"
+AC_MSG_CHECKING([whether ld supports --gc-sections])
+AC_LINK_IFELSE(
+    [AC_LANG_SOURCE([static char UnusedFunc() { return 5; } int main() { return 0;}])],
+    [AC_MSG_RESULT([yes])
+        GC_SECTIONS="-Wl,--gc-sections";],
+    [AC_MSG_RESULT([no])
+        GC_SECTIONS="";])
+LDFLAGS=$save_LDFLAGS
+
+AC_SUBST([GC_SECTIONS])
+
+dnl
 dnl compatibility symlinks
 dnl
 case "$host_os" in
index 57c9ccd..e4c2539 100644 (file)
@@ -77,6 +77,7 @@ libEGL_la_LDFLAGS = \
        -no-undefined \
        -version-number 1:0 \
        -Wl,-Bsymbolic \
+       $(GC_SECTIONS) \
        -Wl,--no-undefined
 
 if HAVE_EGL_PLATFORM_X11
index 3c2673a..d3fe117 100644 (file)
@@ -11,6 +11,7 @@ libwayland_egl_la_SOURCES = wayland-egl.c
 libwayland_egl_la_LDFLAGS = \
        -no-undefined \
        -version-info 1 \
+       $(GC_SECTIONS) \
        -Wl,--no-undefined
 
 TESTS = wayland-egl-symbols-check
index 1151f91..56f1433 100644 (file)
@@ -57,6 +57,7 @@ GALLIUM_DRI_LINKER_FLAGS = \
        -shared \
        -module \
        -avoid-version \
+       $(GC_SECTIONS) \
        -Wl,--version-script=$(DRI_VERSION_SCRIPT)
 
 GALLIUM_VDPAU_LINKER_FLAGS = \
@@ -65,6 +66,7 @@ GALLIUM_VDPAU_LINKER_FLAGS = \
        -no-undefined \
        -version-number $(VDPAU_MAJOR):$(VDPAU_MINOR) \
        -export-symbols-regex $(VDPAU_EXPORTS) \
+       $(GC_SECTIONS) \
        -Wl,--no-undefined
 
 GALLIUM_XVMC_LINKER_FLAGS = \
@@ -73,6 +75,7 @@ GALLIUM_XVMC_LINKER_FLAGS = \
        -no-undefined \
        -version-number $(XVMC_MAJOR):$(XVMC_MINOR) \
        -export-symbols-regex '^XvMC' \
+       $(GC_SECTIONS) \
        -Wl,--no-undefined
 
 GALLIUM_OMX_LINKER_FLAGS = \
@@ -80,6 +83,7 @@ GALLIUM_OMX_LINKER_FLAGS = \
        -module \
        -no-undefined \
        -export-symbols-regex $(EXPORTS) \
+       $(GC_SECTIONS) \
        -Wl,--no-undefined
 
 GALLIUM_VDPAU_LIB_DEPS = \
index 55eb3ba..58ecf69 100644 (file)
@@ -48,6 +48,7 @@ AM_LDFLAGS = \
        -module \
        -no-undefined \
        -avoid-version \
+       $(GC_SECTIONS) \
        -Wl,--no-undefined \
        -Wl,--version-script=$(top_srcdir)/src/gallium/targets/egl-static/egl.link
 
index c109ddb..22b4826 100644 (file)
@@ -70,6 +70,7 @@ gbm_gallium_drm_la_LDFLAGS = \
        -module \
        -no-undefined \
        -avoid-version \
+       $(GC_SECTIONS) \
        -Wl,--no-undefined
 
 if HAVE_MESA_LLVM
index d7aec7b..ef3d23e 100644 (file)
@@ -47,6 +47,7 @@ libGL_la_SOURCES = xlib.c
 libGL_la_LDFLAGS = \
        -no-undefined \
        -version-number $(GL_MAJOR):$(GL_MINOR):$(GL_TINY) \
+       $(GC_SECTIONS) \
        -Wl,--no-undefined
 
 libGL_la_LIBADD = \
index b401d10..aae31ff 100644 (file)
@@ -6,6 +6,7 @@ lib@OPENCL_LIBNAME@_la_LDFLAGS = \
        $(LLVM_LDFLAGS) \
        -no-undefined \
        -version-number 1:0 \
+       $(GC_SECTIONS) \
        -Wl,--no-undefined
 
 
index 651fc5d..559135c 100644 (file)
@@ -43,6 +43,7 @@ lib@OSMESA_LIB@_la_LDFLAGS = \
        -module \
        -no-undefined \
        -version-number @OSMESA_VERSION@ \
+       $(GC_SECTIONS) \
        -Wl,--no-undefined
 
 
index 00823fe..fa463ad 100644 (file)
@@ -49,6 +49,7 @@ AM_LDFLAGS = \
        -module \
        -no-undefined \
        -avoid-version \
+       $(GC_SECTIONS) \
        -Wl,--no-undefined \
        -Wl,--version-script=$(top_srcdir)/src/gallium/targets/pipe-loader/pipe.link
 
index 214c51d..21f29cf 100644 (file)
@@ -67,6 +67,7 @@ endif
 libxatracker_la_LDFLAGS = \
        -no-undefined \
        -version-number $(XA_MAJOR):$(XA_MINOR):$(XA_TINY) \
+       $(GC_SECTIONS) \
        -Wl,--no-undefined
 
 if HAVE_MESA_LLVM
index c71a974..ea06ce1 100644 (file)
@@ -22,6 +22,7 @@ libgbm_la_SOURCES = \
 libgbm_la_LDFLAGS = \
        -no-undefined \
        -version-info 1:0 \
+       $(GC_SECTIONS) \
        -Wl,--no-undefined
 
 libgbm_la_LIBADD = \
index 0c99c07..aa7666a 100644 (file)
@@ -112,6 +112,7 @@ GL_LDFLAGS = \
        -no-undefined \
        -version-number 1:2 \
        -Wl,-Bsymbolic \
+       $(GC_SECTIONS) \
        -Wl,--no-undefined
 
 lib@GL_LIB@_la_SOURCES =
index b958118..febb137 100644 (file)
@@ -47,6 +47,7 @@ libGLESv1_CM_la_LIBADD = $(GLESv1_CM_LIB_DEPS)
 libGLESv1_CM_la_LDFLAGS = \
        -no-undefined \
        -version-number 1:1 \
+       $(GC_SECTIONS) \
        -Wl,--no-undefined
 
 if HAVE_SHARED_GLAPI
index 1fb6d7c..e694a36 100644 (file)
@@ -51,6 +51,7 @@ libGLESv2_la_LIBADD = $(GLESv2_LIB_DEPS)
 libGLESv2_la_LDFLAGS = \
        -no-undefined \
        -version-number 2 \
+       $(GC_SECTIONS) \
        -Wl,--no-undefined
 
 if HAVE_SHARED_GLAPI
index 7e1068f..33a939a 100644 (file)
@@ -11,6 +11,7 @@ libglapi_la_SOURCES = $(MAPI_GLAPI_FILES)
 libglapi_la_LIBADD = $(PTHREAD_LIBS) $(SELINUX_LIBS)
 libglapi_la_LDFLAGS = \
        -no-undefined \
+       $(GC_SECTIONS) \
        -Wl,--no-undefined
 
 include $(GLAPI)/gen/glapi_gen.mk
index f85addc..f946b9b 100644 (file)
@@ -47,6 +47,7 @@ libOpenVG_la_LIBADD = $(VG_LIB_DEPS)
 libOpenVG_la_LDFLAGS = \
        -no-undefined \
        -version-number 1 \
+       $(GC_SECTIONS) \
        -Wl,--no-undefined
 
 vgapi_tmp.h: $(srcdir)/vgapi.csv $(top_srcdir)/src/mapi/mapi_abi.py
index 17ac76b..f1fee12 100644 (file)
@@ -53,6 +53,7 @@ mesa_dri_drivers_la_SOURCES =
 mesa_dri_drivers_la_LDFLAGS = \
         -module -avoid-version -shared \
         -Wl,-Bsymbolic \
+        $(GC_SECTIONS) \
         $()
 mesa_dri_drivers_la_LIBADD = \
         ../../libmesa.la \
index cfab702..b133fd6 100644 (file)
@@ -39,6 +39,7 @@ lib@OSMESA_LIB@_la_LDFLAGS = \
        -module \
        -no-undefined \
        -version-number @OSMESA_VERSION@ \
+       $(GC_SECTIONS) \
        -Wl,--no-undefined
 
 
index 4c63d59..2b38ad3 100644 (file)
@@ -64,6 +64,7 @@ lib@GL_LIB@_la_LIBADD = \
 lib@GL_LIB@_la_LDFLAGS = \
        -no-undefined \
        -version-number $(GL_MAJOR):$(GL_MINOR):$(GL_PATCH) \
+       $(GC_SECTIONS) \
        -Wl,--no-undefined
 
 include $(top_srcdir)/install-lib-links.mk