Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

-gc-sections discards used data

Using avr-gcc, avr-ld

I'm attempting to severely reduce the size of the output file by using fdata-sections -ffunction-sections and gc-sections. When compiled without these options I have an output of ~63KB, and with these options its ~30KB, so it seems great.

Unfortunately, after loading and testing the output I notice it doesn't work correctly. Garbage collection seems to have removed far more than I expected, and examining the .map I notice some key data is nonexistent.

Any idea on what at link time is causing these data/functions to be discarded? (I realize this may be too hopeful of a question with how little detail I provided, if any information is needed please ask and I'd do my best to provide it)

Is there a way to trace what symbols were tossed by garbage collection as they were "unused"?

Without using -fdata-sections -ffunction-sections -gc-sections, can I trace a symbol dependencies? (To verify gc-sections only collected what it should) - I noticed this this as an option, but it would be very painful for me to use the method suggested by Verax

EDIT: compiler and linker lines

avr-ld -mavrxmega6 -gc-sections -Tlinkerscript files libgcc.a -o firmware.elf

avr-gcc -Wall -Wstrict-prototypes -g -ffunction-sections -fdata-sections -Os -mmcu=atxmega256a3 -fno-jump-tables -std=gnu99 -fpack-struct -fno-common -mcall-prologues -c -o file.o file.c

like image 719
Brandon Schaefer Avatar asked Jul 20 '15 16:07

Brandon Schaefer


1 Answers

There was a pretty obvious ld option called -print-gc-sections to review what is being tossed out.

I never found a way to trace dependencies on a symbol, but ended up not needing to with reviewing -print-gc-sections.

I found this that described "magic sections" and I imagine this is similar to what I was seeing. There were many custom linker sections that were discared incorrectly, I used KEEP to prevent gc of these although I'm sure there is dead code not being removed now, but this may be the best I can do.

like image 81
Brandon Schaefer Avatar answered Oct 02 '22 22:10

Brandon Schaefer