How to communicate with Linux kernel   " The power of assembly programming "

  1. Identity of the "bloated software"
  2. Independence from GLIBC
  3. Requirement of exit function
  4. How to communicate with system calls in assembly?
  5. Brief anatomy of Executable and Linking Format (ELF)
  6. Library independent "Hello World"

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.

     
$ gdb test0 GNU gdb 19990928 (no debugging symbols found)... (gdb) disassemble main Dump of assembler code for function main: 0x8048080 <main>: push %ebp 0x8048081 <main+1>: mov %esp,%ebp 0x8048083 <main+3>: mov $0x7b,%eax 0x8048088 <main+8>: jmp 0x8048090 <main+16> 0x804808a <main+10>: lea 0x0(%esi),%esi 0x8048090 <main+16>: leave 0x8048091 <main+17>: ret End of assembler dump.

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
     
#include <stdlib.h> main() { exit(123); }

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.

     
$ gcc -o test1 test1.c $ ls -l test1 -rwxr-xr-x 1 root src 4720 Jan 6 21:26 test1 $ ./test1 ; echo $? 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.

     
$ gcc -nostdlib -o test1 test1.c /usr/bin/ld: warning: cannot find entry symbol _start; defaulting to 08048080 /tmp/ccmRCg3L.o: In function `main': /tmp/ccmRCg3L.o(.text+0xc): undefined reference to `exit' collect2: ld returned 1 exit status

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.