Thursday, May 30, 2013

Internals of Executing a user program.

My first C program was hello world!!.  It was written in a Unix Environment. In this post I will try to cover, as what happens when that binary executable is executed on a shell prompt.

This blog will mostly look in to code journey(path) taken when a program is executed by an user.

As we all know, all binary executable is executed at the the shell(directly or indirectly).  Their is reason for this, shell is a command-line interpreter that provides an user interface for an OS.

To execute an executable program, some preparation is needed and most of it done by kernel. To interact with kernel user needs to invoke specific system call.

When we run a simple program like hello_wold at the shell prompt, the command line interpreter invokes  execve() //system call SYS_execve.

All the argc, argv and envp gets copied from userspace to kernel space.

sys_execve()                                                                     arch/x86/kernel/process.c

do_execve()                                                                       fs/exec.c

do_execve_common()                                                        fs/exec.c
       prepare struct linux_binprm                                        include/linux/binfmts.h
       it holds all the information required when loading binary file.


search_binary_handler()                                                       fs/exec.c

          Lookup for load_binary function callback based on executable format (ELF, COEFF)
          Iterate and match against linux_binprm thru' all supported format.
          Invoke fmt->load_binary callback, since we know the format type is ELF.

load_elf_binary()                                                                  fs/binfmt_elf.c
           // More information needs to be added w.r.t stack initialization/memory for bss
          // and elf format.
           start_thread() macro calls _dl_start() i.e invoke dynamic linker /lib/

No comments:

Post a Comment