CS241 Notes
Post on 17-Oct-2015
100 Views
Preview:
DESCRIPTION
Transcript
5/27/2018 CS241 Notes
1/100
5/27/2018 CS241 Notes
2/100
5/27/2018 CS241 Notes
3/100
5/27/2018 CS241 Notes
4/100
5/27/2018 CS241 Notes
5/100
5/27/2018 CS241 Notes
6/100
5/27/2018 CS241 Notes
7/100
5/27/2018 CS241 Notes
8/100
5/27/2018 CS241 Notes
9/100
5/27/2018 CS241 Notes
10/100
5/27/2018 CS241 Notes
11/100
5/27/2018 CS241 Notes
12/100
5/27/2018 CS241 Notes
13/100
5/27/2018 CS241 Notes
14/100
5/27/2018 CS241 Notes
15/100
5/27/2018 CS241 Notes
16/100
5/27/2018 CS241 Notes
17/100
5/27/2018 CS241 Notes
18/100
5/27/2018 CS241 Notes
19/100
5/27/2018 CS241 Notes
20/100
5/27/2018 CS241 Notes
21/100
5/27/2018 CS241 Notes
22/100
5/27/2018 CS241 Notes
23/100
5/27/2018 CS241 Notes
24/100
5/27/2018 CS241 Notes
25/100
5/27/2018 CS241 Notes
26/100
5/27/2018 CS241 Notes
27/100
5/27/2018 CS241 Notes
28/100
5/27/2018 CS241 Notes
29/100
Loader handoutAuthor: Nomair A. Naeem
This handout is intended to accompany the class lectures on theLoader and Relocator. The handout should NOT be considered asubstitute for the lecture. Dissemination of this handout (includ-ing posting on a website) is explicitly prohibited unless permission isobtained. Please report any errors to nanaeem@uwaterloo.ca.
1 Steps to manually create a MERL file in As-
sembly
The following steps create a MERL file (in ASSEMBLY) that can then be
assembled using an assembler that does not know of the MERL file format e.g.cs241.binasm.
.word A
........ .word B
Assembly
reloc1: .word A
........reloc2: .word B
..........
Add new labelsfor each use of
.word label
Add a labelto mark theend of code
reloc1: .word A
.........reloc2: .word B ............endCode:
Create TableEntries
reloc1: .word A
.........reloc2: .word B ............endCode:.word 1.word reloc1.word 1.word reloc2
The 1 here is a FORMATCODE for relocation
The next line gives thelocation to be relocated
We will see otherformat codes for Linking
Add a label tomark the
end of table
reloc1: .word A
.........reloc2: .word B ............endCode:.word 1.word reloc1.word 1.word reloc2endModule:
Add Header
0x10000002(i.e. beq $0, $0, 2).word endModule
.word endCode.........
reloc1: .word A .........
reloc2: .word B ............
endCode:.word 1.word reloc1.word 1.word reloc2endModule:The first instruction in the header results in a MERL file to act as a normal executable MIPS program.
If a relocator is not being used, the first instruction simply causes the program to jump the next two words in memory andhence the program can run without any relocation with starting memory address 0
If a relocator is used, the relocator knows where to find the information.
1
5/27/2018 CS241 Notes
30/100
The following example illustrates the MERL file in assembly, the memory ad-dresses for each line when loaded at starting address 0, and the actual 32-bit
binary for each line in the assembly file
; .merl VERSION OF RELOCATION EXAMPLE; MERL = MIPS executable relocatable linkable
; address machine l.beq $0, $0, 2 ; skip over header ; 0x00000000 0x10000002.word endmodule ; 0x00000004 0x0000003c.word endcode ; 0x00000008 0x0000002c
lis $3 ; 0x0000000c 0x00001814 .word 0xabc ; 0x00000010 0x00000abc lis $1 ; 0x00000014 0x00000814reloc1: .word A ; 0x00000018 0x00000024 jr $1 ; 0x0000001c 0x00200008 B:
jr $31 ; 0x00000020 0x03e00008 A:
beq $0, $0, B ; 0x00000024 0x1000fffereloc2: .word B ; 0x00000028 0x00000020endcode:
.word 1 ; relocate ; 0x0000002c 0x00000001
.word reloc1 ; location ; 0x00000030 0x00000018
.word 1 ; relocate ; 0x00000034 0x00000001
.word reloc2 ; location ; 0x00000038 0x00000028endmodule:
The last column above is the input to a RELOCATABLE LOADER i.e. a loaderwhich knows how to read the MERL format to relocate the code at any startingaddress . The relocating loader first loads the program at starting address as shown in the diagram on the following page:
(Note: in the diagram below the instruction beq is shown for clarity. Thislocation will in fact contain the 32-bit binary for the beq instruction from theheader. )
2
5/27/2018 CS241 Notes
31/100
--------------------------beq $0, $0, 2endModuleendCode--------------------------
CODE
--------------------------
TABLE
--------------------------
!
!+ 4!+ 8
!+ endCode
!+ endModule
OriginalAddress
048
endCode
endModule
RelocatedAddress
The relocator runs the following code to relocate each relocation entry:
3
5/27/2018 CS241 Notes
32/100
i + MEM[+8] The third word in the original code contained the addressof the endCode label.
Since the program is now loaded at , the 3rd wordis +8. Therefore we read the memory at this locationThis gives us the value of endCode in the original codeSince the program is loaded at we add to this AGAIN!!
end + MEM[+4] The 2nd word in memory contains the address of the end of So we load it through MEM[+4]The value at that address was based on addresses starting atso we add
while ( i < end){ This loop takes us through the footer table containingrelocation entries
if (MEM[i] == 1) { Remember that each entry is 2 words. The first word has valONE to indicate a relocation entry
MEM[
+ MEM[i+4]] +=
Since MEM[i] is the .word 1, MEM[i+4] is the address thatcontained a .word label.Since we started at , the .word label is actually at+ MEM[i+4]We READ in that value MEM[+ MEM[i+4]] and thisrepresents what .word label would have had we started at 0.Add to this, and STORE IT BACK
i +=8; Jump to next table entry}
elseERROR
}
TOOLS
Relocation Tool: cs241.merl
Input: merl file and relocation address
Output: NON-relocatable mips file (MERL HEADER AND FOOTERHAS BEEN REMOVEDready to load at given address
NOTE: mips.twoint, mips.address CAN take a second argument: an ad-dress at which to load a mips file
Relocatable Assembler: cs241.relasmA tool which knows what to write in the header and table to create a relocatablemerl file
4
5/27/2018 CS241 Notes
33/100
5/27/2018 CS241 Notes
34/100
5/27/2018 CS241 Notes
35/100
5/27/2018 CS241 Notes
36/100
5/27/2018 CS241 Notes
37/100
5/27/2018 CS241 Notes
38/100
5/27/2018 CS241 Notes
39/100
5/27/2018 CS241 Notes
40/100
5/27/2018 CS241 Notes
41/100
5/27/2018 CS241 Notes
42/100
5/27/2018 CS241 Notes
43/100
5/27/2018 CS241 Notes
44/100
5/27/2018 CS241 Notes
45/100
5/27/2018 CS241 Notes
46/100
5/27/2018 CS241 Notes
47/100
5/27/2018 CS241 Notes
48/100
5/27/2018 CS241 Notes
49/100
5/27/2018 CS241 Notes
50/100
5/27/2018 CS241 Notes
51/100
5/27/2018 CS241 Notes
52/100
5/27/2018 CS241 Notes
53/100
5/27/2018 CS241 Notes
54/100
5/27/2018 CS241 Notes
55/100
5/27/2018 CS241 Notes
56/100
5/27/2018 CS241 Notes
57/100
5/27/2018 CS241 Notes
58/100
5/27/2018 CS241 Notes
59/100
5/27/2018 CS241 Notes
60/100
5/27/2018 CS241 Notes
61/100
5/27/2018 CS241 Notes
62/100
5/27/2018 CS241 Notes
63/100
5/27/2018 CS241 Notes
64/100
5/27/2018 CS241 Notes
65/100
5/27/2018 CS241 Notes
66/100
5/27/2018 CS241 Notes
67/100
5/27/2018 CS241 Notes
68/100
5/27/2018 CS241 Notes
69/100
5/27/2018 CS241 Notes
70/100
5/27/2018 CS241 Notes
71/100
5/27/2018 CS241 Notes
72/100
5/27/2018 CS241 Notes
73/100
5/27/2018 CS241 Notes
74/100
5/27/2018 CS241 Notes
75/100
5/27/2018 CS241 Notes
76/100
5/27/2018 CS241 Notes
77/100
5/27/2018 CS241 Notes
78/100
5/27/2018 CS241 Notes
79/100
Semantic Rules for WLPP
CS 241
Syntactic Categories
E {expr, term, factor, lvalue} T {test} S{statement, statements} {int, int}
Type Derivation Rules
[Literals and identifiers]NUM :int NULL :int
hid, i decls
id:
[Parenthesized expressions] E :
(E) :
[Pointers] E :int
&E: int
E :int
E: int
E :int
new int [E] :int
[Addition] E1: int E 2: int
E1 + E2: int
E1: int E2: int
E1 + E2: int
E1: int E 2: int
E1 + E2: int
[Subtraction] E1: int E 2: int
E1 E2 : int
E1: int E2: int
E1 E2: int
E1: int E2: int
E1 E2: int
[Multiplication and division] E1: int E 2 : int
E1 E2: int
E1 : int E 2: int
E1/ E2 : int
E1: int E 2: int
E1% E2 : int
1
5/27/2018 CS241 Notes
80/100
Type Correctness Rules
[Comparisons] E1: E2:
well-typed(E1 == E2)
E1: E2:
well-typed(E1! = E2)
E1 : E2:
well-typed(E1 < E2)
E1: E2:
well-typed(E1 E2)
E1 : E2:
well-typed(E1 >= E2)
[Control flow] well-typed(T) well-typed(S)
well-typed(while ( T) { S})
well-typed(T) well-typed(S1) well-typed(S2well-typed(if ( T) { S1} else { S2})
[Deallocation] E : int
well-typed(delete [ ] E; )
[Printing] E: int
well-typed(printlnE; )
[Assignment] E1: E2:
well-typed(E1 = E2; )
[Sequencing]well-typed()
well-typed(S1) well-typed(S2)
well-typed(S1 S2)
[Declns]well-typed()
well-typed(dcls)well-typed(dclsint id= NUM; )
well-typed(dcls)well-typed(dclsint* id= NULL
[Procedure] decl2: int well-typed(decls) well-typed(S) E : int
well-typed(int wain(decl1, decl2) {decls Sreturn E ;})
2
5/27/2018 CS241 Notes
81/100
5/27/2018 CS241 Notes
82/100
5/27/2018 CS241 Notes
83/100
5/27/2018 CS241 Notes
84/100
5/27/2018 CS241 Notes
85/100
5/27/2018 CS241 Notes
86/100
5/27/2018 CS241 Notes
87/100
5/27/2018 CS241 Notes
88/100
5/27/2018 CS241 Notes
89/100
5/27/2018 CS241 Notes
90/100
5/27/2018 CS241 Notes
91/100
5/27/2018 CS241 Notes
92/100
5/27/2018 CS241 Notes
93/100
5/27/2018 CS241 Notes
94/100
5/27/2018 CS241 Notes
95/100
5/27/2018 CS241 Notes
96/100
5/27/2018 CS241 Notes
97/100
5/27/2018 CS241 Notes
98/100
5/27/2018 CS241 Notes
99/100
5/27/2018 CS241 Notes
100/100
top related