Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to load position independent code on cortex-m3

Tags:

position

fpic

arm

I have an embedded application which will have a bootloader which will decide to run 1 of two applications directly from internal flash. I am trying to make these apps position independent so that they both can be compiled for the same base address. There is no operating system, so no dynamic linker is available. So far I have tried building with -fpie option (using gcc) with not too much success. The function calls appear to be correct but the global data does not have the correct address. The locally defined global data seems to have it's address offset by the amount that the app is offset from its original base address. The global data which is declared in other files has a completely wrong address(and if I build with -fpic then both the locally declared global data and global data in other files are completely wrong). I suspect I need to do some manipulation of the GOT section when my app starts but I am not sure.

like image 881
Dan Avatar asked Feb 17 '11 02:02

Dan


1 Answers

I finally got it working. It looks like I need to do the following: All code needs to be complied with -fpic (previously I was trying -fpie)

Also I had my linker script needed modification. I was forcing the GOT into the sram section and it was located after the dynamic section which was in flash. Looks like everything works properly if the GOT section is located prior to the dynamic section in the flash. Not sure why this matters but it seemed to fix everything - prior to this it was as if the code did not locate the GOT properly since the GOT had the correct values stored in it but the address of all my variables were incorrect.

like image 107
Dan Avatar answered Jan 01 '23 22:01

Dan