Issue with Flipping Source Code Address on STM32F407 Microcontroller for Mbed Project

I’m working on an Mbed project and I’m encountering an issue with writing source code to a flipped address on the STM32F407 microcontroller. My objective is to write my boot code at address 0x08000000 and my main Mbed-OS project at 0x08040000. My boot code is a bare-metal Keil project based on HAL, and it works perfectly fine in other projects where the main project is not written in Mbed. However, I’m facing difficulties when flipping the main source address in flash.
I have added the following lines to my mbed_app.json file to specify the custom application start address:

"config": {
    "mbed_app_start": {
        "help": "Use a custom application start address",
        "value": "0x08040000",
        "macro_name": "MBED_APP_START"
    }
},

To change the location of NVIC vectors, I’m using the following code:

NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x8040000);`

This function sets the base address of the interrupt vector table and allows the CPU to locate the interrupt handler routines. However, after jumping from my boot to 0x08040000, the microcontroller freezes.
I tried turning off RTOS in Mbed by adding "requires": ["bare-metal"] to mbed_app.json. This allowed my code to work, but unfortunately, it’s not enough.
I also tried some unconventional approaches and found that if I remove the boot code and write my main Mbed project as RTOS at both 0x08000000 and 0x08040000, it works well without any problem. However, in this case, I shouldn’t use NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x8040000). If I use this function, the microcontroller still freezes.
I wrote my source code with some changes at 0x08040000 and found that it prints and works from 0x08040000. Strangely, there aren’t any jumps, somehow it starts with 0x08000000, uses things from it, and also uses NVIC vectors, but functions are called from 0x08040000. I saw this in the .map file and also confirmed it by printing the addresses of my stack pointer, program counter, and functions, which were correctly at 0x08040000. This is quite perplexing.
I’m using the GCC_ARM toolchain for building.
In conclusion, I’m struggling to flip my main code correctly, and it’s not a trivial task. I’d appreciate it if someone could share their experience or offer some advice. My team and I have been stuck for a week, and we’ve run out of ideas.

There’s an article on arduino, https://docs.arduino.cc/tutorials/portenta-h7/updating-the-bootloader - it points also to flash text edit in the linker (have you done it?).

Dont you set the size of your application (is entire ROM available) ?