310 likes | 550 Views
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.
E N D
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. • This file fills the gap by adding material to course, even without lecture.
Agenda • Recap of our past: The SIC/XE Civilization • Program Linking Procedures • Solution #1 • Solution #2 • Linking Loader • Principles • Overview of Post-Assembly-Time
The SIC/XE Civilization [BECK] • Allows “library sharing” at runtime • Very important, but we can’t talk right now… • Dynamic Linking • Linkage Editor • Lets us create “.exe” for faster loading • Allows us to NOT program linker in SIC or SIC/XE • Linking Loader • Enables Linking support • Control Section • Allows “fully multiple-file” programming • Relocating Loader (SIC or SIC/XE) • M-Record • Relocation Bit Mask • Absolute Loader • BSL, for example
What is Program Linking? OBJ Load Module OBJ Linker OBJ
Reasons for Program Linking (1) • 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.
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. #include MATH MAIN 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.”
Types of Linking • Linking Loader • Link, Load later (Linkage Editor) OBJ OBJ OBJ OBJ OBJ OBJ Linker Linking Loader Load_Module.exe* Loader * Or “755”, you geek.
Understanding Program Offsets SPROGA=0 PROGA • 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 LPROGA SPROGB=SPROGA+LPROGA PROGB LPROGB SPROGC=SPROGB+LPROGB PROGC LPROGC
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
Method #1: Pass 1/2 H,PROGA ,000000,000063 D,LISTA ,000040,ENDA ,000054 • Scan H and D records only • Keep results in ESTAB R,LISTB ,ENDB ,LISTC ,ENDC ... T RECORDS ...... M RECORDS ... E,000020 Current Offset: 0 63 E2 H,PROGB ,000000,00007F H,PROGC ,000000,000051 ESTAB Symbol Address Length D,LISTC ,000030,ENDC ,000042 D,LISTB ,000060,ENDB ,000070 PROGA 00000 00063 R,LISTA ,ENDA ,LISTC ,ENDC R,LISTA ,ENDA ,LISTB ,ENDB LISTA 00040 ... T RECORDS ...... M RECORDS ... ... T RECORDS ...... M RECORDS ... ENDA 00054 E E PROGB 00063 0007F LISTB 000C3 ENDB 000D3 PROGC 000E2 00051 LISTC 00112 ENDC 00124
Method #1: Pass 2/2 H,PROGA ,000000,000063 D,LISTA ,000040,ENDA ,000054 • 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 ... T RECORDS ...... M RECORDS ... E,000020 H,PROGC ,000000,000051 H,PROGB ,000000,00007F D,LISTC ,000030,ENDC ,000042 D,LISTB ,000060,ENDB ,000070 R,LISTA ,ENDA ,LISTC ,ENDC R,LISTA ,ENDA ,LISTB ,ENDB ... T RECORDS ...... M RECORDS ... ... T RECORDS ...... M RECORDS ... E E
Method #1: Pass 2/2: Unify H & E H,LDMODL,000000,000133 • 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 E,000020 H,PROGB ,000000,00007F E H,PROGC ,000000,000051 E E,000020
Method #1: Pass 2/2: Shift T • To make program codes co-exist peacefully, we need to shift addresses. CS PROGA: Offset = 0 T,000020,0A,... ESTAB Symbol Address Length PROGA 00000 00063 +0 LISTA 00040 ENDA 00054 T,000020,0A,... PROGB 00063 0007F LISTB 000C3 ENDB 000D3 PROGC 000E2 00051 LISTC 00112 ENDC 00124
Method #1: Pass 2/2: Shift T (2) • To make program codes co-exist peacefully, we need to shift addresses. CS PROGB: Offset = 0x63 T,000036,0B,... ESTAB Symbol Address Length PROGA 00000 00063 +63 LISTA 00040 ENDA 00054 T,000099,0B,... PROGB 00063 0007F LISTB 000C3 ENDB 000D3 PROGC 000E2 00051 LISTC 00112 ENDC 00124
Method #1: Pass 2/2: Shift T (3) • To make program codes co-exist peacefully, we need to shift addresses. CS PROGC: Offset = 0x63+0x7F T,000018,0C,... ESTAB Symbol Address Length PROGA 00000 00063 +E2 LISTA 00040 ENDA 00054 T,0000FA,0C,... PROGB 00063 0007F LISTB 000C3 ENDB 000D3 PROGC 000E2 00051 LISTC 00112 ENDC 00124
Method #1: Pass 2/2: Change M • Just use ESTAB. (No expressions allowed for now) • Don’t forget to add offset. CS PROGA: Offset = 0 M,000024,05,+LISTBM,000054,06,+LISTCM,000057,06,+ENDC ESTAB Symbol Address Length PROGA 00000 00063 LISTA 00040 +0 Substitute ENDA 00054 PROGB 00063 0007F M,000024,05,+0000C3M,000054,06,+000112M,000057,06,+000124 LISTB 000C3 ENDB 000D3 PROGC 000E2 00051 LISTC 00112 ENDC 00124
Method #1: Pass 2/2: Change M (2) • Don’t forget to add offset. CS PROGB: Offset = 0x63 M,000037,05,+LISTAM,00003E,05,+ENDAM,00003E,05,-LISTA ESTAB Symbol Address Length PROGA 00000 00063 LISTA 00040 +63 Substitute ENDA 00054 PROGB 00063 0007F M,00009A,05,+000040M,0000A1,05,+000054M,0000A1,05,-000040 LISTB 000C3 ENDB 000D3 PROGC 000E2 00051 LISTC 00112 ENDC 00124
Method #1: Pass 2/2: Change M (3) • Offsets add up! CS PROGC: Offset = 0x63+0x7F M,000019,05,+LISTAM,00001D,05,+LISTBM,000021,05,+ENDA ESTAB Symbol Address Length PROGA 00000 00063 LISTA 00040 +E2 Substitute ENDA 00054 PROGB 00063 0007F M,0000FB,05,+000040M,0000FF,05,+0000C3M,000103,05,+000054 LISTB 000C3 ENDB 000D3 PROGC 000E2 00051 LISTC 00112 ENDC 00124
Method #1: Ready for Loading H,LDMODL,000000,000133 • We put the H and E we processed earlier… • And the shifted T records… • Then the M records… • We call this a load module. T,000020,0A,... T,000099,0B,... T,0000FA,0C,... 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 E,000020
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.
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)
Method #2: Pass 1: M-Table M,000024,05,+LISTBM,000054,06,+LISTCM,000057,06,+ENDC M,000057,06,-LISTC • 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,000037,05,+LISTAM,00003E,05,+ENDAM,00003E,05,-LISTA M,000070,06,+ENDA M,000019,05,+LISTAM,00001D,05,+LISTBM,000021,05,+ENDA M,000021,05,-LISTA
Method #2: Pass 1: Make the M-Table • Make M-table! Start = 0 Length = 0x63 M,000024,05,+LISTB M,000054,06,+LISTC M,000057,06,+ENDC M,000057,06,-LISTC Location Length Expression 00024 05 +LISTB 00054 06 +LISTC 00057 06 +ENDC - LISTC Start = 0x63 Length = 0x7F M,000037,05,+LISTA M,00003E,05,+ENDA M,00003E,05,-LISTA M,000070,06,+ENDA 0009A 05 +LISTA 000A1 05 +ENDA - LISTA 000D3 06 +ENDA M,000019,05,+LISTA M,00001D,05,+LISTB M,000021,05,+ENDA M,000021,05,-LISTA Start = 0xE2 Length = 0x51 000FB 05 +LISTA 000FF 05 +LISTB 00103 05 +ENDA - LISTA
Method #2: Pass 1.5: Evaluate M-Table Result ESTAB Old M-Table Location Length Expression ESTAB Symbol Address Eval . Result Type (Abs/ Rel ) 00024 05 +LISTB PROGA 00000 000C3 R 00054 06 +LISTC LISTA 00040 000112 R 00057 06 +ENDC - LISTC ENDA 00054 000012 A 0009A 05 +LISTA PROGB 00063 00040 R 000A1 05 +ENDA - LISTA LISTB 000C3 00014 A 000D3 06 +ENDA ENDB 000D3 000054 R 000FB 05 +LISTA PROGC 000E2 00040 R 000FF 05 +LISTB LISTC 00112 000C3 R 00103 05 +ENDA - LISTA ENDC 00124 00014 A TIP: If (Number of +’s == Number of –’s) then Absolute, else Relative.
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.
Method #2: Pass 2: Edit T records. T,000020,0A,03201D 77100004 ... Location Length Eval . Result Type 00024 05 000C3 R +000C3 00054 06 000112 R 00057 06 000012 A T,000020,0A,03201D 771000C7 ... 0009A 05 00040 R 000A1 05 00014 A T,000054,0F,000014 ... 000D3 06 000054 R +000112 000FB 05 00040 R 000FF 05 000C3 R T,000054,0F,000126 ... 00103 05 00014 A etc.
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 Eval . Result Type 00024 05 000C3 R 00054 06 000112 R 00057 06 000012 A 0009A 05 00040 R 000A1 05 00014 A M,000024,05 M,000054,06 000D3 06 000054 R 000FB 05 00040 R M,00009A,05 M,0000D3,06 000FF 05 000C3 R 00103 05 00014 A M,0000FB,05 M,0000FF,05
Method #2: Finished H,LDMODL,000000,000133 • Just dump the modified T records. • Add the M lines • The rest is no different. T,000020,0A,03201D 771000C7... T,000054,0F,000126 ... T ... T ... T ... M,000024,05 M,000054,06 M ... M ... M ... E,000020
Further Reading • [BECK] L.L. Beck. "Loaders and Linkers" in System Software: An Introduction to Systems Programming, 3rd ed. Addison-Wesley, 1997, ch3. (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