Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob

Who rated this article

Who rated this article

Optimizing Code Size in FX3 SDK - KBA230335

50 questions asked 25 likes received 25 sign-ins

Version: *A

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:

  1. 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:

  1. Right-click the project in Project Explorer.
  2. Click Properties from the pop-up menu.
  3. In the Properties dialog, from the left pane, expand C/C++ Build. Click Settings.
  4. Go to the Tool Settings tab.
  5. 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:

  1. Navigate to Optimization Settings for the project (see Figure 2).
  2. Select Link-time optimizer (-flto) (see Figure 3).


pastedImage_5.pngFigure 3. Enabling Link-time Optimizer

  1. Click Apply, and then click OK.
  2. Before building the project, clean the project once. Right-click the project and click Clean Project.
  3. Now, build the project.
Who rated this article