Can gcc be made to generate PIC code where the data and bss are relocatable and are not known at compile time?
I'm trying to make on-the fly loadable modules work using an RTOS. What I'd like to be able to do is to be able to, at runtime, load the module to an arbitrary address. This is easy to do for things like the .text section, by just having the compiler generate position independent code. I am also compiling with a -msingle-pic-base, as this is the method the RTOS expects to be used.
It is a fairly easy proposition have the data section loaded to an arbitrary address: if I'm compiling PIC code to a different address than I gave to the linker, I can simply fixup the GOT before starting the module. The issue I'm seeing is that variables declared as 'static' are not being looked up through the GOT, but rather by adding a the PC to a fixed offset. To provide an example, here is how a non static variable is loaded (please excuse the formatting):
87c3e: 4b05 ldr r3, [pc, #20] ; load at address 875c4
87c40: f859 3003 ldr.w r3, [r9, r3]
; ... code that is not important ...
87c48: 60da str r2, [r3, #12]
; ...
87c54: 00003f70 .word 0x00003f70
Here, r9 points to the GOT. Everything will be fine, as long as we have the GOT pointing to the right place. We load the offset, which is PC relative, load from the GOT using the static base r9, and then use the real address, pointing into the .data section, which is in r3.
Here is how a static variable is loaded:
87c28: 4b08 ldr r3, [pc, #32] ; load at address 87c4c
87c2a: 447b add r3, pc
; ... code that is not important ...
87c30: 609a str r2, [r3, #8]
; ....
87c4c: 1ff943de .word 0x1ff943de
Here, we're loading an offset from the PC, which is itself PC relative, then add that to the PC and place it in r3. Now r3 points directly into the .data section.
The problem in the case of the static variable is that it is now not (easily) possible to load the data section to an arbitrary area of memory as we no longer have just the GOT to fixup to do and we have things we'd like to change that are now in the .text section.
My question: is this intended behavior? Is it a bug? The behavior with statics seems to not play very well with the concept of using the static base register to point to one's data.
Question information
- Language:
- English Edit question
- Status:
- Solved
- Assignee:
- No assignee Edit question
- Solved by:
- Joshua Goldstein
- Solved:
- Last query:
- Last reply: