Problem 1#

The following C code declares a structure:

struct comp2310 {
	int *p;
	struct {
		int x;
		int y;
	} s;
	struct comp2310 *next;

The declaration embeds one structure within another, just as arrays can be part of structures, and we can have arrays within arrays (e.g., two-dimensional arrays).

The following procedure operates on the above structure. We have intentionally omitted some expressions.

void init(struct comp2310 *cp) {
	cp->s.x = ;
	cp->p = ;
	cp->next = ;


What are the offsets (in bytes) of the following fields?



How many total bytes does the structure require?


The compiler generates the following assembly code for init:

# void init(struct comp2310 *cp)
# cp in %rdi
1 init:
2 	movl 12(%rdi), %eax
3 	movl %eax, 8(%rdi)
4 	leaq 8(%rdi), %rax
5 	movq %rax, (%rdi)
6 	movq %rdi, 16(%rdi)
7 	ret

Fill in the missing expressions in the C code for init based on the basis of this information.

Problem 2#

The following code shows the declaration of a structure of type COMP2310 and the prototype for a function wing_it:

struct COMP2310 {
	long v;
	struct COMP2310 *p;
long  wing_it (struct COMP2310 *ptr);

When the code for wing_it is compiled, GCC generates the following assembly code:

# long wing_it(struct COMP2310 *ptr)
# ptr in %rdi
1 wing_it:
2   movl $0, %eax
3   jmp .L2
4 .L3:
5   addq (%rdi),  %rax
6   movq 8(%rdi), %rdi
7 .L2:
8   testq %rdi, %rdi
9   jne .L3
10  ret


Use your reverse engineering skills to write C code for wing_it.


Describe the data structure that this structure implements and the operation performed by wing_it.

Problem 3#

For each of the following structure declarations, determine the offset of each field, the total size of the structure, and its alignment requirements for x86-64:

A. struct P1 { int i; char c; int j; char d; };
B. struct P2 { int i; char c; char d; long j; };
C. struct P3 { short w[3]; char c[3] };
D. struct P4 { short w[5]; char *c[3] };
E. struct P5 { struct P3 a[2]; struct P2 t };
bars search times arrow-up