cancel
Showing results for 
Search instead for 
Did you mean: 

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

Krzysztof Sawicki
Associate
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.
13 REPLIES 13
Walid FTITI_O
Senior II
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
Senior
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

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.).

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 .
Posted on April 08, 2017 at 23:20

Some things work (GPIO based) and some not.

HAL_Delay() with the use of Systick will never return.

Using a Timer for HAL_Delay(), the application is not even executed.

I'm still looking for the right recipe.

Posted on April 09, 2017 at 00:13

The vector table contains absolute addresses, you'd want to fixup/rebase that into RAM

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
S.Ma
Principal
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.

Posted on April 10, 2017 at 00:09

Magic!

Thank you so much!

Posted on August 01, 2017 at 07:51

Can you please share your solution? I am having some sanity issues with relocating executable code into RAM.