Reverse Engineering for Beginners

(avery) #1

CHAPTER 51. C++ CHAPTER 51. C++


Listing 51.16: Optimizing MSVC 2008 /Ob0

?get_density@solid_object@@QAEHXZ PROC ; solid_object::get_density, COMDAT
; _this$ = ecx
mov eax, DWORD PTR [ecx]
ret 0
?get_density@solid_object@@QAEHXZ ENDP ; solid_object::get_density


But the code of thesolid_box::get_weight()method is much more interesting:


Listing 51.17: Optimizing MSVC 2008 /Ob0

?get_weight@solid_box@@QAEHXZ PROC ; solid_box::get_weight, COMDAT
; _this$ = ecx
push esi
mov esi, ecx
push edi
lea ecx, DWORD PTR [esi+12]
call ?get_density@solid_object@@QAEHXZ ; solid_object::get_density
mov ecx, esi
mov edi, eax
call ?get_volume@box@@QAEHXZ ; box::get_volume
imul eax, edi
pop edi
pop esi
ret 0
?get_weight@solid_box@@QAEHXZ ENDP ; solid_box::get_weight


get_weight()just calls two methods, but forget_volume()it just passes pointer tothis, and forget_density()
it passes a pointer tothisincremented by 12 (or0xC) bytes, and there, in thesolid_boxclass memory layout, the fields
of thesolid_objectclass start.


Thus, thesolid_object::get_density()method will believe like it is dealing with the usualsolid_objectclass,
and thebox::get_volume()method will work with its three fields, believing this is just the usual object of classbox.


Thus, we can say, an object of a class, that inherits from several other classes, is representing in memory as aunitedclass,
that contains all inherited fields. And each inherited method is called with a pointer to the corresponding structure’s part.


51.1.5 Virtual methods


Yet another simple example:


#include <stdio.h>


class object
{
public:
int color;
object() { };
object (int color) { this->color=color; };
virtual void dump()
{
printf ("color=%d\n", color);
};
};


class box : public object
{
private:
int width, height, depth;
public:
box(int color, int width, int height, int depth)
{
this->color=color;
this->width=width;
this->height=height;
this->depth=depth;
};
void dump()
{

Free download pdf