[Skiboot] [PATCH 2/2] Dead code and data elimination

Nicholas Piggin npiggin at gmail.com
Thu Jan 5 20:35:48 AEDT 2017


Add an experimental option to do basic dead code and data elimintation
with -ffunction-sections/-fdata-sections/--gc-sections.

This saves about 80kB of text/data.

Also remove the use of of -ffunction-sections by default. This predates
git history, but I don't think there is a good reason to use it without
--gc-sections. The GCC manual says:

    Only use these options when there are significant benefits from
    doing so. When you specify these options, the assembler and linker
    create larger object and executable files and are also slower. You
    cannot use gprof on all systems if you specify this option, and you
    may have problems with debugging if you specify both this option
    and -g.

Signed-off-by: Nicholas Piggin <npiggin at gmail.com>
---
 Makefile      |  2 ++
 Makefile.main | 10 +++++++++-
 skiboot.lds.S |  5 ++---
 3 files changed, 13 insertions(+), 4 deletions(-)

diff --git a/Makefile b/Makefile
index d4a097f7..157eda2f 100644
--- a/Makefile
+++ b/Makefile
@@ -44,6 +44,8 @@ STACK_CHECK ?= $(DEBUG)
 LITTLE_ENDIAN ?= 0
 # ELF v2 ABI is more efficient and compact
 ELF_ABI_v2 ?= $(LITTLE_ENDIAN)
+# Discard unreferenced code and data at link-time
+DEAD_CODE_ELIMINATION ?= 0
 
 #
 # Where is the source directory, must be a full path (no ~)
diff --git a/Makefile.main b/Makefile.main
index 5656cb72..6a69b65d 100644
--- a/Makefile.main
+++ b/Makefile.main
@@ -48,7 +48,7 @@ VALGRIND=valgrind -q --show-reachable=yes --error-exitcode=99
 
 # Target options
 
-OPTS=-Os -ffunction-sections
+OPTS=-Os
 DBG=-g
 
 CPPFLAGS := -I$(SRC)/include -Iinclude -MMD -include $(SRC)/include/config.h
@@ -78,6 +78,10 @@ else
 CFLAGS += $(call try-cflag,$(CC),-mabi=elfv1)
 endif
 
+ifeq ($(DEAD_CODE_ELIMINATION),1)
+CFLAGS += -ffunction-sections -fdata-sections
+endif
+
 ifeq ($(SKIBOOT_GCOV),1)
 CFLAGS += -fprofile-arcs -ftest-coverage -DSKIBOOT_GCOV=1
 endif
@@ -112,6 +116,10 @@ LDRFLAGS=-melf64ppc
 # Debug stuff
 #LDFLAGS += -Wl,-v -Wl,-Map,foomap 
 
+ifeq ($(DEAD_CODE_ELIMINATION),1)
+LDFLAGS += -Wl,--gc-sections
+endif
+
 AFLAGS := -D__ASSEMBLY__ -mbig-endian -m64
 ifeq ($(ELF_ABI_v2),1)
 AFLAGS += $(call try-cflag,$(CC),-mabi=elfv2)
diff --git a/skiboot.lds.S b/skiboot.lds.S
index 7d3d0cf3..4a5dabe3 100644
--- a/skiboot.lds.S
+++ b/skiboot.lds.S
@@ -85,8 +85,8 @@ SECTIONS
 	. = ALIGN(0x10);
 	.init : {
 		__ctors_start = .;
-		*(.ctors)
-		*(.init_array)
+		KEEP(*(.ctors))
+		KEEP(*(.init_array))
 		__ctors_end = .;
 	}
  
@@ -143,7 +143,6 @@ SECTIONS
 		__sym_map_start = . ;
 		KEEP(*(.sym_map))
 		__sym_map_end = . ;
-		KEEP(*(.sym_map))
 	}
 
 	/* We locate the BSS at 2M to leave room for the symbol map */
-- 
2.11.0



More information about the Skiboot mailing list