How to communicate with Linux kernel " The power of assembly programming "
Last updated 2001-10-26 11:27 pm
Requirement of exit function
To find out the reason of unexpected error, analyze the program using GDB.
|
push ebp; mov esp,ebp; leave; are a formula in assembly (discussed later). This program loads 123 (0x7B in hexadecimal) into EAX register and simply returns to caller. However, where is the caller? We excluded crt1.o from code, so there is no caller in test0. As a result, ret statement pops undefined return address from a stack and CPU jumps into the meaningless address. This is the reason of "segmentation fault".
test1.c |
|
Here is a new version of test1.c. This source uses a standard library function exit(), and it explicitly exits the process with a return code of 123.
|
Because this code depends on a standard library, we can't use -nostdlib option. The code size again increased to 4720 bytes. test1 naturally returns 123 to the shell.
|
Let's try compiling with -nostdlib option. As expected, ld complained there is no exit() entry. We have to create an original exit() function in stead of GLIBC. The code appears in next section.