Search code examples
armcortex-mlinker-scriptsthumb

Why does a Cortex-M4 include ARM to Thumb glue in the linker script


We are working on some code for ARM Cortex M4 on a STM32 chip.

My understanding is that Cortex-M4 has some 32-bit instructions but these are not 32-bit ARM instructions they are just a few special instructions. I thought the glue was for transitioning between ARM and thumb instructions sets. So why does the linker script need the glue?

.text :
{
. = ALIGN(4);
*(.text)           /* .text sections (code) */
*(.text*)          /* .text* sections (code) */
*(.glue_7)         /* glue arm to thumb code */
*(.glue_7t)        /* glue thumb to arm code */

Can I remove the glue_7 and glue_7t since the processor only supports thumb instructions? Would there be any flash memory freed up by doing this?


Solution

  • The posted script is always going to create the sections for ARM/thumb code calling thumb/ARM, if there is nothing to call, the sections are empty. An empty section is benign.

    If you want to remove unused sections without modifying the linker, a clean way is performing the dead code elimination, via --gc-sections:

    Once the objects and static libraries are created with these options, the linker can perform the dead code elimination. You can do this by setting the -Wl,--gc-sections option to gcc command or in the -largs section of gnatmake. This will perform a garbage collection of code and data never referenced.