decreasing executable size with gcc options

Add your suggestions for toolchain improvements here.
Post Reply
jotd
Posts: 32
Joined: Sun Dec 13, 2009 9:43 pm
Location: France
Contact:

decreasing executable size with gcc options

Post by jotd » Mon Dec 28, 2009 4:01 pm

Hi,

Since .elf contents are loaded in memory on Nintendo DS and that we face big memory limitations (specially when including graphics & sound in the .elf file), I propose a modification of the Makefile template (if there's one) or at least suggest some fine option which allow to reduce code size:

compiler options: add -ffunction-sections -fdata-sections => put each data & function in a separate section
link option: add -Wl,--gc-sections => garbage collect unreferenced sections => removes all your unused code, but also compiler generated code & data which is finally unused. It means that even if all of your routines are used, it still saves ROM.

On my project, I could reduce the .elf file from 800k to 700k only with that (that's also some options we are using at work for an embedded application to save a lot of ROM).

bye

Raid
Posts: 2
Joined: Tue Apr 20, 2010 4:43 am

Re: decreasing executable size with gcc options

Post by Raid » Sun Apr 25, 2010 3:34 am

I know this topic is a bit old, but, if anyone is curious..

I'm assuming you're using devkitARM.
Open devkitARM/base_rules, and add:

Code: Select all

export STRIP := $(PREFIX)strip
Then in devkitARM/ds_rules, add:

Code: Select all

	@echo stripping $(notdir $@)
	@$(STRIP) -s $@
To the very bottom of the file in the "%.elf:" section..
At the beginning of each of those 2 lines there is a <TAB>.

jotd
Posts: 32
Joined: Sun Dec 13, 2009 9:43 pm
Location: France
Contact:

Re: decreasing executable size with gcc options

Post by jotd » Sun Apr 25, 2010 7:06 am

Yes, -s is good too, since you don't need debug symbols on the DS :)

Also use -Os instead of -O2. Saves some more bytes.


to sum it up, compile -ffunction-sections, -fdata-sections, -Os, link with --gc-sections and -s

ritz
Posts: 24
Joined: Thu Jun 04, 2009 3:17 pm
Location: Canada

Re: decreasing executable size with gcc options

Post by ritz » Sun Apr 25, 2010 5:41 pm

To my knowledge, objcopy is stripping your elf clean already. It's set up in the rules too.

jotd
Posts: 32
Joined: Sun Dec 13, 2009 9:43 pm
Location: France
Contact:

Re: decreasing executable size with gcc options

Post by jotd » Sun Apr 25, 2010 8:06 pm

ritz wrote:To my knowledge, objcopy is stripping your elf clean already. It's set up in the rules too.
Yeah, so no need for -s switch.

I dunno for you, but I'm not using Makefiles for day-to-day coding. I have set up a Code::Blocks project which is able to generate code for NDS or Windows (different targets). So I have an IDE to work. Makefiles are OK for other people when they want to recompile your work.

Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests