In embedded programming, it is sometimes necessary to place certain variables in specific memory regions. For example, there may be different kinds of RAM memory available, some fast and some slow, and selected variables need to be placed into the fast RAM. The Green Hills C compiler and linker allow you to achieve this and similar goals by grouping variables into program sections and positioning them as desired in memory.
To group program variables and position them in memory, it is necessary to assign a named section to each desired memory region using a linker section map. In addition, information is provided in the program source files to show which variables go into which sections. This is done using the section pragma, with the following syntax:
#pragma ghs section[secttype="sectname"[,secttype="sectname"]...]
The square brackets enclose optional material, and the ... indicates that the preceding square-bracketed material repeats zero or more times.
sectname is the user-defined section name, eight letters or less in length, and by convention starts with a period ("."). The word default may be used in place of any sectname. While normal section names are specified in quotes, the word default is not.
secttype tells which kind of data item is affected by the pragma, and may be one of the following:
bss Zero-initialized variables.
rodata Constant variables and/or strings.
Each occurrence of the section pragma specifies a mapping of data types to section names. Each section pragma leaves mappings from earlier pragmas in place except for those which it explicitly overrides. Specifying default in place of a quoted section name removes any mapping for that particular secttype. The statement removes all mappings and restores the section-assignment rules to their initial state:
#pragma ghs section
Mappings affect variables at the point where they are defined. Each variable's placement to its section is determined by the mapping in the source file. This places different variables to different sections by interspersing section pragmas among the variable declarations. For each variable, the compiler determines which section it would normally fall into and then checks whether variables of that type have a mapping. If so, the section specified in the mapping is used in place of the default.
For example, consider the following line of C code:
int foo=3;
In this example the variable foo is normally placed into the .data section because it is initialized to an explicit value. However, if this line of code were preceded by the following, then the variable foo is placed in the section .mydata instead:
#pragma ghs section data=".mydata"
Here is how three different variables might be assigned to three different sections:
#pragma ghs section data=".data1"
int x1 = 0;
/* Assign x1 to section .data1 */
#pragma ghs section data=".data2"
int x2 = 0;
/* Assign x2 to section .data2 */
#pragma ghs section data=".data3"
int x3 = 0;
/* Assign x3 to section .data3 */
#pragma ghs section data=default
/* Now we are back to default rules */
This allocates variable x1 to section .data1, x2 to .data2, and x3 to .data3.