Welcome to Linux Forums! With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.
Find the answer to your Linux question:
Site Navigation
Linux Forums
Linux Articles
Product Showcase
Linux Downloads
Linux Hosting
Free Magazines
Job Board
IRC Chat
RSS Feeds
Free Publications

This was asked to me by my Prof. Unable to answer I was asked to make a complete Doc on it.. thought it might be helpful to others

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 example 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. And also 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 fulfill basic needs of C programming and come as a part of Standard Libraries .

Rate This Article: poor excellent
Comments about this article

Comment title: * please do not put your response text here