Reverse Engineering for Beginners

(avery) #1

CHAPTER 21. STRUCTURES CHAPTER 21. STRUCTURES


This is an example where the branch delay slots can confuse us. For example, there is the instruction “addiu $a1, 1900” at
line 35 which adds 1900 to the year number. It’s executed before the corresponding JALR at line 34, do not forget about it.


21.3.4 Structure as a set of values


In order to illustrate that the structure is just variables laying side-by-side in one place, let’s rework our example while
looking at thetmstructure definition again: listing.21.8.


#include <stdio.h>
#include <time.h>


void main()
{
int tm_sec, tm_min, tm_hour, tm_mday, tm_mon, tm_year, tm_wday, tm_yday, tm_isdst;
time_t unix_time;


unix_time=time(NULL);

localtime_r (&unix_time, &tm_sec);

printf ("Year: %d\n", tm_year+1900);
printf ("Month: %d\n", tm_mon);
printf ("Day: %d\n", tm_mday);
printf ("Hour: %d\n", tm_hour);
printf ("Minutes: %d\n", tm_min);
printf ("Seconds: %d\n", tm_sec);
};


N.B. The pointer to thetm_secfield is passed intolocaltime_r, i.e., to the first element of the “structure”.


The compiler warns us:


Listing 21.12: GCC 4.7.3

GCC_tm2.c: In function 'main':
GCC_tm2.c:11:5: warning: passing argument 2 of 'localtime_r' from incompatible pointer type [⤦
Çenabled by default]
In file included from GCC_tm2.c:2:0:
/usr/include/time.h:59:12: note: expected 'struct tm ' but argument is of type 'int '


But nevertheless, it generates this:


Listing 21.13: GCC 4.7.3

main proc near


var_30 = dword ptr -30h
var_2C = dword ptr -2Ch
unix_time = dword ptr -1Ch
tm_sec = dword ptr -18h
tm_min = dword ptr -14h
tm_hour = dword ptr -10h
tm_mday = dword ptr -0Ch
tm_mon = dword ptr -8
tm_year = dword ptr -4


push ebp
mov ebp, esp
and esp, 0FFFFFFF0h
sub esp, 30h
call __main
mov [esp+30h+var_30], 0 ; arg 0
call time
mov [esp+30h+unix_time], eax
lea eax, [esp+30h+tm_sec]
mov [esp+30h+var_2C], eax
lea eax, [esp+30h+unix_time]
mov [esp+30h+var_30], eax
call localtime_r
mov eax, [esp+30h+tm_year]
Free download pdf