Usage of -fstack-usage in GCC Toolchain

-fstack-usage is an option provided by GCC (GNU Compiler Collection) that helps developers analyze and optimize the stack memory usage of their code. It generates a stack usage report for each function during the compilation process and saves the information to a .su file. This option is particularly useful for embedded system development, where stack memory resources are often limited and controlling function stack usage is essential.

1. Usage of -fstack-usage

When using -fstack-usage, you can add this option to GCC during compilation, as shown below:

gcc -fstack-usage -o output_file source_file.c

2. Generated .su Files

After compiling the code with -fstack-usage, GCC generates a corresponding .su file for each source file. For example, if the source file is named source_file.c, a file named source_file.su will be created after compilation. This .su file contains information about the stack usage of each function.

Each line in the .su file contains the following fields:

<function_name> <stack_usage_size> <filename>:<line_number>

The meaning of each field is as follows:

<function_name>: The name of the function.

<stack_usage_size>: The amount of memory allocated by the function on the stack (in bytes).

<filename>:<line_number>: The source file and line number where the function is defined.

Original link: https://blog.csdn.net/sz66cm/article/details/143186027

Leave a Comment