How do I resolve the MPLAB® XC32 error "small-data section exceeds 64 KB; lower small-data size limit (see option -G)"?

The -G num option puts the global and static items less than or equal to num bytes into the small data or bss section, instead of the normal data or bss section. This allows the data to be accessed using a single instruction. This option controls the gp-relative addressing threshold. However, gp-relative addressing is limited to 64 KB of small data.
The default number value of -G is 8. This means that data objects that are 8 bytes or smaller go into the small data or bss section and can be accessed more efficiently. The error small-data section exceeds 64KB; lower small-data size limit (see option -G) means that the data variables that are 8 bytes or smaller exceed 64 KB.
To prevent this error, reduce the number of variables that get allocated in the small data section. This can be done by reducing the -G value. In MPLAB® X IDE, under File>Project properties>XC32>Global Options, select ’Use GP relative addressing threshold’ and enter the value 4 (or any other value less than 8). This will pass the option -G 4 to the compiler, and only the objects 4 bytes or smaller will go into the small data section.

You may also disable small-data using -G0 but, this comes with size performance degradation.
If reducing the value to 4 doesn't resolve the error, you may need to explicitly place some variables into a named section using the section attribute.
E.g.,
int attribute((section("mysection"))) foo;

Placing a variable into a named section prevents it from going into the small-data section. This can also be done for variables that are not accessed frequently.

Or, allocate large data in .data/.bss.
E.g.,
unsigned int my_array[2000] attribute((section(“.data")));

© 2024 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.