Ask the linker to remove unused object files for avr / gcc

I am using avr-gcc for an atmega-328p microcontroller. The code is built with:

avr-gcc -g -Wall -Os -mmcu=atmega328p -D__AVR_ATmega3280__ -DARCH_AVR -I/usr/avr/include -DF_CPU=16000000   -c -o heap.o heap.c
avr-gcc -g -Wall -Os -mmcu=atmega328p -D__AVR_ATmega3280__ -DARCH_AVR -I/usr/avr/include -DF_CPU=16000000   -c -o kerneltimer.o kerneltimer.c
avr-gcc -g -Wall -Os -mmcu=atmega328p -D__AVR_ATmega3280__ -DARCH_AVR -I/usr/avr/include -DF_CPU=16000000   -c -o message.o message.c
avr-gcc -g -Wall -Os -mmcu=atmega328p -D__AVR_ATmega3280__ -DARCH_AVR -I/usr/avr/include -DF_CPU=16000000   -c -o driver.o driver.c
avr-gcc -g -Wall -Os -mmcu=atmega328p -D__AVR_ATmega3280__ -DARCH_AVR -I/usr/avr/include -DF_CPU=16000000   -c -o watchdog.o watchdog.c
avr-gcc -g -Wall -Os -mmcu=atmega328p -D__AVR_ATmega3280__ -DARCH_AVR -I/usr/avr/include -DF_CPU=16000000   -c -o mutex.o mutex.c
avr-gcc -g -Wall -Os -mmcu=atmega328p -D__AVR_ATmega3280__ -DARCH_AVR -I/usr/avr/include -DF_CPU=16000000   -c -o semaphore.o semaphore.c
avr-gcc -g -Wall -Os -mmcu=atmega328p -D__AVR_ATmega3280__ -DARCH_AVR -I/usr/avr/include -DF_CPU=16000000   -c -o kernelswi.o kernelswi.c
avr-gcc -g -Wall -Os -mmcu=atmega328p -D__AVR_ATmega3280__ -DARCH_AVR -I/usr/avr/include -DF_CPU=16000000   -c -o task.o task.c
avr-gcc -g -Wall -Os -mmcu=atmega328p -D__AVR_ATmega3280__ -DARCH_AVR -I/usr/avr/include -DF_CPU=16000000   -c -o kernelwdt.o kernelwdt.c
avr-gcc -g -Wall -Os -mmcu=atmega328p -D__AVR_ATmega3280__ -DARCH_AVR -I/usr/avr/include -DF_CPU=16000000   -c -o myfirstapp.o myfirstapp.c
avr-gcc -g -Wall -Os -mmcu=atmega328p -D__AVR_ATmega3280__ -DARCH_AVR -I/usr/avr/include -DF_CPU=16000000   -c -o taskport.o taskport.c
avr-gcc -g -Wall -Os -mmcu=atmega328p -D__AVR_ATmega3280__ -DARCH_AVR -I/usr/avr/include -DF_CPU=16000000   -c -o timer.o timer.c

      

And is related to:

avr-gcc -g -Wall -Os -mmcu=atmega328p -D__AVR_ATmega3280__ -DARCH_AVR -I/usr/avr/include -DF_CPU=16000000 -Wl,-Map,myfirstapp.map -L/usr/avr/lib -Os -o myfirstapp.elf heap.o kerneltimer.o message.o driver.o watchdog.o mutex.o semaphore.o kernelswi.o task.o kernelwdt.o myfirstapp.o taskport.o timer.o 

      

Now, as it turns out for this particular program, nothing in heap.o is used. However, the code is included in the final binary. Is there anyway I can force the linker to at least elide complete .o files if nothing is used anywhere in them?

And while this is just a fun project for educational purposes, I'd rather see if the tools can figure it out than keep track of things like this on my own.

+3


source to share


2 answers


Yes it is possible. Passing --gc-sections

to the linker will direct it to missing sections that are not referenced. In addition, passing the flag to -ffunction-sections

and the -fdata-sections

compiler will direct it to create a section for each function and variable, allowing them to be left on an individual basis, not just at the file level.



+7


source


Adding the -gc sections to the linker didn't work for me. However, adding



-Wl, -gc-sections did work

+1


source







All Articles