A linker is an important utility program that takes the object files, produced by the assembler and compiler, and other code to join them into a single executable file. A loader is a vital component of an operating system that is accountable for loading programs and libraries. 2.
A compiler generates object code files (machine language) from source code. A linker combines these object code files into an executable.
A loader is a part of an operating system that is responsible for loading programs to memory. A compiler is a software that transforms computer code written in one programming language (source code) into another programming language (target code).
A compiler is a special program that translates a programming language's source code into machine code, bytecode or another programming language. The source code is typically written in a high-level, human-readable language such as Java or C++.
=====> COMPILATION PROCESS <======
|
|----> Input is Source file(.c)
|
V
+=================+
| |
| C Preprocessor |
| |
+=================+
|
| ---> Pure C file ( comd:cc -E <file.name> )
|
V
+=================+
| |
| Lexical Analyzer|
| |
+-----------------+
| |
| Syntax Analyzer |
| |
+-----------------+
| |
| Semantic Analyze|
| |
+-----------------+
| |
| Pre Optimization|
| |
+-----------------+
| |
| Code generation |
| |
+-----------------+
| |
| Post Optimize |
| |
+=================+
|
|---> Assembly code (comd: cc -S <file.name> )
|
V
+=================+
| |
| Assembler |
| |
+=================+
|
|---> Object file (.obj) (comd: cc -c <file.name>)
|
V
+=================+
| Linker |
| and |
| loader |
+=================+
|
|---> Executable (.Exe/a.out) (com:cc <file.name> )
|
V
Executable file(a.out)
C preprocessing is the first step in the compilation. It handles:
#define
statements.#include
statements.The purpose of the unit is to convert the C source file into Pure C code file.
There are Six steps in the unit :
It combines characters in the source file, to form a "TOKEN". A token is a set of characters that does not have 'space', 'tab' and 'new line'. Therefore this unit of compilation is also called "TOKENIZER". It also removes the comments, generates symbol table and relocation table entries.
This unit check for the syntax in the code. For ex:
{
int a;
int b;
int c;
int d;
d = a + b - c * ;
}
The above code will generate the parse error because the equation is not balanced. This unit checks this internally by generating the parser tree as follows:
=
/ \
d -
/ \
+ *
/ \ / \
a b c ?
Therefore this unit is also called PARSER.
This unit checks the meaning in the statements. For ex:
{
int i;
int *p;
p = i;
-----
-----
-----
}
The above code generates the error "Assignment of incompatible type".
This unit is independent of the CPU, i.e., there are two types of optimization
This unit optimizes the code in following forms:
For ex:
{
int a = 10;
if ( a > 5 ) {
/*
...
*/
} else {
/*
...
*/
}
}
Here, the compiler knows the value of 'a' at compile time, therefore it also knows that the if condition is always true. Hence it eliminates the else part in the code.
For ex:
{
int a, b, c;
int x, y;
/*
...
*/
x = a + b;
y = a + b + c;
/*
...
*/
}
can be optimized as follows:
{
int a, b, c;
int x, y;
/*
...
*/
x = a + b;
y = x + c; // a + b is replaced by x
/*
...
*/
}
For ex:
{
int a;
for (i = 0; i < 1000; i++ ) {
/*
...
*/
a = 10;
/*
...
*/
}
}
In the above code, if 'a' is local and not used in the loop, then it can be optimized as follows:
{
int a;
a = 10;
for (i = 0; i < 1000; i++ ) {
/*
...
*/
}
}
Here, the compiler generates the assembly code so that the more frequently used variables are stored in the registers.
Here the optimization is CPU dependent. Suppose if there are more than one jumps in the code then they are converted to one as:
-----
jmp:<addr1>
<addr1> jmp:<addr2>
-----
-----
The control jumps to the directly.
Then the last phase is Linking (which creates executable or library). When the executable is run, the libraries it requires are Loaded.
ASCII representation:
[Source Code] ---> Compiler ---> [Object code] --*
|
[Source Code] ---> Compiler ---> [Object code] --*--> Linker --> [Executable] ---> Loader
| |
[Source Code] ---> Compiler ---> [Object code] --* |
| |
[Library file]--* V
[Running Executable in Memory]
Hope this helps you a little more.
First, go through this diagram:
(img source->internet)
You make a piece of code and save the file (Source code), then
Preprocessing :- As the name suggests, it's not part of compilation. They instruct the compiler to do required pre-processing before the actual compilation. You can call this phase Text Substitution or interpreting special preprocessor directives denoted by #.
Compilation :- Compilation is a process in which a program written in one language get translated into another targeted language. If there is some errors, the compiler will detect them and report it.
Assemble :- Assemble code gets translated into machine code. You can call assembler a special type of complier.
Linking:- If these piece of code needs some other source file to be linked, linker link them to make it a executable file.
There are many process that happens after it. Yes, you guessed it right here comes the role of the loader:
Loader:- It loads the executable code into memory; program and data stack are created, register gets initialized.
Little Extra info :- http://www.geeksforgeeks.org/memory-layout-of-c-program/ , you can see the memory layout over there.
Compiler: It is a program which translates a high level language program into a machine language program. A compiler is more intelligent than an assembler. It checks all kinds of limits, ranges, errors etc. But its program run time is more and occupies a larger part of the memory. It has slow speed. Because a compiler goes through the entire program and then translates the entire program into machine codes. If a compiler runs on a computer and produces the machine codes for the same computer then it is known as a self compiler or resident compiler. On the other hand, if a compiler runs on a computer and produces the machine codes for other computer then it is known as a cross compiler.
Linker: In high level languages, some built in header files or libraries are stored. These libraries are predefined and these contain basic functions which are essential for executing the program. These functions are linked to the libraries by a program called Linker. If linker does not find a library of a function then it informs to compiler and then compiler generates an error. The compiler automatically invokes the linker as the last step in compiling a program. Not built in libraries, it also links the user defined functions to the user defined libraries. Usually a longer program is divided into smaller subprograms called modules. And these modules must be combined to execute the program. The process of combining the modules is done by the linker.
Loader: Loader is a program that loads machine codes of a program into the system memory. In Computing, a loader is the part of an Operating System that is responsible for loading programs. It is one of the essential stages in the process of starting a program. Because it places programs into memory and prepares them for execution. Loading a program involves reading the contents of executable file into memory. Once loading is complete, the operating system starts the program by passing control to the loaded program code. All operating systems that support program loading have loaders. In many operating systems the loader is permanently resident in memory.
Wikipedia ought to have a good answer, here's my thoughts:
*
*
Linkers and Loaders from LinuxJournal explains this concept with clarity. It also explains how the classic name a.out came. (assembler output)
A quick summary,
c program --> [compiler] --> objectFile --> [linker] --> executable file (say, a.out)
we got the executable, now give this file to your friend or to your customer who is in need of this software :)
when they run this software, say by typing it in command line ./a.out
execute in command line ./a.out --> [Loader] --> [execve] --> program is loaded in memory
Once the program is loaded into the memory, control is transferred to this program by making the PC (program counter) pointing to the first instruction of a.out
It will read source file which may be of type .c or .cpp etc and translates that to .o file called as object file.
It combines the several .o files which may be generated for multiple source files into an executable file (ELF format in GCC). There are two type of linking:
A program which loads the executable file to the primary memory of the machine.
For an in-detail study about the these three stages of program execution in Linux, please read this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With