cancel
Showing results for 
Search instead for 
Did you mean: 

Why does the linker script map the .text section at a different address depending on build?

Konami
Senior II

I noticed that my .text section gets mapped at one of two different addresses depending on my build. In one instance the hex file had a small (8 byte) gap in it which was causing some issues with some hex file parsing that I do. I've since worked around that, but now I'm curious as to the root cause.

I've been able to reproduce the two different results by commenting out a single print statement. I'm not an expert with linkers so I was hoping someone could help:

  1. Explain why this might happen?
  2. Suggest what I could do to ensure no gap in the hex file

This is on an STM32F3, using the CubeMX generated System Workbench project. Here's the relevant part of the linked script (the .ld is unchanged from the auto generated one) and the relevant sections of the two resulting .map files.

/* Define output sections */
SECTIONS
{
  /* The startup code goes first into FLASH */
  .isr_vector :
  {
    . = ALIGN(4);
    KEEP(*(.isr_vector)) /* Startup code */
    . = ALIGN(4);
  } >FLASH
 
  /* The program code and other data goes into FLASH */
  .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 */
    *(.eh_frame)
 
    KEEP (*(.init))
    KEEP (*(.fini))
 
    . = ALIGN(4);
    _etext = .;        /* define a global symbols at end of code */
  } >FLASH

0693W000004JWuUQAW.png

4 REPLIES 4
Konami
Senior II

I tried forcing the two sections to be contiguous, which then gives me a linker error. I'm confounded as to what might be using those few addresses:

cannot move location counter backwards (from 08000190 to 08000188)

/* Define output sections */
SECTIONS
{
  /* The startup code goes first into FLASH */
  .isr_vector :
  {
    . = ALIGN(4);
    KEEP(*(.isr_vector)) /* Startup code */
    . = ALIGN(4);
    _eisr = .;           /* define a global symbol at end of ISR vector */
  } >FLASH
 
  /* The program code and other data goes into FLASH */
  .text :
  {
    . = _eisr;
    . = ALIGN(4);
    *(.text)           /* .text sections (code) */
    *(.text*)          /* .text* sections (code) */
    *(.glue_7)         /* glue arm to thumb code */
    *(.glue_7t)        /* glue thumb to arm code */
    *(.eh_frame)
 
    KEEP (*(.init))
    KEEP (*(.fini))
 
    . = ALIGN(4);
    _etext = .;        /* define a global symbols at end of code */
  } >FLASH

Konami
Senior II

I still haven't made any further progress with this. Any suggestions as to what may be going on?

Konami
Senior II

Is there a better place to ask about linker script issues? I'm still working around this but would like to get to the bottom of it

Perhaps it has nothing to do with the linker / scripts, but rather alignment flagged on sections within the object files, or by the compiler in different modes (optimization). I really don't have enough data to know, and I'm not using CubeMX/IDE

You'd perhaps want to inspect the .ELF objects for content / flagging

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..