- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试读取一段 Rust 汇编代码,但实际上,它比 C/C++ 编译器生成的 ASM 代码更难读取。那么,如何分析下面这段Rust代码的ASM代码呢?
fn main() {
let closure = |x| println!("{}", x);
let x: fn(x: i32) -> () = closure;
println!("{}", x as i32);
}
相应的汇编代码如下,带有一些注释(我只粘贴了主要部分,完整版本请使用此固定链接:https://play.rust-lang.org/?version=nightly&mode=release&edition=2018&gist=e7ba4844f1ce6e881912dc074152988d):
playground::main: # @playground::main
# %bb.0:
subq $72, %rsp
leaq core::ops::function::FnOnce::call_once(%rip), %rax
movl %eax, 4(%rsp)
leaq 4(%rsp), %rax
movq %rax, 8(%rsp)
movq core::fmt::num::imp::<impl core::fmt::Display for i32>::fmt@GOTPCREL(%rip), %rax
movq %rax, 16(%rsp)
leaq .L__unnamed_2(%rip), %rax # the contents of rdx come from .L__unnamed_2(%rip), how to evaluate this part?
movq %rax, 24(%rsp) # the contents of rdi come from rax.
movq $2, 32(%rsp)
movq $0, 40(%rsp)
leaq 8(%rsp), %rax
movq %rax, 56(%rsp)
movq $1, 64(%rsp)
leaq 24(%rsp), %rdi # rdi should be the register holding the value passed to println!.
callq *std::io::stdio::_print@GOTPCREL(%rip)
addq $72, %rsp
retq
# -- End function
main: # @main
# %bb.0:
subq $8, %rsp
movq %rsi, %rcx
movslq %edi, %rdx
leaq playground::main(%rip), %rax
movq %rax, (%rsp)
leaq .L__unnamed_1(%rip), %rsi
movq %rsp, %rdi
callq *std::rt::lang_start_internal@GOTPCREL(%rip)
# kill: def $eax killed $eax killed $rax
popq %rcx
retq
# -- End function
.L__unnamed_1:
.quad core::ptr::drop_in_place<std::rt::lang_start<()>::{{closure}}>
.quad 8 # 0x8
.quad 8 # 0x8
.quad std::rt::lang_start::{{closure}}
.quad std::rt::lang_start::{{closure}}
.quad core::ops::function::FnOnce::call_once{{vtable.shim}}
.L__unnamed_3:
.L__unnamed_4:
.byte 10
.L__unnamed_2:
.quad .L__unnamed_3
.zero 8
.quad .L__unnamed_4
.asciz "\001\000\000\000\000\000\000"
而且,我正在尝试找出 Rust 编译器如何处理闭包函数指针和普通函数。因此,在这里我尝试使用闭包作为示例,但似乎找不到任何与变量“x”的使用相对应的有效汇编代码。
最佳答案
没有实际调用闭包,因此没有生成调用代码,但 x 变量的使用实际上是在您的帖子中未包含的函数中,该函数的名称具有误导性 core::ops::function::FnOnce::call_once
在 ASM 输出中,但在同一个 Playground 示例的 LLVM 输出中具有更加困惑的名称 @_ZN4core3ops8function6FnOnce9call_once17hefa1aa47132c4122E
。这是闭包的实际内容(println!("{}", x)
)
core::ops::function::FnOnce::call_once: # @core::ops::function::FnOnce::call_once
# %bb.0:
# allocate a bunch of stack space for variables and print arguments
subq $72, %rsp
# %edi has the value of x passed in to the closure, which we store in a new stack allocated variable
movl %edi, 4(%rsp)
# we then load the address of that variable into another variable
leaq 4(%rsp), %rax
movq %rax, 8(%rsp)
# the following is mostly populating the std::fmt::Arguments struct which is passed to print
movq core::fmt::num::imp::<impl core::fmt::Display for i32>::fmt@GOTPCREL(%rip), %rax
movq %rax, 16(%rsp)
leaq .L__unnamed_2(%rip), %rax
movq %rax, 24(%rsp)
movq $2, 32(%rsp)
movq $0, 40(%rsp)
# the address of the address of x is loaded into the arguments struct here
leaq 8(%rsp), %rax
movq %rax, 56(%rsp)
# finish populating the arguments and then call print
movq $1, 64(%rsp)
leaq 24(%rsp), %rdi
callq *std::io::stdio::_print@GOTPCREL(%rip)
addq $72, %rsp
retq
playground main 函数是创建闭包的地方,但它实际上并未被调用,并且与上面的函数一样,主要填充复杂的 std::fmt::Arguments 结构
playground::main: # @playground::main
# %bb.0:
subq $72, %rsp
# this creates the closure by storing a pointer to the closure's function
leaq core::ops::function::FnOnce::call_once(%rip), %rax
movl %eax, 4(%rsp)
# this stores the closure in main's `x` variable (line 3 of the example)
leaq 4(%rsp), %rax
movq %rax, 8(%rsp)
# populate the std::fmt::Arguments struct
movq core::fmt::num::imp::<impl core::fmt::Display for i32>::fmt@GOTPCREL(%rip), %rax
movq %rax, 16(%rsp)
leaq .L__unnamed_2(%rip), %rax # the contents of rdx come from .L__unnamed_2(%rip), how to evaluate this part?
movq %rax, 24(%rsp) # the contents of rdi come from rax.
movq $2, 32(%rsp)
movq $0, 40(%rsp)
# store the closure (stored in `x`) in the std::fmt::Arguments struct
leaq 8(%rsp), %rax
movq %rax, 56(%rsp)
# finish populating and call print
movq $1, 64(%rsp)
leaq 24(%rsp), %rdi # rdi should be the register holding the value passed to println!.
callq *std::io::stdio::_print@GOTPCREL(%rip)
addq $72, %rsp
retq
从 LLVM 输出中,std::fmt::Arguments 定义为 %"std::fmt::Arguments"= type { [0 x i64], { [0 x { [0 x i8] ]*, i64 }]*, i64 }, [0 x i64], { i64*, i64 }, [0 x i64], { [0 x { i8*, i64* }]*, i64 }, [0 x i64] }
而且我不了解太多的内部细节,所以我不确定为什么它引用静态内存区域 .L__unnamed_2 但深入研究 std::fmt::Arguments可能会提供更多线索
关于assembly - 如何读取这段Rust代码的汇编代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67217977/
我试图在图形模式下打印一个字符。通常当我打印我正在做的一个字符时: mov ah,14 ; ah=14 mov al,'x' int 10h ; print the character 这
我试图通过更改其中的一个字节来修改存储在内存中的字符串。我为此使用了 movb,但由于某种原因,给定内存位置的字节没有改变。 在 gdb 调试器上: 14 movb %al, (%r10) # nex
我一直在阅读一些汇编代码,并且开始发现调用指令实际上是与程序计数器相关的。 但是,每当我使用 Visual Studio 或 Windbg 进行调试时,它总是显示 call 0xFFFFFF ...这
我最近一直在使用 Visual C++ 中的内联汇编,我想知道是否可以直接向堆栈上的局部变量添加值,例如: push 5 add [esp], 7 这样做可以吗?我问这个问题是因为我在执行此操作时随机
我有下一个代码: mov al, -5 add al, 132 add al, 1 据我检查,溢出标志和进位标志将在第一个操作中设置,而在第二个操作中,仅设置溢出。 但我不明白为什么: 在无符号数中,
在 64 位 x86 汇编 nasm 中,如何将单个字节从寄存器移动到 .data 节中定义的内存位置? 我知道这有效 global _main section .data quotient db 0
我的汇编代码有问题。我想打印存储在寄存器 cx 中的数字,但是当我尝试打印它时,它打印的是 ascii 字符而不是 ascii 数字,所以我决定编写一个程序将 ascii char 转换为 ascii
为什么第 1B 行的跳转指令(例如)变成了 EBBD? 我知道“jmp”= EB但是BD是怎么计算的呢? 最佳答案 短跳转使用一个带符号的偏移量添加到 JMP 之后的指令地址。 例如,第一个 JMP
以下两者有什么区别: mov eax, [eax+4] 和 add eax, 4 mov eax, [eax] 如果不是,那么汇编器是否会选择哪个来进行某种优化? 最佳答案 这
看《The Shellcoder's Handbook》中的一些汇编和反汇编代码,发现一条指令的序列操作数是不一样的。 例如,在 assembly 上: mov ebx,0 并且,在反汇编时: mov
我有这个非常简单的汇编代码: start: add ax, 100 ; if ax overflow add to bx 1 jmp start 但我不知道如何检测 ax 寄存器溢出,有人可以帮
在 64 位 x86 汇编 nasm 中,如何将单个字节从寄存器移动到 .data 节中定义的内存位置? 我知道这有效 global _main section .data quotient db 0
我的汇编代码有问题。我想打印存储在寄存器 cx 中的数字,但是当我尝试打印它时,它打印的是 ascii 字符而不是 ascii 数字,所以我决定编写一个程序将 ascii char 转换为 ascii
我正在学习一些关于操作系统开发的教程,我发现了一篇关于多重引导 header 。这些是您必须定义的一些“神奇”值才能使用GRUB2。这些是命令: # Declare constants used f
为什么第 1B 行的跳转指令(例如)变成了 EBBD? 我知道“jmp”= EB但是BD是怎么计算的呢? 最佳答案 短跳转使用一个带符号的偏移量添加到 JMP 之后的指令地址。 例如,第一个 JMP
我正在尝试从内存中复制一些单词并使用汇编将其保存到另一个内存地址。我正在尝试为其编写代码,但我不确定其中的某些部分。我将简要描述我想要做什么。 源地址、目标地址和要复制的字数是函数的输入参数。 最佳答
当我们想要像这样创建一个初始化变量时: name db 'zara ali' 我们创建了一个字节大小变量,但我们在其中存储了一个字符串 这怎么可能?? 当我们使用这条指令时: MOV ecx, nam
我还是汇编的新手,我还不知道汇编中的许多命令代码。我想在 16 位寄存器中进行除法。我想打印它的内容。我知道我需要将寄存器的内容转换为 ASCII 进行打印,但同样,我的问题是除法。请帮我。 比如cx
使用有什么区别: c.eq.s $1, $2 bc1t L2 并使用: beq $1, $2, L2 如果他们做同样的事情,为什么有两种分支方式?如果它们不同,那么它们各自的好处是什么
源代码: int main() { int i; for(i=0, i : push rbp 2. 0x000055555555463b :
我是一名优秀的程序员,十分优秀!