How do I find a program's argc
and argv
from a shared object? I am writing a library in C that will be loaded via LD_PRELOAD
. I've been able to find the stack two different ways:
- Read
rsp
via inline__asm__
call. - Read
/proc/<pid>/maps
and parse the entry for stack.
I can then create a pointer, point it at the stack segment, then iterate through looking for data. The problem is I can't figure out an efficient way to determine what bytes are argc
and the pointer to the pointer to the argv
strings.
I know that /proc/<pid>/cmdline
also contains the arguments, each separated by 0x00
, but I'm interested in finding everything in memory.
In gdb I see a DWORD
for argc
followed by a QWORD
which is the first pointer. 20 bytes before the address of argc
is a pointer that points back into the main program's code segment. But that's not a deterministic way to identify argc
and argv
.
I've seen a few posts but no working code:
argc
andargv
would be accessed? It is probably impossible duringLD_PRELOAD
phase. – Catrinacatrionaargv
. I'm not sure what happens to the stack in this case. – Ursala