Windows 函数名称后跟@数字符号? [英] Windows function names followed by @ number symbol?

查看:26
本文介绍了Windows 函数名称后跟@数字符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 NASM 的汇编中为 Windows 编程,我在代码中发现了这一点:

I'm programming for Windows in assembly in NASM, and i found this in the code:

extern _ExitProcess@4
  ;Rest of code...
  ; ...
call  _ExitProcess@4

winapi库函数的声明和调用中的@4是什么意思?

What does the @4 mean in the declaration and call of a winapi library function?

推荐答案

winapi 使用 __stdcall 调用约定.调用者将所有参数从右到左压入堆栈,被调用者再次弹出它们以清理堆栈,通常使用 RET n 指令.

The winapi uses the __stdcall calling convention. The caller pushes all the arguments on the stack from right to left, the callee pops them again to cleanup the stack, typically with a RET n instruction.

它是 __cdecl 调用约定的对立面,这是 C 和 C++ 代码中的常见默认设置,调用者清理堆栈,通常在 CALL 之后使用 ADD ESP,n 指令.__stdcall 的优点是它生成更紧凑的代码,在被调用的函数中只需要一条清理指令,而不是每次调用该函数时执行很多条清理指令.但是有一个很大的缺点:它很危险.

It is the antipode of the __cdecl calling convention, the common default in C and C++ code where the caller cleans up the stack, typically with an ADD ESP,n instruction after the CALL. The advantage of __stdcall is that it is generates more compact code, just one cleanup instruction in the called function instead of many for each call to the function. But one big disadvantage: it is dangerous.

危险潜伏在调用函数的代码中,该函数的编译带有过时的函数声明.例如,通过添加参数更改函数时的典型情况.这结束得非常糟糕,除了尝试使用不可用参数的函数之外,新函数从堆栈中弹出太多参数.这使堆栈不平衡,不仅导致被调用者失败,也会导致调用者失败.极难诊断.

The danger lurks in the code that calls the function having been compiled with an out-dated declaration of the function. Typical when the function was changed by adding an argument for example. This ends very poorly, beyond the function trying to use an argument that is not available, the new function pops too many arguments off the stack. This imbalances the stack, causing not just the callee to fail but the caller as well. Extremely hard to diagnose.

所以他们做了一些事情,他们修饰函数的名称.首先使用前导 _underscore,就像对 __cdecl 函数所做的那样.并附加@nn的值就是函数末尾RET指令的操作数.或者换句话说,堆栈上的参数占用的字节数.

So they did something about that, they decorated the name of the function. First with a leading _underscore, as is done for __cdecl functions. And appended @n, the value of n is the operand of the RET instruction at the end of the function. Or in other words, the number of bytes taken by the arguments on the stack.

这在不匹配时提供链接器诊断,将 foo(int) 函数更改为 foo(int, int) 例如生成名称 _foo@8.尚未重新编译的调用代码将查找 _foo@4 函数.链接器失败,它找不到那个符号.避免了灾难.

This provides a linker diagnostic when there's a mismatch, a change in a foo(int) function to foo(int, int) for example generates the name _foo@8. The calling code not yet recompiled will look for a _foo@4 function. The linker fails, it cannot find that symbol. Disaster avoided.

这篇关于Windows 函数名称后跟@数字符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆