Wednesday, August 19, 2009

C program with NO main() function

Reason for Linux Source Code not containing a “main function”

To understand why some C programs does not require a main() function it is necessary to understand concept called Execution Environment :

Execution Environment:

Whenever we write a piece of C code we do it in an environment known as Execution Environment.

According to C standards the Execution environment includes the characteristics of the processor that will execute the program image (instruction set, number of registers, memory access characteristics, etc.), and the runtime interface to the host environment (storage allocation, function calling conventions, etc.).

In C there are basically two types of execution environments
1. Hosted environment
2. Freestanding environment

Hosted Environment:

Hosted environment is one which a normal developer encounters everyday while writing a piece of code. It already provides inbuilt support of most of the standard libraries and functions. In Hosted environment the developer does not need to worry about the basic
functionally required eg I/O functionality provided by “stdio.h” . GCC is one such platform which provides a Hosted environment which may itself be running on any OS like Linux. Andalso starting point of any hosted environment is the “main function”. Implementations use a variety of techniques to start executing a program image. These usually involve executing some internal, implementation-specific, function before main is called. Then association between this internal function and main is usually made at link-time by having the internal function make a call to “main function “, which is resolved by the
linker in the same way as other function calls. To sum-up hosted environment provides a lot of standard library functions which may or may not be needed by developer but are provided by default.

Freestanding Environment:

Freestanding environment on the other hand is intended to interact directly with the hardware. A freestanding environment does not require any built in library functions. A freestanding implementation is one in which execution may take place without the benefit of an operating system, and has an implementation-defined set of libraries that include certain language-support libraries

Why no “main function” in Linux kernel source code:

In a free standing environment there is no standardize place to begin the execution of the code. In many cases there is no named program at all. Switching on, or resetting, the freestanding host causes the processor instruction pointer to be set to some predefined location in storage, whichever instructions are located at that and subsequent locations being executed. Traditionally there is a small bootstrap loader at this location, which copies a
larger program from a storage device into memory and jumps to the start of that program (it might be a simple operating system). In other cases that storage device is ROM and the program will already be in memory at the predefined location. Translated program instructions are executed directly from the storage device. Once switched on, the host may have buttons that cause the processor to be reset to a different location, causing different
functions to be invoked on startup. For example OS kernel which itself is supposed to provide an hosting environment for
writing C code is written according to freestanding environment in mind, this is the reason there is no “main” function in Linux kernel source code .
Generally in the case on Linux the function placed at the default boot-up address is

start_kernel() .

In most of these cases the code on freestanding environment is used by developers who cannot afford to have all the unnecessary libraries or does not require them like the Embedded System developers .
However there are some libraries which are needed by both hosted and freestanding
environment . They are listed Below :



These libraries fulfil basic needs of C programming and come as a part of Standard Libraries .


2 comments:

  1. Yeah samarth, thanks dude.Very interesting article. Can u add some examples, like adding two numbers in C , execute that code with out main function

    ReplyDelete
  2. i want to know how main() function is implemeted.
    like how it takes arguments from command line.

    ReplyDelete

About Me

My photo
Hi Friends, I am Samarth currently pursuing my Masters degree in Information Technology from International Institute of Information Technology ,Bangalore .