Teensy 3.1/3.2 - region `FLASH' overflowed by 86948 bytes while program is 40kb

I’m using Teensy 3.2 and cannot build my teensy code due to two warnings resulting in an error 1 return.

Warning 1 - .pio/build/teensy31/firmware.elf section .text' will not fit in region FLASH’

Warning 2 - region `FLASH’ overflowed by 86948 bytes

Error - collect2: error: ld returned 1 exit status

From what I read it basically means that the file is too large but my src folder is 40129 bytes and Teensy 3.2 flash size is 262144 bytes as it is written in the platforms/teensy/boards/teensy31.json file.

The src folder is a cpp file (with setup and loop functions) + 4 header files surrounding it with functions used in the cpp file. Also, the 2 warnings in the .h files are unrelated to the issue.

…Huh? O_O

The size of all the source files (as text) does not equal the size of the compiled program. This is not web development. A single line of C/C++ code can cause basically arbitrary usage of flash and RAM (think: (const) byte hugeArray[2*1024*1024];, 2 Megabytes of flash/ram usage in one line :slight_smile: ).

So what matters is the size of the compiled functions, classes and global variables / objects make it in the final build. You should do a test build where you hack the flash size to be very large so that the firmware builds, then throw it in an analyzer like mbed-os-linker-report or PlatformIO’s built-in analyzer.

As a first step for that, you have to modify the referenced linker file mk20dx256.ld. That file should be in C:\Users\<user>\.platformio\packages\framework-arduinoteensy\cores\teensy3. Bump up

MEMORY
{
	FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 256K
	RAM  (rwx) : ORIGIN = 0x1FFF8000, LENGTH = 64K
}

to

MEMORY
{
	FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 512K
	RAM  (rwx) : ORIGIN = 0x1FFF8000, LENGTH = 64K
}

and build the binary. It should go through. Then use on of the analyzing tools above, or send the .elf here.

You might want to read up on related cases like [ESP32] ld: region `dram0_0_seg' overflowed by 156768 bytes.

Got it, I included “iostream” for some reason which caused my memory size to skyrocket. Thanks for the memory change, it helped me to debug a lot!