【问题标题】:Related to C++ and Assembly, what is ebp+8?与C++和Assembly相关,什么是ebp+8?
【发布时间】:2017-05-18 00:34:20
【问题描述】:

我有以下 C++ 代码:

#include <tuple>
std::tuple<int, bool> foo()
{
    return std::make_tuple(128, true);
}
int main()
{
    auto result = foo();
}

以下是foo()函数的反汇编版本:

push    ebp
mov     ebp, esp
sub     esp, 24
mov     BYTE PTR [ebp-13], 1  // second argument
mov     DWORD PTR [ebp-12], 128 // first argument
mov     eax, DWORD PTR [ebp+8] // what is this? why we need this here?
sub     esp, 4
lea     edx, [ebp-13]   
push    edx                   // second
lea     edx, [ebp-12]
push    edx                   // first
push    eax                  // same as "ebp+8", what is this?
call    std::tuple<std::__decay_and_strip<int>::__type, std::__decay_and_strip<bool>::__type> std::make_tuple<int, bool>(int&&, bool&&)
add     esp, 12
mov     eax, DWORD PTR [ebp+8]
leave
ret     4

据我所知ebp+X 用于访问函数参数,但foo 没有这样的东西,那么编译器为什么要使用它呢? 好像是std::make_tuple()的第一个参数。

编辑:

我没有使用优化,我只是想学习 RE。

组装中的主要部分:

lea     eax, [ebp-16]  // loaction of local variable
sub     esp, 12
push    eax           // as hidden argument for foo
call    foo()
add     esp, 12

【问题讨论】:

  • 什么优化级别、编译器版本、编译器选项和其他设置?
  • @NickC 没有任何优化。 gcc -m32 c.cpp
  • 链接后面的信息是相关的,但使用 AT&T 语法(与问题中的 Intel 语法相反)。为清楚起见:intel 语法中的 [base + index * scale + disp] 等同于 AT&T 语法中的 disp(base, index, scale)。问题中base为ebx,disp为8,所以[ebx + 8]指的是在ebx寄存器值后面8个字节的内存位置的值。
  • 学习如何对未优化代码进行逆向工程毫无意义。专注于优化代码。它的噪音小得多,实际上更容易阅读。

标签: c++ assembly x86 g++ reverse-engineering


【解决方案1】:

调用约定指定通过作为参数传递的隐藏指针返回非平凡对象。这就是你所看到的。从技术上讲,您的代码是这样实现的:

std::tuple<int, bool>* foo(std::tuple<int, bool>* result)
{
    *result = std::make_tuple(128, true);
    return result;
}
int main()
{
    std::tuple<int, bool> result;
    foo(&result);
}

【讨论】:

  • 如果您查看了它是如何被调用的,那也会有所帮助;)另外,请查阅相关的调用约定文档。
  • push eax;调用 foo();
  • 是的,但您必须检查 eax 是如何设置的,您可能会看到类似 lea eax, [ebp-x] 的内容,它应该指出它是一个局部变量的地址main,当然只能是result本身。
猜你喜欢
  • 2013-02-07
  • 2015-03-04
  • 1970-01-01
  • 2019-09-06
  • 2012-06-07
  • 2012-05-28
  • 1970-01-01
  • 2023-04-07
  • 2015-01-25
相关资源
最近更新 更多