2

Can anyone please let me know what are the possible ways in which we can store a function(in C) at a fixed memory location.

I am using IAR IDE for ARM cortex m3 core.

Vagish
  • 2,520
  • 19
  • 32
  • 4
    That depends on the toolchain but it seems a very ... odd ... request. Why do you think you need this, just out of interest? – paxdiablo Sep 13 '13 at 04:31
  • 5
    A common scenario is a bootloader starting the main program at a predefined location in the memory. You have to fix the entry point of the main program at that precise address. – Alexandre Vinçon Sep 13 '13 at 07:52
  • 1
    @paxdiablo _External_ function addresses used in a bootloader (or any ROM) need fixed locations for other programs to use consistently. A fixed index to a table of functions is an alternative. – chux - Reinstate Monica Sep 13 '13 at 14:56
  • In reality, for any code which is not *position independent* you basically have to do this anyway - the only differences are that this may already be configured if you have a device-specific toolchain installation, and that you usually only need to specify that your code starts (and, as a check, ends) within a suitable region of memory, not where specific functions fall within. For many toolchains, specifying the location of a specific function is done simply by giving it its own region. – Chris Stratton Sep 13 '13 at 15:00

1 Answers1

3

How functions and data can be placed in memory is described in section "Controlling data and function placement in memory" of the IAR manual "IAR C/C++ Development Guide".

The only way I see to place a specific function at a specific address is to define a section in memory where to place this and only this function. Example:

void MyFunction( void ) @ "MyFunctionsSection"
{
...
}

In the linker file you have to define the section "MyFunctionsSection". There you can define where this section should be placed. Your function will then be placed there.

The disadvantage of this method is that you have to reserve memory for this function. So you must know the size of the function and have to adapt this memory section if the size of the function increases and gets bigger than the reserved memory. Another drawback is that you can only place one function in this section to be sure that your function is placed at the wished address.

But besides this: The idea to place a function at a specific address is generally odd. Better would be to define a pointer to this function and place the pointer at a fix address as it is done in interrupt vector tables. If you have more function pointers the list can easily be expanded.

Habi
  • 3,262
  • 25
  • 23
  • To help you with settings, open .icf file with notepad and define regions like that -> after line -> define memory mem with size = put define region MY_FUNC = mem:[from 0x7FD00000 size 0x2000]; and then after -> place at address mem.. put place in MY_FUNC { readonly MyFunctionsSection}; this works for me, hope it helps. ( use your own address and size, I just copied from my files ) – Luka Pivk Sep 13 '13 at 07:39
  • @Habi: I don't think you need to know the size of the function. I have used `place at address` directive in linker config file, and linker has sorted this out. It was for data though, but I don't think functions are any different. – user694733 Sep 13 '13 at 07:55