How do I fill unused program memory with a known value using the XC8 compiler?

The --FILL driver option allows you to fill the unused program memory with a known value. 
You can access this option in the MPLAB® X IDE > XC8 Project Properties > XC8 Linker > Fill Flash Memory selection in the drop-down menu; and then enter the appropriate argument in the Fill field.

This option utilizes the Hexmate application to fill unused locations, thus it will only work for HEX files — it will not fill unused locations in binary file outputs.

The usage of the driver option is:
--FILL=[const_width:]fill_expr[@address[:end_address]]

Example:

Executing the following command will fill the unused locations in hex file with 0x3FFF

hexmate.exe Code.hex --fill=0x3FFF

fill_expr can use the syntax (where const and increment are n-byte constants):

  • const fill memory with a repeating constant.
    For example: --FILL=0xBEEF becomes 0xBEEF, 0xBEEF, 0xBEEF, 0xBEEF
  • const+=increment fill memory with an incrementing constant.
    For example: --fill=0xBEEF+=1 becomes 0xBEEF, 0xBEF0, 0xBEF1, 0xBEF2
  • const-=increment fill memory with a decrementing constant.
    For example: --fill=0xBEEF-=0x10 becomes 0xBEEF, 0xBEDF, 0xBECF, 0xBEBF
  • const,const,…,const fill memory with a list of repeating constants.
    For example: --FILL=0xDEAD,0xBEEF becomes 0xDEAD,0xBEEF,0xDEAD,0xBEEF

The options following fill_expr result in the following behavior:

  • @unused (or nothing) fill all unused memory with fill_expr.
    For example: --FILL=0xBEEF@unused fills all unused memory with 0xBEEF.
  • @address fill a specific address with fill_expr.
    For example: --FILL=0xBEEF@0x1000 puts 0xBEEF at address 1000h
  • @address:end_address fill a range of memory with fill_expr.
    For example: --FILL=0xBEEF@0:0xFF puts 0xBEEF in unused addresses between 0 and 255
© 2023 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
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.