I need a position-independent code (PIC) running on the STM32F401. But I have a problem with pointers to the functions used, for example. in structure.
A brief example:
struct process {
struct process *next;
const char *name;
PT_THREAD((* thread)(struct pt *, process_event_t, process_data_t));
struct pt pt;
unsigned char state, needspoll;
};
process etimer_process...
static void call_process(struct process *p, process_event_t ev, process_data_t data) {
int ret;
ret = p->thread(&p->pt, ev, data);
}
After assembly:
Disassembly of section .data:
...
20000768 <etimer_process>:
20000768: 00000000 andeq r0, r0, r0
2000076c: 0803b134 stmdaeq r3, {r2, r4, r5, r8, ip, sp, pc}
20000770: 08027435 stmdaeq r2, {r0, r2, r4, r5, sl, ip, sp, lr}
20000774: 00000000 andeq r0, r0, r0
...
Disassembly of section .text:
...
8027da4: 68fb ldr r3, [r7, #12] //R3 = 0x20000768
8027da6: 689b ldr r3, [r3, #8] //R3 = 0x8027435
...
8027db2: 4798 blx r3 //Branch to R3
...
The branch address in R3 is incorrect for code with an offset other than 0. I do not see the use of GOT here. Is this a bug or a missing compiler / linker option?
Things I probably successfully decided:
- Got fixup
- Fix interrupt table
- Newlib compiled with corresponding flags
Used CFLAGS:
-mlittle-endian -mthumb -mthumb-interwork -mcpu = cortex-m4 -fsingle-precision-constant -Wdouble-promotion -msoft-float -fpic -msingle-pic-base -mpic-data-is-text- relative -mpic -register = r10 -Wno-strict-aliasing -lc
and binding to
-fpic