23.03.2013 Views

Quick introduction to reverse engineering for beginners

Quick introduction to reverse engineering for beginners

Quick introduction to reverse engineering for beginners

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

};<br />

int b;<br />

struct outer_struct<br />

{<br />

char a;<br />

int b;<br />

struct inner_struct c;<br />

char d;<br />

int e;<br />

};<br />

void f(struct outer_struct s)<br />

{<br />

printf ("a=%d; b=%d; c.a=%d; c.b=%d; d=%d; e=%d\n",<br />

s.a, s.b, s.c.a, s.c.b, s.d, s.e);<br />

};<br />

... in this case, both inner_struct fields will be placed between a,b and d,e fields of outer_struct.<br />

Let’s compile (MSVC 2010):<br />

_s$ = 8 ; size = 24<br />

_f PROC<br />

push ebp<br />

mov ebp, esp<br />

mov eax, DWORD PTR _s$[ebp+20] ; e<br />

push eax<br />

movsx ecx, BYTE PTR _s$[ebp+16] ; d<br />

push ecx<br />

mov edx, DWORD PTR _s$[ebp+12] ; c.b<br />

push edx<br />

mov eax, DWORD PTR _s$[ebp+8] ; c.a<br />

push eax<br />

mov ecx, DWORD PTR _s$[ebp+4] ; b<br />

push ecx<br />

movsx edx, BYTE PTR _s$[ebp] ;a<br />

push edx<br />

push OFFSET $SG2466<br />

call _printf<br />

add esp, 28 ; 0000001cH<br />

pop ebp<br />

ret 0<br />

_f ENDP<br />

One curious point here is that by looking on<strong>to</strong> this assembler code, we do not even see that another<br />

structure was used inside of it! Thus, we would say, nested structures are finally unfolds in<strong>to</strong> linear or<br />

one-dimensional structure.<br />

Of course, if <strong>to</strong> replace struct inner_struct c; declaration <strong>to</strong> struct inner_struct *c; (thus making<br />

a pointer here) situation will be significally different.<br />

1.15.6 Bit fields in structure<br />

CPUID example<br />

C/C++ language allow <strong>to</strong> define exact number of bits <strong>for</strong> each structure fields. It’s very useful if one need <strong>to</strong><br />

save memory space. For example, one bit is enough <strong>for</strong> variable of bool type. But of course, it’s not rational<br />

71

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!