Segmentation Fault when shutting down Linux with assembly application
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 开发者_JAVA百科$__NR_sync, %eax
int $0x80
movl $1, %eax #syscall exit
movl $0, %eax
int $0x80
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)
?
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()
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
.
精彩评论