What is compiler, linker, loader?

后端 未结 14 952
离开以前
离开以前 2020-12-07 06:48

I wanted to know in depth meaning and working of compiler, linker and loader. With reference to any language preferably c++.

相关标签:
14条回答
  • 2020-12-07 07:01
    • A compiler reads, analyses and translates code into either an object file or a list of error messages.
    • A linker combines one or more object files and possible some library code into either some executable, some library or a list of error messages.
    • A loader reads the executable code into memory, does some address translation and tries to run the program resulting in a running program or an error message (or both).

    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]
    
    0 讨论(0)
  • 2020-12-07 07:02

    Compiler:

    It will read source file which may be of type .c or .cpp etc and translates that to .o file called as object file.

    Linker:

    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:

    • static linking
    • dynamic linking

    Loader:

    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.

    0 讨论(0)
  • 2020-12-07 07:03
    • Compiler: A language translator that converts a complete program into machine language to produce a program that the computer can process in its entirety.
    • Linker: Utility program which takes one or more compiled object files and combines them into an executable file or another object file.
    • Loader: loads the executable code into memory ,creates the program and data stack , initializes the registers and starts the code running.
    0 讨论(0)
  • 2020-12-07 07:10

    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.

    0 讨论(0)
  • 2020-12-07 07:12

    Compiler :it is a system software which correct the error of programs,object file ,messages etc

    Linker:it is a system software which combines One or more objectfiles and possible some library code into either some exicutable some library or a list of error

    Loader: A program which loads the executable file to the primary memory of the machine

    0 讨论(0)
  • 2020-12-07 07:13
    =====> 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 preprocessor :-

    C preprocessing is the first step in the compilation. It handles:

    1. #define statements.
    2. #include statements.
    3. Conditional statements.
    4. Macros

    The purpose of the unit is to convert the C source file into Pure C code file.

    C compilation :

    There are Six steps in the unit :

    1) Lexical Analyzer:

    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.

    2) Syntactic Analyzer:

    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.

    3) Semantic Analyzer:

    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".

    4) Pre-Optimization:

    This unit is independent of the CPU, i.e., there are two types of optimization

    1. Preoptimization (CPU independent)
    2. Postoptimization (CPU dependent)

    This unit optimizes the code in following forms:

    • I) Dead code elimination
    • II) Sub code elimination
    • III) Loop optimization

    I) Dead code elimination:

    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.

    II) Sub code elimination:

    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
    
        /*
         ...
        */
    }
    

    III) Loop optimization:

    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++ ) {
            /*
            ...
            */
        }
    }
    

    5) Code generation:

    Here, the compiler generates the assembly code so that the more frequently used variables are stored in the registers.

    6) Post-Optimization:

    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.

    0 讨论(0)
提交回复
热议问题