This sophisticated and powerful pragma lets you arrange compiled object code into predefined sections and sections you define.
#pragma section [ objecttype | permission ][iname][uname] [data_mode=datamode][code_mode=codemode]
objecttype
specifies where types of object data are stored. It may be one or more of these values:
Specify one or more of these object types without quotes separated by spaces.
The CodeWarrior C/C++ compiler generates some of its own data, such as exception and static initializer objects, which are not affected by #pragma section.
permission
specifies access permission. It may be one or more of these values:
Specify one or more of these object types without quotes separated by spaces.
For more information on access permission, see "Section access permissions" on page 487.
iname
specifies the name of the section where the compiler stores initialized objects. Variables that are initialized at the time they are defined, functions, and character strings are examples of initialized objects.
The iname parameter may be of the form .abs.xxxxxxxx where xxxxxxxx is an 8-digit hexadecimal number specifying the address of the section.
uname
specifies the name of the section where the compiler stores uninitialized objects. This parameter is required for sections that have data objects. The uname parameter value may be a unique name or it may be the name of any previous iname or uname section. If the uname section is also an iname section then uninitialized data is stored in the same section as initialized objects.
The special uname COMM specifies that uninitialized data will be stored in the common section. The linker will put all common section data into the ".bss" section. When the Use Common Section checkbox is checked in the EPPC Processor panel, COMM is the default uname for the .data section. If the Use Common Section checkbox is clear, .bss is the default name of .data section.
The uname parameter value may be changed. For example, you may want most uninitialized data to go into the .bss section while specific variables be stored in the COMM section.
Storing Uninitialized Data in the COMM Section shows an example where specific uninitialized variables are stored in the COMM section.
#pragma push // save the current state #pragma section ".data" "COMM" int red; int sky; #pragma pop // restore the previous state
data_mode=datamode
specifies the compiler for the kind of addressing mode to be used for referring to data objects for a section.
The permissible addressing modes for datamode are:
The sda_rel addressing mode may only be used with the ".sdata", ".sbss", ".sdata2", ".sbss2", ".PPC.EMB.sdata0", and ".PPC.EMB.sbss0" sections.
The default addressing mode for large data sections is far_abs. The default addressing mode for the predefined small data sections is sda_rel.
Specify one of these addressing modes without quotes.
code_mode=codemode
specifies the compiler for the kind of addressing mode to be used for referring to executable routines of a section.
The permissible addressing modes for codemode are:
The default addressing mode for executable code sections is pc_rel.
Specify one of these addressing modes without quotes.
CodeWarrior compilers generate their own data, such as exception and static initializer objects, which the #pragma section statement does not affect.
When you define a section by using #pragma section, its default access permission is read only. Changing the definition of the section by associating an object type with it sets the appropriate access permissions for you. The compiler adjusts the access permission to allow the storage of newly-associated object types while continuing to allow objects of previously-allowed object types. For example, associating code_type with a section adds execute permission to that section. Associating data_type, sdata_type, or sconst_type with a section adds write permission to that section.
Occasionally you might create a section without associating it with an object type. You might do so to force an object into a section with the __declspec keyword. In this case, the compiler automatically updates the access permission for that section to allow the object to be stored in the section, then issue a warning. To avoid such a warning, make sure to give the section the proper access permissions before storing object code or data into it. As with associating an object type to a section, passing a specific permission adds to the permissions that a section already has.
When an object type is associated with the predefined sections, the sections are set as default sections for that object type. After assigning an object type to a non-standard section, you may revert to the default section with one of the forms in "Forms for #pragma section" on page 488.
The compiler predefines the sections in Predefined sections .
#pragma section code_type ".text" data_mode=far_abs code_mode=pc_rel #pragma section data_type ".data" ".bss" data_mode=far_abs code_mode=pc_rel #pragma section const_type ".rodata" ".rodata" data_mode=far_abs code_mode=pc_rel #pragma section sdata_type ".sdata" ".sbss" data_mode=sda_rel code_mode=pc_rel #pragma section sconst_type ".sdata2" ".sbss2" data_mode=sda_rel code_mode=pc_rel #pragma section ".PPC.EMB.sdata0" ".PPC.EMB.sbss0" data_mode=sda_rel code_mode=pc_rel #pragma section RX ".init" ".init" data_mode=far_abs code_mode=pc_rel
#pragma section ".name1"
This form simply creates a section called .name1 if it does not already exist. With this form, the compiler does not store objects in the section without an appropriate, subsequent #pragma section statement or an item defined with the __declspec keyword. If only one section name is specified, it is considered the name of the initialized object section, iname. If the section is already declared, you may also optionally specify the uninitialized object section, uname. If you know that the section must have read and write permission, use #pragma section RW .name1 instead, especially if you use the __declspec keyword.
#pragma section objecttype ".name2"
With the addition of one or more object types, the compiler stores objects of the types specified in the section .name2. If .name2 does not exist, the compiler creates it with the appropriate access permissions. If only one section name is specified, it is considered the name of the initialized object section, iname. If the section is already declared, you may also optionally specify the uninitialized object section, uname.
#pragma section objecttype
When there is no iname parameter, the compiler resets the section for the object types specified to the default section. Resetting the section for an object type does not reset its addressing modes. You must reset them.
When declaring or setting sections, you also can add an uninitialized section to a section that did not have one originally by specifying a uname parameter. The corresponding uninitialized section of an initialized section may be the same.
You may store a specific object of an object type into a section other than the current section for that type without changing the current section. Use the __declspec keyword with the name of the target section and put it next to the extern declaration or static definition of the item you want to store in the section.
Using __declspec to Force Objects into Specific Sections shows examples.
__declspec(section ".data") extern int myVar; #pragma section "constants" __declspec(section "constants") const int myConst = 0x12345678
You can use this pragma with #pragma push and #pragma pop to ease complex or frequent changes to sections settings.
See Storing Uninitialized Data in the COMM Section for an example.