sudar / Arduino-Makefile

Makefile for Arduino sketches. It defines the workflows for compiling code, flashing it to Arduino and even communicating through Serial.
http://hardwarefun.com/tutorials/compiling-arduino-sketches-using-makefile
GNU Lesser General Public License v2.1
2.02k stars 449 forks source link

Wrong Memory Usage report on Arduino Micro #416

Open rnestler opened 8 years ago

rnestler commented 8 years ago

The Arduino-Makefile reports wrong memory usage compared to the Arduino IDE:

Arduino-Makefile:

AVR Memory Usage
----------------
Device: atmega32u4

Program:   27396 bytes (83.6% Full)
(.text + .data + .bootloader)

Data:       1323 bytes (51.7% Full)
(.data + .bss + .noinit)

For the same sketch the Arduino IDE shows:

Sketch uses 27,392 bytes (95%) of program storage space. Maximum is 28,672 bytes.
Global variables use 1,323 bytes (51%) of dynamic memory, leaving 1,237 bytes for local variables. Maximum is 2,560 bytes.

Now the difference between the byte values is not that big, but the difference between the percentage values is.

The evil thing is, if the byte usage grows over the 28,672 bytes, make upload bricks the Arduino micro and I have to reprogram the bootloader! So the Arduino-Makefile doesn't seem to take the size of the bootloader into account and seem to break it when uploading.

Here are some relevant parts from the Makefile I use:

BOARD_TAG     = micro
CFLAGS_STD = -std=gnu11
CXXFLAGS_STD = -std=gnu++11 -fno-threadsafe-statics

System:

Ubuntu 14.04.4 LTS
arduino-1.6.7
sej7278 commented 8 years ago

weird, as we just shell out to avr-size --mcu=atmega32u4 -C --format=avr build-leonardo/blink.elf

i've no idea how the ide calculates it, but calling avr-size on the elf file the ide creates, reports the same size info as the ide does - not the same as the makefile.

potentially its an avr-size bug....?

sej7278 commented 8 years ago

i think this must be something different the IDE and Makefile are doing with linking or flags, not anything to do with avr-size, they're just creating different sized binaries.

as the 1.0.5 IDE and avr-size on its /tmp file report the same:

avr-size --mcu=atmega32u4 -C --format=avr blink.cpp.elf 
AVR Memory Usage
----------------
Device: atmega32u4

Program:    4736 bytes (14.5% Full)
(.text + .data + .bootloader)

Data:        157 bytes (6.1% Full)
(.data + .bss + .noinit)

but the the makefile using the same 1.0.5 core, and avr-size report the same as each other, but different to the IDE - also the actual elf file on disk is different:

avr-size --mcu=atmega32u4 -C --format=avr build-leonardo/blink.elf
AVR Memory Usage
----------------
Device: atmega32u4

Program:    4304 bytes (13.1% Full)
(.text + .data + .bootloader)

Data:        154 bytes (6.0% Full)
(.data + .bss + .noinit)

the same happens with 1.6.12 - ide/builder make different size outputs to the makefile:

IDE:

Sketch uses 1,470 bytes (0%) of program storage space. Maximum is 253,952 bytes.
Global variables use 9 bytes (0%) of dynamic memory, leaving 8,183 bytes for local variables. Maximum is 8,192 bytes.

/tmp file created by the IDE (12448 bytes on disk):

/tmp/arduino_build_285509$ avr-size --mcu=atmega2560 -C --format=avr blink.ino.elf 
AVR Memory Usage
----------------
Device: atmega2560

Program:    1470 bytes (0.6% Full)
(.text + .data + .bootloader)

Data:          9 bytes (0.1% Full)
(.data + .bss + .noinit)

makefile (12544 bytes on disk):

AVR Memory Usage
----------------
Device: atmega2560

Program:    1564 bytes (0.6% Full)
(.text + .data + .bootloader)

Data:          9 bytes (0.1% Full)
(.data + .bss + .noinit)

So it seems its not reporting the wrong size, its just creating binaries of different size to the IDE.

rnestler commented 7 years ago

i think this must be something different the IDE and Makefile are doing with linking or flags, not anything to do with avr-size, they're just creating different sized binaries.

That's what I think as well. But the worst part is, that the Makefile will produce binaries which will destroy the Arduino bootloader! If you try to produce binaries which are bigger than 28,672 bytes, the Arduino IDE will give you an error, but the Makefile will generate a binary which corrupts the bootloader if you try to download it.