Position independent code problem

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Position independent code problem

1,714 Views
davidsherman
Senior Contributor I

I am working on a bootloader for the Kinetis KE18.  I have gotten it to work, except for one problem.  The code for the bootloader is not truly position-independent; I have it linked into flash, and to start it I copy it into RAM, and jump into it.  It works, except I discovered there are a couple of absolute jumps that jump back into flash.  If the image I am programming doesn't match exactly, it crashes after programming the sector where the bootloader is located.

I turned on the option to generate position-independent code, but it doesn't quite work.  It seems if I let the linker automatically locate the global offset table in the .bss section, it doesn't work when I  call the routine to initialize global variables used by the bootloader; it ends up writing to invalid locations.  If I alter the linker to locate the GOT in the bootloader flash section, then the routine works, but it crashes when I launch the bootloader and move the section (including the GOT) into RAM.  I suspect the GOT needs to stay fixed, but what's the trick to getting the position independent code to work properly?

0 Kudos
Reply
2 Replies

1,525 Views
davidsherman
Senior Contributor I

I figured it out.  It seems that turning on the position-independent code option in the compiler settings only works for variables that are truly global.  I had some static variables local to the source file that would not read correctly; it seems you cannot have global variables that are static to the module.  I got the code copy to RAM to work by making the .got section part of the section that copies to RAM.  However, in order to do debugging properly at the source level, the bootloader code has to be located in the .data section, and the linker has to be modified to move the global offset table out of flash and into .data, otherwise, when the flash gets reprogrammed, the .got will be overwritten and things won't work.

0 Kudos
Reply

1,525 Views
converse
Senior Contributor V