x86_64 程序集中写入节 .data 的分段错误: [英] Segmentation fault in x86_64 assembly writing into section .data:
问题描述
为什么会出现分段错误?
Why do I get a segmentation fault?
我正在使用 nasm -f elf64 t.asm -o t.o
ld t.o -o t
在 linux 上编译.
I'm using nasm -f elf64 t.asm -o t.o
ld t.o -o t
to compile on linux.
我已经做了所有我能想到的.
I've done all I can think of.
section .data:
variable_int db 1
variable_string db "yaaaa", 10
section .text:
global _start
_start:
mov rax, 1
mov rdi, 1
mov rsi, variable_string
mov rdx, 14
syscall
mov rax, 60
mov rdi, 0
syscall
推荐答案
section .data:
section .text:
省略冒号.节指令不是标签,冒号被解析为节名的一部分.这会导致您的数据被放置在名为 .data:
的部分中,而链接器期望一个名为 .data
的部分没有冒号.这可能会导致该部分被赋予错误的权限(例如,一个不可执行的 .text:
部分).
Omit the colons. A section directive is not a label, and the colon is parsed as part of the section name. This causes your data to be put in a section called .data:
whereas the linker is expecting a section called .data
without the colon. This may cause the section to be given the wrong permissions (e.g. a .text:
section which is not executable).
还有:
mov rdx, 14
这个参数是要写入的数据的长度,你的字符串不是14字节长的;它只有 6 个.这可能会导致写入额外的垃圾(可能是也可能不是可见字符),或者如果遇到未映射的内存,可能会导致系统调用失败.
This parameter is the length of the data to be written, and your string is not 14 bytes long; it is only 6. This may result in extra garbage being written (which may or may not be visible characters) or potentially a failure of the system call if this runs into unmapped memory.
这篇关于x86_64 程序集中写入节 .data 的分段错误:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!