【问题标题】:How to find VPTR in C++ assembly code?如何在 C++ 汇编代码中找到 VPTR?
【发布时间】:2018-02-06 20:54:37
【问题描述】:
class Base {
 public:
  Base() {}
  virtual void Get() { }
};

class Derivered : public Base {
 public:
  virtual void Get() { }
};

int main() {
  Base* base = new Derivered();
  base->Get();
  return 0;
}

我使用gcc 5.4.0编译代码,使用objdump -S a.out反汇编二进制文件。我想找到Base的vptr,但只显示一个未知地址0x80487d4。最大地址数是0x80487b7,我看不懂。 命令列表:g++ test.cpp -O0; objdump -S a.out

080486fe <_ZN4BaseC1Ev>:
 80486fe:   55                      push   %ebp
 80486ff:   89 e5                   mov    %esp,%ebp
 8048701:   ba d4 87 04 08          mov    $0x80487d4,%edx
 8048706:   8b 45 08                mov    0x8(%ebp),%eax
 8048709:   89 10                   mov    %edx,(%eax)

【问题讨论】:

  • 确保优化已关闭。
  • 你读过这个article 吗?
  • 如果你开启优化,编译器很容易Devirtualize函数调用,内联它,甚至消除它,就像在这个上下文中一样,
  • 是的,优化已关闭。
  • 显示objdump -h a.out的输出

标签: c++ linux gcc code-generation vptr


【解决方案1】:
080486fe <_ZN4BaseC1Ev>:
  80486fe:   55                      push   %ebp
  80486ff:   89 e5                   mov    %esp,%ebp
  8048701:   ba d4 87 04 08          mov    $0x80487d4,%edx
  8048706:   8b 45 08                mov    0x8(%ebp),%eax
  8048709:   89 10                   mov    %edx,(%eax)

是……

push %ebp             ;- save frame pointer
mov %esp, %ebp        ;- mov esp-> ebp -ebp is frame pointer
mov $0x80487d4, %edx  ; load vptr address into edx
mov 0x8(%ebp), %eax   ; ld eax with address of this
mov %edx,(%eax)       ; store vptr in this byte 0

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-18
    • 2018-05-31
    • 2021-07-18
    • 2011-08-27
    • 2015-04-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多