【发布时间】: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