Presentation is loading. Please wait.

Presentation is loading. Please wait.

Loaders and Linkers CSCI/CMPE 3334 David Egle.

Similar presentations


Presentation on theme: "Loaders and Linkers CSCI/CMPE 3334 David Egle."— Presentation transcript:

1 Loaders and Linkers CSCI/CMPE 3334 David Egle

2 Introduction This chapter covers how to prepare object programs for execution Loading brings object program into memory usually provides for relocation, if necessary Linking combines several object programs resolves external references Note that these two functions may be combined in one program

3 Simplest loader Absolute loader Does all work in one pass
does not provide for relocation or linking reads object program and places code in the specified memory locations Does all work in one pass Reads HEADER record to make sure correct program is being read, and that sufficient space is available While no END record Read TEXT record and place code at proper location Start execution at location specified by END record

4 Bootstrap loader Special type of absolute loader used when computer is turned on, or restarted Typically consists of several parts First part: ROM based, or part of hardware very short (in early days, entered at console) reads in second part from another storage device Second part: reads in program to be run usually operating system kernel expects simple object format this kernel then reads in and sets up the operating system

5 Machine dependent features
Relocation permits program to be loaded anywhere in memory Note that this does not apply to modern systems where memory references are made relative to start of user address space if implemented would require that the object code contain modification records indicating which statements required changes

6 Machine dependent features 2
Linking need to resolve all external references cannot perform the resolution until all object programs are read requires knowledge of instruction format Consider code on p

7 PROGA Source Code 0000 PROGA START 0 EXTDEF LISTA, ENDA EXTREF LISTB, ENDB, LISTC, ENDC ……… REF1 LDA LISTA 03201D 0023 REF2 +LDT LISTB REF3 LDX #ENDA-LISTA LISTA EQU * 0054 ENDA EQU * 0054 REF4 WORD ENDA-LISTA+LISTC REF5 WORD ENDC-LISTC-10 FFFFF6 005A REF6 WORD ENDC-LISTC+LISTA F 005D REF7 WORD ENDA-LISTA-(ENDB-LISTB) REF8 WORD LISTB-LISTA FFFFC0 END REF1

8 PROGB Source Code 0000 PROGB START 0 EXTDEF LISTB, ENDB EXTREF LISTA, ENDA, LISTC, ENDC ……… REF1 +LDA LISTA A REF2 LDT LISTB D REF3 +LDX #ENDA-LISTA LISTB EQU * 0070 ENDB EQU * 0070 REF4 WORD ENDA-LISTA+LISTC REF5 WORD ENDC-LISTC-10 FFFFF REF6 WORD ENDC-LISTC+LISTA-1 FFFFFF 0079 REF7 WORD ENDA-LISTA-(ENDB-LISTB) FFFFF0 007C REF8 WORD LISTB-LISTA END

9 PROGC Source Code 0000 PROGC START 0 EXTDEF LISTC, ENDC EXTREF LISTA, ENDA, LISTB, ENDB ……… REF1 +LDA LISTA C REF2 +LDT LISTB REF3 +LDX #ENDA-LISTA LISTC EQU * 0042 ENDC EQU * 0042 REF4 WORD ENDA-LISTA+LISTC REF5 WORD ENDC-LISTC REF6 WORD ENDC-LISTC+LISTA B REF7 WORD ENDA-LISTA-(ENDB-LISTB) E REF8 WORD LISTB-LISTA END

10 Object code – PROGA and PROGB
HPROGA DLISTA ENDA RLISTB ENDB LISTC ENDC … T A03201D …. T F000014FFFFF600003F000014FFFFC0 M LISTB M LISTC M ENDC M LISTC M00005A06+ENDC M00005A06-LISTC M00005A06+PROGA M00005D06-ENDB M00005D06+LISTB M LISTB M PROGA E000020 HPROGB F DLISTB ENDB RLISTA ENDA LISTC ENDC … T B …. T F000000FFFFF6FFFFFFFFFFF M LISTA M00003E05+ENDA M00003E05-LISTA M ENDA M LISTA M LISTC M ENDC M LISTC M ENDC M LISTC M LISTA M ENDA M LISTA M00007C06+PROGB M00007C06-LISTA E

11 Object code – PROGC HPROGC DLISTC ENDC RLISTA ENDA LISTB ENDB … T C …. T F M LISTA M00001D05+LISTB M ENDA M LISTA M ENDA M LISTA M PROGC M LISTA M00004B06+ENDA M00004B06-LISTA M00004B06-ENDB M00004B06+LISTB M00004E06+LISTB M00004E06-LISTA E

12 What needs to be done? Need a list of all the globally defined symbols
Need to know where each program will be loaded in memory Need to fix up the external references Need to load programs into memory Just as in the assembler, this is done in two passes pass 1 – get the addresses of the global symbols pass 2 – use the information to get the program loaded

13 Two approaches Link and load object programs directly into memory
Link programs and save in an executable program which will be loaded into memory later

14 Data Structures External symbol table (ESTAB)
stores the name, address, and control section of each external symbol also holds control section names, starting addresses (CSADDR), and lengths (CSLTH) can be organized as a hash table Program load address (PROGADDR) variable that contains the beginning address in memory for the linked program typically supplied by the OS

15 Pass 1 Algorithm 1. get PROGADDR from operating system 2. set CSADDR to PROGADDR for first control section 3. while not end of input do [for each object file] 3.1. read next input record 3.2. set CSLTH to control section length 3.3. search ESTAB for control section name 3.4. if found then set error flag (duplicate external symbol) else enter control section name into ESTAB with value CSADDR 3.5. while record type <> ‘E’ do process record add CSLTH to CSADDR

16 Process Record 1 1. read next input record 2. if record type == ‘D’ then for each symbol in the record do search ESTAB for symbol name if found then set error flag (duplicate external symbol) else enter symbol into ESTAB with value (CSADDR+ indicated address)

17 Pass 2 Algorithm 1. set CSADDR to PROGADDR 2. set EXECADDR to PROGADDR 3. while not end of input do 3.1. read next input record 3.2. set CSLTH to control section length 3.3. while record type <> ‘E’ do process record if an address is specified in End record then set EXECADDR to (CSADDR + specified address) 3.5. add CSLTH to CADDR 4. jump to location given by EXECADDR to start execution

18 Process Record 2 1. read next input record 2. if record type == ‘T’ then if object code is in character form, convert into internal representation move object code to location (CSADDR + specified address) else if record type == ‘M’ then search ESTAB for modifying symbol name if found then add/subtract symbol value at location (CSADDR + specified address) else set error flag (undefined external reference)

19 An efficient change Assign a reference number to each external symbol referred to in a control section the control section name is assigned the reference number 01 The reference number is used instead of the symbol name in Modification records This technique requires the use of Refer records (not used in our algorithm) Avoids multiple searches of ESTAB during the loading

20 Example of reference numbers
HPROGA DLISTA ENDA R02LISTB 03ENDB 04LISTC 05ENDC … T A03201D T F000014FFFFF600003F000014FFFFC0 M M M M M00005A06+05 M00005A06-04 M00005A06+01 M00005D06-03 M00005D06+02 M M E000020


Download ppt "Loaders and Linkers CSCI/CMPE 3334 David Egle."

Similar presentations


Ads by Google