使用汇编应用程序关闭 Linux 时出现分段错误

发布于 2024-11-18 04:41:20 字数 667 浏览 3 评论 0原文

以下应用程序在执行时会生成分段错误

.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

来日方长 2024-11-25 04:41:20

警告:请记住在调用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 calling reboot(2).

The reboot(2) system call takes 4 parameters.You are confusing it with the libc
wrapper.

WARNING: remember to sync(2) before calling reboot(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 calling reboot(2)?

一身软味 2024-11-25 04:41:20

我正在添加final &工作源代码,因为这个问题将来可能会引起某人的兴趣:

                                     # For the right sys_call numbers on your arch,
                                     # check <asm/unistd_32.h> (or unistd_64.h)

.set __NR_sync, 36                   # sys_call sync()    
.set __NR_reboot, 88                 # sys_call reboot()

.set LINUX_REBOOT_MAGIC1, 0xfee1dead # flags are specified in: <linux/reboot.h>
.set LINUX_REBOOT_MAGIC2, 672274793
.set LINUX_REBOOT_CMD_POWER_OFF, 0x4321fedc
.set LINUX_REBOOT_CMD_RESTART, 0x01234567

.section .text
.globl _start
_start:
   movl $__NR_sync, %eax             # call sync()
   int $0x80

   movl $__NR_reboot, %eax
   movl $LINUX_REBOOT_MAGIC1, %ebx
   movl $LINUX_REBOOT_MAGIC2, %ecx
   movl $LINUX_REBOOT_CMD_RESTART, %edx
   #movl $0, %esi
   int $0x80                         # call reboot()

   movl $1, %eax
   movl $0, %ebx
   int $0x80                         # call exit()

I'm adding the final & working source code as this question might interest somebody in the future:

                                     # For the right sys_call numbers on your arch,
                                     # check <asm/unistd_32.h> (or unistd_64.h)

.set __NR_sync, 36                   # sys_call sync()    
.set __NR_reboot, 88                 # sys_call reboot()

.set LINUX_REBOOT_MAGIC1, 0xfee1dead # flags are specified in: <linux/reboot.h>
.set LINUX_REBOOT_MAGIC2, 672274793
.set LINUX_REBOOT_CMD_POWER_OFF, 0x4321fedc
.set LINUX_REBOOT_CMD_RESTART, 0x01234567

.section .text
.globl _start
_start:
   movl $__NR_sync, %eax             # call sync()
   int $0x80

   movl $__NR_reboot, %eax
   movl $LINUX_REBOOT_MAGIC1, %ebx
   movl $LINUX_REBOOT_MAGIC2, %ecx
   movl $LINUX_REBOOT_CMD_RESTART, %edx
   #movl $0, %esi
   int $0x80                         # call reboot()

   movl $1, %eax
   movl $0, %ebx
   int $0x80                         # call exit()
和我恋爱吧 2024-11-25 04:41:20

来自 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 to movl %ebx, 0.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文