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:

  1. read rsp via inline __asm__ call.
  2. 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

Popular posts from this blog

SVG stroke-linecap doesn't work for circles in Firefox? -

routes - Laravel 4 Wildcard Routing to Different Controllers -

cross browser - XSLT namespace-alias Not Working in Firefox or Chrome -