Microchip Technology SW006023-1N Data Sheet

Page of 238
Common C Interface
 2012 Microchip Technology Inc.
DS51686E-page 39
2.5.13
Assigning Objects to Sections
The __section() specifier may be used to indicate that an object should be located 
in the named section (or psect, using the XC8 terminology). This is typically used when 
the object has special and unique linking requirements which cannot be addressed by 
existing compiler features.
Use the native keywords discussed in the Differences section to look up information on 
the semantics of this specifier.
2.5.13.1
EXAMPLE
The following shows a variable which uses the __section keyword.
int __section("comSec") commonFlag;
2.5.13.2
DIFFERENCES
The 8-bit compilers have used the #pragma psect directive to redirect objects to a 
new section, or psect. The operation of the __section() specifier is different to this 
pragma in several ways, described below.
Unlike with the pragma, the new psect created with __section() does not inherit the 
flags of the psect in which the object would normally have been allocated. This means 
that the new psect can be linked in any memory area, including any data bank. The 
compiler will also make no assumptions about the location of the object in the new sec-
tion. Objects redirected to new psects using the pragma must always be linked in the 
same memory area, albeit at any address in that area.
The __section() specifier allows objects that are initialized to be placed in a different 
psect. Initialization of the object will still be performed even in the new psect. This will 
require the automatic allocation of an additional psect (whose name will be the same 
as the new psect prefixed with the letter i), which will contain the initial values. The 
pragma cannot be used with objects that are initialized.
Objects allocated a different psect with __section() will be cleared by the runtime 
startup code, unlike objects which use the pragma.
You must reserve memory, and locate via a linker option, for any new psect created with 
a __section() specifier in the current XC8 compiler implementation.
The 16- and 32-bit compilers have used the section attribute to indicate a different 
destination section name. The __section() specifier works in a similar way to the 
attribute.
2.5.13.3
MIGRATION TO THE CCI
For XC8, change any occurrence of the #pragma psect directive, such as
#pragma psect text%%u=myText
int getMode(int target) {
//...
}
to the __section() specifier, as in
int __section ("myText") getMode(int target) {
//...
}
For 16- and 32-bit compilers, change any occurrence of the section attribute, as in 
the following example:
int __attribute__((section("myVars"))) intMask;
to:
int __section("myVars") intMask;