This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

gcc dead code elimination

Hello,

I see that the make files that have been included have provision to remove dead code from the projects to reduce code food print. This is accomplished using the following commands

-ffunction-sections -fdata-sections Wl,--gc-sections

However, even with these commands in the make file I am still seeing dead code in my project. Has anyone else noticed this?

Lucas

  • I tested it here by adding an unused dummy function in the code. Checking the map file afterwards confirmed that it was dumped by the linker. Did you make sure to add the flags to the linker, and not just the compiler?

    Example on how to set the flags:

    # keep every function in separate section. This will allow linker to dump unused functions
    CFLAGS += -ffunction-sections
    
    # let linker to dump unused sections
    LDFLAGS := -Wl,--gc-sections
    

    In case you're not aware of it, one trick to easily determine the difference in size of the build is to echo the size. This can be done by adding the following lines to makefile.common:

     #Toolchain commands. Line ~34
    SIZE    		:= "$(GNU_INSTALL_ROOT)/bin/$(GNU_PREFIX)-size"
    
    #Line ~141
    $(SIZE) $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).out
    
Related