Size Of Sketch Pro grammatically

HI guys , Is there a way to know the sketch size grammatically ?
You know when you compile you sketch and the IDE shows
"Sketch uses 7656 bytes (26%) of program storage space. Maximum is 28672 bytes. "

How can I get this number programmatically ? The goal is to know how much Free Space I have in the Flash memory ?
I know the size of the bootloader wich is usually not changing. so I f I add the size of the sketch and subtract it from 32k (atmega32u4) I will get the size of the free space in the flash memory?

Any suggestions ?
i'm using Arduino Micro ATMEGA32u4

Where do you want to know that? In a PC application or in a Arduino sketch?

Depending on a the answer, the next question will probably be why it would useful to know?

so there is three possibilties :-

1- know it in the bootloader
2- know it in the sketch
3- know it in PC environment

Ideally I would like to know it in the bootloader , but knowing it in the sketch will do as well.

why ?

So I know how much free space I have in the Flash Memory, so I only write data up to the maximum size which equales to the free space in FM.

And Yes I CAN write data to FLASH memory from arduino sketch ..

which Arduino architecture? AVR?

Arduino Microusing ATMEGA32u4

From within the sketch, you can reference the symbols that the linker defines:

    extern "C" {char * __data_load_end();}
      :
    Serial.print("Flash size in bytes: ");
    Serial.println((long)__data_load_end*2)

I don't think it's possible to know from the bootloader - loading is typically done one page at a time without any indication how many pages exist in total.

On the PC, I guess you can parse either the elf file (there's a "readelf" library somewhere, I think), or the .hex file

westfw:
From within the sketch, you can reference the symbols that the linker defines:

    extern "C" {char * __data_load_end();}

:
    Serial.print("Flash size in bytes: ");
    Serial.println((long)__data_load_end*2)

Where did you find that?

I used flash storage libraries on SAMD (GitHub - cmaglie/FlashStorage: A convenient way to store data into Flash memory on the ATSAMD21 and ATSAMD) and nRF5 (GitHub - d00616/arduino-NVM: Direct flash memory access, round robin virtual pages and EEPROM like memory.)

with flash access support in Optiboot similar library could be made for AVR. or not?

Juraj:
I used flash storage libraries on SAMD (GitHub - cmaglie/FlashStorage: A convenient way to store data into Flash memory on the ATSAMD21 and ATSAMD) and nRF5 (GitHub - d00616/arduino-NVM: Direct flash memory access, round robin virtual pages and EEPROM like memory.)

with flash access support in Optiboot similar library could be made for AVR. or not?

I think that that is what OP is using (a modified versions of optiboot).

Where did you find that? [__data_load_end

Well, I've actually used _etext/_edata on RAM-only systems to figure out where to start the dynamical memory allocation pool (load a 300k program into 1M of RAM, how do you figure out that you have 700k left to play with?)

A quick experiment showed that _etext wasn't quite right, due to the flash segment used to hold the initial values of the .data section. A quick look with "avr-nm -n *.elf" listed the other symbols nearby, and __data_load_end certainly looked like the right choice. A quick test showed exact agreement with the output of the IDE...

(Note that this is probably dependent on the "linker map" used for a part, and might not be present if you move sufficiently far away (my test sketch wouln't compile for an arduino Zero, for example.))

sterretje:
I think that that is what OP is using (a modified versions of optiboot).

Optiboot 8 supports flash operations.

here is an example, if I understand it right
https://github.com/MCUdude/MiniCore/blob/master/avr/libraries/Optiboot_flasher/examples/SerialReadWrite/SerialReadWrite.ino

westfw:
From within the sketch, you can reference the symbols that the linker defines:

    extern "C" {char * __data_load_end();}

:
    Serial.print("Flash size in bytes: ");
    Serial.println((long)__data_load_end*2)

I tried to compile that for arduino Micro but it error out

Arduino: 1.8.4 (Windows 10), Board: "R&S FS-SNS40"

C:\Program Files (x86)\arduino-1.8.4\arduino-builder -dump-prefs -logger=machine -hardware C:\Program Files (x86)\arduino-1.8.4\hardware -hardware C:\Users\bghobreal\AppData\Local\Arduino15\packages -tools C:\Program Files (x86)\arduino-1.8.4\tools-builder -tools C:\Program Files (x86)\arduino-1.8.4\hardware\tools\avr -tools C:\Users\bghobreal\AppData\Local\Arduino15\packages -built-in-libraries C:\Program Files (x86)\arduino-1.8.4\libraries -libraries C:\Users\bghobreal\Documents\Arduino\libraries -fqbn=arduino:avr:FS-SNS40 -vid-pid=0X0AAD_0X01F0 -ide-version=10804 -build-path C:\Users\bghobreal\Documents\Labview\lib\SNS_Firmware_Working\build -warnings=none -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.avr-gcc.path=C:\Program Files (x86)\arduino-1.8.4\hardware\tools\avr -prefs=runtime.tools.arduinoOTA.path=C:\Program Files (x86)\arduino-1.8.4\hardware\tools\avr -prefs=runtime.tools.avrdude.path=C:\Program Files (x86)\arduino-1.8.4\hardware\tools\avr -verbose C:\Users\bghobreal\Documents\Arduino\Test_SNSBootloader\Test_SNSBootloader.ino
C:\Program Files (x86)\arduino-1.8.4\arduino-builder -compile -logger=machine -hardware C:\Program Files (x86)\arduino-1.8.4\hardware -hardware C:\Users\bghobreal\AppData\Local\Arduino15\packages -tools C:\Program Files (x86)\arduino-1.8.4\tools-builder -tools C:\Program Files (x86)\arduino-1.8.4\hardware\tools\avr -tools C:\Users\bghobreal\AppData\Local\Arduino15\packages -built-in-libraries C:\Program Files (x86)\arduino-1.8.4\libraries -libraries C:\Users\bghobreal\Documents\Arduino\libraries -fqbn=arduino:avr:FS-SNS40 -vid-pid=0X0AAD_0X01F0 -ide-version=10804 -build-path C:\Users\bghobreal\Documents\Labview\lib\SNS_Firmware_Working\build -warnings=none -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.avr-gcc.path=C:\Program Files (x86)\arduino-1.8.4\hardware\tools\avr -prefs=runtime.tools.arduinoOTA.path=C:\Program Files (x86)\arduino-1.8.4\hardware\tools\avr -prefs=runtime.tools.avrdude.path=C:\Program Files (x86)\arduino-1.8.4\hardware\tools\avr -verbose C:\Users\bghobreal\Documents\Arduino\Test_SNSBootloader\Test_SNSBootloader.ino
Using board 'FS-SNS40' from platform in folder: C:\Program Files (x86)\arduino-1.8.4\hardware\arduino\avr
Using core 'arduino' from platform in folder: C:\Program Files (x86)\arduino-1.8.4\hardware\arduino\avr
Detecting libraries used...
"C:\Program Files (x86)\arduino-1.8.4\hardware\tools\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics  -flto -w -x c++ -E -CC -mmcu=atmega32u4 -DF_CPU=16000000L -DARDUINO=10804 -DARDUINO_AVR_FS-SNS40 -DARDUINO_ARCH_AVR  -DUSB_VID=0x0AAD -DUSB_PID=0x01F1 '-DUSB_MANUFACTURER="Rohde & Schwarz "' '-DUSB_PRODUCT="R&S FS-SNS40  "' -DMAGIC_KEY_POS=(RAMEND-1) "-IC:\Program Files (x86)\arduino-1.8.4\hardware\arduino\avr\cores\arduino" "-IC:\Program Files (x86)\arduino-1.8.4\hardware\arduino\avr\variants\R&S FS-SNS" "C:\Users\bghobreal\Documents\Labview\lib\SNS_Firmware_Working\build\sketch\Test_SNSBootloader.ino.cpp" -o "nul"
"C:\Program Files (x86)\arduino-1.8.4\hardware\tools\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics  -flto -w -x c++ -E -CC -mmcu=atmega32u4 -DF_CPU=16000000L -DARDUINO=10804 -DARDUINO_AVR_FS-SNS40 -DARDUINO_ARCH_AVR  -DUSB_VID=0x0AAD -DUSB_PID=0x01F1 '-DUSB_MANUFACTURER="Rohde & Schwarz "' '-DUSB_PRODUCT="R&S FS-SNS40  "' -DMAGIC_KEY_POS=(RAMEND-1) "-IC:\Program Files (x86)\arduino-1.8.4\hardware\arduino\avr\cores\arduino" "-IC:\Program Files (x86)\arduino-1.8.4\hardware\arduino\avr\variants\R&S FS-SNS" "-IC:\Program Files (x86)\arduino-1.8.4\hardware\arduino\avr\libraries\EEPROM\src" "C:\Users\bghobreal\Documents\Labview\lib\SNS_Firmware_Working\build\sketch\Test_SNSBootloader.ino.cpp" -o "nul"
Generating function prototypes...
"C:\Program Files (x86)\arduino-1.8.4\hardware\tools\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics  -flto -w -x c++ -E -CC -mmcu=atmega32u4 -DF_CPU=16000000L -DARDUINO=10804 -DARDUINO_AVR_FS-SNS40 -DARDUINO_ARCH_AVR  -DUSB_VID=0x0AAD -DUSB_PID=0x01F1 '-DUSB_MANUFACTURER="Rohde & Schwarz "' '-DUSB_PRODUCT="R&S FS-SNS40  "' -DMAGIC_KEY_POS=(RAMEND-1) "-IC:\Program Files (x86)\arduino-1.8.4\hardware\arduino\avr\cores\arduino" "-IC:\Program Files (x86)\arduino-1.8.4\hardware\arduino\avr\variants\R&S FS-SNS" "-IC:\Program Files (x86)\arduino-1.8.4\hardware\arduino\avr\libraries\EEPROM\src" "C:\Users\bghobreal\Documents\Labview\lib\SNS_Firmware_Working\build\sketch\Test_SNSBootloader.ino.cpp" -o "C:\Users\bghobreal\Documents\Labview\lib\SNS_Firmware_Working\build\preproc\ctags_target_for_gcc_minus_e.cpp"
"C:\Program Files (x86)\arduino-1.8.4\tools-builder\ctags\5.8-arduino11/ctags" -u --language-force=c++ -f - --c++-kinds=svpf --fields=KSTtzns --line-directives "C:\Users\bghobreal\Documents\Labview\lib\SNS_Firmware_Working\build\preproc\ctags_target_for_gcc_minus_e.cpp"
Compiling sketch...
"C:\Program Files (x86)\arduino-1.8.4\hardware\tools\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -MMD -flto -mmcu=atmega32u4 -DF_CPU=16000000L -DARDUINO=10804 -DARDUINO_AVR_FS-SNS40 -DARDUINO_ARCH_AVR  -DUSB_VID=0x0AAD -DUSB_PID=0x01F1 '-DUSB_MANUFACTURER="Rohde & Schwarz "' '-DUSB_PRODUCT="R&S FS-SNS40  "' -DMAGIC_KEY_POS=(RAMEND-1) "-IC:\Program Files (x86)\arduino-1.8.4\hardware\arduino\avr\cores\arduino" "-IC:\Program Files (x86)\arduino-1.8.4\hardware\arduino\avr\variants\R&S FS-SNS" "-IC:\Program Files (x86)\arduino-1.8.4\hardware\arduino\avr\libraries\EEPROM\src" "C:\Users\bghobreal\Documents\Labview\lib\SNS_Firmware_Working\build\sketch\Test_SNSBootloader.ino.cpp" -o "C:\Users\bghobreal\Documents\Labview\lib\SNS_Firmware_Working\build\sketch\Test_SNSBootloader.ino.cpp.o"
C:\Users\bghobreal\Documents\Arduino\Test_SNSBootloader\Test_SNSBootloader.ino: In function 'void loop()':

Test_SNSBootloader:90: error: expected unqualified-id before string constant

            extern "C" {char * __data_load_end();}

                   ^

Test_SNSBootloader:93: error: '__data_load_end' was not declared in this scope

         Serial.println((long)__data_load_end*2)

                              ^

Using library EEPROM at version 2.0 in folder: C:\Program Files (x86)\arduino-1.8.4\hardware\arduino\avr\libraries\EEPROM 
exit status 1
expected unqualified-id before string constant
[/code/

Are you using IDE Environment ?

bemin:
I tried to compile that for arduino Micro but it error out

Arduino: 1.8.4 (Windows 10), Board: "R&S FS-SNS40"

C:\Program Files (x86)\arduino-1.8.4\arduino-builder -dump-prefs -logger=machine -hardware C:\Program Files (x86)\arduino-1.8.4\hardware -hardware C:\Users\bghobreal\AppData\Local\Arduino15\packages -tools C:\Program Files (x86)\arduino-1.8.4\tools-builder -tools C:\Program Files (x86)\arduino-1.8.4\hardware\tools\avr -tools C:\Users\bghobreal\AppData\Local\Arduino15\packages -built-in-libraries C:\Program Files (x86)\arduino-1.8.4\libraries -libraries C:\Users\bghobreal\Documents\Arduino\libraries -fqbn=arduino:avr:FS-SNS40 -vid-pid=0X0AAD_0X01F0 -ide-version=10804 -build-path C:\Users\bghobreal\Documents\Labview\lib\SNS_Firmware_Working\build -warnings=none -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.avr-gcc.path=C:\Program Files (x86)\arduino-1.8.4\hardware\tools\avr -prefs=runtime.tools.arduinoOTA.path=C:\Program Files (x86)\arduino-1.8.4\hardware\tools\avr -prefs=runtime.tools.avrdude.path=C:\Program Files (x86)\arduino-1.8.4\hardware\tools\avr -verbose C:\Users\bghobreal\Documents\Arduino\Test_SNSBootloader\Test_SNSBootloader.ino
C:\Program Files (x86)\arduino-1.8.4\arduino-builder -compile -logger=machine -hardware C:\Program Files (x86)\arduino-1.8.4\hardware -hardware C:\Users\bghobreal\AppData\Local\Arduino15\packages -tools C:\Program Files (x86)\arduino-1.8.4\tools-builder -tools C:\Program Files (x86)\arduino-1.8.4\hardware\tools\avr -tools C:\Users\bghobreal\AppData\Local\Arduino15\packages -built-in-libraries C:\Program Files (x86)\arduino-1.8.4\libraries -libraries C:\Users\bghobreal\Documents\Arduino\libraries -fqbn=arduino:avr:FS-SNS40 -vid-pid=0X0AAD_0X01F0 -ide-version=10804 -build-path C:\Users\bghobreal\Documents\Labview\lib\SNS_Firmware_Working\build -warnings=none -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.avr-gcc.path=C:\Program Files (x86)\arduino-1.8.4\hardware\tools\avr -prefs=runtime.tools.arduinoOTA.path=C:\Program Files (x86)\arduino-1.8.4\hardware\tools\avr -prefs=runtime.tools.avrdude.path=C:\Program Files (x86)\arduino-1.8.4\hardware\tools\avr -verbose C:\Users\bghobreal\Documents\Arduino\Test_SNSBootloader\Test_SNSBootloader.ino
Using board 'FS-SNS40' from platform in folder: C:\Program Files (x86)\arduino-1.8.4\hardware\arduino\avr
Using core 'arduino' from platform in folder: C:\Program Files (x86)\arduino-1.8.4\hardware\arduino\avr
Detecting libraries used...
"C:\Program Files (x86)\arduino-1.8.4\hardware\tools\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics  -flto -w -x c++ -E -CC -mmcu=atmega32u4 -DF_CPU=16000000L -DARDUINO=10804 -DARDUINO_AVR_FS-SNS40 -DARDUINO_ARCH_AVR  -DUSB_VID=0x0AAD -DUSB_PID=0x01F1 '-DUSB_MANUFACTURER="Rohde & Schwarz "' '-DUSB_PRODUCT="R&S FS-SNS40  "' -DMAGIC_KEY_POS=(RAMEND-1) "-IC:\Program Files (x86)\arduino-1.8.4\hardware\arduino\avr\cores\arduino" "-IC:\Program Files (x86)\arduino-1.8.4\hardware\arduino\avr\variants\R&S FS-SNS" "C:\Users\bghobreal\Documents\Labview\lib\SNS_Firmware_Working\build\sketch\Test_SNSBootloader.ino.cpp" -o "nul"
"C:\Program Files (x86)\arduino-1.8.4\hardware\tools\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics  -flto -w -x c++ -E -CC -mmcu=atmega32u4 -DF_CPU=16000000L -DARDUINO=10804 -DARDUINO_AVR_FS-SNS40 -DARDUINO_ARCH_AVR  -DUSB_VID=0x0AAD -DUSB_PID=0x01F1 '-DUSB_MANUFACTURER="Rohde & Schwarz "' '-DUSB_PRODUCT="R&S FS-SNS40  "' -DMAGIC_KEY_POS=(RAMEND-1) "-IC:\Program Files (x86)\arduino-1.8.4\hardware\arduino\avr\cores\arduino" "-IC:\Program Files (x86)\arduino-1.8.4\hardware\arduino\avr\variants\R&S FS-SNS" "-IC:\Program Files (x86)\arduino-1.8.4\hardware\arduino\avr\libraries\EEPROM\src" "C:\Users\bghobreal\Documents\Labview\lib\SNS_Firmware_Working\build\sketch\Test_SNSBootloader.ino.cpp" -o "nul"
Generating function prototypes...
"C:\Program Files (x86)\arduino-1.8.4\hardware\tools\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics  -flto -w -x c++ -E -CC -mmcu=atmega32u4 -DF_CPU=16000000L -DARDUINO=10804 -DARDUINO_AVR_FS-SNS40 -DARDUINO_ARCH_AVR  -DUSB_VID=0x0AAD -DUSB_PID=0x01F1 '-DUSB_MANUFACTURER="Rohde & Schwarz "' '-DUSB_PRODUCT="R&S FS-SNS40  "' -DMAGIC_KEY_POS=(RAMEND-1) "-IC:\Program Files (x86)\arduino-1.8.4\hardware\arduino\avr\cores\arduino" "-IC:\Program Files (x86)\arduino-1.8.4\hardware\arduino\avr\variants\R&S FS-SNS" "-IC:\Program Files (x86)\arduino-1.8.4\hardware\arduino\avr\libraries\EEPROM\src" "C:\Users\bghobreal\Documents\Labview\lib\SNS_Firmware_Working\build\sketch\Test_SNSBootloader.ino.cpp" -o "C:\Users\bghobreal\Documents\Labview\lib\SNS_Firmware_Working\build\preproc\ctags_target_for_gcc_minus_e.cpp"
"C:\Program Files (x86)\arduino-1.8.4\tools-builder\ctags\5.8-arduino11/ctags" -u --language-force=c++ -f - --c++-kinds=svpf --fields=KSTtzns --line-directives "C:\Users\bghobreal\Documents\Labview\lib\SNS_Firmware_Working\build\preproc\ctags_target_for_gcc_minus_e.cpp"
Compiling sketch...
"C:\Program Files (x86)\arduino-1.8.4\hardware\tools\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -MMD -flto -mmcu=atmega32u4 -DF_CPU=16000000L -DARDUINO=10804 -DARDUINO_AVR_FS-SNS40 -DARDUINO_ARCH_AVR  -DUSB_VID=0x0AAD -DUSB_PID=0x01F1 '-DUSB_MANUFACTURER="Rohde & Schwarz "' '-DUSB_PRODUCT="R&S FS-SNS40  "' -DMAGIC_KEY_POS=(RAMEND-1) "-IC:\Program Files (x86)\arduino-1.8.4\hardware\arduino\avr\cores\arduino" "-IC:\Program Files (x86)\arduino-1.8.4\hardware\arduino\avr\variants\R&S FS-SNS" "-IC:\Program Files (x86)\arduino-1.8.4\hardware\arduino\avr\libraries\EEPROM\src" "C:\Users\bghobreal\Documents\Labview\lib\SNS_Firmware_Working\build\sketch\Test_SNSBootloader.ino.cpp" -o "C:\Users\bghobreal\Documents\Labview\lib\SNS_Firmware_Working\build\sketch\Test_SNSBootloader.ino.cpp.o"
C:\Users\bghobreal\Documents\Arduino\Test_SNSBootloader\Test_SNSBootloader.ino: In function 'void loop()':

Test_SNSBootloader:90: error: expected unqualified-id before string constant

extern "C" {char * __data_load_end();}

^

Test_SNSBootloader:93: error: '__data_load_end' was not declared in this scope

Serial.println((long)__data_load_end*2)

^

Using library EEPROM at version 2.0 in folder: C:\Program Files (x86)\arduino-1.8.4\hardware\arduino\avr\libraries\EEPROM
exit status 1
expected unqualified-id before string constant
[/code/

Are you using IDE Environment ?

 extern "C" {char * __data_load_end();}

is a declaration of external C function. put it outside of any function

oh It needed to be in a header file !
This is perfect it gives me everything I need

It gives me the start of the Sketcch
the end of the Sketch
The Start of Bootloader
The end of the Boottloader

Now I can calculate the Empty flashe memory in between !!

Thanks
ALOT !!!!!

in my elf file I hhave

00007ff6 d _binary_______SNSBootloader_SNSBootloader_bin_end
000017e6 A _binary_______SNSBootloader_SNSBootloader_bin_size
00006810 d _binary_______SNSBootloader_SNSBootloader_bin_start

I should do something like that right ???

  extern "C" {char * _binary_______SNSBootloader_SNSBootloader_bin_end();}

        Serial.println((long)_binary_______SNSBootloader_SNSBootloader_bin_end*2);

but when I do that I get an error

C:\Users\bghobreal\Documents\Arduino\Test_SNSBootloader/Test_SNSBootloader.ino:94: undefined reference to `_binary_______SNSBootloader_SNSBootloader_bin_end'

C:\Users\bghobreal\Documents\Arduino\Test_SNSBootloader/Test_SNSBootloader.ino:94: undefined reference to `_binary_______SNSBootloader_SNSBootloader_bin_end'

It looks like it should work. Although it also looks a bit odd because your symbols are in the data segment. To be flash on an AVR, they ought to be in the text segment, or absolute. My test program produces:

000007d2 A __data_load_start
000007d2 T _etext
00000832 A __data_load_end

(Where do those symbols come from? Linker magic to include .bin files? You might have to adjust the command a bit because of the AVR's memory structure.)

westfw:
It looks like it should work. Although it also looks a bit odd because your symbols are in the data segment. To be flash on an AVR, they ought to be in the text segment, or absolute. My test program produces:

000007d2 A __data_load_start

000007d2 T _etext
00000832 A __data_load_end



(Where do those symbols come from? Linker magic to include .bin files? You might have to adjust the command a bit because of the AVR's memory structure.)

Yes I used the .bin file and i adjusted the location and now it is working
thanks