]> git.ipfire.org Git - thirdparty/libarchive.git/commitdiff
autotools: enable -fdata/function-sections and --gc-sections 1625/head
authorEmil Velikov <emil.l.velikov@gmail.com>
Sun, 21 Nov 2021 17:38:38 +0000 (17:38 +0000)
committerEmil Velikov <emil.l.velikov@gmail.com>
Sun, 21 Nov 2021 19:28:46 +0000 (19:28 +0000)
Analogue to the parent cmake commit, with linker flag detection.

The former two split the functions and data into separate sections
within the object file. Which makes it easier for the latter to properly
garbage collect and discard unused sections.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Makefile.am
configure.ac

index e43531c1e4ac222a52f86ec790ed54874f4f7760..8ba152bd280943d509a6d95ac38f322321fd887f 100644 (file)
@@ -284,7 +284,7 @@ endif
 
 # -no-undefined marks that libarchive doesn't rely on symbols
 # defined in the application.  This is mandatory for cygwin.
-libarchive_la_LDFLAGS= -no-undefined -version-info $(ARCHIVE_LIBTOOL_VERSION)
+libarchive_la_LDFLAGS= -no-undefined -version-info $(ARCHIVE_LIBTOOL_VERSION) $(GC_SECTIONS)
 libarchive_la_LIBADD= $(LTLIBICONV)
 
 # Manpages to install
@@ -1023,7 +1023,7 @@ endif
 
 bsdtar_LDADD= libarchive.la libarchive_fe.la $(LTLIBICONV)
 bsdtar_CPPFLAGS= -I$(top_srcdir)/libarchive -I$(top_srcdir)/libarchive_fe $(bsdtar_ccstatic) $(PLATFORMCPPFLAGS)
-bsdtar_LDFLAGS= $(bsdtar_ldstatic)
+bsdtar_LDFLAGS= $(bsdtar_ldstatic) $(GC_SECTIONS)
 
 bsdtar_EXTRA_DIST= \
        tar/bsdtar.1 \
@@ -1189,7 +1189,7 @@ endif
 
 bsdcpio_LDADD= libarchive_fe.la libarchive.la $(LTLIBICONV)
 bsdcpio_CPPFLAGS= -I$(top_srcdir)/libarchive -I$(top_srcdir)/libarchive_fe $(bsdcpio_ccstatic) $(PLATFORMCPPFLAGS)
-bsdcpio_LDFLAGS= $(bsdcpio_ldstatic)
+bsdcpio_LDFLAGS= $(bsdcpio_ldstatic) $(GC_SECTIONS)
 
 bsdcpio_EXTRA_DIST= \
        cpio/bsdcpio.1 \
@@ -1343,7 +1343,7 @@ endif
 
 bsdcat_LDADD= libarchive_fe.la libarchive.la $(LTLIBICONV)
 bsdcat_CPPFLAGS= -I$(top_srcdir)/libarchive -I$(top_srcdir)/libarchive_fe $(bsdcat_ccstatic) $(PLATFORMCPPFLAGS)
-bsdcat_LDFLAGS= $(bsdcat_ldstatic)
+bsdcat_LDFLAGS= $(bsdcat_ldstatic) $(GC_SECTIONS)
 
 bsdcat_EXTRA_DIST= \
        cat/bsdcat.1 \
index cb89c3ee81530e63e18f12aec2bbf04e8c0a1a34..c8295e4ab47d41405dbb6a23cd9d5cf7abec646f 100644 (file)
@@ -540,6 +540,22 @@ fi
 # Checks for supported compiler flags
 AX_APPEND_COMPILE_FLAGS([-Wall -Wformat -Wformat-security])
 
+# Place the functions and data into separate sections, allowing the linker
+# to garbage collect the unused ones.
+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";
+        AX_APPEND_COMPILE_FLAGS([-ffunction-sections -fdata-sections])],
+    [AC_MSG_RESULT([no])
+        GC_SECTIONS="";])
+LDFLAGS=$save_LDFLAGS
+
+AC_SUBST(GC_SECTIONS)
+
 # Checks for typedefs, structures, and compiler characteristics.
 AC_C_CONST
 # la_TYPE_UID_T defaults to "int", which is incorrect for MinGW