gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > Remove unused functions/dead code

#147547 - Peter - Sat Dec 22, 2007 5:57 pm

I wonder how I can get devkitarm r21 to remove unused functions.

If I remember correctly, GCC could only remove entire unused compilation units, but not seperate unused functions from those. That would make sense, because there are several projects that put every function in a seperate source file. So I did the same with my matrix module today (some functions are unused), but it didn't change anything to the final .nds filesize.

So I went one step further and created a new project using the arm9 template from the devkitpro examples, added three .cpp files with the functions:

    * Unused0();
    * Unused1();
    * Unused2(); (calls Unused1)


None of those functions is called anywhere. When I clean/make the project, its filesize grew. I tried some compiler and linker switches that I found thru google and think are related to it (I'm not an expert with toolchain stuff), but it still doesn't change anything on the size:
Code:

CFLAGS += -ffunction-sections -fdata-sections
LDFLAGS += --gc-sections -fssa -fssa-ccp -fssa-dce


This is the test project: click here to download

Any idea what I'm doing wrong?
_________________
Kind Regards,
Peter

#147564 - freemaan - Sun Dec 23, 2007 11:58 am

You should use "-Wl,--gc-sections", not just "--gc-sections" :)
_________________
My other nickname: davido2

#147565 - Peter - Sun Dec 23, 2007 1:48 pm

freemaan wrote:
You should use "-Wl,--gc-sections", not just "--gc-sections" :)

The -Wl switch is a compiler switch, which instructs gcc/g++ to forward the --gc-sections parameter to the linker. Since I didn't add --gc-sections to the compiler flags (CFLAGS), but to the linker flags (LDFLAGS), I do not need -Wl. However, I wasn't really sure so I tested it, but it didn't change anything. Thanks anyway!
_________________
Kind Regards,
Peter

#147609 - wintermute - Mon Dec 24, 2007 12:11 pm

That's a fairly common misunderstanding - gcc & g++ are not compilers but rather drivers which run the other parts of the toolchain.

The devkitPro build system links code using these drivers rather than calling the linker directly so the LDFLAGS variable contains flags used during the link stage of the build and not linker flags specifically. This is the proper way to link using a gcc based toolchain - using the linker directly bypasses the parts of the compiler suite which know where libraries are located and which system libraries contain circular depencies.

I just checked your example here and the -Wl,--gc-sections option saved 1336 bytes.

When you're comparing things like this you need to make sure that you clean the project and rebuild it from scratch. I get bitten by that one on a regular basis - my personal favourite is making a copy of a project for testing then spending 10 minutes trying to figure out why my code changes aren't being compiled ;)
_________________
devkitPro - professional toolchains at amateur prices
devkitPro IRC support
Personal Blog

#147610 - kusma - Mon Dec 24, 2007 12:49 pm

wintermute wrote:
I just checked your example here and the -Wl,--gc-sections option saved 1336 bytes.

Ouch, off by one!

#147611 - chishm - Mon Dec 24, 2007 1:07 pm

kusma wrote:
wintermute wrote:
I just checked your example here and the -Wl,--gc-sections option saved 1336 bytes.

Ouch, off by one!

Yeah, those damn half-word alignments ruin all the fun.
_________________
http://chishm.drunkencoders.com
http://dldi.drunkencoders.com

#147613 - Peter - Mon Dec 24, 2007 1:25 pm

Indeed it works, yay!
_________________
Kind Regards,
Peter