I must compile a set of C++ files into RISC-V code (RV32IM). I am using riscv-gnu-toolchain and compiles with the following CMakeLists.txt file
cmake_minimum_required(VERSION 3.10.2)
project(project-title)
set(CMAKE_CXX_FLAGS "-march=rv32im -mabi=ilp32 --save-temps")
set(CMAKE_CXX_COMPILER riscv32-unknown-elf-g++)
If I input a test C++ file, like the following
#include <iostream>
using namespace std;
int main() {
cout << "Hello, world!" << endl;
}
then it produces the following .s
file
.file "test.cpp"
.option nopic
.attribute arch, "rv32i2p0_m2p0"
.attribute unaligned_access, 0
.attribute stack_align, 16
.text
.section .srodata,"a"
.align 2
.type _ZStL19piecewise_construct, @object
.size _ZStL19piecewise_construct, 1
_ZStL19piecewise_construct:
.zero 1
.text
.align 2
.type _ZL20__gthread_key_deletei, @function
_ZL20__gthread_key_deletei:
.LFB459:
.cfi_startproc
addi sp,sp,-32
.cfi_def_cfa_offset 32
sw s0,28(sp)
.cfi_offset 8, -4
addi s0,sp,32
.cfi_def_cfa 8, 0
sw a0,-20(s0)
li a5,0
mv a0,a5
lw s0,28(sp)
.cfi_restore 8
.cfi_def_cfa 2, 32
addi sp,sp,32
.cfi_def_cfa_offset 0
jr ra
.cfi_endproc
.LFE459:
.size _ZL20__gthread_key_deletei, .-_ZL20__gthread_key_deletei
.local _ZStL8__ioinit
.comm _ZStL8__ioinit,1,4
.section .rodata
.align 2
.LC0:
.string "Hello, world!"
.text
.align 2
.globl main
.type main, @function
main:
.LFB1492:
.cfi_startproc
addi sp,sp,-16
.cfi_def_cfa_offset 16
sw ra,12(sp)
sw s0,8(sp)
.cfi_offset 1, -4
.cfi_offset 8, -8
addi s0,sp,16
.cfi_def_cfa 8, 0
lui a5,%hi(.LC0)
addi a1,a5,%lo(.LC0)
lui a5,%hi(_ZSt4cout)
addi a0,a5,%lo(_ZSt4cout)
call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
mv a4,a0
lui a5,%hi(_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_)
addi a1,a5,%lo(_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_)
mv a0,a4
call _ZNSolsEPFRSoS_E
li a5,0
mv a0,a5
lw ra,12(sp)
.cfi_restore 1
lw s0,8(sp)
.cfi_restore 8
.cfi_def_cfa 2, 16
addi sp,sp,16
.cfi_def_cfa_offset 0
jr ra
.cfi_endproc
.LFE1492:
.size main, .-main
.align 2
.type _Z41__static_initialization_and_destruction_0ii, @function
_Z41__static_initialization_and_destruction_0ii:
.LFB1981:
.cfi_startproc
addi sp,sp,-32
.cfi_def_cfa_offset 32
sw ra,28(sp)
sw s0,24(sp)
.cfi_offset 1, -4
.cfi_offset 8, -8
addi s0,sp,32
.cfi_def_cfa 8, 0
sw a0,-20(s0)
sw a1,-24(s0)
lw a4,-20(s0)
li a5,1
bne a4,a5,.L7
lw a4,-24(s0)
li a5,65536
addi a5,a5,-1
bne a4,a5,.L7
lui a5,%hi(_ZStL8__ioinit)
addi a0,a5,%lo(_ZStL8__ioinit)
call _ZNSt8ios_base4InitC1Ev
lui a5,%hi(__dso_handle)
addi a2,a5,%lo(__dso_handle)
lui a5,%hi(_ZStL8__ioinit)
addi a1,a5,%lo(_ZStL8__ioinit)
lui a5,%hi(_ZNSt8ios_base4InitD1Ev)
addi a0,a5,%lo(_ZNSt8ios_base4InitD1Ev)
call __cxa_atexit
.L7:
nop
lw ra,28(sp)
.cfi_restore 1
lw s0,24(sp)
.cfi_restore 8
.cfi_def_cfa 2, 32
addi sp,sp,32
.cfi_def_cfa_offset 0
jr ra
.cfi_endproc
.LFE1981:
.size _Z41__static_initialization_and_destruction_0ii, .-_Z41__static_initialization_and_destruction_0ii
.align 2
.type _GLOBAL__sub_I_main, @function
_GLOBAL__sub_I_main:
.LFB1982:
.cfi_startproc
addi sp,sp,-16
.cfi_def_cfa_offset 16
sw ra,12(sp)
sw s0,8(sp)
.cfi_offset 1, -4
.cfi_offset 8, -8
addi s0,sp,16
.cfi_def_cfa 8, 0
li a5,65536
addi a1,a5,-1
li a0,1
call _Z41__static_initialization_and_destruction_0ii
lw ra,12(sp)
.cfi_restore 1
lw s0,8(sp)
.cfi_restore 8
.cfi_def_cfa 2, 16
addi sp,sp,16
.cfi_def_cfa_offset 0
jr ra
.cfi_endproc
.LFE1982:
.size _GLOBAL__sub_I_main, .-_GLOBAL__sub_I_main
.section .init_array,"aw"
.align 2
.word _GLOBAL__sub_I_main
.hidden __dso_handle
.ident "GCC: (GNU) 9.2.0"
If I try to run this in an RISCV emulator (for example Jupiter or BRISC-V), then I doesn't seem to be recognized as valid RISC-V code.
If I try to run with riscv32-unknown-elf-run
, it runs successfully.
What am I doing wrong?
Update 1: Adding error messages
file '(...)/test.s' has errors
Jupiter: (error) asm::2:10: (syntax) unexpected input 'nopic'
> .option nopic
^
Jupiter: (error) asm::7:11: (syntax) unexpected input '.srodata'
> .section .srodata,"a"
^
Jupiter: (error) asm::9:8: (syntax) unexpected input '_ZStL19piecewise_construct'
> .type _ZStL19piecewise_construct, @object
^
Jupiter: (error) asm::12:8: .zero directive can not appear in .text segment
> .zero 1
^
Jupiter: (error) asm::15:8: (syntax) unexpected input '_ZL20__gthread_key_deletei'
> .type _ZL20__gthread_key_deletei, @function
^
Jupiter: (error) asm::17:8: invalid language element: ':'
> .LFB459:
^
Jupiter: (error) asm::18:2: invalid directive '.cfi_startproc'
> .cfi_startproc
^
Jupiter: (error) asm::20:22: (syntax) unexpected input '32'
> .cfi_def_cfa_offset 32
^
Jupiter: (error) asm::22:14: (syntax) unexpected input '8'
> .cfi_offset 8, -4
^
Jupiter: (error) asm::24:15: (syntax) unexpected input '8'
> .cfi_def_cfa 8, 0
^
Jupiter: (error) asm::29:15: (syntax) unexpected input '8'
> .cfi_restore 8
^
Jupiter: (error) asm::32:22: (syntax) unexpected input '0'
> .cfi_def_cfa_offset 0
^
Jupiter: (error) asm::34:2: invalid directive '.cfi_endproc'
> .cfi_endproc
^
Jupiter: (error) asm::35:8: invalid language element: ':'
> .LFE459:
^
Jupiter: (error) asm::41:5: invalid language element: ':'
> .LC0:
^
Jupiter: (error) asm::46:8: (syntax) unexpected input 'main'
> .type main, @function
^
Jupiter: (error) asm::48:9: invalid language element: ':'
> .LFB1492:
^
Jupiter: (error) asm::49:2: invalid directive '.cfi_startproc'
> .cfi_startproc
^
Jupiter: (error) asm::51:22: (syntax) unexpected input '16'
> .cfi_def_cfa_offset 16
^
Jupiter: (error) asm::54:14: (syntax) unexpected input '1'
> .cfi_offset 1, -4
^
Jupiter: (error) asm::57:15: (syntax) unexpected input '8'
> .cfi_def_cfa 8, 0
^
Jupiter: (error) asm::58:13: (syntax) unexpected input '.LC0'
> lui a5,%hi(.LC0)
^
Jupiter: (error) asm::59:17: (syntax) unexpected input '.LC0'
> addi a1,a5,%lo(.LC0)
^
Jupiter: (error) asm::71:15: (syntax) unexpected input '1'
> .cfi_restore 1
^
Jupiter: (error) asm::73:15: (syntax) unexpected input '8'
> .cfi_restore 8
^
Jupiter: (error) asm::76:22: (syntax) unexpected input '0'
> .cfi_def_cfa_offset 0
^
Jupiter: (error) asm::78:2: invalid directive '.cfi_endproc'
> .cfi_endproc
^
Jupiter: (error) asm::79:9: invalid language element: ':'
> .LFE1492:
^
Jupiter: (error) asm::82:8: (syntax) unexpected input '_Z41__static_initialization_and_destruction_0ii'
> .type _Z41__static_initialization_and_destruction_0ii, @function
^
Jupiter: (error) asm::84:9: invalid language element: ':'
> .LFB1981:
^
Jupiter: (error) asm::85:2: invalid directive '.cfi_startproc'
> .cfi_startproc
^
Jupiter: (error) asm::87:22: (syntax) unexpected input '32'
> .cfi_def_cfa_offset 32
^
Jupiter: (error) asm::90:14: (syntax) unexpected input '1'
> .cfi_offset 1, -4
^
Jupiter: (error) asm::93:15: (syntax) unexpected input '8'
> .cfi_def_cfa 8, 0
^
Jupiter: (error) asm::98:12: (syntax) unexpected input '.L7'
> bne a4,a5,.L7
^
Jupiter: (error) asm::102:12: (syntax) unexpected input '.L7'
> bne a4,a5,.L7
^
Jupiter: (error) asm::113:4: invalid language element: ':'
> .L7:
^
Jupiter: (error) asm::116:15: (syntax) unexpected input '1'
> .cfi_restore 1
^
Jupiter: (error) asm::118:15: (syntax) unexpected input '8'
> .cfi_restore 8
^
Jupiter: (error) asm::121:22: (syntax) unexpected input '0'
> .cfi_def_cfa_offset 0
^
Jupiter: (error) asm::123:2: invalid directive '.cfi_endproc'
> .cfi_endproc
^
Jupiter: (error) asm::124:9: invalid language element: ':'
> .LFE1981:
^
Jupiter: (error) asm::127:8: (syntax) unexpected input '_GLOBAL__sub_I_main'
> .type _GLOBAL__sub_I_main, @function
^
Jupiter: (error) asm::129:9: invalid language element: ':'
> .LFB1982:
^
Jupiter: (error) asm::130:2: invalid directive '.cfi_startproc'
> .cfi_startproc
^
Jupiter: (error) asm::132:22: (syntax) unexpected input '16'
> .cfi_def_cfa_offset 16
^
Jupiter: (error) asm::135:14: (syntax) unexpected input '1'
> .cfi_offset 1, -4
^
Jupiter: (error) asm::138:15: (syntax) unexpected input '8'
> .cfi_def_cfa 8, 0
^
Jupiter: (error) asm::144:15: (syntax) unexpected input '1'
> .cfi_restore 1
^
Jupiter: (error) asm::146:15: (syntax) unexpected input '8'
> .cfi_restore 8
^
Jupiter: (error) asm::149:22: (syntax) unexpected input '0'
> .cfi_def_cfa_offset 0
^
Jupiter: (error) asm::151:2: invalid directive '.cfi_endproc'
> .cfi_endproc
^
Jupiter: (error) asm::152:9: invalid language element: ':'
> .LFE1982:
^
Jupiter: (error) asm::156:2: .word directive can not appear in .text segment
> .word _GLOBAL__sub_I_main
^
Jupiter: (error) asm::157:10: (syntax) unexpected input '__dso_handle'
> .hidden __dso_handle
^
Jupiter: (error) simulation halted due to assembly errors
The only thing you are doing wrong is that you are using two different assemblers. The g++
tool-chain uses gas
which uses at most AT&T
syntax and the Jupiter
on which I am not familiar to help with, uses another syntax.
If there is any compatibility bridge between them, then this is the only way you can follow. If there is no compatibility then you have to choose side.
If you choose g++
aka Gnu Compiler Collection
then there is an emulator to work with, Qemu
.
If you choose Jupiter
, I think you have to write in assembly with the Jupiter
's syntax.