使用汇编应用程序关闭 Linux 时出现分段错误
以下应用程序在执行时会生成分段错误:
.set __NR_reboot, 169
.set LINUX_REBOOT_CMD_POWER_OFF, 0x4321FEDC
.section .text
.globl _start
_start:
movl $LINUX_REBOOT_CMD_POWER_OFF, %ebx
movl $__NR_reboot, %eax
int $0x80
这是一个非常简单的应用程序,我一定错过了一些非常明显的东西。有人可以帮助我吗?
它是用以下内容编译的:
as shutdown.s -o shutdown.o
ld shutdown.o -o shutdown
编辑:
即使是一个仅调用系统调用sync()的简单应用程序也会生成分段错误:
.set __NR_sync, 36
.section .text
.globl _start
_start:
movl $__NR_sync, %eax
int $0x80
movl $1, %eax #syscall exit
movl $0, %eax
int $0x80
The following application generates a Segmentation Fault when executed:
.set __NR_reboot, 169
.set LINUX_REBOOT_CMD_POWER_OFF, 0x4321FEDC
.section .text
.globl _start
_start:
movl $LINUX_REBOOT_CMD_POWER_OFF, %ebx
movl $__NR_reboot, %eax
int $0x80
It's a quite simple application and I must be missing something really obvious. Can someone help me?
It was compiled with:
as shutdown.s -o shutdown.o
ld shutdown.o -o shutdown
EDIT:
Even a simple application that just calls syscall sync() generates a Segmentation Fault:
.set __NR_sync, 36
.section .text
.globl _start
_start:
movl $__NR_sync, %eax
int $0x80
movl $1, %eax #syscall exit
movl $0, %eax
int $0x80
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
警告:请记住在调用
reboot(2)
之前先sync(2)
。reboot(2)
系统调用需要 4 个参数。您将其与 libc 混淆了包装纸。
警告:请记住在调用
reboot(2)
之前先sync(2)
。(它实际上采用magic*参数,以便人们必须重新阅读文档并且不要忘记调用
sync(2)
。)警告 :我有说过在调用
reboot(2)
之前必须先sync(2)
吗?WARNING: remember to
sync(2)
before callingreboot(2)
.The
reboot(2)
system call takes 4 parameters.You are confusing it with the libcwrapper.
WARNING: remember to
sync(2)
before callingreboot(2)
.(It actually takes the magic* parameters so that people have to reread the documentation and don't forget calling
sync(2)
.)WARNING: Did I say that you have to
sync(2)
before callingreboot(2)
?我正在添加final &工作源代码,因为这个问题将来可能会引起某人的兴趣:
I'm adding the final & working source code as this question might interest somebody in the future:
来自 linux/i386/ syscall.S:函数编号应该是放置在 %eax 中,所有参数按顺序放置在以下寄存器中:%ebx、%ecx、%edx、%esi、%edi 和 %ebp。
这就是为什么代码中最后一个
movl %eax,0
应更改为movl %ebx, 0
。From linux/i386/syscall.S: The function number should be placed in %eax and any arguments in the following registers in order: %ebx, %ecx, %edx, %esi, %edi, and %ebp.
Which is why the last
movl %eax,0
in the code should be changed tomovl %ebx, 0
.