top of page

Why 2 Linker script in STM32CubeIDE

When working with STM32CubeIDE to build embedded applications, you may notice that two linker scripts are generated by default: `STM32F446RETX_FLASH.ld` and `STM32F446RETX_RAM.ld`. However, during the build process, only one linker script is used. This raises a common question: why are two linker scripts generated, and what purpose do they serve? Let's explore the reasons behind this and how you can switch between them depending on your development needs.


Why Two Linker Scripts Are Generated

STM32CubeIDE generates two linker scripts to give you the flexibility to configure your program to run either from Flash or RAM. Both have distinct use cases:


1. Flash-based Execution (`STM32F446RETX_FLASH.ld`):


- Purpose: This is the default script for most embedded projects. It configures the program to run from Flash memory, which is non-volatile, meaning the program remains even after the device is powered off.

- Why Use Flash?: Flash is ideal for storing and running production code since it retains data after power cycles, making it perfect for applications that need to persist across reboots.


2. RAM-based Execution (`STM32F446RETX_RAM.ld`):


- Purpose: This script is used for applications running entirely from RAM. While RAM is volatile (data is lost when power is turned off), it offers faster access times than Flash. This is mainly useful for debugging or testing scenarios.

- Why Use RAM? Running code from RAM is beneficial when debugging since it avoids the need to frequently erase and program Flash memory. RAM execution speeds up the debugging process, making it easier to set breakpoints and step through code.


How the Linker Scripts Work


1. Flash Linker Script (`STM32F446RETX_FLASH.ld`):

This script places your program’s code (the `.text` section) in Flash and the initialized variables (`.data` section) in RAM. It’s designed to utilize both Flash for storing code and RAM for dynamic data handling.

Example memory layout:

   MEMORY
   {
     FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 512K
     RAM   (xrw) : ORIGIN = 0x20000000, LENGTH = 128K
   }

- Flash holds the program code (`.text`).

- RAM holds initialized variables (`.data`) and uninitialized variables (`.bss`).


NOTE: The .data section is special, The program will execute from RAM (VMA is in RAM), but it is stored initially in Flash (LMA is in Flash). check this blog


2. RAM Linker Script (`STM32F446RETX_RAM.ld`):

This script places both the code and the data sections into RAM, making the entire program run from volatile memory. This configuration is typically used for debugging, where speed and ease of code modifications are prioritized over persistence.

Example memory layout:

   MEMORY
   {
     RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 128K
   }

- RAM holds both the program code (`.text`) and data sections (`.data`, `.bss`), Flash is completely ignored here.


When and Why to Use Each Linker Script


1. Flash-based builds (default):

- Used for production environments.

- Ensures the program persists after a power cycle.

- Ideal for final deployment where the device operates independently and needs to store the code permanently.


2. RAM-based builds (optional):

- Mainly used for debugging or testing.

- Faster execution since accessing RAM is quicker than Flash.

- Ideal for development stages where you need to frequently modify and test the program without worrying about writing to Flash memory every time.


Does STM32CubeIDE Use Both Linker Scripts?

No, STM32CubeIDE only uses one linker script per build. By default, the Flash linker script (`STM32F446RETX_FLASH.ld`) is selected because most embedded applications are designed to run from Flash. However, if you want to run your application from RAM for debugging or specific testing purposes, you can manually configure the project to use the RAM linker script ('STM32F446RETX_RAM.ld`).


Switching to the RAM Linker Script

To use the RAM linker script in your STM32CubeIDE project, follow these steps:


  1. Open the project properties: Right-click on your project in the Project Explorer and select Properties.

  2. Navigate to C/C++ Build Settings: Go to C/C++ Build > Settings.

  3. Select the RAM linker script file: Under Tool Settings, find the option for MCU GCC Linker > General and change the Linker Script file from `STM32F446RETX_FLASH.ld` to `STM32F446RETX_RAM.ld`.

  4. Rebuild your project: After switching the linker script, rebuild your project, and it will now use the RAM-based memory layout for execution.


Conclusion

STM32CubeIDE generates two linker scripts—one for Flash and one for RAM—to give developers flexibility in how their application is built and executed. While most builds use the Flash linker script by default, the RAM linker script is invaluable for debugging and testing purposes. By understanding when and why to use each linker script, you can optimize your development process, whether you're preparing a final product for production or actively debugging code during development.

Comments


Commenting has been turned off.
bottom of page