Content originally posted in LPCWare by CodeRedSupport on Mon Apr 26 03:26:46 MST 2010
Sorry for the delay in contributing further to this thread. Here are a few comments on some of the issues that have been raised....
[U][B]Unused Code elimination[/B][/U]
A big contribution to this difference can often be caused by whether the --gc-sections linker option is being used. When this option is turned on, the linker will remove unused sections from objects that are being linked together. This can typically reduce the overall code size of your application quite considerably. Note that the compiler options -ffunction-sections and -fdata-sections are required on the compile step to cause code/data to be placed into separate sections in an object file created from a c file.
In the original release of LPCXpresso IDE, the --gc-sections was specified for both the Debug and Release configurations of newly created projects (and hence in the provided examples). However a number of users saw problems when debugging Debug builds - typically slightly strange behaviour when single stepping and the viewing of variables.
As the whole point of the Debug configuration is to debug, we therefore took the decision to remove the --gc-sections option from the link step of Debug Configurations in more recent versions of the LPCXpresso IDE, whilst we in the background looked the issues with debugging --gc-sections linked code. This unfortunately means that code size of Debug builds does increase.
One thing that you could therefore do is to add back in the linker --gc-sections, but accept that you may encounter debugging oddities as a result.
A slight modification to this that appears (from my initial tests at least) to allow debugging without such debug oddities, whilst still removing unused C library sections. To do this, add the --gc-sections option to the link step of your application, but ensure that you do **NOT** specify the -ffunction-sections or -fdata-sections in your compile steps for your application (or in any "local" library projects that your application uses, such as CMSIS).
Using this mechanism can make a noticeable difference to the size of a Debug build of your application. For example, building the LPCXpresso1114_systick_twinkle example from the LPCXpresso1114.zip in the LPCXpresso IDE 3.3.4 release gives the following binary sizes....
8568 bytes - as delivered, no --gc-sections
3540 bytes - with --gc-sections
5592 bytes - with --gc-sections, but no -ffunction-sections/-fdata-sections
You can see that using --gc-sections, but with no -ffunction-sections/-fdata-sections compiler options gives a useful saving.
Note that if you look in the .map file that is created automatically by your build, you can see the discarded input sections.
[U][B]Redlib libraries and --gc-sections[/B][/U]
Most of the Redlib C libraries are written in C and have been build with -ffunction-sections. This is what allows the code size reduction described previously when the link step uses --gc-sections. However the test case that igorsk found with helpers.o is actually written is assembler (and hence -ffunction-sections has no effect), and is mainly made up of "wrapper functions", which are not been written into separate sections in the current version. We will look into doing this in a future version of the tools.
[U][B]Multiple copies of NVIC_EnableIRQ[/B][/U]
NVIC_EnableIRQ() is implemented by CMSIS as a static inline function within the header file core_cm0.h. This is done so that the compiler will inline the code of the function, rather than making a subroutine call. This will typically have code size and performance benefits. However the inlining will not take place when you do a Debug build (optimisation level = -O0) - and thus you will end up with a static copy of NVIC_EnableIRQ() in every object where a call to it is made.
I can see that this is potentially not ideal, and we will investigate potential improvements in future release of the CMSIS libraries. [Note that CodeRed are not the maintainers of the CMSIS library sources, but we do work closely with ARM/Keil (and other contributors) on this.]
[U][B]Code size and __main[/B][/U]
We are currently investigating a number of possible ways of decreasing the contribution to code size from RedLib, including the calls made by __main. As previously stated you can cause some reductions by bypassing the call from the startup code to __main and calling main directly. However we would not recommend this, and if you do do it be aware of potential issues this may cause you!
Regards,
CodeRedSupport