1

There is a HAL library of functions for working with flash memory. I want all the functions of this library to work from RAM. How can I do this correctly, without writing _attribute_((section(".RamFunc"))) before each function? And without making any changes to the standard library at all.

It seems to me that the standard library should support such a setting. But I don't know any other techniques, except adding an _attribute_((section(".RamFunc"))) before the functions. I don't like making changes to the standard library file either.

Should the linker maybe be told to place all the functions from this file in RAM?

So far I've only added _attribute_((section(".RamFunc"))).

1 Answer 1

1

HAL is not a standard library. This specific name is reserved for the C language standard library. So do not use this term. Just say the "HAL library".

The HAL library source files have a very specific naming convention. It starts from the MCU family followed by the peripheral name, for example: stm32f4xx_hal_adc_ex.c

You need to place the .text sections from the .o files matching this pattern in your section, which will be placed in RAM and stored in flash.

Then you will need to write the code to copy this section from flash memory to RAM.

And you are done :)

But remember! Calls from your other code in the flash memory to the function placed in RAM will be expensive, as the distance is very long and will be done via a veneer call.

What is a veneer call? A veneer (aka trampoline/stub) is a tiny chunk of code the linker auto‑generates when a direct BL/B instruction can’t reach the real target function. On Cortex‑M (Thumb‑2 only), a BL encodes about ±16 MB of range. If your caller in flash memory (e.g., 0x0800_0000) needs to call a callee in RAM (e.g., 0x2003_0000), that distance is approximately 128 MB—too far. The linker inserts a veneer that is within 16 MB of the caller, and that veneer then jumps the rest of the way using an absolute load + BX.

Sign up to request clarification or add additional context in comments.

1 Comment

Could you show here how this should look in linkerscript?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.