Im trying to compile bland c code to an elf format executable by a RV32i processor.
However when I enable optimizations (-Os or -O2) the resulting assembly contains compressed Instructions which I can not execute. All my selfmade code is being compiled into normal rv32i instructions, only the helper functions* are being compiled to C-Instructions. How can I disable C-Instructions completely?
With helper functions I mean __riscv_save_0, __riscv_restore_0, __riscv_save_4, ...
I compile code using
riscv32-unknown-elf-gcc $(CFLAGS) -T link.ld $(SRCS) -o $@ -lgcc
with CFLAGS being:
-g -O2 -msave-restore -mabi=ilp32 -march=rv32i --specs=nosys.specs -nostdlib -Wall -Wl,-gc-sections -ffunction-sections -ffreestanding -fno-builtin
The Disassembly looks like this - with optimizations on: (all my code for some reason without C-instructions and all the helper functions with C-instructions)
00000174 <__riscv_save_0>:
174: 1141 addi sp,sp,-16
176: c04a sw s2,0(sp)
178: c226 sw s1,4(sp)
17a: c422 sw s0,8(sp)
17c: c606 sw ra,12(sp)
17e: 8282 jr t0
...
00000198 <__riscv_restore_0>:
198: 4902 lw s2,0(sp)
19a: 4492 lw s1,4(sp)
19c: 4422 lw s0,8(sp)
19e: 40b2 lw ra,12(sp)
1a0: 0141 addi sp,sp,16
1a2: 8082 ret
...
000001c4 <main>:
1c4: fb1ff2ef jal t0,174 <__riscv_save_0>
1c8: 034000ef jal ra,1fc <func_a>
1cc: 00000537 lui a0,0x0
1d0: 00000613 li a2,0
1d4: 00000593 li a1,0
1d8: 1a450513 addi a0,a0,420
1dc: 098000ef jal ra,274 <func_b>
1e0: 00000537 lui a0,0x0
1e4: 00000613 li a2,0
1e8: 00000593 li a1,0
1ec: 1b450513 addi a0,a0,436
1f0: 084000ef jal ra,274 <func_b>
1f4: 154000ef jal ra,348 <func_c>
1f8: fa1ff06f j 198 <__riscv_restore_0>
Ok, I found an answer to my problem.
While compiling you can change what architecture to use. But the libraries used in the build are generated whilst building the toolchain.
Thus I made the mistake of building the Risc-V toolchain with the RV32IC architecture but wanted them to be used in a RV32I project only.
So the workaround is to simply recompile the toolchain with RV32I.