Absolute located variable

Technical Note 43262

Architectures:

All

Component:

compiler

Updated:

11/6/2015 12:34 PM

Introduction

This Technical Note discusses how absolute located variable(s) has to be treated.

Problem

In some situations you can get a linker error stating that an absolute located variable has not been defined. This can be related to the non-intuitive way that such variables should be defined.

Solution

Define and locate the variable in a header file that is included by all source files that are refering to the variable.

Background/more info

Variables that are located at an absolute address have a behavior that is slightly different compared to ordinary C/C++ non-auto variables.

Variables that are absolute located are located in an absolute segment, such as ABSOLUTE or xxxx_AN. An absolute segment is not present in the linker file since the variable has already been located at compile time.

Other (ordinary) non-auto variables with file scope are placed in a relocatable segment that has to be described in the link file as usual. This is because the address resolution is made at link time.

The non-obvious behavior:

Each absolute located variable definition is really local for the current compilation unit (file), you can (and should) have a definition for each compilation unit that access the variable. Multiple such definitions will not clash at link time (provided that you have defined them in a consistent way, that is, identical in all modules that defines them). Should you not use the absolute located variable, it is discarded from that compilation unit. Should you use it, it will be present in the output file and this is for two reasons. First, it is used, so it must claim its absolute location at the link stage, second, it allows the linker to check that the definitions are consistent in all modules.

In other words, you should place the definition (not the declaration as you normally do) in a header file if you use it from multiple files:

setup.h:

__no_init struct setup configuration @ 0x100;

Then just include setup.h header file from all source files that need to access it.

One way to remember this is to think of it as having a #define to describe a memory location, with the addition of having the linker being able to check for consistency.

If you instead place the object into a named segment:

__no_init struct setup located_configuration @ "SETUP";

Then the segment SETUP must be defined and located in the link file. Variables located in a segment behaves as ordinary C variables, you put a declaration in the header file:

setup.h:

extern __no_init struct setup located_configuration;

And then you need to put the defintion into one (and only one) source file:

__no_init struct setup located_configuration @ "SETUP";

If you do not like the @-syntax, there is also the equivalent

#pragma location=

that can be used instead.

All product names are trademarks or registered trademarks of their respective owners.

We do no longer support Internet Explorer. To get the best experience of iar.com, we recommend upgrading to a modern browser such as Chrome or Edge.