Note that there are some explanatory texts on larger screens.

plurals
  1. PORetrieving vptr(pointer to virtual Table aka VTABLE)from the Objdump utility?
    primarykey
    data
    text
    <p>How can we know the address of the <strong>VTABLEs</strong> (i.e corresponding vptr) using the objdump utility and dissembled code. vptr is generally stored in the first byte of object .(correct/edit this). There is this simple code using the virtual function : </p> <pre><code>class base { public: int x; virtual void func() { //test function } }; class der : public base { void func() { //test function } }; /* * */ int main() { int s = 9; base b; der d ; std::cout&lt;&lt;"the address of Vptr is = "&lt;&lt;(&amp;b+0)&lt;&lt;std::endl; std::cout&lt;&lt;"the value at Vptr is = "&lt;&lt;(int*)*(int*)((&amp;b+0))&lt;&lt;std::endl; return 0; } </code></pre> <p>following is the output of the code : </p> <pre><code>the address of Vptr is = 0x7fff86a78fe0 the value at Vptr is = **0x400c30** </code></pre> <p>Following is the part of main function - <strong>diassembly</strong> of the code : </p> <pre><code>base b; 4009b4: 48 8d 45 d0 lea -0x30(%rbp),%rax 4009b8: 48 89 c7 mov %rax,%rdi 4009bb: e8 f4 00 00 00 callq 400ab4 &lt;_ZN4baseC1Ev&gt; der d ; 4009c0: 48 8d 45 c0 lea -0x40(%rbp),%rax 4009c4: 48 89 c7 mov %rax,%rdi 4009c7: e8 fe 00 00 00 callq 400aca &lt;_ZN3derC1Ev&gt; </code></pre> <p>It shows here that _ZN4baseC1Ev is the address of the base object and _ZN3derC1Ev is the address of the derived object.</p> <p>in the _ZN4baseC1Ev</p> <pre><code>0000000000400ab4 &lt;_ZN4baseC1Ev&gt;: 400ab4: 55 push %rbp 400ab5: 48 89 e5 mov %rsp,%rbp 400ab8: 48 89 7d f8 mov %rdi,-0x8(%rbp) 400abc: 48 8b 45 f8 mov -0x8(%rbp),%rax 400ac0: 48 c7 00 30 0c 40 00 movq $0x400c30,(%rax) 400ac7: c9 leaveq 400ac8: c3 retq 400ac9: 90 nop 0000000000400aca &lt;_ZN3derC1Ev&gt;: } #include&lt;iostream&gt; class base { public: int x; virtual void func() 400a8a: 55 push %rbp 400a8b: 48 89 e5 mov %rsp,%rbp 400a8e: 48 89 7d f8 mov %rdi,-0x8(%rbp) { //test function } 400a92: c9 leaveq 400a93: c3 retq 0000000000400a94 &lt;_ZN3der4funcEv&gt;: }; class der : public base { void func() 400a94: 55 push %rbp 400a95: 48 89 e5 mov %rsp,%rbp 400a98: 48 89 7d f8 mov %rdi,-0x8(%rbp) { //test function } 400a9c: c9 leaveq 400a9d: c3 retq 0000000000400a9e &lt;_ZN4baseC2Ev&gt;: */ #include &lt;stdlib.h&gt; #include&lt;iostream&gt; class base { 400a9e: 55 push %rbp 400a9f: 48 89 e5 mov %rsp,%rbp 400aa2: 48 89 7d f8 mov %rdi,-0x8(%rbp) 400aa6: 48 8b 45 f8 mov -0x8(%rbp),%rax 400aaa: 48 c7 00 50 0c 40 00 movq $0x400c50,(%rax) 400ab1: c9 leaveq 400ab2: c3 retq 400ab3: 90 nop 0000000000400ab4 &lt;_ZN4baseC1Ev&gt;: 400ab4: 55 push %rbp 400ab5: 48 89 e5 mov %rsp,%rbp 400ab8: 48 89 7d f8 mov %rdi,-0x8(%rbp) 400abc: 48 8b 45 f8 mov -0x8(%rbp),%rax 400ac0: 48 c7 00 50 0c 40 00 movq $0x400c50,(%rax) 400ac7: c9 leaveq 400ac8: c3 retq 400ac9: 90 nop 0000000000400aca &lt;_ZN3derC1Ev&gt;: } }; </code></pre> <p>Here is the link to output of <a href="http://www.pastedump.com/paste/2220" rel="nofollow">objdump</a> -S exe file </p> <p><strong>also objdump -t virtualfunctionsize | grep vtable gives this :</strong> </p> <pre><code>0000000000400c40 w O .rodata 0000000000000018 vtable for base 0000000000601e00 g O .dtors 0000000000000000 .hidden __DTOR_END__ 0000000000400b00 g F .text 0000000000000089 __libc_csu_init 0000000000400c20 w O .rodata 0000000000000018 vtable for der </code></pre> <p>I wanted to know - what it is the <strong>VTABLE</strong> address and corresponding virtual function's denoted by it.</p> <ul> <li><p>the address of Vptr is = 0x7fff86a78fe0 , what does this represent - VTABLE location?</p></li> <li><p>the value at Vptr is = <strong>0x400c30</strong> - What does this represent - the first Virtual function of the base class?</p></li> <li><p>How can the subsequent addresses of the virtual functions of the derived classes can be found?</p></li> </ul> <p>Rgds, softy</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload