Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do STM32 gcc linker scripts automatically discard all input sections from these standard libraries: libc.a, libm.a, libgcc.a?

From the bottom of any auto-generated STM32CubeMx-generated linker script:

/* Remove information from the standard libraries */
/DISCARD/ :
{
  libc.a ( * )
  libm.a ( * )
  libgcc.a ( * )
}

From the GNU Binutils ld (linker script) manual, 3.6.7 Output Section Discarding:

The special output section name ‘/DISCARD/’ may be used to discard input sections. Any input sections which are assigned to an output section named ‘/DISCARD/’ are not included in the output file.

What do these 3 input object files contain, and why do we discard everything (all input sections) from them?

Other STM32 linker script topics of interest:

  1. Is accessing the "value" of a linker script variable undefined behavior in C?
  2. How to get value of variable defined in ld linker script from C
like image 471
Gabriel Staples Avatar asked Apr 10 '19 22:04

Gabriel Staples


1 Answers

Looks like in this example, /DISCARD/ removes any other sections, that are not explicitly defined by script. For example, since *(.text), *(.data), *(.bss), *(.init_array) etc, has been defined earlier in the script, they get into the ELF. But libc, libm or libgcc could contain unnecessary sections for firmware (e.g. .foo, .bar, .debug ...), so /DISCARD/ just wipes them out, but NOT all sections!

like image 67
udik-chudik Avatar answered Nov 12 '22 10:11

udik-chudik