Selective Code Protection

8-bit PIC devices have code protection configuration bits which allow you to prevent unauthorised access of program memory. Some of these devices allow selective protection of the program memory space, and with these devices, you can specify which blocks are protected. There are different sorts of protection, too, for example: write protection or protection from external block table reads.

This article looks at how you can arrange for the MPLAB® XC8 Compiler to place special code into memory areas that you have protected and for the remainder of your program to be placed the unprotected memory. There are four main steps to follow.

  • Place functions to be protected into their own unique section
  • Create a linker class that represents the protected memory area
  • Link the unique sections in this new linker class
  • Reserve the protected area to prevent it from being used by other functions

Functions can be easily placed into a new section (or psect as they are also called) using the __section() specifier. The following is an example of a function allocated to a section called protected_text rather than its default section:

The section name can be any identifier using alphabetics, numerics, and the underscore character. Any section containing code and whose name contains the string text might be optimized if the assembler optimisers are enabled. Choose an alternate section name if you do not want the section contents optimized.

Next, determine the protected memory ranges from your target device's data sheet. For example, PIC18Fx3K20 devices have what is called the boot block spanning 0-1FF and two other blocks spanning 200-FFF, and 1000-1FFF. If, for example, you decide to protect the boot block and place your protected functions anywhere in that block, you can create a suitable linker class with the compiler option -L-APROTECT=0h-1FFh. This is the compiler’s -L- option, which passes the -A linker option (and arguments) directly to the linker application. It requests that PROTECT be made a linker class that consists of one contiguous block of memory ranging from address 0 to 0x1FF.

You can now place your new sections into the memory defined by this class. To do that, use another linker option (-P) passed to the linker via the compiler’s -L- option -L-Pprotected_text=PROTECT.

It is only convention that dictates you use an upper-case class name and lower-case section name, but note that the linker is case sensitive when it comes to these names.

If you are using MPLAB X IDE, add both of the above options to the XC8 linker > Additional options > Extra Linker Options field, as shown below.


You can prevent ordinary functions from being placed into the protected memory space by reserving this area using the —ROM option. For example use —ROM=default,-0-1FFh if you are compiling on the command line, or by entering this option argument into the XC8 linker > Memory model > ROM ranges field, as shown below.


Finally, check the map file to ensure that this all worked as you expected. The following is an extract from the map file, showing that the section created in this example was linked into the boot block memory.


Notice that no other sections were linked in this block. Do not be concerned that the new section is shown under the CODE class in this example; that is the default class assigned to the new section when it was created and this default class is used to arrange sections in the map file.

© 2017 Microchip Technology, Inc.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.