-nostdlib impact on code size

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

-nostdlib impact on code size

1,713 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by jdebliek on Tue Jun 15 15:28:39 MST 2010
The linker properties for a simple C project has the option "[I]No Startup or Default Libs (-nostdlib)" [/I]checked by default (I'm using the LPC1114).  Quite by accident I found that by desselecting this option, the code size was reduced - by over 1.5K in the case of my project.  I would have expected the opposite effect.  Does anyone know why this is, and if there could be any negative effect by not using this option?  My project is quite code size constrained, so I'll take anything I can get.
0 Kudos
Reply
9 Replies

1,608 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by tkreyche on Tue Aug 31 09:12:29 MST 2010
[FONT=Arial][SIZE=2]Yes, you are absolutely correct. I got lazy about including stdio.h because I was mostly commenting out sprintf to test my itoa() implementation.[/SIZE][/FONT]
[FONT=Arial][SIZE=2][/SIZE][/FONT]
[FONT=Arial][SIZE=2]Now CR_INTEGER_PRINTF works fine: in one example it reduces code size from 22852 to 18496.[/SIZE][/FONT]
[FONT=Arial][SIZE=2][/SIZE][/FONT]
[FONT=Arial][SIZE=2]Thanks for your prompt assistance![/SIZE][/FONT]
[FONT=Arial][SIZE=2][/SIZE][/FONT]
[FONT=Arial][SIZE=2]...Tom[/SIZE][/FONT]
0 Kudos
Reply

1,608 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by CodeRedSupport on Tue Aug 31 00:25:18 MST 2010
In your build log, I notice that you have....

Quote: tkreyche

[LEFT][FONT=Arial][SIZE=2]Building file: ../src/ssptest.c[/SIZE][/FONT]
[FONT=Arial][SIZE=2]Invoking: MCU C Compiler[/SIZE][/FONT]
[FONT=Arial][SIZE=2]arm-none-eabi-gcc -D__USE_CMSIS=CMSISv1p30_LPC11xx -DCR_INTEGER_PRINTF -DDEBUG -D__CODE_RED -I"C:\Users\v-tkrey\Documents\lpcxpresso_3.4\workspace\CMSISv1p30_LPC11xx\inc" -O0 -g -Wall -c -fmessage-length=0 -fno-builtin -ffunction-sections -mcpu=cortex-m0 -mthumb -D__REDLIB__ -MMD -MP -MF"src/ssptest.d" -MT"src/ssptest.d" -o"src/ssptest.o" "../src/ssptest.c"[/SIZE][/FONT]
[FONT=Arial][SIZE=2]../src/ssptest.c: In function 'main':[/SIZE][/FONT]
[FONT=Arial][SIZE=2]../src/ssptest.c:161: warning: implicit declaration of function 'sprintf'[/SIZE][/FONT]
[/LEFT]

[LEFT]
[/LEFT]
The implicit declaration warning suggests that you are not actually including stdio.h in ssptest.c. If this is indeed the case, then it will mean that the change from the floating point to integer version of sprintf caused by setting[FONT=Arial][SIZE=2] [FONT=Courier New][SIZE=1]CR_INTEGER_PRINTF[/SIZE][/FONT][/SIZE][/FONT] will not be being picked up.

Thus simply adding [FONT=Courier New][SIZE=1]#include <stdio.h>[/SIZE][/FONT] to ssptest.c should solve the problem.

Regards,
CodeRedSupport.
0 Kudos
Reply

1,608 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by tkreyche on Mon Aug 30 08:03:28 MST 2010
[FONT=Arial][SIZE=2]It's not a huge problem since I plan to replace sprintf with a custom itoa() but it's curious....no code size change with or without CR_INTEGER_PRINTF, with optimization either 0 or 1.[/SIZE][/FONT]

[FONT=Arial][SIZE=2]thanks, Tom[/SIZE][/FONT]
[FONT=Arial][SIZE=2]---------------------------------------[/SIZE][/FONT]
[LEFT][FONT=Arial][SIZE=2]**** Build of configuration Debug for project ssp ****[/SIZE][/FONT][/LEFT]

[LEFT][FONT=Arial][SIZE=2]make all [/SIZE][/FONT]
[FONT=Arial][SIZE=2]Building file: ../src/cr_startup_lpc11.c[/SIZE][/FONT]
[FONT=Arial][SIZE=2]Invoking: MCU C Compiler[/SIZE][/FONT]
[FONT=Arial][SIZE=2]arm-none-eabi-gcc -D__USE_CMSIS=CMSISv1p30_LPC11xx -DCR_INTEGER_PRINTF -DDEBUG -D__CODE_RED -I"C:\Users\v-tkrey\Documents\lpcxpresso_3.4\workspace\CMSISv1p30_LPC11xx\inc" -O0 -g -Wall -c -fmessage-length=0 -fno-builtin -ffunction-sections -mcpu=cortex-m0 -mthumb -D__REDLIB__ -MMD -MP -MF"src/cr_startup_lpc11.d" -MT"src/cr_startup_lpc11.d" -o"src/cr_startup_lpc11.o" "../src/cr_startup_lpc11.c"[/SIZE][/FONT]
[FONT=Arial][SIZE=2]Finished building: ../src/cr_startup_lpc11.c[/SIZE][/FONT][/LEFT]

[LEFT][FONT=Arial][SIZE=2]Building file: ../src/gpio.c[/SIZE][/FONT]
[FONT=Arial][SIZE=2]Invoking: MCU C Compiler[/SIZE][/FONT]
[FONT=Arial][SIZE=2]arm-none-eabi-gcc -D__USE_CMSIS=CMSISv1p30_LPC11xx -DCR_INTEGER_PRINTF -DDEBUG -D__CODE_RED -I"C:\Users\v-tkrey\Documents\lpcxpresso_3.4\workspace\CMSISv1p30_LPC11xx\inc" -O0 -g -Wall -c -fmessage-length=0 -fno-builtin -ffunction-sections -mcpu=cortex-m0 -mthumb -D__REDLIB__ -MMD -MP -MF"src/gpio.d" -MT"src/gpio.d" -o"src/gpio.o" "../src/gpio.c"[/SIZE][/FONT]
[FONT=Arial][SIZE=2]Finished building: ../src/gpio.c[/SIZE][/FONT][/LEFT]

[LEFT][FONT=Arial][SIZE=2]Building file: ../src/ssp.c[/SIZE][/FONT]
[FONT=Arial][SIZE=2]Invoking: MCU C Compiler[/SIZE][/FONT]
[FONT=Arial][SIZE=2]arm-none-eabi-gcc -D__USE_CMSIS=CMSISv1p30_LPC11xx -DCR_INTEGER_PRINTF -DDEBUG -D__CODE_RED -I"C:\Users\v-tkrey\Documents\lpcxpresso_3.4\workspace\CMSISv1p30_LPC11xx\inc" -O0 -g -Wall -c -fmessage-length=0 -fno-builtin -ffunction-sections -mcpu=cortex-m0 -mthumb -D__REDLIB__ -MMD -MP -MF"src/ssp.d" -MT"src/ssp.d" -o"src/ssp.o" "../src/ssp.c"[/SIZE][/FONT]
[FONT=Arial][SIZE=2]Finished building: ../src/ssp.c[/SIZE][/FONT][/LEFT]

[LEFT][FONT=Arial][SIZE=2]Building file: ../src/ssptest.c[/SIZE][/FONT]
[FONT=Arial][SIZE=2]Invoking: MCU C Compiler[/SIZE][/FONT]
[FONT=Arial][SIZE=2]arm-none-eabi-gcc -D__USE_CMSIS=CMSISv1p30_LPC11xx -DCR_INTEGER_PRINTF -DDEBUG -D__CODE_RED -I"C:\Users\v-tkrey\Documents\lpcxpresso_3.4\workspace\CMSISv1p30_LPC11xx\inc" -O0 -g -Wall -c -fmessage-length=0 -fno-builtin -ffunction-sections -mcpu=cortex-m0 -mthumb -D__REDLIB__ -MMD -MP -MF"src/ssptest.d" -MT"src/ssptest.d" -o"src/ssptest.o" "../src/ssptest.c"[/SIZE][/FONT]
[FONT=Arial][SIZE=2]../src/ssptest.c: In function 'main':[/SIZE][/FONT]
[FONT=Arial][SIZE=2]../src/ssptest.c:161: warning: implicit declaration of function 'sprintf'[/SIZE][/FONT]
[FONT=Arial][SIZE=2]Finished building: ../src/ssptest.c[/SIZE][/FONT][/LEFT]

[LEFT][FONT=Arial][SIZE=2]Building file: ../src/timer32.c[/SIZE][/FONT]
[FONT=Arial][SIZE=2]Invoking: MCU C Compiler[/SIZE][/FONT]
[FONT=Arial][SIZE=2]arm-none-eabi-gcc -D__USE_CMSIS=CMSISv1p30_LPC11xx -DCR_INTEGER_PRINTF -DDEBUG -D__CODE_RED -I"C:\Users\v-tkrey\Documents\lpcxpresso_3.4\workspace\CMSISv1p30_LPC11xx\inc" -O0 -g -Wall -c -fmessage-length=0 -fno-builtin -ffunction-sections -mcpu=cortex-m0 -mthumb -D__REDLIB__ -MMD -MP -MF"src/timer32.d" -MT"src/timer32.d" -o"src/timer32.o" "../src/timer32.c"[/SIZE][/FONT]
[FONT=Arial][SIZE=2]Finished building: ../src/timer32.c[/SIZE][/FONT][/LEFT]

[LEFT][FONT=Arial][SIZE=2]Building file: ../src/uart.c[/SIZE][/FONT]
[FONT=Arial][SIZE=2]Invoking: MCU C Compiler[/SIZE][/FONT]
[FONT=Arial][SIZE=2]arm-none-eabi-gcc -D__USE_CMSIS=CMSISv1p30_LPC11xx -DCR_INTEGER_PRINTF -DDEBUG -D__CODE_RED -I"C:\Users\v-tkrey\Documents\lpcxpresso_3.4\workspace\CMSISv1p30_LPC11xx\inc" -O0 -g -Wall -c -fmessage-length=0 -fno-builtin -ffunction-sections -mcpu=cortex-m0 -mthumb -D__REDLIB__ -MMD -MP -MF"src/uart.d" -MT"src/uart.d" -o"src/uart.o" "../src/uart.c"[/SIZE][/FONT]
[FONT=Arial][SIZE=2]Finished building: ../src/uart.c[/SIZE][/FONT][/LEFT]

[LEFT][FONT=Arial][SIZE=2]Building target: ssp.axf[/SIZE][/FONT]
[FONT=Arial][SIZE=2]Invoking: MCU Linker[/SIZE][/FONT]
[FONT=Arial][SIZE=2]arm-none-eabi-gcc -nostdlib -L"C:\Users\v-tkrey\Documents\lpcxpresso_3.4\workspace\CMSISv1p30_LPC11xx\Debug" -Xlinker -Map=ssp.map -Xlinker --gc-sections -mcpu=cortex-m0 -mthumb -T "ssp_Debug.ld" -o"ssp.axf" ./src/cr_startup_lpc11.o ./src/gpio.o ./src/ssp.o ./src/ssptest.o ./src/timer32.o ./src/uart.o -lCMSISv1p30_LPC11xx[/SIZE][/FONT]
[FONT=Arial][SIZE=2]Finished building target: ssp.axf[/SIZE][/FONT][/LEFT]

[LEFT][FONT=Arial][SIZE=2]make --no-print-directory post-build[/SIZE][/FONT]
[FONT=Arial][SIZE=2]Performing post-build steps[/SIZE][/FONT]
[FONT=Arial][SIZE=2]arm-none-eabi-size ssp.axf; # arm-none-eabi-objcopy -O ihex ssp.axf ssp.hex ;[/SIZE][/FONT]
[FONT=Arial][SIZE=2]text data bss dec hex filename[/SIZE][/FONT]
[FONT=Arial][SIZE=2]20980 524 956 22460 57bc ssp.axf[/SIZE][/FONT][/LEFT]
0 Kudos
Reply

1,608 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by CodeRedSupport on Mon Aug 30 03:44:07 MST 2010

Quote: tkreyche
I'm using sprintf only with integers and tried adding CR_INTEGER_PRINTF to 'MCU C Compiler, Symbols, Defined Symbols' and it did not change code size at all.


Please can you do a clean of the project, followed by a build - then copy'n'paste the build log into a text file and post that.

Regards,
CodeRedSupport
0 Kudos
Reply

1,608 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by tkreyche on Sun Aug 29 13:52:42 MST 2010
I'm using sprintf only with integers and tried adding CR_INTEGER_PRINTF to 'MCU C Compiler, Symbols, Defined Symbols' and it did not change code size at all.

Suggestions?
0 Kudos
Reply

1,608 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by jdebliek on Wed Jun 16 08:38:44 MST 2010
I tried using the sprintf function with the CR_INTEGER_PRINTF option, but it caused the code size to exceed the available flash (I have less than 3K available).  So I had to write a custom function to perform the task.
0 Kudos
Reply

1,608 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by CodeRedSupport on Wed Jun 16 08:26:24 MST 2010

Quote: jdebliek
One feature that would be nice is for reduced size/functionality stdio library options.



Which stdio functions are you actually using? And where is your i/o going?

There are some things you can already do here. For example if you are using one of the printf family, and only printing none-float values, then set the symbol CR_INTEGER_PRINTF to pull in a smaller, none-float printf variant.

Regards,
CodeRedSupport.
0 Kudos
Reply

1,608 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by jdebliek on Wed Jun 16 08:12:35 MST 2010
You are right.  Without the -nostdlib option I was getting system crashes, which went away after I re-enabled this option.  I am using the --gc-sections and -Os options as well, but since I need to reserve some of the flash for data storage the code size is constrained.

One feature that would be nice is for reduced size/functionality stdio library options.
0 Kudos
Reply

1,608 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by CodeRedSupport on Wed Jun 16 02:27:21 MST 2010
I strongly recommend that you do not do this. You will end up with parts of two different library systems (redlib and newlib) getting pulled in. Correct behaviour of your application is highly unlikely!

I suspect though that you application does not specify the "[FONT=Courier New][SIZE=2]--gc-sections[/SIZE][/FONT]" options in the link step. New projects created using the current version of the LPCXpresso IDE (3.3.4), and many of the examples provided with it, have this option turned off for Debug builds. This is because of issues some users have seen when debugging images built with this option (in particular slightly strange stepping behaviour and display of variables).

However, in the next release of LPCXpresso IDE (due in the next few weeks) these issues are fixed, and the use of "[FONT=Courier New][SIZE=2]--gc-sections[/SIZE][/FONT]" is again recommended for debug builds. What this option does is remove unused sections from your final image - providing a major decrease in code size in some cases.

For example,  the code size of the Debug variant of the LPCXpresso1114_systick_twinkle example built without "[FONT=Courier New][SIZE=2]--gc-sections[/SIZE][/FONT]" is
  
text    data    bss     dec     hex    
8012    524     32      8568    2178
Built with "[FONT=Courier New][SIZE=2]--gc-sections[/SIZE][/FONT]"
  
text    data    bss     dec     hex 
2984    524     32      3540    dd4    
To enable "[FONT=Courier New][SIZE=2]--gc-sections[/SIZE][/FONT]" in your own builds, go to...

Project Properties - C/C++ Build - Settings - MCU Linker - Miscellaneous

and add the option to the "[I]Other options (-XLinker [option])[/I]" box. You can check which sections have been removed by looking at the "[I]Discarded input sections[/I]" in the linker .map file that building your project creates.

If you are considering Release builds, then this by will enable "[FONT=Courier New][SIZE=2]--gc-sections[/SIZE][/FONT]" by default. You might want to check the compiler optimisation level though and ensure that the [FONT=Courier New][SIZE=2]"-Os"[/SIZE][/FONT] option is specified in the "[I]Other optimization flags[/I]" box.

Finally, please be assured that we at Code Red Technologies take the issue of code size extremely seriously. We continue to investigate ways of decreasing code size further in future releases.

Regards,
CodeRedSupport
0 Kudos
Reply