I'm building code for NXP S32K148 MCU (Arm Cortex M4F) and I need to build code which is position independent. I'm using gcc 6.3.1 provided by NXP.I've almost managed to make it work...
1. I set the compile options "-fpic -msingle-pic-base -mno-pic-data-is-text-relative -mpic-register=r9". 2. I modified startup code: a. Calculate offset between compile time and run-time program counter (relocation offset). b. Copy the global offsets table to RAM while fixing code addresses by the relocation offset. c. Fix the interrupts vectors table pointers by the relocation offset. d. Set R9 to point to the global offsets table.
It worked - except that any function pointers inside initialized variable/structs were not relocated and were not accessed through the global offsets table. When execution reached such a pointer the system of course crashed.
Does anyone have a solution?
Freddy
Hi Freddy, did you manage to find a solution for this issue in gcc? I only found that in arm toolchain and in iar toolchain there is this -ropi switch (read only position independent) which solves exactly this problem, but struggling to find something similar in gcc.
I could not find a good solution. The main problem (as far as I remember) had to do with pointers in static structure and I could not avoid them in runtime library.We didn't need completely position independent code, just needed builds for two images A & B (to allow running from one while updating the other), so we solved the problem by always building both and deciding at boot time which to use and which to update.