6.6.6.6.8 -ffunction-sections

Place each function into its own section in the output file. The name of the function determines the section’s name in the output file.

However, there are many conditions that can effect what exactly this means. Here is a summary:

  • Normal (non interrupt) functions will have the current section name and a "." prepended to them, for example:

    void foo() {}

    will be placed into section .text.foo (the default code section name is .text).

  • The default section name can be modified with the -mtext option. If this option has been used, then current section name will be changed. For example, if -mtext=mytext is specified, then the above function will be placed into mytext.foo.
  • If the function has a section attribute, then it will be placed into that named section without any adulteration. Therefore,

    void __attribute__((section("mytext"))) foo() {}

    will always be placed into the section mytext regardless of whether or not -ffunction-sections is specified.

  • Interrupt functions are normally placed into a special section with the name .isr prepended to the normal section name (as above). Therefore if the current section name is .text (the default), then the ISR is placed into .isr.text.function_name.

    If the -mtext is used to change the name of the default section name, then this will be substituted instead of .text. However, if a named section is used with a section attribute, .isr will still be prepended to the section name.

    The .isr is prepended to allow the --gc-sections option to not throw away interrupt functions. These must be kept.

Only use this option when there are significant benefits for doing so. When you specify this option, the assembler and linker may create larger object and executable files and will also be slower.