4.2.4.1
Allocation of data and program to section
CC-RH can allocate modules and data to arbitrary sections at the C-language level. This makes it possible to specify placement using the relocation attribute characteristics of each section.
(1) | #pragma section directive |
Describe the #pragma section directive in the following format.
#pragma section attribute-strings "section-name"
Variable/function declaration/definition
|
#pragma section attribute-strings
Variable/function declaration/definition
|
#pragma section [character string]
Variable/function declaration/definition
|
Section relocation attributes are specified using attribute strings, and section names are specified in the form "section-name". All static variables included after this pragma will be placed in the specified section in accordance with the section relocation attribute.
If "attribute string" in the format "#pragma section attribute-string" is identical to "character string" in the format "#pragma section [character-string]", then it will be treated as the format "#pragma section attribute-string" format, without outputting a message. However, if the -check=shc option is specified, then a message will be output if treated as the "#pragma section attribute-string" format.
The identity test of attribute-string and character-string is case sensitive. To give an example, the all-caps string R0_DISP32 will not be treated as an attribute string.
#pragma section r0_disp32 /*Here, "data" matches an attribute string, so it is*/
/*seen as the format "#pragma section*/
/*attribute-string"*/
#pragma section R0_DISP32 /*This does not match the case of an attribute*/
/*string, and so it is treated as the format*/
/*"#pragma section [character-string]"*/
|
If the section name starts with a number (0-9), an underscore ("_") is prepended to the section name.
Example | #pragma section 123 -> Section name : _123.bss, _123.const, _123.text, ... |
The following characters can be used in the character string.
If #pragma section is specified for either a declaration or a definition, the one specified with #pragma section is valid.
If different #pragma section directives are specified for declarations or definitions, the #pragma section that appears first is valid.
(a) | Specifying "#pragma section attribute-string "section-name"" |
Renames the default section, and places variables declared after the #pragma in the new section name.
- | This pragma directive renames the default section to the name specified by the user. |
- | This pragma directive is valid for external variables, string literals, function-internal static variables, and functions. |
- | This pragma directive invalidates specifications of the same attribute coded prior to this point, and enables the new section specification. If no new section is specified, then it is valid to the end of the file. |
- | If default is specified as the relocation attribute, then subsequent assignments revert to the default with no #pragma section declared. "default" disables all #pragma section statements. |
- | If there is no variable initialization, then it is placed in the bss section corresponding to the specified relocation attribute |
- | By default, string literals are assigned to .const. The placement for #pragma section declarations is the same as for other const objects. |
- | A section name consists of the user-specified string, followed automatically by a string representing the relocation attribute. Thus depending on whether initialization is enabled, if the relocation attribute is switched automatically, it is possible that a section with the same name will be produced.
Example: #pragma section r0_disp32 "xxx" -> Section name : xxx.data |
Table 4.16 | Section Relocation Attribute |
|
|
|
|
Definition (type data)
|
r0_disp16
r0_disp23
r0_disp32
ep_disp4
ep_disp5
ep_disp7
ep_disp8
ep_disp16
ep_disp23
gp_disp16
gp_disp23
|
.zdata
.zdata23
.data
.tdata4
.tdata5
.tdata7
.tdata8
.edata
.edata23
.sdata
.sdata23
|
xxx.zdata
xxx.zdata23
xxx.data
xxx.tdata4
xxx.tdata5
xxx.tdata7
xxx.tdata8
xxx.edata
xxx.edata23
xxx.sdata
xxx.sdata23
|
Declaration (type bss)
|
.zbss
.zbss23
.bss
.tbss4
.tbss5
.tbss7
.tbss8
.ebss
.ebss23
.sbss
.sbss23
|
xxx.zbss
xxx.zbss23
xxx.bss
xxx.tbss4
xxx.tbss5
xxx.tbss7
xxx.tbss8
xxx.ebss
xxx.ebss23
xxx.sbss
xxx.sbss23
|
Type const
|
const
zconst
zconst23
|
.const
.zconst
.zconst23
|
xxx.const
xxx.zconst
xxx.zconst23
|
Type text
|
text
|
.text
|
xxx.text
|
Clearing of specification
|
default
|
Clears all specifications, and restores the default section name.
|
Note | This is the actual section name when the section name of "xxx" is given. |
#pragma section r0_disp32 "mydata"
int i = 0; /*Allocated to "mydata.data"*/
int j; /*Allocated to "mydata.bss"*/
#pragma section gp_disp16 "mydata2"
int i2 = 1; /*Allocated to "mydata2.sdata"*/
#pragma section const "myconst"
const int c2 = 0; /*Allocated to "myconst.const"*/
int j2; /*Allocated to ".bss"*/
|
#pragma section ep_disp4 "XXX"
extern int i;
void func()
{
i = 5;
}
#pragma section r0_disp32 "OOO"
int i = 5; /*Allocated to "XXX.tdata4"*/
/*When #pragma section is specified for the first*/
/*declaration, it takes priority*/
|
#pragma section ep_disp4
int i;
#pragma section ep_disp23
int i; /*Allocated to "tbss4"*/
/*When #pragma section is specified for the first*/
/*declaration, it takes priority*/
|
<def.c>
#pragma section ep_disp4
extern int i;
#pragma section ep_disp4 "XXX"
int i = 5; /*Allocated to ".tdata4"*/
/*When #pragma section is specified for the*/
/*first declaration, it takes priority*/
<use.c>
#pragma section ep_disp4
extern int i;
void func()
{
i = 5; /*No problems with access*/
}
|
#pragma section ep_disp4 "XXX"
extern int i;
#pragma section ep_disp4 "OOO"
int i = 5; /*Allocated to ".tdata4"*/
/*When #pragma section is specified for the*/
/*first declaration, it takes priority*/
|
#pragma section r0_disp16 "myzdata"
#pragma section const "myconst" /*Specification of "myzdata" ends*/
/*at this line*/
int i = 0; /*No effect, because not .data const*/
const int ci = 0; /*myconst.const*/
#pragma section r0_disp16 "myzadata" /*Re-specify*/
int j = 0; /*Becomes myzdata.zdata*/
const int cj = 0; /*The myconst specification in the*/
/*pragma immediately preceding the*/
/*.const is erased, and it is*/
/*placed in ".const".*/
|
extern int i;
#pragma section ep_disp4
int i; /*Allocated to ".tbss4"*/
/*Although this differs from the declaration side, even if*/
/*this is accessed without #pragma section ep_disp23 in*/
/*other translation units, access is correctly done in*/
/*long-distance addressing mode (however, it is not*/
/*efficient)*/
|
#pragma section ep_disp23
#pragma section default
extern int i;
#pragma section ep_disp4
int i; /*Allocated to ".tbss4"*/
/*#pragma section default, which restores the section name*/
/*to the default name, is not regarded as a section*/
/*specification*/
/*The extern declaration is handled as a declaration*/
/*without section specification*/
|
(b) | Specifying "#pragma section attribute-strings" |
Variables declared after the #pragma will be placed in the default section.
- | Specifies placement of data in the default section name. |
- | The placement rules are the same as for "#pragma section attribute-string "section-name"", except for the fact that they are placed in the default section name. |
Table 4.17 | Section Relocation Attribute |
|
|
|
Definition (type data)
|
r0_disp16
r0_disp23
r0_disp32
ep_auto
ep_disp4
ep_disp5
ep_disp7
ep_disp8
ep_disp16
ep_disp23
gp_disp16
gp_disp23
|
.zdata
.zdata23
.data
Automatically selected from among .tdata4, .tdata5, .tdata7, and .tdata8
.tdata4
.tdata5
.tdata7
.tdata8
.edata
.edata23
.sdata
.sdata23
|
Declaration (type bss)
|
.zbss
.zbss23
.bss
.tbss4
.tbss5
.tbss7
.tbss8
.ebss
.ebss23
.sbss
.sbss23
|
Type const
|
const
zconst
zconst23
|
.const
.zconst
.zconst23
|
Type text
|
text
|
.text
|
Clearing of specification
|
default
|
Clears all specifications, and restores the default section name.
|
#pragma section gp_disp16
int a = 1; /*Allocated to ".sdata"*/
int b; /*Allocated to ".sbss"*/
|
- | -ep_auto produces the same behavior as ep_disp4, ep_disp5, ep_disp7, or ep_disp8 depending on the type of declaration. |
|
|
unsigned char
|
ep_disp4
|
unsigned short
|
ep_disp5
|
signed char
char
_Bool
|
ep_disp7
|
signed short
signed int
unsigned int
signed long
unsigned long
signed long long
unsigned long long
float
double
long double
pointer
enumerate type (when the size of the elements is that of the int type)
|
ep_disp8
|
Remark 1. | The ep_dispn for array-type variables depends on the type of the elements. In a multi-dimensional array (an array of arrays), however, the selected ep_dispn corresponds to the type of the elements stored in the array. |
#pragma section ep_auto
char c_ary[3][5] = {0}; /*.tdada7 (char)*/
char* p_ary[3][5] = {0}; /*.tdata8 (pointer)*/
|
Remark 2. | If a variable of the enumerate type is smaller than one of the int type due to the -Xenum_type option having been specified, the ep_dispn that corresponds to the given smaller type is selected. |
Remark 3. | Structures and unions are not allocated as any from among .tdata4, .tdata5, .tdata7, and .tdata8. |
- | Since ep_auto produces the same behavior as ep_disp4, ep_disp5, ep_disp7, or ep_disp8 depending on the type of declaration, ep_auto and the ep_dispn that produces the same behavior being specified for the declaration and definition of the same variable does not lead to an error. ep_auto and an ep_dispn that produces different behavior being specified for the declaration and definition of the same variable will lead to an error. |
#pragma section ep_auto
extern unsigned char ch;
#pragma section ep_disp4
unsigned char ch; /*Allocated as .tbss4 with no errors or warnings*/
|
#pragma section ep_auto
extern unsigned char ch;
#pragma section ep_disp7
unsigned char ch; /*Allocated as .tbss4 with no errors or warnings*/
/*When #pragma section is specified for the first*/
/*declaration, it takes priority*/
|
(c) | Specifying "#pragma section [character-string]" |
This switches the section name output by the compiler.
The section name after switching from the default section name is as follows.
|
|
|
|
Program
|
#pragma section xxx
|
text
|
xxx.text
|
Constant
|
const
|
xxx.const
|
Initialized data
|
data
|
xxx.data
|
Uninitialized data
|
bss
|
xxx.bss
|
- | If the string is omitted, all section names are set to the default. |
#pragma section abc
int a; /*a is allocated to the section abc.bss*/
const int d=1; /*d is allocated to the section abc.const*/
void f(void) /*f is allocated to the section abc.text*/
{
a = d;
}
#pragma section
int b; /*b is allocated to the section .bss*/
void g(void) /*g is allocated to the section .text*/
{
b = d;
}
#pragma section 123
int c; /*c is allocated to the section _123.bss*/
void h(void) /*h is allocated to the section _123.text*/
{
c = d;
}
|