I am using linux using gdb version 6.8-debian. I was curious how the main function in the c-program is executed and plays in different places, I found out that the __libc_start_main function is responsible for this. The arguments for __libc_start_main are, among other things: the main address (as we know from c, the path is always set to argv [0]), the next argc, which must be in the ESI register, and the next argv, which must be in ECX.
To play around, I made the following simple cmdargs.c program, which simply returns the first command line argument specified at the beginning:
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char *argv[])
{
printf("%s: %s\n", "argv[1]", *++argv);
return EXIT_SUCCESS;
}
Now I start debugging cmdargs and setting a breakpoint on main and __libc_start_main (information from the initial gdb has been deleted):
gdb cmdargs
(gdb) b main
Breakpoint 1 at 0x80483d2
(gdb) b __libc_start_main
Breakpoint 2 at 0xb7f3f5a8
(gdb) r qwerty
2 __libc_start_main argc argv [0]
(gdb) p $esi
(gdb) x/s *($ecx)
, , "qwerty"? , argc argv (?). - , ?
Breakpoint 1, 0x080483d2 in main ()
(gdb) stepi
0x080483d5 in main ()
(gdb) p argc
No symbol "argc" in current context.
(gdb) p argv
No symbol "argv" in current context.
(gdb)