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:
rsp
via inline __asm__
call./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:
This response in your second link contains working source code which worked fine for me (Gnu/Linux elf-based system), including during LD_PRELOAD
.
The code is very short; it consists of a function:
int foo(int argc, char **argv, char **env) {
// Do something with argc, argv (and env, if desired)
}
and a pointer to that function in the .init_array
section:
__attribute__((section(".init_array"))) static void *foo_constructor = &foo;
Putting that into a shared library and then LD_PRELOADing the shared library certainly triggered the call to foo
when I tried it, and it was clearly called with the argc
and argv
which would later be passed to main
(and also the value of environ
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With