riscv objdump -d 的输出不清楚
现在我正在尝试了解 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
根据此页面,(虽然这不是官方规范)。然而,转储的十六进制是1141
。 1141
只能表示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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以告诉 objdump 显示压缩的(16 位)以这种方式使用
-M no-aliases
的指令在这种情况下,以
c.
开头的指令是压缩指令。不幸的是,这也会禁用一些其他别名,如果您习惯了它们,那么汇编会变得不太好读。您只需查看十六进制转储中的字节数(2 与 4)即可确定它是否是压缩指令。
You can tell objdump to show compressed (16-bit) instructions by using
-M no-aliases
in this wayIn 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.