c++ - Find argc and argv from a library -
how find program's argc , argv shared object? writing library in c loaded via ld_preload. i've been able find stack 2 different ways:
- read
rspvia inline__asm__call. - read
/proc/<pid>/maps, parse entry stack.
i can create pointer, point @ stack segment, iterate through looking data. problem can't figure out efficient way determine bytes argc , pointer pointer argv strings.
i know /proc/<pid>/cmdline contains arguments, each separated 0x00, i'm interested in finding in memory.
in gdb see dword argc followed qword first pointer. 20 bytes before address of argc pointer points main program's code segment. that's not deterministic way identify argc , argv.
i've seen few posts no working code:
this response in second link contains working source code worked fine me (gnu/linux elf-based system), including during ld_preload.
the code short; consists of function:
int foo(int argc, char **argv, char **env) { // argc, argv (and env, if desired) } and pointer function in .init_array section:
__attribute__((section(".init_array"))) static void *foo_constructor = &foo; putting shared library , ld_preloading shared library triggered call foo when tried it, , called argc , argv later passed main (and value of environ).
Comments
Post a Comment