Contiguous variables

Technical Note 75313

Targets:
ARM

Component:
Linker

Updated:
11/2/2018 1:37 PM

Introduction

This technical note is focused on the need to force fixed order of placement in RAM.

Example projects are found in ContiguousVariables.zip


Issue 1

Using pragma location you can place variables in a section, but the .map file shows that the variables are placed in arbitrary order.

Solution 1

To enforce order between variables, place variables in separate sections and then declare a "fixed order block" to define the order between the variables, i.e. the order of the sections.

Note that this "fixed order block" will also be placed at an arbitrary location, but with linker directives the placement of the block can be controlled.

Example of C source showing variables placed in separate sections named "1st", "2nd" and "3rd":

int ab[]  @ "1st" = {1, 2, 3, 4, 5, 6, 7, 8};
int a[] @ "2nd" = {1, 2, 3, 4, 5 };
int abc[] @ "3rd" = {1, 2, 3, 4, 5, 6, 7};

Example of linker configuration directives defining the order between the variables by selecting section names:

define block ARRAYS with fixed order
{
section 1st,
section 2nd,
section 3rd
};

place in RAM_region { first block ARRAYS, readwrite, ... };


Issue 2

Instead of section "2nd", in the above example, some reserved RAM space is needed between the variables in section "1st" and "3rd".

Solution 2

Define a block which is placed between the variables and then use the linker directive "keep" to force the block to be included.

Modify the .icf file in the above example and use the following linker directives:

define block RESERVED_SPACE with alignment = 8, size = 1024 { };

define block ARRAYS with fixed order
{
section 1st,
block RESERVED_SPACE,
section 3rd
};

keep { block RESERVED_SPACE }; 


Issue 3

In solution 1 the variables are placed in separate sections, but you prefer a solution where you do not need to modify your source code to place each variable in a section.

Solution 3

To enforce order between variables, declare a "fixed order block" to define the order between the symbol names.

Example of C source showing variables:

int ab[]  = {1, 2, 3, 4, 5, 6, 7, 8};
int a[] = {1, 2, 3, 4, 5 };
int abc[] = {1, 2, 3, 4, 5, 6, 7};

Example of linker configuration directives defining the order between the variables by selecting symbol names:

define block ARRAYS with fixed order
{
symbol ab,
symbol a,
symbol abc
};

place in RAM_region { first block ARRAYS, readwrite, ... };


Issue 4

In the above solutions variables are placed using a "fixed order block", but you prefer a solution where you do not need to use linker configuration directives to do the placement.

Solution 4

To enforce order between variables, use IAR C langauge extensions to declare an "anonymous struct". See chapters "Enabling language extensions" and "Anonymous structs and unions" in Help > C/C++ Development Guide.

Example of C source showing variables placed in fixed order at address 0x20001000:

struct
{
int a;
char b;
int c;
} @ 0x20001000; 

 

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

Related Tech Notes

© IAR Systems 1995-2018 - All rights reserved.

We use cookies on this website to provide you with a better experience. You need to accept cookies to continue using this site. Cookies