program linking
DESCRIPTION
Program Linking. System Software Emergency Resolution Lecture #1. Why This Presentation?. Due to unforeseen circumstances , the entire course was abruptly terminated . Class terminated means no lectures. No lectures means no materials for exam , which, unfortunately, still exists. - PowerPoint PPT PresentationTRANSCRIPT
CPE23 KUProgram Linking
System SoftwareEmergency
Resolution Lecture #1
CPE23 KU
Why This Presentation?• Due to unforeseen circumstances,
the entire course was abruptly terminated.
• Class terminated means no lectures. No lectures means no materials for exam, which, unfortunately, still exists.
• This file fills the gap by adding material to course, even without lecture.
CPE23 KU
Agenda• Recap of our past: The SIC/XE
Civilization• Program Linking Procedures• Solution #1• Solution #2
• Linking Loader• Principles
• Overview of Post-Assembly-Time
CPE23 KU
The SIC/XE Civilization [BECK]
• Allows “library sharing” at runtime• Very important, but we can’t talk right
now…
Dynamic
Linking • Lets us create “.exe” for faster
loading• Allows us to NOT program linker in
SIC or SIC/XE
Linkage
Editor• Enables Linking supportLinking
Loader• Allows “fully multiple-file”
programmingControl Section
• M-Record• Relocation Bit Mask
Relocating Loader (SIC or SIC/XE)
• BSL, for exampleAbsolute Loader
CPE23 KU
What is Program Linking?OBJOBJOBJ
Linker
LoadModu
le
CPE23 KU
Reasons for Program Linking (1)MAIN (symbols)
Address (in Hex)
START 000000dump 00007Emem 0000BE
MATH (symbols)
Address (in Hex)
sin 00001Ecos 00003Etan 00005Easin 00007Eacos 00009Eatan 0000BE… …
Can we differentiate• MAIN.dump and
MATH.asin, or• MAIN.mem and
MATH.atanby byte number?
How can we jump from an instruction in MAIN to, say, MATH.sin, when they “share” the same “address numbers”?
You can’t answer? Good. That’s what’s supposed to happen.
CPE23 KU
Reasons for Program Linking (2)
• So why not just “include”?• Answer: It will work, but
assembly will be slowed down. Libraries like “math.h” are usually pre-compiled (.o, .lib, .pyc, …), very large, and has only some headers to help you “reach” the functions inside. Don’t be fooled by their appearances.
MAIN
MATH
#include MATH
NOTE: In real-world, “headers” usually contain only declarations. So there can already be math.h and math.dll separately. This is actually linking, but still gives illusion of “including.”
CPE23 KU
Types of Linking• Linking Loader • Link, Load later
(Linkage Editor)
Linking Loader
Addr Value01000 001003 001006 001009 0
OBJ
OBJ
OBJ
Linker
OBJ
OBJ
OBJ
Load_Module.exe*
Loader* Or “755”, you geek.
CPE23 KU
Understanding Program Offsets
• Files/CSs are joined end-to-head, like this diagram.
• Each file/CS has its own Length.
• First part starts at zero, or S1=0
• Subsequent parts start at {previous part’s start} + {previous part’s length}, or Sn=Sn-1+Ln-1 for positive int n
PROGA
PROGB
PROGC
SPROGA=0
LPROGA
SPROGB=SPROG
A+LPROGA
LPROGB
SPROGC=SPROG
B+LPROGB
LPROGC
CPE23 KU
Linking Method #1: “Naïve” Linking [CCD1]• Simple• Easy to implement• Use 2-pass style
• Fast (to run: O(2n), n: code lines)• Drawbacks:• Produces lots of M records• May or may not work with complex
expressions
CPE23 KU
Method #1: Pass 1/2• Scan H and D records only• Keep results in ESTAB
R,LISTB ,ENDB ,LISTC ,ENDC
E,000020
H,PROGA ,000000,000063
D,LISTA ,000040,ENDA ,000054
... T RECORDS ...
... M RECORDS ...
R,LISTA ,ENDA ,LISTC ,ENDC
E
H,PROGB ,000000,00007F
D,LISTB ,000060,ENDB ,000070
... T RECORDS ...
... M RECORDS ...
R,LISTA ,ENDA ,LISTB ,ENDB
E
H,PROGC ,000000,000051
D,LISTC ,000030,ENDC ,000042
... T RECORDS ...
... M RECORDS ...
Current Offset: 0 63 E2
ESTAB SymbolAddress LengthPROGA 00000 00063LISTA 00040ENDA 00054PROGB 00063 0007FLISTB 000C3ENDB 000D3PROGC 000E2 00051LISTC 00112ENDC 00124
CPE23 KU
Method #1: Pass 2/2• Time to modify our
code.• Unify H&E• Delete D&R• No ext. symbols means
no D/R• Shift T• Use ESTAB for length
detect• Substitute into & Shift M• Use ESTAB
R,LISTB ,ENDB ,LISTC ,ENDC
E,000020
H,PROGA ,000000,000063
D,LISTA ,000040,ENDA ,000054
... T RECORDS ...
... M RECORDS ...
R,LISTA ,ENDA ,LISTC ,ENDC
E
H,PROGB ,000000,00007F
D,LISTB ,000060,ENDB ,000070
... T RECORDS ...
... M RECORDS ...
R,LISTA ,ENDA ,LISTB ,ENDB
E
H,PROGC ,000000,000051
D,LISTC ,000030,ENDC ,000042
... T RECORDS ...
... M RECORDS ...
CPE23 KU
Method #1: Pass 2/2: Unify H & E
• H• Sum length,
put at top• Program start
= 0• Absolute
programs are usually not linkable…
• E• Keep execution
start address of Main CS
• Put at end of program
H,PROGA ,000000,000063
H,PROGB ,000000,00007F
H,PROGC ,000000,000051
E,000020
E
E
H,LDMODL,000000,000133
E,000020
CPE23 KU
Method #1: Pass 2/2: Shift T
• To make program codes co-exist peacefully, we need to shift addresses.
ESTAB SymbolAddress LengthPROGA 00000 00063LISTA 00040ENDA 00054PROGB 00063 0007FLISTB 000C3ENDB 000D3PROGC 000E2 00051LISTC 00112ENDC 00124
T,000020,0A,...
+0T,000020,0A,...
CS PROGA: Offset = 0
CPE23 KU
Method #1: Pass 2/2: Shift T (2)
• To make program codes co-exist peacefully, we need to shift addresses.
ESTAB SymbolAddress LengthPROGA 00000 00063LISTA 00040ENDA 00054PROGB 00063 0007FLISTB 000C3ENDB 000D3PROGC 000E2 00051LISTC 00112ENDC 00124
T,000036,0B,...
+63
CS PROGB: Offset = 0x63
T,000099,0B,...
CPE23 KU
Method #1: Pass 2/2: Shift T (3)
• To make program codes co-exist peacefully, we need to shift addresses.
ESTAB SymbolAddress LengthPROGA 00000 00063LISTA 00040ENDA 00054PROGB 00063 0007FLISTB 000C3ENDB 000D3PROGC 000E2 00051LISTC 00112ENDC 00124
T,000018,0C,...
+E2
CS PROGC: Offset = 0x63+0x7F
T,0000FA,0C,...
CPE23 KU
Method #1: Pass 2/2: Change M
• Just use ESTAB. (No expressions allowed for now)
• Don’t forget to add offset.M,000024,05,+LISTBM,000054,06,+LISTCM,000057,06,+ENDC
CS PROGA: Offset = 0
M,000024,05,+0000C3M,000054,06,+000112M,000057,06,+000124
+0 Substitute
ESTAB SymbolAddress LengthPROGA 00000 00063LISTA 00040ENDA 00054PROGB 00063 0007FLISTB 000C3ENDB 000D3PROGC 000E2 00051LISTC 00112ENDC 00124
CPE23 KU
Method #1: Pass 2/2: Change M (2)
• Don’t forget to add offset.M,000037,05,+LISTA
M,00003E,05,+ENDAM,00003E,05,-LISTA
CS PROGB: Offset = 0x63
M,00009A,05,+000040M,0000A1,05,+000054M,0000A1,05,-000040
+63 Substitute
ESTAB SymbolAddress LengthPROGA 00000 00063LISTA 00040ENDA 00054PROGB 00063 0007FLISTB 000C3ENDB 000D3PROGC 000E2 00051LISTC 00112ENDC 00124
CPE23 KU
Method #1: Pass 2/2: Change M (3)
• Offsets add up!M,000019,05,+LISTAM,00001D,05,+LISTBM,000021,05,+ENDA
CS PROGC: Offset = 0x63+0x7F
M,0000FB,05,+000040M,0000FF,05,+0000C3M,000103,05,+000054
+E2 Substitute
ESTAB SymbolAddress LengthPROGA 00000 00063LISTA 00040ENDA 00054PROGB 00063 0007FLISTB 000C3ENDB 000D3PROGC 000E2 00051LISTC 00112ENDC 00124
CPE23 KU
Method #1: Ready for Loading
• We put the H and E we processed earlier…
• And the shifted T records…
• Then the M records…
• We call this a load module.
H,LDMODL,000000,000133
E,000020
M,000024,05,+0000C3M,000054,06,+000112M,000057,06,+000124
M,00009A,05,+000040M,0000A1,05,+000054M,0000A1,05,-000040
M,0000FB,05,+000040M,0000FF,05,+0000C3M,000103,05,+000054
T,000020,0A,...
T,000099,0B,...
T,0000FA,0C,...
CPE23 KU
Method #1: Notes• Just one note: In pass-2, you just run
it through and use string processing to determine line types and act automatically. You don’t and shouldn’t need to separate the code lines into types like I demonstrated.
CPE23 KU
Method #2: Introduction [CCD2]• Likeness with Method #1• 2-pass• Utilizes ESTAB
• Difference against Method #1• More compact list of M records, which means faster Loading• Unified M record offset: No need to add/subtract by symbol
addresses• However, you may need to implement Expression (-A+B+C-
D+E…)• Creates M-Table, so this will use more memory in
implementation• And uses more time: O(2n+ms) (m: M-table size, s: # of
Symbols)
CPE23 KU
Method #2: Pass 1: M-Table
• While reading in Pass 1, we scan ALL M records to make M-table.
• M-table should “summarize” the expression. Cut those loose ends.
• For better illustration, we will add one more M-record per CS.
M,000024,05,+LISTBM,000054,06,+LISTCM,000057,06,+ENDCM,000057,06,-LISTCM,000037,05,+LISTAM,00003E,05,+ENDAM,00003E,05,-LISTAM,000070,06,+ENDAM,000019,05,+LISTAM,00001D,05,+LISTBM,000021,05,+ENDAM,000021,05,-LISTA
CPE23 KU
Method #2: Pass 1: Make the M-Table
• Make M-table!LocationLengthExpression
00024 05 +LISTB00054 06 +LISTC00057 06 +ENDC-LISTC0009A 05 +LISTA000A1 05 +ENDA-LISTA000D3 06 +ENDA000FB 05 +LISTA000FF 05 +LISTB00103 05 +ENDA-LISTA
M,000024,05,+LISTBM,000054,06,+LISTCM,000057,06,+ENDCM,000057,06,-LISTCM,000037,05,+LISTAM,00003E,05,+ENDAM,00003E,05,-LISTAM,000070,06,+ENDAM,000019,05,+LISTAM,00001D,05,+LISTBM,000021,05,+ENDAM,000021,05,-LISTA
Start = 0Length = 0x63Start = 0x63Length = 0x7F
Start = 0xE2Length = 0x51
CPE23 KU
Method #2: Pass 1.5: Evaluate M-Table
Eval. ResultType (Abs/Rel)000C3 R000112 R000012 A00040 R00014 A000054 R00040 R000C3 R00014 A
LocationLengthExpression00024 05 +LISTB00054 06 +LISTC00057 06 +ENDC-LISTC0009A 05 +LISTA000A1 05 +ENDA-LISTA000D3 06 +ENDA000FB 05 +LISTA000FF 05 +LISTB00103 05 +ENDA-LISTA
ESTAB SymbolAddressPROGA 00000LISTA 00040ENDA 00054PROGB 00063LISTB 000C3ENDB 000D3PROGC 000E2LISTC 00112ENDC 00124
ESTAB Old M-Table
TIP: If (Number of +’s == Number of –’s) then Absolute, else Relative.
Result
CPE23 KU
Method #2: Pass 2• Same thing as Method #1• Process H and E records normally
• However, …• You must add the M values right into the
code• Since M records are already processed,
we can just dump only Relative rows in.
CPE23 KU
Method #2: Pass 2: Edit T records.
T,000020,0A,03201D 77100004 ...
+000C3T,000020,0A,03201D 771000C7 ...
T,000054,0F,000014 ...
+000112T,000054,0F,000126 ...
etc.
LocationLength00024 0500054 0600057 060009A 05000A1 05000D3 06000FB 05000FF 0500103 05
Eval. ResultType000C3 R000112 R000012 A00040 R00014 A000054 R00040 R000C3 R00014 A
CPE23 KU
Method #2: Dump the M Records
• For R types only:• Dump “empty” M record
to tell Loader to relocate.• Keep only Location and
Length.
LocationLength00024 0500054 0600057 060009A 05000A1 05000D3 06000FB 05000FF 0500103 05
Eval. ResultType000C3 R000112 R000012 A00040 R00014 A000054 R00040 R000C3 R00014 A
M,000024,05M,000054,06
M,00009A,05M,0000D3,06
M,0000FB,05M,0000FF,05
CPE23 KU
Method #2: Finished• Just dump the
modified T records.
• Add the M lines
• The rest is no different.
T ...
T ...
M,000024,05M,000054,06M ...
M ...
M ...
H,LDMODL,000000,000133
E,000020
T,000020,0A,03201D 771000C7...T,000054,0F,000126 ...T ...
CPE23 KU
Further Reading• [BECK] L.L. Beck. "Loaders and Linkers" in System
Software: An Introduction to Systems Programming, 3rd ed. Addison-Wesley, 1997, ch 3. (ISBN: 0201423006)
• [CCD1] C. Chatdokmaiprai. (2004, August 25). Load Module & Linker (Solution #1) [Online]. Available: http://cpe.ku.ac.th/~ccd/204331/lmdfmt1.pdf
• [CCD2] ____________. (2004, August 25). Load Module & Linker (Solution #2) [Online]. Available: http://cpe.ku.ac.th/~ccd/204331/lmdfmt2.pdf
• [CSSH] C.S. Shieh. (?). Linking Loader for SIC/XE Machine [Online]. Available: http://bit.kuas.edu.tw/~csshieh/teach/93A/sp/note/sp07.ppt