简体   繁体   English

Gnu汇编器-使用Fork()

[英]Gnu Assembler - using Fork()

I want to spawn a shell eg /bin/sh. 我想产生一个外壳,例如/ 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. 所以我在这里查找: http : //docs.cs.up.ac.za/programming/asm/derick_tut/syscalls.html所以fork是syscall编号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 Afaik该表是内核中入口时寄存器的状态,而不是您如何调用它

Simply put the syscall preserve ebx and ecx, and process the syscall result as follows: 只需将syscall保留ebx和ecx,并按以下方式处理syscall结果:

    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. 请注意,在BSD系统上,您实际上可能希望调用rfork来生成进程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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