CPSC 410/611 : Operating Systems Linking and Loading ! • Preparing Program for Execution ! • Relocation ! • Address binding ! – Linking, loading ! • Reading: Doeppner 3.4 ! Preparing a Program for Execution ! dynamically loaded system system library library S 1 O 1 L temp L phys O 2 S 2 loader linker S i : Source Program S 3 O 3 O i : Object Module compiler L temp : Load Module L phys : Memory Image • compiler: translates symbolic instructions, operands, and addresses into numerical values. ! • linker: resolves external references; i.e. operands or branch addresses referring to data or instructions within some other module ! • loader: brings program into main memory. ! Memory Management: Linking and Loading 1
CPSC 410/611 : Operating Systems Steps Involved ! 1. Name /Symbol Resolution ! Linking linker: !"#$%&'#()*+,- 2. Relocation ! Loading 3. Program Loading ! loader: .,./0.#$%&'#()*+,- Name/Symbol resolution ! File subr.c : ! A8)5#;)!.#/2*#542*"#3*#)45# int X ; 36*# void subr(int y) { int x = y; } File main.c : ! 12**34#542*"#3*#)45#36*# extern int X; 78.9.#)5#:#".;)*."<# int *aX = &X; int main() { 78.9.#"3#6.#=+>?#68.*#6.# void subr( int ); *.."#43#.,./+4.#5+@9$-#<# int y = X; subr(y); return 0; } Memory Management: Linking and Loading 2
CPSC 410/611 : Operating Systems Symbol Resolution ! • Compile source code into object files ! • Object files? ! – Contains compiled source ! – Symbol tables ! – Relocation data ! (Can view contents of Object files using 3@="+>?# (in linux)) ! • Two types of Object files ! – Relocatable Object files ! – Shared Object files ! Symbol resolution ! / /subr.c : ! int X ; extern int X; //main.c int *aX = &X; void subr(int y) { int main() { int x = y; void subr( int ); } int y = X; subr(y); f 1 return 0; } f 2 O = f 1 U f 2 … O ! O U { f i .O} D ! D U {D i } D1 D2 U ! (U U U i ) U1 U2 -(U " D) Memory Management: Linking and Loading 3
CPSC 410/611 : Operating Systems Symbol resolution with a library ! • Libraries? ! – libc / libm ! – Combination of many object (.o) files ! • Linker goes looking at libraries if the set U is NOT NULL ! Generalization for Name Resolution ! Name Mapping Names Values Context 1. Table lookup • Single Object file (Symbol table) 2. Path name resolution • /home/user/suneil … 3. Search through contexts • Linker Memory Management: Linking and Loading 4
CPSC 410/611 : Operating Systems Linking: Relocation ! int main ( int argc, char *[]) { return argc; } main: pushl %ebp movl %esp, %ebp movl 8(%ebp), %eax movl %ebp, %esp popl %ebp ret This code is inherently relocatable . Linking: Relocation (cont) ! .globl _X .data _X : .long 6 .globl _aX _aX: .quad _X int X=6; .text .globl _main int *aX = &X; _main: pushq %rbp movq %rsp, %rbp int main () { subq $16, %rsp void subr( int ); movl _X(%rip), %eax movl %eax, -4(%rbp) int y = X; movl -4(%rbp), %edi subr(y); call _subr movl $0, %eax return 0; leave } ret .globl _subr _subr : void subr( int i) { pushq %rbp This code is not freely relocatable! ! movq %rsp, %rbp int x = i; movl %edi, -20(%rbp) e.g.: ! } movl -20(%rbp), %eax movl %eax, -4(%rbp) • Variable aX needs to know location of X . ! leave • Call to subr() needs to know location. ! ret Memory Management: Linking and Loading 5
CPSC 410/611 : Operating Systems Linking: Relocation (cont) ! File main.c : ! $> gcc main.c Undefined symbols: " _subr ", referenced from: extern int X; _main in ccLXS9NR.o int *aX = &X; " _X ", referenced from: _main in ccLXS9NR.o int main() { _aX in ccLXS9NR.o void subr( int ); ld: symbol(s) not found int y = X; collect2: ld returned 1 exit status subr(y); $> Compilation: ! return 0; } gcc –o prog main.c subr.c File subr.c : ! This code is no not freely relocatable! ! int X ; e.g.: ! void subr(int y) { • Variable aX needs to know location of X . ! int x = y; • Call to subr() needs to know location of subr() . ! } Linking: Relocation (cont) ! File main.c : ! .globl _aX .data _aX: extern int X; .quad _X int *aX = &X; .text .globl _main _main: int main() { pushq %rbp void subr( int ); movq %rsp, %rbp int y = X; subq $16, %rsp subr(y); movq _X@GOTPCREL(%rip), %rax movl (%rax), %eax return 0; movl %eax, -4(%rbp) } movl -4(%rbp), %edi .text call _subr .globl _subr File subr.c : ! movl $0, %eax _subr: leave pushq %rbp ret movq %rsp, %rbp int X ; movl %edi, -20(%rbp) movl -20(%rbp), %eax void subr(int y) { movl %eax, -4(%rbp) leave int x = y; ret } .comm _X,4,2 Memory Management: Linking and Loading 6
CPSC 410/611 : Operating Systems Address Binding ! • Comp mpile- e-time me binding: ! 100 branch A assembler branch 150 A: ... 150 • Load-time binding (static relocation): other progrms other module other module 00 100 1100 branch A assembler branch 50 linker branch 150 loader branch 1150 A: ... A: ... 50 150 1150 • Execution-time binding (dynamic relocation): other progrms other module other module 00 100 1100 branch A assembler branch 50 linker branch 150 loader branch 150 MMU A: ... 50 150 1150 1150 Loading ! User types Understand the Find which file to command or command ! execute ! clicks on icon ! Pass control to the start point Load that file of the file to into memory ! begin executing ! Memory Management: Linking and Loading 7
CPSC 410/611 : Operating Systems Shared Objects ! • Do not embed common routines in every program ! – Have one copy of the module and share it between multiple programs ! • Instructions associated with memory locations. ! – Different programs may call the shared module with different address ! Jump to location 150 ! Solution: " Position independent code : ! " " " " All addresses are relative (to program counter) ! " " " " Jump to offset X from here. ! Dynamic Loading, Dynamic Linking ! • Dynamic Loading: ! – load routine into memory only when it is needed ! – routines kept on disk in relocatable format ! • Load-time Linking: ! – postpone linking until load time. ! • Dynamic Linking: ! – postpone linking until execution time. ! – Problem: Need help from OS! ! call x x: load routine stub link x to location of routine Memory Management: Linking and Loading 8
CPSC 410/611 : Operating Systems Example ! #include <stdio.h> int main ( int argc, char *[]) { printf(“Hello World”); return 0; } B//#8.!!3C/#D3#8.!!3639!" # CE8.!!3639!"# Symbol Resolution/ Link/ Load ! • Figuring out where all the symbol definitions are ! – printf() … ? ! printf is complicated… ! Takes at least 2 arguments, can be more. ! ?9)*4;#$FG"HIJK*4.B.9L29)2@!.&2>.-M# (in C/C++ Is there a limit to the number of arguments?) ! • Look in other related files, object files, libraries (static and dynamic), environment variables. ! • Load and run the program ! Memory Management: Linking and Loading 9
Recommend
More recommend