进程的创建与可执行程序的加载
forkTest.c
int main() { pid_t pid; pid = fork(); if(pid == 0) { printf("Child process!\n"); } else if(pid > 0) { sleep(1); printf("Parent process!\n"); } else printf("fork failure!\n"); exit(0); }
#include<stdlib.h> #include<stdio.h> #include<sys/types.h> #include<unistd.h> int main() { pid_t pid; pid = fork(); if(pid == 0) { execl("./helloworld", "helloworld", NULL); printf("Child process!\n"); } else if(pid > 0) { sleep(1); printf("Parent process!\n"); } else printf("fork failure!\n"); exit(0); }
#include <stdio.h> int main() { /* val1+val2=val3 */ unsigned int val1 = 1; unsigned int val2 = 2; unsigned int val3 = 0; printf("val1:%d,val2:%d,val3:%d\n",val1,val2,val3); asm volatile( "movl $0,%%eax\n\t" /* clear %eax to 0*/ "addl %1,%%eax\n\t" /* %eax += val1 */ "addl %2,%%eax\n\t" /* %eax += val2 */ "movl %%eax,%0\n\t" /* val2 = %eax*/ : "=m" (val3) /* output =m mean only write output memory variable*/ : "c" (val1),"d" (val2) /* input c or d mean %ecx/%edx*/ ); printf("val1:%d+val2:%d=val3:%d\n",val1,val2,val3); return 0; }执行截图:
sys_asmTest.c
#include <stdio.h> #include <time.h> int main() { time_t tt; struct tm *t; int ret; /* (gdb) disassemble time Dump of assembler code for function time: 0x0804f800 <+0>: push %ebp 0x0804f801 <+1>: mov %esp,%ebp 0x0804f803 <+3>: mov 0x8(%ebp),%edx 0x0804f806 <+6>: push %ebx 0x0804f807 <+7>: xor %ebx,%ebx 0x0804f809 <+9>: mov $0xd,%eax 0x0804f80e <+14>: int $0x80 0x0804f810 <+16>: test %edx,%edx 0x0804f812 <+18>: je 0x804f816 <time+22> 0x0804f814 <+20>: mov %eax,(%edx) 0x0804f816 <+22>: pop %ebx 0x0804f817 <+23>: pop %ebp 0x0804f818 <+24>: ret End of assembler dump. */ #if 0 time(&tt); printf("tt:%ld\n",tt); #else /* 没有使用常规寄存器传参的方法 */ asm volatile( "mov $0,%%ebx\n\t" /* 不使用参数tt */ "mov $0xd,%%eax\n\t" "int $0x80\n\t" "mov %%eax,%0\n\t" : "=m" (tt) ); printf("tt:%ld\n",tt); t = localtime(&tt); printf("time:%d:%d:%d:%d:%d:%d\n",t->tm_year+1900, t->tm_mon, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec); /* 使用常规寄存器传参的方法 */ asm volatile( "mov %1,%%ebx\n\t" /* 使用参数tt */ "mov $0xd,%%eax\n\t" "int $0x80\n\t" "mov %%eax,%0\n\t" : "=m" (ret) : "b" (&tt) ); printf("tt:%ld\n",tt); t = localtime(&tt); printf("time:%d:%d:%d:%d:%d:%d\n",t->tm_year+1900, t->tm_mon, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec); #endif return 0; }运行截图:
Dump of assembler code for function execl: 0xb7ed85f0 <+0>: push %ebp 0xb7ed85f1 <+1>: push %edi 0xb7ed85f2 <+2>: push %esi 0xb7ed85f3 <+3>: push %ebx 0xb7ed85f4 <+4>: sub $0x102c,%esp 0xb7ed85fa <+10>: mov 0x1044(%esp),%edx 0xb7ed8601 <+17>: lea 0x20(%esp),%ecx 0xb7ed8605 <+21>: call 0xb7f4af83 0xb7ed860a <+26>: add $0xec9ea,%ebx 0xb7ed8610 <+32>: lea 0x1048(%esp),%eax 0xb7ed8617 <+39>: mov %ecx,0x18(%esp) 0xb7ed861b <+43>: test %edx,%edx 0xb7ed861d <+45>: mov %edx,0x20(%esp) 0xb7ed8621 <+49>: je 0xb7ed8724 <execl+308> 0xb7ed8627 <+55>: lea 0x4(%eax),%ebp 0xb7ed862a <+58>: mov (%eax),%eax 0xb7ed862c <+60>: mov $0x1,%esi 0xb7ed8631 <+65>: lea 0x20(%esp),%edi 0xb7ed8635 <+69>: mov $0x400,%edx 0xb7ed863a <+74>: test %eax,%eax 0xb7ed863c <+76>: mov %eax,(%edi,%esi,4) 0xb7ed863f <+79>: je 0xb7ed865d <execl+109> 0xb7ed8641 <+81>: lea 0x0(%esi,%eiz,1),%esi 0xb7ed8648 <+88>: add $0x1,%esi 0xb7ed864b <+91>: cmp %esi,%edx 0xb7ed864d <+93>: je 0xb7ed86a0 <execl+176> 0xb7ed864f <+95>: mov %ebp,%eax 0xb7ed8651 <+97>: lea 0x4(%eax),%ebp ---Type <return> to continue, or q <return> to quit--- 0xb7ed8654 <+100>: mov (%eax),%eax 0xb7ed8656 <+102>: test %eax,%eax 0xb7ed8658 <+104>: mov %eax,(%edi,%esi,4) 0xb7ed865b <+107>: jne 0xb7ed8648 <execl+88> 0xb7ed865d <+109>: mov -0xd4(%ebx),%eax 0xb7ed8663 <+115>: mov 0x1040(%esp),%ecx 0xb7ed866a <+122>: mov (%eax),%eax 0xb7ed866c <+124>: mov %edi,0x4(%esp) 0xb7ed8670 <+128>: mov %ecx,(%esp) 0xb7ed8673 <+131>: mov %eax,0x8(%esp) 0xb7ed8677 <+135>: call 0xb7ed82e0 <execve> 0xb7ed867c <+140>: cmp 0x18(%esp),%edi 0xb7ed8680 <+144>: mov %eax,%esi 0xb7ed8682 <+146>: je 0xb7ed868c <execl+156> 0xb7ed8684 <+148>: mov %edi,(%esp) 0xb7ed8687 <+151>: call 0xb7e36ef0 <free@plt+48> 0xb7ed868c <+156>: add $0x102c,%esp 0xb7ed8692 <+162>: mov %esi,%eax 0xb7ed8694 <+164>: pop %ebx 0xb7ed8695 <+165>: pop %esi 0xb7ed8696 <+166>: pop %edi 0xb7ed8697 <+167>: pop %ebp 0xb7ed8698 <+168>: ret 0xb7ed8699 <+169>: lea 0x0(%esi,%eiz,1),%esi 0xb7ed86a0 <+176>: cmp 0x18(%esp),%edi 0xb7ed86a4 <+180>: mov $0x0,%eax 0xb7ed86a9 <+185>: lea (%edx,%edx,1),%ecx 0xb7ed86ac <+188>: mov %ecx,0x1c(%esp) 0xb7ed86b0 <+192>: lea 0x0(,%edx,8),%ecx ---Type <return> to continue, or q <return> to quit--- 0xb7ed86b7 <+199>: cmovne %edi,%eax 0xb7ed86ba <+202>: mov %edx,0x14(%esp) 0xb7ed86be <+206>: mov %ecx,0x4(%esp) 0xb7ed86c2 <+210>: mov %eax,(%esp) 0xb7ed86c5 <+213>: call 0xb7e36e70 <realloc@plt> 0xb7ed86ca <+218>: mov 0x14(%esp),%edx 0xb7ed86ce <+222>: test %eax,%eax 0xb7ed86d0 <+224>: je 0xb7ed8710 <execl+288> 0xb7ed86d2 <+226>: cmp 0x18(%esp),%edi 0xb7ed86d6 <+230>: je 0xb7ed86e8 <execl+248> 0xb7ed86d8 <+232>: mov %eax,%edi 0xb7ed86da <+234>: mov 0x1c(%esp),%edx 0xb7ed86de <+238>: mov %ebp,%eax 0xb7ed86e0 <+240>: jmp 0xb7ed8651 <execl+97> 0xb7ed86e5 <+245>: lea 0x0(%esi),%esi 0xb7ed86e8 <+248>: shl $0x2,%edx 0xb7ed86eb <+251>: mov %edx,0x8(%esp) 0xb7ed86ef <+255>: mov %edi,0x4(%esp) 0xb7ed86f3 <+259>: mov %eax,(%esp) 0xb7ed86f6 <+262>: mov %eax,0x14(%esp) 0xb7ed86fa <+266>: call 0xb7e9f750 0xb7ed86ff <+271>: mov 0x14(%esp),%ecx 0xb7ed8703 <+275>: mov %ebp,%eax 0xb7ed8705 <+277>: mov 0x1c(%esp),%edx 0xb7ed8709 <+281>: mov %ecx,%edi 0xb7ed870b <+283>: jmp 0xb7ed8651 <execl+97> 0xb7ed8710 <+288>: cmp 0x18(%esp),%edi 0xb7ed8714 <+292>: mov $0xffffffff,%esi 0xb7ed8719 <+297>: jne 0xb7ed8684 <execl+148> ---Type <return> to continue, or q <return> to quit--- 0xb7ed871f <+303>: jmp 0xb7ed868c <execl+156> 0xb7ed8724 <+308>: mov -0xd4(%ebx),%eax 0xb7ed872a <+314>: mov 0x1040(%esp),%ecx 0xb7ed8731 <+321>: mov (%eax),%eax 0xb7ed8733 <+323>: mov %ecx,(%esp) 0xb7ed8736 <+326>: mov %eax,0x8(%esp) 0xb7ed873a <+330>: lea 0x20(%esp),%eax 0xb7ed873e <+334>: mov %eax,0x4(%esp) 0xb7ed8742 <+338>: call 0xb7ed82e0 <execve> 0xb7ed8747 <+343>: mov %eax,%esi 0xb7ed8749 <+345>: jmp 0xb7ed868c <execl+156> End of assembler dump.
动态链接的实现步骤: