riscv objdump -d 的输出不清楚

发布于 2025-01-10 11:05:10 字数 1900 浏览 4 评论 0原文

现在我正在尝试了解 RISC-V ISA,但我对机器代码和汇编有不清楚的地方。

我编写了如下的 C 代码:

int main() {
    return 42;
}

然后,我通过以下命令生成了 .s 文件:

$ /opt/riscv/bin/riscv64-unknown-linux-gnu-gcc -S 42.c

输出是:

        .file   "42.c"
        .option nopic
        .text
        .align  1
        .globl  main
        .type   main, @function
main:
        addi    sp,sp,-16
        sd      s0,8(sp)
        addi    s0,sp,16
        li      a5,42
        mv      a0,a5
        ld      s0,8(sp)
        addi    sp,sp,16
        jr      ra
        .size   main, .-main
        .ident  "GCC: (g5964b5cd727) 11.1.0"
        .section        .note.GNU-stack,"",@progbits

现在,我运行以下命令来生成一个精灵。

$ /opt/riscv/bin/riscv64-unknown-linux-gnu-gcc -nostdlib -o 42 42.s

这样,就生成了一个二进制文件。我尝试通过 objdump 读取该内容,如下所示:

$ /opt/riscv/bin/riscv64-unknown-linux-gnu-objdump -d 42

所以输出如下:


42:     file format elf64-littleriscv


Disassembly of section .text:

00000000000100b0 <main>:
   100b0:       1141                    addi    sp,sp,-16
   100b2:       e422                    sd      s0,8(sp)
   100b4:       0800                    addi    s0,sp,16
   100b6:       02a00793                li      a5,42
   100ba:       853e                    mv      a0,a5
   100bc:       6422                    ld      s0,8(sp)
   100be:       0141                    addi    sp,sp,16
   100c0:       8082                    ret

我不明白是objdump输出中机器码的含义。 例如第一条指令addi根据此页面,(虽然这不是官方规范)。然而,转储的十六进制是11411141只能表示2个字节,但指令应该是32位,4个字节。

我想我遗漏了一些点,但是我应该如何读取 riscv 的 objdump 的输出?

Now I am trying to understand the RISC-V ISA but I have an unclear point about the machine code and assembly.

I have written a C code like this:

int main() {
    return 42;
}

Then, I produced the .s file by this command:

$ /opt/riscv/bin/riscv64-unknown-linux-gnu-gcc -S 42.c

The output was:

        .file   "42.c"
        .option nopic
        .text
        .align  1
        .globl  main
        .type   main, @function
main:
        addi    sp,sp,-16
        sd      s0,8(sp)
        addi    s0,sp,16
        li      a5,42
        mv      a0,a5
        ld      s0,8(sp)
        addi    sp,sp,16
        jr      ra
        .size   main, .-main
        .ident  "GCC: (g5964b5cd727) 11.1.0"
        .section        .note.GNU-stack,"",@progbits

Now, I run following command to produce an elf.

$ /opt/riscv/bin/riscv64-unknown-linux-gnu-gcc -nostdlib -o 42 42.s

So, a binary file is produced. I tried to read that by objdump like this:

$ /opt/riscv/bin/riscv64-unknown-linux-gnu-objdump -d 42

So the output was like this:


42:     file format elf64-littleriscv


Disassembly of section .text:

00000000000100b0 <main>:
   100b0:       1141                    addi    sp,sp,-16
   100b2:       e422                    sd      s0,8(sp)
   100b4:       0800                    addi    s0,sp,16
   100b6:       02a00793                li      a5,42
   100ba:       853e                    mv      a0,a5
   100bc:       6422                    ld      s0,8(sp)
   100be:       0141                    addi    sp,sp,16
   100c0:       8082                    ret

What I don't understand is the meaning of the machine code in objdump output.
For example, the first instruction addi is translated into .....0010011 according to this page, (while this is not an official spec). However, the dumped hex is 1141. 1141 can only represent 2 bytes, but the instruction should be 32-bit, 4bytes.

I guess I am missing some points, but how should I read the output of objdump for riscv?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别把无礼当个性 2025-01-17 11:05:10

您可以告诉 objdump 显示压缩的(16 位)以这种方式使用-M no-aliases的指令

riscv64-unknown-elf-objdump -d -M no-aliases

在这种情况下,以c.开头的指令是压缩指令。

不幸的是,这也会禁用一些其他别名,如果您习惯了它们,那么汇编会变得不太好读。您只需查看十六进制转储中的字节数(2 与 4)即可确定它是否是压缩指令。

You can tell objdump to show compressed (16-bit) instructions by using -M no-aliases in this way

riscv64-unknown-elf-objdump -d -M no-aliases

In that case, instructions starting with c. are compressed ones.

Unfortunately that will also disable some other aliases, making the asm less nice to read if you're used to them. You can just look at the number of bytes (2 vs. 4) in the hexdump to see if it's a compressed instruction or not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文