Search code examples
c++cdlopen

access dlopen flags


I'm inside a shared object (code) loaded with dlopen. I want to know the flags given to the loading call. I do not have access to the loader (code) - e.g. it may be a script interpreter - but I have to create subsequent dlopen calls with the same flags.

How can I do this?


Solution

  • I think this is NOT possible without the aid of debuggers.

    From the latest glibc code , here is the source code of dlopen

    void *
    dlopen (const char *file, int mode)
    {
      return __dlopen (file, mode, RETURN_ADDRESS (0));
    }
    

    And __dlopen is in turn defined as

    void *
    __dlopen (const char *file, int mode DL_CALLER_DECL)
    {
    # ifdef SHARED
      if (__builtin_expect (_dlfcn_hook != NULL, 0))
        return _dlfcn_hook->dlopen (file, mode, DL_CALLER);
    # endif
    
      struct dlopen_args args;
      args.file = file;
      args.mode = mode;
      args.caller = DL_CALLER;
    
    # ifdef SHARED
      return _dlerror_run (dlopen_doit, &args) ? NULL : args.new;
    # else
      if (_dlerror_run (dlopen_doit, &args))
        return NULL;
    
      __libc_register_dl_open_hook ((struct link_map *) args.new);
      __libc_register_dlfcn_hook ((struct link_map *) args.new);
    
      return args.new;
    # endif
    }
    

    The flags you are looking for, RTLD_LAZY, RTLD_NOW, RTLD_GLOBAL and RTLD_LOCAL are ORed and stored in mode variable. As you can see there is no route through which it is passed back or anything like that.

    EDIT: It seems there indeed is a way of achieving what you want as shown by the other answer. If you can un-accept my answer, I can delete it so as to help future visitors