GCC_ARM optimisations

Hello.
I’ve been trying to implement MCUboot as per recommendation of a forum user here, following this demo: GitHub - AGlass0fMilk/mbed-mcuboot-demo: Demo of mcuboot with Mbed I’ve already run into some issues such as the board we’re using not supporting the FlashIAPBlockDevice class. So I chose to just use that demo as a guideline as much as possible.
It uses GCC_ARM to compile code the code, now, when I try to use GCC_ARM with a very simple program:

#include "UnbufferedSerial.h"
#include <cstring>

mbed::UnbufferedSerial pc(P2_1, P2_0, 115200);
int main()
{
    const char* string = "\r\nIN USER PROGRAM!!";
    pc.write(string, std::strlen(string));
    
    while (true) 
    {

    }
}

The resulting file size is absolutely gigantic! 540kB for the .bin file.
See the compilation output here. Something weird is going on for sure:

Space used after regions merged: 0x87000
Merging Regions
  Filling region application with .\BUILD\MAX32630FTHR\GCC_ARM\Bootloader_application.bin
Space used after regions merged: 0x87000
| Module               |     .text |   .data |     .bss |
|----------------------|-----------|---------|----------|
| [fill]               |    68(+0) |   0(+0) |  140(+0) |
| [lib]\c_nano.a       |  2634(+0) | 100(+0) |   21(+0) |
| [lib]\gcc.a          |   760(+0) |   0(+0) |    0(+0) |
| [lib]\misc           |   180(+0) |   4(+0) |   28(+0) |
| main.o               |   126(+0) |   0(+0) |  152(+0) |
| mbed-os\cmsis        |  7178(+0) | 168(+0) | 5956(+0) |
| mbed-os\connectivity |    32(+0) |   0(+0) |    0(+0) |
| mbed-os\drivers      |  3034(+0) |   0(+0) |    0(+0) |
| mbed-os\hal          |  1662(+0) |   8(+0) |  115(+0) |
| mbed-os\platform     |  5304(+0) | 260(+0) | 1172(+0) |
| mbed-os\rtos         |   240(+0) |   0(+0) |    0(+0) |
| mbed-os\targets      |  3718(+0) | 260(+0) |  384(+0) |
| Subtotals            | 24936(+0) | 800(+0) | 7968(+0) |
Total Static RAM memory (data + bss): 8768(+0) bytes
Total Flash memory (text + data): 25736(+0) bytes

Are there any optimisation options, and if so, how do I use these?

GCC is doing fine, it has something to do with Mbed merging regions after the build. Don’t use any Mbed bootloader directives in mbed_app.json. Can you show us your mbed_app.json?

Sure:

{
    "target_overrides": {
        "MAX32630FTHR": {
            "target.device_name": "MAX32630",
            "target.bootloader_supported": true,
            "platform.stdio-baud-rate"          : 115200,
            "platform.stdio-buffered-serial"    : true
        }
    }
}

However, when I remove the “target.bootloader_supported” option, the resulting .bin file is still the same size. Even just completely removing mbed_app.json reduces the size by just 4KB.

I’m not sure what is going on but these lines suggest that Mbed is doing something bootloader related:

Merging Regions
Filling region application with .\BUILD\MAX32630FTHR\GCC_ARM\Bootloader_application.bin
Space used after regions merged: 0x87000

Furthermore, how is your program related with mcuboot?

Yes, sorry, I should’ve been more clear. I was trying to follow his the demo/guide mentioned in the OP, but immediately noticed that my file size was much bigger even just building normally with GCC_ARM (that is, without including anything related to MCUboot).
In the meantime I have gotten a response on a related topic regarding MCUboot where the author of that demo also notices this issue.