System Software m3 - Part2
System Software m3 - Part2
gor
it
hm andDat
ast
ruct
uresf
oraLi
nki
ngLoader
Thel i
nkingl oaderusesmodi fi
cati
onr ecordsforrelocati
onsot hatthe
l
inkingandr elocati
onf unct i
onsareperfor
medusi ngthesamemechani sm.
Thei nputf oral inki
ngl oaderconsistsofasetofobj ectprograms(ie,
control secti
ons)ar etobel i
nkedtogether
Li nkingcanbeper formedbet weentwopr ogramsorcont rolsect
ionsonly
i
fbot har eloaded
PASSESOFALI NKINGLOADER
Li
nkingLoaderusest wo- passes.
Pass1:Assignaddr essest oallexternalsymbols
Pass2:Performt heact uall
oading, rel
ocati
on,andli
nking
ESTAB(externalsymbol t
able)
ESTAB( exter
nal symboltable)isthemaindatastructureforalinki
ngloader.
Thi
st ableisusedt ostor
ethenameandaddr essofeachext ernalsymbol
I
talsocont aininformati
onr egardinginwhichcont r
olsection,thesymboli s
defi
ned
Vari
ablePROGADDRhaspr ogram loadaddr
ess(gener atedbyOS)
Vari
ableCSADDRhascont rolsecti
onaddress,cur
rentlyscannedbyt heloader
ESTAB-ESTABf ortheexampl
e( r
efert
hreeprogr
amsPROGAPROGBandPROGC)
gi
veni sasshownbel ow.TheESTABhasf ourentr
iesi
ni t
;theyar
enameoft he
contr
olsecti
on,thesymbolappeari
nginthecontr
olsect
ion,i
tsaddr
essandl
ength
ofthecontr
olsecti
on.
1
PASS1
Pass1assi
gnaddr
essest
oal
lext
ernal
symbol
s.
Thev
ari
abl
es&Dat
ast
ruct
uresuseddur
ingpass1ar
e,PROGADDR(
progr
am
l
oadaddr
ess)f
rom OS,CSADDR(
cont
rolsect
ionaddr
ess)
,CSLTH (
cont
rol
sect
ionl
engt
h)andESTAB.
Thepass1pr
ocessest
heDef
ineRecor
dandHeaderr
ecor
d.
PROGADDRi
sobt
ainedf
rom t
heoper
ati
ngsy
stem.Thi
sPROGADDRbecome
st
t
heCSADDRf
ort
he1 cont
rol
sect
ionl
oaded.
WhenENDr
ecor
disr
ead,t
heCont
rolSect
ionLengt
h(CSLTH)i
saddedwi
th
CSADDRt
ogi
vet
henextcont
rol
sect
ionl
oadaddr
ess
0040 LI
STA EQU *
-
- -
- -
- -
-
-
- -
- -
- -
-
-
- -
- -
- -
-
0054 ENDA EQU *
Theaddr
essofLI
STAandENDAi
sfoundbyaddi
ngt
headdr
essof
0040and0054wi
tht
hest
art
ingl
ocat
ionoft
hepr
ogr
am.
Theot
heraddr
essesar
ecal
cul
atedl
i
kewi
se.
Att
heendofpass1al
ltheext
ernalsy
mbol
sar
est
oredi
ntheESTAB
asabov
e.
PASS2
I
tper
for
msact
ual
loadi
ng,
rel
ocat
ionandl
i
nki
ng
Aseacht
extr
ecor
disr
eadt
heobj
ectcodei
smov
edt
othespeci
fi
edaddr
ess.
Whenmodi
fi
cat
ionr
ecor
disencount
eredt
hesy
mbolwhosel
oadaddr
essi
s
t
obemodi
fi
edi
slookedi
nESTAB
Thecont
rol
ist
ransf
err
edt
othel
oadedpr
ogr
am t
obegi
nexecut
ion
Tr
ansf
erPoi
nt:
TheENDr
ecor
dofeachcont
rolsect
ionmaycont
aint
headdr
essoft
hef
ir
st
i
nst
ruct
ioni
nthatcont
rol
sect
ion
Thel
oadert
akest
hisasat
ransf
erpoi
ntt
obegi
ntheexecut
ionoft
hepr
ogr
am
I
fthet
ransf
erpoi
nti
snotgi
vent
hel
oaderusest
hebegi
nni
ngoft
hel
i
nked
2
pr
ogr
am(
PROGADDR)ast
het
ransf
erpoi
nt
Al
gor
it
hm f
orPass1ofLi
nki
ngLoader
St
eps:
1)Gett
headdr
essoft
hememor
ylocat
ionst
hatar
efr
eef
rom t
heoper
ati
ng
sy
stem i
nthev
ari
abl
ePROGADDR
2)Assi
gnt
hePROGADDR(
gotf
r he1stcont
om OS)ast rol
sect
ionaddr
ess
3)Readt
heHeaderRecor
doft
heCont
rol
Sect
ion
i
. Fr
om,t
hedet
ail
soft
heHeaderr
ead,st
oret
hecont
rolsect
ion
l
engt
hinav
ari
abl
eCSLTH
i
i
. Checki
fthecont
rolsect
ionnamei
spr
esenti
next
ernalsy
mbol
t
abl
eESTAB
i
i
i. I
fthecont
rolsect
ionnamei
spr
esenti
nESTAB,t
hedupl
i
cat
ion
er
rormessagemustbedi
spl
ayed
i
v. Ot
her
wiseent
ert
hecont
rolsect
ionnamewi
thi
tsaddr
essi
ntot
he
ext
ernal
symbol
tabl
e.
4)Dot
hef
oll
owi
ngpr
ocessunt
ilanendr
ecor
disr
eached
i
. Foreachsy
mboli
ntheDr
ecor
d,sear
chESTABt
ochecki
fthesy
mbol
i
sal
readypr
esenti
nit
.Ifso,
”dupl
i
cat
esy
mbol
”er
rormessagemust
bedi
spl
ayed.
i
i
. Ot
her
wiset
hesy
mbolmustbeent
eredi
ntot
hesy
mbolt
abl
eESTAB
al
ong wi
tht
he addr
ess ,
plus t
he cor
respondi
ng cont
rolsect
ion
addr
ess
5) Assi
gnt
hest
art
ingaddr
essofnextcont
rolsect
ionast
headdr
essoft
he
cur
rentcont
rol
sect
ionpl
usi
tsl
engt
h.
6)Repeatt
hepr
ocessf
rom st
ep3t
ost
ep5unt
ilt
her
ear
enomor
einput
.
3
Pass2al
gor
it
hm ofl
i
nki
ngl
oader
Pass2ofl inki
ngl oaderper f
ormt heactualloadi
ng,relocation,andli
nking.
Itusesmodi f
icationr ecordandl ookupt hesy mboli nESTABt oobt ainits
address.
Final
lyitusesendr ecordofamai nprogr
am t oobtai
nt ransferaddress, which
i
sast ar
ti
ngaddr essneededf ortheexecuti
onoft hepr ogram.
The pass 2 pr ocess Tex trecord and Modi f
icati
on r ecord ofthe obj ect
programs.Theal gor i
thm forPass2ofLi nkingLoaderi sgivenbelow.
St
eps:
1)Assi
gnthef i
rstcontrolsecti
onaddr essi nav ariablePROGADDR
2)Assi
gntheexecut ableprogram EXECADDRal sot oPROGADDR
3)ReadtheHeaderRecor d.
i
. Fr om t heinformationoft heHeaderRecor dr ead,st oret hecont rol
secti
onl engthinav ari
ableCSLTH
4)Dothefoll
owingpr ocessunt i
ltheendofr ecordi sreached.
i
. I fthesubsequentr ecordsr eadi sat extrecor d‘T’,andi ft heobject
codei si ncharacterfor m,conv ertitt omachi ner epresentati
on,and
mov etheobj ectcodef rom ther ecordt othememor ylocationwhi chi s
controlsecti
onsaddr essplust hespeci fi
edaddr essinthet extrecord.
ii
. I fthesubsequentr ecordr ead ist hemodi f
ication record‘ M’t hen
searchESTABf rom themodi f
yingsy mbolname, ifiti
sf oundt henadd
orsubt ractthesy mbolv al
ueatt hel ocation( CSADDR+t hespeci fi
ed
4
addressi nt
hemodi f
icati
onrecord)
ii
i. I ft
hesy mboli snotpr esentinESTAB,t henitmustbeanundef ined
symbol t
her
eforeerrormessagemustbedi spl
ayed.
5)Ifanaddressi spr
esentint heendr ecord,t
hensetEXECADDRt othecont rol
secti
onaddr ess+ t hespeci f
ied addressint heend recor
d,whichi st he
star
tingaddressoft heprogr am
6)Add t he controlsection length CSLTH tot he contr
olsections address
CSADDRt of indtheaddr essofnextcont rolsect
ionandr epeattheent i
re
processfrom 3t o6unt i
ltheendofi nputisencount
ered
7)Jumpt othel ocat
ionspecifiedbyt heEXECADDRandst arttheexecuti
onof
l
oadedpr ogram
MACHI
NE-
INDEPENDENTLOADERFEATURES
Herewedi scusssomel oaderf
eat
urest
hatar
enotdi
rect
lyr
elat
edt
omachi
ne
ar
chit
ect
ureanddesign.
5
Aut
omat
icLi
brar
ySear
chand
LoaderOpt
ions
ar
esuchMachi
ne-
independentLoaderFeat
ures.
1)
Aut
omat
icLi
brar
ySear
ch
Thisf eature all
ows a programmert o use st andard subrout
ines without
explici
tl
yincl
udingt hem intheprogram tobel oaded.Ther outi
nesar eautomatical
ly
ret
rievedfrom al i
braryastheyareneededdur i
ngl i
nking.Thisallowsprogrammert o
usesubr outi
nesf rom oneormor elibr
aries.Thesubr out i
nescalledbyt heprogram
beingloadedar eautomat i
call
yfetchedfrom thelibrar
y ,l
inkedwiththemai nprogram
andl oaded.Thel oadersear chesthel i
braryorl i
brariesspecifi
edf orrouti
nest hat
containthedefinit
ionsofthesesy mbolsint hemai nprogr am.
Libr
ariesusual
lycontai
ncompi ledversi
onofthesubr
outines.Theseli
brar
iescanbe
searchedbyscanningt hedefinerecordsforal
ltheobjectprogramsint heli
brar
y.
Butthismethodisineff
ientbecauseinmostcasesaspeci alf
il
estruct
ureisusedfor
theli
brari
eswhichcontaindir
ectorythathasnameofeachr out
ineandpointer
stoits
addresswithi
nthefil
e.
I
fasubr out
ineistobecalledbymorethanonename,bothnamesareenteredinthe
dir
ectory
.Butt heseentr
iespointonl
yt othesanecopyofobjectprogram.Thus
l
ibrar
ysearch invol
vessear ch ofdi
rect
oryfol
lowed byreadi
ng obj
ectpr ogr
am
i
ndicatedbythesearch.
LoaderOpt
ions
Loaderopt ions al
low t he userto speci
fy opt
ions thatmodi fyt he standard
processi
ng.Theopt i
onsmaybespeci fi
edi
nthr
eedi f
fer
entway s.Theyare,specif
ied
usingacommandl anguage,specif
iedasapar tofjobcont rollanguaget hati s
processed byt heoper ati
ng syst
em,and can bespeci f
ied usi
ng loadercont rol
statement
si nthesourcepr ogr
am.
Her
ear
ethesomeexampl
esofhowopt
ioncanbespeci
fi
ed.
I
NCLUDEprogr
am-
name(
li
brar
y-name)-r
eadt
hedesi
gnat
edobj
ectpr
ogr
am
f
rom al
i
brar
y
DELETEcsect-
name– del
etet
henamedcont
rolsect
ionf
rom t
hesetof
pr
ogramsbeingloaded
CHANGEname1,name2-externalsy
mbolname1t
obechangedt
oname2
wher
everi
tappear
sint
heobj
ectprogr
ams
LI
BRARYMYLI
B–sear
chMYLI
Bli
brar
ybef
orest
andar
dli
brar
ies
NOCALL STDDEV,PLOT,CORREL – no l
oadi
ng and l
i
nki
ng ofunneeded
r
outi
nes
Her
ei sonemoreexampl
egi vi
ng,how commandscanbespecif
iedasapar
tof
obj
ectfi
l
e,andt
her
espect
ivechangesar
ecarr
iedoutbyt
hel
oader
.
6
LI
BRARYUTLI
B
I
NCLUDEREAD(
UTLI
B)
I
NCLUDEWRI
TE(
UTLI
B)
DELETERDREC,
WRREC
CHANGERDREC,
READ
CHANGEWRREC,
WRI
TE
NOCALLSQRT,
PLOT
Thecommandsar e,useUTLIB(sayuti
li
tyli
brary)
,includeREADandWRI TEcont
rol
sect
ionsfrom theli
brary
,delet
ethecontrolsecti
onsRDRECandWRRECf r
om t
he
l
oad,thechangecommandcausesal lext
ernalreferencestothesymbolRDRECto
bechangedtothesy mbolREAD,si
milar
lyr
eferencest oWRRECi schangedt
oWRITE,
fi
nal
ly,nocal
ltothefuncti
onsSQRT,PLOT, i
ftheyareusedintheprogram.
LOADERDESI
GNOPTI
ONS
Therearesomecommonal ter
nat
ivesfororganizingtheloadingf
uncti
ons, i
ncl
uding
r
elocat
ionandl i
nki
ng.LinkingLoaders–Per f
or m al
lli
nkingandr el
ocat
ionatl oad
t
ime.TheOt herAlt
ernat
ivesareLinkageeditors,whichper f
ormli
nkingpriortoload
t
imeand,Dynami cl
inki
ng,inwhichli
nkingfunctionisperf
or medatexecut
iont i
me
1)LI
NKI
NGLOADERS
7
Theabovediagram showst heprocessi
ngofanobjectprogram usi
ngLi nki
ngLoader.
Thesourceprogram isfi
rstassembledorcompiled,produci
nganobj ectprogram.A
l
inki
ngloaderperfor
msal ll
inki
ngandl oadi
ngoperat
ions,andloadsthepr ogr
am int
o
memoryf orexecuti
on.
2)LI
NKAGEEDI
TORS
Thefigur
ebelowshowst heprocessingofanobj
ectprogram usi
ngLinkageedit
or.A
l
inkageedit
orproducesal i
nkedv ersi
onofthepr
ogram –oftencall
edal oadmodule
oranexecutableimage–whi chiswr it
tent
oafi
leorlibr
aryforl
aterexecuti
on.The
l
inkedprogram producedisgener all
yinaformthati
ssui t
ableforprocessi
ngbya
rel
ocati
ngloader.
Someusefulfuncti
onsofLinkageedit
orare,anabsol
uteobjectpr
ogram canbe
cr
eated,i
fstart
ingaddressi
sal r
eadyknown.New ver
sionsoftheli
brar
ycanbe
i
ncludedwi
thoutchangi
ngthesourcepr
ogram.
object l
i
nkageedi
tor l
inked
program program
writ
tenint
oa
fi
le
8
memor
y Rel
ocat
ing
l
oader
3)DYNAMI
CLI
NKI
NG
Theschemethatpostponestheli
nki
ngf unct
ionsunt
ilexecut
ion.Asubrouti
neis
l
oadedandli
nkedtotherestoftheprogr
am whenitisfi
rstcal
led.–usuall
ycall
ed
dynami
cli
nki
ng,dy
nami cl
oadi
ngorloadoncall.
Theadvantagesofdy namicli
nkingare,i
tallowsever
alexecutingprogr
amst o
shareonecopyofasubr outi
neorl i
brary
.Inanobj ectori
entedsy stem,dynamic
l
inkingmakesi tpossi
blef oroneobjecttobeshar edbyseveralprograms.Dynamic
l
inkingprovi
dest heabi
li
t ytoloadther outi
nesonl ywhen(andif)theyareneeded.
Theact uall
oadingandlinkingcanbeaccompl ishedusingoperati
ngsy st
em serv
ice
request.
Fi
gur
ebel
owshowsl
oadi
ngandcal
l
ingofasubr
out
ineusi
ngdy
nami
cli
nki
ng
Routi
nesaredynamical
l
yloadedv i
aanoperat
ingsy
stem ser
vicer
equest
cal
led“l
oadandcal
l
”byuserprogr
am[fi
g(a)
]
TheOSexami nesitsi
nter
naltabl
etodeter
minewhet
herornottherouti
nei
s
al
readyl
oaded.ifnott
henecessaryr
out
ineisl
oadedf
rom l
i
brar
y[fi
b(b)]
Cont
rol
ispassedf
rom t
heOSt
orout
inebei
ngcal
l
ed[
fi
g(c)
]
Whenther
outi
necomplet
esit
sprocessi
ngthecont
rolr
etur
nst
otheOSi
n
or
dert
otel
lwhent
herouti
newascomplet
ed[
fi
g(d)
]
Iftherouti
newasret
ainedinthememoryasecondcal
ltoitmaynotrequi
re
anotherloadoper
ati
onr at
herthecont
rolwi
l
lbet r
ansf
err
eddir
ectl
yt othe
routi
ne[f
ig(e)
]
Dynamic Loader
(Par
tofOS)
UserPr
ogr
am
9
Dy namic Loader
(PartofOS)
“l
oadand LI
BRARY
call
”
ERRHANDL
UserPr
ogr
am
Fi
g(a) Fi
g(b)
ERRHANDL
Dynamic Loader
(Par
tofOS)
ERRHANDL
UserPr
ogr
am UserPr
ogr
am
ERRHANDL ERRHANDL
Fi
g(c) Fi
g(d) Fi
g(e)
10