This description is valid for Linux 32 bit: When a Linux program begins, all pointers to command-line arguments are stored on the stack. The number of arguments is stored at 0(%ebp), the name of the program is stored at 4(%ebp), and the arguments are stored from 8(%ebp).
I need the same information for 64 bit.
Edit: I have working code sample which shows how to use argc, argv[0] and argv[1]: http://cubbi.com/fibonacci/asm.html
.globl _start _start: popq %rcx # this is argc, must be 2 for one argument cmpq $2,%rcx jne usage_exit addq $8,%rsp # skip argv[0] popq %rsi # get argv[1] call ... ... }
It looks like parameters are on the stack. Since this code is not clear, I ask this question. My guess that I can keep rsp in rbp, and then access these parameters using 0(%rbp), 8(%rbp), 16(%rbp) etc. It this correct?
I do believe what you need to do is check out the x86-64 ABI. Specifically, I think you need to look at section 3.2.3 Parameter Passing.
It looks like section 3.4 Process Initialization, and specifically figure 3.9, in the already mentioned System V AMD64 ABI describes precisely what you want to know.
Despite the accepted answer being more than sufficient, I would like to give an explicit answer, as there are some other answers which might confuse.
Most important (for more information see examples below): in x86-64 the command line arguments are passed via stack:
It is different from the function parameter passing in x86-64, which uses
%rdi
,%rsi
and so on.One more thing: one should not deduce the behavior from reverse engineering of the C
main
-function. C runtime provides the entry point_start
, wraps the command line arguments and callsmain
as a common function. To see it, let's consider the following example.No C runtime/GCC with -nostdlib
Let's check this simple x86-64 assembler program, which do nothing but returns 42:
We build it with:
or with
run in gdb with
and stop at the breakpoint at
_start
. Let's check the registers:Nothing there. What about the stack?
So the first element on the stack is
4
- the expectedargc
. The next 4 values look a lot like pointers. Let's look at the second pointer:As expected it is the first command line argument.
So there is experimental evidence, that the command line arguments are passed via stack in x86-64. However only by reading the ABI (as the accepted answer suggested) we can be sure, that this is really the case.
With C runtime
We have to change the program slightly, renaming
_start
intomain
, because the entry point_start
is provided by the C runtime.We build it with (C runtime is used per default):
run in gdb with
and stop at the breakpoint at
main
. What is at the stack?It does not look familiar. And registers?
We can see that
rdi
contains theargc
value. But if we now inspect the pointer inrsi
strange things happen:But wait, the second argument of the
main
function in C is notchar *
, butchar **
also:And now we found our arguments, which are passed via registers as it would be for a normal function in x86-64.
Conclusion: As we can see, the is a difference concerning passing of command line arguments between code using C runtime and code which doesn't.