This document discusses linkers and loaders, which integrate program modules and load object codes into memory. It covers:
1) Types of loaders including absolute, relocating, and linking loaders. Relocating loaders add load points to references while linking loaders resolve external references and relocate addresses.
2) How a linking loader performs two passes to allocate space, link modules by resolving externals, relocate addresses, and load instructions into memory.
3) How libraries can contain commonly used routines and are linked between the two passes of the linker to resolve any external references in user programs.
This document discusses linkers and loaders, which integrate program modules and load object codes into memory. It covers:
1) Types of loaders including absolute, relocating, and linking loaders. Relocating loaders add load points to references while linking loaders resolve external references and relocate addresses.
2) How a linking loader performs two passes to allocate space, link modules by resolving externals, relocate addresses, and load instructions into memory.
3) How libraries can contain commonly used routines and are linked between the two passes of the linker to resolve any external references in user programs.
This document discusses linkers and loaders, which integrate program modules and load object codes into memory. It covers:
1) Types of loaders including absolute, relocating, and linking loaders. Relocating loaders add load points to references while linking loaders resolve external references and relocate addresses.
2) How a linking loader performs two passes to allocate space, link modules by resolving externals, relocate addresses, and load instructions into memory.
3) How libraries can contain commonly used routines and are linked between the two passes of the linker to resolve any external references in user programs.
CSE 232 Systems Programming Lecture Notes #3 LINKERS ND L!DERS "# Introdu$tion Installing a code in memory is called %oading. Memory Assembler Loader Obj. prog. Source Object program program
&igure "# Loading an object code into memory The tas o! integrating the program modules together is called %in'ing. relocatable object modules Assembler Liner Loader Module" Module# Source Lined modules Object Module 3 modules &igure 2# Linking and loading as split processes Lining and loading are !re$uently done at one time by a %in'ing %oader. Module " Assembler Lining Loader Module #
relocatable Source object modules modules object Module 3 modules &igure 3# Linking loader " Types of loaders: ". Absolute loaders #. %elocating loaders 3. Lining loaders 2# (so%ute Loaders Assembler generates the code and &rites instructions in a !ile together &ith their load addresses. The loader reads the !ile and places the code at the absolute address gi'en in the !ile. E)amp%e "( Assume that the assembler generates the !ollo&ing code( Address( instruction( )")) *# )" )+ )")3 +, )")+ -. / / )#)) ). )#)" *# )# )) The abo'e code is &ritten in the !ile as !ollo&s( )")) location 0# bytes1 . number o! bytes *# )" )+ code +, -. )#)) location + number o! bytes ). *# code )# )) 2O* # read # bytes 2O* 3 marer return N set L4 to the byte read read a byte and set N- to it read a byte place it into the memory location pointed to by L4 N- 5 N- 6 " L4( Location 4ounter L4 5 L4 7 " N-( Number o! -ytes N 3 N-8) &igure *# Absolute loader 3 3# Re%o$ating Loader independently assembled modules Assembler %elocating Loader relocation in!ormation E)amp%e 2( Assume that the !ollo&ing t&o relocatable codes and the associated 9ATs are generated by the assembler( Address( instruction( )))) *# )) )+ )))3 +, )))+ -. / / 9AT )))" )))) ). )))" *# )) )) / / 9AT )))# The relocating loader adds the load:point o! the code to all the re!erences speci!ied in 9AT. I! the load:points o! the abo'e programs are .)) and ,)); they are placed in the memory as !ollo&s( Memory / ).)) 3A ).)" 05 ).)# 04 ).)3 +, ).)+ -. / ),)) ). ),)" *# ),)# 07 ),)3 00 / + <et load:point L45) %ead a byte 2O* 3 marer return N 3 L4 is in N 9AT %ead ne=t byte >lace the byte at memory location L4 7 Load >oint Add load point L4 5 L4 7 " >lace the bytes at memory locations L4 7 Load >oint and L4 7 Load >oint 7 " L4 5 L4 7 # &igure +# Relocating loader . *# Lin'ing Loader Lining loaders per!orm !our !unctions( ". Allocation( allocating space in memory !or programs #. Linking( resol'ing e=ternal re!erences bet&een modules 3. Relocation( adjusting all address re!erences +. Loading( physically placing machine instructions and data in memory Entry and E)terna% Points ?hen a statement in one module re!ers to a symbol in another module; it is called an e)terna% referen$e 02@T%2*1. Any symbol in a module that can be re!erred to !rom another module is called an entry point 02@T92*1. Module A Module - 2@T2%NAL AL>AA; -2TA 2NT%3 AL>AA; -2TA / / L9A AL>AA / AL>AA( / L@I -2TA / / -2TA( / B E)amp%e 3, The assembler has generated the object codes o! the !ollo&ing source programs &here e=ternal re!erences ha'e not been resol'ed. These codes &ill be lined and loaded into memory by a lining:loader. Source programs 4ode generated by the assembler A >%O<A % SIC2 % -D* % SDM >%O<A( STA%T) address 2@T%2* SIC2; -D*; SDM L9A #"#E )))) #F )" #E STA SIC2 )))3 )4 GG GG L9A #" )))B #F )) )" L9@ #) )))F ). )) )) L"( STA -D*; @ )))4 )* GG GG A99 #" )))* "F )) )" TI@ SIC2 ))"# #4 GG GG H2I L# ))". 3) )) "- 0placed by the assembler in pass #1 H L" ))"E 34 )) )4 0placed by the assembler in pass "1 L#( HSD- SDM ))"- +E GG GG %SD- ))"2 +* )) )) 2N9 9AT ))"B 9AT ))"F M SIC2 )))+ M -D* )))9 M SIC2 ))"3 M SDM ))"- A >%O<- 9 SDM )))) % -D* % SIC2 % TOT >%O<-( STA%T ) 2@T92* SDM 2@T%2* SIC2; -D*; TOT SDM( L9A #) )))) #F )) )) L9@ #) )))3 ). )) )) L3( A99 -D*; @ )))B "- GG GG TI@ SIC2 )))F #4 GG GG H2I L+ )))4 3) )) "# 0placed by the assembler in pass #1 H L3 )))* 34 )) )B 0placed by the assembler in pass "1 L+( STA TOT ))"# )4 GG GG %SD- ))". +* )) )) 2N9 9AT )))9 9AT ))") M -D* ))), M SIC2 )))A M TOT ))"3 A >%O<4 9 SIC2 )))) 9 TOT )))3 9 -D* )))B >%O<4( STA%T ) 2@T92* SIC2; -D*; TOT SIC2( %2S? " )))) GG GG GG TOT( %2S? " )))3 GG GG GG -D*( %2S? #)) )))B GG GG GG 2N9 )))F GG GG GG )))4 GG GG GG / / , %elocatable machine codes
Assembler 9ATs Liner
AJ9J%JM In!ormation The liner does t&o passes( Pass", ". <ets load points o! all programs. #. 4reates 2STA- table; using the in!ormation in 9J% table. 4alculates the absolute address o! each label in 2STA- as( load point 7 relati'e address. Pass 2, ". Dsing the M in!ormation and the absolute addresses in 2STA-; resol'es the e=ternal re!erences in all programs. #. Dsing the 9AT in!ormation; adds load points to the local re!erences. ES-. program load point label absolute address >%O<A "))) >%O<- #))) SDM #))) >%O<4 3))) SIC2 3))) TOT 3))3 -D* 3))B E Source programs 4ode generated 4ode generated by the assembler at the end o! lining address address >%O<A( STA%T) 2@T%2* SIC2; -D*; SDM L9A #"#E )))) #F )" #E )))) #F )" #E STA SIC2 )))3 )4 GG GG )))3 )4 3) )) L9A #" )))B #F )) )" )))B #F )) )" L9@ #) )))F ). )) )) )))F ). )) )) L"( STA -D*; @ )))4 )* GG GG )))4 )* 3) )B A99 #" )))* "F )) )" )))* "F )) )" TI@ SIC2 ))"# #4 GG GG ))"# #4 3) )) H2I L# ))". 3) )) "- ))". 3) 10 1B H L" ))"E 34 )) )4 ))"E 34 10 0C L#( HSD- SDM ))"- +E GG GG ))"- +E #) )) %SD- ))"2 +* )) )) ))"2 +* )) )) 2N9 >%O<-( STA%T ) 2@T92* SDM 2@T%2* SIC2; -D*; TOT SDM( L9A #) )))) #F )) )) )))) #F )) )) L9@ #) )))3 ). )) )) )))3 ). )) )) L3( A99 -D*; @ )))B "- GG GG )))B "- 3) )B TI@ SIC2 )))F #4 GG GG )))F #4 3) )) H2I L+ )))4 3) )) "# )))4 3) 20 12 H L3 )))* 34 )) )B )))* 34 20 06 L+( STA TOT ))"# )4 GG GG ))"# )4 3) )3 %SD- ))". +* )) )) ))". +* )) )) 2N9 >%O<4( STA%T ) 2@T92* SIC2; -D*; TOT SIC2( %2S? " )))) GG GG GG )))) GG GG GG TOT( %2S? " )))3 GG GG GG )))3 GG GG GG -D*( %2S? #)) )))B GG GG GG )))B GG GG GG 2N9 )))F GG GG GG )))F GG GG GG )))4 GG GG GG )))4 GG GG GG / / / / F <et Load >oint %ead program name and enter in 2STA- %ead a record N 9 record 3 2nter symbol in 2STA- and calculate absolute address N end o! program 3 N end o! all programs 3 >ass # &igure /# Pass 1 of te Linker ") <et load point !rom 2STA- and set L4 to the load point %ead a record 3 object code ?rite it in the memory location N pointed by L4 M record 3 N *ind absolute address o! the symbol in 2STA- and &rite it in memory location pointed by L4 7 relati'e location 3 9AT record Add load point to the 'alue in memory N location pointed by L4 7 9AT entry
3 more records N 9one &igure 0# Pass 2 of te Linker "" Lined codes are combined into a single !ile in the !ollo&ing !ormat( *ile !ormat( *ile name *ile type Aeader Number o! blocs Load point 0# bytes1 Number o! bytes 4ode -loc " 4hecsum / / / -loc N Checksum: To calculate checsum; add up all data in the bloc and tae its lo& order byte. ?hen &riting the code to the !ile; calculate checsum and &rite it at the end o! each bloc. ?hen installing the code in memory; recompute checsum and compare it &ith the one read !rom the !ile. "# Loader %ead header N- 5 bloc count %ead load point L4 5 load point %ead byte count -4 5 byte count InitialiKe checsum %ead a byte ?rite the byte in memory at location pointed to by L4 Add the byte to checsum L4 5 L4 7 " -4 5 -4 6 " 3 -48) N %ead checsum byte N 4hecsum print error message OL 3 N- 5 N- 6 " N-( Number o! blocs 3 L4( Location counter N- 8 ) -4( -yte count N %eturn &igure 1# Absolute loader! "ic reads te linked codes from a file and loads tem into te memory# "3 +# Lin'ing 2it3 Li(raries Library contains the relocatable image o! each module. Dser programs are assembled indi'idually. The library routines used in user programs appear as e=ternal re!erences. Library routines %elocatable 9AT 9J%JM machine code In!ormation user %elocatable machine code programs 9AT Liner 9J%JM In!ormation Most liners e=ecute a de!ault transparent search o! a set o! libraries that contain most o! the commonly called routines. Searches are per!ormed at the end o! >ass " Library routines may ha'e global symbols to be resol'ed; liner per!orms iterati'e search until all symbols are resol'ed Library !iles are designed to e=pedite the search. It is not necessary to go through the &hole !ile; instead; a library file eader is re'ie&ed; usually located at the beginning o! a !ile. The header contains all in!ormation. E), Library !ile header Routine $ame %lobal &ymbols Location in te 'ile / / / / / / "+ Library routines are lined &ith user programs; bet&een >ass " and >ass #; &hile the liner is resol'ing e=ternal re!erences. N 3 more Tae an 2=ternal re!erence >ass # re!erences %esol'e it 2ntry 3 !ound !or it in user programs N 4hec all libraries 0or only those speci!ied by the user1 to !ind an 2ntry !or it N !ound print error message 3 4alculate a load point !or the library routine 4on!igure direct addresses in the modules Append the library routine to the user !ile Merge the ne& 9J% in!ormation to the 2STA- &igure 4# Linking te library routines "it te user programs# ". Dynami$ ddress Reso%ution In dynamic lining; a subroutine is loaded and lined to the other programs &hen it is !irst called during the e=ecution. 9ynamic lining allo&s se'eral e=ecuting programs to share the same copy o! a subroutine. *or e=ample; a single copy o! the routines in a dynamic linking library 0run(time library1 can be loaded into the memory; and all currently e=ecuting programs can be lined to this copy. 9ynamic lining pro'ides the ability to load the routines only &hen they are needed. *or e=ample; error correction routines are not need to be lined and loaded i! no errors occur during the program. 9ynamic lining also a'oids the necessity o! loading the entire library !or each e=ecution. *or e=ample; a program may be calling a di!!erent routine depending on the input data. 9ynamic lining and loading has the !ollo&ing steps( ". The user program maes a Load:and:4all re$uest to the operating system 0dynamic loader1. #. The operating system e=amines its internal tables to determine &hether or not the routine is already loaded. I! not; loads it into the memory. Then it trans!ers control to the routine. 3. A!ter the subroutine is processed; it returns control to the operating system. +. The operating system returns control to the program that issued the re$uest. .. The subroutine may be retained in the memory !or later use. .ootstrap %oaders ?hen a computer is !irst turned on or restarted; bootstrap loader is e=ecuted. -ootstrap loader is a simple absolute loader. Its !unction is to load the !irst system program to be run by the computer; &hich is the operating system or a more comple= loader that loads the rest o! the system. -ootstrap loader is coded as a !i=ed:length record and added to the beginning o! the system programs that are to be loaded into an empty system. A built:in hard&are or a 'ery simple program in %OM reads this record into the memory and trans!ers control to it. ?hen it is e=ecuted; it loads the !ollo&ing program; &hich is either the operating system itsel! or other system programs to be run &ithout the operating system. "B