Skip to main content
AP_040
Senior
June 12, 2019
Solved

Generated binary file size in STM32L4

  • June 12, 2019
  • 16 replies
  • 12693 views

Is there any specific address location in generated binary file so we can know the size of that binary file?

Using this, in the firmware to read the size of binary file from that location and use it in our application.

This topic has been closed for replies.
Best answer by Tesla DeLorean

If that doesn't work try

 .word _limit_flash - g_pfnVectors /* compute size */

16 replies

Tesla DeLorean
Guru
June 12, 2019

One typically has linker symbols, and you can refer to them within the code. For fixed location stuff people typically refer to the symbol via an empty/unused vector in the vector table at the front of the image.​

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
AP_040
AP_040Author
Senior
June 12, 2019

@Community member​ 

Yes, can you give example for this?

Tesla DeLorean
Guru
June 12, 2019

Not from my phone.

These​ aren't complicated concepts.

In the GNU tools the symbols are often defined in the linker script, see .LD and .MAP files. In Keil there are $$Limit$$ type symbols.​

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
AP_040
AP_040Author
Senior
June 12, 2019

@Community member​ 

If some constant value needs to be defined it will be okay but how can I store the binary size? How to calculate the size of .bin file?

Please provide some example to do it?

Which symbol is for Atollic TrueStudio?

Tesla DeLorean
Guru
June 12, 2019

I accept PayPal and Amazon Gift cards

https://community.st.com/s/question/0D70X000006Srs3SAC/end-of-program

extern uint32_t Load$$LR$$LR_IROM1$$Limit;

 printf("Limit %08X\n", (uint32_t)&Load$$LR$$LR_IROM1$$Limit);

extern uint32_t Load$$LR$$LR_IROM1$$Length;

 printf("Length %08X\n", (uint32_t)&Load$$LR$$LR_IROM1$$Length);

; In your starup.s file, could be placed at known offset in vector table

 IMPORT ||Load$$LR$$LR_IROM1$$Length||

 DCD ||Load$$LR$$LR_IROM1$$Length||

In GNU define a variable marking the end of text+code sections

Equivalent to

  _etext = .;    /* define a global symbols at end of code */

but including the initialization by placing beyond that.

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
AP_040
AP_040Author
Senior
June 12, 2019

@Community member​ 

I tried but it will give errors.

Here I attached the startup and linker file. So, can you suggest me where I can add those symbol to add binary size?

Tesla DeLorean
Guru
June 12, 2019

Yeah, because that's for Keil, something like this? Perhaps review how your tools function

startup.s

...

g_pfnVectors:

.word _estack

.word Reset_Handler

.word NMI_Handler

.word HardFault_Handler

.word MemManage_Handler

.word BusFault_Handler

.word UsageFault_Handler

.word _limit_flash /* symbol you define in .ld linker script */

.word 0

.word 0

.word 0

.word SVC_Handler

.word DebugMon_Handler

.word 0

...

script.ld

...

 /* User_heap_stack section, used to check that there is enough RAM left */

 ._user_heap_stack :

 {

  . = ALIGN(4);

  PROVIDE ( end = . );

  PROVIDE ( _end = . );

  . = . + _Min_Heap_Size;

  . = . + _Min_Stack_Size;

  . = ALIGN(4);

 } >RAM

 /* End section */

 . = ALIGN(4);

 .endof :

 {

  /* This is used by the startup in order to find the end */

  _limit_flash = .;     /* define a global symbol at the end of flash */

 } >FLASH

 /* Remove information from the standard libraries */

 /DISCARD/ :

 {

  libc.a ( * )

  libm.a ( * )

  libgcc.a ( * )

 }

...

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
AP_040
AP_040Author
Senior
June 12, 2019

For example, binary start at 0x8000000 location and if I read 4bytes from the 0x8000004, i will have the total length of this binary. How I do it?

AP_040
AP_040Author
Senior
June 13, 2019

Thanks, @Community member​ 

Now, I am able to get the size of binary from the specific location. But when we load binary into the MCU, it is not working.

So, I put the ".word _limit_flash" at last in the startup file then location of the size is changed and load .bin file into the MCU, it is working.

AP_040
AP_040Author
Senior
June 13, 2019

@Community member​  Is there any resource where I can find all those symbol list which we will use in startup file like"_limit_flash"?

Tesla DeLorean
Guru
June 13, 2019

The linker generates a .MAP file, I'd expect it to appear in there.

I generated it as a means to demonstrate how one uses linker scripts, the file itself reflects the use of several similar concepts in defining and placing the statics which are copied into RAM by the startup code. I'm not using GNU/GCC on a daily basis, but I can usually get things to work with a little trial and error and reviewing available documentation.

Most of this stuff should come from an understanding of Compilers, Assemblers, Linker and Loaders..

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
AP_040
AP_040Author
Senior
June 14, 2019

I am not too much expertise in all these concept but I will try my best to understand it.

Yes, it is available in .map file but it will generate once the compilation done.

So, before all that we have to know the symbols which will useful to add in the startup(.s) and linker(.ld) file. Where it is present? Is it in the GCC-IDE(Atollic TrueStudio) installation files?

AP_040
AP_040Author
Senior
July 2, 2019

@Community member​  Here one problem is found. When my binary size is greater then 65KB it will not give me the proper size. In every case, when I am checking the binary size and read, it is giving me the first two byte is 0x0802 every time and last two bytes is size.

Attached snapshot of example, my binary size is 119468 bytes(0x1D2AC). When I am read from ST-LINK it is showing 0x0802D2AC.

Why it is added 0x0802 in every binary? Can you please provide me the solution for this?

AP_040
AP_040Author
Senior
July 2, 2019

Can someone have an idea about this?