How to calculate the Code size in the program

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

How to calculate the Code size in the program

Jump to solution
8,662 Views
gregwilson-lind
Contributor II

I want to do a CRC on my code to check if a code update worked. To do that in the program #I need to find out the size of the .text and .data saved in the Flash. I haven't been able to find any symbols that I can use to figure out the total size so far. Does anyone know of any symbols that are available to the program that will allow the total size of the code and data in the Flash to be calculated?

Labels (2)
1 Solution
7,001 Views
walterbuttazzo
Contributor II

I did this:

In loader file I added 

__application_size = SIZEOF(.text) + SIZEOF(.data);

after definition of .text and .data

Warning,

in LpcXpresso you should uncheck Manage Linker Script, located in

proprierties -> C/C++ Build -> Tool Settings -> MCU Linker -> Managed linker script

and link your own linker file in Linker Script text box 

(you can copy the default one and edit it)

In code I used variable 

extern unsigned long __application_size;

#define APPLICATION_SIZE (uint32_t) (&__application_size);

and function 

uint32_t getApplication_size(void)
{
return APPLICATION_SIZE;
}

Hope it works for you

View solution in original post

9 Replies
7,001 Views
converse
Senior Contributor V

Do not switch off managed linker scripts. Use the Frremarker linker script templates to add just the bits you want. That makes you changes future proof, and means you can move to other parts much more easily. See

https://community.nxp.com/thread/389006 

0 Kudos
Reply
7,001 Views
gregwilson-lind
Contributor II

Hi Con Verse,

I added:

__application_size = SIZEOF(.text) + SIZEOF(.data);

into the section_tail.ldt script but the value that I get for the combined size of the .text & .data sections is not what is reported by the linker during the compile/link. Is there a better way/place to do this?

Regards,

Greg

0 Kudos
Reply
7,001 Views
converse
Senior Contributor V

I think that is the correct place to place the addition.

However, your application probably consists of more that just .text and .data.. Depending on your target and its memory configuration, there are other sections where code and data may be placed, such as .data_RAM2 etc. 

0 Kudos
Reply
7,002 Views
walterbuttazzo
Contributor II

I did this:

In loader file I added 

__application_size = SIZEOF(.text) + SIZEOF(.data);

after definition of .text and .data

Warning,

in LpcXpresso you should uncheck Manage Linker Script, located in

proprierties -> C/C++ Build -> Tool Settings -> MCU Linker -> Managed linker script

and link your own linker file in Linker Script text box 

(you can copy the default one and edit it)

In code I used variable 

extern unsigned long __application_size;

#define APPLICATION_SIZE (uint32_t) (&__application_size);

and function 

uint32_t getApplication_size(void)
{
return APPLICATION_SIZE;
}

Hope it works for you

7,001 Views
gregwilson-lind
Contributor II

Hi Walter,

Your solution works just fine and is cleaner than mine. I wasn't thinking of modifying the linker, just using what was already provided.

Thanks,

Greg

0 Kudos
Reply
7,001 Views
converse
Senior Contributor V

Which toolchain are you using?

0 Kudos
Reply
7,001 Views
gregwilson-lind
Contributor II

LPCXpresso, so GCC I guess.

0 Kudos
Reply
7,001 Views
converse
Senior Contributor V

If you look n the startup file and linker script there are a bunch of symbols defined that should give you the details you need. Sorry I'm not near my tools at the moment, so can't give you the symbol names.

7,001 Views
gregwilson-lind
Contributor II

I found some symbols that were not working, turns out I needed to treat them as variable names and take their addresses. All is working now.

The variables are:

__exidx_start or __exidx_end         the end of the .text section.

__start_data_RAM2                        the start of my initialized RAM data

__end_data_RAM2                         the end of my initialized RAM data

So the total length of .text and .data in Flash is the address of __exidx_start + the distance between the start & end of the RAM2 addresses.

Thanks

0 Kudos
Reply