Skip to main content

IAR Embedded Workbench for RISC-V 3.40

Data and function placement in sections

In this section:

The following method can be used for placing data or functions in named sections other than default:

  • The @operator, alternatively the #pragma location directive, can be used for placing individual variables or individual functions in named sections. The named section can either be a predefined section, or a user-defined section.

C++ static member variables can be placed in named sections just like any other static variable.

If you use your own sections, in addition to the predefined sections, the sections must also be defined in the linker configuration file.

Note

Take care when explicitly placing a variable or function in a predefined section other than the one used by default. This is useful in some situations, but incorrect placement can result in anything from error messages during compilation and linking to a malfunctioning application. Carefully consider the circumstances—there might be strict requirements on the declaration and use of the function or variable.

The location of the sections can be controlled from the linker configuration file.

For more information about sections, see Section reference.

Examples of placing variables in named sections

In the following examples, a data object is placed in a user-defined section. Note that you must as always ensure that the section is placed in the appropriate memory area when linking.

__no_init int alpha @ "MY_NOINIT";      /* OK */

#pragma location="MY_CONSTANTS"
const int beta = 42;                    /* OK */

const int gamma @ "MY_CONSTANTS" = 17;  /* OK */
int theta @ "MY_ZEROS";              /* OK */
int phi @ "MY_INITED" = 4711;       /* OK */ 

The linker will normally arrange for the correct type of initialization for each variable. If you want to control or suppress automatic initialization, you can use the initialize and do not initialize directives in the linker configuration file.

Examples of placing functions in named sections
void f(void) @ "MY_FUNCTIONS";

void g(void) @ "MY_FUNCTIONS"
{
}

#pragma location="MY_FUNCTIONS"
void h(void);