在Minix上组装

时间:2014-03-16 07:35:11

标签: assembly x86 minix

我在Windows7 64位的VMWare中安装了Minix 3。我正在尝试在这个VM上练习汇编。刚开始,我尝试了一个简单的程序来将数字加载到eax和ebx中并调用o / s来终止程序。

.globl _start:
.section .text
_start:
movl $1, %eax
movl $0, %ebx
int $0x80

编译器和链接器不会引发任何错误或警告,但是当我运行可执行文件时,我得到了

PM: coredump signal 11 for 282 / <program name>
memory fault

这是由主机是64位机器引起的吗?是因为minix有一个不同的o / s电话?我已经尝试在网上专门搜索minix o / s调用,我找到了特定o / s调用的解决方案,但没有o / s调用表。有人能指出我在正确的方向吗?

1 个答案:

答案 0 :(得分:3)

你走了:

<强> MINIX-exit.S

  .section .text
  .global _start
_start:
  /* _exit(17) */
  and $0xfffffff0, %esp
  sub $0x40, %esp
  movl $17, 0x18(%esp) /* exit status */
  lea 0x10(%esp), %ebx
  movl $1, 0x04(%ebx)  /* EXIT in include/minix/callnr.h */
  movl $0, %eax        /* PM_PROC_NR in include/minix/com.h */
  movl $0x3, %ecx      /* SENDREC in include/minix/ipcconst.h */
  int $33              /* IPCVEC_ORIG in sys/arch/i386/include/asm.h */

构建并运行:

$ clang -nostdlib minix-exit.S -o minix-exit
$ ./minix-exit; echo $?
17

我发现找出Minix 3系统调用实现的最简单方法是在阅读代码之前进行一些逆向工程:

呼叫序列

作为微内核的Minix坚持将系统调用构建为可以发送给不同管理者的消息。通过将exit()发送到EXIT来实现PM_PROC_NR

通过一些逆向工程

来解决这个问题

查看像/bin/cat这样的简单静态链接可执行文件如何在Minix上实现_exit()

<强> dis.gdb

set pagination 0

file /bin/cat
set logging on
disassemble _exit
disassemble _syscall
x/a 0x8072678
disassemble _sendrec_orig
quit

执行命令

gdb -q -x dis.gdb

GDB输出

Dump of assembler code for function _exit:
0x0805b780 :   push   %ebp
0x0805b781 :   mov    %esp,%ebp
0x0805b783 :   and    $0xfffffff0,%esp
0x0805b789 :   sub    $0x40,%esp
0x0805b78c :  mov    0x8(%ebp),%eax
0x0805b78f :  mov    %eax,0x18(%esp)
0x0805b793 :  lea    0x10(%esp),%eax
0x0805b797 :  mov    %eax,0x8(%esp)
0x0805b79b :  movl   $0x1,0x4(%esp)
0x0805b7a3 :  movl   $0x0,(%esp)
0x0805b7aa :  call   0x805dfc0 
0x0805b7af :  call   0x805b7af 
0x0805b7b4 :  nopw   %cs:0x0(%eax,%eax,1)
0x0805b7c0 :  jmp    0x805b7c0 
End of assembler dump.
Dump of assembler code for function syscall:
0x0805dfc0 : push   %ebp
0x0805dfc1 : mov    %esp,%ebp
0x0805dfc3 : push   %esi
0x0805dfc4 : sub    $0xc,%esp
0x0805dfc7 : mov    0xc(%ebp),%eax
0x0805dfca :    mov    0x10(%ebp),%esi
0x0805dfcd :    mov    %eax,0x4(%esi)
0x0805dfd0 :    mov    %esi,0x4(%esp)
0x0805dfd4 :    mov    0x8(%ebp),%ecx
0x0805dfd7 :    mov    %ecx,(%esp)
0x0805dfda :    call   *0x8072678
0x0805dfe0 :    test   %eax,%eax
0x0805dfe2 :    je     0x805dfe9 
0x0805dfe4 :    mov    %eax,0x4(%esi)
0x0805dfe7 :    jmp    0x805dfec 
0x0805dfe9 :    mov    0x4(%esi),%eax
0x0805dfec :    test   %eax,%eax
0x0805dfee :    jns    0x805e000 
0x0805dff0 :    mov    %eax,%esi
0x0805dff2 :    neg    %esi
0x0805dff4 :    call   0x805e010 
0x0805dff9 :    mov    %esi,(%eax)
0x0805dffb :    mov    $0xffffffff,%eax
0x0805e000 :    add    $0xc,%esp
0x0805e003 :    pop    %esi
0x0805e004 :    pop    %ebp
0x0805e005 :    ret    
End of assembler dump.
0x8072678 :   0x805e100 
Dump of assembler code for function _sendrec_orig:
0x0805e100 :   push   %ebp
0x0805e101 :   mov    %esp,%ebp
0x0805e103 :   push   %ebx
0x0805e104 :   mov    0x8(%ebp),%eax
0x0805e107 :   mov    0xc(%ebp),%ebx
0x0805e10a :  mov    $0x3,%ecx
0x0805e10f :  int    $0x21
0x0805e111 :  pop    %ebx
0x0805e112 :  pop    %ebp
0x0805e113 :  ret    
0x0805e114 :  lea    0x0(%esi),%esi
0x0805e11a :  lea    0x0(%edi),%edi
End of assembler dump.