Patchwork [U-Boot,3/6,v2] Enable garbage collelction of unused input sections.

login
register
mail settings
Submitter luigi.mantellini@idf-hit.com
Date Dec. 4, 2010, 1:29 p.m.
Message ID <1291469358-25023-4-git-send-email-luigi.mantellini@idf-hit.com>
Download mbox | patch
Permalink /patch/74264/
State Changes Requested
Headers show

Comments

luigi.mantellini@idf-hit.com - Dec. 4, 2010, 1:29 p.m.
When available, enable the CC -ffunction-sections option and the LD --gc-sections in order to isolate and remove from final executable the unused functions.

From ld manpages:

--gc-sections
--no-gc-sections

Enable garbage collection of unused input sections.  It is ignored on targets that do not support this option.  The default behaviour (of not performing this garbage collection) can be restored by specifying --no-gc-sections on the command line.

--gc-sections decides which input sections are used by examining symbols and relocations.  The section containing the entry symbol and all sections containing symbols undefined on the command-line will be kept, as will sections containing symbols referenced by dynamic objects.  Note that when building shared libraries, the linker must assume that any visible symbol is referenced.  Once this initial set of sections has been determined, the linker recursively marks as used any section referenced by their relocations.  See --entry and --undefined.

From gcc manpages:

-ffunction-sections
-fdata-sections

Place each function or data item into its own section in the output file if the target supports arbitrary sections.  The name of the function or the name of the data item determines the section's name in the output file.

Use these options on systems where the linker can perform optimizations to improve locality of reference in the instruction space.  Most systems using the ELF object format and SPARC processors running Solaris 2 have linkers with such optimizations.  AIX may have these optimizations in the future.

Only use these options when there are significant benefits from doing so.  When you specify these options, the assembler and linker will create larger object and executable files and will also be slower. You will not be able to 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: Luigi 'Comio' Mantellini <luigi.mantellini@idf-hit.com>
---
 config.mk |   13 +++++++++++++
 1 files changed, 13 insertions(+), 0 deletions(-)
Wolfgang Denk - Dec. 4, 2010, 10:29 p.m.
Dear Luigi 'Comio' Mantellini,

In message <1291469358-25023-4-git-send-email-luigi.mantellini@idf-hit.com> you wrote:

Typo in subject.

> When available, enable the CC -ffunction-sections option and the LD --gc-sections in order to isolate and remove from final executable the unused functions.

Lines WAY TOO LONG!!

Please fix globally.


This patch is unrelated from the rest of this series and should be
submitted separately.

> From ld manpages:
> 
> --gc-sections
> --no-gc-sections

I don't see any such changes in that commit?

Are you sure you split this correctly so it remains atomic, bisectable
commits?  I don't think so.



> Enable garbage collection of unused input sections.  It is ignored on targets that do not support this option.  The default behaviour (of not performing this garbage collection) can be restored by specifying --no-gc-sections on the command line.
> 
> --gc-sections decides which input sections are used by examining symbols and relocations.  The section containing the entry symbol and all sections containing symbols undefined on the command-line will be kept, as will sections containing symbols refere
> nced by dynamic objects.  Note that when building shared libraries, the linker must assume that any visible symbol is referenced.  Once this initial set of sections has been determined, the linker recursively marks as used any section referenced by thei
> r relocations.  See --entry and --undefined.
> 
> From gcc manpages:
> 
> -ffunction-sections
> -fdata-sections
> 
> Place each function or data item into its own section in the output file if the target supports arbitrary sections.  The name of the function or the name of the data item determines the section's name in the output file.
> 
> Use these options on systems where the linker can perform optimizations to improve locality of reference in the instruction space.  Most systems using the ELF object format and SPARC processors running Solaris 2 have linkers with such optimizations.  AI
> X may have these optimizations in the future.
> 
> Only use these options when there are significant benefits from doing so.  When you specify these options, the assembler and linker will create larger object and executable files and will also be slower. You will not be able to use "gprof" on all system
> s if you specify this option and you may have problems with debugging if you specify both this option and -g.
> 
> Signed-off-by: Luigi 'Comio' Mantellini <luigi.mantellini@idf-hit.com>
> ---
>  config.mk |   13 +++++++++++++
>  1 files changed, 13 insertions(+), 0 deletions(-)

You cannot do this without adapting the linker scripts of ALL affected
boards.

> diff --git a/config.mk b/config.mk
> index c6d6f7b..591b490 100644
> --- a/config.mk
> +++ b/config.mk
> @@ -23,6 +23,10 @@
>  
>  #########################################################################
>  
> +comma := ,
> +empty :=
> +space := $(empty) $(empty)

Why would that be needed?  Please elucidate.


>  ifneq ($(OBJTREE),$(SRCTREE))
>  ifeq ($(CURDIR),$(SRCTREE))
>  dir :=
> @@ -97,6 +101,8 @@ HOSTCFLAGS	+= -pedantic
>  #
>  cc-option = $(shell if $(CC) $(CFLAGS) $(1) -S -o /dev/null -xc /dev/null \
>  		> /dev/null 2>&1; then echo "$(1)"; else echo "$(2)"; fi ;)
> +ld-option = $(shell if $(CC) -Wl$(comma)$(1) -nostdlib -xc /dev/null -o /dev/null \
> +		> /dev/null 2>&1; then echo "$(1)"; else echo "$(2)"; fi ;)
>  
>  #
>  # Include the make variables (CC, etc...)
> @@ -191,6 +197,13 @@ endif
>  
>  CFLAGS += $(call cc-option,-fno-stack-protector)
>  
> +# Create a section for each function or data (useful for sections garbage collector)
> +CFLAGS += $(call cc-option,-ffunction-sections)
> +CFLAGS += $(call cc-option,-fdata-sections)
> +
> +# Sections garbage collector
> +LDFLAGS += $(call ld-option,--gc-sections)

It seems you mix several things here.  Thi sis not bisectable as is.


Best regards,

Wolfgang Denk

Patch

diff --git a/config.mk b/config.mk
index c6d6f7b..591b490 100644
--- a/config.mk
+++ b/config.mk
@@ -23,6 +23,10 @@ 
 
 #########################################################################
 
+comma := ,
+empty :=
+space := $(empty) $(empty)
+
 ifneq ($(OBJTREE),$(SRCTREE))
 ifeq ($(CURDIR),$(SRCTREE))
 dir :=
@@ -97,6 +101,8 @@  HOSTCFLAGS	+= -pedantic
 #
 cc-option = $(shell if $(CC) $(CFLAGS) $(1) -S -o /dev/null -xc /dev/null \
 		> /dev/null 2>&1; then echo "$(1)"; else echo "$(2)"; fi ;)
+ld-option = $(shell if $(CC) -Wl$(comma)$(1) -nostdlib -xc /dev/null -o /dev/null \
+		> /dev/null 2>&1; then echo "$(1)"; else echo "$(2)"; fi ;)
 
 #
 # Include the make variables (CC, etc...)
@@ -191,6 +197,13 @@  endif
 
 CFLAGS += $(call cc-option,-fno-stack-protector)
 
+# Create a section for each function or data (useful for sections garbage collector)
+CFLAGS += $(call cc-option,-ffunction-sections)
+CFLAGS += $(call cc-option,-fdata-sections)
+
+# Sections garbage collector
+LDFLAGS += $(call ld-option,--gc-sections)
+
 # $(CPPFLAGS) sets -g, which causes gcc to pass a suitable -g<format>
 # option to the assembler.
 AFLAGS_DEBUG :=