Using a linker script in GCC to locate functions at specific memory regions
03/68/2016 | 12:54 AM
How do I place a function in a specific memory region using the GCC linker script?
Placing a function in a specific memory region such as an address range in flash or RAM requires modifications to the linker script and source files to specify a memory region, memory section, and section attribute.
For example, to locate a function, Delay(), in STK3800_blink (EFM32WG), perform the following steps:
1. Create a new MCU project using the EFM32WG STK example, STK3800_blink.
2. Create a custom linker script using the auto-generated debug or release .ld file as a template.
3. Edit the customer linker script as follows:
a. Create a new memory region called MY_REGION, which starts in flash at the 128 KB boundary (0x20000), and is 128 KB in length. The region is specified as readable and executable since flash cannot be written directly. We also reduce the length of the FLASH region to account for this new region.
b. Create a new section called .mysection, which places all objects with a section attribute containing the string .mysection in MY_REGION. We can add this section just after the __etext = .; line so we don't affect the FLASH overflow boundary check. To add overflow checking to make sure that the MY_REGION memory doesn't overflow, we can create pointers to the start and end of the .mysection section by adding __mysection_start__ and __mysection_end__ pointers. The dot (.) symbol indicates the current address during the linking phase.
c. To make sure we don't overflow the MY_MEMORY region, we can add an assert at the end of the linker script to calculate the used length of the MY_MEMORY region and make sure it's less than the total length of the region: