2025-09-08 7:38 AM
I'm currently tinkering with the BLE_Ota example for the P-NUCLEO-WB55.Nucleo and created a debug build spec for it. The default linker script is
/* Specify the memory areas */
MEMORY
{
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 28K
RAM1 (xrw) : ORIGIN = 0x20000008, LENGTH = 0x2FFF8
RAM_SHARED (xrw) : ORIGIN = 0x20030000, LENGTH = 10K
}
but the image built with the debug build spec is larger, so the length of the flash area to use must be adjusted:
/* Specify the memory areas */
MEMORY
{
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 32K
RAM1 (xrw) : ORIGIN = 0x20000008, LENGTH = 0x2FFF8
RAM_SHARED (xrw) : ORIGIN = 0x20030000, LENGTH = 10K
}
that made me wonder, is it possible to define constants in the build specs such that conditional statements (e.g using the DEFINED statement) inside the .ld file can be used? something like
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = DEFINED(DEBUG) ? 32K : 28K
Solved! Go to Solution.
2025-09-09 2:17 AM
this pointed me in the right direction.
adding
-Wl,--defsym=DEBUG=1
to the build spec under MCU GCC Linker -> MISC -> Other flags along with a check in the linker script
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = DEFINED(DEBUG) ? 32K : 28K
produces the expected result (-Wl is necessary to forward the --defsym statement to the linker)
2025-09-08 7:49 AM
Hello,
Try this:
#if defined (DEBUG)
FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 32K dede
#else
FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 28K
#endif
2025-09-08 8:27 AM
The linker script is not interpreted by the C preprocessor so this can't work.
You can run it manually if you want, as a pre-build step. However, it isn't part of a compilation unit so any #defines within files may not be active. Project-level defines will work.
c - Can I use Preprocessor Directives in .ld file - Stack Overflow
2025-09-08 8:40 AM
this causes a warning by the linker:
STM32WB55RGVX_FLASH.ld:45: warning: redeclaration of memory region `FLASH'
the linker seems to ignore lines starting with `#`, only the last FLASH (rx) statement in the file is actually used.
2025-09-08 8:43 AM
I was thinking about passing a parameter to the linker (via the build spec settings in eclypse) to define a symbol that is then used in a DEFINED() statement. apparently flags can be passed to unset symbols but I have not found any to set symbols.
can you elaborate on your proposed solution? have you tried it yourself?
2025-09-08 9:15 AM
May be another idea:
Try to add two linker files one for debug and another for release to your project and for each config : debug or release exclude from build the unused linker file.
2025-09-08 9:30 AM
@mƎALLEm wrote:
May be another idea:
Try to add two linker files one for debug and another for release to your project and for each config : debug or release exclude from build the unused linker file.
Ok it seems that exclude from build could not be used for linker files!
2025-09-08 6:49 PM - edited 2025-09-09 12:02 AM
Try the --defsym symbol=expression command line option.
Also, you can put definitions of symbols (symbol=value ....) in a file and pass it to linker via -T option, before the main link script. The link command can have multiple -T options.
2025-09-08 7:13 PM
Yes, I think they are comments and the Linker script doesn't support a pre-processor pass.
2025-09-09 2:17 AM
this pointed me in the right direction.
adding
-Wl,--defsym=DEBUG=1
to the build spec under MCU GCC Linker -> MISC -> Other flags along with a check in the linker script
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = DEFINED(DEBUG) ? 32K : 28K
produces the expected result (-Wl is necessary to forward the --defsym statement to the linker)