AnsweredAssumed Answered

Position independent code problem

Question asked by David Sherman on Jan 30, 2019
Latest reply on Feb 2, 2019 by David Sherman

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?

Outcomes