Assignment 12 and 13 (200 points) Solution

$30.00

Description

1 Binary Loader (100 points)

You will mimic the job of a binary loader (in a highly simplified manner). You will write a program that will load code from a file, and execute the code. You will be provided a function signature and a binary file that contains the raw bytes of the function’s code. The function is a pre-compiled 32 bit binary. It is not an elf binary. It contains the raw bytes of a calculator function. It has been tested on remote.cs.binghamton.edu.

File func.bin contains a function with signature int calc(char operator, int num1, int num2);. Implement the main function within loader.c.

NOTE: The below instructions are only pointers to help you get started. You are en- couraged to experiment and try different library functions. For example, you may want to experiment with open and read instead of fopen and fread. Or, if you feel adventurous, you may want to allocate memory on the heap, read the raw bytes on to the heap, call mprotect to make the page that contains the raw bytes executable, and execute it.

The main function must accept exactly 4 arguments. Otherwise, main must print “Usage” message and exit. The first argument is the name of the file that contain the binary function, the second argument and fourth argument are integers, and the third argument is a character that represents an operation.

Main function will open the file with first argument as the name. (HINT: use fopen.

The second argument to fopen is the mode in which the file is opened. Because you are opening a binary file, the mode must be rb. Refer man page for fopen for more details).

Main function will read the contents of the file into a local array that is large enough to contain the entire file. (HINT: use fread. You could either read character by character or first calculate number of bytes in the file, and read them all in one call to fread. Refer to man page for fread for usage.)

Declare a function pointer type with name Calc fptr with the same signature as calc.

Convert the second and the fourth arguments to integers using function “atoi”. Refer to the man page of atoi for usage.

Cast the array to type Calc fptr. Invoke it with the second, third and fourth arguments to main as arguments.

Write a Makefile to compile loader.c to generate an executable loader. You will get a segmentation fault when you run loader with the right arguments. This is because, you are trying to execute code that is in the local variable, which is on the stack. Stack is not executable. Now, modify the Makefile and include -Wl,-z,execstack flag. This is a linker flag that tells the linker to mark the stack region as executable. This is for demonstration only. DO NOT use it in production code, if you were ever to write code for a profession. Print the result, and return.

Skeleton Code

1 / l o a d e r . c /

3 / TODO: I n c l u d e a p p r o p r i a t e h e a d e r s /

5 i n t main ( i n t a r g c , c h a r a r g v [ ] ) {

/ TODO: D e c l a r e an a r r a y l a r g e e n o u g h t o h o l d t h e raw b y t e s . Raw b y t e s a r e b e s t s t o r e d i n b y t e a d d r e s s a b l e a r r a y s . P i c k t h e a p p r o p r i a t e t y p e . C a l l

i t r a w b y t e s /

7 / TODO: D e c l a r e a f u n c t i o n p o i n t e r t y p e t h a t m a t c h e s t h e c a l c f u n c t i o n s t y p e . C a l l i t C a l c f p t r /

9 FILE f p ;

u n s i g n e d i n t i ;

11 C a l c f p t r c a l c u l a t o r ;

13 / TODO i f number o f a r g u m e n t s i s n o t 4 ( 5 i n c l u d i n g p r o g r a m name )

p r i n t ( U s a g e %s <f i l e n a m e > <u i n t > <o p e r a t i o n > <u i n t >\n , a r g v [ 0 ] ) and e x i t /

15

/ TODO: Open and r e a d t h e b i n a r y f i l e i n t o r a w b y t e s . Use f o p e n and f r e a d .

/

17

c a l c u l a t o r = ( C a l c f p t r ) r a w b y t e s ;

19 / TODO: P r i n t t h e r e s u l t . R e f e r t o s a m p l e i n p u t and o u t p u t . /

r e t u r n 0 ;

21

Sample input and output

1 $ . / l o a d e r

U s a g e . / l o a d e r <f i l e n a m e > <i n t > <o p e r a t i o n > <i n t >

3 $ . / l o a d e r f u n c . b i n 3 2 + 1 1

3 2 + 1 1 = 4 3

5 $ . / l o a d e r f u n c . b i n 3 2 1 1

3 2 1 1 = 2 1

7 $ . / l o a d e r f u n c . b i n 3 2 1 1

U s a g e . / l o a d e r <f i l e n a m e > <i n t > <o p e r a t i o n > <i n t >

9 $ . / l o a d e r f u n c . b i n 3 2 \ 1 1

3 2 1 1 = 3 5 2

11 $ . / l o a d e r f u n c . b i n 3 2 / 1 1

3 2 / 1 1 = 2

13 $ . / l o a d e r f u n c . b i n 3 2 / 0

F l o a t i n g e x c e p t i o n

Input “32 * 11” is a strange one. The terminal replaces * with all the files in the directory, so you must escape it!

2 Happy Shell (100 points)

The below program implements a simple version of a shell program that runs programs that accept no arguments:

/ h e l l o s h . c /

2 #i n c l u d e < s t d i o . h>

#i n c l u d e < s t d l i b . h>

4 #i n c l u d e < s t r i n g . h>

#i n c l u d e <e r r n o . h>

6

i n t main ( ) {

8 c h a r l i n e [ 1 0 2 4 ] ;

i n t p i d , i ;

10 c h a r a r g s [ ] = {& l i n e , 0 } ;

12 w h i l e ( 1 ) {

p r i n t f ( H e l l o ! ! > ) ;

14 i f ( ! f g e t s ( l i n e , 1 0 2 3 , s t d i n ) ) {

b r e a k ;

16 }

18 i f ( s t r c m p ( l i n e , e x i t \ n ) == 0 ) b r e a

20 f o r ( i = 0 ; i < s t r l e n ( l i n e ) ; i ++) {

i f ( l i n e [ i ] == \ n ) l i n e [ i ] = \ 0 ;

22 }

24 p i d = f o r k ( ) ;

26 i f ( p i d == 0 ) {

/ T h i s i s t h e c h i l d /

28 e x e c v p ( l i n e , a r g s ) ;

f p r i n t f ( s t d e r r , H e l l o ! ! : %s \ n , s t r e r r o r ( e r r n o ) ) ;

30 e x i t ( e r r n o ) ;

} e l s e {

32 / T h i s i s t h e p a r e n t /

w a i t (NULL) ;

34 }

}

36 r e t u r n 0 ;

}

Modify the shell program such that it can execute programs that accept arguments. For example, “ls -l” and “objdump -d -M intel foo > foo.disas” are both valid commands to hello! shell.

In your Makefile, use your modified hellosh.c to build an executable called hellosh

when the command make hellosh is given.

3 Submission

Commit all work to your Github repository, then include the 7-digit SHA hash in your student comment for a MyCourses submission. Do not include any files!