Skip to main content

IAR Embedded Workbench for RL78 5.20

Data and function placement in sections

In this section:

The following methods 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.

  • The ‑‑code_section option can be used for placing functions, which are parts of the whole compilation unit, in named sections.

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. If no memory attribute is specified, the variable will, like any other variable, be treated as if it is located in the default memory. 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.

As usual, you can use memory attributes to select a memory for the variable. Note that you must as always ensure that the section is placed in the appropriate memory area when linking.

__near __no_init int alpha @ "MY_NEAR_NOINIT";/* Placed in near*/
Examples of placing functions in named sections
void f(void) @ "MY_FUNCTIONS";

void g(void) @ "MY_FUNCTIONS"
{
}

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

Specify a memory attribute to direct the function to a specific memory, and then modify the section placement in the linker configuration file accordingly:

__near_func void f(void) @ "MY_NEAR_FUNC_FUNCTIONS";