GCC:命令行参数组装code引用不同 [英] gcc: command line args referenced differently in assembly code
问题描述
我看惯了通过引用的命令行参数(约定(A)):
I am used to seeing command line arguments referenced by (convention (A)):
pushl %ebp
movl %esp, %ebp
movl (%ebp), %eax # argc
movl 4(%ebp), %ebx # pointer to argv[0] string
movl 8($ebp), %ecx # pointer to argv[1] string
有时候,我见过的清单开始以8偏移量,这不是(主)的问题。我也注意到一个程序是这样的翻译和参考,我感到困惑,要获得的argv [1]
(约定(B)):
movl 0xc(%ebp), %eax # pointer to a pointer to argv[0] (argc is at offset 8)
addl $0x4, %eax # argv[1] is a pointer at offset 4 from the pointer to argv[0]
movl (%eax), %eax # load where it points to, which is the argv[1] string
(偏移 16(%EBP)
我看到一个指向一个环境变量)
(at offset 16(%ebp)
I see a pointer to an environmental variable)
(1)是否有任何原因不同的约定?结果
(2)有一个编译器选项强制GCC使用什么我认为是标准的约定(A)以上?结果
(3)是否有一个理由GCC使用约定(B)?结果
(4)为什么额外的8偏移?
(1) Is there any reason for this different convention?
(2) Is there a compiler option to force gcc to use what I believe to be the standard convention (A) above?
(3) Is there a reason gcc uses convention (B)?
(4) Why the additional offset of 8?
系统信息:结果
- Ubuntu的12.04结果
- GCC 4.6.3结果
- 与FNO堆栈保护器编译
System info:
- Ubuntu 12.04
- gcc 4.6.3
- compiled with fno-stack-protector
推荐答案
如果你正在处理已链接到C运行一个程序,那么 ARGC
和的argv
参数在 EBP + 8 $传递(假设86)与
ARGC
C $ C>和的argv
在 EBP + 12
。这是因为C运行时执行它自己的初始化并传递参数的main()
使用正常的C ABI。
If you're dealing with a program that has been linked to the C runtime, then the argc
and argv
parameters are passed (assuming x86) with argc
at ebp+8
and argv
at ebp+12
. that's because the C runtime performs it's own initialization and passes the arguments to main()
using the normal C ABI.
这是你说你看惯了(与 ARGC
在堆栈的顶部,然后按调用约定的argv [0]
.. ARGV [ARGC]
)是由启动一个新的程序在Linux系统调用设置堆栈的状态。
The calling convention that you say you're used to seeing (with argc
at the top of the stack, followed by argv[0]
..argv[argc]
) is the state of the stack as set up by the Linux system call that starts a new program.
请注意,您的装配导向code例如:
Note that your assembly oriented code example:
pushl %ebp
movl %esp, %ebp
movl (%ebp), %eax # argc
movl 4(%ebp), %ebx # pointer to argv[0] string
movl 8($ebp), %ecx # pointer to argv[1] string
看起来由4对每个由于初始 pushl
指令的最后三行处于关机状态。
looks to be off by 4 for each of the last three lines because of the initial pushl
instruction.
这篇关于GCC:命令行参数组装code引用不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!