0

meanwhile, I am learning more and more how to reverse engineer. Ive figured out tons of stuff already, but I came to the point, where I just need some little explanation whats going on in the constructor of a specific class.

I know, that this specific class (Class A) inherits 3 other Classes (lets say: class B, class C, class D). Class A calls the constructors of B,C,D. Everything up to here is clear for me.

But:

Class D has a method "addListener" which points to an attribute (this + 0x34).

(this + 0x34) is assigned in the constructor to a address..

int A::A(void *someObject) {
    B::B();
    C::C();

    *this = 0x1f18e8;
    *(this + 0x28) = 0x1f190c;         // whats going here?
    *(this + 0x34) = 0x1f193c;         // and here?

    // Ive seen, that those addressees are inside of the vtable of Class A
    // 
    //     __ZTV12A:        // vtable for A
    // ...
    // 001f18e8         db  0xc6
    // 001f193c         db  0xb0
    // 001f190c         db  0xba
    // ...

    D::D();

    // Some other attributes, but these are clear for me (just 
    // have to name them right, by figuring out where these attributes are used):

    *(this + 0x38) = someObject;
    *(this + 0x3c) = 0x0;
    *(this + 0x50) = 0x0;
    *(this + 0x54) = 0x0;
    *(this + 0x40) = 0xffffffff;
    *(this + 0x44) = 0xffffffff;
    *(this + 0x48) = 0xffffffff;
    *(this + 0x4c) = 0x0;

    D::addListener(this + 0x34);
}

Am I right with my conclusion, that D::addListener() adds the class it self to the listener?

In fact I just want to figure out what kind of object is added to the "listener":

    D::addListener(this + 0x34);

I hope my question is clear enough :)

2
  • Is is the whole constructor ? If not, can you post it ?
    – w s
    May 9 '16 at 5:40
  • This may be an artifact of multiple inheritance. I found an article which may be useful for understanding this phpcompiler.org/articles/virtualinheritance.html
    – w s
    May 9 '16 at 5:43
2

Your question is a bit unclear as you first say "Class C has a method "addListerner" which points to an attribute (this + 0x34).", then D::addListener(this + 0x34);. Typo?

Also, you should read about (typical) implementations of multiple inheritance. Assume your classes B, C, D have methods b, c, d respectively. A will inherit all of them. Now, if A does not override these methods, and anything calls them, they have to be delegated to the correct superclass - the original methods. But these original methods will expect a class layout that corresponds to the original classes. Which means, A needs to "embed" all 3 classes into itself.

Which means A will be laid out like this:

+-----+-------------------+
|  00 | vtable of A       |
+-----+-------------------+
|  04 | member 1 of A     |
|  08 | member 2 of A     |
|     | ...               |
+-----+-------------------+
|  28 | vtable of B       |
+-----+-------------------+
|  2c | member 1 of B     |
|  30 | member 2 of B     |
|     | ...               |
+-----+-------------------+
|  34 | vtable of C       |
+-----+-------------------+
|  38 | member 1 of C     |
|  3c | member 2 of C     |
|     | ...               |
+-----+-------------------+
|  ?? | vtable of D       |
+-----+-------------------+
|  ?? | member 1 of D     |
|  ?? | member 2 of D     |
|     | ...               |
+-----+-------------------+

So, yes, your conclusion is correct: D::addListener() adds the class itself to the listener. But because D::addListener() expects a D, not an A, the thing that's passed isn't the complete A, it's just the part of A that makes D. To make this look exactly like a D, it needs its own vtable that looks like a D vtable.

But of course, parts of these vtables can be shared. A needs all methods in its vtable, so the vtable pointers of the partial classes B, C and D can point to the appropriate part of the As vtable, they don't need their complete own copies.

As to your "what's going on here" questions - these initialize the vtables of the partial classes. (In your assembly listing, you should treat those addresses as arrays of words, not bytes). What i wonder is why there's only 3 of them, there should be 4 for A, B, C and D. Something seems to be confused or omitted here, just like you said "Class C has" first, then used D::addListener.

7
  • Ive edited my post, of course it should be D::addListener() instead of C::addListener()
    – Vertices
    May 9 '16 at 5:54
  • Something's still wrong as there should be 4 vtables, not 3. Unless one of your classes is really a struct, not a class, that has no methods, not even a constructor. So maybe the offsets, or class names, in my example do not match yours, but the principle still applies. May 9 '16 at 5:59
  • What makes me so confused is, I see that addListener accepts an argument of type *Listener... But I can´t see any of the classes inherits *Listener... I am asking where it comes from. D::addListener(*Listener); I am expecting that A inherits *Listener.... But Ive checked the *Listener class, which don´t have a constructor, only a destructor.
    – Vertices
    May 9 '16 at 6:16
  • I see, B, C, D has constructors. *(arg_0 + 0x34) = 0x1f193c; seems to be an object of type *Listener, which don´t have a constructor. Makes the things more confusing for me.
    – Vertices
    May 9 '16 at 6:18
  • A doesn't have to (explicitly) inherit from Listener if A inherits from D, and D inherits from Listener. And of course, D may have a constructor even if Listener doesn't. And this would explain why the D part of A is the argument, not A itself. May 9 '16 at 6:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.