4.8 Program Sections

Program sections, or psects, are simply a section of code or data. They are a way of grouping together parts of a program (via the psect’s name) even though the source code cannot be physically adjacent in the source file, or even where spread over several modules.

A psect is identified by a name and has several attributes. The PSECT assembler directive is used to define a psect. It requires a name argument, which may be followed an comma-separated list of flags which define its attributes. Linker options that can be used to control psect placement in memory are described in Linker. These options can be accessed from the driver using the -Wl driver option (see 3.4.36 Wl: Pass Option To The Linker, Option), negating the need for you to run the linker explicitly.

See Assembler-provided Psects and Linker Classes for a list of all psects that can be supplied by the assembler.

Unless defined as abs (absolute), psects are relocatable.

Code or data that is not explicitly placed into a psect using the PSECT directive will become part of the default (unnamed) psect. As you have no control over where this psect is linked, it recommended that a PSECT directive always be placed before the code and objects.

When writing assembly code, you can use the psects provided once <xc.inc> has been included. These have a similar name and function to the sections used by the 8-bit MPASM assembler.

If you create your own psects, try to associate them with an existing linker class (see 5.3 Default Linker Classes and 6.1.2 C: Associate Linker Class To Psect) otherwise you can need to specify linker options for them to be allocated correctly.

Note, that the length and placement of psects is important. It is easier to write code if all executable code is located in psects that do not cross any device pages boundaries; so, too, if data psects do not cross bank boundaries. The location of psects (where they are linked) must match the assembly code that accesses the psect contents.