Cha p 3 Loader s an d Linkers

63
Chap 3 Loaders and Linkers

description

Cha p 3 Loader s an d Linkers. 3. 0 preview. Object program contains translated instructions and data values from the source program. Loading , which brings the object program into memory for execution. - PowerPoint PPT Presentation

Transcript of Cha p 3 Loader s an d Linkers

Page 1: Cha p 3  Loader s  an d  Linkers

Chap 3 Loaders and Linkers

Page 2: Cha p 3  Loader s  an d  Linkers

   Object program contains translated instructions and data values from the source program.• Loading, which brings the object program

into memory for execution.• Relocation, which modifies the object

program so that it can be loaded address different from the location originally specified.

• Linking, which combines two or more separate object programs and supplies the information needed to allow between them.

3.0preview

Page 3: Cha p 3  Loader s  an d  Linkers

3.0preview   A loader is a system program that performs the loading function. Many loaders also support relocation and linking.   Some loaders also support relocation and linking. Linker can be used regardless of the original source programming Loader in place of loader and/or linker.

Page 4: Cha p 3  Loader s  an d  Linkers

   Fundamental functions of a loader-bringing an object program into memory and stating its execution.

   Loader does not need to perform such functions as linking and program relocation is checked to verify that the correct been presented for loading.

3.1 Basic loaderfunction

1/5

Page 5: Cha p 3  Loader s  an d  Linkers

   Text record for loading, the object code it contains is moved to the indicated address in memory. The specified address to begin execution of the loaded for which is not there is no Text record shown as .

3.1 Basic loaderfunction

2/5

Page 6: Cha p 3  Loader s  an d  Linkers

3.1 Basic loaderfunction

3/5(1)Fig 3.2 shows algorithm for an absolute loader. This operation code must be stored in a single byte with hexadecimal value.begin

read Header recordverify program name and length read first Text recordwhile record type != 'E'

do begin{if object code is in character form, convert into internal representation}

move object code to specified location in memory read next object program record

endjump to address specified in End recordend

(Figure 3.2 Algorithm for an absolute loader)

Page 7: Cha p 3  Loader s  an d  Linkers

3.1 Basic loaderfunction

4/5

(2)When a computer is first turned on started, a special type of absolute loader, called a bootstrap loader, is executed. It loads the first program to be the first program to be run by the computer-usually an operating system.(3)Fig 3.3 shows bootstrap loader for SIC/XE.

How bootstrap loader working ?

Page 8: Cha p 3  Loader s  an d  Linkers

   The bootstrap address 0 in the memory of the machine (program load for the system). Address 80, which begins the program that was loaded.

   This subroutine reads one character from device F1 and converts ASCII character code to the value of the hexadecimal digit that is used to read and convert a pair of characters from device F1.

3.1 Basic loaderfunction

5/5

Page 9: Cha p 3  Loader s  an d  Linkers

   The programmer to specify address at which it will be loaded into memory for program relocation and linking, as well as functions described in the preceding section.

   The need for program relocation is an indirect consequence of the change to larger and more powerful computers.

3.2 Machine-dependent loader feature 1/14

Page 10: Cha p 3  Loader s  an d  Linkers

3.2 Machine-dependent loader feature

(1) Since• efficient sharing of the machine

(i.e. relocation),• loading the demanded routines.loaders that allow for program relocation are called relocating loaders or relative loaders.(i.e. relative loaders)

2/14

Page 11: Cha p 3  Loader s  an d  Linkers

   A Modification record is used to describe each part be changed when the program is relocated.

   Each Modification record specifies the starting address and length of the field whose value is to be altered.

3.2 Machine-dependent loader feature 3/14

Page 12: Cha p 3  Loader s  an d  Linkers

   The Modification record scheme is a convenient means for specifying, it is not well suited for use with all machine architectures. The important difference between this example and the one in Fig. 3.4 is that the standard SIC relative addressing. In 31 Modification records, in an object program twice as large as the in Fig. 3.5.

3.2 Machine-dependent loader feature 4/14

Page 13: Cha p 3  Loader s  an d  Linkers

3.2 Machine-dependent loader feature

(2)How relocation ? A relocation bit associated with each word of objectcode is needed to modify the address is to be added to the word whichrelocation bit is set to 1 when the program is relocated.

Ex.FFC1111

140033

1111

1100 ( 前 10 個都481039

須做relocationM +

copyM +copy

5/14

Page 14: Cha p 3  Loader s  an d  Linkers

   The Text records are the same as before except that there is a relocation bit associated with each word of object code.

   Address is to be added to this word when the program words are set to 0. All 10 words of object modified during relocation in the second. The first three words are to be modified and used by the by the loader to perform program relocation in the second, the first three words are to be modified.

3.2 Machine-dependent loader feature 6/14

Page 15: Cha p 3  Loader s  an d  Linkers

3.2 Machine-dependent loader feature

(3)Some computers provide a hardware solution to perform program relocation.

Ex. X86 電腦 … CS : IP …

7/14

Page 16: Cha p 3  Loader s  an d  Linkers

   Section 2.3.5 showed a program made up of three control sections that could be assembled independently of one.

   Program contains a list of items; the ends of these lists are marked by the beginnings and ends of the lists are external symbols(they are available for use in linking).

3.2 Machine-dependent loader feature 8/14

Page 17: Cha p 3  Loader s  an d  Linkers

   The first program(PROGA) reference to a label within the program. In PROGB, on the other hand, the same operand an extended-format instruction address field set to 00000. The symbol LISTA to this address field when the program is linked.

3.2 Machine-dependent loader feature 9/14

Page 18: Cha p 3  Loader s  an d  Linkers

   For PROGA, the operand expression consists of an external reference plus a constant, the assembler has all of the information necessary to compute this value. The values of the labels are unknown.     Absolute value independent of the locations at which the programs are loaded.

3.2 Machine-dependent loader feature 10/14

Page 19: Cha p 3  Loader s  an d  Linkers

3.2 Machine-dependent loader feature

(4) Program linking* REF1: PROGA pc relative no relocation or

linking.PROGB (and PROGC) modification record

* REF2: PROGA (and PROGC) modification record PROGB pc relative no relocation or linking

* REF3: PROGA immediate operationPROGB (and PROGC) two external modification

* REF4: PROGA simple external reference PROGB complication of relocation and external reference

Figure 3.10(b) shows the relocation and linking operations performed on REF4 from PROGA

11/14

Page 20: Cha p 3  Loader s  an d  Linkers

   The remaining term are passed on to the loader via Modification, the assembler for PROGA can evaluate. In PEF4 except for the value of LISTC in PROGB contains no terms that can be evaluated by the assembler, that can supply the value of LISTC relative to the beginning of the program relative address of LISTC instruct the loader to add the beginning address of the program add the value of ENDA relocation and external references for PROGC.

3.2 Machine-dependent loader feature 12/14

Page 21: Cha p 3  Loader s  an d  Linkers

   Program as they might appear in memory after loading and linking, reference REF4 in PROGA is located at address 4054 (address of PROGA plus 0054, the relative address PROGA), LISTC is 4112(the beginning address of PROGC), REF4 is located at relative address 70.

   The initial value (000000), the loader adds the values of LISTC (4112) and subtracts the value of LISTA (4040).

3.2 Machine-dependent loader feature 13/14

Page 22: Cha p 3  Loader s  an d  Linkers

   For the reference that are instruction operands, the calculated values after loading do not always appear to be equal. REF1 is an extended format instruction that contains a direct(actual) address.

3.2 Machine-dependent loader feature 14/14

Page 23: Cha p 3  Loader s  an d  Linkers

Relocation and linking operations performed on REF4 from PROGA

HPROGA….. (REF4)T0000540F000014 …...M00005406+LISTC…

PROGA

HPROGC…..DLISTC000030 …...

PROGC

0000…. (REF4) 4050 ……004126….….

Memory contents

4112(Actual address

PROGAof LISTC)

+

+

LOAD addresses00400

0PROGB004063 PROGC 0040E2

Fig3.10(b)

Page 24: Cha p 3  Loader s  an d  Linkers

3.2.3 Tables and logic for a linking loader(1)The linking loader usually

makes two passes over its input:• Pass 1assigns addresses to all external symbols• pass 2performs the actual loading,

relocation, and linking.

Page 25: Cha p 3  Loader s  an d  Linkers

   The table also often indicates in which control section the symbol is defined. PROGADDR is the beginning address assigned to the control section currently to all relative addresses within convert them to actual addresses.

   The loader is concerned only with Define record types in the control sections.  

3.2.3 Tables and logic for a linking loader

Page 26: Cha p 3  Loader s  an d  Linkers

   ESTAB contains all external symbols defined in the set with the address assigned to each. The ability to print a load map that shows these symbols and their addresses.

3.2.3 Tables and logic for a linking loader

Page 27: Cha p 3  Loader s  an d  Linkers

3.2.3 Tables and logic for a linkingloader

(2) Algorithm for Pass 1 of a linking loader:Pass 1: beginget PROOADDR from operating systemset CSADDR to PROOADDR {for first control section} while not end of input do

beginread next input record {Header record for control section} set CSLTH to control section lengthsearch ESTAB for control section name if found then

set error flag {duplicate external symbol} else

enter control section name into ESTAB with value CSADDR while record type ~ 'E' do

beginread next input

record if record type = 'D' then

for each symbol in the record do begin

search ESTAB for symbol name if found then

set error flag (duplicate external symbol)

elseenter symbol into ESTAB with

value (CSADDR + indicated address)

end {for} end {while ~ 'E'}

add CSLTH to CSADDR {starting address for next control section} end {while not EOF}end {Pass 1}Figure 3.11 (a) Algorithm for Pass 1 of a linking loader.

Page 28: Cha p 3  Loader s  an d  Linkers

3.2.3 Tablebsegin and logic for a linkingloader

(3) Algorithm for Pass 2 of a linking loader: Pass 2:

set CSADDR to PROOADDRset EXECADDR to PROOADDRwhile not end of input

do beginread next input

record {Header record}

set CSLTH to control section length while record type != 'E' dobegin

read next input recordif record type = 'T' then

-~ begin{if object code is

in character form, convert

into internal representation}

move object code from record to location (CSADDR + specified address)

end {if 'T'}else if record type = 'M'

then beginsearch ESTAB for modifying symbol nameif found then

add or subtract symbol value at location (CSADDR + specified address)

elseset error flag (undefined external

symbol ) end {if 'M' }end {while !=

'E'}if an address is

specified {in End record} then

set EXECADDR to (CSADDR + specified address) add CSLTH to CSADDRend {while not EOF}

jump to location given by EXECADDR {to start execution of loaded program) end {Pass 2}

Figure 3.11(b) Algorithm for Pass 2 of a linking loader.

Page 29: Cha p 3  Loader s  an d  Linkers

   The loader is usually the transferring of control to the loaded program to begin execution. The address of the first instruction in that control section specifies a transfer address, the loader arbitrarily uses the least one encountered.

   The reference number 01 to the control section symbols may be assigned number as part.

3.2.3 Tables and logic for a linking loader

Page 30: Cha p 3  Loader s  an d  Linkers

3.3 MACHINE-INDEPENDENT LOADER FEATURES 1/6

   Most loaders include fewer different features are found in the typical assembler.

   Automatic library search allows a programmer to use standard subroutines without explicitly including them in the program to be loaded.

Page 31: Cha p 3  Loader s  an d  Linkers

   Linking loaders can automatically incorporate routines from a subprogram library into the program being loaded. Libraries may be specified by control by parameters to the loader called by the program being loaded are automatically fetched from the library, the main program referred to as automatic library call to avoid confusion with the call feature found in most programming languages.

3.3 MACHINE-INDEPENDENT LOADER FEATURES 1/6

Page 32: Cha p 3  Loader s  an d  Linkers

3.3 MACHINE-INDEPENDENT LOADER FEATURES 2/6

(2)Linking loader with automatic library search must keep of symbols that are referred to, but not defined, enter symbols from each Refer record symbol table (ESTAB) unless these symbols are already present. At the end of Pass1, the symbols in ESTAB that remain undefined represent unresolved external references. Libraries specified for routines that contain the definition of these symbols. If there exists unresolved external references, these must be treated as errors.

Page 33: Cha p 3  Loader s  an d  Linkers

   The process just described allows the programmer to override, the library by supplying his or her own routines. For example, the end Pass 1 of the order, SQRT would already be defined, any library search that might be necessary.

   The subroutines contains a directory that of each routine and a pointer to its address within the file, both names are entered into the directory. The object programs indicated by this search.

3.3 MACHINE-INDEPENDENT LOADER FEATURES 1/6

Page 34: Cha p 3  Loader s  an d  Linkers

3.3 MACHINE-INDEPENDENT LOADER FEATURES 3/6(3)Many loaders have a special command language that is used to specify options:

.Selection of alternative sources of input: INCLUDE program-name (library-name).Deletion of the named control section(s): DELETE csect-name.Change external symbol name: CHAGNGE name1,name2.Automatic inclusion of library routines: LIBRARY MYLIB.Unresolved external references: NOCALL STDDEV,PL T,CORRELN.o external references are

resolved by library search.O.utput from the loader.Executed starting address.Errors handling

Page 35: Cha p 3  Loader s  an d  Linkers

   Many loaders allow the user to specify options that modify the standard processing described in Section 3.2. Sometimes there is a separate input file to the loader that contains such control statements. Loader control source program, and the assembler or compiler retains, read the designed object program from a library, it were part of the primary loader input.

3.3 MACHINE-INDEPENDENT LOADER FEATURES 2/6

Page 36: Cha p 3  Loader s  an d  Linkers

3.3 MACHINE-INDEPENDENT LOADER FEATURES 4/6

(4)The actual loading of segment during program execution is handled by an overlay manager, which is automatically included in the root segment of the overlay program by the loader.

Page 37: Cha p 3  Loader s  an d  Linkers

   The external symbol name1 to be changed to name2 wherever containing the object program if Fig. 2.17 is the primary loader input with the loader commands. The loader to include control sections READ from the library UTLIB, COPY had been to READ and WRITE.   Libraries are normally searched before the standard, some references not be resolved in this main function the data using the routines STDDEV, PLOT, and CORREL a statistical library.

3.3 MACHINE-INDEPENDENT LOADER FEATURES 4/6

Page 38: Cha p 3  Loader s  an d  Linkers

3.3 MACHINE-INDEPENDENT LOADER FEATURES 5/6

(5)Overlay rule:If a segment S is present in memory,all of the other segments that lie on the path from S to the root must also be present.

Page 39: Cha p 3  Loader s  an d  Linkers

3.3 MACHINE-INDEPENDENT LOADER FEATURES 6/6

(6)Example of overlay management (Fig.3.16): Segments 1,2 and 4 are loaded ->+JSUB B

->SEGTAB ->segment 2 ->return to segment 1->+ JSUB D ->SEGTAB->OVLMGRsegment 6 isloaded from SEGFILE into memory and

SEGTAB is updated and segments 4 and 2 are removed

->segment 6->return to segment 1.

Page 40: Cha p 3  Loader s  an d  Linkers

   Organizing the loading functions, including relocation and linking. Loaders as described, perform all linking and relocation at load time. We discuss two alternatives to this : linkage editors, which perform linking prior to load time, Linking in which the linking function is performed at execution time.

3.3 MACHINE-INDEPENDENT LOADER FEATURES 6/6

Page 41: Cha p 3  Loader s  an d  Linkers

   Linkage editor performs linking and some relocation; however, the linked program is written library instead of being immediately loaded into memory. Reduces the overhead when the program is executed. At load time is a very simple form of relocation.

3.3 MACHINE-INDEPENDENT LOADER FEATURES 6/6

Page 42: Cha p 3  Loader s  an d  Linkers

   Linking loader is illustrated in Fig. 3.13. The order hand, produces a linked version of the program (called a load module or an executable image).

   The linkage editor performs relocation of all control sections relative to the start of the linked program, symbol table required. This involves much less overhead than using a linking loader.

3.3 MACHINE-INDEPENDENT LOADER FEATURES 6/6

Page 43: Cha p 3  Loader s  an d  Linkers

   A program is to be executed many times without being reassembled, required resolution references and library searching are only performed once, it is reassembled for nearly every execution, development and testing environment. The steps of writing and reading the linked program.

3.3 MACHINE-INDEPENDENT LOADER FEATURES 6/6

Page 44: Cha p 3  Loader s  an d  Linkers

   The linked program produced by the linkage editor is generally in a form that is suitable for processing by a relocating loader. Some mechanism such as Modification records or a bit mask. References is often retained in the linked program. The linked reprocessed by the linkage editor.

3.3 MACHINE-INDEPENDENT LOADER FEATURES 6/6

Page 45: Cha p 3  Loader s  an d  Linkers

3.4 LOADER DESIGN OPTIONS

(1)Linking loaders:it performs all linking and relocation at load time.

Linkage editors:it performs linking prior to load time.

Linkage linking:it performs linking at execution time.

1/14

Page 46: Cha p 3  Loader s  an d  Linkers

Processing of an object program using (a) linking loader and (b) linkage editor

Object program(s)

Linking loader

memory

(a)

Library

Object program(s)

Linkage loaderLibrar

y

Library

Relocating loader

memory

(b)

Page 47: Cha p 3  Loader s  an d  Linkers

3.4 LOADER DESIGN OPTIONS

(2)If a program is to be executed many times without being reassembled, the use of a linkage editor substantially reduces the overhead required.

2/14

Page 48: Cha p 3  Loader s  an d  Linkers

(3)Linkage editors often allow the user to specify that external references are not be resolved by automatic library search. Ex. consider 100 FORTRAN programs using the I/O routines stored on a library.

   Linkage editors can perform all of the needed relocation that is an exact image of the way the program during execution an absolute object program.

3.4 LOADER DESIGN OPTIONS3/14

Page 49: Cha p 3  Loader s  an d  Linkers

   Many useful functions besides simply preparing an object program for execution. Program number of subroutines changed to correct an error or to improve efficiency. The new version of PROJECT is assembled or can be used to replace this subroutine in the linked version of PLANNER.

3.4 LOADER DESIGN OPTIONS4/14

Page 50: Cha p 3  Loader s  an d  Linkers

   Linkage editors used to build packages of subroutines or general used together. Large number of subroutines that used to handle formatted input and output format specifications.

   Named FTNIO could be indexed in the directory of SUBLIB, search of SUBLIB retrieve FINIO instead of the separate routines.

3.4 LOADER DESIGN OPTIONS5/14

Page 51: Cha p 3  Loader s  an d  Linkers

Dynamic Linking        Linking operations before the program is loaded for operations at load time. In the linking function until execution time : subroutine is loaded and linked to the rest of the program function is usually called dynamic linking, dynamic loading, or load on call.

3.4 LOADER DESIGN OPTIONS6/14

Page 52: Cha p 3  Loader s  an d  Linkers

   Dynamic linking is often used to allow several executing programs to con copy of a subroutine or library, and used for references to software objects, makes it possible for one object to be shared by programs, as discussed previously. Dynamic linking library routines in this library could be loaded into the memory, separate copy into each object program.

3.4 LOADER DESIGN OPTIONS7/14

Page 53: Cha p 3  Loader s  an d  Linkers

   Program contains subroutines that correct or clearly diagnose errors are rare, and diagnostic routines may during most executions of the program, every time the program is run the routines only when (and if) needed saving of time and memory space.

3.4 LOADER DESIGN OPTIONS8/14

Page 54: Cha p 3  Loader s  an d  Linkers

   The exact routines needed cannot be predicted until the program examine its input. The library subroutines could potentially be needed, the entire library for each execution, set of subroutines that might be used. Figure 3.14 illustrates operating system service request.

3.4 LOADER DESIGN OPTIONS9/14

Page 55: Cha p 3  Loader s  an d  Linkers

3.4 LOADER DESIGN OPTIONS

   Dynamic linking avoids the necessity of loading the entire library for each execution.Instead of executing a JSUB instruction that refers to an external symbol, the program makes a load-and-call service request to the operating system (delayed binding).

10/14

Page 56: Cha p 3  Loader s  an d  Linkers

BIOSBootstrap loader

MemoryDisk

Loader OS

Bootstrap loader working !!

BACK

1.Power on2.Load Bootstrap loader from ROM to

Memory3.Bootstrap loader loads Loader from

Disk4.Loader loads OS from Disk5.Transfer control form loader to OS

ROM

Page 57: Cha p 3  Loader s  an d  Linkers

Dynamic Linking working

Load-and-call ERRHANDL

Library

Dynamic loader

(part of the operating system)

User program

ERRHANDL

Page 58: Cha p 3  Loader s  an d  Linkers

Dynamic Linking working

Control transfer

Library

Dynamic loader

User program

ERRHANDL

Page 59: Cha p 3  Loader s  an d  Linkers

Dynamic Linking working

Load-and-call ERRHANDL Librar

y

Dynamic loader

User program

ERRHANDL

Page 60: Cha p 3  Loader s  an d  Linkers

   JSUB instruction that refers to an external symbol, a load-and-call service request to the operating system, not the routine is already loaded. The operating system to the routine being called.

   Subroutine completes its processing, it returns to its caller, then returns control to the program that request. Subroutine is completed, that was allocated to load it may be released for after use as long as the storage needed for other processing, shown in Fig. 3.14.

3.4 LOADER DESIGN OPTIONS11/14

Page 61: Cha p 3  Loader s  an d  Linkers

   The association of an actual address with the symbolic name of called routine is not made until the call statement is executed from load time until execution time.

3.4 LOADER DESIGN OPTIONS12/14

Page 62: Cha p 3  Loader s  an d  Linkers

   How is the loader it self loaded into memory? A predefined location in memory, the object code for switches on the computer console.

   Absolute loader program is read-only memory (ROM). The machine begins to execute this ROM program.

3.4 LOADER DESIGN OPTIONS13/14

Page 63: Cha p 3  Loader s  an d  Linkers

   Intermediate solution is to have a hardware function that reads a fixed-length record from some device into memory at a fixed location. This record contains machine instructions that load the absolute program that loading process requires more instructions than can be read. The first referred to as a bootstrap loader.

3.4 LOADER DESIGN OPTIONS14/14