Why is code size so big for even an empty project when using KSDK 2?

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

Why is code size so big for even an empty project when using KSDK 2?

Jump to solution
3,836 Views
abdullahkahrama
Contributor IV

I am using MKL03Z8VFG4 (Kinetis KL03: 48MHz Cortex-M0+ Ultra-Low Power MCU, 8KB Flash, 2KB SRAM, 16-QFN).

My IDE is MCUXpresso v10.0.0 [Build 344].

I have built my custom KSDK 2.2.0  via https://mcuxpresso.nxp.com/en/builder.

Now, I have a literally empty main block in my program. I do nothing, no initialization. Only the startup code is there I guess. I have selected "Optimize for size" and my "Preprocessor symbol list" is below:

Untitled.png

When I compile, the output size is  2588 and I did NOTHING! Keep in mind that this is an MCU with 8 kB flash.

Untitled.png

Untitled.png

What is the reason behind this and how can I reduce that?

Labels (1)
Tags (1)
1 Solution
2,813 Views
ZhangJennie
NXP TechSupport
NXP TechSupport

Hi,

To reduce the code size, I suggest you choose Redlib(none) or Newlib(None) as library type when creates a new project. see attached video.

Each C library family is provided in a number of different variants : None, Nohost and Nohost-nf, Semihost and Semihost-nf (Redlib only). These variants each provide a different set of ‘stubs’ that form the very bottom of the C library and include certain low-level functions used by other functions in the library.

for more information about library variants, Please see MCUXpresso help

pastedImage_1.png


Have a great day,
Jennie Zhang

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

View solution in original post

9 Replies
2,814 Views
ZhangJennie
NXP TechSupport
NXP TechSupport

Hi,

To reduce the code size, I suggest you choose Redlib(none) or Newlib(None) as library type when creates a new project. see attached video.

Each C library family is provided in a number of different variants : None, Nohost and Nohost-nf, Semihost and Semihost-nf (Redlib only). These variants each provide a different set of ‘stubs’ that form the very bottom of the C library and include certain low-level functions used by other functions in the library.

for more information about library variants, Please see MCUXpresso help

pastedImage_1.png


Have a great day,
Jennie Zhang

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

2,813 Views
abdullahkahrama
Contributor IV

Hello Jennie,

Thanks for this info! It greatly reduces the code size :smileyhappy:

However, I am curious about that ~1kB used without doing anything. That is about ~10% of the FLASH. What is the reason for that?

0 Kudos
2,813 Views
ZhangJennie
NXP TechSupport
NXP TechSupport

Hi,

when you use other library for creating project, for example, Redlib(semihost), the project will include io functions for semihosting code. For example, _fdopen, _fclose,etc. Thus the project code size gets increased.

if use Redlib(none), this function  is not included.

Best Regards,

Jennie Zhang

0 Kudos
2,813 Views
abdullahkahrama
Contributor IV

Oh yes, I understand that. However, even if I select redlib(none) or newlib(none) the code size is about 1 kB without writing a single line of code.

I am asking about that?

0 Kudos
2,813 Views
BlackNight
NXP Employee
NXP Employee

Hi Abdullah,

If you don't need any library support, you can set it to 'no libraries':

pastedImage_1.png

But keep in mind that there is still the startup code, so even if you do not have anything in main(), the startup code is used too, together with the vector table (which counts to the FLASH size too).

The startup code does initialization of variables/constructors (required for proper C and C++) plus configures the basic things of the CPU. If you know what you are doing and if you don't need that, you can of course turn things off and jump to main from the reset vector (not recommended, unless you know *exactly* what you do).

As in general about code size considerations, you might have a read at Dealing with Code Size in Kinetis SDK v2.x Projects | MCU on Eclipse and Optimizing the Kinetis gcc Startup | MCU on Eclipse . It is written for GNU toolchains, and the same principles apply to MCUXpresso IDE too. It explains the different things the startup code does.

I hope this helps,

Erich

2,813 Views
abdullahkahrama
Contributor IV

Hello Erich,

Thank you for this info! Yes, I've read all of it, you are the library for kinetis MCUs =)

I have squeezed it down to about 1 kB with the help I got from you and Jennie. I was wondering about that remaining 1 kB, which seems to be the startup code that amazes me. It is too big! 

Thank you again for your contributions to the community.

0 Kudos
2,813 Views
BlackNight
NXP Employee
NXP Employee

Hi Abdullah

the 1K comes from the vector table and the filling up to 0x400. You can see this in the map file (example for K64F):

Around 500 bytes for the vectors:

 *(.isr_vector)
 .isr_vector    0x00000000      0x198 ./startup/startup_mk64f12.o

and the linker fills up the FLASH up to 0x400:

.text           0x00000000      0x424
 FILL mask 0xff

(that filliing is questionable in my view, as this space could be easily used for constant data or code, but it seems this is how the Kinetis SDK is by default generating the linker file).

If you create a S19 file (see MCUXpresso IDE: S-Record, Intel Hex and Binary Files | MCU on Eclipse ), you can easily see this too:

pastedImage_64.png

In above example, the startup code is directly calling main() which is basically 'empty'.

I hope this helps,

Erich

2,813 Views
abdullahkahrama
Contributor IV

Wow, that is insane! Thank you a lot for the insights =)

0 Kudos
2,813 Views
ZhangJennie
NXP TechSupport
NXP TechSupport

Hi Abdullash,

The 1 KB code includes interrupt vector table, handler, startup code, user source code, flash configuration code.

Because flash configuration field starts from 0x00400, the "gap" above 0x00400 is filled with 0xFF,  that causes the total "text" size over 1k.  If we uncheck "Enable automatic placement of Flash Configuration field in image", you will see the code size down.

pastedImage_1.png

attached are two hex file generated with the same project.

MKL03Z8xxx4_Project_INCLUDE_FLASHCFG.hex   - with "Enable automatic placement of Flash Configuration field in image" checked.

MKL03Z8xxx4_Project_NO_FLASHCFG   - with "Enable automatic placement of Flash Configuration field in image" unchecked.

you will see the difference.


Have a great day,
Jennie Zhang

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------