cancel
Showing results for 
Search instead for 
Did you mean: 

Hello!!! How can I fix an address of a function ? I'm using the STM32CUBE_IDE with the STM32F103 MCU.

INaka.1
Associate III
 
1 ACCEPTED SOLUTION

Accepted Solutions
SBEN .2
Senior II

Hello @INaka.1​ 

To elaborate more on @TDK 's response, it is possible to have a function at a predetermined location (alignment withstanding) by adding a new section in the linker file and place the function there: Linker scripts (haw-hamburg.de), a more beginner friendly guide here: Putting Code of Files into Special Section with the GNU Linker.

Some toolchains have special ways to at least suggest which address a function or a variable should be placed (like __attribute__(at()) for ARMCC/ARMLINK, the @ operator for IAR, etc...). But those should avoided even if you have access to such toolchains for portability's sake.

Otherwise, I'd have to agree with @Tesla DeLorean. An exotic solution for an issue is rarely the good solution. His proposal, which is called a branch table, is more flexible especially in case you have multiple functions which addresses must be fixed it is the way to go. In fact that how interrupt handlers have their fixed (but movable to a certain degree with SCB->VTOR for Cortex M at least) addresses. Beware that if you're targeting MISRA compliance you might run into some issue.

Code placement, and linking in general, is an interesting subject. You can place a function at a given address in more than one suboptimal to disastrous ways. Down-below are some of those *techniques* (it goes without saying that they are not recommended at all:(

  1. The fragmented way: Build your function in its own binary with the linker file starting at the intended address, customize the rest of the project to leave some space for the function, concatenate the binaries and load them. This technique has other legitimate uses.
  2. The interrupt way: If you intend to have a fixed address regardless of the location, you can select an unused interrupt that you won't ever use, and have your function as its handler. You have the bonus of invoking the said function in creative ways if you choose to activate the interrupt.
  3. The greedy way: At the end of your function add a sufficient number of NOPs to fill the entire usable .text section. It's true that you won't have a place for the rest of your code, but you'll be sure that the function will always be at the same place. You can control the functions location by encasing it in NOPs both ways with the advantage of winning a free NOP slide.

Best regards,

@SBEN.2

View solution in original post

6 REPLIES 6
TDK
Guru

There's no easy way to do this. You could create a section of flash in the linker script and put that function in there. But not if it's in the middle of the flash you're using.

If you feel a post has answered your question, please click "Accept as Solution".

Perhaps ponder the problem to be solved.

One frequently creates a table of address so the functions can move, whilst the table can be placed at a more fixed/stable address.

One could perhaps use AT directives or attributes, but this does give the linker headaches.

Consider using pointers, or more flexible methods of passing function addresses.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
INaka.1
Associate III

I'll try this solution. Thank you very much

INaka.1
Associate III

Thank you for your answer

SBEN .2
Senior II

Hello @INaka.1​ 

To elaborate more on @TDK 's response, it is possible to have a function at a predetermined location (alignment withstanding) by adding a new section in the linker file and place the function there: Linker scripts (haw-hamburg.de), a more beginner friendly guide here: Putting Code of Files into Special Section with the GNU Linker.

Some toolchains have special ways to at least suggest which address a function or a variable should be placed (like __attribute__(at()) for ARMCC/ARMLINK, the @ operator for IAR, etc...). But those should avoided even if you have access to such toolchains for portability's sake.

Otherwise, I'd have to agree with @Tesla DeLorean. An exotic solution for an issue is rarely the good solution. His proposal, which is called a branch table, is more flexible especially in case you have multiple functions which addresses must be fixed it is the way to go. In fact that how interrupt handlers have their fixed (but movable to a certain degree with SCB->VTOR for Cortex M at least) addresses. Beware that if you're targeting MISRA compliance you might run into some issue.

Code placement, and linking in general, is an interesting subject. You can place a function at a given address in more than one suboptimal to disastrous ways. Down-below are some of those *techniques* (it goes without saying that they are not recommended at all:(

  1. The fragmented way: Build your function in its own binary with the linker file starting at the intended address, customize the rest of the project to leave some space for the function, concatenate the binaries and load them. This technique has other legitimate uses.
  2. The interrupt way: If you intend to have a fixed address regardless of the location, you can select an unused interrupt that you won't ever use, and have your function as its handler. You have the bonus of invoking the said function in creative ways if you choose to activate the interrupt.
  3. The greedy way: At the end of your function add a sufficient number of NOPs to fill the entire usable .text section. It's true that you won't have a place for the rest of your code, but you'll be sure that the function will always be at the same place. You can control the functions location by encasing it in NOPs both ways with the advantage of winning a free NOP slide.

Best regards,

@SBEN.2

INaka.1
Associate III

Thank you!