Tutorial-4 Linker Loader Part1
Tutorial-4 Linker Loader Part1
Samit Bhattacharya
Comp Sc & Engg
Indian Institute of Technology Guwahati
Linking
• Process of combining various pieces of code, module or data together
to form a single executable unit that can be loaded in memory
• Can be done
• at compile time
• at load time (by loaders)
• at run time (by application programs)
Loader
• Program that takes object program and prepares it for execution
• Once executable file has been generated, the actual object module generated
by the linker is deleted
Review - Calling Sequence Convention
Preprocessor
Compiler
Assembler
Linker/loader
0
Code
Data
OS
101010101 101010101
010101010 010101010
101010101 101010101
010101010 010101010
∞ Stack
101010101
010101010
101010101
010101010
• Loaders
• Brings object program into memory and starts its execution
• Initializes registers, stack, arguments to first function
• Jumps to entry‐point
C source files Assembly files Obj files linker
calc.c calc.s calc.o
Executable
math.c math.s math.o program
calc.exe
exists
Compiler io.s io.o
on disk
loader
Assembler libc.o
Executing
libm.o
.o = Linux in
.obj Windows Memory
process
Note
• Three tasks
• Loading (into memory)
• Relocation (program level & memory level)
• Symbol resolution (when combining multiple files)
Note: Compilers and assemblers generate relocatable and shared form of object
files. Linkers combine these object files together to generate executable form of
object files.
Object File Contd…
• Relocatable
• contains binary code and data in a form that can be combined with other
relocatable object files at compile time to create an executable object file
• Executable
• contains binary code and data in a form that can be directly loaded into
memory and executed
• Shared
• a special type of relocatable object file that can be loaded into memory and
linked dynamically, either at load time or at run time
Object File Format
• An object file contains five basic types of information
• Header: size and position of segments of file
• Object code: instructions
• Relocation information: final location (symbol, function)
• Symbols: external (exported) references, unresolved (imported) references
• Debugging information: line number, code address map, etc.
Note: Some object files may contain some more information apart from
these five types.
Example of a Relocatable Object file
• Typical Unix Executable and Linkable Format
(ELF) relocatable object file
• .text : machine code of the complied program
• .rodata : read‐only data (strings in printf)
• .data : Initialize global variables
• .bss : Uninitialized global variables
• .sysmtab : entry for symbols (variable name, function name)
• .rel.text : locations in .text section need to modified when
linker combine it in other obj
• .rel.data : relocation information for any global variables that
are referenced or defined by the module
• .debug : entries for local variables and typedefs defined in
the source
• .line : mapping between line numbers in the original source
• .strtab : Null terminated character string for .sysmtab,
.debug, and header section
Example Object File with Basic Information
main.o
0 main: text section
main.c ...
30 call printf
extern float sin();
...
extern printf(), scanf(); 52 call scanf
...
main() { 60 call sin
double x, result; ...
printf("Type number: "); 86 call printf
scanf("%f", &x);
result = sin(x); 0 _s1: "Type number: " data section
printf("Sine is %f\n", 14 _s2: "%f"
17 _s3: "Sine is %f\n"
result);
} main T[0] symbols
_s1 D[0]
_s2 D[14]
“Store the final location of sin _s3 D[17]
at offset 60 in the text section”
printf T[30] relocation
printf T[86]
scanf T[52]
sin T[60]
_s1 T[24]
_s2 T[54]
_s3 T[80]
Use of Object Code Library
Static and Dynamic Linking
• Static linking
• Big executable files (all/most of needed libraries inside)
• Don’t benefit from updates to library
• No load‐time linking
• Dynamic linking
• Small executable files (just point to shared library)
• Library update benefits all programs that use it
• Load‐time cost to do final linking
• But dll code is probably already in memory
• And can do the linking incrementally, on‐demand
Static Linking
• The linker combines relocatable object files to form an executable
object file.
Dynamic Linking
• A subroutine is loaded and linked to the rest of the program when it is
first called
• Provides the ability to load the routines only when they are needed