# Optimizing with GCC

**URL:** <https://forums.freertos.org/t/optimizing-with-gcc/765>\
**Category:** Kernel\
**Created:** [June 6, 2006, 12:40pm UTC](https://forums.freertos.org/t/optimizing-with-gcc/765 "2006-06-06T12:40:24Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [June 6, 2006, 12:40pm UTC](https://forums.freertos.org/t/optimizing-with-gcc/765/1 "2006-06-06T12:40:24Z")

</div>

**nobody** wrote on [Tuesday, June 06, 2006](https://sourceforge.net/p/freertos/discussion/382005/thread/20426419/#076a):

Hi,

I have a \_VERY\_ small program here : A FreeRTOS kernel running one single task (besides idle), where 4 LEDs are blinking. The thing is that the program takes up 44400 bytes !!!  
The MAP file tells me that a lot of unused functions still are in the hex-file. Functions like AT91F\_UDP\_GetInterruptMaskStatus from lib\_AT91SAM7S256.h and "/cygdrive/c/gnuarm/bin/…/lib/gcc/arm-elf/4.1.0/interwork/libgcc.a(\_udivsi3.o)"

What do I do in order to remove the dead code ?

/RaceMouse

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [June 6, 2006, 12:51pm UTC](https://forums.freertos.org/t/optimizing-with-gcc/765/2 "2006-06-06T12:51:18Z")

</div>

**nobody** wrote on [Tuesday, June 06, 2006](https://sourceforge.net/p/freertos/discussion/382005/thread/20426419/#076a/0fcc):

Yuk.&nbsp; Sound like you have the whole C library library included in your executable.&nbsp; I presume you are not doing any division (other than compile time constants)?

Which GCC distribution are you using?&nbsp; Is it one targeted at embedded?

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [June 6, 2006, 1:11pm UTC](https://forums.freertos.org/t/optimizing-with-gcc/765/3 "2006-06-06T13:11:02Z")

</div>

**nobody** wrote on [Tuesday, June 06, 2006](https://sourceforge.net/p/freertos/discussion/382005/thread/20426419/#076a/7156):

Hi,

It’s:  
arm-elf-gcc 4.1.0  
Binutils 2.16.1  
Newlib 1.14.0

Basically it’s GnuArm - latest…

/RaceMouse

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [June 6, 2006, 1:19pm UTC](https://forums.freertos.org/t/optimizing-with-gcc/765/4 "2006-06-06T13:19:48Z")

</div>

**nobody** wrote on [Tuesday, June 06, 2006](https://sourceforge.net/p/freertos/discussion/382005/thread/20426419/#076a/7156/3c94):

I am also using the latest gnuarm.&nbsp; When I compile the LPC2106 demo I get a bin file (after objcopying the default elf file to bin) that is 15K.&nbsp; This is the whole unmodified demo.

Which sam7 project are your compiling?&nbsp; Is it in the download so I can try it?

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [June 6, 2006, 1:27pm UTC](https://forums.freertos.org/t/optimizing-with-gcc/765/5 "2006-06-06T13:27:00Z")

</div>

**nobody** wrote on [Tuesday, June 06, 2006](https://sourceforge.net/p/freertos/discussion/382005/thread/20426419/#076a/56f6):

It’s the lwIP\_Rowley, where I have removed all lwip/usb and the just addad the one task blinking the LEDs. I changed the heap size to 3000 and changed the AT91SAM7X256 to AT91SAM7S256.

/RaceMouse

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [June 6, 2006, 1:40pm UTC](https://forums.freertos.org/t/optimizing-with-gcc/765/6 "2006-06-06T13:40:53Z")

</div>

**nobody** wrote on [Tuesday, June 06, 2006](https://sourceforge.net/p/freertos/discussion/382005/thread/20426419/#076a/6090):

I checked the size on the LPC2106\_GCC build and i was the ~17K. I cannot, however, seem to find any relevant diffs in the two Makefiles. The Linkerfils from the two projects are also identical (Except the RAM/ROM mapping off course)…

Any Ideas ?

/RaceMouse

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [June 6, 2006, 1:44pm UTC](https://forums.freertos.org/t/optimizing-with-gcc/765/7 "2006-06-06T13:44:48Z")

</div>

**nobody** wrote on [Tuesday, June 06, 2006](https://sourceforge.net/p/freertos/discussion/382005/thread/20426419/#076a/6090/e501):

Not that it helps but I get the same result as you.&nbsp; I removed all lwIP, demo, emac and usb references from the makefile and edited main so only the setup hardware and check task remains.&nbsp; I get a 42K binary.&nbsp; Very odd.&nbsp; I will take a look at the map file also.&nbsp; Must be the atmel library?

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [June 6, 2006, 1:47pm UTC](https://forums.freertos.org/t/optimizing-with-gcc/765/8 "2006-06-06T13:47:43Z")

</div>

**nobody** wrote on [Tuesday, June 06, 2006](https://sourceforge.net/p/freertos/discussion/382005/thread/20426419/#076a/6090/565a):

Using CrossWorks the demo comes to 8K once the lwIP stuff is removed.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [June 6, 2006, 1:55pm UTC](https://forums.freertos.org/t/optimizing-with-gcc/765/9 "2006-06-06T13:55:06Z")

</div>

**nobody** wrote on [Tuesday, June 06, 2006](https://sourceforge.net/p/freertos/discussion/382005/thread/20426419/#076a/b7ec):

Ill’ be damned !

So - what is the difference between a normal CLI built executable and one built using Crossworks ?

Crossworks don’t use the Makefile, do they ?

Is there any way to see what commands they use with GCC ?

/RaceMouse

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [June 6, 2006, 1:58pm UTC](https://forums.freertos.org/t/optimizing-with-gcc/765/10 "2006-06-06T13:58:22Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Tuesday, June 06, 2006](https://sourceforge.net/p/freertos/discussion/382005/thread/20426419/#076a/cadc):

In FreeRTOSConfig.h, set configUSE\_TRACE\_FACILITY to 0.&nbsp; I think this might make a big difference as the trace facility will pull in lot of string handling functions which GCC is not very efficient at.

There are ways of controlling this, I read a thread on it somewhere recently but I cannot remember where unfortunately.

Let me know if this sorts out your problem.

Regards.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [June 6, 2006, 2:18pm UTC](https://forums.freertos.org/t/optimizing-with-gcc/765/11 "2006-06-06T14:18:20Z")

</div>

**nobody** wrote on [Tuesday, June 06, 2006](https://sourceforge.net/p/freertos/discussion/382005/thread/20426419/#076a/e085):

Well, It definately helped a lot. The executable size has now dropped from ~44K to ~9K, so we are getting there…

The following is a snippet from the MAP file:  
.text&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0x001004ec&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0xe04 lib\_AT91SAM7S256\_.o  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0x00100cac&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AT91F\_UDP\_EpClear  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0x00100cd8&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AT91F\_UDP\_GetInterruptMaskStatus  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0x001010fc&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AT91F\_PWMC\_IsInterruptMasked  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0x001006ec&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AT91F\_CKGR\_EnableMainOscillator

Only a couple of the functions in lib\_AT91SAM7S256\_ are used and yet they are all included by in the executable. Do you have any idea how I can eliminate them ?

/RAceMouse

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [June 6, 2006, 2:37pm UTC](https://forums.freertos.org/t/optimizing-with-gcc/765/12 "2006-06-06T14:37:37Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Tuesday, June 06, 2006](https://sourceforge.net/p/freertos/discussion/382005/thread/20426419/#076a/e085/808f):

I have got my build down to 5.5K, quite nice really 😉

Looking in the map file there are no AT91 functions at all.

Looking in the code I find that the portable layer is using AT91F\_AIC\_ConfigureIt(), but I have #defined this to a macro in the header file, hence it does not use the library functions.

The library functions are unfortunately a bit IAR centric.&nbsp; They use inline functions rather than macros.&nbsp; You can define away the inline and create a load of functions, but this is not really a good solution.&nbsp; If you can get the inline functions to work as inline functions you should have the optimised solution.&nbsp; I believe there was a post some time back on this forum about how to do this.&nbsp; You had to setup some #define to the correct ‘inline’ keyword, maybe \_\_inline or something like that.

My preference is to use the inline functions as a guide, then implement my own equivalents, as I did with the AT91F\_AIC\_ConfigureIt().&nbsp; This is fine provided you are not using too many of them.

Regards.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [June 6, 2006, 2:53pm UTC](https://forums.freertos.org/t/optimizing-with-gcc/765/13 "2006-06-06T14:53:41Z")

</div>

**nobody** wrote on [Tuesday, June 06, 2006](https://sourceforge.net/p/freertos/discussion/382005/thread/20426419/#076a/24f2):

I see your point, but still : Should GCC not be able to optimese away unused code ?

I saw an another forum (can’t remember where) that the following Compiler flags should do the trick:  
CFLAGS += -ffunction-sections  
CFLAGS += -fdata-sections  
CFLAGS += -Wl,–gc-sections  
The linker file would have to edited afterwords. I could, however, not make it work.

Has anyone got any experience with this ?

/RaceMouse

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [June 14, 2006, 7:54pm UTC](https://forums.freertos.org/t/optimizing-with-gcc/765/14 "2006-06-14T19:54:45Z")

</div>

**nobody** wrote on [Wednesday, June 14, 2006](https://sourceforge.net/p/freertos/discussion/382005/thread/20426419/#076a/82a6):

The Linker script had to be edited:

\<–snip–\>  
SECTIONS  
{  
&nbsp;&nbsp;&nbsp; . = 0;  
&nbsp;&nbsp;&nbsp; .startup : {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; KEEP(\*(.startup))  
&nbsp;&nbsp;&nbsp; }\>flash

&nbsp;&nbsp;&nbsp; .text :  
\<–!snip–\>

and

\*(.data) to \*(.data.\*)  
\*(.text) to \*(.text.\*)

The KEEP() directive did the trick. The OS is now running and uses ~3.7K Flash.

Thanks for the help 🙂
