system software emergency resolution lecture...
TRANSCRIPT
CPE23 KU
Program Linking
System Software
Emergency 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 support Linking Loader
• Allows “fully multiple-file” programming Control Section
• M-Record • Relocation Bit Mask
Relocating Loader (SIC or SIC/XE)
• BSL, for example Absolute Loader
CPE23 KU What is Program Linking?
OBJ
OBJ
OBJ
Linker Load Module Load
Module
CPE23 KU Reasons for Program Linking (1)
MAIN (symbols) Address (in Hex)
START 000000
dump 00007E
mem 0000BE
MATH (symbols) Address (in Hex)
sin 00001E
cos 00003E
tan 00005E
asin 00007E
acos 00009E
atan 0000BE
… …
Can we differentiate • MAIN.dump and MATH.asin, or • MAIN.mem and MATH.atan by 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 Value
01000 0
01003 0
01006 0
01009 0
OBJ OBJ OBJ
Linker
OBJ OBJ OBJ
Load_Module.exe* 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=SPROGA+LPROGA
LPROGB SPROGC=SPROGB+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 Symbol Address Length
PROGA 00000 00063
LISTA 00040
ENDA 00054
PROGB 00063 0007F
LISTB 000C3
ENDB 000D3
PROGC 000E2 00051
LISTC 00112
ENDC 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 Symbol Address Length
PROGA 00000 00063
LISTA 00040
ENDA 00054
PROGB 00063 0007F
LISTB 000C3
ENDB 000D3
PROGC 000E2 00051
LISTC 00112
ENDC 00124
T,000020,0A,...
+0 T,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 Symbol Address Length
PROGA 00000 00063
LISTA 00040
ENDA 00054
PROGB 00063 0007F
LISTB 000C3
ENDB 000D3
PROGC 000E2 00051
LISTC 00112
ENDC 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 Symbol Address Length
PROGA 00000 00063
LISTA 00040
ENDA 00054
PROGB 00063 0007F
LISTB 000C3
ENDB 000D3
PROGC 000E2 00051
LISTC 00112
ENDC 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,+LISTB
M,000054,06,+LISTC
M,000057,06,+ENDC
CS PROGA: Offset = 0
M,000024,05,+0000C3
M,000054,06,+000112
M,000057,06,+000124
+0 Substitute
ESTAB Symbol Address Length
PROGA 00000 00063
LISTA 00040
ENDA 00054
PROGB 00063 0007F
LISTB 000C3
ENDB 000D3
PROGC 000E2 00051
LISTC 00112
ENDC 00124
CPE23 KU Method #1: Pass 2/2: Change M (2)
• Don’t forget to add offset.
M,000037,05,+LISTA
M,00003E,05,+ENDA
M,00003E,05,-LISTA
CS PROGB: Offset = 0x63
M,00009A,05,+000040
M,0000A1,05,+000054
M,0000A1,05,-000040
+63 Substitute
ESTAB Symbol Address Length
PROGA 00000 00063
LISTA 00040
ENDA 00054
PROGB 00063 0007F
LISTB 000C3
ENDB 000D3
PROGC 000E2 00051
LISTC 00112
ENDC 00124
CPE23 KU Method #1: Pass 2/2: Change M (3)
• Offsets add up!
M,000019,05,+LISTA
M,00001D,05,+LISTB
M,000021,05,+ENDA
CS PROGC: Offset = 0x63+0x7F
M,0000FB,05,+000040
M,0000FF,05,+0000C3
M,000103,05,+000054
+E2 Substitute
ESTAB Symbol Address Length
PROGA 00000 00063
LISTA 00040
ENDA 00054
PROGB 00063 0007F
LISTB 000C3
ENDB 000D3
PROGC 000E2 00051
LISTC 00112
ENDC 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,+0000C3
M,000054,06,+000112
M,000057,06,+000124
M,00009A,05,+000040
M,0000A1,05,+000054
M,0000A1,05,-000040
M,0000FB,05,+000040
M,0000FF,05,+0000C3
M,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,+LISTB
M,000054,06,+LISTC
M,000057,06,+ENDC
M,000057,06,-LISTC
M,000037,05,+LISTA
M,00003E,05,+ENDA
M,00003E,05,-LISTA
M,000070,06,+ENDA
M,000019,05,+LISTA
M,00001D,05,+LISTB
M,000021,05,+ENDA
M,000021,05,-LISTA
CPE23 KU Method #2: Pass 1: Make the M-Table
• Make M-table! Location Length Expression
00024 05 +LISTB
00054 06 +LISTC
00057 06 +ENDC - LISTC
0009A 05 +LISTA
000A1 05 +ENDA - LISTA
000D3 06 +ENDA
000FB 05 +LISTA
000FF 05 +LISTB
00103 05 +ENDA - LISTA
M,000024,05,+LISTB
M,000054,06,+LISTC
M,000057,06,+ENDC
M,000057,06,-LISTC
M,000037,05,+LISTA
M,00003E,05,+ENDA
M,00003E,05,-LISTA
M,000070,06,+ENDA
M,000019,05,+LISTA
M,00001D,05,+LISTB
M,000021,05,+ENDA
M,000021,05,-LISTA
Start = 0 Length = 0x63
Start = 0x63 Length = 0x7F
Start = 0xE2 Length = 0x51
CPE23 KU Method #2: Pass 1.5: Evaluate M-Table
Eval . Result Type (Abs/ Rel )
000C3 R
000112 R
000012 A
00040 R
00014 A
000054 R
00040 R
000C3 R
00014 A
Location Length Expression
00024 05 +LISTB
00054 06 +LISTC
00057 06 +ENDC - LISTC
0009A 05 +LISTA
000A1 05 +ENDA - LISTA
000D3 06 +ENDA
000FB 05 +LISTA
000FF 05 +LISTB
00103 05 +ENDA - LISTA
ESTAB Symbol Address
PROGA 00000
LISTA 00040
ENDA 00054
PROGB 00063
LISTB 000C3
ENDB 000D3
PROGC 000E2
LISTC 00112
ENDC 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 ...
+000C3 T,000020,0A,03201D 771000C7 ...
T,000054,0F,000014 ...
+000112 T,000054,0F,000126 ...
etc.
Location Length
00024 05
00054 06
00057 06
0009A 05
000A1 05
000D3 06
000FB 05
000FF 05
00103 05
Eval . Result Type
000C3 R
000112 R
000012 A
00040 R
00014 A
000054 R
00040 R
000C3 R
00014 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.
Location Length
00024 05
00054 06
00057 06
0009A 05
000A1 05
000D3 06
000FB 05
000FF 05
00103 05
Eval . Result Type
000C3 R
000112 R
000012 A
00040 R
00014 A
000054 R
00040 R
000C3 R
00014 A
M,000024,05
M,000054,06
M,00009A,05
M,0000D3,06
M,0000FB,05
M,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,05
M,000054,06
M ...
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