100% found this document useful (1 vote)
221 views

CS241 Notes

This document provides steps for manually creating a MERL (MIPS executable relocatable linkable) file in assembly language. It begins by showing the assembly code and modifications needed, including adding labels and a table with entries for relocation. It then provides an example MERL file and explains how a relocator works by loading the program at a starting address and adjusting values based on relocation entries.

Uploaded by

fairwayhills702
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
221 views

CS241 Notes

This document provides steps for manually creating a MERL (MIPS executable relocatable linkable) file in assembly language. It begins by showing the assembly code and modifications needed, including adding labels and a table with entries for relocation. It then provides an example MERL file and explains how a relocator works by loading the program at a starting address and adjusting values based on relocation entries.

Uploaded by

fairwayhills702
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 100

Author: Nomair A. Naeem This handout is intended to accompany the class lectures on the Loader and Relocator.

The handout should NOT be considered a substitute for the lecture. Dissemination of this handout (including posting on a website) is explicitly prohibited unless permission is obtained. Please report any errors to [email protected].

Loader handout

Steps to manually create a MERL le in Assembly

The following steps create a MERL le (in ASSEMBLY) that can then be assembled using an assembler that does not know of the MERL le format e.g. cs241.binasm.
Assembly

.word A ........ .word B 0x10000002


(i.e. beq $0, $0, 2)

Add new labels for each use of .word label

reloc1: .word A ........ reloc2: .word B ..........

Add a label to mark the end of code

reloc1: .word A ......... reloc2: .word B ............ endCode:


Create Table Entries

.word endModule .word endCode ......... reloc1: .word A ......... reloc2: .word B ............ endCode: .word 1 .word reloc1 .word 1 .word reloc2 endModule:

Add Header

reloc1: .word A ......... reloc2: .word B ............ endCode: .word 1 .word reloc1 .word 1 .word reloc2 endModule:

Add a label to mark the end of table

reloc1: .word A ......... reloc2: .word B ............ endCode: .word 1 .word reloc1 .word 1 .word reloc2

The 1 here is a FORMAT CODE for relocation The next line gives the location to be relocated

We will see other format codes for Linking

The rst instruction in the header results in a MERL le to act as a normal executable MIPS program. If a relocator is not being used, the rst instruction simply causes the program to jump the next two words in memory and hence the program can run without any relocation with starting memory address 0 If a relocator is used, the relocator knows where to nd the information.

The following example illustrates the MERL le in assembly, the memory addresses for each line when loaded at starting address 0, and the actual 32-bit binary for each line in the assembly le

; .merl VERSION OF RELOCATION EXAMPLE ; MERL = MIPS executable relocatable linkable ; address beq $0, $0, 2 ; skip over header ; 0x00000000 .word endmodule ; 0x00000004 .word endcode ; 0x00000008 lis $3 .word 0xabc lis $1 reloc1: .word A jr $1 B: jr $31 A: beq $0, $0, B reloc2: .word B endcode: .word 1 ; relocate .word reloc1 ; location .word 1 ; relocate .word reloc2 ; location endmodule: ; ; ; ; ; 0x0000000c 0x00000010 0x00000014 0x00000018 0x0000001c machine l. 0x10000002 0x0000003c 0x0000002c 0x00001814 0x00000abc 0x00000814 0x00000024 0x00200008 0x03e00008 0x1000fffe 0x00000020 0x00000001 0x00000018 0x00000001 0x00000028

; 0x00000020 ; 0x00000024 ; 0x00000028 ; 0x0000002c ; 0x00000030 ; 0x00000034 ; 0x00000038

The last column above is the input to a RELOCATABLE LOADER i.e. a loader which knows how to read the MERL format to relocate the code at any starting address . The relocating loader rst 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. This location will in fact contain the 32-bit binary for the beq instruction from the header. )

Relocated Address

Original Address

-------------------------! beq $0, $0, 2 0 ! + 4 endModule 4 ! + 8 endCode 8 -------------------------CODE ! + endCode -------------------------TABLE ! + endModule -------------------------- endModule endCode

The relocator runs the following code to relocate each relocation entry:

i + MEM[+8]

end + MEM[+4]

while ( i < end){ if (MEM[i] == 1) { MEM[ + MEM[i+4]] +=

i +=8; } else } TOOLS Relocation Tool: cs241.merl ERROR

The third word in the original code contained the address of the endCode label. Since the program is now loaded at , the 3rd word is +8. Therefore we read the memory at this location This gives us the value of endCode in the original code Since the program is loaded at we add to this AGAIN!! The 2nd word in memory contains the address of the end of le So we load it through MEM[+4] The value at that address was based on addresses starting at 0, so we add This loop takes us through the footer table containing relocation entries Remember that each entry is 2 words. The rst word has value ONE to indicate a relocation entry Since MEM[i] is the .word 1, MEM[i+4] is the address that contained 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 this represents what .word label would have had we started at 0. Add to this, and STORE IT BACK Jump to next table entry

Input: merl le and relocation address Output: NON-relocatable mips le (MERL HEADER AND FOOTER HAS BEEN REMOVED ready to load at given address NOTE: mips.twoint, mips.address CAN take a second argument: an address at which to load a mips le Relocatable Assembler: cs241.relasm A tool which knows what to write in the header and table to create a relocatable merl le

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 identiers] hid, i decls id :

NUM : int

NULL : int E: (E ) :

[Parenthesized expressions] E : int &E : int E : int E : int

[Pointers]

E : int new int [E ] : int E1 : int E2 : int E1 + E2 : int E1 : int E2 : int E1 E2 : int E1 : int E2 : int E1 % E2 : int

[Addition]

E1 : int E2 : int E1 + E2 : int E1 : int E2 : int E1 E2 : int

E1 : int E2 : int E1 + E2 : int E1 : int E2 : int E1 E2 : int

[Subtraction]

[Multiplication and division]

E1 : int E2 : int E1 E2 : int

E1 : int E2 : int E1 / E2 : int

Type Correctness Rules


[Comparisons] E1 : E 2 : well-typed(E1 == E2 ) E1 : E 2 : well-typed(E1 ! = E2 ) E1 : E 2 : well-typed(E1 < E2 )

E1 : E 2 : well-typed(E1 <= E2 ) [Control ow]

E1 : E 2 : well-typed(E1 > E2 )

E1 : E 2 : well-typed(E1 >= E2 )

well-typed(T ) well-typed(S ) well-typed(while ( T ) { S }) [Deallocation]

well-typed(T ) well-typed(S1 ) well-typed(S2 ) well-typed(if ( T ) { S1 } else { S2 }) E : int well-typed(delete [ ] E ; )

[Printing]

E : int well-typed(println E ; ) E1 : E 2 : well-typed(E1 = E2 ; ) well-typed(S1 ) well-typed(S2 ) well-typed(S1 S2 ) well-typed(dcls ) well-typed(dcls int* id = NULL; )

[Assignment]

[Sequencing]

well-typed()

[Declns]

well-typed() [Procedure]

well-typed(dcls ) well-typed(dcls int id = NUM ; )

decl 2 : int well-typed(decls ) well-typed(S ) E : int well-typed(int wain(decl 1 , decl 2 ) {decls S return E ; })

You might also like