0% found this document useful (0 votes)
36 views3 pages

Pilation Linking Loading

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views3 pages

Pilation Linking Loading

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

System programming

What are compiler, assembler, linker and loader, and C library?


----------------------------------------
C library: a compilation of some of the most frequently needed functions for C
programs
An machine/object code file containing the machine code of all these
functions
Where is the C library on your computer?
/usr/lib/x86_64-linux-gnu/libc-2.31.so
Compiler: application program, which converts one (programming) language to another
Most typically compilers convert a high level language like C, C++, etc. to
Machine code language
E.g. GCC /usr/bin/gcc
Usage: e.g. gcc main.c -o main
Here main.c is the C code, and "main" is the object/machine code file
generated
Input is a file and output is also a file.
Other examples: g++ (for C++), javac (for java)
Assembler: application program, converts assembly code into machine code
What is assembly language. Human readable machine code language.
E.g. x86 assembly
mov 50, r1
add 10, r1
mov r1, $500
Usage. eg..
$ as something.s -o something

Compilation process using GCC for a C program


-------------------------------
gcc is just another program (you can write it!)
gcc reads a C language file as input. what does it do with it?
what happens when you run
$ gcc main.c -o main
0) pre-processing: # directives get processed.
e.g. #define MAX 10
the pre-processor will replace all occurrences of "MAX" with "10"
e.g. #include <stdio.h>
the contents of stdio.h will be copy pasted in "this" location.
Where is this file? /usr/include/stdio.h
$ gcc -E main.c
this will only run the pre-processor stage and stop.
1) tokenization - split the file into "tokens" Lexical Analysis
2) Symantic analysis - arranging the tokens into a logical order according to
grammar of the langauge
and also identifying if there is a wrong order
You get most of your syntax errors
3) converts the C code (tokenized and symantically organized sequence of tokens)
into assembly language
if you want to see the output of this stage, then run
$ gcc -S main.c
output here will depend on : processor, OS
4) invoke the assembler to convert assembly to machine language (minus the linked
code)
$ gcc -c main.c
5) invoke the linker to link the C library and other libraries (if needed) with the
machine code.
$ gcc main.c

What is ELF ?
--------
Executable and Linkable Format
Format for creating machine code files, typically on Linux

When you run a program


$ ./try
Essentially tehre willl be a fork() and exec("./try", ...")
So the OS has to open the file "./try" and understand it.
So each OS will demand it's own object code file format.
E.g. Windows supports "EXE" format, while Linux supports "ELF" format.
That's why EXE files won't run on Linus and ELF files won't run on Windows.

ELF is used not only for executable (complete machine code) programs, but also for
partially compiled files e.g. main.o and library files like libc.so.6

"a.out" was the name of a format used on earleir Unixes.


It so happened that the early compiler writers, also created executable with
default name 'a.out'

Linking process
-------------
Linker is an application program
On linux, it's the "ld" program
E.g. you can run commands like $ ld a.o b.o -o c.o
Normally you have to specify some options to ld to get a proper executable file.

WHen you run $ cc main.o f.o g.o -o try


the CC will internally invoke "ld"
ld does the job of linking

WHat is linking? -- "connecting" the call of a function with the code of the
function.
The resultatnt file "try" here, will contain the codes of all the functions and
linkages also.

What happens with the code of printf()?


THe linker or CC will automatically pick up code from the libc.so.6 file for the
functions.

Loader
-------------
Is a "code" that loads the machine code from a file into memory
E.g. "exec"
Loader is part of OS

Why are they system programs?


--------------------
Compilers, linkers, asssemblers, etc are system programs
because their behaviour and output depends on the system (os + processor)

cross-compilation
-----------
Using a compiler on one platform (os + processor) to generate machine code for
another platform (target).
Typically compilers by default will generaet machine code for "same" platform on
which they execute.
Compilers, good ones, like GCC can be used to do cross-compilation.
E.g. you can run gcc on Ubuntu+x86 to generate an executable file for ARM+some OS.
cross-compilation is needed if compiler does not exist on the "target" platform.

Dynamic and Static Linking


-------------
Static linking: the linker combines the machine code of all functions, into the
executable. Hence the executable has large size. Safer.
Dynamic Linking: the linker does not combine the code of, typically the library
functions into the executable. It inserts "stub" code instead.
When the program starts running, at that time, the "stub" code will help the
loader locate the code of the library, and load it.
Size of executable is small.
Flexibility to change the library code without changing the executable.

Default is dynamic linking.

Dynamic and Static Loading


-------------
Job of loader.
If the entire program is loaded in memory at "exec" time, it's static loading.
If a particualr function is loaded in memory when it's called, it's dynamic
loading.

4 Possibilities of linking + loading


-------------
Static Link, Stat Load
Static Link, Dyn Load
Dyn Link, Stat Load
Dyn Link, Dyn Load

You might also like