Fault when running PIC

Asked by Rohit Gujarathi on 2020-03-17

I have been exploring building PIC using the gcc toolchain and I think there is a bug.

I first build a shared library using
arm-none-eabi-gcc -shared -mcpu=cortex-m4 -mthumb -Wall -g -fPIC -fmessage-length=0 --specs=nosys.specs mylib.c -o libmylib.so

and then link to my code using the following
arm-none-eabi-gcc -x assembler-with-cpp -c -O0 -mcpu=cortex-m4 -mthumb -Wall -fmessage-length=0 -fPIC startup.S -o startup.o
arm-none-eabi-gcc -c -mcpu=cortex-m4 -mthumb -Wall -g -fmessage-length=0 --specs=nosys.specs -fPIC main.c -o main.o
arm-none-eabi-ld startup.o main.o -g -nostdlib -T./stm32_linker.ld -L. -lmylib -o main.elf

Full code is at https://github.com/rgujju/STM32-projects/tree/master/got_plt , generate the required code using 'make got_plt' or view the disassembly and gdb output at https://community.arm.com/developer/ip-products/processors/f/cortex-m-forum/45919/gcc-does-not-generate-correct-code-while-building-pic

What I expect is the function call to <library_function> to go through the GOT and PLT and eventually go into the .dynamic section. I understand that i will have to build a dynamic linker for the actual call to <library_function> work but i just want to check if it runs till this point correctly first. But the code gives a fault at 0x800010c. The branch to the instruction at 0x800010c is from 0x8000126. The value of ip at 0x8000126 is 0x200000a0, hence the PC gets loaded with 0x800010c. The CFSR value after the fault is 0x00020001 which is IACCVIOL and INVSTATE. INVSTATE occurs when the last bit is not set to 1 for a thumb2 instruction. So I changed the 0x800010c to 0x800010d and it seems to work fine.

So my question is, is this a bug in gcc or am i doing something wrong? I am using arm gnu toolchain 9-2019-q4-major

Thanks a lot for your help.

Question information

Language:
English Edit question
Status:
Open
For:
GNU Arm Embedded Toolchain Edit question
Assignee:
No assignee Edit question
Last query:
2020-03-17
Last reply:

Can you help with this problem?

Provide an answer of your own, or ask Rohit Gujarathi for more information if necessary.

To post a message you must log in.