How do I position code or variables at a specific location with the XC8 compiler?

With the MPLAB® XC8 compiler, making an object absolute is the easiest way of specifying a link location. However, you are limited to the object being at a known address. Using psects is more complex, but does allow you to use the features of the linker to allocate memory for the object. You might, for example, have the linker place the object anywhere in an address range, or anywhere provided it is above a specific address or after some other object.

In either case, consider whether you need to do this as it immediately creates non-portable code. If you must place objects at a known location, try to place the object at a sensible location. If it is located in the middle of a bank or page, this will severely hamper the ability of the linker to position other objects and "can't find space" errors may result.

These are the various ways the functions and variables can be stored at an absolute address:

  1. Using __section specifier with Common Compiler Infrastructure (CCI) syntax enabled
  2. Using __at specifier with CCI syntax enabled
  3. Absolute variables in Data Space using the @ qualifier:
  4. Absolute variables in Program Space using the @ qualifier:
  5. Absolute functions using the @ qualifier:

1. Using __section specifier with CCI syntax enabled

To use the __section specifier, make sure that you have enabled the CCI syntax under MPLAB X IDE:
Project Properties > MPLAB XC8 compiler > Preprocessing and messages, check the Use CCI Syntax box.

Variable commonFlag will be placed in section tomSec.

Function mul will be placed in section mytext.

With XC8, the __section() specifier cannot be used with the interrupt functions.

2. Using __at specifier with CCI syntax enabled

To use the __at specifier, make sure that you have enabled the CCI syntax under MPLAB X IDE:
Project Properties > MPLAB XC8 compiler > Preprocessing and messages, check the Use CCI Syntax box

The following shows usage of __at specifier.

3. Absolute variables in Data Space using the @ qualifier

The compiler will reserve storage for this object (if the address falls into general-purpose RAM) and will equate the variable’s identifier to that address. The auto variables cannot be made absolute as they are located in a compiled stack.

Avoid absolute objects if at all possible. If absolute objects must be defined, try to place them at either end of a memory bank or page so that the remaining free memory is not fragmented into smaller chunks.

Absolute variables in RAM cannot be initialized when they are defined. Define the absolute variables, then assign them a value at a suitable point in your main-line code.

Trying to initialize the absolute variables in RAM will throw a warning as:
initialization of absolute variable "x" in RAM is not supported

4. Absolute variables in Program Space using the @ qualifier

The @ symbol will place a marker at 0x200 and the array table at address 0x100 in the program space. Both initialized and uninitialized const objects can be made absolute.

5. Absolute functions using the @ qualifier

The function readInput is placed at 0x200 in Program Space.

Always check either the assembly list or map file to ensure that the object has been located as you expect.

© 2020 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.