从 riscv-gnu-toolchain 生成的 s 文件没有 运行 RISCV 模拟器

s-files generated from riscv-gnu-toolchain does not run RISCV emulator

我必须将一组 C++ 文件编译成 RISC-V 代码 (RV32IM)。我正在使用 riscv-gnu-toolchain 并使用以下 CMakeLists.txt 文件

进行编译
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++)

如果我输入一个测试 C++ 文件,如下所示

#include <iostream>

using namespace std;

int main() {
    cout << "Hello, world!" << endl;
}

然后它会生成以下 .s 文件

    .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"

如果我在 RISCV 模拟器中尝试 运行 这个(例如 Jupiter or BRISC-V),那么我似乎没有被识别为有效的 RISC-V 代码。

如果我尝试 运行 和 riscv32-unknown-elf-run,它会 运行 成功。

我做错了什么?

更新 1:添加错误消息

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

你唯一做错的是你使用了两个不同的汇编程序。 g++ 工具链使用 gas,它最多使用 AT&T 语法,而我不熟悉的 Jupiter 使用另一种语法。

如果它们之间有任何兼容性桥梁,那么这是您唯一可以遵循的方法。如果没有兼容性,则必须选择一方。

如果您选择 g++ 又名 Gnu Compiler Collection,那么可以使用一个模拟器,Qemu

如果您选择 Jupiter,我认为您必须使用 Jupiter 的语法编写汇编。