Can I get argv[0] through _start instead of main in the C function? I don't use any lib.
void _start(void) __attribute__((noreturn));
void _start(void)
{
//how can get argv[0] here . I don't use main as my first entry.
}
Can I get argv[0] through _start instead of main in the C function? I don't use any lib.
void _start(void) __attribute__((noreturn));
void _start(void)
{
//how can get argv[0] here . I don't use main as my first entry.
}
The ELF entry point contract is not a C function in the vast majority of psABIs (processor-specific ABI, the arch-dependent part of ELF). At entry, the stack pointer register points to an array of system-word-sized slots consisting of:
argc, argv[0], ..., argv[argc-1], 0, environ[0], ..., environ[N], 0,
auxv[0].a_type, auxv[0].a_value, ..., 0
You need at least a minimal asm stub to convert this into form that's readable by C. The simplest way to do this is to copy the stack pointer register into the first-argument register, align the stack pointer down according the function call ABI requirements, and call your C function taking a single pointer argument.
You can see the crt_arch.h
files (x86_64 version here) in musl libc for an example of how this is done. (You can probably ignore the part about _DYNAMIC
which is arranging for self-relocation when the entry point is used in the dynamic linker startup or static PIE executables.)