Social Icons

Thursday, December 19, 2013

UNDERSTANDING LINKER...............ARTICLE 16

UNDERSTANDING LINKER

Linker is a tool which will link the libraries to the executable file. This is the definition I have learned as a child. I used to believe that linking of the libraries is the sole purpose of using the linker.
               Later is when I realized linker is the most important tool in the entire tool chain. It has a number of functionalities. We will discuss most of them in this article.
(note:  I wont be discussing anything about the linker script as I am not very familiar with it.)


WHAT IS THE FUNCTIONALITY OF THE LINKER...?

The Linker is the most important tool in the gcc tool chain. We already know that we use many library functions in C and C++. Some of the examples are printf(), scanf(), pow() etc.. These functions are defined in the standard C library and we use the linker to link these libraries into the source code. Well, let me give you more clarity about this. At the time of GNU project one of their most remarkable works is GNU compiler and the standard C library which is called as GLIBC. We can download this from the Linux.org website. 
                                                    We know the functionality of the functions. We write functions for the purpose of reusability. In the same way libraries are also written for the reusability. The library functions like printf(), scanf() etc .. we do not write the code for its functionality. Its already present in the standard C library called glibc. What we do is we just embed that code into our executable file and run it. Now who will embed the library functions code into our soucecode?.. Its the Linker. The Linker will detect what are the libraries required and will automatically link them into our source code. The Linker can also be used to create user defined libraries. 

WHICH TOOL THE LINKER USES...?

Well I have shown you the step by step compilation of a program add.c. We have performed the first three stages of the compilation i.e the pre-processing phase, compiler and the assembler. (refer previous posts on understanding pre-processor, compiler, assembler). The assembler's output is an object file. The object file will be taken as input by the Linker and a binary executable object file will be the output. After the assembly phase we got a file called add.o which is an object file. This will be taken by the Linker for further processing. Please refer previous articles in case you did not understand.
 In the above screen shot you can see that there are  four files in my directory. add.c is my source code file. The order is in the following manner.           

                                                                            add.c(source code)
                                                                               ||
                                                                            CC1(pre-processor)
                                                                               ||
                                                                            add.i  
                                                                               ||
                                                                            CC1(compiler)
                                                                               ||
                                                                            add.s
                                                                               || 
                                                                              AS(assembler)
                                                                               ||
                                                                           add.o  
                                                                               ||
                                                                           LD(linker)
                                                                               || 
                                                                           add.exe
  
The add.exe is the executable file generated by the linker. Its not important to have the ' .exe ' extension. Lets now perform the fourth phase of the compilation. We have used different flags to tell the compiler to invoke a specific tool inorder to perform the step by step compilation. In case of the linker we need not give any flags. Look at the below screen shot.
I am passing the assembler's output to the gcc compiler and generating an executable file. The executable files are displayed in green colour.
Here when I executed the command ' ls ' it displays all the contents in the directory and we can see that there is add.exe which is the executable file. Lets now execute this file and see what's the output generated.
We can execute the file by doing ./ add.exe. The output is saying that " THE VALUE OF C IS 30 ". If you remember we wrote a C program which adds the values of A and B and stores the output in C. 
  

THE VERBOSE OUTPUT OF THE LINKER..

command:-- gcc -v add.o   -o  add.exe

 The Linker will first read a file called specs and it will load all the required libraries, check the flags.. well very similar to that of those previous tools. It uses a tool called ld. linux.so.2 is the linker which the gcc is using. The most important thing to notice here is how it is linked. We will discuss about all that in the next article.

No comments:

Post a Comment