0% found this document useful (0 votes)
27 views19 pages

06 Compilation Linking Loading

The document discusses the compilation process in programming, detailing the roles of the compiler, assembler, linker, and loader, as well as the standard C library. It explains the steps involved in compiling and linking C programs, including the use of the GCC toolchain and the significance of executable file formats like ELF. Additionally, it touches on static and dynamic linking, cross-compiling, and utilities for working with object code files.

Uploaded by

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

06 Compilation Linking Loading

The document discusses the compilation process in programming, detailing the roles of the compiler, assembler, linker, and loader, as well as the standard C library. It explains the steps involved in compiling and linking C programs, including the use of the GCC toolchain and the significance of executable file formats like ELF. Additionally, it touches on static and dynamic linking, cross-compiling, and utilities for working with object code files.

Uploaded by

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

Compilation, Linking, Loading

Abhijit A M
Review of last few lectures
Boot sequence: BIOS, boot-loader, kernel
● Boot sequence: Process world
–kernel->init -> many forks+execs() -> ....
● Hardware interrupts, system calls, exceptions
● Event driven kernel
● System calls
2
What are compiler, assembler, linker
and loader, and C library
System Programs/Utilities
Most essential to make a kernel really usable

3
Standard C Library
A collection of some of the most frequently

needed functions for C programs


–scanf, printf, getchar, system-call wrappers (open,
read, fork, exec, etc.), ...
An machine/object code file containing the

machine code of all these functions


–Not a source code! Neither a header file. More 4
Compiler
●application program, which converts one
(programming) language to another
–Most typically compilers convert
main.c a high gcc
levelmain
language like C, C++, etc. toSource
Machine
code file code Machine code fi

language
● E.g. GCC /usr/bin/gcc
–Usage: e.g. 5
Assembler
application program, converts assembly code into

machine code
● What is assembly language? main.s as main

–Human readable machine code language.


assembly code file Machine code fi

● E.g. x86 assembly code


–mov 50, r1
6
Compilation Process
preprocessor
compiler +
assembler
m/c linking
Exectable
C Code m/c Code
try.c try.o try
Handles all #
Code Checks syntax
Combines machine
errors, grammatical
directives in the code of input file(S)
errors, missing
input C code and library files
declarations,e tc.
And converts the
input code to
machine code

gcc
●From the
textbook
Example
try.c f.c g.c
#include <stdio.h> int g(int); int g(int x) {
#define MAX 30 #define ADD(a, b) (a + b) return x + 10;
int f(int, int); int f(int m, int n) { }
int main() { return ADD(m,n) + g(10);
int i, j, k; }
scanf("%d%d", &i, &j);
k = f(i, j) + MAX; Try these commands, observe the output/errors/warnings, and try to unde
printf("%d\n", k); $ gcc try.c
return 0; $ gcc -c try.c
} $ gcc -c f.c
$ gcc -c g.c
$ gcc try.o f.o g.o -o try
$ gcc -E try.c
$ gcc -E f.c
More about the steps
● Pre-processor
–#define ABC XYZ
cut ABC and paste XYZ

–# include <stdio.h>
copy-paste the file stdio.h

There is no CODE in stdio.h, only typedefs, #includes, #define, #ifdef, etc.


● Linking
–Normally links with the standard C-library by default
–To link with other libraries, use the -l option of gcc
Using gcc itself to understand the
process
Run only the preprocessor

–cc -E test.c

–Shows the output on the screen

Run only till compilation (no linking)


–cc -c test.c

–Generates the “test.o” file , runs compilation + assembler

–gcc -S main.c

–One step before machine code generation, stops at assembly code

Combine multiple .o files (only linking part)



Linking steps
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 gcc

–$ cc main.o f.o g.o -o try


–the CC will internally invoke "ld" . ld does the job of linking
Linking steps
●The resultatnt file "try" here, will contain the codes of all
the functions and linkages also.
● What is linking?
–"connecting" the call of a function with the code of the
function.
● 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.
Executable file format
●An executable file ● ELF : The format on Linux.
needs to execute in an ● Try this
environment created by
–$ file /bin/ls
OS and on a particular
processor –$ file /usr/lib/x86_64-
linux-gnu/libc-2.31.so
–Contains machine code +
other information for OS
–Need for a structured-way of
storing machine code in it
Exec() and ELF
When you run a program
● ● ELF is used not only for
executable (complete machine
–$ ./try
code) programs, but also for
–Essentially there willl be a fork() partially compiled files e.g.
and exec("./try", ...") main.o and library files like
libc.so.6
–So the kernel has to read the
file "./try" and understand it. ●What is a.out?

–Soeach kernel will demand it's –"a.out"was the name of a


own object code file format. format used on earleir Unixes.
–Hence ELF, EXE, etc. Formats –It so happened that the early
Utilities to play with object code files
objdump

ar

–$ objdump -D -x /bin/ls –To create a “statically linked”


–Shows all disassembled library file
machine instructions and –$ ar -crs libmine.a one.o two.o
“headers” ●Gcc to create shared library

hexdump
–$ gcc hello.o -shared -o

–$ hexdump /bin/ls libhello.so


–Justshows the file in ●To see how gcc invokes as, ld,
hexadecimal etc; do this
–$ gcc -v hello.c -o hello
Linker, Loader, Link-Loader
● Linker or linkage-editor or link-editor
–The “ld” program. Does linking.
● Loader
–The exec(). It loads an executable in the memory.
● Link-Loader
–Oftenthe linker is called link-loader in literature.
Because where were days when the linker and
Static, dynamic / linking, loading
● Both linking and loading can be
–Static or dynamic
–Moreabout this when we learn memory
management
● An important fundamental:
–memorymanagement features of processor,
memory management architecture of kernel,
Cross-compiler
●Compiler on system-A, but generate object-code
file for system-B (target system)
–E.g.compile on Ubuntu, but create an EXE for
windows
Normally used when there is no compiler available

on target system
–see gcc -m option

You might also like