9

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

Community
  • 1
  • 1
Brandon Schaefer
  • 451
  • 1
  • 4
  • 12
  • Verax's problem was that he was using C++. You claim to be using just C, so shouldn't run into that, but there's still not enough meat in this question for a good answer. Show some detail: give us your complete compiler and linker lines, and show exactly what's being removed. – Lee Daniel Crocker Jul 20 '15 at 16:19
  • Added compiler and linker lines. What exactly being removed is still unknown as code is extensive, I asked some questions to help determine what exactly is getting discarded. Lastly, I was referring to the method Verax mentioned of adding each symbol to DISCARD and use error output to trace where symbol was referenced from. – Brandon Schaefer Jul 20 '15 at 16:32
  • 1
    Did you compile all code with these options? If there are globals without these options and you do not have the stadnard sections added in your linker script, that might be the cause of trouble. OTOH, ld should warn at least if discarding a used symbol. – too honest for this site Jul 20 '15 at 16:32
  • All code was compiled with these options, yes. Globals? The standard sections (.text, .data, .bss) are in linker script if that is what you are referring to, and ld gave no warnings of discarding used symbols. – Brandon Schaefer Jul 20 '15 at 16:36
  • Just guessing: Interrupt handlers can get lost if not declared as such. – Daniel Jour Jul 20 '15 at 18:37

1 Answers1

11

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.

AJM
  • 1,317
  • 2
  • 15
  • 30
Brandon Schaefer
  • 451
  • 1
  • 4
  • 12