Optimizing Code Size in FX3 SDK - KBA230335
Translation - Japanese: タイトル：FX3 SDKのコードサイズの最適化- KBA230335- Community Translated (JA)
Identifying the space consumed by each symbol
When a new feature is added to an existing project, code space might run out. The arm-none-eabi-nm tool, bundled with the GNU ARM® toolchain, can be used to identify the symbol consuming more code space.
Use the following command in command prompt to identify the symbol consuming the most code space (in decimals).
arm-none-eabi-nm --print-size -–size-sort --radix=d “path of elf file”
The GNU nm tool prints all symbols to an ELF file, while the --size option prints the size of each symbol, and sorts them by size if --size-sort is specified.
This command will print each symbol along with its size in ascending order.
Optimizing the code size
Do any of the following to optimize the code size:
- Building the project in Release mode
While building the project in Debug mode, debugging and symbolic information are included. Also, the optimization options are disabled to enable proper debugging. However, Infineon recommends Release mode, in which debugging and symbolic information are removed, and optimization options are enabled to reduce code size. To build the project in Release mode, select Release (Release configuration) from the build drop-down list.
Figure 1. Selecting Release Configuration for Building Project
When the project is built using Release configuration, the Optimization Level is automatically changed to Optimize most (-O3).
To view the optimization options:
- Right-click the project in Project Explorer.
- Click Properties from the pop-up menu.
- In the Properties dialog, from the left pane, expand C/C++ Build. Click Settings.
- Go to the Tool Settings tab.
- Click Optimization to view the Optimization Level.
Figure 2. Optimization Settings for a Project
2. In some cases, you can reduce the size further by changing the optimization level from Optimize most (-O3) to
Optimize size (-Os). Select Optimize size (-Os) from the Optimization Level drop-down list (see Figure 2).
3. Another way to reduce the size of a .img file is to use link time optimization. Link time optimization is an optimization pass made across all compilation units, which helps in reducing the overall size of a binary. Use this technique only if you want to reduce the code size even further after using the first two methods. Also, while this technique is used to reduce the code size, sufficient testing needs to be done to make sure that the functionality of the code is not broken.
To enable link time optimization:
- Navigate to Optimization Settings for the project (see Figure 2).
- Select Link-time optimizer (-flto) (see Figure 3).
Figure 3. Enabling Link-time Optimizer
- Click Apply, and then click OK.
- Before building the project, clean the project once. Right-click the project and click Clean Project.
- Now, build the project.