Page 1 of 1

decreasing executable size with gcc options

Posted: Mon Dec 28, 2009 4:01 pm
by jotd
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

Re: decreasing executable size with gcc options

Posted: Sun Apr 25, 2010 3:34 am
by Raid
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 [email protected])
	@$(STRIP) -s [email protected]
To the very bottom of the file in the "%.elf:" section..
At the beginning of each of those 2 lines there is a <TAB>.

Re: decreasing executable size with gcc options

Posted: Sun Apr 25, 2010 7:06 am
by jotd
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

Re: decreasing executable size with gcc options

Posted: Sun Apr 25, 2010 5:41 pm
by ritz
To my knowledge, objcopy is stripping your elf clean already. It's set up in the rules too.

Re: decreasing executable size with gcc options

Posted: Sun Apr 25, 2010 8:06 pm
by jotd
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.