Skip to main content
zedeg
Associate III
January 18, 2019
Solved

Why do I keep getting "Flash region overflowed" error?

  • January 18, 2019
  • 2 replies
  • 21504 views

​Hi everyone)

I've attached project, that I refer further.

I'm trying to write program on my STM32F103(C8T6).

"From scratch" I've made CubeMX project, set all needed pins in modes I need to, generated it.

I've tried to flash a led on a couple of ports, it's ok.

So, next thing, I've converted project to C++, and this is part, when all the fun begins.

I've added function "my_main" to my_main.cpp file, used it from main.c, it worked, but when I tried to call some function from my_main() (HAL_Delay, or one of my class methods), I keep getting errors like "ld.exe: region `FLASH' overflowed by 17904 bytes", it can be caused by "stm32f103_lcd_board.elf section `.text' will not fit in region `FLASH'" or for example '.ARM' might not fit too (I've seen it on a project, that I used previously).

I can't understand, what's wrong, since earlier I've done projects like this with no trouble at all.

So, if someone have a clue, or can check attached project, I'll be grateful)

This topic has been closed for replies.
Best answer by zedeg

Sorry, didn't noticed "more posts" button)

So, briefly.

I've found a "fix" for this error. It's seems as TrueStudio bug to me... Let me explain what I did.

First at first, I couldn't get in mind, why there's some big C++ funcs, which I defenetly didn't use at all.

So, I started out with project's properties check... There were level -O0 optimization for both C and C++ parts.

I've changed it to various combinations, but none gave me significant result...

Then I decided to delete all TrueStudio related files (.prj, .ln, .settings and debug folders), and regenerate project with CubeMX, and convert it to C++ proj again. And after that... It compiled and assmbled, just perfectly, with overall size of 19KB.

So, as it turns out, when I generate stock project, it has optimization keys -Os for C and -O0 for C++.

if i change C key to -O0 - this issue shows up. Seems logical...

BUT! Even if I try to revert changes back, and use -Os for C, it takes no signifficant effect (as well, as -Od or other keys).

May be, I miss something important, but I've checked out this behaviour for several times, and it comes down to C optimization key irreversable change. If it's my fault, can somebody explain it to me? Thx)

Also, I've attached console build output screenshots to my post, as well as two 'versions' of project ("freshly-generated", that compiles just fine with 19K, and "spoiled" one, that compiles with unnecessary parts and is ~82K), if someone will want to analyze it.

2 replies

Bob S
Super User
January 18, 2019

How much FLASH does your CPU have? It looks like only 64K. That is not a lot of space. Depending on how much library code gets pulled in (C++ streams??????) you can quickly exceed that space. Or if you have large arrays that get initialized at compile time.

Look at your MAP file and see how much code the compiler is generating. Or find an F103 part with more FLASH in a pin-compatible package.

zedeg
zedegAuthor
Associate III
January 19, 2019

As about .map file, I don't know, where to look at, I'be not seen there any sizes at all.

I've atteched screenshots also, so hopefully you will understand, what I'm up to...

I know that 64k is not much, but.

I'm adding just a little class, with only two methods,​ to CubeMX freshly generated project, and it follows with this error.

This shouldn'tbe like that, since it's not first time using C++ on my projects with this board, and it was OK, even with greater classes, cstring, etc combined.

I've not ​added manually any of C++ std libraries (such as streams, or templates library), or even used any C++ features, besides just classes (no try catch, no templates, containers, nothing at all ).

So it looks pretty ​strange to me.

I can understand, that something adds to my code, that doubles it's ​size, but since I don't do anything that may result to this intentionally, I don't know, where to look at, besides my projects configuration...

Tesla DeLorean
Guru
January 19, 2019

Typically one could tell the linker it has more memory so it can get closure and then review who is eating all the memory. Usually the MAP file will break down code and data contributions at a function level. ​

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
Tesla DeLorean
Guru
January 19, 2019

Use a larger device or decide what you can live without. ​

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
zedeg
zedegAuthor
Associate III
January 19, 2019

But there's no code at all. How can I can live without any code at all? :D

Only MX inits (RCC, USB, GPIOB, I2C1), wich worked just fine previosly for this MCU and board, and smallest C++ class, that you can see on attached screenshot (named gpio.cpp)

As for .map I've honestly tried to read it, but it's too unclear to me. It doesn't have any generated code sizes also...

Yet, new .list (and .hex) are not generated by this point, so I couldn't understand, which part is redundant...

Tesla DeLorean
Guru
January 19, 2019

Edit the linker script to say you have more memory, for the time being, so the linker can get closure and generate map and listings.

C++ brings a lot of baggage with it. Some tools are better at dead code elimination and optimization than others, but library blobs may be harder to reduce if you don't have function level separation of the objects.

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