Skip to main content
Krzysztof Sawicki
Associate
December 9, 2016
Question

Hi! Anyone knows how to compile code for STM32 (using gcc) which can be loaded at any position in flash and works ok?

  • December 9, 2016
  • 13 replies
  • 3601 views
Posted on December 09, 2016 at 08:34

I'm going to write 2 revisions of the same program to flash and make a bootloader which will start one of them, depending on some criterias.

#gcc-position-independend

Note: this post was migrated and contained many threaded conversations, some content may be missing.
    This topic has been closed for replies.

    13 replies

    Walid FTITI_O
    Visitor II
    December 9, 2016
    Posted on December 09, 2016 at 11:41

    Hi

    sawicki.krzysztof

    ,

    You can do this by creating new sections in Flah using scatter (Linker) file . See example shown in this

    https://community.st.com/0D50X00009XkhC7SAJ

    .

    -Hannibal-

    ST Renegade
    Associate
    December 9, 2016
    Posted on December 09, 2016 at 21:43

    Not sure right now, but ST link should be able to load code into any location of memory? However it is much more convenient to have one file, or you can use the gnu tools to append one code at the end of other or so to create one binary out of it (objcopy???). I think it's the same process as loading a bootloader and an app into the MCU.

    Have a nice day,

    Renegade

    Krzysztof Sawicki
    Associate
    December 12, 2016
    Posted on December 12, 2016 at 10:15

    It is not a problem for me to load the code at any position, but the problem is: how to get the machine code which will run at any position (this means no absolute adresses, jumps etc.).

    Paolo ALBERELLI_O
    Visitor II
    December 20, 2016
    Posted on December 20, 2016 at 12:01

    The feature you look for is called 'PIC' (Position Independent Code); At gcc

    -fpic

    option would generate PIC code.

    If can refer directly to the gcc Code Generation Options ( 

    https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html ♯ Code-Gen-Options

     ) and ARM machine dependent options (

    https://gcc.gnu.org/onlinedocs/gcc/ARM-Options.html ♯ ARM-Options

     ) for details.

    ARM Cortex-M should support

    -fpic

    since there are ARM specific options around PIC such as

    -mpic-register=

    reg .
    S.Ma
    Principal
    April 9, 2017
    Posted on April 09, 2017 at 09:21

    Linux style code which can be ram loaded anywhere in the memory space is to be in demand in embedded bare metal or rtos space. It basically forbid to use the satically defined heap space by the linker. In 1990's HW access was done through what became the bios, through so called SWI #nn to make code more relocatable. If the code only use stack, then something might be ironed out. Indirect addressing in accessing data or function by pointers may cost cycles and acceleration bumps. These are performance or energy penalties when developping deep embedded apps on battery budget.

    gaurav patni
    Associate II
    August 16, 2017
    Posted on August 16, 2017 at 06:57

    Hi

    Wagner.Joerg

    ,

    request you please share the solution with details of require compiler - linker settings required.