5.7.1.16 Pure-code Option
The -mpure-code
option ensures that the compiler will not place const
-qualified data into any section that also contain executable code. This is a requirement if you intend to place code into eXecute-Only Memory (XOM). Additionally, this option marks sections in the generated ELF file containing executable code with the SHF_ARM_PURECODE
flag to indicate that they purely contain code and no data. Placement of pure code sections into XOM is performed separately (see 16.4 Changing the Default Function
Allocation).
This option is usable with Arm Cortex-M23 and Cortex-M0+ based target devices. Consult your specific target device's data sheet to determine whether your target device supports XOM and for more information on its usage. Using this option with the MPLAB Code Coverage feature is not supported and will trigger an error from the compiler.
Using this option might affect the code size and execution speed of the program. Data qualified const
is often placed into program memory and read when it is needed. This cannot be performed when this option is in effect, and the compiler will need to generate more/longer instructions to load the constant as an immediate operand. For example, to load an int
const
-qualified constant stored into memory will require the 4 bytes of constant data and typically a 2-byte ldr
instruction to load that constant into a register. To instead load this constant using a movw
+ movt
instruction sequence with immediate operands would require two 4-byte instructions. For devices such as the Cortex M0+ that cannot use such instruction sequences, the code to load the constant byte-by-byte can be significantly larger. When enabling this option, be sure to verify that any increase in code size or execution time still meets your application's timing requirements.
Precompiled libraries provided with MPLAB XC32, such as the Standard C library, are not built for Pure Code. Mapping these functions to an XOM region in your application linker script might result in code failure.
When using both the -mpure-code
and -ffunction-sections
options, the generated input section will be named according to the rules of the -ffunction-sections
option, making it more difficult to map code to XOM in a custom linker script. In this case, you can't rely on the SHF_ARM_PURECODE
section flag, and you must map the section by name instead.
The same is true when combining the -mpure-code
option with function attributes that directly or indirectly affect function placement. Such attributes include tcm
, ramfunc
, address
, always_inline
, etc. Again, map the section by name in the custom linker script rather than using the SHF_ARM_PURECODE
section flag to ensure your code is fully protected.
Procedural abstraction optimizations (-mpa
) might abstract code out of sections designated to be pure code when using the -mpure-code
option, and this abstracted code will need to be appropriately mapped to XOM in the custom linker script.
The -mno-pure-code
form of this option will not separate executable code and const
-qualified data. This is the default action if no option is specified.