PSkocik PSkocik - 11 months ago 48
Linux Question

Accessing main arguments outside of main on Linux

Is it possible to access the arguments to

outside of
(namely in a shared library constructor) on Linux other than by parsing


You can do this by putting the constructor in the .init_array section. Functions in the .init_array (unlike .init) are called with the same arguments main will be called with: argc, argv and env.

Here's a simple example. I used LD_PRELOAD simply to avoid complicating the example with code which actually links and uses a shared library, but it would work in a more normal scenario as well.

file: printargs.c

#include <stdio.h>

static int printargs(int argc, char** argv, char** env) {
  puts("In printargs:");
  for (int i = 0; i < argc; ++i)
  printf("  Arg %d (%p) '%s'\n", i, (void*)argv[i], argv[i]);
  return 0;

/* Put the function into the init_array */
__attribute__((section(".init_array"))) static void *ctr = &printargs;

Build and use the shared library

(If you use -Wall, you will see a warning, because ctr is unused.)

$ gcc -o -std=c11 -shared -fpic printargs.c
$ LD_PRELOAD=./ /bin/echo Hello, world.
In printargs:
  Arg 0 (0x7ffc7617102f) '/bin/echo'
  Arg 1 (0x7ffc76171039) 'Hello,'
  Arg 2 (0x7ffc76171040) 'world.'
Hello, world.

This solution comes from a suggestion by Mike Frysinger in the libc-help mailing list and there is an even more laconic version of this answer here on SO.