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] from _start
: 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?
Editor's note: _start
is the process entry point, not a function: no return address (and RSP%16 == 0
not 8
), and doesn't have register args.
main
is a normal function, with arguments passed to it following the standard calling convention; if you link with glibc start files which call your main, you'll find int argc
and char *argv[]
(and char *envp[]
) in the usual registers for the x86-64 System V ABI.
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.