This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

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?

Freddy

Parents
  • Hello.

    Fortunately, I solved this problem yesterday.

    A workaround is to use "-fPIC" + "-mno-pic-data-is-text-relative" for the compile option and add "-shared" + "-symbolic" for the Link option.

    Of course, by using "-shared", ELF was changed to a shared object file rather than an executable, but

    The desired Dynamic Segment and Relocation Section have been created,

    We were able to proceed with relocation using this.

    Additionally, there is a symptom that the binary size increases as external APIs are included in the ELF.

    And, I didn't process the relocation information that I couldn't understand.

    I'll leave an answer for your reference to the same problem as me in the future.

    Good work, and have a nice day today.​

Reply
  • Hello.

    Fortunately, I solved this problem yesterday.

    A workaround is to use "-fPIC" + "-mno-pic-data-is-text-relative" for the compile option and add "-shared" + "-symbolic" for the Link option.

    Of course, by using "-shared", ELF was changed to a shared object file rather than an executable, but

    The desired Dynamic Segment and Relocation Section have been created,

    We were able to proceed with relocation using this.

    Additionally, there is a symptom that the binary size increases as external APIs are included in the ELF.

    And, I didn't process the relocation information that I couldn't understand.

    I'll leave an answer for your reference to the same problem as me in the future.

    Good work, and have a nice day today.​

Children
No data