Starting from:

$30

Assignment 12 and 13 Binary Loader


Assignment 12 and 13 (200 points)

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 encouraged 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.
2
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 cl u d e a p p r o p ri a t e h e a d e r s ∗/
5 i n t main ( i n t argc , ch a r ∗ argv [ ] ) {
/∗ TODO: D e cl a r e an a r r a y l a r g e enough t o h old the 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 byte−a d d r e s s a bl e a r r a y s . Pick the a p p r o p ri a t e type . C all
i t ” r aw b y t e s ”∗/
7 /∗ TODO: D e cl a r e a f u n c ti o n p oi n t e r type t h a t matches the c a l c f u n c ti o n ’ s
type . C all i t ” C a l c f p t r ” ∗/
9 FILE ∗ f p ;
un si gned 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 arguments i s not 4 ( 5 i n c l u d i n g program name )
p r i n t ( ”Usage %s <fil e n am e <uin t <o p e r a ti o n <uin t \n” , argv [ 0 ] ) and
e x i t ∗/
15
/∗ TODO: Open and re ad the bi n a r y f i l e i n t o r aw b y t e s . Use f open 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 aw b y t e s ;
19 /∗ TODO: P ri n t the r e s u l t . R e f e r t o sample i npu t and output . ∗/
r e t u r n 0 ;
21 }
Sample input and output
1 $ . / l o a d e r
Usage . / l o a d e r <fil e n am e <i n t <o p e r a ti o n <i n t
3
3 $ . / l o a d e r func . bin 32 + 11
32 + 11 = 43
5 $ . / l o a d e r func . bin 32 − 11
32 − 11 = 21
7 $ . / l o a d e r func . bin 32 ∗ 11
Usage . / l o a d e r <fil e n am e <i n t <o p e r a ti o n <i n t
9 $ . / l o a d e r func . bin 32 \∗ 11
32 ∗ 11 = 352
11 $ . / l o a d e r func . bin 32 / 11
32 / 11 = 2
13 $ . / l o a d e r func . bin 32 / 0
Fl o a ti n g e x c e p ti 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 cl u d e <s t d i o . h
#i n cl u d e <s t d l i b . h
4 #i n cl u d e <s t r i n g . h
#i n cl u d e <e r r n o . h
6
i n t main ( ) {
8 ch a r l i n e [ 1 0 2 4 ] ;
i n t pid , i ;
10 ch a r ∗ a r g s [ ] = {&l i n e , 0 } ;
12 w hil e ( 1 ) {
p r i n t f ( ” H ell o !! ” ) ;
14 i f ( ! f g e t s ( l i n e , 1 0 2 3 , s t di n ) ) {
break ;
16 }
18 i f ( strcmp ( l i n e , ” e x i t \n” ) == 0 ) break ;
4
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 pid = f o r k ( ) ;
26 i f ( pid == 0 ) {
/∗ This i s the c h i l d ∗/
28 execvp ( l i n e , a r g s ) ;
f p r i n t f ( s t d e r r , ” H ell 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 /∗ This i s the p a ren t ∗/
w ai 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!
5

More products