切换 C++ 的调用堆栈功能
这是我关于切换 C 调用堆栈的上一个问题。但是,C++ 使用不同的调用约定 (thiscall),并且可能需要一些不同的 asm 代码。有人可以解释这些差异并指出或提供一些切换 C++ 调用堆栈的代码片段(最好是在 GCC 内联汇编中)吗?
谢谢, 詹姆斯
Here's my previous question about switching C callstacks. However, C++ uses a different calling convention (thiscall) and may require some different asm code. Can someone explain the differences and point to or supply some code snippets that switch C++ callstacks (preferably in GCC inline asm)?
Thanks,
James
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
上一个问题中给出的代码应该可以正常工作。
thiscall 调用约定仅在谁是负责将参数从堆栈中弹出。在 thiscall 调用约定下,被调用者弹出参数(此外,
this
指针在ecx
中传递);根据 C 调用约定,调用者弹出参数。这不会影响上下文切换。但是,如果您要自己进行上下文切换,请注意,除了切换堆栈之外,您还需要保存和恢复寄存器(可能在堆栈上)。
请注意,顺便说一句,C++ 并不总是使用 thiscall —— 它仅用于具有固定数量参数的方法(除此之外,它是 Microsoftism...g++ 不使用它)。
The code given in the previous question should work fine.
The thiscall calling convention differs only in who is responsible for popping the arguments off the stack. Under the thiscall calling convention, the callee pops the arguments (and additionally, the
this
pointer is passed inecx
); under the C calling convention, the caller pops the arguments. This does not affect context switches.However, if you're going to do context switches yourself, note that you need to save and restore the registers as well (probably on the stack) in addition to switching stacks.
Note, by the way, that C++ doesn't always use thiscall -- it's only used for methods with a fixed number of arguments (and apart from that, it's a Microsoftism... g++ doesn't use it).
请注意,C++ 的 ABI 未明确定义。
这个想法是编译器制造商能够根据情况使用最佳调用约定,从而使 C++ 更快。
这样做的缺点是每个编译器都有自己的调用约定,因此来自不同编译器的代码不兼容(即使来自同一编译器的不同版本(甚至不同优化标志)的代码也可能不兼容)。
Note the ABI for C++ is not explicitly defined.
The idea was that compiler manufactures are able to use the optimal calling convention for the situation and thus make C++ faster.
The down side of this is that each compiler has its own calling convention thus code from different compilers are not compatable (even code form different versions (or even different optimization flags) of the same compiler can be incompatable).