4.7.4 Changing the Default Function Allocation

You can change the default memory allocation of functions by either:

  • Making functions absolute
  • Placing functions in their own section and linking that section

The easiest method to explicitly place individual functions at a known address is to make them absolute by using the __at() construct in a similar fashion to that used with absolute variables. The CCI must be enabled for this syntax to be accepted and <xc.h> must be included

The compiler will issue a warning if code associated with an absolute function overlaps with code from other absolute functions. The compiler will not locate code associated with ordinary functions over the top of absolute functions.

The following example of an absolute function will place the function at address 400h:

int __at(0x400) mach_status(int mode)
{
  /* function body */
}

If this construct is used with interrupt functions, it will only affect the position of the code associated with the interrupt function body. The interrupt context switch code associated with the interrupt vector will not be relocated.

Functions can be allocated to a user-defined psect using the __section() specifier (see Section 3.15.2 “Changing and Linking the Allocated Section”) so that this new section can then be linked at the required location. This method is the most flexible and allows functions to be placed at a fixed address, after other section, or anywhere in an address range. As with absolute functions, when used with interrupt functions, it will only affect the position of the interrupt function body.

Regardless of how a function is located, take care choosing its address. If possible, avoid fragmenting memory and increasing the possibility of linker errors.