為什么VS2013將函數調用編譯成兩個指令而不是一個?

[英]Why does VS2013 compile a function-call into two instructions instead of one?


Here is a simple program:

這是一個簡單的程序:

void func()
{
    printf("hello");
}

int main()
{
    printf("%p",func);
    func();
    return 0;
}

Stepping over the line printf("%p",func), I get 00F811AE printed on the console.

踩到行printf(“%p”,func),我在控制台上打印00F811AE。

Disassembling the line func(), gives me call _func (0F811AEh) - so far so good.

拆解行func(),給我調用_func(0F811AEh) - 到目前為止一直很好。

But disassembling the contents of func, the first instruction appears at address 00F813C0.

但是反匯編func的內容,第一條指令出現在地址00F813C0。

So I "went to see" what's on address 00F811AE, and there I found jmp func (0F813C0h).

所以我“去看看”00F811AE的地址是什么,在那里我找到了jmp func(0F813C0h)。

To summarize this, it appears that the function-call is compiled as two instructions:

總結一下,似乎函數調用被編譯為兩個指令:

call _func (0F811AEh)
jmp   func (0F813C0h)

Why does the VS2013 compiler use two instructions instead of just one?

為什么VS2013編譯器使用兩條指令而不是一條指令?

It appears that a single jmp would do the the job. I am asking even this because I have a feeling that the other compilers do it in a similar manner (depending on the underlying HW architecture of course).

似乎單個jmp可以勝任這項工作。我甚至都在問這個因為我感覺其他編譯器以類似的方式做這件事(當然取決於底層的HW架構)。

Thanks

1 个解决方案

#1


6  

Learn about "thunking": http://en.wikipedia.org/wiki/Thunk

了解“thunking”:http://en.wikipedia.org/wiki/Thunk

One benefit with "thunking" in your example is that the rest of your code will always call func, but any function performing the same role could be injected into the call at address 0x00F811AE.

在您的示例中,“thunking”的一個好處是代碼的其余部分將始終調用func,但執行相同角色的任何函數都可以注入到地址0x00F811AE的調用中。

Try making func a static one and find out if anything changes.

嘗試使func成為靜態函數,並找出是否有任何變化。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2014/08/14/7210d91cba7ca62f1c95855e74382f60.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com