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-07-30 5:35 pm


Independence from GLIBC

There is no standard function calls in the source. Do we really need libraries? Why not to shave off the flab? -nostdlib option ceases to link standard system libraries and startup files.

     
$ gcc -nostdlib -o test0 test0.c $ ./test0 /usr/bin/ld: warning: cannot find entry symbol _start; defaulting to 08048080

Fmm..., GCC complained there is no entry point named as _start. Executable and Linking Format (ELF) assumes _start as an initial entry point. Do you know "where is _start"?

     
$ nm /usr/lib/crt1.o 00000024 t Letext 00000004 R _IO_stdin_used 00000000 D __data_start U __libc_start_main U _fini U _fp_hw U _init 00000000 T _start 00000000 W data_start U main

Here it is. A startup file crt1.o contains a program whose entry point is _start and the code also includes main() calling statement (NOTE main entry is Undefined in crt1.o). So, the famous "main()" is implicitly called within crt1.o. I'll show you how to escape from the problem.

     
$ gcc -c test0.c $ ls -l test0* -rw------- 1 root src 30 Jan 6 20:37 test0.c -rw-r--r-- 1 root src 745 Jan 6 21:00 test0.o $ ld -e main -o test0 test0.o $ ls -l test0 -rwxr-xr-x 1 root src 989 Jan 6 21:00 test0 $ ldd test0 statically linked (ELF) $ nm test0 08049094 A __bss_start 08049094 A _edata 08049094 A _end 08048080 t gcc2_compiled. 08048080 T main

After compile of test0.c (-c force compile only), link the program using main as an entry point (-e main). Look, the code size is only 989 bytes! It is one fifth of original program. ldd tells you test0 does not depend on GLIBC any more. It is a standalone program. As a result, many symbols has disappeared (nm). Now, it is time to execution.

     
$ ./test0 Segmentation fault

Oh my god... What happened? The answer appears in next section .