Cannot build position independent code that works

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?


More questions in this forum