且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在寄存器中返回结构 - GCC 中的 ARM ABI

更新时间:2022-04-26 23:09:16

根据请求作为答案发布:

Posting as an answer by request:

如果您必须生成一个可以与编译器不支持的 ABI 一起使用的二进制文件,那么您就会遇到一些麻烦.在 C 中您无能为力.在这种情况下,您需要依靠汇编语言编程并调用必要的调用.有两种可能:

If you have to generate a binary that will work with an ABI your compiler doesn't support, you're in for some trouble. There's nothing you can do in C. In this case, you'll need to fall back on assembly language programming and thunk the necessary calls. There are two possibilities:

  1. 从您的二进制文件调用另一个二进制文件的 ABI.
  2. 从其他二进制文件调用您的二进制文件的 ABI.

这两个问题的解决方法类似.要从您的代码中调出,您需要在汇编中制作 shim 函数,围绕调用约定进行调整以匹配外部 ABI,然后从那里调用外部函数.与您的 C 代码的不同之处在于,现在要进行外部调用,您调用内部汇编例程,它会执行任何需要从外部调用的操作,然后将返回值以您的 C 代码能够理解的格式放回,并返回.

Both of these problems are solved similarly. To call out from your code, you'll need to make shim functions in assembly that swizzle around the calling convention to match the external ABI, and then call the external functions from there. The difference to your C code is that now to make external calls, you call your internal assembly routine, and it does whatever it needs to to call out externally, then puts the return value back in a format your C code will understand, and returns.

为了支持从外部二进制文件代码的调用,你做同样的事情,但相反.二进制文件的入口点将是一些小的汇编例程,它们将外部 ABI 混合成您的 C 代码可以理解的格式,调用您的内部函数,然后将返回值放回外部代码可以理解的格式,然后返回.

To support calls from the external binary into your code, you do the same thing, but in reverse. The entry points to your binary will be little assembly routines that swizzle the external ABI into a format your C code can understand, call your internal function, then put the return values back into a format the external code understands, and return.

恐怕有时没有好的解决方案.

Sometimes there's just no good solution, I'm afraid.