Social Icons

Saturday, July 12, 2014



We already know the process of  compilation. The GCC compiler will create an object file and at the end the linker will create an executable. I hope by now the process of the compilation is very clear. So, the relocatable  object file and the executable object file contains machine instruction except the below differences.

1: Address

A relocatable object file contains compile time addresses. Whenever the linker creates a relocatable object file for each instruction, it will assign an address. These addresses are called offset addresses. These are not the actual address where the  code will be loaded in the memory. The offset means, its an array depending on how you position your code line by line. Each line will have an offset address. Where it has to get loaded is not yet decided. The sequence is only decided.

The executable object file contains contains the actual addresses. Each instruction is assigned with a load address. This address is machine specific. If my system is 64 bit it will have 64 bit address length. If my machine is 32 bit, it will have a 32 bit address length.
                              This functionality of the linker is called "Instruction Relocation".


The function calls in the relocatable object file will point to some offset where as the function calls in the executable object file refer to the actual address. It points to the actual load address as assigned by the linker. The call will look something similar to this.
call 80482f0 <printf@plt>------- executable object file
call  11(main + 0x11)----------relocatable object file

                   This functionality of the linker is called "Function relocation".

Executable files contains fully relocated code with runtime. As a programmer whatever we write in a .C file is what we refer as the functionality of the program. Now when we compile the code the assembler creates an relocatable object file. To this file linker will resolve the address and functions which we refer to as instruction relocation and function relocation. Along with this the linker adds a few extra function called the "runtime code".

An executable file is a combination of functionality + runtime. When we say ./a.out we are presenting the executable file to the system to execute it. Now the executable file has to get loaded into the memory. TO load the file in the memory it has to get initialized. TO perform the initialization we require the code called the runtime code.
                               Remember this point always, WHO IS MANAGING THE MEMORY???, the MEMORY MANAGEMENT SUBSYSTEM or we can say the KERNEL. The kernel resides in the Operating system. So OS is allocating the memory for my program to execute when I say ./a.out. So, whenever we need to load a code and request that code to run, that code must have initializations. It must first interact with the O.S. Without initializations the code cannot get loaded nor get executed.

                                       Now we do not know these initialization, the O.S will provide these initialization. It means every time we create an .exe it will include the O.S initializations or application initialization code.

                            In case of Linux three important functions are there
                         1:   _init()
                     2:   _start()
                     3:   _fini() 
These three functions are runtime code provided by the Linux Operating System.
The important concept we need understand is that no matter what language we use the EXE file will have the runtime code. We can summarize it like this

Note: To get more clarity on this concept please continue reading the future posts


No comments:

Post a Comment