繁体   English   中英

如何编写从汇编代码调用的 C function

[英]How to write a C function invoked from assembly code

我需要编写一个 C function 将从 linux kernel 中的汇编代码调用。

应注意哪些特殊问题?

我有一些想法,但任何人都可以提供更多细节:

(1) 调用约定

确保组装中的呼叫者和c中的被呼叫者握手良好。 但是我应该使用哪种调用约定? 我如何声明 c function 并在汇编代码中声明它?

(2) 保护寄存器

在调用之前,一些寄存器应该保存在程序集中。 我大致记得是eax、ecx和edx。 但我不必保存它们,除非我需要在调用 C function 之前引用旧值,对吗?

其他问题是什么?

当您的查询标记为linux-kernel时,请查看以下 kernel header: arch/x86/include/asm/calling.h

   3 x86 function call convention, 64-bit:
   4 -------------------------------------
   5  arguments           |  callee-saved      | extra caller-saved | return
   6 [callee-clobbered]   |                    | [callee-clobbered] |
   7 ---------------------------------------------------------------------------
   8 rdi rsi rdx rcx r8-9 | rbx rbp [*] r12-15 | r10-11             | rax, rdx [**]
   9
  10 ( rsp is obviously invariant across normal function calls. (gcc can 'merge'
  11   functions when it sees tail-call optimization possibilities) rflags is
  12   clobbered. Leftover arguments are passed over the stack frame.)
  13
  14 [*]  In the frame-pointers case rbp is fixed to the stack frame.
  15
  16 [**] for struct return values wider than 64 bits the return convention is a
  17      bit more complex: up to 128 bits width we return small structures
  18      straight in rax, rdx. For structures larger than that (3 words or
  19      larger) the caller puts a pointer to an on-stack return struct
  20      [allocated in the caller's stack frame] into the first argument - i.e.
  21      into rdi. All other arguments shift up by one in this case.
  22      Fortunately this case is rare in the kernel.
  23
  24 For 32-bit we have the following conventions - kernel is built with
  25 -mregparm=3 and -freg-struct-return:
  26
  27 x86 function calling convention, 32-bit:
  28 ----------------------------------------
  29  arguments         | callee-saved        | extra caller-saved | return
  30 [callee-clobbered] |                     | [callee-clobbered] |
  31 -------------------------------------------------------------------------
  32 eax edx ecx        | ebx edi esi ebp [*] | <none>             | eax, edx [**]
  33
  34 ( here too esp is obviously invariant across normal function calls. eflags
  35   is clobbered. Leftover arguments are passed over the stack frame. )
  36
  37 [*]  In the frame-pointers case ebp is fixed to the stack frame.
  38
  39 [**] We build with -freg-struct-return, which on 32-bit means similar
  40      semantics as on 64-bit: edx can be used for a second return value
  41      (i.e. covering integer and structure sizes up to 64 bits) - after that
  42      it gets more complex and more expensive: 3-word or larger struct returns
  43      get done in the caller's frame and the pointer to the return struct goes
  44      into regparm0, i.e. eax - the other arguments shift up and the
  45      function's register parameters degenerate to regparm=2 in essence.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM