[U-Boot] [RFC PATCH] ppc4xx: Use gc-sections to reduce image size

This patch enables gc-sections for PPC4xx. This is done to generate smaller U-Boot images. For this the linker script also needs some tweaking:
- For example change *(text) to *(text*) - Add KEEP to some of the symbols, especially "resetvec"
This patch is tested on the following boards and reduces the image size by the these values:
Canyonlands: 11,642 Katmai: 9,170 Kilauea: 10,742 Sequoia: 11,398
Additionally this patch integrates a bit of cleanup to the PPC4xx default linker script.
Please note that this patch only enables gc-sections for PPC4xx. Other PowerPC's may enable it as well but also need to change the linker scripts accordingly. I didn't do this in this patch since I can't test all those platforms/boards.
Once all PowerPC platforms have gc-sections enabled, the changes to arch/powerpc/cpu/ppc4xx/config.mk should be moved to arch/powerpc/config.mk
Signed-off-by: Stefan Roese sr@denx.de --- arch/powerpc/cpu/ppc4xx/config.mk | 6 ++++ arch/powerpc/cpu/ppc4xx/u-boot.lds | 53 +++++++----------------------------- 2 files changed, 16 insertions(+), 43 deletions(-)
diff --git a/arch/powerpc/cpu/ppc4xx/config.mk b/arch/powerpc/cpu/ppc4xx/config.mk index 5bda710..b0d346a 100644 --- a/arch/powerpc/cpu/ppc4xx/config.mk +++ b/arch/powerpc/cpu/ppc4xx/config.mk @@ -33,5 +33,11 @@ else PLATFORM_CPPFLAGS += -Wa,-m405 -mcpu=405 endif
+# Enable gc-sections to enable generation of smaller images. +# Please note that the linker scripts may need some tweaking with this +# change as well. +PLATFORM_LDFLAGS += --gc-sections +PLATFORM_RELFLAGS += -ffunction-sections -fdata-sections + # Use default linker script. Board port can override in board/*/config.mk LDSCRIPT := $(SRCTREE)/arch/powerpc/cpu/ppc4xx/u-boot.lds diff --git a/arch/powerpc/cpu/ppc4xx/u-boot.lds b/arch/powerpc/cpu/ppc4xx/u-boot.lds index eca1f9d..c3e0e16 100644 --- a/arch/powerpc/cpu/ppc4xx/u-boot.lds +++ b/arch/powerpc/cpu/ppc4xx/u-boot.lds @@ -27,8 +27,7 @@ #endif
OUTPUT_ARCH(powerpc) -/* Do we need any of these for elf? - __DYNAMIC = 0; */ + PHDRS { text PT_LOAD; @@ -39,43 +38,16 @@ SECTIONS { /* Read-only sections, merged into text segment: */ . = + SIZEOF_HEADERS; - .interp : { *(.interp) } - .hash : { *(.hash) } - .dynsym : { *(.dynsym) } - .dynstr : { *(.dynstr) } - .rel.text : { *(.rel.text) } - .rela.text : { *(.rela.text) } - .rel.data : { *(.rel.data) } - .rela.data : { *(.rela.data) } - .rel.rodata : { *(.rel.rodata) } - .rela.rodata : { *(.rela.rodata) } - .rel.got : { *(.rel.got) } - .rela.got : { *(.rela.got) } - .rel.ctors : { *(.rel.ctors) } - .rela.ctors : { *(.rela.ctors) } - .rel.dtors : { *(.rel.dtors) } - .rela.dtors : { *(.rela.dtors) } - .rel.bss : { *(.rel.bss) } - .rela.bss : { *(.rela.bss) } - .rel.plt : { *(.rel.plt) } - .rela.plt : { *(.rela.plt) } - .init : { *(.init) } - .plt : { *(.plt) } .text : { - *(.text) - *(.got1) + *(.text*) } :text _etext = .; PROVIDE (etext = .); .rodata : { - *(.eh_frame) *(SORT_BY_ALIGNMENT(SORT_BY_NAME(.rodata*))) } :text - .fini : { *(.fini) } =0 - .ctors : { *(.ctors) } - .dtors : { *(.dtors) }
/* Read-write section, merged into data segment: */ . = (. + 0x00FF) & 0xFFFFFF00; @@ -83,23 +55,19 @@ SECTIONS PROVIDE (erotext = .); .reloc : { - *(.got) + KEEP(*(.got)) _GOT2_TABLE_ = .; - *(.got2) + KEEP(*(.got2)) _FIXUP_TABLE_ = .; - *(.fixup) + KEEP(*(.fixup)) } __got2_entries = (_FIXUP_TABLE_ - _GOT2_TABLE_) >> 2; __fixup_entries = (. - _FIXUP_TABLE_) >> 2;
.data : { - *(.data) - *(.data1) - *(.sdata) - *(.sdata2) - *(.dynamic) - CONSTRUCTORS + *(.data*) + *(.sdata*) } _edata = .; PROVIDE (edata = .); @@ -138,7 +106,7 @@ SECTIONS
.resetvec RESET_VECTOR_ADDRESS : { - *(.resetvec) + KEEP(*(.resetvec)) } :text = 0xffff
. = RESET_VECTOR_ADDRESS + 0x4; @@ -157,9 +125,8 @@ SECTIONS __bss_start = .; .bss (NOLOAD) : { - *(.sbss) *(.scommon) - *(.dynbss) - *(.bss) + *(.sbss*) + *(.bss*) *(COMMON) } :bss

Hi Stefan,
This patch enables gc-sections for PPC4xx. This is done to generate smaller U-Boot images. For this the linker script also needs some tweaking:
- For example change *(text) to *(text*)
- Add KEEP to some of the symbols, especially "resetvec"
This patch is tested on the following boards and reduces the image size by the these values:
Canyonlands: 11,642 Katmai: 9,170 Kilauea: 10,742 Sequoia: 11,398
Interesting.
Just for whoever is interested - I wondered how to find out what is actually unused to find pointers to unused crufty code.
Actually this is pretty easy by adding "--print-gc-sections" to the PLATFORM_LDFLAGS:
-----8<----- diff --git a/arch/powerpc/cpu/ppc4xx/config.mk b/arch/powerpc/cpu/ppc4xx/config.mk index b0d346a..f1e15c9 100644 --- a/arch/powerpc/cpu/ppc4xx/config.mk +++ b/arch/powerpc/cpu/ppc4xx/config.mk @@ -36,7 +36,7 @@ endif # Enable gc-sections to enable generation of smaller images. # Please note that the linker scripts may need some tweaking with this # change as well. -PLATFORM_LDFLAGS += --gc-sections +PLATFORM_LDFLAGS += --gc-sections --print-gc-sections PLATFORM_RELFLAGS += -ffunction-sections -fdata-sections
# Use default linker script. Board port can override in board/*/config.mk -----8<-----
Using this, the linker prints lots of message slike this:
ppc_4xxFP-ld: Removing unused section '.data' in file 'arch/powerpc/cpu/ppc4xx/start.o' ppc_4xxFP-ld: Removing unused section '.bss' in file 'arch/powerpc/cpu/ppc4xx/start.o' ppc_4xxFP-ld: Removing unused section '.text' in file 'board/amcc/sequoia/init.o' ppc_4xxFP-ld: Removing unused section '.data' in file 'board/amcc/sequoia/init.o' ppc_4xxFP-ld: Removing unused section '.bss' in file 'board/amcc/sequoia/init.o' ppc_4xxFP-ld: Removing unused section '.text' in file 'arch/powerpc/cpu/ppc4xx/resetvec.o' ppc_4xxFP-ld: Removing unused section '.data' in file 'arch/powerpc/cpu/ppc4xx/resetvec.o' ppc_4xxFP-ld: Removing unused section '.bss' in file 'arch/powerpc/cpu/ppc4xx/resetvec.o'
I believe they can be gladly ignored, because the "generic" segments are now empty thanks to the "-ffunction-sections -fdata-sections" directives. What is really interesting are messages like this:
ppc_4xxFP-ld: Removing unused section '.text.gpio_read_out_bit' in file 'arch/powerpc/cpu/ppc4xx/libppc4xx.a(gpio.o)' ppc_4xxFP-ld: Removing unused section '.text.gpio_read_in_bit' in file 'arch/powerpc/cpu/ppc4xx/libppc4xx.a(gpio.o)' ppc_4xxFP-ld: Removing unused section '.text.gpio_config' in file 'arch/powerpc/cpu/ppc4xx/libppc4xx.a(gpio.o)' ppc_4xxFP-ld: Removing unused section '.text.gpio_write_bit' in file 'arch/powerpc/cpu/ppc4xx/libppc4xx.a(gpio.o)'
They essentially show the functions that are not used in this link run.
Cheers Detlev
participants (2)
-
Detlev Zundel
-
Stefan Roese