为什么文字值在放入堆之前先从只读内存复制到堆栈?
Why are literal values copied from read-only memory to the stack before being placed in the heap?
当我尝试使用 Rust 时,我注意到如果盒装数组是从带有 Box::new
的文字创建的,则会从只读内存额外复制到堆栈。为什么 Rust 编译器不能直接从只读内存复制到堆上分配的 space?
Rust代码片段:
fn main() {
let v: Box<[u32]> = Box::new([1u32,2,3456,4,5,6]);
println!("vector {}", v.len());
}
合适的汇编片段:
00000000000049d4 mov rax, qword [ds:0x39ad0]
00000000000049db mov qword [ss:rsp+arg_F8], rax
00000000000049e3 movaps xmm0, xmmword [ds:const1050]
00000000000049ea movaps xmmword [ss:rsp+arg_E8], xmm0
00000000000049f2 mov ecx, 0x18
00000000000049f7 mov edi, ecx ; argument #1 for method _ZN4heap15exchange_malloc20h356508549306a536JeaE
00000000000049f9 mov ecx, 0x4
00000000000049fe mov esi, ecx ; argument #2 for method _ZN4heap15exchange_malloc20h356508549306a536JeaE
0000000000004a00 call _ZN4heap15exchange_malloc20h356508549306a536JeaE ; heap::exchange_malloc::h356508549306a536Jea
0000000000004a05 mov rsi, qword [ss:rsp+arg_F8]
0000000000004a0d mov qword [ds:rax+0x10], rsi
0000000000004a11 movaps xmm0, xmmword [ss:rsp+arg_E8]
0000000000004a19 movups xmmword [ds:rax], xmm0
数组驻留在二进制文件中的 0x39ad0
和 const1050
处:
0000000000039ac8 db 0x80 ; '.'
0000000000039ac9 db 0x0d ; '.'
0000000000039aca db 0x00 ; '.'
0000000000039acb db 0x00 ; '.'
0000000000039acc db 0x04 ; '.'
0000000000039acd db 0x00 ; '.'
0000000000039ace db 0x00 ; '.'
0000000000039acf db 0x00 ; '.'
0000000000039ad0 dq 0x0000000600000005 ; XREF=_ZN4main20h90e2c514439f0097eaaE+52
当我尝试使用 Rust 时,我注意到如果盒装数组是从带有 Box::new
的文字创建的,则会从只读内存额外复制到堆栈。为什么 Rust 编译器不能直接从只读内存复制到堆上分配的 space?
Rust代码片段:
fn main() {
let v: Box<[u32]> = Box::new([1u32,2,3456,4,5,6]);
println!("vector {}", v.len());
}
合适的汇编片段:
00000000000049d4 mov rax, qword [ds:0x39ad0]
00000000000049db mov qword [ss:rsp+arg_F8], rax
00000000000049e3 movaps xmm0, xmmword [ds:const1050]
00000000000049ea movaps xmmword [ss:rsp+arg_E8], xmm0
00000000000049f2 mov ecx, 0x18
00000000000049f7 mov edi, ecx ; argument #1 for method _ZN4heap15exchange_malloc20h356508549306a536JeaE
00000000000049f9 mov ecx, 0x4
00000000000049fe mov esi, ecx ; argument #2 for method _ZN4heap15exchange_malloc20h356508549306a536JeaE
0000000000004a00 call _ZN4heap15exchange_malloc20h356508549306a536JeaE ; heap::exchange_malloc::h356508549306a536Jea
0000000000004a05 mov rsi, qword [ss:rsp+arg_F8]
0000000000004a0d mov qword [ds:rax+0x10], rsi
0000000000004a11 movaps xmm0, xmmword [ss:rsp+arg_E8]
0000000000004a19 movups xmmword [ds:rax], xmm0
数组驻留在二进制文件中的 0x39ad0
和 const1050
处:
0000000000039ac8 db 0x80 ; '.'
0000000000039ac9 db 0x0d ; '.'
0000000000039aca db 0x00 ; '.'
0000000000039acb db 0x00 ; '.'
0000000000039acc db 0x04 ; '.'
0000000000039acd db 0x00 ; '.'
0000000000039ace db 0x00 ; '.'
0000000000039acf db 0x00 ; '.'
0000000000039ad0 dq 0x0000000600000005 ; XREF=_ZN4main20h90e2c514439f0097eaaE+52