简体   繁体   中英

Gnu Assembler - using Fork()

I want to spawn a shell eg /bin/sh.

So I looked up here: http://docs.cs.up.ac.za/programming/asm/derick_tut/syscalls.html So fork is syscall number 2.

So my code would look like:

.globl  _start

.text
_start:
    movl    **WTF-HERE?!?!** (how I use pt_regs?), %ebx
    movl    $2, %eax
    int $0x80

    movl    $0, %ebx
    movl    $1, %eax
    int $0x80
.data

anybody an idea?

Afaik that table is the state of registers on entry in the kernel, not how you call it

Simply put the syscall preserve ebx and ecx, and process the syscall result as follows:

    pushl  ebx          # registers to preserve
    pushl  ecx
    movl    $2, %eax    # system call number for fork.
    int    $0x80        # call int
    popl   ecx          # restore preserved regs.
    popl   ebx
    cmpl  $-4095,%eax   # int returning values between-4095..-1 -> error.
    jb    .LSyscOK
    negl  %eax          # error. Negate value.
    call  seterrno      # call a procedure that sets errno in a PIC safe way.
    movl  $-1,%eax      # set return value in case of error (exactly -1)
 .LSyscOK:

Read the manpages how to determine if you are in the child or in the parent. Pay attention to what you are allowed to do in the parent afterwards. Note on BSD systems you might actually want to call rfork to spawn processes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM